xref: /linux/mm/rmap.c (revision 3ca7b3c5b64d35fe02c35b5d44c2c58b49499fee)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * mm/rmap.c - physical to virtual reverse mappings
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright 2001, Rik van Riel <riel@conectiva.com.br>
51da177e4SLinus Torvalds  * Released under the General Public License (GPL).
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Simple, low overhead reverse mapping scheme.
81da177e4SLinus Torvalds  * Please try to keep this thing as modular as possible.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Provides methods for unmapping each kind of mapped page:
111da177e4SLinus Torvalds  * the anon methods track anonymous pages, and
121da177e4SLinus Torvalds  * the file methods track pages belonging to an inode.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * Original design by Rik van Riel <riel@conectiva.com.br> 2001
151da177e4SLinus Torvalds  * File methods by Dave McCracken <dmccr@us.ibm.com> 2003, 2004
161da177e4SLinus Torvalds  * Anonymous methods by Andrea Arcangeli <andrea@suse.de> 2004
1798f32602SHugh Dickins  * Contributions by Hugh Dickins 2003, 2004
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds /*
211da177e4SLinus Torvalds  * Lock ordering in mm:
221da177e4SLinus Torvalds  *
231b1dcc1bSJes Sorensen  * inode->i_mutex	(while writing or truncating, not reading or faulting)
2482591e6eSNick Piggin  *   inode->i_alloc_sem (vmtruncate_range)
251da177e4SLinus Torvalds  *   mm->mmap_sem
261da177e4SLinus Torvalds  *     page->flags PG_locked (lock_page)
271da177e4SLinus Torvalds  *       mapping->i_mmap_lock
281da177e4SLinus Torvalds  *         anon_vma->lock
29b8072f09SHugh Dickins  *           mm->page_table_lock or pte_lock
30053837fcSNick Piggin  *             zone->lru_lock (in mark_page_accessed, isolate_lru_page)
315d337b91SHugh Dickins  *             swap_lock (in swap_duplicate, swap_info_get)
321da177e4SLinus Torvalds  *               mmlist_lock (in mmput, drain_mmlist and others)
331da177e4SLinus Torvalds  *               mapping->private_lock (in __set_page_dirty_buffers)
341da177e4SLinus Torvalds  *               inode_lock (in set_page_dirty's __mark_inode_dirty)
351da177e4SLinus Torvalds  *                 sb_lock (within inode_lock in fs/fs-writeback.c)
361da177e4SLinus Torvalds  *                 mapping->tree_lock (widely used, in set_page_dirty,
371da177e4SLinus Torvalds  *                           in arch-dependent flush_dcache_mmap_lock,
381da177e4SLinus Torvalds  *                           within inode_lock in __sync_single_inode)
396a46079cSAndi Kleen  *
406a46079cSAndi Kleen  * (code doesn't rely on that order so it could be switched around)
416a46079cSAndi Kleen  * ->tasklist_lock
426a46079cSAndi Kleen  *   anon_vma->lock      (memory_failure, collect_procs_anon)
436a46079cSAndi Kleen  *     pte map lock
441da177e4SLinus Torvalds  */
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include <linux/mm.h>
471da177e4SLinus Torvalds #include <linux/pagemap.h>
481da177e4SLinus Torvalds #include <linux/swap.h>
491da177e4SLinus Torvalds #include <linux/swapops.h>
501da177e4SLinus Torvalds #include <linux/slab.h>
511da177e4SLinus Torvalds #include <linux/init.h>
521da177e4SLinus Torvalds #include <linux/rmap.h>
531da177e4SLinus Torvalds #include <linux/rcupdate.h>
54a48d07afSChristoph Lameter #include <linux/module.h>
558a9f3ccdSBalbir Singh #include <linux/memcontrol.h>
56cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
5764cdd548SKOSAKI Motohiro #include <linux/migrate.h>
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #include <asm/tlbflush.h>
601da177e4SLinus Torvalds 
61b291f000SNick Piggin #include "internal.h"
62b291f000SNick Piggin 
63fdd2e5f8SAdrian Bunk static struct kmem_cache *anon_vma_cachep;
64fdd2e5f8SAdrian Bunk 
65fdd2e5f8SAdrian Bunk static inline struct anon_vma *anon_vma_alloc(void)
66fdd2e5f8SAdrian Bunk {
67fdd2e5f8SAdrian Bunk 	return kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL);
68fdd2e5f8SAdrian Bunk }
69fdd2e5f8SAdrian Bunk 
70fdd2e5f8SAdrian Bunk static inline void anon_vma_free(struct anon_vma *anon_vma)
71fdd2e5f8SAdrian Bunk {
72fdd2e5f8SAdrian Bunk 	kmem_cache_free(anon_vma_cachep, anon_vma);
73fdd2e5f8SAdrian Bunk }
741da177e4SLinus Torvalds 
75d9d332e0SLinus Torvalds /**
76d9d332e0SLinus Torvalds  * anon_vma_prepare - attach an anon_vma to a memory region
77d9d332e0SLinus Torvalds  * @vma: the memory region in question
78d9d332e0SLinus Torvalds  *
79d9d332e0SLinus Torvalds  * This makes sure the memory mapping described by 'vma' has
80d9d332e0SLinus Torvalds  * an 'anon_vma' attached to it, so that we can associate the
81d9d332e0SLinus Torvalds  * anonymous pages mapped into it with that anon_vma.
82d9d332e0SLinus Torvalds  *
83d9d332e0SLinus Torvalds  * The common case will be that we already have one, but if
84d9d332e0SLinus Torvalds  * if not we either need to find an adjacent mapping that we
85d9d332e0SLinus Torvalds  * can re-use the anon_vma from (very common when the only
86d9d332e0SLinus Torvalds  * reason for splitting a vma has been mprotect()), or we
87d9d332e0SLinus Torvalds  * allocate a new one.
88d9d332e0SLinus Torvalds  *
89d9d332e0SLinus Torvalds  * Anon-vma allocations are very subtle, because we may have
90d9d332e0SLinus Torvalds  * optimistically looked up an anon_vma in page_lock_anon_vma()
91d9d332e0SLinus Torvalds  * and that may actually touch the spinlock even in the newly
92d9d332e0SLinus Torvalds  * allocated vma (it depends on RCU to make sure that the
93d9d332e0SLinus Torvalds  * anon_vma isn't actually destroyed).
94d9d332e0SLinus Torvalds  *
95d9d332e0SLinus Torvalds  * As a result, we need to do proper anon_vma locking even
96d9d332e0SLinus Torvalds  * for the new allocation. At the same time, we do not want
97d9d332e0SLinus Torvalds  * to do any locking for the common case of already having
98d9d332e0SLinus Torvalds  * an anon_vma.
99d9d332e0SLinus Torvalds  *
100d9d332e0SLinus Torvalds  * This must be called with the mmap_sem held for reading.
101d9d332e0SLinus Torvalds  */
1021da177e4SLinus Torvalds int anon_vma_prepare(struct vm_area_struct *vma)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	struct anon_vma *anon_vma = vma->anon_vma;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	might_sleep();
1071da177e4SLinus Torvalds 	if (unlikely(!anon_vma)) {
1081da177e4SLinus Torvalds 		struct mm_struct *mm = vma->vm_mm;
109d9d332e0SLinus Torvalds 		struct anon_vma *allocated;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 		anon_vma = find_mergeable_anon_vma(vma);
1121da177e4SLinus Torvalds 		allocated = NULL;
113d9d332e0SLinus Torvalds 		if (!anon_vma) {
1141da177e4SLinus Torvalds 			anon_vma = anon_vma_alloc();
1151da177e4SLinus Torvalds 			if (unlikely(!anon_vma))
1161da177e4SLinus Torvalds 				return -ENOMEM;
1171da177e4SLinus Torvalds 			allocated = anon_vma;
1181da177e4SLinus Torvalds 		}
119d9d332e0SLinus Torvalds 		spin_lock(&anon_vma->lock);
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 		/* page_table_lock to protect against threads */
1221da177e4SLinus Torvalds 		spin_lock(&mm->page_table_lock);
1231da177e4SLinus Torvalds 		if (likely(!vma->anon_vma)) {
1241da177e4SLinus Torvalds 			vma->anon_vma = anon_vma;
1250697212aSChristoph Lameter 			list_add_tail(&vma->anon_vma_node, &anon_vma->head);
1261da177e4SLinus Torvalds 			allocated = NULL;
1271da177e4SLinus Torvalds 		}
1281da177e4SLinus Torvalds 		spin_unlock(&mm->page_table_lock);
1291da177e4SLinus Torvalds 
130d9d332e0SLinus Torvalds 		spin_unlock(&anon_vma->lock);
1311da177e4SLinus Torvalds 		if (unlikely(allocated))
1321da177e4SLinus Torvalds 			anon_vma_free(allocated);
1331da177e4SLinus Torvalds 	}
1341da177e4SLinus Torvalds 	return 0;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds void __anon_vma_merge(struct vm_area_struct *vma, struct vm_area_struct *next)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	BUG_ON(vma->anon_vma != next->anon_vma);
1401da177e4SLinus Torvalds 	list_del(&next->anon_vma_node);
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds void __anon_vma_link(struct vm_area_struct *vma)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	struct anon_vma *anon_vma = vma->anon_vma;
1461da177e4SLinus Torvalds 
14730acbabaSHugh Dickins 	if (anon_vma)
1480697212aSChristoph Lameter 		list_add_tail(&vma->anon_vma_node, &anon_vma->head);
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds void anon_vma_link(struct vm_area_struct *vma)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	struct anon_vma *anon_vma = vma->anon_vma;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	if (anon_vma) {
1561da177e4SLinus Torvalds 		spin_lock(&anon_vma->lock);
1570697212aSChristoph Lameter 		list_add_tail(&vma->anon_vma_node, &anon_vma->head);
1581da177e4SLinus Torvalds 		spin_unlock(&anon_vma->lock);
1591da177e4SLinus Torvalds 	}
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds void anon_vma_unlink(struct vm_area_struct *vma)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	struct anon_vma *anon_vma = vma->anon_vma;
1651da177e4SLinus Torvalds 	int empty;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	if (!anon_vma)
1681da177e4SLinus Torvalds 		return;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	spin_lock(&anon_vma->lock);
1711da177e4SLinus Torvalds 	list_del(&vma->anon_vma_node);
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	/* We must garbage collect the anon_vma if it's empty */
1741da177e4SLinus Torvalds 	empty = list_empty(&anon_vma->head);
1751da177e4SLinus Torvalds 	spin_unlock(&anon_vma->lock);
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	if (empty)
1781da177e4SLinus Torvalds 		anon_vma_free(anon_vma);
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
18151cc5068SAlexey Dobriyan static void anon_vma_ctor(void *data)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds 	struct anon_vma *anon_vma = data;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	spin_lock_init(&anon_vma->lock);
1861da177e4SLinus Torvalds 	INIT_LIST_HEAD(&anon_vma->head);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds void __init anon_vma_init(void)
1901da177e4SLinus Torvalds {
1911da177e4SLinus Torvalds 	anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
19220c2df83SPaul Mundt 			0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor);
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds /*
1961da177e4SLinus Torvalds  * Getting a lock on a stable anon_vma from a page off the LRU is
1971da177e4SLinus Torvalds  * tricky: page_lock_anon_vma rely on RCU to guard against the races.
1981da177e4SLinus Torvalds  */
19910be22dfSAndi Kleen struct anon_vma *page_lock_anon_vma(struct page *page)
2001da177e4SLinus Torvalds {
20134bbd704SOleg Nesterov 	struct anon_vma *anon_vma;
2021da177e4SLinus Torvalds 	unsigned long anon_mapping;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	rcu_read_lock();
2051da177e4SLinus Torvalds 	anon_mapping = (unsigned long) page->mapping;
206*3ca7b3c5SHugh Dickins 	if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
2071da177e4SLinus Torvalds 		goto out;
2081da177e4SLinus Torvalds 	if (!page_mapped(page))
2091da177e4SLinus Torvalds 		goto out;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
2121da177e4SLinus Torvalds 	spin_lock(&anon_vma->lock);
21334bbd704SOleg Nesterov 	return anon_vma;
2141da177e4SLinus Torvalds out:
2151da177e4SLinus Torvalds 	rcu_read_unlock();
21634bbd704SOleg Nesterov 	return NULL;
21734bbd704SOleg Nesterov }
21834bbd704SOleg Nesterov 
21910be22dfSAndi Kleen void page_unlock_anon_vma(struct anon_vma *anon_vma)
22034bbd704SOleg Nesterov {
22134bbd704SOleg Nesterov 	spin_unlock(&anon_vma->lock);
22234bbd704SOleg Nesterov 	rcu_read_unlock();
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds /*
2263ad33b24SLee Schermerhorn  * At what user virtual address is page expected in @vma?
2273ad33b24SLee Schermerhorn  * Returns virtual address or -EFAULT if page's index/offset is not
2283ad33b24SLee Schermerhorn  * within the range mapped the @vma.
2291da177e4SLinus Torvalds  */
2301da177e4SLinus Torvalds static inline unsigned long
2311da177e4SLinus Torvalds vma_address(struct page *page, struct vm_area_struct *vma)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
2341da177e4SLinus Torvalds 	unsigned long address;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
2371da177e4SLinus Torvalds 	if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
2383ad33b24SLee Schermerhorn 		/* page should be within @vma mapping range */
2391da177e4SLinus Torvalds 		return -EFAULT;
2401da177e4SLinus Torvalds 	}
2411da177e4SLinus Torvalds 	return address;
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds /*
245bf89c8c8SHuang Shijie  * At what user virtual address is page expected in vma?
246bf89c8c8SHuang Shijie  * checking that the page matches the vma.
2471da177e4SLinus Torvalds  */
2481da177e4SLinus Torvalds unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	if (PageAnon(page)) {
251*3ca7b3c5SHugh Dickins 		if (vma->anon_vma != page_anon_vma(page))
2521da177e4SLinus Torvalds 			return -EFAULT;
2531da177e4SLinus Torvalds 	} else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) {
254ee498ed7SHugh Dickins 		if (!vma->vm_file ||
255ee498ed7SHugh Dickins 		    vma->vm_file->f_mapping != page->mapping)
2561da177e4SLinus Torvalds 			return -EFAULT;
2571da177e4SLinus Torvalds 	} else
2581da177e4SLinus Torvalds 		return -EFAULT;
2591da177e4SLinus Torvalds 	return vma_address(page, vma);
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds /*
26381b4082dSNikita Danilov  * Check that @page is mapped at @address into @mm.
26481b4082dSNikita Danilov  *
265479db0bfSNick Piggin  * If @sync is false, page_check_address may perform a racy check to avoid
266479db0bfSNick Piggin  * the page table lock when the pte is not present (helpful when reclaiming
267479db0bfSNick Piggin  * highly shared pages).
268479db0bfSNick Piggin  *
269b8072f09SHugh Dickins  * On success returns with pte mapped and locked.
27081b4082dSNikita Danilov  */
271ceffc078SCarsten Otte pte_t *page_check_address(struct page *page, struct mm_struct *mm,
272479db0bfSNick Piggin 			  unsigned long address, spinlock_t **ptlp, int sync)
27381b4082dSNikita Danilov {
27481b4082dSNikita Danilov 	pgd_t *pgd;
27581b4082dSNikita Danilov 	pud_t *pud;
27681b4082dSNikita Danilov 	pmd_t *pmd;
27781b4082dSNikita Danilov 	pte_t *pte;
278c0718806SHugh Dickins 	spinlock_t *ptl;
27981b4082dSNikita Danilov 
28081b4082dSNikita Danilov 	pgd = pgd_offset(mm, address);
281c0718806SHugh Dickins 	if (!pgd_present(*pgd))
282c0718806SHugh Dickins 		return NULL;
283c0718806SHugh Dickins 
28481b4082dSNikita Danilov 	pud = pud_offset(pgd, address);
285c0718806SHugh Dickins 	if (!pud_present(*pud))
286c0718806SHugh Dickins 		return NULL;
287c0718806SHugh Dickins 
28881b4082dSNikita Danilov 	pmd = pmd_offset(pud, address);
289c0718806SHugh Dickins 	if (!pmd_present(*pmd))
290c0718806SHugh Dickins 		return NULL;
291c0718806SHugh Dickins 
29281b4082dSNikita Danilov 	pte = pte_offset_map(pmd, address);
293c0718806SHugh Dickins 	/* Make a quick check before getting the lock */
294479db0bfSNick Piggin 	if (!sync && !pte_present(*pte)) {
29581b4082dSNikita Danilov 		pte_unmap(pte);
296c0718806SHugh Dickins 		return NULL;
29781b4082dSNikita Danilov 	}
298c0718806SHugh Dickins 
2994c21e2f2SHugh Dickins 	ptl = pte_lockptr(mm, pmd);
300c0718806SHugh Dickins 	spin_lock(ptl);
301c0718806SHugh Dickins 	if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
302c0718806SHugh Dickins 		*ptlp = ptl;
303c0718806SHugh Dickins 		return pte;
30481b4082dSNikita Danilov 	}
305c0718806SHugh Dickins 	pte_unmap_unlock(pte, ptl);
306c0718806SHugh Dickins 	return NULL;
30781b4082dSNikita Danilov }
30881b4082dSNikita Danilov 
309b291f000SNick Piggin /**
310b291f000SNick Piggin  * page_mapped_in_vma - check whether a page is really mapped in a VMA
311b291f000SNick Piggin  * @page: the page to test
312b291f000SNick Piggin  * @vma: the VMA to test
313b291f000SNick Piggin  *
314b291f000SNick Piggin  * Returns 1 if the page is mapped into the page tables of the VMA, 0
315b291f000SNick Piggin  * if the page is not mapped into the page tables of this VMA.  Only
316b291f000SNick Piggin  * valid for normal file or anonymous VMAs.
317b291f000SNick Piggin  */
3186a46079cSAndi Kleen int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma)
319b291f000SNick Piggin {
320b291f000SNick Piggin 	unsigned long address;
321b291f000SNick Piggin 	pte_t *pte;
322b291f000SNick Piggin 	spinlock_t *ptl;
323b291f000SNick Piggin 
324b291f000SNick Piggin 	address = vma_address(page, vma);
325b291f000SNick Piggin 	if (address == -EFAULT)		/* out of vma range */
326b291f000SNick Piggin 		return 0;
327b291f000SNick Piggin 	pte = page_check_address(page, vma->vm_mm, address, &ptl, 1);
328b291f000SNick Piggin 	if (!pte)			/* the page is not in this mm */
329b291f000SNick Piggin 		return 0;
330b291f000SNick Piggin 	pte_unmap_unlock(pte, ptl);
331b291f000SNick Piggin 
332b291f000SNick Piggin 	return 1;
333b291f000SNick Piggin }
334b291f000SNick Piggin 
33581b4082dSNikita Danilov /*
3361da177e4SLinus Torvalds  * Subfunctions of page_referenced: page_referenced_one called
3371da177e4SLinus Torvalds  * repeatedly from either page_referenced_anon or page_referenced_file.
3381da177e4SLinus Torvalds  */
3391da177e4SLinus Torvalds static int page_referenced_one(struct page *page,
3406fe6b7e3SWu Fengguang 			       struct vm_area_struct *vma,
3416fe6b7e3SWu Fengguang 			       unsigned int *mapcount,
3426fe6b7e3SWu Fengguang 			       unsigned long *vm_flags)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
3451da177e4SLinus Torvalds 	unsigned long address;
3461da177e4SLinus Torvalds 	pte_t *pte;
347c0718806SHugh Dickins 	spinlock_t *ptl;
3481da177e4SLinus Torvalds 	int referenced = 0;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	address = vma_address(page, vma);
3511da177e4SLinus Torvalds 	if (address == -EFAULT)
3521da177e4SLinus Torvalds 		goto out;
3531da177e4SLinus Torvalds 
354479db0bfSNick Piggin 	pte = page_check_address(page, mm, address, &ptl, 0);
355c0718806SHugh Dickins 	if (!pte)
356c0718806SHugh Dickins 		goto out;
357c0718806SHugh Dickins 
358b291f000SNick Piggin 	/*
359b291f000SNick Piggin 	 * Don't want to elevate referenced for mlocked page that gets this far,
360b291f000SNick Piggin 	 * in order that it progresses to try_to_unmap and is moved to the
361b291f000SNick Piggin 	 * unevictable list.
362b291f000SNick Piggin 	 */
3635a9bbdcdSHugh Dickins 	if (vma->vm_flags & VM_LOCKED) {
3645a9bbdcdSHugh Dickins 		*mapcount = 1;	/* break early from loop */
36503ef83afSMinchan Kim 		*vm_flags |= VM_LOCKED;
366b291f000SNick Piggin 		goto out_unmap;
367b291f000SNick Piggin 	}
368b291f000SNick Piggin 
3694917e5d0SJohannes Weiner 	if (ptep_clear_flush_young_notify(vma, address, pte)) {
3704917e5d0SJohannes Weiner 		/*
3714917e5d0SJohannes Weiner 		 * Don't treat a reference through a sequentially read
3724917e5d0SJohannes Weiner 		 * mapping as such.  If the page has been used in
3734917e5d0SJohannes Weiner 		 * another mapping, we will catch it; if this other
3744917e5d0SJohannes Weiner 		 * mapping is already gone, the unmap path will have
3754917e5d0SJohannes Weiner 		 * set PG_referenced or activated the page.
3764917e5d0SJohannes Weiner 		 */
3774917e5d0SJohannes Weiner 		if (likely(!VM_SequentialReadHint(vma)))
3781da177e4SLinus Torvalds 			referenced++;
3794917e5d0SJohannes Weiner 	}
3801da177e4SLinus Torvalds 
381fcdae29aSRik Van Riel 	/* Pretend the page is referenced if the task has the
382fcdae29aSRik Van Riel 	   swap token and is in the middle of a page fault. */
383f7b7fd8fSRik van Riel 	if (mm != current->mm && has_swap_token(mm) &&
384fcdae29aSRik Van Riel 			rwsem_is_locked(&mm->mmap_sem))
3851da177e4SLinus Torvalds 		referenced++;
3861da177e4SLinus Torvalds 
387b291f000SNick Piggin out_unmap:
3881da177e4SLinus Torvalds 	(*mapcount)--;
389c0718806SHugh Dickins 	pte_unmap_unlock(pte, ptl);
390273f047eSHuang Shijie 
3916fe6b7e3SWu Fengguang 	if (referenced)
3926fe6b7e3SWu Fengguang 		*vm_flags |= vma->vm_flags;
393273f047eSHuang Shijie out:
3941da177e4SLinus Torvalds 	return referenced;
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
397bed7161aSBalbir Singh static int page_referenced_anon(struct page *page,
3986fe6b7e3SWu Fengguang 				struct mem_cgroup *mem_cont,
3996fe6b7e3SWu Fengguang 				unsigned long *vm_flags)
4001da177e4SLinus Torvalds {
4011da177e4SLinus Torvalds 	unsigned int mapcount;
4021da177e4SLinus Torvalds 	struct anon_vma *anon_vma;
4031da177e4SLinus Torvalds 	struct vm_area_struct *vma;
4041da177e4SLinus Torvalds 	int referenced = 0;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	anon_vma = page_lock_anon_vma(page);
4071da177e4SLinus Torvalds 	if (!anon_vma)
4081da177e4SLinus Torvalds 		return referenced;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	mapcount = page_mapcount(page);
4111da177e4SLinus Torvalds 	list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
412bed7161aSBalbir Singh 		/*
413bed7161aSBalbir Singh 		 * If we are reclaiming on behalf of a cgroup, skip
414bed7161aSBalbir Singh 		 * counting on behalf of references from different
415bed7161aSBalbir Singh 		 * cgroups
416bed7161aSBalbir Singh 		 */
417bd845e38SHugh Dickins 		if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
418bed7161aSBalbir Singh 			continue;
4196fe6b7e3SWu Fengguang 		referenced += page_referenced_one(page, vma,
4206fe6b7e3SWu Fengguang 						  &mapcount, vm_flags);
4211da177e4SLinus Torvalds 		if (!mapcount)
4221da177e4SLinus Torvalds 			break;
4231da177e4SLinus Torvalds 	}
42434bbd704SOleg Nesterov 
42534bbd704SOleg Nesterov 	page_unlock_anon_vma(anon_vma);
4261da177e4SLinus Torvalds 	return referenced;
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds /**
4301da177e4SLinus Torvalds  * page_referenced_file - referenced check for object-based rmap
4311da177e4SLinus Torvalds  * @page: the page we're checking references on.
43243d8eac4SRandy Dunlap  * @mem_cont: target memory controller
4336fe6b7e3SWu Fengguang  * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
4341da177e4SLinus Torvalds  *
4351da177e4SLinus Torvalds  * For an object-based mapped page, find all the places it is mapped and
4361da177e4SLinus Torvalds  * check/clear the referenced flag.  This is done by following the page->mapping
4371da177e4SLinus Torvalds  * pointer, then walking the chain of vmas it holds.  It returns the number
4381da177e4SLinus Torvalds  * of references it found.
4391da177e4SLinus Torvalds  *
4401da177e4SLinus Torvalds  * This function is only called from page_referenced for object-based pages.
4411da177e4SLinus Torvalds  */
442bed7161aSBalbir Singh static int page_referenced_file(struct page *page,
4436fe6b7e3SWu Fengguang 				struct mem_cgroup *mem_cont,
4446fe6b7e3SWu Fengguang 				unsigned long *vm_flags)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	unsigned int mapcount;
4471da177e4SLinus Torvalds 	struct address_space *mapping = page->mapping;
4481da177e4SLinus Torvalds 	pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
4491da177e4SLinus Torvalds 	struct vm_area_struct *vma;
4501da177e4SLinus Torvalds 	struct prio_tree_iter iter;
4511da177e4SLinus Torvalds 	int referenced = 0;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	/*
4541da177e4SLinus Torvalds 	 * The caller's checks on page->mapping and !PageAnon have made
4551da177e4SLinus Torvalds 	 * sure that this is a file page: the check for page->mapping
4561da177e4SLinus Torvalds 	 * excludes the case just before it gets set on an anon page.
4571da177e4SLinus Torvalds 	 */
4581da177e4SLinus Torvalds 	BUG_ON(PageAnon(page));
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	/*
4611da177e4SLinus Torvalds 	 * The page lock not only makes sure that page->mapping cannot
4621da177e4SLinus Torvalds 	 * suddenly be NULLified by truncation, it makes sure that the
4631da177e4SLinus Torvalds 	 * structure at mapping cannot be freed and reused yet,
4641da177e4SLinus Torvalds 	 * so we can safely take mapping->i_mmap_lock.
4651da177e4SLinus Torvalds 	 */
4661da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	spin_lock(&mapping->i_mmap_lock);
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	/*
4711da177e4SLinus Torvalds 	 * i_mmap_lock does not stabilize mapcount at all, but mapcount
4721da177e4SLinus Torvalds 	 * is more likely to be accurate if we note it after spinning.
4731da177e4SLinus Torvalds 	 */
4741da177e4SLinus Torvalds 	mapcount = page_mapcount(page);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
477bed7161aSBalbir Singh 		/*
478bed7161aSBalbir Singh 		 * If we are reclaiming on behalf of a cgroup, skip
479bed7161aSBalbir Singh 		 * counting on behalf of references from different
480bed7161aSBalbir Singh 		 * cgroups
481bed7161aSBalbir Singh 		 */
482bd845e38SHugh Dickins 		if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
483bed7161aSBalbir Singh 			continue;
4846fe6b7e3SWu Fengguang 		referenced += page_referenced_one(page, vma,
4856fe6b7e3SWu Fengguang 						  &mapcount, vm_flags);
4861da177e4SLinus Torvalds 		if (!mapcount)
4871da177e4SLinus Torvalds 			break;
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	spin_unlock(&mapping->i_mmap_lock);
4911da177e4SLinus Torvalds 	return referenced;
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds /**
4951da177e4SLinus Torvalds  * page_referenced - test if the page was referenced
4961da177e4SLinus Torvalds  * @page: the page to test
4971da177e4SLinus Torvalds  * @is_locked: caller holds lock on the page
49843d8eac4SRandy Dunlap  * @mem_cont: target memory controller
4996fe6b7e3SWu Fengguang  * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
5001da177e4SLinus Torvalds  *
5011da177e4SLinus Torvalds  * Quick test_and_clear_referenced for all mappings to a page,
5021da177e4SLinus Torvalds  * returns the number of ptes which referenced the page.
5031da177e4SLinus Torvalds  */
5046fe6b7e3SWu Fengguang int page_referenced(struct page *page,
5056fe6b7e3SWu Fengguang 		    int is_locked,
5066fe6b7e3SWu Fengguang 		    struct mem_cgroup *mem_cont,
5076fe6b7e3SWu Fengguang 		    unsigned long *vm_flags)
5081da177e4SLinus Torvalds {
5091da177e4SLinus Torvalds 	int referenced = 0;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	if (TestClearPageReferenced(page))
5121da177e4SLinus Torvalds 		referenced++;
5131da177e4SLinus Torvalds 
5146fe6b7e3SWu Fengguang 	*vm_flags = 0;
515*3ca7b3c5SHugh Dickins 	if (page_mapped(page) && page_rmapping(page)) {
5161da177e4SLinus Torvalds 		if (PageAnon(page))
5176fe6b7e3SWu Fengguang 			referenced += page_referenced_anon(page, mem_cont,
5186fe6b7e3SWu Fengguang 								vm_flags);
5191da177e4SLinus Torvalds 		else if (is_locked)
5206fe6b7e3SWu Fengguang 			referenced += page_referenced_file(page, mem_cont,
5216fe6b7e3SWu Fengguang 								vm_flags);
522529ae9aaSNick Piggin 		else if (!trylock_page(page))
5231da177e4SLinus Torvalds 			referenced++;
5241da177e4SLinus Torvalds 		else {
5251da177e4SLinus Torvalds 			if (page->mapping)
5266fe6b7e3SWu Fengguang 				referenced += page_referenced_file(page,
5276fe6b7e3SWu Fengguang 							mem_cont, vm_flags);
5281da177e4SLinus Torvalds 			unlock_page(page);
5291da177e4SLinus Torvalds 		}
5301da177e4SLinus Torvalds 	}
5315b7baf05SChristian Borntraeger 
5325b7baf05SChristian Borntraeger 	if (page_test_and_clear_young(page))
5335b7baf05SChristian Borntraeger 		referenced++;
5345b7baf05SChristian Borntraeger 
5351da177e4SLinus Torvalds 	return referenced;
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
538d08b3851SPeter Zijlstra static int page_mkclean_one(struct page *page, struct vm_area_struct *vma)
539d08b3851SPeter Zijlstra {
540d08b3851SPeter Zijlstra 	struct mm_struct *mm = vma->vm_mm;
541d08b3851SPeter Zijlstra 	unsigned long address;
542c2fda5feSPeter Zijlstra 	pte_t *pte;
543d08b3851SPeter Zijlstra 	spinlock_t *ptl;
544d08b3851SPeter Zijlstra 	int ret = 0;
545d08b3851SPeter Zijlstra 
546d08b3851SPeter Zijlstra 	address = vma_address(page, vma);
547d08b3851SPeter Zijlstra 	if (address == -EFAULT)
548d08b3851SPeter Zijlstra 		goto out;
549d08b3851SPeter Zijlstra 
550479db0bfSNick Piggin 	pte = page_check_address(page, mm, address, &ptl, 1);
551d08b3851SPeter Zijlstra 	if (!pte)
552d08b3851SPeter Zijlstra 		goto out;
553d08b3851SPeter Zijlstra 
554c2fda5feSPeter Zijlstra 	if (pte_dirty(*pte) || pte_write(*pte)) {
555c2fda5feSPeter Zijlstra 		pte_t entry;
556d08b3851SPeter Zijlstra 
557c2fda5feSPeter Zijlstra 		flush_cache_page(vma, address, pte_pfn(*pte));
558cddb8a5cSAndrea Arcangeli 		entry = ptep_clear_flush_notify(vma, address, pte);
559d08b3851SPeter Zijlstra 		entry = pte_wrprotect(entry);
560c2fda5feSPeter Zijlstra 		entry = pte_mkclean(entry);
561d6e88e67SAl Viro 		set_pte_at(mm, address, pte, entry);
562d08b3851SPeter Zijlstra 		ret = 1;
563c2fda5feSPeter Zijlstra 	}
564d08b3851SPeter Zijlstra 
565d08b3851SPeter Zijlstra 	pte_unmap_unlock(pte, ptl);
566d08b3851SPeter Zijlstra out:
567d08b3851SPeter Zijlstra 	return ret;
568d08b3851SPeter Zijlstra }
569d08b3851SPeter Zijlstra 
570d08b3851SPeter Zijlstra static int page_mkclean_file(struct address_space *mapping, struct page *page)
571d08b3851SPeter Zijlstra {
572d08b3851SPeter Zijlstra 	pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
573d08b3851SPeter Zijlstra 	struct vm_area_struct *vma;
574d08b3851SPeter Zijlstra 	struct prio_tree_iter iter;
575d08b3851SPeter Zijlstra 	int ret = 0;
576d08b3851SPeter Zijlstra 
577d08b3851SPeter Zijlstra 	BUG_ON(PageAnon(page));
578d08b3851SPeter Zijlstra 
579d08b3851SPeter Zijlstra 	spin_lock(&mapping->i_mmap_lock);
580d08b3851SPeter Zijlstra 	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
581d08b3851SPeter Zijlstra 		if (vma->vm_flags & VM_SHARED)
582d08b3851SPeter Zijlstra 			ret += page_mkclean_one(page, vma);
583d08b3851SPeter Zijlstra 	}
584d08b3851SPeter Zijlstra 	spin_unlock(&mapping->i_mmap_lock);
585d08b3851SPeter Zijlstra 	return ret;
586d08b3851SPeter Zijlstra }
587d08b3851SPeter Zijlstra 
588d08b3851SPeter Zijlstra int page_mkclean(struct page *page)
589d08b3851SPeter Zijlstra {
590d08b3851SPeter Zijlstra 	int ret = 0;
591d08b3851SPeter Zijlstra 
592d08b3851SPeter Zijlstra 	BUG_ON(!PageLocked(page));
593d08b3851SPeter Zijlstra 
594d08b3851SPeter Zijlstra 	if (page_mapped(page)) {
595d08b3851SPeter Zijlstra 		struct address_space *mapping = page_mapping(page);
596ce7e9faeSChristian Borntraeger 		if (mapping) {
597d08b3851SPeter Zijlstra 			ret = page_mkclean_file(mapping, page);
5986c210482SMartin Schwidefsky 			if (page_test_dirty(page)) {
5996c210482SMartin Schwidefsky 				page_clear_dirty(page);
600c2fda5feSPeter Zijlstra 				ret = 1;
6016e1beb3cSMartin Schwidefsky 			}
6026c210482SMartin Schwidefsky 		}
603ce7e9faeSChristian Borntraeger 	}
604d08b3851SPeter Zijlstra 
605d08b3851SPeter Zijlstra 	return ret;
606d08b3851SPeter Zijlstra }
60760b59beaSJaya Kumar EXPORT_SYMBOL_GPL(page_mkclean);
608d08b3851SPeter Zijlstra 
6091da177e4SLinus Torvalds /**
61043d8eac4SRandy Dunlap  * __page_set_anon_rmap - setup new anonymous rmap
6111da177e4SLinus Torvalds  * @page:	the page to add the mapping to
6121da177e4SLinus Torvalds  * @vma:	the vm area in which the mapping is added
6131da177e4SLinus Torvalds  * @address:	the user virtual address mapped
6141da177e4SLinus Torvalds  */
6159617d95eSNick Piggin static void __page_set_anon_rmap(struct page *page,
6161da177e4SLinus Torvalds 	struct vm_area_struct *vma, unsigned long address)
6171da177e4SLinus Torvalds {
6182822c1aaSNick Piggin 	struct anon_vma *anon_vma = vma->anon_vma;
6192822c1aaSNick Piggin 
6202822c1aaSNick Piggin 	BUG_ON(!anon_vma);
6211da177e4SLinus Torvalds 	anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
6222822c1aaSNick Piggin 	page->mapping = (struct address_space *) anon_vma;
6232822c1aaSNick Piggin 
6244d7670e0SNick Piggin 	page->index = linear_page_index(vma, address);
6252822c1aaSNick Piggin 
626a74609faSNick Piggin 	/*
627a74609faSNick Piggin 	 * nr_mapped state can be updated without turning off
628a74609faSNick Piggin 	 * interrupts because it is not modified via interrupt.
629a74609faSNick Piggin 	 */
630f3dbd344SChristoph Lameter 	__inc_zone_page_state(page, NR_ANON_PAGES);
6311da177e4SLinus Torvalds }
6329617d95eSNick Piggin 
6339617d95eSNick Piggin /**
63443d8eac4SRandy Dunlap  * __page_check_anon_rmap - sanity check anonymous rmap addition
635c97a9e10SNick Piggin  * @page:	the page to add the mapping to
636c97a9e10SNick Piggin  * @vma:	the vm area in which the mapping is added
637c97a9e10SNick Piggin  * @address:	the user virtual address mapped
638c97a9e10SNick Piggin  */
639c97a9e10SNick Piggin static void __page_check_anon_rmap(struct page *page,
640c97a9e10SNick Piggin 	struct vm_area_struct *vma, unsigned long address)
641c97a9e10SNick Piggin {
642c97a9e10SNick Piggin #ifdef CONFIG_DEBUG_VM
643c97a9e10SNick Piggin 	/*
644c97a9e10SNick Piggin 	 * The page's anon-rmap details (mapping and index) are guaranteed to
645c97a9e10SNick Piggin 	 * be set up correctly at this point.
646c97a9e10SNick Piggin 	 *
647c97a9e10SNick Piggin 	 * We have exclusion against page_add_anon_rmap because the caller
648c97a9e10SNick Piggin 	 * always holds the page locked, except if called from page_dup_rmap,
649c97a9e10SNick Piggin 	 * in which case the page is already known to be setup.
650c97a9e10SNick Piggin 	 *
651c97a9e10SNick Piggin 	 * We have exclusion against page_add_new_anon_rmap because those pages
652c97a9e10SNick Piggin 	 * are initially only visible via the pagetables, and the pte is locked
653c97a9e10SNick Piggin 	 * over the call to page_add_new_anon_rmap.
654c97a9e10SNick Piggin 	 */
655c97a9e10SNick Piggin 	struct anon_vma *anon_vma = vma->anon_vma;
656c97a9e10SNick Piggin 	anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
657c97a9e10SNick Piggin 	BUG_ON(page->mapping != (struct address_space *)anon_vma);
658c97a9e10SNick Piggin 	BUG_ON(page->index != linear_page_index(vma, address));
659c97a9e10SNick Piggin #endif
660c97a9e10SNick Piggin }
661c97a9e10SNick Piggin 
662c97a9e10SNick Piggin /**
6639617d95eSNick Piggin  * page_add_anon_rmap - add pte mapping to an anonymous page
6649617d95eSNick Piggin  * @page:	the page to add the mapping to
6659617d95eSNick Piggin  * @vma:	the vm area in which the mapping is added
6669617d95eSNick Piggin  * @address:	the user virtual address mapped
6679617d95eSNick Piggin  *
668c97a9e10SNick Piggin  * The caller needs to hold the pte lock and the page must be locked.
6699617d95eSNick Piggin  */
6709617d95eSNick Piggin void page_add_anon_rmap(struct page *page,
6719617d95eSNick Piggin 	struct vm_area_struct *vma, unsigned long address)
6729617d95eSNick Piggin {
673c97a9e10SNick Piggin 	VM_BUG_ON(!PageLocked(page));
674c97a9e10SNick Piggin 	VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
6759617d95eSNick Piggin 	if (atomic_inc_and_test(&page->_mapcount))
6769617d95eSNick Piggin 		__page_set_anon_rmap(page, vma, address);
67769029cd5SKAMEZAWA Hiroyuki 	else
678c97a9e10SNick Piggin 		__page_check_anon_rmap(page, vma, address);
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
68143d8eac4SRandy Dunlap /**
6829617d95eSNick Piggin  * page_add_new_anon_rmap - add pte mapping to a new anonymous page
6839617d95eSNick Piggin  * @page:	the page to add the mapping to
6849617d95eSNick Piggin  * @vma:	the vm area in which the mapping is added
6859617d95eSNick Piggin  * @address:	the user virtual address mapped
6869617d95eSNick Piggin  *
6879617d95eSNick Piggin  * Same as page_add_anon_rmap but must only be called on *new* pages.
6889617d95eSNick Piggin  * This means the inc-and-test can be bypassed.
689c97a9e10SNick Piggin  * Page does not have to be locked.
6909617d95eSNick Piggin  */
6919617d95eSNick Piggin void page_add_new_anon_rmap(struct page *page,
6929617d95eSNick Piggin 	struct vm_area_struct *vma, unsigned long address)
6939617d95eSNick Piggin {
694b5934c53SHugh Dickins 	VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
695cbf84b7aSHugh Dickins 	SetPageSwapBacked(page);
696cbf84b7aSHugh Dickins 	atomic_set(&page->_mapcount, 0); /* increment count (starts at -1) */
6979617d95eSNick Piggin 	__page_set_anon_rmap(page, vma, address);
698b5934c53SHugh Dickins 	if (page_evictable(page, vma))
699cbf84b7aSHugh Dickins 		lru_cache_add_lru(page, LRU_ACTIVE_ANON);
700b5934c53SHugh Dickins 	else
701b5934c53SHugh Dickins 		add_page_to_unevictable_list(page);
7029617d95eSNick Piggin }
7039617d95eSNick Piggin 
7041da177e4SLinus Torvalds /**
7051da177e4SLinus Torvalds  * page_add_file_rmap - add pte mapping to a file page
7061da177e4SLinus Torvalds  * @page: the page to add the mapping to
7071da177e4SLinus Torvalds  *
708b8072f09SHugh Dickins  * The caller needs to hold the pte lock.
7091da177e4SLinus Torvalds  */
7101da177e4SLinus Torvalds void page_add_file_rmap(struct page *page)
7111da177e4SLinus Torvalds {
712d69b042fSBalbir Singh 	if (atomic_inc_and_test(&page->_mapcount)) {
71365ba55f5SChristoph Lameter 		__inc_zone_page_state(page, NR_FILE_MAPPED);
714d69b042fSBalbir Singh 		mem_cgroup_update_mapped_file_stat(page, 1);
715d69b042fSBalbir Singh 	}
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds /**
7191da177e4SLinus Torvalds  * page_remove_rmap - take down pte mapping from a page
7201da177e4SLinus Torvalds  * @page: page to remove mapping from
7211da177e4SLinus Torvalds  *
722b8072f09SHugh Dickins  * The caller needs to hold the pte lock.
7231da177e4SLinus Torvalds  */
724edc315fdSHugh Dickins void page_remove_rmap(struct page *page)
7251da177e4SLinus Torvalds {
726b904dcfeSKOSAKI Motohiro 	/* page still mapped by someone else? */
727b904dcfeSKOSAKI Motohiro 	if (!atomic_add_negative(-1, &page->_mapcount))
728b904dcfeSKOSAKI Motohiro 		return;
729b904dcfeSKOSAKI Motohiro 
7301da177e4SLinus Torvalds 	/*
73116f8c5b2SHugh Dickins 	 * Now that the last pte has gone, s390 must transfer dirty
73216f8c5b2SHugh Dickins 	 * flag from storage key to struct page.  We can usually skip
73316f8c5b2SHugh Dickins 	 * this if the page is anon, so about to be freed; but perhaps
73416f8c5b2SHugh Dickins 	 * not if it's in swapcache - there might be another pte slot
73516f8c5b2SHugh Dickins 	 * containing the swap entry, but page not yet written to swap.
73616f8c5b2SHugh Dickins 	 */
737b904dcfeSKOSAKI Motohiro 	if ((!PageAnon(page) || PageSwapCache(page)) && page_test_dirty(page)) {
73816f8c5b2SHugh Dickins 		page_clear_dirty(page);
73916f8c5b2SHugh Dickins 		set_page_dirty(page);
74016f8c5b2SHugh Dickins 	}
741b904dcfeSKOSAKI Motohiro 	if (PageAnon(page)) {
74216f8c5b2SHugh Dickins 		mem_cgroup_uncharge_page(page);
743b904dcfeSKOSAKI Motohiro 		__dec_zone_page_state(page, NR_ANON_PAGES);
744b904dcfeSKOSAKI Motohiro 	} else {
745b904dcfeSKOSAKI Motohiro 		__dec_zone_page_state(page, NR_FILE_MAPPED);
746b904dcfeSKOSAKI Motohiro 	}
747d69b042fSBalbir Singh 	mem_cgroup_update_mapped_file_stat(page, -1);
74816f8c5b2SHugh Dickins 	/*
7491da177e4SLinus Torvalds 	 * It would be tidy to reset the PageAnon mapping here,
7501da177e4SLinus Torvalds 	 * but that might overwrite a racing page_add_anon_rmap
7511da177e4SLinus Torvalds 	 * which increments mapcount after us but sets mapping
7521da177e4SLinus Torvalds 	 * before us: so leave the reset to free_hot_cold_page,
7531da177e4SLinus Torvalds 	 * and remember that it's only reliable while mapped.
7541da177e4SLinus Torvalds 	 * Leaving it set also helps swapoff to reinstate ptes
7551da177e4SLinus Torvalds 	 * faster for those pages still in swapcache.
7561da177e4SLinus Torvalds 	 */
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds /*
7601da177e4SLinus Torvalds  * Subfunctions of try_to_unmap: try_to_unmap_one called
7611da177e4SLinus Torvalds  * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
7621da177e4SLinus Torvalds  */
763a48d07afSChristoph Lameter static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
76414fa31b8SAndi Kleen 				enum ttu_flags flags)
7651da177e4SLinus Torvalds {
7661da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
7671da177e4SLinus Torvalds 	unsigned long address;
7681da177e4SLinus Torvalds 	pte_t *pte;
7691da177e4SLinus Torvalds 	pte_t pteval;
770c0718806SHugh Dickins 	spinlock_t *ptl;
7711da177e4SLinus Torvalds 	int ret = SWAP_AGAIN;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	address = vma_address(page, vma);
7741da177e4SLinus Torvalds 	if (address == -EFAULT)
7751da177e4SLinus Torvalds 		goto out;
7761da177e4SLinus Torvalds 
777479db0bfSNick Piggin 	pte = page_check_address(page, mm, address, &ptl, 0);
778c0718806SHugh Dickins 	if (!pte)
77981b4082dSNikita Danilov 		goto out;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	/*
7821da177e4SLinus Torvalds 	 * If the page is mlock()d, we cannot swap it out.
7831da177e4SLinus Torvalds 	 * If it's recently referenced (perhaps page_referenced
7841da177e4SLinus Torvalds 	 * skipped over this mm) then we should reactivate it.
7851da177e4SLinus Torvalds 	 */
78614fa31b8SAndi Kleen 	if (!(flags & TTU_IGNORE_MLOCK)) {
787b291f000SNick Piggin 		if (vma->vm_flags & VM_LOCKED) {
788b291f000SNick Piggin 			ret = SWAP_MLOCK;
789b291f000SNick Piggin 			goto out_unmap;
790b291f000SNick Piggin 		}
79114fa31b8SAndi Kleen 	}
79214fa31b8SAndi Kleen 	if (!(flags & TTU_IGNORE_ACCESS)) {
793b291f000SNick Piggin 		if (ptep_clear_flush_young_notify(vma, address, pte)) {
7941da177e4SLinus Torvalds 			ret = SWAP_FAIL;
7951da177e4SLinus Torvalds 			goto out_unmap;
7961da177e4SLinus Torvalds 		}
797b291f000SNick Piggin   	}
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	/* Nuke the page table entry. */
8001da177e4SLinus Torvalds 	flush_cache_page(vma, address, page_to_pfn(page));
801cddb8a5cSAndrea Arcangeli 	pteval = ptep_clear_flush_notify(vma, address, pte);
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	/* Move the dirty bit to the physical page now the pte is gone. */
8041da177e4SLinus Torvalds 	if (pte_dirty(pteval))
8051da177e4SLinus Torvalds 		set_page_dirty(page);
8061da177e4SLinus Torvalds 
807365e9c87SHugh Dickins 	/* Update high watermark before we lower rss */
808365e9c87SHugh Dickins 	update_hiwater_rss(mm);
809365e9c87SHugh Dickins 
810888b9f7cSAndi Kleen 	if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) {
811888b9f7cSAndi Kleen 		if (PageAnon(page))
812888b9f7cSAndi Kleen 			dec_mm_counter(mm, anon_rss);
813888b9f7cSAndi Kleen 		else
814888b9f7cSAndi Kleen 			dec_mm_counter(mm, file_rss);
815888b9f7cSAndi Kleen 		set_pte_at(mm, address, pte,
816888b9f7cSAndi Kleen 				swp_entry_to_pte(make_hwpoison_entry(page)));
817888b9f7cSAndi Kleen 	} else if (PageAnon(page)) {
8184c21e2f2SHugh Dickins 		swp_entry_t entry = { .val = page_private(page) };
8190697212aSChristoph Lameter 
8200697212aSChristoph Lameter 		if (PageSwapCache(page)) {
8211da177e4SLinus Torvalds 			/*
8221da177e4SLinus Torvalds 			 * Store the swap location in the pte.
8231da177e4SLinus Torvalds 			 * See handle_pte_fault() ...
8241da177e4SLinus Torvalds 			 */
825570a335bSHugh Dickins 			if (swap_duplicate(entry) < 0) {
826570a335bSHugh Dickins 				set_pte_at(mm, address, pte, pteval);
827570a335bSHugh Dickins 				ret = SWAP_FAIL;
828570a335bSHugh Dickins 				goto out_unmap;
829570a335bSHugh Dickins 			}
8301da177e4SLinus Torvalds 			if (list_empty(&mm->mmlist)) {
8311da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
832f412ac08SHugh Dickins 				if (list_empty(&mm->mmlist))
8331da177e4SLinus Torvalds 					list_add(&mm->mmlist, &init_mm.mmlist);
8341da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
8351da177e4SLinus Torvalds 			}
836442c9137SChristoph Lameter 			dec_mm_counter(mm, anon_rss);
83764cdd548SKOSAKI Motohiro 		} else if (PAGE_MIGRATION) {
8380697212aSChristoph Lameter 			/*
8390697212aSChristoph Lameter 			 * Store the pfn of the page in a special migration
8400697212aSChristoph Lameter 			 * pte. do_swap_page() will wait until the migration
8410697212aSChristoph Lameter 			 * pte is removed and then restart fault handling.
8420697212aSChristoph Lameter 			 */
84314fa31b8SAndi Kleen 			BUG_ON(TTU_ACTION(flags) != TTU_MIGRATION);
8440697212aSChristoph Lameter 			entry = make_migration_entry(page, pte_write(pteval));
8450697212aSChristoph Lameter 		}
8461da177e4SLinus Torvalds 		set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
8471da177e4SLinus Torvalds 		BUG_ON(pte_file(*pte));
84814fa31b8SAndi Kleen 	} else if (PAGE_MIGRATION && (TTU_ACTION(flags) == TTU_MIGRATION)) {
84904e62a29SChristoph Lameter 		/* Establish migration entry for a file page */
85004e62a29SChristoph Lameter 		swp_entry_t entry;
85104e62a29SChristoph Lameter 		entry = make_migration_entry(page, pte_write(pteval));
85204e62a29SChristoph Lameter 		set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
85304e62a29SChristoph Lameter 	} else
8544294621fSHugh Dickins 		dec_mm_counter(mm, file_rss);
8551da177e4SLinus Torvalds 
85604e62a29SChristoph Lameter 
857edc315fdSHugh Dickins 	page_remove_rmap(page);
8581da177e4SLinus Torvalds 	page_cache_release(page);
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds out_unmap:
861c0718806SHugh Dickins 	pte_unmap_unlock(pte, ptl);
8621da177e4SLinus Torvalds out:
8631da177e4SLinus Torvalds 	return ret;
8641da177e4SLinus Torvalds }
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds /*
8671da177e4SLinus Torvalds  * objrmap doesn't work for nonlinear VMAs because the assumption that
8681da177e4SLinus Torvalds  * offset-into-file correlates with offset-into-virtual-addresses does not hold.
8691da177e4SLinus Torvalds  * Consequently, given a particular page and its ->index, we cannot locate the
8701da177e4SLinus Torvalds  * ptes which are mapping that page without an exhaustive linear search.
8711da177e4SLinus Torvalds  *
8721da177e4SLinus Torvalds  * So what this code does is a mini "virtual scan" of each nonlinear VMA which
8731da177e4SLinus Torvalds  * maps the file to which the target page belongs.  The ->vm_private_data field
8741da177e4SLinus Torvalds  * holds the current cursor into that scan.  Successive searches will circulate
8751da177e4SLinus Torvalds  * around the vma's virtual address space.
8761da177e4SLinus Torvalds  *
8771da177e4SLinus Torvalds  * So as more replacement pressure is applied to the pages in a nonlinear VMA,
8781da177e4SLinus Torvalds  * more scanning pressure is placed against them as well.   Eventually pages
8791da177e4SLinus Torvalds  * will become fully unmapped and are eligible for eviction.
8801da177e4SLinus Torvalds  *
8811da177e4SLinus Torvalds  * For very sparsely populated VMAs this is a little inefficient - chances are
8821da177e4SLinus Torvalds  * there there won't be many ptes located within the scan cluster.  In this case
8831da177e4SLinus Torvalds  * maybe we could scan further - to the end of the pte page, perhaps.
884b291f000SNick Piggin  *
885b291f000SNick Piggin  * Mlocked pages:  check VM_LOCKED under mmap_sem held for read, if we can
886b291f000SNick Piggin  * acquire it without blocking.  If vma locked, mlock the pages in the cluster,
887b291f000SNick Piggin  * rather than unmapping them.  If we encounter the "check_page" that vmscan is
888b291f000SNick Piggin  * trying to unmap, return SWAP_MLOCK, else default SWAP_AGAIN.
8891da177e4SLinus Torvalds  */
8901da177e4SLinus Torvalds #define CLUSTER_SIZE	min(32*PAGE_SIZE, PMD_SIZE)
8911da177e4SLinus Torvalds #define CLUSTER_MASK	(~(CLUSTER_SIZE - 1))
8921da177e4SLinus Torvalds 
893b291f000SNick Piggin static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount,
894b291f000SNick Piggin 		struct vm_area_struct *vma, struct page *check_page)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
8971da177e4SLinus Torvalds 	pgd_t *pgd;
8981da177e4SLinus Torvalds 	pud_t *pud;
8991da177e4SLinus Torvalds 	pmd_t *pmd;
900c0718806SHugh Dickins 	pte_t *pte;
9011da177e4SLinus Torvalds 	pte_t pteval;
902c0718806SHugh Dickins 	spinlock_t *ptl;
9031da177e4SLinus Torvalds 	struct page *page;
9041da177e4SLinus Torvalds 	unsigned long address;
9051da177e4SLinus Torvalds 	unsigned long end;
906b291f000SNick Piggin 	int ret = SWAP_AGAIN;
907b291f000SNick Piggin 	int locked_vma = 0;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	address = (vma->vm_start + cursor) & CLUSTER_MASK;
9101da177e4SLinus Torvalds 	end = address + CLUSTER_SIZE;
9111da177e4SLinus Torvalds 	if (address < vma->vm_start)
9121da177e4SLinus Torvalds 		address = vma->vm_start;
9131da177e4SLinus Torvalds 	if (end > vma->vm_end)
9141da177e4SLinus Torvalds 		end = vma->vm_end;
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	pgd = pgd_offset(mm, address);
9171da177e4SLinus Torvalds 	if (!pgd_present(*pgd))
918b291f000SNick Piggin 		return ret;
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	pud = pud_offset(pgd, address);
9211da177e4SLinus Torvalds 	if (!pud_present(*pud))
922b291f000SNick Piggin 		return ret;
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	pmd = pmd_offset(pud, address);
9251da177e4SLinus Torvalds 	if (!pmd_present(*pmd))
926b291f000SNick Piggin 		return ret;
927b291f000SNick Piggin 
928b291f000SNick Piggin 	/*
929b291f000SNick Piggin 	 * MLOCK_PAGES => feature is configured.
930b291f000SNick Piggin 	 * if we can acquire the mmap_sem for read, and vma is VM_LOCKED,
931b291f000SNick Piggin 	 * keep the sem while scanning the cluster for mlocking pages.
932b291f000SNick Piggin 	 */
933b291f000SNick Piggin 	if (MLOCK_PAGES && down_read_trylock(&vma->vm_mm->mmap_sem)) {
934b291f000SNick Piggin 		locked_vma = (vma->vm_flags & VM_LOCKED);
935b291f000SNick Piggin 		if (!locked_vma)
936b291f000SNick Piggin 			up_read(&vma->vm_mm->mmap_sem); /* don't need it */
937b291f000SNick Piggin 	}
938c0718806SHugh Dickins 
939c0718806SHugh Dickins 	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
9401da177e4SLinus Torvalds 
941365e9c87SHugh Dickins 	/* Update high watermark before we lower rss */
942365e9c87SHugh Dickins 	update_hiwater_rss(mm);
943365e9c87SHugh Dickins 
944c0718806SHugh Dickins 	for (; address < end; pte++, address += PAGE_SIZE) {
9451da177e4SLinus Torvalds 		if (!pte_present(*pte))
9461da177e4SLinus Torvalds 			continue;
9476aab341eSLinus Torvalds 		page = vm_normal_page(vma, address, *pte);
9486aab341eSLinus Torvalds 		BUG_ON(!page || PageAnon(page));
9491da177e4SLinus Torvalds 
950b291f000SNick Piggin 		if (locked_vma) {
951b291f000SNick Piggin 			mlock_vma_page(page);   /* no-op if already mlocked */
952b291f000SNick Piggin 			if (page == check_page)
953b291f000SNick Piggin 				ret = SWAP_MLOCK;
954b291f000SNick Piggin 			continue;	/* don't unmap */
955b291f000SNick Piggin 		}
956b291f000SNick Piggin 
957cddb8a5cSAndrea Arcangeli 		if (ptep_clear_flush_young_notify(vma, address, pte))
9581da177e4SLinus Torvalds 			continue;
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 		/* Nuke the page table entry. */
961eca35133SBen Collins 		flush_cache_page(vma, address, pte_pfn(*pte));
962cddb8a5cSAndrea Arcangeli 		pteval = ptep_clear_flush_notify(vma, address, pte);
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 		/* If nonlinear, store the file page offset in the pte. */
9651da177e4SLinus Torvalds 		if (page->index != linear_page_index(vma, address))
9661da177e4SLinus Torvalds 			set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 		/* Move the dirty bit to the physical page now the pte is gone. */
9691da177e4SLinus Torvalds 		if (pte_dirty(pteval))
9701da177e4SLinus Torvalds 			set_page_dirty(page);
9711da177e4SLinus Torvalds 
972edc315fdSHugh Dickins 		page_remove_rmap(page);
9731da177e4SLinus Torvalds 		page_cache_release(page);
9744294621fSHugh Dickins 		dec_mm_counter(mm, file_rss);
9751da177e4SLinus Torvalds 		(*mapcount)--;
9761da177e4SLinus Torvalds 	}
977c0718806SHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
978b291f000SNick Piggin 	if (locked_vma)
979b291f000SNick Piggin 		up_read(&vma->vm_mm->mmap_sem);
980b291f000SNick Piggin 	return ret;
9811da177e4SLinus Torvalds }
9821da177e4SLinus Torvalds 
983b291f000SNick Piggin /*
984b291f000SNick Piggin  * common handling for pages mapped in VM_LOCKED vmas
985b291f000SNick Piggin  */
986b291f000SNick Piggin static int try_to_mlock_page(struct page *page, struct vm_area_struct *vma)
987b291f000SNick Piggin {
988b291f000SNick Piggin 	int mlocked = 0;
989b291f000SNick Piggin 
990b291f000SNick Piggin 	if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
991b291f000SNick Piggin 		if (vma->vm_flags & VM_LOCKED) {
992b291f000SNick Piggin 			mlock_vma_page(page);
993b291f000SNick Piggin 			mlocked++;	/* really mlocked the page */
994b291f000SNick Piggin 		}
995b291f000SNick Piggin 		up_read(&vma->vm_mm->mmap_sem);
996b291f000SNick Piggin 	}
997b291f000SNick Piggin 	return mlocked;
998b291f000SNick Piggin }
999b291f000SNick Piggin 
1000b291f000SNick Piggin /**
1001b291f000SNick Piggin  * try_to_unmap_anon - unmap or unlock anonymous page using the object-based
1002b291f000SNick Piggin  * rmap method
1003b291f000SNick Piggin  * @page: the page to unmap/unlock
10048051be5eSHuang Shijie  * @flags: action and flags
1005b291f000SNick Piggin  *
1006b291f000SNick Piggin  * Find all the mappings of a page using the mapping pointer and the vma chains
1007b291f000SNick Piggin  * contained in the anon_vma struct it points to.
1008b291f000SNick Piggin  *
1009b291f000SNick Piggin  * This function is only called from try_to_unmap/try_to_munlock for
1010b291f000SNick Piggin  * anonymous pages.
1011b291f000SNick Piggin  * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1012b291f000SNick Piggin  * where the page was found will be held for write.  So, we won't recheck
1013b291f000SNick Piggin  * vm_flags for that VMA.  That should be OK, because that vma shouldn't be
1014b291f000SNick Piggin  * 'LOCKED.
1015b291f000SNick Piggin  */
101614fa31b8SAndi Kleen static int try_to_unmap_anon(struct page *page, enum ttu_flags flags)
10171da177e4SLinus Torvalds {
10181da177e4SLinus Torvalds 	struct anon_vma *anon_vma;
10191da177e4SLinus Torvalds 	struct vm_area_struct *vma;
1020b291f000SNick Piggin 	unsigned int mlocked = 0;
10211da177e4SLinus Torvalds 	int ret = SWAP_AGAIN;
102214fa31b8SAndi Kleen 	int unlock = TTU_ACTION(flags) == TTU_MUNLOCK;
10231da177e4SLinus Torvalds 
1024b291f000SNick Piggin 	if (MLOCK_PAGES && unlikely(unlock))
1025b291f000SNick Piggin 		ret = SWAP_SUCCESS;	/* default for try_to_munlock() */
1026b291f000SNick Piggin 
10271da177e4SLinus Torvalds 	anon_vma = page_lock_anon_vma(page);
10281da177e4SLinus Torvalds 	if (!anon_vma)
10291da177e4SLinus Torvalds 		return ret;
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
1032b291f000SNick Piggin 		if (MLOCK_PAGES && unlikely(unlock)) {
1033b291f000SNick Piggin 			if (!((vma->vm_flags & VM_LOCKED) &&
1034b291f000SNick Piggin 			      page_mapped_in_vma(page, vma)))
1035b291f000SNick Piggin 				continue;  /* must visit all unlocked vmas */
1036b291f000SNick Piggin 			ret = SWAP_MLOCK;  /* saw at least one mlocked vma */
1037b291f000SNick Piggin 		} else {
103814fa31b8SAndi Kleen 			ret = try_to_unmap_one(page, vma, flags);
10391da177e4SLinus Torvalds 			if (ret == SWAP_FAIL || !page_mapped(page))
10401da177e4SLinus Torvalds 				break;
10411da177e4SLinus Torvalds 		}
1042b291f000SNick Piggin 		if (ret == SWAP_MLOCK) {
1043b291f000SNick Piggin 			mlocked = try_to_mlock_page(page, vma);
1044b291f000SNick Piggin 			if (mlocked)
1045b291f000SNick Piggin 				break;	/* stop if actually mlocked page */
1046b291f000SNick Piggin 		}
1047b291f000SNick Piggin 	}
104834bbd704SOleg Nesterov 
104934bbd704SOleg Nesterov 	page_unlock_anon_vma(anon_vma);
1050b291f000SNick Piggin 
1051b291f000SNick Piggin 	if (mlocked)
1052b291f000SNick Piggin 		ret = SWAP_MLOCK;	/* actually mlocked the page */
1053b291f000SNick Piggin 	else if (ret == SWAP_MLOCK)
1054b291f000SNick Piggin 		ret = SWAP_AGAIN;	/* saw VM_LOCKED vma */
1055b291f000SNick Piggin 
10561da177e4SLinus Torvalds 	return ret;
10571da177e4SLinus Torvalds }
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds /**
1060b291f000SNick Piggin  * try_to_unmap_file - unmap/unlock file page using the object-based rmap method
1061b291f000SNick Piggin  * @page: the page to unmap/unlock
106214fa31b8SAndi Kleen  * @flags: action and flags
10631da177e4SLinus Torvalds  *
10641da177e4SLinus Torvalds  * Find all the mappings of a page using the mapping pointer and the vma chains
10651da177e4SLinus Torvalds  * contained in the address_space struct it points to.
10661da177e4SLinus Torvalds  *
1067b291f000SNick Piggin  * This function is only called from try_to_unmap/try_to_munlock for
1068b291f000SNick Piggin  * object-based pages.
1069b291f000SNick Piggin  * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1070b291f000SNick Piggin  * where the page was found will be held for write.  So, we won't recheck
1071b291f000SNick Piggin  * vm_flags for that VMA.  That should be OK, because that vma shouldn't be
1072b291f000SNick Piggin  * 'LOCKED.
10731da177e4SLinus Torvalds  */
107414fa31b8SAndi Kleen static int try_to_unmap_file(struct page *page, enum ttu_flags flags)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	struct address_space *mapping = page->mapping;
10771da177e4SLinus Torvalds 	pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
10781da177e4SLinus Torvalds 	struct vm_area_struct *vma;
10791da177e4SLinus Torvalds 	struct prio_tree_iter iter;
10801da177e4SLinus Torvalds 	int ret = SWAP_AGAIN;
10811da177e4SLinus Torvalds 	unsigned long cursor;
10821da177e4SLinus Torvalds 	unsigned long max_nl_cursor = 0;
10831da177e4SLinus Torvalds 	unsigned long max_nl_size = 0;
10841da177e4SLinus Torvalds 	unsigned int mapcount;
1085b291f000SNick Piggin 	unsigned int mlocked = 0;
108614fa31b8SAndi Kleen 	int unlock = TTU_ACTION(flags) == TTU_MUNLOCK;
1087b291f000SNick Piggin 
1088b291f000SNick Piggin 	if (MLOCK_PAGES && unlikely(unlock))
1089b291f000SNick Piggin 		ret = SWAP_SUCCESS;	/* default for try_to_munlock() */
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	spin_lock(&mapping->i_mmap_lock);
10921da177e4SLinus Torvalds 	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1093b291f000SNick Piggin 		if (MLOCK_PAGES && unlikely(unlock)) {
1094508b9f8eSMinChan Kim 			if (!((vma->vm_flags & VM_LOCKED) &&
1095508b9f8eSMinChan Kim 						page_mapped_in_vma(page, vma)))
1096b291f000SNick Piggin 				continue;	/* must visit all vmas */
1097b291f000SNick Piggin 			ret = SWAP_MLOCK;
1098b291f000SNick Piggin 		} else {
109914fa31b8SAndi Kleen 			ret = try_to_unmap_one(page, vma, flags);
11001da177e4SLinus Torvalds 			if (ret == SWAP_FAIL || !page_mapped(page))
11011da177e4SLinus Torvalds 				goto out;
11021da177e4SLinus Torvalds 		}
1103b291f000SNick Piggin 		if (ret == SWAP_MLOCK) {
1104b291f000SNick Piggin 			mlocked = try_to_mlock_page(page, vma);
1105b291f000SNick Piggin 			if (mlocked)
11067b511594SHuang Shijie 				goto out;  /* stop if actually mlocked page */
1107b291f000SNick Piggin 		}
1108b291f000SNick Piggin 	}
1109b291f000SNick Piggin 
11101da177e4SLinus Torvalds 	if (list_empty(&mapping->i_mmap_nonlinear))
11111da177e4SLinus Torvalds 		goto out;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
11141da177e4SLinus Torvalds 						shared.vm_set.list) {
1115b291f000SNick Piggin 		if (MLOCK_PAGES && unlikely(unlock)) {
1116b291f000SNick Piggin 			if (!(vma->vm_flags & VM_LOCKED))
1117b291f000SNick Piggin 				continue;	/* must visit all vmas */
1118b291f000SNick Piggin 			ret = SWAP_MLOCK;	/* leave mlocked == 0 */
1119b291f000SNick Piggin 			goto out;		/* no need to look further */
1120b291f000SNick Piggin 		}
112114fa31b8SAndi Kleen 		if (!MLOCK_PAGES && !(flags & TTU_IGNORE_MLOCK) &&
112214fa31b8SAndi Kleen 			(vma->vm_flags & VM_LOCKED))
11231da177e4SLinus Torvalds 			continue;
11241da177e4SLinus Torvalds 		cursor = (unsigned long) vma->vm_private_data;
11251da177e4SLinus Torvalds 		if (cursor > max_nl_cursor)
11261da177e4SLinus Torvalds 			max_nl_cursor = cursor;
11271da177e4SLinus Torvalds 		cursor = vma->vm_end - vma->vm_start;
11281da177e4SLinus Torvalds 		if (cursor > max_nl_size)
11291da177e4SLinus Torvalds 			max_nl_size = cursor;
11301da177e4SLinus Torvalds 	}
11311da177e4SLinus Torvalds 
1132b291f000SNick Piggin 	if (max_nl_size == 0) {	/* all nonlinears locked or reserved ? */
11331da177e4SLinus Torvalds 		ret = SWAP_FAIL;
11341da177e4SLinus Torvalds 		goto out;
11351da177e4SLinus Torvalds 	}
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	/*
11381da177e4SLinus Torvalds 	 * We don't try to search for this page in the nonlinear vmas,
11391da177e4SLinus Torvalds 	 * and page_referenced wouldn't have found it anyway.  Instead
11401da177e4SLinus Torvalds 	 * just walk the nonlinear vmas trying to age and unmap some.
11411da177e4SLinus Torvalds 	 * The mapcount of the page we came in with is irrelevant,
11421da177e4SLinus Torvalds 	 * but even so use it as a guide to how hard we should try?
11431da177e4SLinus Torvalds 	 */
11441da177e4SLinus Torvalds 	mapcount = page_mapcount(page);
11451da177e4SLinus Torvalds 	if (!mapcount)
11461da177e4SLinus Torvalds 		goto out;
11471da177e4SLinus Torvalds 	cond_resched_lock(&mapping->i_mmap_lock);
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
11501da177e4SLinus Torvalds 	if (max_nl_cursor == 0)
11511da177e4SLinus Torvalds 		max_nl_cursor = CLUSTER_SIZE;
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	do {
11541da177e4SLinus Torvalds 		list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
11551da177e4SLinus Torvalds 						shared.vm_set.list) {
115614fa31b8SAndi Kleen 			if (!MLOCK_PAGES && !(flags & TTU_IGNORE_MLOCK) &&
1157b291f000SNick Piggin 			    (vma->vm_flags & VM_LOCKED))
11581da177e4SLinus Torvalds 				continue;
11591da177e4SLinus Torvalds 			cursor = (unsigned long) vma->vm_private_data;
1160839b9685SHugh Dickins 			while ( cursor < max_nl_cursor &&
11611da177e4SLinus Torvalds 				cursor < vma->vm_end - vma->vm_start) {
1162b291f000SNick Piggin 				ret = try_to_unmap_cluster(cursor, &mapcount,
1163b291f000SNick Piggin 								vma, page);
1164b291f000SNick Piggin 				if (ret == SWAP_MLOCK)
1165b291f000SNick Piggin 					mlocked = 2;	/* to return below */
11661da177e4SLinus Torvalds 				cursor += CLUSTER_SIZE;
11671da177e4SLinus Torvalds 				vma->vm_private_data = (void *) cursor;
11681da177e4SLinus Torvalds 				if ((int)mapcount <= 0)
11691da177e4SLinus Torvalds 					goto out;
11701da177e4SLinus Torvalds 			}
11711da177e4SLinus Torvalds 			vma->vm_private_data = (void *) max_nl_cursor;
11721da177e4SLinus Torvalds 		}
11731da177e4SLinus Torvalds 		cond_resched_lock(&mapping->i_mmap_lock);
11741da177e4SLinus Torvalds 		max_nl_cursor += CLUSTER_SIZE;
11751da177e4SLinus Torvalds 	} while (max_nl_cursor <= max_nl_size);
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	/*
11781da177e4SLinus Torvalds 	 * Don't loop forever (perhaps all the remaining pages are
11791da177e4SLinus Torvalds 	 * in locked vmas).  Reset cursor on all unreserved nonlinear
11801da177e4SLinus Torvalds 	 * vmas, now forgetting on which ones it had fallen behind.
11811da177e4SLinus Torvalds 	 */
1182101d2be7SHugh Dickins 	list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
11831da177e4SLinus Torvalds 		vma->vm_private_data = NULL;
11841da177e4SLinus Torvalds out:
11851da177e4SLinus Torvalds 	spin_unlock(&mapping->i_mmap_lock);
1186b291f000SNick Piggin 	if (mlocked)
1187b291f000SNick Piggin 		ret = SWAP_MLOCK;	/* actually mlocked the page */
1188b291f000SNick Piggin 	else if (ret == SWAP_MLOCK)
1189b291f000SNick Piggin 		ret = SWAP_AGAIN;	/* saw VM_LOCKED vma */
11901da177e4SLinus Torvalds 	return ret;
11911da177e4SLinus Torvalds }
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds /**
11941da177e4SLinus Torvalds  * try_to_unmap - try to remove all page table mappings to a page
11951da177e4SLinus Torvalds  * @page: the page to get unmapped
119614fa31b8SAndi Kleen  * @flags: action and flags
11971da177e4SLinus Torvalds  *
11981da177e4SLinus Torvalds  * Tries to remove all the page table entries which are mapping this
11991da177e4SLinus Torvalds  * page, used in the pageout path.  Caller must hold the page lock.
12001da177e4SLinus Torvalds  * Return values are:
12011da177e4SLinus Torvalds  *
12021da177e4SLinus Torvalds  * SWAP_SUCCESS	- we succeeded in removing all mappings
12031da177e4SLinus Torvalds  * SWAP_AGAIN	- we missed a mapping, try again later
12041da177e4SLinus Torvalds  * SWAP_FAIL	- the page is unswappable
1205b291f000SNick Piggin  * SWAP_MLOCK	- page is mlocked.
12061da177e4SLinus Torvalds  */
120714fa31b8SAndi Kleen int try_to_unmap(struct page *page, enum ttu_flags flags)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	int ret;
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	if (PageAnon(page))
121414fa31b8SAndi Kleen 		ret = try_to_unmap_anon(page, flags);
12151da177e4SLinus Torvalds 	else
121614fa31b8SAndi Kleen 		ret = try_to_unmap_file(page, flags);
1217b291f000SNick Piggin 	if (ret != SWAP_MLOCK && !page_mapped(page))
12181da177e4SLinus Torvalds 		ret = SWAP_SUCCESS;
12191da177e4SLinus Torvalds 	return ret;
12201da177e4SLinus Torvalds }
122181b4082dSNikita Danilov 
1222b291f000SNick Piggin /**
1223b291f000SNick Piggin  * try_to_munlock - try to munlock a page
1224b291f000SNick Piggin  * @page: the page to be munlocked
1225b291f000SNick Piggin  *
1226b291f000SNick Piggin  * Called from munlock code.  Checks all of the VMAs mapping the page
1227b291f000SNick Piggin  * to make sure nobody else has this page mlocked. The page will be
1228b291f000SNick Piggin  * returned with PG_mlocked cleared if no other vmas have it mlocked.
1229b291f000SNick Piggin  *
1230b291f000SNick Piggin  * Return values are:
1231b291f000SNick Piggin  *
1232b291f000SNick Piggin  * SWAP_SUCCESS	- no vma's holding page mlocked.
1233b291f000SNick Piggin  * SWAP_AGAIN	- page mapped in mlocked vma -- couldn't acquire mmap sem
1234b291f000SNick Piggin  * SWAP_MLOCK	- page is now mlocked.
1235b291f000SNick Piggin  */
1236b291f000SNick Piggin int try_to_munlock(struct page *page)
1237b291f000SNick Piggin {
1238b291f000SNick Piggin 	VM_BUG_ON(!PageLocked(page) || PageLRU(page));
1239b291f000SNick Piggin 
1240b291f000SNick Piggin 	if (PageAnon(page))
124114fa31b8SAndi Kleen 		return try_to_unmap_anon(page, TTU_MUNLOCK);
1242b291f000SNick Piggin 	else
124314fa31b8SAndi Kleen 		return try_to_unmap_file(page, TTU_MUNLOCK);
1244b291f000SNick Piggin }
124568377659SKOSAKI Motohiro 
1246