1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/mm/swap.c 4 * 5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 6 */ 7 8 /* 9 * This file contains the default values for the operation of the 10 * Linux VM subsystem. Fine-tuning documentation can be found in 11 * Documentation/admin-guide/sysctl/vm.rst. 12 * Started 18.12.91 13 * Swap aging added 23.2.95, Stephen Tweedie. 14 * Buffermem limits added 12.3.98, Rik van Riel. 15 */ 16 17 #include <linux/mm.h> 18 #include <linux/sched.h> 19 #include <linux/kernel_stat.h> 20 #include <linux/swap.h> 21 #include <linux/mman.h> 22 #include <linux/pagemap.h> 23 #include <linux/pagevec.h> 24 #include <linux/init.h> 25 #include <linux/export.h> 26 #include <linux/mm_inline.h> 27 #include <linux/percpu_counter.h> 28 #include <linux/memremap.h> 29 #include <linux/percpu.h> 30 #include <linux/cpu.h> 31 #include <linux/notifier.h> 32 #include <linux/backing-dev.h> 33 #include <linux/memcontrol.h> 34 #include <linux/gfp.h> 35 #include <linux/uio.h> 36 #include <linux/hugetlb.h> 37 #include <linux/page_idle.h> 38 #include <linux/local_lock.h> 39 #include <linux/buffer_head.h> 40 41 #include "internal.h" 42 43 #define CREATE_TRACE_POINTS 44 #include <trace/events/pagemap.h> 45 46 /* How many pages do we try to swap or page in/out together? As a power of 2 */ 47 int page_cluster; 48 const int page_cluster_max = 31; 49 50 /* Protecting only lru_rotate.fbatch which requires disabling interrupts */ 51 struct lru_rotate { 52 local_lock_t lock; 53 struct folio_batch fbatch; 54 }; 55 static DEFINE_PER_CPU(struct lru_rotate, lru_rotate) = { 56 .lock = INIT_LOCAL_LOCK(lock), 57 }; 58 59 /* 60 * The following folio batches are grouped together because they are protected 61 * by disabling preemption (and interrupts remain enabled). 62 */ 63 struct cpu_fbatches { 64 local_lock_t lock; 65 struct folio_batch lru_add; 66 struct folio_batch lru_deactivate_file; 67 struct folio_batch lru_deactivate; 68 struct folio_batch lru_lazyfree; 69 #ifdef CONFIG_SMP 70 struct folio_batch activate; 71 #endif 72 }; 73 static DEFINE_PER_CPU(struct cpu_fbatches, cpu_fbatches) = { 74 .lock = INIT_LOCAL_LOCK(lock), 75 }; 76 77 /* 78 * This path almost never happens for VM activity - pages are normally freed 79 * in batches. But it gets used by networking - and for compound pages. 80 */ 81 static void __page_cache_release(struct folio *folio) 82 { 83 if (folio_test_lru(folio)) { 84 struct lruvec *lruvec; 85 unsigned long flags; 86 87 lruvec = folio_lruvec_lock_irqsave(folio, &flags); 88 lruvec_del_folio(lruvec, folio); 89 __folio_clear_lru_flags(folio); 90 unlock_page_lruvec_irqrestore(lruvec, flags); 91 } 92 } 93 94 static void __folio_put_small(struct folio *folio) 95 { 96 __page_cache_release(folio); 97 mem_cgroup_uncharge(folio); 98 free_unref_page(&folio->page, 0); 99 } 100 101 static void __folio_put_large(struct folio *folio) 102 { 103 /* 104 * __page_cache_release() is supposed to be called for thp, not for 105 * hugetlb. This is because hugetlb page does never have PageLRU set 106 * (it's never listed to any LRU lists) and no memcg routines should 107 * be called for hugetlb (it has a separate hugetlb_cgroup.) 108 */ 109 if (!folio_test_hugetlb(folio)) 110 __page_cache_release(folio); 111 destroy_large_folio(folio); 112 } 113 114 void __folio_put(struct folio *folio) 115 { 116 if (unlikely(folio_is_zone_device(folio))) 117 free_zone_device_page(&folio->page); 118 else if (unlikely(folio_test_large(folio))) 119 __folio_put_large(folio); 120 else 121 __folio_put_small(folio); 122 } 123 EXPORT_SYMBOL(__folio_put); 124 125 /** 126 * put_pages_list() - release a list of pages 127 * @pages: list of pages threaded on page->lru 128 * 129 * Release a list of pages which are strung together on page.lru. 130 */ 131 void put_pages_list(struct list_head *pages) 132 { 133 struct folio *folio, *next; 134 135 list_for_each_entry_safe(folio, next, pages, lru) { 136 if (!folio_put_testzero(folio)) { 137 list_del(&folio->lru); 138 continue; 139 } 140 if (folio_test_large(folio)) { 141 list_del(&folio->lru); 142 __folio_put_large(folio); 143 continue; 144 } 145 /* LRU flag must be clear because it's passed using the lru */ 146 } 147 148 free_unref_page_list(pages); 149 INIT_LIST_HEAD(pages); 150 } 151 EXPORT_SYMBOL(put_pages_list); 152 153 typedef void (*move_fn_t)(struct lruvec *lruvec, struct folio *folio); 154 155 static void lru_add_fn(struct lruvec *lruvec, struct folio *folio) 156 { 157 int was_unevictable = folio_test_clear_unevictable(folio); 158 long nr_pages = folio_nr_pages(folio); 159 160 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); 161 162 /* 163 * Is an smp_mb__after_atomic() still required here, before 164 * folio_evictable() tests the mlocked flag, to rule out the possibility 165 * of stranding an evictable folio on an unevictable LRU? I think 166 * not, because __munlock_folio() only clears the mlocked flag 167 * while the LRU lock is held. 168 * 169 * (That is not true of __page_cache_release(), and not necessarily 170 * true of release_pages(): but those only clear the mlocked flag after 171 * folio_put_testzero() has excluded any other users of the folio.) 172 */ 173 if (folio_evictable(folio)) { 174 if (was_unevictable) 175 __count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages); 176 } else { 177 folio_clear_active(folio); 178 folio_set_unevictable(folio); 179 /* 180 * folio->mlock_count = !!folio_test_mlocked(folio)? 181 * But that leaves __mlock_folio() in doubt whether another 182 * actor has already counted the mlock or not. Err on the 183 * safe side, underestimate, let page reclaim fix it, rather 184 * than leaving a page on the unevictable LRU indefinitely. 185 */ 186 folio->mlock_count = 0; 187 if (!was_unevictable) 188 __count_vm_events(UNEVICTABLE_PGCULLED, nr_pages); 189 } 190 191 lruvec_add_folio(lruvec, folio); 192 trace_mm_lru_insertion(folio); 193 } 194 195 static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn) 196 { 197 int i; 198 struct lruvec *lruvec = NULL; 199 unsigned long flags = 0; 200 201 for (i = 0; i < folio_batch_count(fbatch); i++) { 202 struct folio *folio = fbatch->folios[i]; 203 204 /* block memcg migration while the folio moves between lru */ 205 if (move_fn != lru_add_fn && !folio_test_clear_lru(folio)) 206 continue; 207 208 lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags); 209 move_fn(lruvec, folio); 210 211 folio_set_lru(folio); 212 } 213 214 if (lruvec) 215 unlock_page_lruvec_irqrestore(lruvec, flags); 216 folios_put(fbatch->folios, folio_batch_count(fbatch)); 217 folio_batch_reinit(fbatch); 218 } 219 220 static void folio_batch_add_and_move(struct folio_batch *fbatch, 221 struct folio *folio, move_fn_t move_fn) 222 { 223 if (folio_batch_add(fbatch, folio) && !folio_test_large(folio) && 224 !lru_cache_disabled()) 225 return; 226 folio_batch_move_lru(fbatch, move_fn); 227 } 228 229 static void lru_move_tail_fn(struct lruvec *lruvec, struct folio *folio) 230 { 231 if (!folio_test_unevictable(folio)) { 232 lruvec_del_folio(lruvec, folio); 233 folio_clear_active(folio); 234 lruvec_add_folio_tail(lruvec, folio); 235 __count_vm_events(PGROTATED, folio_nr_pages(folio)); 236 } 237 } 238 239 /* 240 * Writeback is about to end against a folio which has been marked for 241 * immediate reclaim. If it still appears to be reclaimable, move it 242 * to the tail of the inactive list. 243 * 244 * folio_rotate_reclaimable() must disable IRQs, to prevent nasty races. 245 */ 246 void folio_rotate_reclaimable(struct folio *folio) 247 { 248 if (!folio_test_locked(folio) && !folio_test_dirty(folio) && 249 !folio_test_unevictable(folio) && folio_test_lru(folio)) { 250 struct folio_batch *fbatch; 251 unsigned long flags; 252 253 folio_get(folio); 254 local_lock_irqsave(&lru_rotate.lock, flags); 255 fbatch = this_cpu_ptr(&lru_rotate.fbatch); 256 folio_batch_add_and_move(fbatch, folio, lru_move_tail_fn); 257 local_unlock_irqrestore(&lru_rotate.lock, flags); 258 } 259 } 260 261 void lru_note_cost(struct lruvec *lruvec, bool file, 262 unsigned int nr_io, unsigned int nr_rotated) 263 { 264 unsigned long cost; 265 266 /* 267 * Reflect the relative cost of incurring IO and spending CPU 268 * time on rotations. This doesn't attempt to make a precise 269 * comparison, it just says: if reloads are about comparable 270 * between the LRU lists, or rotations are overwhelmingly 271 * different between them, adjust scan balance for CPU work. 272 */ 273 cost = nr_io * SWAP_CLUSTER_MAX + nr_rotated; 274 275 do { 276 unsigned long lrusize; 277 278 /* 279 * Hold lruvec->lru_lock is safe here, since 280 * 1) The pinned lruvec in reclaim, or 281 * 2) From a pre-LRU page during refault (which also holds the 282 * rcu lock, so would be safe even if the page was on the LRU 283 * and could move simultaneously to a new lruvec). 284 */ 285 spin_lock_irq(&lruvec->lru_lock); 286 /* Record cost event */ 287 if (file) 288 lruvec->file_cost += cost; 289 else 290 lruvec->anon_cost += cost; 291 292 /* 293 * Decay previous events 294 * 295 * Because workloads change over time (and to avoid 296 * overflow) we keep these statistics as a floating 297 * average, which ends up weighing recent refaults 298 * more than old ones. 299 */ 300 lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) + 301 lruvec_page_state(lruvec, NR_ACTIVE_ANON) + 302 lruvec_page_state(lruvec, NR_INACTIVE_FILE) + 303 lruvec_page_state(lruvec, NR_ACTIVE_FILE); 304 305 if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) { 306 lruvec->file_cost /= 2; 307 lruvec->anon_cost /= 2; 308 } 309 spin_unlock_irq(&lruvec->lru_lock); 310 } while ((lruvec = parent_lruvec(lruvec))); 311 } 312 313 void lru_note_cost_refault(struct folio *folio) 314 { 315 lru_note_cost(folio_lruvec(folio), folio_is_file_lru(folio), 316 folio_nr_pages(folio), 0); 317 } 318 319 static void folio_activate_fn(struct lruvec *lruvec, struct folio *folio) 320 { 321 if (!folio_test_active(folio) && !folio_test_unevictable(folio)) { 322 long nr_pages = folio_nr_pages(folio); 323 324 lruvec_del_folio(lruvec, folio); 325 folio_set_active(folio); 326 lruvec_add_folio(lruvec, folio); 327 trace_mm_lru_activate(folio); 328 329 __count_vm_events(PGACTIVATE, nr_pages); 330 __count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE, 331 nr_pages); 332 } 333 } 334 335 #ifdef CONFIG_SMP 336 static void folio_activate_drain(int cpu) 337 { 338 struct folio_batch *fbatch = &per_cpu(cpu_fbatches.activate, cpu); 339 340 if (folio_batch_count(fbatch)) 341 folio_batch_move_lru(fbatch, folio_activate_fn); 342 } 343 344 void folio_activate(struct folio *folio) 345 { 346 if (folio_test_lru(folio) && !folio_test_active(folio) && 347 !folio_test_unevictable(folio)) { 348 struct folio_batch *fbatch; 349 350 folio_get(folio); 351 local_lock(&cpu_fbatches.lock); 352 fbatch = this_cpu_ptr(&cpu_fbatches.activate); 353 folio_batch_add_and_move(fbatch, folio, folio_activate_fn); 354 local_unlock(&cpu_fbatches.lock); 355 } 356 } 357 358 #else 359 static inline void folio_activate_drain(int cpu) 360 { 361 } 362 363 void folio_activate(struct folio *folio) 364 { 365 struct lruvec *lruvec; 366 367 if (folio_test_clear_lru(folio)) { 368 lruvec = folio_lruvec_lock_irq(folio); 369 folio_activate_fn(lruvec, folio); 370 unlock_page_lruvec_irq(lruvec); 371 folio_set_lru(folio); 372 } 373 } 374 #endif 375 376 static void __lru_cache_activate_folio(struct folio *folio) 377 { 378 struct folio_batch *fbatch; 379 int i; 380 381 local_lock(&cpu_fbatches.lock); 382 fbatch = this_cpu_ptr(&cpu_fbatches.lru_add); 383 384 /* 385 * Search backwards on the optimistic assumption that the folio being 386 * activated has just been added to this batch. Note that only 387 * the local batch is examined as a !LRU folio could be in the 388 * process of being released, reclaimed, migrated or on a remote 389 * batch that is currently being drained. Furthermore, marking 390 * a remote batch's folio active potentially hits a race where 391 * a folio is marked active just after it is added to the inactive 392 * list causing accounting errors and BUG_ON checks to trigger. 393 */ 394 for (i = folio_batch_count(fbatch) - 1; i >= 0; i--) { 395 struct folio *batch_folio = fbatch->folios[i]; 396 397 if (batch_folio == folio) { 398 folio_set_active(folio); 399 break; 400 } 401 } 402 403 local_unlock(&cpu_fbatches.lock); 404 } 405 406 #ifdef CONFIG_LRU_GEN 407 static void folio_inc_refs(struct folio *folio) 408 { 409 unsigned long new_flags, old_flags = READ_ONCE(folio->flags); 410 411 if (folio_test_unevictable(folio)) 412 return; 413 414 if (!folio_test_referenced(folio)) { 415 folio_set_referenced(folio); 416 return; 417 } 418 419 if (!folio_test_workingset(folio)) { 420 folio_set_workingset(folio); 421 return; 422 } 423 424 /* see the comment on MAX_NR_TIERS */ 425 do { 426 new_flags = old_flags & LRU_REFS_MASK; 427 if (new_flags == LRU_REFS_MASK) 428 break; 429 430 new_flags += BIT(LRU_REFS_PGOFF); 431 new_flags |= old_flags & ~LRU_REFS_MASK; 432 } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags)); 433 } 434 #else 435 static void folio_inc_refs(struct folio *folio) 436 { 437 } 438 #endif /* CONFIG_LRU_GEN */ 439 440 /* 441 * Mark a page as having seen activity. 442 * 443 * inactive,unreferenced -> inactive,referenced 444 * inactive,referenced -> active,unreferenced 445 * active,unreferenced -> active,referenced 446 * 447 * When a newly allocated page is not yet visible, so safe for non-atomic ops, 448 * __SetPageReferenced(page) may be substituted for mark_page_accessed(page). 449 */ 450 void folio_mark_accessed(struct folio *folio) 451 { 452 if (lru_gen_enabled()) { 453 folio_inc_refs(folio); 454 return; 455 } 456 457 if (!folio_test_referenced(folio)) { 458 folio_set_referenced(folio); 459 } else if (folio_test_unevictable(folio)) { 460 /* 461 * Unevictable pages are on the "LRU_UNEVICTABLE" list. But, 462 * this list is never rotated or maintained, so marking an 463 * unevictable page accessed has no effect. 464 */ 465 } else if (!folio_test_active(folio)) { 466 /* 467 * If the folio is on the LRU, queue it for activation via 468 * cpu_fbatches.activate. Otherwise, assume the folio is in a 469 * folio_batch, mark it active and it'll be moved to the active 470 * LRU on the next drain. 471 */ 472 if (folio_test_lru(folio)) 473 folio_activate(folio); 474 else 475 __lru_cache_activate_folio(folio); 476 folio_clear_referenced(folio); 477 workingset_activation(folio); 478 } 479 if (folio_test_idle(folio)) 480 folio_clear_idle(folio); 481 } 482 EXPORT_SYMBOL(folio_mark_accessed); 483 484 /** 485 * folio_add_lru - Add a folio to an LRU list. 486 * @folio: The folio to be added to the LRU. 487 * 488 * Queue the folio for addition to the LRU. The decision on whether 489 * to add the page to the [in]active [file|anon] list is deferred until the 490 * folio_batch is drained. This gives a chance for the caller of folio_add_lru() 491 * have the folio added to the active list using folio_mark_accessed(). 492 */ 493 void folio_add_lru(struct folio *folio) 494 { 495 struct folio_batch *fbatch; 496 497 VM_BUG_ON_FOLIO(folio_test_active(folio) && 498 folio_test_unevictable(folio), folio); 499 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); 500 501 /* see the comment in lru_gen_add_folio() */ 502 if (lru_gen_enabled() && !folio_test_unevictable(folio) && 503 lru_gen_in_fault() && !(current->flags & PF_MEMALLOC)) 504 folio_set_active(folio); 505 506 folio_get(folio); 507 local_lock(&cpu_fbatches.lock); 508 fbatch = this_cpu_ptr(&cpu_fbatches.lru_add); 509 folio_batch_add_and_move(fbatch, folio, lru_add_fn); 510 local_unlock(&cpu_fbatches.lock); 511 } 512 EXPORT_SYMBOL(folio_add_lru); 513 514 /** 515 * folio_add_lru_vma() - Add a folio to the appropate LRU list for this VMA. 516 * @folio: The folio to be added to the LRU. 517 * @vma: VMA in which the folio is mapped. 518 * 519 * If the VMA is mlocked, @folio is added to the unevictable list. 520 * Otherwise, it is treated the same way as folio_add_lru(). 521 */ 522 void folio_add_lru_vma(struct folio *folio, struct vm_area_struct *vma) 523 { 524 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); 525 526 if (unlikely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED)) 527 mlock_new_folio(folio); 528 else 529 folio_add_lru(folio); 530 } 531 532 /* 533 * If the folio cannot be invalidated, it is moved to the 534 * inactive list to speed up its reclaim. It is moved to the 535 * head of the list, rather than the tail, to give the flusher 536 * threads some time to write it out, as this is much more 537 * effective than the single-page writeout from reclaim. 538 * 539 * If the folio isn't mapped and dirty/writeback, the folio 540 * could be reclaimed asap using the reclaim flag. 541 * 542 * 1. active, mapped folio -> none 543 * 2. active, dirty/writeback folio -> inactive, head, reclaim 544 * 3. inactive, mapped folio -> none 545 * 4. inactive, dirty/writeback folio -> inactive, head, reclaim 546 * 5. inactive, clean -> inactive, tail 547 * 6. Others -> none 548 * 549 * In 4, it moves to the head of the inactive list so the folio is 550 * written out by flusher threads as this is much more efficient 551 * than the single-page writeout from reclaim. 552 */ 553 static void lru_deactivate_file_fn(struct lruvec *lruvec, struct folio *folio) 554 { 555 bool active = folio_test_active(folio); 556 long nr_pages = folio_nr_pages(folio); 557 558 if (folio_test_unevictable(folio)) 559 return; 560 561 /* Some processes are using the folio */ 562 if (folio_mapped(folio)) 563 return; 564 565 lruvec_del_folio(lruvec, folio); 566 folio_clear_active(folio); 567 folio_clear_referenced(folio); 568 569 if (folio_test_writeback(folio) || folio_test_dirty(folio)) { 570 /* 571 * Setting the reclaim flag could race with 572 * folio_end_writeback() and confuse readahead. But the 573 * race window is _really_ small and it's not a critical 574 * problem. 575 */ 576 lruvec_add_folio(lruvec, folio); 577 folio_set_reclaim(folio); 578 } else { 579 /* 580 * The folio's writeback ended while it was in the batch. 581 * We move that folio to the tail of the inactive list. 582 */ 583 lruvec_add_folio_tail(lruvec, folio); 584 __count_vm_events(PGROTATED, nr_pages); 585 } 586 587 if (active) { 588 __count_vm_events(PGDEACTIVATE, nr_pages); 589 __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, 590 nr_pages); 591 } 592 } 593 594 static void lru_deactivate_fn(struct lruvec *lruvec, struct folio *folio) 595 { 596 if (!folio_test_unevictable(folio) && (folio_test_active(folio) || lru_gen_enabled())) { 597 long nr_pages = folio_nr_pages(folio); 598 599 lruvec_del_folio(lruvec, folio); 600 folio_clear_active(folio); 601 folio_clear_referenced(folio); 602 lruvec_add_folio(lruvec, folio); 603 604 __count_vm_events(PGDEACTIVATE, nr_pages); 605 __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, 606 nr_pages); 607 } 608 } 609 610 static void lru_lazyfree_fn(struct lruvec *lruvec, struct folio *folio) 611 { 612 if (folio_test_anon(folio) && folio_test_swapbacked(folio) && 613 !folio_test_swapcache(folio) && !folio_test_unevictable(folio)) { 614 long nr_pages = folio_nr_pages(folio); 615 616 lruvec_del_folio(lruvec, folio); 617 folio_clear_active(folio); 618 folio_clear_referenced(folio); 619 /* 620 * Lazyfree folios are clean anonymous folios. They have 621 * the swapbacked flag cleared, to distinguish them from normal 622 * anonymous folios 623 */ 624 folio_clear_swapbacked(folio); 625 lruvec_add_folio(lruvec, folio); 626 627 __count_vm_events(PGLAZYFREE, nr_pages); 628 __count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE, 629 nr_pages); 630 } 631 } 632 633 /* 634 * Drain pages out of the cpu's folio_batch. 635 * Either "cpu" is the current CPU, and preemption has already been 636 * disabled; or "cpu" is being hot-unplugged, and is already dead. 637 */ 638 void lru_add_drain_cpu(int cpu) 639 { 640 struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu); 641 struct folio_batch *fbatch = &fbatches->lru_add; 642 643 if (folio_batch_count(fbatch)) 644 folio_batch_move_lru(fbatch, lru_add_fn); 645 646 fbatch = &per_cpu(lru_rotate.fbatch, cpu); 647 /* Disabling interrupts below acts as a compiler barrier. */ 648 if (data_race(folio_batch_count(fbatch))) { 649 unsigned long flags; 650 651 /* No harm done if a racing interrupt already did this */ 652 local_lock_irqsave(&lru_rotate.lock, flags); 653 folio_batch_move_lru(fbatch, lru_move_tail_fn); 654 local_unlock_irqrestore(&lru_rotate.lock, flags); 655 } 656 657 fbatch = &fbatches->lru_deactivate_file; 658 if (folio_batch_count(fbatch)) 659 folio_batch_move_lru(fbatch, lru_deactivate_file_fn); 660 661 fbatch = &fbatches->lru_deactivate; 662 if (folio_batch_count(fbatch)) 663 folio_batch_move_lru(fbatch, lru_deactivate_fn); 664 665 fbatch = &fbatches->lru_lazyfree; 666 if (folio_batch_count(fbatch)) 667 folio_batch_move_lru(fbatch, lru_lazyfree_fn); 668 669 folio_activate_drain(cpu); 670 } 671 672 /** 673 * deactivate_file_folio() - Deactivate a file folio. 674 * @folio: Folio to deactivate. 675 * 676 * This function hints to the VM that @folio is a good reclaim candidate, 677 * for example if its invalidation fails due to the folio being dirty 678 * or under writeback. 679 * 680 * Context: Caller holds a reference on the folio. 681 */ 682 void deactivate_file_folio(struct folio *folio) 683 { 684 struct folio_batch *fbatch; 685 686 /* Deactivating an unevictable folio will not accelerate reclaim */ 687 if (folio_test_unevictable(folio)) 688 return; 689 690 folio_get(folio); 691 local_lock(&cpu_fbatches.lock); 692 fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate_file); 693 folio_batch_add_and_move(fbatch, folio, lru_deactivate_file_fn); 694 local_unlock(&cpu_fbatches.lock); 695 } 696 697 /* 698 * folio_deactivate - deactivate a folio 699 * @folio: folio to deactivate 700 * 701 * folio_deactivate() moves @folio to the inactive list if @folio was on the 702 * active list and was not unevictable. This is done to accelerate the 703 * reclaim of @folio. 704 */ 705 void folio_deactivate(struct folio *folio) 706 { 707 if (folio_test_lru(folio) && !folio_test_unevictable(folio) && 708 (folio_test_active(folio) || lru_gen_enabled())) { 709 struct folio_batch *fbatch; 710 711 folio_get(folio); 712 local_lock(&cpu_fbatches.lock); 713 fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate); 714 folio_batch_add_and_move(fbatch, folio, lru_deactivate_fn); 715 local_unlock(&cpu_fbatches.lock); 716 } 717 } 718 719 /** 720 * folio_mark_lazyfree - make an anon folio lazyfree 721 * @folio: folio to deactivate 722 * 723 * folio_mark_lazyfree() moves @folio to the inactive file list. 724 * This is done to accelerate the reclaim of @folio. 725 */ 726 void folio_mark_lazyfree(struct folio *folio) 727 { 728 if (folio_test_lru(folio) && folio_test_anon(folio) && 729 folio_test_swapbacked(folio) && !folio_test_swapcache(folio) && 730 !folio_test_unevictable(folio)) { 731 struct folio_batch *fbatch; 732 733 folio_get(folio); 734 local_lock(&cpu_fbatches.lock); 735 fbatch = this_cpu_ptr(&cpu_fbatches.lru_lazyfree); 736 folio_batch_add_and_move(fbatch, folio, lru_lazyfree_fn); 737 local_unlock(&cpu_fbatches.lock); 738 } 739 } 740 741 void lru_add_drain(void) 742 { 743 local_lock(&cpu_fbatches.lock); 744 lru_add_drain_cpu(smp_processor_id()); 745 local_unlock(&cpu_fbatches.lock); 746 mlock_drain_local(); 747 } 748 749 /* 750 * It's called from per-cpu workqueue context in SMP case so 751 * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on 752 * the same cpu. It shouldn't be a problem in !SMP case since 753 * the core is only one and the locks will disable preemption. 754 */ 755 static void lru_add_and_bh_lrus_drain(void) 756 { 757 local_lock(&cpu_fbatches.lock); 758 lru_add_drain_cpu(smp_processor_id()); 759 local_unlock(&cpu_fbatches.lock); 760 invalidate_bh_lrus_cpu(); 761 mlock_drain_local(); 762 } 763 764 void lru_add_drain_cpu_zone(struct zone *zone) 765 { 766 local_lock(&cpu_fbatches.lock); 767 lru_add_drain_cpu(smp_processor_id()); 768 drain_local_pages(zone); 769 local_unlock(&cpu_fbatches.lock); 770 mlock_drain_local(); 771 } 772 773 #ifdef CONFIG_SMP 774 775 static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work); 776 777 static void lru_add_drain_per_cpu(struct work_struct *dummy) 778 { 779 lru_add_and_bh_lrus_drain(); 780 } 781 782 static bool cpu_needs_drain(unsigned int cpu) 783 { 784 struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu); 785 786 /* Check these in order of likelihood that they're not zero */ 787 return folio_batch_count(&fbatches->lru_add) || 788 data_race(folio_batch_count(&per_cpu(lru_rotate.fbatch, cpu))) || 789 folio_batch_count(&fbatches->lru_deactivate_file) || 790 folio_batch_count(&fbatches->lru_deactivate) || 791 folio_batch_count(&fbatches->lru_lazyfree) || 792 folio_batch_count(&fbatches->activate) || 793 need_mlock_drain(cpu) || 794 has_bh_in_lru(cpu, NULL); 795 } 796 797 /* 798 * Doesn't need any cpu hotplug locking because we do rely on per-cpu 799 * kworkers being shut down before our page_alloc_cpu_dead callback is 800 * executed on the offlined cpu. 801 * Calling this function with cpu hotplug locks held can actually lead 802 * to obscure indirect dependencies via WQ context. 803 */ 804 static inline void __lru_add_drain_all(bool force_all_cpus) 805 { 806 /* 807 * lru_drain_gen - Global pages generation number 808 * 809 * (A) Definition: global lru_drain_gen = x implies that all generations 810 * 0 < n <= x are already *scheduled* for draining. 811 * 812 * This is an optimization for the highly-contended use case where a 813 * user space workload keeps constantly generating a flow of pages for 814 * each CPU. 815 */ 816 static unsigned int lru_drain_gen; 817 static struct cpumask has_work; 818 static DEFINE_MUTEX(lock); 819 unsigned cpu, this_gen; 820 821 /* 822 * Make sure nobody triggers this path before mm_percpu_wq is fully 823 * initialized. 824 */ 825 if (WARN_ON(!mm_percpu_wq)) 826 return; 827 828 /* 829 * Guarantee folio_batch counter stores visible by this CPU 830 * are visible to other CPUs before loading the current drain 831 * generation. 832 */ 833 smp_mb(); 834 835 /* 836 * (B) Locally cache global LRU draining generation number 837 * 838 * The read barrier ensures that the counter is loaded before the mutex 839 * is taken. It pairs with smp_mb() inside the mutex critical section 840 * at (D). 841 */ 842 this_gen = smp_load_acquire(&lru_drain_gen); 843 844 mutex_lock(&lock); 845 846 /* 847 * (C) Exit the draining operation if a newer generation, from another 848 * lru_add_drain_all(), was already scheduled for draining. Check (A). 849 */ 850 if (unlikely(this_gen != lru_drain_gen && !force_all_cpus)) 851 goto done; 852 853 /* 854 * (D) Increment global generation number 855 * 856 * Pairs with smp_load_acquire() at (B), outside of the critical 857 * section. Use a full memory barrier to guarantee that the 858 * new global drain generation number is stored before loading 859 * folio_batch counters. 860 * 861 * This pairing must be done here, before the for_each_online_cpu loop 862 * below which drains the page vectors. 863 * 864 * Let x, y, and z represent some system CPU numbers, where x < y < z. 865 * Assume CPU #z is in the middle of the for_each_online_cpu loop 866 * below and has already reached CPU #y's per-cpu data. CPU #x comes 867 * along, adds some pages to its per-cpu vectors, then calls 868 * lru_add_drain_all(). 869 * 870 * If the paired barrier is done at any later step, e.g. after the 871 * loop, CPU #x will just exit at (C) and miss flushing out all of its 872 * added pages. 873 */ 874 WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1); 875 smp_mb(); 876 877 cpumask_clear(&has_work); 878 for_each_online_cpu(cpu) { 879 struct work_struct *work = &per_cpu(lru_add_drain_work, cpu); 880 881 if (cpu_needs_drain(cpu)) { 882 INIT_WORK(work, lru_add_drain_per_cpu); 883 queue_work_on(cpu, mm_percpu_wq, work); 884 __cpumask_set_cpu(cpu, &has_work); 885 } 886 } 887 888 for_each_cpu(cpu, &has_work) 889 flush_work(&per_cpu(lru_add_drain_work, cpu)); 890 891 done: 892 mutex_unlock(&lock); 893 } 894 895 void lru_add_drain_all(void) 896 { 897 __lru_add_drain_all(false); 898 } 899 #else 900 void lru_add_drain_all(void) 901 { 902 lru_add_drain(); 903 } 904 #endif /* CONFIG_SMP */ 905 906 atomic_t lru_disable_count = ATOMIC_INIT(0); 907 908 /* 909 * lru_cache_disable() needs to be called before we start compiling 910 * a list of pages to be migrated using isolate_lru_page(). 911 * It drains pages on LRU cache and then disable on all cpus until 912 * lru_cache_enable is called. 913 * 914 * Must be paired with a call to lru_cache_enable(). 915 */ 916 void lru_cache_disable(void) 917 { 918 atomic_inc(&lru_disable_count); 919 /* 920 * Readers of lru_disable_count are protected by either disabling 921 * preemption or rcu_read_lock: 922 * 923 * preempt_disable, local_irq_disable [bh_lru_lock()] 924 * rcu_read_lock [rt_spin_lock CONFIG_PREEMPT_RT] 925 * preempt_disable [local_lock !CONFIG_PREEMPT_RT] 926 * 927 * Since v5.1 kernel, synchronize_rcu() is guaranteed to wait on 928 * preempt_disable() regions of code. So any CPU which sees 929 * lru_disable_count = 0 will have exited the critical 930 * section when synchronize_rcu() returns. 931 */ 932 synchronize_rcu_expedited(); 933 #ifdef CONFIG_SMP 934 __lru_add_drain_all(true); 935 #else 936 lru_add_and_bh_lrus_drain(); 937 #endif 938 } 939 940 /** 941 * release_pages - batched put_page() 942 * @arg: array of pages to release 943 * @nr: number of pages 944 * 945 * Decrement the reference count on all the pages in @arg. If it 946 * fell to zero, remove the page from the LRU and free it. 947 * 948 * Note that the argument can be an array of pages, encoded pages, 949 * or folio pointers. We ignore any encoded bits, and turn any of 950 * them into just a folio that gets free'd. 951 */ 952 void release_pages(release_pages_arg arg, int nr) 953 { 954 int i; 955 struct encoded_page **encoded = arg.encoded_pages; 956 LIST_HEAD(pages_to_free); 957 struct lruvec *lruvec = NULL; 958 unsigned long flags = 0; 959 unsigned int lock_batch; 960 961 for (i = 0; i < nr; i++) { 962 struct folio *folio; 963 964 /* Turn any of the argument types into a folio */ 965 folio = page_folio(encoded_page_ptr(encoded[i])); 966 967 /* 968 * Make sure the IRQ-safe lock-holding time does not get 969 * excessive with a continuous string of pages from the 970 * same lruvec. The lock is held only if lruvec != NULL. 971 */ 972 if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) { 973 unlock_page_lruvec_irqrestore(lruvec, flags); 974 lruvec = NULL; 975 } 976 977 if (is_huge_zero_page(&folio->page)) 978 continue; 979 980 if (folio_is_zone_device(folio)) { 981 if (lruvec) { 982 unlock_page_lruvec_irqrestore(lruvec, flags); 983 lruvec = NULL; 984 } 985 if (put_devmap_managed_page(&folio->page)) 986 continue; 987 if (folio_put_testzero(folio)) 988 free_zone_device_page(&folio->page); 989 continue; 990 } 991 992 if (!folio_put_testzero(folio)) 993 continue; 994 995 if (folio_test_large(folio)) { 996 if (lruvec) { 997 unlock_page_lruvec_irqrestore(lruvec, flags); 998 lruvec = NULL; 999 } 1000 __folio_put_large(folio); 1001 continue; 1002 } 1003 1004 if (folio_test_lru(folio)) { 1005 struct lruvec *prev_lruvec = lruvec; 1006 1007 lruvec = folio_lruvec_relock_irqsave(folio, lruvec, 1008 &flags); 1009 if (prev_lruvec != lruvec) 1010 lock_batch = 0; 1011 1012 lruvec_del_folio(lruvec, folio); 1013 __folio_clear_lru_flags(folio); 1014 } 1015 1016 list_add(&folio->lru, &pages_to_free); 1017 } 1018 if (lruvec) 1019 unlock_page_lruvec_irqrestore(lruvec, flags); 1020 1021 mem_cgroup_uncharge_list(&pages_to_free); 1022 free_unref_page_list(&pages_to_free); 1023 } 1024 EXPORT_SYMBOL(release_pages); 1025 1026 /* 1027 * The folios which we're about to release may be in the deferred lru-addition 1028 * queues. That would prevent them from really being freed right now. That's 1029 * OK from a correctness point of view but is inefficient - those folios may be 1030 * cache-warm and we want to give them back to the page allocator ASAP. 1031 * 1032 * So __folio_batch_release() will drain those queues here. 1033 * folio_batch_move_lru() calls folios_put() directly to avoid 1034 * mutual recursion. 1035 */ 1036 void __folio_batch_release(struct folio_batch *fbatch) 1037 { 1038 if (!fbatch->percpu_pvec_drained) { 1039 lru_add_drain(); 1040 fbatch->percpu_pvec_drained = true; 1041 } 1042 release_pages(fbatch->folios, folio_batch_count(fbatch)); 1043 folio_batch_reinit(fbatch); 1044 } 1045 EXPORT_SYMBOL(__folio_batch_release); 1046 1047 /** 1048 * folio_batch_remove_exceptionals() - Prune non-folios from a batch. 1049 * @fbatch: The batch to prune 1050 * 1051 * find_get_entries() fills a batch with both folios and shadow/swap/DAX 1052 * entries. This function prunes all the non-folio entries from @fbatch 1053 * without leaving holes, so that it can be passed on to folio-only batch 1054 * operations. 1055 */ 1056 void folio_batch_remove_exceptionals(struct folio_batch *fbatch) 1057 { 1058 unsigned int i, j; 1059 1060 for (i = 0, j = 0; i < folio_batch_count(fbatch); i++) { 1061 struct folio *folio = fbatch->folios[i]; 1062 if (!xa_is_value(folio)) 1063 fbatch->folios[j++] = folio; 1064 } 1065 fbatch->nr = j; 1066 } 1067 1068 /* 1069 * Perform any setup for the swap system 1070 */ 1071 void __init swap_setup(void) 1072 { 1073 unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT); 1074 1075 /* Use a smaller cluster for small-memory machines */ 1076 if (megs < 16) 1077 page_cluster = 2; 1078 else 1079 page_cluster = 3; 1080 /* 1081 * Right now other parts of the system means that we 1082 * _really_ don't want to cluster much more 1083 */ 1084 } 1085