1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SWAPOPS_H
3 #define _LINUX_SWAPOPS_H
5 #include <linux/radix-tree.h>
7 #include <linux/mm_types.h>
12 #include <linux/swapfile.h>
13 #endif /* CONFIG_SWAP */
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
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().
25 * swp_entry_t's are *never* stored anywhere in their arch-dependent format.
27 #define SWP_TYPE_SHIFT (BITS_PER_XA_VALUE - MAX_SWAPFILES_SHIFT)
28 #define SWP_OFFSET_MASK ((1UL << SWP_TYPE_SHIFT) - 1)
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.
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, \
41 #endif /* MAX_PHYSMEM_BITS */
42 #define SWP_PFN_MASK (BIT(SWP_PFN_BITS) - 1)
45 * Migration swap entry specific bitfield definitions. Layout:
47 * |----------+--------------------|
48 * | swp_type | swp_offset |
49 * |----------+--------+-+-+-------|
50 * | | resv |D|A| PFN |
51 * |----------+--------+-+-+-------|
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)
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.
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)
66 #define SWP_MIG_YOUNG BIT(SWP_MIG_YOUNG_BIT)
67 #define SWP_MIG_DIRTY BIT(SWP_MIG_DIRTY_BIT)
69 static inline bool is_pfn_swap_entry(swp_entry_t entry
);
71 /* Clear all flags but only keep swp_entry_t related information */
72 static inline pte_t
pte_swp_clear_flags(pte_t pte
)
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
);
84 * Store a type+offset into a swp_entry_t in an arch-independent format
86 static inline swp_entry_t
swp_entry(unsigned long type
, pgoff_t offset
)
90 ret
.val
= (type
<< SWP_TYPE_SHIFT
) | (offset
& SWP_OFFSET_MASK
);
95 * Extract the `type' field from a swp_entry_t. The swp_entry_t is in
96 * arch-independent format
98 static inline unsigned swp_type(swp_entry_t entry
)
100 return (entry
.val
>> SWP_TYPE_SHIFT
);
104 * Extract the `offset' field from a swp_entry_t. The swp_entry_t is in
105 * arch-independent format
107 static inline pgoff_t
swp_offset(swp_entry_t entry
)
109 return entry
.val
& SWP_OFFSET_MASK
;
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
117 static inline unsigned long swp_offset_pfn(swp_entry_t entry
)
119 VM_BUG_ON(!is_pfn_swap_entry(entry
));
120 return swp_offset(entry
) & SWP_PFN_MASK
;
123 /* check whether a pte points to a swap entry */
124 static inline int is_swap_pte(pte_t pte
)
126 return !pte_none(pte
) && !pte_present(pte
);
130 * Convert the arch-dependent pte representation of a swp_entry_t into an
131 * arch-independent swp_entry_t.
133 static inline swp_entry_t
pte_to_swp_entry(pte_t pte
)
135 swp_entry_t arch_entry
;
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
));
143 * Convert the arch-independent representation of a swp_entry_t into the
144 * arch-dependent pte representation.
146 static inline pte_t
swp_entry_to_pte(swp_entry_t entry
)
148 swp_entry_t arch_entry
;
150 arch_entry
= __swp_entry(swp_type(entry
), swp_offset(entry
));
151 return __swp_entry_to_pte(arch_entry
);
154 static inline swp_entry_t
radix_to_swp_entry(void *arg
)
158 entry
.val
= xa_to_value(arg
);
162 static inline void *swp_to_radix_entry(swp_entry_t entry
)
164 return xa_mk_value(entry
.val
);
167 #if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
168 static inline swp_entry_t
make_readable_device_private_entry(pgoff_t offset
)
170 return swp_entry(SWP_DEVICE_READ
, offset
);
173 static inline swp_entry_t
make_writable_device_private_entry(pgoff_t offset
)
175 return swp_entry(SWP_DEVICE_WRITE
, offset
);
178 static inline bool is_device_private_entry(swp_entry_t entry
)
180 int type
= swp_type(entry
);
181 return type
== SWP_DEVICE_READ
|| type
== SWP_DEVICE_WRITE
;
184 static inline bool is_writable_device_private_entry(swp_entry_t entry
)
186 return unlikely(swp_type(entry
) == SWP_DEVICE_WRITE
);
189 static inline swp_entry_t
make_readable_device_exclusive_entry(pgoff_t offset
)
191 return swp_entry(SWP_DEVICE_EXCLUSIVE_READ
, offset
);
194 static inline swp_entry_t
make_writable_device_exclusive_entry(pgoff_t offset
)
196 return swp_entry(SWP_DEVICE_EXCLUSIVE_WRITE
, offset
);
199 static inline bool is_device_exclusive_entry(swp_entry_t entry
)
201 return swp_type(entry
) == SWP_DEVICE_EXCLUSIVE_READ
||
202 swp_type(entry
) == SWP_DEVICE_EXCLUSIVE_WRITE
;
205 static inline bool is_writable_device_exclusive_entry(swp_entry_t entry
)
207 return unlikely(swp_type(entry
) == SWP_DEVICE_EXCLUSIVE_WRITE
);
209 #else /* CONFIG_DEVICE_PRIVATE */
210 static inline swp_entry_t
make_readable_device_private_entry(pgoff_t offset
)
212 return swp_entry(0, 0);
215 static inline swp_entry_t
make_writable_device_private_entry(pgoff_t offset
)
217 return swp_entry(0, 0);
220 static inline bool is_device_private_entry(swp_entry_t entry
)
225 static inline bool is_writable_device_private_entry(swp_entry_t entry
)
230 static inline swp_entry_t
make_readable_device_exclusive_entry(pgoff_t offset
)
232 return swp_entry(0, 0);
235 static inline swp_entry_t
make_writable_device_exclusive_entry(pgoff_t offset
)
237 return swp_entry(0, 0);
240 static inline bool is_device_exclusive_entry(swp_entry_t entry
)
245 static inline bool is_writable_device_exclusive_entry(swp_entry_t entry
)
249 #endif /* CONFIG_DEVICE_PRIVATE */
251 #ifdef CONFIG_MIGRATION
252 static inline int is_migration_entry(swp_entry_t entry
)
254 return unlikely(swp_type(entry
) == SWP_MIGRATION_READ
||
255 swp_type(entry
) == SWP_MIGRATION_READ_EXCLUSIVE
||
256 swp_type(entry
) == SWP_MIGRATION_WRITE
);
259 static inline int is_writable_migration_entry(swp_entry_t entry
)
261 return unlikely(swp_type(entry
) == SWP_MIGRATION_WRITE
);
264 static inline int is_readable_migration_entry(swp_entry_t entry
)
266 return unlikely(swp_type(entry
) == SWP_MIGRATION_READ
);
269 static inline int is_readable_exclusive_migration_entry(swp_entry_t entry
)
271 return unlikely(swp_type(entry
) == SWP_MIGRATION_READ_EXCLUSIVE
);
274 static inline swp_entry_t
make_readable_migration_entry(pgoff_t offset
)
276 return swp_entry(SWP_MIGRATION_READ
, offset
);
279 static inline swp_entry_t
make_readable_exclusive_migration_entry(pgoff_t offset
)
281 return swp_entry(SWP_MIGRATION_READ_EXCLUSIVE
, offset
);
284 static inline swp_entry_t
make_writable_migration_entry(pgoff_t offset
)
286 return swp_entry(SWP_MIGRATION_WRITE
, offset
);
290 * Returns whether the host has large enough swap offset field to support
291 * carrying over pgtable A/D bits for page migrations. The result is
292 * pretty much arch specific.
294 static inline bool migration_entry_supports_ad(void)
297 return swap_migration_ad_supported
;
298 #else /* CONFIG_SWAP */
300 #endif /* CONFIG_SWAP */
303 static inline swp_entry_t
make_migration_entry_young(swp_entry_t entry
)
305 if (migration_entry_supports_ad())
306 return swp_entry(swp_type(entry
),
307 swp_offset(entry
) | SWP_MIG_YOUNG
);
311 static inline bool is_migration_entry_young(swp_entry_t entry
)
313 if (migration_entry_supports_ad())
314 return swp_offset(entry
) & SWP_MIG_YOUNG
;
315 /* Keep the old behavior of aging page after migration */
319 static inline swp_entry_t
make_migration_entry_dirty(swp_entry_t entry
)
321 if (migration_entry_supports_ad())
322 return swp_entry(swp_type(entry
),
323 swp_offset(entry
) | SWP_MIG_DIRTY
);
327 static inline bool is_migration_entry_dirty(swp_entry_t entry
)
329 if (migration_entry_supports_ad())
330 return swp_offset(entry
) & SWP_MIG_DIRTY
;
331 /* Keep the old behavior of clean page after migration */
335 extern void migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
,
336 unsigned long address
);
337 extern void migration_entry_wait_huge(struct vm_area_struct
*vma
, unsigned long addr
, pte_t
*pte
);
338 #else /* CONFIG_MIGRATION */
339 static inline swp_entry_t
make_readable_migration_entry(pgoff_t offset
)
341 return swp_entry(0, 0);
344 static inline swp_entry_t
make_readable_exclusive_migration_entry(pgoff_t offset
)
346 return swp_entry(0, 0);
349 static inline swp_entry_t
make_writable_migration_entry(pgoff_t offset
)
351 return swp_entry(0, 0);
354 static inline int is_migration_entry(swp_entry_t swp
)
359 static inline void migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
,
360 unsigned long address
) { }
361 static inline void migration_entry_wait_huge(struct vm_area_struct
*vma
,
362 unsigned long addr
, pte_t
*pte
) { }
363 static inline int is_writable_migration_entry(swp_entry_t entry
)
367 static inline int is_readable_migration_entry(swp_entry_t entry
)
372 static inline swp_entry_t
make_migration_entry_young(swp_entry_t entry
)
377 static inline bool is_migration_entry_young(swp_entry_t entry
)
382 static inline swp_entry_t
make_migration_entry_dirty(swp_entry_t entry
)
387 static inline bool is_migration_entry_dirty(swp_entry_t entry
)
391 #endif /* CONFIG_MIGRATION */
393 #ifdef CONFIG_MEMORY_FAILURE
396 * Support for hardware poisoned pages
398 static inline swp_entry_t
make_hwpoison_entry(struct page
*page
)
400 BUG_ON(!PageLocked(page
));
401 return swp_entry(SWP_HWPOISON
, page_to_pfn(page
));
404 static inline int is_hwpoison_entry(swp_entry_t entry
)
406 return swp_type(entry
) == SWP_HWPOISON
;
411 static inline swp_entry_t
make_hwpoison_entry(struct page
*page
)
413 return swp_entry(0, 0);
416 static inline int is_hwpoison_entry(swp_entry_t swp
)
422 typedef unsigned long pte_marker
;
424 #define PTE_MARKER_UFFD_WP BIT(0)
426 * "Poisoned" here is meant in the very general sense of "future accesses are
427 * invalid", instead of referring very specifically to hardware memory errors.
428 * This marker is meant to represent any of various different causes of this.
430 * Note that, when encountered by the faulting logic, PTEs with this marker will
431 * result in VM_FAULT_HWPOISON and thus regardless trigger hardware memory error
434 #define PTE_MARKER_POISONED BIT(1)
436 * Indicates that, on fault, this PTE will case a SIGSEGV signal to be
437 * sent. This means guard markers behave in effect as if the region were mapped
438 * PROT_NONE, rather than if they were a memory hole or equivalent.
440 #define PTE_MARKER_GUARD BIT(2)
441 #define PTE_MARKER_MASK (BIT(3) - 1)
443 static inline swp_entry_t
make_pte_marker_entry(pte_marker marker
)
445 return swp_entry(SWP_PTE_MARKER
, marker
);
448 static inline bool is_pte_marker_entry(swp_entry_t entry
)
450 return swp_type(entry
) == SWP_PTE_MARKER
;
453 static inline pte_marker
pte_marker_get(swp_entry_t entry
)
455 return swp_offset(entry
) & PTE_MARKER_MASK
;
458 static inline bool is_pte_marker(pte_t pte
)
460 return is_swap_pte(pte
) && is_pte_marker_entry(pte_to_swp_entry(pte
));
463 static inline pte_t
make_pte_marker(pte_marker marker
)
465 return swp_entry_to_pte(make_pte_marker_entry(marker
));
468 static inline swp_entry_t
make_poisoned_swp_entry(void)
470 return make_pte_marker_entry(PTE_MARKER_POISONED
);
473 static inline int is_poisoned_swp_entry(swp_entry_t entry
)
475 return is_pte_marker_entry(entry
) &&
476 (pte_marker_get(entry
) & PTE_MARKER_POISONED
);
480 static inline swp_entry_t
make_guard_swp_entry(void)
482 return make_pte_marker_entry(PTE_MARKER_GUARD
);
485 static inline int is_guard_swp_entry(swp_entry_t entry
)
487 return is_pte_marker_entry(entry
) &&
488 (pte_marker_get(entry
) & PTE_MARKER_GUARD
);
492 * This is a special version to check pte_none() just to cover the case when
493 * the pte is a pte marker. It existed because in many cases the pte marker
494 * should be seen as a none pte; it's just that we have stored some information
495 * onto the none pte so it becomes not-none any more.
497 * It should be used when the pte is file-backed, ram-based and backing
498 * userspace pages, like shmem. It is not needed upon pgtables that do not
499 * support pte markers at all. For example, it's not needed on anonymous
500 * memory, kernel-only memory (including when the system is during-boot),
501 * non-ram based generic file-system. It's fine to be used even there, but the
502 * extra pte marker check will be pure overhead.
504 static inline int pte_none_mostly(pte_t pte
)
506 return pte_none(pte
) || is_pte_marker(pte
);
509 static inline struct page
*pfn_swap_entry_to_page(swp_entry_t entry
)
511 struct page
*p
= pfn_to_page(swp_offset_pfn(entry
));
514 * Any use of migration entries may only occur while the
515 * corresponding page is locked
517 BUG_ON(is_migration_entry(entry
) && !PageLocked(p
));
522 static inline struct folio
*pfn_swap_entry_folio(swp_entry_t entry
)
524 struct folio
*folio
= pfn_folio(swp_offset_pfn(entry
));
527 * Any use of migration entries may only occur while the
528 * corresponding folio is locked
530 BUG_ON(is_migration_entry(entry
) && !folio_test_locked(folio
));
536 * A pfn swap entry is a special type of swap entry that always has a pfn stored
537 * in the swap offset. They can either be used to represent unaddressable device
538 * memory, to restrict access to a page undergoing migration or to represent a
539 * pfn which has been hwpoisoned and unmapped.
541 static inline bool is_pfn_swap_entry(swp_entry_t entry
)
543 /* Make sure the swp offset can always store the needed fields */
544 BUILD_BUG_ON(SWP_TYPE_SHIFT
< SWP_PFN_BITS
);
546 return is_migration_entry(entry
) || is_device_private_entry(entry
) ||
547 is_device_exclusive_entry(entry
) || is_hwpoison_entry(entry
);
550 struct page_vma_mapped_walk
;
552 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
553 extern int set_pmd_migration_entry(struct page_vma_mapped_walk
*pvmw
,
556 extern void remove_migration_pmd(struct page_vma_mapped_walk
*pvmw
,
559 extern void pmd_migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
);
561 static inline swp_entry_t
pmd_to_swp_entry(pmd_t pmd
)
563 swp_entry_t arch_entry
;
565 if (pmd_swp_soft_dirty(pmd
))
566 pmd
= pmd_swp_clear_soft_dirty(pmd
);
567 if (pmd_swp_uffd_wp(pmd
))
568 pmd
= pmd_swp_clear_uffd_wp(pmd
);
569 arch_entry
= __pmd_to_swp_entry(pmd
);
570 return swp_entry(__swp_type(arch_entry
), __swp_offset(arch_entry
));
573 static inline pmd_t
swp_entry_to_pmd(swp_entry_t entry
)
575 swp_entry_t arch_entry
;
577 arch_entry
= __swp_entry(swp_type(entry
), swp_offset(entry
));
578 return __swp_entry_to_pmd(arch_entry
);
581 static inline int is_pmd_migration_entry(pmd_t pmd
)
583 return is_swap_pmd(pmd
) && is_migration_entry(pmd_to_swp_entry(pmd
));
585 #else /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
586 static inline int set_pmd_migration_entry(struct page_vma_mapped_walk
*pvmw
,
592 static inline void remove_migration_pmd(struct page_vma_mapped_walk
*pvmw
,
598 static inline void pmd_migration_entry_wait(struct mm_struct
*m
, pmd_t
*p
) { }
600 static inline swp_entry_t
pmd_to_swp_entry(pmd_t pmd
)
602 return swp_entry(0, 0);
605 static inline pmd_t
swp_entry_to_pmd(swp_entry_t entry
)
610 static inline int is_pmd_migration_entry(pmd_t pmd
)
614 #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
616 static inline int non_swap_entry(swp_entry_t entry
)
618 return swp_type(entry
) >= MAX_SWAPFILES
;
621 #endif /* CONFIG_MMU */
622 #endif /* _LINUX_SWAPOPS_H */