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