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 (BITS_PER_LONG - PAGE_SHIFT)
39 #endif /* MAX_PHYSMEM_BITS */
40 #define SWP_PFN_MASK (BIT(SWP_PFN_BITS) - 1)
43 * Migration swap entry specific bitfield definitions. Layout:
45 * |----------+--------------------|
46 * | swp_type | swp_offset |
47 * |----------+--------+-+-+-------|
48 * | | resv |D|A| PFN |
49 * |----------+--------+-+-+-------|
51 * @SWP_MIG_YOUNG_BIT: Whether the page used to have young bit set (bit A)
52 * @SWP_MIG_DIRTY_BIT: Whether the page used to have dirty bit set (bit D)
54 * Note: A/D bits will be stored in migration entries iff there're enough
55 * free bits in arch specific swp offset. By default we'll ignore A/D bits
56 * when migrating a page. Please refer to migration_entry_supports_ad()
57 * for more information. If there're more bits besides PFN and A/D bits,
58 * they should be reserved and always be zeros.
60 #define SWP_MIG_YOUNG_BIT (SWP_PFN_BITS)
61 #define SWP_MIG_DIRTY_BIT (SWP_PFN_BITS + 1)
62 #define SWP_MIG_TOTAL_BITS (SWP_PFN_BITS + 2)
64 #define SWP_MIG_YOUNG BIT(SWP_MIG_YOUNG_BIT)
65 #define SWP_MIG_DIRTY BIT(SWP_MIG_DIRTY_BIT)
67 static inline bool is_pfn_swap_entry(swp_entry_t entry);
69 /* Clear all flags but only keep swp_entry_t related information */
70 static inline pte_t pte_swp_clear_flags(pte_t pte)
72 if (pte_swp_exclusive(pte))
73 pte = pte_swp_clear_exclusive(pte);
74 if (pte_swp_soft_dirty(pte))
75 pte = pte_swp_clear_soft_dirty(pte);
76 if (pte_swp_uffd_wp(pte))
77 pte = pte_swp_clear_uffd_wp(pte);
82 * Store a type+offset into a swp_entry_t in an arch-independent format
84 static inline swp_entry_t swp_entry(unsigned long type, pgoff_t offset)
88 ret.val = (type << SWP_TYPE_SHIFT) | (offset & SWP_OFFSET_MASK);
93 * Extract the `type' field from a swp_entry_t. The swp_entry_t is in
94 * arch-independent format
96 static inline unsigned swp_type(swp_entry_t entry)
98 return (entry.val >> SWP_TYPE_SHIFT);
102 * Extract the `offset' field from a swp_entry_t. The swp_entry_t is in
103 * arch-independent format
105 static inline pgoff_t swp_offset(swp_entry_t entry)
107 return entry.val & SWP_OFFSET_MASK;
111 * This should only be called upon a pfn swap entry to get the PFN stored
112 * in the swap entry. Please refers to is_pfn_swap_entry() for definition
115 static inline unsigned long swp_offset_pfn(swp_entry_t entry)
117 VM_BUG_ON(!is_pfn_swap_entry(entry));
118 return swp_offset(entry) & SWP_PFN_MASK;
121 /* check whether a pte points to a swap entry */
122 static inline int is_swap_pte(pte_t pte)
124 return !pte_none(pte) && !pte_present(pte);
128 * Convert the arch-dependent pte representation of a swp_entry_t into an
129 * arch-independent swp_entry_t.
131 static inline swp_entry_t pte_to_swp_entry(pte_t pte)
133 swp_entry_t arch_entry;
135 pte = pte_swp_clear_flags(pte);
136 arch_entry = __pte_to_swp_entry(pte);
137 return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
141 * Convert the arch-independent representation of a swp_entry_t into the
142 * arch-dependent pte representation.
144 static inline pte_t swp_entry_to_pte(swp_entry_t entry)
146 swp_entry_t arch_entry;
148 arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
149 return __swp_entry_to_pte(arch_entry);
152 static inline swp_entry_t radix_to_swp_entry(void *arg)
156 entry.val = xa_to_value(arg);
160 static inline void *swp_to_radix_entry(swp_entry_t entry)
162 return xa_mk_value(entry.val);
165 static inline swp_entry_t make_swapin_error_entry(struct page *page)
167 return swp_entry(SWP_SWAPIN_ERROR, page_to_pfn(page));
170 static inline int is_swapin_error_entry(swp_entry_t entry)
172 return swp_type(entry) == SWP_SWAPIN_ERROR;
175 #if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
176 static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
178 return swp_entry(SWP_DEVICE_READ, offset);
181 static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
183 return swp_entry(SWP_DEVICE_WRITE, offset);
186 static inline bool is_device_private_entry(swp_entry_t entry)
188 int type = swp_type(entry);
189 return type == SWP_DEVICE_READ || type == SWP_DEVICE_WRITE;
192 static inline bool is_writable_device_private_entry(swp_entry_t entry)
194 return unlikely(swp_type(entry) == SWP_DEVICE_WRITE);
197 static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
199 return swp_entry(SWP_DEVICE_EXCLUSIVE_READ, offset);
202 static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
204 return swp_entry(SWP_DEVICE_EXCLUSIVE_WRITE, offset);
207 static inline bool is_device_exclusive_entry(swp_entry_t entry)
209 return swp_type(entry) == SWP_DEVICE_EXCLUSIVE_READ ||
210 swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE;
213 static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
215 return unlikely(swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE);
217 #else /* CONFIG_DEVICE_PRIVATE */
218 static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
220 return swp_entry(0, 0);
223 static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
225 return swp_entry(0, 0);
228 static inline bool is_device_private_entry(swp_entry_t entry)
233 static inline bool is_writable_device_private_entry(swp_entry_t entry)
238 static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
240 return swp_entry(0, 0);
243 static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
245 return swp_entry(0, 0);
248 static inline bool is_device_exclusive_entry(swp_entry_t entry)
253 static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
257 #endif /* CONFIG_DEVICE_PRIVATE */
259 #ifdef CONFIG_MIGRATION
260 static inline int is_migration_entry(swp_entry_t entry)
262 return unlikely(swp_type(entry) == SWP_MIGRATION_READ ||
263 swp_type(entry) == SWP_MIGRATION_READ_EXCLUSIVE ||
264 swp_type(entry) == SWP_MIGRATION_WRITE);
267 static inline int is_writable_migration_entry(swp_entry_t entry)
269 return unlikely(swp_type(entry) == SWP_MIGRATION_WRITE);
272 static inline int is_readable_migration_entry(swp_entry_t entry)
274 return unlikely(swp_type(entry) == SWP_MIGRATION_READ);
277 static inline int is_readable_exclusive_migration_entry(swp_entry_t entry)
279 return unlikely(swp_type(entry) == SWP_MIGRATION_READ_EXCLUSIVE);
282 static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
284 return swp_entry(SWP_MIGRATION_READ, offset);
287 static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
289 return swp_entry(SWP_MIGRATION_READ_EXCLUSIVE, offset);
292 static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
294 return swp_entry(SWP_MIGRATION_WRITE, offset);
298 * Returns whether the host has large enough swap offset field to support
299 * carrying over pgtable A/D bits for page migrations. The result is
300 * pretty much arch specific.
302 static inline bool migration_entry_supports_ad(void)
305 return swap_migration_ad_supported;
306 #else /* CONFIG_SWAP */
308 #endif /* CONFIG_SWAP */
311 static inline swp_entry_t make_migration_entry_young(swp_entry_t entry)
313 if (migration_entry_supports_ad())
314 return swp_entry(swp_type(entry),
315 swp_offset(entry) | SWP_MIG_YOUNG);
319 static inline bool is_migration_entry_young(swp_entry_t entry)
321 if (migration_entry_supports_ad())
322 return swp_offset(entry) & SWP_MIG_YOUNG;
323 /* Keep the old behavior of aging page after migration */
327 static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry)
329 if (migration_entry_supports_ad())
330 return swp_entry(swp_type(entry),
331 swp_offset(entry) | SWP_MIG_DIRTY);
335 static inline bool is_migration_entry_dirty(swp_entry_t entry)
337 if (migration_entry_supports_ad())
338 return swp_offset(entry) & SWP_MIG_DIRTY;
339 /* Keep the old behavior of clean page after migration */
343 extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
345 extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
346 unsigned long address);
347 #ifdef CONFIG_HUGETLB_PAGE
348 extern void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl);
349 extern void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *pte);
350 #endif /* CONFIG_HUGETLB_PAGE */
351 #else /* CONFIG_MIGRATION */
352 static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
354 return swp_entry(0, 0);
357 static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
359 return swp_entry(0, 0);
362 static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
364 return swp_entry(0, 0);
367 static inline int is_migration_entry(swp_entry_t swp)
372 static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
374 static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
375 unsigned long address) { }
376 #ifdef CONFIG_HUGETLB_PAGE
377 static inline void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl) { }
378 static inline void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *pte) { }
379 #endif /* CONFIG_HUGETLB_PAGE */
380 static inline int is_writable_migration_entry(swp_entry_t entry)
384 static inline int is_readable_migration_entry(swp_entry_t entry)
389 static inline swp_entry_t make_migration_entry_young(swp_entry_t entry)
394 static inline bool is_migration_entry_young(swp_entry_t entry)
399 static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry)
404 static inline bool is_migration_entry_dirty(swp_entry_t entry)
408 #endif /* CONFIG_MIGRATION */
410 typedef unsigned long pte_marker;
412 #define PTE_MARKER_UFFD_WP BIT(0)
413 #define PTE_MARKER_MASK (PTE_MARKER_UFFD_WP)
415 #ifdef CONFIG_PTE_MARKER
417 static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
419 return swp_entry(SWP_PTE_MARKER, marker);
422 static inline bool is_pte_marker_entry(swp_entry_t entry)
424 return swp_type(entry) == SWP_PTE_MARKER;
427 static inline pte_marker pte_marker_get(swp_entry_t entry)
429 return swp_offset(entry) & PTE_MARKER_MASK;
432 static inline bool is_pte_marker(pte_t pte)
434 return is_swap_pte(pte) && is_pte_marker_entry(pte_to_swp_entry(pte));
437 #else /* CONFIG_PTE_MARKER */
439 static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
441 /* This should never be called if !CONFIG_PTE_MARKER */
443 return swp_entry(0, 0);
446 static inline bool is_pte_marker_entry(swp_entry_t entry)
451 static inline pte_marker pte_marker_get(swp_entry_t entry)
456 static inline bool is_pte_marker(pte_t pte)
461 #endif /* CONFIG_PTE_MARKER */
463 static inline pte_t make_pte_marker(pte_marker marker)
465 return swp_entry_to_pte(make_pte_marker_entry(marker));
469 * This is a special version to check pte_none() just to cover the case when
470 * the pte is a pte marker. It existed because in many cases the pte marker
471 * should be seen as a none pte; it's just that we have stored some information
472 * onto the none pte so it becomes not-none any more.
474 * It should be used when the pte is file-backed, ram-based and backing
475 * userspace pages, like shmem. It is not needed upon pgtables that do not
476 * support pte markers at all. For example, it's not needed on anonymous
477 * memory, kernel-only memory (including when the system is during-boot),
478 * non-ram based generic file-system. It's fine to be used even there, but the
479 * extra pte marker check will be pure overhead.
481 * For systems configured with !CONFIG_PTE_MARKER this will be automatically
482 * optimized to pte_none().
484 static inline int pte_none_mostly(pte_t pte)
486 return pte_none(pte) || is_pte_marker(pte);
489 static inline struct page *pfn_swap_entry_to_page(swp_entry_t entry)
491 struct page *p = pfn_to_page(swp_offset_pfn(entry));
494 * Any use of migration entries may only occur while the
495 * corresponding page is locked
497 BUG_ON(is_migration_entry(entry) && !PageLocked(p));
503 * A pfn swap entry is a special type of swap entry that always has a pfn stored
504 * in the swap offset. They are used to represent unaddressable device memory
505 * and to restrict access to a page undergoing migration.
507 static inline bool is_pfn_swap_entry(swp_entry_t entry)
509 /* Make sure the swp offset can always store the needed fields */
510 BUILD_BUG_ON(SWP_TYPE_SHIFT < SWP_PFN_BITS);
512 return is_migration_entry(entry) || is_device_private_entry(entry) ||
513 is_device_exclusive_entry(entry);
516 struct page_vma_mapped_walk;
518 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
519 extern int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
522 extern void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
525 extern void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd);
527 static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
529 swp_entry_t arch_entry;
531 if (pmd_swp_soft_dirty(pmd))
532 pmd = pmd_swp_clear_soft_dirty(pmd);
533 if (pmd_swp_uffd_wp(pmd))
534 pmd = pmd_swp_clear_uffd_wp(pmd);
535 arch_entry = __pmd_to_swp_entry(pmd);
536 return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
539 static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
541 swp_entry_t arch_entry;
543 arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
544 return __swp_entry_to_pmd(arch_entry);
547 static inline int is_pmd_migration_entry(pmd_t pmd)
549 return is_swap_pmd(pmd) && is_migration_entry(pmd_to_swp_entry(pmd));
551 #else /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
552 static inline int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
558 static inline void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
564 static inline void pmd_migration_entry_wait(struct mm_struct *m, pmd_t *p) { }
566 static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
568 return swp_entry(0, 0);
571 static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
576 static inline int is_pmd_migration_entry(pmd_t pmd)
580 #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
582 #ifdef CONFIG_MEMORY_FAILURE
584 extern atomic_long_t num_poisoned_pages __read_mostly;
587 * Support for hardware poisoned pages
589 static inline swp_entry_t make_hwpoison_entry(struct page *page)
591 BUG_ON(!PageLocked(page));
592 return swp_entry(SWP_HWPOISON, page_to_pfn(page));
595 static inline int is_hwpoison_entry(swp_entry_t entry)
597 return swp_type(entry) == SWP_HWPOISON;
600 static inline void num_poisoned_pages_inc(void)
602 atomic_long_inc(&num_poisoned_pages);
605 static inline void num_poisoned_pages_sub(long i)
607 atomic_long_sub(i, &num_poisoned_pages);
610 #else /* CONFIG_MEMORY_FAILURE */
612 static inline swp_entry_t make_hwpoison_entry(struct page *page)
614 return swp_entry(0, 0);
617 static inline int is_hwpoison_entry(swp_entry_t swp)
622 static inline void num_poisoned_pages_inc(void)
626 static inline void num_poisoned_pages_sub(long i)
629 #endif /* CONFIG_MEMORY_FAILURE */
631 static inline int non_swap_entry(swp_entry_t entry)
633 return swp_type(entry) >= MAX_SWAPFILES;
636 #endif /* CONFIG_MMU */
637 #endif /* _LINUX_SWAPOPS_H */