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
15 /*
16 * The anon_vma heads a list of private "related" vmas, to scan if
17 * an anonymous page pointing to this anon_vma needs to be unmapped:
18 * the vmas on the list will be related by forking, or by splitting.
19 *
20 * Since vmas come and go as they are split and merged (particularly
21 * in mprotect), the mapping field of an anonymous page cannot point
22 * directly to a vma: instead it points to an anon_vma, on whose list
23 * the related vmas can be easily linked or unlinked.
24 *
25 * After unlinking the last vma on the list, we must garbage collect
26 * the anon_vma object itself: we're guaranteed no page can be
27 * pointing to this anon_vma once its vma list is empty.
28 */
29 struct anon_vma {
30 struct anon_vma *root; /* Root of this anon_vma tree */
31 struct rw_semaphore rwsem; /* W: modification, R: walking the list */
32 /*
33 * The refcount is taken on an anon_vma when there is no
34 * guarantee that the vma of page tables will exist for
35 * the duration of the operation. A caller that takes
36 * the reference is responsible for clearing up the
37 * anon_vma if they are the last user on release
38 */
39 atomic_t refcount;
40
41 unsigned degree; /* ANDROID: KABI preservation, DO NOT USE! */
42
43 struct anon_vma *parent; /* Parent of this anon_vma */
44
45 /*
46 * NOTE: the LSB of the rb_root.rb_node is set by
47 * mm_take_all_locks() _after_ taking the above lock. So the
48 * rb_root must only be read/written after taking the above lock
49 * to be sure to see a valid next pointer. The LSB bit itself
50 * is serialized by a system wide lock only visible to
51 * mm_take_all_locks() (mm_all_locks_mutex).
52 */
53
54 /* Interval tree of private "related" vmas */
55 struct rb_root_cached rb_root;
56
57 /*
58 * ANDROID: KABI preservation, it's safe to put these at the end of this structure as it's
59 * only passed by a pointer everywhere, the size and internal structures are local to the
60 * core kernel.
61 */
62 #ifndef __GENKSYMS__
63 /*
64 * Count of child anon_vmas. Equals to the count of all anon_vmas that
65 * have ->parent pointing to this one, including itself.
66 *
67 * This counter is used for making decision about reusing anon_vma
68 * instead of forking new one. See comments in function anon_vma_clone.
69 */
70 unsigned long num_children;
71 /* Count of VMAs whose ->anon_vma pointer points to this object. */
72 unsigned long num_active_vmas;
73 #endif
74
75 };
76
77 /*
78 * The copy-on-write semantics of fork mean that an anon_vma
79 * can become associated with multiple processes. Furthermore,
80 * each child process will have its own anon_vma, where new
81 * pages for that process are instantiated.
82 *
83 * This structure allows us to find the anon_vmas associated
84 * with a VMA, or the VMAs associated with an anon_vma.
85 * The "same_vma" list contains the anon_vma_chains linking
86 * all the anon_vmas associated with this VMA.
87 * The "rb" field indexes on an interval tree the anon_vma_chains
88 * which link all the VMAs associated with this anon_vma.
89 */
90 struct anon_vma_chain {
91 struct vm_area_struct *vma;
92 struct anon_vma *anon_vma;
93 struct list_head same_vma; /* locked by mmap_sem & page_table_lock */
94 struct rb_node rb; /* locked by anon_vma->rwsem */
95 unsigned long rb_subtree_last;
96 #ifdef CONFIG_DEBUG_VM_RB
97 unsigned long cached_vma_start, cached_vma_last;
98 #endif
99 };
100
101 enum ttu_flags {
102 TTU_MIGRATION = 0x1, /* migration mode */
103 TTU_MUNLOCK = 0x2, /* munlock mode */
104
105 TTU_SPLIT_HUGE_PMD = 0x4, /* split huge PMD if any */
106 TTU_IGNORE_MLOCK = 0x8, /* ignore mlock */
107 TTU_IGNORE_ACCESS = 0x10, /* don't age */
108 TTU_IGNORE_HWPOISON = 0x20, /* corrupted page is recoverable */
109 TTU_BATCH_FLUSH = 0x40, /* Batch TLB flushes where possible
110 * and caller guarantees they will
111 * do a final flush if necessary */
112 TTU_RMAP_LOCKED = 0x80, /* do not grab rmap lock:
113 * caller holds it */
114 TTU_SPLIT_FREEZE = 0x100, /* freeze pte under splitting thp */
115 TTU_SYNC = 0x200, /* avoid racy checks with PVMW_SYNC */
116 };
117
118 #ifdef CONFIG_MMU
get_anon_vma(struct anon_vma * anon_vma)119 static inline void get_anon_vma(struct anon_vma *anon_vma)
120 {
121 atomic_inc(&anon_vma->refcount);
122 }
123
124 void __put_anon_vma(struct anon_vma *anon_vma);
125
put_anon_vma(struct anon_vma * anon_vma)126 static inline void put_anon_vma(struct anon_vma *anon_vma)
127 {
128 if (atomic_dec_and_test(&anon_vma->refcount))
129 __put_anon_vma(anon_vma);
130 }
131
anon_vma_lock_write(struct anon_vma * anon_vma)132 static inline void anon_vma_lock_write(struct anon_vma *anon_vma)
133 {
134 down_write(&anon_vma->root->rwsem);
135 }
136
anon_vma_unlock_write(struct anon_vma * anon_vma)137 static inline void anon_vma_unlock_write(struct anon_vma *anon_vma)
138 {
139 up_write(&anon_vma->root->rwsem);
140 }
141
anon_vma_lock_read(struct anon_vma * anon_vma)142 static inline void anon_vma_lock_read(struct anon_vma *anon_vma)
143 {
144 down_read(&anon_vma->root->rwsem);
145 }
146
anon_vma_trylock_read(struct anon_vma * anon_vma)147 static inline int anon_vma_trylock_read(struct anon_vma *anon_vma)
148 {
149 return down_read_trylock(&anon_vma->root->rwsem);
150 }
151
anon_vma_unlock_read(struct anon_vma * anon_vma)152 static inline void anon_vma_unlock_read(struct anon_vma *anon_vma)
153 {
154 up_read(&anon_vma->root->rwsem);
155 }
156
157
158 /*
159 * anon_vma helper functions.
160 */
161 void anon_vma_init(void); /* create anon_vma_cachep */
162 int __anon_vma_prepare(struct vm_area_struct *);
163 void unlink_anon_vmas(struct vm_area_struct *);
164 int anon_vma_clone(struct vm_area_struct *, struct vm_area_struct *);
165 int anon_vma_fork(struct vm_area_struct *, struct vm_area_struct *);
166
anon_vma_prepare(struct vm_area_struct * vma)167 static inline int anon_vma_prepare(struct vm_area_struct *vma)
168 {
169 if (likely(vma->anon_vma))
170 return 0;
171
172 return __anon_vma_prepare(vma);
173 }
174
anon_vma_merge(struct vm_area_struct * vma,struct vm_area_struct * next)175 static inline void anon_vma_merge(struct vm_area_struct *vma,
176 struct vm_area_struct *next)
177 {
178 VM_BUG_ON_VMA(vma->anon_vma != next->anon_vma, vma);
179 unlink_anon_vmas(next);
180 }
181
182 struct anon_vma *page_get_anon_vma(struct page *page);
183
184 /* bitflags for do_page_add_anon_rmap() */
185 #define RMAP_EXCLUSIVE 0x01
186 #define RMAP_COMPOUND 0x02
187
188 /*
189 * rmap interfaces called when adding or removing pte of page
190 */
191 void page_move_anon_rmap(struct page *, struct vm_area_struct *);
192 void page_add_anon_rmap(struct page *, struct vm_area_struct *,
193 unsigned long, bool);
194 void do_page_add_anon_rmap(struct page *, struct vm_area_struct *,
195 unsigned long, int);
196 void page_add_new_anon_rmap(struct page *, struct vm_area_struct *,
197 unsigned long, bool);
198 void page_add_file_rmap(struct page *, bool);
199 void page_remove_rmap(struct page *, bool);
200
201 void hugepage_add_anon_rmap(struct page *, struct vm_area_struct *,
202 unsigned long);
203 void hugepage_add_new_anon_rmap(struct page *, struct vm_area_struct *,
204 unsigned long);
205
page_dup_rmap(struct page * page,bool compound)206 static inline void page_dup_rmap(struct page *page, bool compound)
207 {
208 atomic_inc(compound ? compound_mapcount_ptr(page) : &page->_mapcount);
209 }
210
211 /*
212 * Called from mm/vmscan.c to handle paging out
213 */
214 int page_referenced(struct page *, int is_locked,
215 struct mem_cgroup *memcg, unsigned long *vm_flags);
216
217 bool try_to_unmap(struct page *, enum ttu_flags flags);
218
219 /* Avoid racy checks */
220 #define PVMW_SYNC (1 << 0)
221 /* Look for migarion entries rather than present PTEs */
222 #define PVMW_MIGRATION (1 << 1)
223
224 struct page_vma_mapped_walk {
225 struct page *page;
226 struct vm_area_struct *vma;
227 unsigned long address;
228 pmd_t *pmd;
229 pte_t *pte;
230 spinlock_t *ptl;
231 unsigned int flags;
232 };
233
page_vma_mapped_walk_done(struct page_vma_mapped_walk * pvmw)234 static inline void page_vma_mapped_walk_done(struct page_vma_mapped_walk *pvmw)
235 {
236 /* HugeTLB pte is set to the relevant page table entry without pte_mapped. */
237 if (pvmw->pte && !PageHuge(pvmw->page))
238 pte_unmap(pvmw->pte);
239 if (pvmw->ptl)
240 spin_unlock(pvmw->ptl);
241 }
242
243 bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw);
244
245 /*
246 * Used by swapoff to help locate where page is expected in vma.
247 */
248 unsigned long page_address_in_vma(struct page *, struct vm_area_struct *);
249
250 /*
251 * Cleans the PTEs of shared mappings.
252 * (and since clean PTEs should also be readonly, write protects them too)
253 *
254 * returns the number of cleaned PTEs.
255 */
256 int page_mkclean(struct page *);
257
258 /*
259 * called in munlock()/munmap() path to check for other vmas holding
260 * the page mlocked.
261 */
262 void try_to_munlock(struct page *);
263
264 void remove_migration_ptes(struct page *old, struct page *new, bool locked);
265
266 int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma);
267
268 /*
269 * rmap_walk_control: To control rmap traversing for specific needs
270 *
271 * arg: passed to rmap_one() and invalid_vma()
272 * try_lock: bail out if the rmap lock is contended
273 * contended: indicate the rmap traversal bailed out due to lock contention
274 * rmap_one: executed on each vma where page is mapped
275 * done: for checking traversing termination condition
276 * anon_lock: for getting anon_lock by optimized way rather than default
277 * invalid_vma: for skipping uninterested vma
278 */
279 struct rmap_walk_control {
280 void *arg;
281 bool try_lock;
282 bool contended;
283 /*
284 * Return false if page table scanning in rmap_walk should be stopped.
285 * Otherwise, return true.
286 */
287 bool (*rmap_one)(struct page *page, struct vm_area_struct *vma,
288 unsigned long addr, void *arg);
289 int (*done)(struct page *page);
290 struct anon_vma *(*anon_lock)(struct page *page,
291 struct rmap_walk_control *rwc);
292 bool (*invalid_vma)(struct vm_area_struct *vma, void *arg);
293 };
294
295 void rmap_walk(struct page *page, struct rmap_walk_control *rwc);
296 void rmap_walk_locked(struct page *page, struct rmap_walk_control *rwc);
297
298 /*
299 * Called by memory-failure.c to kill processes.
300 */
301 struct anon_vma *page_lock_anon_vma_read(struct page *page,
302 struct rmap_walk_control *rwc);
303 void page_unlock_anon_vma_read(struct anon_vma *anon_vma);
304
305 #else /* !CONFIG_MMU */
306
307 #define anon_vma_init() do {} while (0)
308 #define anon_vma_prepare(vma) (0)
309 #define anon_vma_link(vma) do {} while (0)
310
page_referenced(struct page * page,int is_locked,struct mem_cgroup * memcg,unsigned long * vm_flags)311 static inline int page_referenced(struct page *page, int is_locked,
312 struct mem_cgroup *memcg,
313 unsigned long *vm_flags)
314 {
315 *vm_flags = 0;
316 return 0;
317 }
318
319 #define try_to_unmap(page, refs) false
320
page_mkclean(struct page * page)321 static inline int page_mkclean(struct page *page)
322 {
323 return 0;
324 }
325
326
327 #endif /* CONFIG_MMU */
328
329 #endif /* _LINUX_RMAP_H */
330