blob: 6aebd1747251212fc42e0681c216382f4fce8045 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Dickins98f32602009-05-21 20:33:58 +010017 * Contributions by Hugh Dickins 2003, 2004
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
20/*
21 * Lock ordering in mm:
22 *
Jan Kara96087032021-04-12 15:50:21 +020023 * inode->i_rwsem (while writing or truncating, not reading or faulting)
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070024 * mm->mmap_lock
Jan Kara730633f2021-01-28 19:19:45 +010025 * 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)
37 * lruvec->lru_lock (in lock_page_lruvec_irq)
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 Kleen6a460792009-09-16 11:50:15 +020044 *
Jan Kara96087032021-04-12 15:50:21 +020045 * anon_vma->rwsem,mapping->i_mmap_rwsem (memory_failure, collect_procs_anon)
Peter Zijlstra9b679322011-06-27 16:18:09 -070046 * ->tasklist_lock
Andi Kleen6a460792009-09-16 11:50:15 +020047 * pte map lock
Mike Kravetzc0d03812020-04-01 21:11:05 -070048 *
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 Torvalds1da177e2005-04-16 15:20:36 -070053 */
54
55#include <linux/mm.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010056#include <linux/sched/mm.h>
Ingo Molnar29930022017-02-08 18:51:36 +010057#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#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 Dickins5ad64682009-12-14 17:59:24 -080063#include <linux/ksm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/rmap.h>
65#include <linux/rcupdate.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040066#include <linux/export.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080067#include <linux/memcontrol.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070068#include <linux/mmu_notifier.h>
KOSAKI Motohiro64cdd542009-01-06 14:39:16 -080069#include <linux/migrate.h>
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +090070#include <linux/hugetlb.h>
Ben Dooks444f84f2019-10-18 20:20:17 -070071#include <linux/huge_mm.h>
Jan Karaef5d4372012-10-25 13:37:31 -070072#include <linux/backing-dev.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070073#include <linux/page_idle.h>
Jérôme Glissea5430dd2017-09-08 16:12:17 -070074#include <linux/memremap.h>
Christian Borntraegerbce73e42018-07-13 16:58:52 -070075#include <linux/userfaultfd_k.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#include <asm/tlbflush.h>
78
Mel Gorman72b252a2015-09-04 15:47:32 -070079#include <trace/events/tlb.h>
80
Nick Pigginb291f002008-10-18 20:26:44 -070081#include "internal.h"
82
Adrian Bunkfdd2e5f2008-10-18 20:28:38 -070083static struct kmem_cache *anon_vma_cachep;
Rik van Riel5beb4932010-03-05 13:42:07 -080084static struct kmem_cache *anon_vma_chain_cachep;
Adrian Bunkfdd2e5f2008-10-18 20:28:38 -070085
86static inline struct anon_vma *anon_vma_alloc(void)
87{
Peter Zijlstra01d8b202011-03-22 16:32:49 -070088 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 Khlebnikov7a3ef202015-01-08 14:32:15 -080093 anon_vma->degree = 1; /* Reference for first vma */
94 anon_vma->parent = anon_vma;
Peter Zijlstra01d8b202011-03-22 16:32:49 -070095 /*
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 Bunkfdd2e5f2008-10-18 20:28:38 -0700103}
104
Peter Zijlstra01d8b202011-03-22 16:32:49 -0700105static inline void anon_vma_free(struct anon_vma *anon_vma)
Adrian Bunkfdd2e5f2008-10-18 20:28:38 -0700106{
Peter Zijlstra01d8b202011-03-22 16:32:49 -0700107 VM_BUG_ON(atomic_read(&anon_vma->refcount));
Peter Zijlstra88c22082011-05-24 17:12:13 -0700108
109 /*
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000110 * Synchronize against page_lock_anon_vma_read() such that
Peter Zijlstra88c22082011-05-24 17:12:13 -0700111 * 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 Molnar4fc3f1d2012-12-02 19:56:50 +0000116 * down_read_trylock() from page_lock_anon_vma_read(). This orders:
Peter Zijlstra88c22082011-05-24 17:12:13 -0700117 *
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000118 * page_lock_anon_vma_read() VS put_anon_vma()
119 * down_read_trylock() atomic_dec_and_test()
Peter Zijlstra88c22082011-05-24 17:12:13 -0700120 * LOCK MB
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000121 * atomic_read() rwsem_is_locked()
Peter Zijlstra88c22082011-05-24 17:12:13 -0700122 *
123 * LOCK should suffice since the actual taking of the lock must
124 * happen _before_ what follows.
125 */
Hugh Dickins7f39dda2014-06-04 16:05:33 -0700126 might_sleep();
Ingo Molnar5a505082012-12-02 19:56:46 +0000127 if (rwsem_is_locked(&anon_vma->root->rwsem)) {
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000128 anon_vma_lock_write(anon_vma);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -0800129 anon_vma_unlock_write(anon_vma);
Peter Zijlstra88c22082011-05-24 17:12:13 -0700130 }
131
Adrian Bunkfdd2e5f2008-10-18 20:28:38 -0700132 kmem_cache_free(anon_vma_cachep, anon_vma);
133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvaldsdd347392011-06-17 19:05:36 -0700135static inline struct anon_vma_chain *anon_vma_chain_alloc(gfp_t gfp)
Rik van Riel5beb4932010-03-05 13:42:07 -0800136{
Linus Torvaldsdd347392011-06-17 19:05:36 -0700137 return kmem_cache_alloc(anon_vma_chain_cachep, gfp);
Rik van Riel5beb4932010-03-05 13:42:07 -0800138}
139
Namhyung Kime574b5f2010-10-26 14:22:02 -0700140static void anon_vma_chain_free(struct anon_vma_chain *anon_vma_chain)
Rik van Riel5beb4932010-03-05 13:42:07 -0800141{
142 kmem_cache_free(anon_vma_chain_cachep, anon_vma_chain);
143}
144
Kautuk Consul6583a842012-03-21 16:34:01 -0700145static 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 Lespinassebf181b92012-10-08 16:31:39 -0700152 anon_vma_interval_tree_insert(avc, &anon_vma->rb_root);
Kautuk Consul6583a842012-03-21 16:34:01 -0700153}
154
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700155/**
Vlastimil Babkad5a187d2016-12-12 16:44:38 -0800156 * __anon_vma_prepare - attach an anon_vma to a memory region
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700157 * @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 Babkad5a187d2016-12-12 16:44:38 -0800163 * The common case will be that we already have one, which
164 * is handled inline by anon_vma_prepare(). But if
Figo.zhang23a07902010-12-27 15:14:06 +0100165 * not we either need to find an adjacent mapping that we
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700166 * 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 Molnar4fc3f1d2012-12-02 19:56:50 +0000171 * optimistically looked up an anon_vma in page_lock_anon_vma_read()
Miaohe Linaaf1f992021-02-25 17:17:53 -0800172 * and that may actually touch the rwsem even in the newly
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700173 * 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 Lespinassec1e8d7c2020-06-08 21:33:54 -0700181 * This must be called with the mmap_lock held for reading.
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700182 */
Vlastimil Babkad5a187d2016-12-12 16:44:38 -0800183int __anon_vma_prepare(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Vlastimil Babkad5a187d2016-12-12 16:44:38 -0800185 struct mm_struct *mm = vma->vm_mm;
186 struct anon_vma *anon_vma, *allocated;
Rik van Riel5beb4932010-03-05 13:42:07 -0800187 struct anon_vma_chain *avc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Vlastimil Babkad5a187d2016-12-12 16:44:38 -0800191 avc = anon_vma_chain_alloc(GFP_KERNEL);
192 if (!avc)
193 goto out_enomem;
Rik van Riel5beb4932010-03-05 13:42:07 -0800194
Vlastimil Babkad5a187d2016-12-12 16:44:38 -0800195 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 Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Vlastimil Babkad5a187d2016-12-12 16:44:38 -0800203
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 Torvalds1da177e2005-04-16 15:20:36 -0700223 return 0;
Rik van Riel5beb4932010-03-05 13:42:07 -0800224
225 out_enomem_free_avc:
226 anon_vma_chain_free(avc);
227 out_enomem:
228 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Linus Torvaldsbb4aa392011-06-16 20:44:51 -0700231/*
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 */
239static 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 Molnar5a505082012-12-02 19:56:46 +0000244 up_write(&root->rwsem);
Linus Torvaldsbb4aa392011-06-16 20:44:51 -0700245 root = new_root;
Ingo Molnar5a505082012-12-02 19:56:46 +0000246 down_write(&root->rwsem);
Linus Torvaldsbb4aa392011-06-16 20:44:51 -0700247 }
248 return root;
249}
250
251static inline void unlock_anon_vma_root(struct anon_vma *root)
252{
253 if (root)
Ingo Molnar5a505082012-12-02 19:56:46 +0000254 up_write(&root->rwsem);
Linus Torvaldsbb4aa392011-06-16 20:44:51 -0700255}
256
Rik van Riel5beb4932010-03-05 13:42:07 -0800257/*
258 * Attach the anon_vmas from src to dst.
259 * Returns 0 on success, -ENOMEM on failure.
Konstantin Khlebnikov7a3ef202015-01-08 14:32:15 -0800260 *
Shijie Luocb152a12021-05-06 18:05:51 -0700261 * anon_vma_clone() is called by __vma_adjust(), __split_vma(), copy_vma() and
Wei Yang47b390d2019-11-30 17:50:56 -0800262 * 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 Riel5beb4932010-03-05 13:42:07 -0800274 */
275int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Rik van Riel5beb4932010-03-05 13:42:07 -0800277 struct anon_vma_chain *avc, *pavc;
Linus Torvaldsbb4aa392011-06-16 20:44:51 -0700278 struct anon_vma *root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds646d87b2010-04-11 17:15:03 -0700280 list_for_each_entry_reverse(pavc, &src->anon_vma_chain, same_vma) {
Linus Torvaldsbb4aa392011-06-16 20:44:51 -0700281 struct anon_vma *anon_vma;
282
Linus Torvaldsdd347392011-06-17 19:05:36 -0700283 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 Torvaldsbb4aa392011-06-16 20:44:51 -0700291 anon_vma = pavc->anon_vma;
292 root = lock_anon_vma_root(root, anon_vma);
293 anon_vma_chain_link(dst, avc, anon_vma);
Konstantin Khlebnikov7a3ef202015-01-08 14:32:15 -0800294
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 Yang47b390d2019-11-30 17:50:56 -0800303 if (!dst->anon_vma && src->anon_vma &&
304 anon_vma != src->anon_vma && anon_vma->degree < 2)
Konstantin Khlebnikov7a3ef202015-01-08 14:32:15 -0800305 dst->anon_vma = anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Konstantin Khlebnikov7a3ef202015-01-08 14:32:15 -0800307 if (dst->anon_vma)
308 dst->anon_vma->degree++;
Linus Torvaldsbb4aa392011-06-16 20:44:51 -0700309 unlock_anon_vma_root(root);
Rik van Riel5beb4932010-03-05 13:42:07 -0800310 return 0;
311
312 enomem_failure:
Leon Yu3fe89b32015-03-25 15:55:11 -0700313 /*
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 Riel5beb4932010-03-05 13:42:07 -0800320 unlink_anon_vmas(dst);
321 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Rik van Riel5beb4932010-03-05 13:42:07 -0800324/*
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 */
329int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Rik van Riel5beb4932010-03-05 13:42:07 -0800331 struct anon_vma_chain *avc;
332 struct anon_vma *anon_vma;
Daniel Forrestc4ea95d2014-12-02 15:59:42 -0800333 int error;
Rik van Riel5beb4932010-03-05 13:42:07 -0800334
335 /* Don't bother if the parent process has no anon_vma here. */
336 if (!pvma->anon_vma)
337 return 0;
338
Konstantin Khlebnikov7a3ef202015-01-08 14:32:15 -0800339 /* Drop inherited anon_vma, we'll reuse existing or allocate new. */
340 vma->anon_vma = NULL;
341
Rik van Riel5beb4932010-03-05 13:42:07 -0800342 /*
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 Forrestc4ea95d2014-12-02 15:59:42 -0800346 error = anon_vma_clone(vma, pvma);
347 if (error)
348 return error;
Rik van Riel5beb4932010-03-05 13:42:07 -0800349
Konstantin Khlebnikov7a3ef202015-01-08 14:32:15 -0800350 /* An existing anon_vma has been reused, all done then. */
351 if (vma->anon_vma)
352 return 0;
353
Rik van Riel5beb4932010-03-05 13:42:07 -0800354 /* Then add our own anon_vma. */
355 anon_vma = anon_vma_alloc();
356 if (!anon_vma)
357 goto out_error;
Linus Torvaldsdd347392011-06-17 19:05:36 -0700358 avc = anon_vma_chain_alloc(GFP_KERNEL);
Rik van Riel5beb4932010-03-05 13:42:07 -0800359 if (!avc)
360 goto out_error_free_anon_vma;
Rik van Riel5c341ee12010-08-09 17:18:39 -0700361
362 /*
Miaohe Linaaf1f992021-02-25 17:17:53 -0800363 * The root anon_vma's rwsem is the lock actually used when we
Rik van Riel5c341ee12010-08-09 17:18:39 -0700364 * lock any of the anon_vmas in this anon_vma tree.
365 */
366 anon_vma->root = pvma->anon_vma->root;
Konstantin Khlebnikov7a3ef202015-01-08 14:32:15 -0800367 anon_vma->parent = pvma->anon_vma;
Rik van Riel76545062010-08-09 17:18:41 -0700368 /*
Peter Zijlstra01d8b202011-03-22 16:32:49 -0700369 * 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 Riel76545062010-08-09 17:18:41 -0700372 */
373 get_anon_vma(anon_vma->root);
Rik van Riel5beb4932010-03-05 13:42:07 -0800374 /* Mark this anon_vma as the one where our new (COWed) pages go. */
375 vma->anon_vma = anon_vma;
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000376 anon_vma_lock_write(anon_vma);
Rik van Riel5c341ee12010-08-09 17:18:39 -0700377 anon_vma_chain_link(vma, avc, anon_vma);
Konstantin Khlebnikov7a3ef202015-01-08 14:32:15 -0800378 anon_vma->parent->degree++;
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -0800379 anon_vma_unlock_write(anon_vma);
Rik van Riel5beb4932010-03-05 13:42:07 -0800380
381 return 0;
382
383 out_error_free_anon_vma:
Peter Zijlstra01d8b202011-03-22 16:32:49 -0700384 put_anon_vma(anon_vma);
Rik van Riel5beb4932010-03-05 13:42:07 -0800385 out_error:
Rik van Riel4946d542010-04-05 12:13:33 -0400386 unlink_anon_vmas(vma);
Rik van Riel5beb4932010-03-05 13:42:07 -0800387 return -ENOMEM;
388}
389
Rik van Riel5beb4932010-03-05 13:42:07 -0800390void unlink_anon_vmas(struct vm_area_struct *vma)
391{
392 struct anon_vma_chain *avc, *next;
Peter Zijlstraeee2acb2011-06-17 13:54:23 +0200393 struct anon_vma *root = NULL;
Rik van Riel5beb4932010-03-05 13:42:07 -0800394
Rik van Riel5c341ee12010-08-09 17:18:39 -0700395 /*
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 Riel5beb4932010-03-05 13:42:07 -0800399 list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) {
Peter Zijlstraeee2acb2011-06-17 13:54:23 +0200400 struct anon_vma *anon_vma = avc->anon_vma;
401
402 root = lock_anon_vma_root(root, anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700403 anon_vma_interval_tree_remove(avc, &anon_vma->rb_root);
Peter Zijlstraeee2acb2011-06-17 13:54:23 +0200404
405 /*
406 * Leave empty anon_vmas on the list - we'll need
407 * to free them outside the lock.
408 */
Davidlohr Buesof808c132017-09-08 16:15:08 -0700409 if (RB_EMPTY_ROOT(&anon_vma->rb_root.rb_root)) {
Konstantin Khlebnikov7a3ef202015-01-08 14:32:15 -0800410 anon_vma->parent->degree--;
Peter Zijlstraeee2acb2011-06-17 13:54:23 +0200411 continue;
Konstantin Khlebnikov7a3ef202015-01-08 14:32:15 -0800412 }
Peter Zijlstraeee2acb2011-06-17 13:54:23 +0200413
414 list_del(&avc->same_vma);
415 anon_vma_chain_free(avc);
416 }
Li Xinhaiee8ab192021-02-24 12:04:49 -0800417 if (vma->anon_vma) {
Konstantin Khlebnikov7a3ef202015-01-08 14:32:15 -0800418 vma->anon_vma->degree--;
Li Xinhaiee8ab192021-02-24 12:04:49 -0800419
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 Zijlstraeee2acb2011-06-17 13:54:23 +0200426 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 Molnar5a505082012-12-02 19:56:46 +0000431 * needing to write-acquire the anon_vma->root->rwsem.
Peter Zijlstraeee2acb2011-06-17 13:54:23 +0200432 */
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 Khlebnikove4c58002016-05-19 17:11:46 -0700436 VM_WARN_ON(anon_vma->degree);
Peter Zijlstraeee2acb2011-06-17 13:54:23 +0200437 put_anon_vma(anon_vma);
438
Rik van Riel5beb4932010-03-05 13:42:07 -0800439 list_del(&avc->same_vma);
440 anon_vma_chain_free(avc);
441 }
442}
443
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700444static void anon_vma_ctor(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Christoph Lametera35afb82007-05-16 22:10:57 -0700446 struct anon_vma *anon_vma = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Ingo Molnar5a505082012-12-02 19:56:46 +0000448 init_rwsem(&anon_vma->rwsem);
Peter Zijlstra83813262011-03-22 16:32:48 -0700449 atomic_set(&anon_vma->refcount, 0);
Davidlohr Buesof808c132017-09-08 16:15:08 -0700450 anon_vma->rb_root = RB_ROOT_CACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453void __init anon_vma_init(void)
454{
455 anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800456 0, SLAB_TYPESAFE_BY_RCU|SLAB_PANIC|SLAB_ACCOUNT,
Vladimir Davydov5d097052016-01-14 15:18:21 -0800457 anon_vma_ctor);
458 anon_vma_chain_cachep = KMEM_CACHE(anon_vma_chain,
459 SLAB_PANIC|SLAB_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462/*
Peter Zijlstra6111e4c2011-05-24 17:12:08 -0700463 * 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 Linad8a20c2021-02-25 17:18:06 -0800466 * the best this function can do is return a refcount increased anon_vma
467 * that might have been relevant to this page.
Peter Zijlstra6111e4c2011-05-24 17:12:08 -0700468 *
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 Zijlstrabc658c92011-05-29 10:33:44 +0200472 * 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 Zijlstra6111e4c2011-05-24 17:12:08 -0700477 * 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 Chen091e4292019-11-30 17:51:23 -0800481 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700485 */
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700486struct anon_vma *page_get_anon_vma(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700488 struct anon_vma *anon_vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 unsigned long anon_mapping;
490
491 rcu_read_lock();
Jason Low4db0c3c2015-04-15 16:14:08 -0700492 anon_mapping = (unsigned long)READ_ONCE(page->mapping);
Hugh Dickins3ca7b3c2009-12-14 17:58:57 -0800493 if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto out;
495 if (!page_mapped(page))
496 goto out;
497
498 anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700499 if (!atomic_inc_not_zero(&anon_vma->refcount)) {
500 anon_vma = NULL;
501 goto out;
502 }
Hugh Dickinsf18194272010-08-25 23:12:54 -0700503
504 /*
505 * If this page is still mapped, then its anon_vma cannot have been
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700506 * 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. McKenney5f0d5a32017-01-18 02:53:44 -0800508 * SLAB_TYPESAFE_BY_RCU guarantees that - so the atomic_inc_not_zero()
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700509 * above cannot corrupt).
Hugh Dickinsf18194272010-08-25 23:12:54 -0700510 */
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700511 if (!page_mapped(page)) {
Hugh Dickins7f39dda2014-06-04 16:05:33 -0700512 rcu_read_unlock();
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700513 put_anon_vma(anon_vma);
Hugh Dickins7f39dda2014-06-04 16:05:33 -0700514 return NULL;
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516out:
517 rcu_read_unlock();
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700518
519 return anon_vma;
520}
521
Peter Zijlstra88c22082011-05-24 17:12:13 -0700522/*
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 Molnar4fc3f1d2012-12-02 19:56:50 +0000529struct anon_vma *page_lock_anon_vma_read(struct page *page)
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700530{
Peter Zijlstra88c22082011-05-24 17:12:13 -0700531 struct anon_vma *anon_vma = NULL;
Hugh Dickinseee0f252011-05-28 13:20:21 -0700532 struct anon_vma *root_anon_vma;
Peter Zijlstra88c22082011-05-24 17:12:13 -0700533 unsigned long anon_mapping;
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700534
Peter Zijlstra88c22082011-05-24 17:12:13 -0700535 rcu_read_lock();
Jason Low4db0c3c2015-04-15 16:14:08 -0700536 anon_mapping = (unsigned long)READ_ONCE(page->mapping);
Peter Zijlstra88c22082011-05-24 17:12:13 -0700537 if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
538 goto out;
539 if (!page_mapped(page))
540 goto out;
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700541
Peter Zijlstra88c22082011-05-24 17:12:13 -0700542 anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
Jason Low4db0c3c2015-04-15 16:14:08 -0700543 root_anon_vma = READ_ONCE(anon_vma->root);
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000544 if (down_read_trylock(&root_anon_vma->rwsem)) {
Peter Zijlstra88c22082011-05-24 17:12:13 -0700545 /*
Hugh Dickinseee0f252011-05-28 13:20:21 -0700546 * 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 Zijlstrabc658c92011-05-29 10:33:44 +0200548 * not go away, see anon_vma_free().
Peter Zijlstra88c22082011-05-24 17:12:13 -0700549 */
Hugh Dickinseee0f252011-05-28 13:20:21 -0700550 if (!page_mapped(page)) {
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000551 up_read(&root_anon_vma->rwsem);
Peter Zijlstra88c22082011-05-24 17:12:13 -0700552 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 Dickins7f39dda2014-06-04 16:05:33 -0700564 rcu_read_unlock();
Peter Zijlstra88c22082011-05-24 17:12:13 -0700565 put_anon_vma(anon_vma);
Hugh Dickins7f39dda2014-06-04 16:05:33 -0700566 return NULL;
Peter Zijlstra88c22082011-05-24 17:12:13 -0700567 }
568
569 /* we pinned the anon_vma, its safe to sleep */
570 rcu_read_unlock();
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000571 anon_vma_lock_read(anon_vma);
Peter Zijlstra88c22082011-05-24 17:12:13 -0700572
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 Molnar4fc3f1d2012-12-02 19:56:50 +0000577 * we'll deadlock on the anon_vma_lock_write() recursion.
Peter Zijlstra88c22082011-05-24 17:12:13 -0700578 */
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000579 anon_vma_unlock_read(anon_vma);
Peter Zijlstra88c22082011-05-24 17:12:13 -0700580 __put_anon_vma(anon_vma);
581 anon_vma = NULL;
582 }
583
584 return anon_vma;
585
586out:
587 rcu_read_unlock();
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700588 return anon_vma;
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800589}
590
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000591void page_unlock_anon_vma_read(struct anon_vma *anon_vma)
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800592{
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000593 anon_vma_unlock_read(anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Mel Gorman72b252a2015-09-04 15:47:32 -0700596#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
Mel Gorman72b252a2015-09-04 15:47:32 -0700597/*
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 */
603void try_to_unmap_flush(void)
604{
605 struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
Mel Gorman72b252a2015-09-04 15:47:32 -0700606
607 if (!tlb_ubc->flush_required)
608 return;
609
Andy Lutomirskie73ad5f2017-05-22 15:30:03 -0700610 arch_tlbbatch_flush(&tlb_ubc->arch);
Mel Gorman72b252a2015-09-04 15:47:32 -0700611 tlb_ubc->flush_required = false;
Mel Gormand950c942015-09-04 15:47:35 -0700612 tlb_ubc->writable = false;
Mel Gorman72b252a2015-09-04 15:47:32 -0700613}
614
Mel Gormand950c942015-09-04 15:47:35 -0700615/* Flush iff there are potentially writable TLB entries that can race with IO */
616void try_to_unmap_flush_dirty(void)
617{
618 struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
619
620 if (tlb_ubc->writable)
621 try_to_unmap_flush();
622}
623
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -0800624static void set_tlb_ubc_flush_pending(struct mm_struct *mm, bool writable)
Mel Gorman72b252a2015-09-04 15:47:32 -0700625{
626 struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
627
Andy Lutomirskie73ad5f2017-05-22 15:30:03 -0700628 arch_tlbbatch_add_mm(&tlb_ubc->arch, mm);
Mel Gorman72b252a2015-09-04 15:47:32 -0700629 tlb_ubc->flush_required = true;
Mel Gormand950c942015-09-04 15:47:35 -0700630
631 /*
Mel Gorman3ea27712017-08-02 13:31:52 -0700632 * Ensure compiler does not re-order the setting of tlb_flush_batched
633 * before the PTE is cleared.
634 */
635 barrier();
636 mm->tlb_flush_batched = true;
637
638 /*
Mel Gormand950c942015-09-04 15:47:35 -0700639 * If the PTE was dirty then it's best to assume it's writable. The
640 * caller must use try_to_unmap_flush_dirty() or try_to_unmap_flush()
641 * before the page is queued for IO.
642 */
643 if (writable)
644 tlb_ubc->writable = true;
Mel Gorman72b252a2015-09-04 15:47:32 -0700645}
646
647/*
648 * Returns true if the TLB flush should be deferred to the end of a batch of
649 * unmap operations to reduce IPIs.
650 */
651static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags)
652{
653 bool should_defer = false;
654
655 if (!(flags & TTU_BATCH_FLUSH))
656 return false;
657
658 /* If remote CPUs need to be flushed then defer batch the flush */
659 if (cpumask_any_but(mm_cpumask(mm), get_cpu()) < nr_cpu_ids)
660 should_defer = true;
661 put_cpu();
662
663 return should_defer;
664}
Mel Gorman3ea27712017-08-02 13:31:52 -0700665
666/*
667 * Reclaim unmaps pages under the PTL but do not flush the TLB prior to
668 * releasing the PTL if TLB flushes are batched. It's possible for a parallel
669 * operation such as mprotect or munmap to race between reclaim unmapping
670 * the page and flushing the page. If this race occurs, it potentially allows
671 * access to data via a stale TLB entry. Tracking all mm's that have TLB
672 * batching in flight would be expensive during reclaim so instead track
673 * whether TLB batching occurred in the past and if so then do a flush here
674 * if required. This will cost one additional flush per reclaim cycle paid
675 * by the first operation at risk such as mprotect and mumap.
676 *
677 * This must be called under the PTL so that an access to tlb_flush_batched
678 * that is potentially a "reclaim vs mprotect/munmap/etc" race will synchronise
679 * via the PTL.
680 */
681void flush_tlb_batched_pending(struct mm_struct *mm)
682{
Qian Cai9c1177b2020-08-14 17:31:47 -0700683 if (data_race(mm->tlb_flush_batched)) {
Mel Gorman3ea27712017-08-02 13:31:52 -0700684 flush_tlb_mm(mm);
685
686 /*
687 * Do not allow the compiler to re-order the clearing of
688 * tlb_flush_batched before the tlb is flushed.
689 */
690 barrier();
691 mm->tlb_flush_batched = false;
692 }
693}
Mel Gorman72b252a2015-09-04 15:47:32 -0700694#else
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -0800695static void set_tlb_ubc_flush_pending(struct mm_struct *mm, bool writable)
Mel Gorman72b252a2015-09-04 15:47:32 -0700696{
697}
698
699static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags)
700{
701 return false;
702}
703#endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/*
Huang Shijiebf89c8c2009-10-01 15:44:04 -0700706 * At what user virtual address is page expected in vma?
Naoya Horiguchiab941e02010-05-11 14:06:55 -0700707 * Caller should check the page is actually part of the vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 */
709unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
710{
Andrea Arcangeli21d0d442010-08-09 17:19:10 -0700711 if (PageAnon(page)) {
Hugh Dickins4829b902010-10-02 17:46:06 -0700712 struct anon_vma *page__anon_vma = page_anon_vma(page);
713 /*
714 * Note: swapoff's unuse_vma() is more efficient with this
715 * check, and needs it to match anon_vma when KSM is active.
716 */
717 if (!vma->anon_vma || !page__anon_vma ||
718 vma->anon_vma->root != page__anon_vma->root)
Andrea Arcangeli21d0d442010-08-09 17:19:10 -0700719 return -EFAULT;
Jue Wang31657172021-06-15 18:24:00 -0700720 } else if (!vma->vm_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -EFAULT;
Jue Wang31657172021-06-15 18:24:00 -0700722 } else if (vma->vm_file->f_mapping != compound_head(page)->mapping) {
723 return -EFAULT;
724 }
Hugh Dickins494334e2021-06-15 18:23:56 -0700725
726 return vma_address(page, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Bob Liu62190492012-12-11 16:00:37 -0800729pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address)
730{
731 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300732 p4d_t *p4d;
Bob Liu62190492012-12-11 16:00:37 -0800733 pud_t *pud;
734 pmd_t *pmd = NULL;
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -0700735 pmd_t pmde;
Bob Liu62190492012-12-11 16:00:37 -0800736
737 pgd = pgd_offset(mm, address);
738 if (!pgd_present(*pgd))
739 goto out;
740
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300741 p4d = p4d_offset(pgd, address);
742 if (!p4d_present(*p4d))
743 goto out;
744
745 pud = pud_offset(p4d, address);
Bob Liu62190492012-12-11 16:00:37 -0800746 if (!pud_present(*pud))
747 goto out;
748
749 pmd = pmd_offset(pud, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -0700750 /*
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -0700751 * Some THP functions use the sequence pmdp_huge_clear_flush(), set_pmd_at()
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -0700752 * without holding anon_vma lock for write. So when looking for a
753 * genuine pmde (in which to find pte), test present and !THP together.
754 */
Christian Borntraegere37c6982014-12-07 21:41:33 +0100755 pmde = *pmd;
756 barrier();
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -0700757 if (!pmd_present(pmde) || pmd_trans_huge(pmde))
Bob Liu62190492012-12-11 16:00:37 -0800758 pmd = NULL;
759out:
760 return pmd;
761}
762
Joonsoo Kim9f326242014-01-21 15:49:53 -0800763struct page_referenced_arg {
764 int mapcount;
765 int referenced;
766 unsigned long vm_flags;
767 struct mem_cgroup *memcg;
768};
Nikita Danilov81b40822005-05-01 08:58:36 -0700769/*
Joonsoo Kim9f326242014-01-21 15:49:53 -0800770 * arg: page_referenced_arg will be passed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 */
Minchan Kime4b82222017-05-03 14:54:27 -0700772static bool page_referenced_one(struct page *page, struct vm_area_struct *vma,
Joonsoo Kim9f326242014-01-21 15:49:53 -0800773 unsigned long address, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Joonsoo Kim9f326242014-01-21 15:49:53 -0800775 struct page_referenced_arg *pra = arg;
Kirill A. Shutemov8eaeded2017-02-24 14:57:48 -0800776 struct page_vma_mapped_walk pvmw = {
777 .page = page,
778 .vma = vma,
779 .address = address,
780 };
Vladimir Davydov8749cfe2016-01-15 16:54:45 -0800781 int referenced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Kirill A. Shutemov8eaeded2017-02-24 14:57:48 -0800783 while (page_vma_mapped_walk(&pvmw)) {
784 address = pvmw.address;
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -0800785
Kirill A. Shutemov8eaeded2017-02-24 14:57:48 -0800786 if (vma->vm_flags & VM_LOCKED) {
787 page_vma_mapped_walk_done(&pvmw);
788 pra->vm_flags |= VM_LOCKED;
Minchan Kime4b82222017-05-03 14:54:27 -0700789 return false; /* To break the loop */
Vladimir Davydov8749cfe2016-01-15 16:54:45 -0800790 }
Kirill A. Shutemov8eaeded2017-02-24 14:57:48 -0800791
792 if (pvmw.pte) {
793 if (ptep_clear_flush_young_notify(vma, address,
794 pvmw.pte)) {
795 /*
796 * Don't treat a reference through
797 * a sequentially read mapping as such.
798 * If the page has been used in another mapping,
799 * we will catch it; if this other mapping is
800 * already gone, the unmap path will have set
801 * PG_referenced or activated the page.
802 */
803 if (likely(!(vma->vm_flags & VM_SEQ_READ)))
804 referenced++;
805 }
806 } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
807 if (pmdp_clear_flush_young_notify(vma, address,
808 pvmw.pmd))
809 referenced++;
810 } else {
811 /* unexpected pmd-mapped page? */
812 WARN_ON_ONCE(1);
813 }
814
815 pra->mapcount--;
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -0800816 }
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -0800817
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700818 if (referenced)
819 clear_page_idle(page);
820 if (test_and_clear_page_young(page))
821 referenced++;
822
Joonsoo Kim9f326242014-01-21 15:49:53 -0800823 if (referenced) {
824 pra->referenced++;
825 pra->vm_flags |= vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800827
Joonsoo Kim9f326242014-01-21 15:49:53 -0800828 if (!pra->mapcount)
Minchan Kime4b82222017-05-03 14:54:27 -0700829 return false; /* To break the loop */
Joonsoo Kim9f326242014-01-21 15:49:53 -0800830
Minchan Kime4b82222017-05-03 14:54:27 -0700831 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Joonsoo Kim9f326242014-01-21 15:49:53 -0800834static bool invalid_page_referenced_vma(struct vm_area_struct *vma, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Joonsoo Kim9f326242014-01-21 15:49:53 -0800836 struct page_referenced_arg *pra = arg;
837 struct mem_cgroup *memcg = pra->memcg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Joonsoo Kim9f326242014-01-21 15:49:53 -0800839 if (!mm_match_cgroup(vma->vm_mm, memcg))
840 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Joonsoo Kim9f326242014-01-21 15:49:53 -0800842 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
845/**
846 * page_referenced - test if the page was referenced
847 * @page: the page to test
848 * @is_locked: caller holds lock on the page
Johannes Weiner72835c82012-01-12 17:18:32 -0800849 * @memcg: target memory cgroup
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700850 * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 *
852 * Quick test_and_clear_referenced for all mappings to a page,
853 * returns the number of ptes which referenced the page.
854 */
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700855int page_referenced(struct page *page,
856 int is_locked,
Johannes Weiner72835c82012-01-12 17:18:32 -0800857 struct mem_cgroup *memcg,
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700858 unsigned long *vm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Hugh Dickins5ad64682009-12-14 17:59:24 -0800860 int we_locked = 0;
Joonsoo Kim9f326242014-01-21 15:49:53 -0800861 struct page_referenced_arg pra = {
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -0800862 .mapcount = total_mapcount(page),
Joonsoo Kim9f326242014-01-21 15:49:53 -0800863 .memcg = memcg,
864 };
865 struct rmap_walk_control rwc = {
866 .rmap_one = page_referenced_one,
867 .arg = (void *)&pra,
868 .anon_lock = page_lock_anon_vma_read,
869 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700871 *vm_flags = 0;
Huang Shijie059d8442019-05-13 17:21:07 -0700872 if (!pra.mapcount)
Joonsoo Kim9f326242014-01-21 15:49:53 -0800873 return 0;
874
875 if (!page_rmapping(page))
876 return 0;
877
878 if (!is_locked && (!PageAnon(page) || PageKsm(page))) {
879 we_locked = trylock_page(page);
880 if (!we_locked)
881 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
Joonsoo Kim9f326242014-01-21 15:49:53 -0800883
884 /*
885 * If we are reclaiming on behalf of a cgroup, skip
886 * counting on behalf of references from different
887 * cgroups
888 */
889 if (memcg) {
890 rwc.invalid_vma = invalid_page_referenced_vma;
891 }
892
Minchan Kimc24f3862017-05-03 14:54:00 -0700893 rmap_walk(page, &rwc);
Joonsoo Kim9f326242014-01-21 15:49:53 -0800894 *vm_flags = pra.vm_flags;
895
896 if (we_locked)
897 unlock_page(page);
898
899 return pra.referenced;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Minchan Kime4b82222017-05-03 14:54:27 -0700902static bool page_mkclean_one(struct page *page, struct vm_area_struct *vma,
Joonsoo Kim9853a402014-01-21 15:49:55 -0800903 unsigned long address, void *arg)
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700904{
Kirill A. Shutemovf27176c2017-02-24 14:57:57 -0800905 struct page_vma_mapped_walk pvmw = {
906 .page = page,
907 .vma = vma,
908 .address = address,
909 .flags = PVMW_SYNC,
910 };
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800911 struct mmu_notifier_range range;
Joonsoo Kim9853a402014-01-21 15:49:55 -0800912 int *cleaned = arg;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700913
Jérôme Glisse369ea822017-08-31 17:17:27 -0400914 /*
915 * We have to assume the worse case ie pmd for invalidation. Note that
916 * the page can not be free from this function.
917 */
Jérôme Glisse7269f992019-05-13 17:20:53 -0700918 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE,
919 0, vma, vma->vm_mm, address,
Hugh Dickins494334e2021-06-15 18:23:56 -0700920 vma_address_end(page, vma));
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800921 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse369ea822017-08-31 17:17:27 -0400922
Kirill A. Shutemovf27176c2017-02-24 14:57:57 -0800923 while (page_vma_mapped_walk(&pvmw)) {
924 int ret = 0;
Jérôme Glisse369ea822017-08-31 17:17:27 -0400925
YueHaibing1f18b292019-09-23 15:34:22 -0700926 address = pvmw.address;
Kirill A. Shutemovf27176c2017-02-24 14:57:57 -0800927 if (pvmw.pte) {
928 pte_t entry;
929 pte_t *pte = pvmw.pte;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700930
Kirill A. Shutemovf27176c2017-02-24 14:57:57 -0800931 if (!pte_dirty(*pte) && !pte_write(*pte))
932 continue;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700933
Linus Torvalds785373b2017-08-29 09:11:06 -0700934 flush_cache_page(vma, address, pte_pfn(*pte));
935 entry = ptep_clear_flush(vma, address, pte);
Kirill A. Shutemovf27176c2017-02-24 14:57:57 -0800936 entry = pte_wrprotect(entry);
937 entry = pte_mkclean(entry);
Linus Torvalds785373b2017-08-29 09:11:06 -0700938 set_pte_at(vma->vm_mm, address, pte, entry);
Kirill A. Shutemovf27176c2017-02-24 14:57:57 -0800939 ret = 1;
940 } else {
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700941#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovf27176c2017-02-24 14:57:57 -0800942 pmd_t *pmd = pvmw.pmd;
943 pmd_t entry;
944
945 if (!pmd_dirty(*pmd) && !pmd_write(*pmd))
946 continue;
947
Linus Torvalds785373b2017-08-29 09:11:06 -0700948 flush_cache_page(vma, address, page_to_pfn(page));
Aneesh Kumar K.V024eee02019-05-13 17:19:11 -0700949 entry = pmdp_invalidate(vma, address, pmd);
Kirill A. Shutemovf27176c2017-02-24 14:57:57 -0800950 entry = pmd_wrprotect(entry);
951 entry = pmd_mkclean(entry);
Linus Torvalds785373b2017-08-29 09:11:06 -0700952 set_pmd_at(vma->vm_mm, address, pmd, entry);
Kirill A. Shutemovf27176c2017-02-24 14:57:57 -0800953 ret = 1;
954#else
955 /* unexpected pmd-mapped page? */
956 WARN_ON_ONCE(1);
957#endif
958 }
959
Jérôme Glisse0f108512017-11-15 17:34:07 -0800960 /*
961 * No need to call mmu_notifier_invalidate_range() as we are
962 * downgrading page table protection not changing it to point
963 * to a new page.
964 *
Mike Rapoportad56b732018-03-21 21:22:47 +0200965 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -0800966 */
967 if (ret)
Kirill A. Shutemovf27176c2017-02-24 14:57:57 -0800968 (*cleaned)++;
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100969 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700970
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800971 mmu_notifier_invalidate_range_end(&range);
Jérôme Glisse369ea822017-08-31 17:17:27 -0400972
Minchan Kime4b82222017-05-03 14:54:27 -0700973 return true;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700974}
975
Joonsoo Kim9853a402014-01-21 15:49:55 -0800976static bool invalid_mkclean_vma(struct vm_area_struct *vma, void *arg)
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700977{
Joonsoo Kim9853a402014-01-21 15:49:55 -0800978 if (vma->vm_flags & VM_SHARED)
Fengguang Wu871beb82014-01-23 15:53:41 -0800979 return false;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700980
Fengguang Wu871beb82014-01-23 15:53:41 -0800981 return true;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700982}
983
984int page_mkclean(struct page *page)
985{
Joonsoo Kim9853a402014-01-21 15:49:55 -0800986 int cleaned = 0;
987 struct address_space *mapping;
988 struct rmap_walk_control rwc = {
989 .arg = (void *)&cleaned,
990 .rmap_one = page_mkclean_one,
991 .invalid_vma = invalid_mkclean_vma,
992 };
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700993
994 BUG_ON(!PageLocked(page));
995
Joonsoo Kim9853a402014-01-21 15:49:55 -0800996 if (!page_mapped(page))
997 return 0;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700998
Joonsoo Kim9853a402014-01-21 15:49:55 -0800999 mapping = page_mapping(page);
1000 if (!mapping)
1001 return 0;
1002
1003 rmap_walk(page, &rwc);
1004
1005 return cleaned;
Peter Zijlstrad08b3852006-09-25 23:30:57 -07001006}
Jaya Kumar60b59be2007-05-08 00:37:37 -07001007EXPORT_SYMBOL_GPL(page_mkclean);
Peter Zijlstrad08b3852006-09-25 23:30:57 -07001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009/**
Rik van Rielc44b6742010-03-05 13:42:09 -08001010 * page_move_anon_rmap - move a page to our anon_vma
1011 * @page: the page to move to our anon_vma
1012 * @vma: the vma the page belongs to
Rik van Rielc44b6742010-03-05 13:42:09 -08001013 *
1014 * When a page belongs exclusively to one process after a COW event,
1015 * that page can be moved into the anon_vma that belongs to just that
1016 * process, so the rmap code will not search the parent or sibling
1017 * processes.
1018 */
Hugh Dickins5a499732016-07-14 12:07:38 -07001019void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma)
Rik van Rielc44b6742010-03-05 13:42:09 -08001020{
1021 struct anon_vma *anon_vma = vma->anon_vma;
1022
Hugh Dickins5a499732016-07-14 12:07:38 -07001023 page = compound_head(page);
1024
Sasha Levin309381fea2014-01-23 15:52:54 -08001025 VM_BUG_ON_PAGE(!PageLocked(page), page);
Sasha Levin81d1b092014-10-09 15:28:10 -07001026 VM_BUG_ON_VMA(!anon_vma, vma);
Rik van Rielc44b6742010-03-05 13:42:09 -08001027
1028 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
Vladimir Davydov414e2fb2015-06-24 16:56:56 -07001029 /*
1030 * Ensure that anon_vma and the PAGE_MAPPING_ANON bit are written
1031 * simultaneously, so a concurrent reader (eg page_referenced()'s
1032 * PageAnon()) will not see one without the other.
1033 */
1034 WRITE_ONCE(page->mapping, (struct address_space *) anon_vma);
Rik van Rielc44b6742010-03-05 13:42:09 -08001035}
1036
1037/**
Andi Kleen4e1c1972010-09-22 12:43:56 +02001038 * __page_set_anon_rmap - set up new anonymous rmap
Kirill Tkhai451b9512018-12-28 00:39:31 -08001039 * @page: Page or Hugepage to add to rmap
Andi Kleen4e1c1972010-09-22 12:43:56 +02001040 * @vma: VM area to add page to.
1041 * @address: User virtual address of the mapping
Rik van Riele8a03fe2010-04-14 17:59:28 -04001042 * @exclusive: the page is exclusively owned by the current process
Nick Piggin9617d952006-01-06 00:11:12 -08001043 */
1044static void __page_set_anon_rmap(struct page *page,
Rik van Riele8a03fe2010-04-14 17:59:28 -04001045 struct vm_area_struct *vma, unsigned long address, int exclusive)
Nick Piggin9617d952006-01-06 00:11:12 -08001046{
Rik van Riele8a03fe2010-04-14 17:59:28 -04001047 struct anon_vma *anon_vma = vma->anon_vma;
Nick Piggin9617d952006-01-06 00:11:12 -08001048
Rik van Riele8a03fe2010-04-14 17:59:28 -04001049 BUG_ON(!anon_vma);
Linus Torvaldsea900022010-04-12 12:44:29 -07001050
Andi Kleen4e1c1972010-09-22 12:43:56 +02001051 if (PageAnon(page))
1052 return;
1053
Linus Torvaldsea900022010-04-12 12:44:29 -07001054 /*
Rik van Riele8a03fe2010-04-14 17:59:28 -04001055 * If the page isn't exclusively mapped into this vma,
1056 * we must use the _oldest_ possible anon_vma for the
1057 * page mapping!
Linus Torvaldsea900022010-04-12 12:44:29 -07001058 */
Andi Kleen4e1c1972010-09-22 12:43:56 +02001059 if (!exclusive)
Andrea Arcangeli288468c2010-08-09 17:19:09 -07001060 anon_vma = anon_vma->root;
Linus Torvaldsea900022010-04-12 12:44:29 -07001061
Alex Shi16f5e702020-12-15 12:33:42 -08001062 /*
1063 * page_idle does a lockless/optimistic rmap scan on page->mapping.
1064 * Make sure the compiler doesn't split the stores of anon_vma and
1065 * the PAGE_MAPPING_ANON type identifier, otherwise the rmap code
1066 * could mistake the mapping for a struct address_space and crash.
1067 */
Nick Piggin9617d952006-01-06 00:11:12 -08001068 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
Alex Shi16f5e702020-12-15 12:33:42 -08001069 WRITE_ONCE(page->mapping, (struct address_space *) anon_vma);
Nick Piggin9617d952006-01-06 00:11:12 -08001070 page->index = linear_page_index(vma, address);
Nick Piggin9617d952006-01-06 00:11:12 -08001071}
1072
1073/**
Randy Dunlap43d8eac2008-03-19 17:00:43 -07001074 * __page_check_anon_rmap - sanity check anonymous rmap addition
Nick Pigginc97a9e12007-05-16 22:11:21 -07001075 * @page: the page to add the mapping to
1076 * @vma: the vm area in which the mapping is added
1077 * @address: the user virtual address mapped
1078 */
1079static void __page_check_anon_rmap(struct page *page,
1080 struct vm_area_struct *vma, unsigned long address)
1081{
Nick Pigginc97a9e12007-05-16 22:11:21 -07001082 /*
1083 * The page's anon-rmap details (mapping and index) are guaranteed to
1084 * be set up correctly at this point.
1085 *
1086 * We have exclusion against page_add_anon_rmap because the caller
Miaohe Lin90aaca82021-02-25 17:17:59 -08001087 * always holds the page locked.
Nick Pigginc97a9e12007-05-16 22:11:21 -07001088 *
1089 * We have exclusion against page_add_new_anon_rmap because those pages
1090 * are initially only visible via the pagetables, and the pte is locked
1091 * over the call to page_add_new_anon_rmap.
1092 */
Yang Shi30c46382019-11-30 17:51:26 -08001093 VM_BUG_ON_PAGE(page_anon_vma(page)->root != vma->anon_vma->root, page);
1094 VM_BUG_ON_PAGE(page_to_pgoff(page) != linear_page_index(vma, address),
1095 page);
Nick Pigginc97a9e12007-05-16 22:11:21 -07001096}
1097
1098/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 * page_add_anon_rmap - add pte mapping to an anonymous page
1100 * @page: the page to add the mapping to
1101 * @vma: the vm area in which the mapping is added
1102 * @address: the user virtual address mapped
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001103 * @compound: charge the page as compound or small page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 *
Hugh Dickins5ad64682009-12-14 17:59:24 -08001105 * The caller needs to hold the pte lock, and the page must be locked in
Hugh Dickins80e148222009-12-14 17:59:29 -08001106 * the anon_vma case: to serialize mapping,index checking after setting,
1107 * and to ensure that PageAnon is not being upgraded racily to PageKsm
1108 * (but PageKsm is never downgraded to PageAnon).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 */
1110void page_add_anon_rmap(struct page *page,
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001111 struct vm_area_struct *vma, unsigned long address, bool compound)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001113 do_page_add_anon_rmap(page, vma, address, compound ? RMAP_COMPOUND : 0);
Rik van Rielad8c2ee2010-08-09 17:19:48 -07001114}
1115
1116/*
1117 * Special version of the above for do_swap_page, which often runs
1118 * into pages that are exclusively owned by the current process.
1119 * Everybody else should continue to use page_add_anon_rmap above.
1120 */
1121void do_page_add_anon_rmap(struct page *page,
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001122 struct vm_area_struct *vma, unsigned long address, int flags)
Rik van Rielad8c2ee2010-08-09 17:19:48 -07001123{
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001124 bool compound = flags & RMAP_COMPOUND;
1125 bool first;
1126
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001127 if (unlikely(PageKsm(page)))
1128 lock_page_memcg(page);
1129 else
1130 VM_BUG_ON_PAGE(!PageLocked(page), page);
1131
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08001132 if (compound) {
1133 atomic_t *mapcount;
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001134 VM_BUG_ON_PAGE(!PageLocked(page), page);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08001135 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
1136 mapcount = compound_mapcount_ptr(page);
1137 first = atomic_inc_and_test(mapcount);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001138 } else {
1139 first = atomic_inc_and_test(&page->_mapcount);
1140 }
1141
Andrea Arcangeli79134172011-01-13 15:46:58 -08001142 if (first) {
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001143 int nr = compound ? thp_nr_pages(page) : 1;
Jianyu Zhanbea04b02014-06-04 16:09:51 -07001144 /*
1145 * We use the irq-unsafe __{inc|mod}_zone_page_stat because
1146 * these counters are not modified in interrupt context, and
1147 * pte lock(a spinlock) is held, which implies preemption
1148 * disabled.
1149 */
Kirill A. Shutemov65c45372016-07-26 15:26:10 -07001150 if (compound)
Muchun Song69473e52021-02-24 12:03:23 -08001151 __mod_lruvec_page_state(page, NR_ANON_THPS, nr);
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001152 __mod_lruvec_page_state(page, NR_ANON_MAPPED, nr);
Andrea Arcangeli79134172011-01-13 15:46:58 -08001153 }
Hugh Dickins5ad64682009-12-14 17:59:24 -08001154
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001155 if (unlikely(PageKsm(page))) {
1156 unlock_page_memcg(page);
1157 return;
1158 }
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001159
Hugh Dickins5dbe0af2011-05-28 13:17:04 -07001160 /* address might be in next vma when migration races vma_adjust */
Hugh Dickins5ad64682009-12-14 17:59:24 -08001161 if (first)
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001162 __page_set_anon_rmap(page, vma, address,
1163 flags & RMAP_EXCLUSIVE);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001164 else
Nick Pigginc97a9e12007-05-16 22:11:21 -07001165 __page_check_anon_rmap(page, vma, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Randy Dunlap43d8eac2008-03-19 17:00:43 -07001168/**
Nick Piggin9617d952006-01-06 00:11:12 -08001169 * page_add_new_anon_rmap - add pte mapping to a new anonymous page
1170 * @page: the page to add the mapping to
1171 * @vma: the vm area in which the mapping is added
1172 * @address: the user virtual address mapped
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001173 * @compound: charge the page as compound or small page
Nick Piggin9617d952006-01-06 00:11:12 -08001174 *
1175 * Same as page_add_anon_rmap but must only be called on *new* pages.
1176 * This means the inc-and-test can be bypassed.
Nick Pigginc97a9e12007-05-16 22:11:21 -07001177 * Page does not have to be locked.
Nick Piggin9617d952006-01-06 00:11:12 -08001178 */
1179void page_add_new_anon_rmap(struct page *page,
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001180 struct vm_area_struct *vma, unsigned long address, bool compound)
Nick Piggin9617d952006-01-06 00:11:12 -08001181{
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001182 int nr = compound ? thp_nr_pages(page) : 1;
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001183
Sasha Levin81d1b092014-10-09 15:28:10 -07001184 VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
Hugh Dickinsfa9949d2016-05-19 17:12:41 -07001185 __SetPageSwapBacked(page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001186 if (compound) {
1187 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001188 /* increment count (starts at -1) */
1189 atomic_set(compound_mapcount_ptr(page), 0);
John Hubbard47e29d32020-04-01 21:05:33 -07001190 if (hpage_pincount_available(page))
1191 atomic_set(compound_pincount_ptr(page), 0);
1192
Muchun Song69473e52021-02-24 12:03:23 -08001193 __mod_lruvec_page_state(page, NR_ANON_THPS, nr);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001194 } else {
1195 /* Anon THP always mapped first with PMD */
1196 VM_BUG_ON_PAGE(PageTransCompound(page), page);
1197 /* increment count (starts at -1) */
1198 atomic_set(&page->_mapcount, 0);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001199 }
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001200 __mod_lruvec_page_state(page, NR_ANON_MAPPED, nr);
Rik van Riele8a03fe2010-04-14 17:59:28 -04001201 __page_set_anon_rmap(page, vma, address, 1);
Nick Piggin9617d952006-01-06 00:11:12 -08001202}
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204/**
1205 * page_add_file_rmap - add pte mapping to a file page
1206 * @page: the page to add the mapping to
Mike Rapoporte8b098f2018-04-05 16:24:57 -07001207 * @compound: charge the page as compound or small page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -07001209 * The caller needs to hold the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 */
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001211void page_add_file_rmap(struct page *page, bool compound)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001213 int i, nr = 1;
1214
1215 VM_BUG_ON_PAGE(compound && !PageTransHuge(page), page);
Johannes Weiner62cccb82016-03-15 14:57:22 -07001216 lock_page_memcg(page);
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001217 if (compound && PageTransHuge(page)) {
Muchun Songa1528e22021-02-24 12:03:35 -08001218 int nr_pages = thp_nr_pages(page);
1219
1220 for (i = 0, nr = 0; i < nr_pages; i++) {
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001221 if (atomic_inc_and_test(&page[i]._mapcount))
1222 nr++;
1223 }
1224 if (!atomic_inc_and_test(compound_mapcount_ptr(page)))
1225 goto out;
Song Liu99cb0db2019-09-23 15:38:00 -07001226 if (PageSwapBacked(page))
Muchun Songa1528e22021-02-24 12:03:35 -08001227 __mod_lruvec_page_state(page, NR_SHMEM_PMDMAPPED,
1228 nr_pages);
Song Liu99cb0db2019-09-23 15:38:00 -07001229 else
Muchun Song380780e2021-02-24 12:03:39 -08001230 __mod_lruvec_page_state(page, NR_FILE_PMDMAPPED,
1231 nr_pages);
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001232 } else {
Kirill A. Shutemovc8efc392016-08-10 16:27:52 -07001233 if (PageTransCompound(page) && page_mapping(page)) {
Muchun Songfe3df442021-09-07 19:55:55 -07001234 struct page *head = compound_head(page);
1235
Kirill A. Shutemovc8efc392016-08-10 16:27:52 -07001236 VM_WARN_ON_ONCE(!PageLocked(page));
1237
Muchun Songfe3df442021-09-07 19:55:55 -07001238 SetPageDoubleMap(head);
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001239 if (PageMlocked(page))
Muchun Songfe3df442021-09-07 19:55:55 -07001240 clear_page_mlock(head);
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001241 }
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001242 if (!atomic_inc_and_test(&page->_mapcount))
1243 goto out;
Balbir Singhd69b0422009-06-17 16:26:34 -07001244 }
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001245 __mod_lruvec_page_state(page, NR_FILE_MAPPED, nr);
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001246out:
Johannes Weiner62cccb82016-03-15 14:57:22 -07001247 unlock_page_memcg(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001250static void page_remove_file_rmap(struct page *page, bool compound)
Johannes Weiner8186eb62014-10-29 14:50:51 -07001251{
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001252 int i, nr = 1;
1253
Steve Capper57dea932016-08-10 16:27:55 -07001254 VM_BUG_ON_PAGE(compound && !PageHead(page), page);
Johannes Weiner8186eb62014-10-29 14:50:51 -07001255
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001256 /* Hugepages are not counted in NR_FILE_MAPPED for now. */
1257 if (unlikely(PageHuge(page))) {
1258 /* hugetlb pages are always mapped with pmds */
1259 atomic_dec(compound_mapcount_ptr(page));
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001260 return;
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001261 }
1262
Johannes Weiner8186eb62014-10-29 14:50:51 -07001263 /* page still mapped by someone else? */
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001264 if (compound && PageTransHuge(page)) {
Muchun Songa1528e22021-02-24 12:03:35 -08001265 int nr_pages = thp_nr_pages(page);
1266
1267 for (i = 0, nr = 0; i < nr_pages; i++) {
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001268 if (atomic_add_negative(-1, &page[i]._mapcount))
1269 nr++;
1270 }
1271 if (!atomic_add_negative(-1, compound_mapcount_ptr(page)))
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001272 return;
Song Liu99cb0db2019-09-23 15:38:00 -07001273 if (PageSwapBacked(page))
Muchun Songa1528e22021-02-24 12:03:35 -08001274 __mod_lruvec_page_state(page, NR_SHMEM_PMDMAPPED,
1275 -nr_pages);
Song Liu99cb0db2019-09-23 15:38:00 -07001276 else
Muchun Song380780e2021-02-24 12:03:39 -08001277 __mod_lruvec_page_state(page, NR_FILE_PMDMAPPED,
1278 -nr_pages);
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001279 } else {
1280 if (!atomic_add_negative(-1, &page->_mapcount))
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001281 return;
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001282 }
Johannes Weiner8186eb62014-10-29 14:50:51 -07001283
Johannes Weiner8186eb62014-10-29 14:50:51 -07001284 /*
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001285 * We use the irq-unsafe __{inc|mod}_lruvec_page_state because
Johannes Weiner8186eb62014-10-29 14:50:51 -07001286 * these counters are not modified in interrupt context, and
1287 * pte lock(a spinlock) is held, which implies preemption disabled.
1288 */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001289 __mod_lruvec_page_state(page, NR_FILE_MAPPED, -nr);
Johannes Weiner8186eb62014-10-29 14:50:51 -07001290
1291 if (unlikely(PageMlocked(page)))
1292 clear_page_mlock(page);
Johannes Weiner8186eb62014-10-29 14:50:51 -07001293}
1294
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001295static void page_remove_anon_compound_rmap(struct page *page)
1296{
1297 int i, nr;
1298
1299 if (!atomic_add_negative(-1, compound_mapcount_ptr(page)))
1300 return;
1301
1302 /* Hugepages are not counted in NR_ANON_PAGES for now. */
1303 if (unlikely(PageHuge(page)))
1304 return;
1305
1306 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
1307 return;
1308
Muchun Song69473e52021-02-24 12:03:23 -08001309 __mod_lruvec_page_state(page, NR_ANON_THPS, -thp_nr_pages(page));
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001310
1311 if (TestClearPageDoubleMap(page)) {
1312 /*
1313 * Subpages can be mapped with PTEs too. Check how many of
Kirill A. Shutemovf1fe80d2019-11-30 17:57:15 -08001314 * them are still mapped.
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001315 */
Matthew Wilcox (Oracle)5eaf35a2020-10-15 20:05:46 -07001316 for (i = 0, nr = 0; i < thp_nr_pages(page); i++) {
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001317 if (atomic_add_negative(-1, &page[i]._mapcount))
1318 nr++;
1319 }
Kirill A. Shutemovf1fe80d2019-11-30 17:57:15 -08001320
1321 /*
1322 * Queue the page for deferred split if at least one small
1323 * page of the compound page is unmapped, but at least one
1324 * small page is still mapped.
1325 */
Matthew Wilcox (Oracle)5eaf35a2020-10-15 20:05:46 -07001326 if (nr && nr < thp_nr_pages(page))
Kirill A. Shutemovf1fe80d2019-11-30 17:57:15 -08001327 deferred_split_huge_page(page);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001328 } else {
Matthew Wilcox (Oracle)5eaf35a2020-10-15 20:05:46 -07001329 nr = thp_nr_pages(page);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001330 }
1331
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001332 if (unlikely(PageMlocked(page)))
1333 clear_page_mlock(page);
1334
Kirill A. Shutemovf1fe80d2019-11-30 17:57:15 -08001335 if (nr)
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001336 __mod_lruvec_page_state(page, NR_ANON_MAPPED, -nr);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001337}
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339/**
1340 * page_remove_rmap - take down pte mapping from a page
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001341 * @page: page to remove mapping from
1342 * @compound: uncharge the page as compound or small page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -07001344 * The caller needs to hold the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 */
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001346void page_remove_rmap(struct page *page, bool compound)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001348 lock_page_memcg(page);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001349
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001350 if (!PageAnon(page)) {
1351 page_remove_file_rmap(page, compound);
1352 goto out;
1353 }
1354
1355 if (compound) {
1356 page_remove_anon_compound_rmap(page);
1357 goto out;
1358 }
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001359
KOSAKI Motohirob904dcf2009-09-21 17:01:28 -07001360 /* page still mapped by someone else? */
1361 if (!atomic_add_negative(-1, &page->_mapcount))
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001362 goto out;
Johannes Weiner8186eb62014-10-29 14:50:51 -07001363
KOSAKI Motohirob904dcf2009-09-21 17:01:28 -07001364 /*
Jianyu Zhanbea04b02014-06-04 16:09:51 -07001365 * We use the irq-unsafe __{inc|mod}_zone_page_stat because
1366 * these counters are not modified in interrupt context, and
Jianyu Zhanbea04b02014-06-04 16:09:51 -07001367 * pte lock(a spinlock) is held, which implies preemption disabled.
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09001368 */
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001369 __dec_lruvec_page_state(page, NR_ANON_MAPPED);
Johannes Weiner8186eb62014-10-29 14:50:51 -07001370
Hugh Dickinse6c509f2012-10-08 16:33:19 -07001371 if (unlikely(PageMlocked(page)))
1372 clear_page_mlock(page);
Johannes Weiner8186eb62014-10-29 14:50:51 -07001373
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001374 if (PageTransCompound(page))
1375 deferred_split_huge_page(compound_head(page));
1376
KOSAKI Motohirob904dcf2009-09-21 17:01:28 -07001377 /*
1378 * It would be tidy to reset the PageAnon mapping here,
1379 * but that might overwrite a racing page_add_anon_rmap
1380 * which increments mapcount after us but sets mapping
Mel Gorman2d4894b2017-11-15 17:37:59 -08001381 * before us: so leave the reset to free_unref_page,
KOSAKI Motohirob904dcf2009-09-21 17:01:28 -07001382 * and remember that it's only reliable while mapped.
1383 * Leaving it set also helps swapoff to reinstate ptes
1384 * faster for those pages still in swapcache.
1385 */
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001386out:
1387 unlock_page_memcg(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
1390/*
Joonsoo Kim52629502014-01-21 15:49:50 -08001391 * @arg: enum ttu_flags will be passed to this argument
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 */
Minchan Kime4b82222017-05-03 14:54:27 -07001393static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
Joonsoo Kim52629502014-01-21 15:49:50 -08001394 unsigned long address, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001397 struct page_vma_mapped_walk pvmw = {
1398 .page = page,
1399 .vma = vma,
1400 .address = address,
1401 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 pte_t pteval;
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001403 struct page *subpage;
Linus Torvalds785373b2017-08-29 09:11:06 -07001404 bool ret = true;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001405 struct mmu_notifier_range range;
Palmer Dabbelt4708f312020-04-06 20:08:00 -07001406 enum ttu_flags flags = (enum ttu_flags)(long)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Hugh Dickins732ed552021-06-15 18:23:53 -07001408 /*
1409 * When racing against e.g. zap_pte_range() on another cpu,
1410 * in between its ptep_get_and_clear_full() and page_remove_rmap(),
Yang Shi1fb08ac2021-06-30 18:52:01 -07001411 * try_to_unmap() may return before page_mapped() has become false,
Hugh Dickins732ed552021-06-15 18:23:53 -07001412 * if page table locking is skipped: use TTU_SYNC to wait for that.
1413 */
1414 if (flags & TTU_SYNC)
1415 pvmw.flags = PVMW_SYNC;
1416
Alistair Popplea98a2f02021-06-30 18:54:16 -07001417 if (flags & TTU_SPLIT_HUGE_PMD)
1418 split_huge_pmd_address(vma, address, false, page);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07001419
Jérôme Glisse369ea822017-08-31 17:17:27 -04001420 /*
Mike Kravetz017b1662018-10-05 15:51:29 -07001421 * For THP, we have to assume the worse case ie pmd for invalidation.
1422 * For hugetlb, it could be much worse if we need to do pud
1423 * invalidation in the case of pmd sharing.
1424 *
1425 * Note that the page can not be free in this function as call of
1426 * try_to_unmap() must hold a reference on the page.
Jérôme Glisse369ea822017-08-31 17:17:27 -04001427 */
Hugh Dickins494334e2021-06-15 18:23:56 -07001428 range.end = PageKsm(page) ?
1429 address + PAGE_SIZE : vma_address_end(page, vma);
Jérôme Glisse7269f992019-05-13 17:20:53 -07001430 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
Hugh Dickins494334e2021-06-15 18:23:56 -07001431 address, range.end);
Mike Kravetz017b1662018-10-05 15:51:29 -07001432 if (PageHuge(page)) {
1433 /*
1434 * If sharing is possible, start and end will be adjusted
1435 * accordingly.
1436 */
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001437 adjust_range_if_pmd_sharing_possible(vma, &range.start,
1438 &range.end);
Mike Kravetz017b1662018-10-05 15:51:29 -07001439 }
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001440 mmu_notifier_invalidate_range_start(&range);
Jérôme Glisse369ea822017-08-31 17:17:27 -04001441
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001442 while (page_vma_mapped_walk(&pvmw)) {
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001443 /*
1444 * If the page is mlock()d, we cannot swap it out.
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001445 */
Hugh Dickinsefdb6722021-07-11 20:10:49 -07001446 if (!(flags & TTU_IGNORE_MLOCK) &&
1447 (vma->vm_flags & VM_LOCKED)) {
1448 /*
1449 * PTE-mapped THP are never marked as mlocked: so do
1450 * not set it on a DoubleMap THP, nor on an Anon THP
1451 * (which may still be PTE-mapped after DoubleMap was
1452 * cleared). But stop unmapping even in those cases.
1453 */
1454 if (!PageTransCompound(page) || (PageHead(page) &&
1455 !PageDoubleMap(page) && !PageAnon(page)))
1456 mlock_vma_page(page);
1457 page_vma_mapped_walk_done(&pvmw);
1458 ret = false;
1459 break;
Hugh Dickinsb87537d9e2015-11-05 18:49:33 -08001460 }
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001461
Kirill A. Shutemov83462422017-03-09 16:17:20 -08001462 /* Unexpected PMD-mapped THP? */
1463 VM_BUG_ON_PAGE(!pvmw.pte, page);
1464
1465 subpage = page - page_to_pfn(page) + pte_pfn(*pvmw.pte);
Linus Torvalds785373b2017-08-29 09:11:06 -07001466 address = pvmw.address;
1467
Mike Kravetz336bf302020-11-13 22:52:16 -08001468 if (PageHuge(page) && !PageAnon(page)) {
Mike Kravetzc0d03812020-04-01 21:11:05 -07001469 /*
1470 * To call huge_pmd_unshare, i_mmap_rwsem must be
1471 * held in write mode. Caller needs to explicitly
1472 * do this outside rmap routines.
1473 */
1474 VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));
Mike Kravetz34ae2042020-08-11 18:31:38 -07001475 if (huge_pmd_unshare(mm, vma, &address, pvmw.pte)) {
Mike Kravetz017b1662018-10-05 15:51:29 -07001476 /*
1477 * huge_pmd_unshare unmapped an entire PMD
1478 * page. There is no way of knowing exactly
1479 * which PMDs may be cached for this mm, so
1480 * we must flush them all. start/end were
1481 * already adjusted above to cover this range.
1482 */
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001483 flush_cache_range(vma, range.start, range.end);
1484 flush_tlb_range(vma, range.start, range.end);
1485 mmu_notifier_invalidate_range(mm, range.start,
1486 range.end);
Mike Kravetz017b1662018-10-05 15:51:29 -07001487
1488 /*
1489 * The ref count of the PMD page was dropped
1490 * which is part of the way map counting
1491 * is done for shared PMDs. Return 'true'
1492 * here. When there is no other sharing,
1493 * huge_pmd_unshare returns false and we will
1494 * unmap the actual page and drop map count
1495 * to zero.
1496 */
1497 page_vma_mapped_walk_done(&pvmw);
1498 break;
1499 }
1500 }
Kirill A. Shutemov83462422017-03-09 16:17:20 -08001501
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001502 /* Nuke the page table entry. */
Linus Torvalds785373b2017-08-29 09:11:06 -07001503 flush_cache_page(vma, address, pte_pfn(*pvmw.pte));
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001504 if (should_defer_flush(mm, flags)) {
1505 /*
1506 * We clear the PTE but do not flush so potentially
1507 * a remote CPU could still be writing to the page.
1508 * If the entry was previously clean then the
1509 * architecture must guarantee that a clear->dirty
1510 * transition on a cached TLB entry is written through
1511 * and traps if the PTE is unmapped.
1512 */
Linus Torvalds785373b2017-08-29 09:11:06 -07001513 pteval = ptep_get_and_clear(mm, address, pvmw.pte);
Mel Gorman72b252a2015-09-04 15:47:32 -07001514
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001515 set_tlb_ubc_flush_pending(mm, pte_dirty(pteval));
Naoya Horiguchi5d317b22015-11-05 18:47:14 -08001516 } else {
Linus Torvalds785373b2017-08-29 09:11:06 -07001517 pteval = ptep_clear_flush(vma, address, pvmw.pte);
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001518 }
1519
1520 /* Move the dirty bit to the page. Now the pte is gone. */
1521 if (pte_dirty(pteval))
1522 set_page_dirty(page);
1523
1524 /* Update high watermark before we lower rss */
1525 update_hiwater_rss(mm);
1526
1527 if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) {
Punit Agrawal5fd27b82017-07-06 15:39:53 -07001528 pteval = swp_entry_to_pte(make_hwpoison_entry(subpage));
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001529 if (PageHuge(page)) {
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001530 hugetlb_count_sub(compound_nr(page), mm);
Linus Torvalds785373b2017-08-29 09:11:06 -07001531 set_huge_swap_pte_at(mm, address,
Punit Agrawal5fd27b82017-07-06 15:39:53 -07001532 pvmw.pte, pteval,
1533 vma_mmu_pagesize(vma));
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001534 } else {
1535 dec_mm_counter(mm, mm_counter(page));
Linus Torvalds785373b2017-08-29 09:11:06 -07001536 set_pte_at(mm, address, pvmw.pte, pteval);
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001537 }
1538
Christian Borntraegerbce73e42018-07-13 16:58:52 -07001539 } else if (pte_unused(pteval) && !userfaultfd_armed(vma)) {
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001540 /*
1541 * The guest indicated that the page content is of no
1542 * interest anymore. Simply discard the pte, vmscan
1543 * will take care of the rest.
Christian Borntraegerbce73e42018-07-13 16:58:52 -07001544 * A future reference will then fault in a new zero
1545 * page. When userfaultfd is active, we must not drop
1546 * this page though, as its main user (postcopy
1547 * migration) will not expect userfaults on already
1548 * copied pages.
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001549 */
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001550 dec_mm_counter(mm, mm_counter(page));
Jérôme Glisse0f108512017-11-15 17:34:07 -08001551 /* We have to invalidate as we cleared the pte */
1552 mmu_notifier_invalidate_range(mm, address,
1553 address + PAGE_SIZE);
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001554 } else if (PageAnon(page)) {
1555 swp_entry_t entry = { .val = page_private(subpage) };
1556 pte_t swp_pte;
1557 /*
1558 * Store the swap location in the pte.
1559 * See handle_pte_fault() ...
1560 */
Minchan Kimeb94a872017-05-03 14:52:36 -07001561 if (unlikely(PageSwapBacked(page) != PageSwapCache(page))) {
1562 WARN_ON_ONCE(1);
Minchan Kim83612a92017-05-03 14:54:30 -07001563 ret = false;
Jérôme Glisse369ea822017-08-31 17:17:27 -04001564 /* We have to invalidate as we cleared the pte */
Jérôme Glisse0f108512017-11-15 17:34:07 -08001565 mmu_notifier_invalidate_range(mm, address,
1566 address + PAGE_SIZE);
Minchan Kimeb94a872017-05-03 14:52:36 -07001567 page_vma_mapped_walk_done(&pvmw);
1568 break;
1569 }
Minchan Kim854e9ed2016-01-15 16:54:53 -08001570
Shaohua Li802a3a92017-05-03 14:52:32 -07001571 /* MADV_FREE page check */
1572 if (!PageSwapBacked(page)) {
1573 if (!PageDirty(page)) {
Jérôme Glisse0f108512017-11-15 17:34:07 -08001574 /* Invalidate as we cleared the pte */
1575 mmu_notifier_invalidate_range(mm,
1576 address, address + PAGE_SIZE);
Shaohua Li802a3a92017-05-03 14:52:32 -07001577 dec_mm_counter(mm, MM_ANONPAGES);
1578 goto discard;
1579 }
1580
1581 /*
1582 * If the page was redirtied, it cannot be
1583 * discarded. Remap the page to page table.
1584 */
Linus Torvalds785373b2017-08-29 09:11:06 -07001585 set_pte_at(mm, address, pvmw.pte, pteval);
Minchan Kim18863d32017-05-03 14:54:04 -07001586 SetPageSwapBacked(page);
Minchan Kime4b82222017-05-03 14:54:27 -07001587 ret = false;
Shaohua Li802a3a92017-05-03 14:52:32 -07001588 page_vma_mapped_walk_done(&pvmw);
1589 break;
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001590 }
1591
1592 if (swap_duplicate(entry) < 0) {
Linus Torvalds785373b2017-08-29 09:11:06 -07001593 set_pte_at(mm, address, pvmw.pte, pteval);
Minchan Kime4b82222017-05-03 14:54:27 -07001594 ret = false;
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001595 page_vma_mapped_walk_done(&pvmw);
1596 break;
1597 }
Khalid Azizca827d52018-02-21 10:15:44 -07001598 if (arch_unmap_one(mm, vma, address, pteval) < 0) {
1599 set_pte_at(mm, address, pvmw.pte, pteval);
1600 ret = false;
1601 page_vma_mapped_walk_done(&pvmw);
1602 break;
1603 }
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001604 if (list_empty(&mm->mmlist)) {
1605 spin_lock(&mmlist_lock);
1606 if (list_empty(&mm->mmlist))
1607 list_add(&mm->mmlist, &init_mm.mmlist);
1608 spin_unlock(&mmlist_lock);
1609 }
Minchan Kim854e9ed2016-01-15 16:54:53 -08001610 dec_mm_counter(mm, MM_ANONPAGES);
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001611 inc_mm_counter(mm, MM_SWAPENTS);
1612 swp_pte = swp_entry_to_pte(entry);
1613 if (pte_soft_dirty(pteval))
1614 swp_pte = pte_swp_mksoft_dirty(swp_pte);
Peter Xuf45ec5f2020-04-06 20:06:01 -07001615 if (pte_uffd_wp(pteval))
1616 swp_pte = pte_swp_mkuffd_wp(swp_pte);
Linus Torvalds785373b2017-08-29 09:11:06 -07001617 set_pte_at(mm, address, pvmw.pte, swp_pte);
Jérôme Glisse0f108512017-11-15 17:34:07 -08001618 /* Invalidate as we cleared the pte */
1619 mmu_notifier_invalidate_range(mm, address,
1620 address + PAGE_SIZE);
1621 } else {
1622 /*
Hugh Dickins906f9cd2018-11-30 14:10:13 -08001623 * This is a locked file-backed page, thus it cannot
1624 * be removed from the page cache and replaced by a new
1625 * page before mmu_notifier_invalidate_range_end, so no
Jérôme Glisse0f108512017-11-15 17:34:07 -08001626 * concurrent thread might update its page table to
1627 * point at new page while a device still is using this
1628 * page.
1629 *
Mike Rapoportad56b732018-03-21 21:22:47 +02001630 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08001631 */
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001632 dec_mm_counter(mm, mm_counter_file(page));
Jérôme Glisse0f108512017-11-15 17:34:07 -08001633 }
Minchan Kim854e9ed2016-01-15 16:54:53 -08001634discard:
Jérôme Glisse0f108512017-11-15 17:34:07 -08001635 /*
1636 * No need to call mmu_notifier_invalidate_range() it has be
1637 * done above for all cases requiring it to happen under page
1638 * table lock before mmu_notifier_invalidate_range_end()
1639 *
Mike Rapoportad56b732018-03-21 21:22:47 +02001640 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08001641 */
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001642 page_remove_rmap(subpage, PageHuge(page));
1643 put_page(page);
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08001644 }
Jérôme Glisse369ea822017-08-31 17:17:27 -04001645
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001646 mmu_notifier_invalidate_range_end(&range);
Jérôme Glisse369ea822017-08-31 17:17:27 -04001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return ret;
1649}
1650
Joonsoo Kim52629502014-01-21 15:49:50 -08001651static bool invalid_migration_vma(struct vm_area_struct *vma, void *arg)
1652{
Anshuman Khandual222100e2020-04-01 21:07:52 -07001653 return vma_is_temporary_stack(vma);
Joonsoo Kim52629502014-01-21 15:49:50 -08001654}
1655
Miaohe Linb7e188e2021-02-25 17:18:03 -08001656static int page_not_mapped(struct page *page)
Joonsoo Kim52629502014-01-21 15:49:50 -08001657{
Miaohe Linb7e188e2021-02-25 17:18:03 -08001658 return !page_mapped(page);
Kirill A. Shutemov2a52bcb2016-03-17 14:20:04 -07001659}
Joonsoo Kim52629502014-01-21 15:49:50 -08001660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661/**
1662 * try_to_unmap - try to remove all page table mappings to a page
1663 * @page: the page to get unmapped
Andi Kleen14fa31b2009-09-16 11:50:10 +02001664 * @flags: action and flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 *
1666 * Tries to remove all the page table entries which are mapping this
1667 * page, used in the pageout path. Caller must hold the page lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 *
Yang Shi1fb08ac2021-06-30 18:52:01 -07001669 * It is the caller's responsibility to check if the page is still
1670 * mapped when needed (use TTU_SYNC to prevent accounting races).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 */
Yang Shi1fb08ac2021-06-30 18:52:01 -07001672void try_to_unmap(struct page *page, enum ttu_flags flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Joonsoo Kim52629502014-01-21 15:49:50 -08001674 struct rmap_walk_control rwc = {
1675 .rmap_one = try_to_unmap_one,
Shaohua Li802a3a92017-05-03 14:52:32 -07001676 .arg = (void *)flags,
Miaohe Linb7e188e2021-02-25 17:18:03 -08001677 .done = page_not_mapped,
Joonsoo Kim52629502014-01-21 15:49:50 -08001678 .anon_lock = page_lock_anon_vma_read,
1679 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Alistair Popplea98a2f02021-06-30 18:54:16 -07001681 if (flags & TTU_RMAP_LOCKED)
1682 rmap_walk_locked(page, &rwc);
1683 else
1684 rmap_walk(page, &rwc);
1685}
1686
1687/*
1688 * @arg: enum ttu_flags will be passed to this argument.
1689 *
1690 * If TTU_SPLIT_HUGE_PMD is specified any PMD mappings will be split into PTEs
Hugh Dickins64b586d2021-07-07 13:06:17 -07001691 * containing migration entries.
Alistair Popplea98a2f02021-06-30 18:54:16 -07001692 */
1693static bool try_to_migrate_one(struct page *page, struct vm_area_struct *vma,
1694 unsigned long address, void *arg)
1695{
1696 struct mm_struct *mm = vma->vm_mm;
1697 struct page_vma_mapped_walk pvmw = {
1698 .page = page,
1699 .vma = vma,
1700 .address = address,
1701 };
1702 pte_t pteval;
1703 struct page *subpage;
1704 bool ret = true;
1705 struct mmu_notifier_range range;
1706 enum ttu_flags flags = (enum ttu_flags)(long)arg;
1707
Alistair Popplea98a2f02021-06-30 18:54:16 -07001708 /*
1709 * When racing against e.g. zap_pte_range() on another cpu,
1710 * in between its ptep_get_and_clear_full() and page_remove_rmap(),
1711 * try_to_migrate() may return before page_mapped() has become false,
1712 * if page table locking is skipped: use TTU_SYNC to wait for that.
1713 */
1714 if (flags & TTU_SYNC)
1715 pvmw.flags = PVMW_SYNC;
1716
1717 /*
1718 * unmap_page() in mm/huge_memory.c is the only user of migration with
1719 * TTU_SPLIT_HUGE_PMD and it wants to freeze.
1720 */
1721 if (flags & TTU_SPLIT_HUGE_PMD)
1722 split_huge_pmd_address(vma, address, true, page);
1723
1724 /*
1725 * For THP, we have to assume the worse case ie pmd for invalidation.
1726 * For hugetlb, it could be much worse if we need to do pud
1727 * invalidation in the case of pmd sharing.
1728 *
1729 * Note that the page can not be free in this function as call of
1730 * try_to_unmap() must hold a reference on the page.
1731 */
1732 range.end = PageKsm(page) ?
1733 address + PAGE_SIZE : vma_address_end(page, vma);
1734 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
1735 address, range.end);
1736 if (PageHuge(page)) {
1737 /*
1738 * If sharing is possible, start and end will be adjusted
1739 * accordingly.
1740 */
1741 adjust_range_if_pmd_sharing_possible(vma, &range.start,
1742 &range.end);
1743 }
1744 mmu_notifier_invalidate_range_start(&range);
1745
1746 while (page_vma_mapped_walk(&pvmw)) {
1747#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1748 /* PMD-mapped THP migration entry */
1749 if (!pvmw.pte) {
1750 VM_BUG_ON_PAGE(PageHuge(page) ||
1751 !PageTransCompound(page), page);
1752
1753 set_pmd_migration_entry(&pvmw, page);
1754 continue;
1755 }
1756#endif
1757
1758 /* Unexpected PMD-mapped THP? */
1759 VM_BUG_ON_PAGE(!pvmw.pte, page);
1760
1761 subpage = page - page_to_pfn(page) + pte_pfn(*pvmw.pte);
1762 address = pvmw.address;
1763
1764 if (PageHuge(page) && !PageAnon(page)) {
1765 /*
1766 * To call huge_pmd_unshare, i_mmap_rwsem must be
1767 * held in write mode. Caller needs to explicitly
1768 * do this outside rmap routines.
1769 */
1770 VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));
1771 if (huge_pmd_unshare(mm, vma, &address, pvmw.pte)) {
1772 /*
1773 * huge_pmd_unshare unmapped an entire PMD
1774 * page. There is no way of knowing exactly
1775 * which PMDs may be cached for this mm, so
1776 * we must flush them all. start/end were
1777 * already adjusted above to cover this range.
1778 */
1779 flush_cache_range(vma, range.start, range.end);
1780 flush_tlb_range(vma, range.start, range.end);
1781 mmu_notifier_invalidate_range(mm, range.start,
1782 range.end);
1783
1784 /*
1785 * The ref count of the PMD page was dropped
1786 * which is part of the way map counting
1787 * is done for shared PMDs. Return 'true'
1788 * here. When there is no other sharing,
1789 * huge_pmd_unshare returns false and we will
1790 * unmap the actual page and drop map count
1791 * to zero.
1792 */
1793 page_vma_mapped_walk_done(&pvmw);
1794 break;
1795 }
1796 }
1797
1798 /* Nuke the page table entry. */
1799 flush_cache_page(vma, address, pte_pfn(*pvmw.pte));
1800 pteval = ptep_clear_flush(vma, address, pvmw.pte);
1801
1802 /* Move the dirty bit to the page. Now the pte is gone. */
1803 if (pte_dirty(pteval))
1804 set_page_dirty(page);
1805
1806 /* Update high watermark before we lower rss */
1807 update_hiwater_rss(mm);
1808
1809 if (is_zone_device_page(page)) {
1810 swp_entry_t entry;
1811 pte_t swp_pte;
1812
1813 /*
1814 * Store the pfn of the page in a special migration
1815 * pte. do_swap_page() will wait until the migration
1816 * pte is removed and then restart fault handling.
1817 */
1818 entry = make_readable_migration_entry(
1819 page_to_pfn(page));
1820 swp_pte = swp_entry_to_pte(entry);
1821
1822 /*
1823 * pteval maps a zone device page and is therefore
1824 * a swap pte.
1825 */
1826 if (pte_swp_soft_dirty(pteval))
1827 swp_pte = pte_swp_mksoft_dirty(swp_pte);
1828 if (pte_swp_uffd_wp(pteval))
1829 swp_pte = pte_swp_mkuffd_wp(swp_pte);
1830 set_pte_at(mm, pvmw.address, pvmw.pte, swp_pte);
1831 /*
1832 * No need to invalidate here it will synchronize on
1833 * against the special swap migration pte.
1834 *
1835 * The assignment to subpage above was computed from a
1836 * swap PTE which results in an invalid pointer.
1837 * Since only PAGE_SIZE pages can currently be
1838 * migrated, just set it to page. This will need to be
1839 * changed when hugepage migrations to device private
1840 * memory are supported.
1841 */
1842 subpage = page;
1843 } else if (PageHWPoison(page)) {
1844 pteval = swp_entry_to_pte(make_hwpoison_entry(subpage));
1845 if (PageHuge(page)) {
1846 hugetlb_count_sub(compound_nr(page), mm);
1847 set_huge_swap_pte_at(mm, address,
1848 pvmw.pte, pteval,
1849 vma_mmu_pagesize(vma));
1850 } else {
1851 dec_mm_counter(mm, mm_counter(page));
1852 set_pte_at(mm, address, pvmw.pte, pteval);
1853 }
1854
1855 } else if (pte_unused(pteval) && !userfaultfd_armed(vma)) {
1856 /*
1857 * The guest indicated that the page content is of no
1858 * interest anymore. Simply discard the pte, vmscan
1859 * will take care of the rest.
1860 * A future reference will then fault in a new zero
1861 * page. When userfaultfd is active, we must not drop
1862 * this page though, as its main user (postcopy
1863 * migration) will not expect userfaults on already
1864 * copied pages.
1865 */
1866 dec_mm_counter(mm, mm_counter(page));
1867 /* We have to invalidate as we cleared the pte */
1868 mmu_notifier_invalidate_range(mm, address,
1869 address + PAGE_SIZE);
1870 } else {
1871 swp_entry_t entry;
1872 pte_t swp_pte;
1873
1874 if (arch_unmap_one(mm, vma, address, pteval) < 0) {
1875 set_pte_at(mm, address, pvmw.pte, pteval);
1876 ret = false;
1877 page_vma_mapped_walk_done(&pvmw);
1878 break;
1879 }
1880
1881 /*
1882 * Store the pfn of the page in a special migration
1883 * pte. do_swap_page() will wait until the migration
1884 * pte is removed and then restart fault handling.
1885 */
1886 if (pte_write(pteval))
1887 entry = make_writable_migration_entry(
1888 page_to_pfn(subpage));
1889 else
1890 entry = make_readable_migration_entry(
1891 page_to_pfn(subpage));
1892
1893 swp_pte = swp_entry_to_pte(entry);
1894 if (pte_soft_dirty(pteval))
1895 swp_pte = pte_swp_mksoft_dirty(swp_pte);
1896 if (pte_uffd_wp(pteval))
1897 swp_pte = pte_swp_mkuffd_wp(swp_pte);
1898 set_pte_at(mm, address, pvmw.pte, swp_pte);
1899 /*
1900 * No need to invalidate here it will synchronize on
1901 * against the special swap migration pte.
1902 */
1903 }
1904
1905 /*
1906 * No need to call mmu_notifier_invalidate_range() it has be
1907 * done above for all cases requiring it to happen under page
1908 * table lock before mmu_notifier_invalidate_range_end()
1909 *
1910 * See Documentation/vm/mmu_notifier.rst
1911 */
1912 page_remove_rmap(subpage, PageHuge(page));
1913 put_page(page);
1914 }
1915
1916 mmu_notifier_invalidate_range_end(&range);
1917
1918 return ret;
1919}
1920
1921/**
1922 * try_to_migrate - try to replace all page table mappings with swap entries
1923 * @page: the page to replace page table entries for
1924 * @flags: action and flags
1925 *
1926 * Tries to remove all the page table entries which are mapping this page and
1927 * replace them with special swap entries. Caller must hold the page lock.
Alistair Popplea98a2f02021-06-30 18:54:16 -07001928 */
1929void try_to_migrate(struct page *page, enum ttu_flags flags)
1930{
1931 struct rmap_walk_control rwc = {
1932 .rmap_one = try_to_migrate_one,
1933 .arg = (void *)flags,
1934 .done = page_not_mapped,
1935 .anon_lock = page_lock_anon_vma_read,
1936 };
1937
1938 /*
1939 * Migration always ignores mlock and only supports TTU_RMAP_LOCKED and
1940 * TTU_SPLIT_HUGE_PMD and TTU_SYNC flags.
1941 */
1942 if (WARN_ON_ONCE(flags & ~(TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD |
1943 TTU_SYNC)))
1944 return;
1945
Hugh Dickins6c855fc2021-07-07 13:13:33 -07001946 if (is_zone_device_page(page) && !is_device_private_page(page))
1947 return;
1948
Joonsoo Kim52629502014-01-21 15:49:50 -08001949 /*
1950 * During exec, a temporary VMA is setup and later moved.
1951 * The VMA is moved under the anon_vma lock but not the
1952 * page tables leading to a race where migration cannot
1953 * find the migration ptes. Rather than increasing the
1954 * locking requirements of exec(), migration skips
1955 * temporary VMAs until after exec() completes.
1956 */
Alistair Popplea98a2f02021-06-30 18:54:16 -07001957 if (!PageKsm(page) && PageAnon(page))
Joonsoo Kim52629502014-01-21 15:49:50 -08001958 rwc.invalid_vma = invalid_migration_vma;
1959
Kirill A. Shutemov2a52bcb2016-03-17 14:20:04 -07001960 if (flags & TTU_RMAP_LOCKED)
Minchan Kim33fc80e2017-05-03 14:54:17 -07001961 rmap_walk_locked(page, &rwc);
Kirill A. Shutemov2a52bcb2016-03-17 14:20:04 -07001962 else
Minchan Kim33fc80e2017-05-03 14:54:17 -07001963 rmap_walk(page, &rwc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
Nikita Danilov81b40822005-05-01 08:58:36 -07001965
Alistair Popplecd627342021-06-30 18:54:12 -07001966/*
1967 * Walks the vma's mapping a page and mlocks the page if any locked vma's are
1968 * found. Once one is found the page is locked and the scan can be terminated.
Nick Pigginb291f002008-10-18 20:26:44 -07001969 */
Alistair Popplecd627342021-06-30 18:54:12 -07001970static bool page_mlock_one(struct page *page, struct vm_area_struct *vma,
1971 unsigned long address, void *unused)
1972{
1973 struct page_vma_mapped_walk pvmw = {
1974 .page = page,
1975 .vma = vma,
1976 .address = address,
1977 };
Minchan Kim854e9ed2016-01-15 16:54:53 -08001978
Alistair Popplecd627342021-06-30 18:54:12 -07001979 /* An un-locked vma doesn't have any pages to lock, continue the scan */
1980 if (!(vma->vm_flags & VM_LOCKED))
1981 return true;
1982
1983 while (page_vma_mapped_walk(&pvmw)) {
1984 /*
1985 * Need to recheck under the ptl to serialise with
1986 * __munlock_pagevec_fill() after VM_LOCKED is cleared in
1987 * munlock_vma_pages_range().
1988 */
1989 if (vma->vm_flags & VM_LOCKED) {
Hugh Dickinsd9770fc2021-07-07 13:08:53 -07001990 /*
Hugh Dickinsefdb6722021-07-11 20:10:49 -07001991 * PTE-mapped THP are never marked as mlocked; but
1992 * this function is never called on a DoubleMap THP,
1993 * nor on an Anon THP (which may still be PTE-mapped
1994 * after DoubleMap was cleared).
Hugh Dickinsd9770fc2021-07-07 13:08:53 -07001995 */
1996 mlock_vma_page(page);
Hugh Dickins023e1a82021-07-07 13:11:24 -07001997 /*
1998 * No need to scan further once the page is marked
1999 * as mlocked.
2000 */
Alistair Popplecd627342021-06-30 18:54:12 -07002001 page_vma_mapped_walk_done(&pvmw);
Hugh Dickins023e1a82021-07-07 13:11:24 -07002002 return false;
Alistair Popplecd627342021-06-30 18:54:12 -07002003 }
Alistair Popplecd627342021-06-30 18:54:12 -07002004 }
2005
2006 return true;
2007}
2008
2009/**
2010 * page_mlock - try to mlock a page
2011 * @page: the page to be mlocked
2012 *
2013 * Called from munlock code. Checks all of the VMAs mapping the page and mlocks
2014 * the page if any are found. The page will be returned with PG_mlocked cleared
2015 * if it is not mapped by any locked vmas.
2016 */
2017void page_mlock(struct page *page)
Minchan Kim192d7232017-05-03 14:54:10 -07002018{
Joonsoo Kime8351ac2014-01-21 15:49:52 -08002019 struct rmap_walk_control rwc = {
Alistair Popplecd627342021-06-30 18:54:12 -07002020 .rmap_one = page_mlock_one,
Joonsoo Kime8351ac2014-01-21 15:49:52 -08002021 .done = page_not_mapped,
Joonsoo Kime8351ac2014-01-21 15:49:52 -08002022 .anon_lock = page_lock_anon_vma_read,
2023
2024 };
2025
Sasha Levin309381fea2014-01-23 15:52:54 -08002026 VM_BUG_ON_PAGE(!PageLocked(page) || PageLRU(page), page);
Minchan Kim192d7232017-05-03 14:54:10 -07002027 VM_BUG_ON_PAGE(PageCompound(page) && PageDoubleMap(page), page);
Nick Pigginb291f002008-10-18 20:26:44 -07002028
Hugh Dickinsefdb6722021-07-11 20:10:49 -07002029 /* Anon THP are only marked as mlocked when singly mapped */
2030 if (PageTransCompound(page) && PageAnon(page))
2031 return;
2032
Minchan Kim192d7232017-05-03 14:54:10 -07002033 rmap_walk(page, &rwc);
Nick Pigginb291f002008-10-18 20:26:44 -07002034}
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002035
Alistair Poppleb756a3b2021-06-30 18:54:25 -07002036#ifdef CONFIG_DEVICE_PRIVATE
2037struct make_exclusive_args {
2038 struct mm_struct *mm;
2039 unsigned long address;
2040 void *owner;
2041 bool valid;
2042};
2043
2044static bool page_make_device_exclusive_one(struct page *page,
2045 struct vm_area_struct *vma, unsigned long address, void *priv)
2046{
2047 struct mm_struct *mm = vma->vm_mm;
2048 struct page_vma_mapped_walk pvmw = {
2049 .page = page,
2050 .vma = vma,
2051 .address = address,
2052 };
2053 struct make_exclusive_args *args = priv;
2054 pte_t pteval;
2055 struct page *subpage;
2056 bool ret = true;
2057 struct mmu_notifier_range range;
2058 swp_entry_t entry;
2059 pte_t swp_pte;
2060
2061 mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, vma,
2062 vma->vm_mm, address, min(vma->vm_end,
2063 address + page_size(page)), args->owner);
2064 mmu_notifier_invalidate_range_start(&range);
2065
2066 while (page_vma_mapped_walk(&pvmw)) {
2067 /* Unexpected PMD-mapped THP? */
2068 VM_BUG_ON_PAGE(!pvmw.pte, page);
2069
2070 if (!pte_present(*pvmw.pte)) {
2071 ret = false;
2072 page_vma_mapped_walk_done(&pvmw);
2073 break;
2074 }
2075
2076 subpage = page - page_to_pfn(page) + pte_pfn(*pvmw.pte);
2077 address = pvmw.address;
2078
2079 /* Nuke the page table entry. */
2080 flush_cache_page(vma, address, pte_pfn(*pvmw.pte));
2081 pteval = ptep_clear_flush(vma, address, pvmw.pte);
2082
2083 /* Move the dirty bit to the page. Now the pte is gone. */
2084 if (pte_dirty(pteval))
2085 set_page_dirty(page);
2086
2087 /*
2088 * Check that our target page is still mapped at the expected
2089 * address.
2090 */
2091 if (args->mm == mm && args->address == address &&
2092 pte_write(pteval))
2093 args->valid = true;
2094
2095 /*
2096 * Store the pfn of the page in a special migration
2097 * pte. do_swap_page() will wait until the migration
2098 * pte is removed and then restart fault handling.
2099 */
2100 if (pte_write(pteval))
2101 entry = make_writable_device_exclusive_entry(
2102 page_to_pfn(subpage));
2103 else
2104 entry = make_readable_device_exclusive_entry(
2105 page_to_pfn(subpage));
2106 swp_pte = swp_entry_to_pte(entry);
2107 if (pte_soft_dirty(pteval))
2108 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2109 if (pte_uffd_wp(pteval))
2110 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2111
2112 set_pte_at(mm, address, pvmw.pte, swp_pte);
2113
2114 /*
2115 * There is a reference on the page for the swap entry which has
2116 * been removed, so shouldn't take another.
2117 */
2118 page_remove_rmap(subpage, false);
2119 }
2120
2121 mmu_notifier_invalidate_range_end(&range);
2122
2123 return ret;
2124}
2125
2126/**
2127 * page_make_device_exclusive - mark the page exclusively owned by a device
2128 * @page: the page to replace page table entries for
2129 * @mm: the mm_struct where the page is expected to be mapped
2130 * @address: address where the page is expected to be mapped
2131 * @owner: passed to MMU_NOTIFY_EXCLUSIVE range notifier callbacks
2132 *
2133 * Tries to remove all the page table entries which are mapping this page and
2134 * replace them with special device exclusive swap entries to grant a device
2135 * exclusive access to the page. Caller must hold the page lock.
2136 *
2137 * Returns false if the page is still mapped, or if it could not be unmapped
2138 * from the expected address. Otherwise returns true (success).
2139 */
2140static bool page_make_device_exclusive(struct page *page, struct mm_struct *mm,
2141 unsigned long address, void *owner)
2142{
2143 struct make_exclusive_args args = {
2144 .mm = mm,
2145 .address = address,
2146 .owner = owner,
2147 .valid = false,
2148 };
2149 struct rmap_walk_control rwc = {
2150 .rmap_one = page_make_device_exclusive_one,
2151 .done = page_not_mapped,
2152 .anon_lock = page_lock_anon_vma_read,
2153 .arg = &args,
2154 };
2155
2156 /*
2157 * Restrict to anonymous pages for now to avoid potential writeback
2158 * issues. Also tail pages shouldn't be passed to rmap_walk so skip
2159 * those.
2160 */
2161 if (!PageAnon(page) || PageTail(page))
2162 return false;
2163
2164 rmap_walk(page, &rwc);
2165
2166 return args.valid && !page_mapcount(page);
2167}
2168
2169/**
2170 * make_device_exclusive_range() - Mark a range for exclusive use by a device
2171 * @mm: mm_struct of assoicated target process
2172 * @start: start of the region to mark for exclusive device access
2173 * @end: end address of region
2174 * @pages: returns the pages which were successfully marked for exclusive access
2175 * @owner: passed to MMU_NOTIFY_EXCLUSIVE range notifier to allow filtering
2176 *
2177 * Returns: number of pages found in the range by GUP. A page is marked for
2178 * exclusive access only if the page pointer is non-NULL.
2179 *
2180 * This function finds ptes mapping page(s) to the given address range, locks
2181 * them and replaces mappings with special swap entries preventing userspace CPU
2182 * access. On fault these entries are replaced with the original mapping after
2183 * calling MMU notifiers.
2184 *
2185 * A driver using this to program access from a device must use a mmu notifier
2186 * critical section to hold a device specific lock during programming. Once
2187 * programming is complete it should drop the page lock and reference after
2188 * which point CPU access to the page will revoke the exclusive access.
2189 */
2190int make_device_exclusive_range(struct mm_struct *mm, unsigned long start,
2191 unsigned long end, struct page **pages,
2192 void *owner)
2193{
2194 long npages = (end - start) >> PAGE_SHIFT;
2195 long i;
2196
2197 npages = get_user_pages_remote(mm, start, npages,
2198 FOLL_GET | FOLL_WRITE | FOLL_SPLIT_PMD,
2199 pages, NULL, NULL);
2200 if (npages < 0)
2201 return npages;
2202
2203 for (i = 0; i < npages; i++, start += PAGE_SIZE) {
2204 if (!trylock_page(pages[i])) {
2205 put_page(pages[i]);
2206 pages[i] = NULL;
2207 continue;
2208 }
2209
2210 if (!page_make_device_exclusive(pages[i], mm, start, owner)) {
2211 unlock_page(pages[i]);
2212 put_page(pages[i]);
2213 pages[i] = NULL;
2214 }
2215 }
2216
2217 return npages;
2218}
2219EXPORT_SYMBOL_GPL(make_device_exclusive_range);
2220#endif
2221
Peter Zijlstra01d8b202011-03-22 16:32:49 -07002222void __put_anon_vma(struct anon_vma *anon_vma)
Rik van Riel76545062010-08-09 17:18:41 -07002223{
Peter Zijlstra01d8b202011-03-22 16:32:49 -07002224 struct anon_vma *root = anon_vma->root;
Rik van Riel76545062010-08-09 17:18:41 -07002225
Andrey Ryabinin624483f2014-06-06 19:09:30 +04002226 anon_vma_free(anon_vma);
Peter Zijlstra01d8b202011-03-22 16:32:49 -07002227 if (root != anon_vma && atomic_dec_and_test(&root->refcount))
2228 anon_vma_free(root);
Rik van Riel76545062010-08-09 17:18:41 -07002229}
Rik van Riel76545062010-08-09 17:18:41 -07002230
Joonsoo Kim0dd1c7b2014-01-21 15:49:49 -08002231static struct anon_vma *rmap_walk_anon_lock(struct page *page,
2232 struct rmap_walk_control *rwc)
Joonsoo Kimfaecd8d2014-01-21 15:49:46 -08002233{
2234 struct anon_vma *anon_vma;
2235
Joonsoo Kim0dd1c7b2014-01-21 15:49:49 -08002236 if (rwc->anon_lock)
2237 return rwc->anon_lock(page);
2238
Joonsoo Kimfaecd8d2014-01-21 15:49:46 -08002239 /*
2240 * Note: remove_migration_ptes() cannot use page_lock_anon_vma_read()
2241 * because that depends on page_mapped(); but not all its usages
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002242 * are holding mmap_lock. Users without mmap_lock are required to
Joonsoo Kimfaecd8d2014-01-21 15:49:46 -08002243 * take a reference count to prevent the anon_vma disappearing
2244 */
2245 anon_vma = page_anon_vma(page);
2246 if (!anon_vma)
2247 return NULL;
2248
2249 anon_vma_lock_read(anon_vma);
2250 return anon_vma;
2251}
2252
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002253/*
Joonsoo Kime8351ac2014-01-21 15:49:52 -08002254 * rmap_walk_anon - do something to anonymous page using the object-based
2255 * rmap method
2256 * @page: the page to be handled
2257 * @rwc: control variable according to each walk type
2258 *
2259 * Find all the mappings of a page using the mapping pointer and the vma chains
2260 * contained in the anon_vma struct it points to.
2261 *
Alistair Popplecd627342021-06-30 18:54:12 -07002262 * When called from page_mlock(), the mmap_lock of the mm containing the vma
Joonsoo Kime8351ac2014-01-21 15:49:52 -08002263 * where the page was found will be held for write. So, we won't recheck
2264 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
2265 * LOCKED.
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002266 */
Minchan Kim1df631a2017-05-03 14:54:23 -07002267static void rmap_walk_anon(struct page *page, struct rmap_walk_control *rwc,
Kirill A. Shutemovb9773192016-03-17 14:20:01 -07002268 bool locked)
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002269{
2270 struct anon_vma *anon_vma;
Kirill A. Shutemova8fa41ad2017-02-24 14:57:54 -08002271 pgoff_t pgoff_start, pgoff_end;
Rik van Riel5beb4932010-03-05 13:42:07 -08002272 struct anon_vma_chain *avc;
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002273
Kirill A. Shutemovb9773192016-03-17 14:20:01 -07002274 if (locked) {
2275 anon_vma = page_anon_vma(page);
2276 /* anon_vma disappear under us? */
2277 VM_BUG_ON_PAGE(!anon_vma, page);
2278 } else {
2279 anon_vma = rmap_walk_anon_lock(page, rwc);
2280 }
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002281 if (!anon_vma)
Minchan Kim1df631a2017-05-03 14:54:23 -07002282 return;
Joonsoo Kimfaecd8d2014-01-21 15:49:46 -08002283
Kirill A. Shutemova8fa41ad2017-02-24 14:57:54 -08002284 pgoff_start = page_to_pgoff(page);
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07002285 pgoff_end = pgoff_start + thp_nr_pages(page) - 1;
Kirill A. Shutemova8fa41ad2017-02-24 14:57:54 -08002286 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root,
2287 pgoff_start, pgoff_end) {
Rik van Riel5beb4932010-03-05 13:42:07 -08002288 struct vm_area_struct *vma = avc->vma;
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002289 unsigned long address = vma_address(page, vma);
Joonsoo Kim0dd1c7b2014-01-21 15:49:49 -08002290
Hugh Dickins494334e2021-06-15 18:23:56 -07002291 VM_BUG_ON_VMA(address == -EFAULT, vma);
Andrea Arcangeliad126952015-11-05 18:49:07 -08002292 cond_resched();
2293
Joonsoo Kim0dd1c7b2014-01-21 15:49:49 -08002294 if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
2295 continue;
2296
Minchan Kime4b82222017-05-03 14:54:27 -07002297 if (!rwc->rmap_one(page, vma, address, rwc->arg))
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002298 break;
Joonsoo Kim0dd1c7b2014-01-21 15:49:49 -08002299 if (rwc->done && rwc->done(page))
2300 break;
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002301 }
Kirill A. Shutemovb9773192016-03-17 14:20:01 -07002302
2303 if (!locked)
2304 anon_vma_unlock_read(anon_vma);
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002305}
2306
Joonsoo Kime8351ac2014-01-21 15:49:52 -08002307/*
2308 * rmap_walk_file - do something to file page using the object-based rmap method
2309 * @page: the page to be handled
2310 * @rwc: control variable according to each walk type
2311 *
2312 * Find all the mappings of a page using the mapping pointer and the vma chains
2313 * contained in the address_space struct it points to.
2314 *
Alistair Popplecd627342021-06-30 18:54:12 -07002315 * When called from page_mlock(), the mmap_lock of the mm containing the vma
Joonsoo Kime8351ac2014-01-21 15:49:52 -08002316 * where the page was found will be held for write. So, we won't recheck
2317 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
2318 * LOCKED.
2319 */
Minchan Kim1df631a2017-05-03 14:54:23 -07002320static void rmap_walk_file(struct page *page, struct rmap_walk_control *rwc,
Kirill A. Shutemovb9773192016-03-17 14:20:01 -07002321 bool locked)
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002322{
Kirill A. Shutemovb9773192016-03-17 14:20:01 -07002323 struct address_space *mapping = page_mapping(page);
Kirill A. Shutemova8fa41ad2017-02-24 14:57:54 -08002324 pgoff_t pgoff_start, pgoff_end;
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002325 struct vm_area_struct *vma;
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002326
Joonsoo Kim9f326242014-01-21 15:49:53 -08002327 /*
2328 * The page lock not only makes sure that page->mapping cannot
2329 * suddenly be NULLified by truncation, it makes sure that the
2330 * structure at mapping cannot be freed and reused yet,
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08002331 * so we can safely take mapping->i_mmap_rwsem.
Joonsoo Kim9f326242014-01-21 15:49:53 -08002332 */
Sasha Levin81d1b092014-10-09 15:28:10 -07002333 VM_BUG_ON_PAGE(!PageLocked(page), page);
Joonsoo Kim9f326242014-01-21 15:49:53 -08002334
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002335 if (!mapping)
Minchan Kim1df631a2017-05-03 14:54:23 -07002336 return;
Davidlohr Bueso3dec0ba2014-12-12 16:54:27 -08002337
Kirill A. Shutemova8fa41ad2017-02-24 14:57:54 -08002338 pgoff_start = page_to_pgoff(page);
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07002339 pgoff_end = pgoff_start + thp_nr_pages(page) - 1;
Kirill A. Shutemovb9773192016-03-17 14:20:01 -07002340 if (!locked)
2341 i_mmap_lock_read(mapping);
Kirill A. Shutemova8fa41ad2017-02-24 14:57:54 -08002342 vma_interval_tree_foreach(vma, &mapping->i_mmap,
2343 pgoff_start, pgoff_end) {
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002344 unsigned long address = vma_address(page, vma);
Joonsoo Kim0dd1c7b2014-01-21 15:49:49 -08002345
Hugh Dickins494334e2021-06-15 18:23:56 -07002346 VM_BUG_ON_VMA(address == -EFAULT, vma);
Andrea Arcangeliad126952015-11-05 18:49:07 -08002347 cond_resched();
2348
Joonsoo Kim0dd1c7b2014-01-21 15:49:49 -08002349 if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
2350 continue;
2351
Minchan Kime4b82222017-05-03 14:54:27 -07002352 if (!rwc->rmap_one(page, vma, address, rwc->arg))
Joonsoo Kim0dd1c7b2014-01-21 15:49:49 -08002353 goto done;
2354 if (rwc->done && rwc->done(page))
2355 goto done;
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002356 }
Joonsoo Kim0dd1c7b2014-01-21 15:49:49 -08002357
Joonsoo Kim0dd1c7b2014-01-21 15:49:49 -08002358done:
Kirill A. Shutemovb9773192016-03-17 14:20:01 -07002359 if (!locked)
2360 i_mmap_unlock_read(mapping);
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002361}
2362
Minchan Kim1df631a2017-05-03 14:54:23 -07002363void rmap_walk(struct page *page, struct rmap_walk_control *rwc)
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002364{
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002365 if (unlikely(PageKsm(page)))
Minchan Kim1df631a2017-05-03 14:54:23 -07002366 rmap_walk_ksm(page, rwc);
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002367 else if (PageAnon(page))
Minchan Kim1df631a2017-05-03 14:54:23 -07002368 rmap_walk_anon(page, rwc, false);
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002369 else
Minchan Kim1df631a2017-05-03 14:54:23 -07002370 rmap_walk_file(page, rwc, false);
Kirill A. Shutemovb9773192016-03-17 14:20:01 -07002371}
2372
2373/* Like rmap_walk, but caller holds relevant rmap lock */
Minchan Kim1df631a2017-05-03 14:54:23 -07002374void rmap_walk_locked(struct page *page, struct rmap_walk_control *rwc)
Kirill A. Shutemovb9773192016-03-17 14:20:01 -07002375{
2376 /* no ksm support for now */
2377 VM_BUG_ON_PAGE(PageKsm(page), page);
2378 if (PageAnon(page))
Minchan Kim1df631a2017-05-03 14:54:23 -07002379 rmap_walk_anon(page, rwc, true);
Kirill A. Shutemovb9773192016-03-17 14:20:01 -07002380 else
Minchan Kim1df631a2017-05-03 14:54:23 -07002381 rmap_walk_file(page, rwc, true);
Hugh Dickinse9995ef2009-12-14 17:59:31 -08002382}
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002383
Naoya Horiguchie3390f62010-06-15 13:18:13 +09002384#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002385/*
Kirill Tkhai451b9512018-12-28 00:39:31 -08002386 * The following two functions are for anonymous (private mapped) hugepages.
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002387 * Unlike common anonymous pages, anonymous hugepages have no accounting code
2388 * and no lru code, because we handle hugepages differently from common pages.
2389 */
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002390void hugepage_add_anon_rmap(struct page *page,
2391 struct vm_area_struct *vma, unsigned long address)
2392{
2393 struct anon_vma *anon_vma = vma->anon_vma;
2394 int first;
Naoya Horiguchia850ea32010-09-10 13:23:06 +09002395
2396 BUG_ON(!PageLocked(page));
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002397 BUG_ON(!anon_vma);
Hugh Dickins5dbe0af2011-05-28 13:17:04 -07002398 /* address might be in next vma when migration races vma_adjust */
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08002399 first = atomic_inc_and_test(compound_mapcount_ptr(page));
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002400 if (first)
Kirill Tkhai451b9512018-12-28 00:39:31 -08002401 __page_set_anon_rmap(page, vma, address, 0);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002402}
2403
2404void hugepage_add_new_anon_rmap(struct page *page,
2405 struct vm_area_struct *vma, unsigned long address)
2406{
2407 BUG_ON(address < vma->vm_start || address >= vma->vm_end);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08002408 atomic_set(compound_mapcount_ptr(page), 0);
John Hubbard47e29d32020-04-01 21:05:33 -07002409 if (hpage_pincount_available(page))
2410 atomic_set(compound_pincount_ptr(page), 0);
2411
Kirill Tkhai451b9512018-12-28 00:39:31 -08002412 __page_set_anon_rmap(page, vma, address, 1);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002413}
Naoya Horiguchie3390f62010-06-15 13:18:13 +09002414#endif /* CONFIG_HUGETLB_PAGE */