HWPOISON: Add simple debugfs interface to inject hwpoison on arbitary PFNs
[linux-2.6.git] / mm / rmap.c
blob09c3d0b961168b6168a41b47a80e7f2c4774799f
1 /*
2 * mm/rmap.c - physical to virtual reverse mappings
4 * Copyright 2001, Rik van Riel <riel@conectiva.com.br>
5 * Released under the General Public License (GPL).
7 * Simple, low overhead reverse mapping scheme.
8 * Please try to keep this thing as modular as possible.
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.
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 2003, 2004
21 * Lock ordering in mm:
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)
40 * (code doesn't rely on that order so it could be switched around)
41 * ->tasklist_lock
42 * anon_vma->lock (memory_failure, collect_procs_anon)
43 * pte map lock
46 #include <linux/mm.h>
47 #include <linux/pagemap.h>
48 #include <linux/swap.h>
49 #include <linux/swapops.h>
50 #include <linux/slab.h>
51 #include <linux/init.h>
52 #include <linux/rmap.h>
53 #include <linux/rcupdate.h>
54 #include <linux/module.h>
55 #include <linux/memcontrol.h>
56 #include <linux/mmu_notifier.h>
57 #include <linux/migrate.h>
59 #include <asm/tlbflush.h>
61 #include "internal.h"
63 static struct kmem_cache *anon_vma_cachep;
65 static inline struct anon_vma *anon_vma_alloc(void)
67 return kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL);
70 static inline void anon_vma_free(struct anon_vma *anon_vma)
72 kmem_cache_free(anon_vma_cachep, anon_vma);
75 /**
76 * anon_vma_prepare - attach an anon_vma to a memory region
77 * @vma: the memory region in question
79 * This makes sure the memory mapping described by 'vma' has
80 * an 'anon_vma' attached to it, so that we can associate the
81 * anonymous pages mapped into it with that anon_vma.
83 * The common case will be that we already have one, but if
84 * if not we either need to find an adjacent mapping that we
85 * can re-use the anon_vma from (very common when the only
86 * reason for splitting a vma has been mprotect()), or we
87 * allocate a new one.
89 * Anon-vma allocations are very subtle, because we may have
90 * optimistically looked up an anon_vma in page_lock_anon_vma()
91 * and that may actually touch the spinlock even in the newly
92 * allocated vma (it depends on RCU to make sure that the
93 * anon_vma isn't actually destroyed).
95 * As a result, we need to do proper anon_vma locking even
96 * for the new allocation. At the same time, we do not want
97 * to do any locking for the common case of already having
98 * an anon_vma.
100 * This must be called with the mmap_sem held for reading.
102 int anon_vma_prepare(struct vm_area_struct *vma)
104 struct anon_vma *anon_vma = vma->anon_vma;
106 might_sleep();
107 if (unlikely(!anon_vma)) {
108 struct mm_struct *mm = vma->vm_mm;
109 struct anon_vma *allocated;
111 anon_vma = find_mergeable_anon_vma(vma);
112 allocated = NULL;
113 if (!anon_vma) {
114 anon_vma = anon_vma_alloc();
115 if (unlikely(!anon_vma))
116 return -ENOMEM;
117 allocated = anon_vma;
119 spin_lock(&anon_vma->lock);
121 /* page_table_lock to protect against threads */
122 spin_lock(&mm->page_table_lock);
123 if (likely(!vma->anon_vma)) {
124 vma->anon_vma = anon_vma;
125 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
126 allocated = NULL;
128 spin_unlock(&mm->page_table_lock);
130 spin_unlock(&anon_vma->lock);
131 if (unlikely(allocated))
132 anon_vma_free(allocated);
134 return 0;
137 void __anon_vma_merge(struct vm_area_struct *vma, struct vm_area_struct *next)
139 BUG_ON(vma->anon_vma != next->anon_vma);
140 list_del(&next->anon_vma_node);
143 void __anon_vma_link(struct vm_area_struct *vma)
145 struct anon_vma *anon_vma = vma->anon_vma;
147 if (anon_vma)
148 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
151 void anon_vma_link(struct vm_area_struct *vma)
153 struct anon_vma *anon_vma = vma->anon_vma;
155 if (anon_vma) {
156 spin_lock(&anon_vma->lock);
157 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
158 spin_unlock(&anon_vma->lock);
162 void anon_vma_unlink(struct vm_area_struct *vma)
164 struct anon_vma *anon_vma = vma->anon_vma;
165 int empty;
167 if (!anon_vma)
168 return;
170 spin_lock(&anon_vma->lock);
171 list_del(&vma->anon_vma_node);
173 /* We must garbage collect the anon_vma if it's empty */
174 empty = list_empty(&anon_vma->head);
175 spin_unlock(&anon_vma->lock);
177 if (empty)
178 anon_vma_free(anon_vma);
181 static void anon_vma_ctor(void *data)
183 struct anon_vma *anon_vma = data;
185 spin_lock_init(&anon_vma->lock);
186 INIT_LIST_HEAD(&anon_vma->head);
189 void __init anon_vma_init(void)
191 anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
192 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor);
196 * Getting a lock on a stable anon_vma from a page off the LRU is
197 * tricky: page_lock_anon_vma rely on RCU to guard against the races.
199 struct anon_vma *page_lock_anon_vma(struct page *page)
201 struct anon_vma *anon_vma;
202 unsigned long anon_mapping;
204 rcu_read_lock();
205 anon_mapping = (unsigned long) page->mapping;
206 if (!(anon_mapping & PAGE_MAPPING_ANON))
207 goto out;
208 if (!page_mapped(page))
209 goto out;
211 anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
212 spin_lock(&anon_vma->lock);
213 return anon_vma;
214 out:
215 rcu_read_unlock();
216 return NULL;
219 void page_unlock_anon_vma(struct anon_vma *anon_vma)
221 spin_unlock(&anon_vma->lock);
222 rcu_read_unlock();
226 * At what user virtual address is page expected in @vma?
227 * Returns virtual address or -EFAULT if page's index/offset is not
228 * within the range mapped the @vma.
230 static inline unsigned long
231 vma_address(struct page *page, struct vm_area_struct *vma)
233 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
234 unsigned long address;
236 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
237 if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
238 /* page should be within @vma mapping range */
239 return -EFAULT;
241 return address;
245 * At what user virtual address is page expected in vma? checking that the
246 * page matches the vma: currently only used on anon pages, by unuse_vma;
248 unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
250 if (PageAnon(page)) {
251 if ((void *)vma->anon_vma !=
252 (void *)page->mapping - PAGE_MAPPING_ANON)
253 return -EFAULT;
254 } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) {
255 if (!vma->vm_file ||
256 vma->vm_file->f_mapping != page->mapping)
257 return -EFAULT;
258 } else
259 return -EFAULT;
260 return vma_address(page, vma);
264 * Check that @page is mapped at @address into @mm.
266 * If @sync is false, page_check_address may perform a racy check to avoid
267 * the page table lock when the pte is not present (helpful when reclaiming
268 * highly shared pages).
270 * On success returns with pte mapped and locked.
272 pte_t *page_check_address(struct page *page, struct mm_struct *mm,
273 unsigned long address, spinlock_t **ptlp, int sync)
275 pgd_t *pgd;
276 pud_t *pud;
277 pmd_t *pmd;
278 pte_t *pte;
279 spinlock_t *ptl;
281 pgd = pgd_offset(mm, address);
282 if (!pgd_present(*pgd))
283 return NULL;
285 pud = pud_offset(pgd, address);
286 if (!pud_present(*pud))
287 return NULL;
289 pmd = pmd_offset(pud, address);
290 if (!pmd_present(*pmd))
291 return NULL;
293 pte = pte_offset_map(pmd, address);
294 /* Make a quick check before getting the lock */
295 if (!sync && !pte_present(*pte)) {
296 pte_unmap(pte);
297 return NULL;
300 ptl = pte_lockptr(mm, pmd);
301 spin_lock(ptl);
302 if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
303 *ptlp = ptl;
304 return pte;
306 pte_unmap_unlock(pte, ptl);
307 return NULL;
311 * page_mapped_in_vma - check whether a page is really mapped in a VMA
312 * @page: the page to test
313 * @vma: the VMA to test
315 * Returns 1 if the page is mapped into the page tables of the VMA, 0
316 * if the page is not mapped into the page tables of this VMA. Only
317 * valid for normal file or anonymous VMAs.
319 int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma)
321 unsigned long address;
322 pte_t *pte;
323 spinlock_t *ptl;
325 address = vma_address(page, vma);
326 if (address == -EFAULT) /* out of vma range */
327 return 0;
328 pte = page_check_address(page, vma->vm_mm, address, &ptl, 1);
329 if (!pte) /* the page is not in this mm */
330 return 0;
331 pte_unmap_unlock(pte, ptl);
333 return 1;
337 * Subfunctions of page_referenced: page_referenced_one called
338 * repeatedly from either page_referenced_anon or page_referenced_file.
340 static int page_referenced_one(struct page *page,
341 struct vm_area_struct *vma,
342 unsigned int *mapcount,
343 unsigned long *vm_flags)
345 struct mm_struct *mm = vma->vm_mm;
346 unsigned long address;
347 pte_t *pte;
348 spinlock_t *ptl;
349 int referenced = 0;
351 address = vma_address(page, vma);
352 if (address == -EFAULT)
353 goto out;
355 pte = page_check_address(page, mm, address, &ptl, 0);
356 if (!pte)
357 goto out;
360 * Don't want to elevate referenced for mlocked page that gets this far,
361 * in order that it progresses to try_to_unmap and is moved to the
362 * unevictable list.
364 if (vma->vm_flags & VM_LOCKED) {
365 *mapcount = 1; /* break early from loop */
366 *vm_flags |= VM_LOCKED;
367 goto out_unmap;
370 if (ptep_clear_flush_young_notify(vma, address, pte)) {
372 * Don't treat a reference through a sequentially read
373 * mapping as such. If the page has been used in
374 * another mapping, we will catch it; if this other
375 * mapping is already gone, the unmap path will have
376 * set PG_referenced or activated the page.
378 if (likely(!VM_SequentialReadHint(vma)))
379 referenced++;
382 /* Pretend the page is referenced if the task has the
383 swap token and is in the middle of a page fault. */
384 if (mm != current->mm && has_swap_token(mm) &&
385 rwsem_is_locked(&mm->mmap_sem))
386 referenced++;
388 out_unmap:
389 (*mapcount)--;
390 pte_unmap_unlock(pte, ptl);
391 out:
392 if (referenced)
393 *vm_flags |= vma->vm_flags;
394 return referenced;
397 static int page_referenced_anon(struct page *page,
398 struct mem_cgroup *mem_cont,
399 unsigned long *vm_flags)
401 unsigned int mapcount;
402 struct anon_vma *anon_vma;
403 struct vm_area_struct *vma;
404 int referenced = 0;
406 anon_vma = page_lock_anon_vma(page);
407 if (!anon_vma)
408 return referenced;
410 mapcount = page_mapcount(page);
411 list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
413 * If we are reclaiming on behalf of a cgroup, skip
414 * counting on behalf of references from different
415 * cgroups
417 if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
418 continue;
419 referenced += page_referenced_one(page, vma,
420 &mapcount, vm_flags);
421 if (!mapcount)
422 break;
425 page_unlock_anon_vma(anon_vma);
426 return referenced;
430 * page_referenced_file - referenced check for object-based rmap
431 * @page: the page we're checking references on.
432 * @mem_cont: target memory controller
433 * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
435 * For an object-based mapped page, find all the places it is mapped and
436 * check/clear the referenced flag. This is done by following the page->mapping
437 * pointer, then walking the chain of vmas it holds. It returns the number
438 * of references it found.
440 * This function is only called from page_referenced for object-based pages.
442 static int page_referenced_file(struct page *page,
443 struct mem_cgroup *mem_cont,
444 unsigned long *vm_flags)
446 unsigned int mapcount;
447 struct address_space *mapping = page->mapping;
448 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
449 struct vm_area_struct *vma;
450 struct prio_tree_iter iter;
451 int referenced = 0;
454 * The caller's checks on page->mapping and !PageAnon have made
455 * sure that this is a file page: the check for page->mapping
456 * excludes the case just before it gets set on an anon page.
458 BUG_ON(PageAnon(page));
461 * The page lock not only makes sure that page->mapping cannot
462 * suddenly be NULLified by truncation, it makes sure that the
463 * structure at mapping cannot be freed and reused yet,
464 * so we can safely take mapping->i_mmap_lock.
466 BUG_ON(!PageLocked(page));
468 spin_lock(&mapping->i_mmap_lock);
471 * i_mmap_lock does not stabilize mapcount at all, but mapcount
472 * is more likely to be accurate if we note it after spinning.
474 mapcount = page_mapcount(page);
476 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
478 * If we are reclaiming on behalf of a cgroup, skip
479 * counting on behalf of references from different
480 * cgroups
482 if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
483 continue;
484 referenced += page_referenced_one(page, vma,
485 &mapcount, vm_flags);
486 if (!mapcount)
487 break;
490 spin_unlock(&mapping->i_mmap_lock);
491 return referenced;
495 * page_referenced - test if the page was referenced
496 * @page: the page to test
497 * @is_locked: caller holds lock on the page
498 * @mem_cont: target memory controller
499 * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
501 * Quick test_and_clear_referenced for all mappings to a page,
502 * returns the number of ptes which referenced the page.
504 int page_referenced(struct page *page,
505 int is_locked,
506 struct mem_cgroup *mem_cont,
507 unsigned long *vm_flags)
509 int referenced = 0;
511 if (TestClearPageReferenced(page))
512 referenced++;
514 *vm_flags = 0;
515 if (page_mapped(page) && page->mapping) {
516 if (PageAnon(page))
517 referenced += page_referenced_anon(page, mem_cont,
518 vm_flags);
519 else if (is_locked)
520 referenced += page_referenced_file(page, mem_cont,
521 vm_flags);
522 else if (!trylock_page(page))
523 referenced++;
524 else {
525 if (page->mapping)
526 referenced += page_referenced_file(page,
527 mem_cont, vm_flags);
528 unlock_page(page);
532 if (page_test_and_clear_young(page))
533 referenced++;
535 return referenced;
538 static int page_mkclean_one(struct page *page, struct vm_area_struct *vma)
540 struct mm_struct *mm = vma->vm_mm;
541 unsigned long address;
542 pte_t *pte;
543 spinlock_t *ptl;
544 int ret = 0;
546 address = vma_address(page, vma);
547 if (address == -EFAULT)
548 goto out;
550 pte = page_check_address(page, mm, address, &ptl, 1);
551 if (!pte)
552 goto out;
554 if (pte_dirty(*pte) || pte_write(*pte)) {
555 pte_t entry;
557 flush_cache_page(vma, address, pte_pfn(*pte));
558 entry = ptep_clear_flush_notify(vma, address, pte);
559 entry = pte_wrprotect(entry);
560 entry = pte_mkclean(entry);
561 set_pte_at(mm, address, pte, entry);
562 ret = 1;
565 pte_unmap_unlock(pte, ptl);
566 out:
567 return ret;
570 static int page_mkclean_file(struct address_space *mapping, struct page *page)
572 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
573 struct vm_area_struct *vma;
574 struct prio_tree_iter iter;
575 int ret = 0;
577 BUG_ON(PageAnon(page));
579 spin_lock(&mapping->i_mmap_lock);
580 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
581 if (vma->vm_flags & VM_SHARED)
582 ret += page_mkclean_one(page, vma);
584 spin_unlock(&mapping->i_mmap_lock);
585 return ret;
588 int page_mkclean(struct page *page)
590 int ret = 0;
592 BUG_ON(!PageLocked(page));
594 if (page_mapped(page)) {
595 struct address_space *mapping = page_mapping(page);
596 if (mapping) {
597 ret = page_mkclean_file(mapping, page);
598 if (page_test_dirty(page)) {
599 page_clear_dirty(page);
600 ret = 1;
605 return ret;
607 EXPORT_SYMBOL_GPL(page_mkclean);
610 * __page_set_anon_rmap - setup new anonymous rmap
611 * @page: the page to add the mapping to
612 * @vma: the vm area in which the mapping is added
613 * @address: the user virtual address mapped
615 static void __page_set_anon_rmap(struct page *page,
616 struct vm_area_struct *vma, unsigned long address)
618 struct anon_vma *anon_vma = vma->anon_vma;
620 BUG_ON(!anon_vma);
621 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
622 page->mapping = (struct address_space *) anon_vma;
624 page->index = linear_page_index(vma, address);
627 * nr_mapped state can be updated without turning off
628 * interrupts because it is not modified via interrupt.
630 __inc_zone_page_state(page, NR_ANON_PAGES);
634 * __page_check_anon_rmap - sanity check anonymous rmap addition
635 * @page: the page to add the mapping to
636 * @vma: the vm area in which the mapping is added
637 * @address: the user virtual address mapped
639 static void __page_check_anon_rmap(struct page *page,
640 struct vm_area_struct *vma, unsigned long address)
642 #ifdef CONFIG_DEBUG_VM
644 * The page's anon-rmap details (mapping and index) are guaranteed to
645 * be set up correctly at this point.
647 * We have exclusion against page_add_anon_rmap because the caller
648 * always holds the page locked, except if called from page_dup_rmap,
649 * in which case the page is already known to be setup.
651 * We have exclusion against page_add_new_anon_rmap because those pages
652 * are initially only visible via the pagetables, and the pte is locked
653 * over the call to page_add_new_anon_rmap.
655 struct anon_vma *anon_vma = vma->anon_vma;
656 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
657 BUG_ON(page->mapping != (struct address_space *)anon_vma);
658 BUG_ON(page->index != linear_page_index(vma, address));
659 #endif
663 * page_add_anon_rmap - add pte mapping to an anonymous page
664 * @page: the page to add the mapping to
665 * @vma: the vm area in which the mapping is added
666 * @address: the user virtual address mapped
668 * The caller needs to hold the pte lock and the page must be locked.
670 void page_add_anon_rmap(struct page *page,
671 struct vm_area_struct *vma, unsigned long address)
673 VM_BUG_ON(!PageLocked(page));
674 VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
675 if (atomic_inc_and_test(&page->_mapcount))
676 __page_set_anon_rmap(page, vma, address);
677 else
678 __page_check_anon_rmap(page, vma, address);
682 * page_add_new_anon_rmap - add pte mapping to a new anonymous page
683 * @page: the page to add the mapping to
684 * @vma: the vm area in which the mapping is added
685 * @address: the user virtual address mapped
687 * Same as page_add_anon_rmap but must only be called on *new* pages.
688 * This means the inc-and-test can be bypassed.
689 * Page does not have to be locked.
691 void page_add_new_anon_rmap(struct page *page,
692 struct vm_area_struct *vma, unsigned long address)
694 VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
695 SetPageSwapBacked(page);
696 atomic_set(&page->_mapcount, 0); /* increment count (starts at -1) */
697 __page_set_anon_rmap(page, vma, address);
698 if (page_evictable(page, vma))
699 lru_cache_add_lru(page, LRU_ACTIVE_ANON);
700 else
701 add_page_to_unevictable_list(page);
705 * page_add_file_rmap - add pte mapping to a file page
706 * @page: the page to add the mapping to
708 * The caller needs to hold the pte lock.
710 void page_add_file_rmap(struct page *page)
712 if (atomic_inc_and_test(&page->_mapcount)) {
713 __inc_zone_page_state(page, NR_FILE_MAPPED);
714 mem_cgroup_update_mapped_file_stat(page, 1);
718 #ifdef CONFIG_DEBUG_VM
720 * page_dup_rmap - duplicate pte mapping to a page
721 * @page: the page to add the mapping to
722 * @vma: the vm area being duplicated
723 * @address: the user virtual address mapped
725 * For copy_page_range only: minimal extract from page_add_file_rmap /
726 * page_add_anon_rmap, avoiding unnecessary tests (already checked) so it's
727 * quicker.
729 * The caller needs to hold the pte lock.
731 void page_dup_rmap(struct page *page, struct vm_area_struct *vma, unsigned long address)
733 if (PageAnon(page))
734 __page_check_anon_rmap(page, vma, address);
735 atomic_inc(&page->_mapcount);
737 #endif
740 * page_remove_rmap - take down pte mapping from a page
741 * @page: page to remove mapping from
743 * The caller needs to hold the pte lock.
745 void page_remove_rmap(struct page *page)
747 if (atomic_add_negative(-1, &page->_mapcount)) {
749 * Now that the last pte has gone, s390 must transfer dirty
750 * flag from storage key to struct page. We can usually skip
751 * this if the page is anon, so about to be freed; but perhaps
752 * not if it's in swapcache - there might be another pte slot
753 * containing the swap entry, but page not yet written to swap.
755 if ((!PageAnon(page) || PageSwapCache(page)) &&
756 page_test_dirty(page)) {
757 page_clear_dirty(page);
758 set_page_dirty(page);
760 if (PageAnon(page))
761 mem_cgroup_uncharge_page(page);
762 __dec_zone_page_state(page,
763 PageAnon(page) ? NR_ANON_PAGES : NR_FILE_MAPPED);
764 mem_cgroup_update_mapped_file_stat(page, -1);
766 * It would be tidy to reset the PageAnon mapping here,
767 * but that might overwrite a racing page_add_anon_rmap
768 * which increments mapcount after us but sets mapping
769 * before us: so leave the reset to free_hot_cold_page,
770 * and remember that it's only reliable while mapped.
771 * Leaving it set also helps swapoff to reinstate ptes
772 * faster for those pages still in swapcache.
778 * Subfunctions of try_to_unmap: try_to_unmap_one called
779 * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
781 static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
782 enum ttu_flags flags)
784 struct mm_struct *mm = vma->vm_mm;
785 unsigned long address;
786 pte_t *pte;
787 pte_t pteval;
788 spinlock_t *ptl;
789 int ret = SWAP_AGAIN;
791 address = vma_address(page, vma);
792 if (address == -EFAULT)
793 goto out;
795 pte = page_check_address(page, mm, address, &ptl, 0);
796 if (!pte)
797 goto out;
800 * If the page is mlock()d, we cannot swap it out.
801 * If it's recently referenced (perhaps page_referenced
802 * skipped over this mm) then we should reactivate it.
804 if (!(flags & TTU_IGNORE_MLOCK)) {
805 if (vma->vm_flags & VM_LOCKED) {
806 ret = SWAP_MLOCK;
807 goto out_unmap;
810 if (!(flags & TTU_IGNORE_ACCESS)) {
811 if (ptep_clear_flush_young_notify(vma, address, pte)) {
812 ret = SWAP_FAIL;
813 goto out_unmap;
817 /* Nuke the page table entry. */
818 flush_cache_page(vma, address, page_to_pfn(page));
819 pteval = ptep_clear_flush_notify(vma, address, pte);
821 /* Move the dirty bit to the physical page now the pte is gone. */
822 if (pte_dirty(pteval))
823 set_page_dirty(page);
825 /* Update high watermark before we lower rss */
826 update_hiwater_rss(mm);
828 if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) {
829 if (PageAnon(page))
830 dec_mm_counter(mm, anon_rss);
831 else
832 dec_mm_counter(mm, file_rss);
833 set_pte_at(mm, address, pte,
834 swp_entry_to_pte(make_hwpoison_entry(page)));
835 } else if (PageAnon(page)) {
836 swp_entry_t entry = { .val = page_private(page) };
838 if (PageSwapCache(page)) {
840 * Store the swap location in the pte.
841 * See handle_pte_fault() ...
843 swap_duplicate(entry);
844 if (list_empty(&mm->mmlist)) {
845 spin_lock(&mmlist_lock);
846 if (list_empty(&mm->mmlist))
847 list_add(&mm->mmlist, &init_mm.mmlist);
848 spin_unlock(&mmlist_lock);
850 dec_mm_counter(mm, anon_rss);
851 } else if (PAGE_MIGRATION) {
853 * Store the pfn of the page in a special migration
854 * pte. do_swap_page() will wait until the migration
855 * pte is removed and then restart fault handling.
857 BUG_ON(TTU_ACTION(flags) != TTU_MIGRATION);
858 entry = make_migration_entry(page, pte_write(pteval));
860 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
861 BUG_ON(pte_file(*pte));
862 } else if (PAGE_MIGRATION && (TTU_ACTION(flags) == TTU_MIGRATION)) {
863 /* Establish migration entry for a file page */
864 swp_entry_t entry;
865 entry = make_migration_entry(page, pte_write(pteval));
866 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
867 } else
868 dec_mm_counter(mm, file_rss);
871 page_remove_rmap(page);
872 page_cache_release(page);
874 out_unmap:
875 pte_unmap_unlock(pte, ptl);
876 out:
877 return ret;
881 * objrmap doesn't work for nonlinear VMAs because the assumption that
882 * offset-into-file correlates with offset-into-virtual-addresses does not hold.
883 * Consequently, given a particular page and its ->index, we cannot locate the
884 * ptes which are mapping that page without an exhaustive linear search.
886 * So what this code does is a mini "virtual scan" of each nonlinear VMA which
887 * maps the file to which the target page belongs. The ->vm_private_data field
888 * holds the current cursor into that scan. Successive searches will circulate
889 * around the vma's virtual address space.
891 * So as more replacement pressure is applied to the pages in a nonlinear VMA,
892 * more scanning pressure is placed against them as well. Eventually pages
893 * will become fully unmapped and are eligible for eviction.
895 * For very sparsely populated VMAs this is a little inefficient - chances are
896 * there there won't be many ptes located within the scan cluster. In this case
897 * maybe we could scan further - to the end of the pte page, perhaps.
899 * Mlocked pages: check VM_LOCKED under mmap_sem held for read, if we can
900 * acquire it without blocking. If vma locked, mlock the pages in the cluster,
901 * rather than unmapping them. If we encounter the "check_page" that vmscan is
902 * trying to unmap, return SWAP_MLOCK, else default SWAP_AGAIN.
904 #define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
905 #define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
907 static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount,
908 struct vm_area_struct *vma, struct page *check_page)
910 struct mm_struct *mm = vma->vm_mm;
911 pgd_t *pgd;
912 pud_t *pud;
913 pmd_t *pmd;
914 pte_t *pte;
915 pte_t pteval;
916 spinlock_t *ptl;
917 struct page *page;
918 unsigned long address;
919 unsigned long end;
920 int ret = SWAP_AGAIN;
921 int locked_vma = 0;
923 address = (vma->vm_start + cursor) & CLUSTER_MASK;
924 end = address + CLUSTER_SIZE;
925 if (address < vma->vm_start)
926 address = vma->vm_start;
927 if (end > vma->vm_end)
928 end = vma->vm_end;
930 pgd = pgd_offset(mm, address);
931 if (!pgd_present(*pgd))
932 return ret;
934 pud = pud_offset(pgd, address);
935 if (!pud_present(*pud))
936 return ret;
938 pmd = pmd_offset(pud, address);
939 if (!pmd_present(*pmd))
940 return ret;
943 * MLOCK_PAGES => feature is configured.
944 * if we can acquire the mmap_sem for read, and vma is VM_LOCKED,
945 * keep the sem while scanning the cluster for mlocking pages.
947 if (MLOCK_PAGES && down_read_trylock(&vma->vm_mm->mmap_sem)) {
948 locked_vma = (vma->vm_flags & VM_LOCKED);
949 if (!locked_vma)
950 up_read(&vma->vm_mm->mmap_sem); /* don't need it */
953 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
955 /* Update high watermark before we lower rss */
956 update_hiwater_rss(mm);
958 for (; address < end; pte++, address += PAGE_SIZE) {
959 if (!pte_present(*pte))
960 continue;
961 page = vm_normal_page(vma, address, *pte);
962 BUG_ON(!page || PageAnon(page));
964 if (locked_vma) {
965 mlock_vma_page(page); /* no-op if already mlocked */
966 if (page == check_page)
967 ret = SWAP_MLOCK;
968 continue; /* don't unmap */
971 if (ptep_clear_flush_young_notify(vma, address, pte))
972 continue;
974 /* Nuke the page table entry. */
975 flush_cache_page(vma, address, pte_pfn(*pte));
976 pteval = ptep_clear_flush_notify(vma, address, pte);
978 /* If nonlinear, store the file page offset in the pte. */
979 if (page->index != linear_page_index(vma, address))
980 set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
982 /* Move the dirty bit to the physical page now the pte is gone. */
983 if (pte_dirty(pteval))
984 set_page_dirty(page);
986 page_remove_rmap(page);
987 page_cache_release(page);
988 dec_mm_counter(mm, file_rss);
989 (*mapcount)--;
991 pte_unmap_unlock(pte - 1, ptl);
992 if (locked_vma)
993 up_read(&vma->vm_mm->mmap_sem);
994 return ret;
998 * common handling for pages mapped in VM_LOCKED vmas
1000 static int try_to_mlock_page(struct page *page, struct vm_area_struct *vma)
1002 int mlocked = 0;
1004 if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
1005 if (vma->vm_flags & VM_LOCKED) {
1006 mlock_vma_page(page);
1007 mlocked++; /* really mlocked the page */
1009 up_read(&vma->vm_mm->mmap_sem);
1011 return mlocked;
1015 * try_to_unmap_anon - unmap or unlock anonymous page using the object-based
1016 * rmap method
1017 * @page: the page to unmap/unlock
1018 * @unlock: request for unlock rather than unmap [unlikely]
1019 * @migration: unmapping for migration - ignored if @unlock
1021 * Find all the mappings of a page using the mapping pointer and the vma chains
1022 * contained in the anon_vma struct it points to.
1024 * This function is only called from try_to_unmap/try_to_munlock for
1025 * anonymous pages.
1026 * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1027 * where the page was found will be held for write. So, we won't recheck
1028 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
1029 * 'LOCKED.
1031 static int try_to_unmap_anon(struct page *page, enum ttu_flags flags)
1033 struct anon_vma *anon_vma;
1034 struct vm_area_struct *vma;
1035 unsigned int mlocked = 0;
1036 int ret = SWAP_AGAIN;
1037 int unlock = TTU_ACTION(flags) == TTU_MUNLOCK;
1039 if (MLOCK_PAGES && unlikely(unlock))
1040 ret = SWAP_SUCCESS; /* default for try_to_munlock() */
1042 anon_vma = page_lock_anon_vma(page);
1043 if (!anon_vma)
1044 return ret;
1046 list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
1047 if (MLOCK_PAGES && unlikely(unlock)) {
1048 if (!((vma->vm_flags & VM_LOCKED) &&
1049 page_mapped_in_vma(page, vma)))
1050 continue; /* must visit all unlocked vmas */
1051 ret = SWAP_MLOCK; /* saw at least one mlocked vma */
1052 } else {
1053 ret = try_to_unmap_one(page, vma, flags);
1054 if (ret == SWAP_FAIL || !page_mapped(page))
1055 break;
1057 if (ret == SWAP_MLOCK) {
1058 mlocked = try_to_mlock_page(page, vma);
1059 if (mlocked)
1060 break; /* stop if actually mlocked page */
1064 page_unlock_anon_vma(anon_vma);
1066 if (mlocked)
1067 ret = SWAP_MLOCK; /* actually mlocked the page */
1068 else if (ret == SWAP_MLOCK)
1069 ret = SWAP_AGAIN; /* saw VM_LOCKED vma */
1071 return ret;
1075 * try_to_unmap_file - unmap/unlock file page using the object-based rmap method
1076 * @page: the page to unmap/unlock
1077 * @flags: action and flags
1079 * Find all the mappings of a page using the mapping pointer and the vma chains
1080 * contained in the address_space struct it points to.
1082 * This function is only called from try_to_unmap/try_to_munlock for
1083 * object-based pages.
1084 * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1085 * where the page was found will be held for write. So, we won't recheck
1086 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
1087 * 'LOCKED.
1089 static int try_to_unmap_file(struct page *page, enum ttu_flags flags)
1091 struct address_space *mapping = page->mapping;
1092 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1093 struct vm_area_struct *vma;
1094 struct prio_tree_iter iter;
1095 int ret = SWAP_AGAIN;
1096 unsigned long cursor;
1097 unsigned long max_nl_cursor = 0;
1098 unsigned long max_nl_size = 0;
1099 unsigned int mapcount;
1100 unsigned int mlocked = 0;
1101 int unlock = TTU_ACTION(flags) == TTU_MUNLOCK;
1103 if (MLOCK_PAGES && unlikely(unlock))
1104 ret = SWAP_SUCCESS; /* default for try_to_munlock() */
1106 spin_lock(&mapping->i_mmap_lock);
1107 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1108 if (MLOCK_PAGES && unlikely(unlock)) {
1109 if (!((vma->vm_flags & VM_LOCKED) &&
1110 page_mapped_in_vma(page, vma)))
1111 continue; /* must visit all vmas */
1112 ret = SWAP_MLOCK;
1113 } else {
1114 ret = try_to_unmap_one(page, vma, flags);
1115 if (ret == SWAP_FAIL || !page_mapped(page))
1116 goto out;
1118 if (ret == SWAP_MLOCK) {
1119 mlocked = try_to_mlock_page(page, vma);
1120 if (mlocked)
1121 break; /* stop if actually mlocked page */
1125 if (mlocked)
1126 goto out;
1128 if (list_empty(&mapping->i_mmap_nonlinear))
1129 goto out;
1131 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1132 shared.vm_set.list) {
1133 if (MLOCK_PAGES && unlikely(unlock)) {
1134 if (!(vma->vm_flags & VM_LOCKED))
1135 continue; /* must visit all vmas */
1136 ret = SWAP_MLOCK; /* leave mlocked == 0 */
1137 goto out; /* no need to look further */
1139 if (!MLOCK_PAGES && !(flags & TTU_IGNORE_MLOCK) &&
1140 (vma->vm_flags & VM_LOCKED))
1141 continue;
1142 cursor = (unsigned long) vma->vm_private_data;
1143 if (cursor > max_nl_cursor)
1144 max_nl_cursor = cursor;
1145 cursor = vma->vm_end - vma->vm_start;
1146 if (cursor > max_nl_size)
1147 max_nl_size = cursor;
1150 if (max_nl_size == 0) { /* all nonlinears locked or reserved ? */
1151 ret = SWAP_FAIL;
1152 goto out;
1156 * We don't try to search for this page in the nonlinear vmas,
1157 * and page_referenced wouldn't have found it anyway. Instead
1158 * just walk the nonlinear vmas trying to age and unmap some.
1159 * The mapcount of the page we came in with is irrelevant,
1160 * but even so use it as a guide to how hard we should try?
1162 mapcount = page_mapcount(page);
1163 if (!mapcount)
1164 goto out;
1165 cond_resched_lock(&mapping->i_mmap_lock);
1167 max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
1168 if (max_nl_cursor == 0)
1169 max_nl_cursor = CLUSTER_SIZE;
1171 do {
1172 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1173 shared.vm_set.list) {
1174 if (!MLOCK_PAGES && !(flags & TTU_IGNORE_MLOCK) &&
1175 (vma->vm_flags & VM_LOCKED))
1176 continue;
1177 cursor = (unsigned long) vma->vm_private_data;
1178 while ( cursor < max_nl_cursor &&
1179 cursor < vma->vm_end - vma->vm_start) {
1180 ret = try_to_unmap_cluster(cursor, &mapcount,
1181 vma, page);
1182 if (ret == SWAP_MLOCK)
1183 mlocked = 2; /* to return below */
1184 cursor += CLUSTER_SIZE;
1185 vma->vm_private_data = (void *) cursor;
1186 if ((int)mapcount <= 0)
1187 goto out;
1189 vma->vm_private_data = (void *) max_nl_cursor;
1191 cond_resched_lock(&mapping->i_mmap_lock);
1192 max_nl_cursor += CLUSTER_SIZE;
1193 } while (max_nl_cursor <= max_nl_size);
1196 * Don't loop forever (perhaps all the remaining pages are
1197 * in locked vmas). Reset cursor on all unreserved nonlinear
1198 * vmas, now forgetting on which ones it had fallen behind.
1200 list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1201 vma->vm_private_data = NULL;
1202 out:
1203 spin_unlock(&mapping->i_mmap_lock);
1204 if (mlocked)
1205 ret = SWAP_MLOCK; /* actually mlocked the page */
1206 else if (ret == SWAP_MLOCK)
1207 ret = SWAP_AGAIN; /* saw VM_LOCKED vma */
1208 return ret;
1212 * try_to_unmap - try to remove all page table mappings to a page
1213 * @page: the page to get unmapped
1214 * @flags: action and flags
1216 * Tries to remove all the page table entries which are mapping this
1217 * page, used in the pageout path. Caller must hold the page lock.
1218 * Return values are:
1220 * SWAP_SUCCESS - we succeeded in removing all mappings
1221 * SWAP_AGAIN - we missed a mapping, try again later
1222 * SWAP_FAIL - the page is unswappable
1223 * SWAP_MLOCK - page is mlocked.
1225 int try_to_unmap(struct page *page, enum ttu_flags flags)
1227 int ret;
1229 BUG_ON(!PageLocked(page));
1231 if (PageAnon(page))
1232 ret = try_to_unmap_anon(page, flags);
1233 else
1234 ret = try_to_unmap_file(page, flags);
1235 if (ret != SWAP_MLOCK && !page_mapped(page))
1236 ret = SWAP_SUCCESS;
1237 return ret;
1241 * try_to_munlock - try to munlock a page
1242 * @page: the page to be munlocked
1244 * Called from munlock code. Checks all of the VMAs mapping the page
1245 * to make sure nobody else has this page mlocked. The page will be
1246 * returned with PG_mlocked cleared if no other vmas have it mlocked.
1248 * Return values are:
1250 * SWAP_SUCCESS - no vma's holding page mlocked.
1251 * SWAP_AGAIN - page mapped in mlocked vma -- couldn't acquire mmap sem
1252 * SWAP_MLOCK - page is now mlocked.
1254 int try_to_munlock(struct page *page)
1256 VM_BUG_ON(!PageLocked(page) || PageLRU(page));
1258 if (PageAnon(page))
1259 return try_to_unmap_anon(page, TTU_MUNLOCK);
1260 else
1261 return try_to_unmap_file(page, TTU_MUNLOCK);