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 #ifdef CONFIG_HUGETLB_PAGE
248 extern void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl);
249 extern void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *pte);
252 static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
254 return swp_entry(0, 0);
257 static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
259 return swp_entry(0, 0);
262 static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
264 return swp_entry(0, 0);
267 static inline int is_migration_entry(swp_entry_t swp)
272 static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
274 static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
275 unsigned long address) { }
276 #ifdef CONFIG_HUGETLB_PAGE
277 static inline void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl) { }
278 static inline void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *pte) { }
280 static inline int is_writable_migration_entry(swp_entry_t entry)
284 static inline int is_readable_migration_entry(swp_entry_t entry)
291 typedef unsigned long pte_marker;
293 #define PTE_MARKER_UFFD_WP BIT(0)
294 #define PTE_MARKER_MASK (PTE_MARKER_UFFD_WP)
296 #ifdef CONFIG_PTE_MARKER
298 static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
300 return swp_entry(SWP_PTE_MARKER, marker);
303 static inline bool is_pte_marker_entry(swp_entry_t entry)
305 return swp_type(entry) == SWP_PTE_MARKER;
308 static inline pte_marker pte_marker_get(swp_entry_t entry)
310 return swp_offset(entry) & PTE_MARKER_MASK;
313 static inline bool is_pte_marker(pte_t pte)
315 return is_swap_pte(pte) && is_pte_marker_entry(pte_to_swp_entry(pte));
318 #else /* CONFIG_PTE_MARKER */
320 static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
322 /* This should never be called if !CONFIG_PTE_MARKER */
324 return swp_entry(0, 0);
327 static inline bool is_pte_marker_entry(swp_entry_t entry)
332 static inline pte_marker pte_marker_get(swp_entry_t entry)
337 static inline bool is_pte_marker(pte_t pte)
342 #endif /* CONFIG_PTE_MARKER */
344 static inline pte_t make_pte_marker(pte_marker marker)
346 return swp_entry_to_pte(make_pte_marker_entry(marker));
350 * This is a special version to check pte_none() just to cover the case when
351 * the pte is a pte marker. It existed because in many cases the pte marker
352 * should be seen as a none pte; it's just that we have stored some information
353 * onto the none pte so it becomes not-none any more.
355 * It should be used when the pte is file-backed, ram-based and backing
356 * userspace pages, like shmem. It is not needed upon pgtables that do not
357 * support pte markers at all. For example, it's not needed on anonymous
358 * memory, kernel-only memory (including when the system is during-boot),
359 * non-ram based generic file-system. It's fine to be used even there, but the
360 * extra pte marker check will be pure overhead.
362 * For systems configured with !CONFIG_PTE_MARKER this will be automatically
363 * optimized to pte_none().
365 static inline int pte_none_mostly(pte_t pte)
367 return pte_none(pte) || is_pte_marker(pte);
370 static inline struct page *pfn_swap_entry_to_page(swp_entry_t entry)
372 struct page *p = pfn_to_page(swp_offset(entry));
375 * Any use of migration entries may only occur while the
376 * corresponding page is locked
378 BUG_ON(is_migration_entry(entry) && !PageLocked(p));
384 * A pfn swap entry is a special type of swap entry that always has a pfn stored
385 * in the swap offset. They are used to represent unaddressable device memory
386 * and to restrict access to a page undergoing migration.
388 static inline bool is_pfn_swap_entry(swp_entry_t entry)
390 return is_migration_entry(entry) || is_device_private_entry(entry) ||
391 is_device_exclusive_entry(entry);
394 struct page_vma_mapped_walk;
396 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
397 extern int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
400 extern void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
403 extern void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd);
405 static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
407 swp_entry_t arch_entry;
409 if (pmd_swp_soft_dirty(pmd))
410 pmd = pmd_swp_clear_soft_dirty(pmd);
411 if (pmd_swp_uffd_wp(pmd))
412 pmd = pmd_swp_clear_uffd_wp(pmd);
413 arch_entry = __pmd_to_swp_entry(pmd);
414 return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
417 static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
419 swp_entry_t arch_entry;
421 arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
422 return __swp_entry_to_pmd(arch_entry);
425 static inline int is_pmd_migration_entry(pmd_t pmd)
427 return is_swap_pmd(pmd) && is_migration_entry(pmd_to_swp_entry(pmd));
430 static inline int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
436 static inline void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
442 static inline void pmd_migration_entry_wait(struct mm_struct *m, pmd_t *p) { }
444 static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
446 return swp_entry(0, 0);
449 static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
454 static inline int is_pmd_migration_entry(pmd_t pmd)
460 #ifdef CONFIG_MEMORY_FAILURE
462 extern atomic_long_t num_poisoned_pages __read_mostly;
465 * Support for hardware poisoned pages
467 static inline swp_entry_t make_hwpoison_entry(struct page *page)
469 BUG_ON(!PageLocked(page));
470 return swp_entry(SWP_HWPOISON, page_to_pfn(page));
473 static inline int is_hwpoison_entry(swp_entry_t entry)
475 return swp_type(entry) == SWP_HWPOISON;
478 static inline unsigned long hwpoison_entry_to_pfn(swp_entry_t entry)
480 return swp_offset(entry);
483 static inline void num_poisoned_pages_inc(void)
485 atomic_long_inc(&num_poisoned_pages);
488 static inline void num_poisoned_pages_dec(void)
490 atomic_long_dec(&num_poisoned_pages);
493 static inline void num_poisoned_pages_sub(long i)
495 atomic_long_sub(i, &num_poisoned_pages);
500 static inline swp_entry_t make_hwpoison_entry(struct page *page)
502 return swp_entry(0, 0);
505 static inline int is_hwpoison_entry(swp_entry_t swp)
510 static inline void num_poisoned_pages_inc(void)
514 static inline void num_poisoned_pages_sub(long i)
519 static inline int non_swap_entry(swp_entry_t entry)
521 return swp_type(entry) >= MAX_SWAPFILES;
524 #endif /* CONFIG_MMU */
525 #endif /* _LINUX_SWAPOPS_H */