1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
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.
13 * Swap aging added 23.2.95, Stephen Tweedie.
14 * Buffermem limits added 12.3.98, Rik van Riel.
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>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/pagemap.h>
46 /* How many pages do we try to swap or page in/out together? As a power of 2 */
48 const int page_cluster_max = 31;
52 * The following folio batches are grouped together because they are protected
53 * by disabling preemption (and interrupts remain enabled).
56 struct folio_batch lru_add;
57 struct folio_batch lru_deactivate_file;
58 struct folio_batch lru_deactivate;
59 struct folio_batch lru_lazyfree;
61 struct folio_batch lru_activate;
63 /* Protecting the following batches which require disabling interrupts */
64 local_lock_t lock_irq;
65 struct folio_batch lru_move_tail;
68 static DEFINE_PER_CPU(struct cpu_fbatches, cpu_fbatches) = {
69 .lock = INIT_LOCAL_LOCK(lock),
70 .lock_irq = INIT_LOCAL_LOCK(lock_irq),
73 static void __page_cache_release(struct folio *folio, struct lruvec **lruvecp,
74 unsigned long *flagsp)
76 if (folio_test_lru(folio)) {
77 folio_lruvec_relock_irqsave(folio, lruvecp, flagsp);
78 lruvec_del_folio(*lruvecp, folio);
79 __folio_clear_lru_flags(folio);
84 * This path almost never happens for VM activity - pages are normally freed
85 * in batches. But it gets used by networking - and for compound pages.
87 static void page_cache_release(struct folio *folio)
89 struct lruvec *lruvec = NULL;
92 __page_cache_release(folio, &lruvec, &flags);
94 unlock_page_lruvec_irqrestore(lruvec, flags);
97 void __folio_put(struct folio *folio)
99 if (unlikely(folio_is_zone_device(folio))) {
100 free_zone_device_folio(folio);
104 if (folio_test_hugetlb(folio)) {
105 free_huge_folio(folio);
109 page_cache_release(folio);
110 folio_unqueue_deferred_split(folio);
111 mem_cgroup_uncharge(folio);
112 free_unref_page(&folio->page, folio_order(folio));
114 EXPORT_SYMBOL(__folio_put);
117 * put_pages_list() - release a list of pages
118 * @pages: list of pages threaded on page->lru
120 * Release a list of pages which are strung together on page.lru.
122 void put_pages_list(struct list_head *pages)
124 struct folio_batch fbatch;
125 struct folio *folio, *next;
127 folio_batch_init(&fbatch);
128 list_for_each_entry_safe(folio, next, pages, lru) {
129 if (!folio_put_testzero(folio))
131 if (folio_test_hugetlb(folio)) {
132 free_huge_folio(folio);
135 /* LRU flag must be clear because it's passed using the lru */
136 if (folio_batch_add(&fbatch, folio) > 0)
138 free_unref_folios(&fbatch);
142 free_unref_folios(&fbatch);
143 INIT_LIST_HEAD(pages);
145 EXPORT_SYMBOL(put_pages_list);
147 typedef void (*move_fn_t)(struct lruvec *lruvec, struct folio *folio);
149 static void lru_add(struct lruvec *lruvec, struct folio *folio)
151 int was_unevictable = folio_test_clear_unevictable(folio);
152 long nr_pages = folio_nr_pages(folio);
154 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
157 * Is an smp_mb__after_atomic() still required here, before
158 * folio_evictable() tests the mlocked flag, to rule out the possibility
159 * of stranding an evictable folio on an unevictable LRU? I think
160 * not, because __munlock_folio() only clears the mlocked flag
161 * while the LRU lock is held.
163 * (That is not true of __page_cache_release(), and not necessarily
164 * true of folios_put(): but those only clear the mlocked flag after
165 * folio_put_testzero() has excluded any other users of the folio.)
167 if (folio_evictable(folio)) {
169 __count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages);
171 folio_clear_active(folio);
172 folio_set_unevictable(folio);
174 * folio->mlock_count = !!folio_test_mlocked(folio)?
175 * But that leaves __mlock_folio() in doubt whether another
176 * actor has already counted the mlock or not. Err on the
177 * safe side, underestimate, let page reclaim fix it, rather
178 * than leaving a page on the unevictable LRU indefinitely.
180 folio->mlock_count = 0;
181 if (!was_unevictable)
182 __count_vm_events(UNEVICTABLE_PGCULLED, nr_pages);
185 lruvec_add_folio(lruvec, folio);
186 trace_mm_lru_insertion(folio);
189 static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn)
192 struct lruvec *lruvec = NULL;
193 unsigned long flags = 0;
195 for (i = 0; i < folio_batch_count(fbatch); i++) {
196 struct folio *folio = fbatch->folios[i];
198 folio_lruvec_relock_irqsave(folio, &lruvec, &flags);
199 move_fn(lruvec, folio);
201 folio_set_lru(folio);
205 unlock_page_lruvec_irqrestore(lruvec, flags);
209 static void __folio_batch_add_and_move(struct folio_batch __percpu *fbatch,
210 struct folio *folio, move_fn_t move_fn,
211 bool on_lru, bool disable_irq)
215 if (on_lru && !folio_test_clear_lru(folio))
221 local_lock_irqsave(&cpu_fbatches.lock_irq, flags);
223 local_lock(&cpu_fbatches.lock);
225 if (!folio_batch_add(this_cpu_ptr(fbatch), folio) || folio_test_large(folio) ||
226 lru_cache_disabled())
227 folio_batch_move_lru(this_cpu_ptr(fbatch), move_fn);
230 local_unlock_irqrestore(&cpu_fbatches.lock_irq, flags);
232 local_unlock(&cpu_fbatches.lock);
235 #define folio_batch_add_and_move(folio, op, on_lru) \
236 __folio_batch_add_and_move( \
241 offsetof(struct cpu_fbatches, op) >= offsetof(struct cpu_fbatches, lock_irq) \
244 static void lru_move_tail(struct lruvec *lruvec, struct folio *folio)
246 if (folio_test_unevictable(folio))
249 lruvec_del_folio(lruvec, folio);
250 folio_clear_active(folio);
251 lruvec_add_folio_tail(lruvec, folio);
252 __count_vm_events(PGROTATED, folio_nr_pages(folio));
256 * Writeback is about to end against a folio which has been marked for
257 * immediate reclaim. If it still appears to be reclaimable, move it
258 * to the tail of the inactive list.
260 * folio_rotate_reclaimable() must disable IRQs, to prevent nasty races.
262 void folio_rotate_reclaimable(struct folio *folio)
264 if (folio_test_locked(folio) || folio_test_dirty(folio) ||
265 folio_test_unevictable(folio))
268 folio_batch_add_and_move(folio, lru_move_tail, true);
271 void lru_note_cost(struct lruvec *lruvec, bool file,
272 unsigned int nr_io, unsigned int nr_rotated)
277 * Reflect the relative cost of incurring IO and spending CPU
278 * time on rotations. This doesn't attempt to make a precise
279 * comparison, it just says: if reloads are about comparable
280 * between the LRU lists, or rotations are overwhelmingly
281 * different between them, adjust scan balance for CPU work.
283 cost = nr_io * SWAP_CLUSTER_MAX + nr_rotated;
286 unsigned long lrusize;
289 * Hold lruvec->lru_lock is safe here, since
290 * 1) The pinned lruvec in reclaim, or
291 * 2) From a pre-LRU page during refault (which also holds the
292 * rcu lock, so would be safe even if the page was on the LRU
293 * and could move simultaneously to a new lruvec).
295 spin_lock_irq(&lruvec->lru_lock);
296 /* Record cost event */
298 lruvec->file_cost += cost;
300 lruvec->anon_cost += cost;
303 * Decay previous events
305 * Because workloads change over time (and to avoid
306 * overflow) we keep these statistics as a floating
307 * average, which ends up weighing recent refaults
308 * more than old ones.
310 lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) +
311 lruvec_page_state(lruvec, NR_ACTIVE_ANON) +
312 lruvec_page_state(lruvec, NR_INACTIVE_FILE) +
313 lruvec_page_state(lruvec, NR_ACTIVE_FILE);
315 if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) {
316 lruvec->file_cost /= 2;
317 lruvec->anon_cost /= 2;
319 spin_unlock_irq(&lruvec->lru_lock);
320 } while ((lruvec = parent_lruvec(lruvec)));
323 void lru_note_cost_refault(struct folio *folio)
325 lru_note_cost(folio_lruvec(folio), folio_is_file_lru(folio),
326 folio_nr_pages(folio), 0);
329 static void lru_activate(struct lruvec *lruvec, struct folio *folio)
331 long nr_pages = folio_nr_pages(folio);
333 if (folio_test_active(folio) || folio_test_unevictable(folio))
337 lruvec_del_folio(lruvec, folio);
338 folio_set_active(folio);
339 lruvec_add_folio(lruvec, folio);
340 trace_mm_lru_activate(folio);
342 __count_vm_events(PGACTIVATE, nr_pages);
343 __count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE, nr_pages);
347 static void folio_activate_drain(int cpu)
349 struct folio_batch *fbatch = &per_cpu(cpu_fbatches.lru_activate, cpu);
351 if (folio_batch_count(fbatch))
352 folio_batch_move_lru(fbatch, lru_activate);
355 void folio_activate(struct folio *folio)
357 if (folio_test_active(folio) || folio_test_unevictable(folio))
360 folio_batch_add_and_move(folio, lru_activate, true);
364 static inline void folio_activate_drain(int cpu)
368 void folio_activate(struct folio *folio)
370 struct lruvec *lruvec;
372 if (!folio_test_clear_lru(folio))
375 lruvec = folio_lruvec_lock_irq(folio);
376 lru_activate(lruvec, folio);
377 unlock_page_lruvec_irq(lruvec);
378 folio_set_lru(folio);
382 static void __lru_cache_activate_folio(struct folio *folio)
384 struct folio_batch *fbatch;
387 local_lock(&cpu_fbatches.lock);
388 fbatch = this_cpu_ptr(&cpu_fbatches.lru_add);
391 * Search backwards on the optimistic assumption that the folio being
392 * activated has just been added to this batch. Note that only
393 * the local batch is examined as a !LRU folio could be in the
394 * process of being released, reclaimed, migrated or on a remote
395 * batch that is currently being drained. Furthermore, marking
396 * a remote batch's folio active potentially hits a race where
397 * a folio is marked active just after it is added to the inactive
398 * list causing accounting errors and BUG_ON checks to trigger.
400 for (i = folio_batch_count(fbatch) - 1; i >= 0; i--) {
401 struct folio *batch_folio = fbatch->folios[i];
403 if (batch_folio == folio) {
404 folio_set_active(folio);
409 local_unlock(&cpu_fbatches.lock);
412 #ifdef CONFIG_LRU_GEN
413 static void folio_inc_refs(struct folio *folio)
415 unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
417 if (folio_test_unevictable(folio))
420 if (!folio_test_referenced(folio)) {
421 folio_set_referenced(folio);
425 if (!folio_test_workingset(folio)) {
426 folio_set_workingset(folio);
430 /* see the comment on MAX_NR_TIERS */
432 new_flags = old_flags & LRU_REFS_MASK;
433 if (new_flags == LRU_REFS_MASK)
436 new_flags += BIT(LRU_REFS_PGOFF);
437 new_flags |= old_flags & ~LRU_REFS_MASK;
438 } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags));
441 static void folio_inc_refs(struct folio *folio)
444 #endif /* CONFIG_LRU_GEN */
447 * folio_mark_accessed - Mark a folio as having seen activity.
448 * @folio: The folio to mark.
450 * This function will perform one of the following transitions:
452 * * inactive,unreferenced -> inactive,referenced
453 * * inactive,referenced -> active,unreferenced
454 * * active,unreferenced -> active,referenced
456 * When a newly allocated folio is not yet visible, so safe for non-atomic ops,
457 * __folio_set_referenced() may be substituted for folio_mark_accessed().
459 void folio_mark_accessed(struct folio *folio)
461 if (lru_gen_enabled()) {
462 folio_inc_refs(folio);
466 if (!folio_test_referenced(folio)) {
467 folio_set_referenced(folio);
468 } else if (folio_test_unevictable(folio)) {
470 * Unevictable pages are on the "LRU_UNEVICTABLE" list. But,
471 * this list is never rotated or maintained, so marking an
472 * unevictable page accessed has no effect.
474 } else if (!folio_test_active(folio)) {
476 * If the folio is on the LRU, queue it for activation via
477 * cpu_fbatches.lru_activate. Otherwise, assume the folio is in a
478 * folio_batch, mark it active and it'll be moved to the active
479 * LRU on the next drain.
481 if (folio_test_lru(folio))
482 folio_activate(folio);
484 __lru_cache_activate_folio(folio);
485 folio_clear_referenced(folio);
486 workingset_activation(folio);
488 if (folio_test_idle(folio))
489 folio_clear_idle(folio);
491 EXPORT_SYMBOL(folio_mark_accessed);
494 * folio_add_lru - Add a folio to an LRU list.
495 * @folio: The folio to be added to the LRU.
497 * Queue the folio for addition to the LRU. The decision on whether
498 * to add the page to the [in]active [file|anon] list is deferred until the
499 * folio_batch is drained. This gives a chance for the caller of folio_add_lru()
500 * have the folio added to the active list using folio_mark_accessed().
502 void folio_add_lru(struct folio *folio)
504 VM_BUG_ON_FOLIO(folio_test_active(folio) &&
505 folio_test_unevictable(folio), folio);
506 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
508 /* see the comment in lru_gen_add_folio() */
509 if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
510 lru_gen_in_fault() && !(current->flags & PF_MEMALLOC))
511 folio_set_active(folio);
513 folio_batch_add_and_move(folio, lru_add, false);
515 EXPORT_SYMBOL(folio_add_lru);
518 * folio_add_lru_vma() - Add a folio to the appropate LRU list for this VMA.
519 * @folio: The folio to be added to the LRU.
520 * @vma: VMA in which the folio is mapped.
522 * If the VMA is mlocked, @folio is added to the unevictable list.
523 * Otherwise, it is treated the same way as folio_add_lru().
525 void folio_add_lru_vma(struct folio *folio, struct vm_area_struct *vma)
527 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
529 if (unlikely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED))
530 mlock_new_folio(folio);
532 folio_add_lru(folio);
536 * If the folio cannot be invalidated, it is moved to the
537 * inactive list to speed up its reclaim. It is moved to the
538 * head of the list, rather than the tail, to give the flusher
539 * threads some time to write it out, as this is much more
540 * effective than the single-page writeout from reclaim.
542 * If the folio isn't mapped and dirty/writeback, the folio
543 * could be reclaimed asap using the reclaim flag.
545 * 1. active, mapped folio -> none
546 * 2. active, dirty/writeback folio -> inactive, head, reclaim
547 * 3. inactive, mapped folio -> none
548 * 4. inactive, dirty/writeback folio -> inactive, head, reclaim
549 * 5. inactive, clean -> inactive, tail
552 * In 4, it moves to the head of the inactive list so the folio is
553 * written out by flusher threads as this is much more efficient
554 * than the single-page writeout from reclaim.
556 static void lru_deactivate_file(struct lruvec *lruvec, struct folio *folio)
558 bool active = folio_test_active(folio);
559 long nr_pages = folio_nr_pages(folio);
561 if (folio_test_unevictable(folio))
564 /* Some processes are using the folio */
565 if (folio_mapped(folio))
568 lruvec_del_folio(lruvec, folio);
569 folio_clear_active(folio);
570 folio_clear_referenced(folio);
572 if (folio_test_writeback(folio) || folio_test_dirty(folio)) {
574 * Setting the reclaim flag could race with
575 * folio_end_writeback() and confuse readahead. But the
576 * race window is _really_ small and it's not a critical
579 lruvec_add_folio(lruvec, folio);
580 folio_set_reclaim(folio);
583 * The folio's writeback ended while it was in the batch.
584 * We move that folio to the tail of the inactive list.
586 lruvec_add_folio_tail(lruvec, folio);
587 __count_vm_events(PGROTATED, nr_pages);
591 __count_vm_events(PGDEACTIVATE, nr_pages);
592 __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
597 static void lru_deactivate(struct lruvec *lruvec, struct folio *folio)
599 long nr_pages = folio_nr_pages(folio);
601 if (folio_test_unevictable(folio) || !(folio_test_active(folio) || lru_gen_enabled()))
604 lruvec_del_folio(lruvec, folio);
605 folio_clear_active(folio);
606 folio_clear_referenced(folio);
607 lruvec_add_folio(lruvec, folio);
609 __count_vm_events(PGDEACTIVATE, nr_pages);
610 __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_pages);
613 static void lru_lazyfree(struct lruvec *lruvec, struct folio *folio)
615 long nr_pages = folio_nr_pages(folio);
617 if (!folio_test_anon(folio) || !folio_test_swapbacked(folio) ||
618 folio_test_swapcache(folio) || folio_test_unevictable(folio))
621 lruvec_del_folio(lruvec, folio);
622 folio_clear_active(folio);
623 folio_clear_referenced(folio);
625 * Lazyfree folios are clean anonymous folios. They have
626 * the swapbacked flag cleared, to distinguish them from normal
629 folio_clear_swapbacked(folio);
630 lruvec_add_folio(lruvec, folio);
632 __count_vm_events(PGLAZYFREE, nr_pages);
633 __count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE, nr_pages);
637 * Drain pages out of the cpu's folio_batch.
638 * Either "cpu" is the current CPU, and preemption has already been
639 * disabled; or "cpu" is being hot-unplugged, and is already dead.
641 void lru_add_drain_cpu(int cpu)
643 struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
644 struct folio_batch *fbatch = &fbatches->lru_add;
646 if (folio_batch_count(fbatch))
647 folio_batch_move_lru(fbatch, lru_add);
649 fbatch = &fbatches->lru_move_tail;
650 /* Disabling interrupts below acts as a compiler barrier. */
651 if (data_race(folio_batch_count(fbatch))) {
654 /* No harm done if a racing interrupt already did this */
655 local_lock_irqsave(&cpu_fbatches.lock_irq, flags);
656 folio_batch_move_lru(fbatch, lru_move_tail);
657 local_unlock_irqrestore(&cpu_fbatches.lock_irq, flags);
660 fbatch = &fbatches->lru_deactivate_file;
661 if (folio_batch_count(fbatch))
662 folio_batch_move_lru(fbatch, lru_deactivate_file);
664 fbatch = &fbatches->lru_deactivate;
665 if (folio_batch_count(fbatch))
666 folio_batch_move_lru(fbatch, lru_deactivate);
668 fbatch = &fbatches->lru_lazyfree;
669 if (folio_batch_count(fbatch))
670 folio_batch_move_lru(fbatch, lru_lazyfree);
672 folio_activate_drain(cpu);
676 * deactivate_file_folio() - Deactivate a file folio.
677 * @folio: Folio to deactivate.
679 * This function hints to the VM that @folio is a good reclaim candidate,
680 * for example if its invalidation fails due to the folio being dirty
681 * or under writeback.
683 * Context: Caller holds a reference on the folio.
685 void deactivate_file_folio(struct folio *folio)
687 /* Deactivating an unevictable folio will not accelerate reclaim */
688 if (folio_test_unevictable(folio))
691 folio_batch_add_and_move(folio, lru_deactivate_file, true);
695 * folio_deactivate - deactivate a folio
696 * @folio: folio to deactivate
698 * folio_deactivate() moves @folio to the inactive list if @folio was on the
699 * active list and was not unevictable. This is done to accelerate the
702 void folio_deactivate(struct folio *folio)
704 if (folio_test_unevictable(folio) || !(folio_test_active(folio) || lru_gen_enabled()))
707 folio_batch_add_and_move(folio, lru_deactivate, true);
711 * folio_mark_lazyfree - make an anon folio lazyfree
712 * @folio: folio to deactivate
714 * folio_mark_lazyfree() moves @folio to the inactive file list.
715 * This is done to accelerate the reclaim of @folio.
717 void folio_mark_lazyfree(struct folio *folio)
719 if (!folio_test_anon(folio) || !folio_test_swapbacked(folio) ||
720 folio_test_swapcache(folio) || folio_test_unevictable(folio))
723 folio_batch_add_and_move(folio, lru_lazyfree, true);
726 void lru_add_drain(void)
728 local_lock(&cpu_fbatches.lock);
729 lru_add_drain_cpu(smp_processor_id());
730 local_unlock(&cpu_fbatches.lock);
735 * It's called from per-cpu workqueue context in SMP case so
736 * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on
737 * the same cpu. It shouldn't be a problem in !SMP case since
738 * the core is only one and the locks will disable preemption.
740 static void lru_add_and_bh_lrus_drain(void)
742 local_lock(&cpu_fbatches.lock);
743 lru_add_drain_cpu(smp_processor_id());
744 local_unlock(&cpu_fbatches.lock);
745 invalidate_bh_lrus_cpu();
749 void lru_add_drain_cpu_zone(struct zone *zone)
751 local_lock(&cpu_fbatches.lock);
752 lru_add_drain_cpu(smp_processor_id());
753 drain_local_pages(zone);
754 local_unlock(&cpu_fbatches.lock);
760 static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
762 static void lru_add_drain_per_cpu(struct work_struct *dummy)
764 lru_add_and_bh_lrus_drain();
767 static bool cpu_needs_drain(unsigned int cpu)
769 struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);
771 /* Check these in order of likelihood that they're not zero */
772 return folio_batch_count(&fbatches->lru_add) ||
773 folio_batch_count(&fbatches->lru_move_tail) ||
774 folio_batch_count(&fbatches->lru_deactivate_file) ||
775 folio_batch_count(&fbatches->lru_deactivate) ||
776 folio_batch_count(&fbatches->lru_lazyfree) ||
777 folio_batch_count(&fbatches->lru_activate) ||
778 need_mlock_drain(cpu) ||
779 has_bh_in_lru(cpu, NULL);
783 * Doesn't need any cpu hotplug locking because we do rely on per-cpu
784 * kworkers being shut down before our page_alloc_cpu_dead callback is
785 * executed on the offlined cpu.
786 * Calling this function with cpu hotplug locks held can actually lead
787 * to obscure indirect dependencies via WQ context.
789 static inline void __lru_add_drain_all(bool force_all_cpus)
792 * lru_drain_gen - Global pages generation number
794 * (A) Definition: global lru_drain_gen = x implies that all generations
795 * 0 < n <= x are already *scheduled* for draining.
797 * This is an optimization for the highly-contended use case where a
798 * user space workload keeps constantly generating a flow of pages for
801 static unsigned int lru_drain_gen;
802 static struct cpumask has_work;
803 static DEFINE_MUTEX(lock);
804 unsigned cpu, this_gen;
807 * Make sure nobody triggers this path before mm_percpu_wq is fully
810 if (WARN_ON(!mm_percpu_wq))
814 * Guarantee folio_batch counter stores visible by this CPU
815 * are visible to other CPUs before loading the current drain
821 * (B) Locally cache global LRU draining generation number
823 * The read barrier ensures that the counter is loaded before the mutex
824 * is taken. It pairs with smp_mb() inside the mutex critical section
827 this_gen = smp_load_acquire(&lru_drain_gen);
832 * (C) Exit the draining operation if a newer generation, from another
833 * lru_add_drain_all(), was already scheduled for draining. Check (A).
835 if (unlikely(this_gen != lru_drain_gen && !force_all_cpus))
839 * (D) Increment global generation number
841 * Pairs with smp_load_acquire() at (B), outside of the critical
842 * section. Use a full memory barrier to guarantee that the
843 * new global drain generation number is stored before loading
844 * folio_batch counters.
846 * This pairing must be done here, before the for_each_online_cpu loop
847 * below which drains the page vectors.
849 * Let x, y, and z represent some system CPU numbers, where x < y < z.
850 * Assume CPU #z is in the middle of the for_each_online_cpu loop
851 * below and has already reached CPU #y's per-cpu data. CPU #x comes
852 * along, adds some pages to its per-cpu vectors, then calls
853 * lru_add_drain_all().
855 * If the paired barrier is done at any later step, e.g. after the
856 * loop, CPU #x will just exit at (C) and miss flushing out all of its
859 WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1);
862 cpumask_clear(&has_work);
863 for_each_online_cpu(cpu) {
864 struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
866 if (cpu_needs_drain(cpu)) {
867 INIT_WORK(work, lru_add_drain_per_cpu);
868 queue_work_on(cpu, mm_percpu_wq, work);
869 __cpumask_set_cpu(cpu, &has_work);
873 for_each_cpu(cpu, &has_work)
874 flush_work(&per_cpu(lru_add_drain_work, cpu));
880 void lru_add_drain_all(void)
882 __lru_add_drain_all(false);
885 void lru_add_drain_all(void)
889 #endif /* CONFIG_SMP */
891 atomic_t lru_disable_count = ATOMIC_INIT(0);
894 * lru_cache_disable() needs to be called before we start compiling
895 * a list of folios to be migrated using folio_isolate_lru().
896 * It drains folios on LRU cache and then disable on all cpus until
897 * lru_cache_enable is called.
899 * Must be paired with a call to lru_cache_enable().
901 void lru_cache_disable(void)
903 atomic_inc(&lru_disable_count);
905 * Readers of lru_disable_count are protected by either disabling
906 * preemption or rcu_read_lock:
908 * preempt_disable, local_irq_disable [bh_lru_lock()]
909 * rcu_read_lock [rt_spin_lock CONFIG_PREEMPT_RT]
910 * preempt_disable [local_lock !CONFIG_PREEMPT_RT]
912 * Since v5.1 kernel, synchronize_rcu() is guaranteed to wait on
913 * preempt_disable() regions of code. So any CPU which sees
914 * lru_disable_count = 0 will have exited the critical
915 * section when synchronize_rcu() returns.
917 synchronize_rcu_expedited();
919 __lru_add_drain_all(true);
921 lru_add_and_bh_lrus_drain();
926 * folios_put_refs - Reduce the reference count on a batch of folios.
927 * @folios: The folios.
928 * @refs: The number of refs to subtract from each folio.
930 * Like folio_put(), but for a batch of folios. This is more efficient
931 * than writing the loop yourself as it will optimise the locks which need
932 * to be taken if the folios are freed. The folios batch is returned
933 * empty and ready to be reused for another batch; there is no need
934 * to reinitialise it. If @refs is NULL, we subtract one from each
937 * Context: May be called in process or interrupt context, but not in NMI
938 * context. May be called while holding a spinlock.
940 void folios_put_refs(struct folio_batch *folios, unsigned int *refs)
943 struct lruvec *lruvec = NULL;
944 unsigned long flags = 0;
946 for (i = 0, j = 0; i < folios->nr; i++) {
947 struct folio *folio = folios->folios[i];
948 unsigned int nr_refs = refs ? refs[i] : 1;
950 if (is_huge_zero_folio(folio))
953 if (folio_is_zone_device(folio)) {
955 unlock_page_lruvec_irqrestore(lruvec, flags);
958 if (put_devmap_managed_folio_refs(folio, nr_refs))
960 if (folio_ref_sub_and_test(folio, nr_refs))
961 free_zone_device_folio(folio);
965 if (!folio_ref_sub_and_test(folio, nr_refs))
968 /* hugetlb has its own memcg */
969 if (folio_test_hugetlb(folio)) {
971 unlock_page_lruvec_irqrestore(lruvec, flags);
974 free_huge_folio(folio);
977 folio_unqueue_deferred_split(folio);
978 __page_cache_release(folio, &lruvec, &flags);
981 folios->folios[j] = folio;
985 unlock_page_lruvec_irqrestore(lruvec, flags);
987 folio_batch_reinit(folios);
992 mem_cgroup_uncharge_folios(folios);
993 free_unref_folios(folios);
995 EXPORT_SYMBOL(folios_put_refs);
998 * release_pages - batched put_page()
999 * @arg: array of pages to release
1000 * @nr: number of pages
1002 * Decrement the reference count on all the pages in @arg. If it
1003 * fell to zero, remove the page from the LRU and free it.
1005 * Note that the argument can be an array of pages, encoded pages,
1006 * or folio pointers. We ignore any encoded bits, and turn any of
1007 * them into just a folio that gets free'd.
1009 void release_pages(release_pages_arg arg, int nr)
1011 struct folio_batch fbatch;
1012 int refs[PAGEVEC_SIZE];
1013 struct encoded_page **encoded = arg.encoded_pages;
1016 folio_batch_init(&fbatch);
1017 for (i = 0; i < nr; i++) {
1018 /* Turn any of the argument types into a folio */
1019 struct folio *folio = page_folio(encoded_page_ptr(encoded[i]));
1021 /* Is our next entry actually "nr_pages" -> "nr_refs" ? */
1022 refs[fbatch.nr] = 1;
1023 if (unlikely(encoded_page_flags(encoded[i]) &
1024 ENCODED_PAGE_BIT_NR_PAGES_NEXT))
1025 refs[fbatch.nr] = encoded_nr_pages(encoded[++i]);
1027 if (folio_batch_add(&fbatch, folio) > 0)
1029 folios_put_refs(&fbatch, refs);
1033 folios_put_refs(&fbatch, refs);
1035 EXPORT_SYMBOL(release_pages);
1038 * The folios which we're about to release may be in the deferred lru-addition
1039 * queues. That would prevent them from really being freed right now. That's
1040 * OK from a correctness point of view but is inefficient - those folios may be
1041 * cache-warm and we want to give them back to the page allocator ASAP.
1043 * So __folio_batch_release() will drain those queues here.
1044 * folio_batch_move_lru() calls folios_put() directly to avoid
1047 void __folio_batch_release(struct folio_batch *fbatch)
1049 if (!fbatch->percpu_pvec_drained) {
1051 fbatch->percpu_pvec_drained = true;
1055 EXPORT_SYMBOL(__folio_batch_release);
1058 * folio_batch_remove_exceptionals() - Prune non-folios from a batch.
1059 * @fbatch: The batch to prune
1061 * find_get_entries() fills a batch with both folios and shadow/swap/DAX
1062 * entries. This function prunes all the non-folio entries from @fbatch
1063 * without leaving holes, so that it can be passed on to folio-only batch
1066 void folio_batch_remove_exceptionals(struct folio_batch *fbatch)
1070 for (i = 0, j = 0; i < folio_batch_count(fbatch); i++) {
1071 struct folio *folio = fbatch->folios[i];
1072 if (!xa_is_value(folio))
1073 fbatch->folios[j++] = folio;
1079 * Perform any setup for the swap system
1081 void __init swap_setup(void)
1083 unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
1085 /* Use a smaller cluster for small-memory machines */
1091 * Right now other parts of the system means that we
1092 * _really_ don't want to cluster much more