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 and VMAs which points to this anon_vma.
46 * This counter is used for making decision about reusing anon_vma
47 * instead of forking new one. See comments in function anon_vma_clone.
51 struct anon_vma *parent; /* Parent of this anon_vma */
54 * NOTE: the LSB of the rb_root.rb_node is set by
55 * mm_take_all_locks() _after_ taking the above lock. So the
56 * rb_root must only be read/written after taking the above lock
57 * to be sure to see a valid next pointer. The LSB bit itself
58 * is serialized by a system wide lock only visible to
59 * mm_take_all_locks() (mm_all_locks_mutex).
62 /* Interval tree of private "related" vmas */
63 struct rb_root_cached rb_root;
67 * The copy-on-write semantics of fork mean that an anon_vma
68 * can become associated with multiple processes. Furthermore,
69 * each child process will have its own anon_vma, where new
70 * pages for that process are instantiated.
72 * This structure allows us to find the anon_vmas associated
73 * with a VMA, or the VMAs associated with an anon_vma.
74 * The "same_vma" list contains the anon_vma_chains linking
75 * all the anon_vmas associated with this VMA.
76 * The "rb" field indexes on an interval tree the anon_vma_chains
77 * which link all the VMAs associated with this anon_vma.
79 struct anon_vma_chain {
80 struct vm_area_struct *vma;
81 struct anon_vma *anon_vma;
82 struct list_head same_vma; /* locked by mmap_lock & page_table_lock */
83 struct rb_node rb; /* locked by anon_vma->rwsem */
84 unsigned long rb_subtree_last;
85 #ifdef CONFIG_DEBUG_VM_RB
86 unsigned long cached_vma_start, cached_vma_last;
91 TTU_SPLIT_HUGE_PMD = 0x4, /* split huge PMD if any */
92 TTU_IGNORE_MLOCK = 0x8, /* ignore mlock */
93 TTU_SYNC = 0x10, /* avoid racy checks with PVMW_SYNC */
94 TTU_IGNORE_HWPOISON = 0x20, /* corrupted page is recoverable */
95 TTU_BATCH_FLUSH = 0x40, /* Batch TLB flushes where possible
96 * and caller guarantees they will
97 * do a final flush if necessary */
98 TTU_RMAP_LOCKED = 0x80, /* do not grab rmap lock:
103 static inline void get_anon_vma(struct anon_vma *anon_vma)
105 atomic_inc(&anon_vma->refcount);
108 void __put_anon_vma(struct anon_vma *anon_vma);
110 static inline void put_anon_vma(struct anon_vma *anon_vma)
112 if (atomic_dec_and_test(&anon_vma->refcount))
113 __put_anon_vma(anon_vma);
116 static inline void anon_vma_lock_write(struct anon_vma *anon_vma)
118 down_write(&anon_vma->root->rwsem);
121 static inline void anon_vma_unlock_write(struct anon_vma *anon_vma)
123 up_write(&anon_vma->root->rwsem);
126 static inline void anon_vma_lock_read(struct anon_vma *anon_vma)
128 down_read(&anon_vma->root->rwsem);
131 static inline int anon_vma_trylock_read(struct anon_vma *anon_vma)
133 return down_read_trylock(&anon_vma->root->rwsem);
136 static inline void anon_vma_unlock_read(struct anon_vma *anon_vma)
138 up_read(&anon_vma->root->rwsem);
143 * anon_vma helper functions.
145 void anon_vma_init(void); /* create anon_vma_cachep */
146 int __anon_vma_prepare(struct vm_area_struct *);
147 void unlink_anon_vmas(struct vm_area_struct *);
148 int anon_vma_clone(struct vm_area_struct *, struct vm_area_struct *);
149 int anon_vma_fork(struct vm_area_struct *, struct vm_area_struct *);
151 static inline int anon_vma_prepare(struct vm_area_struct *vma)
153 if (likely(vma->anon_vma))
156 return __anon_vma_prepare(vma);
159 static inline void anon_vma_merge(struct vm_area_struct *vma,
160 struct vm_area_struct *next)
162 VM_BUG_ON_VMA(vma->anon_vma != next->anon_vma, vma);
163 unlink_anon_vmas(next);
166 struct anon_vma *page_get_anon_vma(struct page *page);
168 /* RMAP flags, currently only relevant for some anon rmap operations. */
169 typedef int __bitwise rmap_t;
172 * No special request: if the page is a subpage of a compound page, it is
173 * mapped via a PTE. The mapped (sub)page is possibly shared between processes.
175 #define RMAP_NONE ((__force rmap_t)0)
177 /* The (sub)page is exclusive to a single process. */
178 #define RMAP_EXCLUSIVE ((__force rmap_t)BIT(0))
181 * The compound page is not mapped via PTEs, but instead via a single PMD and
182 * should be accounted accordingly.
184 #define RMAP_COMPOUND ((__force rmap_t)BIT(1))
187 * rmap interfaces called when adding or removing pte of page
189 void page_move_anon_rmap(struct page *, struct vm_area_struct *);
190 void page_add_anon_rmap(struct page *, struct vm_area_struct *,
191 unsigned long address, rmap_t flags);
192 void page_add_new_anon_rmap(struct page *, struct vm_area_struct *,
193 unsigned long address);
194 void page_add_file_rmap(struct page *, struct vm_area_struct *,
196 void page_remove_rmap(struct page *, struct vm_area_struct *,
199 void hugepage_add_anon_rmap(struct page *, struct vm_area_struct *,
200 unsigned long address, rmap_t flags);
201 void hugepage_add_new_anon_rmap(struct page *, struct vm_area_struct *,
202 unsigned long address);
204 static inline void __page_dup_rmap(struct page *page, bool compound)
206 atomic_inc(compound ? compound_mapcount_ptr(page) : &page->_mapcount);
209 static inline void page_dup_file_rmap(struct page *page, bool compound)
211 __page_dup_rmap(page, compound);
215 * page_try_dup_anon_rmap - try duplicating a mapping of an already mapped
217 * @page: the page to duplicate the mapping for
218 * @compound: the page is mapped as compound or as a small page
219 * @vma: the source vma
221 * The caller needs to hold the PT lock and the vma->vma_mm->write_protect_seq.
223 * Duplicating the mapping can only fail if the page may be pinned; device
224 * private pages cannot get pinned and consequently this function cannot fail.
226 * If duplicating the mapping succeeds, the page has to be mapped R/O into
227 * the parent and the child. It must *not* get mapped writable after this call.
229 * Returns 0 if duplicating the mapping succeeded. Returns -EBUSY otherwise.
231 static inline int page_try_dup_anon_rmap(struct page *page, bool compound,
232 struct vm_area_struct *vma)
234 VM_BUG_ON_PAGE(!PageAnon(page), page);
237 * No need to check+clear for already shared pages, including KSM
240 if (!PageAnonExclusive(page))
244 * If this page may have been pinned by the parent process,
245 * don't allow to duplicate the mapping but instead require to e.g.,
246 * copy the page immediately for the child so that we'll always
247 * guarantee the pinned page won't be randomly replaced in the
248 * future on write faults.
250 if (likely(!is_device_private_page(page) &&
251 unlikely(page_needs_cow_for_dma(vma, page))))
254 ClearPageAnonExclusive(page);
256 * It's okay to share the anon page between both processes, mapping
257 * the page R/O into both processes.
260 __page_dup_rmap(page, compound);
265 * page_try_share_anon_rmap - try marking an exclusive anonymous page possibly
266 * shared to prepare for KSM or temporary unmapping
267 * @page: the exclusive anonymous page to try marking possibly shared
269 * The caller needs to hold the PT lock and has to have the page table entry
270 * cleared/invalidated+flushed, to properly sync against GUP-fast.
272 * This is similar to page_try_dup_anon_rmap(), however, not used during fork()
273 * to duplicate a mapping, but instead to prepare for KSM or temporarily
274 * unmapping a page (swap, migration) via page_remove_rmap().
276 * Marking the page shared can only fail if the page may be pinned; device
277 * private pages cannot get pinned and consequently this function cannot fail.
279 * Returns 0 if marking the page possibly shared succeeded. Returns -EBUSY
282 static inline int page_try_share_anon_rmap(struct page *page)
284 VM_BUG_ON_PAGE(!PageAnon(page) || !PageAnonExclusive(page), page);
286 /* See page_try_dup_anon_rmap(). */
287 if (likely(!is_device_private_page(page) &&
288 unlikely(page_maybe_dma_pinned(page))))
291 ClearPageAnonExclusive(page);
296 * Called from mm/vmscan.c to handle paging out
298 int folio_referenced(struct folio *, int is_locked,
299 struct mem_cgroup *memcg, unsigned long *vm_flags);
301 void try_to_migrate(struct folio *folio, enum ttu_flags flags);
302 void try_to_unmap(struct folio *, enum ttu_flags flags);
304 int make_device_exclusive_range(struct mm_struct *mm, unsigned long start,
305 unsigned long end, struct page **pages,
308 /* Avoid racy checks */
309 #define PVMW_SYNC (1 << 0)
310 /* Look for migration entries rather than present PTEs */
311 #define PVMW_MIGRATION (1 << 1)
313 struct page_vma_mapped_walk {
315 unsigned long nr_pages;
317 struct vm_area_struct *vma;
318 unsigned long address;
325 #define DEFINE_PAGE_VMA_WALK(name, _page, _vma, _address, _flags) \
326 struct page_vma_mapped_walk name = { \
327 .pfn = page_to_pfn(_page), \
328 .nr_pages = compound_nr(_page), \
329 .pgoff = page_to_pgoff(_page), \
331 .address = _address, \
335 #define DEFINE_FOLIO_VMA_WALK(name, _folio, _vma, _address, _flags) \
336 struct page_vma_mapped_walk name = { \
337 .pfn = folio_pfn(_folio), \
338 .nr_pages = folio_nr_pages(_folio), \
339 .pgoff = folio_pgoff(_folio), \
341 .address = _address, \
345 static inline void page_vma_mapped_walk_done(struct page_vma_mapped_walk *pvmw)
347 /* HugeTLB pte is set to the relevant page table entry without pte_mapped. */
348 if (pvmw->pte && !is_vm_hugetlb_page(pvmw->vma))
349 pte_unmap(pvmw->pte);
351 spin_unlock(pvmw->ptl);
354 bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw);
357 * Used by swapoff to help locate where page is expected in vma.
359 unsigned long page_address_in_vma(struct page *, struct vm_area_struct *);
362 * Cleans the PTEs of shared mappings.
363 * (and since clean PTEs should also be readonly, write protects them too)
365 * returns the number of cleaned PTEs.
367 int folio_mkclean(struct folio *);
369 int pfn_mkclean_range(unsigned long pfn, unsigned long nr_pages, pgoff_t pgoff,
370 struct vm_area_struct *vma);
372 void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked);
374 int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma);
377 * rmap_walk_control: To control rmap traversing for specific needs
379 * arg: passed to rmap_one() and invalid_vma()
380 * try_lock: bail out if the rmap lock is contended
381 * contended: indicate the rmap traversal bailed out due to lock contention
382 * rmap_one: executed on each vma where page is mapped
383 * done: for checking traversing termination condition
384 * anon_lock: for getting anon_lock by optimized way rather than default
385 * invalid_vma: for skipping uninterested vma
387 struct rmap_walk_control {
392 * Return false if page table scanning in rmap_walk should be stopped.
393 * Otherwise, return true.
395 bool (*rmap_one)(struct folio *folio, struct vm_area_struct *vma,
396 unsigned long addr, void *arg);
397 int (*done)(struct folio *folio);
398 struct anon_vma *(*anon_lock)(struct folio *folio,
399 struct rmap_walk_control *rwc);
400 bool (*invalid_vma)(struct vm_area_struct *vma, void *arg);
403 void rmap_walk(struct folio *folio, struct rmap_walk_control *rwc);
404 void rmap_walk_locked(struct folio *folio, struct rmap_walk_control *rwc);
407 * Called by memory-failure.c to kill processes.
409 struct anon_vma *folio_lock_anon_vma_read(struct folio *folio,
410 struct rmap_walk_control *rwc);
411 void page_unlock_anon_vma_read(struct anon_vma *anon_vma);
413 #else /* !CONFIG_MMU */
415 #define anon_vma_init() do {} while (0)
416 #define anon_vma_prepare(vma) (0)
417 #define anon_vma_link(vma) do {} while (0)
419 static inline int folio_referenced(struct folio *folio, int is_locked,
420 struct mem_cgroup *memcg,
421 unsigned long *vm_flags)
427 static inline void try_to_unmap(struct folio *folio, enum ttu_flags flags)
431 static inline int folio_mkclean(struct folio *folio)
435 #endif /* CONFIG_MMU */
437 static inline int page_mkclean(struct page *page)
439 return folio_mkclean(page_folio(page));
441 #endif /* _LINUX_RMAP_H */