Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * mm/rmap.c - physical to virtual reverse mappings |
| 3 | * |
| 4 | * Copyright 2001, Rik van Riel <riel@conectiva.com.br> |
| 5 | * Released under the General Public License (GPL). |
| 6 | * |
| 7 | * Simple, low overhead reverse mapping scheme. |
| 8 | * Please try to keep this thing as modular as possible. |
| 9 | * |
| 10 | * Provides methods for unmapping each kind of mapped page: |
| 11 | * the anon methods track anonymous pages, and |
| 12 | * the file methods track pages belonging to an inode. |
| 13 | * |
| 14 | * Original design by Rik van Riel <riel@conectiva.com.br> 2001 |
| 15 | * File methods by Dave McCracken <dmccr@us.ibm.com> 2003, 2004 |
| 16 | * Anonymous methods by Andrea Arcangeli <andrea@suse.de> 2004 |
Hugh Dickins | 98f3260 | 2009-05-21 20:33:58 +0100 | [diff] [blame] | 17 | * Contributions by Hugh Dickins 2003, 2004 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Lock ordering in mm: |
| 22 | * |
Jan Kara | 9608703 | 2021-04-12 15:50:21 +0200 | [diff] [blame] | 23 | * inode->i_rwsem (while writing or truncating, not reading or faulting) |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 24 | * mm->mmap_lock |
Jan Kara | 730633f | 2021-01-28 19:19:45 +0100 | [diff] [blame] | 25 | * mapping->invalidate_lock (in filemap_fault) |
| 26 | * page->flags PG_locked (lock_page) * (see hugetlbfs below) |
| 27 | * hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share) |
| 28 | * mapping->i_mmap_rwsem |
| 29 | * hugetlb_fault_mutex (hugetlbfs specific page fault mutex) |
| 30 | * anon_vma->rwsem |
| 31 | * mm->page_table_lock or pte_lock |
| 32 | * swap_lock (in swap_duplicate, swap_info_get) |
| 33 | * mmlist_lock (in mmput, drain_mmlist and others) |
| 34 | * mapping->private_lock (in __set_page_dirty_buffers) |
| 35 | * lock_page_memcg move_lock (in __set_page_dirty_buffers) |
| 36 | * i_pages lock (widely used) |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 37 | * lruvec->lru_lock (in folio_lruvec_lock_irq) |
Jan Kara | 730633f | 2021-01-28 19:19:45 +0100 | [diff] [blame] | 38 | * inode->i_lock (in set_page_dirty's __mark_inode_dirty) |
| 39 | * bdi.wb->list_lock (in set_page_dirty's __mark_inode_dirty) |
| 40 | * sb_lock (within inode_lock in fs/fs-writeback.c) |
| 41 | * i_pages lock (widely used, in set_page_dirty, |
| 42 | * in arch-dependent flush_dcache_mmap_lock, |
| 43 | * within bdi.wb->list_lock in __sync_single_inode) |
Andi Kleen | 6a46079 | 2009-09-16 11:50:15 +0200 | [diff] [blame] | 44 | * |
Jan Kara | 9608703 | 2021-04-12 15:50:21 +0200 | [diff] [blame] | 45 | * anon_vma->rwsem,mapping->i_mmap_rwsem (memory_failure, collect_procs_anon) |
Peter Zijlstra | 9b67932 | 2011-06-27 16:18:09 -0700 | [diff] [blame] | 46 | * ->tasklist_lock |
Andi Kleen | 6a46079 | 2009-09-16 11:50:15 +0200 | [diff] [blame] | 47 | * pte map lock |
Mike Kravetz | c0d0381 | 2020-04-01 21:11:05 -0700 | [diff] [blame] | 48 | * |
| 49 | * * hugetlbfs PageHuge() pages take locks in this order: |
| 50 | * mapping->i_mmap_rwsem |
| 51 | * hugetlb_fault_mutex (hugetlbfs specific page fault mutex) |
| 52 | * page->flags PG_locked (lock_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | */ |
| 54 | |
| 55 | #include <linux/mm.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 56 | #include <linux/sched/mm.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 57 | #include <linux/sched/task.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <linux/pagemap.h> |
| 59 | #include <linux/swap.h> |
| 60 | #include <linux/swapops.h> |
| 61 | #include <linux/slab.h> |
| 62 | #include <linux/init.h> |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 63 | #include <linux/ksm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #include <linux/rmap.h> |
| 65 | #include <linux/rcupdate.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 66 | #include <linux/export.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 67 | #include <linux/memcontrol.h> |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 68 | #include <linux/mmu_notifier.h> |
KOSAKI Motohiro | 64cdd54 | 2009-01-06 14:39:16 -0800 | [diff] [blame] | 69 | #include <linux/migrate.h> |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 70 | #include <linux/hugetlb.h> |
Ben Dooks | 444f84f | 2019-10-18 20:20:17 -0700 | [diff] [blame] | 71 | #include <linux/huge_mm.h> |
Jan Kara | ef5d437 | 2012-10-25 13:37:31 -0700 | [diff] [blame] | 72 | #include <linux/backing-dev.h> |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 73 | #include <linux/page_idle.h> |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 74 | #include <linux/memremap.h> |
Christian Borntraeger | bce73e4 | 2018-07-13 16:58:52 -0700 | [diff] [blame] | 75 | #include <linux/userfaultfd_k.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | #include <asm/tlbflush.h> |
| 78 | |
Mel Gorman | 72b252a | 2015-09-04 15:47:32 -0700 | [diff] [blame] | 79 | #include <trace/events/tlb.h> |
| 80 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 81 | #include "internal.h" |
| 82 | |
Adrian Bunk | fdd2e5f | 2008-10-18 20:28:38 -0700 | [diff] [blame] | 83 | static struct kmem_cache *anon_vma_cachep; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 84 | static struct kmem_cache *anon_vma_chain_cachep; |
Adrian Bunk | fdd2e5f | 2008-10-18 20:28:38 -0700 | [diff] [blame] | 85 | |
| 86 | static inline struct anon_vma *anon_vma_alloc(void) |
| 87 | { |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 88 | struct anon_vma *anon_vma; |
| 89 | |
| 90 | anon_vma = kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL); |
| 91 | if (anon_vma) { |
| 92 | atomic_set(&anon_vma->refcount, 1); |
Konstantin Khlebnikov | 7a3ef20 | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 93 | anon_vma->degree = 1; /* Reference for first vma */ |
| 94 | anon_vma->parent = anon_vma; |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 95 | /* |
| 96 | * Initialise the anon_vma root to point to itself. If called |
| 97 | * from fork, the root will be reset to the parents anon_vma. |
| 98 | */ |
| 99 | anon_vma->root = anon_vma; |
| 100 | } |
| 101 | |
| 102 | return anon_vma; |
Adrian Bunk | fdd2e5f | 2008-10-18 20:28:38 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 105 | static inline void anon_vma_free(struct anon_vma *anon_vma) |
Adrian Bunk | fdd2e5f | 2008-10-18 20:28:38 -0700 | [diff] [blame] | 106 | { |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 107 | VM_BUG_ON(atomic_read(&anon_vma->refcount)); |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 108 | |
| 109 | /* |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 110 | * Synchronize against page_lock_anon_vma_read() such that |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 111 | * we can safely hold the lock without the anon_vma getting |
| 112 | * freed. |
| 113 | * |
| 114 | * Relies on the full mb implied by the atomic_dec_and_test() from |
| 115 | * put_anon_vma() against the acquire barrier implied by |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 116 | * down_read_trylock() from page_lock_anon_vma_read(). This orders: |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 117 | * |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 118 | * page_lock_anon_vma_read() VS put_anon_vma() |
| 119 | * down_read_trylock() atomic_dec_and_test() |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 120 | * LOCK MB |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 121 | * atomic_read() rwsem_is_locked() |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 122 | * |
| 123 | * LOCK should suffice since the actual taking of the lock must |
| 124 | * happen _before_ what follows. |
| 125 | */ |
Hugh Dickins | 7f39dda | 2014-06-04 16:05:33 -0700 | [diff] [blame] | 126 | might_sleep(); |
Ingo Molnar | 5a50508 | 2012-12-02 19:56:46 +0000 | [diff] [blame] | 127 | if (rwsem_is_locked(&anon_vma->root->rwsem)) { |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 128 | anon_vma_lock_write(anon_vma); |
Konstantin Khlebnikov | 08b5270 | 2013-02-22 16:34:40 -0800 | [diff] [blame] | 129 | anon_vma_unlock_write(anon_vma); |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Adrian Bunk | fdd2e5f | 2008-10-18 20:28:38 -0700 | [diff] [blame] | 132 | kmem_cache_free(anon_vma_cachep, anon_vma); |
| 133 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Linus Torvalds | dd34739 | 2011-06-17 19:05:36 -0700 | [diff] [blame] | 135 | static inline struct anon_vma_chain *anon_vma_chain_alloc(gfp_t gfp) |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 136 | { |
Linus Torvalds | dd34739 | 2011-06-17 19:05:36 -0700 | [diff] [blame] | 137 | return kmem_cache_alloc(anon_vma_chain_cachep, gfp); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Namhyung Kim | e574b5f | 2010-10-26 14:22:02 -0700 | [diff] [blame] | 140 | static void anon_vma_chain_free(struct anon_vma_chain *anon_vma_chain) |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 141 | { |
| 142 | kmem_cache_free(anon_vma_chain_cachep, anon_vma_chain); |
| 143 | } |
| 144 | |
Kautuk Consul | 6583a84 | 2012-03-21 16:34:01 -0700 | [diff] [blame] | 145 | static void anon_vma_chain_link(struct vm_area_struct *vma, |
| 146 | struct anon_vma_chain *avc, |
| 147 | struct anon_vma *anon_vma) |
| 148 | { |
| 149 | avc->vma = vma; |
| 150 | avc->anon_vma = anon_vma; |
| 151 | list_add(&avc->same_vma, &vma->anon_vma_chain); |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 152 | anon_vma_interval_tree_insert(avc, &anon_vma->rb_root); |
Kautuk Consul | 6583a84 | 2012-03-21 16:34:01 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Linus Torvalds | d9d332e | 2008-10-19 10:32:20 -0700 | [diff] [blame] | 155 | /** |
Vlastimil Babka | d5a187d | 2016-12-12 16:44:38 -0800 | [diff] [blame] | 156 | * __anon_vma_prepare - attach an anon_vma to a memory region |
Linus Torvalds | d9d332e | 2008-10-19 10:32:20 -0700 | [diff] [blame] | 157 | * @vma: the memory region in question |
| 158 | * |
| 159 | * This makes sure the memory mapping described by 'vma' has |
| 160 | * an 'anon_vma' attached to it, so that we can associate the |
| 161 | * anonymous pages mapped into it with that anon_vma. |
| 162 | * |
Vlastimil Babka | d5a187d | 2016-12-12 16:44:38 -0800 | [diff] [blame] | 163 | * The common case will be that we already have one, which |
| 164 | * is handled inline by anon_vma_prepare(). But if |
Figo.zhang | 23a0790 | 2010-12-27 15:14:06 +0100 | [diff] [blame] | 165 | * not we either need to find an adjacent mapping that we |
Linus Torvalds | d9d332e | 2008-10-19 10:32:20 -0700 | [diff] [blame] | 166 | * can re-use the anon_vma from (very common when the only |
| 167 | * reason for splitting a vma has been mprotect()), or we |
| 168 | * allocate a new one. |
| 169 | * |
| 170 | * Anon-vma allocations are very subtle, because we may have |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 171 | * optimistically looked up an anon_vma in page_lock_anon_vma_read() |
Miaohe Lin | aaf1f99 | 2021-02-25 17:17:53 -0800 | [diff] [blame] | 172 | * and that may actually touch the rwsem even in the newly |
Linus Torvalds | d9d332e | 2008-10-19 10:32:20 -0700 | [diff] [blame] | 173 | * allocated vma (it depends on RCU to make sure that the |
| 174 | * anon_vma isn't actually destroyed). |
| 175 | * |
| 176 | * As a result, we need to do proper anon_vma locking even |
| 177 | * for the new allocation. At the same time, we do not want |
| 178 | * to do any locking for the common case of already having |
| 179 | * an anon_vma. |
| 180 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 181 | * This must be called with the mmap_lock held for reading. |
Linus Torvalds | d9d332e | 2008-10-19 10:32:20 -0700 | [diff] [blame] | 182 | */ |
Vlastimil Babka | d5a187d | 2016-12-12 16:44:38 -0800 | [diff] [blame] | 183 | int __anon_vma_prepare(struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Vlastimil Babka | d5a187d | 2016-12-12 16:44:38 -0800 | [diff] [blame] | 185 | struct mm_struct *mm = vma->vm_mm; |
| 186 | struct anon_vma *anon_vma, *allocated; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 187 | struct anon_vma_chain *avc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | might_sleep(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Vlastimil Babka | d5a187d | 2016-12-12 16:44:38 -0800 | [diff] [blame] | 191 | avc = anon_vma_chain_alloc(GFP_KERNEL); |
| 192 | if (!avc) |
| 193 | goto out_enomem; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 194 | |
Vlastimil Babka | d5a187d | 2016-12-12 16:44:38 -0800 | [diff] [blame] | 195 | anon_vma = find_mergeable_anon_vma(vma); |
| 196 | allocated = NULL; |
| 197 | if (!anon_vma) { |
| 198 | anon_vma = anon_vma_alloc(); |
| 199 | if (unlikely(!anon_vma)) |
| 200 | goto out_enomem_free_avc; |
| 201 | allocated = anon_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
Vlastimil Babka | d5a187d | 2016-12-12 16:44:38 -0800 | [diff] [blame] | 203 | |
| 204 | anon_vma_lock_write(anon_vma); |
| 205 | /* page_table_lock to protect against threads */ |
| 206 | spin_lock(&mm->page_table_lock); |
| 207 | if (likely(!vma->anon_vma)) { |
| 208 | vma->anon_vma = anon_vma; |
| 209 | anon_vma_chain_link(vma, avc, anon_vma); |
| 210 | /* vma reference or self-parent link for new root */ |
| 211 | anon_vma->degree++; |
| 212 | allocated = NULL; |
| 213 | avc = NULL; |
| 214 | } |
| 215 | spin_unlock(&mm->page_table_lock); |
| 216 | anon_vma_unlock_write(anon_vma); |
| 217 | |
| 218 | if (unlikely(allocated)) |
| 219 | put_anon_vma(allocated); |
| 220 | if (unlikely(avc)) |
| 221 | anon_vma_chain_free(avc); |
| 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | return 0; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 224 | |
| 225 | out_enomem_free_avc: |
| 226 | anon_vma_chain_free(avc); |
| 227 | out_enomem: |
| 228 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 231 | /* |
| 232 | * This is a useful helper function for locking the anon_vma root as |
| 233 | * we traverse the vma->anon_vma_chain, looping over anon_vma's that |
| 234 | * have the same vma. |
| 235 | * |
| 236 | * Such anon_vma's should have the same root, so you'd expect to see |
| 237 | * just a single mutex_lock for the whole traversal. |
| 238 | */ |
| 239 | static inline struct anon_vma *lock_anon_vma_root(struct anon_vma *root, struct anon_vma *anon_vma) |
| 240 | { |
| 241 | struct anon_vma *new_root = anon_vma->root; |
| 242 | if (new_root != root) { |
| 243 | if (WARN_ON_ONCE(root)) |
Ingo Molnar | 5a50508 | 2012-12-02 19:56:46 +0000 | [diff] [blame] | 244 | up_write(&root->rwsem); |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 245 | root = new_root; |
Ingo Molnar | 5a50508 | 2012-12-02 19:56:46 +0000 | [diff] [blame] | 246 | down_write(&root->rwsem); |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 247 | } |
| 248 | return root; |
| 249 | } |
| 250 | |
| 251 | static inline void unlock_anon_vma_root(struct anon_vma *root) |
| 252 | { |
| 253 | if (root) |
Ingo Molnar | 5a50508 | 2012-12-02 19:56:46 +0000 | [diff] [blame] | 254 | up_write(&root->rwsem); |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 257 | /* |
| 258 | * Attach the anon_vmas from src to dst. |
| 259 | * Returns 0 on success, -ENOMEM on failure. |
Konstantin Khlebnikov | 7a3ef20 | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 260 | * |
Shijie Luo | cb152a1 | 2021-05-06 18:05:51 -0700 | [diff] [blame] | 261 | * anon_vma_clone() is called by __vma_adjust(), __split_vma(), copy_vma() and |
Wei Yang | 47b390d | 2019-11-30 17:50:56 -0800 | [diff] [blame] | 262 | * anon_vma_fork(). The first three want an exact copy of src, while the last |
| 263 | * one, anon_vma_fork(), may try to reuse an existing anon_vma to prevent |
| 264 | * endless growth of anon_vma. Since dst->anon_vma is set to NULL before call, |
| 265 | * we can identify this case by checking (!dst->anon_vma && src->anon_vma). |
| 266 | * |
| 267 | * If (!dst->anon_vma && src->anon_vma) is true, this function tries to find |
| 268 | * and reuse existing anon_vma which has no vmas and only one child anon_vma. |
| 269 | * This prevents degradation of anon_vma hierarchy to endless linear chain in |
| 270 | * case of constantly forking task. On the other hand, an anon_vma with more |
| 271 | * than one child isn't reused even if there was no alive vma, thus rmap |
| 272 | * walker has a good chance of avoiding scanning the whole hierarchy when it |
| 273 | * searches where page is mapped. |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 274 | */ |
| 275 | int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 277 | struct anon_vma_chain *avc, *pavc; |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 278 | struct anon_vma *root = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Linus Torvalds | 646d87b | 2010-04-11 17:15:03 -0700 | [diff] [blame] | 280 | list_for_each_entry_reverse(pavc, &src->anon_vma_chain, same_vma) { |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 281 | struct anon_vma *anon_vma; |
| 282 | |
Linus Torvalds | dd34739 | 2011-06-17 19:05:36 -0700 | [diff] [blame] | 283 | avc = anon_vma_chain_alloc(GFP_NOWAIT | __GFP_NOWARN); |
| 284 | if (unlikely(!avc)) { |
| 285 | unlock_anon_vma_root(root); |
| 286 | root = NULL; |
| 287 | avc = anon_vma_chain_alloc(GFP_KERNEL); |
| 288 | if (!avc) |
| 289 | goto enomem_failure; |
| 290 | } |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 291 | anon_vma = pavc->anon_vma; |
| 292 | root = lock_anon_vma_root(root, anon_vma); |
| 293 | anon_vma_chain_link(dst, avc, anon_vma); |
Konstantin Khlebnikov | 7a3ef20 | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 294 | |
| 295 | /* |
| 296 | * Reuse existing anon_vma if its degree lower than two, |
| 297 | * that means it has no vma and only one anon_vma child. |
| 298 | * |
| 299 | * Do not chose parent anon_vma, otherwise first child |
| 300 | * will always reuse it. Root anon_vma is never reused: |
| 301 | * it has self-parent reference and at least one child. |
| 302 | */ |
Wei Yang | 47b390d | 2019-11-30 17:50:56 -0800 | [diff] [blame] | 303 | if (!dst->anon_vma && src->anon_vma && |
| 304 | anon_vma != src->anon_vma && anon_vma->degree < 2) |
Konstantin Khlebnikov | 7a3ef20 | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 305 | dst->anon_vma = anon_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
Konstantin Khlebnikov | 7a3ef20 | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 307 | if (dst->anon_vma) |
| 308 | dst->anon_vma->degree++; |
Linus Torvalds | bb4aa39 | 2011-06-16 20:44:51 -0700 | [diff] [blame] | 309 | unlock_anon_vma_root(root); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 310 | return 0; |
| 311 | |
| 312 | enomem_failure: |
Leon Yu | 3fe89b3 | 2015-03-25 15:55:11 -0700 | [diff] [blame] | 313 | /* |
| 314 | * dst->anon_vma is dropped here otherwise its degree can be incorrectly |
| 315 | * decremented in unlink_anon_vmas(). |
| 316 | * We can safely do this because callers of anon_vma_clone() don't care |
| 317 | * about dst->anon_vma if anon_vma_clone() failed. |
| 318 | */ |
| 319 | dst->anon_vma = NULL; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 320 | unlink_anon_vmas(dst); |
| 321 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 324 | /* |
| 325 | * Attach vma to its own anon_vma, as well as to the anon_vmas that |
| 326 | * the corresponding VMA in the parent process is attached to. |
| 327 | * Returns 0 on success, non-zero on failure. |
| 328 | */ |
| 329 | int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | { |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 331 | struct anon_vma_chain *avc; |
| 332 | struct anon_vma *anon_vma; |
Daniel Forrest | c4ea95d | 2014-12-02 15:59:42 -0800 | [diff] [blame] | 333 | int error; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 334 | |
| 335 | /* Don't bother if the parent process has no anon_vma here. */ |
| 336 | if (!pvma->anon_vma) |
| 337 | return 0; |
| 338 | |
Konstantin Khlebnikov | 7a3ef20 | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 339 | /* Drop inherited anon_vma, we'll reuse existing or allocate new. */ |
| 340 | vma->anon_vma = NULL; |
| 341 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 342 | /* |
| 343 | * First, attach the new VMA to the parent VMA's anon_vmas, |
| 344 | * so rmap can find non-COWed pages in child processes. |
| 345 | */ |
Daniel Forrest | c4ea95d | 2014-12-02 15:59:42 -0800 | [diff] [blame] | 346 | error = anon_vma_clone(vma, pvma); |
| 347 | if (error) |
| 348 | return error; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 349 | |
Konstantin Khlebnikov | 7a3ef20 | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 350 | /* An existing anon_vma has been reused, all done then. */ |
| 351 | if (vma->anon_vma) |
| 352 | return 0; |
| 353 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 354 | /* Then add our own anon_vma. */ |
| 355 | anon_vma = anon_vma_alloc(); |
| 356 | if (!anon_vma) |
| 357 | goto out_error; |
Linus Torvalds | dd34739 | 2011-06-17 19:05:36 -0700 | [diff] [blame] | 358 | avc = anon_vma_chain_alloc(GFP_KERNEL); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 359 | if (!avc) |
| 360 | goto out_error_free_anon_vma; |
Rik van Riel | 5c341ee1 | 2010-08-09 17:18:39 -0700 | [diff] [blame] | 361 | |
| 362 | /* |
Miaohe Lin | aaf1f99 | 2021-02-25 17:17:53 -0800 | [diff] [blame] | 363 | * The root anon_vma's rwsem is the lock actually used when we |
Rik van Riel | 5c341ee1 | 2010-08-09 17:18:39 -0700 | [diff] [blame] | 364 | * lock any of the anon_vmas in this anon_vma tree. |
| 365 | */ |
| 366 | anon_vma->root = pvma->anon_vma->root; |
Konstantin Khlebnikov | 7a3ef20 | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 367 | anon_vma->parent = pvma->anon_vma; |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 368 | /* |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 369 | * With refcounts, an anon_vma can stay around longer than the |
| 370 | * process it belongs to. The root anon_vma needs to be pinned until |
| 371 | * this anon_vma is freed, because the lock lives in the root. |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 372 | */ |
| 373 | get_anon_vma(anon_vma->root); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 374 | /* Mark this anon_vma as the one where our new (COWed) pages go. */ |
| 375 | vma->anon_vma = anon_vma; |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 376 | anon_vma_lock_write(anon_vma); |
Rik van Riel | 5c341ee1 | 2010-08-09 17:18:39 -0700 | [diff] [blame] | 377 | anon_vma_chain_link(vma, avc, anon_vma); |
Konstantin Khlebnikov | 7a3ef20 | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 378 | anon_vma->parent->degree++; |
Konstantin Khlebnikov | 08b5270 | 2013-02-22 16:34:40 -0800 | [diff] [blame] | 379 | anon_vma_unlock_write(anon_vma); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 380 | |
| 381 | return 0; |
| 382 | |
| 383 | out_error_free_anon_vma: |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 384 | put_anon_vma(anon_vma); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 385 | out_error: |
Rik van Riel | 4946d54 | 2010-04-05 12:13:33 -0400 | [diff] [blame] | 386 | unlink_anon_vmas(vma); |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 387 | return -ENOMEM; |
| 388 | } |
| 389 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 390 | void unlink_anon_vmas(struct vm_area_struct *vma) |
| 391 | { |
| 392 | struct anon_vma_chain *avc, *next; |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 393 | struct anon_vma *root = NULL; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 394 | |
Rik van Riel | 5c341ee1 | 2010-08-09 17:18:39 -0700 | [diff] [blame] | 395 | /* |
| 396 | * Unlink each anon_vma chained to the VMA. This list is ordered |
| 397 | * from newest to oldest, ensuring the root anon_vma gets freed last. |
| 398 | */ |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 399 | list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) { |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 400 | struct anon_vma *anon_vma = avc->anon_vma; |
| 401 | |
| 402 | root = lock_anon_vma_root(root, anon_vma); |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 403 | anon_vma_interval_tree_remove(avc, &anon_vma->rb_root); |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 404 | |
| 405 | /* |
| 406 | * Leave empty anon_vmas on the list - we'll need |
| 407 | * to free them outside the lock. |
| 408 | */ |
Davidlohr Bueso | f808c13 | 2017-09-08 16:15:08 -0700 | [diff] [blame] | 409 | if (RB_EMPTY_ROOT(&anon_vma->rb_root.rb_root)) { |
Konstantin Khlebnikov | 7a3ef20 | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 410 | anon_vma->parent->degree--; |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 411 | continue; |
Konstantin Khlebnikov | 7a3ef20 | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 412 | } |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 413 | |
| 414 | list_del(&avc->same_vma); |
| 415 | anon_vma_chain_free(avc); |
| 416 | } |
Li Xinhai | ee8ab19 | 2021-02-24 12:04:49 -0800 | [diff] [blame] | 417 | if (vma->anon_vma) { |
Konstantin Khlebnikov | 7a3ef20 | 2015-01-08 14:32:15 -0800 | [diff] [blame] | 418 | vma->anon_vma->degree--; |
Li Xinhai | ee8ab19 | 2021-02-24 12:04:49 -0800 | [diff] [blame] | 419 | |
| 420 | /* |
| 421 | * vma would still be needed after unlink, and anon_vma will be prepared |
| 422 | * when handle fault. |
| 423 | */ |
| 424 | vma->anon_vma = NULL; |
| 425 | } |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 426 | unlock_anon_vma_root(root); |
| 427 | |
| 428 | /* |
| 429 | * Iterate the list once more, it now only contains empty and unlinked |
| 430 | * anon_vmas, destroy them. Could not do before due to __put_anon_vma() |
Ingo Molnar | 5a50508 | 2012-12-02 19:56:46 +0000 | [diff] [blame] | 431 | * needing to write-acquire the anon_vma->root->rwsem. |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 432 | */ |
| 433 | list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) { |
| 434 | struct anon_vma *anon_vma = avc->anon_vma; |
| 435 | |
Konstantin Khlebnikov | e4c5800 | 2016-05-19 17:11:46 -0700 | [diff] [blame] | 436 | VM_WARN_ON(anon_vma->degree); |
Peter Zijlstra | eee2acb | 2011-06-17 13:54:23 +0200 | [diff] [blame] | 437 | put_anon_vma(anon_vma); |
| 438 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 439 | list_del(&avc->same_vma); |
| 440 | anon_vma_chain_free(avc); |
| 441 | } |
| 442 | } |
| 443 | |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 444 | static void anon_vma_ctor(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 446 | struct anon_vma *anon_vma = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Ingo Molnar | 5a50508 | 2012-12-02 19:56:46 +0000 | [diff] [blame] | 448 | init_rwsem(&anon_vma->rwsem); |
Peter Zijlstra | 8381326 | 2011-03-22 16:32:48 -0700 | [diff] [blame] | 449 | atomic_set(&anon_vma->refcount, 0); |
Davidlohr Bueso | f808c13 | 2017-09-08 16:15:08 -0700 | [diff] [blame] | 450 | anon_vma->rb_root = RB_ROOT_CACHED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | void __init anon_vma_init(void) |
| 454 | { |
| 455 | anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma), |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 456 | 0, SLAB_TYPESAFE_BY_RCU|SLAB_PANIC|SLAB_ACCOUNT, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 457 | anon_vma_ctor); |
| 458 | anon_vma_chain_cachep = KMEM_CACHE(anon_vma_chain, |
| 459 | SLAB_PANIC|SLAB_ACCOUNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* |
Peter Zijlstra | 6111e4c | 2011-05-24 17:12:08 -0700 | [diff] [blame] | 463 | * Getting a lock on a stable anon_vma from a page off the LRU is tricky! |
| 464 | * |
| 465 | * Since there is no serialization what so ever against page_remove_rmap() |
Miaohe Lin | ad8a20c | 2021-02-25 17:18:06 -0800 | [diff] [blame] | 466 | * the best this function can do is return a refcount increased anon_vma |
| 467 | * that might have been relevant to this page. |
Peter Zijlstra | 6111e4c | 2011-05-24 17:12:08 -0700 | [diff] [blame] | 468 | * |
| 469 | * The page might have been remapped to a different anon_vma or the anon_vma |
| 470 | * returned may already be freed (and even reused). |
| 471 | * |
Peter Zijlstra | bc658c9 | 2011-05-29 10:33:44 +0200 | [diff] [blame] | 472 | * In case it was remapped to a different anon_vma, the new anon_vma will be a |
| 473 | * child of the old anon_vma, and the anon_vma lifetime rules will therefore |
| 474 | * ensure that any anon_vma obtained from the page will still be valid for as |
| 475 | * long as we observe page_mapped() [ hence all those page_mapped() tests ]. |
| 476 | * |
Peter Zijlstra | 6111e4c | 2011-05-24 17:12:08 -0700 | [diff] [blame] | 477 | * All users of this function must be very careful when walking the anon_vma |
| 478 | * chain and verify that the page in question is indeed mapped in it |
| 479 | * [ something equivalent to page_mapped_in_vma() ]. |
| 480 | * |
Miles Chen | 091e429 | 2019-11-30 17:51:23 -0800 | [diff] [blame] | 481 | * Since anon_vma's slab is SLAB_TYPESAFE_BY_RCU and we know from |
| 482 | * page_remove_rmap() that the anon_vma pointer from page->mapping is valid |
| 483 | * if there is a mapcount, we can dereference the anon_vma after observing |
| 484 | * those. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | */ |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 486 | struct anon_vma *page_get_anon_vma(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 488 | struct anon_vma *anon_vma = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | unsigned long anon_mapping; |
| 490 | |
| 491 | rcu_read_lock(); |
Jason Low | 4db0c3c | 2015-04-15 16:14:08 -0700 | [diff] [blame] | 492 | anon_mapping = (unsigned long)READ_ONCE(page->mapping); |
Hugh Dickins | 3ca7b3c | 2009-12-14 17:58:57 -0800 | [diff] [blame] | 493 | if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | goto out; |
| 495 | if (!page_mapped(page)) |
| 496 | goto out; |
| 497 | |
| 498 | anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON); |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 499 | if (!atomic_inc_not_zero(&anon_vma->refcount)) { |
| 500 | anon_vma = NULL; |
| 501 | goto out; |
| 502 | } |
Hugh Dickins | f1819427 | 2010-08-25 23:12:54 -0700 | [diff] [blame] | 503 | |
| 504 | /* |
| 505 | * If this page is still mapped, then its anon_vma cannot have been |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 506 | * freed. But if it has been unmapped, we have no security against the |
| 507 | * anon_vma structure being freed and reused (for another anon_vma: |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 508 | * SLAB_TYPESAFE_BY_RCU guarantees that - so the atomic_inc_not_zero() |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 509 | * above cannot corrupt). |
Hugh Dickins | f1819427 | 2010-08-25 23:12:54 -0700 | [diff] [blame] | 510 | */ |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 511 | if (!page_mapped(page)) { |
Hugh Dickins | 7f39dda | 2014-06-04 16:05:33 -0700 | [diff] [blame] | 512 | rcu_read_unlock(); |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 513 | put_anon_vma(anon_vma); |
Hugh Dickins | 7f39dda | 2014-06-04 16:05:33 -0700 | [diff] [blame] | 514 | return NULL; |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 515 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | out: |
| 517 | rcu_read_unlock(); |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 518 | |
| 519 | return anon_vma; |
| 520 | } |
| 521 | |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 522 | /* |
| 523 | * Similar to page_get_anon_vma() except it locks the anon_vma. |
| 524 | * |
| 525 | * Its a little more complex as it tries to keep the fast path to a single |
| 526 | * atomic op -- the trylock. If we fail the trylock, we fall back to getting a |
| 527 | * reference like with page_get_anon_vma() and then block on the mutex. |
| 528 | */ |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 529 | struct anon_vma *page_lock_anon_vma_read(struct page *page) |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 530 | { |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 531 | struct anon_vma *anon_vma = NULL; |
Hugh Dickins | eee0f25 | 2011-05-28 13:20:21 -0700 | [diff] [blame] | 532 | struct anon_vma *root_anon_vma; |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 533 | unsigned long anon_mapping; |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 534 | |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 535 | rcu_read_lock(); |
Jason Low | 4db0c3c | 2015-04-15 16:14:08 -0700 | [diff] [blame] | 536 | anon_mapping = (unsigned long)READ_ONCE(page->mapping); |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 537 | if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON) |
| 538 | goto out; |
| 539 | if (!page_mapped(page)) |
| 540 | goto out; |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 541 | |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 542 | anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON); |
Jason Low | 4db0c3c | 2015-04-15 16:14:08 -0700 | [diff] [blame] | 543 | root_anon_vma = READ_ONCE(anon_vma->root); |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 544 | if (down_read_trylock(&root_anon_vma->rwsem)) { |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 545 | /* |
Hugh Dickins | eee0f25 | 2011-05-28 13:20:21 -0700 | [diff] [blame] | 546 | * If the page is still mapped, then this anon_vma is still |
| 547 | * its anon_vma, and holding the mutex ensures that it will |
Peter Zijlstra | bc658c9 | 2011-05-29 10:33:44 +0200 | [diff] [blame] | 548 | * not go away, see anon_vma_free(). |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 549 | */ |
Hugh Dickins | eee0f25 | 2011-05-28 13:20:21 -0700 | [diff] [blame] | 550 | if (!page_mapped(page)) { |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 551 | up_read(&root_anon_vma->rwsem); |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 552 | anon_vma = NULL; |
| 553 | } |
| 554 | goto out; |
| 555 | } |
| 556 | |
| 557 | /* trylock failed, we got to sleep */ |
| 558 | if (!atomic_inc_not_zero(&anon_vma->refcount)) { |
| 559 | anon_vma = NULL; |
| 560 | goto out; |
| 561 | } |
| 562 | |
| 563 | if (!page_mapped(page)) { |
Hugh Dickins | 7f39dda | 2014-06-04 16:05:33 -0700 | [diff] [blame] | 564 | rcu_read_unlock(); |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 565 | put_anon_vma(anon_vma); |
Hugh Dickins | 7f39dda | 2014-06-04 16:05:33 -0700 | [diff] [blame] | 566 | return NULL; |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* we pinned the anon_vma, its safe to sleep */ |
| 570 | rcu_read_unlock(); |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 571 | anon_vma_lock_read(anon_vma); |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 572 | |
| 573 | if (atomic_dec_and_test(&anon_vma->refcount)) { |
| 574 | /* |
| 575 | * Oops, we held the last refcount, release the lock |
| 576 | * and bail -- can't simply use put_anon_vma() because |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 577 | * we'll deadlock on the anon_vma_lock_write() recursion. |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 578 | */ |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 579 | anon_vma_unlock_read(anon_vma); |
Peter Zijlstra | 88c2208 | 2011-05-24 17:12:13 -0700 | [diff] [blame] | 580 | __put_anon_vma(anon_vma); |
| 581 | anon_vma = NULL; |
| 582 | } |
| 583 | |
| 584 | return anon_vma; |
| 585 | |
| 586 | out: |
| 587 | rcu_read_unlock(); |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 588 | return anon_vma; |
Oleg Nesterov | 34bbd70 | 2007-02-28 20:13:49 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 591 | void page_unlock_anon_vma_read(struct anon_vma *anon_vma) |
Oleg Nesterov | 34bbd70 | 2007-02-28 20:13:49 -0800 | [diff] [blame] | 592 | { |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 593 | anon_vma_unlock_read(anon_vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Mel Gorman | 72b252a | 2015-09-04 15:47:32 -0700 | [diff] [blame] | 596 | #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH |
Mel Gorman | 72b252a | 2015-09-04 15:47:32 -0700 | [diff] [blame] | 597 | /* |
| 598 | * Flush TLB entries for recently unmapped pages from remote CPUs. It is |
| 599 | * important if a PTE was dirty when it was unmapped that it's flushed |
| 600 | * before any IO is initiated on the page to prevent lost writes. Similarly, |
| 601 | * it must be flushed before freeing to prevent data leakage. |
| 602 | */ |
| 603 | void try_to_unmap_flush(void) |
| 604 | { |
| 605 | struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; |
Mel Gorman | 72b252a | 2015-09-04 15:47:32 -0700 | [diff] [blame] | 606 | |
| 607 | if (!tlb_ubc->flush_required) |
| 608 | return; |
| 609 | |
Andy Lutomirski | e73ad5f | 2017-05-22 15:30:03 -0700 | [diff] [blame] | 610 | arch_tlbbatch_flush(&tlb_ubc->arch); |
Mel Gorman | 72b252a | 2015-09-04 15:47:32 -0700 | [diff] [blame] | 611 | tlb_ubc->flush_required = false; |
Mel Gorman | d950c94 | 2015-09-04 15:47:35 -0700 | [diff] [blame] | 612 | tlb_ubc->writable = false; |
Mel Gorman | 72b252a | 2015-09-04 15:47:32 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Mel Gorman | d950c94 | 2015-09-04 15:47:35 -0700 | [diff] [blame] | 615 | /* Flush iff there are potentially writable TLB entries that can race with IO */ |
| 616 | void try_to_unmap_flush_dirty(void) |
| 617 | { |
| 618 | struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; |
| 619 | |
| 620 | if (tlb_ubc->writable) |
| 621 | try_to_unmap_flush(); |
| 622 | } |
| 623 | |
Huang Ying | 5ee2fa2 | 2022-01-14 14:09:16 -0800 | [diff] [blame] | 624 | /* |
| 625 | * Bits 0-14 of mm->tlb_flush_batched record pending generations. |
| 626 | * Bits 16-30 of mm->tlb_flush_batched bit record flushed generations. |
| 627 | */ |
| 628 | #define TLB_FLUSH_BATCH_FLUSHED_SHIFT 16 |
| 629 | #define TLB_FLUSH_BATCH_PENDING_MASK \ |
| 630 | ((1 << (TLB_FLUSH_BATCH_FLUSHED_SHIFT - 1)) - 1) |
| 631 | #define TLB_FLUSH_BATCH_PENDING_LARGE \ |
| 632 | (TLB_FLUSH_BATCH_PENDING_MASK / 2) |
| 633 | |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 634 | static void set_tlb_ubc_flush_pending(struct mm_struct *mm, bool writable) |
Mel Gorman | 72b252a | 2015-09-04 15:47:32 -0700 | [diff] [blame] | 635 | { |
| 636 | struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; |
Huang Ying | 5ee2fa2 | 2022-01-14 14:09:16 -0800 | [diff] [blame] | 637 | int batch, nbatch; |
Mel Gorman | 72b252a | 2015-09-04 15:47:32 -0700 | [diff] [blame] | 638 | |
Andy Lutomirski | e73ad5f | 2017-05-22 15:30:03 -0700 | [diff] [blame] | 639 | arch_tlbbatch_add_mm(&tlb_ubc->arch, mm); |
Mel Gorman | 72b252a | 2015-09-04 15:47:32 -0700 | [diff] [blame] | 640 | tlb_ubc->flush_required = true; |
Mel Gorman | d950c94 | 2015-09-04 15:47:35 -0700 | [diff] [blame] | 641 | |
| 642 | /* |
Mel Gorman | 3ea2771 | 2017-08-02 13:31:52 -0700 | [diff] [blame] | 643 | * Ensure compiler does not re-order the setting of tlb_flush_batched |
| 644 | * before the PTE is cleared. |
| 645 | */ |
| 646 | barrier(); |
Huang Ying | 5ee2fa2 | 2022-01-14 14:09:16 -0800 | [diff] [blame] | 647 | batch = atomic_read(&mm->tlb_flush_batched); |
| 648 | retry: |
| 649 | if ((batch & TLB_FLUSH_BATCH_PENDING_MASK) > TLB_FLUSH_BATCH_PENDING_LARGE) { |
| 650 | /* |
| 651 | * Prevent `pending' from catching up with `flushed' because of |
| 652 | * overflow. Reset `pending' and `flushed' to be 1 and 0 if |
| 653 | * `pending' becomes large. |
| 654 | */ |
| 655 | nbatch = atomic_cmpxchg(&mm->tlb_flush_batched, batch, 1); |
| 656 | if (nbatch != batch) { |
| 657 | batch = nbatch; |
| 658 | goto retry; |
| 659 | } |
| 660 | } else { |
| 661 | atomic_inc(&mm->tlb_flush_batched); |
| 662 | } |
Mel Gorman | 3ea2771 | 2017-08-02 13:31:52 -0700 | [diff] [blame] | 663 | |
| 664 | /* |
Mel Gorman | d950c94 | 2015-09-04 15:47:35 -0700 | [diff] [blame] | 665 | * If the PTE was dirty then it's best to assume it's writable. The |
| 666 | * caller must use try_to_unmap_flush_dirty() or try_to_unmap_flush() |
| 667 | * before the page is queued for IO. |
| 668 | */ |
| 669 | if (writable) |
| 670 | tlb_ubc->writable = true; |
Mel Gorman | 72b252a | 2015-09-04 15:47:32 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* |
| 674 | * Returns true if the TLB flush should be deferred to the end of a batch of |
| 675 | * unmap operations to reduce IPIs. |
| 676 | */ |
| 677 | static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags) |
| 678 | { |
| 679 | bool should_defer = false; |
| 680 | |
| 681 | if (!(flags & TTU_BATCH_FLUSH)) |
| 682 | return false; |
| 683 | |
| 684 | /* If remote CPUs need to be flushed then defer batch the flush */ |
| 685 | if (cpumask_any_but(mm_cpumask(mm), get_cpu()) < nr_cpu_ids) |
| 686 | should_defer = true; |
| 687 | put_cpu(); |
| 688 | |
| 689 | return should_defer; |
| 690 | } |
Mel Gorman | 3ea2771 | 2017-08-02 13:31:52 -0700 | [diff] [blame] | 691 | |
| 692 | /* |
| 693 | * Reclaim unmaps pages under the PTL but do not flush the TLB prior to |
| 694 | * releasing the PTL if TLB flushes are batched. It's possible for a parallel |
| 695 | * operation such as mprotect or munmap to race between reclaim unmapping |
| 696 | * the page and flushing the page. If this race occurs, it potentially allows |
| 697 | * access to data via a stale TLB entry. Tracking all mm's that have TLB |
| 698 | * batching in flight would be expensive during reclaim so instead track |
| 699 | * whether TLB batching occurred in the past and if so then do a flush here |
| 700 | * if required. This will cost one additional flush per reclaim cycle paid |
| 701 | * by the first operation at risk such as mprotect and mumap. |
| 702 | * |
| 703 | * This must be called under the PTL so that an access to tlb_flush_batched |
| 704 | * that is potentially a "reclaim vs mprotect/munmap/etc" race will synchronise |
| 705 | * via the PTL. |
| 706 | */ |
| 707 | void flush_tlb_batched_pending(struct mm_struct *mm) |
| 708 | { |
Huang Ying | 5ee2fa2 | 2022-01-14 14:09:16 -0800 | [diff] [blame] | 709 | int batch = atomic_read(&mm->tlb_flush_batched); |
| 710 | int pending = batch & TLB_FLUSH_BATCH_PENDING_MASK; |
| 711 | int flushed = batch >> TLB_FLUSH_BATCH_FLUSHED_SHIFT; |
Mel Gorman | 3ea2771 | 2017-08-02 13:31:52 -0700 | [diff] [blame] | 712 | |
Huang Ying | 5ee2fa2 | 2022-01-14 14:09:16 -0800 | [diff] [blame] | 713 | if (pending != flushed) { |
| 714 | flush_tlb_mm(mm); |
Mel Gorman | 3ea2771 | 2017-08-02 13:31:52 -0700 | [diff] [blame] | 715 | /* |
Huang Ying | 5ee2fa2 | 2022-01-14 14:09:16 -0800 | [diff] [blame] | 716 | * If the new TLB flushing is pending during flushing, leave |
| 717 | * mm->tlb_flush_batched as is, to avoid losing flushing. |
Mel Gorman | 3ea2771 | 2017-08-02 13:31:52 -0700 | [diff] [blame] | 718 | */ |
Huang Ying | 5ee2fa2 | 2022-01-14 14:09:16 -0800 | [diff] [blame] | 719 | atomic_cmpxchg(&mm->tlb_flush_batched, batch, |
| 720 | pending | (pending << TLB_FLUSH_BATCH_FLUSHED_SHIFT)); |
Mel Gorman | 3ea2771 | 2017-08-02 13:31:52 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
Mel Gorman | 72b252a | 2015-09-04 15:47:32 -0700 | [diff] [blame] | 723 | #else |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 724 | static void set_tlb_ubc_flush_pending(struct mm_struct *mm, bool writable) |
Mel Gorman | 72b252a | 2015-09-04 15:47:32 -0700 | [diff] [blame] | 725 | { |
| 726 | } |
| 727 | |
| 728 | static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags) |
| 729 | { |
| 730 | return false; |
| 731 | } |
| 732 | #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ |
| 733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | /* |
Huang Shijie | bf89c8c | 2009-10-01 15:44:04 -0700 | [diff] [blame] | 735 | * At what user virtual address is page expected in vma? |
Naoya Horiguchi | ab941e0 | 2010-05-11 14:06:55 -0700 | [diff] [blame] | 736 | * Caller should check the page is actually part of the vma. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | */ |
| 738 | unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma) |
| 739 | { |
Andrea Arcangeli | 21d0d44 | 2010-08-09 17:19:10 -0700 | [diff] [blame] | 740 | if (PageAnon(page)) { |
Hugh Dickins | 4829b90 | 2010-10-02 17:46:06 -0700 | [diff] [blame] | 741 | struct anon_vma *page__anon_vma = page_anon_vma(page); |
| 742 | /* |
| 743 | * Note: swapoff's unuse_vma() is more efficient with this |
| 744 | * check, and needs it to match anon_vma when KSM is active. |
| 745 | */ |
| 746 | if (!vma->anon_vma || !page__anon_vma || |
| 747 | vma->anon_vma->root != page__anon_vma->root) |
Andrea Arcangeli | 21d0d44 | 2010-08-09 17:19:10 -0700 | [diff] [blame] | 748 | return -EFAULT; |
Jue Wang | 3165717 | 2021-06-15 18:24:00 -0700 | [diff] [blame] | 749 | } else if (!vma->vm_file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | return -EFAULT; |
Jue Wang | 3165717 | 2021-06-15 18:24:00 -0700 | [diff] [blame] | 751 | } else if (vma->vm_file->f_mapping != compound_head(page)->mapping) { |
| 752 | return -EFAULT; |
| 753 | } |
Hugh Dickins | 494334e | 2021-06-15 18:23:56 -0700 | [diff] [blame] | 754 | |
| 755 | return vma_address(page, vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 758 | pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address) |
| 759 | { |
| 760 | pgd_t *pgd; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 761 | p4d_t *p4d; |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 762 | pud_t *pud; |
| 763 | pmd_t *pmd = NULL; |
Hugh Dickins | f72e7dc | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 764 | pmd_t pmde; |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 765 | |
| 766 | pgd = pgd_offset(mm, address); |
| 767 | if (!pgd_present(*pgd)) |
| 768 | goto out; |
| 769 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 770 | p4d = p4d_offset(pgd, address); |
| 771 | if (!p4d_present(*p4d)) |
| 772 | goto out; |
| 773 | |
| 774 | pud = pud_offset(p4d, address); |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 775 | if (!pud_present(*pud)) |
| 776 | goto out; |
| 777 | |
| 778 | pmd = pmd_offset(pud, address); |
Hugh Dickins | f72e7dc | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 779 | /* |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 780 | * Some THP functions use the sequence pmdp_huge_clear_flush(), set_pmd_at() |
Hugh Dickins | f72e7dc | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 781 | * without holding anon_vma lock for write. So when looking for a |
| 782 | * genuine pmde (in which to find pte), test present and !THP together. |
| 783 | */ |
Christian Borntraeger | e37c698 | 2014-12-07 21:41:33 +0100 | [diff] [blame] | 784 | pmde = *pmd; |
| 785 | barrier(); |
Hugh Dickins | f72e7dc | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 786 | if (!pmd_present(pmde) || pmd_trans_huge(pmde)) |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 787 | pmd = NULL; |
| 788 | out: |
| 789 | return pmd; |
| 790 | } |
| 791 | |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 792 | struct page_referenced_arg { |
| 793 | int mapcount; |
| 794 | int referenced; |
| 795 | unsigned long vm_flags; |
| 796 | struct mem_cgroup *memcg; |
| 797 | }; |
Nikita Danilov | 81b4082 | 2005-05-01 08:58:36 -0700 | [diff] [blame] | 798 | /* |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 799 | * arg: page_referenced_arg will be passed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | */ |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 801 | static bool page_referenced_one(struct page *page, struct vm_area_struct *vma, |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 802 | unsigned long address, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | { |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 804 | struct page_referenced_arg *pra = arg; |
Kirill A. Shutemov | 8eaeded | 2017-02-24 14:57:48 -0800 | [diff] [blame] | 805 | struct page_vma_mapped_walk pvmw = { |
| 806 | .page = page, |
| 807 | .vma = vma, |
| 808 | .address = address, |
| 809 | }; |
Vladimir Davydov | 8749cfe | 2016-01-15 16:54:45 -0800 | [diff] [blame] | 810 | int referenced = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | |
Kirill A. Shutemov | 8eaeded | 2017-02-24 14:57:48 -0800 | [diff] [blame] | 812 | while (page_vma_mapped_walk(&pvmw)) { |
| 813 | address = pvmw.address; |
Kirill A. Shutemov | b20ce5e | 2016-01-15 16:54:37 -0800 | [diff] [blame] | 814 | |
Kirill A. Shutemov | 8eaeded | 2017-02-24 14:57:48 -0800 | [diff] [blame] | 815 | if (vma->vm_flags & VM_LOCKED) { |
| 816 | page_vma_mapped_walk_done(&pvmw); |
| 817 | pra->vm_flags |= VM_LOCKED; |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 818 | return false; /* To break the loop */ |
Vladimir Davydov | 8749cfe | 2016-01-15 16:54:45 -0800 | [diff] [blame] | 819 | } |
Kirill A. Shutemov | 8eaeded | 2017-02-24 14:57:48 -0800 | [diff] [blame] | 820 | |
| 821 | if (pvmw.pte) { |
| 822 | if (ptep_clear_flush_young_notify(vma, address, |
| 823 | pvmw.pte)) { |
| 824 | /* |
| 825 | * Don't treat a reference through |
| 826 | * a sequentially read mapping as such. |
| 827 | * If the page has been used in another mapping, |
| 828 | * we will catch it; if this other mapping is |
| 829 | * already gone, the unmap path will have set |
| 830 | * PG_referenced or activated the page. |
| 831 | */ |
| 832 | if (likely(!(vma->vm_flags & VM_SEQ_READ))) |
| 833 | referenced++; |
| 834 | } |
| 835 | } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { |
| 836 | if (pmdp_clear_flush_young_notify(vma, address, |
| 837 | pvmw.pmd)) |
| 838 | referenced++; |
| 839 | } else { |
| 840 | /* unexpected pmd-mapped page? */ |
| 841 | WARN_ON_ONCE(1); |
| 842 | } |
| 843 | |
| 844 | pra->mapcount--; |
Kirill A. Shutemov | b20ce5e | 2016-01-15 16:54:37 -0800 | [diff] [blame] | 845 | } |
Kirill A. Shutemov | b20ce5e | 2016-01-15 16:54:37 -0800 | [diff] [blame] | 846 | |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 847 | if (referenced) |
| 848 | clear_page_idle(page); |
| 849 | if (test_and_clear_page_young(page)) |
| 850 | referenced++; |
| 851 | |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 852 | if (referenced) { |
| 853 | pra->referenced++; |
| 854 | pra->vm_flags |= vma->vm_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | } |
Oleg Nesterov | 34bbd70 | 2007-02-28 20:13:49 -0800 | [diff] [blame] | 856 | |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 857 | if (!pra->mapcount) |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 858 | return false; /* To break the loop */ |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 859 | |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 860 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 863 | static bool invalid_page_referenced_vma(struct vm_area_struct *vma, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | { |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 865 | struct page_referenced_arg *pra = arg; |
| 866 | struct mem_cgroup *memcg = pra->memcg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 868 | if (!mm_match_cgroup(vma->vm_mm, memcg)) |
| 869 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 871 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /** |
| 875 | * page_referenced - test if the page was referenced |
| 876 | * @page: the page to test |
| 877 | * @is_locked: caller holds lock on the page |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 878 | * @memcg: target memory cgroup |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 879 | * @vm_flags: collect encountered vma->vm_flags who actually referenced the page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | * |
| 881 | * Quick test_and_clear_referenced for all mappings to a page, |
| 882 | * returns the number of ptes which referenced the page. |
| 883 | */ |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 884 | int page_referenced(struct page *page, |
| 885 | int is_locked, |
Johannes Weiner | 72835c8 | 2012-01-12 17:18:32 -0800 | [diff] [blame] | 886 | struct mem_cgroup *memcg, |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 887 | unsigned long *vm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | { |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 889 | int we_locked = 0; |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 890 | struct page_referenced_arg pra = { |
Kirill A. Shutemov | b20ce5e | 2016-01-15 16:54:37 -0800 | [diff] [blame] | 891 | .mapcount = total_mapcount(page), |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 892 | .memcg = memcg, |
| 893 | }; |
| 894 | struct rmap_walk_control rwc = { |
| 895 | .rmap_one = page_referenced_one, |
| 896 | .arg = (void *)&pra, |
| 897 | .anon_lock = page_lock_anon_vma_read, |
| 898 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
Wu Fengguang | 6fe6b7e | 2009-06-16 15:33:05 -0700 | [diff] [blame] | 900 | *vm_flags = 0; |
Huang Shijie | 059d844 | 2019-05-13 17:21:07 -0700 | [diff] [blame] | 901 | if (!pra.mapcount) |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 902 | return 0; |
| 903 | |
| 904 | if (!page_rmapping(page)) |
| 905 | return 0; |
| 906 | |
| 907 | if (!is_locked && (!PageAnon(page) || PageKsm(page))) { |
| 908 | we_locked = trylock_page(page); |
| 909 | if (!we_locked) |
| 910 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | } |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 912 | |
| 913 | /* |
| 914 | * If we are reclaiming on behalf of a cgroup, skip |
| 915 | * counting on behalf of references from different |
| 916 | * cgroups |
| 917 | */ |
| 918 | if (memcg) { |
| 919 | rwc.invalid_vma = invalid_page_referenced_vma; |
| 920 | } |
| 921 | |
Minchan Kim | c24f386 | 2017-05-03 14:54:00 -0700 | [diff] [blame] | 922 | rmap_walk(page, &rwc); |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 923 | *vm_flags = pra.vm_flags; |
| 924 | |
| 925 | if (we_locked) |
| 926 | unlock_page(page); |
| 927 | |
| 928 | return pra.referenced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 931 | static bool page_mkclean_one(struct page *page, struct vm_area_struct *vma, |
Joonsoo Kim | 9853a40 | 2014-01-21 15:49:55 -0800 | [diff] [blame] | 932 | unsigned long address, void *arg) |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 933 | { |
Kirill A. Shutemov | f27176c | 2017-02-24 14:57:57 -0800 | [diff] [blame] | 934 | struct page_vma_mapped_walk pvmw = { |
| 935 | .page = page, |
| 936 | .vma = vma, |
| 937 | .address = address, |
| 938 | .flags = PVMW_SYNC, |
| 939 | }; |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 940 | struct mmu_notifier_range range; |
Joonsoo Kim | 9853a40 | 2014-01-21 15:49:55 -0800 | [diff] [blame] | 941 | int *cleaned = arg; |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 942 | |
Jérôme Glisse | 369ea82 | 2017-08-31 17:17:27 -0400 | [diff] [blame] | 943 | /* |
| 944 | * We have to assume the worse case ie pmd for invalidation. Note that |
| 945 | * the page can not be free from this function. |
| 946 | */ |
Jérôme Glisse | 7269f99 | 2019-05-13 17:20:53 -0700 | [diff] [blame] | 947 | mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE, |
| 948 | 0, vma, vma->vm_mm, address, |
Hugh Dickins | 494334e | 2021-06-15 18:23:56 -0700 | [diff] [blame] | 949 | vma_address_end(page, vma)); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 950 | mmu_notifier_invalidate_range_start(&range); |
Jérôme Glisse | 369ea82 | 2017-08-31 17:17:27 -0400 | [diff] [blame] | 951 | |
Kirill A. Shutemov | f27176c | 2017-02-24 14:57:57 -0800 | [diff] [blame] | 952 | while (page_vma_mapped_walk(&pvmw)) { |
| 953 | int ret = 0; |
Jérôme Glisse | 369ea82 | 2017-08-31 17:17:27 -0400 | [diff] [blame] | 954 | |
YueHaibing | 1f18b29 | 2019-09-23 15:34:22 -0700 | [diff] [blame] | 955 | address = pvmw.address; |
Kirill A. Shutemov | f27176c | 2017-02-24 14:57:57 -0800 | [diff] [blame] | 956 | if (pvmw.pte) { |
| 957 | pte_t entry; |
| 958 | pte_t *pte = pvmw.pte; |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 959 | |
Kirill A. Shutemov | f27176c | 2017-02-24 14:57:57 -0800 | [diff] [blame] | 960 | if (!pte_dirty(*pte) && !pte_write(*pte)) |
| 961 | continue; |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 962 | |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 963 | flush_cache_page(vma, address, pte_pfn(*pte)); |
| 964 | entry = ptep_clear_flush(vma, address, pte); |
Kirill A. Shutemov | f27176c | 2017-02-24 14:57:57 -0800 | [diff] [blame] | 965 | entry = pte_wrprotect(entry); |
| 966 | entry = pte_mkclean(entry); |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 967 | set_pte_at(vma->vm_mm, address, pte, entry); |
Kirill A. Shutemov | f27176c | 2017-02-24 14:57:57 -0800 | [diff] [blame] | 968 | ret = 1; |
| 969 | } else { |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 970 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Kirill A. Shutemov | f27176c | 2017-02-24 14:57:57 -0800 | [diff] [blame] | 971 | pmd_t *pmd = pvmw.pmd; |
| 972 | pmd_t entry; |
| 973 | |
| 974 | if (!pmd_dirty(*pmd) && !pmd_write(*pmd)) |
| 975 | continue; |
| 976 | |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 977 | flush_cache_page(vma, address, page_to_pfn(page)); |
Aneesh Kumar K.V | 024eee0 | 2019-05-13 17:19:11 -0700 | [diff] [blame] | 978 | entry = pmdp_invalidate(vma, address, pmd); |
Kirill A. Shutemov | f27176c | 2017-02-24 14:57:57 -0800 | [diff] [blame] | 979 | entry = pmd_wrprotect(entry); |
| 980 | entry = pmd_mkclean(entry); |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 981 | set_pmd_at(vma->vm_mm, address, pmd, entry); |
Kirill A. Shutemov | f27176c | 2017-02-24 14:57:57 -0800 | [diff] [blame] | 982 | ret = 1; |
| 983 | #else |
| 984 | /* unexpected pmd-mapped page? */ |
| 985 | WARN_ON_ONCE(1); |
| 986 | #endif |
| 987 | } |
| 988 | |
Jérôme Glisse | 0f10851 | 2017-11-15 17:34:07 -0800 | [diff] [blame] | 989 | /* |
| 990 | * No need to call mmu_notifier_invalidate_range() as we are |
| 991 | * downgrading page table protection not changing it to point |
| 992 | * to a new page. |
| 993 | * |
Mike Rapoport | ad56b73 | 2018-03-21 21:22:47 +0200 | [diff] [blame] | 994 | * See Documentation/vm/mmu_notifier.rst |
Jérôme Glisse | 0f10851 | 2017-11-15 17:34:07 -0800 | [diff] [blame] | 995 | */ |
| 996 | if (ret) |
Kirill A. Shutemov | f27176c | 2017-02-24 14:57:57 -0800 | [diff] [blame] | 997 | (*cleaned)++; |
Peter Zijlstra | c2fda5f | 2006-12-22 14:25:52 +0100 | [diff] [blame] | 998 | } |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 999 | |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1000 | mmu_notifier_invalidate_range_end(&range); |
Jérôme Glisse | 369ea82 | 2017-08-31 17:17:27 -0400 | [diff] [blame] | 1001 | |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 1002 | return true; |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
Joonsoo Kim | 9853a40 | 2014-01-21 15:49:55 -0800 | [diff] [blame] | 1005 | static bool invalid_mkclean_vma(struct vm_area_struct *vma, void *arg) |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1006 | { |
Joonsoo Kim | 9853a40 | 2014-01-21 15:49:55 -0800 | [diff] [blame] | 1007 | if (vma->vm_flags & VM_SHARED) |
Fengguang Wu | 871beb8 | 2014-01-23 15:53:41 -0800 | [diff] [blame] | 1008 | return false; |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1009 | |
Fengguang Wu | 871beb8 | 2014-01-23 15:53:41 -0800 | [diff] [blame] | 1010 | return true; |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Matthew Wilcox (Oracle) | d9c08e2 | 2021-02-28 16:02:57 -0500 | [diff] [blame] | 1013 | int folio_mkclean(struct folio *folio) |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1014 | { |
Joonsoo Kim | 9853a40 | 2014-01-21 15:49:55 -0800 | [diff] [blame] | 1015 | int cleaned = 0; |
| 1016 | struct address_space *mapping; |
| 1017 | struct rmap_walk_control rwc = { |
| 1018 | .arg = (void *)&cleaned, |
| 1019 | .rmap_one = page_mkclean_one, |
| 1020 | .invalid_vma = invalid_mkclean_vma, |
| 1021 | }; |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1022 | |
Matthew Wilcox (Oracle) | d9c08e2 | 2021-02-28 16:02:57 -0500 | [diff] [blame] | 1023 | BUG_ON(!folio_test_locked(folio)); |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1024 | |
Matthew Wilcox (Oracle) | d9c08e2 | 2021-02-28 16:02:57 -0500 | [diff] [blame] | 1025 | if (!folio_mapped(folio)) |
Joonsoo Kim | 9853a40 | 2014-01-21 15:49:55 -0800 | [diff] [blame] | 1026 | return 0; |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1027 | |
Matthew Wilcox (Oracle) | d9c08e2 | 2021-02-28 16:02:57 -0500 | [diff] [blame] | 1028 | mapping = folio_mapping(folio); |
Joonsoo Kim | 9853a40 | 2014-01-21 15:49:55 -0800 | [diff] [blame] | 1029 | if (!mapping) |
| 1030 | return 0; |
| 1031 | |
Matthew Wilcox (Oracle) | d9c08e2 | 2021-02-28 16:02:57 -0500 | [diff] [blame] | 1032 | rmap_walk(&folio->page, &rwc); |
Joonsoo Kim | 9853a40 | 2014-01-21 15:49:55 -0800 | [diff] [blame] | 1033 | |
| 1034 | return cleaned; |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1035 | } |
Matthew Wilcox (Oracle) | d9c08e2 | 2021-02-28 16:02:57 -0500 | [diff] [blame] | 1036 | EXPORT_SYMBOL_GPL(folio_mkclean); |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 1037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | /** |
Rik van Riel | c44b674 | 2010-03-05 13:42:09 -0800 | [diff] [blame] | 1039 | * page_move_anon_rmap - move a page to our anon_vma |
| 1040 | * @page: the page to move to our anon_vma |
| 1041 | * @vma: the vma the page belongs to |
Rik van Riel | c44b674 | 2010-03-05 13:42:09 -0800 | [diff] [blame] | 1042 | * |
| 1043 | * When a page belongs exclusively to one process after a COW event, |
| 1044 | * that page can be moved into the anon_vma that belongs to just that |
| 1045 | * process, so the rmap code will not search the parent or sibling |
| 1046 | * processes. |
| 1047 | */ |
Hugh Dickins | 5a49973 | 2016-07-14 12:07:38 -0700 | [diff] [blame] | 1048 | void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma) |
Rik van Riel | c44b674 | 2010-03-05 13:42:09 -0800 | [diff] [blame] | 1049 | { |
| 1050 | struct anon_vma *anon_vma = vma->anon_vma; |
| 1051 | |
Hugh Dickins | 5a49973 | 2016-07-14 12:07:38 -0700 | [diff] [blame] | 1052 | page = compound_head(page); |
| 1053 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1054 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
Sasha Levin | 81d1b09 | 2014-10-09 15:28:10 -0700 | [diff] [blame] | 1055 | VM_BUG_ON_VMA(!anon_vma, vma); |
Rik van Riel | c44b674 | 2010-03-05 13:42:09 -0800 | [diff] [blame] | 1056 | |
| 1057 | anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON; |
Vladimir Davydov | 414e2fb | 2015-06-24 16:56:56 -0700 | [diff] [blame] | 1058 | /* |
| 1059 | * Ensure that anon_vma and the PAGE_MAPPING_ANON bit are written |
| 1060 | * simultaneously, so a concurrent reader (eg page_referenced()'s |
| 1061 | * PageAnon()) will not see one without the other. |
| 1062 | */ |
| 1063 | WRITE_ONCE(page->mapping, (struct address_space *) anon_vma); |
Rik van Riel | c44b674 | 2010-03-05 13:42:09 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | /** |
Andi Kleen | 4e1c197 | 2010-09-22 12:43:56 +0200 | [diff] [blame] | 1067 | * __page_set_anon_rmap - set up new anonymous rmap |
Kirill Tkhai | 451b951 | 2018-12-28 00:39:31 -0800 | [diff] [blame] | 1068 | * @page: Page or Hugepage to add to rmap |
Andi Kleen | 4e1c197 | 2010-09-22 12:43:56 +0200 | [diff] [blame] | 1069 | * @vma: VM area to add page to. |
| 1070 | * @address: User virtual address of the mapping |
Rik van Riel | e8a03fe | 2010-04-14 17:59:28 -0400 | [diff] [blame] | 1071 | * @exclusive: the page is exclusively owned by the current process |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1072 | */ |
| 1073 | static void __page_set_anon_rmap(struct page *page, |
Rik van Riel | e8a03fe | 2010-04-14 17:59:28 -0400 | [diff] [blame] | 1074 | struct vm_area_struct *vma, unsigned long address, int exclusive) |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1075 | { |
Rik van Riel | e8a03fe | 2010-04-14 17:59:28 -0400 | [diff] [blame] | 1076 | struct anon_vma *anon_vma = vma->anon_vma; |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1077 | |
Rik van Riel | e8a03fe | 2010-04-14 17:59:28 -0400 | [diff] [blame] | 1078 | BUG_ON(!anon_vma); |
Linus Torvalds | ea90002 | 2010-04-12 12:44:29 -0700 | [diff] [blame] | 1079 | |
Andi Kleen | 4e1c197 | 2010-09-22 12:43:56 +0200 | [diff] [blame] | 1080 | if (PageAnon(page)) |
| 1081 | return; |
| 1082 | |
Linus Torvalds | ea90002 | 2010-04-12 12:44:29 -0700 | [diff] [blame] | 1083 | /* |
Rik van Riel | e8a03fe | 2010-04-14 17:59:28 -0400 | [diff] [blame] | 1084 | * If the page isn't exclusively mapped into this vma, |
| 1085 | * we must use the _oldest_ possible anon_vma for the |
| 1086 | * page mapping! |
Linus Torvalds | ea90002 | 2010-04-12 12:44:29 -0700 | [diff] [blame] | 1087 | */ |
Andi Kleen | 4e1c197 | 2010-09-22 12:43:56 +0200 | [diff] [blame] | 1088 | if (!exclusive) |
Andrea Arcangeli | 288468c | 2010-08-09 17:19:09 -0700 | [diff] [blame] | 1089 | anon_vma = anon_vma->root; |
Linus Torvalds | ea90002 | 2010-04-12 12:44:29 -0700 | [diff] [blame] | 1090 | |
Alex Shi | 16f5e70 | 2020-12-15 12:33:42 -0800 | [diff] [blame] | 1091 | /* |
| 1092 | * page_idle does a lockless/optimistic rmap scan on page->mapping. |
| 1093 | * Make sure the compiler doesn't split the stores of anon_vma and |
| 1094 | * the PAGE_MAPPING_ANON type identifier, otherwise the rmap code |
| 1095 | * could mistake the mapping for a struct address_space and crash. |
| 1096 | */ |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1097 | anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON; |
Alex Shi | 16f5e70 | 2020-12-15 12:33:42 -0800 | [diff] [blame] | 1098 | WRITE_ONCE(page->mapping, (struct address_space *) anon_vma); |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1099 | page->index = linear_page_index(vma, address); |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /** |
Randy Dunlap | 43d8eac | 2008-03-19 17:00:43 -0700 | [diff] [blame] | 1103 | * __page_check_anon_rmap - sanity check anonymous rmap addition |
Nick Piggin | c97a9e1 | 2007-05-16 22:11:21 -0700 | [diff] [blame] | 1104 | * @page: the page to add the mapping to |
| 1105 | * @vma: the vm area in which the mapping is added |
| 1106 | * @address: the user virtual address mapped |
| 1107 | */ |
| 1108 | static void __page_check_anon_rmap(struct page *page, |
| 1109 | struct vm_area_struct *vma, unsigned long address) |
| 1110 | { |
Nick Piggin | c97a9e1 | 2007-05-16 22:11:21 -0700 | [diff] [blame] | 1111 | /* |
| 1112 | * The page's anon-rmap details (mapping and index) are guaranteed to |
| 1113 | * be set up correctly at this point. |
| 1114 | * |
| 1115 | * We have exclusion against page_add_anon_rmap because the caller |
Miaohe Lin | 90aaca8 | 2021-02-25 17:17:59 -0800 | [diff] [blame] | 1116 | * always holds the page locked. |
Nick Piggin | c97a9e1 | 2007-05-16 22:11:21 -0700 | [diff] [blame] | 1117 | * |
| 1118 | * We have exclusion against page_add_new_anon_rmap because those pages |
| 1119 | * are initially only visible via the pagetables, and the pte is locked |
| 1120 | * over the call to page_add_new_anon_rmap. |
| 1121 | */ |
Yang Shi | 30c4638 | 2019-11-30 17:51:26 -0800 | [diff] [blame] | 1122 | VM_BUG_ON_PAGE(page_anon_vma(page)->root != vma->anon_vma->root, page); |
| 1123 | VM_BUG_ON_PAGE(page_to_pgoff(page) != linear_page_index(vma, address), |
| 1124 | page); |
Nick Piggin | c97a9e1 | 2007-05-16 22:11:21 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | * page_add_anon_rmap - add pte mapping to an anonymous page |
| 1129 | * @page: the page to add the mapping to |
| 1130 | * @vma: the vm area in which the mapping is added |
| 1131 | * @address: the user virtual address mapped |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 1132 | * @compound: charge the page as compound or small page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | * |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1134 | * The caller needs to hold the pte lock, and the page must be locked in |
Hugh Dickins | 80e14822 | 2009-12-14 17:59:29 -0800 | [diff] [blame] | 1135 | * the anon_vma case: to serialize mapping,index checking after setting, |
| 1136 | * and to ensure that PageAnon is not being upgraded racily to PageKsm |
| 1137 | * (but PageKsm is never downgraded to PageAnon). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | */ |
| 1139 | void page_add_anon_rmap(struct page *page, |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 1140 | struct vm_area_struct *vma, unsigned long address, bool compound) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | { |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 1142 | do_page_add_anon_rmap(page, vma, address, compound ? RMAP_COMPOUND : 0); |
Rik van Riel | ad8c2ee | 2010-08-09 17:19:48 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * Special version of the above for do_swap_page, which often runs |
| 1147 | * into pages that are exclusively owned by the current process. |
| 1148 | * Everybody else should continue to use page_add_anon_rmap above. |
| 1149 | */ |
| 1150 | void do_page_add_anon_rmap(struct page *page, |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 1151 | struct vm_area_struct *vma, unsigned long address, int flags) |
Rik van Riel | ad8c2ee | 2010-08-09 17:19:48 -0700 | [diff] [blame] | 1152 | { |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1153 | bool compound = flags & RMAP_COMPOUND; |
| 1154 | bool first; |
| 1155 | |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 1156 | if (unlikely(PageKsm(page))) |
| 1157 | lock_page_memcg(page); |
| 1158 | else |
| 1159 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 1160 | |
Kirill A. Shutemov | e9b61f1 | 2016-01-15 16:54:10 -0800 | [diff] [blame] | 1161 | if (compound) { |
| 1162 | atomic_t *mapcount; |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1163 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
Kirill A. Shutemov | e9b61f1 | 2016-01-15 16:54:10 -0800 | [diff] [blame] | 1164 | VM_BUG_ON_PAGE(!PageTransHuge(page), page); |
| 1165 | mapcount = compound_mapcount_ptr(page); |
| 1166 | first = atomic_inc_and_test(mapcount); |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1167 | } else { |
| 1168 | first = atomic_inc_and_test(&page->_mapcount); |
| 1169 | } |
| 1170 | |
Andrea Arcangeli | 7913417 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1171 | if (first) { |
Matthew Wilcox (Oracle) | 6c35784 | 2020-08-14 17:30:37 -0700 | [diff] [blame] | 1172 | int nr = compound ? thp_nr_pages(page) : 1; |
Jianyu Zhan | bea04b0 | 2014-06-04 16:09:51 -0700 | [diff] [blame] | 1173 | /* |
| 1174 | * We use the irq-unsafe __{inc|mod}_zone_page_stat because |
| 1175 | * these counters are not modified in interrupt context, and |
| 1176 | * pte lock(a spinlock) is held, which implies preemption |
| 1177 | * disabled. |
| 1178 | */ |
Kirill A. Shutemov | 65c4537 | 2016-07-26 15:26:10 -0700 | [diff] [blame] | 1179 | if (compound) |
Muchun Song | 69473e5 | 2021-02-24 12:03:23 -0800 | [diff] [blame] | 1180 | __mod_lruvec_page_state(page, NR_ANON_THPS, nr); |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 1181 | __mod_lruvec_page_state(page, NR_ANON_MAPPED, nr); |
Andrea Arcangeli | 7913417 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1182 | } |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1183 | |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 1184 | if (unlikely(PageKsm(page))) { |
| 1185 | unlock_page_memcg(page); |
| 1186 | return; |
| 1187 | } |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1188 | |
Hugh Dickins | 5dbe0af | 2011-05-28 13:17:04 -0700 | [diff] [blame] | 1189 | /* address might be in next vma when migration races vma_adjust */ |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 1190 | if (first) |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 1191 | __page_set_anon_rmap(page, vma, address, |
| 1192 | flags & RMAP_EXCLUSIVE); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1193 | else |
Nick Piggin | c97a9e1 | 2007-05-16 22:11:21 -0700 | [diff] [blame] | 1194 | __page_check_anon_rmap(page, vma, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Randy Dunlap | 43d8eac | 2008-03-19 17:00:43 -0700 | [diff] [blame] | 1197 | /** |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1198 | * page_add_new_anon_rmap - add pte mapping to a new anonymous page |
| 1199 | * @page: the page to add the mapping to |
| 1200 | * @vma: the vm area in which the mapping is added |
| 1201 | * @address: the user virtual address mapped |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 1202 | * @compound: charge the page as compound or small page |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1203 | * |
| 1204 | * Same as page_add_anon_rmap but must only be called on *new* pages. |
| 1205 | * This means the inc-and-test can be bypassed. |
Nick Piggin | c97a9e1 | 2007-05-16 22:11:21 -0700 | [diff] [blame] | 1206 | * Page does not have to be locked. |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1207 | */ |
| 1208 | void page_add_new_anon_rmap(struct page *page, |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 1209 | struct vm_area_struct *vma, unsigned long address, bool compound) |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1210 | { |
Matthew Wilcox (Oracle) | 6c35784 | 2020-08-14 17:30:37 -0700 | [diff] [blame] | 1211 | int nr = compound ? thp_nr_pages(page) : 1; |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 1212 | |
Sasha Levin | 81d1b09 | 2014-10-09 15:28:10 -0700 | [diff] [blame] | 1213 | VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma); |
Hugh Dickins | fa9949d | 2016-05-19 17:12:41 -0700 | [diff] [blame] | 1214 | __SetPageSwapBacked(page); |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 1215 | if (compound) { |
| 1216 | VM_BUG_ON_PAGE(!PageTransHuge(page), page); |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1217 | /* increment count (starts at -1) */ |
| 1218 | atomic_set(compound_mapcount_ptr(page), 0); |
John Hubbard | 47e29d3 | 2020-04-01 21:05:33 -0700 | [diff] [blame] | 1219 | if (hpage_pincount_available(page)) |
| 1220 | atomic_set(compound_pincount_ptr(page), 0); |
| 1221 | |
Muchun Song | 69473e5 | 2021-02-24 12:03:23 -0800 | [diff] [blame] | 1222 | __mod_lruvec_page_state(page, NR_ANON_THPS, nr); |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1223 | } else { |
| 1224 | /* Anon THP always mapped first with PMD */ |
| 1225 | VM_BUG_ON_PAGE(PageTransCompound(page), page); |
| 1226 | /* increment count (starts at -1) */ |
| 1227 | atomic_set(&page->_mapcount, 0); |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 1228 | } |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 1229 | __mod_lruvec_page_state(page, NR_ANON_MAPPED, nr); |
Rik van Riel | e8a03fe | 2010-04-14 17:59:28 -0400 | [diff] [blame] | 1230 | __page_set_anon_rmap(page, vma, address, 1); |
Nick Piggin | 9617d95 | 2006-01-06 00:11:12 -0800 | [diff] [blame] | 1231 | } |
| 1232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | /** |
| 1234 | * page_add_file_rmap - add pte mapping to a file page |
| 1235 | * @page: the page to add the mapping to |
Mike Rapoport | e8b098f | 2018-04-05 16:24:57 -0700 | [diff] [blame] | 1236 | * @compound: charge the page as compound or small page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | * |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 1238 | * The caller needs to hold the pte lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | */ |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 1240 | void page_add_file_rmap(struct page *page, bool compound) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | { |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 1242 | int i, nr = 1; |
| 1243 | |
| 1244 | VM_BUG_ON_PAGE(compound && !PageTransHuge(page), page); |
Johannes Weiner | 62cccb8 | 2016-03-15 14:57:22 -0700 | [diff] [blame] | 1245 | lock_page_memcg(page); |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 1246 | if (compound && PageTransHuge(page)) { |
Muchun Song | a1528e2 | 2021-02-24 12:03:35 -0800 | [diff] [blame] | 1247 | int nr_pages = thp_nr_pages(page); |
| 1248 | |
| 1249 | for (i = 0, nr = 0; i < nr_pages; i++) { |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 1250 | if (atomic_inc_and_test(&page[i]._mapcount)) |
| 1251 | nr++; |
| 1252 | } |
| 1253 | if (!atomic_inc_and_test(compound_mapcount_ptr(page))) |
| 1254 | goto out; |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1255 | if (PageSwapBacked(page)) |
Muchun Song | a1528e2 | 2021-02-24 12:03:35 -0800 | [diff] [blame] | 1256 | __mod_lruvec_page_state(page, NR_SHMEM_PMDMAPPED, |
| 1257 | nr_pages); |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1258 | else |
Muchun Song | 380780e | 2021-02-24 12:03:39 -0800 | [diff] [blame] | 1259 | __mod_lruvec_page_state(page, NR_FILE_PMDMAPPED, |
| 1260 | nr_pages); |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 1261 | } else { |
Kirill A. Shutemov | c8efc39 | 2016-08-10 16:27:52 -0700 | [diff] [blame] | 1262 | if (PageTransCompound(page) && page_mapping(page)) { |
Muchun Song | fe3df44 | 2021-09-07 19:55:55 -0700 | [diff] [blame] | 1263 | struct page *head = compound_head(page); |
| 1264 | |
Kirill A. Shutemov | c8efc39 | 2016-08-10 16:27:52 -0700 | [diff] [blame] | 1265 | VM_WARN_ON_ONCE(!PageLocked(page)); |
| 1266 | |
Muchun Song | fe3df44 | 2021-09-07 19:55:55 -0700 | [diff] [blame] | 1267 | SetPageDoubleMap(head); |
Kirill A. Shutemov | 9a73f61 | 2016-07-26 15:25:53 -0700 | [diff] [blame] | 1268 | if (PageMlocked(page)) |
Muchun Song | fe3df44 | 2021-09-07 19:55:55 -0700 | [diff] [blame] | 1269 | clear_page_mlock(head); |
Kirill A. Shutemov | 9a73f61 | 2016-07-26 15:25:53 -0700 | [diff] [blame] | 1270 | } |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 1271 | if (!atomic_inc_and_test(&page->_mapcount)) |
| 1272 | goto out; |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1273 | } |
Johannes Weiner | 00f3ca2 | 2017-07-06 15:40:52 -0700 | [diff] [blame] | 1274 | __mod_lruvec_page_state(page, NR_FILE_MAPPED, nr); |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 1275 | out: |
Johannes Weiner | 62cccb8 | 2016-03-15 14:57:22 -0700 | [diff] [blame] | 1276 | unlock_page_memcg(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 1279 | static void page_remove_file_rmap(struct page *page, bool compound) |
Johannes Weiner | 8186eb6 | 2014-10-29 14:50:51 -0700 | [diff] [blame] | 1280 | { |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 1281 | int i, nr = 1; |
| 1282 | |
Steve Capper | 57dea93 | 2016-08-10 16:27:55 -0700 | [diff] [blame] | 1283 | VM_BUG_ON_PAGE(compound && !PageHead(page), page); |
Johannes Weiner | 8186eb6 | 2014-10-29 14:50:51 -0700 | [diff] [blame] | 1284 | |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1285 | /* Hugepages are not counted in NR_FILE_MAPPED for now. */ |
| 1286 | if (unlikely(PageHuge(page))) { |
| 1287 | /* hugetlb pages are always mapped with pmds */ |
| 1288 | atomic_dec(compound_mapcount_ptr(page)); |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 1289 | return; |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
Johannes Weiner | 8186eb6 | 2014-10-29 14:50:51 -0700 | [diff] [blame] | 1292 | /* page still mapped by someone else? */ |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 1293 | if (compound && PageTransHuge(page)) { |
Muchun Song | a1528e2 | 2021-02-24 12:03:35 -0800 | [diff] [blame] | 1294 | int nr_pages = thp_nr_pages(page); |
| 1295 | |
| 1296 | for (i = 0, nr = 0; i < nr_pages; i++) { |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 1297 | if (atomic_add_negative(-1, &page[i]._mapcount)) |
| 1298 | nr++; |
| 1299 | } |
| 1300 | if (!atomic_add_negative(-1, compound_mapcount_ptr(page))) |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 1301 | return; |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1302 | if (PageSwapBacked(page)) |
Muchun Song | a1528e2 | 2021-02-24 12:03:35 -0800 | [diff] [blame] | 1303 | __mod_lruvec_page_state(page, NR_SHMEM_PMDMAPPED, |
| 1304 | -nr_pages); |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1305 | else |
Muchun Song | 380780e | 2021-02-24 12:03:39 -0800 | [diff] [blame] | 1306 | __mod_lruvec_page_state(page, NR_FILE_PMDMAPPED, |
| 1307 | -nr_pages); |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 1308 | } else { |
| 1309 | if (!atomic_add_negative(-1, &page->_mapcount)) |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 1310 | return; |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 1311 | } |
Johannes Weiner | 8186eb6 | 2014-10-29 14:50:51 -0700 | [diff] [blame] | 1312 | |
Johannes Weiner | 8186eb6 | 2014-10-29 14:50:51 -0700 | [diff] [blame] | 1313 | /* |
Johannes Weiner | 00f3ca2 | 2017-07-06 15:40:52 -0700 | [diff] [blame] | 1314 | * We use the irq-unsafe __{inc|mod}_lruvec_page_state because |
Johannes Weiner | 8186eb6 | 2014-10-29 14:50:51 -0700 | [diff] [blame] | 1315 | * these counters are not modified in interrupt context, and |
| 1316 | * pte lock(a spinlock) is held, which implies preemption disabled. |
| 1317 | */ |
Johannes Weiner | 00f3ca2 | 2017-07-06 15:40:52 -0700 | [diff] [blame] | 1318 | __mod_lruvec_page_state(page, NR_FILE_MAPPED, -nr); |
Johannes Weiner | 8186eb6 | 2014-10-29 14:50:51 -0700 | [diff] [blame] | 1319 | |
| 1320 | if (unlikely(PageMlocked(page))) |
| 1321 | clear_page_mlock(page); |
Johannes Weiner | 8186eb6 | 2014-10-29 14:50:51 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1324 | static void page_remove_anon_compound_rmap(struct page *page) |
| 1325 | { |
| 1326 | int i, nr; |
| 1327 | |
| 1328 | if (!atomic_add_negative(-1, compound_mapcount_ptr(page))) |
| 1329 | return; |
| 1330 | |
| 1331 | /* Hugepages are not counted in NR_ANON_PAGES for now. */ |
| 1332 | if (unlikely(PageHuge(page))) |
| 1333 | return; |
| 1334 | |
| 1335 | if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) |
| 1336 | return; |
| 1337 | |
Muchun Song | 69473e5 | 2021-02-24 12:03:23 -0800 | [diff] [blame] | 1338 | __mod_lruvec_page_state(page, NR_ANON_THPS, -thp_nr_pages(page)); |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1339 | |
| 1340 | if (TestClearPageDoubleMap(page)) { |
| 1341 | /* |
| 1342 | * Subpages can be mapped with PTEs too. Check how many of |
Kirill A. Shutemov | f1fe80d | 2019-11-30 17:57:15 -0800 | [diff] [blame] | 1343 | * them are still mapped. |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1344 | */ |
Matthew Wilcox (Oracle) | 5eaf35a | 2020-10-15 20:05:46 -0700 | [diff] [blame] | 1345 | for (i = 0, nr = 0; i < thp_nr_pages(page); i++) { |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1346 | if (atomic_add_negative(-1, &page[i]._mapcount)) |
| 1347 | nr++; |
| 1348 | } |
Kirill A. Shutemov | f1fe80d | 2019-11-30 17:57:15 -0800 | [diff] [blame] | 1349 | |
| 1350 | /* |
| 1351 | * Queue the page for deferred split if at least one small |
| 1352 | * page of the compound page is unmapped, but at least one |
| 1353 | * small page is still mapped. |
| 1354 | */ |
Matthew Wilcox (Oracle) | 5eaf35a | 2020-10-15 20:05:46 -0700 | [diff] [blame] | 1355 | if (nr && nr < thp_nr_pages(page)) |
Kirill A. Shutemov | f1fe80d | 2019-11-30 17:57:15 -0800 | [diff] [blame] | 1356 | deferred_split_huge_page(page); |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1357 | } else { |
Matthew Wilcox (Oracle) | 5eaf35a | 2020-10-15 20:05:46 -0700 | [diff] [blame] | 1358 | nr = thp_nr_pages(page); |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1359 | } |
| 1360 | |
Kirill A. Shutemov | e90309c | 2016-01-15 16:54:33 -0800 | [diff] [blame] | 1361 | if (unlikely(PageMlocked(page))) |
| 1362 | clear_page_mlock(page); |
| 1363 | |
Kirill A. Shutemov | f1fe80d | 2019-11-30 17:57:15 -0800 | [diff] [blame] | 1364 | if (nr) |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 1365 | __mod_lruvec_page_state(page, NR_ANON_MAPPED, -nr); |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1366 | } |
| 1367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | /** |
| 1369 | * page_remove_rmap - take down pte mapping from a page |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 1370 | * @page: page to remove mapping from |
| 1371 | * @compound: uncharge the page as compound or small page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | * |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 1373 | * The caller needs to hold the pte lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | */ |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 1375 | void page_remove_rmap(struct page *page, bool compound) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | { |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 1377 | lock_page_memcg(page); |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 1378 | |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 1379 | if (!PageAnon(page)) { |
| 1380 | page_remove_file_rmap(page, compound); |
| 1381 | goto out; |
| 1382 | } |
| 1383 | |
| 1384 | if (compound) { |
| 1385 | page_remove_anon_compound_rmap(page); |
| 1386 | goto out; |
| 1387 | } |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1388 | |
KOSAKI Motohiro | b904dcf | 2009-09-21 17:01:28 -0700 | [diff] [blame] | 1389 | /* page still mapped by someone else? */ |
| 1390 | if (!atomic_add_negative(-1, &page->_mapcount)) |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 1391 | goto out; |
Johannes Weiner | 8186eb6 | 2014-10-29 14:50:51 -0700 | [diff] [blame] | 1392 | |
KOSAKI Motohiro | b904dcf | 2009-09-21 17:01:28 -0700 | [diff] [blame] | 1393 | /* |
Jianyu Zhan | bea04b0 | 2014-06-04 16:09:51 -0700 | [diff] [blame] | 1394 | * We use the irq-unsafe __{inc|mod}_zone_page_stat because |
| 1395 | * these counters are not modified in interrupt context, and |
Jianyu Zhan | bea04b0 | 2014-06-04 16:09:51 -0700 | [diff] [blame] | 1396 | * pte lock(a spinlock) is held, which implies preemption disabled. |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 1397 | */ |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 1398 | __dec_lruvec_page_state(page, NR_ANON_MAPPED); |
Johannes Weiner | 8186eb6 | 2014-10-29 14:50:51 -0700 | [diff] [blame] | 1399 | |
Hugh Dickins | e6c509f | 2012-10-08 16:33:19 -0700 | [diff] [blame] | 1400 | if (unlikely(PageMlocked(page))) |
| 1401 | clear_page_mlock(page); |
Johannes Weiner | 8186eb6 | 2014-10-29 14:50:51 -0700 | [diff] [blame] | 1402 | |
Kirill A. Shutemov | 9a98225 | 2016-01-15 16:54:17 -0800 | [diff] [blame] | 1403 | if (PageTransCompound(page)) |
| 1404 | deferred_split_huge_page(compound_head(page)); |
| 1405 | |
KOSAKI Motohiro | b904dcf | 2009-09-21 17:01:28 -0700 | [diff] [blame] | 1406 | /* |
| 1407 | * It would be tidy to reset the PageAnon mapping here, |
| 1408 | * but that might overwrite a racing page_add_anon_rmap |
| 1409 | * which increments mapcount after us but sets mapping |
Mel Gorman | 2d4894b | 2017-11-15 17:37:59 -0800 | [diff] [blame] | 1410 | * before us: so leave the reset to free_unref_page, |
KOSAKI Motohiro | b904dcf | 2009-09-21 17:01:28 -0700 | [diff] [blame] | 1411 | * and remember that it's only reliable while mapped. |
| 1412 | * Leaving it set also helps swapoff to reinstate ptes |
| 1413 | * faster for those pages still in swapcache. |
| 1414 | */ |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 1415 | out: |
| 1416 | unlock_page_memcg(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | /* |
Joonsoo Kim | 5262950 | 2014-01-21 15:49:50 -0800 | [diff] [blame] | 1420 | * @arg: enum ttu_flags will be passed to this argument |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | */ |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 1422 | static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma, |
Joonsoo Kim | 5262950 | 2014-01-21 15:49:50 -0800 | [diff] [blame] | 1423 | unsigned long address, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | { |
| 1425 | struct mm_struct *mm = vma->vm_mm; |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1426 | struct page_vma_mapped_walk pvmw = { |
| 1427 | .page = page, |
| 1428 | .vma = vma, |
| 1429 | .address = address, |
| 1430 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | pte_t pteval; |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1432 | struct page *subpage; |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 1433 | bool ret = true; |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1434 | struct mmu_notifier_range range; |
Palmer Dabbelt | 4708f31 | 2020-04-06 20:08:00 -0700 | [diff] [blame] | 1435 | enum ttu_flags flags = (enum ttu_flags)(long)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | |
Hugh Dickins | 732ed55 | 2021-06-15 18:23:53 -0700 | [diff] [blame] | 1437 | /* |
| 1438 | * When racing against e.g. zap_pte_range() on another cpu, |
| 1439 | * in between its ptep_get_and_clear_full() and page_remove_rmap(), |
Yang Shi | 1fb08ac | 2021-06-30 18:52:01 -0700 | [diff] [blame] | 1440 | * try_to_unmap() may return before page_mapped() has become false, |
Hugh Dickins | 732ed55 | 2021-06-15 18:23:53 -0700 | [diff] [blame] | 1441 | * if page table locking is skipped: use TTU_SYNC to wait for that. |
| 1442 | */ |
| 1443 | if (flags & TTU_SYNC) |
| 1444 | pvmw.flags = PVMW_SYNC; |
| 1445 | |
Alistair Popple | a98a2f0 | 2021-06-30 18:54:16 -0700 | [diff] [blame] | 1446 | if (flags & TTU_SPLIT_HUGE_PMD) |
| 1447 | split_huge_pmd_address(vma, address, false, page); |
Kirill A. Shutemov | fec89c1 | 2016-03-17 14:20:10 -0700 | [diff] [blame] | 1448 | |
Jérôme Glisse | 369ea82 | 2017-08-31 17:17:27 -0400 | [diff] [blame] | 1449 | /* |
Mike Kravetz | 017b166 | 2018-10-05 15:51:29 -0700 | [diff] [blame] | 1450 | * For THP, we have to assume the worse case ie pmd for invalidation. |
| 1451 | * For hugetlb, it could be much worse if we need to do pud |
| 1452 | * invalidation in the case of pmd sharing. |
| 1453 | * |
| 1454 | * Note that the page can not be free in this function as call of |
| 1455 | * try_to_unmap() must hold a reference on the page. |
Jérôme Glisse | 369ea82 | 2017-08-31 17:17:27 -0400 | [diff] [blame] | 1456 | */ |
Hugh Dickins | 494334e | 2021-06-15 18:23:56 -0700 | [diff] [blame] | 1457 | range.end = PageKsm(page) ? |
| 1458 | address + PAGE_SIZE : vma_address_end(page, vma); |
Jérôme Glisse | 7269f99 | 2019-05-13 17:20:53 -0700 | [diff] [blame] | 1459 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, |
Hugh Dickins | 494334e | 2021-06-15 18:23:56 -0700 | [diff] [blame] | 1460 | address, range.end); |
Mike Kravetz | 017b166 | 2018-10-05 15:51:29 -0700 | [diff] [blame] | 1461 | if (PageHuge(page)) { |
| 1462 | /* |
| 1463 | * If sharing is possible, start and end will be adjusted |
| 1464 | * accordingly. |
| 1465 | */ |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1466 | adjust_range_if_pmd_sharing_possible(vma, &range.start, |
| 1467 | &range.end); |
Mike Kravetz | 017b166 | 2018-10-05 15:51:29 -0700 | [diff] [blame] | 1468 | } |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1469 | mmu_notifier_invalidate_range_start(&range); |
Jérôme Glisse | 369ea82 | 2017-08-31 17:17:27 -0400 | [diff] [blame] | 1470 | |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1471 | while (page_vma_mapped_walk(&pvmw)) { |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1472 | /* |
| 1473 | * If the page is mlock()d, we cannot swap it out. |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1474 | */ |
Hugh Dickins | efdb672 | 2021-07-11 20:10:49 -0700 | [diff] [blame] | 1475 | if (!(flags & TTU_IGNORE_MLOCK) && |
| 1476 | (vma->vm_flags & VM_LOCKED)) { |
| 1477 | /* |
| 1478 | * PTE-mapped THP are never marked as mlocked: so do |
| 1479 | * not set it on a DoubleMap THP, nor on an Anon THP |
| 1480 | * (which may still be PTE-mapped after DoubleMap was |
| 1481 | * cleared). But stop unmapping even in those cases. |
| 1482 | */ |
| 1483 | if (!PageTransCompound(page) || (PageHead(page) && |
| 1484 | !PageDoubleMap(page) && !PageAnon(page))) |
| 1485 | mlock_vma_page(page); |
| 1486 | page_vma_mapped_walk_done(&pvmw); |
| 1487 | ret = false; |
| 1488 | break; |
Hugh Dickins | b87537d9e | 2015-11-05 18:49:33 -0800 | [diff] [blame] | 1489 | } |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1490 | |
Kirill A. Shutemov | 8346242 | 2017-03-09 16:17:20 -0800 | [diff] [blame] | 1491 | /* Unexpected PMD-mapped THP? */ |
| 1492 | VM_BUG_ON_PAGE(!pvmw.pte, page); |
| 1493 | |
| 1494 | subpage = page - page_to_pfn(page) + pte_pfn(*pvmw.pte); |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 1495 | address = pvmw.address; |
| 1496 | |
Mike Kravetz | 336bf30 | 2020-11-13 22:52:16 -0800 | [diff] [blame] | 1497 | if (PageHuge(page) && !PageAnon(page)) { |
Mike Kravetz | c0d0381 | 2020-04-01 21:11:05 -0700 | [diff] [blame] | 1498 | /* |
| 1499 | * To call huge_pmd_unshare, i_mmap_rwsem must be |
| 1500 | * held in write mode. Caller needs to explicitly |
| 1501 | * do this outside rmap routines. |
| 1502 | */ |
| 1503 | VM_BUG_ON(!(flags & TTU_RMAP_LOCKED)); |
Mike Kravetz | 34ae204 | 2020-08-11 18:31:38 -0700 | [diff] [blame] | 1504 | if (huge_pmd_unshare(mm, vma, &address, pvmw.pte)) { |
Mike Kravetz | 017b166 | 2018-10-05 15:51:29 -0700 | [diff] [blame] | 1505 | /* |
| 1506 | * huge_pmd_unshare unmapped an entire PMD |
| 1507 | * page. There is no way of knowing exactly |
| 1508 | * which PMDs may be cached for this mm, so |
| 1509 | * we must flush them all. start/end were |
| 1510 | * already adjusted above to cover this range. |
| 1511 | */ |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1512 | flush_cache_range(vma, range.start, range.end); |
| 1513 | flush_tlb_range(vma, range.start, range.end); |
| 1514 | mmu_notifier_invalidate_range(mm, range.start, |
| 1515 | range.end); |
Mike Kravetz | 017b166 | 2018-10-05 15:51:29 -0700 | [diff] [blame] | 1516 | |
| 1517 | /* |
| 1518 | * The ref count of the PMD page was dropped |
| 1519 | * which is part of the way map counting |
| 1520 | * is done for shared PMDs. Return 'true' |
| 1521 | * here. When there is no other sharing, |
| 1522 | * huge_pmd_unshare returns false and we will |
| 1523 | * unmap the actual page and drop map count |
| 1524 | * to zero. |
| 1525 | */ |
| 1526 | page_vma_mapped_walk_done(&pvmw); |
| 1527 | break; |
| 1528 | } |
| 1529 | } |
Kirill A. Shutemov | 8346242 | 2017-03-09 16:17:20 -0800 | [diff] [blame] | 1530 | |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1531 | /* Nuke the page table entry. */ |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 1532 | flush_cache_page(vma, address, pte_pfn(*pvmw.pte)); |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1533 | if (should_defer_flush(mm, flags)) { |
| 1534 | /* |
| 1535 | * We clear the PTE but do not flush so potentially |
| 1536 | * a remote CPU could still be writing to the page. |
| 1537 | * If the entry was previously clean then the |
| 1538 | * architecture must guarantee that a clear->dirty |
| 1539 | * transition on a cached TLB entry is written through |
| 1540 | * and traps if the PTE is unmapped. |
| 1541 | */ |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 1542 | pteval = ptep_get_and_clear(mm, address, pvmw.pte); |
Mel Gorman | 72b252a | 2015-09-04 15:47:32 -0700 | [diff] [blame] | 1543 | |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1544 | set_tlb_ubc_flush_pending(mm, pte_dirty(pteval)); |
Naoya Horiguchi | 5d317b2 | 2015-11-05 18:47:14 -0800 | [diff] [blame] | 1545 | } else { |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 1546 | pteval = ptep_clear_flush(vma, address, pvmw.pte); |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | /* Move the dirty bit to the page. Now the pte is gone. */ |
| 1550 | if (pte_dirty(pteval)) |
| 1551 | set_page_dirty(page); |
| 1552 | |
| 1553 | /* Update high watermark before we lower rss */ |
| 1554 | update_hiwater_rss(mm); |
| 1555 | |
| 1556 | if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) { |
Punit Agrawal | 5fd27b8 | 2017-07-06 15:39:53 -0700 | [diff] [blame] | 1557 | pteval = swp_entry_to_pte(make_hwpoison_entry(subpage)); |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1558 | if (PageHuge(page)) { |
Matthew Wilcox (Oracle) | d8c6546 | 2019-09-23 15:34:30 -0700 | [diff] [blame] | 1559 | hugetlb_count_sub(compound_nr(page), mm); |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 1560 | set_huge_swap_pte_at(mm, address, |
Punit Agrawal | 5fd27b8 | 2017-07-06 15:39:53 -0700 | [diff] [blame] | 1561 | pvmw.pte, pteval, |
| 1562 | vma_mmu_pagesize(vma)); |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1563 | } else { |
| 1564 | dec_mm_counter(mm, mm_counter(page)); |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 1565 | set_pte_at(mm, address, pvmw.pte, pteval); |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1566 | } |
| 1567 | |
Christian Borntraeger | bce73e4 | 2018-07-13 16:58:52 -0700 | [diff] [blame] | 1568 | } else if (pte_unused(pteval) && !userfaultfd_armed(vma)) { |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1569 | /* |
| 1570 | * The guest indicated that the page content is of no |
| 1571 | * interest anymore. Simply discard the pte, vmscan |
| 1572 | * will take care of the rest. |
Christian Borntraeger | bce73e4 | 2018-07-13 16:58:52 -0700 | [diff] [blame] | 1573 | * A future reference will then fault in a new zero |
| 1574 | * page. When userfaultfd is active, we must not drop |
| 1575 | * this page though, as its main user (postcopy |
| 1576 | * migration) will not expect userfaults on already |
| 1577 | * copied pages. |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1578 | */ |
Jerome Marchand | eca56ff | 2016-01-14 15:19:26 -0800 | [diff] [blame] | 1579 | dec_mm_counter(mm, mm_counter(page)); |
Jérôme Glisse | 0f10851 | 2017-11-15 17:34:07 -0800 | [diff] [blame] | 1580 | /* We have to invalidate as we cleared the pte */ |
| 1581 | mmu_notifier_invalidate_range(mm, address, |
| 1582 | address + PAGE_SIZE); |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1583 | } else if (PageAnon(page)) { |
| 1584 | swp_entry_t entry = { .val = page_private(subpage) }; |
| 1585 | pte_t swp_pte; |
| 1586 | /* |
| 1587 | * Store the swap location in the pte. |
| 1588 | * See handle_pte_fault() ... |
| 1589 | */ |
Minchan Kim | eb94a87 | 2017-05-03 14:52:36 -0700 | [diff] [blame] | 1590 | if (unlikely(PageSwapBacked(page) != PageSwapCache(page))) { |
| 1591 | WARN_ON_ONCE(1); |
Minchan Kim | 83612a9 | 2017-05-03 14:54:30 -0700 | [diff] [blame] | 1592 | ret = false; |
Jérôme Glisse | 369ea82 | 2017-08-31 17:17:27 -0400 | [diff] [blame] | 1593 | /* We have to invalidate as we cleared the pte */ |
Jérôme Glisse | 0f10851 | 2017-11-15 17:34:07 -0800 | [diff] [blame] | 1594 | mmu_notifier_invalidate_range(mm, address, |
| 1595 | address + PAGE_SIZE); |
Minchan Kim | eb94a87 | 2017-05-03 14:52:36 -0700 | [diff] [blame] | 1596 | page_vma_mapped_walk_done(&pvmw); |
| 1597 | break; |
| 1598 | } |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 1599 | |
Shaohua Li | 802a3a9 | 2017-05-03 14:52:32 -0700 | [diff] [blame] | 1600 | /* MADV_FREE page check */ |
| 1601 | if (!PageSwapBacked(page)) { |
| 1602 | if (!PageDirty(page)) { |
Jérôme Glisse | 0f10851 | 2017-11-15 17:34:07 -0800 | [diff] [blame] | 1603 | /* Invalidate as we cleared the pte */ |
| 1604 | mmu_notifier_invalidate_range(mm, |
| 1605 | address, address + PAGE_SIZE); |
Shaohua Li | 802a3a9 | 2017-05-03 14:52:32 -0700 | [diff] [blame] | 1606 | dec_mm_counter(mm, MM_ANONPAGES); |
| 1607 | goto discard; |
| 1608 | } |
| 1609 | |
| 1610 | /* |
| 1611 | * If the page was redirtied, it cannot be |
| 1612 | * discarded. Remap the page to page table. |
| 1613 | */ |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 1614 | set_pte_at(mm, address, pvmw.pte, pteval); |
Minchan Kim | 18863d3 | 2017-05-03 14:54:04 -0700 | [diff] [blame] | 1615 | SetPageSwapBacked(page); |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 1616 | ret = false; |
Shaohua Li | 802a3a9 | 2017-05-03 14:52:32 -0700 | [diff] [blame] | 1617 | page_vma_mapped_walk_done(&pvmw); |
| 1618 | break; |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | if (swap_duplicate(entry) < 0) { |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 1622 | set_pte_at(mm, address, pvmw.pte, pteval); |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 1623 | ret = false; |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1624 | page_vma_mapped_walk_done(&pvmw); |
| 1625 | break; |
| 1626 | } |
Khalid Aziz | ca827d5 | 2018-02-21 10:15:44 -0700 | [diff] [blame] | 1627 | if (arch_unmap_one(mm, vma, address, pteval) < 0) { |
| 1628 | set_pte_at(mm, address, pvmw.pte, pteval); |
| 1629 | ret = false; |
| 1630 | page_vma_mapped_walk_done(&pvmw); |
| 1631 | break; |
| 1632 | } |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1633 | if (list_empty(&mm->mmlist)) { |
| 1634 | spin_lock(&mmlist_lock); |
| 1635 | if (list_empty(&mm->mmlist)) |
| 1636 | list_add(&mm->mmlist, &init_mm.mmlist); |
| 1637 | spin_unlock(&mmlist_lock); |
| 1638 | } |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 1639 | dec_mm_counter(mm, MM_ANONPAGES); |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1640 | inc_mm_counter(mm, MM_SWAPENTS); |
| 1641 | swp_pte = swp_entry_to_pte(entry); |
| 1642 | if (pte_soft_dirty(pteval)) |
| 1643 | swp_pte = pte_swp_mksoft_dirty(swp_pte); |
Peter Xu | f45ec5f | 2020-04-06 20:06:01 -0700 | [diff] [blame] | 1644 | if (pte_uffd_wp(pteval)) |
| 1645 | swp_pte = pte_swp_mkuffd_wp(swp_pte); |
Linus Torvalds | 785373b | 2017-08-29 09:11:06 -0700 | [diff] [blame] | 1646 | set_pte_at(mm, address, pvmw.pte, swp_pte); |
Jérôme Glisse | 0f10851 | 2017-11-15 17:34:07 -0800 | [diff] [blame] | 1647 | /* Invalidate as we cleared the pte */ |
| 1648 | mmu_notifier_invalidate_range(mm, address, |
| 1649 | address + PAGE_SIZE); |
| 1650 | } else { |
| 1651 | /* |
Hugh Dickins | 906f9cd | 2018-11-30 14:10:13 -0800 | [diff] [blame] | 1652 | * This is a locked file-backed page, thus it cannot |
| 1653 | * be removed from the page cache and replaced by a new |
| 1654 | * page before mmu_notifier_invalidate_range_end, so no |
Jérôme Glisse | 0f10851 | 2017-11-15 17:34:07 -0800 | [diff] [blame] | 1655 | * concurrent thread might update its page table to |
| 1656 | * point at new page while a device still is using this |
| 1657 | * page. |
| 1658 | * |
Mike Rapoport | ad56b73 | 2018-03-21 21:22:47 +0200 | [diff] [blame] | 1659 | * See Documentation/vm/mmu_notifier.rst |
Jérôme Glisse | 0f10851 | 2017-11-15 17:34:07 -0800 | [diff] [blame] | 1660 | */ |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1661 | dec_mm_counter(mm, mm_counter_file(page)); |
Jérôme Glisse | 0f10851 | 2017-11-15 17:34:07 -0800 | [diff] [blame] | 1662 | } |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 1663 | discard: |
Jérôme Glisse | 0f10851 | 2017-11-15 17:34:07 -0800 | [diff] [blame] | 1664 | /* |
| 1665 | * No need to call mmu_notifier_invalidate_range() it has be |
| 1666 | * done above for all cases requiring it to happen under page |
| 1667 | * table lock before mmu_notifier_invalidate_range_end() |
| 1668 | * |
Mike Rapoport | ad56b73 | 2018-03-21 21:22:47 +0200 | [diff] [blame] | 1669 | * See Documentation/vm/mmu_notifier.rst |
Jérôme Glisse | 0f10851 | 2017-11-15 17:34:07 -0800 | [diff] [blame] | 1670 | */ |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1671 | page_remove_rmap(subpage, PageHuge(page)); |
| 1672 | put_page(page); |
Kirill A. Shutemov | c7ab0d2 | 2017-02-24 14:58:01 -0800 | [diff] [blame] | 1673 | } |
Jérôme Glisse | 369ea82 | 2017-08-31 17:17:27 -0400 | [diff] [blame] | 1674 | |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1675 | mmu_notifier_invalidate_range_end(&range); |
Jérôme Glisse | 369ea82 | 2017-08-31 17:17:27 -0400 | [diff] [blame] | 1676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | return ret; |
| 1678 | } |
| 1679 | |
Joonsoo Kim | 5262950 | 2014-01-21 15:49:50 -0800 | [diff] [blame] | 1680 | static bool invalid_migration_vma(struct vm_area_struct *vma, void *arg) |
| 1681 | { |
Anshuman Khandual | 222100e | 2020-04-01 21:07:52 -0700 | [diff] [blame] | 1682 | return vma_is_temporary_stack(vma); |
Joonsoo Kim | 5262950 | 2014-01-21 15:49:50 -0800 | [diff] [blame] | 1683 | } |
| 1684 | |
Miaohe Lin | b7e188e | 2021-02-25 17:18:03 -0800 | [diff] [blame] | 1685 | static int page_not_mapped(struct page *page) |
Joonsoo Kim | 5262950 | 2014-01-21 15:49:50 -0800 | [diff] [blame] | 1686 | { |
Miaohe Lin | b7e188e | 2021-02-25 17:18:03 -0800 | [diff] [blame] | 1687 | return !page_mapped(page); |
Kirill A. Shutemov | 2a52bcb | 2016-03-17 14:20:04 -0700 | [diff] [blame] | 1688 | } |
Joonsoo Kim | 5262950 | 2014-01-21 15:49:50 -0800 | [diff] [blame] | 1689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | /** |
| 1691 | * try_to_unmap - try to remove all page table mappings to a page |
| 1692 | * @page: the page to get unmapped |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 1693 | * @flags: action and flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | * |
| 1695 | * Tries to remove all the page table entries which are mapping this |
| 1696 | * page, used in the pageout path. Caller must hold the page lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | * |
Yang Shi | 1fb08ac | 2021-06-30 18:52:01 -0700 | [diff] [blame] | 1698 | * It is the caller's responsibility to check if the page is still |
| 1699 | * mapped when needed (use TTU_SYNC to prevent accounting races). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | */ |
Yang Shi | 1fb08ac | 2021-06-30 18:52:01 -0700 | [diff] [blame] | 1701 | void try_to_unmap(struct page *page, enum ttu_flags flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | { |
Joonsoo Kim | 5262950 | 2014-01-21 15:49:50 -0800 | [diff] [blame] | 1703 | struct rmap_walk_control rwc = { |
| 1704 | .rmap_one = try_to_unmap_one, |
Shaohua Li | 802a3a9 | 2017-05-03 14:52:32 -0700 | [diff] [blame] | 1705 | .arg = (void *)flags, |
Miaohe Lin | b7e188e | 2021-02-25 17:18:03 -0800 | [diff] [blame] | 1706 | .done = page_not_mapped, |
Joonsoo Kim | 5262950 | 2014-01-21 15:49:50 -0800 | [diff] [blame] | 1707 | .anon_lock = page_lock_anon_vma_read, |
| 1708 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | |
Alistair Popple | a98a2f0 | 2021-06-30 18:54:16 -0700 | [diff] [blame] | 1710 | if (flags & TTU_RMAP_LOCKED) |
| 1711 | rmap_walk_locked(page, &rwc); |
| 1712 | else |
| 1713 | rmap_walk(page, &rwc); |
| 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | * @arg: enum ttu_flags will be passed to this argument. |
| 1718 | * |
| 1719 | * If TTU_SPLIT_HUGE_PMD is specified any PMD mappings will be split into PTEs |
Hugh Dickins | 64b586d | 2021-07-07 13:06:17 -0700 | [diff] [blame] | 1720 | * containing migration entries. |
Alistair Popple | a98a2f0 | 2021-06-30 18:54:16 -0700 | [diff] [blame] | 1721 | */ |
| 1722 | static bool try_to_migrate_one(struct page *page, struct vm_area_struct *vma, |
| 1723 | unsigned long address, void *arg) |
| 1724 | { |
| 1725 | struct mm_struct *mm = vma->vm_mm; |
| 1726 | struct page_vma_mapped_walk pvmw = { |
| 1727 | .page = page, |
| 1728 | .vma = vma, |
| 1729 | .address = address, |
| 1730 | }; |
| 1731 | pte_t pteval; |
| 1732 | struct page *subpage; |
| 1733 | bool ret = true; |
| 1734 | struct mmu_notifier_range range; |
| 1735 | enum ttu_flags flags = (enum ttu_flags)(long)arg; |
| 1736 | |
Alistair Popple | a98a2f0 | 2021-06-30 18:54:16 -0700 | [diff] [blame] | 1737 | /* |
| 1738 | * When racing against e.g. zap_pte_range() on another cpu, |
| 1739 | * in between its ptep_get_and_clear_full() and page_remove_rmap(), |
| 1740 | * try_to_migrate() may return before page_mapped() has become false, |
| 1741 | * if page table locking is skipped: use TTU_SYNC to wait for that. |
| 1742 | */ |
| 1743 | if (flags & TTU_SYNC) |
| 1744 | pvmw.flags = PVMW_SYNC; |
| 1745 | |
| 1746 | /* |
| 1747 | * unmap_page() in mm/huge_memory.c is the only user of migration with |
| 1748 | * TTU_SPLIT_HUGE_PMD and it wants to freeze. |
| 1749 | */ |
| 1750 | if (flags & TTU_SPLIT_HUGE_PMD) |
| 1751 | split_huge_pmd_address(vma, address, true, page); |
| 1752 | |
| 1753 | /* |
| 1754 | * For THP, we have to assume the worse case ie pmd for invalidation. |
| 1755 | * For hugetlb, it could be much worse if we need to do pud |
| 1756 | * invalidation in the case of pmd sharing. |
| 1757 | * |
| 1758 | * Note that the page can not be free in this function as call of |
| 1759 | * try_to_unmap() must hold a reference on the page. |
| 1760 | */ |
| 1761 | range.end = PageKsm(page) ? |
| 1762 | address + PAGE_SIZE : vma_address_end(page, vma); |
| 1763 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, |
| 1764 | address, range.end); |
| 1765 | if (PageHuge(page)) { |
| 1766 | /* |
| 1767 | * If sharing is possible, start and end will be adjusted |
| 1768 | * accordingly. |
| 1769 | */ |
| 1770 | adjust_range_if_pmd_sharing_possible(vma, &range.start, |
| 1771 | &range.end); |
| 1772 | } |
| 1773 | mmu_notifier_invalidate_range_start(&range); |
| 1774 | |
| 1775 | while (page_vma_mapped_walk(&pvmw)) { |
| 1776 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
| 1777 | /* PMD-mapped THP migration entry */ |
| 1778 | if (!pvmw.pte) { |
| 1779 | VM_BUG_ON_PAGE(PageHuge(page) || |
| 1780 | !PageTransCompound(page), page); |
| 1781 | |
| 1782 | set_pmd_migration_entry(&pvmw, page); |
| 1783 | continue; |
| 1784 | } |
| 1785 | #endif |
| 1786 | |
| 1787 | /* Unexpected PMD-mapped THP? */ |
| 1788 | VM_BUG_ON_PAGE(!pvmw.pte, page); |
| 1789 | |
| 1790 | subpage = page - page_to_pfn(page) + pte_pfn(*pvmw.pte); |
| 1791 | address = pvmw.address; |
| 1792 | |
| 1793 | if (PageHuge(page) && !PageAnon(page)) { |
| 1794 | /* |
| 1795 | * To call huge_pmd_unshare, i_mmap_rwsem must be |
| 1796 | * held in write mode. Caller needs to explicitly |
| 1797 | * do this outside rmap routines. |
| 1798 | */ |
| 1799 | VM_BUG_ON(!(flags & TTU_RMAP_LOCKED)); |
| 1800 | if (huge_pmd_unshare(mm, vma, &address, pvmw.pte)) { |
| 1801 | /* |
| 1802 | * huge_pmd_unshare unmapped an entire PMD |
| 1803 | * page. There is no way of knowing exactly |
| 1804 | * which PMDs may be cached for this mm, so |
| 1805 | * we must flush them all. start/end were |
| 1806 | * already adjusted above to cover this range. |
| 1807 | */ |
| 1808 | flush_cache_range(vma, range.start, range.end); |
| 1809 | flush_tlb_range(vma, range.start, range.end); |
| 1810 | mmu_notifier_invalidate_range(mm, range.start, |
| 1811 | range.end); |
| 1812 | |
| 1813 | /* |
| 1814 | * The ref count of the PMD page was dropped |
| 1815 | * which is part of the way map counting |
| 1816 | * is done for shared PMDs. Return 'true' |
| 1817 | * here. When there is no other sharing, |
| 1818 | * huge_pmd_unshare returns false and we will |
| 1819 | * unmap the actual page and drop map count |
| 1820 | * to zero. |
| 1821 | */ |
| 1822 | page_vma_mapped_walk_done(&pvmw); |
| 1823 | break; |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | /* Nuke the page table entry. */ |
| 1828 | flush_cache_page(vma, address, pte_pfn(*pvmw.pte)); |
| 1829 | pteval = ptep_clear_flush(vma, address, pvmw.pte); |
| 1830 | |
| 1831 | /* Move the dirty bit to the page. Now the pte is gone. */ |
| 1832 | if (pte_dirty(pteval)) |
| 1833 | set_page_dirty(page); |
| 1834 | |
| 1835 | /* Update high watermark before we lower rss */ |
| 1836 | update_hiwater_rss(mm); |
| 1837 | |
| 1838 | if (is_zone_device_page(page)) { |
Alistair Popple | 3d88705 | 2021-11-05 13:45:00 -0700 | [diff] [blame] | 1839 | unsigned long pfn = page_to_pfn(page); |
Alistair Popple | a98a2f0 | 2021-06-30 18:54:16 -0700 | [diff] [blame] | 1840 | swp_entry_t entry; |
| 1841 | pte_t swp_pte; |
| 1842 | |
| 1843 | /* |
| 1844 | * Store the pfn of the page in a special migration |
| 1845 | * pte. do_swap_page() will wait until the migration |
| 1846 | * pte is removed and then restart fault handling. |
| 1847 | */ |
Alistair Popple | 3d88705 | 2021-11-05 13:45:00 -0700 | [diff] [blame] | 1848 | entry = pte_to_swp_entry(pteval); |
| 1849 | if (is_writable_device_private_entry(entry)) |
| 1850 | entry = make_writable_migration_entry(pfn); |
| 1851 | else |
| 1852 | entry = make_readable_migration_entry(pfn); |
Alistair Popple | a98a2f0 | 2021-06-30 18:54:16 -0700 | [diff] [blame] | 1853 | swp_pte = swp_entry_to_pte(entry); |
| 1854 | |
| 1855 | /* |
| 1856 | * pteval maps a zone device page and is therefore |
| 1857 | * a swap pte. |
| 1858 | */ |
| 1859 | if (pte_swp_soft_dirty(pteval)) |
| 1860 | swp_pte = pte_swp_mksoft_dirty(swp_pte); |
| 1861 | if (pte_swp_uffd_wp(pteval)) |
| 1862 | swp_pte = pte_swp_mkuffd_wp(swp_pte); |
| 1863 | set_pte_at(mm, pvmw.address, pvmw.pte, swp_pte); |
| 1864 | /* |
| 1865 | * No need to invalidate here it will synchronize on |
| 1866 | * against the special swap migration pte. |
| 1867 | * |
| 1868 | * The assignment to subpage above was computed from a |
| 1869 | * swap PTE which results in an invalid pointer. |
| 1870 | * Since only PAGE_SIZE pages can currently be |
| 1871 | * migrated, just set it to page. This will need to be |
| 1872 | * changed when hugepage migrations to device private |
| 1873 | * memory are supported. |
| 1874 | */ |
| 1875 | subpage = page; |
| 1876 | } else if (PageHWPoison(page)) { |
| 1877 | pteval = swp_entry_to_pte(make_hwpoison_entry(subpage)); |
| 1878 | if (PageHuge(page)) { |
| 1879 | hugetlb_count_sub(compound_nr(page), mm); |
| 1880 | set_huge_swap_pte_at(mm, address, |
| 1881 | pvmw.pte, pteval, |
| 1882 | vma_mmu_pagesize(vma)); |
| 1883 | } else { |
| 1884 | dec_mm_counter(mm, mm_counter(page)); |
| 1885 | set_pte_at(mm, address, pvmw.pte, pteval); |
| 1886 | } |
| 1887 | |
| 1888 | } else if (pte_unused(pteval) && !userfaultfd_armed(vma)) { |
| 1889 | /* |
| 1890 | * The guest indicated that the page content is of no |
| 1891 | * interest anymore. Simply discard the pte, vmscan |
| 1892 | * will take care of the rest. |
| 1893 | * A future reference will then fault in a new zero |
| 1894 | * page. When userfaultfd is active, we must not drop |
| 1895 | * this page though, as its main user (postcopy |
| 1896 | * migration) will not expect userfaults on already |
| 1897 | * copied pages. |
| 1898 | */ |
| 1899 | dec_mm_counter(mm, mm_counter(page)); |
| 1900 | /* We have to invalidate as we cleared the pte */ |
| 1901 | mmu_notifier_invalidate_range(mm, address, |
| 1902 | address + PAGE_SIZE); |
| 1903 | } else { |
| 1904 | swp_entry_t entry; |
| 1905 | pte_t swp_pte; |
| 1906 | |
| 1907 | if (arch_unmap_one(mm, vma, address, pteval) < 0) { |
| 1908 | set_pte_at(mm, address, pvmw.pte, pteval); |
| 1909 | ret = false; |
| 1910 | page_vma_mapped_walk_done(&pvmw); |
| 1911 | break; |
| 1912 | } |
| 1913 | |
| 1914 | /* |
| 1915 | * Store the pfn of the page in a special migration |
| 1916 | * pte. do_swap_page() will wait until the migration |
| 1917 | * pte is removed and then restart fault handling. |
| 1918 | */ |
| 1919 | if (pte_write(pteval)) |
| 1920 | entry = make_writable_migration_entry( |
| 1921 | page_to_pfn(subpage)); |
| 1922 | else |
| 1923 | entry = make_readable_migration_entry( |
| 1924 | page_to_pfn(subpage)); |
| 1925 | |
| 1926 | swp_pte = swp_entry_to_pte(entry); |
| 1927 | if (pte_soft_dirty(pteval)) |
| 1928 | swp_pte = pte_swp_mksoft_dirty(swp_pte); |
| 1929 | if (pte_uffd_wp(pteval)) |
| 1930 | swp_pte = pte_swp_mkuffd_wp(swp_pte); |
| 1931 | set_pte_at(mm, address, pvmw.pte, swp_pte); |
| 1932 | /* |
| 1933 | * No need to invalidate here it will synchronize on |
| 1934 | * against the special swap migration pte. |
| 1935 | */ |
| 1936 | } |
| 1937 | |
| 1938 | /* |
| 1939 | * No need to call mmu_notifier_invalidate_range() it has be |
| 1940 | * done above for all cases requiring it to happen under page |
| 1941 | * table lock before mmu_notifier_invalidate_range_end() |
| 1942 | * |
| 1943 | * See Documentation/vm/mmu_notifier.rst |
| 1944 | */ |
| 1945 | page_remove_rmap(subpage, PageHuge(page)); |
| 1946 | put_page(page); |
| 1947 | } |
| 1948 | |
| 1949 | mmu_notifier_invalidate_range_end(&range); |
| 1950 | |
| 1951 | return ret; |
| 1952 | } |
| 1953 | |
| 1954 | /** |
| 1955 | * try_to_migrate - try to replace all page table mappings with swap entries |
| 1956 | * @page: the page to replace page table entries for |
| 1957 | * @flags: action and flags |
| 1958 | * |
| 1959 | * Tries to remove all the page table entries which are mapping this page and |
| 1960 | * replace them with special swap entries. Caller must hold the page lock. |
Alistair Popple | a98a2f0 | 2021-06-30 18:54:16 -0700 | [diff] [blame] | 1961 | */ |
| 1962 | void try_to_migrate(struct page *page, enum ttu_flags flags) |
| 1963 | { |
| 1964 | struct rmap_walk_control rwc = { |
| 1965 | .rmap_one = try_to_migrate_one, |
| 1966 | .arg = (void *)flags, |
| 1967 | .done = page_not_mapped, |
| 1968 | .anon_lock = page_lock_anon_vma_read, |
| 1969 | }; |
| 1970 | |
| 1971 | /* |
| 1972 | * Migration always ignores mlock and only supports TTU_RMAP_LOCKED and |
| 1973 | * TTU_SPLIT_HUGE_PMD and TTU_SYNC flags. |
| 1974 | */ |
| 1975 | if (WARN_ON_ONCE(flags & ~(TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD | |
| 1976 | TTU_SYNC))) |
| 1977 | return; |
| 1978 | |
Hugh Dickins | 6c855fc | 2021-07-07 13:13:33 -0700 | [diff] [blame] | 1979 | if (is_zone_device_page(page) && !is_device_private_page(page)) |
| 1980 | return; |
| 1981 | |
Joonsoo Kim | 5262950 | 2014-01-21 15:49:50 -0800 | [diff] [blame] | 1982 | /* |
| 1983 | * During exec, a temporary VMA is setup and later moved. |
| 1984 | * The VMA is moved under the anon_vma lock but not the |
| 1985 | * page tables leading to a race where migration cannot |
| 1986 | * find the migration ptes. Rather than increasing the |
| 1987 | * locking requirements of exec(), migration skips |
| 1988 | * temporary VMAs until after exec() completes. |
| 1989 | */ |
Alistair Popple | a98a2f0 | 2021-06-30 18:54:16 -0700 | [diff] [blame] | 1990 | if (!PageKsm(page) && PageAnon(page)) |
Joonsoo Kim | 5262950 | 2014-01-21 15:49:50 -0800 | [diff] [blame] | 1991 | rwc.invalid_vma = invalid_migration_vma; |
| 1992 | |
Kirill A. Shutemov | 2a52bcb | 2016-03-17 14:20:04 -0700 | [diff] [blame] | 1993 | if (flags & TTU_RMAP_LOCKED) |
Minchan Kim | 33fc80e | 2017-05-03 14:54:17 -0700 | [diff] [blame] | 1994 | rmap_walk_locked(page, &rwc); |
Kirill A. Shutemov | 2a52bcb | 2016-03-17 14:20:04 -0700 | [diff] [blame] | 1995 | else |
Minchan Kim | 33fc80e | 2017-05-03 14:54:17 -0700 | [diff] [blame] | 1996 | rmap_walk(page, &rwc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | } |
Nikita Danilov | 81b4082 | 2005-05-01 08:58:36 -0700 | [diff] [blame] | 1998 | |
Alistair Popple | cd62734 | 2021-06-30 18:54:12 -0700 | [diff] [blame] | 1999 | /* |
| 2000 | * Walks the vma's mapping a page and mlocks the page if any locked vma's are |
| 2001 | * found. Once one is found the page is locked and the scan can be terminated. |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 2002 | */ |
Alistair Popple | cd62734 | 2021-06-30 18:54:12 -0700 | [diff] [blame] | 2003 | static bool page_mlock_one(struct page *page, struct vm_area_struct *vma, |
| 2004 | unsigned long address, void *unused) |
| 2005 | { |
| 2006 | struct page_vma_mapped_walk pvmw = { |
| 2007 | .page = page, |
| 2008 | .vma = vma, |
| 2009 | .address = address, |
| 2010 | }; |
Minchan Kim | 854e9ed | 2016-01-15 16:54:53 -0800 | [diff] [blame] | 2011 | |
Alistair Popple | cd62734 | 2021-06-30 18:54:12 -0700 | [diff] [blame] | 2012 | /* An un-locked vma doesn't have any pages to lock, continue the scan */ |
| 2013 | if (!(vma->vm_flags & VM_LOCKED)) |
| 2014 | return true; |
| 2015 | |
| 2016 | while (page_vma_mapped_walk(&pvmw)) { |
| 2017 | /* |
| 2018 | * Need to recheck under the ptl to serialise with |
| 2019 | * __munlock_pagevec_fill() after VM_LOCKED is cleared in |
| 2020 | * munlock_vma_pages_range(). |
| 2021 | */ |
| 2022 | if (vma->vm_flags & VM_LOCKED) { |
Hugh Dickins | d9770fc | 2021-07-07 13:08:53 -0700 | [diff] [blame] | 2023 | /* |
Hugh Dickins | efdb672 | 2021-07-11 20:10:49 -0700 | [diff] [blame] | 2024 | * PTE-mapped THP are never marked as mlocked; but |
| 2025 | * this function is never called on a DoubleMap THP, |
| 2026 | * nor on an Anon THP (which may still be PTE-mapped |
| 2027 | * after DoubleMap was cleared). |
Hugh Dickins | d9770fc | 2021-07-07 13:08:53 -0700 | [diff] [blame] | 2028 | */ |
| 2029 | mlock_vma_page(page); |
Hugh Dickins | 023e1a8 | 2021-07-07 13:11:24 -0700 | [diff] [blame] | 2030 | /* |
| 2031 | * No need to scan further once the page is marked |
| 2032 | * as mlocked. |
| 2033 | */ |
Alistair Popple | cd62734 | 2021-06-30 18:54:12 -0700 | [diff] [blame] | 2034 | page_vma_mapped_walk_done(&pvmw); |
Hugh Dickins | 023e1a8 | 2021-07-07 13:11:24 -0700 | [diff] [blame] | 2035 | return false; |
Alistair Popple | cd62734 | 2021-06-30 18:54:12 -0700 | [diff] [blame] | 2036 | } |
Alistair Popple | cd62734 | 2021-06-30 18:54:12 -0700 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | return true; |
| 2040 | } |
| 2041 | |
| 2042 | /** |
| 2043 | * page_mlock - try to mlock a page |
| 2044 | * @page: the page to be mlocked |
| 2045 | * |
| 2046 | * Called from munlock code. Checks all of the VMAs mapping the page and mlocks |
| 2047 | * the page if any are found. The page will be returned with PG_mlocked cleared |
| 2048 | * if it is not mapped by any locked vmas. |
| 2049 | */ |
| 2050 | void page_mlock(struct page *page) |
Minchan Kim | 192d723 | 2017-05-03 14:54:10 -0700 | [diff] [blame] | 2051 | { |
Joonsoo Kim | e8351ac | 2014-01-21 15:49:52 -0800 | [diff] [blame] | 2052 | struct rmap_walk_control rwc = { |
Alistair Popple | cd62734 | 2021-06-30 18:54:12 -0700 | [diff] [blame] | 2053 | .rmap_one = page_mlock_one, |
Joonsoo Kim | e8351ac | 2014-01-21 15:49:52 -0800 | [diff] [blame] | 2054 | .done = page_not_mapped, |
Joonsoo Kim | e8351ac | 2014-01-21 15:49:52 -0800 | [diff] [blame] | 2055 | .anon_lock = page_lock_anon_vma_read, |
| 2056 | |
| 2057 | }; |
| 2058 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 2059 | VM_BUG_ON_PAGE(!PageLocked(page) || PageLRU(page), page); |
Minchan Kim | 192d723 | 2017-05-03 14:54:10 -0700 | [diff] [blame] | 2060 | VM_BUG_ON_PAGE(PageCompound(page) && PageDoubleMap(page), page); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 2061 | |
Hugh Dickins | efdb672 | 2021-07-11 20:10:49 -0700 | [diff] [blame] | 2062 | /* Anon THP are only marked as mlocked when singly mapped */ |
| 2063 | if (PageTransCompound(page) && PageAnon(page)) |
| 2064 | return; |
| 2065 | |
Minchan Kim | 192d723 | 2017-05-03 14:54:10 -0700 | [diff] [blame] | 2066 | rmap_walk(page, &rwc); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 2067 | } |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2068 | |
Alistair Popple | b756a3b | 2021-06-30 18:54:25 -0700 | [diff] [blame] | 2069 | #ifdef CONFIG_DEVICE_PRIVATE |
| 2070 | struct make_exclusive_args { |
| 2071 | struct mm_struct *mm; |
| 2072 | unsigned long address; |
| 2073 | void *owner; |
| 2074 | bool valid; |
| 2075 | }; |
| 2076 | |
| 2077 | static bool page_make_device_exclusive_one(struct page *page, |
| 2078 | struct vm_area_struct *vma, unsigned long address, void *priv) |
| 2079 | { |
| 2080 | struct mm_struct *mm = vma->vm_mm; |
| 2081 | struct page_vma_mapped_walk pvmw = { |
| 2082 | .page = page, |
| 2083 | .vma = vma, |
| 2084 | .address = address, |
| 2085 | }; |
| 2086 | struct make_exclusive_args *args = priv; |
| 2087 | pte_t pteval; |
| 2088 | struct page *subpage; |
| 2089 | bool ret = true; |
| 2090 | struct mmu_notifier_range range; |
| 2091 | swp_entry_t entry; |
| 2092 | pte_t swp_pte; |
| 2093 | |
| 2094 | mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, vma, |
| 2095 | vma->vm_mm, address, min(vma->vm_end, |
| 2096 | address + page_size(page)), args->owner); |
| 2097 | mmu_notifier_invalidate_range_start(&range); |
| 2098 | |
| 2099 | while (page_vma_mapped_walk(&pvmw)) { |
| 2100 | /* Unexpected PMD-mapped THP? */ |
| 2101 | VM_BUG_ON_PAGE(!pvmw.pte, page); |
| 2102 | |
| 2103 | if (!pte_present(*pvmw.pte)) { |
| 2104 | ret = false; |
| 2105 | page_vma_mapped_walk_done(&pvmw); |
| 2106 | break; |
| 2107 | } |
| 2108 | |
| 2109 | subpage = page - page_to_pfn(page) + pte_pfn(*pvmw.pte); |
| 2110 | address = pvmw.address; |
| 2111 | |
| 2112 | /* Nuke the page table entry. */ |
| 2113 | flush_cache_page(vma, address, pte_pfn(*pvmw.pte)); |
| 2114 | pteval = ptep_clear_flush(vma, address, pvmw.pte); |
| 2115 | |
| 2116 | /* Move the dirty bit to the page. Now the pte is gone. */ |
| 2117 | if (pte_dirty(pteval)) |
| 2118 | set_page_dirty(page); |
| 2119 | |
| 2120 | /* |
| 2121 | * Check that our target page is still mapped at the expected |
| 2122 | * address. |
| 2123 | */ |
| 2124 | if (args->mm == mm && args->address == address && |
| 2125 | pte_write(pteval)) |
| 2126 | args->valid = true; |
| 2127 | |
| 2128 | /* |
| 2129 | * Store the pfn of the page in a special migration |
| 2130 | * pte. do_swap_page() will wait until the migration |
| 2131 | * pte is removed and then restart fault handling. |
| 2132 | */ |
| 2133 | if (pte_write(pteval)) |
| 2134 | entry = make_writable_device_exclusive_entry( |
| 2135 | page_to_pfn(subpage)); |
| 2136 | else |
| 2137 | entry = make_readable_device_exclusive_entry( |
| 2138 | page_to_pfn(subpage)); |
| 2139 | swp_pte = swp_entry_to_pte(entry); |
| 2140 | if (pte_soft_dirty(pteval)) |
| 2141 | swp_pte = pte_swp_mksoft_dirty(swp_pte); |
| 2142 | if (pte_uffd_wp(pteval)) |
| 2143 | swp_pte = pte_swp_mkuffd_wp(swp_pte); |
| 2144 | |
| 2145 | set_pte_at(mm, address, pvmw.pte, swp_pte); |
| 2146 | |
| 2147 | /* |
| 2148 | * There is a reference on the page for the swap entry which has |
| 2149 | * been removed, so shouldn't take another. |
| 2150 | */ |
| 2151 | page_remove_rmap(subpage, false); |
| 2152 | } |
| 2153 | |
| 2154 | mmu_notifier_invalidate_range_end(&range); |
| 2155 | |
| 2156 | return ret; |
| 2157 | } |
| 2158 | |
| 2159 | /** |
| 2160 | * page_make_device_exclusive - mark the page exclusively owned by a device |
| 2161 | * @page: the page to replace page table entries for |
| 2162 | * @mm: the mm_struct where the page is expected to be mapped |
| 2163 | * @address: address where the page is expected to be mapped |
| 2164 | * @owner: passed to MMU_NOTIFY_EXCLUSIVE range notifier callbacks |
| 2165 | * |
| 2166 | * Tries to remove all the page table entries which are mapping this page and |
| 2167 | * replace them with special device exclusive swap entries to grant a device |
| 2168 | * exclusive access to the page. Caller must hold the page lock. |
| 2169 | * |
| 2170 | * Returns false if the page is still mapped, or if it could not be unmapped |
| 2171 | * from the expected address. Otherwise returns true (success). |
| 2172 | */ |
| 2173 | static bool page_make_device_exclusive(struct page *page, struct mm_struct *mm, |
| 2174 | unsigned long address, void *owner) |
| 2175 | { |
| 2176 | struct make_exclusive_args args = { |
| 2177 | .mm = mm, |
| 2178 | .address = address, |
| 2179 | .owner = owner, |
| 2180 | .valid = false, |
| 2181 | }; |
| 2182 | struct rmap_walk_control rwc = { |
| 2183 | .rmap_one = page_make_device_exclusive_one, |
| 2184 | .done = page_not_mapped, |
| 2185 | .anon_lock = page_lock_anon_vma_read, |
| 2186 | .arg = &args, |
| 2187 | }; |
| 2188 | |
| 2189 | /* |
| 2190 | * Restrict to anonymous pages for now to avoid potential writeback |
| 2191 | * issues. Also tail pages shouldn't be passed to rmap_walk so skip |
| 2192 | * those. |
| 2193 | */ |
| 2194 | if (!PageAnon(page) || PageTail(page)) |
| 2195 | return false; |
| 2196 | |
| 2197 | rmap_walk(page, &rwc); |
| 2198 | |
| 2199 | return args.valid && !page_mapcount(page); |
| 2200 | } |
| 2201 | |
| 2202 | /** |
| 2203 | * make_device_exclusive_range() - Mark a range for exclusive use by a device |
| 2204 | * @mm: mm_struct of assoicated target process |
| 2205 | * @start: start of the region to mark for exclusive device access |
| 2206 | * @end: end address of region |
| 2207 | * @pages: returns the pages which were successfully marked for exclusive access |
| 2208 | * @owner: passed to MMU_NOTIFY_EXCLUSIVE range notifier to allow filtering |
| 2209 | * |
| 2210 | * Returns: number of pages found in the range by GUP. A page is marked for |
| 2211 | * exclusive access only if the page pointer is non-NULL. |
| 2212 | * |
| 2213 | * This function finds ptes mapping page(s) to the given address range, locks |
| 2214 | * them and replaces mappings with special swap entries preventing userspace CPU |
| 2215 | * access. On fault these entries are replaced with the original mapping after |
| 2216 | * calling MMU notifiers. |
| 2217 | * |
| 2218 | * A driver using this to program access from a device must use a mmu notifier |
| 2219 | * critical section to hold a device specific lock during programming. Once |
| 2220 | * programming is complete it should drop the page lock and reference after |
| 2221 | * which point CPU access to the page will revoke the exclusive access. |
| 2222 | */ |
| 2223 | int make_device_exclusive_range(struct mm_struct *mm, unsigned long start, |
| 2224 | unsigned long end, struct page **pages, |
| 2225 | void *owner) |
| 2226 | { |
| 2227 | long npages = (end - start) >> PAGE_SHIFT; |
| 2228 | long i; |
| 2229 | |
| 2230 | npages = get_user_pages_remote(mm, start, npages, |
| 2231 | FOLL_GET | FOLL_WRITE | FOLL_SPLIT_PMD, |
| 2232 | pages, NULL, NULL); |
| 2233 | if (npages < 0) |
| 2234 | return npages; |
| 2235 | |
| 2236 | for (i = 0; i < npages; i++, start += PAGE_SIZE) { |
| 2237 | if (!trylock_page(pages[i])) { |
| 2238 | put_page(pages[i]); |
| 2239 | pages[i] = NULL; |
| 2240 | continue; |
| 2241 | } |
| 2242 | |
| 2243 | if (!page_make_device_exclusive(pages[i], mm, start, owner)) { |
| 2244 | unlock_page(pages[i]); |
| 2245 | put_page(pages[i]); |
| 2246 | pages[i] = NULL; |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | return npages; |
| 2251 | } |
| 2252 | EXPORT_SYMBOL_GPL(make_device_exclusive_range); |
| 2253 | #endif |
| 2254 | |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 2255 | void __put_anon_vma(struct anon_vma *anon_vma) |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 2256 | { |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 2257 | struct anon_vma *root = anon_vma->root; |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 2258 | |
Andrey Ryabinin | 624483f | 2014-06-06 19:09:30 +0400 | [diff] [blame] | 2259 | anon_vma_free(anon_vma); |
Peter Zijlstra | 01d8b20 | 2011-03-22 16:32:49 -0700 | [diff] [blame] | 2260 | if (root != anon_vma && atomic_dec_and_test(&root->refcount)) |
| 2261 | anon_vma_free(root); |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 2262 | } |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 2263 | |
Joonsoo Kim | 0dd1c7b | 2014-01-21 15:49:49 -0800 | [diff] [blame] | 2264 | static struct anon_vma *rmap_walk_anon_lock(struct page *page, |
| 2265 | struct rmap_walk_control *rwc) |
Joonsoo Kim | faecd8d | 2014-01-21 15:49:46 -0800 | [diff] [blame] | 2266 | { |
| 2267 | struct anon_vma *anon_vma; |
| 2268 | |
Joonsoo Kim | 0dd1c7b | 2014-01-21 15:49:49 -0800 | [diff] [blame] | 2269 | if (rwc->anon_lock) |
| 2270 | return rwc->anon_lock(page); |
| 2271 | |
Joonsoo Kim | faecd8d | 2014-01-21 15:49:46 -0800 | [diff] [blame] | 2272 | /* |
| 2273 | * Note: remove_migration_ptes() cannot use page_lock_anon_vma_read() |
| 2274 | * because that depends on page_mapped(); but not all its usages |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2275 | * are holding mmap_lock. Users without mmap_lock are required to |
Joonsoo Kim | faecd8d | 2014-01-21 15:49:46 -0800 | [diff] [blame] | 2276 | * take a reference count to prevent the anon_vma disappearing |
| 2277 | */ |
| 2278 | anon_vma = page_anon_vma(page); |
| 2279 | if (!anon_vma) |
| 2280 | return NULL; |
| 2281 | |
| 2282 | anon_vma_lock_read(anon_vma); |
| 2283 | return anon_vma; |
| 2284 | } |
| 2285 | |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2286 | /* |
Joonsoo Kim | e8351ac | 2014-01-21 15:49:52 -0800 | [diff] [blame] | 2287 | * rmap_walk_anon - do something to anonymous page using the object-based |
| 2288 | * rmap method |
| 2289 | * @page: the page to be handled |
| 2290 | * @rwc: control variable according to each walk type |
| 2291 | * |
| 2292 | * Find all the mappings of a page using the mapping pointer and the vma chains |
| 2293 | * contained in the anon_vma struct it points to. |
| 2294 | * |
Alistair Popple | cd62734 | 2021-06-30 18:54:12 -0700 | [diff] [blame] | 2295 | * When called from page_mlock(), the mmap_lock of the mm containing the vma |
Joonsoo Kim | e8351ac | 2014-01-21 15:49:52 -0800 | [diff] [blame] | 2296 | * where the page was found will be held for write. So, we won't recheck |
| 2297 | * vm_flags for that VMA. That should be OK, because that vma shouldn't be |
| 2298 | * LOCKED. |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2299 | */ |
Minchan Kim | 1df631a | 2017-05-03 14:54:23 -0700 | [diff] [blame] | 2300 | static void rmap_walk_anon(struct page *page, struct rmap_walk_control *rwc, |
Kirill A. Shutemov | b977319 | 2016-03-17 14:20:01 -0700 | [diff] [blame] | 2301 | bool locked) |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2302 | { |
| 2303 | struct anon_vma *anon_vma; |
Kirill A. Shutemov | a8fa41ad | 2017-02-24 14:57:54 -0800 | [diff] [blame] | 2304 | pgoff_t pgoff_start, pgoff_end; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 2305 | struct anon_vma_chain *avc; |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2306 | |
Kirill A. Shutemov | b977319 | 2016-03-17 14:20:01 -0700 | [diff] [blame] | 2307 | if (locked) { |
| 2308 | anon_vma = page_anon_vma(page); |
| 2309 | /* anon_vma disappear under us? */ |
| 2310 | VM_BUG_ON_PAGE(!anon_vma, page); |
| 2311 | } else { |
| 2312 | anon_vma = rmap_walk_anon_lock(page, rwc); |
| 2313 | } |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2314 | if (!anon_vma) |
Minchan Kim | 1df631a | 2017-05-03 14:54:23 -0700 | [diff] [blame] | 2315 | return; |
Joonsoo Kim | faecd8d | 2014-01-21 15:49:46 -0800 | [diff] [blame] | 2316 | |
Kirill A. Shutemov | a8fa41ad | 2017-02-24 14:57:54 -0800 | [diff] [blame] | 2317 | pgoff_start = page_to_pgoff(page); |
Matthew Wilcox (Oracle) | 6c35784 | 2020-08-14 17:30:37 -0700 | [diff] [blame] | 2318 | pgoff_end = pgoff_start + thp_nr_pages(page) - 1; |
Kirill A. Shutemov | a8fa41ad | 2017-02-24 14:57:54 -0800 | [diff] [blame] | 2319 | anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, |
| 2320 | pgoff_start, pgoff_end) { |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 2321 | struct vm_area_struct *vma = avc->vma; |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2322 | unsigned long address = vma_address(page, vma); |
Joonsoo Kim | 0dd1c7b | 2014-01-21 15:49:49 -0800 | [diff] [blame] | 2323 | |
Hugh Dickins | 494334e | 2021-06-15 18:23:56 -0700 | [diff] [blame] | 2324 | VM_BUG_ON_VMA(address == -EFAULT, vma); |
Andrea Arcangeli | ad12695 | 2015-11-05 18:49:07 -0800 | [diff] [blame] | 2325 | cond_resched(); |
| 2326 | |
Joonsoo Kim | 0dd1c7b | 2014-01-21 15:49:49 -0800 | [diff] [blame] | 2327 | if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg)) |
| 2328 | continue; |
| 2329 | |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 2330 | if (!rwc->rmap_one(page, vma, address, rwc->arg)) |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2331 | break; |
Joonsoo Kim | 0dd1c7b | 2014-01-21 15:49:49 -0800 | [diff] [blame] | 2332 | if (rwc->done && rwc->done(page)) |
| 2333 | break; |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2334 | } |
Kirill A. Shutemov | b977319 | 2016-03-17 14:20:01 -0700 | [diff] [blame] | 2335 | |
| 2336 | if (!locked) |
| 2337 | anon_vma_unlock_read(anon_vma); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2338 | } |
| 2339 | |
Joonsoo Kim | e8351ac | 2014-01-21 15:49:52 -0800 | [diff] [blame] | 2340 | /* |
| 2341 | * rmap_walk_file - do something to file page using the object-based rmap method |
| 2342 | * @page: the page to be handled |
| 2343 | * @rwc: control variable according to each walk type |
| 2344 | * |
| 2345 | * Find all the mappings of a page using the mapping pointer and the vma chains |
| 2346 | * contained in the address_space struct it points to. |
| 2347 | * |
Alistair Popple | cd62734 | 2021-06-30 18:54:12 -0700 | [diff] [blame] | 2348 | * When called from page_mlock(), the mmap_lock of the mm containing the vma |
Joonsoo Kim | e8351ac | 2014-01-21 15:49:52 -0800 | [diff] [blame] | 2349 | * where the page was found will be held for write. So, we won't recheck |
| 2350 | * vm_flags for that VMA. That should be OK, because that vma shouldn't be |
| 2351 | * LOCKED. |
| 2352 | */ |
Minchan Kim | 1df631a | 2017-05-03 14:54:23 -0700 | [diff] [blame] | 2353 | static void rmap_walk_file(struct page *page, struct rmap_walk_control *rwc, |
Kirill A. Shutemov | b977319 | 2016-03-17 14:20:01 -0700 | [diff] [blame] | 2354 | bool locked) |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2355 | { |
Kirill A. Shutemov | b977319 | 2016-03-17 14:20:01 -0700 | [diff] [blame] | 2356 | struct address_space *mapping = page_mapping(page); |
Kirill A. Shutemov | a8fa41ad | 2017-02-24 14:57:54 -0800 | [diff] [blame] | 2357 | pgoff_t pgoff_start, pgoff_end; |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2358 | struct vm_area_struct *vma; |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2359 | |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 2360 | /* |
| 2361 | * The page lock not only makes sure that page->mapping cannot |
| 2362 | * suddenly be NULLified by truncation, it makes sure that the |
| 2363 | * structure at mapping cannot be freed and reused yet, |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 2364 | * so we can safely take mapping->i_mmap_rwsem. |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 2365 | */ |
Sasha Levin | 81d1b09 | 2014-10-09 15:28:10 -0700 | [diff] [blame] | 2366 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
Joonsoo Kim | 9f32624 | 2014-01-21 15:49:53 -0800 | [diff] [blame] | 2367 | |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2368 | if (!mapping) |
Minchan Kim | 1df631a | 2017-05-03 14:54:23 -0700 | [diff] [blame] | 2369 | return; |
Davidlohr Bueso | 3dec0ba | 2014-12-12 16:54:27 -0800 | [diff] [blame] | 2370 | |
Kirill A. Shutemov | a8fa41ad | 2017-02-24 14:57:54 -0800 | [diff] [blame] | 2371 | pgoff_start = page_to_pgoff(page); |
Matthew Wilcox (Oracle) | 6c35784 | 2020-08-14 17:30:37 -0700 | [diff] [blame] | 2372 | pgoff_end = pgoff_start + thp_nr_pages(page) - 1; |
Kirill A. Shutemov | b977319 | 2016-03-17 14:20:01 -0700 | [diff] [blame] | 2373 | if (!locked) |
| 2374 | i_mmap_lock_read(mapping); |
Kirill A. Shutemov | a8fa41ad | 2017-02-24 14:57:54 -0800 | [diff] [blame] | 2375 | vma_interval_tree_foreach(vma, &mapping->i_mmap, |
| 2376 | pgoff_start, pgoff_end) { |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2377 | unsigned long address = vma_address(page, vma); |
Joonsoo Kim | 0dd1c7b | 2014-01-21 15:49:49 -0800 | [diff] [blame] | 2378 | |
Hugh Dickins | 494334e | 2021-06-15 18:23:56 -0700 | [diff] [blame] | 2379 | VM_BUG_ON_VMA(address == -EFAULT, vma); |
Andrea Arcangeli | ad12695 | 2015-11-05 18:49:07 -0800 | [diff] [blame] | 2380 | cond_resched(); |
| 2381 | |
Joonsoo Kim | 0dd1c7b | 2014-01-21 15:49:49 -0800 | [diff] [blame] | 2382 | if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg)) |
| 2383 | continue; |
| 2384 | |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 2385 | if (!rwc->rmap_one(page, vma, address, rwc->arg)) |
Joonsoo Kim | 0dd1c7b | 2014-01-21 15:49:49 -0800 | [diff] [blame] | 2386 | goto done; |
| 2387 | if (rwc->done && rwc->done(page)) |
| 2388 | goto done; |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2389 | } |
Joonsoo Kim | 0dd1c7b | 2014-01-21 15:49:49 -0800 | [diff] [blame] | 2390 | |
Joonsoo Kim | 0dd1c7b | 2014-01-21 15:49:49 -0800 | [diff] [blame] | 2391 | done: |
Kirill A. Shutemov | b977319 | 2016-03-17 14:20:01 -0700 | [diff] [blame] | 2392 | if (!locked) |
| 2393 | i_mmap_unlock_read(mapping); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2394 | } |
| 2395 | |
Minchan Kim | 1df631a | 2017-05-03 14:54:23 -0700 | [diff] [blame] | 2396 | void rmap_walk(struct page *page, struct rmap_walk_control *rwc) |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2397 | { |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2398 | if (unlikely(PageKsm(page))) |
Minchan Kim | 1df631a | 2017-05-03 14:54:23 -0700 | [diff] [blame] | 2399 | rmap_walk_ksm(page, rwc); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2400 | else if (PageAnon(page)) |
Minchan Kim | 1df631a | 2017-05-03 14:54:23 -0700 | [diff] [blame] | 2401 | rmap_walk_anon(page, rwc, false); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2402 | else |
Minchan Kim | 1df631a | 2017-05-03 14:54:23 -0700 | [diff] [blame] | 2403 | rmap_walk_file(page, rwc, false); |
Kirill A. Shutemov | b977319 | 2016-03-17 14:20:01 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | /* Like rmap_walk, but caller holds relevant rmap lock */ |
Minchan Kim | 1df631a | 2017-05-03 14:54:23 -0700 | [diff] [blame] | 2407 | void rmap_walk_locked(struct page *page, struct rmap_walk_control *rwc) |
Kirill A. Shutemov | b977319 | 2016-03-17 14:20:01 -0700 | [diff] [blame] | 2408 | { |
| 2409 | /* no ksm support for now */ |
| 2410 | VM_BUG_ON_PAGE(PageKsm(page), page); |
| 2411 | if (PageAnon(page)) |
Minchan Kim | 1df631a | 2017-05-03 14:54:23 -0700 | [diff] [blame] | 2412 | rmap_walk_anon(page, rwc, true); |
Kirill A. Shutemov | b977319 | 2016-03-17 14:20:01 -0700 | [diff] [blame] | 2413 | else |
Minchan Kim | 1df631a | 2017-05-03 14:54:23 -0700 | [diff] [blame] | 2414 | rmap_walk_file(page, rwc, true); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 2415 | } |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 2416 | |
Naoya Horiguchi | e3390f6 | 2010-06-15 13:18:13 +0900 | [diff] [blame] | 2417 | #ifdef CONFIG_HUGETLB_PAGE |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 2418 | /* |
Kirill Tkhai | 451b951 | 2018-12-28 00:39:31 -0800 | [diff] [blame] | 2419 | * The following two functions are for anonymous (private mapped) hugepages. |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 2420 | * Unlike common anonymous pages, anonymous hugepages have no accounting code |
| 2421 | * and no lru code, because we handle hugepages differently from common pages. |
| 2422 | */ |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 2423 | void hugepage_add_anon_rmap(struct page *page, |
| 2424 | struct vm_area_struct *vma, unsigned long address) |
| 2425 | { |
| 2426 | struct anon_vma *anon_vma = vma->anon_vma; |
| 2427 | int first; |
Naoya Horiguchi | a850ea3 | 2010-09-10 13:23:06 +0900 | [diff] [blame] | 2428 | |
| 2429 | BUG_ON(!PageLocked(page)); |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 2430 | BUG_ON(!anon_vma); |
Hugh Dickins | 5dbe0af | 2011-05-28 13:17:04 -0700 | [diff] [blame] | 2431 | /* address might be in next vma when migration races vma_adjust */ |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 2432 | first = atomic_inc_and_test(compound_mapcount_ptr(page)); |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 2433 | if (first) |
Kirill Tkhai | 451b951 | 2018-12-28 00:39:31 -0800 | [diff] [blame] | 2434 | __page_set_anon_rmap(page, vma, address, 0); |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | void hugepage_add_new_anon_rmap(struct page *page, |
| 2438 | struct vm_area_struct *vma, unsigned long address) |
| 2439 | { |
| 2440 | BUG_ON(address < vma->vm_start || address >= vma->vm_end); |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 2441 | atomic_set(compound_mapcount_ptr(page), 0); |
John Hubbard | 47e29d3 | 2020-04-01 21:05:33 -0700 | [diff] [blame] | 2442 | if (hpage_pincount_available(page)) |
| 2443 | atomic_set(compound_pincount_ptr(page), 0); |
| 2444 | |
Kirill Tkhai | 451b951 | 2018-12-28 00:39:31 -0800 | [diff] [blame] | 2445 | __page_set_anon_rmap(page, vma, address, 1); |
Naoya Horiguchi | 0fe6e20 | 2010-05-28 09:29:16 +0900 | [diff] [blame] | 2446 | } |
Naoya Horiguchi | e3390f6 | 2010-06-15 13:18:13 +0900 | [diff] [blame] | 2447 | #endif /* CONFIG_HUGETLB_PAGE */ |