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>
17 * The anon_vma heads a list of private "related" vmas, to scan if
18 * an anonymous page pointing to this anon_vma needs to be unmapped:
19 * the vmas on the list will be related by forking, or by splitting.
21 * Since vmas come and go as they are split and merged (particularly
22 * in mprotect), the mapping field of an anonymous page cannot point
23 * directly to a vma: instead it points to an anon_vma, on whose list
24 * the related vmas can be easily linked or unlinked.
26 * After unlinking the last vma on the list, we must garbage collect
27 * the anon_vma object itself: we're guaranteed no page can be
28 * pointing to this anon_vma once its vma list is empty.
31 struct anon_vma *root; /* Root of this anon_vma tree */
32 struct rw_semaphore rwsem; /* W: modification, R: walking the list */
34 * The refcount is taken on an anon_vma when there is no
35 * guarantee that the vma of page tables will exist for
36 * the duration of the operation. A caller that takes
37 * the reference is responsible for clearing up the
38 * anon_vma if they are the last user on release
43 * Count of child anon_vmas and VMAs which points to this anon_vma.
45 * This counter is used for making decision about reusing anon_vma
46 * instead of forking new one. See comments in function anon_vma_clone.
50 struct anon_vma *parent; /* Parent of this anon_vma */
53 * NOTE: the LSB of the rb_root.rb_node is set by
54 * mm_take_all_locks() _after_ taking the above lock. So the
55 * rb_root must only be read/written after taking the above lock
56 * to be sure to see a valid next pointer. The LSB bit itself
57 * is serialized by a system wide lock only visible to
58 * mm_take_all_locks() (mm_all_locks_mutex).
61 /* Interval tree of private "related" vmas */
62 struct rb_root_cached rb_root;
66 * The copy-on-write semantics of fork mean that an anon_vma
67 * can become associated with multiple processes. Furthermore,
68 * each child process will have its own anon_vma, where new
69 * pages for that process are instantiated.
71 * This structure allows us to find the anon_vmas associated
72 * with a VMA, or the VMAs associated with an anon_vma.
73 * The "same_vma" list contains the anon_vma_chains linking
74 * all the anon_vmas associated with this VMA.
75 * The "rb" field indexes on an interval tree the anon_vma_chains
76 * which link all the VMAs associated with this anon_vma.
78 struct anon_vma_chain {
79 struct vm_area_struct *vma;
80 struct anon_vma *anon_vma;
81 struct list_head same_vma; /* locked by mmap_lock & page_table_lock */
82 struct rb_node rb; /* locked by anon_vma->rwsem */
83 unsigned long rb_subtree_last;
84 #ifdef CONFIG_DEBUG_VM_RB
85 unsigned long cached_vma_start, cached_vma_last;
90 TTU_SPLIT_HUGE_PMD = 0x4, /* split huge PMD if any */
91 TTU_IGNORE_MLOCK = 0x8, /* ignore mlock */
92 TTU_SYNC = 0x10, /* avoid racy checks with PVMW_SYNC */
93 TTU_IGNORE_HWPOISON = 0x20, /* corrupted page is recoverable */
94 TTU_BATCH_FLUSH = 0x40, /* Batch TLB flushes where possible
95 * and caller guarantees they will
96 * do a final flush if necessary */
97 TTU_RMAP_LOCKED = 0x80, /* do not grab rmap lock:
102 static inline void get_anon_vma(struct anon_vma *anon_vma)
104 atomic_inc(&anon_vma->refcount);
107 void __put_anon_vma(struct anon_vma *anon_vma);
109 static inline void put_anon_vma(struct anon_vma *anon_vma)
111 if (atomic_dec_and_test(&anon_vma->refcount))
112 __put_anon_vma(anon_vma);
115 static inline void anon_vma_lock_write(struct anon_vma *anon_vma)
117 down_write(&anon_vma->root->rwsem);
120 static inline void anon_vma_unlock_write(struct anon_vma *anon_vma)
122 up_write(&anon_vma->root->rwsem);
125 static inline void anon_vma_lock_read(struct anon_vma *anon_vma)
127 down_read(&anon_vma->root->rwsem);
130 static inline void anon_vma_unlock_read(struct anon_vma *anon_vma)
132 up_read(&anon_vma->root->rwsem);
137 * anon_vma helper functions.
139 void anon_vma_init(void); /* create anon_vma_cachep */
140 int __anon_vma_prepare(struct vm_area_struct *);
141 void unlink_anon_vmas(struct vm_area_struct *);
142 int anon_vma_clone(struct vm_area_struct *, struct vm_area_struct *);
143 int anon_vma_fork(struct vm_area_struct *, struct vm_area_struct *);
145 static inline int anon_vma_prepare(struct vm_area_struct *vma)
147 if (likely(vma->anon_vma))
150 return __anon_vma_prepare(vma);
153 static inline void anon_vma_merge(struct vm_area_struct *vma,
154 struct vm_area_struct *next)
156 VM_BUG_ON_VMA(vma->anon_vma != next->anon_vma, vma);
157 unlink_anon_vmas(next);
160 struct anon_vma *page_get_anon_vma(struct page *page);
162 /* bitflags for do_page_add_anon_rmap() */
163 #define RMAP_EXCLUSIVE 0x01
164 #define RMAP_COMPOUND 0x02
167 * rmap interfaces called when adding or removing pte of page
169 void page_move_anon_rmap(struct page *, struct vm_area_struct *);
170 void page_add_anon_rmap(struct page *, struct vm_area_struct *,
171 unsigned long address, bool compound);
172 void do_page_add_anon_rmap(struct page *, struct vm_area_struct *,
173 unsigned long address, int flags);
174 void page_add_new_anon_rmap(struct page *, struct vm_area_struct *,
175 unsigned long address, bool compound);
176 void page_add_file_rmap(struct page *, struct vm_area_struct *,
178 void page_remove_rmap(struct page *, struct vm_area_struct *,
180 void hugepage_add_anon_rmap(struct page *, struct vm_area_struct *,
181 unsigned long address);
182 void hugepage_add_new_anon_rmap(struct page *, struct vm_area_struct *,
183 unsigned long address);
185 static inline void page_dup_rmap(struct page *page, bool compound)
187 atomic_inc(compound ? compound_mapcount_ptr(page) : &page->_mapcount);
191 * Called from mm/vmscan.c to handle paging out
193 int folio_referenced(struct folio *, int is_locked,
194 struct mem_cgroup *memcg, unsigned long *vm_flags);
196 void try_to_migrate(struct folio *folio, enum ttu_flags flags);
197 void try_to_unmap(struct folio *, enum ttu_flags flags);
199 int make_device_exclusive_range(struct mm_struct *mm, unsigned long start,
200 unsigned long end, struct page **pages,
203 /* Avoid racy checks */
204 #define PVMW_SYNC (1 << 0)
205 /* Look for migration entries rather than present PTEs */
206 #define PVMW_MIGRATION (1 << 1)
208 struct page_vma_mapped_walk {
210 unsigned long nr_pages;
212 struct vm_area_struct *vma;
213 unsigned long address;
220 #define DEFINE_PAGE_VMA_WALK(name, _page, _vma, _address, _flags) \
221 struct page_vma_mapped_walk name = { \
222 .pfn = page_to_pfn(_page), \
223 .nr_pages = compound_nr(page), \
224 .pgoff = page_to_pgoff(page), \
226 .address = _address, \
230 #define DEFINE_FOLIO_VMA_WALK(name, _folio, _vma, _address, _flags) \
231 struct page_vma_mapped_walk name = { \
232 .pfn = folio_pfn(_folio), \
233 .nr_pages = folio_nr_pages(_folio), \
234 .pgoff = folio_pgoff(_folio), \
236 .address = _address, \
240 static inline void page_vma_mapped_walk_done(struct page_vma_mapped_walk *pvmw)
242 /* HugeTLB pte is set to the relevant page table entry without pte_mapped. */
243 if (pvmw->pte && !is_vm_hugetlb_page(pvmw->vma))
244 pte_unmap(pvmw->pte);
246 spin_unlock(pvmw->ptl);
249 bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw);
252 * Used by swapoff to help locate where page is expected in vma.
254 unsigned long page_address_in_vma(struct page *, struct vm_area_struct *);
257 * Cleans the PTEs of shared mappings.
258 * (and since clean PTEs should also be readonly, write protects them too)
260 * returns the number of cleaned PTEs.
262 int folio_mkclean(struct folio *);
264 void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked);
267 * Called by memory-failure.c to kill processes.
269 struct anon_vma *folio_lock_anon_vma_read(struct folio *folio);
270 void page_unlock_anon_vma_read(struct anon_vma *anon_vma);
271 int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma);
274 * rmap_walk_control: To control rmap traversing for specific needs
276 * arg: passed to rmap_one() and invalid_vma()
277 * rmap_one: executed on each vma where page is mapped
278 * done: for checking traversing termination condition
279 * anon_lock: for getting anon_lock by optimized way rather than default
280 * invalid_vma: for skipping uninterested vma
282 struct rmap_walk_control {
285 * Return false if page table scanning in rmap_walk should be stopped.
286 * Otherwise, return true.
288 bool (*rmap_one)(struct folio *folio, struct vm_area_struct *vma,
289 unsigned long addr, void *arg);
290 int (*done)(struct folio *folio);
291 struct anon_vma *(*anon_lock)(struct folio *folio);
292 bool (*invalid_vma)(struct vm_area_struct *vma, void *arg);
295 void rmap_walk(struct folio *folio, const struct rmap_walk_control *rwc);
296 void rmap_walk_locked(struct folio *folio, const struct rmap_walk_control *rwc);
298 #else /* !CONFIG_MMU */
300 #define anon_vma_init() do {} while (0)
301 #define anon_vma_prepare(vma) (0)
302 #define anon_vma_link(vma) do {} while (0)
304 static inline int folio_referenced(struct folio *folio, int is_locked,
305 struct mem_cgroup *memcg,
306 unsigned long *vm_flags)
312 static inline void try_to_unmap(struct folio *folio, enum ttu_flags flags)
316 static inline int folio_mkclean(struct folio *folio)
320 #endif /* CONFIG_MMU */
322 static inline int page_mkclean(struct page *page)
324 return folio_mkclean(page_folio(page));
326 #endif /* _LINUX_RMAP_H */