xref: /linux-6.15/include/linux/swapops.h (revision 2fc60e2f)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SWAPOPS_H
3 #define _LINUX_SWAPOPS_H
4 
5 #include <linux/radix-tree.h>
6 #include <linux/bug.h>
7 #include <linux/mm_types.h>
8 
9 #ifdef CONFIG_MMU
10 
11 #ifdef CONFIG_SWAP
12 #include <linux/swapfile.h>
13 #endif	/* CONFIG_SWAP */
14 
15 /*
16  * swapcache pages are stored in the swapper_space radix tree.  We want to
17  * get good packing density in that tree, so the index should be dense in
18  * the low-order bits.
19  *
20  * We arrange the `type' and `offset' fields so that `type' is at the six
21  * high-order bits of the swp_entry_t and `offset' is right-aligned in the
22  * remaining bits.  Although `type' itself needs only five bits, we allow for
23  * shmem/tmpfs to shift it all up a further one bit: see swp_to_radix_entry().
24  *
25  * swp_entry_t's are *never* stored anywhere in their arch-dependent format.
26  */
27 #define SWP_TYPE_SHIFT	(BITS_PER_XA_VALUE - MAX_SWAPFILES_SHIFT)
28 #define SWP_OFFSET_MASK	((1UL << SWP_TYPE_SHIFT) - 1)
29 
30 /*
31  * Definitions only for PFN swap entries (see is_pfn_swap_entry()).  To
32  * store PFN, we only need SWP_PFN_BITS bits.  Each of the pfn swap entries
33  * can use the extra bits to store other information besides PFN.
34  */
35 #ifdef MAX_PHYSMEM_BITS
36 #define SWP_PFN_BITS		(MAX_PHYSMEM_BITS - PAGE_SHIFT)
37 #else  /* MAX_PHYSMEM_BITS */
38 #define SWP_PFN_BITS		min_t(int, \
39 				      sizeof(phys_addr_t) * 8 - PAGE_SHIFT, \
40 				      SWP_TYPE_SHIFT)
41 #endif	/* MAX_PHYSMEM_BITS */
42 #define SWP_PFN_MASK		(BIT(SWP_PFN_BITS) - 1)
43 
44 /**
45  * Migration swap entry specific bitfield definitions.  Layout:
46  *
47  *   |----------+--------------------|
48  *   | swp_type | swp_offset         |
49  *   |----------+--------+-+-+-------|
50  *   |          | resv   |D|A|  PFN  |
51  *   |----------+--------+-+-+-------|
52  *
53  * @SWP_MIG_YOUNG_BIT: Whether the page used to have young bit set (bit A)
54  * @SWP_MIG_DIRTY_BIT: Whether the page used to have dirty bit set (bit D)
55  *
56  * Note: A/D bits will be stored in migration entries iff there're enough
57  * free bits in arch specific swp offset.  By default we'll ignore A/D bits
58  * when migrating a page.  Please refer to migration_entry_supports_ad()
59  * for more information.  If there're more bits besides PFN and A/D bits,
60  * they should be reserved and always be zeros.
61  */
62 #define SWP_MIG_YOUNG_BIT		(SWP_PFN_BITS)
63 #define SWP_MIG_DIRTY_BIT		(SWP_PFN_BITS + 1)
64 #define SWP_MIG_TOTAL_BITS		(SWP_PFN_BITS + 2)
65 
66 #define SWP_MIG_YOUNG			BIT(SWP_MIG_YOUNG_BIT)
67 #define SWP_MIG_DIRTY			BIT(SWP_MIG_DIRTY_BIT)
68 
69 static inline bool is_pfn_swap_entry(swp_entry_t entry);
70 
71 /* Clear all flags but only keep swp_entry_t related information */
72 static inline pte_t pte_swp_clear_flags(pte_t pte)
73 {
74 	if (pte_swp_exclusive(pte))
75 		pte = pte_swp_clear_exclusive(pte);
76 	if (pte_swp_soft_dirty(pte))
77 		pte = pte_swp_clear_soft_dirty(pte);
78 	if (pte_swp_uffd_wp(pte))
79 		pte = pte_swp_clear_uffd_wp(pte);
80 	return pte;
81 }
82 
83 /*
84  * Store a type+offset into a swp_entry_t in an arch-independent format
85  */
86 static inline swp_entry_t swp_entry(unsigned long type, pgoff_t offset)
87 {
88 	swp_entry_t ret;
89 
90 	ret.val = (type << SWP_TYPE_SHIFT) | (offset & SWP_OFFSET_MASK);
91 	return ret;
92 }
93 
94 /*
95  * Extract the `type' field from a swp_entry_t.  The swp_entry_t is in
96  * arch-independent format
97  */
98 static inline unsigned swp_type(swp_entry_t entry)
99 {
100 	return (entry.val >> SWP_TYPE_SHIFT);
101 }
102 
103 /*
104  * Extract the `offset' field from a swp_entry_t.  The swp_entry_t is in
105  * arch-independent format
106  */
107 static inline pgoff_t swp_offset(swp_entry_t entry)
108 {
109 	return entry.val & SWP_OFFSET_MASK;
110 }
111 
112 /*
113  * This should only be called upon a pfn swap entry to get the PFN stored
114  * in the swap entry.  Please refers to is_pfn_swap_entry() for definition
115  * of pfn swap entry.
116  */
117 static inline unsigned long swp_offset_pfn(swp_entry_t entry)
118 {
119 	VM_BUG_ON(!is_pfn_swap_entry(entry));
120 	return swp_offset(entry) & SWP_PFN_MASK;
121 }
122 
123 /* check whether a pte points to a swap entry */
124 static inline int is_swap_pte(pte_t pte)
125 {
126 	return !pte_none(pte) && !pte_present(pte);
127 }
128 
129 /*
130  * Convert the arch-dependent pte representation of a swp_entry_t into an
131  * arch-independent swp_entry_t.
132  */
133 static inline swp_entry_t pte_to_swp_entry(pte_t pte)
134 {
135 	swp_entry_t arch_entry;
136 
137 	pte = pte_swp_clear_flags(pte);
138 	arch_entry = __pte_to_swp_entry(pte);
139 	return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
140 }
141 
142 /*
143  * Convert the arch-independent representation of a swp_entry_t into the
144  * arch-dependent pte representation.
145  */
146 static inline pte_t swp_entry_to_pte(swp_entry_t entry)
147 {
148 	swp_entry_t arch_entry;
149 
150 	arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
151 	return __swp_entry_to_pte(arch_entry);
152 }
153 
154 static inline swp_entry_t radix_to_swp_entry(void *arg)
155 {
156 	swp_entry_t entry;
157 
158 	entry.val = xa_to_value(arg);
159 	return entry;
160 }
161 
162 static inline void *swp_to_radix_entry(swp_entry_t entry)
163 {
164 	return xa_mk_value(entry.val);
165 }
166 
167 static inline swp_entry_t make_swapin_error_entry(struct page *page)
168 {
169 	return swp_entry(SWP_SWAPIN_ERROR, page_to_pfn(page));
170 }
171 
172 static inline int is_swapin_error_entry(swp_entry_t entry)
173 {
174 	return swp_type(entry) == SWP_SWAPIN_ERROR;
175 }
176 
177 #if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
178 static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
179 {
180 	return swp_entry(SWP_DEVICE_READ, offset);
181 }
182 
183 static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
184 {
185 	return swp_entry(SWP_DEVICE_WRITE, offset);
186 }
187 
188 static inline bool is_device_private_entry(swp_entry_t entry)
189 {
190 	int type = swp_type(entry);
191 	return type == SWP_DEVICE_READ || type == SWP_DEVICE_WRITE;
192 }
193 
194 static inline bool is_writable_device_private_entry(swp_entry_t entry)
195 {
196 	return unlikely(swp_type(entry) == SWP_DEVICE_WRITE);
197 }
198 
199 static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
200 {
201 	return swp_entry(SWP_DEVICE_EXCLUSIVE_READ, offset);
202 }
203 
204 static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
205 {
206 	return swp_entry(SWP_DEVICE_EXCLUSIVE_WRITE, offset);
207 }
208 
209 static inline bool is_device_exclusive_entry(swp_entry_t entry)
210 {
211 	return swp_type(entry) == SWP_DEVICE_EXCLUSIVE_READ ||
212 		swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE;
213 }
214 
215 static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
216 {
217 	return unlikely(swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE);
218 }
219 #else /* CONFIG_DEVICE_PRIVATE */
220 static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
221 {
222 	return swp_entry(0, 0);
223 }
224 
225 static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
226 {
227 	return swp_entry(0, 0);
228 }
229 
230 static inline bool is_device_private_entry(swp_entry_t entry)
231 {
232 	return false;
233 }
234 
235 static inline bool is_writable_device_private_entry(swp_entry_t entry)
236 {
237 	return false;
238 }
239 
240 static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
241 {
242 	return swp_entry(0, 0);
243 }
244 
245 static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
246 {
247 	return swp_entry(0, 0);
248 }
249 
250 static inline bool is_device_exclusive_entry(swp_entry_t entry)
251 {
252 	return false;
253 }
254 
255 static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
256 {
257 	return false;
258 }
259 #endif /* CONFIG_DEVICE_PRIVATE */
260 
261 #ifdef CONFIG_MIGRATION
262 static inline int is_migration_entry(swp_entry_t entry)
263 {
264 	return unlikely(swp_type(entry) == SWP_MIGRATION_READ ||
265 			swp_type(entry) == SWP_MIGRATION_READ_EXCLUSIVE ||
266 			swp_type(entry) == SWP_MIGRATION_WRITE);
267 }
268 
269 static inline int is_writable_migration_entry(swp_entry_t entry)
270 {
271 	return unlikely(swp_type(entry) == SWP_MIGRATION_WRITE);
272 }
273 
274 static inline int is_readable_migration_entry(swp_entry_t entry)
275 {
276 	return unlikely(swp_type(entry) == SWP_MIGRATION_READ);
277 }
278 
279 static inline int is_readable_exclusive_migration_entry(swp_entry_t entry)
280 {
281 	return unlikely(swp_type(entry) == SWP_MIGRATION_READ_EXCLUSIVE);
282 }
283 
284 static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
285 {
286 	return swp_entry(SWP_MIGRATION_READ, offset);
287 }
288 
289 static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
290 {
291 	return swp_entry(SWP_MIGRATION_READ_EXCLUSIVE, offset);
292 }
293 
294 static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
295 {
296 	return swp_entry(SWP_MIGRATION_WRITE, offset);
297 }
298 
299 /*
300  * Returns whether the host has large enough swap offset field to support
301  * carrying over pgtable A/D bits for page migrations.  The result is
302  * pretty much arch specific.
303  */
304 static inline bool migration_entry_supports_ad(void)
305 {
306 #ifdef CONFIG_SWAP
307 	return swap_migration_ad_supported;
308 #else  /* CONFIG_SWAP */
309 	return false;
310 #endif	/* CONFIG_SWAP */
311 }
312 
313 static inline swp_entry_t make_migration_entry_young(swp_entry_t entry)
314 {
315 	if (migration_entry_supports_ad())
316 		return swp_entry(swp_type(entry),
317 				 swp_offset(entry) | SWP_MIG_YOUNG);
318 	return entry;
319 }
320 
321 static inline bool is_migration_entry_young(swp_entry_t entry)
322 {
323 	if (migration_entry_supports_ad())
324 		return swp_offset(entry) & SWP_MIG_YOUNG;
325 	/* Keep the old behavior of aging page after migration */
326 	return false;
327 }
328 
329 static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry)
330 {
331 	if (migration_entry_supports_ad())
332 		return swp_entry(swp_type(entry),
333 				 swp_offset(entry) | SWP_MIG_DIRTY);
334 	return entry;
335 }
336 
337 static inline bool is_migration_entry_dirty(swp_entry_t entry)
338 {
339 	if (migration_entry_supports_ad())
340 		return swp_offset(entry) & SWP_MIG_DIRTY;
341 	/* Keep the old behavior of clean page after migration */
342 	return false;
343 }
344 
345 extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
346 					spinlock_t *ptl);
347 extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
348 					unsigned long address);
349 #ifdef CONFIG_HUGETLB_PAGE
350 extern void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl);
351 extern void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *pte);
352 #endif	/* CONFIG_HUGETLB_PAGE */
353 #else  /* CONFIG_MIGRATION */
354 static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
355 {
356 	return swp_entry(0, 0);
357 }
358 
359 static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
360 {
361 	return swp_entry(0, 0);
362 }
363 
364 static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
365 {
366 	return swp_entry(0, 0);
367 }
368 
369 static inline int is_migration_entry(swp_entry_t swp)
370 {
371 	return 0;
372 }
373 
374 static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
375 					spinlock_t *ptl) { }
376 static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
377 					 unsigned long address) { }
378 #ifdef CONFIG_HUGETLB_PAGE
379 static inline void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl) { }
380 static inline void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *pte) { }
381 #endif	/* CONFIG_HUGETLB_PAGE */
382 static inline int is_writable_migration_entry(swp_entry_t entry)
383 {
384 	return 0;
385 }
386 static inline int is_readable_migration_entry(swp_entry_t entry)
387 {
388 	return 0;
389 }
390 
391 static inline swp_entry_t make_migration_entry_young(swp_entry_t entry)
392 {
393 	return entry;
394 }
395 
396 static inline bool is_migration_entry_young(swp_entry_t entry)
397 {
398 	return false;
399 }
400 
401 static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry)
402 {
403 	return entry;
404 }
405 
406 static inline bool is_migration_entry_dirty(swp_entry_t entry)
407 {
408 	return false;
409 }
410 #endif	/* CONFIG_MIGRATION */
411 
412 typedef unsigned long pte_marker;
413 
414 #define  PTE_MARKER_UFFD_WP  BIT(0)
415 #define  PTE_MARKER_MASK     (PTE_MARKER_UFFD_WP)
416 
417 #ifdef CONFIG_PTE_MARKER
418 
419 static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
420 {
421 	return swp_entry(SWP_PTE_MARKER, marker);
422 }
423 
424 static inline bool is_pte_marker_entry(swp_entry_t entry)
425 {
426 	return swp_type(entry) == SWP_PTE_MARKER;
427 }
428 
429 static inline pte_marker pte_marker_get(swp_entry_t entry)
430 {
431 	return swp_offset(entry) & PTE_MARKER_MASK;
432 }
433 
434 static inline bool is_pte_marker(pte_t pte)
435 {
436 	return is_swap_pte(pte) && is_pte_marker_entry(pte_to_swp_entry(pte));
437 }
438 
439 #else /* CONFIG_PTE_MARKER */
440 
441 static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
442 {
443 	/* This should never be called if !CONFIG_PTE_MARKER */
444 	WARN_ON_ONCE(1);
445 	return swp_entry(0, 0);
446 }
447 
448 static inline bool is_pte_marker_entry(swp_entry_t entry)
449 {
450 	return false;
451 }
452 
453 static inline pte_marker pte_marker_get(swp_entry_t entry)
454 {
455 	return 0;
456 }
457 
458 static inline bool is_pte_marker(pte_t pte)
459 {
460 	return false;
461 }
462 
463 #endif /* CONFIG_PTE_MARKER */
464 
465 static inline pte_t make_pte_marker(pte_marker marker)
466 {
467 	return swp_entry_to_pte(make_pte_marker_entry(marker));
468 }
469 
470 /*
471  * This is a special version to check pte_none() just to cover the case when
472  * the pte is a pte marker.  It existed because in many cases the pte marker
473  * should be seen as a none pte; it's just that we have stored some information
474  * onto the none pte so it becomes not-none any more.
475  *
476  * It should be used when the pte is file-backed, ram-based and backing
477  * userspace pages, like shmem.  It is not needed upon pgtables that do not
478  * support pte markers at all.  For example, it's not needed on anonymous
479  * memory, kernel-only memory (including when the system is during-boot),
480  * non-ram based generic file-system.  It's fine to be used even there, but the
481  * extra pte marker check will be pure overhead.
482  *
483  * For systems configured with !CONFIG_PTE_MARKER this will be automatically
484  * optimized to pte_none().
485  */
486 static inline int pte_none_mostly(pte_t pte)
487 {
488 	return pte_none(pte) || is_pte_marker(pte);
489 }
490 
491 static inline struct page *pfn_swap_entry_to_page(swp_entry_t entry)
492 {
493 	struct page *p = pfn_to_page(swp_offset_pfn(entry));
494 
495 	/*
496 	 * Any use of migration entries may only occur while the
497 	 * corresponding page is locked
498 	 */
499 	BUG_ON(is_migration_entry(entry) && !PageLocked(p));
500 
501 	return p;
502 }
503 
504 /*
505  * A pfn swap entry is a special type of swap entry that always has a pfn stored
506  * in the swap offset. They are used to represent unaddressable device memory
507  * and to restrict access to a page undergoing migration.
508  */
509 static inline bool is_pfn_swap_entry(swp_entry_t entry)
510 {
511 	/* Make sure the swp offset can always store the needed fields */
512 	BUILD_BUG_ON(SWP_TYPE_SHIFT < SWP_PFN_BITS);
513 
514 	return is_migration_entry(entry) || is_device_private_entry(entry) ||
515 	       is_device_exclusive_entry(entry);
516 }
517 
518 struct page_vma_mapped_walk;
519 
520 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
521 extern int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
522 		struct page *page);
523 
524 extern void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
525 		struct page *new);
526 
527 extern void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd);
528 
529 static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
530 {
531 	swp_entry_t arch_entry;
532 
533 	if (pmd_swp_soft_dirty(pmd))
534 		pmd = pmd_swp_clear_soft_dirty(pmd);
535 	if (pmd_swp_uffd_wp(pmd))
536 		pmd = pmd_swp_clear_uffd_wp(pmd);
537 	arch_entry = __pmd_to_swp_entry(pmd);
538 	return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
539 }
540 
541 static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
542 {
543 	swp_entry_t arch_entry;
544 
545 	arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
546 	return __swp_entry_to_pmd(arch_entry);
547 }
548 
549 static inline int is_pmd_migration_entry(pmd_t pmd)
550 {
551 	return is_swap_pmd(pmd) && is_migration_entry(pmd_to_swp_entry(pmd));
552 }
553 #else  /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
554 static inline int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
555 		struct page *page)
556 {
557 	BUILD_BUG();
558 }
559 
560 static inline void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
561 		struct page *new)
562 {
563 	BUILD_BUG();
564 }
565 
566 static inline void pmd_migration_entry_wait(struct mm_struct *m, pmd_t *p) { }
567 
568 static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
569 {
570 	return swp_entry(0, 0);
571 }
572 
573 static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
574 {
575 	return __pmd(0);
576 }
577 
578 static inline int is_pmd_migration_entry(pmd_t pmd)
579 {
580 	return 0;
581 }
582 #endif  /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
583 
584 #ifdef CONFIG_MEMORY_FAILURE
585 
586 extern atomic_long_t num_poisoned_pages __read_mostly;
587 
588 /*
589  * Support for hardware poisoned pages
590  */
591 static inline swp_entry_t make_hwpoison_entry(struct page *page)
592 {
593 	BUG_ON(!PageLocked(page));
594 	return swp_entry(SWP_HWPOISON, page_to_pfn(page));
595 }
596 
597 static inline int is_hwpoison_entry(swp_entry_t entry)
598 {
599 	return swp_type(entry) == SWP_HWPOISON;
600 }
601 
602 static inline void num_poisoned_pages_inc(void)
603 {
604 	atomic_long_inc(&num_poisoned_pages);
605 }
606 
607 static inline void num_poisoned_pages_sub(long i)
608 {
609 	atomic_long_sub(i, &num_poisoned_pages);
610 }
611 
612 #else  /* CONFIG_MEMORY_FAILURE */
613 
614 static inline swp_entry_t make_hwpoison_entry(struct page *page)
615 {
616 	return swp_entry(0, 0);
617 }
618 
619 static inline int is_hwpoison_entry(swp_entry_t swp)
620 {
621 	return 0;
622 }
623 
624 static inline void num_poisoned_pages_inc(void)
625 {
626 }
627 
628 static inline void num_poisoned_pages_sub(long i)
629 {
630 }
631 #endif  /* CONFIG_MEMORY_FAILURE */
632 
633 static inline int non_swap_entry(swp_entry_t entry)
634 {
635 	return swp_type(entry) >= MAX_SWAPFILES;
636 }
637 
638 #endif /* CONFIG_MMU */
639 #endif /* _LINUX_SWAPOPS_H */
640