mm/swap: add helper swap_offset_available()
[linux-block.git] / include / linux / swapops.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
a2c16d6c
HD
2#ifndef _LINUX_SWAPOPS_H
3#define _LINUX_SWAPOPS_H
4
5#include <linux/radix-tree.h>
187f1882 6#include <linux/bug.h>
2b740303 7#include <linux/mm_types.h>
a2c16d6c 8
9b98fa22
CH
9#ifdef CONFIG_MMU
10
1da177e4
LT
11/*
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
14 * the low-order bits.
15 *
9b15b817 16 * We arrange the `type' and `offset' fields so that `type' is at the seven
e83a9596 17 * high-order bits of the swp_entry_t and `offset' is right-aligned in the
9b15b817
HD
18 * remaining bits. Although `type' itself needs only five bits, we allow for
19 * shmem/tmpfs to shift it all up a further two bits: see swp_to_radix_entry().
1da177e4
LT
20 *
21 * swp_entry_t's are *never* stored anywhere in their arch-dependent format.
22 */
3159f943
MW
23#define SWP_TYPE_SHIFT (BITS_PER_XA_VALUE - MAX_SWAPFILES_SHIFT)
24#define SWP_OFFSET_MASK ((1UL << SWP_TYPE_SHIFT) - 1)
1da177e4 25
099dd687
PX
26/* Clear all flags but only keep swp_entry_t related information */
27static inline pte_t pte_swp_clear_flags(pte_t pte)
28{
1493a191
DH
29 if (pte_swp_exclusive(pte))
30 pte = pte_swp_clear_exclusive(pte);
099dd687
PX
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);
35 return pte;
36}
37
1da177e4
LT
38/*
39 * Store a type+offset into a swp_entry_t in an arch-independent format
40 */
41static inline swp_entry_t swp_entry(unsigned long type, pgoff_t offset)
42{
43 swp_entry_t ret;
44
3159f943 45 ret.val = (type << SWP_TYPE_SHIFT) | (offset & SWP_OFFSET_MASK);
1da177e4
LT
46 return ret;
47}
48
49/*
50 * Extract the `type' field from a swp_entry_t. The swp_entry_t is in
51 * arch-independent format
52 */
53static inline unsigned swp_type(swp_entry_t entry)
54{
3159f943 55 return (entry.val >> SWP_TYPE_SHIFT);
1da177e4
LT
56}
57
58/*
59 * Extract the `offset' field from a swp_entry_t. The swp_entry_t is in
60 * arch-independent format
61 */
62static inline pgoff_t swp_offset(swp_entry_t entry)
63{
3159f943 64 return entry.val & SWP_OFFSET_MASK;
1da177e4
LT
65}
66
698dd4ba
MM
67/* check whether a pte points to a swap entry */
68static inline int is_swap_pte(pte_t pte)
69{
21d9ee3e 70 return !pte_none(pte) && !pte_present(pte);
698dd4ba
MM
71}
72
1da177e4
LT
73/*
74 * Convert the arch-dependent pte representation of a swp_entry_t into an
75 * arch-independent swp_entry_t.
76 */
77static inline swp_entry_t pte_to_swp_entry(pte_t pte)
78{
79 swp_entry_t arch_entry;
80
099dd687 81 pte = pte_swp_clear_flags(pte);
1da177e4
LT
82 arch_entry = __pte_to_swp_entry(pte);
83 return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
84}
85
86/*
87 * Convert the arch-independent representation of a swp_entry_t into the
88 * arch-dependent pte representation.
89 */
90static inline pte_t swp_entry_to_pte(swp_entry_t entry)
91{
92 swp_entry_t arch_entry;
93
94 arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
1da177e4
LT
95 return __swp_entry_to_pte(arch_entry);
96}
0697212a 97
a2c16d6c
HD
98static inline swp_entry_t radix_to_swp_entry(void *arg)
99{
100 swp_entry_t entry;
101
3159f943 102 entry.val = xa_to_value(arg);
a2c16d6c
HD
103 return entry;
104}
105
106static inline void *swp_to_radix_entry(swp_entry_t entry)
107{
3159f943 108 return xa_mk_value(entry.val);
a2c16d6c
HD
109}
110
5042db43 111#if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
4dd845b5 112static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
5042db43 113{
4dd845b5 114 return swp_entry(SWP_DEVICE_READ, offset);
5042db43
JG
115}
116
4dd845b5 117static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
5042db43 118{
4dd845b5 119 return swp_entry(SWP_DEVICE_WRITE, offset);
5042db43
JG
120}
121
4dd845b5 122static inline bool is_device_private_entry(swp_entry_t entry)
5042db43 123{
4dd845b5
AP
124 int type = swp_type(entry);
125 return type == SWP_DEVICE_READ || type == SWP_DEVICE_WRITE;
5042db43
JG
126}
127
4dd845b5 128static inline bool is_writable_device_private_entry(swp_entry_t entry)
5042db43
JG
129{
130 return unlikely(swp_type(entry) == SWP_DEVICE_WRITE);
131}
b756a3b5
AP
132
133static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
134{
135 return swp_entry(SWP_DEVICE_EXCLUSIVE_READ, offset);
136}
137
138static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
139{
140 return swp_entry(SWP_DEVICE_EXCLUSIVE_WRITE, offset);
141}
142
143static inline bool is_device_exclusive_entry(swp_entry_t entry)
144{
145 return swp_type(entry) == SWP_DEVICE_EXCLUSIVE_READ ||
146 swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE;
147}
148
149static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
150{
151 return unlikely(swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE);
152}
5042db43 153#else /* CONFIG_DEVICE_PRIVATE */
4dd845b5 154static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
5042db43
JG
155{
156 return swp_entry(0, 0);
157}
158
4dd845b5 159static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
5042db43 160{
4dd845b5 161 return swp_entry(0, 0);
5042db43
JG
162}
163
164static inline bool is_device_private_entry(swp_entry_t entry)
165{
166 return false;
167}
168
4dd845b5 169static inline bool is_writable_device_private_entry(swp_entry_t entry)
5042db43
JG
170{
171 return false;
172}
b756a3b5
AP
173
174static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
175{
176 return swp_entry(0, 0);
177}
178
179static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
180{
181 return swp_entry(0, 0);
182}
183
184static inline bool is_device_exclusive_entry(swp_entry_t entry)
185{
186 return false;
187}
188
189static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
190{
191 return false;
192}
5042db43
JG
193#endif /* CONFIG_DEVICE_PRIVATE */
194
0697212a 195#ifdef CONFIG_MIGRATION
0697212a
CL
196static inline int is_migration_entry(swp_entry_t entry)
197{
198 return unlikely(swp_type(entry) == SWP_MIGRATION_READ ||
6c287605 199 swp_type(entry) == SWP_MIGRATION_READ_EXCLUSIVE ||
0697212a
CL
200 swp_type(entry) == SWP_MIGRATION_WRITE);
201}
202
4dd845b5 203static inline int is_writable_migration_entry(swp_entry_t entry)
0697212a
CL
204{
205 return unlikely(swp_type(entry) == SWP_MIGRATION_WRITE);
206}
207
6c287605
DH
208static inline int is_readable_migration_entry(swp_entry_t entry)
209{
210 return unlikely(swp_type(entry) == SWP_MIGRATION_READ);
211}
212
213static inline int is_readable_exclusive_migration_entry(swp_entry_t entry)
214{
215 return unlikely(swp_type(entry) == SWP_MIGRATION_READ_EXCLUSIVE);
216}
217
4dd845b5 218static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
0697212a 219{
4dd845b5
AP
220 return swp_entry(SWP_MIGRATION_READ, offset);
221}
222
6c287605
DH
223static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
224{
225 return swp_entry(SWP_MIGRATION_READ_EXCLUSIVE, offset);
226}
227
4dd845b5
AP
228static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
229{
230 return swp_entry(SWP_MIGRATION_WRITE, offset);
0697212a
CL
231}
232
e66f17ff
NH
233extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
234 spinlock_t *ptl);
0697212a
CL
235extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
236 unsigned long address);
cb900f41
KS
237extern void migration_entry_wait_huge(struct vm_area_struct *vma,
238 struct mm_struct *mm, pte_t *pte);
0697212a 239#else
4dd845b5
AP
240static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
241{
242 return swp_entry(0, 0);
243}
244
6c287605
DH
245static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
246{
247 return swp_entry(0, 0);
248}
249
4dd845b5
AP
250static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
251{
252 return swp_entry(0, 0);
253}
0697212a 254
5ec553a9
AM
255static inline int is_migration_entry(swp_entry_t swp)
256{
257 return 0;
258}
0d665e7b 259
e66f17ff
NH
260static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
261 spinlock_t *ptl) { }
0697212a
CL
262static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
263 unsigned long address) { }
cb900f41
KS
264static inline void migration_entry_wait_huge(struct vm_area_struct *vma,
265 struct mm_struct *mm, pte_t *pte) { }
4dd845b5 266static inline int is_writable_migration_entry(swp_entry_t entry)
0697212a
CL
267{
268 return 0;
269}
6c287605
DH
270static inline int is_readable_migration_entry(swp_entry_t entry)
271{
272 return 0;
273}
0697212a
CL
274
275#endif
276
679d1033
PX
277typedef unsigned long pte_marker;
278
1db9dbc2
PX
279#define PTE_MARKER_UFFD_WP BIT(0)
280#define PTE_MARKER_MASK (PTE_MARKER_UFFD_WP)
679d1033
PX
281
282#ifdef CONFIG_PTE_MARKER
283
284static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
285{
286 return swp_entry(SWP_PTE_MARKER, marker);
287}
288
289static inline bool is_pte_marker_entry(swp_entry_t entry)
290{
291 return swp_type(entry) == SWP_PTE_MARKER;
292}
293
294static inline pte_marker pte_marker_get(swp_entry_t entry)
295{
296 return swp_offset(entry) & PTE_MARKER_MASK;
297}
298
299static inline bool is_pte_marker(pte_t pte)
300{
301 return is_swap_pte(pte) && is_pte_marker_entry(pte_to_swp_entry(pte));
302}
303
304#else /* CONFIG_PTE_MARKER */
305
306static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
307{
308 /* This should never be called if !CONFIG_PTE_MARKER */
309 WARN_ON_ONCE(1);
310 return swp_entry(0, 0);
311}
312
313static inline bool is_pte_marker_entry(swp_entry_t entry)
314{
315 return false;
316}
317
318static inline pte_marker pte_marker_get(swp_entry_t entry)
319{
320 return 0;
321}
322
323static inline bool is_pte_marker(pte_t pte)
324{
325 return false;
326}
327
328#endif /* CONFIG_PTE_MARKER */
329
330static inline pte_t make_pte_marker(pte_marker marker)
331{
332 return swp_entry_to_pte(make_pte_marker_entry(marker));
333}
334
335/*
336 * This is a special version to check pte_none() just to cover the case when
337 * the pte is a pte marker. It existed because in many cases the pte marker
338 * should be seen as a none pte; it's just that we have stored some information
339 * onto the none pte so it becomes not-none any more.
340 *
341 * It should be used when the pte is file-backed, ram-based and backing
342 * userspace pages, like shmem. It is not needed upon pgtables that do not
343 * support pte markers at all. For example, it's not needed on anonymous
344 * memory, kernel-only memory (including when the system is during-boot),
345 * non-ram based generic file-system. It's fine to be used even there, but the
346 * extra pte marker check will be pure overhead.
347 *
348 * For systems configured with !CONFIG_PTE_MARKER this will be automatically
349 * optimized to pte_none().
350 */
351static inline int pte_none_mostly(pte_t pte)
352{
353 return pte_none(pte) || is_pte_marker(pte);
354}
355
af5cdaf8
AP
356static inline struct page *pfn_swap_entry_to_page(swp_entry_t entry)
357{
358 struct page *p = pfn_to_page(swp_offset(entry));
359
360 /*
361 * Any use of migration entries may only occur while the
362 * corresponding page is locked
363 */
364 BUG_ON(is_migration_entry(entry) && !PageLocked(p));
365
366 return p;
367}
368
369/*
370 * A pfn swap entry is a special type of swap entry that always has a pfn stored
371 * in the swap offset. They are used to represent unaddressable device memory
372 * and to restrict access to a page undergoing migration.
373 */
374static inline bool is_pfn_swap_entry(swp_entry_t entry)
375{
b756a3b5
AP
376 return is_migration_entry(entry) || is_device_private_entry(entry) ||
377 is_device_exclusive_entry(entry);
af5cdaf8
AP
378}
379
616b8371
ZY
380struct page_vma_mapped_walk;
381
382#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
7f5abe60 383extern int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
616b8371
ZY
384 struct page *page);
385
386extern void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
387 struct page *new);
388
389extern void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd);
390
391static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
392{
393 swp_entry_t arch_entry;
394
ab6e3d09
NH
395 if (pmd_swp_soft_dirty(pmd))
396 pmd = pmd_swp_clear_soft_dirty(pmd);
8f34f1ea
PX
397 if (pmd_swp_uffd_wp(pmd))
398 pmd = pmd_swp_clear_uffd_wp(pmd);
616b8371
ZY
399 arch_entry = __pmd_to_swp_entry(pmd);
400 return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
401}
402
403static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
404{
405 swp_entry_t arch_entry;
406
407 arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
408 return __swp_entry_to_pmd(arch_entry);
409}
410
411static inline int is_pmd_migration_entry(pmd_t pmd)
412{
b304c6f0 413 return is_swap_pmd(pmd) && is_migration_entry(pmd_to_swp_entry(pmd));
616b8371
ZY
414}
415#else
7f5abe60 416static inline int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
616b8371
ZY
417 struct page *page)
418{
419 BUILD_BUG();
420}
421
422static inline void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
423 struct page *new)
424{
425 BUILD_BUG();
426}
427
428static inline void pmd_migration_entry_wait(struct mm_struct *m, pmd_t *p) { }
429
430static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
431{
432 return swp_entry(0, 0);
433}
434
435static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
436{
437 return __pmd(0);
438}
439
440static inline int is_pmd_migration_entry(pmd_t pmd)
441{
442 return 0;
443}
444#endif
445
a7420aa5 446#ifdef CONFIG_MEMORY_FAILURE
8e30456b
NH
447
448extern atomic_long_t num_poisoned_pages __read_mostly;
449
a7420aa5
AK
450/*
451 * Support for hardware poisoned pages
452 */
453static inline swp_entry_t make_hwpoison_entry(struct page *page)
454{
455 BUG_ON(!PageLocked(page));
456 return swp_entry(SWP_HWPOISON, page_to_pfn(page));
457}
458
459static inline int is_hwpoison_entry(swp_entry_t entry)
460{
461 return swp_type(entry) == SWP_HWPOISON;
462}
8e30456b 463
a3f5d80e
NH
464static inline unsigned long hwpoison_entry_to_pfn(swp_entry_t entry)
465{
466 return swp_offset(entry);
467}
468
8e30456b
NH
469static inline void num_poisoned_pages_inc(void)
470{
471 atomic_long_inc(&num_poisoned_pages);
472}
473
474static inline void num_poisoned_pages_dec(void)
475{
476 atomic_long_dec(&num_poisoned_pages);
477}
478
a7420aa5
AK
479#else
480
481static inline swp_entry_t make_hwpoison_entry(struct page *page)
482{
483 return swp_entry(0, 0);
484}
485
486static inline int is_hwpoison_entry(swp_entry_t swp)
487{
488 return 0;
489}
da1b13cc 490
da1b13cc
WL
491static inline void num_poisoned_pages_inc(void)
492{
493}
a7420aa5
AK
494#endif
495
a7420aa5
AK
496static inline int non_swap_entry(swp_entry_t entry)
497{
498 return swp_type(entry) >= MAX_SWAPFILES;
499}
a2c16d6c 500
9b98fa22 501#endif /* CONFIG_MMU */
a2c16d6c 502#endif /* _LINUX_SWAPOPS_H */