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