1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _MM_SWAP_H 3 #define _MM_SWAP_H 4 5 #include <linux/atomic.h> /* for atomic_long_t */ 6 #include <linux/mm.h> /* for PAGE_SHIFT */ 7 struct mempolicy; 8 struct swap_iocb; 9 struct swap_memcg_table; 10 11 #if defined(MAX_POSSIBLE_PHYSMEM_BITS) 12 #define SWAP_CACHE_PFN_BITS (MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT) 13 #elif defined(MAX_PHYSMEM_BITS) 14 #define SWAP_CACHE_PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT) 15 #else 16 #define SWAP_CACHE_PFN_BITS (BITS_PER_LONG - PAGE_SHIFT) 17 #endif 18 19 /* Swap table marker, 0x1 means shadow, 0x2 means PFN (SWP_TB_PFN_MARK) */ 20 #define SWAP_CACHE_PFN_MARK_BITS 2 21 /* At least 2 bits are needed to distinguish SWP_TB_COUNT_MAX, 1 and 0 */ 22 #define SWAP_COUNT_MIN_BITS 2 23 /* If there are enough bits besides PFN and marker, store zero flag inline */ 24 #define SWAP_TABLE_HAS_ZEROFLAG ((BITS_PER_LONG - SWAP_CACHE_PFN_MARK_BITS - \ 25 SWAP_CACHE_PFN_BITS) > SWAP_COUNT_MIN_BITS) 26 27 #ifdef CONFIG_THP_SWAP 28 #define SWAPFILE_CLUSTER HPAGE_PMD_NR 29 #define swap_entry_order(order) (order) 30 #else 31 #define SWAPFILE_CLUSTER 256 32 #define swap_entry_order(order) 0 33 #endif 34 35 extern struct swap_info_struct *swap_info[]; 36 37 /* 38 * We use this to track usage of a cluster. A cluster is a block of swap disk 39 * space with SWAPFILE_CLUSTER pages long and naturally aligns in disk. All 40 * free clusters are organized into a list. We fetch an entry from the list to 41 * get a free cluster. 42 * 43 * The flags field determines if a cluster is free. This is 44 * protected by cluster lock. 45 */ 46 struct swap_cluster_info { 47 spinlock_t lock; /* 48 * Protect swap_cluster_info fields 49 * other than list, and swap_info_struct->swap_map 50 * elements corresponding to the swap cluster. 51 */ 52 u16 count; 53 u8 flags; 54 u8 order; 55 atomic_long_t __rcu *table; /* Swap table entries, see mm/swap_table.h */ 56 unsigned int *extend_table; /* For large swap count, protected by ci->lock */ 57 #ifdef CONFIG_MEMCG 58 struct swap_memcg_table *memcg_table; /* Swap table entries' cgroup record */ 59 #endif 60 #if !SWAP_TABLE_HAS_ZEROFLAG 61 unsigned long *zero_bitmap; 62 #endif 63 struct list_head list; 64 }; 65 66 /* All on-list cluster must have a non-zero flag. */ 67 enum swap_cluster_flags { 68 CLUSTER_FLAG_NONE = 0, /* For temporary off-list cluster */ 69 CLUSTER_FLAG_FREE, 70 CLUSTER_FLAG_NONFULL, 71 CLUSTER_FLAG_FRAG, 72 /* Clusters with flags above are allocatable */ 73 CLUSTER_FLAG_USABLE = CLUSTER_FLAG_FRAG, 74 CLUSTER_FLAG_FULL, 75 CLUSTER_FLAG_DISCARD, 76 CLUSTER_FLAG_MAX, 77 }; 78 79 #ifdef CONFIG_SWAP 80 #include <linux/swapops.h> /* for swp_offset */ 81 #include <linux/blk_types.h> /* for bio_end_io_t */ 82 83 static inline unsigned int swp_cluster_offset(swp_entry_t entry) 84 { 85 return swp_offset(entry) % SWAPFILE_CLUSTER; 86 } 87 88 /* 89 * Callers of all helpers below must ensure the entry, type, or offset is 90 * valid, and protect the swap device with reference count or locks. 91 */ 92 static inline struct swap_info_struct *__swap_type_to_info(int type) 93 { 94 struct swap_info_struct *si; 95 96 si = READ_ONCE(swap_info[type]); /* rcu_dereference() */ 97 VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */ 98 return si; 99 } 100 101 static inline struct swap_info_struct *__swap_entry_to_info(swp_entry_t entry) 102 { 103 return __swap_type_to_info(swp_type(entry)); 104 } 105 106 static inline struct swap_cluster_info *__swap_offset_to_cluster( 107 struct swap_info_struct *si, pgoff_t offset) 108 { 109 VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */ 110 VM_WARN_ON_ONCE(offset >= roundup(si->max, SWAPFILE_CLUSTER)); 111 return &si->cluster_info[offset / SWAPFILE_CLUSTER]; 112 } 113 114 static inline struct swap_cluster_info *__swap_entry_to_cluster(swp_entry_t entry) 115 { 116 return __swap_offset_to_cluster(__swap_entry_to_info(entry), 117 swp_offset(entry)); 118 } 119 120 static __always_inline struct swap_cluster_info *__swap_cluster_lock( 121 struct swap_info_struct *si, unsigned long offset, bool irq) 122 { 123 struct swap_cluster_info *ci = __swap_offset_to_cluster(si, offset); 124 125 /* 126 * Nothing modifies swap cache in an IRQ context. All access to 127 * swap cache is wrapped by swap_cache_* helpers, and swap cache 128 * writeback is handled outside of IRQs. Swapin or swapout never 129 * occurs in IRQ, and neither does in-place split or replace. 130 * 131 * Besides, modifying swap cache requires synchronization with 132 * swap_map, which was never IRQ safe. 133 */ 134 VM_WARN_ON_ONCE(!in_task()); 135 VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */ 136 if (irq) 137 spin_lock_irq(&ci->lock); 138 else 139 spin_lock(&ci->lock); 140 return ci; 141 } 142 143 /** 144 * swap_cluster_lock - Lock and return the swap cluster of given offset. 145 * @si: swap device the cluster belongs to. 146 * @offset: the swap entry offset, pointing to a valid slot. 147 * 148 * Context: The caller must ensure the offset is in the valid range and 149 * protect the swap device with reference count or locks. 150 */ 151 static inline struct swap_cluster_info *swap_cluster_lock( 152 struct swap_info_struct *si, unsigned long offset) 153 { 154 return __swap_cluster_lock(si, offset, false); 155 } 156 157 static inline struct swap_cluster_info *__swap_cluster_get_and_lock( 158 const struct folio *folio, bool irq) 159 { 160 VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); 161 VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio); 162 return __swap_cluster_lock(__swap_entry_to_info(folio->swap), 163 swp_offset(folio->swap), irq); 164 } 165 166 /* 167 * swap_cluster_get_and_lock - Locks the cluster that holds a folio's entries. 168 * @folio: The folio. 169 * 170 * This locks and returns the swap cluster that contains a folio's swap 171 * entries. The swap entries of a folio are always in one single cluster. 172 * The folio has to be locked so its swap entries won't change and the 173 * cluster won't be freed. 174 * 175 * Context: Caller must ensure the folio is locked and in the swap cache. 176 * Return: Pointer to the swap cluster. 177 */ 178 static inline struct swap_cluster_info *swap_cluster_get_and_lock( 179 const struct folio *folio) 180 { 181 return __swap_cluster_get_and_lock(folio, false); 182 } 183 184 /* 185 * swap_cluster_get_and_lock_irq - Locks the cluster that holds a folio's entries. 186 * @folio: The folio. 187 * 188 * Same as swap_cluster_get_and_lock but also disable IRQ. 189 * 190 * Context: Caller must ensure the folio is locked and in the swap cache. 191 * Return: Pointer to the swap cluster. 192 */ 193 static inline struct swap_cluster_info *swap_cluster_get_and_lock_irq( 194 const struct folio *folio) 195 { 196 return __swap_cluster_get_and_lock(folio, true); 197 } 198 199 static inline void swap_cluster_unlock(struct swap_cluster_info *ci) 200 { 201 spin_unlock(&ci->lock); 202 } 203 204 static inline void swap_cluster_unlock_irq(struct swap_cluster_info *ci) 205 { 206 spin_unlock_irq(&ci->lock); 207 } 208 209 extern int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp); 210 211 /* 212 * Below are the core routines for doing swap for a folio. 213 * All helpers requires the folio to be locked, and a locked folio 214 * in the swap cache pins the swap entries / slots allocated to the 215 * folio, swap relies heavily on the swap cache and folio lock for 216 * synchronization. 217 * 218 * folio_alloc_swap(): the entry point for a folio to be swapped 219 * out. It allocates swap slots and pins the slots with swap cache. 220 * The slots start with a swap count of zero. The slots are pinned 221 * by swap cache reference which doesn't contribute to swap count. 222 * 223 * folio_dup_swap(): increases the swap count of a folio, usually 224 * during it gets unmapped and a swap entry is installed to replace 225 * it (e.g., swap entry in page table). A swap slot with swap 226 * count == 0 can only be increased by this helper. 227 * 228 * folio_put_swap(): does the opposite thing of folio_dup_swap(). 229 */ 230 int folio_alloc_swap(struct folio *folio); 231 int folio_dup_swap(struct folio *folio, struct page *page); 232 void folio_put_swap(struct folio *folio, struct page *page); 233 234 /* For internal use */ 235 extern void __swap_cluster_free_entries(struct swap_info_struct *si, 236 struct swap_cluster_info *ci, 237 unsigned int ci_off, unsigned int nr_pages); 238 239 /* linux/mm/page_io.c */ 240 int sio_pool_init(void); 241 struct swap_iocb; 242 void swap_read_folio(struct folio *folio, struct swap_iocb **plug); 243 void __swap_read_unplug(struct swap_iocb *plug); 244 static inline void swap_read_unplug(struct swap_iocb *plug) 245 { 246 if (unlikely(plug)) 247 __swap_read_unplug(plug); 248 } 249 void swap_write_unplug(struct swap_iocb *sio); 250 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug); 251 void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug); 252 253 /* linux/mm/swap_state.c */ 254 extern struct address_space swap_space __read_mostly; 255 static inline struct address_space *swap_address_space(swp_entry_t entry) 256 { 257 return &swap_space; 258 } 259 260 /* 261 * Return the swap device position of the swap entry. 262 */ 263 static inline loff_t swap_dev_pos(swp_entry_t entry) 264 { 265 return ((loff_t)swp_offset(entry)) << PAGE_SHIFT; 266 } 267 268 /** 269 * folio_matches_swap_entry - Check if a folio matches a given swap entry. 270 * @folio: The folio. 271 * @entry: The swap entry to check against. 272 * 273 * Context: The caller should have the folio locked to ensure it's stable 274 * and nothing will move it in or out of the swap cache. 275 * Return: true or false. 276 */ 277 static inline bool folio_matches_swap_entry(const struct folio *folio, 278 swp_entry_t entry) 279 { 280 swp_entry_t folio_entry = folio->swap; 281 long nr_pages = folio_nr_pages(folio); 282 283 VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); 284 if (!folio_test_swapcache(folio)) 285 return false; 286 VM_WARN_ON_ONCE_FOLIO(!IS_ALIGNED(folio_entry.val, nr_pages), folio); 287 return folio_entry.val == round_down(entry.val, nr_pages); 288 } 289 290 /* 291 * All swap cache helpers below require the caller to ensure the swap entries 292 * used are valid and stabilize the device by any of the following ways: 293 * - Hold a reference by get_swap_device(): this ensures a single entry is 294 * valid and increases the swap device's refcount. 295 * - Locking a folio in the swap cache: this ensures the folio's swap entries 296 * are valid and pinned, also implies reference to the device. 297 * - Locking anything referencing the swap entry: e.g. PTL that protects 298 * swap entries in the page table, similar to locking swap cache folio. 299 * - See the comment of get_swap_device() for more complex usage. 300 */ 301 bool swap_cache_has_folio(swp_entry_t entry); 302 struct folio *swap_cache_get_folio(swp_entry_t entry); 303 void *swap_cache_get_shadow(swp_entry_t entry); 304 void swap_cache_del_folio(struct folio *folio); 305 struct folio *swap_cache_alloc_folio(swp_entry_t target_entry, gfp_t gfp_mask, 306 unsigned long orders, struct vm_fault *vmf, 307 struct mempolicy *mpol, pgoff_t ilx); 308 /* Below helpers require the caller to lock and pass in the swap cluster. */ 309 void __swap_cache_add_folio(struct swap_cluster_info *ci, 310 struct folio *folio, swp_entry_t entry); 311 void __swap_cache_del_folio(struct swap_cluster_info *ci, 312 struct folio *folio, swp_entry_t entry, void *shadow); 313 void __swap_cache_replace_folio(struct swap_cluster_info *ci, 314 struct folio *old, struct folio *new); 315 316 void show_swap_cache_info(void); 317 void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry, int nr); 318 struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, 319 struct vm_area_struct *vma, unsigned long addr, 320 struct swap_iocb **plug); 321 struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t flag, 322 struct mempolicy *mpol, pgoff_t ilx); 323 struct folio *swapin_readahead(swp_entry_t entry, gfp_t flag, 324 struct vm_fault *vmf); 325 struct folio *swapin_sync(swp_entry_t entry, gfp_t flag, unsigned long orders, 326 struct vm_fault *vmf, struct mempolicy *mpol, pgoff_t ilx); 327 void swap_update_readahead(struct folio *folio, struct vm_area_struct *vma, 328 unsigned long addr); 329 330 static inline unsigned int folio_swap_flags(struct folio *folio) 331 { 332 return __swap_entry_to_info(folio->swap)->flags; 333 } 334 335 #else /* CONFIG_SWAP */ 336 struct swap_iocb; 337 static inline struct swap_cluster_info *swap_cluster_lock( 338 struct swap_info_struct *si, pgoff_t offset, bool irq) 339 { 340 return NULL; 341 } 342 343 static inline struct swap_cluster_info *swap_cluster_get_and_lock( 344 struct folio *folio) 345 { 346 return NULL; 347 } 348 349 static inline struct swap_cluster_info *swap_cluster_get_and_lock_irq( 350 struct folio *folio) 351 { 352 return NULL; 353 } 354 355 static inline void swap_cluster_unlock(struct swap_cluster_info *ci) 356 { 357 } 358 359 static inline void swap_cluster_unlock_irq(struct swap_cluster_info *ci) 360 { 361 } 362 363 static inline struct swap_info_struct *__swap_entry_to_info(swp_entry_t entry) 364 { 365 return NULL; 366 } 367 368 static inline int folio_alloc_swap(struct folio *folio) 369 { 370 return -EINVAL; 371 } 372 373 static inline int folio_dup_swap(struct folio *folio, struct page *page) 374 { 375 return -EINVAL; 376 } 377 378 static inline void folio_put_swap(struct folio *folio, struct page *page) 379 { 380 } 381 382 static inline void swap_read_folio(struct folio *folio, struct swap_iocb **plug) 383 { 384 } 385 386 static inline void swap_write_unplug(struct swap_iocb *sio) 387 { 388 } 389 390 static inline struct address_space *swap_address_space(swp_entry_t entry) 391 { 392 return NULL; 393 } 394 395 static inline bool folio_matches_swap_entry(const struct folio *folio, swp_entry_t entry) 396 { 397 return false; 398 } 399 400 static inline void show_swap_cache_info(void) 401 { 402 } 403 404 static inline struct folio *swap_cluster_readahead(swp_entry_t entry, 405 gfp_t gfp_mask, struct mempolicy *mpol, pgoff_t ilx) 406 { 407 return NULL; 408 } 409 410 static inline struct folio *swapin_readahead(swp_entry_t swp, gfp_t gfp_mask, 411 struct vm_fault *vmf) 412 { 413 return NULL; 414 } 415 416 static inline struct folio *swapin_sync( 417 swp_entry_t entry, gfp_t flag, unsigned long orders, 418 struct vm_fault *vmf, struct mempolicy *mpol, pgoff_t ilx) 419 { 420 return NULL; 421 } 422 423 static inline void swap_update_readahead(struct folio *folio, 424 struct vm_area_struct *vma, unsigned long addr) 425 { 426 } 427 428 static inline int swap_writeout(struct folio *folio, 429 struct swap_iocb **swap_plug) 430 { 431 return 0; 432 } 433 434 static inline int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp) 435 { 436 return -EINVAL; 437 } 438 439 static inline bool swap_cache_has_folio(swp_entry_t entry) 440 { 441 return false; 442 } 443 444 static inline struct folio *swap_cache_get_folio(swp_entry_t entry) 445 { 446 return NULL; 447 } 448 449 static inline void *swap_cache_get_shadow(swp_entry_t entry) 450 { 451 return NULL; 452 } 453 454 static inline void swap_cache_del_folio(struct folio *folio) 455 { 456 } 457 458 static inline void __swap_cache_del_folio(struct swap_cluster_info *ci, 459 struct folio *folio, swp_entry_t entry, void *shadow) 460 { 461 } 462 463 static inline void __swap_cache_replace_folio(struct swap_cluster_info *ci, 464 struct folio *old, struct folio *new) 465 { 466 } 467 468 static inline unsigned int folio_swap_flags(struct folio *folio) 469 { 470 return 0; 471 } 472 473 #endif /* CONFIG_SWAP */ 474 #endif /* _MM_SWAP_H */ 475