xref: /linux-6.15/include/linux/rmap.h (revision a13d0964)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_RMAP_H
3 #define _LINUX_RMAP_H
4 /*
5  * Declarations for Reverse Mapping functions in mm/rmap.c
6  */
7 
8 #include <linux/list.h>
9 #include <linux/slab.h>
10 #include <linux/mm.h>
11 #include <linux/rwsem.h>
12 #include <linux/memcontrol.h>
13 #include <linux/highmem.h>
14 #include <linux/pagemap.h>
15 #include <linux/memremap.h>
16 
17 /*
18  * The anon_vma heads a list of private "related" vmas, to scan if
19  * an anonymous page pointing to this anon_vma needs to be unmapped:
20  * the vmas on the list will be related by forking, or by splitting.
21  *
22  * Since vmas come and go as they are split and merged (particularly
23  * in mprotect), the mapping field of an anonymous page cannot point
24  * directly to a vma: instead it points to an anon_vma, on whose list
25  * the related vmas can be easily linked or unlinked.
26  *
27  * After unlinking the last vma on the list, we must garbage collect
28  * the anon_vma object itself: we're guaranteed no page can be
29  * pointing to this anon_vma once its vma list is empty.
30  */
31 struct anon_vma {
32 	struct anon_vma *root;		/* Root of this anon_vma tree */
33 	struct rw_semaphore rwsem;	/* W: modification, R: walking the list */
34 	/*
35 	 * The refcount is taken on an anon_vma when there is no
36 	 * guarantee that the vma of page tables will exist for
37 	 * the duration of the operation. A caller that takes
38 	 * the reference is responsible for clearing up the
39 	 * anon_vma if they are the last user on release
40 	 */
41 	atomic_t refcount;
42 
43 	/*
44 	 * Count of child anon_vmas. Equals to the count of all anon_vmas that
45 	 * have ->parent pointing to this one, including itself.
46 	 *
47 	 * This counter is used for making decision about reusing anon_vma
48 	 * instead of forking new one. See comments in function anon_vma_clone.
49 	 */
50 	unsigned long num_children;
51 	/* Count of VMAs whose ->anon_vma pointer points to this object. */
52 	unsigned long num_active_vmas;
53 
54 	struct anon_vma *parent;	/* Parent of this anon_vma */
55 
56 	/*
57 	 * NOTE: the LSB of the rb_root.rb_node is set by
58 	 * mm_take_all_locks() _after_ taking the above lock. So the
59 	 * rb_root must only be read/written after taking the above lock
60 	 * to be sure to see a valid next pointer. The LSB bit itself
61 	 * is serialized by a system wide lock only visible to
62 	 * mm_take_all_locks() (mm_all_locks_mutex).
63 	 */
64 
65 	/* Interval tree of private "related" vmas */
66 	struct rb_root_cached rb_root;
67 };
68 
69 /*
70  * The copy-on-write semantics of fork mean that an anon_vma
71  * can become associated with multiple processes. Furthermore,
72  * each child process will have its own anon_vma, where new
73  * pages for that process are instantiated.
74  *
75  * This structure allows us to find the anon_vmas associated
76  * with a VMA, or the VMAs associated with an anon_vma.
77  * The "same_vma" list contains the anon_vma_chains linking
78  * all the anon_vmas associated with this VMA.
79  * The "rb" field indexes on an interval tree the anon_vma_chains
80  * which link all the VMAs associated with this anon_vma.
81  */
82 struct anon_vma_chain {
83 	struct vm_area_struct *vma;
84 	struct anon_vma *anon_vma;
85 	struct list_head same_vma;   /* locked by mmap_lock & page_table_lock */
86 	struct rb_node rb;			/* locked by anon_vma->rwsem */
87 	unsigned long rb_subtree_last;
88 #ifdef CONFIG_DEBUG_VM_RB
89 	unsigned long cached_vma_start, cached_vma_last;
90 #endif
91 };
92 
93 enum ttu_flags {
94 	TTU_SPLIT_HUGE_PMD	= 0x4,	/* split huge PMD if any */
95 	TTU_IGNORE_MLOCK	= 0x8,	/* ignore mlock */
96 	TTU_SYNC		= 0x10,	/* avoid racy checks with PVMW_SYNC */
97 	TTU_HWPOISON		= 0x20,	/* do convert pte to hwpoison entry */
98 	TTU_BATCH_FLUSH		= 0x40,	/* Batch TLB flushes where possible
99 					 * and caller guarantees they will
100 					 * do a final flush if necessary */
101 	TTU_RMAP_LOCKED		= 0x80,	/* do not grab rmap lock:
102 					 * caller holds it */
103 };
104 
105 #ifdef CONFIG_MMU
106 static inline void get_anon_vma(struct anon_vma *anon_vma)
107 {
108 	atomic_inc(&anon_vma->refcount);
109 }
110 
111 void __put_anon_vma(struct anon_vma *anon_vma);
112 
113 static inline void put_anon_vma(struct anon_vma *anon_vma)
114 {
115 	if (atomic_dec_and_test(&anon_vma->refcount))
116 		__put_anon_vma(anon_vma);
117 }
118 
119 static inline void anon_vma_lock_write(struct anon_vma *anon_vma)
120 {
121 	down_write(&anon_vma->root->rwsem);
122 }
123 
124 static inline int anon_vma_trylock_write(struct anon_vma *anon_vma)
125 {
126 	return down_write_trylock(&anon_vma->root->rwsem);
127 }
128 
129 static inline void anon_vma_unlock_write(struct anon_vma *anon_vma)
130 {
131 	up_write(&anon_vma->root->rwsem);
132 }
133 
134 static inline void anon_vma_lock_read(struct anon_vma *anon_vma)
135 {
136 	down_read(&anon_vma->root->rwsem);
137 }
138 
139 static inline int anon_vma_trylock_read(struct anon_vma *anon_vma)
140 {
141 	return down_read_trylock(&anon_vma->root->rwsem);
142 }
143 
144 static inline void anon_vma_unlock_read(struct anon_vma *anon_vma)
145 {
146 	up_read(&anon_vma->root->rwsem);
147 }
148 
149 
150 /*
151  * anon_vma helper functions.
152  */
153 void anon_vma_init(void);	/* create anon_vma_cachep */
154 int  __anon_vma_prepare(struct vm_area_struct *);
155 void unlink_anon_vmas(struct vm_area_struct *);
156 int anon_vma_clone(struct vm_area_struct *, struct vm_area_struct *);
157 int anon_vma_fork(struct vm_area_struct *, struct vm_area_struct *);
158 
159 static inline int anon_vma_prepare(struct vm_area_struct *vma)
160 {
161 	if (likely(vma->anon_vma))
162 		return 0;
163 
164 	return __anon_vma_prepare(vma);
165 }
166 
167 static inline void anon_vma_merge(struct vm_area_struct *vma,
168 				  struct vm_area_struct *next)
169 {
170 	VM_BUG_ON_VMA(vma->anon_vma != next->anon_vma, vma);
171 	unlink_anon_vmas(next);
172 }
173 
174 struct anon_vma *folio_get_anon_vma(struct folio *folio);
175 
176 /* RMAP flags, currently only relevant for some anon rmap operations. */
177 typedef int __bitwise rmap_t;
178 
179 /*
180  * No special request: A mapped anonymous (sub)page is possibly shared between
181  * processes.
182  */
183 #define RMAP_NONE		((__force rmap_t)0)
184 
185 /* The anonymous (sub)page is exclusive to a single process. */
186 #define RMAP_EXCLUSIVE		((__force rmap_t)BIT(0))
187 
188 /*
189  * Internally, we're using an enum to specify the granularity. We make the
190  * compiler emit specialized code for each granularity.
191  */
192 enum rmap_level {
193 	RMAP_LEVEL_PTE = 0,
194 	RMAP_LEVEL_PMD,
195 };
196 
197 static inline void __folio_rmap_sanity_checks(struct folio *folio,
198 		struct page *page, int nr_pages, enum rmap_level level)
199 {
200 	/* hugetlb folios are handled separately. */
201 	VM_WARN_ON_FOLIO(folio_test_hugetlb(folio), folio);
202 	VM_WARN_ON_FOLIO(folio_test_large(folio) &&
203 			 !folio_test_large_rmappable(folio), folio);
204 
205 	VM_WARN_ON_ONCE(nr_pages <= 0);
206 	VM_WARN_ON_FOLIO(page_folio(page) != folio, folio);
207 	VM_WARN_ON_FOLIO(page_folio(page + nr_pages - 1) != folio, folio);
208 
209 	switch (level) {
210 	case RMAP_LEVEL_PTE:
211 		break;
212 	case RMAP_LEVEL_PMD:
213 		/*
214 		 * We don't support folios larger than a single PMD yet. So
215 		 * when RMAP_LEVEL_PMD is set, we assume that we are creating
216 		 * a single "entire" mapping of the folio.
217 		 */
218 		VM_WARN_ON_FOLIO(folio_nr_pages(folio) != HPAGE_PMD_NR, folio);
219 		VM_WARN_ON_FOLIO(nr_pages != HPAGE_PMD_NR, folio);
220 		break;
221 	default:
222 		VM_WARN_ON_ONCE(true);
223 	}
224 }
225 
226 /*
227  * rmap interfaces called when adding or removing pte of page
228  */
229 void folio_move_anon_rmap(struct folio *, struct vm_area_struct *);
230 void folio_add_anon_rmap_ptes(struct folio *, struct page *, int nr_pages,
231 		struct vm_area_struct *, unsigned long address, rmap_t flags);
232 #define folio_add_anon_rmap_pte(folio, page, vma, address, flags) \
233 	folio_add_anon_rmap_ptes(folio, page, 1, vma, address, flags)
234 void folio_add_anon_rmap_pmd(struct folio *, struct page *,
235 		struct vm_area_struct *, unsigned long address, rmap_t flags);
236 void folio_add_new_anon_rmap(struct folio *, struct vm_area_struct *,
237 		unsigned long address);
238 void folio_add_file_rmap_ptes(struct folio *, struct page *, int nr_pages,
239 		struct vm_area_struct *);
240 #define folio_add_file_rmap_pte(folio, page, vma) \
241 	folio_add_file_rmap_ptes(folio, page, 1, vma)
242 void folio_add_file_rmap_pmd(struct folio *, struct page *,
243 		struct vm_area_struct *);
244 void folio_remove_rmap_ptes(struct folio *, struct page *, int nr_pages,
245 		struct vm_area_struct *);
246 #define folio_remove_rmap_pte(folio, page, vma) \
247 	folio_remove_rmap_ptes(folio, page, 1, vma)
248 void folio_remove_rmap_pmd(struct folio *, struct page *,
249 		struct vm_area_struct *);
250 
251 void hugetlb_add_anon_rmap(struct folio *, struct vm_area_struct *,
252 		unsigned long address, rmap_t flags);
253 void hugetlb_add_new_anon_rmap(struct folio *, struct vm_area_struct *,
254 		unsigned long address);
255 
256 /* See folio_try_dup_anon_rmap_*() */
257 static inline int hugetlb_try_dup_anon_rmap(struct folio *folio,
258 		struct vm_area_struct *vma)
259 {
260 	VM_WARN_ON_FOLIO(!folio_test_hugetlb(folio), folio);
261 	VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio);
262 
263 	if (PageAnonExclusive(&folio->page)) {
264 		if (unlikely(folio_needs_cow_for_dma(vma, folio)))
265 			return -EBUSY;
266 		ClearPageAnonExclusive(&folio->page);
267 	}
268 	atomic_inc(&folio->_entire_mapcount);
269 	return 0;
270 }
271 
272 /* See page_try_share_anon_rmap() */
273 static inline int hugetlb_try_share_anon_rmap(struct folio *folio)
274 {
275 	VM_WARN_ON_FOLIO(!folio_test_hugetlb(folio), folio);
276 	VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio);
277 	VM_WARN_ON_FOLIO(!PageAnonExclusive(&folio->page), folio);
278 
279 	/* Paired with the memory barrier in try_grab_folio(). */
280 	if (IS_ENABLED(CONFIG_HAVE_FAST_GUP))
281 		smp_mb();
282 
283 	if (unlikely(folio_maybe_dma_pinned(folio)))
284 		return -EBUSY;
285 	ClearPageAnonExclusive(&folio->page);
286 
287 	/*
288 	 * This is conceptually a smp_wmb() paired with the smp_rmb() in
289 	 * gup_must_unshare().
290 	 */
291 	if (IS_ENABLED(CONFIG_HAVE_FAST_GUP))
292 		smp_mb__after_atomic();
293 	return 0;
294 }
295 
296 static inline void hugetlb_add_file_rmap(struct folio *folio)
297 {
298 	VM_WARN_ON_FOLIO(!folio_test_hugetlb(folio), folio);
299 	VM_WARN_ON_FOLIO(folio_test_anon(folio), folio);
300 
301 	atomic_inc(&folio->_entire_mapcount);
302 }
303 
304 static inline void hugetlb_remove_rmap(struct folio *folio)
305 {
306 	VM_WARN_ON_FOLIO(!folio_test_hugetlb(folio), folio);
307 
308 	atomic_dec(&folio->_entire_mapcount);
309 }
310 
311 static __always_inline void __folio_dup_file_rmap(struct folio *folio,
312 		struct page *page, int nr_pages, enum rmap_level level)
313 {
314 	__folio_rmap_sanity_checks(folio, page, nr_pages, level);
315 
316 	switch (level) {
317 	case RMAP_LEVEL_PTE:
318 		do {
319 			atomic_inc(&page->_mapcount);
320 		} while (page++, --nr_pages > 0);
321 		break;
322 	case RMAP_LEVEL_PMD:
323 		atomic_inc(&folio->_entire_mapcount);
324 		break;
325 	}
326 }
327 
328 /**
329  * folio_dup_file_rmap_ptes - duplicate PTE mappings of a page range of a folio
330  * @folio:	The folio to duplicate the mappings of
331  * @page:	The first page to duplicate the mappings of
332  * @nr_pages:	The number of pages of which the mapping will be duplicated
333  *
334  * The page range of the folio is defined by [page, page + nr_pages)
335  *
336  * The caller needs to hold the page table lock.
337  */
338 static inline void folio_dup_file_rmap_ptes(struct folio *folio,
339 		struct page *page, int nr_pages)
340 {
341 	__folio_dup_file_rmap(folio, page, nr_pages, RMAP_LEVEL_PTE);
342 }
343 #define folio_dup_file_rmap_pte(folio, page) \
344 	folio_dup_file_rmap_ptes(folio, page, 1)
345 
346 /**
347  * folio_dup_file_rmap_pmd - duplicate a PMD mapping of a page range of a folio
348  * @folio:	The folio to duplicate the mapping of
349  * @page:	The first page to duplicate the mapping of
350  *
351  * The page range of the folio is defined by [page, page + HPAGE_PMD_NR)
352  *
353  * The caller needs to hold the page table lock.
354  */
355 static inline void folio_dup_file_rmap_pmd(struct folio *folio,
356 		struct page *page)
357 {
358 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
359 	__folio_dup_file_rmap(folio, page, HPAGE_PMD_NR, RMAP_LEVEL_PTE);
360 #else
361 	WARN_ON_ONCE(true);
362 #endif
363 }
364 
365 static __always_inline int __folio_try_dup_anon_rmap(struct folio *folio,
366 		struct page *page, int nr_pages, struct vm_area_struct *src_vma,
367 		enum rmap_level level)
368 {
369 	bool maybe_pinned;
370 	int i;
371 
372 	VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio);
373 	__folio_rmap_sanity_checks(folio, page, nr_pages, level);
374 
375 	/*
376 	 * If this folio may have been pinned by the parent process,
377 	 * don't allow to duplicate the mappings but instead require to e.g.,
378 	 * copy the subpage immediately for the child so that we'll always
379 	 * guarantee the pinned folio won't be randomly replaced in the
380 	 * future on write faults.
381 	 */
382 	maybe_pinned = likely(!folio_is_device_private(folio)) &&
383 		       unlikely(folio_needs_cow_for_dma(src_vma, folio));
384 
385 	/*
386 	 * No need to check+clear for already shared PTEs/PMDs of the
387 	 * folio. But if any page is PageAnonExclusive, we must fallback to
388 	 * copying if the folio maybe pinned.
389 	 */
390 	switch (level) {
391 	case RMAP_LEVEL_PTE:
392 		if (unlikely(maybe_pinned)) {
393 			for (i = 0; i < nr_pages; i++)
394 				if (PageAnonExclusive(page + i))
395 					return -EBUSY;
396 		}
397 		do {
398 			if (PageAnonExclusive(page))
399 				ClearPageAnonExclusive(page);
400 			atomic_inc(&page->_mapcount);
401 		} while (page++, --nr_pages > 0);
402 		break;
403 	case RMAP_LEVEL_PMD:
404 		if (PageAnonExclusive(page)) {
405 			if (unlikely(maybe_pinned))
406 				return -EBUSY;
407 			ClearPageAnonExclusive(page);
408 		}
409 		atomic_inc(&folio->_entire_mapcount);
410 		break;
411 	}
412 	return 0;
413 }
414 
415 /**
416  * folio_try_dup_anon_rmap_ptes - try duplicating PTE mappings of a page range
417  *				  of a folio
418  * @folio:	The folio to duplicate the mappings of
419  * @page:	The first page to duplicate the mappings of
420  * @nr_pages:	The number of pages of which the mapping will be duplicated
421  * @src_vma:	The vm area from which the mappings are duplicated
422  *
423  * The page range of the folio is defined by [page, page + nr_pages)
424  *
425  * The caller needs to hold the page table lock and the
426  * vma->vma_mm->write_protect_seq.
427  *
428  * Duplicating the mappings can only fail if the folio may be pinned; device
429  * private folios cannot get pinned and consequently this function cannot fail
430  * for them.
431  *
432  * If duplicating the mappings succeeded, the duplicated PTEs have to be R/O in
433  * the parent and the child. They must *not* be writable after this call
434  * succeeded.
435  *
436  * Returns 0 if duplicating the mappings succeeded. Returns -EBUSY otherwise.
437  */
438 static inline int folio_try_dup_anon_rmap_ptes(struct folio *folio,
439 		struct page *page, int nr_pages, struct vm_area_struct *src_vma)
440 {
441 	return __folio_try_dup_anon_rmap(folio, page, nr_pages, src_vma,
442 					 RMAP_LEVEL_PTE);
443 }
444 #define folio_try_dup_anon_rmap_pte(folio, page, vma) \
445 	folio_try_dup_anon_rmap_ptes(folio, page, 1, vma)
446 
447 /**
448  * folio_try_dup_anon_rmap_pmd - try duplicating a PMD mapping of a page range
449  *				 of a folio
450  * @folio:	The folio to duplicate the mapping of
451  * @page:	The first page to duplicate the mapping of
452  * @src_vma:	The vm area from which the mapping is duplicated
453  *
454  * The page range of the folio is defined by [page, page + HPAGE_PMD_NR)
455  *
456  * The caller needs to hold the page table lock and the
457  * vma->vma_mm->write_protect_seq.
458  *
459  * Duplicating the mapping can only fail if the folio may be pinned; device
460  * private folios cannot get pinned and consequently this function cannot fail
461  * for them.
462  *
463  * If duplicating the mapping succeeds, the duplicated PMD has to be R/O in
464  * the parent and the child. They must *not* be writable after this call
465  * succeeded.
466  *
467  * Returns 0 if duplicating the mapping succeeded. Returns -EBUSY otherwise.
468  */
469 static inline int folio_try_dup_anon_rmap_pmd(struct folio *folio,
470 		struct page *page, struct vm_area_struct *src_vma)
471 {
472 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
473 	return __folio_try_dup_anon_rmap(folio, page, HPAGE_PMD_NR, src_vma,
474 					 RMAP_LEVEL_PMD);
475 #else
476 	WARN_ON_ONCE(true);
477 	return -EBUSY;
478 #endif
479 }
480 
481 /**
482  * page_try_share_anon_rmap - try marking an exclusive anonymous page possibly
483  *			      shared to prepare for KSM or temporary unmapping
484  * @page: the exclusive anonymous page to try marking possibly shared
485  *
486  * The caller needs to hold the PT lock and has to have the page table entry
487  * cleared/invalidated.
488  *
489  * This is similar to folio_try_dup_anon_rmap_*(), however, not used during
490  * fork() to duplicate a mapping, but instead to prepare for KSM or temporarily
491  * unmapping a page (swap, migration) via folio_remove_rmap_*().
492  *
493  * Marking the page shared can only fail if the page may be pinned; device
494  * private pages cannot get pinned and consequently this function cannot fail.
495  *
496  * Returns 0 if marking the page possibly shared succeeded. Returns -EBUSY
497  * otherwise.
498  */
499 static inline int page_try_share_anon_rmap(struct page *page)
500 {
501 	VM_WARN_ON(folio_test_hugetlb(page_folio(page)));
502 	VM_BUG_ON_PAGE(!PageAnon(page) || !PageAnonExclusive(page), page);
503 
504 	/* device private pages cannot get pinned via GUP. */
505 	if (unlikely(is_device_private_page(page))) {
506 		ClearPageAnonExclusive(page);
507 		return 0;
508 	}
509 
510 	/*
511 	 * We have to make sure that when we clear PageAnonExclusive, that
512 	 * the page is not pinned and that concurrent GUP-fast won't succeed in
513 	 * concurrently pinning the page.
514 	 *
515 	 * Conceptually, PageAnonExclusive clearing consists of:
516 	 * (A1) Clear PTE
517 	 * (A2) Check if the page is pinned; back off if so.
518 	 * (A3) Clear PageAnonExclusive
519 	 * (A4) Restore PTE (optional, but certainly not writable)
520 	 *
521 	 * When clearing PageAnonExclusive, we cannot possibly map the page
522 	 * writable again, because anon pages that may be shared must never
523 	 * be writable. So in any case, if the PTE was writable it cannot
524 	 * be writable anymore afterwards and there would be a PTE change. Only
525 	 * if the PTE wasn't writable, there might not be a PTE change.
526 	 *
527 	 * Conceptually, GUP-fast pinning of an anon page consists of:
528 	 * (B1) Read the PTE
529 	 * (B2) FOLL_WRITE: check if the PTE is not writable; back off if so.
530 	 * (B3) Pin the mapped page
531 	 * (B4) Check if the PTE changed by re-reading it; back off if so.
532 	 * (B5) If the original PTE is not writable, check if
533 	 *	PageAnonExclusive is not set; back off if so.
534 	 *
535 	 * If the PTE was writable, we only have to make sure that GUP-fast
536 	 * observes a PTE change and properly backs off.
537 	 *
538 	 * If the PTE was not writable, we have to make sure that GUP-fast either
539 	 * detects a (temporary) PTE change or that PageAnonExclusive is cleared
540 	 * and properly backs off.
541 	 *
542 	 * Consequently, when clearing PageAnonExclusive(), we have to make
543 	 * sure that (A1), (A2)/(A3) and (A4) happen in the right memory
544 	 * order. In GUP-fast pinning code, we have to make sure that (B3),(B4)
545 	 * and (B5) happen in the right memory order.
546 	 *
547 	 * We assume that there might not be a memory barrier after
548 	 * clearing/invalidating the PTE (A1) and before restoring the PTE (A4),
549 	 * so we use explicit ones here.
550 	 */
551 
552 	/* Paired with the memory barrier in try_grab_folio(). */
553 	if (IS_ENABLED(CONFIG_HAVE_FAST_GUP))
554 		smp_mb();
555 
556 	if (unlikely(page_maybe_dma_pinned(page)))
557 		return -EBUSY;
558 	ClearPageAnonExclusive(page);
559 
560 	/*
561 	 * This is conceptually a smp_wmb() paired with the smp_rmb() in
562 	 * gup_must_unshare().
563 	 */
564 	if (IS_ENABLED(CONFIG_HAVE_FAST_GUP))
565 		smp_mb__after_atomic();
566 	return 0;
567 }
568 
569 /*
570  * Called from mm/vmscan.c to handle paging out
571  */
572 int folio_referenced(struct folio *, int is_locked,
573 			struct mem_cgroup *memcg, unsigned long *vm_flags);
574 
575 void try_to_migrate(struct folio *folio, enum ttu_flags flags);
576 void try_to_unmap(struct folio *, enum ttu_flags flags);
577 
578 int make_device_exclusive_range(struct mm_struct *mm, unsigned long start,
579 				unsigned long end, struct page **pages,
580 				void *arg);
581 
582 /* Avoid racy checks */
583 #define PVMW_SYNC		(1 << 0)
584 /* Look for migration entries rather than present PTEs */
585 #define PVMW_MIGRATION		(1 << 1)
586 
587 struct page_vma_mapped_walk {
588 	unsigned long pfn;
589 	unsigned long nr_pages;
590 	pgoff_t pgoff;
591 	struct vm_area_struct *vma;
592 	unsigned long address;
593 	pmd_t *pmd;
594 	pte_t *pte;
595 	spinlock_t *ptl;
596 	unsigned int flags;
597 };
598 
599 #define DEFINE_PAGE_VMA_WALK(name, _page, _vma, _address, _flags)	\
600 	struct page_vma_mapped_walk name = {				\
601 		.pfn = page_to_pfn(_page),				\
602 		.nr_pages = compound_nr(_page),				\
603 		.pgoff = page_to_pgoff(_page),				\
604 		.vma = _vma,						\
605 		.address = _address,					\
606 		.flags = _flags,					\
607 	}
608 
609 #define DEFINE_FOLIO_VMA_WALK(name, _folio, _vma, _address, _flags)	\
610 	struct page_vma_mapped_walk name = {				\
611 		.pfn = folio_pfn(_folio),				\
612 		.nr_pages = folio_nr_pages(_folio),			\
613 		.pgoff = folio_pgoff(_folio),				\
614 		.vma = _vma,						\
615 		.address = _address,					\
616 		.flags = _flags,					\
617 	}
618 
619 static inline void page_vma_mapped_walk_done(struct page_vma_mapped_walk *pvmw)
620 {
621 	/* HugeTLB pte is set to the relevant page table entry without pte_mapped. */
622 	if (pvmw->pte && !is_vm_hugetlb_page(pvmw->vma))
623 		pte_unmap(pvmw->pte);
624 	if (pvmw->ptl)
625 		spin_unlock(pvmw->ptl);
626 }
627 
628 bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw);
629 
630 /*
631  * Used by swapoff to help locate where page is expected in vma.
632  */
633 unsigned long page_address_in_vma(struct page *, struct vm_area_struct *);
634 
635 /*
636  * Cleans the PTEs of shared mappings.
637  * (and since clean PTEs should also be readonly, write protects them too)
638  *
639  * returns the number of cleaned PTEs.
640  */
641 int folio_mkclean(struct folio *);
642 
643 int pfn_mkclean_range(unsigned long pfn, unsigned long nr_pages, pgoff_t pgoff,
644 		      struct vm_area_struct *vma);
645 
646 void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked);
647 
648 int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma);
649 
650 /*
651  * rmap_walk_control: To control rmap traversing for specific needs
652  *
653  * arg: passed to rmap_one() and invalid_vma()
654  * try_lock: bail out if the rmap lock is contended
655  * contended: indicate the rmap traversal bailed out due to lock contention
656  * rmap_one: executed on each vma where page is mapped
657  * done: for checking traversing termination condition
658  * anon_lock: for getting anon_lock by optimized way rather than default
659  * invalid_vma: for skipping uninterested vma
660  */
661 struct rmap_walk_control {
662 	void *arg;
663 	bool try_lock;
664 	bool contended;
665 	/*
666 	 * Return false if page table scanning in rmap_walk should be stopped.
667 	 * Otherwise, return true.
668 	 */
669 	bool (*rmap_one)(struct folio *folio, struct vm_area_struct *vma,
670 					unsigned long addr, void *arg);
671 	int (*done)(struct folio *folio);
672 	struct anon_vma *(*anon_lock)(struct folio *folio,
673 				      struct rmap_walk_control *rwc);
674 	bool (*invalid_vma)(struct vm_area_struct *vma, void *arg);
675 };
676 
677 void rmap_walk(struct folio *folio, struct rmap_walk_control *rwc);
678 void rmap_walk_locked(struct folio *folio, struct rmap_walk_control *rwc);
679 struct anon_vma *folio_lock_anon_vma_read(struct folio *folio,
680 					  struct rmap_walk_control *rwc);
681 
682 #else	/* !CONFIG_MMU */
683 
684 #define anon_vma_init()		do {} while (0)
685 #define anon_vma_prepare(vma)	(0)
686 
687 static inline int folio_referenced(struct folio *folio, int is_locked,
688 				  struct mem_cgroup *memcg,
689 				  unsigned long *vm_flags)
690 {
691 	*vm_flags = 0;
692 	return 0;
693 }
694 
695 static inline void try_to_unmap(struct folio *folio, enum ttu_flags flags)
696 {
697 }
698 
699 static inline int folio_mkclean(struct folio *folio)
700 {
701 	return 0;
702 }
703 #endif	/* CONFIG_MMU */
704 
705 static inline int page_mkclean(struct page *page)
706 {
707 	return folio_mkclean(page_folio(page));
708 }
709 #endif	/* _LINUX_RMAP_H */
710