2 * linux/mm/swap_state.c
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 * Swap reorganised 29.12.95, Stephen Tweedie
7 * Rewritten to use page cache, (C) 1998 Stephen Tweedie
11 #include <linux/kernel_stat.h>
12 #include <linux/swap.h>
13 #include <linux/swapctl.h>
14 #include <linux/init.h>
15 #include <linux/pagemap.h>
17 #include <asm/pgtable.h>
20 * Keep a reserved false inode which we will use to mark pages in the
21 * page cache are acting as swap cache instead of file cache.
23 * We only need a unique pointer to satisfy the page cache, but we'll
24 * reserve an entire zeroed inode structure for the purpose just to
25 * ensure that any mistaken dereferences of this structure cause a
29 static struct inode_operations swapper_inode_operations
= {
30 NULL
, /* default file operations */
41 NULL
, /* follow_link */
45 block_flushpage
, /* flushpage */
47 NULL
, /* permission */
52 struct inode swapper_inode
= { i_op
: &swapper_inode_operations
};
54 #ifdef SWAP_CACHE_INFO
55 unsigned long swap_cache_add_total
= 0;
56 unsigned long swap_cache_del_total
= 0;
57 unsigned long swap_cache_find_total
= 0;
58 unsigned long swap_cache_find_success
= 0;
60 void show_swap_cache_info(void)
62 printk("Swap cache: add %ld, delete %ld, find %ld/%ld\n",
65 swap_cache_find_success
, swap_cache_find_total
);
69 void add_to_swap_cache(struct page
*page
, unsigned long entry
)
71 #ifdef SWAP_CACHE_INFO
72 swap_cache_add_total
++;
75 printk("DebugVM: add_to_swap_cache(%08lx count %d, entry %08lx)\n",
76 page_address(page
), page_count(page
), entry
);
78 if (PageTestandSetSwapCache(page
)) {
79 printk(KERN_ERR
"swap_cache: replacing non-empty entry %08lx "
81 page
->offset
, page_address(page
));
84 printk(KERN_ERR
"swap_cache: replacing page-cached entry "
85 "on page %08lx\n", page_address(page
));
87 add_to_page_cache(page
, &swapper_inode
, entry
);
91 * Verify that a swap entry is valid and increment its swap map count.
93 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
94 * "permanent", but will be reclaimed by the next swapoff.
96 int swap_duplicate(unsigned long entry
)
98 struct swap_info_struct
* p
;
99 unsigned long offset
, type
;
104 type
= SWP_TYPE(entry
);
105 if (type
& SHM_SWP_TYPE
)
107 if (type
>= nr_swapfiles
)
109 p
= type
+ swap_info
;
110 offset
= SWP_OFFSET(entry
);
111 if (offset
>= p
->max
)
113 if (!p
->swap_map
[offset
])
116 * Entry is valid, so increment the map count.
118 if (p
->swap_map
[offset
] < SWAP_MAP_MAX
)
119 p
->swap_map
[offset
]++;
121 static int overflow
= 0;
124 "swap_duplicate: entry %08lx map count=%d\n",
125 entry
, p
->swap_map
[offset
]);
126 p
->swap_map
[offset
] = SWAP_MAP_MAX
;
130 printk("DebugVM: swap_duplicate(entry %08lx, count now %d)\n",
131 entry
, p
->swap_map
[offset
]);
138 "swap_duplicate: entry %08lx, nonexistent swap file\n", entry
);
142 "swap_duplicate: entry %08lx, offset exceeds max\n", entry
);
146 "swap_duplicate at %8p: entry %08lx, unused page\n",
147 __builtin_return_address(0), entry
);
151 int swap_count(unsigned long entry
)
153 struct swap_info_struct
* p
;
154 unsigned long offset
, type
;
159 type
= SWP_TYPE(entry
);
160 if (type
& SHM_SWP_TYPE
)
162 if (type
>= nr_swapfiles
)
164 p
= type
+ swap_info
;
165 offset
= SWP_OFFSET(entry
);
166 if (offset
>= p
->max
)
168 if (!p
->swap_map
[offset
])
170 retval
= p
->swap_map
[offset
];
172 printk("DebugVM: swap_count(entry %08lx, count %d)\n",
179 printk(KERN_ERR
"swap_count: null entry!\n");
183 "swap_count: entry %08lx, nonexistent swap file!\n", entry
);
187 "swap_count: entry %08lx, offset exceeds max!\n", entry
);
191 "swap_count at %8p: entry %08lx, unused page!\n",
192 __builtin_return_address(0), entry
);
196 static inline void remove_from_swap_cache(struct page
*page
)
198 struct inode
*inode
= page
->inode
;
201 printk ("VM: Removing swap cache page with zero inode hash "
202 "on page %08lx\n", page_address(page
));
205 if (inode
!= &swapper_inode
) {
206 printk ("VM: Removing swap cache page with wrong inode hash "
207 "on page %08lx\n", page_address(page
));
209 if (!PageSwapCache(page
))
213 printk("DebugVM: remove_from_swap_cache(%08lx count %d)\n",
214 page_address(page
), page_count(page
));
216 PageClearSwapCache(page
);
217 if (inode
->i_op
->flushpage
)
218 inode
->i_op
->flushpage(inode
, page
, 0);
219 remove_inode_page(page
);
223 * This must be called only on pages that have
224 * been verified to be in the swap cache.
226 void __delete_from_swap_cache(struct page
*page
)
228 long entry
= page
->offset
;
230 #ifdef SWAP_CACHE_INFO
231 swap_cache_del_total
++;
234 printk("DebugVM: delete_from_swap_cache(%08lx count %d, "
236 page_address(page
), page_count(page
), entry
);
238 remove_from_swap_cache (page
);
243 * This must be called only on pages that have
244 * been verified to be in the swap cache.
246 void delete_from_swap_cache(struct page
*page
)
250 __delete_from_swap_cache(page
);
253 page_cache_release(page
);
257 * Perform a free_page(), also freeing any swap cache associated with
258 * this page if it is the last user of the page.
261 void free_page_and_swap_cache(unsigned long addr
)
263 struct page
*page
= mem_map
+ MAP_NR(addr
);
266 * If we are the only user, then free up the swap cache.
269 if (PageSwapCache(page
) && !is_page_shared(page
)) {
270 long entry
= page
->offset
;
271 remove_from_swap_cache(page
);
273 page_cache_release(page
);
282 * Lookup a swap entry in the swap cache. A found page will be returned
283 * unlocked and with its refcount incremented - we rely on the kernel
284 * lock getting page table operations atomic even if we drop the page
285 * lock before returning.
288 struct page
* lookup_swap_cache(unsigned long entry
)
292 #ifdef SWAP_CACHE_INFO
293 swap_cache_find_total
++;
296 found
= find_lock_page(&swapper_inode
, entry
);
299 if (found
->inode
!= &swapper_inode
|| !PageSwapCache(found
))
301 #ifdef SWAP_CACHE_INFO
302 swap_cache_find_success
++;
309 printk (KERN_ERR
"VM: Found a non-swapper swap page!\n");
316 * Locate a page of swap in physical memory, reserving swap cache space
317 * and reading the disk if it is not already cached. If wait==0, we are
318 * only doing readahead, so don't worry if the page is already locked.
320 * A failure return means that either the page allocation failed or that
321 * the swap entry is no longer in use.
324 struct page
* read_swap_cache_async(unsigned long entry
, int wait
)
326 struct page
*found_page
= 0, *new_page
;
327 unsigned long new_page_addr
;
330 printk("DebugVM: read_swap_cache_async entry %08lx%s\n",
331 entry
, wait
? ", wait" : "");
334 * Make sure the swap entry is still in use.
336 if (!swap_duplicate(entry
)) /* Account for the swap cache */
339 * Look for the page in the swap cache.
341 found_page
= lookup_swap_cache(entry
);
345 new_page_addr
= __get_free_page(GFP_USER
);
347 goto out_free_swap
; /* Out of memory */
348 new_page
= mem_map
+ MAP_NR(new_page_addr
);
351 * Check the swap cache again, in case we stalled above.
353 found_page
= lookup_swap_cache(entry
);
357 * Add it to the swap cache and read its contents.
359 add_to_swap_cache(new_page
, entry
);
360 rw_swap_page(READ
, new_page
, wait
);
362 printk("DebugVM: read_swap_cache_async created "
363 "entry %08lx at %p\n",
364 entry
, (char *) page_address(new_page
));
369 __free_page(new_page
);