1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Declarations for Reverse Mapping functions in mm/rmap.c
8 #include <linux/list.h>
9 #include <linux/slab.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>
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.
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.
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.
32 struct anon_vma
*root
; /* Root of this anon_vma tree */
33 struct rw_semaphore rwsem
; /* W: modification, R: walking the list */
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
44 * Count of child anon_vmas. Equals to the count of all anon_vmas that
45 * have ->parent pointing to this one, including itself.
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.
50 unsigned long num_children
;
51 /* Count of VMAs whose ->anon_vma pointer points to this object. */
52 unsigned long num_active_vmas
;
54 struct anon_vma
*parent
; /* Parent of this anon_vma */
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).
65 /* Interval tree of private "related" vmas */
66 struct rb_root_cached rb_root
;
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.
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.
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
;
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:
106 static inline void get_anon_vma(struct anon_vma
*anon_vma
)
108 atomic_inc(&anon_vma
->refcount
);
111 void __put_anon_vma(struct anon_vma
*anon_vma
);
113 static inline void put_anon_vma(struct anon_vma
*anon_vma
)
115 if (atomic_dec_and_test(&anon_vma
->refcount
))
116 __put_anon_vma(anon_vma
);
119 static inline void anon_vma_lock_write(struct anon_vma
*anon_vma
)
121 down_write(&anon_vma
->root
->rwsem
);
124 static inline int anon_vma_trylock_write(struct anon_vma
*anon_vma
)
126 return down_write_trylock(&anon_vma
->root
->rwsem
);
129 static inline void anon_vma_unlock_write(struct anon_vma
*anon_vma
)
131 up_write(&anon_vma
->root
->rwsem
);
134 static inline void anon_vma_lock_read(struct anon_vma
*anon_vma
)
136 down_read(&anon_vma
->root
->rwsem
);
139 static inline int anon_vma_trylock_read(struct anon_vma
*anon_vma
)
141 return down_read_trylock(&anon_vma
->root
->rwsem
);
144 static inline void anon_vma_unlock_read(struct anon_vma
*anon_vma
)
146 up_read(&anon_vma
->root
->rwsem
);
151 * anon_vma helper functions.
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
*);
159 static inline int anon_vma_prepare(struct vm_area_struct
*vma
)
161 if (likely(vma
->anon_vma
))
164 return __anon_vma_prepare(vma
);
167 static inline void anon_vma_merge(struct vm_area_struct
*vma
,
168 struct vm_area_struct
*next
)
170 VM_BUG_ON_VMA(vma
->anon_vma
!= next
->anon_vma
, vma
);
171 unlink_anon_vmas(next
);
174 struct anon_vma
*folio_get_anon_vma(const struct folio
*folio
);
176 /* RMAP flags, currently only relevant for some anon rmap operations. */
177 typedef int __bitwise rmap_t
;
180 * No special request: A mapped anonymous (sub)page is possibly shared between
183 #define RMAP_NONE ((__force rmap_t)0)
185 /* The anonymous (sub)page is exclusive to a single process. */
186 #define RMAP_EXCLUSIVE ((__force rmap_t)BIT(0))
189 * Internally, we're using an enum to specify the granularity. We make the
190 * compiler emit specialized code for each granularity.
197 static inline void __folio_rmap_sanity_checks(const struct folio
*folio
,
198 const struct page
*page
, int nr_pages
, enum rmap_level level
)
200 /* hugetlb folios are handled separately. */
201 VM_WARN_ON_FOLIO(folio_test_hugetlb(folio
), folio
);
203 /* When (un)mapping zeropages, we should never touch ref+mapcount. */
204 VM_WARN_ON_FOLIO(is_zero_folio(folio
), folio
);
207 * TODO: we get driver-allocated folios that have nothing to do with
208 * the rmap using vm_insert_page(); therefore, we cannot assume that
209 * folio_test_large_rmappable() holds for large folios. We should
210 * handle any desired mapcount+stats accounting for these folios in
211 * VM_MIXEDMAP VMAs separately, and then sanity-check here that
212 * we really only get rmappable folios.
215 VM_WARN_ON_ONCE(nr_pages
<= 0);
216 VM_WARN_ON_FOLIO(page_folio(page
) != folio
, folio
);
217 VM_WARN_ON_FOLIO(page_folio(page
+ nr_pages
- 1) != folio
, folio
);
224 * We don't support folios larger than a single PMD yet. So
225 * when RMAP_LEVEL_PMD is set, we assume that we are creating
226 * a single "entire" mapping of the folio.
228 VM_WARN_ON_FOLIO(folio_nr_pages(folio
) != HPAGE_PMD_NR
, folio
);
229 VM_WARN_ON_FOLIO(nr_pages
!= HPAGE_PMD_NR
, folio
);
232 VM_WARN_ON_ONCE(true);
237 * rmap interfaces called when adding or removing pte of page
239 void folio_move_anon_rmap(struct folio
*, struct vm_area_struct
*);
240 void folio_add_anon_rmap_ptes(struct folio
*, struct page
*, int nr_pages
,
241 struct vm_area_struct
*, unsigned long address
, rmap_t flags
);
242 #define folio_add_anon_rmap_pte(folio, page, vma, address, flags) \
243 folio_add_anon_rmap_ptes(folio, page, 1, vma, address, flags)
244 void folio_add_anon_rmap_pmd(struct folio
*, struct page
*,
245 struct vm_area_struct
*, unsigned long address
, rmap_t flags
);
246 void folio_add_new_anon_rmap(struct folio
*, struct vm_area_struct
*,
247 unsigned long address
, rmap_t flags
);
248 void folio_add_file_rmap_ptes(struct folio
*, struct page
*, int nr_pages
,
249 struct vm_area_struct
*);
250 #define folio_add_file_rmap_pte(folio, page, vma) \
251 folio_add_file_rmap_ptes(folio, page, 1, vma)
252 void folio_add_file_rmap_pmd(struct folio
*, struct page
*,
253 struct vm_area_struct
*);
254 void folio_remove_rmap_ptes(struct folio
*, struct page
*, int nr_pages
,
255 struct vm_area_struct
*);
256 #define folio_remove_rmap_pte(folio, page, vma) \
257 folio_remove_rmap_ptes(folio, page, 1, vma)
258 void folio_remove_rmap_pmd(struct folio
*, struct page
*,
259 struct vm_area_struct
*);
261 void hugetlb_add_anon_rmap(struct folio
*, struct vm_area_struct
*,
262 unsigned long address
, rmap_t flags
);
263 void hugetlb_add_new_anon_rmap(struct folio
*, struct vm_area_struct
*,
264 unsigned long address
);
266 /* See folio_try_dup_anon_rmap_*() */
267 static inline int hugetlb_try_dup_anon_rmap(struct folio
*folio
,
268 struct vm_area_struct
*vma
)
270 VM_WARN_ON_FOLIO(!folio_test_hugetlb(folio
), folio
);
271 VM_WARN_ON_FOLIO(!folio_test_anon(folio
), folio
);
273 if (PageAnonExclusive(&folio
->page
)) {
274 if (unlikely(folio_needs_cow_for_dma(vma
, folio
)))
276 ClearPageAnonExclusive(&folio
->page
);
278 atomic_inc(&folio
->_entire_mapcount
);
279 atomic_inc(&folio
->_large_mapcount
);
283 /* See folio_try_share_anon_rmap_*() */
284 static inline int hugetlb_try_share_anon_rmap(struct folio
*folio
)
286 VM_WARN_ON_FOLIO(!folio_test_hugetlb(folio
), folio
);
287 VM_WARN_ON_FOLIO(!folio_test_anon(folio
), folio
);
288 VM_WARN_ON_FOLIO(!PageAnonExclusive(&folio
->page
), folio
);
290 /* Paired with the memory barrier in try_grab_folio(). */
291 if (IS_ENABLED(CONFIG_HAVE_GUP_FAST
))
294 if (unlikely(folio_maybe_dma_pinned(folio
)))
296 ClearPageAnonExclusive(&folio
->page
);
299 * This is conceptually a smp_wmb() paired with the smp_rmb() in
300 * gup_must_unshare().
302 if (IS_ENABLED(CONFIG_HAVE_GUP_FAST
))
303 smp_mb__after_atomic();
307 static inline void hugetlb_add_file_rmap(struct folio
*folio
)
309 VM_WARN_ON_FOLIO(!folio_test_hugetlb(folio
), folio
);
310 VM_WARN_ON_FOLIO(folio_test_anon(folio
), folio
);
312 atomic_inc(&folio
->_entire_mapcount
);
313 atomic_inc(&folio
->_large_mapcount
);
316 static inline void hugetlb_remove_rmap(struct folio
*folio
)
318 VM_WARN_ON_FOLIO(!folio_test_hugetlb(folio
), folio
);
320 atomic_dec(&folio
->_entire_mapcount
);
321 atomic_dec(&folio
->_large_mapcount
);
324 static __always_inline
void __folio_dup_file_rmap(struct folio
*folio
,
325 struct page
*page
, int nr_pages
, enum rmap_level level
)
327 const int orig_nr_pages
= nr_pages
;
329 __folio_rmap_sanity_checks(folio
, page
, nr_pages
, level
);
333 if (!folio_test_large(folio
)) {
334 atomic_inc(&folio
->_mapcount
);
339 atomic_inc(&page
->_mapcount
);
340 } while (page
++, --nr_pages
> 0);
341 atomic_add(orig_nr_pages
, &folio
->_large_mapcount
);
344 atomic_inc(&folio
->_entire_mapcount
);
345 atomic_inc(&folio
->_large_mapcount
);
351 * folio_dup_file_rmap_ptes - duplicate PTE mappings of a page range of a folio
352 * @folio: The folio to duplicate the mappings of
353 * @page: The first page to duplicate the mappings of
354 * @nr_pages: The number of pages of which the mapping will be duplicated
356 * The page range of the folio is defined by [page, page + nr_pages)
358 * The caller needs to hold the page table lock.
360 static inline void folio_dup_file_rmap_ptes(struct folio
*folio
,
361 struct page
*page
, int nr_pages
)
363 __folio_dup_file_rmap(folio
, page
, nr_pages
, RMAP_LEVEL_PTE
);
366 static __always_inline
void folio_dup_file_rmap_pte(struct folio
*folio
,
369 __folio_dup_file_rmap(folio
, page
, 1, RMAP_LEVEL_PTE
);
373 * folio_dup_file_rmap_pmd - duplicate a PMD mapping of a page range of a folio
374 * @folio: The folio to duplicate the mapping of
375 * @page: The first page to duplicate the mapping of
377 * The page range of the folio is defined by [page, page + HPAGE_PMD_NR)
379 * The caller needs to hold the page table lock.
381 static inline void folio_dup_file_rmap_pmd(struct folio
*folio
,
384 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
385 __folio_dup_file_rmap(folio
, page
, HPAGE_PMD_NR
, RMAP_LEVEL_PTE
);
391 static __always_inline
int __folio_try_dup_anon_rmap(struct folio
*folio
,
392 struct page
*page
, int nr_pages
, struct vm_area_struct
*src_vma
,
393 enum rmap_level level
)
395 const int orig_nr_pages
= nr_pages
;
399 VM_WARN_ON_FOLIO(!folio_test_anon(folio
), folio
);
400 __folio_rmap_sanity_checks(folio
, page
, nr_pages
, level
);
403 * If this folio may have been pinned by the parent process,
404 * don't allow to duplicate the mappings but instead require to e.g.,
405 * copy the subpage immediately for the child so that we'll always
406 * guarantee the pinned folio won't be randomly replaced in the
407 * future on write faults.
409 maybe_pinned
= likely(!folio_is_device_private(folio
)) &&
410 unlikely(folio_needs_cow_for_dma(src_vma
, folio
));
413 * No need to check+clear for already shared PTEs/PMDs of the
414 * folio. But if any page is PageAnonExclusive, we must fallback to
415 * copying if the folio maybe pinned.
419 if (unlikely(maybe_pinned
)) {
420 for (i
= 0; i
< nr_pages
; i
++)
421 if (PageAnonExclusive(page
+ i
))
425 if (!folio_test_large(folio
)) {
426 if (PageAnonExclusive(page
))
427 ClearPageAnonExclusive(page
);
428 atomic_inc(&folio
->_mapcount
);
433 if (PageAnonExclusive(page
))
434 ClearPageAnonExclusive(page
);
435 atomic_inc(&page
->_mapcount
);
436 } while (page
++, --nr_pages
> 0);
437 atomic_add(orig_nr_pages
, &folio
->_large_mapcount
);
440 if (PageAnonExclusive(page
)) {
441 if (unlikely(maybe_pinned
))
443 ClearPageAnonExclusive(page
);
445 atomic_inc(&folio
->_entire_mapcount
);
446 atomic_inc(&folio
->_large_mapcount
);
453 * folio_try_dup_anon_rmap_ptes - try duplicating PTE mappings of a page range
455 * @folio: The folio to duplicate the mappings of
456 * @page: The first page to duplicate the mappings of
457 * @nr_pages: The number of pages of which the mapping will be duplicated
458 * @src_vma: The vm area from which the mappings are duplicated
460 * The page range of the folio is defined by [page, page + nr_pages)
462 * The caller needs to hold the page table lock and the
463 * vma->vma_mm->write_protect_seq.
465 * Duplicating the mappings can only fail if the folio may be pinned; device
466 * private folios cannot get pinned and consequently this function cannot fail
469 * If duplicating the mappings succeeded, the duplicated PTEs have to be R/O in
470 * the parent and the child. They must *not* be writable after this call
473 * Returns 0 if duplicating the mappings succeeded. Returns -EBUSY otherwise.
475 static inline int folio_try_dup_anon_rmap_ptes(struct folio
*folio
,
476 struct page
*page
, int nr_pages
, struct vm_area_struct
*src_vma
)
478 return __folio_try_dup_anon_rmap(folio
, page
, nr_pages
, src_vma
,
482 static __always_inline
int folio_try_dup_anon_rmap_pte(struct folio
*folio
,
483 struct page
*page
, struct vm_area_struct
*src_vma
)
485 return __folio_try_dup_anon_rmap(folio
, page
, 1, src_vma
,
490 * folio_try_dup_anon_rmap_pmd - try duplicating a PMD mapping of a page range
492 * @folio: The folio to duplicate the mapping of
493 * @page: The first page to duplicate the mapping of
494 * @src_vma: The vm area from which the mapping is duplicated
496 * The page range of the folio is defined by [page, page + HPAGE_PMD_NR)
498 * The caller needs to hold the page table lock and the
499 * vma->vma_mm->write_protect_seq.
501 * Duplicating the mapping can only fail if the folio may be pinned; device
502 * private folios cannot get pinned and consequently this function cannot fail
505 * If duplicating the mapping succeeds, the duplicated PMD has to be R/O in
506 * the parent and the child. They must *not* be writable after this call
509 * Returns 0 if duplicating the mapping succeeded. Returns -EBUSY otherwise.
511 static inline int folio_try_dup_anon_rmap_pmd(struct folio
*folio
,
512 struct page
*page
, struct vm_area_struct
*src_vma
)
514 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
515 return __folio_try_dup_anon_rmap(folio
, page
, HPAGE_PMD_NR
, src_vma
,
523 static __always_inline
int __folio_try_share_anon_rmap(struct folio
*folio
,
524 struct page
*page
, int nr_pages
, enum rmap_level level
)
526 VM_WARN_ON_FOLIO(!folio_test_anon(folio
), folio
);
527 VM_WARN_ON_FOLIO(!PageAnonExclusive(page
), folio
);
528 __folio_rmap_sanity_checks(folio
, page
, nr_pages
, level
);
530 /* device private folios cannot get pinned via GUP. */
531 if (unlikely(folio_is_device_private(folio
))) {
532 ClearPageAnonExclusive(page
);
537 * We have to make sure that when we clear PageAnonExclusive, that
538 * the page is not pinned and that concurrent GUP-fast won't succeed in
539 * concurrently pinning the page.
541 * Conceptually, PageAnonExclusive clearing consists of:
543 * (A2) Check if the page is pinned; back off if so.
544 * (A3) Clear PageAnonExclusive
545 * (A4) Restore PTE (optional, but certainly not writable)
547 * When clearing PageAnonExclusive, we cannot possibly map the page
548 * writable again, because anon pages that may be shared must never
549 * be writable. So in any case, if the PTE was writable it cannot
550 * be writable anymore afterwards and there would be a PTE change. Only
551 * if the PTE wasn't writable, there might not be a PTE change.
553 * Conceptually, GUP-fast pinning of an anon page consists of:
555 * (B2) FOLL_WRITE: check if the PTE is not writable; back off if so.
556 * (B3) Pin the mapped page
557 * (B4) Check if the PTE changed by re-reading it; back off if so.
558 * (B5) If the original PTE is not writable, check if
559 * PageAnonExclusive is not set; back off if so.
561 * If the PTE was writable, we only have to make sure that GUP-fast
562 * observes a PTE change and properly backs off.
564 * If the PTE was not writable, we have to make sure that GUP-fast either
565 * detects a (temporary) PTE change or that PageAnonExclusive is cleared
566 * and properly backs off.
568 * Consequently, when clearing PageAnonExclusive(), we have to make
569 * sure that (A1), (A2)/(A3) and (A4) happen in the right memory
570 * order. In GUP-fast pinning code, we have to make sure that (B3),(B4)
571 * and (B5) happen in the right memory order.
573 * We assume that there might not be a memory barrier after
574 * clearing/invalidating the PTE (A1) and before restoring the PTE (A4),
575 * so we use explicit ones here.
578 /* Paired with the memory barrier in try_grab_folio(). */
579 if (IS_ENABLED(CONFIG_HAVE_GUP_FAST
))
582 if (unlikely(folio_maybe_dma_pinned(folio
)))
584 ClearPageAnonExclusive(page
);
587 * This is conceptually a smp_wmb() paired with the smp_rmb() in
588 * gup_must_unshare().
590 if (IS_ENABLED(CONFIG_HAVE_GUP_FAST
))
591 smp_mb__after_atomic();
596 * folio_try_share_anon_rmap_pte - try marking an exclusive anonymous page
597 * mapped by a PTE possibly shared to prepare
598 * for KSM or temporary unmapping
599 * @folio: The folio to share a mapping of
600 * @page: The mapped exclusive page
602 * The caller needs to hold the page table lock and has to have the page table
603 * entries cleared/invalidated.
605 * This is similar to folio_try_dup_anon_rmap_pte(), however, not used during
606 * fork() to duplicate mappings, but instead to prepare for KSM or temporarily
607 * unmapping parts of a folio (swap, migration) via folio_remove_rmap_pte().
609 * Marking the mapped page shared can only fail if the folio maybe pinned;
610 * device private folios cannot get pinned and consequently this function cannot
613 * Returns 0 if marking the mapped page possibly shared succeeded. Returns
616 static inline int folio_try_share_anon_rmap_pte(struct folio
*folio
,
619 return __folio_try_share_anon_rmap(folio
, page
, 1, RMAP_LEVEL_PTE
);
623 * folio_try_share_anon_rmap_pmd - try marking an exclusive anonymous page
624 * range mapped by a PMD possibly shared to
625 * prepare for temporary unmapping
626 * @folio: The folio to share the mapping of
627 * @page: The first page to share the mapping of
629 * The page range of the folio is defined by [page, page + HPAGE_PMD_NR)
631 * The caller needs to hold the page table lock and has to have the page table
632 * entries cleared/invalidated.
634 * This is similar to folio_try_dup_anon_rmap_pmd(), however, not used during
635 * fork() to duplicate a mapping, but instead to prepare for temporarily
636 * unmapping parts of a folio (swap, migration) via folio_remove_rmap_pmd().
638 * Marking the mapped pages shared can only fail if the folio maybe pinned;
639 * device private folios cannot get pinned and consequently this function cannot
642 * Returns 0 if marking the mapped pages possibly shared succeeded. Returns
645 static inline int folio_try_share_anon_rmap_pmd(struct folio
*folio
,
648 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
649 return __folio_try_share_anon_rmap(folio
, page
, HPAGE_PMD_NR
,
658 * Called from mm/vmscan.c to handle paging out
660 int folio_referenced(struct folio
*, int is_locked
,
661 struct mem_cgroup
*memcg
, unsigned long *vm_flags
);
663 void try_to_migrate(struct folio
*folio
, enum ttu_flags flags
);
664 void try_to_unmap(struct folio
*, enum ttu_flags flags
);
666 int make_device_exclusive_range(struct mm_struct
*mm
, unsigned long start
,
667 unsigned long end
, struct page
**pages
,
670 /* Avoid racy checks */
671 #define PVMW_SYNC (1 << 0)
672 /* Look for migration entries rather than present PTEs */
673 #define PVMW_MIGRATION (1 << 1)
675 struct page_vma_mapped_walk
{
677 unsigned long nr_pages
;
679 struct vm_area_struct
*vma
;
680 unsigned long address
;
687 #define DEFINE_FOLIO_VMA_WALK(name, _folio, _vma, _address, _flags) \
688 struct page_vma_mapped_walk name = { \
689 .pfn = folio_pfn(_folio), \
690 .nr_pages = folio_nr_pages(_folio), \
691 .pgoff = folio_pgoff(_folio), \
693 .address = _address, \
697 static inline void page_vma_mapped_walk_done(struct page_vma_mapped_walk
*pvmw
)
699 /* HugeTLB pte is set to the relevant page table entry without pte_mapped. */
700 if (pvmw
->pte
&& !is_vm_hugetlb_page(pvmw
->vma
))
701 pte_unmap(pvmw
->pte
);
703 spin_unlock(pvmw
->ptl
);
707 * page_vma_mapped_walk_restart - Restart the page table walk.
708 * @pvmw: Pointer to struct page_vma_mapped_walk.
710 * It restarts the page table walk when changes occur in the page
711 * table, such as splitting a PMD. Ensures that the PTL held during
712 * the previous walk is released and resets the state to allow for
713 * a new walk starting at the current address stored in pvmw->address.
716 page_vma_mapped_walk_restart(struct page_vma_mapped_walk
*pvmw
)
718 WARN_ON_ONCE(!pvmw
->pmd
&& !pvmw
->pte
);
720 if (likely(pvmw
->ptl
))
721 spin_unlock(pvmw
->ptl
);
730 bool page_vma_mapped_walk(struct page_vma_mapped_walk
*pvmw
);
731 unsigned long page_address_in_vma(const struct folio
*folio
,
732 const struct page
*, const struct vm_area_struct
*);
735 * Cleans the PTEs of shared mappings.
736 * (and since clean PTEs should also be readonly, write protects them too)
738 * returns the number of cleaned PTEs.
740 int folio_mkclean(struct folio
*);
742 int pfn_mkclean_range(unsigned long pfn
, unsigned long nr_pages
, pgoff_t pgoff
,
743 struct vm_area_struct
*vma
);
747 RMP_USE_SHARED_ZEROPAGE
= 1 << 1,
750 void remove_migration_ptes(struct folio
*src
, struct folio
*dst
, int flags
);
753 * rmap_walk_control: To control rmap traversing for specific needs
755 * arg: passed to rmap_one() and invalid_vma()
756 * try_lock: bail out if the rmap lock is contended
757 * contended: indicate the rmap traversal bailed out due to lock contention
758 * rmap_one: executed on each vma where page is mapped
759 * done: for checking traversing termination condition
760 * anon_lock: for getting anon_lock by optimized way rather than default
761 * invalid_vma: for skipping uninterested vma
763 struct rmap_walk_control
{
768 * Return false if page table scanning in rmap_walk should be stopped.
769 * Otherwise, return true.
771 bool (*rmap_one
)(struct folio
*folio
, struct vm_area_struct
*vma
,
772 unsigned long addr
, void *arg
);
773 int (*done
)(struct folio
*folio
);
774 struct anon_vma
*(*anon_lock
)(const struct folio
*folio
,
775 struct rmap_walk_control
*rwc
);
776 bool (*invalid_vma
)(struct vm_area_struct
*vma
, void *arg
);
779 void rmap_walk(struct folio
*folio
, struct rmap_walk_control
*rwc
);
780 void rmap_walk_locked(struct folio
*folio
, struct rmap_walk_control
*rwc
);
781 struct anon_vma
*folio_lock_anon_vma_read(const struct folio
*folio
,
782 struct rmap_walk_control
*rwc
);
784 #else /* !CONFIG_MMU */
786 #define anon_vma_init() do {} while (0)
787 #define anon_vma_prepare(vma) (0)
789 static inline int folio_referenced(struct folio
*folio
, int is_locked
,
790 struct mem_cgroup
*memcg
,
791 unsigned long *vm_flags
)
797 static inline void try_to_unmap(struct folio
*folio
, enum ttu_flags flags
)
801 static inline int folio_mkclean(struct folio
*folio
)
805 #endif /* CONFIG_MMU */
807 #endif /* _LINUX_RMAP_H */