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 * 239608703eSJan Kara * inode->i_rwsem (while writing or truncating, not reading or faulting) 24c1e8d7c6SMichel Lespinasse * mm->mmap_lock 25730633f0SJan Kara * mapping->invalidate_lock (in filemap_fault) 269608703eSJan Kara * page->flags PG_locked (lock_page) * (see hugetlbfs below) 2788f306b6SKirill A. Shutemov * hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share) 28c8c06efaSDavidlohr Bueso * mapping->i_mmap_rwsem 29c0d0381aSMike Kravetz * hugetlb_fault_mutex (hugetlbfs specific page fault mutex) 305a505085SIngo Molnar * anon_vma->rwsem 31b8072f09SHugh Dickins * mm->page_table_lock or pte_lock 325d337b91SHugh Dickins * swap_lock (in swap_duplicate, swap_info_get) 331da177e4SLinus Torvalds * mmlist_lock (in mmput, drain_mmlist and others) 341da177e4SLinus Torvalds * mapping->private_lock (in __set_page_dirty_buffers) 3515b44736SHugh Dickins * lock_page_memcg move_lock (in __set_page_dirty_buffers) 36b93b0163SMatthew Wilcox * i_pages lock (widely used) 37e809c3feSMatthew Wilcox (Oracle) * lruvec->lru_lock (in folio_lruvec_lock_irq) 38250df6edSDave Chinner * inode->i_lock (in set_page_dirty's __mark_inode_dirty) 39f758eeabSChristoph Hellwig * bdi.wb->list_lock (in set_page_dirty's __mark_inode_dirty) 401da177e4SLinus Torvalds * sb_lock (within inode_lock in fs/fs-writeback.c) 41b93b0163SMatthew Wilcox * i_pages lock (widely used, in set_page_dirty, 421da177e4SLinus Torvalds * in arch-dependent flush_dcache_mmap_lock, 43f758eeabSChristoph Hellwig * within bdi.wb->list_lock in __sync_single_inode) 446a46079cSAndi Kleen * 459608703eSJan Kara * anon_vma->rwsem,mapping->i_mmap_rwsem (memory_failure, collect_procs_anon) 466a46079cSAndi Kleen * ->tasklist_lock 476a46079cSAndi Kleen * pte map lock 48c0d0381aSMike Kravetz * 49c0d0381aSMike Kravetz * * hugetlbfs PageHuge() pages take locks in this order: 50c0d0381aSMike Kravetz * mapping->i_mmap_rwsem 51c0d0381aSMike Kravetz * hugetlb_fault_mutex (hugetlbfs specific page fault mutex) 52c0d0381aSMike Kravetz * page->flags PG_locked (lock_page) 531da177e4SLinus Torvalds */ 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #include <linux/mm.h> 566e84f315SIngo Molnar #include <linux/sched/mm.h> 5729930025SIngo Molnar #include <linux/sched/task.h> 581da177e4SLinus Torvalds #include <linux/pagemap.h> 591da177e4SLinus Torvalds #include <linux/swap.h> 601da177e4SLinus Torvalds #include <linux/swapops.h> 611da177e4SLinus Torvalds #include <linux/slab.h> 621da177e4SLinus Torvalds #include <linux/init.h> 635ad64688SHugh Dickins #include <linux/ksm.h> 641da177e4SLinus Torvalds #include <linux/rmap.h> 651da177e4SLinus Torvalds #include <linux/rcupdate.h> 66b95f1b31SPaul Gortmaker #include <linux/export.h> 678a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 68cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 6964cdd548SKOSAKI Motohiro #include <linux/migrate.h> 700fe6e20bSNaoya Horiguchi #include <linux/hugetlb.h> 71444f84fdSBen Dooks #include <linux/huge_mm.h> 72ef5d437fSJan Kara #include <linux/backing-dev.h> 7333c3fc71SVladimir Davydov #include <linux/page_idle.h> 74a5430ddaSJérôme Glisse #include <linux/memremap.h> 75bce73e48SChristian Borntraeger #include <linux/userfaultfd_k.h> 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds #include <asm/tlbflush.h> 781da177e4SLinus Torvalds 7972b252aeSMel Gorman #include <trace/events/tlb.h> 8072b252aeSMel Gorman 81b291f000SNick Piggin #include "internal.h" 82b291f000SNick Piggin 83fdd2e5f8SAdrian Bunk static struct kmem_cache *anon_vma_cachep; 845beb4930SRik van Riel static struct kmem_cache *anon_vma_chain_cachep; 85fdd2e5f8SAdrian Bunk 86fdd2e5f8SAdrian Bunk static inline struct anon_vma *anon_vma_alloc(void) 87fdd2e5f8SAdrian Bunk { 8801d8b20dSPeter Zijlstra struct anon_vma *anon_vma; 8901d8b20dSPeter Zijlstra 9001d8b20dSPeter Zijlstra anon_vma = kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL); 9101d8b20dSPeter Zijlstra if (anon_vma) { 9201d8b20dSPeter Zijlstra atomic_set(&anon_vma->refcount, 1); 937a3ef208SKonstantin Khlebnikov anon_vma->degree = 1; /* Reference for first vma */ 947a3ef208SKonstantin Khlebnikov anon_vma->parent = anon_vma; 9501d8b20dSPeter Zijlstra /* 9601d8b20dSPeter Zijlstra * Initialise the anon_vma root to point to itself. If called 9701d8b20dSPeter Zijlstra * from fork, the root will be reset to the parents anon_vma. 9801d8b20dSPeter Zijlstra */ 9901d8b20dSPeter Zijlstra anon_vma->root = anon_vma; 100fdd2e5f8SAdrian Bunk } 101fdd2e5f8SAdrian Bunk 10201d8b20dSPeter Zijlstra return anon_vma; 10301d8b20dSPeter Zijlstra } 10401d8b20dSPeter Zijlstra 10501d8b20dSPeter Zijlstra static inline void anon_vma_free(struct anon_vma *anon_vma) 106fdd2e5f8SAdrian Bunk { 10701d8b20dSPeter Zijlstra VM_BUG_ON(atomic_read(&anon_vma->refcount)); 10888c22088SPeter Zijlstra 10988c22088SPeter Zijlstra /* 1104fc3f1d6SIngo Molnar * Synchronize against page_lock_anon_vma_read() such that 11188c22088SPeter Zijlstra * we can safely hold the lock without the anon_vma getting 11288c22088SPeter Zijlstra * freed. 11388c22088SPeter Zijlstra * 11488c22088SPeter Zijlstra * Relies on the full mb implied by the atomic_dec_and_test() from 11588c22088SPeter Zijlstra * put_anon_vma() against the acquire barrier implied by 1164fc3f1d6SIngo Molnar * down_read_trylock() from page_lock_anon_vma_read(). This orders: 11788c22088SPeter Zijlstra * 1184fc3f1d6SIngo Molnar * page_lock_anon_vma_read() VS put_anon_vma() 1194fc3f1d6SIngo Molnar * down_read_trylock() atomic_dec_and_test() 12088c22088SPeter Zijlstra * LOCK MB 1214fc3f1d6SIngo Molnar * atomic_read() rwsem_is_locked() 12288c22088SPeter Zijlstra * 12388c22088SPeter Zijlstra * LOCK should suffice since the actual taking of the lock must 12488c22088SPeter Zijlstra * happen _before_ what follows. 12588c22088SPeter Zijlstra */ 1267f39dda9SHugh Dickins might_sleep(); 1275a505085SIngo Molnar if (rwsem_is_locked(&anon_vma->root->rwsem)) { 1284fc3f1d6SIngo Molnar anon_vma_lock_write(anon_vma); 12908b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 13088c22088SPeter Zijlstra } 13188c22088SPeter Zijlstra 132fdd2e5f8SAdrian Bunk kmem_cache_free(anon_vma_cachep, anon_vma); 133fdd2e5f8SAdrian Bunk } 1341da177e4SLinus Torvalds 135dd34739cSLinus Torvalds static inline struct anon_vma_chain *anon_vma_chain_alloc(gfp_t gfp) 1365beb4930SRik van Riel { 137dd34739cSLinus Torvalds return kmem_cache_alloc(anon_vma_chain_cachep, gfp); 1385beb4930SRik van Riel } 1395beb4930SRik van Riel 140e574b5fdSNamhyung Kim static void anon_vma_chain_free(struct anon_vma_chain *anon_vma_chain) 1415beb4930SRik van Riel { 1425beb4930SRik van Riel kmem_cache_free(anon_vma_chain_cachep, anon_vma_chain); 1435beb4930SRik van Riel } 1445beb4930SRik van Riel 1456583a843SKautuk Consul static void anon_vma_chain_link(struct vm_area_struct *vma, 1466583a843SKautuk Consul struct anon_vma_chain *avc, 1476583a843SKautuk Consul struct anon_vma *anon_vma) 1486583a843SKautuk Consul { 1496583a843SKautuk Consul avc->vma = vma; 1506583a843SKautuk Consul avc->anon_vma = anon_vma; 1516583a843SKautuk Consul list_add(&avc->same_vma, &vma->anon_vma_chain); 152bf181b9fSMichel Lespinasse anon_vma_interval_tree_insert(avc, &anon_vma->rb_root); 1536583a843SKautuk Consul } 1546583a843SKautuk Consul 155d9d332e0SLinus Torvalds /** 156d5a187daSVlastimil Babka * __anon_vma_prepare - attach an anon_vma to a memory region 157d9d332e0SLinus Torvalds * @vma: the memory region in question 158d9d332e0SLinus Torvalds * 159d9d332e0SLinus Torvalds * This makes sure the memory mapping described by 'vma' has 160d9d332e0SLinus Torvalds * an 'anon_vma' attached to it, so that we can associate the 161d9d332e0SLinus Torvalds * anonymous pages mapped into it with that anon_vma. 162d9d332e0SLinus Torvalds * 163d5a187daSVlastimil Babka * The common case will be that we already have one, which 164d5a187daSVlastimil Babka * is handled inline by anon_vma_prepare(). But if 16523a0790aSFigo.zhang * not we either need to find an adjacent mapping that we 166d9d332e0SLinus Torvalds * can re-use the anon_vma from (very common when the only 167d9d332e0SLinus Torvalds * reason for splitting a vma has been mprotect()), or we 168d9d332e0SLinus Torvalds * allocate a new one. 169d9d332e0SLinus Torvalds * 170d9d332e0SLinus Torvalds * Anon-vma allocations are very subtle, because we may have 1714fc3f1d6SIngo Molnar * optimistically looked up an anon_vma in page_lock_anon_vma_read() 172aaf1f990SMiaohe Lin * and that may actually touch the rwsem even in the newly 173d9d332e0SLinus Torvalds * allocated vma (it depends on RCU to make sure that the 174d9d332e0SLinus Torvalds * anon_vma isn't actually destroyed). 175d9d332e0SLinus Torvalds * 176d9d332e0SLinus Torvalds * As a result, we need to do proper anon_vma locking even 177d9d332e0SLinus Torvalds * for the new allocation. At the same time, we do not want 178d9d332e0SLinus Torvalds * to do any locking for the common case of already having 179d9d332e0SLinus Torvalds * an anon_vma. 180d9d332e0SLinus Torvalds * 181c1e8d7c6SMichel Lespinasse * This must be called with the mmap_lock held for reading. 182d9d332e0SLinus Torvalds */ 183d5a187daSVlastimil Babka int __anon_vma_prepare(struct vm_area_struct *vma) 1841da177e4SLinus Torvalds { 185d5a187daSVlastimil Babka struct mm_struct *mm = vma->vm_mm; 186d5a187daSVlastimil Babka struct anon_vma *anon_vma, *allocated; 1875beb4930SRik van Riel struct anon_vma_chain *avc; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds might_sleep(); 1901da177e4SLinus Torvalds 191dd34739cSLinus Torvalds avc = anon_vma_chain_alloc(GFP_KERNEL); 1925beb4930SRik van Riel if (!avc) 1935beb4930SRik van Riel goto out_enomem; 1945beb4930SRik van Riel 1951da177e4SLinus Torvalds anon_vma = find_mergeable_anon_vma(vma); 1961da177e4SLinus Torvalds allocated = NULL; 197d9d332e0SLinus Torvalds if (!anon_vma) { 1981da177e4SLinus Torvalds anon_vma = anon_vma_alloc(); 1991da177e4SLinus Torvalds if (unlikely(!anon_vma)) 2005beb4930SRik van Riel goto out_enomem_free_avc; 2011da177e4SLinus Torvalds allocated = anon_vma; 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 2044fc3f1d6SIngo Molnar anon_vma_lock_write(anon_vma); 2051da177e4SLinus Torvalds /* page_table_lock to protect against threads */ 2061da177e4SLinus Torvalds spin_lock(&mm->page_table_lock); 2071da177e4SLinus Torvalds if (likely(!vma->anon_vma)) { 2081da177e4SLinus Torvalds vma->anon_vma = anon_vma; 2096583a843SKautuk Consul anon_vma_chain_link(vma, avc, anon_vma); 2107a3ef208SKonstantin Khlebnikov /* vma reference or self-parent link for new root */ 2117a3ef208SKonstantin Khlebnikov anon_vma->degree++; 2121da177e4SLinus Torvalds allocated = NULL; 21331f2b0ebSOleg Nesterov avc = NULL; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds spin_unlock(&mm->page_table_lock); 21608b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 21731f2b0ebSOleg Nesterov 21831f2b0ebSOleg Nesterov if (unlikely(allocated)) 21901d8b20dSPeter Zijlstra put_anon_vma(allocated); 22031f2b0ebSOleg Nesterov if (unlikely(avc)) 2215beb4930SRik van Riel anon_vma_chain_free(avc); 222d5a187daSVlastimil Babka 2231da177e4SLinus Torvalds return 0; 2245beb4930SRik van Riel 2255beb4930SRik van Riel out_enomem_free_avc: 2265beb4930SRik van Riel anon_vma_chain_free(avc); 2275beb4930SRik van Riel out_enomem: 2285beb4930SRik van Riel return -ENOMEM; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 231bb4aa396SLinus Torvalds /* 232bb4aa396SLinus Torvalds * This is a useful helper function for locking the anon_vma root as 233bb4aa396SLinus Torvalds * we traverse the vma->anon_vma_chain, looping over anon_vma's that 234bb4aa396SLinus Torvalds * have the same vma. 235bb4aa396SLinus Torvalds * 236bb4aa396SLinus Torvalds * Such anon_vma's should have the same root, so you'd expect to see 237bb4aa396SLinus Torvalds * just a single mutex_lock for the whole traversal. 238bb4aa396SLinus Torvalds */ 239bb4aa396SLinus Torvalds static inline struct anon_vma *lock_anon_vma_root(struct anon_vma *root, struct anon_vma *anon_vma) 240bb4aa396SLinus Torvalds { 241bb4aa396SLinus Torvalds struct anon_vma *new_root = anon_vma->root; 242bb4aa396SLinus Torvalds if (new_root != root) { 243bb4aa396SLinus Torvalds if (WARN_ON_ONCE(root)) 2445a505085SIngo Molnar up_write(&root->rwsem); 245bb4aa396SLinus Torvalds root = new_root; 2465a505085SIngo Molnar down_write(&root->rwsem); 247bb4aa396SLinus Torvalds } 248bb4aa396SLinus Torvalds return root; 249bb4aa396SLinus Torvalds } 250bb4aa396SLinus Torvalds 251bb4aa396SLinus Torvalds static inline void unlock_anon_vma_root(struct anon_vma *root) 252bb4aa396SLinus Torvalds { 253bb4aa396SLinus Torvalds if (root) 2545a505085SIngo Molnar up_write(&root->rwsem); 255bb4aa396SLinus Torvalds } 256bb4aa396SLinus Torvalds 2575beb4930SRik van Riel /* 2585beb4930SRik van Riel * Attach the anon_vmas from src to dst. 2595beb4930SRik van Riel * Returns 0 on success, -ENOMEM on failure. 2607a3ef208SKonstantin Khlebnikov * 261cb152a1aSShijie Luo * anon_vma_clone() is called by __vma_adjust(), __split_vma(), copy_vma() and 26247b390d2SWei Yang * anon_vma_fork(). The first three want an exact copy of src, while the last 26347b390d2SWei Yang * one, anon_vma_fork(), may try to reuse an existing anon_vma to prevent 26447b390d2SWei Yang * endless growth of anon_vma. Since dst->anon_vma is set to NULL before call, 26547b390d2SWei Yang * we can identify this case by checking (!dst->anon_vma && src->anon_vma). 26647b390d2SWei Yang * 26747b390d2SWei Yang * If (!dst->anon_vma && src->anon_vma) is true, this function tries to find 26847b390d2SWei Yang * and reuse existing anon_vma which has no vmas and only one child anon_vma. 26947b390d2SWei Yang * This prevents degradation of anon_vma hierarchy to endless linear chain in 27047b390d2SWei Yang * case of constantly forking task. On the other hand, an anon_vma with more 27147b390d2SWei Yang * than one child isn't reused even if there was no alive vma, thus rmap 27247b390d2SWei Yang * walker has a good chance of avoiding scanning the whole hierarchy when it 27347b390d2SWei Yang * searches where page is mapped. 2745beb4930SRik van Riel */ 2755beb4930SRik van Riel int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src) 2765beb4930SRik van Riel { 2775beb4930SRik van Riel struct anon_vma_chain *avc, *pavc; 278bb4aa396SLinus Torvalds struct anon_vma *root = NULL; 2795beb4930SRik van Riel 280646d87b4SLinus Torvalds list_for_each_entry_reverse(pavc, &src->anon_vma_chain, same_vma) { 281bb4aa396SLinus Torvalds struct anon_vma *anon_vma; 282bb4aa396SLinus Torvalds 283dd34739cSLinus Torvalds avc = anon_vma_chain_alloc(GFP_NOWAIT | __GFP_NOWARN); 284dd34739cSLinus Torvalds if (unlikely(!avc)) { 285dd34739cSLinus Torvalds unlock_anon_vma_root(root); 286dd34739cSLinus Torvalds root = NULL; 287dd34739cSLinus Torvalds avc = anon_vma_chain_alloc(GFP_KERNEL); 2885beb4930SRik van Riel if (!avc) 2895beb4930SRik van Riel goto enomem_failure; 290dd34739cSLinus Torvalds } 291bb4aa396SLinus Torvalds anon_vma = pavc->anon_vma; 292bb4aa396SLinus Torvalds root = lock_anon_vma_root(root, anon_vma); 293bb4aa396SLinus Torvalds anon_vma_chain_link(dst, avc, anon_vma); 2947a3ef208SKonstantin Khlebnikov 2957a3ef208SKonstantin Khlebnikov /* 2967a3ef208SKonstantin Khlebnikov * Reuse existing anon_vma if its degree lower than two, 2977a3ef208SKonstantin Khlebnikov * that means it has no vma and only one anon_vma child. 2987a3ef208SKonstantin Khlebnikov * 2997a3ef208SKonstantin Khlebnikov * Do not chose parent anon_vma, otherwise first child 3007a3ef208SKonstantin Khlebnikov * will always reuse it. Root anon_vma is never reused: 3017a3ef208SKonstantin Khlebnikov * it has self-parent reference and at least one child. 3027a3ef208SKonstantin Khlebnikov */ 30347b390d2SWei Yang if (!dst->anon_vma && src->anon_vma && 30447b390d2SWei Yang anon_vma != src->anon_vma && anon_vma->degree < 2) 3057a3ef208SKonstantin Khlebnikov dst->anon_vma = anon_vma; 3065beb4930SRik van Riel } 3077a3ef208SKonstantin Khlebnikov if (dst->anon_vma) 3087a3ef208SKonstantin Khlebnikov dst->anon_vma->degree++; 309bb4aa396SLinus Torvalds unlock_anon_vma_root(root); 3105beb4930SRik van Riel return 0; 3115beb4930SRik van Riel 3125beb4930SRik van Riel enomem_failure: 3133fe89b3eSLeon Yu /* 3143fe89b3eSLeon Yu * dst->anon_vma is dropped here otherwise its degree can be incorrectly 3153fe89b3eSLeon Yu * decremented in unlink_anon_vmas(). 3163fe89b3eSLeon Yu * We can safely do this because callers of anon_vma_clone() don't care 3173fe89b3eSLeon Yu * about dst->anon_vma if anon_vma_clone() failed. 3183fe89b3eSLeon Yu */ 3193fe89b3eSLeon Yu dst->anon_vma = NULL; 3205beb4930SRik van Riel unlink_anon_vmas(dst); 3215beb4930SRik van Riel return -ENOMEM; 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds 3245beb4930SRik van Riel /* 3255beb4930SRik van Riel * Attach vma to its own anon_vma, as well as to the anon_vmas that 3265beb4930SRik van Riel * the corresponding VMA in the parent process is attached to. 3275beb4930SRik van Riel * Returns 0 on success, non-zero on failure. 3285beb4930SRik van Riel */ 3295beb4930SRik van Riel int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) 3301da177e4SLinus Torvalds { 3315beb4930SRik van Riel struct anon_vma_chain *avc; 3325beb4930SRik van Riel struct anon_vma *anon_vma; 333c4ea95d7SDaniel Forrest int error; 3345beb4930SRik van Riel 3355beb4930SRik van Riel /* Don't bother if the parent process has no anon_vma here. */ 3365beb4930SRik van Riel if (!pvma->anon_vma) 3375beb4930SRik van Riel return 0; 3385beb4930SRik van Riel 3397a3ef208SKonstantin Khlebnikov /* Drop inherited anon_vma, we'll reuse existing or allocate new. */ 3407a3ef208SKonstantin Khlebnikov vma->anon_vma = NULL; 3417a3ef208SKonstantin Khlebnikov 3425beb4930SRik van Riel /* 3435beb4930SRik van Riel * First, attach the new VMA to the parent VMA's anon_vmas, 3445beb4930SRik van Riel * so rmap can find non-COWed pages in child processes. 3455beb4930SRik van Riel */ 346c4ea95d7SDaniel Forrest error = anon_vma_clone(vma, pvma); 347c4ea95d7SDaniel Forrest if (error) 348c4ea95d7SDaniel Forrest return error; 3495beb4930SRik van Riel 3507a3ef208SKonstantin Khlebnikov /* An existing anon_vma has been reused, all done then. */ 3517a3ef208SKonstantin Khlebnikov if (vma->anon_vma) 3527a3ef208SKonstantin Khlebnikov return 0; 3537a3ef208SKonstantin Khlebnikov 3545beb4930SRik van Riel /* Then add our own anon_vma. */ 3555beb4930SRik van Riel anon_vma = anon_vma_alloc(); 3565beb4930SRik van Riel if (!anon_vma) 3575beb4930SRik van Riel goto out_error; 358dd34739cSLinus Torvalds avc = anon_vma_chain_alloc(GFP_KERNEL); 3595beb4930SRik van Riel if (!avc) 3605beb4930SRik van Riel goto out_error_free_anon_vma; 3615c341ee1SRik van Riel 3625c341ee1SRik van Riel /* 363aaf1f990SMiaohe Lin * The root anon_vma's rwsem is the lock actually used when we 3645c341ee1SRik van Riel * lock any of the anon_vmas in this anon_vma tree. 3655c341ee1SRik van Riel */ 3665c341ee1SRik van Riel anon_vma->root = pvma->anon_vma->root; 3677a3ef208SKonstantin Khlebnikov anon_vma->parent = pvma->anon_vma; 36876545066SRik van Riel /* 36901d8b20dSPeter Zijlstra * With refcounts, an anon_vma can stay around longer than the 37001d8b20dSPeter Zijlstra * process it belongs to. The root anon_vma needs to be pinned until 37101d8b20dSPeter Zijlstra * this anon_vma is freed, because the lock lives in the root. 37276545066SRik van Riel */ 37376545066SRik van Riel get_anon_vma(anon_vma->root); 3745beb4930SRik van Riel /* Mark this anon_vma as the one where our new (COWed) pages go. */ 3755beb4930SRik van Riel vma->anon_vma = anon_vma; 3764fc3f1d6SIngo Molnar anon_vma_lock_write(anon_vma); 3775c341ee1SRik van Riel anon_vma_chain_link(vma, avc, anon_vma); 3787a3ef208SKonstantin Khlebnikov anon_vma->parent->degree++; 37908b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 3805beb4930SRik van Riel 3815beb4930SRik van Riel return 0; 3825beb4930SRik van Riel 3835beb4930SRik van Riel out_error_free_anon_vma: 38401d8b20dSPeter Zijlstra put_anon_vma(anon_vma); 3855beb4930SRik van Riel out_error: 3864946d54cSRik van Riel unlink_anon_vmas(vma); 3875beb4930SRik van Riel return -ENOMEM; 3885beb4930SRik van Riel } 3895beb4930SRik van Riel 3905beb4930SRik van Riel void unlink_anon_vmas(struct vm_area_struct *vma) 3915beb4930SRik van Riel { 3925beb4930SRik van Riel struct anon_vma_chain *avc, *next; 393eee2acbaSPeter Zijlstra struct anon_vma *root = NULL; 3945beb4930SRik van Riel 3955c341ee1SRik van Riel /* 3965c341ee1SRik van Riel * Unlink each anon_vma chained to the VMA. This list is ordered 3975c341ee1SRik van Riel * from newest to oldest, ensuring the root anon_vma gets freed last. 3985c341ee1SRik van Riel */ 3995beb4930SRik van Riel list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) { 400eee2acbaSPeter Zijlstra struct anon_vma *anon_vma = avc->anon_vma; 401eee2acbaSPeter Zijlstra 402eee2acbaSPeter Zijlstra root = lock_anon_vma_root(root, anon_vma); 403bf181b9fSMichel Lespinasse anon_vma_interval_tree_remove(avc, &anon_vma->rb_root); 404eee2acbaSPeter Zijlstra 405eee2acbaSPeter Zijlstra /* 406eee2acbaSPeter Zijlstra * Leave empty anon_vmas on the list - we'll need 407eee2acbaSPeter Zijlstra * to free them outside the lock. 408eee2acbaSPeter Zijlstra */ 409f808c13fSDavidlohr Bueso if (RB_EMPTY_ROOT(&anon_vma->rb_root.rb_root)) { 4107a3ef208SKonstantin Khlebnikov anon_vma->parent->degree--; 411eee2acbaSPeter Zijlstra continue; 4127a3ef208SKonstantin Khlebnikov } 413eee2acbaSPeter Zijlstra 414eee2acbaSPeter Zijlstra list_del(&avc->same_vma); 415eee2acbaSPeter Zijlstra anon_vma_chain_free(avc); 416eee2acbaSPeter Zijlstra } 417ee8ab190SLi Xinhai if (vma->anon_vma) { 4187a3ef208SKonstantin Khlebnikov vma->anon_vma->degree--; 419ee8ab190SLi Xinhai 420ee8ab190SLi Xinhai /* 421ee8ab190SLi Xinhai * vma would still be needed after unlink, and anon_vma will be prepared 422ee8ab190SLi Xinhai * when handle fault. 423ee8ab190SLi Xinhai */ 424ee8ab190SLi Xinhai vma->anon_vma = NULL; 425ee8ab190SLi Xinhai } 426eee2acbaSPeter Zijlstra unlock_anon_vma_root(root); 427eee2acbaSPeter Zijlstra 428eee2acbaSPeter Zijlstra /* 429eee2acbaSPeter Zijlstra * Iterate the list once more, it now only contains empty and unlinked 430eee2acbaSPeter Zijlstra * anon_vmas, destroy them. Could not do before due to __put_anon_vma() 4315a505085SIngo Molnar * needing to write-acquire the anon_vma->root->rwsem. 432eee2acbaSPeter Zijlstra */ 433eee2acbaSPeter Zijlstra list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) { 434eee2acbaSPeter Zijlstra struct anon_vma *anon_vma = avc->anon_vma; 435eee2acbaSPeter Zijlstra 436e4c5800aSKonstantin Khlebnikov VM_WARN_ON(anon_vma->degree); 437eee2acbaSPeter Zijlstra put_anon_vma(anon_vma); 438eee2acbaSPeter Zijlstra 4395beb4930SRik van Riel list_del(&avc->same_vma); 4405beb4930SRik van Riel anon_vma_chain_free(avc); 4415beb4930SRik van Riel } 4425beb4930SRik van Riel } 4435beb4930SRik van Riel 44451cc5068SAlexey Dobriyan static void anon_vma_ctor(void *data) 4451da177e4SLinus Torvalds { 4461da177e4SLinus Torvalds struct anon_vma *anon_vma = data; 4471da177e4SLinus Torvalds 4485a505085SIngo Molnar init_rwsem(&anon_vma->rwsem); 44983813267SPeter Zijlstra atomic_set(&anon_vma->refcount, 0); 450f808c13fSDavidlohr Bueso anon_vma->rb_root = RB_ROOT_CACHED; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds void __init anon_vma_init(void) 4541da177e4SLinus Torvalds { 4551da177e4SLinus Torvalds anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma), 4565f0d5a3aSPaul E. McKenney 0, SLAB_TYPESAFE_BY_RCU|SLAB_PANIC|SLAB_ACCOUNT, 4575d097056SVladimir Davydov anon_vma_ctor); 4585d097056SVladimir Davydov anon_vma_chain_cachep = KMEM_CACHE(anon_vma_chain, 4595d097056SVladimir Davydov SLAB_PANIC|SLAB_ACCOUNT); 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds /* 4636111e4caSPeter Zijlstra * Getting a lock on a stable anon_vma from a page off the LRU is tricky! 4646111e4caSPeter Zijlstra * 4656111e4caSPeter Zijlstra * Since there is no serialization what so ever against page_remove_rmap() 466ad8a20cfSMiaohe Lin * the best this function can do is return a refcount increased anon_vma 467ad8a20cfSMiaohe Lin * that might have been relevant to this page. 4686111e4caSPeter Zijlstra * 4696111e4caSPeter Zijlstra * The page might have been remapped to a different anon_vma or the anon_vma 4706111e4caSPeter Zijlstra * returned may already be freed (and even reused). 4716111e4caSPeter Zijlstra * 472bc658c96SPeter Zijlstra * In case it was remapped to a different anon_vma, the new anon_vma will be a 473bc658c96SPeter Zijlstra * child of the old anon_vma, and the anon_vma lifetime rules will therefore 474bc658c96SPeter Zijlstra * ensure that any anon_vma obtained from the page will still be valid for as 475bc658c96SPeter Zijlstra * long as we observe page_mapped() [ hence all those page_mapped() tests ]. 476bc658c96SPeter Zijlstra * 4776111e4caSPeter Zijlstra * All users of this function must be very careful when walking the anon_vma 4786111e4caSPeter Zijlstra * chain and verify that the page in question is indeed mapped in it 4796111e4caSPeter Zijlstra * [ something equivalent to page_mapped_in_vma() ]. 4806111e4caSPeter Zijlstra * 481091e4299SMiles Chen * Since anon_vma's slab is SLAB_TYPESAFE_BY_RCU and we know from 482091e4299SMiles Chen * page_remove_rmap() that the anon_vma pointer from page->mapping is valid 483091e4299SMiles Chen * if there is a mapcount, we can dereference the anon_vma after observing 484091e4299SMiles Chen * those. 4851da177e4SLinus Torvalds */ 486746b18d4SPeter Zijlstra struct anon_vma *page_get_anon_vma(struct page *page) 4871da177e4SLinus Torvalds { 488746b18d4SPeter Zijlstra struct anon_vma *anon_vma = NULL; 4891da177e4SLinus Torvalds unsigned long anon_mapping; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds rcu_read_lock(); 4924db0c3c2SJason Low anon_mapping = (unsigned long)READ_ONCE(page->mapping); 4933ca7b3c5SHugh Dickins if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON) 4941da177e4SLinus Torvalds goto out; 4951da177e4SLinus Torvalds if (!page_mapped(page)) 4961da177e4SLinus Torvalds goto out; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON); 499746b18d4SPeter Zijlstra if (!atomic_inc_not_zero(&anon_vma->refcount)) { 500746b18d4SPeter Zijlstra anon_vma = NULL; 501746b18d4SPeter Zijlstra goto out; 502746b18d4SPeter Zijlstra } 503f1819427SHugh Dickins 504f1819427SHugh Dickins /* 505f1819427SHugh Dickins * If this page is still mapped, then its anon_vma cannot have been 506746b18d4SPeter Zijlstra * freed. But if it has been unmapped, we have no security against the 507746b18d4SPeter Zijlstra * anon_vma structure being freed and reused (for another anon_vma: 5085f0d5a3aSPaul E. McKenney * SLAB_TYPESAFE_BY_RCU guarantees that - so the atomic_inc_not_zero() 509746b18d4SPeter Zijlstra * above cannot corrupt). 510f1819427SHugh Dickins */ 511746b18d4SPeter Zijlstra if (!page_mapped(page)) { 5127f39dda9SHugh Dickins rcu_read_unlock(); 513746b18d4SPeter Zijlstra put_anon_vma(anon_vma); 5147f39dda9SHugh Dickins return NULL; 515746b18d4SPeter Zijlstra } 5161da177e4SLinus Torvalds out: 5171da177e4SLinus Torvalds rcu_read_unlock(); 518746b18d4SPeter Zijlstra 519746b18d4SPeter Zijlstra return anon_vma; 520746b18d4SPeter Zijlstra } 521746b18d4SPeter Zijlstra 52288c22088SPeter Zijlstra /* 52388c22088SPeter Zijlstra * Similar to page_get_anon_vma() except it locks the anon_vma. 52488c22088SPeter Zijlstra * 52588c22088SPeter Zijlstra * Its a little more complex as it tries to keep the fast path to a single 52688c22088SPeter Zijlstra * atomic op -- the trylock. If we fail the trylock, we fall back to getting a 52788c22088SPeter Zijlstra * reference like with page_get_anon_vma() and then block on the mutex. 52888c22088SPeter Zijlstra */ 529*9595d769SMatthew Wilcox (Oracle) struct anon_vma *folio_lock_anon_vma_read(struct folio *folio) 530746b18d4SPeter Zijlstra { 53188c22088SPeter Zijlstra struct anon_vma *anon_vma = NULL; 532eee0f252SHugh Dickins struct anon_vma *root_anon_vma; 53388c22088SPeter Zijlstra unsigned long anon_mapping; 534746b18d4SPeter Zijlstra 53588c22088SPeter Zijlstra rcu_read_lock(); 536*9595d769SMatthew Wilcox (Oracle) anon_mapping = (unsigned long)READ_ONCE(folio->mapping); 53788c22088SPeter Zijlstra if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON) 53888c22088SPeter Zijlstra goto out; 539*9595d769SMatthew Wilcox (Oracle) if (!folio_mapped(folio)) 54088c22088SPeter Zijlstra goto out; 54188c22088SPeter Zijlstra 54288c22088SPeter Zijlstra anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON); 5434db0c3c2SJason Low root_anon_vma = READ_ONCE(anon_vma->root); 5444fc3f1d6SIngo Molnar if (down_read_trylock(&root_anon_vma->rwsem)) { 54588c22088SPeter Zijlstra /* 546*9595d769SMatthew Wilcox (Oracle) * If the folio is still mapped, then this anon_vma is still 547eee0f252SHugh Dickins * its anon_vma, and holding the mutex ensures that it will 548bc658c96SPeter Zijlstra * not go away, see anon_vma_free(). 54988c22088SPeter Zijlstra */ 550*9595d769SMatthew Wilcox (Oracle) if (!folio_mapped(folio)) { 5514fc3f1d6SIngo Molnar up_read(&root_anon_vma->rwsem); 55288c22088SPeter Zijlstra anon_vma = NULL; 55388c22088SPeter Zijlstra } 55488c22088SPeter Zijlstra goto out; 55588c22088SPeter Zijlstra } 55688c22088SPeter Zijlstra 55788c22088SPeter Zijlstra /* trylock failed, we got to sleep */ 55888c22088SPeter Zijlstra if (!atomic_inc_not_zero(&anon_vma->refcount)) { 55988c22088SPeter Zijlstra anon_vma = NULL; 56088c22088SPeter Zijlstra goto out; 56188c22088SPeter Zijlstra } 56288c22088SPeter Zijlstra 563*9595d769SMatthew Wilcox (Oracle) if (!folio_mapped(folio)) { 5647f39dda9SHugh Dickins rcu_read_unlock(); 56588c22088SPeter Zijlstra put_anon_vma(anon_vma); 5667f39dda9SHugh Dickins return NULL; 56788c22088SPeter Zijlstra } 56888c22088SPeter Zijlstra 56988c22088SPeter Zijlstra /* we pinned the anon_vma, its safe to sleep */ 57088c22088SPeter Zijlstra rcu_read_unlock(); 5714fc3f1d6SIngo Molnar anon_vma_lock_read(anon_vma); 572746b18d4SPeter Zijlstra 57388c22088SPeter Zijlstra if (atomic_dec_and_test(&anon_vma->refcount)) { 57488c22088SPeter Zijlstra /* 57588c22088SPeter Zijlstra * Oops, we held the last refcount, release the lock 57688c22088SPeter Zijlstra * and bail -- can't simply use put_anon_vma() because 5774fc3f1d6SIngo Molnar * we'll deadlock on the anon_vma_lock_write() recursion. 57888c22088SPeter Zijlstra */ 5794fc3f1d6SIngo Molnar anon_vma_unlock_read(anon_vma); 58088c22088SPeter Zijlstra __put_anon_vma(anon_vma); 58188c22088SPeter Zijlstra anon_vma = NULL; 58288c22088SPeter Zijlstra } 58388c22088SPeter Zijlstra 58488c22088SPeter Zijlstra return anon_vma; 58588c22088SPeter Zijlstra 58688c22088SPeter Zijlstra out: 58788c22088SPeter Zijlstra rcu_read_unlock(); 588746b18d4SPeter Zijlstra return anon_vma; 58934bbd704SOleg Nesterov } 59034bbd704SOleg Nesterov 5914fc3f1d6SIngo Molnar void page_unlock_anon_vma_read(struct anon_vma *anon_vma) 59234bbd704SOleg Nesterov { 5934fc3f1d6SIngo Molnar anon_vma_unlock_read(anon_vma); 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 59672b252aeSMel Gorman #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH 59772b252aeSMel Gorman /* 59872b252aeSMel Gorman * Flush TLB entries for recently unmapped pages from remote CPUs. It is 59972b252aeSMel Gorman * important if a PTE was dirty when it was unmapped that it's flushed 60072b252aeSMel Gorman * before any IO is initiated on the page to prevent lost writes. Similarly, 60172b252aeSMel Gorman * it must be flushed before freeing to prevent data leakage. 60272b252aeSMel Gorman */ 60372b252aeSMel Gorman void try_to_unmap_flush(void) 60472b252aeSMel Gorman { 60572b252aeSMel Gorman struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; 60672b252aeSMel Gorman 60772b252aeSMel Gorman if (!tlb_ubc->flush_required) 60872b252aeSMel Gorman return; 60972b252aeSMel Gorman 610e73ad5ffSAndy Lutomirski arch_tlbbatch_flush(&tlb_ubc->arch); 61172b252aeSMel Gorman tlb_ubc->flush_required = false; 612d950c947SMel Gorman tlb_ubc->writable = false; 61372b252aeSMel Gorman } 61472b252aeSMel Gorman 615d950c947SMel Gorman /* Flush iff there are potentially writable TLB entries that can race with IO */ 616d950c947SMel Gorman void try_to_unmap_flush_dirty(void) 617d950c947SMel Gorman { 618d950c947SMel Gorman struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; 619d950c947SMel Gorman 620d950c947SMel Gorman if (tlb_ubc->writable) 621d950c947SMel Gorman try_to_unmap_flush(); 622d950c947SMel Gorman } 623d950c947SMel Gorman 6245ee2fa2fSHuang Ying /* 6255ee2fa2fSHuang Ying * Bits 0-14 of mm->tlb_flush_batched record pending generations. 6265ee2fa2fSHuang Ying * Bits 16-30 of mm->tlb_flush_batched bit record flushed generations. 6275ee2fa2fSHuang Ying */ 6285ee2fa2fSHuang Ying #define TLB_FLUSH_BATCH_FLUSHED_SHIFT 16 6295ee2fa2fSHuang Ying #define TLB_FLUSH_BATCH_PENDING_MASK \ 6305ee2fa2fSHuang Ying ((1 << (TLB_FLUSH_BATCH_FLUSHED_SHIFT - 1)) - 1) 6315ee2fa2fSHuang Ying #define TLB_FLUSH_BATCH_PENDING_LARGE \ 6325ee2fa2fSHuang Ying (TLB_FLUSH_BATCH_PENDING_MASK / 2) 6335ee2fa2fSHuang Ying 634c7ab0d2fSKirill A. Shutemov static void set_tlb_ubc_flush_pending(struct mm_struct *mm, bool writable) 63572b252aeSMel Gorman { 63672b252aeSMel Gorman struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; 6375ee2fa2fSHuang Ying int batch, nbatch; 63872b252aeSMel Gorman 639e73ad5ffSAndy Lutomirski arch_tlbbatch_add_mm(&tlb_ubc->arch, mm); 64072b252aeSMel Gorman tlb_ubc->flush_required = true; 641d950c947SMel Gorman 642d950c947SMel Gorman /* 6433ea27719SMel Gorman * Ensure compiler does not re-order the setting of tlb_flush_batched 6443ea27719SMel Gorman * before the PTE is cleared. 6453ea27719SMel Gorman */ 6463ea27719SMel Gorman barrier(); 6475ee2fa2fSHuang Ying batch = atomic_read(&mm->tlb_flush_batched); 6485ee2fa2fSHuang Ying retry: 6495ee2fa2fSHuang Ying if ((batch & TLB_FLUSH_BATCH_PENDING_MASK) > TLB_FLUSH_BATCH_PENDING_LARGE) { 6505ee2fa2fSHuang Ying /* 6515ee2fa2fSHuang Ying * Prevent `pending' from catching up with `flushed' because of 6525ee2fa2fSHuang Ying * overflow. Reset `pending' and `flushed' to be 1 and 0 if 6535ee2fa2fSHuang Ying * `pending' becomes large. 6545ee2fa2fSHuang Ying */ 6555ee2fa2fSHuang Ying nbatch = atomic_cmpxchg(&mm->tlb_flush_batched, batch, 1); 6565ee2fa2fSHuang Ying if (nbatch != batch) { 6575ee2fa2fSHuang Ying batch = nbatch; 6585ee2fa2fSHuang Ying goto retry; 6595ee2fa2fSHuang Ying } 6605ee2fa2fSHuang Ying } else { 6615ee2fa2fSHuang Ying atomic_inc(&mm->tlb_flush_batched); 6625ee2fa2fSHuang Ying } 6633ea27719SMel Gorman 6643ea27719SMel Gorman /* 665d950c947SMel Gorman * If the PTE was dirty then it's best to assume it's writable. The 666d950c947SMel Gorman * caller must use try_to_unmap_flush_dirty() or try_to_unmap_flush() 667d950c947SMel Gorman * before the page is queued for IO. 668d950c947SMel Gorman */ 669d950c947SMel Gorman if (writable) 670d950c947SMel Gorman tlb_ubc->writable = true; 67172b252aeSMel Gorman } 67272b252aeSMel Gorman 67372b252aeSMel Gorman /* 67472b252aeSMel Gorman * Returns true if the TLB flush should be deferred to the end of a batch of 67572b252aeSMel Gorman * unmap operations to reduce IPIs. 67672b252aeSMel Gorman */ 67772b252aeSMel Gorman static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags) 67872b252aeSMel Gorman { 67972b252aeSMel Gorman bool should_defer = false; 68072b252aeSMel Gorman 68172b252aeSMel Gorman if (!(flags & TTU_BATCH_FLUSH)) 68272b252aeSMel Gorman return false; 68372b252aeSMel Gorman 68472b252aeSMel Gorman /* If remote CPUs need to be flushed then defer batch the flush */ 68572b252aeSMel Gorman if (cpumask_any_but(mm_cpumask(mm), get_cpu()) < nr_cpu_ids) 68672b252aeSMel Gorman should_defer = true; 68772b252aeSMel Gorman put_cpu(); 68872b252aeSMel Gorman 68972b252aeSMel Gorman return should_defer; 69072b252aeSMel Gorman } 6913ea27719SMel Gorman 6923ea27719SMel Gorman /* 6933ea27719SMel Gorman * Reclaim unmaps pages under the PTL but do not flush the TLB prior to 6943ea27719SMel Gorman * releasing the PTL if TLB flushes are batched. It's possible for a parallel 6953ea27719SMel Gorman * operation such as mprotect or munmap to race between reclaim unmapping 6963ea27719SMel Gorman * the page and flushing the page. If this race occurs, it potentially allows 6973ea27719SMel Gorman * access to data via a stale TLB entry. Tracking all mm's that have TLB 6983ea27719SMel Gorman * batching in flight would be expensive during reclaim so instead track 6993ea27719SMel Gorman * whether TLB batching occurred in the past and if so then do a flush here 7003ea27719SMel Gorman * if required. This will cost one additional flush per reclaim cycle paid 7013ea27719SMel Gorman * by the first operation at risk such as mprotect and mumap. 7023ea27719SMel Gorman * 7033ea27719SMel Gorman * This must be called under the PTL so that an access to tlb_flush_batched 7043ea27719SMel Gorman * that is potentially a "reclaim vs mprotect/munmap/etc" race will synchronise 7053ea27719SMel Gorman * via the PTL. 7063ea27719SMel Gorman */ 7073ea27719SMel Gorman void flush_tlb_batched_pending(struct mm_struct *mm) 7083ea27719SMel Gorman { 7095ee2fa2fSHuang Ying int batch = atomic_read(&mm->tlb_flush_batched); 7105ee2fa2fSHuang Ying int pending = batch & TLB_FLUSH_BATCH_PENDING_MASK; 7115ee2fa2fSHuang Ying int flushed = batch >> TLB_FLUSH_BATCH_FLUSHED_SHIFT; 7123ea27719SMel Gorman 7135ee2fa2fSHuang Ying if (pending != flushed) { 7145ee2fa2fSHuang Ying flush_tlb_mm(mm); 7153ea27719SMel Gorman /* 7165ee2fa2fSHuang Ying * If the new TLB flushing is pending during flushing, leave 7175ee2fa2fSHuang Ying * mm->tlb_flush_batched as is, to avoid losing flushing. 7183ea27719SMel Gorman */ 7195ee2fa2fSHuang Ying atomic_cmpxchg(&mm->tlb_flush_batched, batch, 7205ee2fa2fSHuang Ying pending | (pending << TLB_FLUSH_BATCH_FLUSHED_SHIFT)); 7213ea27719SMel Gorman } 7223ea27719SMel Gorman } 72372b252aeSMel Gorman #else 724c7ab0d2fSKirill A. Shutemov static void set_tlb_ubc_flush_pending(struct mm_struct *mm, bool writable) 72572b252aeSMel Gorman { 72672b252aeSMel Gorman } 72772b252aeSMel Gorman 72872b252aeSMel Gorman static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags) 72972b252aeSMel Gorman { 73072b252aeSMel Gorman return false; 73172b252aeSMel Gorman } 73272b252aeSMel Gorman #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ 73372b252aeSMel Gorman 7341da177e4SLinus Torvalds /* 735bf89c8c8SHuang Shijie * At what user virtual address is page expected in vma? 736ab941e0fSNaoya Horiguchi * Caller should check the page is actually part of the vma. 7371da177e4SLinus Torvalds */ 7381da177e4SLinus Torvalds unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma) 7391da177e4SLinus Torvalds { 74021d0d443SAndrea Arcangeli if (PageAnon(page)) { 7414829b906SHugh Dickins struct anon_vma *page__anon_vma = page_anon_vma(page); 7424829b906SHugh Dickins /* 7434829b906SHugh Dickins * Note: swapoff's unuse_vma() is more efficient with this 7444829b906SHugh Dickins * check, and needs it to match anon_vma when KSM is active. 7454829b906SHugh Dickins */ 7464829b906SHugh Dickins if (!vma->anon_vma || !page__anon_vma || 7474829b906SHugh Dickins vma->anon_vma->root != page__anon_vma->root) 74821d0d443SAndrea Arcangeli return -EFAULT; 74931657170SJue Wang } else if (!vma->vm_file) { 7501da177e4SLinus Torvalds return -EFAULT; 75131657170SJue Wang } else if (vma->vm_file->f_mapping != compound_head(page)->mapping) { 7521da177e4SLinus Torvalds return -EFAULT; 75331657170SJue Wang } 754494334e4SHugh Dickins 755494334e4SHugh Dickins return vma_address(page, vma); 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 7586219049aSBob Liu pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address) 7596219049aSBob Liu { 7606219049aSBob Liu pgd_t *pgd; 761c2febafcSKirill A. Shutemov p4d_t *p4d; 7626219049aSBob Liu pud_t *pud; 7636219049aSBob Liu pmd_t *pmd = NULL; 764f72e7dcdSHugh Dickins pmd_t pmde; 7656219049aSBob Liu 7666219049aSBob Liu pgd = pgd_offset(mm, address); 7676219049aSBob Liu if (!pgd_present(*pgd)) 7686219049aSBob Liu goto out; 7696219049aSBob Liu 770c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, address); 771c2febafcSKirill A. Shutemov if (!p4d_present(*p4d)) 772c2febafcSKirill A. Shutemov goto out; 773c2febafcSKirill A. Shutemov 774c2febafcSKirill A. Shutemov pud = pud_offset(p4d, address); 7756219049aSBob Liu if (!pud_present(*pud)) 7766219049aSBob Liu goto out; 7776219049aSBob Liu 7786219049aSBob Liu pmd = pmd_offset(pud, address); 779f72e7dcdSHugh Dickins /* 7808809aa2dSAneesh Kumar K.V * Some THP functions use the sequence pmdp_huge_clear_flush(), set_pmd_at() 781f72e7dcdSHugh Dickins * without holding anon_vma lock for write. So when looking for a 782f72e7dcdSHugh Dickins * genuine pmde (in which to find pte), test present and !THP together. 783f72e7dcdSHugh Dickins */ 784e37c6982SChristian Borntraeger pmde = *pmd; 785e37c6982SChristian Borntraeger barrier(); 786f72e7dcdSHugh Dickins if (!pmd_present(pmde) || pmd_trans_huge(pmde)) 7876219049aSBob Liu pmd = NULL; 7886219049aSBob Liu out: 7896219049aSBob Liu return pmd; 7906219049aSBob Liu } 7916219049aSBob Liu 792b3ac0413SMatthew Wilcox (Oracle) struct folio_referenced_arg { 7939f32624bSJoonsoo Kim int mapcount; 7949f32624bSJoonsoo Kim int referenced; 7959f32624bSJoonsoo Kim unsigned long vm_flags; 7969f32624bSJoonsoo Kim struct mem_cgroup *memcg; 7979f32624bSJoonsoo Kim }; 79881b4082dSNikita Danilov /* 799b3ac0413SMatthew Wilcox (Oracle) * arg: folio_referenced_arg will be passed 8001da177e4SLinus Torvalds */ 801b3ac0413SMatthew Wilcox (Oracle) static bool folio_referenced_one(struct page *page, struct vm_area_struct *vma, 8029f32624bSJoonsoo Kim unsigned long address, void *arg) 8031da177e4SLinus Torvalds { 804b3ac0413SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 805b3ac0413SMatthew Wilcox (Oracle) struct folio_referenced_arg *pra = arg; 806b3ac0413SMatthew Wilcox (Oracle) DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); 8078749cfeaSVladimir Davydov int referenced = 0; 8082da28bfdSAndrea Arcangeli 8098eaededeSKirill A. Shutemov while (page_vma_mapped_walk(&pvmw)) { 8108eaededeSKirill A. Shutemov address = pvmw.address; 8112da28bfdSAndrea Arcangeli 81247d4f3eeSHugh Dickins if ((vma->vm_flags & VM_LOCKED) && 813b3ac0413SMatthew Wilcox (Oracle) (!folio_test_large(folio) || !pvmw.pte)) { 81447d4f3eeSHugh Dickins /* Restore the mlock which got missed */ 815b3ac0413SMatthew Wilcox (Oracle) mlock_vma_folio(folio, vma, !pvmw.pte); 8168eaededeSKirill A. Shutemov page_vma_mapped_walk_done(&pvmw); 8179f32624bSJoonsoo Kim pra->vm_flags |= VM_LOCKED; 818e4b82222SMinchan Kim return false; /* To break the loop */ 8192da28bfdSAndrea Arcangeli } 8202da28bfdSAndrea Arcangeli 8218eaededeSKirill A. Shutemov if (pvmw.pte) { 8228eaededeSKirill A. Shutemov if (ptep_clear_flush_young_notify(vma, address, 8238eaededeSKirill A. Shutemov pvmw.pte)) { 8244917e5d0SJohannes Weiner /* 8258eaededeSKirill A. Shutemov * Don't treat a reference through 8268eaededeSKirill A. Shutemov * a sequentially read mapping as such. 827b3ac0413SMatthew Wilcox (Oracle) * If the folio has been used in another mapping, 8288eaededeSKirill A. Shutemov * we will catch it; if this other mapping is 8298eaededeSKirill A. Shutemov * already gone, the unmap path will have set 830b3ac0413SMatthew Wilcox (Oracle) * the referenced flag or activated the folio. 8314917e5d0SJohannes Weiner */ 83264363aadSJoe Perches if (likely(!(vma->vm_flags & VM_SEQ_READ))) 8331da177e4SLinus Torvalds referenced++; 8344917e5d0SJohannes Weiner } 8358749cfeaSVladimir Davydov } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { 8368eaededeSKirill A. Shutemov if (pmdp_clear_flush_young_notify(vma, address, 8378eaededeSKirill A. Shutemov pvmw.pmd)) 8388749cfeaSVladimir Davydov referenced++; 8398749cfeaSVladimir Davydov } else { 840b3ac0413SMatthew Wilcox (Oracle) /* unexpected pmd-mapped folio? */ 8418749cfeaSVladimir Davydov WARN_ON_ONCE(1); 8428749cfeaSVladimir Davydov } 8438eaededeSKirill A. Shutemov 8448eaededeSKirill A. Shutemov pra->mapcount--; 8458eaededeSKirill A. Shutemov } 84671e3aac0SAndrea Arcangeli 84733c3fc71SVladimir Davydov if (referenced) 848b3ac0413SMatthew Wilcox (Oracle) folio_clear_idle(folio); 849b3ac0413SMatthew Wilcox (Oracle) if (folio_test_clear_young(folio)) 85033c3fc71SVladimir Davydov referenced++; 85133c3fc71SVladimir Davydov 8529f32624bSJoonsoo Kim if (referenced) { 8539f32624bSJoonsoo Kim pra->referenced++; 85447d4f3eeSHugh Dickins pra->vm_flags |= vma->vm_flags & ~VM_LOCKED; 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 8579f32624bSJoonsoo Kim if (!pra->mapcount) 858e4b82222SMinchan Kim return false; /* To break the loop */ 8599f32624bSJoonsoo Kim 860e4b82222SMinchan Kim return true; 8619f32624bSJoonsoo Kim } 8629f32624bSJoonsoo Kim 863b3ac0413SMatthew Wilcox (Oracle) static bool invalid_folio_referenced_vma(struct vm_area_struct *vma, void *arg) 8641da177e4SLinus Torvalds { 865b3ac0413SMatthew Wilcox (Oracle) struct folio_referenced_arg *pra = arg; 8669f32624bSJoonsoo Kim struct mem_cgroup *memcg = pra->memcg; 8671da177e4SLinus Torvalds 8689f32624bSJoonsoo Kim if (!mm_match_cgroup(vma->vm_mm, memcg)) 8699f32624bSJoonsoo Kim return true; 8701da177e4SLinus Torvalds 8719f32624bSJoonsoo Kim return false; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds /** 875b3ac0413SMatthew Wilcox (Oracle) * folio_referenced() - Test if the folio was referenced. 876b3ac0413SMatthew Wilcox (Oracle) * @folio: The folio to test. 877b3ac0413SMatthew Wilcox (Oracle) * @is_locked: Caller holds lock on the folio. 87872835c86SJohannes Weiner * @memcg: target memory cgroup 879b3ac0413SMatthew Wilcox (Oracle) * @vm_flags: A combination of all the vma->vm_flags which referenced the folio. 8801da177e4SLinus Torvalds * 881b3ac0413SMatthew Wilcox (Oracle) * Quick test_and_clear_referenced for all mappings of a folio, 882b3ac0413SMatthew Wilcox (Oracle) * 883b3ac0413SMatthew Wilcox (Oracle) * Return: The number of mappings which referenced the folio. 8841da177e4SLinus Torvalds */ 885b3ac0413SMatthew Wilcox (Oracle) int folio_referenced(struct folio *folio, int is_locked, 886b3ac0413SMatthew Wilcox (Oracle) struct mem_cgroup *memcg, unsigned long *vm_flags) 8871da177e4SLinus Torvalds { 8885ad64688SHugh Dickins int we_locked = 0; 889b3ac0413SMatthew Wilcox (Oracle) struct folio_referenced_arg pra = { 890b3ac0413SMatthew Wilcox (Oracle) .mapcount = folio_mapcount(folio), 8919f32624bSJoonsoo Kim .memcg = memcg, 8929f32624bSJoonsoo Kim }; 8939f32624bSJoonsoo Kim struct rmap_walk_control rwc = { 894b3ac0413SMatthew Wilcox (Oracle) .rmap_one = folio_referenced_one, 8959f32624bSJoonsoo Kim .arg = (void *)&pra, 8969f32624bSJoonsoo Kim .anon_lock = page_lock_anon_vma_read, 8979f32624bSJoonsoo Kim }; 8981da177e4SLinus Torvalds 8996fe6b7e3SWu Fengguang *vm_flags = 0; 900059d8442SHuang Shijie if (!pra.mapcount) 9019f32624bSJoonsoo Kim return 0; 9029f32624bSJoonsoo Kim 903b3ac0413SMatthew Wilcox (Oracle) if (!folio_raw_mapping(folio)) 9049f32624bSJoonsoo Kim return 0; 9059f32624bSJoonsoo Kim 906b3ac0413SMatthew Wilcox (Oracle) if (!is_locked && (!folio_test_anon(folio) || folio_test_ksm(folio))) { 907b3ac0413SMatthew Wilcox (Oracle) we_locked = folio_trylock(folio); 9089f32624bSJoonsoo Kim if (!we_locked) 9099f32624bSJoonsoo Kim return 1; 9105ad64688SHugh Dickins } 9119f32624bSJoonsoo Kim 9129f32624bSJoonsoo Kim /* 9139f32624bSJoonsoo Kim * If we are reclaiming on behalf of a cgroup, skip 9149f32624bSJoonsoo Kim * counting on behalf of references from different 9159f32624bSJoonsoo Kim * cgroups 9169f32624bSJoonsoo Kim */ 9179f32624bSJoonsoo Kim if (memcg) { 918b3ac0413SMatthew Wilcox (Oracle) rwc.invalid_vma = invalid_folio_referenced_vma; 9195ad64688SHugh Dickins } 9209f32624bSJoonsoo Kim 921b3ac0413SMatthew Wilcox (Oracle) rmap_walk(&folio->page, &rwc); 9229f32624bSJoonsoo Kim *vm_flags = pra.vm_flags; 9239f32624bSJoonsoo Kim 9245ad64688SHugh Dickins if (we_locked) 925b3ac0413SMatthew Wilcox (Oracle) folio_unlock(folio); 9269f32624bSJoonsoo Kim 9279f32624bSJoonsoo Kim return pra.referenced; 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds 930e4b82222SMinchan Kim static bool page_mkclean_one(struct page *page, struct vm_area_struct *vma, 9319853a407SJoonsoo Kim unsigned long address, void *arg) 932d08b3851SPeter Zijlstra { 933e83c09a2SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 934e83c09a2SMatthew Wilcox (Oracle) DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, PVMW_SYNC); 935ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 9369853a407SJoonsoo Kim int *cleaned = arg; 937d08b3851SPeter Zijlstra 938369ea824SJérôme Glisse /* 939369ea824SJérôme Glisse * We have to assume the worse case ie pmd for invalidation. Note that 940e83c09a2SMatthew Wilcox (Oracle) * the folio can not be freed from this function. 941369ea824SJérôme Glisse */ 9427269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE, 9437269f999SJérôme Glisse 0, vma, vma->vm_mm, address, 9442aff7a47SMatthew Wilcox (Oracle) vma_address_end(&pvmw)); 945ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 946369ea824SJérôme Glisse 947f27176cfSKirill A. Shutemov while (page_vma_mapped_walk(&pvmw)) { 948f27176cfSKirill A. Shutemov int ret = 0; 949369ea824SJérôme Glisse 9501f18b296SYueHaibing address = pvmw.address; 951f27176cfSKirill A. Shutemov if (pvmw.pte) { 952c2fda5feSPeter Zijlstra pte_t entry; 953f27176cfSKirill A. Shutemov pte_t *pte = pvmw.pte; 954f27176cfSKirill A. Shutemov 955f27176cfSKirill A. Shutemov if (!pte_dirty(*pte) && !pte_write(*pte)) 956f27176cfSKirill A. Shutemov continue; 957d08b3851SPeter Zijlstra 958785373b4SLinus Torvalds flush_cache_page(vma, address, pte_pfn(*pte)); 959785373b4SLinus Torvalds entry = ptep_clear_flush(vma, address, pte); 960d08b3851SPeter Zijlstra entry = pte_wrprotect(entry); 961c2fda5feSPeter Zijlstra entry = pte_mkclean(entry); 962785373b4SLinus Torvalds set_pte_at(vma->vm_mm, address, pte, entry); 963d08b3851SPeter Zijlstra ret = 1; 964f27176cfSKirill A. Shutemov } else { 965396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 966f27176cfSKirill A. Shutemov pmd_t *pmd = pvmw.pmd; 967f27176cfSKirill A. Shutemov pmd_t entry; 968d08b3851SPeter Zijlstra 969f27176cfSKirill A. Shutemov if (!pmd_dirty(*pmd) && !pmd_write(*pmd)) 970f27176cfSKirill A. Shutemov continue; 971f27176cfSKirill A. Shutemov 972e83c09a2SMatthew Wilcox (Oracle) flush_cache_page(vma, address, folio_pfn(folio)); 973024eee0eSAneesh Kumar K.V entry = pmdp_invalidate(vma, address, pmd); 974f27176cfSKirill A. Shutemov entry = pmd_wrprotect(entry); 975f27176cfSKirill A. Shutemov entry = pmd_mkclean(entry); 976785373b4SLinus Torvalds set_pmd_at(vma->vm_mm, address, pmd, entry); 977f27176cfSKirill A. Shutemov ret = 1; 978f27176cfSKirill A. Shutemov #else 979e83c09a2SMatthew Wilcox (Oracle) /* unexpected pmd-mapped folio? */ 980f27176cfSKirill A. Shutemov WARN_ON_ONCE(1); 981f27176cfSKirill A. Shutemov #endif 982f27176cfSKirill A. Shutemov } 9832ec74c3eSSagi Grimberg 9840f10851eSJérôme Glisse /* 9850f10851eSJérôme Glisse * No need to call mmu_notifier_invalidate_range() as we are 9860f10851eSJérôme Glisse * downgrading page table protection not changing it to point 9870f10851eSJérôme Glisse * to a new page. 9880f10851eSJérôme Glisse * 989ad56b738SMike Rapoport * See Documentation/vm/mmu_notifier.rst 9900f10851eSJérôme Glisse */ 9910f10851eSJérôme Glisse if (ret) 9929853a407SJoonsoo Kim (*cleaned)++; 9939853a407SJoonsoo Kim } 994f27176cfSKirill A. Shutemov 995ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 996369ea824SJérôme Glisse 997e4b82222SMinchan Kim return true; 998d08b3851SPeter Zijlstra } 999d08b3851SPeter Zijlstra 10009853a407SJoonsoo Kim static bool invalid_mkclean_vma(struct vm_area_struct *vma, void *arg) 1001d08b3851SPeter Zijlstra { 10029853a407SJoonsoo Kim if (vma->vm_flags & VM_SHARED) 1003871beb8cSFengguang Wu return false; 1004d08b3851SPeter Zijlstra 1005871beb8cSFengguang Wu return true; 1006d08b3851SPeter Zijlstra } 1007d08b3851SPeter Zijlstra 1008d9c08e22SMatthew Wilcox (Oracle) int folio_mkclean(struct folio *folio) 1009d08b3851SPeter Zijlstra { 10109853a407SJoonsoo Kim int cleaned = 0; 10119853a407SJoonsoo Kim struct address_space *mapping; 10129853a407SJoonsoo Kim struct rmap_walk_control rwc = { 10139853a407SJoonsoo Kim .arg = (void *)&cleaned, 10149853a407SJoonsoo Kim .rmap_one = page_mkclean_one, 10159853a407SJoonsoo Kim .invalid_vma = invalid_mkclean_vma, 10169853a407SJoonsoo Kim }; 1017d08b3851SPeter Zijlstra 1018d9c08e22SMatthew Wilcox (Oracle) BUG_ON(!folio_test_locked(folio)); 1019d08b3851SPeter Zijlstra 1020d9c08e22SMatthew Wilcox (Oracle) if (!folio_mapped(folio)) 10219853a407SJoonsoo Kim return 0; 1022d08b3851SPeter Zijlstra 1023d9c08e22SMatthew Wilcox (Oracle) mapping = folio_mapping(folio); 10249853a407SJoonsoo Kim if (!mapping) 10259853a407SJoonsoo Kim return 0; 10269853a407SJoonsoo Kim 1027d9c08e22SMatthew Wilcox (Oracle) rmap_walk(&folio->page, &rwc); 10289853a407SJoonsoo Kim 10299853a407SJoonsoo Kim return cleaned; 1030d08b3851SPeter Zijlstra } 1031d9c08e22SMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(folio_mkclean); 1032d08b3851SPeter Zijlstra 10331da177e4SLinus Torvalds /** 1034c44b6743SRik van Riel * page_move_anon_rmap - move a page to our anon_vma 1035c44b6743SRik van Riel * @page: the page to move to our anon_vma 1036c44b6743SRik van Riel * @vma: the vma the page belongs to 1037c44b6743SRik van Riel * 1038c44b6743SRik van Riel * When a page belongs exclusively to one process after a COW event, 1039c44b6743SRik van Riel * that page can be moved into the anon_vma that belongs to just that 1040c44b6743SRik van Riel * process, so the rmap code will not search the parent or sibling 1041c44b6743SRik van Riel * processes. 1042c44b6743SRik van Riel */ 10435a49973dSHugh Dickins void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma) 1044c44b6743SRik van Riel { 1045c44b6743SRik van Riel struct anon_vma *anon_vma = vma->anon_vma; 1046c44b6743SRik van Riel 10475a49973dSHugh Dickins page = compound_head(page); 10485a49973dSHugh Dickins 1049309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 105081d1b09cSSasha Levin VM_BUG_ON_VMA(!anon_vma, vma); 1051c44b6743SRik van Riel 1052c44b6743SRik van Riel anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON; 1053414e2fb8SVladimir Davydov /* 1054414e2fb8SVladimir Davydov * Ensure that anon_vma and the PAGE_MAPPING_ANON bit are written 1055b3ac0413SMatthew Wilcox (Oracle) * simultaneously, so a concurrent reader (eg folio_referenced()'s 1056b3ac0413SMatthew Wilcox (Oracle) * folio_test_anon()) will not see one without the other. 1057414e2fb8SVladimir Davydov */ 1058414e2fb8SVladimir Davydov WRITE_ONCE(page->mapping, (struct address_space *) anon_vma); 1059c44b6743SRik van Riel } 1060c44b6743SRik van Riel 1061c44b6743SRik van Riel /** 106243d8eac4SRandy Dunlap * __page_set_anon_rmap - set up new anonymous rmap 1063451b9514SKirill Tkhai * @page: Page or Hugepage to add to rmap 10644e1c1975SAndi Kleen * @vma: VM area to add page to. 10654e1c1975SAndi Kleen * @address: User virtual address of the mapping 1066e8a03febSRik van Riel * @exclusive: the page is exclusively owned by the current process 10671da177e4SLinus Torvalds */ 10689617d95eSNick Piggin static void __page_set_anon_rmap(struct page *page, 1069e8a03febSRik van Riel struct vm_area_struct *vma, unsigned long address, int exclusive) 10701da177e4SLinus Torvalds { 1071e8a03febSRik van Riel struct anon_vma *anon_vma = vma->anon_vma; 10722822c1aaSNick Piggin 1073e8a03febSRik van Riel BUG_ON(!anon_vma); 1074ea90002bSLinus Torvalds 10754e1c1975SAndi Kleen if (PageAnon(page)) 10764e1c1975SAndi Kleen return; 10774e1c1975SAndi Kleen 1078ea90002bSLinus Torvalds /* 1079e8a03febSRik van Riel * If the page isn't exclusively mapped into this vma, 1080e8a03febSRik van Riel * we must use the _oldest_ possible anon_vma for the 1081e8a03febSRik van Riel * page mapping! 1082ea90002bSLinus Torvalds */ 10834e1c1975SAndi Kleen if (!exclusive) 1084288468c3SAndrea Arcangeli anon_vma = anon_vma->root; 1085ea90002bSLinus Torvalds 108616f5e707SAlex Shi /* 108716f5e707SAlex Shi * page_idle does a lockless/optimistic rmap scan on page->mapping. 108816f5e707SAlex Shi * Make sure the compiler doesn't split the stores of anon_vma and 108916f5e707SAlex Shi * the PAGE_MAPPING_ANON type identifier, otherwise the rmap code 109016f5e707SAlex Shi * could mistake the mapping for a struct address_space and crash. 109116f5e707SAlex Shi */ 10921da177e4SLinus Torvalds anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON; 109316f5e707SAlex Shi WRITE_ONCE(page->mapping, (struct address_space *) anon_vma); 10944d7670e0SNick Piggin page->index = linear_page_index(vma, address); 10951da177e4SLinus Torvalds } 10969617d95eSNick Piggin 10979617d95eSNick Piggin /** 109843d8eac4SRandy Dunlap * __page_check_anon_rmap - sanity check anonymous rmap addition 1099c97a9e10SNick Piggin * @page: the page to add the mapping to 1100c97a9e10SNick Piggin * @vma: the vm area in which the mapping is added 1101c97a9e10SNick Piggin * @address: the user virtual address mapped 1102c97a9e10SNick Piggin */ 1103c97a9e10SNick Piggin static void __page_check_anon_rmap(struct page *page, 1104c97a9e10SNick Piggin struct vm_area_struct *vma, unsigned long address) 1105c97a9e10SNick Piggin { 1106c97a9e10SNick Piggin /* 1107c97a9e10SNick Piggin * The page's anon-rmap details (mapping and index) are guaranteed to 1108c97a9e10SNick Piggin * be set up correctly at this point. 1109c97a9e10SNick Piggin * 1110c97a9e10SNick Piggin * We have exclusion against page_add_anon_rmap because the caller 111190aaca85SMiaohe Lin * always holds the page locked. 1112c97a9e10SNick Piggin * 1113c97a9e10SNick Piggin * We have exclusion against page_add_new_anon_rmap because those pages 1114c97a9e10SNick Piggin * are initially only visible via the pagetables, and the pte is locked 1115c97a9e10SNick Piggin * over the call to page_add_new_anon_rmap. 1116c97a9e10SNick Piggin */ 111730c46382SYang Shi VM_BUG_ON_PAGE(page_anon_vma(page)->root != vma->anon_vma->root, page); 111830c46382SYang Shi VM_BUG_ON_PAGE(page_to_pgoff(page) != linear_page_index(vma, address), 111930c46382SYang Shi page); 1120c97a9e10SNick Piggin } 1121c97a9e10SNick Piggin 1122c97a9e10SNick Piggin /** 11239617d95eSNick Piggin * page_add_anon_rmap - add pte mapping to an anonymous page 11249617d95eSNick Piggin * @page: the page to add the mapping to 11259617d95eSNick Piggin * @vma: the vm area in which the mapping is added 11269617d95eSNick Piggin * @address: the user virtual address mapped 1127d281ee61SKirill A. Shutemov * @compound: charge the page as compound or small page 11289617d95eSNick Piggin * 11295ad64688SHugh Dickins * The caller needs to hold the pte lock, and the page must be locked in 113080e14822SHugh Dickins * the anon_vma case: to serialize mapping,index checking after setting, 113180e14822SHugh Dickins * and to ensure that PageAnon is not being upgraded racily to PageKsm 113280e14822SHugh Dickins * (but PageKsm is never downgraded to PageAnon). 11339617d95eSNick Piggin */ 11349617d95eSNick Piggin void page_add_anon_rmap(struct page *page, 1135d281ee61SKirill A. Shutemov struct vm_area_struct *vma, unsigned long address, bool compound) 11369617d95eSNick Piggin { 1137d281ee61SKirill A. Shutemov do_page_add_anon_rmap(page, vma, address, compound ? RMAP_COMPOUND : 0); 1138ad8c2ee8SRik van Riel } 1139ad8c2ee8SRik van Riel 1140ad8c2ee8SRik van Riel /* 1141ad8c2ee8SRik van Riel * Special version of the above for do_swap_page, which often runs 1142ad8c2ee8SRik van Riel * into pages that are exclusively owned by the current process. 1143ad8c2ee8SRik van Riel * Everybody else should continue to use page_add_anon_rmap above. 1144ad8c2ee8SRik van Riel */ 1145ad8c2ee8SRik van Riel void do_page_add_anon_rmap(struct page *page, 1146d281ee61SKirill A. Shutemov struct vm_area_struct *vma, unsigned long address, int flags) 1147ad8c2ee8SRik van Riel { 1148d281ee61SKirill A. Shutemov bool compound = flags & RMAP_COMPOUND; 114953f9263bSKirill A. Shutemov bool first; 115053f9263bSKirill A. Shutemov 1151be5d0a74SJohannes Weiner if (unlikely(PageKsm(page))) 1152be5d0a74SJohannes Weiner lock_page_memcg(page); 1153be5d0a74SJohannes Weiner else 1154be5d0a74SJohannes Weiner VM_BUG_ON_PAGE(!PageLocked(page), page); 1155be5d0a74SJohannes Weiner 115653f9263bSKirill A. Shutemov if (compound) { 115753f9263bSKirill A. Shutemov atomic_t *mapcount; 1158e9b61f19SKirill A. Shutemov VM_BUG_ON_PAGE(!PageLocked(page), page); 115953f9263bSKirill A. Shutemov VM_BUG_ON_PAGE(!PageTransHuge(page), page); 116053f9263bSKirill A. Shutemov mapcount = compound_mapcount_ptr(page); 116153f9263bSKirill A. Shutemov first = atomic_inc_and_test(mapcount); 116253f9263bSKirill A. Shutemov } else { 116353f9263bSKirill A. Shutemov first = atomic_inc_and_test(&page->_mapcount); 116453f9263bSKirill A. Shutemov } 116553f9263bSKirill A. Shutemov 116653f9263bSKirill A. Shutemov if (first) { 11676c357848SMatthew Wilcox (Oracle) int nr = compound ? thp_nr_pages(page) : 1; 1168bea04b07SJianyu Zhan /* 1169bea04b07SJianyu Zhan * We use the irq-unsafe __{inc|mod}_zone_page_stat because 1170bea04b07SJianyu Zhan * these counters are not modified in interrupt context, and 1171bea04b07SJianyu Zhan * pte lock(a spinlock) is held, which implies preemption 1172bea04b07SJianyu Zhan * disabled. 1173bea04b07SJianyu Zhan */ 117465c45377SKirill A. Shutemov if (compound) 117569473e5dSMuchun Song __mod_lruvec_page_state(page, NR_ANON_THPS, nr); 1176be5d0a74SJohannes Weiner __mod_lruvec_page_state(page, NR_ANON_MAPPED, nr); 117779134171SAndrea Arcangeli } 11785ad64688SHugh Dickins 1179cea86fe2SHugh Dickins if (unlikely(PageKsm(page))) 1180be5d0a74SJohannes Weiner unlock_page_memcg(page); 118153f9263bSKirill A. Shutemov 11825dbe0af4SHugh Dickins /* address might be in next vma when migration races vma_adjust */ 1183cea86fe2SHugh Dickins else if (first) 1184d281ee61SKirill A. Shutemov __page_set_anon_rmap(page, vma, address, 1185d281ee61SKirill A. Shutemov flags & RMAP_EXCLUSIVE); 118669029cd5SKAMEZAWA Hiroyuki else 1187c97a9e10SNick Piggin __page_check_anon_rmap(page, vma, address); 1188cea86fe2SHugh Dickins 1189cea86fe2SHugh Dickins mlock_vma_page(page, vma, compound); 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 119243d8eac4SRandy Dunlap /** 11939617d95eSNick Piggin * page_add_new_anon_rmap - add pte mapping to a new anonymous page 11949617d95eSNick Piggin * @page: the page to add the mapping to 11959617d95eSNick Piggin * @vma: the vm area in which the mapping is added 11969617d95eSNick Piggin * @address: the user virtual address mapped 1197d281ee61SKirill A. Shutemov * @compound: charge the page as compound or small page 11989617d95eSNick Piggin * 11999617d95eSNick Piggin * Same as page_add_anon_rmap but must only be called on *new* pages. 12009617d95eSNick Piggin * This means the inc-and-test can be bypassed. 1201c97a9e10SNick Piggin * Page does not have to be locked. 12029617d95eSNick Piggin */ 12039617d95eSNick Piggin void page_add_new_anon_rmap(struct page *page, 1204d281ee61SKirill A. Shutemov struct vm_area_struct *vma, unsigned long address, bool compound) 12059617d95eSNick Piggin { 12066c357848SMatthew Wilcox (Oracle) int nr = compound ? thp_nr_pages(page) : 1; 1207d281ee61SKirill A. Shutemov 120881d1b09cSSasha Levin VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma); 1209fa9949daSHugh Dickins __SetPageSwapBacked(page); 1210d281ee61SKirill A. Shutemov if (compound) { 1211d281ee61SKirill A. Shutemov VM_BUG_ON_PAGE(!PageTransHuge(page), page); 121253f9263bSKirill A. Shutemov /* increment count (starts at -1) */ 121353f9263bSKirill A. Shutemov atomic_set(compound_mapcount_ptr(page), 0); 121447e29d32SJohn Hubbard atomic_set(compound_pincount_ptr(page), 0); 121547e29d32SJohn Hubbard 121669473e5dSMuchun Song __mod_lruvec_page_state(page, NR_ANON_THPS, nr); 121753f9263bSKirill A. Shutemov } else { 121853f9263bSKirill A. Shutemov /* Anon THP always mapped first with PMD */ 121953f9263bSKirill A. Shutemov VM_BUG_ON_PAGE(PageTransCompound(page), page); 122053f9263bSKirill A. Shutemov /* increment count (starts at -1) */ 122153f9263bSKirill A. Shutemov atomic_set(&page->_mapcount, 0); 1222d281ee61SKirill A. Shutemov } 1223be5d0a74SJohannes Weiner __mod_lruvec_page_state(page, NR_ANON_MAPPED, nr); 1224e8a03febSRik van Riel __page_set_anon_rmap(page, vma, address, 1); 12259617d95eSNick Piggin } 12269617d95eSNick Piggin 12271da177e4SLinus Torvalds /** 12281da177e4SLinus Torvalds * page_add_file_rmap - add pte mapping to a file page 12291da177e4SLinus Torvalds * @page: the page to add the mapping to 1230cea86fe2SHugh Dickins * @vma: the vm area in which the mapping is added 1231e8b098fcSMike Rapoport * @compound: charge the page as compound or small page 12321da177e4SLinus Torvalds * 1233b8072f09SHugh Dickins * The caller needs to hold the pte lock. 12341da177e4SLinus Torvalds */ 1235cea86fe2SHugh Dickins void page_add_file_rmap(struct page *page, 1236cea86fe2SHugh Dickins struct vm_area_struct *vma, bool compound) 12371da177e4SLinus Torvalds { 1238dd78feddSKirill A. Shutemov int i, nr = 1; 1239dd78feddSKirill A. Shutemov 1240dd78feddSKirill A. Shutemov VM_BUG_ON_PAGE(compound && !PageTransHuge(page), page); 124162cccb8cSJohannes Weiner lock_page_memcg(page); 1242dd78feddSKirill A. Shutemov if (compound && PageTransHuge(page)) { 1243a1528e21SMuchun Song int nr_pages = thp_nr_pages(page); 1244a1528e21SMuchun Song 1245a1528e21SMuchun Song for (i = 0, nr = 0; i < nr_pages; i++) { 1246dd78feddSKirill A. Shutemov if (atomic_inc_and_test(&page[i]._mapcount)) 1247dd78feddSKirill A. Shutemov nr++; 1248d69b042fSBalbir Singh } 1249dd78feddSKirill A. Shutemov if (!atomic_inc_and_test(compound_mapcount_ptr(page))) 1250dd78feddSKirill A. Shutemov goto out; 125199cb0dbdSSong Liu if (PageSwapBacked(page)) 1252a1528e21SMuchun Song __mod_lruvec_page_state(page, NR_SHMEM_PMDMAPPED, 1253a1528e21SMuchun Song nr_pages); 125499cb0dbdSSong Liu else 1255380780e7SMuchun Song __mod_lruvec_page_state(page, NR_FILE_PMDMAPPED, 1256380780e7SMuchun Song nr_pages); 1257dd78feddSKirill A. Shutemov } else { 1258c8efc390SKirill A. Shutemov if (PageTransCompound(page) && page_mapping(page)) { 1259c8efc390SKirill A. Shutemov VM_WARN_ON_ONCE(!PageLocked(page)); 1260cea86fe2SHugh Dickins SetPageDoubleMap(compound_head(page)); 12619a73f61bSKirill A. Shutemov } 1262dd78feddSKirill A. Shutemov if (!atomic_inc_and_test(&page->_mapcount)) 1263dd78feddSKirill A. Shutemov goto out; 1264dd78feddSKirill A. Shutemov } 126500f3ca2cSJohannes Weiner __mod_lruvec_page_state(page, NR_FILE_MAPPED, nr); 1266dd78feddSKirill A. Shutemov out: 126762cccb8cSJohannes Weiner unlock_page_memcg(page); 1268cea86fe2SHugh Dickins 1269cea86fe2SHugh Dickins mlock_vma_page(page, vma, compound); 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds 1272dd78feddSKirill A. Shutemov static void page_remove_file_rmap(struct page *page, bool compound) 12738186eb6aSJohannes Weiner { 1274dd78feddSKirill A. Shutemov int i, nr = 1; 1275dd78feddSKirill A. Shutemov 127657dea93aSSteve Capper VM_BUG_ON_PAGE(compound && !PageHead(page), page); 12778186eb6aSJohannes Weiner 127853f9263bSKirill A. Shutemov /* Hugepages are not counted in NR_FILE_MAPPED for now. */ 127953f9263bSKirill A. Shutemov if (unlikely(PageHuge(page))) { 128053f9263bSKirill A. Shutemov /* hugetlb pages are always mapped with pmds */ 128153f9263bSKirill A. Shutemov atomic_dec(compound_mapcount_ptr(page)); 1282be5d0a74SJohannes Weiner return; 128353f9263bSKirill A. Shutemov } 128453f9263bSKirill A. Shutemov 12858186eb6aSJohannes Weiner /* page still mapped by someone else? */ 1286dd78feddSKirill A. Shutemov if (compound && PageTransHuge(page)) { 1287a1528e21SMuchun Song int nr_pages = thp_nr_pages(page); 1288a1528e21SMuchun Song 1289a1528e21SMuchun Song for (i = 0, nr = 0; i < nr_pages; i++) { 1290dd78feddSKirill A. Shutemov if (atomic_add_negative(-1, &page[i]._mapcount)) 1291dd78feddSKirill A. Shutemov nr++; 1292dd78feddSKirill A. Shutemov } 1293dd78feddSKirill A. Shutemov if (!atomic_add_negative(-1, compound_mapcount_ptr(page))) 1294be5d0a74SJohannes Weiner return; 129599cb0dbdSSong Liu if (PageSwapBacked(page)) 1296a1528e21SMuchun Song __mod_lruvec_page_state(page, NR_SHMEM_PMDMAPPED, 1297a1528e21SMuchun Song -nr_pages); 129899cb0dbdSSong Liu else 1299380780e7SMuchun Song __mod_lruvec_page_state(page, NR_FILE_PMDMAPPED, 1300380780e7SMuchun Song -nr_pages); 1301dd78feddSKirill A. Shutemov } else { 13028186eb6aSJohannes Weiner if (!atomic_add_negative(-1, &page->_mapcount)) 1303be5d0a74SJohannes Weiner return; 1304dd78feddSKirill A. Shutemov } 13058186eb6aSJohannes Weiner 13068186eb6aSJohannes Weiner /* 130700f3ca2cSJohannes Weiner * We use the irq-unsafe __{inc|mod}_lruvec_page_state because 13088186eb6aSJohannes Weiner * these counters are not modified in interrupt context, and 13098186eb6aSJohannes Weiner * pte lock(a spinlock) is held, which implies preemption disabled. 13108186eb6aSJohannes Weiner */ 131100f3ca2cSJohannes Weiner __mod_lruvec_page_state(page, NR_FILE_MAPPED, -nr); 13128186eb6aSJohannes Weiner } 13138186eb6aSJohannes Weiner 131453f9263bSKirill A. Shutemov static void page_remove_anon_compound_rmap(struct page *page) 131553f9263bSKirill A. Shutemov { 131653f9263bSKirill A. Shutemov int i, nr; 131753f9263bSKirill A. Shutemov 131853f9263bSKirill A. Shutemov if (!atomic_add_negative(-1, compound_mapcount_ptr(page))) 131953f9263bSKirill A. Shutemov return; 132053f9263bSKirill A. Shutemov 132153f9263bSKirill A. Shutemov /* Hugepages are not counted in NR_ANON_PAGES for now. */ 132253f9263bSKirill A. Shutemov if (unlikely(PageHuge(page))) 132353f9263bSKirill A. Shutemov return; 132453f9263bSKirill A. Shutemov 132553f9263bSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 132653f9263bSKirill A. Shutemov return; 132753f9263bSKirill A. Shutemov 132869473e5dSMuchun Song __mod_lruvec_page_state(page, NR_ANON_THPS, -thp_nr_pages(page)); 132953f9263bSKirill A. Shutemov 133053f9263bSKirill A. Shutemov if (TestClearPageDoubleMap(page)) { 133153f9263bSKirill A. Shutemov /* 133253f9263bSKirill A. Shutemov * Subpages can be mapped with PTEs too. Check how many of 1333f1fe80d4SKirill A. Shutemov * them are still mapped. 133453f9263bSKirill A. Shutemov */ 13355eaf35abSMatthew Wilcox (Oracle) for (i = 0, nr = 0; i < thp_nr_pages(page); i++) { 133653f9263bSKirill A. Shutemov if (atomic_add_negative(-1, &page[i]._mapcount)) 133753f9263bSKirill A. Shutemov nr++; 133853f9263bSKirill A. Shutemov } 1339f1fe80d4SKirill A. Shutemov 1340f1fe80d4SKirill A. Shutemov /* 1341f1fe80d4SKirill A. Shutemov * Queue the page for deferred split if at least one small 1342f1fe80d4SKirill A. Shutemov * page of the compound page is unmapped, but at least one 1343f1fe80d4SKirill A. Shutemov * small page is still mapped. 1344f1fe80d4SKirill A. Shutemov */ 13455eaf35abSMatthew Wilcox (Oracle) if (nr && nr < thp_nr_pages(page)) 1346f1fe80d4SKirill A. Shutemov deferred_split_huge_page(page); 134753f9263bSKirill A. Shutemov } else { 13485eaf35abSMatthew Wilcox (Oracle) nr = thp_nr_pages(page); 134953f9263bSKirill A. Shutemov } 135053f9263bSKirill A. Shutemov 1351f1fe80d4SKirill A. Shutemov if (nr) 1352be5d0a74SJohannes Weiner __mod_lruvec_page_state(page, NR_ANON_MAPPED, -nr); 135353f9263bSKirill A. Shutemov } 135453f9263bSKirill A. Shutemov 13551da177e4SLinus Torvalds /** 13561da177e4SLinus Torvalds * page_remove_rmap - take down pte mapping from a page 13571da177e4SLinus Torvalds * @page: page to remove mapping from 1358cea86fe2SHugh Dickins * @vma: the vm area from which the mapping is removed 1359d281ee61SKirill A. Shutemov * @compound: uncharge the page as compound or small page 13601da177e4SLinus Torvalds * 1361b8072f09SHugh Dickins * The caller needs to hold the pte lock. 13621da177e4SLinus Torvalds */ 1363cea86fe2SHugh Dickins void page_remove_rmap(struct page *page, 1364cea86fe2SHugh Dickins struct vm_area_struct *vma, bool compound) 13651da177e4SLinus Torvalds { 1366be5d0a74SJohannes Weiner lock_page_memcg(page); 136789c06bd5SKAMEZAWA Hiroyuki 1368be5d0a74SJohannes Weiner if (!PageAnon(page)) { 1369be5d0a74SJohannes Weiner page_remove_file_rmap(page, compound); 1370be5d0a74SJohannes Weiner goto out; 1371be5d0a74SJohannes Weiner } 1372be5d0a74SJohannes Weiner 1373be5d0a74SJohannes Weiner if (compound) { 1374be5d0a74SJohannes Weiner page_remove_anon_compound_rmap(page); 1375be5d0a74SJohannes Weiner goto out; 1376be5d0a74SJohannes Weiner } 137753f9263bSKirill A. Shutemov 1378b904dcfeSKOSAKI Motohiro /* page still mapped by someone else? */ 1379b904dcfeSKOSAKI Motohiro if (!atomic_add_negative(-1, &page->_mapcount)) 1380be5d0a74SJohannes Weiner goto out; 13818186eb6aSJohannes Weiner 13821da177e4SLinus Torvalds /* 1383bea04b07SJianyu Zhan * We use the irq-unsafe __{inc|mod}_zone_page_stat because 1384bea04b07SJianyu Zhan * these counters are not modified in interrupt context, and 1385bea04b07SJianyu Zhan * pte lock(a spinlock) is held, which implies preemption disabled. 13860fe6e20bSNaoya Horiguchi */ 1387be5d0a74SJohannes Weiner __dec_lruvec_page_state(page, NR_ANON_MAPPED); 13888186eb6aSJohannes Weiner 13899a982250SKirill A. Shutemov if (PageTransCompound(page)) 13909a982250SKirill A. Shutemov deferred_split_huge_page(compound_head(page)); 13919a982250SKirill A. Shutemov 139216f8c5b2SHugh Dickins /* 13931da177e4SLinus Torvalds * It would be tidy to reset the PageAnon mapping here, 13941da177e4SLinus Torvalds * but that might overwrite a racing page_add_anon_rmap 13951da177e4SLinus Torvalds * which increments mapcount after us but sets mapping 13962d4894b5SMel Gorman * before us: so leave the reset to free_unref_page, 13971da177e4SLinus Torvalds * and remember that it's only reliable while mapped. 13981da177e4SLinus Torvalds * Leaving it set also helps swapoff to reinstate ptes 13991da177e4SLinus Torvalds * faster for those pages still in swapcache. 14001da177e4SLinus Torvalds */ 1401be5d0a74SJohannes Weiner out: 1402be5d0a74SJohannes Weiner unlock_page_memcg(page); 1403cea86fe2SHugh Dickins 1404cea86fe2SHugh Dickins munlock_vma_page(page, vma, compound); 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds /* 140852629506SJoonsoo Kim * @arg: enum ttu_flags will be passed to this argument 14091da177e4SLinus Torvalds */ 1410e4b82222SMinchan Kim static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma, 141152629506SJoonsoo Kim unsigned long address, void *arg) 14121da177e4SLinus Torvalds { 1413af28a988SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 14141da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 1415869f7ee6SMatthew Wilcox (Oracle) DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); 14161da177e4SLinus Torvalds pte_t pteval; 1417c7ab0d2fSKirill A. Shutemov struct page *subpage; 1418785373b4SLinus Torvalds bool ret = true; 1419ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 14204708f318SPalmer Dabbelt enum ttu_flags flags = (enum ttu_flags)(long)arg; 14211da177e4SLinus Torvalds 1422732ed558SHugh Dickins /* 1423732ed558SHugh Dickins * When racing against e.g. zap_pte_range() on another cpu, 1424732ed558SHugh Dickins * in between its ptep_get_and_clear_full() and page_remove_rmap(), 14251fb08ac6SYang Shi * try_to_unmap() may return before page_mapped() has become false, 1426732ed558SHugh Dickins * if page table locking is skipped: use TTU_SYNC to wait for that. 1427732ed558SHugh Dickins */ 1428732ed558SHugh Dickins if (flags & TTU_SYNC) 1429732ed558SHugh Dickins pvmw.flags = PVMW_SYNC; 1430732ed558SHugh Dickins 1431a98a2f0cSAlistair Popple if (flags & TTU_SPLIT_HUGE_PMD) 1432af28a988SMatthew Wilcox (Oracle) split_huge_pmd_address(vma, address, false, folio); 1433fec89c10SKirill A. Shutemov 1434369ea824SJérôme Glisse /* 1435017b1660SMike Kravetz * For THP, we have to assume the worse case ie pmd for invalidation. 1436017b1660SMike Kravetz * For hugetlb, it could be much worse if we need to do pud 1437017b1660SMike Kravetz * invalidation in the case of pmd sharing. 1438017b1660SMike Kravetz * 1439869f7ee6SMatthew Wilcox (Oracle) * Note that the folio can not be freed in this function as call of 1440869f7ee6SMatthew Wilcox (Oracle) * try_to_unmap() must hold a reference on the folio. 1441369ea824SJérôme Glisse */ 14422aff7a47SMatthew Wilcox (Oracle) range.end = vma_address_end(&pvmw); 14437269f999SJérôme Glisse mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, 1444494334e4SHugh Dickins address, range.end); 1445869f7ee6SMatthew Wilcox (Oracle) if (folio_test_hugetlb(folio)) { 1446017b1660SMike Kravetz /* 1447017b1660SMike Kravetz * If sharing is possible, start and end will be adjusted 1448017b1660SMike Kravetz * accordingly. 1449017b1660SMike Kravetz */ 1450ac46d4f3SJérôme Glisse adjust_range_if_pmd_sharing_possible(vma, &range.start, 1451ac46d4f3SJérôme Glisse &range.end); 1452017b1660SMike Kravetz } 1453ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_start(&range); 1454369ea824SJérôme Glisse 1455c7ab0d2fSKirill A. Shutemov while (page_vma_mapped_walk(&pvmw)) { 1456cea86fe2SHugh Dickins /* Unexpected PMD-mapped THP? */ 1457869f7ee6SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!pvmw.pte, folio); 1458cea86fe2SHugh Dickins 14591da177e4SLinus Torvalds /* 1460869f7ee6SMatthew Wilcox (Oracle) * If the folio is in an mlock()d vma, we must not swap it out. 14611da177e4SLinus Torvalds */ 1462efdb6720SHugh Dickins if (!(flags & TTU_IGNORE_MLOCK) && 1463efdb6720SHugh Dickins (vma->vm_flags & VM_LOCKED)) { 1464cea86fe2SHugh Dickins /* Restore the mlock which got missed */ 1465869f7ee6SMatthew Wilcox (Oracle) mlock_vma_folio(folio, vma, false); 1466c7ab0d2fSKirill A. Shutemov page_vma_mapped_walk_done(&pvmw); 1467efdb6720SHugh Dickins ret = false; 1468c7ab0d2fSKirill A. Shutemov break; 1469b87537d9SHugh Dickins } 1470c7ab0d2fSKirill A. Shutemov 1471869f7ee6SMatthew Wilcox (Oracle) subpage = folio_page(folio, 1472869f7ee6SMatthew Wilcox (Oracle) pte_pfn(*pvmw.pte) - folio_pfn(folio)); 1473785373b4SLinus Torvalds address = pvmw.address; 1474785373b4SLinus Torvalds 1475869f7ee6SMatthew Wilcox (Oracle) if (folio_test_hugetlb(folio) && !folio_test_anon(folio)) { 1476c0d0381aSMike Kravetz /* 1477c0d0381aSMike Kravetz * To call huge_pmd_unshare, i_mmap_rwsem must be 1478c0d0381aSMike Kravetz * held in write mode. Caller needs to explicitly 1479c0d0381aSMike Kravetz * do this outside rmap routines. 1480c0d0381aSMike Kravetz */ 1481c0d0381aSMike Kravetz VM_BUG_ON(!(flags & TTU_RMAP_LOCKED)); 148234ae204fSMike Kravetz if (huge_pmd_unshare(mm, vma, &address, pvmw.pte)) { 1483017b1660SMike Kravetz /* 1484017b1660SMike Kravetz * huge_pmd_unshare unmapped an entire PMD 1485017b1660SMike Kravetz * page. There is no way of knowing exactly 1486017b1660SMike Kravetz * which PMDs may be cached for this mm, so 1487017b1660SMike Kravetz * we must flush them all. start/end were 1488017b1660SMike Kravetz * already adjusted above to cover this range. 1489017b1660SMike Kravetz */ 1490ac46d4f3SJérôme Glisse flush_cache_range(vma, range.start, range.end); 1491ac46d4f3SJérôme Glisse flush_tlb_range(vma, range.start, range.end); 1492ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range(mm, range.start, 1493ac46d4f3SJérôme Glisse range.end); 1494017b1660SMike Kravetz 1495017b1660SMike Kravetz /* 1496017b1660SMike Kravetz * The ref count of the PMD page was dropped 1497017b1660SMike Kravetz * which is part of the way map counting 1498017b1660SMike Kravetz * is done for shared PMDs. Return 'true' 1499017b1660SMike Kravetz * here. When there is no other sharing, 1500017b1660SMike Kravetz * huge_pmd_unshare returns false and we will 1501017b1660SMike Kravetz * unmap the actual page and drop map count 1502017b1660SMike Kravetz * to zero. 1503017b1660SMike Kravetz */ 1504017b1660SMike Kravetz page_vma_mapped_walk_done(&pvmw); 1505017b1660SMike Kravetz break; 1506017b1660SMike Kravetz } 1507017b1660SMike Kravetz } 15088346242aSKirill A. Shutemov 15091da177e4SLinus Torvalds /* Nuke the page table entry. */ 1510785373b4SLinus Torvalds flush_cache_page(vma, address, pte_pfn(*pvmw.pte)); 151172b252aeSMel Gorman if (should_defer_flush(mm, flags)) { 151272b252aeSMel Gorman /* 1513c7ab0d2fSKirill A. Shutemov * We clear the PTE but do not flush so potentially 1514869f7ee6SMatthew Wilcox (Oracle) * a remote CPU could still be writing to the folio. 1515c7ab0d2fSKirill A. Shutemov * If the entry was previously clean then the 1516c7ab0d2fSKirill A. Shutemov * architecture must guarantee that a clear->dirty 1517c7ab0d2fSKirill A. Shutemov * transition on a cached TLB entry is written through 1518c7ab0d2fSKirill A. Shutemov * and traps if the PTE is unmapped. 151972b252aeSMel Gorman */ 1520785373b4SLinus Torvalds pteval = ptep_get_and_clear(mm, address, pvmw.pte); 152172b252aeSMel Gorman 1522c7ab0d2fSKirill A. Shutemov set_tlb_ubc_flush_pending(mm, pte_dirty(pteval)); 152372b252aeSMel Gorman } else { 1524785373b4SLinus Torvalds pteval = ptep_clear_flush(vma, address, pvmw.pte); 152572b252aeSMel Gorman } 15261da177e4SLinus Torvalds 1527869f7ee6SMatthew Wilcox (Oracle) /* Set the dirty flag on the folio now the pte is gone. */ 15281da177e4SLinus Torvalds if (pte_dirty(pteval)) 1529869f7ee6SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 15301da177e4SLinus Torvalds 1531365e9c87SHugh Dickins /* Update high watermark before we lower rss */ 1532365e9c87SHugh Dickins update_hiwater_rss(mm); 1533365e9c87SHugh Dickins 1534869f7ee6SMatthew Wilcox (Oracle) if (PageHWPoison(subpage) && !(flags & TTU_IGNORE_HWPOISON)) { 15355fd27b8eSPunit Agrawal pteval = swp_entry_to_pte(make_hwpoison_entry(subpage)); 1536869f7ee6SMatthew Wilcox (Oracle) if (folio_test_hugetlb(folio)) { 1537869f7ee6SMatthew Wilcox (Oracle) hugetlb_count_sub(folio_nr_pages(folio), mm); 1538785373b4SLinus Torvalds set_huge_swap_pte_at(mm, address, 15395fd27b8eSPunit Agrawal pvmw.pte, pteval, 15405fd27b8eSPunit Agrawal vma_mmu_pagesize(vma)); 15415d317b2bSNaoya Horiguchi } else { 1542869f7ee6SMatthew Wilcox (Oracle) dec_mm_counter(mm, mm_counter(&folio->page)); 1543785373b4SLinus Torvalds set_pte_at(mm, address, pvmw.pte, pteval); 15445f24ae58SNaoya Horiguchi } 1545c7ab0d2fSKirill A. Shutemov 1546bce73e48SChristian Borntraeger } else if (pte_unused(pteval) && !userfaultfd_armed(vma)) { 154745961722SKonstantin Weitz /* 154845961722SKonstantin Weitz * The guest indicated that the page content is of no 154945961722SKonstantin Weitz * interest anymore. Simply discard the pte, vmscan 155045961722SKonstantin Weitz * will take care of the rest. 1551bce73e48SChristian Borntraeger * A future reference will then fault in a new zero 1552bce73e48SChristian Borntraeger * page. When userfaultfd is active, we must not drop 1553bce73e48SChristian Borntraeger * this page though, as its main user (postcopy 1554bce73e48SChristian Borntraeger * migration) will not expect userfaults on already 1555bce73e48SChristian Borntraeger * copied pages. 155645961722SKonstantin Weitz */ 1557869f7ee6SMatthew Wilcox (Oracle) dec_mm_counter(mm, mm_counter(&folio->page)); 15580f10851eSJérôme Glisse /* We have to invalidate as we cleared the pte */ 15590f10851eSJérôme Glisse mmu_notifier_invalidate_range(mm, address, 15600f10851eSJérôme Glisse address + PAGE_SIZE); 1561869f7ee6SMatthew Wilcox (Oracle) } else if (folio_test_anon(folio)) { 1562c7ab0d2fSKirill A. Shutemov swp_entry_t entry = { .val = page_private(subpage) }; 1563179ef71cSCyrill Gorcunov pte_t swp_pte; 15641da177e4SLinus Torvalds /* 15651da177e4SLinus Torvalds * Store the swap location in the pte. 15661da177e4SLinus Torvalds * See handle_pte_fault() ... 15671da177e4SLinus Torvalds */ 1568869f7ee6SMatthew Wilcox (Oracle) if (unlikely(folio_test_swapbacked(folio) != 1569869f7ee6SMatthew Wilcox (Oracle) folio_test_swapcache(folio))) { 1570eb94a878SMinchan Kim WARN_ON_ONCE(1); 157183612a94SMinchan Kim ret = false; 1572369ea824SJérôme Glisse /* We have to invalidate as we cleared the pte */ 15730f10851eSJérôme Glisse mmu_notifier_invalidate_range(mm, address, 15740f10851eSJérôme Glisse address + PAGE_SIZE); 1575eb94a878SMinchan Kim page_vma_mapped_walk_done(&pvmw); 1576eb94a878SMinchan Kim break; 1577eb94a878SMinchan Kim } 1578854e9ed0SMinchan Kim 1579802a3a92SShaohua Li /* MADV_FREE page check */ 1580869f7ee6SMatthew Wilcox (Oracle) if (!folio_test_swapbacked(folio)) { 1581869f7ee6SMatthew Wilcox (Oracle) if (!folio_test_dirty(folio)) { 15820f10851eSJérôme Glisse /* Invalidate as we cleared the pte */ 15830f10851eSJérôme Glisse mmu_notifier_invalidate_range(mm, 15840f10851eSJérôme Glisse address, address + PAGE_SIZE); 1585854e9ed0SMinchan Kim dec_mm_counter(mm, MM_ANONPAGES); 1586854e9ed0SMinchan Kim goto discard; 1587854e9ed0SMinchan Kim } 1588854e9ed0SMinchan Kim 1589802a3a92SShaohua Li /* 1590869f7ee6SMatthew Wilcox (Oracle) * If the folio was redirtied, it cannot be 1591802a3a92SShaohua Li * discarded. Remap the page to page table. 1592802a3a92SShaohua Li */ 1593785373b4SLinus Torvalds set_pte_at(mm, address, pvmw.pte, pteval); 1594869f7ee6SMatthew Wilcox (Oracle) folio_set_swapbacked(folio); 1595e4b82222SMinchan Kim ret = false; 1596802a3a92SShaohua Li page_vma_mapped_walk_done(&pvmw); 1597802a3a92SShaohua Li break; 1598802a3a92SShaohua Li } 1599802a3a92SShaohua Li 1600570a335bSHugh Dickins if (swap_duplicate(entry) < 0) { 1601785373b4SLinus Torvalds set_pte_at(mm, address, pvmw.pte, pteval); 1602e4b82222SMinchan Kim ret = false; 1603c7ab0d2fSKirill A. Shutemov page_vma_mapped_walk_done(&pvmw); 1604c7ab0d2fSKirill A. Shutemov break; 1605570a335bSHugh Dickins } 1606ca827d55SKhalid Aziz if (arch_unmap_one(mm, vma, address, pteval) < 0) { 1607ca827d55SKhalid Aziz set_pte_at(mm, address, pvmw.pte, pteval); 1608ca827d55SKhalid Aziz ret = false; 1609ca827d55SKhalid Aziz page_vma_mapped_walk_done(&pvmw); 1610ca827d55SKhalid Aziz break; 1611ca827d55SKhalid Aziz } 16121da177e4SLinus Torvalds if (list_empty(&mm->mmlist)) { 16131da177e4SLinus Torvalds spin_lock(&mmlist_lock); 1614f412ac08SHugh Dickins if (list_empty(&mm->mmlist)) 16151da177e4SLinus Torvalds list_add(&mm->mmlist, &init_mm.mmlist); 16161da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 16171da177e4SLinus Torvalds } 1618d559db08SKAMEZAWA Hiroyuki dec_mm_counter(mm, MM_ANONPAGES); 1619b084d435SKAMEZAWA Hiroyuki inc_mm_counter(mm, MM_SWAPENTS); 1620179ef71cSCyrill Gorcunov swp_pte = swp_entry_to_pte(entry); 1621179ef71cSCyrill Gorcunov if (pte_soft_dirty(pteval)) 1622179ef71cSCyrill Gorcunov swp_pte = pte_swp_mksoft_dirty(swp_pte); 1623f45ec5ffSPeter Xu if (pte_uffd_wp(pteval)) 1624f45ec5ffSPeter Xu swp_pte = pte_swp_mkuffd_wp(swp_pte); 1625785373b4SLinus Torvalds set_pte_at(mm, address, pvmw.pte, swp_pte); 16260f10851eSJérôme Glisse /* Invalidate as we cleared the pte */ 1627369ea824SJérôme Glisse mmu_notifier_invalidate_range(mm, address, 1628369ea824SJérôme Glisse address + PAGE_SIZE); 16290f10851eSJérôme Glisse } else { 16300f10851eSJérôme Glisse /* 1631869f7ee6SMatthew Wilcox (Oracle) * This is a locked file-backed folio, 1632869f7ee6SMatthew Wilcox (Oracle) * so it cannot be removed from the page 1633869f7ee6SMatthew Wilcox (Oracle) * cache and replaced by a new folio before 1634869f7ee6SMatthew Wilcox (Oracle) * mmu_notifier_invalidate_range_end, so no 1635869f7ee6SMatthew Wilcox (Oracle) * concurrent thread might update its page table 1636869f7ee6SMatthew Wilcox (Oracle) * to point at a new folio while a device is 1637869f7ee6SMatthew Wilcox (Oracle) * still using this folio. 16380f10851eSJérôme Glisse * 1639ad56b738SMike Rapoport * See Documentation/vm/mmu_notifier.rst 16400f10851eSJérôme Glisse */ 1641869f7ee6SMatthew Wilcox (Oracle) dec_mm_counter(mm, mm_counter_file(&folio->page)); 16420f10851eSJérôme Glisse } 16430f10851eSJérôme Glisse discard: 16440f10851eSJérôme Glisse /* 16450f10851eSJérôme Glisse * No need to call mmu_notifier_invalidate_range() it has be 16460f10851eSJérôme Glisse * done above for all cases requiring it to happen under page 16470f10851eSJérôme Glisse * table lock before mmu_notifier_invalidate_range_end() 16480f10851eSJérôme Glisse * 1649ad56b738SMike Rapoport * See Documentation/vm/mmu_notifier.rst 16500f10851eSJérôme Glisse */ 1651869f7ee6SMatthew Wilcox (Oracle) page_remove_rmap(subpage, vma, folio_test_hugetlb(folio)); 1652b7435507SHugh Dickins if (vma->vm_flags & VM_LOCKED) 1653b7435507SHugh Dickins mlock_page_drain(smp_processor_id()); 1654869f7ee6SMatthew Wilcox (Oracle) folio_put(folio); 1655c7ab0d2fSKirill A. Shutemov } 1656369ea824SJérôme Glisse 1657ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 1658369ea824SJérôme Glisse 1659caed0f48SKOSAKI Motohiro return ret; 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds 166252629506SJoonsoo Kim static bool invalid_migration_vma(struct vm_area_struct *vma, void *arg) 166352629506SJoonsoo Kim { 1664222100eeSAnshuman Khandual return vma_is_temporary_stack(vma); 166552629506SJoonsoo Kim } 166652629506SJoonsoo Kim 1667b7e188ecSMiaohe Lin static int page_not_mapped(struct page *page) 166852629506SJoonsoo Kim { 1669b7e188ecSMiaohe Lin return !page_mapped(page); 16702a52bcbcSKirill A. Shutemov } 167152629506SJoonsoo Kim 16721da177e4SLinus Torvalds /** 1673869f7ee6SMatthew Wilcox (Oracle) * try_to_unmap - Try to remove all page table mappings to a folio. 1674869f7ee6SMatthew Wilcox (Oracle) * @folio: The folio to unmap. 167514fa31b8SAndi Kleen * @flags: action and flags 16761da177e4SLinus Torvalds * 16771da177e4SLinus Torvalds * Tries to remove all the page table entries which are mapping this 1678869f7ee6SMatthew Wilcox (Oracle) * folio. It is the caller's responsibility to check if the folio is 1679869f7ee6SMatthew Wilcox (Oracle) * still mapped if needed (use TTU_SYNC to prevent accounting races). 16801da177e4SLinus Torvalds * 1681869f7ee6SMatthew Wilcox (Oracle) * Context: Caller must hold the folio lock. 16821da177e4SLinus Torvalds */ 1683869f7ee6SMatthew Wilcox (Oracle) void try_to_unmap(struct folio *folio, enum ttu_flags flags) 16841da177e4SLinus Torvalds { 168552629506SJoonsoo Kim struct rmap_walk_control rwc = { 168652629506SJoonsoo Kim .rmap_one = try_to_unmap_one, 1687802a3a92SShaohua Li .arg = (void *)flags, 1688b7e188ecSMiaohe Lin .done = page_not_mapped, 168952629506SJoonsoo Kim .anon_lock = page_lock_anon_vma_read, 169052629506SJoonsoo Kim }; 16911da177e4SLinus Torvalds 1692a98a2f0cSAlistair Popple if (flags & TTU_RMAP_LOCKED) 1693869f7ee6SMatthew Wilcox (Oracle) rmap_walk_locked(&folio->page, &rwc); 1694a98a2f0cSAlistair Popple else 1695869f7ee6SMatthew Wilcox (Oracle) rmap_walk(&folio->page, &rwc); 1696a98a2f0cSAlistair Popple } 1697a98a2f0cSAlistair Popple 1698a98a2f0cSAlistair Popple /* 1699a98a2f0cSAlistair Popple * @arg: enum ttu_flags will be passed to this argument. 1700a98a2f0cSAlistair Popple * 1701a98a2f0cSAlistair Popple * If TTU_SPLIT_HUGE_PMD is specified any PMD mappings will be split into PTEs 170264b586d1SHugh Dickins * containing migration entries. 1703a98a2f0cSAlistair Popple */ 1704a98a2f0cSAlistair Popple static bool try_to_migrate_one(struct page *page, struct vm_area_struct *vma, 1705a98a2f0cSAlistair Popple unsigned long address, void *arg) 1706a98a2f0cSAlistair Popple { 1707af28a988SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 1708a98a2f0cSAlistair Popple struct mm_struct *mm = vma->vm_mm; 17094b8554c5SMatthew Wilcox (Oracle) DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); 1710a98a2f0cSAlistair Popple pte_t pteval; 1711a98a2f0cSAlistair Popple struct page *subpage; 1712a98a2f0cSAlistair Popple bool ret = true; 1713a98a2f0cSAlistair Popple struct mmu_notifier_range range; 1714a98a2f0cSAlistair Popple enum ttu_flags flags = (enum ttu_flags)(long)arg; 1715a98a2f0cSAlistair Popple 1716a98a2f0cSAlistair Popple /* 1717a98a2f0cSAlistair Popple * When racing against e.g. zap_pte_range() on another cpu, 1718a98a2f0cSAlistair Popple * in between its ptep_get_and_clear_full() and page_remove_rmap(), 1719a98a2f0cSAlistair Popple * try_to_migrate() may return before page_mapped() has become false, 1720a98a2f0cSAlistair Popple * if page table locking is skipped: use TTU_SYNC to wait for that. 1721a98a2f0cSAlistair Popple */ 1722a98a2f0cSAlistair Popple if (flags & TTU_SYNC) 1723a98a2f0cSAlistair Popple pvmw.flags = PVMW_SYNC; 1724a98a2f0cSAlistair Popple 1725a98a2f0cSAlistair Popple /* 1726a98a2f0cSAlistair Popple * unmap_page() in mm/huge_memory.c is the only user of migration with 1727a98a2f0cSAlistair Popple * TTU_SPLIT_HUGE_PMD and it wants to freeze. 1728a98a2f0cSAlistair Popple */ 1729a98a2f0cSAlistair Popple if (flags & TTU_SPLIT_HUGE_PMD) 1730af28a988SMatthew Wilcox (Oracle) split_huge_pmd_address(vma, address, true, folio); 1731a98a2f0cSAlistair Popple 1732a98a2f0cSAlistair Popple /* 1733a98a2f0cSAlistair Popple * For THP, we have to assume the worse case ie pmd for invalidation. 1734a98a2f0cSAlistair Popple * For hugetlb, it could be much worse if we need to do pud 1735a98a2f0cSAlistair Popple * invalidation in the case of pmd sharing. 1736a98a2f0cSAlistair Popple * 1737a98a2f0cSAlistair Popple * Note that the page can not be free in this function as call of 1738a98a2f0cSAlistair Popple * try_to_unmap() must hold a reference on the page. 1739a98a2f0cSAlistair Popple */ 17402aff7a47SMatthew Wilcox (Oracle) range.end = vma_address_end(&pvmw); 1741a98a2f0cSAlistair Popple mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, 1742a98a2f0cSAlistair Popple address, range.end); 17434b8554c5SMatthew Wilcox (Oracle) if (folio_test_hugetlb(folio)) { 1744a98a2f0cSAlistair Popple /* 1745a98a2f0cSAlistair Popple * If sharing is possible, start and end will be adjusted 1746a98a2f0cSAlistair Popple * accordingly. 1747a98a2f0cSAlistair Popple */ 1748a98a2f0cSAlistair Popple adjust_range_if_pmd_sharing_possible(vma, &range.start, 1749a98a2f0cSAlistair Popple &range.end); 1750a98a2f0cSAlistair Popple } 1751a98a2f0cSAlistair Popple mmu_notifier_invalidate_range_start(&range); 1752a98a2f0cSAlistair Popple 1753a98a2f0cSAlistair Popple while (page_vma_mapped_walk(&pvmw)) { 1754a98a2f0cSAlistair Popple #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 1755a98a2f0cSAlistair Popple /* PMD-mapped THP migration entry */ 1756a98a2f0cSAlistair Popple if (!pvmw.pte) { 17574b8554c5SMatthew Wilcox (Oracle) subpage = folio_page(folio, 17584b8554c5SMatthew Wilcox (Oracle) pmd_pfn(*pvmw.pmd) - folio_pfn(folio)); 17594b8554c5SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_hugetlb(folio) || 17604b8554c5SMatthew Wilcox (Oracle) !folio_test_pmd_mappable(folio), folio); 1761a98a2f0cSAlistair Popple 17624b8554c5SMatthew Wilcox (Oracle) set_pmd_migration_entry(&pvmw, subpage); 1763a98a2f0cSAlistair Popple continue; 1764a98a2f0cSAlistair Popple } 1765a98a2f0cSAlistair Popple #endif 1766a98a2f0cSAlistair Popple 1767a98a2f0cSAlistair Popple /* Unexpected PMD-mapped THP? */ 17684b8554c5SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!pvmw.pte, folio); 1769a98a2f0cSAlistair Popple 17704b8554c5SMatthew Wilcox (Oracle) subpage = folio_page(folio, 17714b8554c5SMatthew Wilcox (Oracle) pte_pfn(*pvmw.pte) - folio_pfn(folio)); 1772a98a2f0cSAlistair Popple address = pvmw.address; 1773a98a2f0cSAlistair Popple 17744b8554c5SMatthew Wilcox (Oracle) if (folio_test_hugetlb(folio) && !folio_test_anon(folio)) { 1775a98a2f0cSAlistair Popple /* 1776a98a2f0cSAlistair Popple * To call huge_pmd_unshare, i_mmap_rwsem must be 1777a98a2f0cSAlistair Popple * held in write mode. Caller needs to explicitly 1778a98a2f0cSAlistair Popple * do this outside rmap routines. 1779a98a2f0cSAlistair Popple */ 1780a98a2f0cSAlistair Popple VM_BUG_ON(!(flags & TTU_RMAP_LOCKED)); 1781a98a2f0cSAlistair Popple if (huge_pmd_unshare(mm, vma, &address, pvmw.pte)) { 1782a98a2f0cSAlistair Popple /* 1783a98a2f0cSAlistair Popple * huge_pmd_unshare unmapped an entire PMD 1784a98a2f0cSAlistair Popple * page. There is no way of knowing exactly 1785a98a2f0cSAlistair Popple * which PMDs may be cached for this mm, so 1786a98a2f0cSAlistair Popple * we must flush them all. start/end were 1787a98a2f0cSAlistair Popple * already adjusted above to cover this range. 1788a98a2f0cSAlistair Popple */ 1789a98a2f0cSAlistair Popple flush_cache_range(vma, range.start, range.end); 1790a98a2f0cSAlistair Popple flush_tlb_range(vma, range.start, range.end); 1791a98a2f0cSAlistair Popple mmu_notifier_invalidate_range(mm, range.start, 1792a98a2f0cSAlistair Popple range.end); 1793a98a2f0cSAlistair Popple 1794a98a2f0cSAlistair Popple /* 1795a98a2f0cSAlistair Popple * The ref count of the PMD page was dropped 1796a98a2f0cSAlistair Popple * which is part of the way map counting 1797a98a2f0cSAlistair Popple * is done for shared PMDs. Return 'true' 1798a98a2f0cSAlistair Popple * here. When there is no other sharing, 1799a98a2f0cSAlistair Popple * huge_pmd_unshare returns false and we will 1800a98a2f0cSAlistair Popple * unmap the actual page and drop map count 1801a98a2f0cSAlistair Popple * to zero. 1802a98a2f0cSAlistair Popple */ 1803a98a2f0cSAlistair Popple page_vma_mapped_walk_done(&pvmw); 1804a98a2f0cSAlistair Popple break; 1805a98a2f0cSAlistair Popple } 1806a98a2f0cSAlistair Popple } 1807a98a2f0cSAlistair Popple 1808a98a2f0cSAlistair Popple /* Nuke the page table entry. */ 1809a98a2f0cSAlistair Popple flush_cache_page(vma, address, pte_pfn(*pvmw.pte)); 1810a98a2f0cSAlistair Popple pteval = ptep_clear_flush(vma, address, pvmw.pte); 1811a98a2f0cSAlistair Popple 18124b8554c5SMatthew Wilcox (Oracle) /* Set the dirty flag on the folio now the pte is gone. */ 1813a98a2f0cSAlistair Popple if (pte_dirty(pteval)) 18144b8554c5SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 1815a98a2f0cSAlistair Popple 1816a98a2f0cSAlistair Popple /* Update high watermark before we lower rss */ 1817a98a2f0cSAlistair Popple update_hiwater_rss(mm); 1818a98a2f0cSAlistair Popple 18194b8554c5SMatthew Wilcox (Oracle) if (folio_is_zone_device(folio)) { 18204b8554c5SMatthew Wilcox (Oracle) unsigned long pfn = folio_pfn(folio); 1821a98a2f0cSAlistair Popple swp_entry_t entry; 1822a98a2f0cSAlistair Popple pte_t swp_pte; 1823a98a2f0cSAlistair Popple 1824a98a2f0cSAlistair Popple /* 1825a98a2f0cSAlistair Popple * Store the pfn of the page in a special migration 1826a98a2f0cSAlistair Popple * pte. do_swap_page() will wait until the migration 1827a98a2f0cSAlistair Popple * pte is removed and then restart fault handling. 1828a98a2f0cSAlistair Popple */ 18293d88705cSAlistair Popple entry = pte_to_swp_entry(pteval); 18303d88705cSAlistair Popple if (is_writable_device_private_entry(entry)) 18313d88705cSAlistair Popple entry = make_writable_migration_entry(pfn); 18323d88705cSAlistair Popple else 18333d88705cSAlistair Popple entry = make_readable_migration_entry(pfn); 1834a98a2f0cSAlistair Popple swp_pte = swp_entry_to_pte(entry); 1835a98a2f0cSAlistair Popple 1836a98a2f0cSAlistair Popple /* 1837a98a2f0cSAlistair Popple * pteval maps a zone device page and is therefore 1838a98a2f0cSAlistair Popple * a swap pte. 1839a98a2f0cSAlistair Popple */ 1840a98a2f0cSAlistair Popple if (pte_swp_soft_dirty(pteval)) 1841a98a2f0cSAlistair Popple swp_pte = pte_swp_mksoft_dirty(swp_pte); 1842a98a2f0cSAlistair Popple if (pte_swp_uffd_wp(pteval)) 1843a98a2f0cSAlistair Popple swp_pte = pte_swp_mkuffd_wp(swp_pte); 1844a98a2f0cSAlistair Popple set_pte_at(mm, pvmw.address, pvmw.pte, swp_pte); 1845a98a2f0cSAlistair Popple /* 1846a98a2f0cSAlistair Popple * No need to invalidate here it will synchronize on 1847a98a2f0cSAlistair Popple * against the special swap migration pte. 1848a98a2f0cSAlistair Popple * 1849a98a2f0cSAlistair Popple * The assignment to subpage above was computed from a 1850a98a2f0cSAlistair Popple * swap PTE which results in an invalid pointer. 1851a98a2f0cSAlistair Popple * Since only PAGE_SIZE pages can currently be 1852a98a2f0cSAlistair Popple * migrated, just set it to page. This will need to be 1853a98a2f0cSAlistair Popple * changed when hugepage migrations to device private 1854a98a2f0cSAlistair Popple * memory are supported. 1855a98a2f0cSAlistair Popple */ 18564b8554c5SMatthew Wilcox (Oracle) subpage = &folio->page; 18574b8554c5SMatthew Wilcox (Oracle) } else if (PageHWPoison(subpage)) { 1858a98a2f0cSAlistair Popple pteval = swp_entry_to_pte(make_hwpoison_entry(subpage)); 18594b8554c5SMatthew Wilcox (Oracle) if (folio_test_hugetlb(folio)) { 18604b8554c5SMatthew Wilcox (Oracle) hugetlb_count_sub(folio_nr_pages(folio), mm); 1861a98a2f0cSAlistair Popple set_huge_swap_pte_at(mm, address, 1862a98a2f0cSAlistair Popple pvmw.pte, pteval, 1863a98a2f0cSAlistair Popple vma_mmu_pagesize(vma)); 1864a98a2f0cSAlistair Popple } else { 18654b8554c5SMatthew Wilcox (Oracle) dec_mm_counter(mm, mm_counter(&folio->page)); 1866a98a2f0cSAlistair Popple set_pte_at(mm, address, pvmw.pte, pteval); 1867a98a2f0cSAlistair Popple } 1868a98a2f0cSAlistair Popple 1869a98a2f0cSAlistair Popple } else if (pte_unused(pteval) && !userfaultfd_armed(vma)) { 1870a98a2f0cSAlistair Popple /* 1871a98a2f0cSAlistair Popple * The guest indicated that the page content is of no 1872a98a2f0cSAlistair Popple * interest anymore. Simply discard the pte, vmscan 1873a98a2f0cSAlistair Popple * will take care of the rest. 1874a98a2f0cSAlistair Popple * A future reference will then fault in a new zero 1875a98a2f0cSAlistair Popple * page. When userfaultfd is active, we must not drop 1876a98a2f0cSAlistair Popple * this page though, as its main user (postcopy 1877a98a2f0cSAlistair Popple * migration) will not expect userfaults on already 1878a98a2f0cSAlistair Popple * copied pages. 1879a98a2f0cSAlistair Popple */ 18804b8554c5SMatthew Wilcox (Oracle) dec_mm_counter(mm, mm_counter(&folio->page)); 1881a98a2f0cSAlistair Popple /* We have to invalidate as we cleared the pte */ 1882a98a2f0cSAlistair Popple mmu_notifier_invalidate_range(mm, address, 1883a98a2f0cSAlistair Popple address + PAGE_SIZE); 1884a98a2f0cSAlistair Popple } else { 1885a98a2f0cSAlistair Popple swp_entry_t entry; 1886a98a2f0cSAlistair Popple pte_t swp_pte; 1887a98a2f0cSAlistair Popple 1888a98a2f0cSAlistair Popple if (arch_unmap_one(mm, vma, address, pteval) < 0) { 1889a98a2f0cSAlistair Popple set_pte_at(mm, address, pvmw.pte, pteval); 1890a98a2f0cSAlistair Popple ret = false; 1891a98a2f0cSAlistair Popple page_vma_mapped_walk_done(&pvmw); 1892a98a2f0cSAlistair Popple break; 1893a98a2f0cSAlistair Popple } 1894a98a2f0cSAlistair Popple 1895a98a2f0cSAlistair Popple /* 1896a98a2f0cSAlistair Popple * Store the pfn of the page in a special migration 1897a98a2f0cSAlistair Popple * pte. do_swap_page() will wait until the migration 1898a98a2f0cSAlistair Popple * pte is removed and then restart fault handling. 1899a98a2f0cSAlistair Popple */ 1900a98a2f0cSAlistair Popple if (pte_write(pteval)) 1901a98a2f0cSAlistair Popple entry = make_writable_migration_entry( 1902a98a2f0cSAlistair Popple page_to_pfn(subpage)); 1903a98a2f0cSAlistair Popple else 1904a98a2f0cSAlistair Popple entry = make_readable_migration_entry( 1905a98a2f0cSAlistair Popple page_to_pfn(subpage)); 1906a98a2f0cSAlistair Popple 1907a98a2f0cSAlistair Popple swp_pte = swp_entry_to_pte(entry); 1908a98a2f0cSAlistair Popple if (pte_soft_dirty(pteval)) 1909a98a2f0cSAlistair Popple swp_pte = pte_swp_mksoft_dirty(swp_pte); 1910a98a2f0cSAlistair Popple if (pte_uffd_wp(pteval)) 1911a98a2f0cSAlistair Popple swp_pte = pte_swp_mkuffd_wp(swp_pte); 1912a98a2f0cSAlistair Popple set_pte_at(mm, address, pvmw.pte, swp_pte); 1913a98a2f0cSAlistair Popple /* 1914a98a2f0cSAlistair Popple * No need to invalidate here it will synchronize on 1915a98a2f0cSAlistair Popple * against the special swap migration pte. 1916a98a2f0cSAlistair Popple */ 1917a98a2f0cSAlistair Popple } 1918a98a2f0cSAlistair Popple 1919a98a2f0cSAlistair Popple /* 1920a98a2f0cSAlistair Popple * No need to call mmu_notifier_invalidate_range() it has be 1921a98a2f0cSAlistair Popple * done above for all cases requiring it to happen under page 1922a98a2f0cSAlistair Popple * table lock before mmu_notifier_invalidate_range_end() 1923a98a2f0cSAlistair Popple * 1924a98a2f0cSAlistair Popple * See Documentation/vm/mmu_notifier.rst 1925a98a2f0cSAlistair Popple */ 19264b8554c5SMatthew Wilcox (Oracle) page_remove_rmap(subpage, vma, folio_test_hugetlb(folio)); 1927b7435507SHugh Dickins if (vma->vm_flags & VM_LOCKED) 1928b7435507SHugh Dickins mlock_page_drain(smp_processor_id()); 19294b8554c5SMatthew Wilcox (Oracle) folio_put(folio); 1930a98a2f0cSAlistair Popple } 1931a98a2f0cSAlistair Popple 1932a98a2f0cSAlistair Popple mmu_notifier_invalidate_range_end(&range); 1933a98a2f0cSAlistair Popple 1934a98a2f0cSAlistair Popple return ret; 1935a98a2f0cSAlistair Popple } 1936a98a2f0cSAlistair Popple 1937a98a2f0cSAlistair Popple /** 1938a98a2f0cSAlistair Popple * try_to_migrate - try to replace all page table mappings with swap entries 19394b8554c5SMatthew Wilcox (Oracle) * @folio: the folio to replace page table entries for 1940a98a2f0cSAlistair Popple * @flags: action and flags 1941a98a2f0cSAlistair Popple * 19424b8554c5SMatthew Wilcox (Oracle) * Tries to remove all the page table entries which are mapping this folio and 19434b8554c5SMatthew Wilcox (Oracle) * replace them with special swap entries. Caller must hold the folio lock. 1944a98a2f0cSAlistair Popple */ 19454b8554c5SMatthew Wilcox (Oracle) void try_to_migrate(struct folio *folio, enum ttu_flags flags) 1946a98a2f0cSAlistair Popple { 1947a98a2f0cSAlistair Popple struct rmap_walk_control rwc = { 1948a98a2f0cSAlistair Popple .rmap_one = try_to_migrate_one, 1949a98a2f0cSAlistair Popple .arg = (void *)flags, 1950a98a2f0cSAlistair Popple .done = page_not_mapped, 1951a98a2f0cSAlistair Popple .anon_lock = page_lock_anon_vma_read, 1952a98a2f0cSAlistair Popple }; 1953a98a2f0cSAlistair Popple 1954a98a2f0cSAlistair Popple /* 1955a98a2f0cSAlistair Popple * Migration always ignores mlock and only supports TTU_RMAP_LOCKED and 1956a98a2f0cSAlistair Popple * TTU_SPLIT_HUGE_PMD and TTU_SYNC flags. 1957a98a2f0cSAlistair Popple */ 1958a98a2f0cSAlistair Popple if (WARN_ON_ONCE(flags & ~(TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD | 1959a98a2f0cSAlistair Popple TTU_SYNC))) 1960a98a2f0cSAlistair Popple return; 1961a98a2f0cSAlistair Popple 19624b8554c5SMatthew Wilcox (Oracle) if (folio_is_zone_device(folio) && !folio_is_device_private(folio)) 19636c855fceSHugh Dickins return; 19646c855fceSHugh Dickins 196552629506SJoonsoo Kim /* 196652629506SJoonsoo Kim * During exec, a temporary VMA is setup and later moved. 196752629506SJoonsoo Kim * The VMA is moved under the anon_vma lock but not the 196852629506SJoonsoo Kim * page tables leading to a race where migration cannot 196952629506SJoonsoo Kim * find the migration ptes. Rather than increasing the 197052629506SJoonsoo Kim * locking requirements of exec(), migration skips 197152629506SJoonsoo Kim * temporary VMAs until after exec() completes. 197252629506SJoonsoo Kim */ 19734b8554c5SMatthew Wilcox (Oracle) if (!folio_test_ksm(folio) && folio_test_anon(folio)) 197452629506SJoonsoo Kim rwc.invalid_vma = invalid_migration_vma; 197552629506SJoonsoo Kim 19762a52bcbcSKirill A. Shutemov if (flags & TTU_RMAP_LOCKED) 19774b8554c5SMatthew Wilcox (Oracle) rmap_walk_locked(&folio->page, &rwc); 19782a52bcbcSKirill A. Shutemov else 19794b8554c5SMatthew Wilcox (Oracle) rmap_walk(&folio->page, &rwc); 19801da177e4SLinus Torvalds } 198181b4082dSNikita Danilov 1982b756a3b5SAlistair Popple #ifdef CONFIG_DEVICE_PRIVATE 1983b756a3b5SAlistair Popple struct make_exclusive_args { 1984b756a3b5SAlistair Popple struct mm_struct *mm; 1985b756a3b5SAlistair Popple unsigned long address; 1986b756a3b5SAlistair Popple void *owner; 1987b756a3b5SAlistair Popple bool valid; 1988b756a3b5SAlistair Popple }; 1989b756a3b5SAlistair Popple 1990b756a3b5SAlistair Popple static bool page_make_device_exclusive_one(struct page *page, 1991b756a3b5SAlistair Popple struct vm_area_struct *vma, unsigned long address, void *priv) 1992b756a3b5SAlistair Popple { 19930d251485SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 1994b756a3b5SAlistair Popple struct mm_struct *mm = vma->vm_mm; 19950d251485SMatthew Wilcox (Oracle) DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); 1996b756a3b5SAlistair Popple struct make_exclusive_args *args = priv; 1997b756a3b5SAlistair Popple pte_t pteval; 1998b756a3b5SAlistair Popple struct page *subpage; 1999b756a3b5SAlistair Popple bool ret = true; 2000b756a3b5SAlistair Popple struct mmu_notifier_range range; 2001b756a3b5SAlistair Popple swp_entry_t entry; 2002b756a3b5SAlistair Popple pte_t swp_pte; 2003b756a3b5SAlistair Popple 2004b756a3b5SAlistair Popple mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, vma, 2005b756a3b5SAlistair Popple vma->vm_mm, address, min(vma->vm_end, 20060d251485SMatthew Wilcox (Oracle) address + folio_size(folio)), 20070d251485SMatthew Wilcox (Oracle) args->owner); 2008b756a3b5SAlistair Popple mmu_notifier_invalidate_range_start(&range); 2009b756a3b5SAlistair Popple 2010b756a3b5SAlistair Popple while (page_vma_mapped_walk(&pvmw)) { 2011b756a3b5SAlistair Popple /* Unexpected PMD-mapped THP? */ 20120d251485SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!pvmw.pte, folio); 2013b756a3b5SAlistair Popple 2014b756a3b5SAlistair Popple if (!pte_present(*pvmw.pte)) { 2015b756a3b5SAlistair Popple ret = false; 2016b756a3b5SAlistair Popple page_vma_mapped_walk_done(&pvmw); 2017b756a3b5SAlistair Popple break; 2018b756a3b5SAlistair Popple } 2019b756a3b5SAlistair Popple 20200d251485SMatthew Wilcox (Oracle) subpage = folio_page(folio, 20210d251485SMatthew Wilcox (Oracle) pte_pfn(*pvmw.pte) - folio_pfn(folio)); 2022b756a3b5SAlistair Popple address = pvmw.address; 2023b756a3b5SAlistair Popple 2024b756a3b5SAlistair Popple /* Nuke the page table entry. */ 2025b756a3b5SAlistair Popple flush_cache_page(vma, address, pte_pfn(*pvmw.pte)); 2026b756a3b5SAlistair Popple pteval = ptep_clear_flush(vma, address, pvmw.pte); 2027b756a3b5SAlistair Popple 20280d251485SMatthew Wilcox (Oracle) /* Set the dirty flag on the folio now the pte is gone. */ 2029b756a3b5SAlistair Popple if (pte_dirty(pteval)) 20300d251485SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 2031b756a3b5SAlistair Popple 2032b756a3b5SAlistair Popple /* 2033b756a3b5SAlistair Popple * Check that our target page is still mapped at the expected 2034b756a3b5SAlistair Popple * address. 2035b756a3b5SAlistair Popple */ 2036b756a3b5SAlistair Popple if (args->mm == mm && args->address == address && 2037b756a3b5SAlistair Popple pte_write(pteval)) 2038b756a3b5SAlistair Popple args->valid = true; 2039b756a3b5SAlistair Popple 2040b756a3b5SAlistair Popple /* 2041b756a3b5SAlistair Popple * Store the pfn of the page in a special migration 2042b756a3b5SAlistair Popple * pte. do_swap_page() will wait until the migration 2043b756a3b5SAlistair Popple * pte is removed and then restart fault handling. 2044b756a3b5SAlistair Popple */ 2045b756a3b5SAlistair Popple if (pte_write(pteval)) 2046b756a3b5SAlistair Popple entry = make_writable_device_exclusive_entry( 2047b756a3b5SAlistair Popple page_to_pfn(subpage)); 2048b756a3b5SAlistair Popple else 2049b756a3b5SAlistair Popple entry = make_readable_device_exclusive_entry( 2050b756a3b5SAlistair Popple page_to_pfn(subpage)); 2051b756a3b5SAlistair Popple swp_pte = swp_entry_to_pte(entry); 2052b756a3b5SAlistair Popple if (pte_soft_dirty(pteval)) 2053b756a3b5SAlistair Popple swp_pte = pte_swp_mksoft_dirty(swp_pte); 2054b756a3b5SAlistair Popple if (pte_uffd_wp(pteval)) 2055b756a3b5SAlistair Popple swp_pte = pte_swp_mkuffd_wp(swp_pte); 2056b756a3b5SAlistair Popple 2057b756a3b5SAlistair Popple set_pte_at(mm, address, pvmw.pte, swp_pte); 2058b756a3b5SAlistair Popple 2059b756a3b5SAlistair Popple /* 2060b756a3b5SAlistair Popple * There is a reference on the page for the swap entry which has 2061b756a3b5SAlistair Popple * been removed, so shouldn't take another. 2062b756a3b5SAlistair Popple */ 2063cea86fe2SHugh Dickins page_remove_rmap(subpage, vma, false); 2064b756a3b5SAlistair Popple } 2065b756a3b5SAlistair Popple 2066b756a3b5SAlistair Popple mmu_notifier_invalidate_range_end(&range); 2067b756a3b5SAlistair Popple 2068b756a3b5SAlistair Popple return ret; 2069b756a3b5SAlistair Popple } 2070b756a3b5SAlistair Popple 2071b756a3b5SAlistair Popple /** 20720d251485SMatthew Wilcox (Oracle) * folio_make_device_exclusive - Mark the folio exclusively owned by a device. 20730d251485SMatthew Wilcox (Oracle) * @folio: The folio to replace page table entries for. 20740d251485SMatthew Wilcox (Oracle) * @mm: The mm_struct where the folio is expected to be mapped. 20750d251485SMatthew Wilcox (Oracle) * @address: Address where the folio is expected to be mapped. 2076b756a3b5SAlistair Popple * @owner: passed to MMU_NOTIFY_EXCLUSIVE range notifier callbacks 2077b756a3b5SAlistair Popple * 20780d251485SMatthew Wilcox (Oracle) * Tries to remove all the page table entries which are mapping this 20790d251485SMatthew Wilcox (Oracle) * folio and replace them with special device exclusive swap entries to 20800d251485SMatthew Wilcox (Oracle) * grant a device exclusive access to the folio. 2081b756a3b5SAlistair Popple * 20820d251485SMatthew Wilcox (Oracle) * Context: Caller must hold the folio lock. 20830d251485SMatthew Wilcox (Oracle) * Return: false if the page is still mapped, or if it could not be unmapped 2084b756a3b5SAlistair Popple * from the expected address. Otherwise returns true (success). 2085b756a3b5SAlistair Popple */ 20860d251485SMatthew Wilcox (Oracle) static bool folio_make_device_exclusive(struct folio *folio, 20870d251485SMatthew Wilcox (Oracle) struct mm_struct *mm, unsigned long address, void *owner) 2088b756a3b5SAlistair Popple { 2089b756a3b5SAlistair Popple struct make_exclusive_args args = { 2090b756a3b5SAlistair Popple .mm = mm, 2091b756a3b5SAlistair Popple .address = address, 2092b756a3b5SAlistair Popple .owner = owner, 2093b756a3b5SAlistair Popple .valid = false, 2094b756a3b5SAlistair Popple }; 2095b756a3b5SAlistair Popple struct rmap_walk_control rwc = { 2096b756a3b5SAlistair Popple .rmap_one = page_make_device_exclusive_one, 2097b756a3b5SAlistair Popple .done = page_not_mapped, 2098b756a3b5SAlistair Popple .anon_lock = page_lock_anon_vma_read, 2099b756a3b5SAlistair Popple .arg = &args, 2100b756a3b5SAlistair Popple }; 2101b756a3b5SAlistair Popple 2102b756a3b5SAlistair Popple /* 21030d251485SMatthew Wilcox (Oracle) * Restrict to anonymous folios for now to avoid potential writeback 21040d251485SMatthew Wilcox (Oracle) * issues. 2105b756a3b5SAlistair Popple */ 21060d251485SMatthew Wilcox (Oracle) if (!folio_test_anon(folio)) 2107b756a3b5SAlistair Popple return false; 2108b756a3b5SAlistair Popple 21090d251485SMatthew Wilcox (Oracle) rmap_walk(&folio->page, &rwc); 2110b756a3b5SAlistair Popple 21110d251485SMatthew Wilcox (Oracle) return args.valid && !folio_mapcount(folio); 2112b756a3b5SAlistair Popple } 2113b756a3b5SAlistair Popple 2114b756a3b5SAlistair Popple /** 2115b756a3b5SAlistair Popple * make_device_exclusive_range() - Mark a range for exclusive use by a device 2116b756a3b5SAlistair Popple * @mm: mm_struct of assoicated target process 2117b756a3b5SAlistair Popple * @start: start of the region to mark for exclusive device access 2118b756a3b5SAlistair Popple * @end: end address of region 2119b756a3b5SAlistair Popple * @pages: returns the pages which were successfully marked for exclusive access 2120b756a3b5SAlistair Popple * @owner: passed to MMU_NOTIFY_EXCLUSIVE range notifier to allow filtering 2121b756a3b5SAlistair Popple * 2122b756a3b5SAlistair Popple * Returns: number of pages found in the range by GUP. A page is marked for 2123b756a3b5SAlistair Popple * exclusive access only if the page pointer is non-NULL. 2124b756a3b5SAlistair Popple * 2125b756a3b5SAlistair Popple * This function finds ptes mapping page(s) to the given address range, locks 2126b756a3b5SAlistair Popple * them and replaces mappings with special swap entries preventing userspace CPU 2127b756a3b5SAlistair Popple * access. On fault these entries are replaced with the original mapping after 2128b756a3b5SAlistair Popple * calling MMU notifiers. 2129b756a3b5SAlistair Popple * 2130b756a3b5SAlistair Popple * A driver using this to program access from a device must use a mmu notifier 2131b756a3b5SAlistair Popple * critical section to hold a device specific lock during programming. Once 2132b756a3b5SAlistair Popple * programming is complete it should drop the page lock and reference after 2133b756a3b5SAlistair Popple * which point CPU access to the page will revoke the exclusive access. 2134b756a3b5SAlistair Popple */ 2135b756a3b5SAlistair Popple int make_device_exclusive_range(struct mm_struct *mm, unsigned long start, 2136b756a3b5SAlistair Popple unsigned long end, struct page **pages, 2137b756a3b5SAlistair Popple void *owner) 2138b756a3b5SAlistair Popple { 2139b756a3b5SAlistair Popple long npages = (end - start) >> PAGE_SHIFT; 2140b756a3b5SAlistair Popple long i; 2141b756a3b5SAlistair Popple 2142b756a3b5SAlistair Popple npages = get_user_pages_remote(mm, start, npages, 2143b756a3b5SAlistair Popple FOLL_GET | FOLL_WRITE | FOLL_SPLIT_PMD, 2144b756a3b5SAlistair Popple pages, NULL, NULL); 2145b756a3b5SAlistair Popple if (npages < 0) 2146b756a3b5SAlistair Popple return npages; 2147b756a3b5SAlistair Popple 2148b756a3b5SAlistair Popple for (i = 0; i < npages; i++, start += PAGE_SIZE) { 21490d251485SMatthew Wilcox (Oracle) struct folio *folio = page_folio(pages[i]); 21500d251485SMatthew Wilcox (Oracle) if (PageTail(pages[i]) || !folio_trylock(folio)) { 21510d251485SMatthew Wilcox (Oracle) folio_put(folio); 2152b756a3b5SAlistair Popple pages[i] = NULL; 2153b756a3b5SAlistair Popple continue; 2154b756a3b5SAlistair Popple } 2155b756a3b5SAlistair Popple 21560d251485SMatthew Wilcox (Oracle) if (!folio_make_device_exclusive(folio, mm, start, owner)) { 21570d251485SMatthew Wilcox (Oracle) folio_unlock(folio); 21580d251485SMatthew Wilcox (Oracle) folio_put(folio); 2159b756a3b5SAlistair Popple pages[i] = NULL; 2160b756a3b5SAlistair Popple } 2161b756a3b5SAlistair Popple } 2162b756a3b5SAlistair Popple 2163b756a3b5SAlistair Popple return npages; 2164b756a3b5SAlistair Popple } 2165b756a3b5SAlistair Popple EXPORT_SYMBOL_GPL(make_device_exclusive_range); 2166b756a3b5SAlistair Popple #endif 2167b756a3b5SAlistair Popple 216801d8b20dSPeter Zijlstra void __put_anon_vma(struct anon_vma *anon_vma) 216976545066SRik van Riel { 217076545066SRik van Riel struct anon_vma *root = anon_vma->root; 217176545066SRik van Riel 2172624483f3SAndrey Ryabinin anon_vma_free(anon_vma); 217301d8b20dSPeter Zijlstra if (root != anon_vma && atomic_dec_and_test(&root->refcount)) 217476545066SRik van Riel anon_vma_free(root); 217576545066SRik van Riel } 217676545066SRik van Riel 21770dd1c7bbSJoonsoo Kim static struct anon_vma *rmap_walk_anon_lock(struct page *page, 21780dd1c7bbSJoonsoo Kim struct rmap_walk_control *rwc) 2179faecd8ddSJoonsoo Kim { 2180faecd8ddSJoonsoo Kim struct anon_vma *anon_vma; 2181faecd8ddSJoonsoo Kim 21820dd1c7bbSJoonsoo Kim if (rwc->anon_lock) 21830dd1c7bbSJoonsoo Kim return rwc->anon_lock(page); 21840dd1c7bbSJoonsoo Kim 2185faecd8ddSJoonsoo Kim /* 2186faecd8ddSJoonsoo Kim * Note: remove_migration_ptes() cannot use page_lock_anon_vma_read() 2187faecd8ddSJoonsoo Kim * because that depends on page_mapped(); but not all its usages 2188c1e8d7c6SMichel Lespinasse * are holding mmap_lock. Users without mmap_lock are required to 2189faecd8ddSJoonsoo Kim * take a reference count to prevent the anon_vma disappearing 2190faecd8ddSJoonsoo Kim */ 2191faecd8ddSJoonsoo Kim anon_vma = page_anon_vma(page); 2192faecd8ddSJoonsoo Kim if (!anon_vma) 2193faecd8ddSJoonsoo Kim return NULL; 2194faecd8ddSJoonsoo Kim 2195faecd8ddSJoonsoo Kim anon_vma_lock_read(anon_vma); 2196faecd8ddSJoonsoo Kim return anon_vma; 2197faecd8ddSJoonsoo Kim } 2198faecd8ddSJoonsoo Kim 2199e9995ef9SHugh Dickins /* 2200e8351ac9SJoonsoo Kim * rmap_walk_anon - do something to anonymous page using the object-based 2201e8351ac9SJoonsoo Kim * rmap method 2202e8351ac9SJoonsoo Kim * @page: the page to be handled 2203e8351ac9SJoonsoo Kim * @rwc: control variable according to each walk type 2204e8351ac9SJoonsoo Kim * 2205e8351ac9SJoonsoo Kim * Find all the mappings of a page using the mapping pointer and the vma chains 2206e8351ac9SJoonsoo Kim * contained in the anon_vma struct it points to. 2207e9995ef9SHugh Dickins */ 22081df631aeSMinchan Kim static void rmap_walk_anon(struct page *page, struct rmap_walk_control *rwc, 2209b9773199SKirill A. Shutemov bool locked) 2210e9995ef9SHugh Dickins { 2211e9995ef9SHugh Dickins struct anon_vma *anon_vma; 2212a8fa41adSKirill A. Shutemov pgoff_t pgoff_start, pgoff_end; 22135beb4930SRik van Riel struct anon_vma_chain *avc; 2214e9995ef9SHugh Dickins 2215b9773199SKirill A. Shutemov if (locked) { 2216b9773199SKirill A. Shutemov anon_vma = page_anon_vma(page); 2217b9773199SKirill A. Shutemov /* anon_vma disappear under us? */ 2218b9773199SKirill A. Shutemov VM_BUG_ON_PAGE(!anon_vma, page); 2219b9773199SKirill A. Shutemov } else { 22200dd1c7bbSJoonsoo Kim anon_vma = rmap_walk_anon_lock(page, rwc); 2221b9773199SKirill A. Shutemov } 2222e9995ef9SHugh Dickins if (!anon_vma) 22231df631aeSMinchan Kim return; 2224faecd8ddSJoonsoo Kim 2225a8fa41adSKirill A. Shutemov pgoff_start = page_to_pgoff(page); 22266c357848SMatthew Wilcox (Oracle) pgoff_end = pgoff_start + thp_nr_pages(page) - 1; 2227a8fa41adSKirill A. Shutemov anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, 2228a8fa41adSKirill A. Shutemov pgoff_start, pgoff_end) { 22295beb4930SRik van Riel struct vm_area_struct *vma = avc->vma; 2230e9995ef9SHugh Dickins unsigned long address = vma_address(page, vma); 22310dd1c7bbSJoonsoo Kim 2232494334e4SHugh Dickins VM_BUG_ON_VMA(address == -EFAULT, vma); 2233ad12695fSAndrea Arcangeli cond_resched(); 2234ad12695fSAndrea Arcangeli 22350dd1c7bbSJoonsoo Kim if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg)) 22360dd1c7bbSJoonsoo Kim continue; 22370dd1c7bbSJoonsoo Kim 2238e4b82222SMinchan Kim if (!rwc->rmap_one(page, vma, address, rwc->arg)) 2239e9995ef9SHugh Dickins break; 22400dd1c7bbSJoonsoo Kim if (rwc->done && rwc->done(page)) 22410dd1c7bbSJoonsoo Kim break; 2242e9995ef9SHugh Dickins } 2243b9773199SKirill A. Shutemov 2244b9773199SKirill A. Shutemov if (!locked) 22454fc3f1d6SIngo Molnar anon_vma_unlock_read(anon_vma); 2246e9995ef9SHugh Dickins } 2247e9995ef9SHugh Dickins 2248e8351ac9SJoonsoo Kim /* 2249e8351ac9SJoonsoo Kim * rmap_walk_file - do something to file page using the object-based rmap method 2250e8351ac9SJoonsoo Kim * @page: the page to be handled 2251e8351ac9SJoonsoo Kim * @rwc: control variable according to each walk type 2252e8351ac9SJoonsoo Kim * 2253e8351ac9SJoonsoo Kim * Find all the mappings of a page using the mapping pointer and the vma chains 2254e8351ac9SJoonsoo Kim * contained in the address_space struct it points to. 2255e8351ac9SJoonsoo Kim */ 22561df631aeSMinchan Kim static void rmap_walk_file(struct page *page, struct rmap_walk_control *rwc, 2257b9773199SKirill A. Shutemov bool locked) 2258e9995ef9SHugh Dickins { 2259b9773199SKirill A. Shutemov struct address_space *mapping = page_mapping(page); 2260a8fa41adSKirill A. Shutemov pgoff_t pgoff_start, pgoff_end; 2261e9995ef9SHugh Dickins struct vm_area_struct *vma; 2262e9995ef9SHugh Dickins 22639f32624bSJoonsoo Kim /* 22649f32624bSJoonsoo Kim * The page lock not only makes sure that page->mapping cannot 22659f32624bSJoonsoo Kim * suddenly be NULLified by truncation, it makes sure that the 22669f32624bSJoonsoo Kim * structure at mapping cannot be freed and reused yet, 2267c8c06efaSDavidlohr Bueso * so we can safely take mapping->i_mmap_rwsem. 22689f32624bSJoonsoo Kim */ 226981d1b09cSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 22709f32624bSJoonsoo Kim 2271e9995ef9SHugh Dickins if (!mapping) 22721df631aeSMinchan Kim return; 22733dec0ba0SDavidlohr Bueso 2274a8fa41adSKirill A. Shutemov pgoff_start = page_to_pgoff(page); 22756c357848SMatthew Wilcox (Oracle) pgoff_end = pgoff_start + thp_nr_pages(page) - 1; 2276b9773199SKirill A. Shutemov if (!locked) 22773dec0ba0SDavidlohr Bueso i_mmap_lock_read(mapping); 2278a8fa41adSKirill A. Shutemov vma_interval_tree_foreach(vma, &mapping->i_mmap, 2279a8fa41adSKirill A. Shutemov pgoff_start, pgoff_end) { 2280e9995ef9SHugh Dickins unsigned long address = vma_address(page, vma); 22810dd1c7bbSJoonsoo Kim 2282494334e4SHugh Dickins VM_BUG_ON_VMA(address == -EFAULT, vma); 2283ad12695fSAndrea Arcangeli cond_resched(); 2284ad12695fSAndrea Arcangeli 22850dd1c7bbSJoonsoo Kim if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg)) 22860dd1c7bbSJoonsoo Kim continue; 22870dd1c7bbSJoonsoo Kim 2288e4b82222SMinchan Kim if (!rwc->rmap_one(page, vma, address, rwc->arg)) 22890dd1c7bbSJoonsoo Kim goto done; 22900dd1c7bbSJoonsoo Kim if (rwc->done && rwc->done(page)) 22910dd1c7bbSJoonsoo Kim goto done; 2292e9995ef9SHugh Dickins } 22930dd1c7bbSJoonsoo Kim 22940dd1c7bbSJoonsoo Kim done: 2295b9773199SKirill A. Shutemov if (!locked) 22963dec0ba0SDavidlohr Bueso i_mmap_unlock_read(mapping); 2297e9995ef9SHugh Dickins } 2298e9995ef9SHugh Dickins 22991df631aeSMinchan Kim void rmap_walk(struct page *page, struct rmap_walk_control *rwc) 2300e9995ef9SHugh Dickins { 2301e9995ef9SHugh Dickins if (unlikely(PageKsm(page))) 23021df631aeSMinchan Kim rmap_walk_ksm(page, rwc); 2303e9995ef9SHugh Dickins else if (PageAnon(page)) 23041df631aeSMinchan Kim rmap_walk_anon(page, rwc, false); 2305e9995ef9SHugh Dickins else 23061df631aeSMinchan Kim rmap_walk_file(page, rwc, false); 2307b9773199SKirill A. Shutemov } 2308b9773199SKirill A. Shutemov 2309b9773199SKirill A. Shutemov /* Like rmap_walk, but caller holds relevant rmap lock */ 23101df631aeSMinchan Kim void rmap_walk_locked(struct page *page, struct rmap_walk_control *rwc) 2311b9773199SKirill A. Shutemov { 2312b9773199SKirill A. Shutemov /* no ksm support for now */ 2313b9773199SKirill A. Shutemov VM_BUG_ON_PAGE(PageKsm(page), page); 2314b9773199SKirill A. Shutemov if (PageAnon(page)) 23151df631aeSMinchan Kim rmap_walk_anon(page, rwc, true); 2316b9773199SKirill A. Shutemov else 23171df631aeSMinchan Kim rmap_walk_file(page, rwc, true); 2318e9995ef9SHugh Dickins } 23190fe6e20bSNaoya Horiguchi 2320e3390f67SNaoya Horiguchi #ifdef CONFIG_HUGETLB_PAGE 23210fe6e20bSNaoya Horiguchi /* 2322451b9514SKirill Tkhai * The following two functions are for anonymous (private mapped) hugepages. 23230fe6e20bSNaoya Horiguchi * Unlike common anonymous pages, anonymous hugepages have no accounting code 23240fe6e20bSNaoya Horiguchi * and no lru code, because we handle hugepages differently from common pages. 23250fe6e20bSNaoya Horiguchi */ 23260fe6e20bSNaoya Horiguchi void hugepage_add_anon_rmap(struct page *page, 23270fe6e20bSNaoya Horiguchi struct vm_area_struct *vma, unsigned long address) 23280fe6e20bSNaoya Horiguchi { 23290fe6e20bSNaoya Horiguchi struct anon_vma *anon_vma = vma->anon_vma; 23300fe6e20bSNaoya Horiguchi int first; 2331a850ea30SNaoya Horiguchi 2332a850ea30SNaoya Horiguchi BUG_ON(!PageLocked(page)); 23330fe6e20bSNaoya Horiguchi BUG_ON(!anon_vma); 23345dbe0af4SHugh Dickins /* address might be in next vma when migration races vma_adjust */ 233553f9263bSKirill A. Shutemov first = atomic_inc_and_test(compound_mapcount_ptr(page)); 23360fe6e20bSNaoya Horiguchi if (first) 2337451b9514SKirill Tkhai __page_set_anon_rmap(page, vma, address, 0); 23380fe6e20bSNaoya Horiguchi } 23390fe6e20bSNaoya Horiguchi 23400fe6e20bSNaoya Horiguchi void hugepage_add_new_anon_rmap(struct page *page, 23410fe6e20bSNaoya Horiguchi struct vm_area_struct *vma, unsigned long address) 23420fe6e20bSNaoya Horiguchi { 23430fe6e20bSNaoya Horiguchi BUG_ON(address < vma->vm_start || address >= vma->vm_end); 234453f9263bSKirill A. Shutemov atomic_set(compound_mapcount_ptr(page), 0); 234547e29d32SJohn Hubbard atomic_set(compound_pincount_ptr(page), 0); 234647e29d32SJohn Hubbard 2347451b9514SKirill Tkhai __page_set_anon_rmap(page, vma, address, 1); 23480fe6e20bSNaoya Horiguchi } 2349e3390f67SNaoya Horiguchi #endif /* CONFIG_HUGETLB_PAGE */ 2350