Line data Source code
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 :
16 : /*
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.
20 : *
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.
25 : *
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.
29 : */
30 : struct anon_vma {
31 : struct anon_vma *root; /* Root of this anon_vma tree */
32 : struct rw_semaphore rwsem; /* W: modification, R: walking the list */
33 : /*
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
39 : */
40 : atomic_t refcount;
41 :
42 : /*
43 : * Count of child anon_vmas and VMAs which points to this anon_vma.
44 : *
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.
47 : */
48 : unsigned degree;
49 :
50 : struct anon_vma *parent; /* Parent of this anon_vma */
51 :
52 : /*
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).
59 : */
60 :
61 : /* Interval tree of private "related" vmas */
62 : struct rb_root_cached rb_root;
63 : };
64 :
65 : /*
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.
70 : *
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.
77 : */
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;
86 : #endif
87 : };
88 :
89 : enum ttu_flags {
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:
98 : * caller holds it */
99 : };
100 :
101 : #ifdef CONFIG_MMU
102 : static inline void get_anon_vma(struct anon_vma *anon_vma)
103 : {
104 0 : atomic_inc(&anon_vma->refcount);
105 : }
106 :
107 : void __put_anon_vma(struct anon_vma *anon_vma);
108 :
109 : static inline void put_anon_vma(struct anon_vma *anon_vma)
110 : {
111 0 : if (atomic_dec_and_test(&anon_vma->refcount))
112 0 : __put_anon_vma(anon_vma);
113 : }
114 :
115 : static inline void anon_vma_lock_write(struct anon_vma *anon_vma)
116 : {
117 0 : down_write(&anon_vma->root->rwsem);
118 : }
119 :
120 : static inline void anon_vma_unlock_write(struct anon_vma *anon_vma)
121 : {
122 0 : up_write(&anon_vma->root->rwsem);
123 : }
124 :
125 : static inline void anon_vma_lock_read(struct anon_vma *anon_vma)
126 : {
127 0 : down_read(&anon_vma->root->rwsem);
128 : }
129 :
130 : static inline void anon_vma_unlock_read(struct anon_vma *anon_vma)
131 : {
132 0 : up_read(&anon_vma->root->rwsem);
133 : }
134 :
135 :
136 : /*
137 : * anon_vma helper functions.
138 : */
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 *);
144 :
145 : static inline int anon_vma_prepare(struct vm_area_struct *vma)
146 : {
147 0 : if (likely(vma->anon_vma))
148 : return 0;
149 :
150 0 : return __anon_vma_prepare(vma);
151 : }
152 :
153 : static inline void anon_vma_merge(struct vm_area_struct *vma,
154 : struct vm_area_struct *next)
155 : {
156 : VM_BUG_ON_VMA(vma->anon_vma != next->anon_vma, vma);
157 0 : unlink_anon_vmas(next);
158 : }
159 :
160 : struct anon_vma *page_get_anon_vma(struct page *page);
161 :
162 : /* bitflags for do_page_add_anon_rmap() */
163 : #define RMAP_EXCLUSIVE 0x01
164 : #define RMAP_COMPOUND 0x02
165 :
166 : /*
167 : * rmap interfaces called when adding or removing pte of page
168 : */
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 *,
177 : bool compound);
178 : void page_remove_rmap(struct page *, struct vm_area_struct *,
179 : bool compound);
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);
184 :
185 : static inline void page_dup_rmap(struct page *page, bool compound)
186 : {
187 0 : atomic_inc(compound ? compound_mapcount_ptr(page) : &page->_mapcount);
188 : }
189 :
190 : /*
191 : * Called from mm/vmscan.c to handle paging out
192 : */
193 : int folio_referenced(struct folio *, int is_locked,
194 : struct mem_cgroup *memcg, unsigned long *vm_flags);
195 :
196 : void try_to_migrate(struct folio *folio, enum ttu_flags flags);
197 : void try_to_unmap(struct folio *, enum ttu_flags flags);
198 :
199 : int make_device_exclusive_range(struct mm_struct *mm, unsigned long start,
200 : unsigned long end, struct page **pages,
201 : void *arg);
202 :
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)
207 :
208 : struct page_vma_mapped_walk {
209 : unsigned long pfn;
210 : unsigned long nr_pages;
211 : pgoff_t pgoff;
212 : struct vm_area_struct *vma;
213 : unsigned long address;
214 : pmd_t *pmd;
215 : pte_t *pte;
216 : spinlock_t *ptl;
217 : unsigned int flags;
218 : };
219 :
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), \
225 : .vma = _vma, \
226 : .address = _address, \
227 : .flags = _flags, \
228 : }
229 :
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), \
235 : .vma = _vma, \
236 : .address = _address, \
237 : .flags = _flags, \
238 : }
239 :
240 : static inline void page_vma_mapped_walk_done(struct page_vma_mapped_walk *pvmw)
241 : {
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);
245 0 : if (pvmw->ptl)
246 0 : spin_unlock(pvmw->ptl);
247 : }
248 :
249 : bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw);
250 :
251 : /*
252 : * Used by swapoff to help locate where page is expected in vma.
253 : */
254 : unsigned long page_address_in_vma(struct page *, struct vm_area_struct *);
255 :
256 : /*
257 : * Cleans the PTEs of shared mappings.
258 : * (and since clean PTEs should also be readonly, write protects them too)
259 : *
260 : * returns the number of cleaned PTEs.
261 : */
262 : int folio_mkclean(struct folio *);
263 :
264 : void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked);
265 :
266 : /*
267 : * Called by memory-failure.c to kill processes.
268 : */
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);
272 :
273 : /*
274 : * rmap_walk_control: To control rmap traversing for specific needs
275 : *
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
281 : */
282 : struct rmap_walk_control {
283 : void *arg;
284 : /*
285 : * Return false if page table scanning in rmap_walk should be stopped.
286 : * Otherwise, return true.
287 : */
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);
293 : };
294 :
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);
297 :
298 : #else /* !CONFIG_MMU */
299 :
300 : #define anon_vma_init() do {} while (0)
301 : #define anon_vma_prepare(vma) (0)
302 : #define anon_vma_link(vma) do {} while (0)
303 :
304 : static inline int folio_referenced(struct folio *folio, int is_locked,
305 : struct mem_cgroup *memcg,
306 : unsigned long *vm_flags)
307 : {
308 : *vm_flags = 0;
309 : return 0;
310 : }
311 :
312 : static inline void try_to_unmap(struct folio *folio, enum ttu_flags flags)
313 : {
314 : }
315 :
316 : static inline int folio_mkclean(struct folio *folio)
317 : {
318 : return 0;
319 : }
320 : #endif /* CONFIG_MMU */
321 :
322 : static inline int page_mkclean(struct page *page)
323 : {
324 0 : return folio_mkclean(page_folio(page));
325 : }
326 : #endif /* _LINUX_RMAP_H */
|