xref: /linux/include/linux/swapops.h (revision 49bda4826843be0ef97a162009a29ea3a63f3935)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SWAPOPS_H
3 #define _LINUX_SWAPOPS_H
4 
5 #include <linux/radix-tree.h>
6 #include <linux/bug.h>
7 #include <linux/mm_types.h>
8 #include <linux/swap.h>
9 
10 #ifdef CONFIG_MMU
11 
12 #ifdef CONFIG_SWAP
13 #include <linux/swapfile.h>
14 #endif	/* CONFIG_SWAP */
15 
16 /*
17  * swapcache pages are stored in the swapper_space radix tree.  We want to
18  * get good packing density in that tree, so the index should be dense in
19  * the low-order bits.
20  *
21  * We arrange the `type' and `offset' fields so that `type' is at the six
22  * high-order bits of the swp_entry_t and `offset' is right-aligned in the
23  * remaining bits.  Although `type' itself needs only five bits, we allow for
24  * shmem/tmpfs to shift it all up a further one bit: see swp_to_radix_entry().
25  *
26  * swp_entry_t's are *never* stored anywhere in their arch-dependent format.
27  */
28 #define SWP_TYPE_SHIFT	(BITS_PER_XA_VALUE - MAX_SWAPFILES_SHIFT)
29 #define SWP_OFFSET_MASK	((1UL << SWP_TYPE_SHIFT) - 1)
30 
31 /*
32  * Definitions only for PFN swap entries (see leafeant_has_pfn()).  To
33  * store PFN, we only need SWP_PFN_BITS bits.  Each of the pfn swap entries
34  * can use the extra bits to store other information besides PFN.
35  */
36 #ifdef MAX_PHYSMEM_BITS
37 #define SWP_PFN_BITS		(MAX_PHYSMEM_BITS - PAGE_SHIFT)
38 #else  /* MAX_PHYSMEM_BITS */
39 #define SWP_PFN_BITS		min_t(int, \
40 				      sizeof(phys_addr_t) * 8 - PAGE_SHIFT, \
41 				      SWP_TYPE_SHIFT)
42 #endif	/* MAX_PHYSMEM_BITS */
43 #define SWP_PFN_MASK		(BIT(SWP_PFN_BITS) - 1)
44 
45 /**
46  * Migration swap entry specific bitfield definitions.  Layout:
47  *
48  *   |----------+--------------------|
49  *   | swp_type | swp_offset         |
50  *   |----------+--------+-+-+-------|
51  *   |          | resv   |D|A|  PFN  |
52  *   |----------+--------+-+-+-------|
53  *
54  * @SWP_MIG_YOUNG_BIT: Whether the page used to have young bit set (bit A)
55  * @SWP_MIG_DIRTY_BIT: Whether the page used to have dirty bit set (bit D)
56  *
57  * Note: A/D bits will be stored in migration entries iff there're enough
58  * free bits in arch specific swp offset.  By default we'll ignore A/D bits
59  * when migrating a page.  Please refer to migration_entry_supports_ad()
60  * for more information.  If there're more bits besides PFN and A/D bits,
61  * they should be reserved and always be zeros.
62  */
63 #define SWP_MIG_YOUNG_BIT		(SWP_PFN_BITS)
64 #define SWP_MIG_DIRTY_BIT		(SWP_PFN_BITS + 1)
65 #define SWP_MIG_TOTAL_BITS		(SWP_PFN_BITS + 2)
66 
67 #define SWP_MIG_YOUNG			BIT(SWP_MIG_YOUNG_BIT)
68 #define SWP_MIG_DIRTY			BIT(SWP_MIG_DIRTY_BIT)
69 
70 /* Clear all flags but only keep swp_entry_t related information */
pte_swp_clear_flags(pte_t pte)71 static inline pte_t pte_swp_clear_flags(pte_t pte)
72 {
73 	if (pte_swp_exclusive(pte))
74 		pte = pte_swp_clear_exclusive(pte);
75 	if (pte_swp_soft_dirty(pte))
76 		pte = pte_swp_clear_soft_dirty(pte);
77 	if (pte_swp_uffd(pte))
78 		pte = pte_swp_clear_uffd(pte);
79 	return pte;
80 }
81 
82 /*
83  * Store a type+offset into a swp_entry_t in an arch-independent format
84  */
swp_entry(unsigned long type,pgoff_t offset)85 static inline swp_entry_t swp_entry(unsigned long type, pgoff_t offset)
86 {
87 	swp_entry_t ret;
88 
89 	ret.val = (type << SWP_TYPE_SHIFT) | (offset & SWP_OFFSET_MASK);
90 	return ret;
91 }
92 
93 /*
94  * Extract the `type' field from a swp_entry_t.  The swp_entry_t is in
95  * arch-independent format
96  */
swp_type(swp_entry_t entry)97 static inline unsigned swp_type(swp_entry_t entry)
98 {
99 	return (entry.val >> SWP_TYPE_SHIFT);
100 }
101 
102 /*
103  * Extract the `offset' field from a swp_entry_t.  The swp_entry_t is in
104  * arch-independent format
105  */
swp_offset(swp_entry_t entry)106 static inline pgoff_t swp_offset(swp_entry_t entry)
107 {
108 	return entry.val & SWP_OFFSET_MASK;
109 }
110 
111 /*
112  * Convert the arch-independent representation of a swp_entry_t into the
113  * arch-dependent pte representation.
114  */
swp_entry_to_pte(swp_entry_t entry)115 static inline pte_t swp_entry_to_pte(swp_entry_t entry)
116 {
117 	swp_entry_t arch_entry;
118 
119 	arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
120 	return __swp_entry_to_pte(arch_entry);
121 }
122 
radix_to_swp_entry(void * arg)123 static inline swp_entry_t radix_to_swp_entry(void *arg)
124 {
125 	swp_entry_t entry;
126 
127 	entry.val = xa_to_value(arg);
128 	return entry;
129 }
130 
swp_to_radix_entry(swp_entry_t entry)131 static inline void *swp_to_radix_entry(swp_entry_t entry)
132 {
133 	return xa_mk_value(entry.val);
134 }
135 
136 #if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
make_readable_device_private_entry(pgoff_t offset)137 static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
138 {
139 	return swp_entry(SWP_DEVICE_READ, offset);
140 }
141 
make_writable_device_private_entry(pgoff_t offset)142 static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
143 {
144 	return swp_entry(SWP_DEVICE_WRITE, offset);
145 }
146 
make_device_exclusive_entry(pgoff_t offset)147 static inline swp_entry_t make_device_exclusive_entry(pgoff_t offset)
148 {
149 	return swp_entry(SWP_DEVICE_EXCLUSIVE, offset);
150 }
151 
152 #else /* CONFIG_DEVICE_PRIVATE */
make_readable_device_private_entry(pgoff_t offset)153 static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
154 {
155 	return swp_entry(0, 0);
156 }
157 
make_writable_device_private_entry(pgoff_t offset)158 static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
159 {
160 	return swp_entry(0, 0);
161 }
162 
make_device_exclusive_entry(pgoff_t offset)163 static inline swp_entry_t make_device_exclusive_entry(pgoff_t offset)
164 {
165 	return swp_entry(0, 0);
166 }
167 
168 #endif /* CONFIG_DEVICE_PRIVATE */
169 
170 #ifdef CONFIG_MIGRATION
171 
make_readable_migration_entry(pgoff_t offset)172 static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
173 {
174 	return swp_entry(SWP_MIGRATION_READ, offset);
175 }
176 
make_readable_exclusive_migration_entry(pgoff_t offset)177 static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
178 {
179 	return swp_entry(SWP_MIGRATION_READ_EXCLUSIVE, offset);
180 }
181 
make_writable_migration_entry(pgoff_t offset)182 static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
183 {
184 	return swp_entry(SWP_MIGRATION_WRITE, offset);
185 }
186 
187 /*
188  * Returns whether the host has large enough swap offset field to support
189  * carrying over pgtable A/D bits for page migrations.  The result is
190  * pretty much arch specific.
191  */
migration_entry_supports_ad(void)192 static inline bool migration_entry_supports_ad(void)
193 {
194 #ifdef CONFIG_SWAP
195 	return swap_migration_ad_supported;
196 #else  /* CONFIG_SWAP */
197 	return false;
198 #endif	/* CONFIG_SWAP */
199 }
200 
make_migration_entry_young(swp_entry_t entry)201 static inline swp_entry_t make_migration_entry_young(swp_entry_t entry)
202 {
203 	if (migration_entry_supports_ad())
204 		return swp_entry(swp_type(entry),
205 				 swp_offset(entry) | SWP_MIG_YOUNG);
206 	return entry;
207 }
208 
make_migration_entry_dirty(swp_entry_t entry)209 static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry)
210 {
211 	if (migration_entry_supports_ad())
212 		return swp_entry(swp_type(entry),
213 				 swp_offset(entry) | SWP_MIG_DIRTY);
214 	return entry;
215 }
216 
217 extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
218 					unsigned long address);
219 extern void migration_entry_wait_huge(struct vm_area_struct *vma, unsigned long addr, pte_t *pte);
220 #else  /* CONFIG_MIGRATION */
make_readable_migration_entry(pgoff_t offset)221 static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
222 {
223 	return swp_entry(0, 0);
224 }
225 
make_readable_exclusive_migration_entry(pgoff_t offset)226 static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
227 {
228 	return swp_entry(0, 0);
229 }
230 
make_writable_migration_entry(pgoff_t offset)231 static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
232 {
233 	return swp_entry(0, 0);
234 }
235 
migration_entry_wait(struct mm_struct * mm,pmd_t * pmd,unsigned long address)236 static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
237 					unsigned long address) { }
migration_entry_wait_huge(struct vm_area_struct * vma,unsigned long addr,pte_t * pte)238 static inline void migration_entry_wait_huge(struct vm_area_struct *vma,
239 					     unsigned long addr, pte_t *pte) { }
240 
make_migration_entry_young(swp_entry_t entry)241 static inline swp_entry_t make_migration_entry_young(swp_entry_t entry)
242 {
243 	return entry;
244 }
245 
make_migration_entry_dirty(swp_entry_t entry)246 static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry)
247 {
248 	return entry;
249 }
250 
251 #endif	/* CONFIG_MIGRATION */
252 
253 #ifdef CONFIG_MEMORY_FAILURE
254 
255 /*
256  * Support for hardware poisoned pages
257  */
make_hwpoison_entry(struct page * page)258 static inline swp_entry_t make_hwpoison_entry(struct page *page)
259 {
260 	BUG_ON(!PageLocked(page));
261 	return swp_entry(SWP_HWPOISON, page_to_pfn(page));
262 }
263 
is_hwpoison_entry(swp_entry_t entry)264 static inline int is_hwpoison_entry(swp_entry_t entry)
265 {
266 	return swp_type(entry) == SWP_HWPOISON;
267 }
268 
269 #else
270 
make_hwpoison_entry(struct page * page)271 static inline swp_entry_t make_hwpoison_entry(struct page *page)
272 {
273 	return swp_entry(0, 0);
274 }
275 
is_hwpoison_entry(swp_entry_t swp)276 static inline int is_hwpoison_entry(swp_entry_t swp)
277 {
278 	return 0;
279 }
280 #endif
281 
282 typedef unsigned long pte_marker;
283 
284 #define  PTE_MARKER_UFFD_WP			BIT(0)
285 /*
286  * "Poisoned" here is meant in the very general sense of "future accesses are
287  * invalid", instead of referring very specifically to hardware memory errors.
288  * This marker is meant to represent any of various different causes of this.
289  *
290  * Note that, when encountered by the faulting logic, PTEs with this marker will
291  * result in VM_FAULT_HWPOISON and thus regardless trigger hardware memory error
292  * logic.
293  */
294 #define  PTE_MARKER_POISONED			BIT(1)
295 /*
296  * Indicates that, on fault, this PTE will case a SIGSEGV signal to be
297  * sent. This means guard markers behave in effect as if the region were mapped
298  * PROT_NONE, rather than if they were a memory hole or equivalent.
299  */
300 #define  PTE_MARKER_GUARD			BIT(2)
301 #define  PTE_MARKER_MASK			(BIT(3) - 1)
302 
make_pte_marker_entry(pte_marker marker)303 static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
304 {
305 	return swp_entry(SWP_PTE_MARKER, marker);
306 }
307 
make_pte_marker(pte_marker marker)308 static inline pte_t make_pte_marker(pte_marker marker)
309 {
310 	return swp_entry_to_pte(make_pte_marker_entry(marker));
311 }
312 
make_poisoned_swp_entry(void)313 static inline swp_entry_t make_poisoned_swp_entry(void)
314 {
315 	return make_pte_marker_entry(PTE_MARKER_POISONED);
316 }
317 
make_guard_swp_entry(void)318 static inline swp_entry_t make_guard_swp_entry(void)
319 {
320 	return make_pte_marker_entry(PTE_MARKER_GUARD);
321 }
322 
323 struct page_vma_mapped_walk;
324 
325 #ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES
326 extern int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
327 		struct page *page);
328 
329 void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
330 		struct folio *folio);
331 
332 extern void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd);
333 
swp_entry_to_pmd(swp_entry_t entry)334 static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
335 {
336 	swp_entry_t arch_entry;
337 
338 	arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
339 	return __swp_entry_to_pmd(arch_entry);
340 }
341 
342 #else  /* CONFIG_ARCH_HAS_PMD_SOFTLEAVES */
set_pmd_migration_entry(struct page_vma_mapped_walk * pvmw,struct page * page)343 static inline int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
344 		struct page *page)
345 {
346 	BUILD_BUG();
347 }
348 
remove_migration_pmd(struct page_vma_mapped_walk * pvmw,struct folio * folio)349 static inline void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
350 		struct folio *folio)
351 {
352 	BUILD_BUG();
353 }
354 
pmd_migration_entry_wait(struct mm_struct * m,pmd_t * p)355 static inline void pmd_migration_entry_wait(struct mm_struct *m, pmd_t *p) { }
356 
swp_entry_to_pmd(swp_entry_t entry)357 static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
358 {
359 	return __pmd(0);
360 }
361 
362 #endif  /* CONFIG_ARCH_HAS_PMD_SOFTLEAVES */
363 
364 #endif /* CONFIG_MMU */
365 #endif /* _LINUX_SWAPOPS_H */
366