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 * swapcache pages are stored in the swapper_space radix tree. We want to
13 * get good packing density in that tree, so the index should be dense in
16 * We arrange the `type' and `offset' fields so that `type' is at the six
17 * high-order bits of the swp_entry_t and `offset' is right-aligned in the
18 * remaining bits. Although `type' itself needs only five bits, we allow for
19 * shmem/tmpfs to shift it all up a further one bit: see swp_to_radix_entry().
21 * swp_entry_t's are *never* stored anywhere in their arch-dependent format.
23 #define SWP_TYPE_SHIFT (BITS_PER_XA_VALUE - MAX_SWAPFILES_SHIFT)
24 #define SWP_OFFSET_MASK ((1UL << SWP_TYPE_SHIFT) - 1)
26 /* Clear all flags but only keep swp_entry_t related information */
27 static inline pte_t pte_swp_clear_flags(pte_t pte)
29 if (pte_swp_exclusive(pte))
30 pte = pte_swp_clear_exclusive(pte);
31 if (pte_swp_soft_dirty(pte))
32 pte = pte_swp_clear_soft_dirty(pte);
33 if (pte_swp_uffd_wp(pte))
34 pte = pte_swp_clear_uffd_wp(pte);
39 * Store a type+offset into a swp_entry_t in an arch-independent format
41 static inline swp_entry_t swp_entry(unsigned long type, pgoff_t offset)
45 ret.val = (type << SWP_TYPE_SHIFT) | (offset & SWP_OFFSET_MASK);
50 * Extract the `type' field from a swp_entry_t. The swp_entry_t is in
51 * arch-independent format
53 static inline unsigned swp_type(swp_entry_t entry)
55 return (entry.val >> SWP_TYPE_SHIFT);
59 * Extract the `offset' field from a swp_entry_t. The swp_entry_t is in
60 * arch-independent format
62 static inline pgoff_t swp_offset(swp_entry_t entry)
64 return entry.val & SWP_OFFSET_MASK;
67 /* check whether a pte points to a swap entry */
68 static inline int is_swap_pte(pte_t pte)
70 return !pte_none(pte) && !pte_present(pte);
74 * Convert the arch-dependent pte representation of a swp_entry_t into an
75 * arch-independent swp_entry_t.
77 static inline swp_entry_t pte_to_swp_entry(pte_t pte)
79 swp_entry_t arch_entry;
81 pte = pte_swp_clear_flags(pte);
82 arch_entry = __pte_to_swp_entry(pte);
83 return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
87 * Convert the arch-independent representation of a swp_entry_t into the
88 * arch-dependent pte representation.
90 static inline pte_t swp_entry_to_pte(swp_entry_t entry)
92 swp_entry_t arch_entry;
94 arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
95 return __swp_entry_to_pte(arch_entry);
98 static inline swp_entry_t radix_to_swp_entry(void *arg)
102 entry.val = xa_to_value(arg);
106 static inline void *swp_to_radix_entry(swp_entry_t entry)
108 return xa_mk_value(entry.val);
111 static inline swp_entry_t make_swapin_error_entry(struct page *page)
113 return swp_entry(SWP_SWAPIN_ERROR, page_to_pfn(page));
116 static inline int is_swapin_error_entry(swp_entry_t entry)
118 return swp_type(entry) == SWP_SWAPIN_ERROR;
121 #if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
122 static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
124 return swp_entry(SWP_DEVICE_READ, offset);
127 static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
129 return swp_entry(SWP_DEVICE_WRITE, offset);
132 static inline bool is_device_private_entry(swp_entry_t entry)
134 int type = swp_type(entry);
135 return type == SWP_DEVICE_READ || type == SWP_DEVICE_WRITE;
138 static inline bool is_writable_device_private_entry(swp_entry_t entry)
140 return unlikely(swp_type(entry) == SWP_DEVICE_WRITE);
143 static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
145 return swp_entry(SWP_DEVICE_EXCLUSIVE_READ, offset);
148 static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
150 return swp_entry(SWP_DEVICE_EXCLUSIVE_WRITE, offset);
153 static inline bool is_device_exclusive_entry(swp_entry_t entry)
155 return swp_type(entry) == SWP_DEVICE_EXCLUSIVE_READ ||
156 swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE;
159 static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
161 return unlikely(swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE);
163 #else /* CONFIG_DEVICE_PRIVATE */
164 static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
166 return swp_entry(0, 0);
169 static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
171 return swp_entry(0, 0);
174 static inline bool is_device_private_entry(swp_entry_t entry)
179 static inline bool is_writable_device_private_entry(swp_entry_t entry)
184 static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
186 return swp_entry(0, 0);
189 static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
191 return swp_entry(0, 0);
194 static inline bool is_device_exclusive_entry(swp_entry_t entry)
199 static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
203 #endif /* CONFIG_DEVICE_PRIVATE */
205 #ifdef CONFIG_MIGRATION
206 static inline int is_migration_entry(swp_entry_t entry)
208 return unlikely(swp_type(entry) == SWP_MIGRATION_READ ||
209 swp_type(entry) == SWP_MIGRATION_READ_EXCLUSIVE ||
210 swp_type(entry) == SWP_MIGRATION_WRITE);
213 static inline int is_writable_migration_entry(swp_entry_t entry)
215 return unlikely(swp_type(entry) == SWP_MIGRATION_WRITE);
218 static inline int is_readable_migration_entry(swp_entry_t entry)
220 return unlikely(swp_type(entry) == SWP_MIGRATION_READ);
223 static inline int is_readable_exclusive_migration_entry(swp_entry_t entry)
225 return unlikely(swp_type(entry) == SWP_MIGRATION_READ_EXCLUSIVE);
228 static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
230 return swp_entry(SWP_MIGRATION_READ, offset);
233 static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
235 return swp_entry(SWP_MIGRATION_READ_EXCLUSIVE, offset);
238 static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
240 return swp_entry(SWP_MIGRATION_WRITE, offset);
243 extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
245 extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
246 unsigned long address);
247 extern void migration_entry_wait_huge(struct vm_area_struct *vma,
248 struct mm_struct *mm, pte_t *pte);
250 static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
252 return swp_entry(0, 0);
255 static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
257 return swp_entry(0, 0);
260 static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
262 return swp_entry(0, 0);
265 static inline int is_migration_entry(swp_entry_t swp)
270 static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
272 static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
273 unsigned long address) { }
274 static inline void migration_entry_wait_huge(struct vm_area_struct *vma,
275 struct mm_struct *mm, pte_t *pte) { }
276 static inline int is_writable_migration_entry(swp_entry_t entry)
280 static inline int is_readable_migration_entry(swp_entry_t entry)
287 typedef unsigned long pte_marker;
289 #define PTE_MARKER_UFFD_WP BIT(0)
290 #define PTE_MARKER_MASK (PTE_MARKER_UFFD_WP)
292 #ifdef CONFIG_PTE_MARKER
294 static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
296 return swp_entry(SWP_PTE_MARKER, marker);
299 static inline bool is_pte_marker_entry(swp_entry_t entry)
301 return swp_type(entry) == SWP_PTE_MARKER;
304 static inline pte_marker pte_marker_get(swp_entry_t entry)
306 return swp_offset(entry) & PTE_MARKER_MASK;
309 static inline bool is_pte_marker(pte_t pte)
311 return is_swap_pte(pte) && is_pte_marker_entry(pte_to_swp_entry(pte));
314 #else /* CONFIG_PTE_MARKER */
316 static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
318 /* This should never be called if !CONFIG_PTE_MARKER */
320 return swp_entry(0, 0);
323 static inline bool is_pte_marker_entry(swp_entry_t entry)
328 static inline pte_marker pte_marker_get(swp_entry_t entry)
333 static inline bool is_pte_marker(pte_t pte)
338 #endif /* CONFIG_PTE_MARKER */
340 static inline pte_t make_pte_marker(pte_marker marker)
342 return swp_entry_to_pte(make_pte_marker_entry(marker));
346 * This is a special version to check pte_none() just to cover the case when
347 * the pte is a pte marker. It existed because in many cases the pte marker
348 * should be seen as a none pte; it's just that we have stored some information
349 * onto the none pte so it becomes not-none any more.
351 * It should be used when the pte is file-backed, ram-based and backing
352 * userspace pages, like shmem. It is not needed upon pgtables that do not
353 * support pte markers at all. For example, it's not needed on anonymous
354 * memory, kernel-only memory (including when the system is during-boot),
355 * non-ram based generic file-system. It's fine to be used even there, but the
356 * extra pte marker check will be pure overhead.
358 * For systems configured with !CONFIG_PTE_MARKER this will be automatically
359 * optimized to pte_none().
361 static inline int pte_none_mostly(pte_t pte)
363 return pte_none(pte) || is_pte_marker(pte);
366 static inline struct page *pfn_swap_entry_to_page(swp_entry_t entry)
368 struct page *p = pfn_to_page(swp_offset(entry));
371 * Any use of migration entries may only occur while the
372 * corresponding page is locked
374 BUG_ON(is_migration_entry(entry) && !PageLocked(p));
380 * A pfn swap entry is a special type of swap entry that always has a pfn stored
381 * in the swap offset. They are used to represent unaddressable device memory
382 * and to restrict access to a page undergoing migration.
384 static inline bool is_pfn_swap_entry(swp_entry_t entry)
386 return is_migration_entry(entry) || is_device_private_entry(entry) ||
387 is_device_exclusive_entry(entry);
390 struct page_vma_mapped_walk;
392 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
393 extern int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
396 extern void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
399 extern void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd);
401 static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
403 swp_entry_t arch_entry;
405 if (pmd_swp_soft_dirty(pmd))
406 pmd = pmd_swp_clear_soft_dirty(pmd);
407 if (pmd_swp_uffd_wp(pmd))
408 pmd = pmd_swp_clear_uffd_wp(pmd);
409 arch_entry = __pmd_to_swp_entry(pmd);
410 return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
413 static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
415 swp_entry_t arch_entry;
417 arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
418 return __swp_entry_to_pmd(arch_entry);
421 static inline int is_pmd_migration_entry(pmd_t pmd)
423 return is_swap_pmd(pmd) && is_migration_entry(pmd_to_swp_entry(pmd));
426 static inline int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
432 static inline void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
438 static inline void pmd_migration_entry_wait(struct mm_struct *m, pmd_t *p) { }
440 static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
442 return swp_entry(0, 0);
445 static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
450 static inline int is_pmd_migration_entry(pmd_t pmd)
456 #ifdef CONFIG_MEMORY_FAILURE
458 extern atomic_long_t num_poisoned_pages __read_mostly;
461 * Support for hardware poisoned pages
463 static inline swp_entry_t make_hwpoison_entry(struct page *page)
465 BUG_ON(!PageLocked(page));
466 return swp_entry(SWP_HWPOISON, page_to_pfn(page));
469 static inline int is_hwpoison_entry(swp_entry_t entry)
471 return swp_type(entry) == SWP_HWPOISON;
474 static inline unsigned long hwpoison_entry_to_pfn(swp_entry_t entry)
476 return swp_offset(entry);
479 static inline void num_poisoned_pages_inc(void)
481 atomic_long_inc(&num_poisoned_pages);
484 static inline void num_poisoned_pages_dec(void)
486 atomic_long_dec(&num_poisoned_pages);
491 static inline swp_entry_t make_hwpoison_entry(struct page *page)
493 return swp_entry(0, 0);
496 static inline int is_hwpoison_entry(swp_entry_t swp)
501 static inline void num_poisoned_pages_inc(void)
506 static inline int non_swap_entry(swp_entry_t entry)
508 return swp_type(entry) >= MAX_SWAPFILES;
511 #endif /* CONFIG_MMU */
512 #endif /* _LINUX_SWAPOPS_H */