Linux Audio

Check our new training course

Loading...
v5.14.15
  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
  9#ifdef CONFIG_MMU
 10
 
 
 
 
 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 *
 16 * We arrange the `type' and `offset' fields so that `type' is at the seven
 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 two bits: see swp_to_radix_entry().
 20 *
 21 * swp_entry_t's are *never* stored anywhere in their arch-dependent format.
 22 */
 23#define SWP_TYPE_SHIFT	(BITS_PER_XA_VALUE - MAX_SWAPFILES_SHIFT)
 24#define SWP_OFFSET_MASK	((1UL << SWP_TYPE_SHIFT) - 1)
 25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 26/* Clear all flags but only keep swp_entry_t related information */
 27static inline pte_t pte_swp_clear_flags(pte_t pte)
 28{
 
 
 29	if (pte_swp_soft_dirty(pte))
 30		pte = pte_swp_clear_soft_dirty(pte);
 31	if (pte_swp_uffd_wp(pte))
 32		pte = pte_swp_clear_uffd_wp(pte);
 33	return pte;
 34}
 35
 36/*
 37 * Store a type+offset into a swp_entry_t in an arch-independent format
 38 */
 39static inline swp_entry_t swp_entry(unsigned long type, pgoff_t offset)
 40{
 41	swp_entry_t ret;
 42
 43	ret.val = (type << SWP_TYPE_SHIFT) | (offset & SWP_OFFSET_MASK);
 44	return ret;
 45}
 46
 47/*
 48 * Extract the `type' field from a swp_entry_t.  The swp_entry_t is in
 49 * arch-independent format
 50 */
 51static inline unsigned swp_type(swp_entry_t entry)
 52{
 53	return (entry.val >> SWP_TYPE_SHIFT);
 54}
 55
 56/*
 57 * Extract the `offset' field from a swp_entry_t.  The swp_entry_t is in
 58 * arch-independent format
 59 */
 60static inline pgoff_t swp_offset(swp_entry_t entry)
 61{
 62	return entry.val & SWP_OFFSET_MASK;
 63}
 64
 
 
 
 
 
 
 
 
 
 
 
 65/* check whether a pte points to a swap entry */
 66static inline int is_swap_pte(pte_t pte)
 67{
 68	return !pte_none(pte) && !pte_present(pte);
 69}
 70
 71/*
 72 * Convert the arch-dependent pte representation of a swp_entry_t into an
 73 * arch-independent swp_entry_t.
 74 */
 75static inline swp_entry_t pte_to_swp_entry(pte_t pte)
 76{
 77	swp_entry_t arch_entry;
 78
 79	pte = pte_swp_clear_flags(pte);
 80	arch_entry = __pte_to_swp_entry(pte);
 81	return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
 82}
 83
 84/*
 85 * Convert the arch-independent representation of a swp_entry_t into the
 86 * arch-dependent pte representation.
 87 */
 88static inline pte_t swp_entry_to_pte(swp_entry_t entry)
 89{
 90	swp_entry_t arch_entry;
 91
 92	arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
 93	return __swp_entry_to_pte(arch_entry);
 94}
 95
 96static inline swp_entry_t radix_to_swp_entry(void *arg)
 97{
 98	swp_entry_t entry;
 99
100	entry.val = xa_to_value(arg);
101	return entry;
102}
103
104static inline void *swp_to_radix_entry(swp_entry_t entry)
105{
106	return xa_mk_value(entry.val);
107}
108
109#if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
110static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
111{
112	return swp_entry(SWP_DEVICE_READ, offset);
113}
114
115static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
116{
117	return swp_entry(SWP_DEVICE_WRITE, offset);
118}
119
120static inline bool is_device_private_entry(swp_entry_t entry)
121{
122	int type = swp_type(entry);
123	return type == SWP_DEVICE_READ || type == SWP_DEVICE_WRITE;
124}
125
126static inline bool is_writable_device_private_entry(swp_entry_t entry)
127{
128	return unlikely(swp_type(entry) == SWP_DEVICE_WRITE);
129}
130
131static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
132{
133	return swp_entry(SWP_DEVICE_EXCLUSIVE_READ, offset);
134}
135
136static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
137{
138	return swp_entry(SWP_DEVICE_EXCLUSIVE_WRITE, offset);
139}
140
141static inline bool is_device_exclusive_entry(swp_entry_t entry)
142{
143	return swp_type(entry) == SWP_DEVICE_EXCLUSIVE_READ ||
144		swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE;
145}
146
147static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
148{
149	return unlikely(swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE);
150}
151#else /* CONFIG_DEVICE_PRIVATE */
152static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
153{
154	return swp_entry(0, 0);
155}
156
157static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
158{
159	return swp_entry(0, 0);
160}
161
162static inline bool is_device_private_entry(swp_entry_t entry)
163{
164	return false;
165}
166
167static inline bool is_writable_device_private_entry(swp_entry_t entry)
168{
169	return false;
170}
171
172static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
173{
174	return swp_entry(0, 0);
175}
176
177static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
178{
179	return swp_entry(0, 0);
180}
181
182static inline bool is_device_exclusive_entry(swp_entry_t entry)
183{
184	return false;
185}
186
187static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
188{
189	return false;
190}
191#endif /* CONFIG_DEVICE_PRIVATE */
192
193#ifdef CONFIG_MIGRATION
194static inline int is_migration_entry(swp_entry_t entry)
195{
196	return unlikely(swp_type(entry) == SWP_MIGRATION_READ ||
 
197			swp_type(entry) == SWP_MIGRATION_WRITE);
198}
199
200static inline int is_writable_migration_entry(swp_entry_t entry)
201{
202	return unlikely(swp_type(entry) == SWP_MIGRATION_WRITE);
203}
204
 
 
 
 
 
 
 
 
 
 
205static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
206{
207	return swp_entry(SWP_MIGRATION_READ, offset);
208}
209
 
 
 
 
 
210static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
211{
212	return swp_entry(SWP_MIGRATION_WRITE, offset);
213}
214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
216					spinlock_t *ptl);
217extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
218					unsigned long address);
219extern void migration_entry_wait_huge(struct vm_area_struct *vma,
220		struct mm_struct *mm, pte_t *pte);
221#else
 
 
222static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
223{
224	return swp_entry(0, 0);
225}
226
 
 
 
 
 
227static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
228{
229	return swp_entry(0, 0);
230}
231
232static inline int is_migration_entry(swp_entry_t swp)
233{
234	return 0;
235}
236
237static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
238					spinlock_t *ptl) { }
239static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
240					 unsigned long address) { }
241static inline void migration_entry_wait_huge(struct vm_area_struct *vma,
242		struct mm_struct *mm, pte_t *pte) { }
 
 
243static inline int is_writable_migration_entry(swp_entry_t entry)
244{
245	return 0;
246}
 
 
 
 
247
248#endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
250static inline struct page *pfn_swap_entry_to_page(swp_entry_t entry)
251{
252	struct page *p = pfn_to_page(swp_offset(entry));
253
254	/*
255	 * Any use of migration entries may only occur while the
256	 * corresponding page is locked
257	 */
258	BUG_ON(is_migration_entry(entry) && !PageLocked(p));
259
260	return p;
261}
262
263/*
264 * A pfn swap entry is a special type of swap entry that always has a pfn stored
265 * in the swap offset. They are used to represent unaddressable device memory
266 * and to restrict access to a page undergoing migration.
267 */
268static inline bool is_pfn_swap_entry(swp_entry_t entry)
269{
 
 
 
270	return is_migration_entry(entry) || is_device_private_entry(entry) ||
271	       is_device_exclusive_entry(entry);
272}
273
274struct page_vma_mapped_walk;
275
276#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
277extern void set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
278		struct page *page);
279
280extern void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
281		struct page *new);
282
283extern void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd);
284
285static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
286{
287	swp_entry_t arch_entry;
288
289	if (pmd_swp_soft_dirty(pmd))
290		pmd = pmd_swp_clear_soft_dirty(pmd);
291	if (pmd_swp_uffd_wp(pmd))
292		pmd = pmd_swp_clear_uffd_wp(pmd);
293	arch_entry = __pmd_to_swp_entry(pmd);
294	return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
295}
296
297static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
298{
299	swp_entry_t arch_entry;
300
301	arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
302	return __swp_entry_to_pmd(arch_entry);
303}
304
305static inline int is_pmd_migration_entry(pmd_t pmd)
306{
307	return !pmd_present(pmd) && is_migration_entry(pmd_to_swp_entry(pmd));
308}
309#else
310static inline void set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
311		struct page *page)
312{
313	BUILD_BUG();
314}
315
316static inline void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
317		struct page *new)
318{
319	BUILD_BUG();
320}
321
322static inline void pmd_migration_entry_wait(struct mm_struct *m, pmd_t *p) { }
323
324static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
325{
326	return swp_entry(0, 0);
327}
328
329static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
330{
331	return __pmd(0);
332}
333
334static inline int is_pmd_migration_entry(pmd_t pmd)
335{
336	return 0;
337}
338#endif
339
340#ifdef CONFIG_MEMORY_FAILURE
341
342extern atomic_long_t num_poisoned_pages __read_mostly;
343
344/*
345 * Support for hardware poisoned pages
346 */
347static inline swp_entry_t make_hwpoison_entry(struct page *page)
348{
349	BUG_ON(!PageLocked(page));
350	return swp_entry(SWP_HWPOISON, page_to_pfn(page));
351}
352
353static inline int is_hwpoison_entry(swp_entry_t entry)
354{
355	return swp_type(entry) == SWP_HWPOISON;
356}
357
358static inline unsigned long hwpoison_entry_to_pfn(swp_entry_t entry)
359{
360	return swp_offset(entry);
361}
362
363static inline void num_poisoned_pages_inc(void)
364{
365	atomic_long_inc(&num_poisoned_pages);
366}
367
368static inline void num_poisoned_pages_dec(void)
369{
370	atomic_long_dec(&num_poisoned_pages);
371}
372
373#else
374
375static inline swp_entry_t make_hwpoison_entry(struct page *page)
376{
377	return swp_entry(0, 0);
378}
379
380static inline int is_hwpoison_entry(swp_entry_t swp)
381{
382	return 0;
383}
384
385static inline void num_poisoned_pages_inc(void)
386{
387}
388#endif
389
390#if defined(CONFIG_MEMORY_FAILURE) || defined(CONFIG_MIGRATION) || \
391    defined(CONFIG_DEVICE_PRIVATE)
392static inline int non_swap_entry(swp_entry_t entry)
393{
394	return swp_type(entry) >= MAX_SWAPFILES;
395}
396#else
397static inline int non_swap_entry(swp_entry_t entry)
398{
399	return 0;
400}
401#endif
402
403#endif /* CONFIG_MMU */
404#endif /* _LINUX_SWAPOPS_H */
v6.2
  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
  9#ifdef CONFIG_MMU
 10
 11#ifdef CONFIG_SWAP
 12#include <linux/swapfile.h>
 13#endif	/* CONFIG_SWAP */
 14
 15/*
 16 * swapcache pages are stored in the swapper_space radix tree.  We want to
 17 * get good packing density in that tree, so the index should be dense in
 18 * the low-order bits.
 19 *
 20 * We arrange the `type' and `offset' fields so that `type' is at the six
 21 * high-order bits of the swp_entry_t and `offset' is right-aligned in the
 22 * remaining bits.  Although `type' itself needs only five bits, we allow for
 23 * shmem/tmpfs to shift it all up a further one bit: see swp_to_radix_entry().
 24 *
 25 * swp_entry_t's are *never* stored anywhere in their arch-dependent format.
 26 */
 27#define SWP_TYPE_SHIFT	(BITS_PER_XA_VALUE - MAX_SWAPFILES_SHIFT)
 28#define SWP_OFFSET_MASK	((1UL << SWP_TYPE_SHIFT) - 1)
 29
 30/*
 31 * Definitions only for PFN swap entries (see is_pfn_swap_entry()).  To
 32 * store PFN, we only need SWP_PFN_BITS bits.  Each of the pfn swap entries
 33 * can use the extra bits to store other information besides PFN.
 34 */
 35#ifdef MAX_PHYSMEM_BITS
 36#define SWP_PFN_BITS		(MAX_PHYSMEM_BITS - PAGE_SHIFT)
 37#else  /* MAX_PHYSMEM_BITS */
 38#define SWP_PFN_BITS		min_t(int, \
 39				      sizeof(phys_addr_t) * 8 - PAGE_SHIFT, \
 40				      SWP_TYPE_SHIFT)
 41#endif	/* MAX_PHYSMEM_BITS */
 42#define SWP_PFN_MASK		(BIT(SWP_PFN_BITS) - 1)
 43
 44/**
 45 * Migration swap entry specific bitfield definitions.  Layout:
 46 *
 47 *   |----------+--------------------|
 48 *   | swp_type | swp_offset         |
 49 *   |----------+--------+-+-+-------|
 50 *   |          | resv   |D|A|  PFN  |
 51 *   |----------+--------+-+-+-------|
 52 *
 53 * @SWP_MIG_YOUNG_BIT: Whether the page used to have young bit set (bit A)
 54 * @SWP_MIG_DIRTY_BIT: Whether the page used to have dirty bit set (bit D)
 55 *
 56 * Note: A/D bits will be stored in migration entries iff there're enough
 57 * free bits in arch specific swp offset.  By default we'll ignore A/D bits
 58 * when migrating a page.  Please refer to migration_entry_supports_ad()
 59 * for more information.  If there're more bits besides PFN and A/D bits,
 60 * they should be reserved and always be zeros.
 61 */
 62#define SWP_MIG_YOUNG_BIT		(SWP_PFN_BITS)
 63#define SWP_MIG_DIRTY_BIT		(SWP_PFN_BITS + 1)
 64#define SWP_MIG_TOTAL_BITS		(SWP_PFN_BITS + 2)
 65
 66#define SWP_MIG_YOUNG			BIT(SWP_MIG_YOUNG_BIT)
 67#define SWP_MIG_DIRTY			BIT(SWP_MIG_DIRTY_BIT)
 68
 69static inline bool is_pfn_swap_entry(swp_entry_t entry);
 70
 71/* Clear all flags but only keep swp_entry_t related information */
 72static inline pte_t pte_swp_clear_flags(pte_t pte)
 73{
 74	if (pte_swp_exclusive(pte))
 75		pte = pte_swp_clear_exclusive(pte);
 76	if (pte_swp_soft_dirty(pte))
 77		pte = pte_swp_clear_soft_dirty(pte);
 78	if (pte_swp_uffd_wp(pte))
 79		pte = pte_swp_clear_uffd_wp(pte);
 80	return pte;
 81}
 82
 83/*
 84 * Store a type+offset into a swp_entry_t in an arch-independent format
 85 */
 86static inline swp_entry_t swp_entry(unsigned long type, pgoff_t offset)
 87{
 88	swp_entry_t ret;
 89
 90	ret.val = (type << SWP_TYPE_SHIFT) | (offset & SWP_OFFSET_MASK);
 91	return ret;
 92}
 93
 94/*
 95 * Extract the `type' field from a swp_entry_t.  The swp_entry_t is in
 96 * arch-independent format
 97 */
 98static inline unsigned swp_type(swp_entry_t entry)
 99{
100	return (entry.val >> SWP_TYPE_SHIFT);
101}
102
103/*
104 * Extract the `offset' field from a swp_entry_t.  The swp_entry_t is in
105 * arch-independent format
106 */
107static inline pgoff_t swp_offset(swp_entry_t entry)
108{
109	return entry.val & SWP_OFFSET_MASK;
110}
111
112/*
113 * This should only be called upon a pfn swap entry to get the PFN stored
114 * in the swap entry.  Please refers to is_pfn_swap_entry() for definition
115 * of pfn swap entry.
116 */
117static inline unsigned long swp_offset_pfn(swp_entry_t entry)
118{
119	VM_BUG_ON(!is_pfn_swap_entry(entry));
120	return swp_offset(entry) & SWP_PFN_MASK;
121}
122
123/* check whether a pte points to a swap entry */
124static inline int is_swap_pte(pte_t pte)
125{
126	return !pte_none(pte) && !pte_present(pte);
127}
128
129/*
130 * Convert the arch-dependent pte representation of a swp_entry_t into an
131 * arch-independent swp_entry_t.
132 */
133static inline swp_entry_t pte_to_swp_entry(pte_t pte)
134{
135	swp_entry_t arch_entry;
136
137	pte = pte_swp_clear_flags(pte);
138	arch_entry = __pte_to_swp_entry(pte);
139	return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
140}
141
142/*
143 * Convert the arch-independent representation of a swp_entry_t into the
144 * arch-dependent pte representation.
145 */
146static inline pte_t swp_entry_to_pte(swp_entry_t entry)
147{
148	swp_entry_t arch_entry;
149
150	arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
151	return __swp_entry_to_pte(arch_entry);
152}
153
154static inline swp_entry_t radix_to_swp_entry(void *arg)
155{
156	swp_entry_t entry;
157
158	entry.val = xa_to_value(arg);
159	return entry;
160}
161
162static inline void *swp_to_radix_entry(swp_entry_t entry)
163{
164	return xa_mk_value(entry.val);
165}
166
167#if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
168static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
169{
170	return swp_entry(SWP_DEVICE_READ, offset);
171}
172
173static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
174{
175	return swp_entry(SWP_DEVICE_WRITE, offset);
176}
177
178static inline bool is_device_private_entry(swp_entry_t entry)
179{
180	int type = swp_type(entry);
181	return type == SWP_DEVICE_READ || type == SWP_DEVICE_WRITE;
182}
183
184static inline bool is_writable_device_private_entry(swp_entry_t entry)
185{
186	return unlikely(swp_type(entry) == SWP_DEVICE_WRITE);
187}
188
189static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
190{
191	return swp_entry(SWP_DEVICE_EXCLUSIVE_READ, offset);
192}
193
194static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
195{
196	return swp_entry(SWP_DEVICE_EXCLUSIVE_WRITE, offset);
197}
198
199static inline bool is_device_exclusive_entry(swp_entry_t entry)
200{
201	return swp_type(entry) == SWP_DEVICE_EXCLUSIVE_READ ||
202		swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE;
203}
204
205static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
206{
207	return unlikely(swp_type(entry) == SWP_DEVICE_EXCLUSIVE_WRITE);
208}
209#else /* CONFIG_DEVICE_PRIVATE */
210static inline swp_entry_t make_readable_device_private_entry(pgoff_t offset)
211{
212	return swp_entry(0, 0);
213}
214
215static inline swp_entry_t make_writable_device_private_entry(pgoff_t offset)
216{
217	return swp_entry(0, 0);
218}
219
220static inline bool is_device_private_entry(swp_entry_t entry)
221{
222	return false;
223}
224
225static inline bool is_writable_device_private_entry(swp_entry_t entry)
226{
227	return false;
228}
229
230static inline swp_entry_t make_readable_device_exclusive_entry(pgoff_t offset)
231{
232	return swp_entry(0, 0);
233}
234
235static inline swp_entry_t make_writable_device_exclusive_entry(pgoff_t offset)
236{
237	return swp_entry(0, 0);
238}
239
240static inline bool is_device_exclusive_entry(swp_entry_t entry)
241{
242	return false;
243}
244
245static inline bool is_writable_device_exclusive_entry(swp_entry_t entry)
246{
247	return false;
248}
249#endif /* CONFIG_DEVICE_PRIVATE */
250
251#ifdef CONFIG_MIGRATION
252static inline int is_migration_entry(swp_entry_t entry)
253{
254	return unlikely(swp_type(entry) == SWP_MIGRATION_READ ||
255			swp_type(entry) == SWP_MIGRATION_READ_EXCLUSIVE ||
256			swp_type(entry) == SWP_MIGRATION_WRITE);
257}
258
259static inline int is_writable_migration_entry(swp_entry_t entry)
260{
261	return unlikely(swp_type(entry) == SWP_MIGRATION_WRITE);
262}
263
264static inline int is_readable_migration_entry(swp_entry_t entry)
265{
266	return unlikely(swp_type(entry) == SWP_MIGRATION_READ);
267}
268
269static inline int is_readable_exclusive_migration_entry(swp_entry_t entry)
270{
271	return unlikely(swp_type(entry) == SWP_MIGRATION_READ_EXCLUSIVE);
272}
273
274static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
275{
276	return swp_entry(SWP_MIGRATION_READ, offset);
277}
278
279static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
280{
281	return swp_entry(SWP_MIGRATION_READ_EXCLUSIVE, offset);
282}
283
284static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
285{
286	return swp_entry(SWP_MIGRATION_WRITE, offset);
287}
288
289/*
290 * Returns whether the host has large enough swap offset field to support
291 * carrying over pgtable A/D bits for page migrations.  The result is
292 * pretty much arch specific.
293 */
294static inline bool migration_entry_supports_ad(void)
295{
296#ifdef CONFIG_SWAP
297	return swap_migration_ad_supported;
298#else  /* CONFIG_SWAP */
299	return false;
300#endif	/* CONFIG_SWAP */
301}
302
303static inline swp_entry_t make_migration_entry_young(swp_entry_t entry)
304{
305	if (migration_entry_supports_ad())
306		return swp_entry(swp_type(entry),
307				 swp_offset(entry) | SWP_MIG_YOUNG);
308	return entry;
309}
310
311static inline bool is_migration_entry_young(swp_entry_t entry)
312{
313	if (migration_entry_supports_ad())
314		return swp_offset(entry) & SWP_MIG_YOUNG;
315	/* Keep the old behavior of aging page after migration */
316	return false;
317}
318
319static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry)
320{
321	if (migration_entry_supports_ad())
322		return swp_entry(swp_type(entry),
323				 swp_offset(entry) | SWP_MIG_DIRTY);
324	return entry;
325}
326
327static inline bool is_migration_entry_dirty(swp_entry_t entry)
328{
329	if (migration_entry_supports_ad())
330		return swp_offset(entry) & SWP_MIG_DIRTY;
331	/* Keep the old behavior of clean page after migration */
332	return false;
333}
334
335extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
336					spinlock_t *ptl);
337extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
338					unsigned long address);
339#ifdef CONFIG_HUGETLB_PAGE
340extern void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl);
341extern void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *pte);
342#endif	/* CONFIG_HUGETLB_PAGE */
343#else  /* CONFIG_MIGRATION */
344static inline swp_entry_t make_readable_migration_entry(pgoff_t offset)
345{
346	return swp_entry(0, 0);
347}
348
349static inline swp_entry_t make_readable_exclusive_migration_entry(pgoff_t offset)
350{
351	return swp_entry(0, 0);
352}
353
354static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
355{
356	return swp_entry(0, 0);
357}
358
359static inline int is_migration_entry(swp_entry_t swp)
360{
361	return 0;
362}
363
364static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
365					spinlock_t *ptl) { }
366static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
367					 unsigned long address) { }
368#ifdef CONFIG_HUGETLB_PAGE
369static inline void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl) { }
370static inline void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *pte) { }
371#endif	/* CONFIG_HUGETLB_PAGE */
372static inline int is_writable_migration_entry(swp_entry_t entry)
373{
374	return 0;
375}
376static inline int is_readable_migration_entry(swp_entry_t entry)
377{
378	return 0;
379}
380
381static inline swp_entry_t make_migration_entry_young(swp_entry_t entry)
382{
383	return entry;
384}
385
386static inline bool is_migration_entry_young(swp_entry_t entry)
387{
388	return false;
389}
390
391static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry)
392{
393	return entry;
394}
395
396static inline bool is_migration_entry_dirty(swp_entry_t entry)
397{
398	return false;
399}
400#endif	/* CONFIG_MIGRATION */
401
402typedef unsigned long pte_marker;
403
404#define  PTE_MARKER_UFFD_WP			BIT(0)
405#define  PTE_MARKER_SWAPIN_ERROR		BIT(1)
406#define  PTE_MARKER_MASK			(BIT(2) - 1)
407
408static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
409{
410	return swp_entry(SWP_PTE_MARKER, marker);
411}
412
413static inline bool is_pte_marker_entry(swp_entry_t entry)
414{
415	return swp_type(entry) == SWP_PTE_MARKER;
416}
417
418static inline pte_marker pte_marker_get(swp_entry_t entry)
419{
420	return swp_offset(entry) & PTE_MARKER_MASK;
421}
422
423static inline bool is_pte_marker(pte_t pte)
424{
425	return is_swap_pte(pte) && is_pte_marker_entry(pte_to_swp_entry(pte));
426}
427
428static inline pte_t make_pte_marker(pte_marker marker)
429{
430	return swp_entry_to_pte(make_pte_marker_entry(marker));
431}
432
433static inline swp_entry_t make_swapin_error_entry(void)
434{
435	return make_pte_marker_entry(PTE_MARKER_SWAPIN_ERROR);
436}
437
438static inline int is_swapin_error_entry(swp_entry_t entry)
439{
440	return is_pte_marker_entry(entry) &&
441	    (pte_marker_get(entry) & PTE_MARKER_SWAPIN_ERROR);
442}
443
444/*
445 * This is a special version to check pte_none() just to cover the case when
446 * the pte is a pte marker.  It existed because in many cases the pte marker
447 * should be seen as a none pte; it's just that we have stored some information
448 * onto the none pte so it becomes not-none any more.
449 *
450 * It should be used when the pte is file-backed, ram-based and backing
451 * userspace pages, like shmem.  It is not needed upon pgtables that do not
452 * support pte markers at all.  For example, it's not needed on anonymous
453 * memory, kernel-only memory (including when the system is during-boot),
454 * non-ram based generic file-system.  It's fine to be used even there, but the
455 * extra pte marker check will be pure overhead.
456 */
457static inline int pte_none_mostly(pte_t pte)
458{
459	return pte_none(pte) || is_pte_marker(pte);
460}
461
462static inline struct page *pfn_swap_entry_to_page(swp_entry_t entry)
463{
464	struct page *p = pfn_to_page(swp_offset_pfn(entry));
465
466	/*
467	 * Any use of migration entries may only occur while the
468	 * corresponding page is locked
469	 */
470	BUG_ON(is_migration_entry(entry) && !PageLocked(p));
471
472	return p;
473}
474
475/*
476 * A pfn swap entry is a special type of swap entry that always has a pfn stored
477 * in the swap offset. They are used to represent unaddressable device memory
478 * and to restrict access to a page undergoing migration.
479 */
480static inline bool is_pfn_swap_entry(swp_entry_t entry)
481{
482	/* Make sure the swp offset can always store the needed fields */
483	BUILD_BUG_ON(SWP_TYPE_SHIFT < SWP_PFN_BITS);
484
485	return is_migration_entry(entry) || is_device_private_entry(entry) ||
486	       is_device_exclusive_entry(entry);
487}
488
489struct page_vma_mapped_walk;
490
491#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
492extern int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
493		struct page *page);
494
495extern void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
496		struct page *new);
497
498extern void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd);
499
500static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
501{
502	swp_entry_t arch_entry;
503
504	if (pmd_swp_soft_dirty(pmd))
505		pmd = pmd_swp_clear_soft_dirty(pmd);
506	if (pmd_swp_uffd_wp(pmd))
507		pmd = pmd_swp_clear_uffd_wp(pmd);
508	arch_entry = __pmd_to_swp_entry(pmd);
509	return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
510}
511
512static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
513{
514	swp_entry_t arch_entry;
515
516	arch_entry = __swp_entry(swp_type(entry), swp_offset(entry));
517	return __swp_entry_to_pmd(arch_entry);
518}
519
520static inline int is_pmd_migration_entry(pmd_t pmd)
521{
522	return is_swap_pmd(pmd) && is_migration_entry(pmd_to_swp_entry(pmd));
523}
524#else  /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
525static inline int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
526		struct page *page)
527{
528	BUILD_BUG();
529}
530
531static inline void remove_migration_pmd(struct page_vma_mapped_walk *pvmw,
532		struct page *new)
533{
534	BUILD_BUG();
535}
536
537static inline void pmd_migration_entry_wait(struct mm_struct *m, pmd_t *p) { }
538
539static inline swp_entry_t pmd_to_swp_entry(pmd_t pmd)
540{
541	return swp_entry(0, 0);
542}
543
544static inline pmd_t swp_entry_to_pmd(swp_entry_t entry)
545{
546	return __pmd(0);
547}
548
549static inline int is_pmd_migration_entry(pmd_t pmd)
550{
551	return 0;
552}
553#endif  /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
554
555#ifdef CONFIG_MEMORY_FAILURE
556
 
 
557/*
558 * Support for hardware poisoned pages
559 */
560static inline swp_entry_t make_hwpoison_entry(struct page *page)
561{
562	BUG_ON(!PageLocked(page));
563	return swp_entry(SWP_HWPOISON, page_to_pfn(page));
564}
565
566static inline int is_hwpoison_entry(swp_entry_t entry)
567{
568	return swp_type(entry) == SWP_HWPOISON;
569}
570
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
571#else
572
573static inline swp_entry_t make_hwpoison_entry(struct page *page)
574{
575	return swp_entry(0, 0);
576}
577
578static inline int is_hwpoison_entry(swp_entry_t swp)
579{
580	return 0;
581}
 
 
 
 
582#endif
583
 
 
584static inline int non_swap_entry(swp_entry_t entry)
585{
586	return swp_type(entry) >= MAX_SWAPFILES;
587}
 
 
 
 
 
 
588
589#endif /* CONFIG_MMU */
590#endif /* _LINUX_SWAPOPS_H */