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