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 static inline swp_entry_t make_swapin_error_entry(struct page *page)
169 return swp_entry(SWP_SWAPIN_ERROR, page_to_pfn(page));
172 static inline int is_swapin_error_entry(swp_entry_t entry)
174 return swp_type(entry) == SWP_SWAPIN_ERROR;
177 #if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
178 static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
180 return swp_entry(SWP_DEVICE_READ, offset);
183 static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
185 return swp_entry(SWP_DEVICE_WRITE, offset);
188 static inline bool is_device_private_entry(swp_entry_t entry)
190 int type = swp_type(entry);
191 return type == SWP_DEVICE_READ || type == SWP_DEVICE_WRITE;
194 static inline bool is_writable_device_private_entry(swp_entry_t entry)
196 return unlikely(swp_type(entry) == SWP_DEVICE_WRITE);
199 static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
201 return swp_entry(SWP_DEVICE_EXCLUSIVE_READ, offset);
204 static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
206 return swp_entry(SWP_DEVICE_EXCLUSIVE_WRITE, offset);
209 static inline bool is_device_exclusive_entry(swp_entry_t entry)
211 return swp_type(entry) == SWP_DEVICE_EXCLUSIVE_READ ||
212 swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE;
215 static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
217 return unlikely(swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE);
219 #else /* CONFIG_DEVICE_PRIVATE */
220 static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
222 return swp_entry(0, 0);
225 static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
227 return swp_entry(0, 0);
230 static inline bool is_device_private_entry(swp_entry_t entry)
235 static inline bool is_writable_device_private_entry(swp_entry_t entry)
240 static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
242 return swp_entry(0, 0);
245 static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
247 return swp_entry(0, 0);
250 static inline bool is_device_exclusive_entry(swp_entry_t entry)
255 static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
259 #endif /* CONFIG_DEVICE_PRIVATE */
261 #ifdef CONFIG_MIGRATION
262 static inline int is_migration_entry(swp_entry_t entry)
264 return unlikely(swp_type(entry) == SWP_MIGRATION_READ ||
265 swp_type(entry) == SWP_MIGRATION_READ_EXCLUSIVE ||
266 swp_type(entry) == SWP_MIGRATION_WRITE);
269 static inline int is_writable_migration_entry(swp_entry_t entry)
271 return unlikely(swp_type(entry) == SWP_MIGRATION_WRITE);
274 static inline int is_readable_migration_entry(swp_entry_t entry)
276 return unlikely(swp_type(entry) == SWP_MIGRATION_READ);
279 static inline int is_readable_exclusive_migration_entry(swp_entry_t entry)
281 return unlikely(swp_type(entry) == SWP_MIGRATION_READ_EXCLUSIVE);
284 static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
286 return swp_entry(SWP_MIGRATION_READ, offset);
289 static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
291 return swp_entry(SWP_MIGRATION_READ_EXCLUSIVE, offset);
294 static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
296 return swp_entry(SWP_MIGRATION_WRITE, offset);
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.
304 static inline bool migration_entry_supports_ad(void)
307 return swap_migration_ad_supported;
308 #else /* CONFIG_SWAP */
310 #endif /* CONFIG_SWAP */
313 static inline swp_entry_t make_migration_entry_young(swp_entry_t entry)
315 if (migration_entry_supports_ad())
316 return swp_entry(swp_type(entry),
317 swp_offset(entry) | SWP_MIG_YOUNG);
321 static inline bool is_migration_entry_young(swp_entry_t entry)
323 if (migration_entry_supports_ad())
324 return swp_offset(entry) & SWP_MIG_YOUNG;
325 /* Keep the old behavior of aging page after migration */
329 static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry)
331 if (migration_entry_supports_ad())
332 return swp_entry(swp_type(entry),
333 swp_offset(entry) | SWP_MIG_DIRTY);
337 static inline bool is_migration_entry_dirty(swp_entry_t entry)
339 if (migration_entry_supports_ad())
340 return swp_offset(entry) & SWP_MIG_DIRTY;
341 /* Keep the old behavior of clean page after migration */
345 extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
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)
356 return swp_entry(0, 0);
359 static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
361 return swp_entry(0, 0);
364 static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
366 return swp_entry(0, 0);
369 static inline int is_migration_entry(swp_entry_t swp)
374 static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
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)
386 static inline int is_readable_migration_entry(swp_entry_t entry)
391 static inline swp_entry_t make_migration_entry_young(swp_entry_t entry)
396 static inline bool is_migration_entry_young(swp_entry_t entry)
401 static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry)
406 static inline bool is_migration_entry_dirty(swp_entry_t entry)
410 #endif /* CONFIG_MIGRATION */
412 typedef unsigned long pte_marker;
414 #define PTE_MARKER_UFFD_WP BIT(0)
415 #define PTE_MARKER_MASK (PTE_MARKER_UFFD_WP)
417 #ifdef CONFIG_PTE_MARKER
419 static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
421 return swp_entry(SWP_PTE_MARKER, marker);
424 static inline bool is_pte_marker_entry(swp_entry_t entry)
426 return swp_type(entry) == SWP_PTE_MARKER;
429 static inline pte_marker pte_marker_get(swp_entry_t entry)
431 return swp_offset(entry) & PTE_MARKER_MASK;
434 static inline bool is_pte_marker(pte_t pte)
436 return is_swap_pte(pte) && is_pte_marker_entry(pte_to_swp_entry(pte));
439 #else /* CONFIG_PTE_MARKER */
441 static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
443 /* This should never be called if !CONFIG_PTE_MARKER */
445 return swp_entry(0, 0);
448 static inline bool is_pte_marker_entry(swp_entry_t entry)
453 static inline pte_marker pte_marker_get(swp_entry_t entry)
458 static inline bool is_pte_marker(pte_t pte)
463 #endif /* CONFIG_PTE_MARKER */
465 static inline pte_t make_pte_marker(pte_marker marker)
467 return swp_entry_to_pte(make_pte_marker_entry(marker));
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.
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.
483 * For systems configured with !CONFIG_PTE_MARKER this will be automatically
484 * optimized to pte_none().
486 static inline int pte_none_mostly(pte_t pte)
488 return pte_none(pte) || is_pte_marker(pte);
491 static inline struct page *pfn_swap_entry_to_page(swp_entry_t entry)
493 struct page *p = pfn_to_page(swp_offset_pfn(entry));
496 * Any use of migration entries may only occur while the
497 * corresponding page is locked
499 BUG_ON(is_migration_entry(entry) && !PageLocked(p));
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.
509 static inline bool is_pfn_swap_entry(swp_entry_t entry)
511 /* Make sure the swp offset can always store the needed fields */
512 BUILD_BUG_ON(SWP_TYPE_SHIFT < SWP_PFN_BITS);
514 return is_migration_entry(entry) || is_device_private_entry(entry) ||
515 is_device_exclusive_entry(entry);
518 struct page_vma_mapped_walk;
520 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
521 extern int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
524 extern void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
527 extern void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd);
529 static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
531 swp_entry_t arch_entry;
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));
541 static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
543 swp_entry_t arch_entry;
545 arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
546 return __swp_entry_to_pmd(arch_entry);
549 static inline int is_pmd_migration_entry(pmd_t pmd)
551 return is_swap_pmd(pmd) && is_migration_entry(pmd_to_swp_entry(pmd));
553 #else /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
554 static inline int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
560 static inline void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
566 static inline void pmd_migration_entry_wait(struct mm_struct *m, pmd_t *p) { }
568 static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
570 return swp_entry(0, 0);
573 static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
578 static inline int is_pmd_migration_entry(pmd_t pmd)
582 #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
584 #ifdef CONFIG_MEMORY_FAILURE
586 extern atomic_long_t num_poisoned_pages __read_mostly;
589 * Support for hardware poisoned pages
591 static inline swp_entry_t make_hwpoison_entry(struct page *page)
593 BUG_ON(!PageLocked(page));
594 return swp_entry(SWP_HWPOISON, page_to_pfn(page));
597 static inline int is_hwpoison_entry(swp_entry_t entry)
599 return swp_type(entry) == SWP_HWPOISON;
602 static inline void num_poisoned_pages_inc(void)
604 atomic_long_inc(&num_poisoned_pages);
607 static inline void num_poisoned_pages_sub(long i)
609 atomic_long_sub(i, &num_poisoned_pages);
612 #else /* CONFIG_MEMORY_FAILURE */
614 static inline swp_entry_t make_hwpoison_entry(struct page *page)
616 return swp_entry(0, 0);
619 static inline int is_hwpoison_entry(swp_entry_t swp)
624 static inline void num_poisoned_pages_inc(void)
628 static inline void num_poisoned_pages_sub(long i)
631 #endif /* CONFIG_MEMORY_FAILURE */
633 static inline int non_swap_entry(swp_entry_t entry)
635 return swp_type(entry) >= MAX_SWAPFILES;
638 #endif /* CONFIG_MMU */
639 #endif /* _LINUX_SWAPOPS_H */