1 /*
2 * linux/mm/swap_state.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 * Swap reorganised 29.12.95, Stephen Tweedie
6 *
7 * Rewritten to use page cache, (C) 1998 Stephen Tweedie
8 */
9 #include <linux/module.h>
10 #include <linux/mm.h>
11 #include <linux/kernel_stat.h>
12 #include <linux/swap.h>
13 #include <linux/swapops.h>
14 #include <linux/init.h>
15 #include <linux/pagemap.h>
16 #include <linux/buffer_head.h>
17 #include <linux/backing-dev.h>
18 #include <linux/pagevec.h>
19 #include <linux/migrate.h>
20
21 #include <asm/pgtable.h>
22
23 /*
24 * swapper_space is a fiction, retained to simplify the path through
25 * vmscan's shrink_page_list, to make sync_page look nicer, and to allow
26 * future use of radix_tree tags in the swap cache.
27 */
28 static const struct address_space_operations swap_aops = {
29 .writepage = swap_writepage,
30 .sync_page = block_sync_page,
31 .set_page_dirty = __set_page_dirty_nobuffers,
32 .migratepage = migrate_page,
33 };
34
35 static struct backing_dev_info swap_backing_dev_info = {
36 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
37 .unplug_io_fn = swap_unplug_io_fn,
38 };
39
40 struct address_space swapper_space = {
41 .page_tree = RADIX_TREE_INIT(GFP_ATOMIC|__GFP_NOWARN),
42 .a_ops = &swap_aops,
43 .i_mmap_nonlinear = LIST_HEAD_INIT(swapper_space.i_mmap_nonlinear),
44 .backing_dev_info = &swap_backing_dev_info,
45 };
46
47 #define INC_CACHE_INFO(x) do { swap_cache_info.x++; } while (0)
48
49 static struct {
50 unsigned long add_total;
51 unsigned long del_total;
52 unsigned long find_success;
53 unsigned long find_total;
54 } swap_cache_info;
55
56 void show_swap_cache_info(void)
57 {
58 printk("Swap cache: add %lu, delete %lu, find %lu/%lu\n",
59 swap_cache_info.add_total, swap_cache_info.del_total,
60 swap_cache_info.find_success, swap_cache_info.find_total);
61 printk("Free swap = %lukB\n", nr_swap_pages << (PAGE_SHIFT - 10));
62 printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
63 }
64
65 /*
66 * add_to_swap_cache resembles add_to_page_cache on swapper_space,
67 * but sets SwapCache flag and private instead of mapping and index.
68 */
69 int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp_mask)
70 {
71 int error;
72
73 BUG_ON(!PageLocked(page));
74 BUG_ON(PageSwapCache(page));
75 BUG_ON(PagePrivate(page));
76 error = radix_tree_preload(gfp_mask);
77 if (!error) {
78 DEFINE_RADIX_TREE_CONTEXT(ctx, &swapper_space.page_tree);
79
80 lock_page_ref_irq(page);
81 radix_tree_lock(&ctx);
82 error = radix_tree_insert(ctx.tree, entry.val, page);
83 radix_tree_unlock(&ctx);
84 if (!error) {
85 page_cache_get(page);
86 SetPageSwapCache(page);
87 set_page_private(page, entry.val);
88 mapping_nrpages_inc(&swapper_space);
89 __inc_zone_page_state(page, NR_FILE_PAGES);
90 INC_CACHE_INFO(add_total);
91 }
92 unlock_page_ref_irq(page);
93 radix_tree_preload_end();
94 }
95 return error;
96 }
97
98 /*
99 * This must be called only on pages that have
100 * been verified to be in the swap cache.
101 */
102 void __delete_from_swap_cache(struct page *page)
103 {
104 DEFINE_RADIX_TREE_CONTEXT(ctx, &swapper_space.page_tree);
105
106 BUG_ON(!PageLocked(page));
107 BUG_ON(!PageSwapCache(page));
108 BUG_ON(PageWriteback(page));
109 BUG_ON(PagePrivate(page));
110
111 radix_tree_lock(&ctx);
112 radix_tree_delete(ctx.tree, page_private(page));
113 radix_tree_unlock(&ctx);
114 set_page_private(page, 0);
115 ClearPageSwapCache(page);
116 mapping_nrpages_dec(&swapper_space);
117 __dec_zone_page_state(page, NR_FILE_PAGES);
118 INC_CACHE_INFO(del_total);
119 }
120
121 /**
122 * add_to_swap - allocate swap space for a page
123 * @page: page we want to move to swap
124 * @gfp_mask: memory allocation flags
125 *
126 * Allocate swap space for the page and add the page to the
127 * swap cache. Caller needs to hold the page lock.
128 */
129 int add_to_swap(struct page * page, gfp_t gfp_mask)
130 {
131 swp_entry_t entry;
132 int err;
133
134 BUG_ON(!PageLocked(page));
135 BUG_ON(!PageUptodate(page));
136
137 for (;;) {
138 entry = get_swap_page();
139 if (!entry.val)
140 return 0;
141
142 /*
143 * Radix-tree node allocations from PF_MEMALLOC contexts could
144 * completely exhaust the page allocator. __GFP_NOMEMALLOC
145 * stops emergency reserves from being allocated.
146 *
147 * TODO: this could cause a theoretical memory reclaim
148 * deadlock in the swap out path.
149 */
150 /*
151 * Add it to the swap cache and mark it dirty
152 */
153 err = add_to_swap_cache(page, entry,
154 gfp_mask|__GFP_NOMEMALLOC|__GFP_NOWARN);
155
156 switch (err) {
157 case 0: /* Success */
158 SetPageDirty(page);
159 return 1;
160 case -EEXIST:
161 /* Raced with "speculative" read_swap_cache_async */
162 swap_free(entry);
163 continue;
164 default:
165 /* -ENOMEM radix-tree allocation failure */
166 swap_free(entry);
167 return 0;
168 }
169 }
170 }
171
172 /*
173 * This must be called only on pages that have
174 * been verified to be in the swap cache and locked.
175 * It will never put the page into the free list,
176 * the caller has a reference on the page.
177 */
178 void delete_from_swap_cache(struct page *page)
179 {
180 swp_entry_t entry;
181
182 entry.val = page_private(page);
183
184 lock_page_ref_irq(page);
185 __delete_from_swap_cache(page);
186 unlock_page_ref_irq(page);
187
188 swap_free(entry);
189 page_cache_release(page);
190 }
191
192 /*
193 * If we are the only user, then try to free up the swap cache.
194 *
195 * Its ok to check for PageSwapCache without the page lock
196 * here because we are going to recheck again inside
197 * exclusive_swap_page() _with_ the lock.
198 * - Marcelo
199 */
200 static inline void free_swap_cache(struct page *page)
201 {
202 if (PageSwapCache(page) && !TestSetPageLocked(page)) {
203 remove_exclusive_swap_page(page);
204 unlock_page(page);
205 }
206 }
207
208 /*
209 * Perform a free_page(), also freeing any swap cache associated with
210 * this page if it is the last user of the page.
211 */
212 void free_page_and_swap_cache(struct page *page)
213 {
214 free_swap_cache(page);
215 page_cache_release(page);
216 }
217
218 /*
219 * Passed an array of pages, drop them all from swapcache and then release
220 * them. They are removed from the LRU and freed if this is their last use.
221 */
222 void free_pages_and_swap_cache(struct page **pages, int nr)
223 {
224 struct page **pagep = pages;
225
226 lru_add_drain();
227 while (nr) {
228 int todo = min(nr, PAGEVEC_SIZE);
229 int i;
230
231 for (i = 0; i < todo; i++)
232 free_swap_cache(pagep[i]);
233 release_pages(pagep, todo, 0);
234 pagep += todo;
235 nr -= todo;
236 }
237 }
238
239 /*
240 * Lookup a swap entry in the swap cache. A found page will be returned
241 * unlocked and with its refcount incremented - we rely on the kernel
242 * lock getting page table operations atomic even if we drop the page
243 * lock before returning.
244 */
245 struct page * lookup_swap_cache(swp_entry_t entry)
246 {
247 struct page *page;
248
249 page = find_get_page(&swapper_space, entry.val);
250
251 if (page)
252 INC_CACHE_INFO(find_success);
253
254 INC_CACHE_INFO(find_total);
255 return page;
256 }
257
258 /*
259 * Locate a page of swap in physical memory, reserving swap cache space
260 * and reading the disk if it is not already cached.
261 * A failure return means that either the page allocation failed or that
262 * the swap entry is no longer in use.
263 */
264 struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
265 struct vm_area_struct *vma, unsigned long addr)
266 {
267 struct page *found_page, *new_page = NULL;
268 int err;
269
270 do {
271 /*
272 * First check the swap cache. Since this is normally
273 * called after lookup_swap_cache() failed, re-calling
274 * that would confuse statistics.
275 */
276 found_page = find_get_page(&swapper_space, entry.val);
277 if (found_page)
278 break;
279
280 /*
281 * Get a new page to read into from swap.
282 */
283 if (!new_page) {
284 new_page = alloc_page_vma(gfp_mask, vma, addr);
285 if (!new_page)
286 break; /* Out of memory */
287 }
288
289 /*
290 * Swap entry may have been freed since our caller observed it.
291 */
292 if (!swap_duplicate(entry))
293 break;
294
295 /*
296 * Associate the page with swap entry in the swap cache.
297 * May fail (-EEXIST) if there is already a page associated
298 * with this entry in the swap cache: added by a racing
299 * read_swap_cache_async, or add_to_swap or shmem_writepage
300 * re-using the just freed swap entry for an existing page.
301 * May fail (-ENOMEM) if radix-tree node allocation failed.
302 */
303 SetPageLocked(new_page);
304 err = add_to_swap_cache(new_page, entry, gfp_mask & GFP_KERNEL);
305 if (!err) {
306 /*
307 * Initiate read into locked page and return.
308 */
309 lru_cache_add_active(new_page);
310 swap_readpage(NULL, new_page);
311 return new_page;
312 }
313 ClearPageLocked(new_page);
314 swap_free(entry);
315 } while (err != -ENOMEM);
316
317 if (new_page)
318 page_cache_release(new_page);
319 return found_page;
320 }
321
322 /**
323 * swapin_readahead - swap in pages in hope we need them soon
324 * @entry: swap entry of this memory
325 * @gfp_mask: memory allocation flags
326 * @vma: user vma this address belongs to
327 * @addr: target address for mempolicy
328 *
329 * Returns the struct page for entry and addr, after queueing swapin.
330 *
331 * Primitive swap readahead code. We simply read an aligned block of
332 * (1 << page_cluster) entries in the swap area. This method is chosen
333 * because it doesn't cost us any seek time. We also make sure to queue
334 * the 'original' request together with the readahead ones...
335 *
336 * This has been extended to use the NUMA policies from the mm triggering
337 * the readahead.
338 *
339 * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
340 */
341 struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
342 struct vm_area_struct *vma, unsigned long addr)
343 {
344 int nr_pages;
345 struct page *page;
346 unsigned long offset;
347 unsigned long end_offset;
348
349 /*
350 * Get starting offset for readaround, and number of pages to read.
351 * Adjust starting address by readbehind (for NUMA interleave case)?
352 * No, it's very unlikely that swap layout would follow vma layout,
353 * more likely that neighbouring swap pages came from the same node:
354 * so use the same "addr" to choose the same node for each swap read.
355 */
356 nr_pages = valid_swaphandles(entry, &offset);
357 for (end_offset = offset + nr_pages; offset < end_offset; offset++) {
358 /* Ok, do the async read-ahead now */
359 page = read_swap_cache_async(swp_entry(swp_type(entry), offset),
360 gfp_mask, vma, addr);
361 if (!page)
362 break;
363 page_cache_release(page);
364 }
365 lru_add_drain(); /* Push any new pages onto the LRU now */
366 return read_swap_cache_async(entry, gfp_mask, vma, addr);
367 }
368
|
This page was automatically generated by the
LXR engine.
|