1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_SWAP_H 3 #define _LINUX_SWAP_H 4 5 #include <linux/spinlock.h> 6 #include <linux/linkage.h> 7 #include <linux/mmzone.h> 8 #include <linux/list.h> 9 #include <linux/memcontrol.h> 10 #include <linux/sched.h> 11 #include <linux/node.h> 12 #include <linux/fs.h> 13 #include <linux/pagemap.h> 14 #include <linux/atomic.h> 15 #include <linux/page-flags.h> 16 #include <uapi/linux/mempolicy.h> 17 #include <asm/page.h> 18 19 #define SWAP_FLAG_PREFER 0x8000 /* set if swap priority specified */ 20 #define SWAP_FLAG_PRIO_MASK 0x7fff 21 #define SWAP_FLAG_DISCARD 0x10000 /* enable discard for swap */ 22 #define SWAP_FLAG_DISCARD_ONCE 0x20000 /* discard swap area at swapon-time */ 23 #define SWAP_FLAG_DISCARD_PAGES 0x40000 /* discard page-clusters after use */ 24 25 #define SWAP_FLAGS_VALID (SWAP_FLAG_PRIO_MASK | SWAP_FLAG_PREFER | \ 26 SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | \ 27 SWAP_FLAG_DISCARD_PAGES) 28 /* 29 * MAX_SWAPFILES defines the maximum number of swaptypes: things which can 30 * be swapped to. The swap type and the offset into that swap type are 31 * encoded into pte's and into pgoff_t's in the swapcache. Using five bits 32 * for the type means that the maximum number of swapcache pages is 27 bits 33 * on 32-bit-pgoff_t architectures. And that assumes that the architecture packs 34 * the type/offset into the pte as 5/27 as well. 35 */ 36 #define MAX_SWAPFILES_SHIFT 5 37 38 /* 39 * Use some of the swap files numbers for other purposes. This 40 * is a convenient way to hook into the VM to trigger special 41 * actions on faults. 42 */ 43 44 /* 45 * PTE markers are used to persist information onto PTEs that otherwise 46 * should be a none pte. As its name "PTE" hints, it should only be 47 * applied to the leaves of pgtables. 48 */ 49 #define SWP_PTE_MARKER_NUM 1 50 #define SWP_PTE_MARKER (MAX_SWAPFILES + SWP_HWPOISON_NUM + \ 51 SWP_MIGRATION_NUM + SWP_DEVICE_NUM) 52 53 /* 54 * Unaddressable device memory support. See include/linux/hmm.h and 55 * Documentation/mm/hmm.rst. Short description is we need struct pages for 56 * device memory that is unaddressable (inaccessible) by CPU, so that we can 57 * migrate part of a process memory to device memory. 58 * 59 * When a page is migrated from CPU to device, we set the CPU page table entry 60 * to a special SWP_DEVICE_{READ|WRITE} entry. 61 * 62 * When a page is mapped by the device for exclusive access we set the CPU page 63 * table entries to a special SWP_DEVICE_EXCLUSIVE entry. 64 */ 65 #ifdef CONFIG_DEVICE_PRIVATE 66 #define SWP_DEVICE_NUM 3 67 #define SWP_DEVICE_WRITE (MAX_SWAPFILES+SWP_HWPOISON_NUM+SWP_MIGRATION_NUM) 68 #define SWP_DEVICE_READ (MAX_SWAPFILES+SWP_HWPOISON_NUM+SWP_MIGRATION_NUM+1) 69 #define SWP_DEVICE_EXCLUSIVE (MAX_SWAPFILES+SWP_HWPOISON_NUM+SWP_MIGRATION_NUM+2) 70 #else 71 #define SWP_DEVICE_NUM 0 72 #endif 73 74 /* 75 * Page migration support. 76 * 77 * SWP_MIGRATION_READ_EXCLUSIVE is only applicable to anonymous pages and 78 * indicates that the referenced (part of) an anonymous page is exclusive to 79 * a single process. For SWP_MIGRATION_WRITE, that information is implicit: 80 * (part of) an anonymous page that are mapped writable are exclusive to a 81 * single process. 82 */ 83 #ifdef CONFIG_MIGRATION 84 #define SWP_MIGRATION_NUM 3 85 #define SWP_MIGRATION_READ (MAX_SWAPFILES + SWP_HWPOISON_NUM) 86 #define SWP_MIGRATION_READ_EXCLUSIVE (MAX_SWAPFILES + SWP_HWPOISON_NUM + 1) 87 #define SWP_MIGRATION_WRITE (MAX_SWAPFILES + SWP_HWPOISON_NUM + 2) 88 #else 89 #define SWP_MIGRATION_NUM 0 90 #endif 91 92 /* 93 * Handling of hardware poisoned pages with memory corruption. 94 */ 95 #ifdef CONFIG_MEMORY_FAILURE 96 #define SWP_HWPOISON_NUM 1 97 #define SWP_HWPOISON MAX_SWAPFILES 98 #else 99 #define SWP_HWPOISON_NUM 0 100 #endif 101 102 #define MAX_SWAPFILES \ 103 ((1 << MAX_SWAPFILES_SHIFT) - SWP_DEVICE_NUM - \ 104 SWP_MIGRATION_NUM - SWP_HWPOISON_NUM - \ 105 SWP_PTE_MARKER_NUM) 106 107 /* 108 * Magic header for a swap area. The first part of the union is 109 * what the swap magic looks like for the old (limited to 128MB) 110 * swap area format, the second part of the union adds - in the 111 * old reserved area - some extra information. Note that the first 112 * kilobyte is reserved for boot loader or disk label stuff... 113 * 114 * Having the magic at the end of the PAGE_SIZE makes detecting swap 115 * areas somewhat tricky on machines that support multiple page sizes. 116 * For 2.5 we'll probably want to move the magic to just beyond the 117 * bootbits... 118 */ 119 union swap_header { 120 struct { 121 char reserved[PAGE_SIZE - 10]; 122 char magic[10]; /* SWAP-SPACE or SWAPSPACE2 */ 123 } magic; 124 struct { 125 char bootbits[1024]; /* Space for disklabel etc. */ 126 __u32 version; 127 __u32 last_page; 128 __u32 nr_badpages; 129 unsigned char sws_uuid[16]; 130 unsigned char sws_volume[16]; 131 __u32 padding[117]; 132 __u32 badpages[1]; 133 } info; 134 }; 135 136 /* 137 * current->reclaim_state points to one of these when a task is running 138 * memory reclaim 139 */ 140 struct reclaim_state { 141 /* pages reclaimed outside of LRU-based reclaim */ 142 unsigned long reclaimed; 143 #ifdef CONFIG_LRU_GEN 144 /* per-thread mm walk data */ 145 struct lru_gen_mm_walk *mm_walk; 146 #endif 147 }; 148 149 /* 150 * mm_account_reclaimed_pages(): account reclaimed pages outside of LRU-based 151 * reclaim 152 * @pages: number of pages reclaimed 153 * 154 * If the current process is undergoing a reclaim operation, increment the 155 * number of reclaimed pages by @pages. 156 */ 157 static inline void mm_account_reclaimed_pages(unsigned long pages) 158 { 159 if (current->reclaim_state) 160 current->reclaim_state->reclaimed += pages; 161 } 162 163 #ifdef __KERNEL__ 164 165 struct address_space; 166 struct sysinfo; 167 struct zone; 168 169 /* 170 * A swap extent maps a range of a swapfile's PAGE_SIZE pages onto a range of 171 * disk blocks. A rbtree of swap extents maps the entire swapfile (Where the 172 * term `swapfile' refers to either a blockdevice or an IS_REG file). Apart 173 * from setup, they're handled identically. 174 * 175 * We always assume that blocks are of size PAGE_SIZE. 176 */ 177 struct swap_extent { 178 struct rb_node rb_node; 179 pgoff_t start_page; 180 pgoff_t nr_pages; 181 sector_t start_block; 182 }; 183 184 /* 185 * Max bad pages in the new format.. 186 */ 187 #define MAX_SWAP_BADPAGES \ 188 ((offsetof(union swap_header, magic.magic) - \ 189 offsetof(union swap_header, info.badpages)) / sizeof(int)) 190 191 enum { 192 SWP_USED = (1 << 0), /* is slot in swap_info[] used? */ 193 SWP_WRITEOK = (1 << 1), /* ok to write to this swap? */ 194 SWP_DISCARDABLE = (1 << 2), /* blkdev support discard */ 195 SWP_DISCARDING = (1 << 3), /* now discarding a free cluster */ 196 SWP_SOLIDSTATE = (1 << 4), /* blkdev seeks are cheap */ 197 SWP_BLKDEV = (1 << 6), /* its a block device */ 198 SWP_ACTIVATED = (1 << 7), /* set after swap_activate success */ 199 SWP_AREA_DISCARD = (1 << 9), /* single-time swap area discards */ 200 SWP_PAGE_DISCARD = (1 << 10), /* freed swap page-cluster discards */ 201 SWP_STABLE_WRITES = (1 << 11), /* no overwrite PG_writeback pages */ 202 SWP_SYNCHRONOUS_IO = (1 << 12), /* synchronous IO is efficient */ 203 SWP_HIBERNATION = (1 << 13), /* pinned for hibernation */ 204 /* add others here before... */ 205 }; 206 207 #define SWAP_CLUSTER_MAX 32UL 208 #define SWAP_CLUSTER_MAX_SKIPPED (SWAP_CLUSTER_MAX << 10) 209 #define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX 210 211 /* 212 * The first page in the swap file is the swap header, which is always marked 213 * bad to prevent it from being allocated as an entry. This also prevents the 214 * cluster to which it belongs being marked free. Therefore 0 is safe to use as 215 * a sentinel to indicate an entry is not valid. 216 */ 217 #define SWAP_ENTRY_INVALID 0 218 219 #ifdef CONFIG_THP_SWAP 220 #define SWAP_NR_ORDERS (PMD_ORDER + 1) 221 #else 222 #define SWAP_NR_ORDERS 1 223 #endif 224 225 /* 226 * We keep using same cluster for rotational device so IO will be sequential. 227 * The purpose is to optimize SWAP throughput on these device. 228 */ 229 struct swap_sequential_cluster { 230 unsigned int next[SWAP_NR_ORDERS]; /* Likely next allocation offset */ 231 }; 232 233 /* 234 * The in-memory structure used to track swap areas. 235 */ 236 struct swap_info_struct { 237 struct percpu_ref users; /* indicate and keep swap device valid. */ 238 unsigned long flags; /* SWP_USED etc: see above */ 239 signed short prio; /* swap priority of this type */ 240 struct plist_node list; /* entry in swap_active_head */ 241 signed char type; /* strange name for an index */ 242 unsigned int max; /* size of this swap device */ 243 struct swap_cluster_info *cluster_info; /* array, one entry per cluster */ 244 struct list_head free_clusters; /* free clusters list */ 245 struct list_head full_clusters; /* full clusters list */ 246 struct list_head nonfull_clusters[SWAP_NR_ORDERS]; 247 /* list of cluster that contains at least one free slot */ 248 struct list_head frag_clusters[SWAP_NR_ORDERS]; 249 /* list of cluster that are fragmented or contented */ 250 unsigned int pages; /* total of usable pages of swap */ 251 atomic_long_t inuse_pages; /* number of those currently in use */ 252 struct swap_sequential_cluster *global_cluster; /* Use one global cluster for rotating device */ 253 spinlock_t global_cluster_lock; /* Serialize usage of global cluster */ 254 struct rb_root swap_extent_root;/* root of the swap extent rbtree */ 255 struct block_device *bdev; /* swap device or bdev of swap file */ 256 struct file *swap_file; /* seldom referenced */ 257 struct completion comp; /* seldom referenced */ 258 spinlock_t lock; /* 259 * protect map scan related fields like 260 * inuse_pages and all cluster lists. 261 * Other fields are only changed 262 * at swapon/swapoff, so are protected 263 * by swap_lock. changing flags need 264 * hold this lock and swap_lock. If 265 * both locks need hold, hold swap_lock 266 * first. 267 */ 268 struct work_struct discard_work; /* discard worker */ 269 struct work_struct reclaim_work; /* reclaim worker */ 270 struct list_head discard_clusters; /* discard clusters list */ 271 struct plist_node avail_list; /* entry in swap_avail_head */ 272 const struct swap_ops *ops; 273 }; 274 275 /** 276 * folio_swap_entry - Return the swap entry at a page index within a folio. 277 * @folio: The folio. 278 * @idx: The index of the page within the folio. 279 * 280 * A folio in the swap cache occupies folio_nr_pages() contiguous swap 281 * entries starting at folio->swap. The caller must ensure the folio is 282 * in the swap cache and that @idx is within the folio. 283 */ 284 static inline 285 swp_entry_t folio_swap_entry(const struct folio *folio, unsigned long idx) 286 { 287 swp_entry_t entry = folio->swap; 288 289 VM_WARN_ON_ONCE_FOLIO(idx >= folio_nr_pages(folio), folio); 290 entry.val += idx; 291 return entry; 292 } 293 294 /** 295 * folio_page_swap_entry - Return the swap entry of a page in a folio. 296 * @folio: The folio containing @page. 297 * @page: A page within @folio. 298 * 299 * The caller must ensure the folio is in the swap cache and that @page 300 * is part of @folio. 301 */ 302 static inline swp_entry_t folio_page_swap_entry(const struct folio *folio, 303 const struct page *page) 304 { 305 return folio_swap_entry(folio, folio_page_idx(folio, page)); 306 } 307 308 /* linux/mm/page_alloc.c */ 309 extern unsigned long totalreserve_pages; 310 311 /* Definition of global_zone_page_state not available yet */ 312 #define nr_free_pages() global_zone_page_state(NR_FREE_PAGES) 313 314 /* linux/mm/folio.c */ 315 void folio_add_lru(struct folio *folio); 316 void folio_mark_accessed(struct folio *folio); 317 void lru_add_drain_all(void); 318 319 enum lru_cache_drained { 320 LRU_CACHE_NOT_DRAINED, 321 LRU_CACHE_DRAINED, 322 LRU_CACHE_DRAINED_ALL, 323 }; 324 void lru_cache_drain_for_folio(const struct folio *folio, 325 unsigned int extra_refs, enum lru_cache_drained *drained); 326 327 /* linux/mm/folio-compat.c */ 328 void mark_page_accessed(struct page *page); 329 330 extern atomic_t lru_disable_count; 331 332 static inline bool lru_cache_disabled(void) 333 { 334 return atomic_read(&lru_disable_count); 335 } 336 337 extern unsigned long shrink_all_memory(unsigned long nr_pages); 338 long remove_mapping(struct address_space *mapping, struct folio *folio); 339 long remove_mapping_set_shadow(struct address_space *mapping, 340 struct folio *folio, 341 struct mem_cgroup *target_memcg); 342 343 #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 344 extern int reclaim_register_node(struct node *node); 345 extern void reclaim_unregister_node(struct node *node); 346 347 #else 348 349 static inline int reclaim_register_node(struct node *node) 350 { 351 return 0; 352 } 353 354 static inline void reclaim_unregister_node(struct node *node) 355 { 356 } 357 #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 358 359 void check_move_unevictable_folios(struct folio_batch *fbatch); 360 361 extern void __meminit kswapd_run(int nid); 362 extern void __meminit kswapd_stop(int nid); 363 bool current_is_kswapd(void); 364 365 #ifdef CONFIG_SWAP 366 int add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 367 unsigned long nr_pages, sector_t start_block); 368 int generic_swapfile_activate(struct swap_info_struct *, struct file *, 369 sector_t *); 370 371 static inline unsigned long total_swapcache_pages(void) 372 { 373 return global_node_page_state(NR_SWAPCACHE); 374 } 375 376 void free_swap_cache(struct folio *folio); 377 void free_folio_and_swap_cache(struct folio *folio); 378 void free_pages_and_swap_cache(struct encoded_page **, int); 379 /* linux/mm/swapfile.c */ 380 extern atomic_long_t nr_swap_pages; 381 extern long total_swap_pages; 382 extern atomic_t nr_rotate_swap; 383 384 /* Swap 50% full? Release swapcache more aggressively.. */ 385 static inline bool vm_swap_full(void) 386 { 387 return atomic_long_read(&nr_swap_pages) * 2 < total_swap_pages; 388 } 389 390 static inline long get_nr_swap_pages(void) 391 { 392 return atomic_long_read(&nr_swap_pages); 393 } 394 395 extern void si_swapinfo(struct sysinfo *); 396 extern int pin_hibernation_swap_type(dev_t device, sector_t offset); 397 extern void unpin_hibernation_swap_type(int type); 398 extern int find_hibernation_swap_type(dev_t device, sector_t offset); 399 int find_first_swap(dev_t *device); 400 extern unsigned int count_swap_pages(int, int); 401 extern sector_t swapdev_block(int, pgoff_t); 402 extern int __swap_count(swp_entry_t entry); 403 extern bool swap_entry_swapped(struct swap_info_struct *si, swp_entry_t entry); 404 extern int swp_swapcount(swp_entry_t entry); 405 extern struct swap_info_struct *get_swap_device(swp_entry_t entry); 406 sector_t swap_folio_sector(struct folio *folio); 407 408 /* 409 * If there is an existing swap slot reference (swap entry) and the caller 410 * guarantees that there is no race modification of it (e.g., PTL 411 * protecting the swap entry in page table; shmem's cmpxchg protects t 412 * he swap entry in shmem mapping), these two helpers below can be used 413 * to put/dup the entries directly. 414 * 415 * All entries must be allocated by folio_alloc_swap(). And they must have 416 * a swap count > 1. See comments of folio_*_swap helpers for more info. 417 */ 418 int swap_dup_entry_direct(swp_entry_t entry); 419 void swap_put_entries_direct(swp_entry_t entry, int nr); 420 421 /* 422 * folio_free_swap tries to free the swap entries pinned by a swap cache 423 * folio, it has to be here to be called by other components. 424 */ 425 bool folio_free_swap(struct folio *folio); 426 427 void swap_writeback_dropbehind_folio(struct folio *folio); 428 429 /* Allocate / free (hibernation) exclusive entries */ 430 swp_entry_t swap_alloc_hibernation_slot(int type); 431 void swap_free_hibernation_slot(swp_entry_t entry); 432 433 static inline void put_swap_device(struct swap_info_struct *si) 434 { 435 percpu_ref_put(&si->users); 436 } 437 438 #else /* CONFIG_SWAP */ 439 static inline void swap_writeback_dropbehind_folio(struct folio *folio) {} 440 static inline struct swap_info_struct *get_swap_device(swp_entry_t entry) 441 { 442 return NULL; 443 } 444 445 static inline void put_swap_device(struct swap_info_struct *si) 446 { 447 } 448 449 #define get_nr_swap_pages() 0L 450 #define total_swap_pages 0L 451 #define total_swapcache_pages() 0UL 452 #define vm_swap_full() 0 453 454 #define si_swapinfo(val) \ 455 do { (val)->freeswap = (val)->totalswap = 0; } while (0) 456 #define free_folio_and_swap_cache(folio) \ 457 folio_put(folio) 458 #define free_pages_and_swap_cache(pages, nr) \ 459 release_pages((pages), (nr)); 460 461 static inline void free_swap_cache(struct folio *folio) 462 { 463 } 464 465 static inline int swap_dup_entry_direct(swp_entry_t ent) 466 { 467 return 0; 468 } 469 470 static inline void swap_put_entries_direct(swp_entry_t ent, int nr) 471 { 472 } 473 474 static inline int __swap_count(swp_entry_t entry) 475 { 476 return 0; 477 } 478 479 static inline bool swap_entry_swapped(struct swap_info_struct *si, swp_entry_t entry) 480 { 481 return false; 482 } 483 484 static inline int swp_swapcount(swp_entry_t entry) 485 { 486 return 0; 487 } 488 489 static inline bool folio_free_swap(struct folio *folio) 490 { 491 return false; 492 } 493 494 static inline int add_swap_extent(struct swap_info_struct *sis, 495 unsigned long start_page, 496 unsigned long nr_pages, sector_t start_block) 497 { 498 return -EINVAL; 499 } 500 #endif /* CONFIG_SWAP */ 501 #ifdef CONFIG_MEMCG 502 void lru_reparent_memcg(struct mem_cgroup *memcg, struct mem_cgroup *parent, int nid); 503 #endif 504 505 #if defined(CONFIG_SWAP) && defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP) 506 void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp); 507 static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp) 508 { 509 if (mem_cgroup_disabled()) 510 return; 511 __folio_throttle_swaprate(folio, gfp); 512 } 513 #else 514 static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp) 515 { 516 } 517 #endif 518 519 #if defined(CONFIG_MEMCG) && defined(CONFIG_SWAP) 520 int __mem_cgroup_try_charge_swap(struct folio *folio); 521 static inline int mem_cgroup_try_charge_swap(struct folio *folio) 522 { 523 if (mem_cgroup_disabled()) 524 return 0; 525 return __mem_cgroup_try_charge_swap(folio); 526 } 527 528 extern void __mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages); 529 static inline void mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages) 530 { 531 if (mem_cgroup_disabled()) 532 return; 533 __mem_cgroup_uncharge_swap(id, nr_pages); 534 } 535 536 long mem_cgroup_get_folio_swap_margin(const struct folio *folio); 537 long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg); 538 bool mem_cgroup_swap_full(const struct folio *folio); 539 #else 540 static inline int mem_cgroup_try_charge_swap(struct folio *folio) 541 { 542 return 0; 543 } 544 545 static inline void mem_cgroup_uncharge_swap(unsigned short id, 546 unsigned int nr_pages) 547 { 548 } 549 550 static inline long mem_cgroup_get_folio_swap_margin(const struct folio *folio) 551 { 552 return PAGE_COUNTER_MAX; 553 } 554 555 static inline long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg) 556 { 557 return get_nr_swap_pages(); 558 } 559 560 static inline bool mem_cgroup_swap_full(const struct folio *folio) 561 { 562 return vm_swap_full(); 563 } 564 #endif 565 566 /* for_each_managed_zone_pgdat - helper macro to iterate over all managed zones in a pgdat up to 567 * and including the specified highidx 568 * @zone: The current zone in the iterator 569 * @pgdat: The pgdat which node_zones are being iterated 570 * @idx: The index variable 571 * @highidx: The index of the highest zone to return 572 * 573 * This macro iterates through all managed zones up to and including the specified highidx. 574 * The zone iterator enters an invalid state after macro call and must be reinitialized 575 * before it can be used again. 576 */ 577 #define for_each_managed_zone_pgdat(zone, pgdat, idx, highidx) \ 578 for ((idx) = 0, (zone) = (pgdat)->node_zones; \ 579 (idx) <= (highidx); \ 580 (idx)++, (zone)++) \ 581 if (!managed_zone(zone)) \ 582 continue; \ 583 else 584 585 #endif /* __KERNEL__*/ 586 #endif /* _LINUX_SWAP_H */ 587