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) 241da177e4SLinus Torvalds * mm->mmap_sem 251da177e4SLinus Torvalds * page->flags PG_locked (lock_page) 26c8c06efaSDavidlohr Bueso * mapping->i_mmap_rwsem 275a505085SIngo Molnar * anon_vma->rwsem 28b8072f09SHugh Dickins * mm->page_table_lock or pte_lock 29053837fcSNick Piggin * zone->lru_lock (in mark_page_accessed, isolate_lru_page) 305d337b91SHugh Dickins * swap_lock (in swap_duplicate, swap_info_get) 311da177e4SLinus Torvalds * mmlist_lock (in mmput, drain_mmlist and others) 321da177e4SLinus Torvalds * mapping->private_lock (in __set_page_dirty_buffers) 33250df6edSDave Chinner * inode->i_lock (in set_page_dirty's __mark_inode_dirty) 34f758eeabSChristoph Hellwig * bdi.wb->list_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, 38f758eeabSChristoph Hellwig * within bdi.wb->list_lock in __sync_single_inode) 396a46079cSAndi Kleen * 405a505085SIngo Molnar * anon_vma->rwsem,mapping->i_mutex (memory_failure, collect_procs_anon) 416a46079cSAndi Kleen * ->tasklist_lock 426a46079cSAndi Kleen * pte map lock 431da177e4SLinus Torvalds */ 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #include <linux/mm.h> 461da177e4SLinus Torvalds #include <linux/pagemap.h> 471da177e4SLinus Torvalds #include <linux/swap.h> 481da177e4SLinus Torvalds #include <linux/swapops.h> 491da177e4SLinus Torvalds #include <linux/slab.h> 501da177e4SLinus Torvalds #include <linux/init.h> 515ad64688SHugh Dickins #include <linux/ksm.h> 521da177e4SLinus Torvalds #include <linux/rmap.h> 531da177e4SLinus Torvalds #include <linux/rcupdate.h> 54b95f1b31SPaul Gortmaker #include <linux/export.h> 558a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 56cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 5764cdd548SKOSAKI Motohiro #include <linux/migrate.h> 580fe6e20bSNaoya Horiguchi #include <linux/hugetlb.h> 59ef5d437fSJan Kara #include <linux/backing-dev.h> 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds #include <asm/tlbflush.h> 621da177e4SLinus Torvalds 63b291f000SNick Piggin #include "internal.h" 64b291f000SNick Piggin 65fdd2e5f8SAdrian Bunk static struct kmem_cache *anon_vma_cachep; 665beb4930SRik van Riel static struct kmem_cache *anon_vma_chain_cachep; 67fdd2e5f8SAdrian Bunk 68fdd2e5f8SAdrian Bunk static inline struct anon_vma *anon_vma_alloc(void) 69fdd2e5f8SAdrian Bunk { 7001d8b20dSPeter Zijlstra struct anon_vma *anon_vma; 7101d8b20dSPeter Zijlstra 7201d8b20dSPeter Zijlstra anon_vma = kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL); 7301d8b20dSPeter Zijlstra if (anon_vma) { 7401d8b20dSPeter Zijlstra atomic_set(&anon_vma->refcount, 1); 7501d8b20dSPeter Zijlstra /* 7601d8b20dSPeter Zijlstra * Initialise the anon_vma root to point to itself. If called 7701d8b20dSPeter Zijlstra * from fork, the root will be reset to the parents anon_vma. 7801d8b20dSPeter Zijlstra */ 7901d8b20dSPeter Zijlstra anon_vma->root = anon_vma; 80fdd2e5f8SAdrian Bunk } 81fdd2e5f8SAdrian Bunk 8201d8b20dSPeter Zijlstra return anon_vma; 8301d8b20dSPeter Zijlstra } 8401d8b20dSPeter Zijlstra 8501d8b20dSPeter Zijlstra static inline void anon_vma_free(struct anon_vma *anon_vma) 86fdd2e5f8SAdrian Bunk { 8701d8b20dSPeter Zijlstra VM_BUG_ON(atomic_read(&anon_vma->refcount)); 8888c22088SPeter Zijlstra 8988c22088SPeter Zijlstra /* 904fc3f1d6SIngo Molnar * Synchronize against page_lock_anon_vma_read() such that 9188c22088SPeter Zijlstra * we can safely hold the lock without the anon_vma getting 9288c22088SPeter Zijlstra * freed. 9388c22088SPeter Zijlstra * 9488c22088SPeter Zijlstra * Relies on the full mb implied by the atomic_dec_and_test() from 9588c22088SPeter Zijlstra * put_anon_vma() against the acquire barrier implied by 964fc3f1d6SIngo Molnar * down_read_trylock() from page_lock_anon_vma_read(). This orders: 9788c22088SPeter Zijlstra * 984fc3f1d6SIngo Molnar * page_lock_anon_vma_read() VS put_anon_vma() 994fc3f1d6SIngo Molnar * down_read_trylock() atomic_dec_and_test() 10088c22088SPeter Zijlstra * LOCK MB 1014fc3f1d6SIngo Molnar * atomic_read() rwsem_is_locked() 10288c22088SPeter Zijlstra * 10388c22088SPeter Zijlstra * LOCK should suffice since the actual taking of the lock must 10488c22088SPeter Zijlstra * happen _before_ what follows. 10588c22088SPeter Zijlstra */ 1067f39dda9SHugh Dickins might_sleep(); 1075a505085SIngo Molnar if (rwsem_is_locked(&anon_vma->root->rwsem)) { 1084fc3f1d6SIngo Molnar anon_vma_lock_write(anon_vma); 10908b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 11088c22088SPeter Zijlstra } 11188c22088SPeter Zijlstra 112fdd2e5f8SAdrian Bunk kmem_cache_free(anon_vma_cachep, anon_vma); 113fdd2e5f8SAdrian Bunk } 1141da177e4SLinus Torvalds 115dd34739cSLinus Torvalds static inline struct anon_vma_chain *anon_vma_chain_alloc(gfp_t gfp) 1165beb4930SRik van Riel { 117dd34739cSLinus Torvalds return kmem_cache_alloc(anon_vma_chain_cachep, gfp); 1185beb4930SRik van Riel } 1195beb4930SRik van Riel 120e574b5fdSNamhyung Kim static void anon_vma_chain_free(struct anon_vma_chain *anon_vma_chain) 1215beb4930SRik van Riel { 1225beb4930SRik van Riel kmem_cache_free(anon_vma_chain_cachep, anon_vma_chain); 1235beb4930SRik van Riel } 1245beb4930SRik van Riel 1256583a843SKautuk Consul static void anon_vma_chain_link(struct vm_area_struct *vma, 1266583a843SKautuk Consul struct anon_vma_chain *avc, 1276583a843SKautuk Consul struct anon_vma *anon_vma) 1286583a843SKautuk Consul { 1296583a843SKautuk Consul avc->vma = vma; 1306583a843SKautuk Consul avc->anon_vma = anon_vma; 1316583a843SKautuk Consul list_add(&avc->same_vma, &vma->anon_vma_chain); 132bf181b9fSMichel Lespinasse anon_vma_interval_tree_insert(avc, &anon_vma->rb_root); 1336583a843SKautuk Consul } 1346583a843SKautuk Consul 135d9d332e0SLinus Torvalds /** 136d9d332e0SLinus Torvalds * anon_vma_prepare - attach an anon_vma to a memory region 137d9d332e0SLinus Torvalds * @vma: the memory region in question 138d9d332e0SLinus Torvalds * 139d9d332e0SLinus Torvalds * This makes sure the memory mapping described by 'vma' has 140d9d332e0SLinus Torvalds * an 'anon_vma' attached to it, so that we can associate the 141d9d332e0SLinus Torvalds * anonymous pages mapped into it with that anon_vma. 142d9d332e0SLinus Torvalds * 143d9d332e0SLinus Torvalds * The common case will be that we already have one, but if 14423a0790aSFigo.zhang * not we either need to find an adjacent mapping that we 145d9d332e0SLinus Torvalds * can re-use the anon_vma from (very common when the only 146d9d332e0SLinus Torvalds * reason for splitting a vma has been mprotect()), or we 147d9d332e0SLinus Torvalds * allocate a new one. 148d9d332e0SLinus Torvalds * 149d9d332e0SLinus Torvalds * Anon-vma allocations are very subtle, because we may have 1504fc3f1d6SIngo Molnar * optimistically looked up an anon_vma in page_lock_anon_vma_read() 151d9d332e0SLinus Torvalds * and that may actually touch the spinlock even in the newly 152d9d332e0SLinus Torvalds * allocated vma (it depends on RCU to make sure that the 153d9d332e0SLinus Torvalds * anon_vma isn't actually destroyed). 154d9d332e0SLinus Torvalds * 155d9d332e0SLinus Torvalds * As a result, we need to do proper anon_vma locking even 156d9d332e0SLinus Torvalds * for the new allocation. At the same time, we do not want 157d9d332e0SLinus Torvalds * to do any locking for the common case of already having 158d9d332e0SLinus Torvalds * an anon_vma. 159d9d332e0SLinus Torvalds * 160d9d332e0SLinus Torvalds * This must be called with the mmap_sem held for reading. 161d9d332e0SLinus Torvalds */ 1621da177e4SLinus Torvalds int anon_vma_prepare(struct vm_area_struct *vma) 1631da177e4SLinus Torvalds { 1641da177e4SLinus Torvalds struct anon_vma *anon_vma = vma->anon_vma; 1655beb4930SRik van Riel struct anon_vma_chain *avc; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds might_sleep(); 1681da177e4SLinus Torvalds if (unlikely(!anon_vma)) { 1691da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 170d9d332e0SLinus Torvalds struct anon_vma *allocated; 1711da177e4SLinus Torvalds 172dd34739cSLinus Torvalds avc = anon_vma_chain_alloc(GFP_KERNEL); 1735beb4930SRik van Riel if (!avc) 1745beb4930SRik van Riel goto out_enomem; 1755beb4930SRik van Riel 1761da177e4SLinus Torvalds anon_vma = find_mergeable_anon_vma(vma); 1771da177e4SLinus Torvalds allocated = NULL; 178d9d332e0SLinus Torvalds if (!anon_vma) { 1791da177e4SLinus Torvalds anon_vma = anon_vma_alloc(); 1801da177e4SLinus Torvalds if (unlikely(!anon_vma)) 1815beb4930SRik van Riel goto out_enomem_free_avc; 1821da177e4SLinus Torvalds allocated = anon_vma; 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1854fc3f1d6SIngo Molnar anon_vma_lock_write(anon_vma); 1861da177e4SLinus Torvalds /* page_table_lock to protect against threads */ 1871da177e4SLinus Torvalds spin_lock(&mm->page_table_lock); 1881da177e4SLinus Torvalds if (likely(!vma->anon_vma)) { 1891da177e4SLinus Torvalds vma->anon_vma = anon_vma; 1906583a843SKautuk Consul anon_vma_chain_link(vma, avc, anon_vma); 1911da177e4SLinus Torvalds allocated = NULL; 19231f2b0ebSOleg Nesterov avc = NULL; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds spin_unlock(&mm->page_table_lock); 19508b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 19631f2b0ebSOleg Nesterov 19731f2b0ebSOleg Nesterov if (unlikely(allocated)) 19801d8b20dSPeter Zijlstra put_anon_vma(allocated); 19931f2b0ebSOleg Nesterov if (unlikely(avc)) 2005beb4930SRik van Riel anon_vma_chain_free(avc); 2015beb4930SRik van Riel } 2021da177e4SLinus Torvalds return 0; 2035beb4930SRik van Riel 2045beb4930SRik van Riel out_enomem_free_avc: 2055beb4930SRik van Riel anon_vma_chain_free(avc); 2065beb4930SRik van Riel out_enomem: 2075beb4930SRik van Riel return -ENOMEM; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 210bb4aa396SLinus Torvalds /* 211bb4aa396SLinus Torvalds * This is a useful helper function for locking the anon_vma root as 212bb4aa396SLinus Torvalds * we traverse the vma->anon_vma_chain, looping over anon_vma's that 213bb4aa396SLinus Torvalds * have the same vma. 214bb4aa396SLinus Torvalds * 215bb4aa396SLinus Torvalds * Such anon_vma's should have the same root, so you'd expect to see 216bb4aa396SLinus Torvalds * just a single mutex_lock for the whole traversal. 217bb4aa396SLinus Torvalds */ 218bb4aa396SLinus Torvalds static inline struct anon_vma *lock_anon_vma_root(struct anon_vma *root, struct anon_vma *anon_vma) 219bb4aa396SLinus Torvalds { 220bb4aa396SLinus Torvalds struct anon_vma *new_root = anon_vma->root; 221bb4aa396SLinus Torvalds if (new_root != root) { 222bb4aa396SLinus Torvalds if (WARN_ON_ONCE(root)) 2235a505085SIngo Molnar up_write(&root->rwsem); 224bb4aa396SLinus Torvalds root = new_root; 2255a505085SIngo Molnar down_write(&root->rwsem); 226bb4aa396SLinus Torvalds } 227bb4aa396SLinus Torvalds return root; 228bb4aa396SLinus Torvalds } 229bb4aa396SLinus Torvalds 230bb4aa396SLinus Torvalds static inline void unlock_anon_vma_root(struct anon_vma *root) 231bb4aa396SLinus Torvalds { 232bb4aa396SLinus Torvalds if (root) 2335a505085SIngo Molnar up_write(&root->rwsem); 234bb4aa396SLinus Torvalds } 235bb4aa396SLinus Torvalds 2365beb4930SRik van Riel /* 2375beb4930SRik van Riel * Attach the anon_vmas from src to dst. 2385beb4930SRik van Riel * Returns 0 on success, -ENOMEM on failure. 2395beb4930SRik van Riel */ 2405beb4930SRik van Riel int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src) 2415beb4930SRik van Riel { 2425beb4930SRik van Riel struct anon_vma_chain *avc, *pavc; 243bb4aa396SLinus Torvalds struct anon_vma *root = NULL; 2445beb4930SRik van Riel 245646d87b4SLinus Torvalds list_for_each_entry_reverse(pavc, &src->anon_vma_chain, same_vma) { 246bb4aa396SLinus Torvalds struct anon_vma *anon_vma; 247bb4aa396SLinus Torvalds 248dd34739cSLinus Torvalds avc = anon_vma_chain_alloc(GFP_NOWAIT | __GFP_NOWARN); 249dd34739cSLinus Torvalds if (unlikely(!avc)) { 250dd34739cSLinus Torvalds unlock_anon_vma_root(root); 251dd34739cSLinus Torvalds root = NULL; 252dd34739cSLinus Torvalds avc = anon_vma_chain_alloc(GFP_KERNEL); 2535beb4930SRik van Riel if (!avc) 2545beb4930SRik van Riel goto enomem_failure; 255dd34739cSLinus Torvalds } 256bb4aa396SLinus Torvalds anon_vma = pavc->anon_vma; 257bb4aa396SLinus Torvalds root = lock_anon_vma_root(root, anon_vma); 258bb4aa396SLinus Torvalds anon_vma_chain_link(dst, avc, anon_vma); 2595beb4930SRik van Riel } 260bb4aa396SLinus Torvalds unlock_anon_vma_root(root); 2615beb4930SRik van Riel return 0; 2625beb4930SRik van Riel 2635beb4930SRik van Riel enomem_failure: 2645beb4930SRik van Riel unlink_anon_vmas(dst); 2655beb4930SRik van Riel return -ENOMEM; 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 2685beb4930SRik van Riel /* 2695beb4930SRik van Riel * Attach vma to its own anon_vma, as well as to the anon_vmas that 2705beb4930SRik van Riel * the corresponding VMA in the parent process is attached to. 2715beb4930SRik van Riel * Returns 0 on success, non-zero on failure. 2725beb4930SRik van Riel */ 2735beb4930SRik van Riel int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) 2741da177e4SLinus Torvalds { 2755beb4930SRik van Riel struct anon_vma_chain *avc; 2765beb4930SRik van Riel struct anon_vma *anon_vma; 277c4ea95d7SDaniel Forrest int error; 2785beb4930SRik van Riel 2795beb4930SRik van Riel /* Don't bother if the parent process has no anon_vma here. */ 2805beb4930SRik van Riel if (!pvma->anon_vma) 2815beb4930SRik van Riel return 0; 2825beb4930SRik van Riel 2835beb4930SRik van Riel /* 2845beb4930SRik van Riel * First, attach the new VMA to the parent VMA's anon_vmas, 2855beb4930SRik van Riel * so rmap can find non-COWed pages in child processes. 2865beb4930SRik van Riel */ 287c4ea95d7SDaniel Forrest error = anon_vma_clone(vma, pvma); 288c4ea95d7SDaniel Forrest if (error) 289c4ea95d7SDaniel Forrest return error; 2905beb4930SRik van Riel 2915beb4930SRik van Riel /* Then add our own anon_vma. */ 2925beb4930SRik van Riel anon_vma = anon_vma_alloc(); 2935beb4930SRik van Riel if (!anon_vma) 2945beb4930SRik van Riel goto out_error; 295dd34739cSLinus Torvalds avc = anon_vma_chain_alloc(GFP_KERNEL); 2965beb4930SRik van Riel if (!avc) 2975beb4930SRik van Riel goto out_error_free_anon_vma; 2985c341ee1SRik van Riel 2995c341ee1SRik van Riel /* 3005c341ee1SRik van Riel * The root anon_vma's spinlock is the lock actually used when we 3015c341ee1SRik van Riel * lock any of the anon_vmas in this anon_vma tree. 3025c341ee1SRik van Riel */ 3035c341ee1SRik van Riel anon_vma->root = pvma->anon_vma->root; 30476545066SRik van Riel /* 30501d8b20dSPeter Zijlstra * With refcounts, an anon_vma can stay around longer than the 30601d8b20dSPeter Zijlstra * process it belongs to. The root anon_vma needs to be pinned until 30701d8b20dSPeter Zijlstra * this anon_vma is freed, because the lock lives in the root. 30876545066SRik van Riel */ 30976545066SRik van Riel get_anon_vma(anon_vma->root); 3105beb4930SRik van Riel /* Mark this anon_vma as the one where our new (COWed) pages go. */ 3115beb4930SRik van Riel vma->anon_vma = anon_vma; 3124fc3f1d6SIngo Molnar anon_vma_lock_write(anon_vma); 3135c341ee1SRik van Riel anon_vma_chain_link(vma, avc, anon_vma); 31408b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 3155beb4930SRik van Riel 3165beb4930SRik van Riel return 0; 3175beb4930SRik van Riel 3185beb4930SRik van Riel out_error_free_anon_vma: 31901d8b20dSPeter Zijlstra put_anon_vma(anon_vma); 3205beb4930SRik van Riel out_error: 3214946d54cSRik van Riel unlink_anon_vmas(vma); 3225beb4930SRik van Riel return -ENOMEM; 3235beb4930SRik van Riel } 3245beb4930SRik van Riel 3255beb4930SRik van Riel void unlink_anon_vmas(struct vm_area_struct *vma) 3265beb4930SRik van Riel { 3275beb4930SRik van Riel struct anon_vma_chain *avc, *next; 328eee2acbaSPeter Zijlstra struct anon_vma *root = NULL; 3295beb4930SRik van Riel 3305c341ee1SRik van Riel /* 3315c341ee1SRik van Riel * Unlink each anon_vma chained to the VMA. This list is ordered 3325c341ee1SRik van Riel * from newest to oldest, ensuring the root anon_vma gets freed last. 3335c341ee1SRik van Riel */ 3345beb4930SRik van Riel list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) { 335eee2acbaSPeter Zijlstra struct anon_vma *anon_vma = avc->anon_vma; 336eee2acbaSPeter Zijlstra 337eee2acbaSPeter Zijlstra root = lock_anon_vma_root(root, anon_vma); 338bf181b9fSMichel Lespinasse anon_vma_interval_tree_remove(avc, &anon_vma->rb_root); 339eee2acbaSPeter Zijlstra 340eee2acbaSPeter Zijlstra /* 341eee2acbaSPeter Zijlstra * Leave empty anon_vmas on the list - we'll need 342eee2acbaSPeter Zijlstra * to free them outside the lock. 343eee2acbaSPeter Zijlstra */ 344bf181b9fSMichel Lespinasse if (RB_EMPTY_ROOT(&anon_vma->rb_root)) 345eee2acbaSPeter Zijlstra continue; 346eee2acbaSPeter Zijlstra 347eee2acbaSPeter Zijlstra list_del(&avc->same_vma); 348eee2acbaSPeter Zijlstra anon_vma_chain_free(avc); 349eee2acbaSPeter Zijlstra } 350eee2acbaSPeter Zijlstra unlock_anon_vma_root(root); 351eee2acbaSPeter Zijlstra 352eee2acbaSPeter Zijlstra /* 353eee2acbaSPeter Zijlstra * Iterate the list once more, it now only contains empty and unlinked 354eee2acbaSPeter Zijlstra * anon_vmas, destroy them. Could not do before due to __put_anon_vma() 3555a505085SIngo Molnar * needing to write-acquire the anon_vma->root->rwsem. 356eee2acbaSPeter Zijlstra */ 357eee2acbaSPeter Zijlstra list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) { 358eee2acbaSPeter Zijlstra struct anon_vma *anon_vma = avc->anon_vma; 359eee2acbaSPeter Zijlstra 360eee2acbaSPeter Zijlstra put_anon_vma(anon_vma); 361eee2acbaSPeter Zijlstra 3625beb4930SRik van Riel list_del(&avc->same_vma); 3635beb4930SRik van Riel anon_vma_chain_free(avc); 3645beb4930SRik van Riel } 3655beb4930SRik van Riel } 3665beb4930SRik van Riel 36751cc5068SAlexey Dobriyan static void anon_vma_ctor(void *data) 3681da177e4SLinus Torvalds { 3691da177e4SLinus Torvalds struct anon_vma *anon_vma = data; 3701da177e4SLinus Torvalds 3715a505085SIngo Molnar init_rwsem(&anon_vma->rwsem); 37283813267SPeter Zijlstra atomic_set(&anon_vma->refcount, 0); 373bf181b9fSMichel Lespinasse anon_vma->rb_root = RB_ROOT; 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds void __init anon_vma_init(void) 3771da177e4SLinus Torvalds { 3781da177e4SLinus Torvalds anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma), 37920c2df83SPaul Mundt 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor); 3805beb4930SRik van Riel anon_vma_chain_cachep = KMEM_CACHE(anon_vma_chain, SLAB_PANIC); 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds /* 3846111e4caSPeter Zijlstra * Getting a lock on a stable anon_vma from a page off the LRU is tricky! 3856111e4caSPeter Zijlstra * 3866111e4caSPeter Zijlstra * Since there is no serialization what so ever against page_remove_rmap() 3876111e4caSPeter Zijlstra * the best this function can do is return a locked anon_vma that might 3886111e4caSPeter Zijlstra * have been relevant to this page. 3896111e4caSPeter Zijlstra * 3906111e4caSPeter Zijlstra * The page might have been remapped to a different anon_vma or the anon_vma 3916111e4caSPeter Zijlstra * returned may already be freed (and even reused). 3926111e4caSPeter Zijlstra * 393bc658c96SPeter Zijlstra * In case it was remapped to a different anon_vma, the new anon_vma will be a 394bc658c96SPeter Zijlstra * child of the old anon_vma, and the anon_vma lifetime rules will therefore 395bc658c96SPeter Zijlstra * ensure that any anon_vma obtained from the page will still be valid for as 396bc658c96SPeter Zijlstra * long as we observe page_mapped() [ hence all those page_mapped() tests ]. 397bc658c96SPeter Zijlstra * 3986111e4caSPeter Zijlstra * All users of this function must be very careful when walking the anon_vma 3996111e4caSPeter Zijlstra * chain and verify that the page in question is indeed mapped in it 4006111e4caSPeter Zijlstra * [ something equivalent to page_mapped_in_vma() ]. 4016111e4caSPeter Zijlstra * 4026111e4caSPeter Zijlstra * Since anon_vma's slab is DESTROY_BY_RCU and we know from page_remove_rmap() 4036111e4caSPeter Zijlstra * that the anon_vma pointer from page->mapping is valid if there is a 4046111e4caSPeter Zijlstra * mapcount, we can dereference the anon_vma after observing those. 4051da177e4SLinus Torvalds */ 406746b18d4SPeter Zijlstra struct anon_vma *page_get_anon_vma(struct page *page) 4071da177e4SLinus Torvalds { 408746b18d4SPeter Zijlstra struct anon_vma *anon_vma = NULL; 4091da177e4SLinus Torvalds unsigned long anon_mapping; 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds rcu_read_lock(); 41280e14822SHugh Dickins anon_mapping = (unsigned long) ACCESS_ONCE(page->mapping); 4133ca7b3c5SHugh Dickins if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON) 4141da177e4SLinus Torvalds goto out; 4151da177e4SLinus Torvalds if (!page_mapped(page)) 4161da177e4SLinus Torvalds goto out; 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON); 419746b18d4SPeter Zijlstra if (!atomic_inc_not_zero(&anon_vma->refcount)) { 420746b18d4SPeter Zijlstra anon_vma = NULL; 421746b18d4SPeter Zijlstra goto out; 422746b18d4SPeter Zijlstra } 423f1819427SHugh Dickins 424f1819427SHugh Dickins /* 425f1819427SHugh Dickins * If this page is still mapped, then its anon_vma cannot have been 426746b18d4SPeter Zijlstra * freed. But if it has been unmapped, we have no security against the 427746b18d4SPeter Zijlstra * anon_vma structure being freed and reused (for another anon_vma: 428746b18d4SPeter Zijlstra * SLAB_DESTROY_BY_RCU guarantees that - so the atomic_inc_not_zero() 429746b18d4SPeter Zijlstra * above cannot corrupt). 430f1819427SHugh Dickins */ 431746b18d4SPeter Zijlstra if (!page_mapped(page)) { 4327f39dda9SHugh Dickins rcu_read_unlock(); 433746b18d4SPeter Zijlstra put_anon_vma(anon_vma); 4347f39dda9SHugh Dickins return NULL; 435746b18d4SPeter Zijlstra } 4361da177e4SLinus Torvalds out: 4371da177e4SLinus Torvalds rcu_read_unlock(); 438746b18d4SPeter Zijlstra 439746b18d4SPeter Zijlstra return anon_vma; 440746b18d4SPeter Zijlstra } 441746b18d4SPeter Zijlstra 44288c22088SPeter Zijlstra /* 44388c22088SPeter Zijlstra * Similar to page_get_anon_vma() except it locks the anon_vma. 44488c22088SPeter Zijlstra * 44588c22088SPeter Zijlstra * Its a little more complex as it tries to keep the fast path to a single 44688c22088SPeter Zijlstra * atomic op -- the trylock. If we fail the trylock, we fall back to getting a 44788c22088SPeter Zijlstra * reference like with page_get_anon_vma() and then block on the mutex. 44888c22088SPeter Zijlstra */ 4494fc3f1d6SIngo Molnar struct anon_vma *page_lock_anon_vma_read(struct page *page) 450746b18d4SPeter Zijlstra { 45188c22088SPeter Zijlstra struct anon_vma *anon_vma = NULL; 452eee0f252SHugh Dickins struct anon_vma *root_anon_vma; 45388c22088SPeter Zijlstra unsigned long anon_mapping; 454746b18d4SPeter Zijlstra 45588c22088SPeter Zijlstra rcu_read_lock(); 45688c22088SPeter Zijlstra anon_mapping = (unsigned long) ACCESS_ONCE(page->mapping); 45788c22088SPeter Zijlstra if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON) 45888c22088SPeter Zijlstra goto out; 45988c22088SPeter Zijlstra if (!page_mapped(page)) 46088c22088SPeter Zijlstra goto out; 46188c22088SPeter Zijlstra 46288c22088SPeter Zijlstra anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON); 463eee0f252SHugh Dickins root_anon_vma = ACCESS_ONCE(anon_vma->root); 4644fc3f1d6SIngo Molnar if (down_read_trylock(&root_anon_vma->rwsem)) { 46588c22088SPeter Zijlstra /* 466eee0f252SHugh Dickins * If the page is still mapped, then this anon_vma is still 467eee0f252SHugh Dickins * its anon_vma, and holding the mutex ensures that it will 468bc658c96SPeter Zijlstra * not go away, see anon_vma_free(). 46988c22088SPeter Zijlstra */ 470eee0f252SHugh Dickins if (!page_mapped(page)) { 4714fc3f1d6SIngo Molnar up_read(&root_anon_vma->rwsem); 47288c22088SPeter Zijlstra anon_vma = NULL; 47388c22088SPeter Zijlstra } 47488c22088SPeter Zijlstra goto out; 47588c22088SPeter Zijlstra } 47688c22088SPeter Zijlstra 47788c22088SPeter Zijlstra /* trylock failed, we got to sleep */ 47888c22088SPeter Zijlstra if (!atomic_inc_not_zero(&anon_vma->refcount)) { 47988c22088SPeter Zijlstra anon_vma = NULL; 48088c22088SPeter Zijlstra goto out; 48188c22088SPeter Zijlstra } 48288c22088SPeter Zijlstra 48388c22088SPeter Zijlstra if (!page_mapped(page)) { 4847f39dda9SHugh Dickins rcu_read_unlock(); 48588c22088SPeter Zijlstra put_anon_vma(anon_vma); 4867f39dda9SHugh Dickins return NULL; 48788c22088SPeter Zijlstra } 48888c22088SPeter Zijlstra 48988c22088SPeter Zijlstra /* we pinned the anon_vma, its safe to sleep */ 49088c22088SPeter Zijlstra rcu_read_unlock(); 4914fc3f1d6SIngo Molnar anon_vma_lock_read(anon_vma); 492746b18d4SPeter Zijlstra 49388c22088SPeter Zijlstra if (atomic_dec_and_test(&anon_vma->refcount)) { 49488c22088SPeter Zijlstra /* 49588c22088SPeter Zijlstra * Oops, we held the last refcount, release the lock 49688c22088SPeter Zijlstra * and bail -- can't simply use put_anon_vma() because 4974fc3f1d6SIngo Molnar * we'll deadlock on the anon_vma_lock_write() recursion. 49888c22088SPeter Zijlstra */ 4994fc3f1d6SIngo Molnar anon_vma_unlock_read(anon_vma); 50088c22088SPeter Zijlstra __put_anon_vma(anon_vma); 50188c22088SPeter Zijlstra anon_vma = NULL; 50288c22088SPeter Zijlstra } 50388c22088SPeter Zijlstra 50488c22088SPeter Zijlstra return anon_vma; 50588c22088SPeter Zijlstra 50688c22088SPeter Zijlstra out: 50788c22088SPeter Zijlstra rcu_read_unlock(); 508746b18d4SPeter Zijlstra return anon_vma; 50934bbd704SOleg Nesterov } 51034bbd704SOleg Nesterov 5114fc3f1d6SIngo Molnar void page_unlock_anon_vma_read(struct anon_vma *anon_vma) 51234bbd704SOleg Nesterov { 5134fc3f1d6SIngo Molnar anon_vma_unlock_read(anon_vma); 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* 5173ad33b24SLee Schermerhorn * At what user virtual address is page expected in @vma? 5181da177e4SLinus Torvalds */ 51986c2ad19SMichel Lespinasse static inline unsigned long 52086c2ad19SMichel Lespinasse __vma_address(struct page *page, struct vm_area_struct *vma) 5211da177e4SLinus Torvalds { 522a0f7a756SNaoya Horiguchi pgoff_t pgoff = page_to_pgoff(page); 52386c2ad19SMichel Lespinasse return vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 5241da177e4SLinus Torvalds } 52586c2ad19SMichel Lespinasse 52686c2ad19SMichel Lespinasse inline unsigned long 52786c2ad19SMichel Lespinasse vma_address(struct page *page, struct vm_area_struct *vma) 52886c2ad19SMichel Lespinasse { 52986c2ad19SMichel Lespinasse unsigned long address = __vma_address(page, vma); 53086c2ad19SMichel Lespinasse 53186c2ad19SMichel Lespinasse /* page should be within @vma mapping range */ 53281d1b09cSSasha Levin VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma); 53386c2ad19SMichel Lespinasse 5341da177e4SLinus Torvalds return address; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds /* 538bf89c8c8SHuang Shijie * At what user virtual address is page expected in vma? 539ab941e0fSNaoya Horiguchi * Caller should check the page is actually part of the vma. 5401da177e4SLinus Torvalds */ 5411da177e4SLinus Torvalds unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma) 5421da177e4SLinus Torvalds { 54386c2ad19SMichel Lespinasse unsigned long address; 54421d0d443SAndrea Arcangeli if (PageAnon(page)) { 5454829b906SHugh Dickins struct anon_vma *page__anon_vma = page_anon_vma(page); 5464829b906SHugh Dickins /* 5474829b906SHugh Dickins * Note: swapoff's unuse_vma() is more efficient with this 5484829b906SHugh Dickins * check, and needs it to match anon_vma when KSM is active. 5494829b906SHugh Dickins */ 5504829b906SHugh Dickins if (!vma->anon_vma || !page__anon_vma || 5514829b906SHugh Dickins vma->anon_vma->root != page__anon_vma->root) 55221d0d443SAndrea Arcangeli return -EFAULT; 55321d0d443SAndrea Arcangeli } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) { 554ee498ed7SHugh Dickins if (!vma->vm_file || 555ee498ed7SHugh Dickins vma->vm_file->f_mapping != page->mapping) 5561da177e4SLinus Torvalds return -EFAULT; 5571da177e4SLinus Torvalds } else 5581da177e4SLinus Torvalds return -EFAULT; 55986c2ad19SMichel Lespinasse address = __vma_address(page, vma); 56086c2ad19SMichel Lespinasse if (unlikely(address < vma->vm_start || address >= vma->vm_end)) 56186c2ad19SMichel Lespinasse return -EFAULT; 56286c2ad19SMichel Lespinasse return address; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds 5656219049aSBob Liu pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address) 5666219049aSBob Liu { 5676219049aSBob Liu pgd_t *pgd; 5686219049aSBob Liu pud_t *pud; 5696219049aSBob Liu pmd_t *pmd = NULL; 570f72e7dcdSHugh Dickins pmd_t pmde; 5716219049aSBob Liu 5726219049aSBob Liu pgd = pgd_offset(mm, address); 5736219049aSBob Liu if (!pgd_present(*pgd)) 5746219049aSBob Liu goto out; 5756219049aSBob Liu 5766219049aSBob Liu pud = pud_offset(pgd, address); 5776219049aSBob Liu if (!pud_present(*pud)) 5786219049aSBob Liu goto out; 5796219049aSBob Liu 5806219049aSBob Liu pmd = pmd_offset(pud, address); 581f72e7dcdSHugh Dickins /* 582f72e7dcdSHugh Dickins * Some THP functions use the sequence pmdp_clear_flush(), set_pmd_at() 583f72e7dcdSHugh Dickins * without holding anon_vma lock for write. So when looking for a 584f72e7dcdSHugh Dickins * genuine pmde (in which to find pte), test present and !THP together. 585f72e7dcdSHugh Dickins */ 586*e37c6982SChristian Borntraeger pmde = *pmd; 587*e37c6982SChristian Borntraeger barrier(); 588f72e7dcdSHugh Dickins if (!pmd_present(pmde) || pmd_trans_huge(pmde)) 5896219049aSBob Liu pmd = NULL; 5906219049aSBob Liu out: 5916219049aSBob Liu return pmd; 5926219049aSBob Liu } 5936219049aSBob Liu 5941da177e4SLinus Torvalds /* 59581b4082dSNikita Danilov * Check that @page is mapped at @address into @mm. 59681b4082dSNikita Danilov * 597479db0bfSNick Piggin * If @sync is false, page_check_address may perform a racy check to avoid 598479db0bfSNick Piggin * the page table lock when the pte is not present (helpful when reclaiming 599479db0bfSNick Piggin * highly shared pages). 600479db0bfSNick Piggin * 601b8072f09SHugh Dickins * On success returns with pte mapped and locked. 60281b4082dSNikita Danilov */ 603e9a81a82SNamhyung Kim pte_t *__page_check_address(struct page *page, struct mm_struct *mm, 604479db0bfSNick Piggin unsigned long address, spinlock_t **ptlp, int sync) 60581b4082dSNikita Danilov { 60681b4082dSNikita Danilov pmd_t *pmd; 60781b4082dSNikita Danilov pte_t *pte; 608c0718806SHugh Dickins spinlock_t *ptl; 60981b4082dSNikita Danilov 6100fe6e20bSNaoya Horiguchi if (unlikely(PageHuge(page))) { 61198398c32SJianguo Wu /* when pud is not present, pte will be NULL */ 6120fe6e20bSNaoya Horiguchi pte = huge_pte_offset(mm, address); 61398398c32SJianguo Wu if (!pte) 61498398c32SJianguo Wu return NULL; 61598398c32SJianguo Wu 616cb900f41SKirill A. Shutemov ptl = huge_pte_lockptr(page_hstate(page), mm, pte); 6170fe6e20bSNaoya Horiguchi goto check; 6180fe6e20bSNaoya Horiguchi } 6190fe6e20bSNaoya Horiguchi 6206219049aSBob Liu pmd = mm_find_pmd(mm, address); 6216219049aSBob Liu if (!pmd) 622c0718806SHugh Dickins return NULL; 623c0718806SHugh Dickins 62481b4082dSNikita Danilov pte = pte_offset_map(pmd, address); 625c0718806SHugh Dickins /* Make a quick check before getting the lock */ 626479db0bfSNick Piggin if (!sync && !pte_present(*pte)) { 62781b4082dSNikita Danilov pte_unmap(pte); 628c0718806SHugh Dickins return NULL; 62981b4082dSNikita Danilov } 630c0718806SHugh Dickins 6314c21e2f2SHugh Dickins ptl = pte_lockptr(mm, pmd); 6320fe6e20bSNaoya Horiguchi check: 633c0718806SHugh Dickins spin_lock(ptl); 634c0718806SHugh Dickins if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) { 635c0718806SHugh Dickins *ptlp = ptl; 636c0718806SHugh Dickins return pte; 63781b4082dSNikita Danilov } 638c0718806SHugh Dickins pte_unmap_unlock(pte, ptl); 639c0718806SHugh Dickins return NULL; 64081b4082dSNikita Danilov } 64181b4082dSNikita Danilov 642b291f000SNick Piggin /** 643b291f000SNick Piggin * page_mapped_in_vma - check whether a page is really mapped in a VMA 644b291f000SNick Piggin * @page: the page to test 645b291f000SNick Piggin * @vma: the VMA to test 646b291f000SNick Piggin * 647b291f000SNick Piggin * Returns 1 if the page is mapped into the page tables of the VMA, 0 648b291f000SNick Piggin * if the page is not mapped into the page tables of this VMA. Only 649b291f000SNick Piggin * valid for normal file or anonymous VMAs. 650b291f000SNick Piggin */ 6516a46079cSAndi Kleen int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma) 652b291f000SNick Piggin { 653b291f000SNick Piggin unsigned long address; 654b291f000SNick Piggin pte_t *pte; 655b291f000SNick Piggin spinlock_t *ptl; 656b291f000SNick Piggin 65786c2ad19SMichel Lespinasse address = __vma_address(page, vma); 65886c2ad19SMichel Lespinasse if (unlikely(address < vma->vm_start || address >= vma->vm_end)) 659b291f000SNick Piggin return 0; 660b291f000SNick Piggin pte = page_check_address(page, vma->vm_mm, address, &ptl, 1); 661b291f000SNick Piggin if (!pte) /* the page is not in this mm */ 662b291f000SNick Piggin return 0; 663b291f000SNick Piggin pte_unmap_unlock(pte, ptl); 664b291f000SNick Piggin 665b291f000SNick Piggin return 1; 666b291f000SNick Piggin } 667b291f000SNick Piggin 6689f32624bSJoonsoo Kim struct page_referenced_arg { 6699f32624bSJoonsoo Kim int mapcount; 6709f32624bSJoonsoo Kim int referenced; 6719f32624bSJoonsoo Kim unsigned long vm_flags; 6729f32624bSJoonsoo Kim struct mem_cgroup *memcg; 6739f32624bSJoonsoo Kim }; 67481b4082dSNikita Danilov /* 6759f32624bSJoonsoo Kim * arg: page_referenced_arg will be passed 6761da177e4SLinus Torvalds */ 677ac769501SKirill A. Shutemov static int page_referenced_one(struct page *page, struct vm_area_struct *vma, 6789f32624bSJoonsoo Kim unsigned long address, void *arg) 6791da177e4SLinus Torvalds { 6801da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 681117b0791SKirill A. Shutemov spinlock_t *ptl; 6821da177e4SLinus Torvalds int referenced = 0; 6839f32624bSJoonsoo Kim struct page_referenced_arg *pra = arg; 6841da177e4SLinus Torvalds 6852da28bfdSAndrea Arcangeli if (unlikely(PageTransHuge(page))) { 6862da28bfdSAndrea Arcangeli pmd_t *pmd; 6872da28bfdSAndrea Arcangeli 688b291f000SNick Piggin /* 6892da28bfdSAndrea Arcangeli * rmap might return false positives; we must filter 6902da28bfdSAndrea Arcangeli * these out using page_check_address_pmd(). 691b291f000SNick Piggin */ 6922da28bfdSAndrea Arcangeli pmd = page_check_address_pmd(page, mm, address, 693117b0791SKirill A. Shutemov PAGE_CHECK_ADDRESS_PMD_FLAG, &ptl); 694117b0791SKirill A. Shutemov if (!pmd) 6959f32624bSJoonsoo Kim return SWAP_AGAIN; 6962da28bfdSAndrea Arcangeli 6975a9bbdcdSHugh Dickins if (vma->vm_flags & VM_LOCKED) { 698117b0791SKirill A. Shutemov spin_unlock(ptl); 6999f32624bSJoonsoo Kim pra->vm_flags |= VM_LOCKED; 7009f32624bSJoonsoo Kim return SWAP_FAIL; /* To break the loop */ 701b291f000SNick Piggin } 702b291f000SNick Piggin 7032da28bfdSAndrea Arcangeli /* go ahead even if the pmd is pmd_trans_splitting() */ 7042da28bfdSAndrea Arcangeli if (pmdp_clear_flush_young_notify(vma, address, pmd)) 70571e3aac0SAndrea Arcangeli referenced++; 706117b0791SKirill A. Shutemov spin_unlock(ptl); 70771e3aac0SAndrea Arcangeli } else { 70871e3aac0SAndrea Arcangeli pte_t *pte; 70971e3aac0SAndrea Arcangeli 7102da28bfdSAndrea Arcangeli /* 7112da28bfdSAndrea Arcangeli * rmap might return false positives; we must filter 7122da28bfdSAndrea Arcangeli * these out using page_check_address(). 7132da28bfdSAndrea Arcangeli */ 71471e3aac0SAndrea Arcangeli pte = page_check_address(page, mm, address, &ptl, 0); 71571e3aac0SAndrea Arcangeli if (!pte) 7169f32624bSJoonsoo Kim return SWAP_AGAIN; 71771e3aac0SAndrea Arcangeli 7182da28bfdSAndrea Arcangeli if (vma->vm_flags & VM_LOCKED) { 7192da28bfdSAndrea Arcangeli pte_unmap_unlock(pte, ptl); 7209f32624bSJoonsoo Kim pra->vm_flags |= VM_LOCKED; 7219f32624bSJoonsoo Kim return SWAP_FAIL; /* To break the loop */ 7222da28bfdSAndrea Arcangeli } 7232da28bfdSAndrea Arcangeli 7244917e5d0SJohannes Weiner if (ptep_clear_flush_young_notify(vma, address, pte)) { 7254917e5d0SJohannes Weiner /* 7264917e5d0SJohannes Weiner * Don't treat a reference through a sequentially read 7274917e5d0SJohannes Weiner * mapping as such. If the page has been used in 7284917e5d0SJohannes Weiner * another mapping, we will catch it; if this other 7294917e5d0SJohannes Weiner * mapping is already gone, the unmap path will have 7304917e5d0SJohannes Weiner * set PG_referenced or activated the page. 7314917e5d0SJohannes Weiner */ 73264363aadSJoe Perches if (likely(!(vma->vm_flags & VM_SEQ_READ))) 7331da177e4SLinus Torvalds referenced++; 7344917e5d0SJohannes Weiner } 735c0718806SHugh Dickins pte_unmap_unlock(pte, ptl); 73671e3aac0SAndrea Arcangeli } 73771e3aac0SAndrea Arcangeli 7389f32624bSJoonsoo Kim if (referenced) { 7399f32624bSJoonsoo Kim pra->referenced++; 7409f32624bSJoonsoo Kim pra->vm_flags |= vma->vm_flags; 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds 7439f32624bSJoonsoo Kim pra->mapcount--; 7449f32624bSJoonsoo Kim if (!pra->mapcount) 7459f32624bSJoonsoo Kim return SWAP_SUCCESS; /* To break the loop */ 7469f32624bSJoonsoo Kim 7479f32624bSJoonsoo Kim return SWAP_AGAIN; 7489f32624bSJoonsoo Kim } 7499f32624bSJoonsoo Kim 7509f32624bSJoonsoo Kim static bool invalid_page_referenced_vma(struct vm_area_struct *vma, void *arg) 7511da177e4SLinus Torvalds { 7529f32624bSJoonsoo Kim struct page_referenced_arg *pra = arg; 7539f32624bSJoonsoo Kim struct mem_cgroup *memcg = pra->memcg; 7541da177e4SLinus Torvalds 7559f32624bSJoonsoo Kim if (!mm_match_cgroup(vma->vm_mm, memcg)) 7569f32624bSJoonsoo Kim return true; 7571da177e4SLinus Torvalds 7589f32624bSJoonsoo Kim return false; 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds /** 7621da177e4SLinus Torvalds * page_referenced - test if the page was referenced 7631da177e4SLinus Torvalds * @page: the page to test 7641da177e4SLinus Torvalds * @is_locked: caller holds lock on the page 76572835c86SJohannes Weiner * @memcg: target memory cgroup 7666fe6b7e3SWu Fengguang * @vm_flags: collect encountered vma->vm_flags who actually referenced the page 7671da177e4SLinus Torvalds * 7681da177e4SLinus Torvalds * Quick test_and_clear_referenced for all mappings to a page, 7691da177e4SLinus Torvalds * returns the number of ptes which referenced the page. 7701da177e4SLinus Torvalds */ 7716fe6b7e3SWu Fengguang int page_referenced(struct page *page, 7726fe6b7e3SWu Fengguang int is_locked, 77372835c86SJohannes Weiner struct mem_cgroup *memcg, 7746fe6b7e3SWu Fengguang unsigned long *vm_flags) 7751da177e4SLinus Torvalds { 7769f32624bSJoonsoo Kim int ret; 7775ad64688SHugh Dickins int we_locked = 0; 7789f32624bSJoonsoo Kim struct page_referenced_arg pra = { 7799f32624bSJoonsoo Kim .mapcount = page_mapcount(page), 7809f32624bSJoonsoo Kim .memcg = memcg, 7819f32624bSJoonsoo Kim }; 7829f32624bSJoonsoo Kim struct rmap_walk_control rwc = { 7839f32624bSJoonsoo Kim .rmap_one = page_referenced_one, 7849f32624bSJoonsoo Kim .arg = (void *)&pra, 7859f32624bSJoonsoo Kim .anon_lock = page_lock_anon_vma_read, 7869f32624bSJoonsoo Kim }; 7871da177e4SLinus Torvalds 7886fe6b7e3SWu Fengguang *vm_flags = 0; 7899f32624bSJoonsoo Kim if (!page_mapped(page)) 7909f32624bSJoonsoo Kim return 0; 7919f32624bSJoonsoo Kim 7929f32624bSJoonsoo Kim if (!page_rmapping(page)) 7939f32624bSJoonsoo Kim return 0; 7949f32624bSJoonsoo Kim 7955ad64688SHugh Dickins if (!is_locked && (!PageAnon(page) || PageKsm(page))) { 7965ad64688SHugh Dickins we_locked = trylock_page(page); 7979f32624bSJoonsoo Kim if (!we_locked) 7989f32624bSJoonsoo Kim return 1; 7995ad64688SHugh Dickins } 8009f32624bSJoonsoo Kim 8019f32624bSJoonsoo Kim /* 8029f32624bSJoonsoo Kim * If we are reclaiming on behalf of a cgroup, skip 8039f32624bSJoonsoo Kim * counting on behalf of references from different 8049f32624bSJoonsoo Kim * cgroups 8059f32624bSJoonsoo Kim */ 8069f32624bSJoonsoo Kim if (memcg) { 8079f32624bSJoonsoo Kim rwc.invalid_vma = invalid_page_referenced_vma; 8085ad64688SHugh Dickins } 8099f32624bSJoonsoo Kim 8109f32624bSJoonsoo Kim ret = rmap_walk(page, &rwc); 8119f32624bSJoonsoo Kim *vm_flags = pra.vm_flags; 8129f32624bSJoonsoo Kim 8135ad64688SHugh Dickins if (we_locked) 8141da177e4SLinus Torvalds unlock_page(page); 8159f32624bSJoonsoo Kim 8169f32624bSJoonsoo Kim return pra.referenced; 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds 8191cb1729bSHugh Dickins static int page_mkclean_one(struct page *page, struct vm_area_struct *vma, 8209853a407SJoonsoo Kim unsigned long address, void *arg) 821d08b3851SPeter Zijlstra { 822d08b3851SPeter Zijlstra struct mm_struct *mm = vma->vm_mm; 823c2fda5feSPeter Zijlstra pte_t *pte; 824d08b3851SPeter Zijlstra spinlock_t *ptl; 825d08b3851SPeter Zijlstra int ret = 0; 8269853a407SJoonsoo Kim int *cleaned = arg; 827d08b3851SPeter Zijlstra 828479db0bfSNick Piggin pte = page_check_address(page, mm, address, &ptl, 1); 829d08b3851SPeter Zijlstra if (!pte) 830d08b3851SPeter Zijlstra goto out; 831d08b3851SPeter Zijlstra 832c2fda5feSPeter Zijlstra if (pte_dirty(*pte) || pte_write(*pte)) { 833c2fda5feSPeter Zijlstra pte_t entry; 834d08b3851SPeter Zijlstra 835c2fda5feSPeter Zijlstra flush_cache_page(vma, address, pte_pfn(*pte)); 8362ec74c3eSSagi Grimberg entry = ptep_clear_flush(vma, address, pte); 837d08b3851SPeter Zijlstra entry = pte_wrprotect(entry); 838c2fda5feSPeter Zijlstra entry = pte_mkclean(entry); 839d6e88e67SAl Viro set_pte_at(mm, address, pte, entry); 840d08b3851SPeter Zijlstra ret = 1; 841c2fda5feSPeter Zijlstra } 842d08b3851SPeter Zijlstra 843d08b3851SPeter Zijlstra pte_unmap_unlock(pte, ptl); 8442ec74c3eSSagi Grimberg 8459853a407SJoonsoo Kim if (ret) { 8462ec74c3eSSagi Grimberg mmu_notifier_invalidate_page(mm, address); 8479853a407SJoonsoo Kim (*cleaned)++; 8489853a407SJoonsoo Kim } 849d08b3851SPeter Zijlstra out: 8509853a407SJoonsoo Kim return SWAP_AGAIN; 851d08b3851SPeter Zijlstra } 852d08b3851SPeter Zijlstra 8539853a407SJoonsoo Kim static bool invalid_mkclean_vma(struct vm_area_struct *vma, void *arg) 854d08b3851SPeter Zijlstra { 8559853a407SJoonsoo Kim if (vma->vm_flags & VM_SHARED) 856871beb8cSFengguang Wu return false; 857d08b3851SPeter Zijlstra 858871beb8cSFengguang Wu return true; 859d08b3851SPeter Zijlstra } 860d08b3851SPeter Zijlstra 861d08b3851SPeter Zijlstra int page_mkclean(struct page *page) 862d08b3851SPeter Zijlstra { 8639853a407SJoonsoo Kim int cleaned = 0; 8649853a407SJoonsoo Kim struct address_space *mapping; 8659853a407SJoonsoo Kim struct rmap_walk_control rwc = { 8669853a407SJoonsoo Kim .arg = (void *)&cleaned, 8679853a407SJoonsoo Kim .rmap_one = page_mkclean_one, 8689853a407SJoonsoo Kim .invalid_vma = invalid_mkclean_vma, 8699853a407SJoonsoo Kim }; 870d08b3851SPeter Zijlstra 871d08b3851SPeter Zijlstra BUG_ON(!PageLocked(page)); 872d08b3851SPeter Zijlstra 8739853a407SJoonsoo Kim if (!page_mapped(page)) 8749853a407SJoonsoo Kim return 0; 875d08b3851SPeter Zijlstra 8769853a407SJoonsoo Kim mapping = page_mapping(page); 8779853a407SJoonsoo Kim if (!mapping) 8789853a407SJoonsoo Kim return 0; 8799853a407SJoonsoo Kim 8809853a407SJoonsoo Kim rmap_walk(page, &rwc); 8819853a407SJoonsoo Kim 8829853a407SJoonsoo Kim return cleaned; 883d08b3851SPeter Zijlstra } 88460b59beaSJaya Kumar EXPORT_SYMBOL_GPL(page_mkclean); 885d08b3851SPeter Zijlstra 8861da177e4SLinus Torvalds /** 887c44b6743SRik van Riel * page_move_anon_rmap - move a page to our anon_vma 888c44b6743SRik van Riel * @page: the page to move to our anon_vma 889c44b6743SRik van Riel * @vma: the vma the page belongs to 890c44b6743SRik van Riel * @address: the user virtual address mapped 891c44b6743SRik van Riel * 892c44b6743SRik van Riel * When a page belongs exclusively to one process after a COW event, 893c44b6743SRik van Riel * that page can be moved into the anon_vma that belongs to just that 894c44b6743SRik van Riel * process, so the rmap code will not search the parent or sibling 895c44b6743SRik van Riel * processes. 896c44b6743SRik van Riel */ 897c44b6743SRik van Riel void page_move_anon_rmap(struct page *page, 898c44b6743SRik van Riel struct vm_area_struct *vma, unsigned long address) 899c44b6743SRik van Riel { 900c44b6743SRik van Riel struct anon_vma *anon_vma = vma->anon_vma; 901c44b6743SRik van Riel 902309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 90381d1b09cSSasha Levin VM_BUG_ON_VMA(!anon_vma, vma); 904309381feSSasha Levin VM_BUG_ON_PAGE(page->index != linear_page_index(vma, address), page); 905c44b6743SRik van Riel 906c44b6743SRik van Riel anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON; 907c44b6743SRik van Riel page->mapping = (struct address_space *) anon_vma; 908c44b6743SRik van Riel } 909c44b6743SRik van Riel 910c44b6743SRik van Riel /** 91143d8eac4SRandy Dunlap * __page_set_anon_rmap - set up new anonymous rmap 9124e1c1975SAndi Kleen * @page: Page to add to rmap 9134e1c1975SAndi Kleen * @vma: VM area to add page to. 9144e1c1975SAndi Kleen * @address: User virtual address of the mapping 915e8a03febSRik van Riel * @exclusive: the page is exclusively owned by the current process 9161da177e4SLinus Torvalds */ 9179617d95eSNick Piggin static void __page_set_anon_rmap(struct page *page, 918e8a03febSRik van Riel struct vm_area_struct *vma, unsigned long address, int exclusive) 9191da177e4SLinus Torvalds { 920e8a03febSRik van Riel struct anon_vma *anon_vma = vma->anon_vma; 9212822c1aaSNick Piggin 922e8a03febSRik van Riel BUG_ON(!anon_vma); 923ea90002bSLinus Torvalds 9244e1c1975SAndi Kleen if (PageAnon(page)) 9254e1c1975SAndi Kleen return; 9264e1c1975SAndi Kleen 927ea90002bSLinus Torvalds /* 928e8a03febSRik van Riel * If the page isn't exclusively mapped into this vma, 929e8a03febSRik van Riel * we must use the _oldest_ possible anon_vma for the 930e8a03febSRik van Riel * page mapping! 931ea90002bSLinus Torvalds */ 9324e1c1975SAndi Kleen if (!exclusive) 933288468c3SAndrea Arcangeli anon_vma = anon_vma->root; 934ea90002bSLinus Torvalds 9351da177e4SLinus Torvalds anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON; 9362822c1aaSNick Piggin page->mapping = (struct address_space *) anon_vma; 9374d7670e0SNick Piggin page->index = linear_page_index(vma, address); 9381da177e4SLinus Torvalds } 9399617d95eSNick Piggin 9409617d95eSNick Piggin /** 94143d8eac4SRandy Dunlap * __page_check_anon_rmap - sanity check anonymous rmap addition 942c97a9e10SNick Piggin * @page: the page to add the mapping to 943c97a9e10SNick Piggin * @vma: the vm area in which the mapping is added 944c97a9e10SNick Piggin * @address: the user virtual address mapped 945c97a9e10SNick Piggin */ 946c97a9e10SNick Piggin static void __page_check_anon_rmap(struct page *page, 947c97a9e10SNick Piggin struct vm_area_struct *vma, unsigned long address) 948c97a9e10SNick Piggin { 949c97a9e10SNick Piggin #ifdef CONFIG_DEBUG_VM 950c97a9e10SNick Piggin /* 951c97a9e10SNick Piggin * The page's anon-rmap details (mapping and index) are guaranteed to 952c97a9e10SNick Piggin * be set up correctly at this point. 953c97a9e10SNick Piggin * 954c97a9e10SNick Piggin * We have exclusion against page_add_anon_rmap because the caller 955c97a9e10SNick Piggin * always holds the page locked, except if called from page_dup_rmap, 956c97a9e10SNick Piggin * in which case the page is already known to be setup. 957c97a9e10SNick Piggin * 958c97a9e10SNick Piggin * We have exclusion against page_add_new_anon_rmap because those pages 959c97a9e10SNick Piggin * are initially only visible via the pagetables, and the pte is locked 960c97a9e10SNick Piggin * over the call to page_add_new_anon_rmap. 961c97a9e10SNick Piggin */ 96244ab57a0SAndrea Arcangeli BUG_ON(page_anon_vma(page)->root != vma->anon_vma->root); 963c97a9e10SNick Piggin BUG_ON(page->index != linear_page_index(vma, address)); 964c97a9e10SNick Piggin #endif 965c97a9e10SNick Piggin } 966c97a9e10SNick Piggin 967c97a9e10SNick Piggin /** 9689617d95eSNick Piggin * page_add_anon_rmap - add pte mapping to an anonymous page 9699617d95eSNick Piggin * @page: the page to add the mapping to 9709617d95eSNick Piggin * @vma: the vm area in which the mapping is added 9719617d95eSNick Piggin * @address: the user virtual address mapped 9729617d95eSNick Piggin * 9735ad64688SHugh Dickins * The caller needs to hold the pte lock, and the page must be locked in 97480e14822SHugh Dickins * the anon_vma case: to serialize mapping,index checking after setting, 97580e14822SHugh Dickins * and to ensure that PageAnon is not being upgraded racily to PageKsm 97680e14822SHugh Dickins * (but PageKsm is never downgraded to PageAnon). 9779617d95eSNick Piggin */ 9789617d95eSNick Piggin void page_add_anon_rmap(struct page *page, 9799617d95eSNick Piggin struct vm_area_struct *vma, unsigned long address) 9809617d95eSNick Piggin { 981ad8c2ee8SRik van Riel do_page_add_anon_rmap(page, vma, address, 0); 982ad8c2ee8SRik van Riel } 983ad8c2ee8SRik van Riel 984ad8c2ee8SRik van Riel /* 985ad8c2ee8SRik van Riel * Special version of the above for do_swap_page, which often runs 986ad8c2ee8SRik van Riel * into pages that are exclusively owned by the current process. 987ad8c2ee8SRik van Riel * Everybody else should continue to use page_add_anon_rmap above. 988ad8c2ee8SRik van Riel */ 989ad8c2ee8SRik van Riel void do_page_add_anon_rmap(struct page *page, 990ad8c2ee8SRik van Riel struct vm_area_struct *vma, unsigned long address, int exclusive) 991ad8c2ee8SRik van Riel { 9925ad64688SHugh Dickins int first = atomic_inc_and_test(&page->_mapcount); 99379134171SAndrea Arcangeli if (first) { 994bea04b07SJianyu Zhan /* 995bea04b07SJianyu Zhan * We use the irq-unsafe __{inc|mod}_zone_page_stat because 996bea04b07SJianyu Zhan * these counters are not modified in interrupt context, and 997bea04b07SJianyu Zhan * pte lock(a spinlock) is held, which implies preemption 998bea04b07SJianyu Zhan * disabled. 999bea04b07SJianyu Zhan */ 10003cd14fcdSKirill A. Shutemov if (PageTransHuge(page)) 100179134171SAndrea Arcangeli __inc_zone_page_state(page, 100279134171SAndrea Arcangeli NR_ANON_TRANSPARENT_HUGEPAGES); 10033cd14fcdSKirill A. Shutemov __mod_zone_page_state(page_zone(page), NR_ANON_PAGES, 10043cd14fcdSKirill A. Shutemov hpage_nr_pages(page)); 100579134171SAndrea Arcangeli } 10065ad64688SHugh Dickins if (unlikely(PageKsm(page))) 10075ad64688SHugh Dickins return; 10085ad64688SHugh Dickins 1009309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 10105dbe0af4SHugh Dickins /* address might be in next vma when migration races vma_adjust */ 10115ad64688SHugh Dickins if (first) 1012ad8c2ee8SRik van Riel __page_set_anon_rmap(page, vma, address, exclusive); 101369029cd5SKAMEZAWA Hiroyuki else 1014c97a9e10SNick Piggin __page_check_anon_rmap(page, vma, address); 10151da177e4SLinus Torvalds } 10161da177e4SLinus Torvalds 101743d8eac4SRandy Dunlap /** 10189617d95eSNick Piggin * page_add_new_anon_rmap - add pte mapping to a new anonymous page 10199617d95eSNick Piggin * @page: the page to add the mapping to 10209617d95eSNick Piggin * @vma: the vm area in which the mapping is added 10219617d95eSNick Piggin * @address: the user virtual address mapped 10229617d95eSNick Piggin * 10239617d95eSNick Piggin * Same as page_add_anon_rmap but must only be called on *new* pages. 10249617d95eSNick Piggin * This means the inc-and-test can be bypassed. 1025c97a9e10SNick Piggin * Page does not have to be locked. 10269617d95eSNick Piggin */ 10279617d95eSNick Piggin void page_add_new_anon_rmap(struct page *page, 10289617d95eSNick Piggin struct vm_area_struct *vma, unsigned long address) 10299617d95eSNick Piggin { 103081d1b09cSSasha Levin VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma); 1031cbf84b7aSHugh Dickins SetPageSwapBacked(page); 1032cbf84b7aSHugh Dickins atomic_set(&page->_mapcount, 0); /* increment count (starts at -1) */ 10333cd14fcdSKirill A. Shutemov if (PageTransHuge(page)) 103479134171SAndrea Arcangeli __inc_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES); 10353cd14fcdSKirill A. Shutemov __mod_zone_page_state(page_zone(page), NR_ANON_PAGES, 10363cd14fcdSKirill A. Shutemov hpage_nr_pages(page)); 1037e8a03febSRik van Riel __page_set_anon_rmap(page, vma, address, 1); 10389617d95eSNick Piggin } 10399617d95eSNick Piggin 10401da177e4SLinus Torvalds /** 10411da177e4SLinus Torvalds * page_add_file_rmap - add pte mapping to a file page 10421da177e4SLinus Torvalds * @page: the page to add the mapping to 10431da177e4SLinus Torvalds * 1044b8072f09SHugh Dickins * The caller needs to hold the pte lock. 10451da177e4SLinus Torvalds */ 10461da177e4SLinus Torvalds void page_add_file_rmap(struct page *page) 10471da177e4SLinus Torvalds { 1048d7365e78SJohannes Weiner struct mem_cgroup *memcg; 104989c06bd5SKAMEZAWA Hiroyuki unsigned long flags; 1050d7365e78SJohannes Weiner bool locked; 105189c06bd5SKAMEZAWA Hiroyuki 1052d7365e78SJohannes Weiner memcg = mem_cgroup_begin_page_stat(page, &locked, &flags); 1053d69b042fSBalbir Singh if (atomic_inc_and_test(&page->_mapcount)) { 105465ba55f5SChristoph Lameter __inc_zone_page_state(page, NR_FILE_MAPPED); 1055d7365e78SJohannes Weiner mem_cgroup_inc_page_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED); 1056d69b042fSBalbir Singh } 1057e4bd6a02SMichal Hocko mem_cgroup_end_page_stat(memcg, &locked, &flags); 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 10608186eb6aSJohannes Weiner static void page_remove_file_rmap(struct page *page) 10618186eb6aSJohannes Weiner { 10628186eb6aSJohannes Weiner struct mem_cgroup *memcg; 10638186eb6aSJohannes Weiner unsigned long flags; 10648186eb6aSJohannes Weiner bool locked; 10658186eb6aSJohannes Weiner 10668186eb6aSJohannes Weiner memcg = mem_cgroup_begin_page_stat(page, &locked, &flags); 10678186eb6aSJohannes Weiner 10688186eb6aSJohannes Weiner /* page still mapped by someone else? */ 10698186eb6aSJohannes Weiner if (!atomic_add_negative(-1, &page->_mapcount)) 10708186eb6aSJohannes Weiner goto out; 10718186eb6aSJohannes Weiner 10728186eb6aSJohannes Weiner /* Hugepages are not counted in NR_FILE_MAPPED for now. */ 10738186eb6aSJohannes Weiner if (unlikely(PageHuge(page))) 10748186eb6aSJohannes Weiner goto out; 10758186eb6aSJohannes Weiner 10768186eb6aSJohannes Weiner /* 10778186eb6aSJohannes Weiner * We use the irq-unsafe __{inc|mod}_zone_page_stat because 10788186eb6aSJohannes Weiner * these counters are not modified in interrupt context, and 10798186eb6aSJohannes Weiner * pte lock(a spinlock) is held, which implies preemption disabled. 10808186eb6aSJohannes Weiner */ 10818186eb6aSJohannes Weiner __dec_zone_page_state(page, NR_FILE_MAPPED); 10828186eb6aSJohannes Weiner mem_cgroup_dec_page_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED); 10838186eb6aSJohannes Weiner 10848186eb6aSJohannes Weiner if (unlikely(PageMlocked(page))) 10858186eb6aSJohannes Weiner clear_page_mlock(page); 10868186eb6aSJohannes Weiner out: 1087e4bd6a02SMichal Hocko mem_cgroup_end_page_stat(memcg, &locked, &flags); 10888186eb6aSJohannes Weiner } 10898186eb6aSJohannes Weiner 10901da177e4SLinus Torvalds /** 10911da177e4SLinus Torvalds * page_remove_rmap - take down pte mapping from a page 10921da177e4SLinus Torvalds * @page: page to remove mapping from 10931da177e4SLinus Torvalds * 1094b8072f09SHugh Dickins * The caller needs to hold the pte lock. 10951da177e4SLinus Torvalds */ 1096edc315fdSHugh Dickins void page_remove_rmap(struct page *page) 10971da177e4SLinus Torvalds { 10988186eb6aSJohannes Weiner if (!PageAnon(page)) { 10998186eb6aSJohannes Weiner page_remove_file_rmap(page); 11008186eb6aSJohannes Weiner return; 11018186eb6aSJohannes Weiner } 110289c06bd5SKAMEZAWA Hiroyuki 1103b904dcfeSKOSAKI Motohiro /* page still mapped by someone else? */ 1104b904dcfeSKOSAKI Motohiro if (!atomic_add_negative(-1, &page->_mapcount)) 11058186eb6aSJohannes Weiner return; 11068186eb6aSJohannes Weiner 11078186eb6aSJohannes Weiner /* Hugepages are not counted in NR_ANON_PAGES for now. */ 11088186eb6aSJohannes Weiner if (unlikely(PageHuge(page))) 11098186eb6aSJohannes Weiner return; 1110b904dcfeSKOSAKI Motohiro 11111da177e4SLinus Torvalds /* 1112bea04b07SJianyu Zhan * We use the irq-unsafe __{inc|mod}_zone_page_stat because 1113bea04b07SJianyu Zhan * these counters are not modified in interrupt context, and 1114bea04b07SJianyu Zhan * pte lock(a spinlock) is held, which implies preemption disabled. 11150fe6e20bSNaoya Horiguchi */ 11163cd14fcdSKirill A. Shutemov if (PageTransHuge(page)) 11178186eb6aSJohannes Weiner __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES); 11188186eb6aSJohannes Weiner 11193cd14fcdSKirill A. Shutemov __mod_zone_page_state(page_zone(page), NR_ANON_PAGES, 11203cd14fcdSKirill A. Shutemov -hpage_nr_pages(page)); 11218186eb6aSJohannes Weiner 1122e6c509f8SHugh Dickins if (unlikely(PageMlocked(page))) 1123e6c509f8SHugh Dickins clear_page_mlock(page); 11248186eb6aSJohannes Weiner 112516f8c5b2SHugh Dickins /* 11261da177e4SLinus Torvalds * It would be tidy to reset the PageAnon mapping here, 11271da177e4SLinus Torvalds * but that might overwrite a racing page_add_anon_rmap 11281da177e4SLinus Torvalds * which increments mapcount after us but sets mapping 11291da177e4SLinus Torvalds * before us: so leave the reset to free_hot_cold_page, 11301da177e4SLinus Torvalds * and remember that it's only reliable while mapped. 11311da177e4SLinus Torvalds * Leaving it set also helps swapoff to reinstate ptes 11321da177e4SLinus Torvalds * faster for those pages still in swapcache. 11331da177e4SLinus Torvalds */ 11341da177e4SLinus Torvalds } 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds /* 113752629506SJoonsoo Kim * @arg: enum ttu_flags will be passed to this argument 11381da177e4SLinus Torvalds */ 1139ac769501SKirill A. Shutemov static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma, 114052629506SJoonsoo Kim unsigned long address, void *arg) 11411da177e4SLinus Torvalds { 11421da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 11431da177e4SLinus Torvalds pte_t *pte; 11441da177e4SLinus Torvalds pte_t pteval; 1145c0718806SHugh Dickins spinlock_t *ptl; 11461da177e4SLinus Torvalds int ret = SWAP_AGAIN; 114752629506SJoonsoo Kim enum ttu_flags flags = (enum ttu_flags)arg; 11481da177e4SLinus Torvalds 1149479db0bfSNick Piggin pte = page_check_address(page, mm, address, &ptl, 0); 1150c0718806SHugh Dickins if (!pte) 115181b4082dSNikita Danilov goto out; 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds /* 11541da177e4SLinus Torvalds * If the page is mlock()d, we cannot swap it out. 11551da177e4SLinus Torvalds * If it's recently referenced (perhaps page_referenced 11561da177e4SLinus Torvalds * skipped over this mm) then we should reactivate it. 11571da177e4SLinus Torvalds */ 115814fa31b8SAndi Kleen if (!(flags & TTU_IGNORE_MLOCK)) { 1159caed0f48SKOSAKI Motohiro if (vma->vm_flags & VM_LOCKED) 1160caed0f48SKOSAKI Motohiro goto out_mlock; 1161caed0f48SKOSAKI Motohiro 1162daa5ba76SKonstantin Khlebnikov if (flags & TTU_MUNLOCK) 116353f79acbSHugh Dickins goto out_unmap; 116414fa31b8SAndi Kleen } 116514fa31b8SAndi Kleen if (!(flags & TTU_IGNORE_ACCESS)) { 1166b291f000SNick Piggin if (ptep_clear_flush_young_notify(vma, address, pte)) { 11671da177e4SLinus Torvalds ret = SWAP_FAIL; 11681da177e4SLinus Torvalds goto out_unmap; 11691da177e4SLinus Torvalds } 1170b291f000SNick Piggin } 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds /* Nuke the page table entry. */ 11731da177e4SLinus Torvalds flush_cache_page(vma, address, page_to_pfn(page)); 11742ec74c3eSSagi Grimberg pteval = ptep_clear_flush(vma, address, pte); 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds /* Move the dirty bit to the physical page now the pte is gone. */ 11771da177e4SLinus Torvalds if (pte_dirty(pteval)) 11781da177e4SLinus Torvalds set_page_dirty(page); 11791da177e4SLinus Torvalds 1180365e9c87SHugh Dickins /* Update high watermark before we lower rss */ 1181365e9c87SHugh Dickins update_hiwater_rss(mm); 1182365e9c87SHugh Dickins 1183888b9f7cSAndi Kleen if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) { 11845f24ae58SNaoya Horiguchi if (!PageHuge(page)) { 1185888b9f7cSAndi Kleen if (PageAnon(page)) 1186d559db08SKAMEZAWA Hiroyuki dec_mm_counter(mm, MM_ANONPAGES); 1187888b9f7cSAndi Kleen else 1188d559db08SKAMEZAWA Hiroyuki dec_mm_counter(mm, MM_FILEPAGES); 11895f24ae58SNaoya Horiguchi } 1190888b9f7cSAndi Kleen set_pte_at(mm, address, pte, 1191888b9f7cSAndi Kleen swp_entry_to_pte(make_hwpoison_entry(page))); 119245961722SKonstantin Weitz } else if (pte_unused(pteval)) { 119345961722SKonstantin Weitz /* 119445961722SKonstantin Weitz * The guest indicated that the page content is of no 119545961722SKonstantin Weitz * interest anymore. Simply discard the pte, vmscan 119645961722SKonstantin Weitz * will take care of the rest. 119745961722SKonstantin Weitz */ 119845961722SKonstantin Weitz if (PageAnon(page)) 119945961722SKonstantin Weitz dec_mm_counter(mm, MM_ANONPAGES); 120045961722SKonstantin Weitz else 120145961722SKonstantin Weitz dec_mm_counter(mm, MM_FILEPAGES); 1202888b9f7cSAndi Kleen } else if (PageAnon(page)) { 12034c21e2f2SHugh Dickins swp_entry_t entry = { .val = page_private(page) }; 1204179ef71cSCyrill Gorcunov pte_t swp_pte; 12050697212aSChristoph Lameter 12060697212aSChristoph Lameter if (PageSwapCache(page)) { 12071da177e4SLinus Torvalds /* 12081da177e4SLinus Torvalds * Store the swap location in the pte. 12091da177e4SLinus Torvalds * See handle_pte_fault() ... 12101da177e4SLinus Torvalds */ 1211570a335bSHugh Dickins if (swap_duplicate(entry) < 0) { 1212570a335bSHugh Dickins set_pte_at(mm, address, pte, pteval); 1213570a335bSHugh Dickins ret = SWAP_FAIL; 1214570a335bSHugh Dickins goto out_unmap; 1215570a335bSHugh Dickins } 12161da177e4SLinus Torvalds if (list_empty(&mm->mmlist)) { 12171da177e4SLinus Torvalds spin_lock(&mmlist_lock); 1218f412ac08SHugh Dickins if (list_empty(&mm->mmlist)) 12191da177e4SLinus Torvalds list_add(&mm->mmlist, &init_mm.mmlist); 12201da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 12211da177e4SLinus Torvalds } 1222d559db08SKAMEZAWA Hiroyuki dec_mm_counter(mm, MM_ANONPAGES); 1223b084d435SKAMEZAWA Hiroyuki inc_mm_counter(mm, MM_SWAPENTS); 1224ce1744f4SKonstantin Khlebnikov } else if (IS_ENABLED(CONFIG_MIGRATION)) { 12250697212aSChristoph Lameter /* 12260697212aSChristoph Lameter * Store the pfn of the page in a special migration 12270697212aSChristoph Lameter * pte. do_swap_page() will wait until the migration 12280697212aSChristoph Lameter * pte is removed and then restart fault handling. 12290697212aSChristoph Lameter */ 1230daa5ba76SKonstantin Khlebnikov BUG_ON(!(flags & TTU_MIGRATION)); 12310697212aSChristoph Lameter entry = make_migration_entry(page, pte_write(pteval)); 12320697212aSChristoph Lameter } 1233179ef71cSCyrill Gorcunov swp_pte = swp_entry_to_pte(entry); 1234179ef71cSCyrill Gorcunov if (pte_soft_dirty(pteval)) 1235179ef71cSCyrill Gorcunov swp_pte = pte_swp_mksoft_dirty(swp_pte); 1236179ef71cSCyrill Gorcunov set_pte_at(mm, address, pte, swp_pte); 12371da177e4SLinus Torvalds BUG_ON(pte_file(*pte)); 1238ce1744f4SKonstantin Khlebnikov } else if (IS_ENABLED(CONFIG_MIGRATION) && 1239daa5ba76SKonstantin Khlebnikov (flags & TTU_MIGRATION)) { 124004e62a29SChristoph Lameter /* Establish migration entry for a file page */ 124104e62a29SChristoph Lameter swp_entry_t entry; 124204e62a29SChristoph Lameter entry = make_migration_entry(page, pte_write(pteval)); 124304e62a29SChristoph Lameter set_pte_at(mm, address, pte, swp_entry_to_pte(entry)); 124404e62a29SChristoph Lameter } else 1245d559db08SKAMEZAWA Hiroyuki dec_mm_counter(mm, MM_FILEPAGES); 12461da177e4SLinus Torvalds 1247edc315fdSHugh Dickins page_remove_rmap(page); 12481da177e4SLinus Torvalds page_cache_release(page); 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds out_unmap: 1251c0718806SHugh Dickins pte_unmap_unlock(pte, ptl); 1252daa5ba76SKonstantin Khlebnikov if (ret != SWAP_FAIL && !(flags & TTU_MUNLOCK)) 12532ec74c3eSSagi Grimberg mmu_notifier_invalidate_page(mm, address); 1254caed0f48SKOSAKI Motohiro out: 1255caed0f48SKOSAKI Motohiro return ret; 125653f79acbSHugh Dickins 1257caed0f48SKOSAKI Motohiro out_mlock: 1258caed0f48SKOSAKI Motohiro pte_unmap_unlock(pte, ptl); 1259caed0f48SKOSAKI Motohiro 1260caed0f48SKOSAKI Motohiro 1261caed0f48SKOSAKI Motohiro /* 1262caed0f48SKOSAKI Motohiro * We need mmap_sem locking, Otherwise VM_LOCKED check makes 1263caed0f48SKOSAKI Motohiro * unstable result and race. Plus, We can't wait here because 1264c8c06efaSDavidlohr Bueso * we now hold anon_vma->rwsem or mapping->i_mmap_rwsem. 1265caed0f48SKOSAKI Motohiro * if trylock failed, the page remain in evictable lru and later 1266caed0f48SKOSAKI Motohiro * vmscan could retry to move the page to unevictable lru if the 1267caed0f48SKOSAKI Motohiro * page is actually mlocked. 1268caed0f48SKOSAKI Motohiro */ 126953f79acbSHugh Dickins if (down_read_trylock(&vma->vm_mm->mmap_sem)) { 127053f79acbSHugh Dickins if (vma->vm_flags & VM_LOCKED) { 127153f79acbSHugh Dickins mlock_vma_page(page); 127253f79acbSHugh Dickins ret = SWAP_MLOCK; 127353f79acbSHugh Dickins } 127453f79acbSHugh Dickins up_read(&vma->vm_mm->mmap_sem); 127553f79acbSHugh Dickins } 12761da177e4SLinus Torvalds return ret; 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds /* 12801da177e4SLinus Torvalds * objrmap doesn't work for nonlinear VMAs because the assumption that 12811da177e4SLinus Torvalds * offset-into-file correlates with offset-into-virtual-addresses does not hold. 12821da177e4SLinus Torvalds * Consequently, given a particular page and its ->index, we cannot locate the 12831da177e4SLinus Torvalds * ptes which are mapping that page without an exhaustive linear search. 12841da177e4SLinus Torvalds * 12851da177e4SLinus Torvalds * So what this code does is a mini "virtual scan" of each nonlinear VMA which 12861da177e4SLinus Torvalds * maps the file to which the target page belongs. The ->vm_private_data field 12871da177e4SLinus Torvalds * holds the current cursor into that scan. Successive searches will circulate 12881da177e4SLinus Torvalds * around the vma's virtual address space. 12891da177e4SLinus Torvalds * 12901da177e4SLinus Torvalds * So as more replacement pressure is applied to the pages in a nonlinear VMA, 12911da177e4SLinus Torvalds * more scanning pressure is placed against them as well. Eventually pages 12921da177e4SLinus Torvalds * will become fully unmapped and are eligible for eviction. 12931da177e4SLinus Torvalds * 12941da177e4SLinus Torvalds * For very sparsely populated VMAs this is a little inefficient - chances are 12951da177e4SLinus Torvalds * there there won't be many ptes located within the scan cluster. In this case 12961da177e4SLinus Torvalds * maybe we could scan further - to the end of the pte page, perhaps. 1297b291f000SNick Piggin * 1298b291f000SNick Piggin * Mlocked pages: check VM_LOCKED under mmap_sem held for read, if we can 1299b291f000SNick Piggin * acquire it without blocking. If vma locked, mlock the pages in the cluster, 1300b291f000SNick Piggin * rather than unmapping them. If we encounter the "check_page" that vmscan is 1301b291f000SNick Piggin * trying to unmap, return SWAP_MLOCK, else default SWAP_AGAIN. 13021da177e4SLinus Torvalds */ 13031da177e4SLinus Torvalds #define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE) 13041da177e4SLinus Torvalds #define CLUSTER_MASK (~(CLUSTER_SIZE - 1)) 13051da177e4SLinus Torvalds 1306b291f000SNick Piggin static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount, 1307b291f000SNick Piggin struct vm_area_struct *vma, struct page *check_page) 13081da177e4SLinus Torvalds { 13091da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 13101da177e4SLinus Torvalds pmd_t *pmd; 1311c0718806SHugh Dickins pte_t *pte; 13121da177e4SLinus Torvalds pte_t pteval; 1313c0718806SHugh Dickins spinlock_t *ptl; 13141da177e4SLinus Torvalds struct page *page; 13151da177e4SLinus Torvalds unsigned long address; 13162ec74c3eSSagi Grimberg unsigned long mmun_start; /* For mmu_notifiers */ 13172ec74c3eSSagi Grimberg unsigned long mmun_end; /* For mmu_notifiers */ 13181da177e4SLinus Torvalds unsigned long end; 1319b291f000SNick Piggin int ret = SWAP_AGAIN; 1320b291f000SNick Piggin int locked_vma = 0; 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds address = (vma->vm_start + cursor) & CLUSTER_MASK; 13231da177e4SLinus Torvalds end = address + CLUSTER_SIZE; 13241da177e4SLinus Torvalds if (address < vma->vm_start) 13251da177e4SLinus Torvalds address = vma->vm_start; 13261da177e4SLinus Torvalds if (end > vma->vm_end) 13271da177e4SLinus Torvalds end = vma->vm_end; 13281da177e4SLinus Torvalds 13296219049aSBob Liu pmd = mm_find_pmd(mm, address); 13306219049aSBob Liu if (!pmd) 1331b291f000SNick Piggin return ret; 1332b291f000SNick Piggin 13332ec74c3eSSagi Grimberg mmun_start = address; 13342ec74c3eSSagi Grimberg mmun_end = end; 13352ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); 13362ec74c3eSSagi Grimberg 1337b291f000SNick Piggin /* 1338af8e3354SHugh Dickins * If we can acquire the mmap_sem for read, and vma is VM_LOCKED, 1339b291f000SNick Piggin * keep the sem while scanning the cluster for mlocking pages. 1340b291f000SNick Piggin */ 1341af8e3354SHugh Dickins if (down_read_trylock(&vma->vm_mm->mmap_sem)) { 1342b291f000SNick Piggin locked_vma = (vma->vm_flags & VM_LOCKED); 1343b291f000SNick Piggin if (!locked_vma) 1344b291f000SNick Piggin up_read(&vma->vm_mm->mmap_sem); /* don't need it */ 1345b291f000SNick Piggin } 1346c0718806SHugh Dickins 1347c0718806SHugh Dickins pte = pte_offset_map_lock(mm, pmd, address, &ptl); 13481da177e4SLinus Torvalds 1349365e9c87SHugh Dickins /* Update high watermark before we lower rss */ 1350365e9c87SHugh Dickins update_hiwater_rss(mm); 1351365e9c87SHugh Dickins 1352c0718806SHugh Dickins for (; address < end; pte++, address += PAGE_SIZE) { 13531da177e4SLinus Torvalds if (!pte_present(*pte)) 13541da177e4SLinus Torvalds continue; 13556aab341eSLinus Torvalds page = vm_normal_page(vma, address, *pte); 13566aab341eSLinus Torvalds BUG_ON(!page || PageAnon(page)); 13571da177e4SLinus Torvalds 1358b291f000SNick Piggin if (locked_vma) { 135957e68e9cSVlastimil Babka if (page == check_page) { 136057e68e9cSVlastimil Babka /* we know we have check_page locked */ 136157e68e9cSVlastimil Babka mlock_vma_page(page); 1362b291f000SNick Piggin ret = SWAP_MLOCK; 136357e68e9cSVlastimil Babka } else if (trylock_page(page)) { 136457e68e9cSVlastimil Babka /* 136557e68e9cSVlastimil Babka * If we can lock the page, perform mlock. 136657e68e9cSVlastimil Babka * Otherwise leave the page alone, it will be 136757e68e9cSVlastimil Babka * eventually encountered again later. 136857e68e9cSVlastimil Babka */ 136957e68e9cSVlastimil Babka mlock_vma_page(page); 137057e68e9cSVlastimil Babka unlock_page(page); 137157e68e9cSVlastimil Babka } 1372b291f000SNick Piggin continue; /* don't unmap */ 1373b291f000SNick Piggin } 1374b291f000SNick Piggin 137557128468SAndres Lagar-Cavilla /* 137657128468SAndres Lagar-Cavilla * No need for _notify because we're within an 137757128468SAndres Lagar-Cavilla * mmu_notifier_invalidate_range_ {start|end} scope. 137857128468SAndres Lagar-Cavilla */ 137957128468SAndres Lagar-Cavilla if (ptep_clear_flush_young(vma, address, pte)) 13801da177e4SLinus Torvalds continue; 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds /* Nuke the page table entry. */ 1383eca35133SBen Collins flush_cache_page(vma, address, pte_pfn(*pte)); 138434ee645eSJoerg Roedel pteval = ptep_clear_flush_notify(vma, address, pte); 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds /* If nonlinear, store the file page offset in the pte. */ 138741bb3476SCyrill Gorcunov if (page->index != linear_page_index(vma, address)) { 138841bb3476SCyrill Gorcunov pte_t ptfile = pgoff_to_pte(page->index); 138941bb3476SCyrill Gorcunov if (pte_soft_dirty(pteval)) 1390b43790eeSCyrill Gorcunov ptfile = pte_file_mksoft_dirty(ptfile); 139141bb3476SCyrill Gorcunov set_pte_at(mm, address, pte, ptfile); 139241bb3476SCyrill Gorcunov } 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds /* Move the dirty bit to the physical page now the pte is gone. */ 13951da177e4SLinus Torvalds if (pte_dirty(pteval)) 13961da177e4SLinus Torvalds set_page_dirty(page); 13971da177e4SLinus Torvalds 1398edc315fdSHugh Dickins page_remove_rmap(page); 13991da177e4SLinus Torvalds page_cache_release(page); 1400d559db08SKAMEZAWA Hiroyuki dec_mm_counter(mm, MM_FILEPAGES); 14011da177e4SLinus Torvalds (*mapcount)--; 14021da177e4SLinus Torvalds } 1403c0718806SHugh Dickins pte_unmap_unlock(pte - 1, ptl); 14042ec74c3eSSagi Grimberg mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); 1405b291f000SNick Piggin if (locked_vma) 1406b291f000SNick Piggin up_read(&vma->vm_mm->mmap_sem); 1407b291f000SNick Piggin return ret; 14081da177e4SLinus Torvalds } 14091da177e4SLinus Torvalds 14100f843c6aSJoonsoo Kim static int try_to_unmap_nonlinear(struct page *page, 14117e09e738SHugh Dickins struct address_space *mapping, void *arg) 14120f843c6aSJoonsoo Kim { 14137e09e738SHugh Dickins struct vm_area_struct *vma; 14140f843c6aSJoonsoo Kim int ret = SWAP_AGAIN; 14150f843c6aSJoonsoo Kim unsigned long cursor; 14160f843c6aSJoonsoo Kim unsigned long max_nl_cursor = 0; 14170f843c6aSJoonsoo Kim unsigned long max_nl_size = 0; 14180f843c6aSJoonsoo Kim unsigned int mapcount; 14190f843c6aSJoonsoo Kim 14200f843c6aSJoonsoo Kim list_for_each_entry(vma, 14210f843c6aSJoonsoo Kim &mapping->i_mmap_nonlinear, shared.nonlinear) { 14220f843c6aSJoonsoo Kim 14230f843c6aSJoonsoo Kim cursor = (unsigned long) vma->vm_private_data; 14240f843c6aSJoonsoo Kim if (cursor > max_nl_cursor) 14250f843c6aSJoonsoo Kim max_nl_cursor = cursor; 14260f843c6aSJoonsoo Kim cursor = vma->vm_end - vma->vm_start; 14270f843c6aSJoonsoo Kim if (cursor > max_nl_size) 14280f843c6aSJoonsoo Kim max_nl_size = cursor; 14290f843c6aSJoonsoo Kim } 14300f843c6aSJoonsoo Kim 14310f843c6aSJoonsoo Kim if (max_nl_size == 0) { /* all nonlinears locked or reserved ? */ 14320f843c6aSJoonsoo Kim return SWAP_FAIL; 14330f843c6aSJoonsoo Kim } 14340f843c6aSJoonsoo Kim 14350f843c6aSJoonsoo Kim /* 14360f843c6aSJoonsoo Kim * We don't try to search for this page in the nonlinear vmas, 14370f843c6aSJoonsoo Kim * and page_referenced wouldn't have found it anyway. Instead 14380f843c6aSJoonsoo Kim * just walk the nonlinear vmas trying to age and unmap some. 14390f843c6aSJoonsoo Kim * The mapcount of the page we came in with is irrelevant, 14400f843c6aSJoonsoo Kim * but even so use it as a guide to how hard we should try? 14410f843c6aSJoonsoo Kim */ 14420f843c6aSJoonsoo Kim mapcount = page_mapcount(page); 14430f843c6aSJoonsoo Kim if (!mapcount) 14440f843c6aSJoonsoo Kim return ret; 14450f843c6aSJoonsoo Kim 14460f843c6aSJoonsoo Kim cond_resched(); 14470f843c6aSJoonsoo Kim 14480f843c6aSJoonsoo Kim max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK; 14490f843c6aSJoonsoo Kim if (max_nl_cursor == 0) 14500f843c6aSJoonsoo Kim max_nl_cursor = CLUSTER_SIZE; 14510f843c6aSJoonsoo Kim 14520f843c6aSJoonsoo Kim do { 14530f843c6aSJoonsoo Kim list_for_each_entry(vma, 14540f843c6aSJoonsoo Kim &mapping->i_mmap_nonlinear, shared.nonlinear) { 14550f843c6aSJoonsoo Kim 14560f843c6aSJoonsoo Kim cursor = (unsigned long) vma->vm_private_data; 14570f843c6aSJoonsoo Kim while (cursor < max_nl_cursor && 14580f843c6aSJoonsoo Kim cursor < vma->vm_end - vma->vm_start) { 14590f843c6aSJoonsoo Kim if (try_to_unmap_cluster(cursor, &mapcount, 14600f843c6aSJoonsoo Kim vma, page) == SWAP_MLOCK) 14610f843c6aSJoonsoo Kim ret = SWAP_MLOCK; 14620f843c6aSJoonsoo Kim cursor += CLUSTER_SIZE; 14630f843c6aSJoonsoo Kim vma->vm_private_data = (void *) cursor; 14640f843c6aSJoonsoo Kim if ((int)mapcount <= 0) 14650f843c6aSJoonsoo Kim return ret; 14660f843c6aSJoonsoo Kim } 14670f843c6aSJoonsoo Kim vma->vm_private_data = (void *) max_nl_cursor; 14680f843c6aSJoonsoo Kim } 14690f843c6aSJoonsoo Kim cond_resched(); 14700f843c6aSJoonsoo Kim max_nl_cursor += CLUSTER_SIZE; 14710f843c6aSJoonsoo Kim } while (max_nl_cursor <= max_nl_size); 14720f843c6aSJoonsoo Kim 14730f843c6aSJoonsoo Kim /* 14740f843c6aSJoonsoo Kim * Don't loop forever (perhaps all the remaining pages are 14750f843c6aSJoonsoo Kim * in locked vmas). Reset cursor on all unreserved nonlinear 14760f843c6aSJoonsoo Kim * vmas, now forgetting on which ones it had fallen behind. 14770f843c6aSJoonsoo Kim */ 14780f843c6aSJoonsoo Kim list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.nonlinear) 14790f843c6aSJoonsoo Kim vma->vm_private_data = NULL; 14800f843c6aSJoonsoo Kim 14810f843c6aSJoonsoo Kim return ret; 14820f843c6aSJoonsoo Kim } 14830f843c6aSJoonsoo Kim 148471e3aac0SAndrea Arcangeli bool is_vma_temporary_stack(struct vm_area_struct *vma) 1485a8bef8ffSMel Gorman { 1486a8bef8ffSMel Gorman int maybe_stack = vma->vm_flags & (VM_GROWSDOWN | VM_GROWSUP); 1487a8bef8ffSMel Gorman 1488a8bef8ffSMel Gorman if (!maybe_stack) 1489a8bef8ffSMel Gorman return false; 1490a8bef8ffSMel Gorman 1491a8bef8ffSMel Gorman if ((vma->vm_flags & VM_STACK_INCOMPLETE_SETUP) == 1492a8bef8ffSMel Gorman VM_STACK_INCOMPLETE_SETUP) 1493a8bef8ffSMel Gorman return true; 1494a8bef8ffSMel Gorman 1495a8bef8ffSMel Gorman return false; 1496a8bef8ffSMel Gorman } 1497a8bef8ffSMel Gorman 149852629506SJoonsoo Kim static bool invalid_migration_vma(struct vm_area_struct *vma, void *arg) 149952629506SJoonsoo Kim { 150052629506SJoonsoo Kim return is_vma_temporary_stack(vma); 150152629506SJoonsoo Kim } 150252629506SJoonsoo Kim 150352629506SJoonsoo Kim static int page_not_mapped(struct page *page) 150452629506SJoonsoo Kim { 150552629506SJoonsoo Kim return !page_mapped(page); 150652629506SJoonsoo Kim }; 150752629506SJoonsoo Kim 15081da177e4SLinus Torvalds /** 15091da177e4SLinus Torvalds * try_to_unmap - try to remove all page table mappings to a page 15101da177e4SLinus Torvalds * @page: the page to get unmapped 151114fa31b8SAndi Kleen * @flags: action and flags 15121da177e4SLinus Torvalds * 15131da177e4SLinus Torvalds * Tries to remove all the page table entries which are mapping this 15141da177e4SLinus Torvalds * page, used in the pageout path. Caller must hold the page lock. 15151da177e4SLinus Torvalds * Return values are: 15161da177e4SLinus Torvalds * 15171da177e4SLinus Torvalds * SWAP_SUCCESS - we succeeded in removing all mappings 15181da177e4SLinus Torvalds * SWAP_AGAIN - we missed a mapping, try again later 15191da177e4SLinus Torvalds * SWAP_FAIL - the page is unswappable 1520b291f000SNick Piggin * SWAP_MLOCK - page is mlocked. 15211da177e4SLinus Torvalds */ 152214fa31b8SAndi Kleen int try_to_unmap(struct page *page, enum ttu_flags flags) 15231da177e4SLinus Torvalds { 15241da177e4SLinus Torvalds int ret; 152552629506SJoonsoo Kim struct rmap_walk_control rwc = { 152652629506SJoonsoo Kim .rmap_one = try_to_unmap_one, 152752629506SJoonsoo Kim .arg = (void *)flags, 152852629506SJoonsoo Kim .done = page_not_mapped, 152952629506SJoonsoo Kim .file_nonlinear = try_to_unmap_nonlinear, 153052629506SJoonsoo Kim .anon_lock = page_lock_anon_vma_read, 153152629506SJoonsoo Kim }; 15321da177e4SLinus Torvalds 1533309381feSSasha Levin VM_BUG_ON_PAGE(!PageHuge(page) && PageTransHuge(page), page); 15341da177e4SLinus Torvalds 153552629506SJoonsoo Kim /* 153652629506SJoonsoo Kim * During exec, a temporary VMA is setup and later moved. 153752629506SJoonsoo Kim * The VMA is moved under the anon_vma lock but not the 153852629506SJoonsoo Kim * page tables leading to a race where migration cannot 153952629506SJoonsoo Kim * find the migration ptes. Rather than increasing the 154052629506SJoonsoo Kim * locking requirements of exec(), migration skips 154152629506SJoonsoo Kim * temporary VMAs until after exec() completes. 154252629506SJoonsoo Kim */ 1543daa5ba76SKonstantin Khlebnikov if ((flags & TTU_MIGRATION) && !PageKsm(page) && PageAnon(page)) 154452629506SJoonsoo Kim rwc.invalid_vma = invalid_migration_vma; 154552629506SJoonsoo Kim 154652629506SJoonsoo Kim ret = rmap_walk(page, &rwc); 154752629506SJoonsoo Kim 1548b291f000SNick Piggin if (ret != SWAP_MLOCK && !page_mapped(page)) 15491da177e4SLinus Torvalds ret = SWAP_SUCCESS; 15501da177e4SLinus Torvalds return ret; 15511da177e4SLinus Torvalds } 155281b4082dSNikita Danilov 1553b291f000SNick Piggin /** 1554b291f000SNick Piggin * try_to_munlock - try to munlock a page 1555b291f000SNick Piggin * @page: the page to be munlocked 1556b291f000SNick Piggin * 1557b291f000SNick Piggin * Called from munlock code. Checks all of the VMAs mapping the page 1558b291f000SNick Piggin * to make sure nobody else has this page mlocked. The page will be 1559b291f000SNick Piggin * returned with PG_mlocked cleared if no other vmas have it mlocked. 1560b291f000SNick Piggin * 1561b291f000SNick Piggin * Return values are: 1562b291f000SNick Piggin * 156353f79acbSHugh Dickins * SWAP_AGAIN - no vma is holding page mlocked, or, 1564b291f000SNick Piggin * SWAP_AGAIN - page mapped in mlocked vma -- couldn't acquire mmap sem 15655ad64688SHugh Dickins * SWAP_FAIL - page cannot be located at present 1566b291f000SNick Piggin * SWAP_MLOCK - page is now mlocked. 1567b291f000SNick Piggin */ 1568b291f000SNick Piggin int try_to_munlock(struct page *page) 1569b291f000SNick Piggin { 1570e8351ac9SJoonsoo Kim int ret; 1571e8351ac9SJoonsoo Kim struct rmap_walk_control rwc = { 1572e8351ac9SJoonsoo Kim .rmap_one = try_to_unmap_one, 1573e8351ac9SJoonsoo Kim .arg = (void *)TTU_MUNLOCK, 1574e8351ac9SJoonsoo Kim .done = page_not_mapped, 1575e8351ac9SJoonsoo Kim /* 1576e8351ac9SJoonsoo Kim * We don't bother to try to find the munlocked page in 1577e8351ac9SJoonsoo Kim * nonlinears. It's costly. Instead, later, page reclaim logic 1578e8351ac9SJoonsoo Kim * may call try_to_unmap() and recover PG_mlocked lazily. 1579e8351ac9SJoonsoo Kim */ 1580e8351ac9SJoonsoo Kim .file_nonlinear = NULL, 1581e8351ac9SJoonsoo Kim .anon_lock = page_lock_anon_vma_read, 1582e8351ac9SJoonsoo Kim 1583e8351ac9SJoonsoo Kim }; 1584e8351ac9SJoonsoo Kim 1585309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page) || PageLRU(page), page); 1586b291f000SNick Piggin 1587e8351ac9SJoonsoo Kim ret = rmap_walk(page, &rwc); 1588e8351ac9SJoonsoo Kim return ret; 1589b291f000SNick Piggin } 1590e9995ef9SHugh Dickins 159101d8b20dSPeter Zijlstra void __put_anon_vma(struct anon_vma *anon_vma) 159276545066SRik van Riel { 159376545066SRik van Riel struct anon_vma *root = anon_vma->root; 159476545066SRik van Riel 1595624483f3SAndrey Ryabinin anon_vma_free(anon_vma); 159601d8b20dSPeter Zijlstra if (root != anon_vma && atomic_dec_and_test(&root->refcount)) 159776545066SRik van Riel anon_vma_free(root); 159876545066SRik van Riel } 159976545066SRik van Riel 16000dd1c7bbSJoonsoo Kim static struct anon_vma *rmap_walk_anon_lock(struct page *page, 16010dd1c7bbSJoonsoo Kim struct rmap_walk_control *rwc) 1602faecd8ddSJoonsoo Kim { 1603faecd8ddSJoonsoo Kim struct anon_vma *anon_vma; 1604faecd8ddSJoonsoo Kim 16050dd1c7bbSJoonsoo Kim if (rwc->anon_lock) 16060dd1c7bbSJoonsoo Kim return rwc->anon_lock(page); 16070dd1c7bbSJoonsoo Kim 1608faecd8ddSJoonsoo Kim /* 1609faecd8ddSJoonsoo Kim * Note: remove_migration_ptes() cannot use page_lock_anon_vma_read() 1610faecd8ddSJoonsoo Kim * because that depends on page_mapped(); but not all its usages 1611faecd8ddSJoonsoo Kim * are holding mmap_sem. Users without mmap_sem are required to 1612faecd8ddSJoonsoo Kim * take a reference count to prevent the anon_vma disappearing 1613faecd8ddSJoonsoo Kim */ 1614faecd8ddSJoonsoo Kim anon_vma = page_anon_vma(page); 1615faecd8ddSJoonsoo Kim if (!anon_vma) 1616faecd8ddSJoonsoo Kim return NULL; 1617faecd8ddSJoonsoo Kim 1618faecd8ddSJoonsoo Kim anon_vma_lock_read(anon_vma); 1619faecd8ddSJoonsoo Kim return anon_vma; 1620faecd8ddSJoonsoo Kim } 1621faecd8ddSJoonsoo Kim 1622e9995ef9SHugh Dickins /* 1623e8351ac9SJoonsoo Kim * rmap_walk_anon - do something to anonymous page using the object-based 1624e8351ac9SJoonsoo Kim * rmap method 1625e8351ac9SJoonsoo Kim * @page: the page to be handled 1626e8351ac9SJoonsoo Kim * @rwc: control variable according to each walk type 1627e8351ac9SJoonsoo Kim * 1628e8351ac9SJoonsoo Kim * Find all the mappings of a page using the mapping pointer and the vma chains 1629e8351ac9SJoonsoo Kim * contained in the anon_vma struct it points to. 1630e8351ac9SJoonsoo Kim * 1631e8351ac9SJoonsoo Kim * When called from try_to_munlock(), the mmap_sem of the mm containing the vma 1632e8351ac9SJoonsoo Kim * where the page was found will be held for write. So, we won't recheck 1633e8351ac9SJoonsoo Kim * vm_flags for that VMA. That should be OK, because that vma shouldn't be 1634e8351ac9SJoonsoo Kim * LOCKED. 1635e9995ef9SHugh Dickins */ 1636051ac83aSJoonsoo Kim static int rmap_walk_anon(struct page *page, struct rmap_walk_control *rwc) 1637e9995ef9SHugh Dickins { 1638e9995ef9SHugh Dickins struct anon_vma *anon_vma; 1639b258d860SDavidlohr Bueso pgoff_t pgoff; 16405beb4930SRik van Riel struct anon_vma_chain *avc; 1641e9995ef9SHugh Dickins int ret = SWAP_AGAIN; 1642e9995ef9SHugh Dickins 16430dd1c7bbSJoonsoo Kim anon_vma = rmap_walk_anon_lock(page, rwc); 1644e9995ef9SHugh Dickins if (!anon_vma) 1645e9995ef9SHugh Dickins return ret; 1646faecd8ddSJoonsoo Kim 1647b258d860SDavidlohr Bueso pgoff = page_to_pgoff(page); 1648bf181b9fSMichel Lespinasse anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) { 16495beb4930SRik van Riel struct vm_area_struct *vma = avc->vma; 1650e9995ef9SHugh Dickins unsigned long address = vma_address(page, vma); 16510dd1c7bbSJoonsoo Kim 16520dd1c7bbSJoonsoo Kim if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg)) 16530dd1c7bbSJoonsoo Kim continue; 16540dd1c7bbSJoonsoo Kim 1655051ac83aSJoonsoo Kim ret = rwc->rmap_one(page, vma, address, rwc->arg); 1656e9995ef9SHugh Dickins if (ret != SWAP_AGAIN) 1657e9995ef9SHugh Dickins break; 16580dd1c7bbSJoonsoo Kim if (rwc->done && rwc->done(page)) 16590dd1c7bbSJoonsoo Kim break; 1660e9995ef9SHugh Dickins } 16614fc3f1d6SIngo Molnar anon_vma_unlock_read(anon_vma); 1662e9995ef9SHugh Dickins return ret; 1663e9995ef9SHugh Dickins } 1664e9995ef9SHugh Dickins 1665e8351ac9SJoonsoo Kim /* 1666e8351ac9SJoonsoo Kim * rmap_walk_file - do something to file page using the object-based rmap method 1667e8351ac9SJoonsoo Kim * @page: the page to be handled 1668e8351ac9SJoonsoo Kim * @rwc: control variable according to each walk type 1669e8351ac9SJoonsoo Kim * 1670e8351ac9SJoonsoo Kim * Find all the mappings of a page using the mapping pointer and the vma chains 1671e8351ac9SJoonsoo Kim * contained in the address_space struct it points to. 1672e8351ac9SJoonsoo Kim * 1673e8351ac9SJoonsoo Kim * When called from try_to_munlock(), the mmap_sem of the mm containing the vma 1674e8351ac9SJoonsoo Kim * where the page was found will be held for write. So, we won't recheck 1675e8351ac9SJoonsoo Kim * vm_flags for that VMA. That should be OK, because that vma shouldn't be 1676e8351ac9SJoonsoo Kim * LOCKED. 1677e8351ac9SJoonsoo Kim */ 1678051ac83aSJoonsoo Kim static int rmap_walk_file(struct page *page, struct rmap_walk_control *rwc) 1679e9995ef9SHugh Dickins { 1680e9995ef9SHugh Dickins struct address_space *mapping = page->mapping; 1681b258d860SDavidlohr Bueso pgoff_t pgoff; 1682e9995ef9SHugh Dickins struct vm_area_struct *vma; 1683e9995ef9SHugh Dickins int ret = SWAP_AGAIN; 1684e9995ef9SHugh Dickins 16859f32624bSJoonsoo Kim /* 16869f32624bSJoonsoo Kim * The page lock not only makes sure that page->mapping cannot 16879f32624bSJoonsoo Kim * suddenly be NULLified by truncation, it makes sure that the 16889f32624bSJoonsoo Kim * structure at mapping cannot be freed and reused yet, 1689c8c06efaSDavidlohr Bueso * so we can safely take mapping->i_mmap_rwsem. 16909f32624bSJoonsoo Kim */ 169181d1b09cSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 16929f32624bSJoonsoo Kim 1693e9995ef9SHugh Dickins if (!mapping) 1694e9995ef9SHugh Dickins return ret; 16953dec0ba0SDavidlohr Bueso 1696b258d860SDavidlohr Bueso pgoff = page_to_pgoff(page); 16973dec0ba0SDavidlohr Bueso i_mmap_lock_read(mapping); 16986b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { 1699e9995ef9SHugh Dickins unsigned long address = vma_address(page, vma); 17000dd1c7bbSJoonsoo Kim 17010dd1c7bbSJoonsoo Kim if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg)) 17020dd1c7bbSJoonsoo Kim continue; 17030dd1c7bbSJoonsoo Kim 1704051ac83aSJoonsoo Kim ret = rwc->rmap_one(page, vma, address, rwc->arg); 1705e9995ef9SHugh Dickins if (ret != SWAP_AGAIN) 17060dd1c7bbSJoonsoo Kim goto done; 17070dd1c7bbSJoonsoo Kim if (rwc->done && rwc->done(page)) 17080dd1c7bbSJoonsoo Kim goto done; 1709e9995ef9SHugh Dickins } 17100dd1c7bbSJoonsoo Kim 17110dd1c7bbSJoonsoo Kim if (!rwc->file_nonlinear) 17120dd1c7bbSJoonsoo Kim goto done; 17130dd1c7bbSJoonsoo Kim 17140dd1c7bbSJoonsoo Kim if (list_empty(&mapping->i_mmap_nonlinear)) 17150dd1c7bbSJoonsoo Kim goto done; 17160dd1c7bbSJoonsoo Kim 17177e09e738SHugh Dickins ret = rwc->file_nonlinear(page, mapping, rwc->arg); 17180dd1c7bbSJoonsoo Kim done: 17193dec0ba0SDavidlohr Bueso i_mmap_unlock_read(mapping); 1720e9995ef9SHugh Dickins return ret; 1721e9995ef9SHugh Dickins } 1722e9995ef9SHugh Dickins 1723051ac83aSJoonsoo Kim int rmap_walk(struct page *page, struct rmap_walk_control *rwc) 1724e9995ef9SHugh Dickins { 1725e9995ef9SHugh Dickins if (unlikely(PageKsm(page))) 1726051ac83aSJoonsoo Kim return rmap_walk_ksm(page, rwc); 1727e9995ef9SHugh Dickins else if (PageAnon(page)) 1728051ac83aSJoonsoo Kim return rmap_walk_anon(page, rwc); 1729e9995ef9SHugh Dickins else 1730051ac83aSJoonsoo Kim return rmap_walk_file(page, rwc); 1731e9995ef9SHugh Dickins } 17320fe6e20bSNaoya Horiguchi 1733e3390f67SNaoya Horiguchi #ifdef CONFIG_HUGETLB_PAGE 17340fe6e20bSNaoya Horiguchi /* 17350fe6e20bSNaoya Horiguchi * The following three functions are for anonymous (private mapped) hugepages. 17360fe6e20bSNaoya Horiguchi * Unlike common anonymous pages, anonymous hugepages have no accounting code 17370fe6e20bSNaoya Horiguchi * and no lru code, because we handle hugepages differently from common pages. 17380fe6e20bSNaoya Horiguchi */ 17390fe6e20bSNaoya Horiguchi static void __hugepage_set_anon_rmap(struct page *page, 17400fe6e20bSNaoya Horiguchi struct vm_area_struct *vma, unsigned long address, int exclusive) 17410fe6e20bSNaoya Horiguchi { 17420fe6e20bSNaoya Horiguchi struct anon_vma *anon_vma = vma->anon_vma; 1743433abed6SNaoya Horiguchi 17440fe6e20bSNaoya Horiguchi BUG_ON(!anon_vma); 1745433abed6SNaoya Horiguchi 1746433abed6SNaoya Horiguchi if (PageAnon(page)) 1747433abed6SNaoya Horiguchi return; 1748433abed6SNaoya Horiguchi if (!exclusive) 1749433abed6SNaoya Horiguchi anon_vma = anon_vma->root; 1750433abed6SNaoya Horiguchi 17510fe6e20bSNaoya Horiguchi anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON; 17520fe6e20bSNaoya Horiguchi page->mapping = (struct address_space *) anon_vma; 17530fe6e20bSNaoya Horiguchi page->index = linear_page_index(vma, address); 17540fe6e20bSNaoya Horiguchi } 17550fe6e20bSNaoya Horiguchi 17560fe6e20bSNaoya Horiguchi void hugepage_add_anon_rmap(struct page *page, 17570fe6e20bSNaoya Horiguchi struct vm_area_struct *vma, unsigned long address) 17580fe6e20bSNaoya Horiguchi { 17590fe6e20bSNaoya Horiguchi struct anon_vma *anon_vma = vma->anon_vma; 17600fe6e20bSNaoya Horiguchi int first; 1761a850ea30SNaoya Horiguchi 1762a850ea30SNaoya Horiguchi BUG_ON(!PageLocked(page)); 17630fe6e20bSNaoya Horiguchi BUG_ON(!anon_vma); 17645dbe0af4SHugh Dickins /* address might be in next vma when migration races vma_adjust */ 17650fe6e20bSNaoya Horiguchi first = atomic_inc_and_test(&page->_mapcount); 17660fe6e20bSNaoya Horiguchi if (first) 17670fe6e20bSNaoya Horiguchi __hugepage_set_anon_rmap(page, vma, address, 0); 17680fe6e20bSNaoya Horiguchi } 17690fe6e20bSNaoya Horiguchi 17700fe6e20bSNaoya Horiguchi void hugepage_add_new_anon_rmap(struct page *page, 17710fe6e20bSNaoya Horiguchi struct vm_area_struct *vma, unsigned long address) 17720fe6e20bSNaoya Horiguchi { 17730fe6e20bSNaoya Horiguchi BUG_ON(address < vma->vm_start || address >= vma->vm_end); 17740fe6e20bSNaoya Horiguchi atomic_set(&page->_mapcount, 0); 17750fe6e20bSNaoya Horiguchi __hugepage_set_anon_rmap(page, vma, address, 1); 17760fe6e20bSNaoya Horiguchi } 1777e3390f67SNaoya Horiguchi #endif /* CONFIG_HUGETLB_PAGE */ 1778