• 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_SPLIT_HUGE_PMD	= 0x4,	/* split huge PMD if any */
107 	TTU_IGNORE_MLOCK	= 0x8,	/* ignore mlock */
108 	TTU_SYNC		= 0x10,	/* avoid racy checks with PVMW_SYNC */
109 	TTU_IGNORE_HWPOISON	= 0x20,	/* corrupted page is recoverable */
110 	TTU_BATCH_FLUSH		= 0x40,	/* Batch TLB flushes where possible
111 					 * and caller guarantees they will
112 					 * do a final flush if necessary */
113 	TTU_RMAP_LOCKED		= 0x80,	/* do not grab rmap lock:
114 					 * caller holds it */
115 };
116 
117 #ifdef CONFIG_MMU
get_anon_vma(struct anon_vma * anon_vma)118 static inline void get_anon_vma(struct anon_vma *anon_vma)
119 {
120 	atomic_inc(&anon_vma->refcount);
121 }
122 
123 void __put_anon_vma(struct anon_vma *anon_vma);
124 
put_anon_vma(struct anon_vma * anon_vma)125 static inline void put_anon_vma(struct anon_vma *anon_vma)
126 {
127 	if (atomic_dec_and_test(&anon_vma->refcount))
128 		__put_anon_vma(anon_vma);
129 }
130 
anon_vma_lock_write(struct anon_vma * anon_vma)131 static inline void anon_vma_lock_write(struct anon_vma *anon_vma)
132 {
133 	down_write(&anon_vma->root->rwsem);
134 }
135 
anon_vma_unlock_write(struct anon_vma * anon_vma)136 static inline void anon_vma_unlock_write(struct anon_vma *anon_vma)
137 {
138 	up_write(&anon_vma->root->rwsem);
139 }
140 
anon_vma_lock_read(struct anon_vma * anon_vma)141 static inline void anon_vma_lock_read(struct anon_vma *anon_vma)
142 {
143 	down_read(&anon_vma->root->rwsem);
144 }
145 
anon_vma_trylock_read(struct anon_vma * anon_vma)146 static inline int anon_vma_trylock_read(struct anon_vma *anon_vma)
147 {
148 	return down_read_trylock(&anon_vma->root->rwsem);
149 }
150 
anon_vma_unlock_read(struct anon_vma * anon_vma)151 static inline void anon_vma_unlock_read(struct anon_vma *anon_vma)
152 {
153 	up_read(&anon_vma->root->rwsem);
154 }
155 
156 
157 /*
158  * anon_vma helper functions.
159  */
160 void anon_vma_init(void);	/* create anon_vma_cachep */
161 int  __anon_vma_prepare(struct vm_area_struct *);
162 void unlink_anon_vmas(struct vm_area_struct *);
163 int anon_vma_clone(struct vm_area_struct *, struct vm_area_struct *);
164 int anon_vma_fork(struct vm_area_struct *, struct vm_area_struct *);
165 
anon_vma_prepare(struct vm_area_struct * vma)166 static inline int anon_vma_prepare(struct vm_area_struct *vma)
167 {
168 	if (likely(vma->anon_vma))
169 		return 0;
170 
171 	return __anon_vma_prepare(vma);
172 }
173 
anon_vma_merge(struct vm_area_struct * vma,struct vm_area_struct * next)174 static inline void anon_vma_merge(struct vm_area_struct *vma,
175 				  struct vm_area_struct *next)
176 {
177 	VM_BUG_ON_VMA(vma->anon_vma != next->anon_vma, vma);
178 	unlink_anon_vmas(next);
179 }
180 
181 struct anon_vma *page_get_anon_vma(struct page *page);
182 
183 /* bitflags for do_page_add_anon_rmap() */
184 #define RMAP_EXCLUSIVE 0x01
185 #define RMAP_COMPOUND 0x02
186 
187 /*
188  * rmap interfaces called when adding or removing pte of page
189  */
190 void page_move_anon_rmap(struct page *, struct vm_area_struct *);
191 void page_add_anon_rmap(struct page *, struct vm_area_struct *,
192 		unsigned long, bool);
193 void do_page_add_anon_rmap(struct page *, struct vm_area_struct *,
194 			   unsigned long, int);
195 void page_add_new_anon_rmap(struct page *, struct vm_area_struct *,
196 		unsigned long, bool);
197 void page_add_file_rmap(struct page *, bool);
198 void page_remove_rmap(struct page *, bool);
199 
200 void hugepage_add_anon_rmap(struct page *, struct vm_area_struct *,
201 			    unsigned long);
202 void hugepage_add_new_anon_rmap(struct page *, struct vm_area_struct *,
203 				unsigned long);
204 
page_dup_rmap(struct page * page,bool compound)205 static inline void page_dup_rmap(struct page *page, bool compound)
206 {
207 	bool success = false;
208 
209 	if (!compound)
210 		trace_android_vh_update_page_mapcount(page, true, compound, NULL, &success);
211 	if (!success)
212 		atomic_inc(compound ? compound_mapcount_ptr(page) : &page->_mapcount);
213 }
214 
215 /*
216  * Called from mm/vmscan.c to handle paging out
217  */
218 int page_referenced(struct page *, int is_locked,
219 			struct mem_cgroup *memcg, unsigned long *vm_flags);
220 
221 void try_to_migrate(struct page *page, enum ttu_flags flags);
222 void try_to_unmap(struct page *, enum ttu_flags flags);
223 
224 int make_device_exclusive_range(struct mm_struct *mm, unsigned long start,
225 				unsigned long end, struct page **pages,
226 				void *arg);
227 
228 /* Avoid racy checks */
229 #define PVMW_SYNC		(1 << 0)
230 /* Look for migarion entries rather than present PTEs */
231 #define PVMW_MIGRATION		(1 << 1)
232 
233 struct page_vma_mapped_walk {
234 	struct page *page;
235 	struct vm_area_struct *vma;
236 	unsigned long address;
237 	pmd_t *pmd;
238 	pte_t *pte;
239 	spinlock_t *ptl;
240 	unsigned int flags;
241 };
242 
page_vma_mapped_walk_done(struct page_vma_mapped_walk * pvmw)243 static inline void page_vma_mapped_walk_done(struct page_vma_mapped_walk *pvmw)
244 {
245 	/* HugeTLB pte is set to the relevant page table entry without pte_mapped. */
246 	if (pvmw->pte && !PageHuge(pvmw->page))
247 		pte_unmap(pvmw->pte);
248 	if (pvmw->ptl)
249 		spin_unlock(pvmw->ptl);
250 }
251 
252 bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw);
253 
254 /*
255  * Used by swapoff to help locate where page is expected in vma.
256  */
257 unsigned long page_address_in_vma(struct page *, struct vm_area_struct *);
258 
259 /*
260  * Cleans the PTEs of shared mappings.
261  * (and since clean PTEs should also be readonly, write protects them too)
262  *
263  * returns the number of cleaned PTEs.
264  */
265 int page_mkclean(struct page *);
266 
267 /*
268  * called in munlock()/munmap() path to check for other vmas holding
269  * the page mlocked.
270  */
271 void page_mlock(struct page *page);
272 
273 void remove_migration_ptes(struct page *old, struct page *new, bool locked);
274 
275 int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma);
276 
277 /*
278  * rmap_walk_control: To control rmap traversing for specific needs
279  *
280  * arg: passed to rmap_one() and invalid_vma()
281  * try_lock: bail out if the rmap lock is contended
282  * contended: indicate the rmap traversal bailed out due to lock contention
283  * rmap_one: executed on each vma where page is mapped
284  * done: for checking traversing termination condition
285  * anon_lock: for getting anon_lock by optimized way rather than default
286  * invalid_vma: for skipping uninterested vma
287  */
288 struct rmap_walk_control {
289 	void *arg;
290 	bool try_lock;
291 	bool contended;
292 	/*
293 	 * Return false if page table scanning in rmap_walk should be stopped.
294 	 * Otherwise, return true.
295 	 */
296 	bool (*rmap_one)(struct page *page, struct vm_area_struct *vma,
297 					unsigned long addr, void *arg);
298 	int (*done)(struct page *page);
299 	struct anon_vma *(*anon_lock)(struct page *page,
300 				      struct rmap_walk_control *rwc);
301 	bool (*invalid_vma)(struct vm_area_struct *vma, void *arg);
302 };
303 
304 void rmap_walk(struct page *page, struct rmap_walk_control *rwc);
305 void rmap_walk_locked(struct page *page, struct rmap_walk_control *rwc);
306 
307 /*
308  * Called by memory-failure.c to kill processes.
309  */
310 struct anon_vma *page_lock_anon_vma_read(struct page *page,
311 					 struct rmap_walk_control *rwc);
312 void page_unlock_anon_vma_read(struct anon_vma *anon_vma);
313 
314 #else	/* !CONFIG_MMU */
315 
316 #define anon_vma_init()		do {} while (0)
317 #define anon_vma_prepare(vma)	(0)
318 #define anon_vma_link(vma)	do {} while (0)
319 
page_referenced(struct page * page,int is_locked,struct mem_cgroup * memcg,unsigned long * vm_flags)320 static inline int page_referenced(struct page *page, int is_locked,
321 				  struct mem_cgroup *memcg,
322 				  unsigned long *vm_flags)
323 {
324 	*vm_flags = 0;
325 	return 0;
326 }
327 
try_to_unmap(struct page * page,enum ttu_flags flags)328 static inline void try_to_unmap(struct page *page, enum ttu_flags flags)
329 {
330 }
331 
page_mkclean(struct page * page)332 static inline int page_mkclean(struct page *page)
333 {
334 	return 0;
335 }
336 
337 
338 #endif	/* CONFIG_MMU */
339 
340 #endif	/* _LINUX_RMAP_H */
341