1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _MM_SWAP_TABLE_H 3 #define _MM_SWAP_TABLE_H 4 5 #include "swap.h" 6 7 /* 8 * A swap table entry represents the status of a swap slot on a swap 9 * (physical or virtual) device. The swap table in each cluster is a 10 * 1:1 map of the swap slots in this cluster. 11 * 12 * Each swap table entry could be a pointer (folio), a XA_VALUE 13 * (shadow), or NULL. 14 */ 15 16 /* 17 * Helpers for casting one type of info into a swap table entry. 18 */ 19 static inline unsigned long null_to_swp_tb(void) 20 { 21 BUILD_BUG_ON(sizeof(unsigned long) != sizeof(atomic_long_t)); 22 return 0; 23 } 24 25 static inline unsigned long folio_to_swp_tb(struct folio *folio) 26 { 27 BUILD_BUG_ON(sizeof(unsigned long) != sizeof(void *)); 28 return (unsigned long)folio; 29 } 30 31 static inline unsigned long shadow_swp_to_tb(void *shadow) 32 { 33 BUILD_BUG_ON((BITS_PER_XA_VALUE + 1) != 34 BITS_PER_BYTE * sizeof(unsigned long)); 35 VM_WARN_ON_ONCE(shadow && !xa_is_value(shadow)); 36 return (unsigned long)shadow; 37 } 38 39 /* 40 * Helpers for swap table entry type checking. 41 */ 42 static inline bool swp_tb_is_null(unsigned long swp_tb) 43 { 44 return !swp_tb; 45 } 46 47 static inline bool swp_tb_is_folio(unsigned long swp_tb) 48 { 49 return !xa_is_value((void *)swp_tb) && !swp_tb_is_null(swp_tb); 50 } 51 52 static inline bool swp_tb_is_shadow(unsigned long swp_tb) 53 { 54 return xa_is_value((void *)swp_tb); 55 } 56 57 /* 58 * Helpers for retrieving info from swap table. 59 */ 60 static inline struct folio *swp_tb_to_folio(unsigned long swp_tb) 61 { 62 VM_WARN_ON(!swp_tb_is_folio(swp_tb)); 63 return (void *)swp_tb; 64 } 65 66 static inline void *swp_tb_to_shadow(unsigned long swp_tb) 67 { 68 VM_WARN_ON(!swp_tb_is_shadow(swp_tb)); 69 return (void *)swp_tb; 70 } 71 72 /* 73 * Helpers for accessing or modifying the swap table of a cluster, 74 * the swap cluster must be locked. 75 */ 76 static inline void __swap_table_set(struct swap_cluster_info *ci, 77 unsigned int off, unsigned long swp_tb) 78 { 79 VM_WARN_ON_ONCE(off >= SWAPFILE_CLUSTER); 80 atomic_long_set(&ci->table[off], swp_tb); 81 } 82 83 static inline unsigned long __swap_table_xchg(struct swap_cluster_info *ci, 84 unsigned int off, unsigned long swp_tb) 85 { 86 VM_WARN_ON_ONCE(off >= SWAPFILE_CLUSTER); 87 /* Ordering is guaranteed by cluster lock, relax */ 88 return atomic_long_xchg_relaxed(&ci->table[off], swp_tb); 89 } 90 91 static inline unsigned long __swap_table_get(struct swap_cluster_info *ci, 92 unsigned int off) 93 { 94 VM_WARN_ON_ONCE(off >= SWAPFILE_CLUSTER); 95 return atomic_long_read(&ci->table[off]); 96 } 97 #endif 98