1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef LINUX_MM_INLINE_H
3 #define LINUX_MM_INLINE_H
4 
5 #include <linux/atomic.h>
6 #include <linux/huge_mm.h>
7 #include <linux/mm_types.h>
8 #include <linux/swap.h>
9 #include <linux/string.h>
10 #include <linux/userfaultfd_k.h>
11 #include <linux/swapops.h>
12 
13 /**
14  * folio_is_file_lru - Should the folio be on a file LRU or anon LRU?
15  * @folio: The folio to test.
16  *
17  * We would like to get this info without a page flag, but the state
18  * needs to survive until the folio is last deleted from the LRU, which
19  * could be as far down as __page_cache_release.
20  *
21  * Return: An integer (not a boolean!) used to sort a folio onto the
22  * right LRU list and to account folios correctly.
23  * 1 if @folio is a regular filesystem backed page cache folio
24  * or a lazily freed anonymous folio (e.g. via MADV_FREE).
25  * 0 if @folio is a normal anonymous folio, a tmpfs folio or otherwise
26  * ram or swap backed folio.
27  */
folio_is_file_lru(struct folio * folio)28 static inline int folio_is_file_lru(struct folio *folio)
29 {
30 	return !folio_test_swapbacked(folio);
31 }
32 
page_is_file_lru(struct page * page)33 static inline int page_is_file_lru(struct page *page)
34 {
35 	return folio_is_file_lru(page_folio(page));
36 }
37 
__update_lru_size(struct lruvec * lruvec,enum lru_list lru,enum zone_type zid,long nr_pages)38 static __always_inline void __update_lru_size(struct lruvec *lruvec,
39 				enum lru_list lru, enum zone_type zid,
40 				long nr_pages)
41 {
42 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
43 
44 	lockdep_assert_held(&lruvec->lru_lock);
45 	WARN_ON_ONCE(nr_pages != (int)nr_pages);
46 
47 	__mod_lruvec_state(lruvec, NR_LRU_BASE + lru, nr_pages);
48 	__mod_zone_page_state(&pgdat->node_zones[zid],
49 				NR_ZONE_LRU_BASE + lru, nr_pages);
50 }
51 
update_lru_size(struct lruvec * lruvec,enum lru_list lru,enum zone_type zid,long nr_pages)52 static __always_inline void update_lru_size(struct lruvec *lruvec,
53 				enum lru_list lru, enum zone_type zid,
54 				long nr_pages)
55 {
56 	__update_lru_size(lruvec, lru, zid, nr_pages);
57 #ifdef CONFIG_MEMCG
58 	mem_cgroup_update_lru_size(lruvec, lru, zid, nr_pages);
59 #endif
60 }
61 
62 /**
63  * __folio_clear_lru_flags - Clear page lru flags before releasing a page.
64  * @folio: The folio that was on lru and now has a zero reference.
65  */
__folio_clear_lru_flags(struct folio * folio)66 static __always_inline void __folio_clear_lru_flags(struct folio *folio)
67 {
68 	VM_BUG_ON_FOLIO(!folio_test_lru(folio), folio);
69 
70 	__folio_clear_lru(folio);
71 
72 	/* this shouldn't happen, so leave the flags to bad_page() */
73 	if (folio_test_active(folio) && folio_test_unevictable(folio))
74 		return;
75 
76 	__folio_clear_active(folio);
77 	__folio_clear_unevictable(folio);
78 }
79 
80 /**
81  * folio_lru_list - Which LRU list should a folio be on?
82  * @folio: The folio to test.
83  *
84  * Return: The LRU list a folio should be on, as an index
85  * into the array of LRU lists.
86  */
folio_lru_list(struct folio * folio)87 static __always_inline enum lru_list folio_lru_list(struct folio *folio)
88 {
89 	enum lru_list lru;
90 
91 	VM_BUG_ON_FOLIO(folio_test_active(folio) && folio_test_unevictable(folio), folio);
92 
93 	if (folio_test_unevictable(folio))
94 		return LRU_UNEVICTABLE;
95 
96 	lru = folio_is_file_lru(folio) ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON;
97 	if (folio_test_active(folio))
98 		lru += LRU_ACTIVE;
99 
100 	return lru;
101 }
102 
103 #ifdef CONFIG_LRU_GEN
104 
105 #ifdef CONFIG_LRU_GEN_ENABLED
lru_gen_enabled(void)106 static inline bool lru_gen_enabled(void)
107 {
108 	DECLARE_STATIC_KEY_TRUE(lru_gen_caps[NR_LRU_GEN_CAPS]);
109 
110 	return static_branch_likely(&lru_gen_caps[LRU_GEN_CORE]);
111 }
112 #else
lru_gen_enabled(void)113 static inline bool lru_gen_enabled(void)
114 {
115 	DECLARE_STATIC_KEY_FALSE(lru_gen_caps[NR_LRU_GEN_CAPS]);
116 
117 	return static_branch_unlikely(&lru_gen_caps[LRU_GEN_CORE]);
118 }
119 #endif
120 
lru_gen_in_fault(void)121 static inline bool lru_gen_in_fault(void)
122 {
123 	return current->in_lru_fault;
124 }
125 
lru_gen_from_seq(unsigned long seq)126 static inline int lru_gen_from_seq(unsigned long seq)
127 {
128 	return seq % MAX_NR_GENS;
129 }
130 
lru_hist_from_seq(unsigned long seq)131 static inline int lru_hist_from_seq(unsigned long seq)
132 {
133 	return seq % NR_HIST_GENS;
134 }
135 
lru_tier_from_refs(int refs,bool workingset)136 static inline int lru_tier_from_refs(int refs, bool workingset)
137 {
138 	VM_WARN_ON_ONCE(refs > BIT(LRU_REFS_WIDTH));
139 
140 	/* see the comment on MAX_NR_TIERS */
141 	return workingset ? MAX_NR_TIERS - 1 : order_base_2(refs);
142 }
143 
folio_lru_refs(struct folio * folio)144 static inline int folio_lru_refs(struct folio *folio)
145 {
146 	unsigned long flags = READ_ONCE(folio->flags);
147 
148 	if (!(flags & BIT(PG_referenced)))
149 		return 0;
150 	/*
151 	 * Return the total number of accesses including PG_referenced. Also see
152 	 * the comment on LRU_REFS_FLAGS.
153 	 */
154 	return ((flags & LRU_REFS_MASK) >> LRU_REFS_PGOFF) + 1;
155 }
156 
folio_lru_gen(struct folio * folio)157 static inline int folio_lru_gen(struct folio *folio)
158 {
159 	unsigned long flags = READ_ONCE(folio->flags);
160 
161 	return ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
162 }
163 
lru_gen_is_active(struct lruvec * lruvec,int gen)164 static inline bool lru_gen_is_active(struct lruvec *lruvec, int gen)
165 {
166 	unsigned long max_seq = lruvec->lrugen.max_seq;
167 
168 	VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
169 
170 	/* see the comment on MIN_NR_GENS */
171 	return gen == lru_gen_from_seq(max_seq) || gen == lru_gen_from_seq(max_seq - 1);
172 }
173 
lru_gen_update_size(struct lruvec * lruvec,struct folio * folio,int old_gen,int new_gen)174 static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio *folio,
175 				       int old_gen, int new_gen)
176 {
177 	int type = folio_is_file_lru(folio);
178 	int zone = folio_zonenum(folio);
179 	int delta = folio_nr_pages(folio);
180 	enum lru_list lru = type * LRU_INACTIVE_FILE;
181 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
182 
183 	VM_WARN_ON_ONCE(old_gen != -1 && old_gen >= MAX_NR_GENS);
184 	VM_WARN_ON_ONCE(new_gen != -1 && new_gen >= MAX_NR_GENS);
185 	VM_WARN_ON_ONCE(old_gen == -1 && new_gen == -1);
186 
187 	if (old_gen >= 0)
188 		WRITE_ONCE(lrugen->nr_pages[old_gen][type][zone],
189 			   lrugen->nr_pages[old_gen][type][zone] - delta);
190 	if (new_gen >= 0)
191 		WRITE_ONCE(lrugen->nr_pages[new_gen][type][zone],
192 			   lrugen->nr_pages[new_gen][type][zone] + delta);
193 
194 	/* addition */
195 	if (old_gen < 0) {
196 		if (lru_gen_is_active(lruvec, new_gen))
197 			lru += LRU_ACTIVE;
198 		__update_lru_size(lruvec, lru, zone, delta);
199 		return;
200 	}
201 
202 	/* deletion */
203 	if (new_gen < 0) {
204 		if (lru_gen_is_active(lruvec, old_gen))
205 			lru += LRU_ACTIVE;
206 		__update_lru_size(lruvec, lru, zone, -delta);
207 		return;
208 	}
209 
210 	/* promotion */
211 	if (!lru_gen_is_active(lruvec, old_gen) && lru_gen_is_active(lruvec, new_gen)) {
212 		__update_lru_size(lruvec, lru, zone, -delta);
213 		__update_lru_size(lruvec, lru + LRU_ACTIVE, zone, delta);
214 	}
215 
216 	/* demotion requires isolation, e.g., lru_deactivate_fn() */
217 	VM_WARN_ON_ONCE(lru_gen_is_active(lruvec, old_gen) && !lru_gen_is_active(lruvec, new_gen));
218 }
219 
lru_gen_folio_seq(struct lruvec * lruvec,struct folio * folio,bool reclaiming)220 static inline unsigned long lru_gen_folio_seq(struct lruvec *lruvec, struct folio *folio,
221 					      bool reclaiming)
222 {
223 	int gen;
224 	int type = folio_is_file_lru(folio);
225 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
226 
227 	/*
228 	 * +-----------------------------------+-----------------------------------+
229 	 * | Accessed through page tables and  | Accessed through file descriptors |
230 	 * | promoted by folio_update_gen()    | and protected by folio_inc_gen()  |
231 	 * +-----------------------------------+-----------------------------------+
232 	 * | PG_active (set while isolated)    |                                   |
233 	 * +-----------------+-----------------+-----------------+-----------------+
234 	 * |  PG_workingset  |  PG_referenced  |  PG_workingset  |  LRU_REFS_FLAGS |
235 	 * +-----------------------------------+-----------------------------------+
236 	 * |<---------- MIN_NR_GENS ---------->|                                   |
237 	 * |<---------------------------- MAX_NR_GENS ---------------------------->|
238 	 */
239 	if (folio_test_active(folio))
240 		gen = MIN_NR_GENS - folio_test_workingset(folio);
241 	else if (reclaiming)
242 		gen = MAX_NR_GENS;
243 	else if ((!folio_is_file_lru(folio) && !folio_test_swapcache(folio)) ||
244 		 (folio_test_reclaim(folio) &&
245 		  (folio_test_dirty(folio) || folio_test_writeback(folio))))
246 		gen = MIN_NR_GENS;
247 	else
248 		gen = MAX_NR_GENS - folio_test_workingset(folio);
249 
250 	return max(READ_ONCE(lrugen->max_seq) - gen + 1, READ_ONCE(lrugen->min_seq[type]));
251 }
252 
lru_gen_add_folio(struct lruvec * lruvec,struct folio * folio,bool reclaiming)253 static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)
254 {
255 	unsigned long seq;
256 	unsigned long flags;
257 	int gen = folio_lru_gen(folio);
258 	int type = folio_is_file_lru(folio);
259 	int zone = folio_zonenum(folio);
260 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
261 
262 	VM_WARN_ON_ONCE_FOLIO(gen != -1, folio);
263 
264 	if (folio_test_unevictable(folio) || !lrugen->enabled)
265 		return false;
266 
267 	seq = lru_gen_folio_seq(lruvec, folio, reclaiming);
268 	gen = lru_gen_from_seq(seq);
269 	flags = (gen + 1UL) << LRU_GEN_PGOFF;
270 	/* see the comment on MIN_NR_GENS about PG_active */
271 	set_mask_bits(&folio->flags, LRU_GEN_MASK | BIT(PG_active), flags);
272 
273 	lru_gen_update_size(lruvec, folio, -1, gen);
274 	/* for folio_rotate_reclaimable() */
275 	if (reclaiming)
276 		list_add_tail(&folio->lru, &lrugen->folios[gen][type][zone]);
277 	else
278 		list_add(&folio->lru, &lrugen->folios[gen][type][zone]);
279 
280 	return true;
281 }
282 
lru_gen_del_folio(struct lruvec * lruvec,struct folio * folio,bool reclaiming)283 static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)
284 {
285 	unsigned long flags;
286 	int gen = folio_lru_gen(folio);
287 
288 	if (gen < 0)
289 		return false;
290 
291 	VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio);
292 	VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
293 
294 	/* for folio_migrate_flags() */
295 	flags = !reclaiming && lru_gen_is_active(lruvec, gen) ? BIT(PG_active) : 0;
296 	flags = set_mask_bits(&folio->flags, LRU_GEN_MASK, flags);
297 	gen = ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
298 
299 	lru_gen_update_size(lruvec, folio, gen, -1);
300 	list_del(&folio->lru);
301 
302 	return true;
303 }
304 
folio_migrate_refs(struct folio * new,struct folio * old)305 static inline void folio_migrate_refs(struct folio *new, struct folio *old)
306 {
307 	unsigned long refs = READ_ONCE(old->flags) & LRU_REFS_MASK;
308 
309 	set_mask_bits(&new->flags, LRU_REFS_MASK, refs);
310 }
311 #else /* !CONFIG_LRU_GEN */
312 
lru_gen_enabled(void)313 static inline bool lru_gen_enabled(void)
314 {
315 	return false;
316 }
317 
lru_gen_in_fault(void)318 static inline bool lru_gen_in_fault(void)
319 {
320 	return false;
321 }
322 
lru_gen_add_folio(struct lruvec * lruvec,struct folio * folio,bool reclaiming)323 static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)
324 {
325 	return false;
326 }
327 
lru_gen_del_folio(struct lruvec * lruvec,struct folio * folio,bool reclaiming)328 static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)
329 {
330 	return false;
331 }
332 
folio_migrate_refs(struct folio * new,struct folio * old)333 static inline void folio_migrate_refs(struct folio *new, struct folio *old)
334 {
335 
336 }
337 #endif /* CONFIG_LRU_GEN */
338 
339 static __always_inline
lruvec_add_folio(struct lruvec * lruvec,struct folio * folio)340 void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio)
341 {
342 	enum lru_list lru = folio_lru_list(folio);
343 
344 	if (lru_gen_add_folio(lruvec, folio, false))
345 		return;
346 
347 	update_lru_size(lruvec, lru, folio_zonenum(folio),
348 			folio_nr_pages(folio));
349 	if (lru != LRU_UNEVICTABLE)
350 		list_add(&folio->lru, &lruvec->lists[lru]);
351 }
352 
353 static __always_inline
lruvec_add_folio_tail(struct lruvec * lruvec,struct folio * folio)354 void lruvec_add_folio_tail(struct lruvec *lruvec, struct folio *folio)
355 {
356 	enum lru_list lru = folio_lru_list(folio);
357 
358 	if (lru_gen_add_folio(lruvec, folio, true))
359 		return;
360 
361 	update_lru_size(lruvec, lru, folio_zonenum(folio),
362 			folio_nr_pages(folio));
363 	/* This is not expected to be used on LRU_UNEVICTABLE */
364 	list_add_tail(&folio->lru, &lruvec->lists[lru]);
365 }
366 
367 static __always_inline
lruvec_del_folio(struct lruvec * lruvec,struct folio * folio)368 void lruvec_del_folio(struct lruvec *lruvec, struct folio *folio)
369 {
370 	enum lru_list lru = folio_lru_list(folio);
371 
372 	if (lru_gen_del_folio(lruvec, folio, false))
373 		return;
374 
375 	if (lru != LRU_UNEVICTABLE)
376 		list_del(&folio->lru);
377 	update_lru_size(lruvec, lru, folio_zonenum(folio),
378 			-folio_nr_pages(folio));
379 }
380 
381 #ifdef CONFIG_ANON_VMA_NAME
382 /* mmap_lock should be read-locked */
anon_vma_name_get(struct anon_vma_name * anon_name)383 static inline void anon_vma_name_get(struct anon_vma_name *anon_name)
384 {
385 	if (anon_name)
386 		kref_get(&anon_name->kref);
387 }
388 
anon_vma_name_put(struct anon_vma_name * anon_name)389 static inline void anon_vma_name_put(struct anon_vma_name *anon_name)
390 {
391 	if (anon_name)
392 		kref_put(&anon_name->kref, anon_vma_name_free);
393 }
394 
395 static inline
anon_vma_name_reuse(struct anon_vma_name * anon_name)396 struct anon_vma_name *anon_vma_name_reuse(struct anon_vma_name *anon_name)
397 {
398 	/* Prevent anon_name refcount saturation early on */
399 	if (kref_read(&anon_name->kref) < REFCOUNT_MAX) {
400 		anon_vma_name_get(anon_name);
401 		return anon_name;
402 
403 	}
404 	return anon_vma_name_alloc(anon_name->name);
405 }
406 
dup_anon_vma_name(struct vm_area_struct * orig_vma,struct vm_area_struct * new_vma)407 static inline void dup_anon_vma_name(struct vm_area_struct *orig_vma,
408 				     struct vm_area_struct *new_vma)
409 {
410 	struct anon_vma_name *anon_name = anon_vma_name(orig_vma);
411 
412 	if (anon_name)
413 		new_vma->anon_name = anon_vma_name_reuse(anon_name);
414 }
415 
free_anon_vma_name(struct vm_area_struct * vma)416 static inline void free_anon_vma_name(struct vm_area_struct *vma)
417 {
418 	/*
419 	 * Not using anon_vma_name because it generates a warning if mmap_lock
420 	 * is not held, which might be the case here.
421 	 */
422 	anon_vma_name_put(vma->anon_name);
423 }
424 
anon_vma_name_eq(struct anon_vma_name * anon_name1,struct anon_vma_name * anon_name2)425 static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1,
426 				    struct anon_vma_name *anon_name2)
427 {
428 	if (anon_name1 == anon_name2)
429 		return true;
430 
431 	return anon_name1 && anon_name2 &&
432 		!strcmp(anon_name1->name, anon_name2->name);
433 }
434 
435 #else /* CONFIG_ANON_VMA_NAME */
anon_vma_name_get(struct anon_vma_name * anon_name)436 static inline void anon_vma_name_get(struct anon_vma_name *anon_name) {}
anon_vma_name_put(struct anon_vma_name * anon_name)437 static inline void anon_vma_name_put(struct anon_vma_name *anon_name) {}
dup_anon_vma_name(struct vm_area_struct * orig_vma,struct vm_area_struct * new_vma)438 static inline void dup_anon_vma_name(struct vm_area_struct *orig_vma,
439 				     struct vm_area_struct *new_vma) {}
free_anon_vma_name(struct vm_area_struct * vma)440 static inline void free_anon_vma_name(struct vm_area_struct *vma) {}
441 
anon_vma_name_eq(struct anon_vma_name * anon_name1,struct anon_vma_name * anon_name2)442 static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1,
443 				    struct anon_vma_name *anon_name2)
444 {
445 	return true;
446 }
447 
448 #endif  /* CONFIG_ANON_VMA_NAME */
449 
init_tlb_flush_pending(struct mm_struct * mm)450 static inline void init_tlb_flush_pending(struct mm_struct *mm)
451 {
452 	atomic_set(&mm->tlb_flush_pending, 0);
453 }
454 
inc_tlb_flush_pending(struct mm_struct * mm)455 static inline void inc_tlb_flush_pending(struct mm_struct *mm)
456 {
457 	atomic_inc(&mm->tlb_flush_pending);
458 	/*
459 	 * The only time this value is relevant is when there are indeed pages
460 	 * to flush. And we'll only flush pages after changing them, which
461 	 * requires the PTL.
462 	 *
463 	 * So the ordering here is:
464 	 *
465 	 *	atomic_inc(&mm->tlb_flush_pending);
466 	 *	spin_lock(&ptl);
467 	 *	...
468 	 *	set_pte_at();
469 	 *	spin_unlock(&ptl);
470 	 *
471 	 *				spin_lock(&ptl)
472 	 *				mm_tlb_flush_pending();
473 	 *				....
474 	 *				spin_unlock(&ptl);
475 	 *
476 	 *	flush_tlb_range();
477 	 *	atomic_dec(&mm->tlb_flush_pending);
478 	 *
479 	 * Where the increment if constrained by the PTL unlock, it thus
480 	 * ensures that the increment is visible if the PTE modification is
481 	 * visible. After all, if there is no PTE modification, nobody cares
482 	 * about TLB flushes either.
483 	 *
484 	 * This very much relies on users (mm_tlb_flush_pending() and
485 	 * mm_tlb_flush_nested()) only caring about _specific_ PTEs (and
486 	 * therefore specific PTLs), because with SPLIT_PTE_PTLOCKS and RCpc
487 	 * locks (PPC) the unlock of one doesn't order against the lock of
488 	 * another PTL.
489 	 *
490 	 * The decrement is ordered by the flush_tlb_range(), such that
491 	 * mm_tlb_flush_pending() will not return false unless all flushes have
492 	 * completed.
493 	 */
494 }
495 
dec_tlb_flush_pending(struct mm_struct * mm)496 static inline void dec_tlb_flush_pending(struct mm_struct *mm)
497 {
498 	/*
499 	 * See inc_tlb_flush_pending().
500 	 *
501 	 * This cannot be smp_mb__before_atomic() because smp_mb() simply does
502 	 * not order against TLB invalidate completion, which is what we need.
503 	 *
504 	 * Therefore we must rely on tlb_flush_*() to guarantee order.
505 	 */
506 	atomic_dec(&mm->tlb_flush_pending);
507 }
508 
mm_tlb_flush_pending(struct mm_struct * mm)509 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
510 {
511 	/*
512 	 * Must be called after having acquired the PTL; orders against that
513 	 * PTLs release and therefore ensures that if we observe the modified
514 	 * PTE we must also observe the increment from inc_tlb_flush_pending().
515 	 *
516 	 * That is, it only guarantees to return true if there is a flush
517 	 * pending for _this_ PTL.
518 	 */
519 	return atomic_read(&mm->tlb_flush_pending);
520 }
521 
mm_tlb_flush_nested(struct mm_struct * mm)522 static inline bool mm_tlb_flush_nested(struct mm_struct *mm)
523 {
524 	/*
525 	 * Similar to mm_tlb_flush_pending(), we must have acquired the PTL
526 	 * for which there is a TLB flush pending in order to guarantee
527 	 * we've seen both that PTE modification and the increment.
528 	 *
529 	 * (no requirement on actually still holding the PTL, that is irrelevant)
530 	 */
531 	return atomic_read(&mm->tlb_flush_pending) > 1;
532 }
533 
534 #ifdef CONFIG_MMU
535 /*
536  * Computes the pte marker to copy from the given source entry into dst_vma.
537  * If no marker should be copied, returns 0.
538  * The caller should insert a new pte created with make_pte_marker().
539  */
copy_pte_marker(swp_entry_t entry,struct vm_area_struct * dst_vma)540 static inline pte_marker copy_pte_marker(
541 		swp_entry_t entry, struct vm_area_struct *dst_vma)
542 {
543 	pte_marker srcm = pte_marker_get(entry);
544 	/* Always copy error entries. */
545 	pte_marker dstm = srcm & (PTE_MARKER_POISONED | PTE_MARKER_GUARD);
546 
547 	/* Only copy PTE markers if UFFD register matches. */
548 	if ((srcm & PTE_MARKER_UFFD_WP) && userfaultfd_wp(dst_vma))
549 		dstm |= PTE_MARKER_UFFD_WP;
550 
551 	return dstm;
552 }
553 #endif
554 
555 /*
556  * If this pte is wr-protected by uffd-wp in any form, arm the special pte to
557  * replace a none pte.  NOTE!  This should only be called when *pte is already
558  * cleared so we will never accidentally replace something valuable.  Meanwhile
559  * none pte also means we are not demoting the pte so tlb flushed is not needed.
560  * E.g., when pte cleared the caller should have taken care of the tlb flush.
561  *
562  * Must be called with pgtable lock held so that no thread will see the none
563  * pte, and if they see it, they'll fault and serialize at the pgtable lock.
564  *
565  * Returns true if an uffd-wp pte was installed, false otherwise.
566  */
567 static inline bool
pte_install_uffd_wp_if_needed(struct vm_area_struct * vma,unsigned long addr,pte_t * pte,pte_t pteval)568 pte_install_uffd_wp_if_needed(struct vm_area_struct *vma, unsigned long addr,
569 			      pte_t *pte, pte_t pteval)
570 {
571 #ifdef CONFIG_PTE_MARKER_UFFD_WP
572 	bool arm_uffd_pte = false;
573 
574 	/* The current status of the pte should be "cleared" before calling */
575 	WARN_ON_ONCE(!pte_none(ptep_get(pte)));
576 
577 	/*
578 	 * NOTE: userfaultfd_wp_unpopulated() doesn't need this whole
579 	 * thing, because when zapping either it means it's dropping the
580 	 * page, or in TTU where the present pte will be quickly replaced
581 	 * with a swap pte.  There's no way of leaking the bit.
582 	 */
583 	if (vma_is_anonymous(vma) || !userfaultfd_wp(vma))
584 		return false;
585 
586 	/* A uffd-wp wr-protected normal pte */
587 	if (unlikely(pte_present(pteval) && pte_uffd_wp(pteval)))
588 		arm_uffd_pte = true;
589 
590 	/*
591 	 * A uffd-wp wr-protected swap pte.  Note: this should even cover an
592 	 * existing pte marker with uffd-wp bit set.
593 	 */
594 	if (unlikely(pte_swp_uffd_wp_any(pteval)))
595 		arm_uffd_pte = true;
596 
597 	if (unlikely(arm_uffd_pte)) {
598 		set_pte_at(vma->vm_mm, addr, pte,
599 			   make_pte_marker(PTE_MARKER_UFFD_WP));
600 		return true;
601 	}
602 #endif
603 	return false;
604 }
605 
vma_has_recency(struct vm_area_struct * vma)606 static inline bool vma_has_recency(struct vm_area_struct *vma)
607 {
608 	if (vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ))
609 		return false;
610 
611 	if (vma->vm_file && (vma->vm_file->f_mode & FMODE_NOREUSE))
612 		return false;
613 
614 	return true;
615 }
616 
617 #endif
618