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>
10 * swapcache pages are stored in the swapper_space radix tree. We want to
11 * get good packing density in that tree, so the index should be dense in
14 * We arrange the `type' and `offset' fields so that `type' is at the seven
15 * high-order bits of the swp_entry_t and `offset' is right-aligned in the
16 * remaining bits. Although `type' itself needs only five bits, we allow for
17 * shmem/tmpfs to shift it all up a further two bits: see swp_to_radix_entry().
19 * swp_entry_t's are *never* stored anywhere in their arch-dependent format.
21 #define SWP_TYPE_SHIFT (BITS_PER_XA_VALUE - MAX_SWAPFILES_SHIFT)
22 #define SWP_OFFSET_MASK ((1UL << SWP_TYPE_SHIFT) - 1)
25 * Store a type+offset into a swp_entry_t in an arch-independent format
27 static inline swp_entry_t
swp_entry(unsigned long type
, pgoff_t offset
)
31 ret
.val
= (type
<< SWP_TYPE_SHIFT
) | (offset
& SWP_OFFSET_MASK
);
36 * Extract the `type' field from a swp_entry_t. The swp_entry_t is in
37 * arch-independent format
39 static inline unsigned swp_type(swp_entry_t entry
)
41 return (entry
.val
>> SWP_TYPE_SHIFT
);
45 * Extract the `offset' field from a swp_entry_t. The swp_entry_t is in
46 * arch-independent format
48 static inline pgoff_t
swp_offset(swp_entry_t entry
)
50 return entry
.val
& SWP_OFFSET_MASK
;
54 /* check whether a pte points to a swap entry */
55 static inline int is_swap_pte(pte_t pte
)
57 return !pte_none(pte
) && !pte_present(pte
);
62 * Convert the arch-dependent pte representation of a swp_entry_t into an
63 * arch-independent swp_entry_t.
65 static inline swp_entry_t
pte_to_swp_entry(pte_t pte
)
67 swp_entry_t arch_entry
;
69 if (pte_swp_soft_dirty(pte
))
70 pte
= pte_swp_clear_soft_dirty(pte
);
71 arch_entry
= __pte_to_swp_entry(pte
);
72 return swp_entry(__swp_type(arch_entry
), __swp_offset(arch_entry
));
76 * Convert the arch-independent representation of a swp_entry_t into the
77 * arch-dependent pte representation.
79 static inline pte_t
swp_entry_to_pte(swp_entry_t entry
)
81 swp_entry_t arch_entry
;
83 arch_entry
= __swp_entry(swp_type(entry
), swp_offset(entry
));
84 return __swp_entry_to_pte(arch_entry
);
87 static inline swp_entry_t
radix_to_swp_entry(void *arg
)
91 entry
.val
= xa_to_value(arg
);
95 static inline void *swp_to_radix_entry(swp_entry_t entry
)
97 return xa_mk_value(entry
.val
);
100 #if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
101 static inline swp_entry_t
make_device_private_entry(struct page
*page
, bool write
)
103 return swp_entry(write
? SWP_DEVICE_WRITE
: SWP_DEVICE_READ
,
107 static inline bool is_device_private_entry(swp_entry_t entry
)
109 int type
= swp_type(entry
);
110 return type
== SWP_DEVICE_READ
|| type
== SWP_DEVICE_WRITE
;
113 static inline void make_device_private_entry_read(swp_entry_t
*entry
)
115 *entry
= swp_entry(SWP_DEVICE_READ
, swp_offset(*entry
));
118 static inline bool is_write_device_private_entry(swp_entry_t entry
)
120 return unlikely(swp_type(entry
) == SWP_DEVICE_WRITE
);
123 static inline unsigned long device_private_entry_to_pfn(swp_entry_t entry
)
125 return swp_offset(entry
);
128 static inline struct page
*device_private_entry_to_page(swp_entry_t entry
)
130 return pfn_to_page(swp_offset(entry
));
133 vm_fault_t
device_private_entry_fault(struct vm_area_struct
*vma
,
138 #else /* CONFIG_DEVICE_PRIVATE */
139 static inline swp_entry_t
make_device_private_entry(struct page
*page
, bool write
)
141 return swp_entry(0, 0);
144 static inline void make_device_private_entry_read(swp_entry_t
*entry
)
148 static inline bool is_device_private_entry(swp_entry_t entry
)
153 static inline bool is_write_device_private_entry(swp_entry_t entry
)
158 static inline unsigned long device_private_entry_to_pfn(swp_entry_t entry
)
163 static inline struct page
*device_private_entry_to_page(swp_entry_t entry
)
168 static inline vm_fault_t
device_private_entry_fault(struct vm_area_struct
*vma
,
174 return VM_FAULT_SIGBUS
;
176 #endif /* CONFIG_DEVICE_PRIVATE */
178 #ifdef CONFIG_MIGRATION
179 static inline swp_entry_t
make_migration_entry(struct page
*page
, int write
)
181 BUG_ON(!PageLocked(compound_head(page
)));
183 return swp_entry(write
? SWP_MIGRATION_WRITE
: SWP_MIGRATION_READ
,
187 static inline int is_migration_entry(swp_entry_t entry
)
189 return unlikely(swp_type(entry
) == SWP_MIGRATION_READ
||
190 swp_type(entry
) == SWP_MIGRATION_WRITE
);
193 static inline int is_write_migration_entry(swp_entry_t entry
)
195 return unlikely(swp_type(entry
) == SWP_MIGRATION_WRITE
);
198 static inline unsigned long migration_entry_to_pfn(swp_entry_t entry
)
200 return swp_offset(entry
);
203 static inline struct page
*migration_entry_to_page(swp_entry_t entry
)
205 struct page
*p
= pfn_to_page(swp_offset(entry
));
207 * Any use of migration entries may only occur while the
208 * corresponding page is locked
210 BUG_ON(!PageLocked(compound_head(p
)));
214 static inline void make_migration_entry_read(swp_entry_t
*entry
)
216 *entry
= swp_entry(SWP_MIGRATION_READ
, swp_offset(*entry
));
219 extern void __migration_entry_wait(struct mm_struct
*mm
, pte_t
*ptep
,
221 extern void migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
,
222 unsigned long address
);
223 extern void migration_entry_wait_huge(struct vm_area_struct
*vma
,
224 struct mm_struct
*mm
, pte_t
*pte
);
227 #define make_migration_entry(page, write) swp_entry(0, 0)
228 static inline int is_migration_entry(swp_entry_t swp
)
233 static inline unsigned long migration_entry_to_pfn(swp_entry_t entry
)
238 static inline struct page
*migration_entry_to_page(swp_entry_t entry
)
243 static inline void make_migration_entry_read(swp_entry_t
*entryp
) { }
244 static inline void __migration_entry_wait(struct mm_struct
*mm
, pte_t
*ptep
,
246 static inline void migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
,
247 unsigned long address
) { }
248 static inline void migration_entry_wait_huge(struct vm_area_struct
*vma
,
249 struct mm_struct
*mm
, pte_t
*pte
) { }
250 static inline int is_write_migration_entry(swp_entry_t entry
)
257 struct page_vma_mapped_walk
;
259 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
260 extern void set_pmd_migration_entry(struct page_vma_mapped_walk
*pvmw
,
263 extern void remove_migration_pmd(struct page_vma_mapped_walk
*pvmw
,
266 extern void pmd_migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
);
268 static inline swp_entry_t
pmd_to_swp_entry(pmd_t pmd
)
270 swp_entry_t arch_entry
;
272 if (pmd_swp_soft_dirty(pmd
))
273 pmd
= pmd_swp_clear_soft_dirty(pmd
);
274 arch_entry
= __pmd_to_swp_entry(pmd
);
275 return swp_entry(__swp_type(arch_entry
), __swp_offset(arch_entry
));
278 static inline pmd_t
swp_entry_to_pmd(swp_entry_t entry
)
280 swp_entry_t arch_entry
;
282 arch_entry
= __swp_entry(swp_type(entry
), swp_offset(entry
));
283 return __swp_entry_to_pmd(arch_entry
);
286 static inline int is_pmd_migration_entry(pmd_t pmd
)
288 return !pmd_present(pmd
) && is_migration_entry(pmd_to_swp_entry(pmd
));
291 static inline void set_pmd_migration_entry(struct page_vma_mapped_walk
*pvmw
,
297 static inline void remove_migration_pmd(struct page_vma_mapped_walk
*pvmw
,
303 static inline void pmd_migration_entry_wait(struct mm_struct
*m
, pmd_t
*p
) { }
305 static inline swp_entry_t
pmd_to_swp_entry(pmd_t pmd
)
307 return swp_entry(0, 0);
310 static inline pmd_t
swp_entry_to_pmd(swp_entry_t entry
)
315 static inline int is_pmd_migration_entry(pmd_t pmd
)
321 #ifdef CONFIG_MEMORY_FAILURE
323 extern atomic_long_t num_poisoned_pages __read_mostly
;
326 * Support for hardware poisoned pages
328 static inline swp_entry_t
make_hwpoison_entry(struct page
*page
)
330 BUG_ON(!PageLocked(page
));
331 return swp_entry(SWP_HWPOISON
, page_to_pfn(page
));
334 static inline int is_hwpoison_entry(swp_entry_t entry
)
336 return swp_type(entry
) == SWP_HWPOISON
;
339 static inline void num_poisoned_pages_inc(void)
341 atomic_long_inc(&num_poisoned_pages
);
344 static inline void num_poisoned_pages_dec(void)
346 atomic_long_dec(&num_poisoned_pages
);
351 static inline swp_entry_t
make_hwpoison_entry(struct page
*page
)
353 return swp_entry(0, 0);
356 static inline int is_hwpoison_entry(swp_entry_t swp
)
361 static inline void num_poisoned_pages_inc(void)
366 #if defined(CONFIG_MEMORY_FAILURE) || defined(CONFIG_MIGRATION)
367 static inline int non_swap_entry(swp_entry_t entry
)
369 return swp_type(entry
) >= MAX_SWAPFILES
;
372 static inline int non_swap_entry(swp_entry_t entry
)
378 #endif /* _LINUX_SWAPOPS_H */