This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / mm / rmap.c
1 /*
2  * mm/rmap.c - physical to virtual reverse mappings
3  *
4  * Copyright 2001, Rik van Riel <riel@conectiva.com.br>
5  * Released under the General Public License (GPL).
6  *
7  * Simple, low overhead reverse mapping scheme.
8  * Please try to keep this thing as modular as possible.
9  *
10  * Provides methods for unmapping each kind of mapped page:
11  * the anon methods track anonymous pages, and
12  * the file methods track pages belonging to an inode.
13  *
14  * Original design by Rik van Riel <riel@conectiva.com.br> 2001
15  * File methods by Dave McCracken <dmccr@us.ibm.com> 2003, 2004
16  * Anonymous methods by Andrea Arcangeli <andrea@suse.de> 2004
17  * Contributions by Hugh Dickins <hugh@veritas.com> 2003, 2004
18  */
19
20 /*
21  * Locking:
22  * - the page->mapcount field is protected by the PG_maplock bit,
23  *   which nests within the mm->page_table_lock,
24  *   which nests within the page lock.
25  * - because swapout locking is opposite to the locking order
26  *   in the page fault path, the swapout path uses trylocks
27  *   on the mm->page_table_lock
28  */
29 #include <linux/mm.h>
30 #include <linux/pagemap.h>
31 #include <linux/swap.h>
32 #include <linux/swapops.h>
33 #include <linux/slab.h>
34 #include <linux/init.h>
35 #include <linux/rmap.h>
36
37 #include <asm/tlbflush.h>
38
39 //#define RMAP_DEBUG /* can be enabled only for debugging */
40
41 kmem_cache_t *anon_vma_cachep;
42
43 static inline void validate_anon_vma(struct vm_area_struct *find_vma)
44 {
45 #ifdef RMAP_DEBUG
46         struct anon_vma *anon_vma = find_vma->anon_vma;
47         struct vm_area_struct *vma;
48         unsigned int mapcount = 0;
49         int found = 0;
50
51         list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
52                 mapcount++;
53                 BUG_ON(mapcount > 100000);
54                 if (vma == find_vma)
55                         found = 1;
56         }
57         BUG_ON(!found);
58 #endif
59 }
60
61 /* This must be called under the mmap_sem. */
62 int anon_vma_prepare(struct vm_area_struct *vma)
63 {
64         struct anon_vma *anon_vma = vma->anon_vma;
65
66         might_sleep();
67         if (unlikely(!anon_vma)) {
68                 struct mm_struct *mm = vma->vm_mm;
69                 struct anon_vma *allocated = NULL;
70
71                 anon_vma = find_mergeable_anon_vma(vma);
72                 if (!anon_vma) {
73                         anon_vma = anon_vma_alloc();
74                         if (unlikely(!anon_vma))
75                                 return -ENOMEM;
76                         allocated = anon_vma;
77                 }
78
79                 /* page_table_lock to protect against threads */
80                 spin_lock(&mm->page_table_lock);
81                 if (likely(!vma->anon_vma)) {
82                         vma->anon_vma = anon_vma;
83                         list_add(&vma->anon_vma_node, &anon_vma->head);
84                         allocated = NULL;
85                 }
86                 spin_unlock(&mm->page_table_lock);
87                 if (unlikely(allocated))
88                         anon_vma_free(allocated);
89         }
90         return 0;
91 }
92
93 void __anon_vma_merge(struct vm_area_struct *vma, struct vm_area_struct *next)
94 {
95         if (!vma->anon_vma) {
96                 BUG_ON(!next->anon_vma);
97                 vma->anon_vma = next->anon_vma;
98                 list_add(&vma->anon_vma_node, &next->anon_vma_node);
99         } else {
100                 /* if they're both non-null they must be the same */
101                 BUG_ON(vma->anon_vma != next->anon_vma);
102         }
103         list_del(&next->anon_vma_node);
104 }
105
106 void __anon_vma_link(struct vm_area_struct *vma)
107 {
108         struct anon_vma *anon_vma = vma->anon_vma;
109
110         if (anon_vma) {
111                 list_add(&vma->anon_vma_node, &anon_vma->head);
112                 validate_anon_vma(vma);
113         }
114 }
115
116 void anon_vma_link(struct vm_area_struct *vma)
117 {
118         struct anon_vma *anon_vma = vma->anon_vma;
119
120         if (anon_vma) {
121                 spin_lock(&anon_vma->lock);
122                 list_add(&vma->anon_vma_node, &anon_vma->head);
123                 validate_anon_vma(vma);
124                 spin_unlock(&anon_vma->lock);
125         }
126 }
127
128 void anon_vma_unlink(struct vm_area_struct *vma)
129 {
130         struct anon_vma *anon_vma = vma->anon_vma;
131         int empty;
132
133         if (!anon_vma)
134                 return;
135
136         spin_lock(&anon_vma->lock);
137         validate_anon_vma(vma);
138         list_del(&vma->anon_vma_node);
139
140         /* We must garbage collect the anon_vma if it's empty */
141         empty = list_empty(&anon_vma->head);
142         spin_unlock(&anon_vma->lock);
143
144         if (empty)
145                 anon_vma_free(anon_vma);
146 }
147
148 static void anon_vma_ctor(void *data, kmem_cache_t *cachep, unsigned long flags)
149 {
150         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
151                                                 SLAB_CTOR_CONSTRUCTOR) {
152                 struct anon_vma *anon_vma = data;
153
154                 spin_lock_init(&anon_vma->lock);
155                 INIT_LIST_HEAD(&anon_vma->head);
156         }
157 }
158
159 void __init anon_vma_init(void)
160 {
161         anon_vma_cachep = kmem_cache_create("anon_vma",
162                 sizeof(struct anon_vma), 0, SLAB_PANIC, anon_vma_ctor, NULL);
163 }
164
165 /* this needs the page->flags PG_maplock held */
166 static inline void clear_page_anon(struct page *page)
167 {
168         BUG_ON(!page->mapping);
169         page->mapping = NULL;
170         ClearPageAnon(page);
171 }
172
173 /*
174  * At what user virtual address is page expected in vma?
175  */
176 static inline unsigned long
177 vma_address(struct page *page, struct vm_area_struct *vma)
178 {
179         pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
180         unsigned long address;
181
182         address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
183         if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
184                 /* page should be within any vma from prio_tree_next */
185                 BUG_ON(!PageAnon(page));
186                 return -EFAULT;
187         }
188         return address;
189 }
190
191 /*
192  * Subfunctions of page_referenced: page_referenced_one called
193  * repeatedly from either page_referenced_anon or page_referenced_file.
194  */
195 static int page_referenced_one(struct page *page,
196         struct vm_area_struct *vma, unsigned int *mapcount, int *failed)
197 {
198         struct mm_struct *mm = vma->vm_mm;
199         unsigned long address;
200         pgd_t *pgd;
201         pmd_t *pmd;
202         pte_t *pte;
203         int referenced = 0;
204
205         if (!mm->rss)
206                 goto out;
207         address = vma_address(page, vma);
208         if (address == -EFAULT)
209                 goto out;
210
211         if (!spin_trylock(&mm->page_table_lock)) {
212                 /*
213                  * For debug we're currently warning if not all found,
214                  * but in this case that's expected: suppress warning.
215                  */
216                 (*failed)++;
217                 goto out;
218         }
219
220         pgd = pgd_offset(mm, address);
221         if (!pgd_present(*pgd))
222                 goto out_unlock;
223
224         pmd = pmd_offset(pgd, address);
225         if (!pmd_present(*pmd))
226                 goto out_unlock;
227
228         pte = pte_offset_map(pmd, address);
229         if (!pte_present(*pte))
230                 goto out_unmap;
231
232         if (page_to_pfn(page) != pte_pfn(*pte))
233                 goto out_unmap;
234
235         if (ptep_test_and_clear_young(pte))
236                 referenced++;
237
238         (*mapcount)--;
239
240 out_unmap:
241         pte_unmap(pte);
242 out_unlock:
243         spin_unlock(&mm->page_table_lock);
244 out:
245         return referenced;
246 }
247
248 static inline int page_referenced_anon(struct page *page)
249 {
250         unsigned int mapcount = page->mapcount;
251         struct anon_vma *anon_vma = (struct anon_vma *) page->mapping;
252         struct vm_area_struct *vma;
253         int referenced = 0;
254         int failed = 0;
255
256         spin_lock(&anon_vma->lock);
257         BUG_ON(list_empty(&anon_vma->head));
258         list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
259                 referenced += page_referenced_one(page, vma,
260                                                   &mapcount, &failed);
261                 if (!mapcount)
262                         goto out;
263         }
264         WARN_ON(!failed);
265 out:
266         spin_unlock(&anon_vma->lock);
267         return referenced;
268 }
269
270 /**
271  * page_referenced_file - referenced check for object-based rmap
272  * @page: the page we're checking references on.
273  *
274  * For an object-based mapped page, find all the places it is mapped and
275  * check/clear the referenced flag.  This is done by following the page->mapping
276  * pointer, then walking the chain of vmas it holds.  It returns the number
277  * of references it found.
278  *
279  * This function is only called from page_referenced for object-based pages.
280  *
281  * The spinlock address_space->i_mmap_lock is tried.  If it can't be gotten,
282  * assume a reference count of 0, so try_to_unmap will then have a go.
283  */
284 static inline int page_referenced_file(struct page *page)
285 {
286         unsigned int mapcount = page->mapcount;
287         struct address_space *mapping = page->mapping;
288         pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
289         struct vm_area_struct *vma = NULL;
290         struct prio_tree_iter iter;
291         int referenced = 0;
292         int failed = 0;
293
294         if (!spin_trylock(&mapping->i_mmap_lock))
295                 return 0;
296
297         while ((vma = vma_prio_tree_next(vma, &mapping->i_mmap,
298                                         &iter, pgoff, pgoff)) != NULL) {
299                 if ((vma->vm_flags & (VM_LOCKED|VM_MAYSHARE))
300                                   == (VM_LOCKED|VM_MAYSHARE)) {
301                         referenced++;
302                         goto out;
303                 }
304                 referenced += page_referenced_one(page, vma,
305                                                   &mapcount, &failed);
306                 if (!mapcount)
307                         goto out;
308         }
309
310         if (list_empty(&mapping->i_mmap_nonlinear))
311                 WARN_ON(!failed);
312 out:
313         spin_unlock(&mapping->i_mmap_lock);
314         return referenced;
315 }
316
317 /**
318  * page_referenced - test if the page was referenced
319  * @page: the page to test
320  *
321  * Quick test_and_clear_referenced for all mappings to a page,
322  * returns the number of ptes which referenced the page.
323  * Caller needs to hold the rmap lock.
324  */
325 int page_referenced(struct page *page)
326 {
327         int referenced = 0;
328
329         if (page_test_and_clear_young(page))
330                 referenced++;
331
332         if (TestClearPageReferenced(page))
333                 referenced++;
334
335         if (page->mapcount && page->mapping) {
336                 if (PageAnon(page))
337                         referenced += page_referenced_anon(page);
338                 else
339                         referenced += page_referenced_file(page);
340         }
341         return referenced;
342 }
343
344 /**
345  * page_add_anon_rmap - add pte mapping to an anonymous page
346  * @page:       the page to add the mapping to
347  * @vma:        the vm area in which the mapping is added
348  * @address:    the user virtual address mapped
349  *
350  * The caller needs to hold the mm->page_table_lock.
351  */
352 void page_add_anon_rmap(struct page *page,
353         struct vm_area_struct *vma, unsigned long address)
354 {
355         struct anon_vma *anon_vma = vma->anon_vma;
356         pgoff_t index;
357
358         BUG_ON(PageReserved(page));
359         BUG_ON(!anon_vma);
360
361         index = (address - vma->vm_start) >> PAGE_SHIFT;
362         index += vma->vm_pgoff;
363         index >>= PAGE_CACHE_SHIFT - PAGE_SHIFT;
364
365         /*
366          * Setting and clearing PG_anon must always happen inside
367          * page_map_lock to avoid races between mapping and
368          * unmapping on different processes of the same
369          * shared cow swapcache page. And while we take the
370          * page_map_lock PG_anon cannot change from under us.
371          * Actually PG_anon cannot change under fork either
372          * since fork holds a reference on the page so it cannot
373          * be unmapped under fork and in turn copy_page_range is
374          * allowed to read PG_anon outside the page_map_lock.
375          */
376         page_map_lock(page);
377         if (!page->mapcount) {
378                 BUG_ON(PageAnon(page));
379                 BUG_ON(page->mapping);
380                 SetPageAnon(page);
381                 page->index = index;
382                 page->mapping = (struct address_space *) anon_vma;
383                 inc_page_state(nr_mapped);
384         } else {
385                 BUG_ON(!PageAnon(page));
386                 BUG_ON(page->index != index);
387                 BUG_ON(page->mapping != (struct address_space *) anon_vma);
388         }
389         page->mapcount++;
390         page_map_unlock(page);
391 }
392
393 /**
394  * page_add_file_rmap - add pte mapping to a file page
395  * @page: the page to add the mapping to
396  *
397  * The caller needs to hold the mm->page_table_lock.
398  */
399 void page_add_file_rmap(struct page *page)
400 {
401         BUG_ON(PageAnon(page));
402         if (!pfn_valid(page_to_pfn(page)) || PageReserved(page))
403                 return;
404
405         page_map_lock(page);
406         if (!page->mapcount)
407                 inc_page_state(nr_mapped);
408         page->mapcount++;
409         page_map_unlock(page);
410 }
411
412 /**
413  * page_remove_rmap - take down pte mapping from a page
414  * @page: page to remove mapping from
415  *
416  * Caller needs to hold the mm->page_table_lock.
417  */
418 void page_remove_rmap(struct page *page)
419 {
420         BUG_ON(PageReserved(page));
421         BUG_ON(!page->mapcount);
422
423         page_map_lock(page);
424         page->mapcount--;
425         if (!page->mapcount) {
426                 if (page_test_and_clear_dirty(page))
427                         set_page_dirty(page);
428                 if (PageAnon(page))
429                         clear_page_anon(page);
430                 dec_page_state(nr_mapped);
431         }
432         page_map_unlock(page);
433 }
434
435 /*
436  * Subfunctions of try_to_unmap: try_to_unmap_one called
437  * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
438  */
439 static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma)
440 {
441         struct mm_struct *mm = vma->vm_mm;
442         unsigned long address;
443         pgd_t *pgd;
444         pmd_t *pmd;
445         pte_t *pte;
446         pte_t pteval;
447         int ret = SWAP_AGAIN;
448
449         if (!mm->rss)
450                 goto out;
451         address = vma_address(page, vma);
452         if (address == -EFAULT)
453                 goto out;
454
455         /*
456          * We need the page_table_lock to protect us from page faults,
457          * munmap, fork, etc...
458          */
459         if (!spin_trylock(&mm->page_table_lock))
460                 goto out;
461
462         pgd = pgd_offset(mm, address);
463         if (!pgd_present(*pgd))
464                 goto out_unlock;
465
466         pmd = pmd_offset(pgd, address);
467         if (!pmd_present(*pmd))
468                 goto out_unlock;
469
470         pte = pte_offset_map(pmd, address);
471         if (!pte_present(*pte))
472                 goto out_unmap;
473
474         if (page_to_pfn(page) != pte_pfn(*pte))
475                 goto out_unmap;
476
477         /*
478          * If the page is mlock()d, we cannot swap it out.
479          * If it's recently referenced (perhaps page_referenced
480          * skipped over this mm) then we should reactivate it.
481          */
482         if ((vma->vm_flags & (VM_LOCKED|VM_RESERVED)) ||
483                         ptep_test_and_clear_young(pte)) {
484                 ret = SWAP_FAIL;
485                 goto out_unmap;
486         }
487
488         /* Nuke the page table entry. */
489         flush_cache_page(vma, address);
490         pteval = ptep_clear_flush(vma, address, pte);
491
492         /* Move the dirty bit to the physical page now the pte is gone. */
493         if (pte_dirty(pteval))
494                 set_page_dirty(page);
495
496         if (PageAnon(page)) {
497                 swp_entry_t entry = { .val = page->private };
498                 /*
499                  * Store the swap location in the pte.
500                  * See handle_pte_fault() ...
501                  */
502                 BUG_ON(!PageSwapCache(page));
503                 swap_duplicate(entry);
504                 set_pte(pte, swp_entry_to_pte(entry));
505                 BUG_ON(pte_file(*pte));
506         }
507
508         // mm->rss--;
509         vx_rsspages_dec(mm);
510         BUG_ON(!page->mapcount);
511         page->mapcount--;
512         page_cache_release(page);
513
514 out_unmap:
515         pte_unmap(pte);
516 out_unlock:
517         spin_unlock(&mm->page_table_lock);
518 out:
519         return ret;
520 }
521
522 /*
523  * objrmap doesn't work for nonlinear VMAs because the assumption that
524  * offset-into-file correlates with offset-into-virtual-addresses does not hold.
525  * Consequently, given a particular page and its ->index, we cannot locate the
526  * ptes which are mapping that page without an exhaustive linear search.
527  *
528  * So what this code does is a mini "virtual scan" of each nonlinear VMA which
529  * maps the file to which the target page belongs.  The ->vm_private_data field
530  * holds the current cursor into that scan.  Successive searches will circulate
531  * around the vma's virtual address space.
532  *
533  * So as more replacement pressure is applied to the pages in a nonlinear VMA,
534  * more scanning pressure is placed against them as well.   Eventually pages
535  * will become fully unmapped and are eligible for eviction.
536  *
537  * For very sparsely populated VMAs this is a little inefficient - chances are
538  * there there won't be many ptes located within the scan cluster.  In this case
539  * maybe we could scan further - to the end of the pte page, perhaps.
540  */
541 #define CLUSTER_SIZE    min(32*PAGE_SIZE, PMD_SIZE)
542 #define CLUSTER_MASK    (~(CLUSTER_SIZE - 1))
543
544 static int try_to_unmap_cluster(unsigned long cursor,
545         unsigned int *mapcount, struct vm_area_struct *vma)
546 {
547         struct mm_struct *mm = vma->vm_mm;
548         pgd_t *pgd;
549         pmd_t *pmd;
550         pte_t *pte;
551         pte_t pteval;
552         struct page *page;
553         unsigned long address;
554         unsigned long end;
555         unsigned long pfn;
556
557         /*
558          * We need the page_table_lock to protect us from page faults,
559          * munmap, fork, etc...
560          */
561         if (!spin_trylock(&mm->page_table_lock))
562                 return SWAP_FAIL;
563
564         address = (vma->vm_start + cursor) & CLUSTER_MASK;
565         end = address + CLUSTER_SIZE;
566         if (address < vma->vm_start)
567                 address = vma->vm_start;
568         if (end > vma->vm_end)
569                 end = vma->vm_end;
570
571         pgd = pgd_offset(mm, address);
572         if (!pgd_present(*pgd))
573                 goto out_unlock;
574
575         pmd = pmd_offset(pgd, address);
576         if (!pmd_present(*pmd))
577                 goto out_unlock;
578
579         for (pte = pte_offset_map(pmd, address);
580                         address < end; pte++, address += PAGE_SIZE) {
581
582                 if (!pte_present(*pte))
583                         continue;
584
585                 pfn = pte_pfn(*pte);
586                 if (!pfn_valid(pfn))
587                         continue;
588
589                 page = pfn_to_page(pfn);
590                 BUG_ON(PageAnon(page));
591                 if (PageReserved(page))
592                         continue;
593
594                 if (ptep_test_and_clear_young(pte))
595                         continue;
596
597                 /* Nuke the page table entry. */
598                 flush_cache_page(vma, address);
599                 pteval = ptep_clear_flush(vma, address, pte);
600
601                 /* If nonlinear, store the file page offset in the pte. */
602                 if (page->index != linear_page_index(vma, address))
603                         set_pte(pte, pgoff_to_pte(page->index));
604
605                 /* Move the dirty bit to the physical page now the pte is gone. */
606                 if (pte_dirty(pteval))
607                         set_page_dirty(page);
608
609                 page_remove_rmap(page);
610                 page_cache_release(page);
611                 mm->rss--;
612                 (*mapcount)--;
613         }
614
615         pte_unmap(pte);
616
617 out_unlock:
618         spin_unlock(&mm->page_table_lock);
619         return SWAP_AGAIN;
620 }
621
622 static inline int try_to_unmap_anon(struct page *page)
623 {
624         struct anon_vma *anon_vma = (struct anon_vma *) page->mapping;
625         struct vm_area_struct *vma;
626         int ret = SWAP_AGAIN;
627
628         spin_lock(&anon_vma->lock);
629         BUG_ON(list_empty(&anon_vma->head));
630         list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
631                 ret = try_to_unmap_one(page, vma);
632                 if (ret == SWAP_FAIL || !page->mapcount)
633                         break;
634         }
635         spin_unlock(&anon_vma->lock);
636         return ret;
637 }
638
639 /**
640  * try_to_unmap_file - unmap file page using the object-based rmap method
641  * @page: the page to unmap
642  *
643  * Find all the mappings of a page using the mapping pointer and the vma chains
644  * contained in the address_space struct it points to.
645  *
646  * This function is only called from try_to_unmap for object-based pages.
647  *
648  * The spinlock address_space->i_mmap_lock is tried.  If it can't be gotten,
649  * return a temporary error.
650  */
651 static inline int try_to_unmap_file(struct page *page)
652 {
653         struct address_space *mapping = page->mapping;
654         pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
655         struct vm_area_struct *vma = NULL;
656         struct prio_tree_iter iter;
657         int ret = SWAP_AGAIN;
658         unsigned long cursor;
659         unsigned long max_nl_cursor = 0;
660         unsigned long max_nl_size = 0;
661         unsigned int mapcount;
662
663         if (!spin_trylock(&mapping->i_mmap_lock))
664                 return ret;
665
666         while ((vma = vma_prio_tree_next(vma, &mapping->i_mmap,
667                                         &iter, pgoff, pgoff)) != NULL) {
668                 ret = try_to_unmap_one(page, vma);
669                 if (ret == SWAP_FAIL || !page->mapcount)
670                         goto out;
671         }
672
673         if (list_empty(&mapping->i_mmap_nonlinear))
674                 goto out;
675
676         list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
677                                                 shared.vm_set.list) {
678                 if (vma->vm_flags & (VM_LOCKED|VM_RESERVED))
679                         continue;
680                 cursor = (unsigned long) vma->vm_private_data;
681                 if (cursor > max_nl_cursor)
682                         max_nl_cursor = cursor;
683                 cursor = vma->vm_end - vma->vm_start;
684                 if (cursor > max_nl_size)
685                         max_nl_size = cursor;
686         }
687
688         if (max_nl_size == 0)   /* any nonlinears locked or reserved */
689                 goto out;
690
691         /*
692          * We don't try to search for this page in the nonlinear vmas,
693          * and page_referenced wouldn't have found it anyway.  Instead
694          * just walk the nonlinear vmas trying to age and unmap some.
695          * The mapcount of the page we came in with is irrelevant,
696          * but even so use it as a guide to how hard we should try?
697          */
698         mapcount = page->mapcount;
699         page_map_unlock(page);
700         cond_resched_lock(&mapping->i_mmap_lock);
701
702         max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
703         if (max_nl_cursor == 0)
704                 max_nl_cursor = CLUSTER_SIZE;
705
706         do {
707                 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
708                                                 shared.vm_set.list) {
709                         if (vma->vm_flags & (VM_LOCKED|VM_RESERVED))
710                                 continue;
711                         cursor = (unsigned long) vma->vm_private_data;
712                         while (vma->vm_mm->rss &&
713                                 cursor < max_nl_cursor &&
714                                 cursor < vma->vm_end - vma->vm_start) {
715                                 ret = try_to_unmap_cluster(
716                                                 cursor, &mapcount, vma);
717                                 if (ret == SWAP_FAIL)
718                                         break;
719                                 cursor += CLUSTER_SIZE;
720                                 vma->vm_private_data = (void *) cursor;
721                                 if ((int)mapcount <= 0)
722                                         goto relock;
723                         }
724                         if (ret != SWAP_FAIL)
725                                 vma->vm_private_data =
726                                         (void *) max_nl_cursor;
727                         ret = SWAP_AGAIN;
728                 }
729                 cond_resched_lock(&mapping->i_mmap_lock);
730                 max_nl_cursor += CLUSTER_SIZE;
731         } while (max_nl_cursor <= max_nl_size);
732
733         /*
734          * Don't loop forever (perhaps all the remaining pages are
735          * in locked vmas).  Reset cursor on all unreserved nonlinear
736          * vmas, now forgetting on which ones it had fallen behind.
737          */
738         list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
739                                                 shared.vm_set.list) {
740                 if (!(vma->vm_flags & VM_RESERVED))
741                         vma->vm_private_data = 0;
742         }
743 relock:
744         page_map_lock(page);
745 out:
746         spin_unlock(&mapping->i_mmap_lock);
747         return ret;
748 }
749
750 /**
751  * try_to_unmap - try to remove all page table mappings to a page
752  * @page: the page to get unmapped
753  *
754  * Tries to remove all the page table entries which are mapping this
755  * page, used in the pageout path.  Caller must hold the page lock
756  * and its rmap lock.  Return values are:
757  *
758  * SWAP_SUCCESS - we succeeded in removing all mappings
759  * SWAP_AGAIN   - we missed a trylock, try again later
760  * SWAP_FAIL    - the page is unswappable
761  */
762 int try_to_unmap(struct page *page)
763 {
764         int ret;
765
766         BUG_ON(PageReserved(page));
767         BUG_ON(!PageLocked(page));
768         BUG_ON(!page->mapcount);
769
770         if (PageAnon(page))
771                 ret = try_to_unmap_anon(page);
772         else
773                 ret = try_to_unmap_file(page);
774
775         if (!page->mapcount) {
776                 if (page_test_and_clear_dirty(page))
777                         set_page_dirty(page);
778                 if (PageAnon(page))
779                         clear_page_anon(page);
780                 dec_page_state(nr_mapped);
781                 ret = SWAP_SUCCESS;
782         }
783         return ret;
784 }