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