1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/mm/swap_state.c 4 * 5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 6 * Swap reorganised 29.12.95, Stephen Tweedie 7 * 8 * Rewritten to use page cache, (C) 1998 Stephen Tweedie 9 */ 10 #include <linux/mm.h> 11 #include <linux/gfp.h> 12 #include <linux/kernel_stat.h> 13 #include <linux/mempolicy.h> 14 #include <linux/swap.h> 15 #include <linux/swapops.h> 16 #include <linux/init.h> 17 #include <linux/pagemap.h> 18 #include <linux/pagevec.h> 19 #include <linux/backing-dev.h> 20 #include <linux/blkdev.h> 21 #include <linux/migrate.h> 22 #include <linux/vmalloc.h> 23 #include <linux/huge_mm.h> 24 #include <linux/shmem_fs.h> 25 #include "internal.h" 26 #include "swap.h" 27 28 /* 29 * swapper_space is a fiction, retained to simplify the path through 30 * vmscan's shrink_folio_list. 31 */ 32 static const struct address_space_operations swap_aops = { 33 .writepage = swap_writepage, 34 .dirty_folio = noop_dirty_folio, 35 #ifdef CONFIG_MIGRATION 36 .migrate_folio = migrate_folio, 37 #endif 38 }; 39 40 struct address_space *swapper_spaces[MAX_SWAPFILES] __read_mostly; 41 static unsigned int nr_swapper_spaces[MAX_SWAPFILES] __read_mostly; 42 static bool enable_vma_readahead __read_mostly = true; 43 44 #define SWAP_RA_ORDER_CEILING 5 45 46 #define SWAP_RA_WIN_SHIFT (PAGE_SHIFT / 2) 47 #define SWAP_RA_HITS_MASK ((1UL << SWAP_RA_WIN_SHIFT) - 1) 48 #define SWAP_RA_HITS_MAX SWAP_RA_HITS_MASK 49 #define SWAP_RA_WIN_MASK (~PAGE_MASK & ~SWAP_RA_HITS_MASK) 50 51 #define SWAP_RA_HITS(v) ((v) & SWAP_RA_HITS_MASK) 52 #define SWAP_RA_WIN(v) (((v) & SWAP_RA_WIN_MASK) >> SWAP_RA_WIN_SHIFT) 53 #define SWAP_RA_ADDR(v) ((v) & PAGE_MASK) 54 55 #define SWAP_RA_VAL(addr, win, hits) \ 56 (((addr) & PAGE_MASK) | \ 57 (((win) << SWAP_RA_WIN_SHIFT) & SWAP_RA_WIN_MASK) | \ 58 ((hits) & SWAP_RA_HITS_MASK)) 59 60 /* Initial readahead hits is 4 to start up with a small window */ 61 #define GET_SWAP_RA_VAL(vma) \ 62 (atomic_long_read(&(vma)->swap_readahead_info) ? : 4) 63 64 static atomic_t swapin_readahead_hits = ATOMIC_INIT(4); 65 66 void show_swap_cache_info(void) 67 { 68 printk("%lu pages in swap cache\n", total_swapcache_pages()); 69 printk("Free swap = %ldkB\n", K(get_nr_swap_pages())); 70 printk("Total swap = %lukB\n", K(total_swap_pages)); 71 } 72 73 void *get_shadow_from_swap_cache(swp_entry_t entry) 74 { 75 struct address_space *address_space = swap_address_space(entry); 76 pgoff_t idx = swap_cache_index(entry); 77 void *shadow; 78 79 shadow = xa_load(&address_space->i_pages, idx); 80 if (xa_is_value(shadow)) 81 return shadow; 82 return NULL; 83 } 84 85 /* 86 * add_to_swap_cache resembles filemap_add_folio on swapper_space, 87 * but sets SwapCache flag and 'swap' instead of mapping and index. 88 */ 89 int add_to_swap_cache(struct folio *folio, swp_entry_t entry, 90 gfp_t gfp, void **shadowp) 91 { 92 struct address_space *address_space = swap_address_space(entry); 93 pgoff_t idx = swap_cache_index(entry); 94 XA_STATE_ORDER(xas, &address_space->i_pages, idx, folio_order(folio)); 95 unsigned long i, nr = folio_nr_pages(folio); 96 void *old; 97 98 xas_set_update(&xas, workingset_update_node); 99 100 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 101 VM_BUG_ON_FOLIO(folio_test_swapcache(folio), folio); 102 VM_BUG_ON_FOLIO(!folio_test_swapbacked(folio), folio); 103 104 folio_ref_add(folio, nr); 105 folio_set_swapcache(folio); 106 folio->swap = entry; 107 108 do { 109 xas_lock_irq(&xas); 110 xas_create_range(&xas); 111 if (xas_error(&xas)) 112 goto unlock; 113 for (i = 0; i < nr; i++) { 114 VM_BUG_ON_FOLIO(xas.xa_index != idx + i, folio); 115 if (shadowp) { 116 old = xas_load(&xas); 117 if (xa_is_value(old)) 118 *shadowp = old; 119 } 120 xas_store(&xas, folio); 121 xas_next(&xas); 122 } 123 address_space->nrpages += nr; 124 __node_stat_mod_folio(folio, NR_FILE_PAGES, nr); 125 __lruvec_stat_mod_folio(folio, NR_SWAPCACHE, nr); 126 unlock: 127 xas_unlock_irq(&xas); 128 } while (xas_nomem(&xas, gfp)); 129 130 if (!xas_error(&xas)) 131 return 0; 132 133 folio_clear_swapcache(folio); 134 folio_ref_sub(folio, nr); 135 return xas_error(&xas); 136 } 137 138 /* 139 * This must be called only on folios that have 140 * been verified to be in the swap cache. 141 */ 142 void __delete_from_swap_cache(struct folio *folio, 143 swp_entry_t entry, void *shadow) 144 { 145 struct address_space *address_space = swap_address_space(entry); 146 int i; 147 long nr = folio_nr_pages(folio); 148 pgoff_t idx = swap_cache_index(entry); 149 XA_STATE(xas, &address_space->i_pages, idx); 150 151 xas_set_update(&xas, workingset_update_node); 152 153 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 154 VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio); 155 VM_BUG_ON_FOLIO(folio_test_writeback(folio), folio); 156 157 for (i = 0; i < nr; i++) { 158 void *entry = xas_store(&xas, shadow); 159 VM_BUG_ON_PAGE(entry != folio, entry); 160 xas_next(&xas); 161 } 162 folio->swap.val = 0; 163 folio_clear_swapcache(folio); 164 address_space->nrpages -= nr; 165 __node_stat_mod_folio(folio, NR_FILE_PAGES, -nr); 166 __lruvec_stat_mod_folio(folio, NR_SWAPCACHE, -nr); 167 } 168 169 /* 170 * This must be called only on folios that have 171 * been verified to be in the swap cache and locked. 172 * It will never put the folio into the free list, 173 * the caller has a reference on the folio. 174 */ 175 void delete_from_swap_cache(struct folio *folio) 176 { 177 swp_entry_t entry = folio->swap; 178 struct address_space *address_space = swap_address_space(entry); 179 180 xa_lock_irq(&address_space->i_pages); 181 __delete_from_swap_cache(folio, entry, NULL); 182 xa_unlock_irq(&address_space->i_pages); 183 184 put_swap_folio(folio, entry); 185 folio_ref_sub(folio, folio_nr_pages(folio)); 186 } 187 188 void clear_shadow_from_swap_cache(int type, unsigned long begin, 189 unsigned long end) 190 { 191 unsigned long curr = begin; 192 void *old; 193 194 for (;;) { 195 swp_entry_t entry = swp_entry(type, curr); 196 unsigned long index = curr & SWAP_ADDRESS_SPACE_MASK; 197 struct address_space *address_space = swap_address_space(entry); 198 XA_STATE(xas, &address_space->i_pages, index); 199 200 xas_set_update(&xas, workingset_update_node); 201 202 xa_lock_irq(&address_space->i_pages); 203 xas_for_each(&xas, old, min(index + (end - curr), SWAP_ADDRESS_SPACE_PAGES)) { 204 if (!xa_is_value(old)) 205 continue; 206 xas_store(&xas, NULL); 207 } 208 xa_unlock_irq(&address_space->i_pages); 209 210 /* search the next swapcache until we meet end */ 211 curr = ALIGN((curr + 1), SWAP_ADDRESS_SPACE_PAGES); 212 if (curr > end) 213 break; 214 } 215 } 216 217 /* 218 * If we are the only user, then try to free up the swap cache. 219 * 220 * Its ok to check the swapcache flag without the folio lock 221 * here because we are going to recheck again inside 222 * folio_free_swap() _with_ the lock. 223 * - Marcelo 224 */ 225 void free_swap_cache(struct folio *folio) 226 { 227 if (folio_test_swapcache(folio) && !folio_mapped(folio) && 228 folio_trylock(folio)) { 229 folio_free_swap(folio); 230 folio_unlock(folio); 231 } 232 } 233 234 /* 235 * Freeing a folio and also freeing any swap cache associated with 236 * this folio if it is the last user. 237 */ 238 void free_folio_and_swap_cache(struct folio *folio) 239 { 240 free_swap_cache(folio); 241 if (!is_huge_zero_folio(folio)) 242 folio_put(folio); 243 } 244 245 /* 246 * Passed an array of pages, drop them all from swapcache and then release 247 * them. They are removed from the LRU and freed if this is their last use. 248 */ 249 void free_pages_and_swap_cache(struct encoded_page **pages, int nr) 250 { 251 struct folio_batch folios; 252 unsigned int refs[PAGEVEC_SIZE]; 253 254 folio_batch_init(&folios); 255 for (int i = 0; i < nr; i++) { 256 struct folio *folio = page_folio(encoded_page_ptr(pages[i])); 257 258 free_swap_cache(folio); 259 refs[folios.nr] = 1; 260 if (unlikely(encoded_page_flags(pages[i]) & 261 ENCODED_PAGE_BIT_NR_PAGES_NEXT)) 262 refs[folios.nr] = encoded_nr_pages(pages[++i]); 263 264 if (folio_batch_add(&folios, folio) == 0) 265 folios_put_refs(&folios, refs); 266 } 267 if (folios.nr) 268 folios_put_refs(&folios, refs); 269 } 270 271 static inline bool swap_use_vma_readahead(void) 272 { 273 return READ_ONCE(enable_vma_readahead) && !atomic_read(&nr_rotate_swap); 274 } 275 276 /* 277 * Lookup a swap entry in the swap cache. A found folio will be returned 278 * unlocked and with its refcount incremented - we rely on the kernel 279 * lock getting page table operations atomic even if we drop the folio 280 * lock before returning. 281 * 282 * Caller must lock the swap device or hold a reference to keep it valid. 283 */ 284 struct folio *swap_cache_get_folio(swp_entry_t entry, 285 struct vm_area_struct *vma, unsigned long addr) 286 { 287 struct folio *folio; 288 289 folio = filemap_get_folio(swap_address_space(entry), swap_cache_index(entry)); 290 if (!IS_ERR(folio)) { 291 bool vma_ra = swap_use_vma_readahead(); 292 bool readahead; 293 294 /* 295 * At the moment, we don't support PG_readahead for anon THP 296 * so let's bail out rather than confusing the readahead stat. 297 */ 298 if (unlikely(folio_test_large(folio))) 299 return folio; 300 301 readahead = folio_test_clear_readahead(folio); 302 if (vma && vma_ra) { 303 unsigned long ra_val; 304 int win, hits; 305 306 ra_val = GET_SWAP_RA_VAL(vma); 307 win = SWAP_RA_WIN(ra_val); 308 hits = SWAP_RA_HITS(ra_val); 309 if (readahead) 310 hits = min_t(int, hits + 1, SWAP_RA_HITS_MAX); 311 atomic_long_set(&vma->swap_readahead_info, 312 SWAP_RA_VAL(addr, win, hits)); 313 } 314 315 if (readahead) { 316 count_vm_event(SWAP_RA_HIT); 317 if (!vma || !vma_ra) 318 atomic_inc(&swapin_readahead_hits); 319 } 320 } else { 321 folio = NULL; 322 } 323 324 return folio; 325 } 326 327 /** 328 * filemap_get_incore_folio - Find and get a folio from the page or swap caches. 329 * @mapping: The address_space to search. 330 * @index: The page cache index. 331 * 332 * This differs from filemap_get_folio() in that it will also look for the 333 * folio in the swap cache. 334 * 335 * Return: The found folio or %NULL. 336 */ 337 struct folio *filemap_get_incore_folio(struct address_space *mapping, 338 pgoff_t index) 339 { 340 swp_entry_t swp; 341 struct swap_info_struct *si; 342 struct folio *folio = filemap_get_entry(mapping, index); 343 344 if (!folio) 345 return ERR_PTR(-ENOENT); 346 if (!xa_is_value(folio)) 347 return folio; 348 if (!shmem_mapping(mapping)) 349 return ERR_PTR(-ENOENT); 350 351 swp = radix_to_swp_entry(folio); 352 /* There might be swapin error entries in shmem mapping. */ 353 if (non_swap_entry(swp)) 354 return ERR_PTR(-ENOENT); 355 /* Prevent swapoff from happening to us */ 356 si = get_swap_device(swp); 357 if (!si) 358 return ERR_PTR(-ENOENT); 359 index = swap_cache_index(swp); 360 folio = filemap_get_folio(swap_address_space(swp), index); 361 put_swap_device(si); 362 return folio; 363 } 364 365 struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, 366 struct mempolicy *mpol, pgoff_t ilx, bool *new_page_allocated, 367 bool skip_if_exists) 368 { 369 struct swap_info_struct *si = swp_swap_info(entry); 370 struct folio *folio; 371 struct folio *new_folio = NULL; 372 struct folio *result = NULL; 373 void *shadow = NULL; 374 375 *new_page_allocated = false; 376 for (;;) { 377 int err; 378 /* 379 * First check the swap cache. Since this is normally 380 * called after swap_cache_get_folio() failed, re-calling 381 * that would confuse statistics. 382 */ 383 folio = filemap_get_folio(swap_address_space(entry), 384 swap_cache_index(entry)); 385 if (!IS_ERR(folio)) 386 goto got_folio; 387 388 /* 389 * Just skip read ahead for unused swap slot. 390 */ 391 if (!swap_entry_swapped(si, entry)) 392 goto put_and_return; 393 394 /* 395 * Get a new folio to read into from swap. Allocate it now if 396 * new_folio not exist, before marking swap_map SWAP_HAS_CACHE, 397 * when -EEXIST will cause any racers to loop around until we 398 * add it to cache. 399 */ 400 if (!new_folio) { 401 new_folio = folio_alloc_mpol(gfp_mask, 0, mpol, ilx, numa_node_id()); 402 if (!new_folio) 403 goto put_and_return; 404 } 405 406 /* 407 * Swap entry may have been freed since our caller observed it. 408 */ 409 err = swapcache_prepare(entry, 1); 410 if (!err) 411 break; 412 else if (err != -EEXIST) 413 goto put_and_return; 414 415 /* 416 * Protect against a recursive call to __read_swap_cache_async() 417 * on the same entry waiting forever here because SWAP_HAS_CACHE 418 * is set but the folio is not the swap cache yet. This can 419 * happen today if mem_cgroup_swapin_charge_folio() below 420 * triggers reclaim through zswap, which may call 421 * __read_swap_cache_async() in the writeback path. 422 */ 423 if (skip_if_exists) 424 goto put_and_return; 425 426 /* 427 * We might race against __delete_from_swap_cache(), and 428 * stumble across a swap_map entry whose SWAP_HAS_CACHE 429 * has not yet been cleared. Or race against another 430 * __read_swap_cache_async(), which has set SWAP_HAS_CACHE 431 * in swap_map, but not yet added its folio to swap cache. 432 */ 433 schedule_timeout_uninterruptible(1); 434 } 435 436 /* 437 * The swap entry is ours to swap in. Prepare the new folio. 438 */ 439 __folio_set_locked(new_folio); 440 __folio_set_swapbacked(new_folio); 441 442 if (mem_cgroup_swapin_charge_folio(new_folio, NULL, gfp_mask, entry)) 443 goto fail_unlock; 444 445 /* May fail (-ENOMEM) if XArray node allocation failed. */ 446 if (add_to_swap_cache(new_folio, entry, gfp_mask & GFP_RECLAIM_MASK, &shadow)) 447 goto fail_unlock; 448 449 memcg1_swapin(entry, 1); 450 451 if (shadow) 452 workingset_refault(new_folio, shadow); 453 454 /* Caller will initiate read into locked new_folio */ 455 folio_add_lru(new_folio); 456 *new_page_allocated = true; 457 folio = new_folio; 458 got_folio: 459 result = folio; 460 goto put_and_return; 461 462 fail_unlock: 463 put_swap_folio(new_folio, entry); 464 folio_unlock(new_folio); 465 put_and_return: 466 if (!(*new_page_allocated) && new_folio) 467 folio_put(new_folio); 468 return result; 469 } 470 471 /* 472 * Locate a page of swap in physical memory, reserving swap cache space 473 * and reading the disk if it is not already cached. 474 * A failure return means that either the page allocation failed or that 475 * the swap entry is no longer in use. 476 * 477 * get/put_swap_device() aren't needed to call this function, because 478 * __read_swap_cache_async() call them and swap_read_folio() holds the 479 * swap cache folio lock. 480 */ 481 struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, 482 struct vm_area_struct *vma, unsigned long addr, 483 struct swap_iocb **plug) 484 { 485 struct swap_info_struct *si; 486 bool page_allocated; 487 struct mempolicy *mpol; 488 pgoff_t ilx; 489 struct folio *folio; 490 491 si = get_swap_device(entry); 492 if (!si) 493 return NULL; 494 495 mpol = get_vma_policy(vma, addr, 0, &ilx); 496 folio = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, 497 &page_allocated, false); 498 mpol_cond_put(mpol); 499 500 if (page_allocated) 501 swap_read_folio(folio, plug); 502 503 put_swap_device(si); 504 return folio; 505 } 506 507 static unsigned int __swapin_nr_pages(unsigned long prev_offset, 508 unsigned long offset, 509 int hits, 510 int max_pages, 511 int prev_win) 512 { 513 unsigned int pages, last_ra; 514 515 /* 516 * This heuristic has been found to work well on both sequential and 517 * random loads, swapping to hard disk or to SSD: please don't ask 518 * what the "+ 2" means, it just happens to work well, that's all. 519 */ 520 pages = hits + 2; 521 if (pages == 2) { 522 /* 523 * We can have no readahead hits to judge by: but must not get 524 * stuck here forever, so check for an adjacent offset instead 525 * (and don't even bother to check whether swap type is same). 526 */ 527 if (offset != prev_offset + 1 && offset != prev_offset - 1) 528 pages = 1; 529 } else { 530 unsigned int roundup = 4; 531 while (roundup < pages) 532 roundup <<= 1; 533 pages = roundup; 534 } 535 536 if (pages > max_pages) 537 pages = max_pages; 538 539 /* Don't shrink readahead too fast */ 540 last_ra = prev_win / 2; 541 if (pages < last_ra) 542 pages = last_ra; 543 544 return pages; 545 } 546 547 static unsigned long swapin_nr_pages(unsigned long offset) 548 { 549 static unsigned long prev_offset; 550 unsigned int hits, pages, max_pages; 551 static atomic_t last_readahead_pages; 552 553 max_pages = 1 << READ_ONCE(page_cluster); 554 if (max_pages <= 1) 555 return 1; 556 557 hits = atomic_xchg(&swapin_readahead_hits, 0); 558 pages = __swapin_nr_pages(READ_ONCE(prev_offset), offset, hits, 559 max_pages, 560 atomic_read(&last_readahead_pages)); 561 if (!hits) 562 WRITE_ONCE(prev_offset, offset); 563 atomic_set(&last_readahead_pages, pages); 564 565 return pages; 566 } 567 568 /** 569 * swap_cluster_readahead - swap in pages in hope we need them soon 570 * @entry: swap entry of this memory 571 * @gfp_mask: memory allocation flags 572 * @mpol: NUMA memory allocation policy to be applied 573 * @ilx: NUMA interleave index, for use only when MPOL_INTERLEAVE 574 * 575 * Returns the struct folio for entry and addr, after queueing swapin. 576 * 577 * Primitive swap readahead code. We simply read an aligned block of 578 * (1 << page_cluster) entries in the swap area. This method is chosen 579 * because it doesn't cost us any seek time. We also make sure to queue 580 * the 'original' request together with the readahead ones... 581 * 582 * Note: it is intentional that the same NUMA policy and interleave index 583 * are used for every page of the readahead: neighbouring pages on swap 584 * are fairly likely to have been swapped out from the same node. 585 */ 586 struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, 587 struct mempolicy *mpol, pgoff_t ilx) 588 { 589 struct folio *folio; 590 unsigned long entry_offset = swp_offset(entry); 591 unsigned long offset = entry_offset; 592 unsigned long start_offset, end_offset; 593 unsigned long mask; 594 struct swap_info_struct *si = swp_swap_info(entry); 595 struct blk_plug plug; 596 struct swap_iocb *splug = NULL; 597 bool page_allocated; 598 599 mask = swapin_nr_pages(offset) - 1; 600 if (!mask) 601 goto skip; 602 603 /* Read a page_cluster sized and aligned cluster around offset. */ 604 start_offset = offset & ~mask; 605 end_offset = offset | mask; 606 if (!start_offset) /* First page is swap header. */ 607 start_offset++; 608 if (end_offset >= si->max) 609 end_offset = si->max - 1; 610 611 blk_start_plug(&plug); 612 for (offset = start_offset; offset <= end_offset ; offset++) { 613 /* Ok, do the async read-ahead now */ 614 folio = __read_swap_cache_async( 615 swp_entry(swp_type(entry), offset), 616 gfp_mask, mpol, ilx, &page_allocated, false); 617 if (!folio) 618 continue; 619 if (page_allocated) { 620 swap_read_folio(folio, &splug); 621 if (offset != entry_offset) { 622 folio_set_readahead(folio); 623 count_vm_event(SWAP_RA); 624 } 625 } 626 folio_put(folio); 627 } 628 blk_finish_plug(&plug); 629 swap_read_unplug(splug); 630 lru_add_drain(); /* Push any new pages onto the LRU now */ 631 skip: 632 /* The page was likely read above, so no need for plugging here */ 633 folio = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, 634 &page_allocated, false); 635 if (unlikely(page_allocated)) 636 swap_read_folio(folio, NULL); 637 return folio; 638 } 639 640 int init_swap_address_space(unsigned int type, unsigned long nr_pages) 641 { 642 struct address_space *spaces, *space; 643 unsigned int i, nr; 644 645 nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES); 646 spaces = kvcalloc(nr, sizeof(struct address_space), GFP_KERNEL); 647 if (!spaces) 648 return -ENOMEM; 649 for (i = 0; i < nr; i++) { 650 space = spaces + i; 651 xa_init_flags(&space->i_pages, XA_FLAGS_LOCK_IRQ); 652 atomic_set(&space->i_mmap_writable, 0); 653 space->a_ops = &swap_aops; 654 /* swap cache doesn't use writeback related tags */ 655 mapping_set_no_writeback_tags(space); 656 } 657 nr_swapper_spaces[type] = nr; 658 swapper_spaces[type] = spaces; 659 660 return 0; 661 } 662 663 void exit_swap_address_space(unsigned int type) 664 { 665 int i; 666 struct address_space *spaces = swapper_spaces[type]; 667 668 for (i = 0; i < nr_swapper_spaces[type]; i++) 669 VM_WARN_ON_ONCE(!mapping_empty(&spaces[i])); 670 kvfree(spaces); 671 nr_swapper_spaces[type] = 0; 672 swapper_spaces[type] = NULL; 673 } 674 675 static int swap_vma_ra_win(struct vm_fault *vmf, unsigned long *start, 676 unsigned long *end) 677 { 678 struct vm_area_struct *vma = vmf->vma; 679 unsigned long ra_val; 680 unsigned long faddr, prev_faddr, left, right; 681 unsigned int max_win, hits, prev_win, win; 682 683 max_win = 1 << min(READ_ONCE(page_cluster), SWAP_RA_ORDER_CEILING); 684 if (max_win == 1) 685 return 1; 686 687 faddr = vmf->address; 688 ra_val = GET_SWAP_RA_VAL(vma); 689 prev_faddr = SWAP_RA_ADDR(ra_val); 690 prev_win = SWAP_RA_WIN(ra_val); 691 hits = SWAP_RA_HITS(ra_val); 692 win = __swapin_nr_pages(PFN_DOWN(prev_faddr), PFN_DOWN(faddr), hits, 693 max_win, prev_win); 694 atomic_long_set(&vma->swap_readahead_info, SWAP_RA_VAL(faddr, win, 0)); 695 if (win == 1) 696 return 1; 697 698 if (faddr == prev_faddr + PAGE_SIZE) 699 left = faddr; 700 else if (prev_faddr == faddr + PAGE_SIZE) 701 left = faddr - (win << PAGE_SHIFT) + PAGE_SIZE; 702 else 703 left = faddr - (((win - 1) / 2) << PAGE_SHIFT); 704 right = left + (win << PAGE_SHIFT); 705 if ((long)left < 0) 706 left = 0; 707 *start = max3(left, vma->vm_start, faddr & PMD_MASK); 708 *end = min3(right, vma->vm_end, (faddr & PMD_MASK) + PMD_SIZE); 709 710 return win; 711 } 712 713 /** 714 * swap_vma_readahead - swap in pages in hope we need them soon 715 * @targ_entry: swap entry of the targeted memory 716 * @gfp_mask: memory allocation flags 717 * @mpol: NUMA memory allocation policy to be applied 718 * @targ_ilx: NUMA interleave index, for use only when MPOL_INTERLEAVE 719 * @vmf: fault information 720 * 721 * Returns the struct folio for entry and addr, after queueing swapin. 722 * 723 * Primitive swap readahead code. We simply read in a few pages whose 724 * virtual addresses are around the fault address in the same vma. 725 * 726 * Caller must hold read mmap_lock if vmf->vma is not NULL. 727 * 728 */ 729 static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, 730 struct mempolicy *mpol, pgoff_t targ_ilx, struct vm_fault *vmf) 731 { 732 struct blk_plug plug; 733 struct swap_iocb *splug = NULL; 734 struct folio *folio; 735 pte_t *pte = NULL, pentry; 736 int win; 737 unsigned long start, end, addr; 738 swp_entry_t entry; 739 pgoff_t ilx; 740 bool page_allocated; 741 742 win = swap_vma_ra_win(vmf, &start, &end); 743 if (win == 1) 744 goto skip; 745 746 ilx = targ_ilx - PFN_DOWN(vmf->address - start); 747 748 blk_start_plug(&plug); 749 for (addr = start; addr < end; ilx++, addr += PAGE_SIZE) { 750 if (!pte++) { 751 pte = pte_offset_map(vmf->pmd, addr); 752 if (!pte) 753 break; 754 } 755 pentry = ptep_get_lockless(pte); 756 if (!is_swap_pte(pentry)) 757 continue; 758 entry = pte_to_swp_entry(pentry); 759 if (unlikely(non_swap_entry(entry))) 760 continue; 761 pte_unmap(pte); 762 pte = NULL; 763 folio = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, 764 &page_allocated, false); 765 if (!folio) 766 continue; 767 if (page_allocated) { 768 swap_read_folio(folio, &splug); 769 if (addr != vmf->address) { 770 folio_set_readahead(folio); 771 count_vm_event(SWAP_RA); 772 } 773 } 774 folio_put(folio); 775 } 776 if (pte) 777 pte_unmap(pte); 778 blk_finish_plug(&plug); 779 swap_read_unplug(splug); 780 lru_add_drain(); 781 skip: 782 /* The folio was likely read above, so no need for plugging here */ 783 folio = __read_swap_cache_async(targ_entry, gfp_mask, mpol, targ_ilx, 784 &page_allocated, false); 785 if (unlikely(page_allocated)) 786 swap_read_folio(folio, NULL); 787 return folio; 788 } 789 790 /** 791 * swapin_readahead - swap in pages in hope we need them soon 792 * @entry: swap entry of this memory 793 * @gfp_mask: memory allocation flags 794 * @vmf: fault information 795 * 796 * Returns the struct folio for entry and addr, after queueing swapin. 797 * 798 * It's a main entry function for swap readahead. By the configuration, 799 * it will read ahead blocks by cluster-based(ie, physical disk based) 800 * or vma-based(ie, virtual address based on faulty address) readahead. 801 */ 802 struct folio *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, 803 struct vm_fault *vmf) 804 { 805 struct mempolicy *mpol; 806 pgoff_t ilx; 807 struct folio *folio; 808 809 mpol = get_vma_policy(vmf->vma, vmf->address, 0, &ilx); 810 folio = swap_use_vma_readahead() ? 811 swap_vma_readahead(entry, gfp_mask, mpol, ilx, vmf) : 812 swap_cluster_readahead(entry, gfp_mask, mpol, ilx); 813 mpol_cond_put(mpol); 814 815 return folio; 816 } 817 818 #ifdef CONFIG_SYSFS 819 static ssize_t vma_ra_enabled_show(struct kobject *kobj, 820 struct kobj_attribute *attr, char *buf) 821 { 822 return sysfs_emit(buf, "%s\n", str_true_false(enable_vma_readahead)); 823 } 824 static ssize_t vma_ra_enabled_store(struct kobject *kobj, 825 struct kobj_attribute *attr, 826 const char *buf, size_t count) 827 { 828 ssize_t ret; 829 830 ret = kstrtobool(buf, &enable_vma_readahead); 831 if (ret) 832 return ret; 833 834 return count; 835 } 836 static struct kobj_attribute vma_ra_enabled_attr = __ATTR_RW(vma_ra_enabled); 837 838 static struct attribute *swap_attrs[] = { 839 &vma_ra_enabled_attr.attr, 840 NULL, 841 }; 842 843 static const struct attribute_group swap_attr_group = { 844 .attrs = swap_attrs, 845 }; 846 847 static int __init swap_init_sysfs(void) 848 { 849 int err; 850 struct kobject *swap_kobj; 851 852 swap_kobj = kobject_create_and_add("swap", mm_kobj); 853 if (!swap_kobj) { 854 pr_err("failed to create swap kobject\n"); 855 return -ENOMEM; 856 } 857 err = sysfs_create_group(swap_kobj, &swap_attr_group); 858 if (err) { 859 pr_err("failed to register swap group\n"); 860 goto delete_obj; 861 } 862 return 0; 863 864 delete_obj: 865 kobject_put(swap_kobj); 866 return err; 867 } 868 subsys_initcall(swap_init_sysfs); 869 #endif 870