This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / mm / swap_state.c
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
10 #include <linux/mm.h>
11 #include <linux/kernel_stat.h>
12 #include <linux/swap.h>
13 #include <linux/init.h>
14 #include <linux/pagemap.h>
15 #include <linux/buffer_head.h>
16 #include <linux/backing-dev.h>
17
18 #include <asm/pgtable.h>
19
20 /*
21  * swapper_space is a fiction, retained to simplify the path through
22  * vmscan's shrink_list.  Only those fields initialized below are used.
23  */
24 static struct address_space_operations swap_aops = {
25         .writepage      = swap_writepage,
26         .sync_page      = block_sync_page,
27         .set_page_dirty = __set_page_dirty_nobuffers,
28 };
29
30 static struct backing_dev_info swap_backing_dev_info = {
31         .memory_backed  = 1,    /* Does not contribute to dirty memory */
32         .unplug_io_fn   = swap_unplug_io_fn,
33 };
34
35 struct address_space swapper_space = {
36         .page_tree      = RADIX_TREE_INIT(GFP_ATOMIC),
37         .tree_lock      = SPIN_LOCK_UNLOCKED,
38         .a_ops          = &swap_aops,
39         .backing_dev_info = &swap_backing_dev_info,
40 };
41
42 #define INC_CACHE_INFO(x)       do { swap_cache_info.x++; } while (0)
43
44 static struct {
45         unsigned long add_total;
46         unsigned long del_total;
47         unsigned long find_success;
48         unsigned long find_total;
49         unsigned long noent_race;
50         unsigned long exist_race;
51 } swap_cache_info;
52
53 void show_swap_cache_info(void)
54 {
55         printk("Swap cache: add %lu, delete %lu, find %lu/%lu, race %lu+%lu\n",
56                 swap_cache_info.add_total, swap_cache_info.del_total,
57                 swap_cache_info.find_success, swap_cache_info.find_total,
58                 swap_cache_info.noent_race, swap_cache_info.exist_race);
59 }
60
61 /*
62  * __add_to_swap_cache resembles add_to_page_cache on swapper_space,
63  * but sets SwapCache flag and private instead of mapping and index.
64  */
65 static int __add_to_swap_cache(struct page *page,
66                 swp_entry_t entry, int gfp_mask)
67 {
68         int error;
69
70         BUG_ON(PageSwapCache(page));
71         BUG_ON(PagePrivate(page));
72         error = radix_tree_preload(gfp_mask);
73         if (!error) {
74                 spin_lock_irq(&swapper_space.tree_lock);
75                 error = radix_tree_insert(&swapper_space.page_tree,
76                                                 entry.val, page);
77                 if (!error) {
78                         page_cache_get(page);
79                         SetPageLocked(page);
80                         SetPageSwapCache(page);
81                         page->private = entry.val;
82                         total_swapcache_pages++;
83                         pagecache_acct(1);
84                 }
85                 spin_unlock_irq(&swapper_space.tree_lock);
86                 radix_tree_preload_end();
87         }
88         return error;
89 }
90
91 static int add_to_swap_cache(struct page *page, swp_entry_t entry)
92 {
93         int error;
94
95         if (!swap_duplicate(entry)) {
96                 INC_CACHE_INFO(noent_race);
97                 return -ENOENT;
98         }
99         error = __add_to_swap_cache(page, entry, GFP_KERNEL);
100         /*
101          * Anon pages are already on the LRU, we don't run lru_cache_add here.
102          */
103         if (error) {
104                 swap_free(entry);
105                 if (error == -EEXIST)
106                         INC_CACHE_INFO(exist_race);
107                 return error;
108         }
109         INC_CACHE_INFO(add_total);
110         return 0;
111 }
112
113 /*
114  * This must be called only on pages that have
115  * been verified to be in the swap cache.
116  */
117 void __delete_from_swap_cache(struct page *page)
118 {
119         BUG_ON(!PageLocked(page));
120         BUG_ON(!PageSwapCache(page));
121         BUG_ON(PageWriteback(page));
122
123         radix_tree_delete(&swapper_space.page_tree, page->private);
124         page->private = 0;
125         ClearPageSwapCache(page);
126         total_swapcache_pages--;
127         pagecache_acct(-1);
128         INC_CACHE_INFO(del_total);
129 }
130
131 /**
132  * add_to_swap - allocate swap space for a page
133  * @page: page we want to move to swap
134  *
135  * Allocate swap space for the page and add the page to the
136  * swap cache.  Caller needs to hold the page lock. 
137  */
138 int add_to_swap(struct page * page)
139 {
140         swp_entry_t entry;
141         int pf_flags;
142         int err;
143
144         if (!PageLocked(page))
145                 BUG();
146
147         for (;;) {
148                 entry = get_swap_page();
149                 if (!entry.val)
150                         return 0;
151
152                 /* Radix-tree node allocations are performing
153                  * GFP_ATOMIC allocations under PF_MEMALLOC.  
154                  * They can completely exhaust the page allocator.  
155                  *
156                  * So PF_MEMALLOC is dropped here.  This causes the slab 
157                  * allocations to fail earlier, so radix-tree nodes will 
158                  * then be allocated from the mempool reserves.
159                  *
160                  * We're still using __GFP_HIGH for radix-tree node
161                  * allocations, so some of the emergency pools are available,
162                  * just not all of them.
163                  */
164
165                 pf_flags = current->flags;
166                 current->flags &= ~PF_MEMALLOC;
167
168                 /*
169                  * Add it to the swap cache and mark it dirty
170                  */
171                 err = __add_to_swap_cache(page, entry, GFP_ATOMIC);
172
173                 if (pf_flags & PF_MEMALLOC)
174                         current->flags |= PF_MEMALLOC;
175
176                 switch (err) {
177                 case 0:                         /* Success */
178                         SetPageUptodate(page);
179                         SetPageDirty(page);
180                         INC_CACHE_INFO(add_total);
181                         return 1;
182                 case -EEXIST:
183                         /* Raced with "speculative" read_swap_cache_async */
184                         INC_CACHE_INFO(exist_race);
185                         swap_free(entry);
186                         continue;
187                 default:
188                         /* -ENOMEM radix-tree allocation failure */
189                         swap_free(entry);
190                         return 0;
191                 }
192         }
193 }
194
195 /*
196  * This must be called only on pages that have
197  * been verified to be in the swap cache and locked.
198  * It will never put the page into the free list,
199  * the caller has a reference on the page.
200  */
201 void delete_from_swap_cache(struct page *page)
202 {
203         swp_entry_t entry;
204
205         BUG_ON(!PageSwapCache(page));
206         BUG_ON(!PageLocked(page));
207         BUG_ON(PageWriteback(page));
208         BUG_ON(PagePrivate(page));
209   
210         entry.val = page->private;
211
212         spin_lock_irq(&swapper_space.tree_lock);
213         __delete_from_swap_cache(page);
214         spin_unlock_irq(&swapper_space.tree_lock);
215
216         swap_free(entry);
217         page_cache_release(page);
218 }
219
220 /*
221  * Strange swizzling function only for use by shmem_writepage
222  */
223 int move_to_swap_cache(struct page *page, swp_entry_t entry)
224 {
225         int err = __add_to_swap_cache(page, entry, GFP_ATOMIC);
226         if (!err) {
227                 remove_from_page_cache(page);
228                 page_cache_release(page);       /* pagecache ref */
229                 if (!swap_duplicate(entry))
230                         BUG();
231                 SetPageDirty(page);
232                 INC_CACHE_INFO(add_total);
233         } else if (err == -EEXIST)
234                 INC_CACHE_INFO(exist_race);
235         return err;
236 }
237
238 /*
239  * Strange swizzling function for shmem_getpage (and shmem_unuse)
240  */
241 int move_from_swap_cache(struct page *page, unsigned long index,
242                 struct address_space *mapping)
243 {
244         int err = add_to_page_cache(page, mapping, index, GFP_ATOMIC);
245         if (!err) {
246                 delete_from_swap_cache(page);
247                 /* shift page from clean_pages to dirty_pages list */
248                 ClearPageDirty(page);
249                 set_page_dirty(page);
250         }
251         return err;
252 }
253
254 /* 
255  * If we are the only user, then try to free up the swap cache. 
256  * 
257  * Its ok to check for PageSwapCache without the page lock
258  * here because we are going to recheck again inside 
259  * exclusive_swap_page() _with_ the lock. 
260  *                                      - Marcelo
261  */
262 static inline void free_swap_cache(struct page *page)
263 {
264         if (PageSwapCache(page) && !TestSetPageLocked(page)) {
265                 remove_exclusive_swap_page(page);
266                 unlock_page(page);
267         }
268 }
269
270 /* 
271  * Perform a free_page(), also freeing any swap cache associated with
272  * this page if it is the last user of the page. Can not do a lock_page,
273  * as we are holding the page_table_lock spinlock.
274  */
275 void free_page_and_swap_cache(struct page *page)
276 {
277         free_swap_cache(page);
278         page_cache_release(page);
279 }
280
281 /*
282  * Passed an array of pages, drop them all from swapcache and then release
283  * them.  They are removed from the LRU and freed if this is their last use.
284  */
285 void free_pages_and_swap_cache(struct page **pages, int nr)
286 {
287         int chunk = 16;
288         struct page **pagep = pages;
289
290         lru_add_drain();
291         while (nr) {
292                 int todo = min(chunk, nr);
293                 int i;
294
295                 for (i = 0; i < todo; i++)
296                         free_swap_cache(pagep[i]);
297                 release_pages(pagep, todo, 0);
298                 pagep += todo;
299                 nr -= todo;
300         }
301 }
302
303 /*
304  * Lookup a swap entry in the swap cache. A found page will be returned
305  * unlocked and with its refcount incremented - we rely on the kernel
306  * lock getting page table operations atomic even if we drop the page
307  * lock before returning.
308  */
309 struct page * lookup_swap_cache(swp_entry_t entry)
310 {
311         struct page *page;
312
313         spin_lock_irq(&swapper_space.tree_lock);
314         page = radix_tree_lookup(&swapper_space.page_tree, entry.val);
315         if (page) {
316                 page_cache_get(page);
317                 INC_CACHE_INFO(find_success);
318         }
319         spin_unlock_irq(&swapper_space.tree_lock);
320         INC_CACHE_INFO(find_total);
321         return page;
322 }
323
324 /* 
325  * Locate a page of swap in physical memory, reserving swap cache space
326  * and reading the disk if it is not already cached.
327  * A failure return means that either the page allocation failed or that
328  * the swap entry is no longer in use.
329  */
330 struct page *read_swap_cache_async(swp_entry_t entry,
331                         struct vm_area_struct *vma, unsigned long addr)
332 {
333         struct page *found_page, *new_page = NULL;
334         int err;
335
336         do {
337                 /*
338                  * First check the swap cache.  Since this is normally
339                  * called after lookup_swap_cache() failed, re-calling
340                  * that would confuse statistics.
341                  */
342                 spin_lock_irq(&swapper_space.tree_lock);
343                 found_page = radix_tree_lookup(&swapper_space.page_tree,
344                                                 entry.val);
345                 if (found_page)
346                         page_cache_get(found_page);
347                 spin_unlock_irq(&swapper_space.tree_lock);
348                 if (found_page)
349                         break;
350
351                 /*
352                  * Get a new page to read into from swap.
353                  */
354                 if (!new_page) {
355                         new_page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
356                         if (!new_page)
357                                 break;          /* Out of memory */
358                 }
359
360                 /*
361                  * Associate the page with swap entry in the swap cache.
362                  * May fail (-ENOENT) if swap entry has been freed since
363                  * our caller observed it.  May fail (-EEXIST) if there
364                  * is already a page associated with this entry in the
365                  * swap cache: added by a racing read_swap_cache_async,
366                  * or by try_to_swap_out (or shmem_writepage) re-using
367                  * the just freed swap entry for an existing page.
368                  * May fail (-ENOMEM) if radix-tree node allocation failed.
369                  */
370                 err = add_to_swap_cache(new_page, entry);
371                 if (!err) {
372                         /*
373                          * Initiate read into locked page and return.
374                          */
375                         lru_cache_add_active(new_page);
376                         swap_readpage(NULL, new_page);
377                         return new_page;
378                 }
379         } while (err != -ENOENT && err != -ENOMEM);
380
381         if (new_page)
382                 page_cache_release(new_page);
383         return found_page;
384 }