mm: write_cache_pages terminate quickly
[pandora-kernel.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  * Lock ordering in mm:
22  *
23  * inode->i_mutex       (while writing or truncating, not reading or faulting)
24  *   inode->i_alloc_sem (vmtruncate_range)
25  *   mm->mmap_sem
26  *     page->flags PG_locked (lock_page)
27  *       mapping->i_mmap_lock
28  *         anon_vma->lock
29  *           mm->page_table_lock or pte_lock
30  *             zone->lru_lock (in mark_page_accessed, isolate_lru_page)
31  *             swap_lock (in swap_duplicate, swap_info_get)
32  *               mmlist_lock (in mmput, drain_mmlist and others)
33  *               mapping->private_lock (in __set_page_dirty_buffers)
34  *               inode_lock (in set_page_dirty's __mark_inode_dirty)
35  *                 sb_lock (within inode_lock in fs/fs-writeback.c)
36  *                 mapping->tree_lock (widely used, in set_page_dirty,
37  *                           in arch-dependent flush_dcache_mmap_lock,
38  *                           within inode_lock in __sync_single_inode)
39  */
40
41 #include <linux/mm.h>
42 #include <linux/pagemap.h>
43 #include <linux/swap.h>
44 #include <linux/swapops.h>
45 #include <linux/slab.h>
46 #include <linux/init.h>
47 #include <linux/rmap.h>
48 #include <linux/rcupdate.h>
49 #include <linux/module.h>
50 #include <linux/kallsyms.h>
51 #include <linux/memcontrol.h>
52 #include <linux/mmu_notifier.h>
53
54 #include <asm/tlbflush.h>
55
56 struct kmem_cache *anon_vma_cachep;
57
58 /**
59  * anon_vma_prepare - attach an anon_vma to a memory region
60  * @vma: the memory region in question
61  *
62  * This makes sure the memory mapping described by 'vma' has
63  * an 'anon_vma' attached to it, so that we can associate the
64  * anonymous pages mapped into it with that anon_vma.
65  *
66  * The common case will be that we already have one, but if
67  * if not we either need to find an adjacent mapping that we
68  * can re-use the anon_vma from (very common when the only
69  * reason for splitting a vma has been mprotect()), or we
70  * allocate a new one.
71  *
72  * Anon-vma allocations are very subtle, because we may have
73  * optimistically looked up an anon_vma in page_lock_anon_vma()
74  * and that may actually touch the spinlock even in the newly
75  * allocated vma (it depends on RCU to make sure that the
76  * anon_vma isn't actually destroyed).
77  *
78  * As a result, we need to do proper anon_vma locking even
79  * for the new allocation. At the same time, we do not want
80  * to do any locking for the common case of already having
81  * an anon_vma.
82  *
83  * This must be called with the mmap_sem held for reading.
84  */
85 int anon_vma_prepare(struct vm_area_struct *vma)
86 {
87         struct anon_vma *anon_vma = vma->anon_vma;
88
89         might_sleep();
90         if (unlikely(!anon_vma)) {
91                 struct mm_struct *mm = vma->vm_mm;
92                 struct anon_vma *allocated;
93
94                 anon_vma = find_mergeable_anon_vma(vma);
95                 allocated = NULL;
96                 if (!anon_vma) {
97                         anon_vma = anon_vma_alloc();
98                         if (unlikely(!anon_vma))
99                                 return -ENOMEM;
100                         allocated = anon_vma;
101                 }
102                 spin_lock(&anon_vma->lock);
103
104                 /* page_table_lock to protect against threads */
105                 spin_lock(&mm->page_table_lock);
106                 if (likely(!vma->anon_vma)) {
107                         vma->anon_vma = anon_vma;
108                         list_add_tail(&vma->anon_vma_node, &anon_vma->head);
109                         allocated = NULL;
110                 }
111                 spin_unlock(&mm->page_table_lock);
112
113                 spin_unlock(&anon_vma->lock);
114                 if (unlikely(allocated))
115                         anon_vma_free(allocated);
116         }
117         return 0;
118 }
119
120 void __anon_vma_merge(struct vm_area_struct *vma, struct vm_area_struct *next)
121 {
122         BUG_ON(vma->anon_vma != next->anon_vma);
123         list_del(&next->anon_vma_node);
124 }
125
126 void __anon_vma_link(struct vm_area_struct *vma)
127 {
128         struct anon_vma *anon_vma = vma->anon_vma;
129
130         if (anon_vma)
131                 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
132 }
133
134 void anon_vma_link(struct vm_area_struct *vma)
135 {
136         struct anon_vma *anon_vma = vma->anon_vma;
137
138         if (anon_vma) {
139                 spin_lock(&anon_vma->lock);
140                 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
141                 spin_unlock(&anon_vma->lock);
142         }
143 }
144
145 void anon_vma_unlink(struct vm_area_struct *vma)
146 {
147         struct anon_vma *anon_vma = vma->anon_vma;
148         int empty;
149
150         if (!anon_vma)
151                 return;
152
153         spin_lock(&anon_vma->lock);
154         list_del(&vma->anon_vma_node);
155
156         /* We must garbage collect the anon_vma if it's empty */
157         empty = list_empty(&anon_vma->head);
158         spin_unlock(&anon_vma->lock);
159
160         if (empty)
161                 anon_vma_free(anon_vma);
162 }
163
164 static void anon_vma_ctor(void *data)
165 {
166         struct anon_vma *anon_vma = data;
167
168         spin_lock_init(&anon_vma->lock);
169         INIT_LIST_HEAD(&anon_vma->head);
170 }
171
172 void __init anon_vma_init(void)
173 {
174         anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
175                         0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor);
176 }
177
178 /*
179  * Getting a lock on a stable anon_vma from a page off the LRU is
180  * tricky: page_lock_anon_vma rely on RCU to guard against the races.
181  */
182 static struct anon_vma *page_lock_anon_vma(struct page *page)
183 {
184         struct anon_vma *anon_vma;
185         unsigned long anon_mapping;
186
187         rcu_read_lock();
188         anon_mapping = (unsigned long) page->mapping;
189         if (!(anon_mapping & PAGE_MAPPING_ANON))
190                 goto out;
191         if (!page_mapped(page))
192                 goto out;
193
194         anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
195         spin_lock(&anon_vma->lock);
196         return anon_vma;
197 out:
198         rcu_read_unlock();
199         return NULL;
200 }
201
202 static void page_unlock_anon_vma(struct anon_vma *anon_vma)
203 {
204         spin_unlock(&anon_vma->lock);
205         rcu_read_unlock();
206 }
207
208 /*
209  * At what user virtual address is page expected in @vma?
210  * Returns virtual address or -EFAULT if page's index/offset is not
211  * within the range mapped the @vma.
212  */
213 static inline unsigned long
214 vma_address(struct page *page, struct vm_area_struct *vma)
215 {
216         pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
217         unsigned long address;
218
219         address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
220         if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
221                 /* page should be within @vma mapping range */
222                 return -EFAULT;
223         }
224         return address;
225 }
226
227 /*
228  * At what user virtual address is page expected in vma? checking that the
229  * page matches the vma: currently only used on anon pages, by unuse_vma;
230  */
231 unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
232 {
233         if (PageAnon(page)) {
234                 if ((void *)vma->anon_vma !=
235                     (void *)page->mapping - PAGE_MAPPING_ANON)
236                         return -EFAULT;
237         } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) {
238                 if (!vma->vm_file ||
239                     vma->vm_file->f_mapping != page->mapping)
240                         return -EFAULT;
241         } else
242                 return -EFAULT;
243         return vma_address(page, vma);
244 }
245
246 /*
247  * Check that @page is mapped at @address into @mm.
248  *
249  * If @sync is false, page_check_address may perform a racy check to avoid
250  * the page table lock when the pte is not present (helpful when reclaiming
251  * highly shared pages).
252  *
253  * On success returns with pte mapped and locked.
254  */
255 pte_t *page_check_address(struct page *page, struct mm_struct *mm,
256                           unsigned long address, spinlock_t **ptlp, int sync)
257 {
258         pgd_t *pgd;
259         pud_t *pud;
260         pmd_t *pmd;
261         pte_t *pte;
262         spinlock_t *ptl;
263
264         pgd = pgd_offset(mm, address);
265         if (!pgd_present(*pgd))
266                 return NULL;
267
268         pud = pud_offset(pgd, address);
269         if (!pud_present(*pud))
270                 return NULL;
271
272         pmd = pmd_offset(pud, address);
273         if (!pmd_present(*pmd))
274                 return NULL;
275
276         pte = pte_offset_map(pmd, address);
277         /* Make a quick check before getting the lock */
278         if (!sync && !pte_present(*pte)) {
279                 pte_unmap(pte);
280                 return NULL;
281         }
282
283         ptl = pte_lockptr(mm, pmd);
284         spin_lock(ptl);
285         if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
286                 *ptlp = ptl;
287                 return pte;
288         }
289         pte_unmap_unlock(pte, ptl);
290         return NULL;
291 }
292
293 /*
294  * Subfunctions of page_referenced: page_referenced_one called
295  * repeatedly from either page_referenced_anon or page_referenced_file.
296  */
297 static int page_referenced_one(struct page *page,
298         struct vm_area_struct *vma, unsigned int *mapcount)
299 {
300         struct mm_struct *mm = vma->vm_mm;
301         unsigned long address;
302         pte_t *pte;
303         spinlock_t *ptl;
304         int referenced = 0;
305
306         address = vma_address(page, vma);
307         if (address == -EFAULT)
308                 goto out;
309
310         pte = page_check_address(page, mm, address, &ptl, 0);
311         if (!pte)
312                 goto out;
313
314         if (vma->vm_flags & VM_LOCKED) {
315                 referenced++;
316                 *mapcount = 1;  /* break early from loop */
317         } else if (ptep_clear_flush_young_notify(vma, address, pte))
318                 referenced++;
319
320         /* Pretend the page is referenced if the task has the
321            swap token and is in the middle of a page fault. */
322         if (mm != current->mm && has_swap_token(mm) &&
323                         rwsem_is_locked(&mm->mmap_sem))
324                 referenced++;
325
326         (*mapcount)--;
327         pte_unmap_unlock(pte, ptl);
328 out:
329         return referenced;
330 }
331
332 static int page_referenced_anon(struct page *page,
333                                 struct mem_cgroup *mem_cont)
334 {
335         unsigned int mapcount;
336         struct anon_vma *anon_vma;
337         struct vm_area_struct *vma;
338         int referenced = 0;
339
340         anon_vma = page_lock_anon_vma(page);
341         if (!anon_vma)
342                 return referenced;
343
344         mapcount = page_mapcount(page);
345         list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
346                 /*
347                  * If we are reclaiming on behalf of a cgroup, skip
348                  * counting on behalf of references from different
349                  * cgroups
350                  */
351                 if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
352                         continue;
353                 referenced += page_referenced_one(page, vma, &mapcount);
354                 if (!mapcount)
355                         break;
356         }
357
358         page_unlock_anon_vma(anon_vma);
359         return referenced;
360 }
361
362 /**
363  * page_referenced_file - referenced check for object-based rmap
364  * @page: the page we're checking references on.
365  * @mem_cont: target memory controller
366  *
367  * For an object-based mapped page, find all the places it is mapped and
368  * check/clear the referenced flag.  This is done by following the page->mapping
369  * pointer, then walking the chain of vmas it holds.  It returns the number
370  * of references it found.
371  *
372  * This function is only called from page_referenced for object-based pages.
373  */
374 static int page_referenced_file(struct page *page,
375                                 struct mem_cgroup *mem_cont)
376 {
377         unsigned int mapcount;
378         struct address_space *mapping = page->mapping;
379         pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
380         struct vm_area_struct *vma;
381         struct prio_tree_iter iter;
382         int referenced = 0;
383
384         /*
385          * The caller's checks on page->mapping and !PageAnon have made
386          * sure that this is a file page: the check for page->mapping
387          * excludes the case just before it gets set on an anon page.
388          */
389         BUG_ON(PageAnon(page));
390
391         /*
392          * The page lock not only makes sure that page->mapping cannot
393          * suddenly be NULLified by truncation, it makes sure that the
394          * structure at mapping cannot be freed and reused yet,
395          * so we can safely take mapping->i_mmap_lock.
396          */
397         BUG_ON(!PageLocked(page));
398
399         spin_lock(&mapping->i_mmap_lock);
400
401         /*
402          * i_mmap_lock does not stabilize mapcount at all, but mapcount
403          * is more likely to be accurate if we note it after spinning.
404          */
405         mapcount = page_mapcount(page);
406
407         vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
408                 /*
409                  * If we are reclaiming on behalf of a cgroup, skip
410                  * counting on behalf of references from different
411                  * cgroups
412                  */
413                 if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
414                         continue;
415                 if ((vma->vm_flags & (VM_LOCKED|VM_MAYSHARE))
416                                   == (VM_LOCKED|VM_MAYSHARE)) {
417                         referenced++;
418                         break;
419                 }
420                 referenced += page_referenced_one(page, vma, &mapcount);
421                 if (!mapcount)
422                         break;
423         }
424
425         spin_unlock(&mapping->i_mmap_lock);
426         return referenced;
427 }
428
429 /**
430  * page_referenced - test if the page was referenced
431  * @page: the page to test
432  * @is_locked: caller holds lock on the page
433  * @mem_cont: target memory controller
434  *
435  * Quick test_and_clear_referenced for all mappings to a page,
436  * returns the number of ptes which referenced the page.
437  */
438 int page_referenced(struct page *page, int is_locked,
439                         struct mem_cgroup *mem_cont)
440 {
441         int referenced = 0;
442
443         if (TestClearPageReferenced(page))
444                 referenced++;
445
446         if (page_mapped(page) && page->mapping) {
447                 if (PageAnon(page))
448                         referenced += page_referenced_anon(page, mem_cont);
449                 else if (is_locked)
450                         referenced += page_referenced_file(page, mem_cont);
451                 else if (!trylock_page(page))
452                         referenced++;
453                 else {
454                         if (page->mapping)
455                                 referenced +=
456                                         page_referenced_file(page, mem_cont);
457                         unlock_page(page);
458                 }
459         }
460
461         if (page_test_and_clear_young(page))
462                 referenced++;
463
464         return referenced;
465 }
466
467 static int page_mkclean_one(struct page *page, struct vm_area_struct *vma)
468 {
469         struct mm_struct *mm = vma->vm_mm;
470         unsigned long address;
471         pte_t *pte;
472         spinlock_t *ptl;
473         int ret = 0;
474
475         address = vma_address(page, vma);
476         if (address == -EFAULT)
477                 goto out;
478
479         pte = page_check_address(page, mm, address, &ptl, 1);
480         if (!pte)
481                 goto out;
482
483         if (pte_dirty(*pte) || pte_write(*pte)) {
484                 pte_t entry;
485
486                 flush_cache_page(vma, address, pte_pfn(*pte));
487                 entry = ptep_clear_flush_notify(vma, address, pte);
488                 entry = pte_wrprotect(entry);
489                 entry = pte_mkclean(entry);
490                 set_pte_at(mm, address, pte, entry);
491                 ret = 1;
492         }
493
494         pte_unmap_unlock(pte, ptl);
495 out:
496         return ret;
497 }
498
499 static int page_mkclean_file(struct address_space *mapping, struct page *page)
500 {
501         pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
502         struct vm_area_struct *vma;
503         struct prio_tree_iter iter;
504         int ret = 0;
505
506         BUG_ON(PageAnon(page));
507
508         spin_lock(&mapping->i_mmap_lock);
509         vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
510                 if (vma->vm_flags & VM_SHARED)
511                         ret += page_mkclean_one(page, vma);
512         }
513         spin_unlock(&mapping->i_mmap_lock);
514         return ret;
515 }
516
517 int page_mkclean(struct page *page)
518 {
519         int ret = 0;
520
521         BUG_ON(!PageLocked(page));
522
523         if (page_mapped(page)) {
524                 struct address_space *mapping = page_mapping(page);
525                 if (mapping) {
526                         ret = page_mkclean_file(mapping, page);
527                         if (page_test_dirty(page)) {
528                                 page_clear_dirty(page);
529                                 ret = 1;
530                         }
531                 }
532         }
533
534         return ret;
535 }
536 EXPORT_SYMBOL_GPL(page_mkclean);
537
538 /**
539  * __page_set_anon_rmap - setup new anonymous rmap
540  * @page:       the page to add the mapping to
541  * @vma:        the vm area in which the mapping is added
542  * @address:    the user virtual address mapped
543  */
544 static void __page_set_anon_rmap(struct page *page,
545         struct vm_area_struct *vma, unsigned long address)
546 {
547         struct anon_vma *anon_vma = vma->anon_vma;
548
549         BUG_ON(!anon_vma);
550         anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
551         page->mapping = (struct address_space *) anon_vma;
552
553         page->index = linear_page_index(vma, address);
554
555         /*
556          * nr_mapped state can be updated without turning off
557          * interrupts because it is not modified via interrupt.
558          */
559         __inc_zone_page_state(page, NR_ANON_PAGES);
560 }
561
562 /**
563  * __page_check_anon_rmap - sanity check anonymous rmap addition
564  * @page:       the page to add the mapping to
565  * @vma:        the vm area in which the mapping is added
566  * @address:    the user virtual address mapped
567  */
568 static void __page_check_anon_rmap(struct page *page,
569         struct vm_area_struct *vma, unsigned long address)
570 {
571 #ifdef CONFIG_DEBUG_VM
572         /*
573          * The page's anon-rmap details (mapping and index) are guaranteed to
574          * be set up correctly at this point.
575          *
576          * We have exclusion against page_add_anon_rmap because the caller
577          * always holds the page locked, except if called from page_dup_rmap,
578          * in which case the page is already known to be setup.
579          *
580          * We have exclusion against page_add_new_anon_rmap because those pages
581          * are initially only visible via the pagetables, and the pte is locked
582          * over the call to page_add_new_anon_rmap.
583          */
584         struct anon_vma *anon_vma = vma->anon_vma;
585         anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
586         BUG_ON(page->mapping != (struct address_space *)anon_vma);
587         BUG_ON(page->index != linear_page_index(vma, address));
588 #endif
589 }
590
591 /**
592  * page_add_anon_rmap - add pte mapping to an anonymous page
593  * @page:       the page to add the mapping to
594  * @vma:        the vm area in which the mapping is added
595  * @address:    the user virtual address mapped
596  *
597  * The caller needs to hold the pte lock and the page must be locked.
598  */
599 void page_add_anon_rmap(struct page *page,
600         struct vm_area_struct *vma, unsigned long address)
601 {
602         VM_BUG_ON(!PageLocked(page));
603         VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
604         if (atomic_inc_and_test(&page->_mapcount))
605                 __page_set_anon_rmap(page, vma, address);
606         else
607                 __page_check_anon_rmap(page, vma, address);
608 }
609
610 /**
611  * page_add_new_anon_rmap - add pte mapping to a new anonymous page
612  * @page:       the page to add the mapping to
613  * @vma:        the vm area in which the mapping is added
614  * @address:    the user virtual address mapped
615  *
616  * Same as page_add_anon_rmap but must only be called on *new* pages.
617  * This means the inc-and-test can be bypassed.
618  * Page does not have to be locked.
619  */
620 void page_add_new_anon_rmap(struct page *page,
621         struct vm_area_struct *vma, unsigned long address)
622 {
623         BUG_ON(address < vma->vm_start || address >= vma->vm_end);
624         atomic_set(&page->_mapcount, 0); /* elevate count by 1 (starts at -1) */
625         __page_set_anon_rmap(page, vma, address);
626 }
627
628 /**
629  * page_add_file_rmap - add pte mapping to a file page
630  * @page: the page to add the mapping to
631  *
632  * The caller needs to hold the pte lock.
633  */
634 void page_add_file_rmap(struct page *page)
635 {
636         if (atomic_inc_and_test(&page->_mapcount))
637                 __inc_zone_page_state(page, NR_FILE_MAPPED);
638 }
639
640 #ifdef CONFIG_DEBUG_VM
641 /**
642  * page_dup_rmap - duplicate pte mapping to a page
643  * @page:       the page to add the mapping to
644  * @vma:        the vm area being duplicated
645  * @address:    the user virtual address mapped
646  *
647  * For copy_page_range only: minimal extract from page_add_file_rmap /
648  * page_add_anon_rmap, avoiding unnecessary tests (already checked) so it's
649  * quicker.
650  *
651  * The caller needs to hold the pte lock.
652  */
653 void page_dup_rmap(struct page *page, struct vm_area_struct *vma, unsigned long address)
654 {
655         BUG_ON(page_mapcount(page) == 0);
656         if (PageAnon(page))
657                 __page_check_anon_rmap(page, vma, address);
658         atomic_inc(&page->_mapcount);
659 }
660 #endif
661
662 /**
663  * page_remove_rmap - take down pte mapping from a page
664  * @page: page to remove mapping from
665  * @vma: the vm area in which the mapping is removed
666  *
667  * The caller needs to hold the pte lock.
668  */
669 void page_remove_rmap(struct page *page, struct vm_area_struct *vma)
670 {
671         if (atomic_add_negative(-1, &page->_mapcount)) {
672                 if (unlikely(page_mapcount(page) < 0)) {
673                         printk (KERN_EMERG "Eeek! page_mapcount(page) went negative! (%d)\n", page_mapcount(page));
674                         printk (KERN_EMERG "  page pfn = %lx\n", page_to_pfn(page));
675                         printk (KERN_EMERG "  page->flags = %lx\n", page->flags);
676                         printk (KERN_EMERG "  page->count = %x\n", page_count(page));
677                         printk (KERN_EMERG "  page->mapping = %p\n", page->mapping);
678                         print_symbol (KERN_EMERG "  vma->vm_ops = %s\n", (unsigned long)vma->vm_ops);
679                         if (vma->vm_ops) {
680                                 print_symbol (KERN_EMERG "  vma->vm_ops->fault = %s\n", (unsigned long)vma->vm_ops->fault);
681                         }
682                         if (vma->vm_file && vma->vm_file->f_op)
683                                 print_symbol (KERN_EMERG "  vma->vm_file->f_op->mmap = %s\n", (unsigned long)vma->vm_file->f_op->mmap);
684                         BUG();
685                 }
686
687                 /*
688                  * Now that the last pte has gone, s390 must transfer dirty
689                  * flag from storage key to struct page.  We can usually skip
690                  * this if the page is anon, so about to be freed; but perhaps
691                  * not if it's in swapcache - there might be another pte slot
692                  * containing the swap entry, but page not yet written to swap.
693                  */
694                 if ((!PageAnon(page) || PageSwapCache(page)) &&
695                     page_test_dirty(page)) {
696                         page_clear_dirty(page);
697                         set_page_dirty(page);
698                 }
699
700                 mem_cgroup_uncharge_page(page);
701                 __dec_zone_page_state(page,
702                         PageAnon(page) ? NR_ANON_PAGES : NR_FILE_MAPPED);
703                 /*
704                  * It would be tidy to reset the PageAnon mapping here,
705                  * but that might overwrite a racing page_add_anon_rmap
706                  * which increments mapcount after us but sets mapping
707                  * before us: so leave the reset to free_hot_cold_page,
708                  * and remember that it's only reliable while mapped.
709                  * Leaving it set also helps swapoff to reinstate ptes
710                  * faster for those pages still in swapcache.
711                  */
712         }
713 }
714
715 /*
716  * Subfunctions of try_to_unmap: try_to_unmap_one called
717  * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
718  */
719 static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
720                                 int migration)
721 {
722         struct mm_struct *mm = vma->vm_mm;
723         unsigned long address;
724         pte_t *pte;
725         pte_t pteval;
726         spinlock_t *ptl;
727         int ret = SWAP_AGAIN;
728
729         address = vma_address(page, vma);
730         if (address == -EFAULT)
731                 goto out;
732
733         pte = page_check_address(page, mm, address, &ptl, 0);
734         if (!pte)
735                 goto out;
736
737         /*
738          * If the page is mlock()d, we cannot swap it out.
739          * If it's recently referenced (perhaps page_referenced
740          * skipped over this mm) then we should reactivate it.
741          */
742         if (!migration && ((vma->vm_flags & VM_LOCKED) ||
743                         (ptep_clear_flush_young_notify(vma, address, pte)))) {
744                 ret = SWAP_FAIL;
745                 goto out_unmap;
746         }
747
748         /* Nuke the page table entry. */
749         flush_cache_page(vma, address, page_to_pfn(page));
750         pteval = ptep_clear_flush_notify(vma, address, pte);
751
752         /* Move the dirty bit to the physical page now the pte is gone. */
753         if (pte_dirty(pteval))
754                 set_page_dirty(page);
755
756         /* Update high watermark before we lower rss */
757         update_hiwater_rss(mm);
758
759         if (PageAnon(page)) {
760                 swp_entry_t entry = { .val = page_private(page) };
761
762                 if (PageSwapCache(page)) {
763                         /*
764                          * Store the swap location in the pte.
765                          * See handle_pte_fault() ...
766                          */
767                         swap_duplicate(entry);
768                         if (list_empty(&mm->mmlist)) {
769                                 spin_lock(&mmlist_lock);
770                                 if (list_empty(&mm->mmlist))
771                                         list_add(&mm->mmlist, &init_mm.mmlist);
772                                 spin_unlock(&mmlist_lock);
773                         }
774                         dec_mm_counter(mm, anon_rss);
775 #ifdef CONFIG_MIGRATION
776                 } else {
777                         /*
778                          * Store the pfn of the page in a special migration
779                          * pte. do_swap_page() will wait until the migration
780                          * pte is removed and then restart fault handling.
781                          */
782                         BUG_ON(!migration);
783                         entry = make_migration_entry(page, pte_write(pteval));
784 #endif
785                 }
786                 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
787                 BUG_ON(pte_file(*pte));
788         } else
789 #ifdef CONFIG_MIGRATION
790         if (migration) {
791                 /* Establish migration entry for a file page */
792                 swp_entry_t entry;
793                 entry = make_migration_entry(page, pte_write(pteval));
794                 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
795         } else
796 #endif
797                 dec_mm_counter(mm, file_rss);
798
799
800         page_remove_rmap(page, vma);
801         page_cache_release(page);
802
803 out_unmap:
804         pte_unmap_unlock(pte, ptl);
805 out:
806         return ret;
807 }
808
809 /*
810  * objrmap doesn't work for nonlinear VMAs because the assumption that
811  * offset-into-file correlates with offset-into-virtual-addresses does not hold.
812  * Consequently, given a particular page and its ->index, we cannot locate the
813  * ptes which are mapping that page without an exhaustive linear search.
814  *
815  * So what this code does is a mini "virtual scan" of each nonlinear VMA which
816  * maps the file to which the target page belongs.  The ->vm_private_data field
817  * holds the current cursor into that scan.  Successive searches will circulate
818  * around the vma's virtual address space.
819  *
820  * So as more replacement pressure is applied to the pages in a nonlinear VMA,
821  * more scanning pressure is placed against them as well.   Eventually pages
822  * will become fully unmapped and are eligible for eviction.
823  *
824  * For very sparsely populated VMAs this is a little inefficient - chances are
825  * there there won't be many ptes located within the scan cluster.  In this case
826  * maybe we could scan further - to the end of the pte page, perhaps.
827  */
828 #define CLUSTER_SIZE    min(32*PAGE_SIZE, PMD_SIZE)
829 #define CLUSTER_MASK    (~(CLUSTER_SIZE - 1))
830
831 static void try_to_unmap_cluster(unsigned long cursor,
832         unsigned int *mapcount, struct vm_area_struct *vma)
833 {
834         struct mm_struct *mm = vma->vm_mm;
835         pgd_t *pgd;
836         pud_t *pud;
837         pmd_t *pmd;
838         pte_t *pte;
839         pte_t pteval;
840         spinlock_t *ptl;
841         struct page *page;
842         unsigned long address;
843         unsigned long end;
844
845         address = (vma->vm_start + cursor) & CLUSTER_MASK;
846         end = address + CLUSTER_SIZE;
847         if (address < vma->vm_start)
848                 address = vma->vm_start;
849         if (end > vma->vm_end)
850                 end = vma->vm_end;
851
852         pgd = pgd_offset(mm, address);
853         if (!pgd_present(*pgd))
854                 return;
855
856         pud = pud_offset(pgd, address);
857         if (!pud_present(*pud))
858                 return;
859
860         pmd = pmd_offset(pud, address);
861         if (!pmd_present(*pmd))
862                 return;
863
864         pte = pte_offset_map_lock(mm, pmd, address, &ptl);
865
866         /* Update high watermark before we lower rss */
867         update_hiwater_rss(mm);
868
869         for (; address < end; pte++, address += PAGE_SIZE) {
870                 if (!pte_present(*pte))
871                         continue;
872                 page = vm_normal_page(vma, address, *pte);
873                 BUG_ON(!page || PageAnon(page));
874
875                 if (ptep_clear_flush_young_notify(vma, address, pte))
876                         continue;
877
878                 /* Nuke the page table entry. */
879                 flush_cache_page(vma, address, pte_pfn(*pte));
880                 pteval = ptep_clear_flush_notify(vma, address, pte);
881
882                 /* If nonlinear, store the file page offset in the pte. */
883                 if (page->index != linear_page_index(vma, address))
884                         set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
885
886                 /* Move the dirty bit to the physical page now the pte is gone. */
887                 if (pte_dirty(pteval))
888                         set_page_dirty(page);
889
890                 page_remove_rmap(page, vma);
891                 page_cache_release(page);
892                 dec_mm_counter(mm, file_rss);
893                 (*mapcount)--;
894         }
895         pte_unmap_unlock(pte - 1, ptl);
896 }
897
898 static int try_to_unmap_anon(struct page *page, int migration)
899 {
900         struct anon_vma *anon_vma;
901         struct vm_area_struct *vma;
902         int ret = SWAP_AGAIN;
903
904         anon_vma = page_lock_anon_vma(page);
905         if (!anon_vma)
906                 return ret;
907
908         list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
909                 ret = try_to_unmap_one(page, vma, migration);
910                 if (ret == SWAP_FAIL || !page_mapped(page))
911                         break;
912         }
913
914         page_unlock_anon_vma(anon_vma);
915         return ret;
916 }
917
918 /**
919  * try_to_unmap_file - unmap file page using the object-based rmap method
920  * @page: the page to unmap
921  * @migration: migration flag
922  *
923  * Find all the mappings of a page using the mapping pointer and the vma chains
924  * contained in the address_space struct it points to.
925  *
926  * This function is only called from try_to_unmap for object-based pages.
927  */
928 static int try_to_unmap_file(struct page *page, int migration)
929 {
930         struct address_space *mapping = page->mapping;
931         pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
932         struct vm_area_struct *vma;
933         struct prio_tree_iter iter;
934         int ret = SWAP_AGAIN;
935         unsigned long cursor;
936         unsigned long max_nl_cursor = 0;
937         unsigned long max_nl_size = 0;
938         unsigned int mapcount;
939
940         spin_lock(&mapping->i_mmap_lock);
941         vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
942                 ret = try_to_unmap_one(page, vma, migration);
943                 if (ret == SWAP_FAIL || !page_mapped(page))
944                         goto out;
945         }
946
947         if (list_empty(&mapping->i_mmap_nonlinear))
948                 goto out;
949
950         list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
951                                                 shared.vm_set.list) {
952                 if ((vma->vm_flags & VM_LOCKED) && !migration)
953                         continue;
954                 cursor = (unsigned long) vma->vm_private_data;
955                 if (cursor > max_nl_cursor)
956                         max_nl_cursor = cursor;
957                 cursor = vma->vm_end - vma->vm_start;
958                 if (cursor > max_nl_size)
959                         max_nl_size = cursor;
960         }
961
962         if (max_nl_size == 0) { /* any nonlinears locked or reserved */
963                 ret = SWAP_FAIL;
964                 goto out;
965         }
966
967         /*
968          * We don't try to search for this page in the nonlinear vmas,
969          * and page_referenced wouldn't have found it anyway.  Instead
970          * just walk the nonlinear vmas trying to age and unmap some.
971          * The mapcount of the page we came in with is irrelevant,
972          * but even so use it as a guide to how hard we should try?
973          */
974         mapcount = page_mapcount(page);
975         if (!mapcount)
976                 goto out;
977         cond_resched_lock(&mapping->i_mmap_lock);
978
979         max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
980         if (max_nl_cursor == 0)
981                 max_nl_cursor = CLUSTER_SIZE;
982
983         do {
984                 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
985                                                 shared.vm_set.list) {
986                         if ((vma->vm_flags & VM_LOCKED) && !migration)
987                                 continue;
988                         cursor = (unsigned long) vma->vm_private_data;
989                         while ( cursor < max_nl_cursor &&
990                                 cursor < vma->vm_end - vma->vm_start) {
991                                 try_to_unmap_cluster(cursor, &mapcount, vma);
992                                 cursor += CLUSTER_SIZE;
993                                 vma->vm_private_data = (void *) cursor;
994                                 if ((int)mapcount <= 0)
995                                         goto out;
996                         }
997                         vma->vm_private_data = (void *) max_nl_cursor;
998                 }
999                 cond_resched_lock(&mapping->i_mmap_lock);
1000                 max_nl_cursor += CLUSTER_SIZE;
1001         } while (max_nl_cursor <= max_nl_size);
1002
1003         /*
1004          * Don't loop forever (perhaps all the remaining pages are
1005          * in locked vmas).  Reset cursor on all unreserved nonlinear
1006          * vmas, now forgetting on which ones it had fallen behind.
1007          */
1008         list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1009                 vma->vm_private_data = NULL;
1010 out:
1011         spin_unlock(&mapping->i_mmap_lock);
1012         return ret;
1013 }
1014
1015 /**
1016  * try_to_unmap - try to remove all page table mappings to a page
1017  * @page: the page to get unmapped
1018  * @migration: migration flag
1019  *
1020  * Tries to remove all the page table entries which are mapping this
1021  * page, used in the pageout path.  Caller must hold the page lock.
1022  * Return values are:
1023  *
1024  * SWAP_SUCCESS - we succeeded in removing all mappings
1025  * SWAP_AGAIN   - we missed a mapping, try again later
1026  * SWAP_FAIL    - the page is unswappable
1027  */
1028 int try_to_unmap(struct page *page, int migration)
1029 {
1030         int ret;
1031
1032         BUG_ON(!PageLocked(page));
1033
1034         if (PageAnon(page))
1035                 ret = try_to_unmap_anon(page, migration);
1036         else
1037                 ret = try_to_unmap_file(page, migration);
1038
1039         if (!page_mapped(page))
1040                 ret = SWAP_SUCCESS;
1041         return ret;
1042 }
1043