1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 * Swap reorganised 29.12.95, Stephen Tweedie.
6 * kswapd added: 7.1.96 sct
7 * Removed kswapd_ctl limits, and swap out as many pages as needed
8 * to bring the system back to freepages.high: 2.4.97, Rik van Riel.
9 * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
10 * Multiqueue VM started 5.8.00, Rik van Riel.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/sched/mm.h>
17 #include <linux/module.h>
18 #include <linux/gfp.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/swap.h>
21 #include <linux/pagemap.h>
22 #include <linux/init.h>
23 #include <linux/highmem.h>
24 #include <linux/vmpressure.h>
25 #include <linux/vmstat.h>
26 #include <linux/file.h>
27 #include <linux/writeback.h>
28 #include <linux/blkdev.h>
29 #include <linux/buffer_head.h> /* for buffer_heads_over_limit */
30 #include <linux/mm_inline.h>
31 #include <linux/backing-dev.h>
32 #include <linux/rmap.h>
33 #include <linux/topology.h>
34 #include <linux/cpu.h>
35 #include <linux/cpuset.h>
36 #include <linux/compaction.h>
37 #include <linux/notifier.h>
38 #include <linux/rwsem.h>
39 #include <linux/delay.h>
40 #include <linux/kthread.h>
41 #include <linux/freezer.h>
42 #include <linux/memcontrol.h>
43 #include <linux/migrate.h>
44 #include <linux/delayacct.h>
45 #include <linux/sysctl.h>
46 #include <linux/oom.h>
47 #include <linux/pagevec.h>
48 #include <linux/prefetch.h>
49 #include <linux/printk.h>
50 #include <linux/dax.h>
51 #include <linux/psi.h>
53 #include <asm/tlbflush.h>
54 #include <asm/div64.h>
56 #include <linux/swapops.h>
57 #include <linux/balloon_compaction.h>
58 #include <linux/sched/sysctl.h>
63 #define CREATE_TRACE_POINTS
64 #include <trace/events/vmscan.h>
67 /* How many pages shrink_list() should reclaim */
68 unsigned long nr_to_reclaim;
71 * Nodemask of nodes allowed by the caller. If NULL, all nodes
77 * The memory cgroup that hit its limit and as a result is the
78 * primary target of this reclaim invocation.
80 struct mem_cgroup *target_mem_cgroup;
83 * Scan pressure balancing between anon and file LRUs
85 unsigned long anon_cost;
86 unsigned long file_cost;
88 /* Can active pages be deactivated as part of reclaim? */
89 #define DEACTIVATE_ANON 1
90 #define DEACTIVATE_FILE 2
91 unsigned int may_deactivate:2;
92 unsigned int force_deactivate:1;
93 unsigned int skipped_deactivate:1;
95 /* Writepage batching in laptop mode; RECLAIM_WRITE */
96 unsigned int may_writepage:1;
98 /* Can mapped pages be reclaimed? */
99 unsigned int may_unmap:1;
101 /* Can pages be swapped as part of reclaim? */
102 unsigned int may_swap:1;
104 /* Proactive reclaim invoked by userspace through memory.reclaim */
105 unsigned int proactive:1;
108 * Cgroup memory below memory.low is protected as long as we
109 * don't threaten to OOM. If any cgroup is reclaimed at
110 * reduced force or passed over entirely due to its memory.low
111 * setting (memcg_low_skipped), and nothing is reclaimed as a
112 * result, then go back for one more cycle that reclaims the protected
113 * memory (memcg_low_reclaim) to avert OOM.
115 unsigned int memcg_low_reclaim:1;
116 unsigned int memcg_low_skipped:1;
118 unsigned int hibernation_mode:1;
120 /* One of the zones is ready for compaction */
121 unsigned int compaction_ready:1;
123 /* There is easily reclaimable cold cache in the current node */
124 unsigned int cache_trim_mode:1;
126 /* The file pages on the current node are dangerously low */
127 unsigned int file_is_tiny:1;
129 /* Always discard instead of demoting to lower tier memory */
130 unsigned int no_demotion:1;
132 /* Allocation order */
135 /* Scan (total_size >> priority) pages at once */
138 /* The highest zone to isolate pages for reclaim from */
141 /* This context's GFP mask */
144 /* Incremented by the number of inactive pages that were scanned */
145 unsigned long nr_scanned;
147 /* Number of pages freed so far during a call to shrink_zones() */
148 unsigned long nr_reclaimed;
152 unsigned int unqueued_dirty;
153 unsigned int congested;
154 unsigned int writeback;
155 unsigned int immediate;
156 unsigned int file_taken;
160 /* for recording the reclaimed slab by now */
161 struct reclaim_state reclaim_state;
164 #ifdef ARCH_HAS_PREFETCHW
165 #define prefetchw_prev_lru_folio(_folio, _base, _field) \
167 if ((_folio)->lru.prev != _base) { \
168 struct folio *prev; \
170 prev = lru_to_folio(&(_folio->lru)); \
171 prefetchw(&prev->_field); \
175 #define prefetchw_prev_lru_folio(_folio, _base, _field) do { } while (0)
179 * From 0 .. 200. Higher means more swappy.
181 int vm_swappiness = 60;
183 static void set_task_reclaim_state(struct task_struct *task,
184 struct reclaim_state *rs)
186 /* Check for an overwrite */
187 WARN_ON_ONCE(rs && task->reclaim_state);
189 /* Check for the nulling of an already-nulled member */
190 WARN_ON_ONCE(!rs && !task->reclaim_state);
192 task->reclaim_state = rs;
195 LIST_HEAD(shrinker_list);
196 DECLARE_RWSEM(shrinker_rwsem);
199 static int shrinker_nr_max;
201 /* The shrinker_info is expanded in a batch of BITS_PER_LONG */
202 static inline int shrinker_map_size(int nr_items)
204 return (DIV_ROUND_UP(nr_items, BITS_PER_LONG) * sizeof(unsigned long));
207 static inline int shrinker_defer_size(int nr_items)
209 return (round_up(nr_items, BITS_PER_LONG) * sizeof(atomic_long_t));
212 static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg,
215 return rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_info,
216 lockdep_is_held(&shrinker_rwsem));
219 static int expand_one_shrinker_info(struct mem_cgroup *memcg,
220 int map_size, int defer_size,
221 int old_map_size, int old_defer_size)
223 struct shrinker_info *new, *old;
224 struct mem_cgroup_per_node *pn;
226 int size = map_size + defer_size;
229 pn = memcg->nodeinfo[nid];
230 old = shrinker_info_protected(memcg, nid);
231 /* Not yet online memcg */
235 new = kvmalloc_node(sizeof(*new) + size, GFP_KERNEL, nid);
239 new->nr_deferred = (atomic_long_t *)(new + 1);
240 new->map = (void *)new->nr_deferred + defer_size;
242 /* map: set all old bits, clear all new bits */
243 memset(new->map, (int)0xff, old_map_size);
244 memset((void *)new->map + old_map_size, 0, map_size - old_map_size);
245 /* nr_deferred: copy old values, clear all new values */
246 memcpy(new->nr_deferred, old->nr_deferred, old_defer_size);
247 memset((void *)new->nr_deferred + old_defer_size, 0,
248 defer_size - old_defer_size);
250 rcu_assign_pointer(pn->shrinker_info, new);
251 kvfree_rcu(old, rcu);
257 void free_shrinker_info(struct mem_cgroup *memcg)
259 struct mem_cgroup_per_node *pn;
260 struct shrinker_info *info;
264 pn = memcg->nodeinfo[nid];
265 info = rcu_dereference_protected(pn->shrinker_info, true);
267 rcu_assign_pointer(pn->shrinker_info, NULL);
271 int alloc_shrinker_info(struct mem_cgroup *memcg)
273 struct shrinker_info *info;
274 int nid, size, ret = 0;
275 int map_size, defer_size = 0;
277 down_write(&shrinker_rwsem);
278 map_size = shrinker_map_size(shrinker_nr_max);
279 defer_size = shrinker_defer_size(shrinker_nr_max);
280 size = map_size + defer_size;
282 info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid);
284 free_shrinker_info(memcg);
288 info->nr_deferred = (atomic_long_t *)(info + 1);
289 info->map = (void *)info->nr_deferred + defer_size;
290 rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info);
292 up_write(&shrinker_rwsem);
297 static inline bool need_expand(int nr_max)
299 return round_up(nr_max, BITS_PER_LONG) >
300 round_up(shrinker_nr_max, BITS_PER_LONG);
303 static int expand_shrinker_info(int new_id)
306 int new_nr_max = new_id + 1;
307 int map_size, defer_size = 0;
308 int old_map_size, old_defer_size = 0;
309 struct mem_cgroup *memcg;
311 if (!need_expand(new_nr_max))
314 if (!root_mem_cgroup)
317 lockdep_assert_held(&shrinker_rwsem);
319 map_size = shrinker_map_size(new_nr_max);
320 defer_size = shrinker_defer_size(new_nr_max);
321 old_map_size = shrinker_map_size(shrinker_nr_max);
322 old_defer_size = shrinker_defer_size(shrinker_nr_max);
324 memcg = mem_cgroup_iter(NULL, NULL, NULL);
326 ret = expand_one_shrinker_info(memcg, map_size, defer_size,
327 old_map_size, old_defer_size);
329 mem_cgroup_iter_break(NULL, memcg);
332 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
335 shrinker_nr_max = new_nr_max;
340 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id)
342 if (shrinker_id >= 0 && memcg && !mem_cgroup_is_root(memcg)) {
343 struct shrinker_info *info;
346 info = rcu_dereference(memcg->nodeinfo[nid]->shrinker_info);
347 /* Pairs with smp mb in shrink_slab() */
348 smp_mb__before_atomic();
349 set_bit(shrinker_id, info->map);
354 static DEFINE_IDR(shrinker_idr);
356 static int prealloc_memcg_shrinker(struct shrinker *shrinker)
358 int id, ret = -ENOMEM;
360 if (mem_cgroup_disabled())
363 down_write(&shrinker_rwsem);
364 /* This may call shrinker, so it must use down_read_trylock() */
365 id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL);
369 if (id >= shrinker_nr_max) {
370 if (expand_shrinker_info(id)) {
371 idr_remove(&shrinker_idr, id);
378 up_write(&shrinker_rwsem);
382 static void unregister_memcg_shrinker(struct shrinker *shrinker)
384 int id = shrinker->id;
388 lockdep_assert_held(&shrinker_rwsem);
390 idr_remove(&shrinker_idr, id);
393 static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker,
394 struct mem_cgroup *memcg)
396 struct shrinker_info *info;
398 info = shrinker_info_protected(memcg, nid);
399 return atomic_long_xchg(&info->nr_deferred[shrinker->id], 0);
402 static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker,
403 struct mem_cgroup *memcg)
405 struct shrinker_info *info;
407 info = shrinker_info_protected(memcg, nid);
408 return atomic_long_add_return(nr, &info->nr_deferred[shrinker->id]);
411 void reparent_shrinker_deferred(struct mem_cgroup *memcg)
415 struct mem_cgroup *parent;
416 struct shrinker_info *child_info, *parent_info;
418 parent = parent_mem_cgroup(memcg);
420 parent = root_mem_cgroup;
422 /* Prevent from concurrent shrinker_info expand */
423 down_read(&shrinker_rwsem);
425 child_info = shrinker_info_protected(memcg, nid);
426 parent_info = shrinker_info_protected(parent, nid);
427 for (i = 0; i < shrinker_nr_max; i++) {
428 nr = atomic_long_read(&child_info->nr_deferred[i]);
429 atomic_long_add(nr, &parent_info->nr_deferred[i]);
432 up_read(&shrinker_rwsem);
435 static bool cgroup_reclaim(struct scan_control *sc)
437 return sc->target_mem_cgroup;
441 * writeback_throttling_sane - is the usual dirty throttling mechanism available?
442 * @sc: scan_control in question
444 * The normal page dirty throttling mechanism in balance_dirty_pages() is
445 * completely broken with the legacy memcg and direct stalling in
446 * shrink_page_list() is used for throttling instead, which lacks all the
447 * niceties such as fairness, adaptive pausing, bandwidth proportional
448 * allocation and configurability.
450 * This function tests whether the vmscan currently in progress can assume
451 * that the normal dirty throttling mechanism is operational.
453 static bool writeback_throttling_sane(struct scan_control *sc)
455 if (!cgroup_reclaim(sc))
457 #ifdef CONFIG_CGROUP_WRITEBACK
458 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
464 static int prealloc_memcg_shrinker(struct shrinker *shrinker)
469 static void unregister_memcg_shrinker(struct shrinker *shrinker)
473 static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker,
474 struct mem_cgroup *memcg)
479 static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker,
480 struct mem_cgroup *memcg)
485 static bool cgroup_reclaim(struct scan_control *sc)
490 static bool writeback_throttling_sane(struct scan_control *sc)
496 static long xchg_nr_deferred(struct shrinker *shrinker,
497 struct shrink_control *sc)
501 if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
505 (shrinker->flags & SHRINKER_MEMCG_AWARE))
506 return xchg_nr_deferred_memcg(nid, shrinker,
509 return atomic_long_xchg(&shrinker->nr_deferred[nid], 0);
513 static long add_nr_deferred(long nr, struct shrinker *shrinker,
514 struct shrink_control *sc)
518 if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
522 (shrinker->flags & SHRINKER_MEMCG_AWARE))
523 return add_nr_deferred_memcg(nr, nid, shrinker,
526 return atomic_long_add_return(nr, &shrinker->nr_deferred[nid]);
529 static bool can_demote(int nid, struct scan_control *sc)
531 if (!numa_demotion_enabled)
533 if (sc && sc->no_demotion)
535 if (next_demotion_node(nid) == NUMA_NO_NODE)
541 static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
543 struct scan_control *sc)
547 * For non-memcg reclaim, is there
548 * space in any swap device?
550 if (get_nr_swap_pages() > 0)
553 /* Is the memcg below its swap limit? */
554 if (mem_cgroup_get_nr_swap_pages(memcg) > 0)
559 * The page can not be swapped.
561 * Can it be reclaimed from this node via demotion?
563 return can_demote(nid, sc);
567 * This misses isolated pages which are not accounted for to save counters.
568 * As the data only determines if reclaim or compaction continues, it is
569 * not expected that isolated pages will be a dominating factor.
571 unsigned long zone_reclaimable_pages(struct zone *zone)
575 nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) +
576 zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE);
577 if (can_reclaim_anon_pages(NULL, zone_to_nid(zone), NULL))
578 nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) +
579 zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON);
585 * lruvec_lru_size - Returns the number of pages on the given LRU list.
586 * @lruvec: lru vector
588 * @zone_idx: zones to consider (use MAX_NR_ZONES - 1 for the whole LRU list)
590 static unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru,
593 unsigned long size = 0;
596 for (zid = 0; zid <= zone_idx; zid++) {
597 struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid];
599 if (!managed_zone(zone))
602 if (!mem_cgroup_disabled())
603 size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid);
605 size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru);
611 * Add a shrinker callback to be called from the vm.
613 static int __prealloc_shrinker(struct shrinker *shrinker)
618 if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
619 err = prealloc_memcg_shrinker(shrinker);
623 shrinker->flags &= ~SHRINKER_MEMCG_AWARE;
626 size = sizeof(*shrinker->nr_deferred);
627 if (shrinker->flags & SHRINKER_NUMA_AWARE)
630 shrinker->nr_deferred = kzalloc(size, GFP_KERNEL);
631 if (!shrinker->nr_deferred)
637 #ifdef CONFIG_SHRINKER_DEBUG
638 int prealloc_shrinker(struct shrinker *shrinker, const char *fmt, ...)
644 shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap);
649 err = __prealloc_shrinker(shrinker);
651 kfree_const(shrinker->name);
652 shrinker->name = NULL;
658 int prealloc_shrinker(struct shrinker *shrinker, const char *fmt, ...)
660 return __prealloc_shrinker(shrinker);
664 void free_prealloced_shrinker(struct shrinker *shrinker)
666 #ifdef CONFIG_SHRINKER_DEBUG
667 kfree_const(shrinker->name);
668 shrinker->name = NULL;
670 if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
671 down_write(&shrinker_rwsem);
672 unregister_memcg_shrinker(shrinker);
673 up_write(&shrinker_rwsem);
677 kfree(shrinker->nr_deferred);
678 shrinker->nr_deferred = NULL;
681 void register_shrinker_prepared(struct shrinker *shrinker)
683 down_write(&shrinker_rwsem);
684 list_add_tail(&shrinker->list, &shrinker_list);
685 shrinker->flags |= SHRINKER_REGISTERED;
686 shrinker_debugfs_add(shrinker);
687 up_write(&shrinker_rwsem);
690 static int __register_shrinker(struct shrinker *shrinker)
692 int err = __prealloc_shrinker(shrinker);
696 register_shrinker_prepared(shrinker);
700 #ifdef CONFIG_SHRINKER_DEBUG
701 int register_shrinker(struct shrinker *shrinker, const char *fmt, ...)
707 shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap);
712 err = __register_shrinker(shrinker);
714 kfree_const(shrinker->name);
715 shrinker->name = NULL;
720 int register_shrinker(struct shrinker *shrinker, const char *fmt, ...)
722 return __register_shrinker(shrinker);
725 EXPORT_SYMBOL(register_shrinker);
730 void unregister_shrinker(struct shrinker *shrinker)
732 if (!(shrinker->flags & SHRINKER_REGISTERED))
735 down_write(&shrinker_rwsem);
736 list_del(&shrinker->list);
737 shrinker->flags &= ~SHRINKER_REGISTERED;
738 if (shrinker->flags & SHRINKER_MEMCG_AWARE)
739 unregister_memcg_shrinker(shrinker);
740 shrinker_debugfs_remove(shrinker);
741 up_write(&shrinker_rwsem);
743 kfree(shrinker->nr_deferred);
744 shrinker->nr_deferred = NULL;
746 EXPORT_SYMBOL(unregister_shrinker);
749 * synchronize_shrinkers - Wait for all running shrinkers to complete.
751 * This is equivalent to calling unregister_shrink() and register_shrinker(),
752 * but atomically and with less overhead. This is useful to guarantee that all
753 * shrinker invocations have seen an update, before freeing memory, similar to
756 void synchronize_shrinkers(void)
758 down_write(&shrinker_rwsem);
759 up_write(&shrinker_rwsem);
761 EXPORT_SYMBOL(synchronize_shrinkers);
763 #define SHRINK_BATCH 128
765 static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
766 struct shrinker *shrinker, int priority)
768 unsigned long freed = 0;
769 unsigned long long delta;
774 long batch_size = shrinker->batch ? shrinker->batch
776 long scanned = 0, next_deferred;
778 freeable = shrinker->count_objects(shrinker, shrinkctl);
779 if (freeable == 0 || freeable == SHRINK_EMPTY)
783 * copy the current shrinker scan count into a local variable
784 * and zero it so that other concurrent shrinker invocations
785 * don't also do this scanning work.
787 nr = xchg_nr_deferred(shrinker, shrinkctl);
789 if (shrinker->seeks) {
790 delta = freeable >> priority;
792 do_div(delta, shrinker->seeks);
795 * These objects don't require any IO to create. Trim
796 * them aggressively under memory pressure to keep
797 * them from causing refetches in the IO caches.
799 delta = freeable / 2;
802 total_scan = nr >> priority;
804 total_scan = min(total_scan, (2 * freeable));
806 trace_mm_shrink_slab_start(shrinker, shrinkctl, nr,
807 freeable, delta, total_scan, priority);
810 * Normally, we should not scan less than batch_size objects in one
811 * pass to avoid too frequent shrinker calls, but if the slab has less
812 * than batch_size objects in total and we are really tight on memory,
813 * we will try to reclaim all available objects, otherwise we can end
814 * up failing allocations although there are plenty of reclaimable
815 * objects spread over several slabs with usage less than the
818 * We detect the "tight on memory" situations by looking at the total
819 * number of objects we want to scan (total_scan). If it is greater
820 * than the total number of objects on slab (freeable), we must be
821 * scanning at high prio and therefore should try to reclaim as much as
824 while (total_scan >= batch_size ||
825 total_scan >= freeable) {
827 unsigned long nr_to_scan = min(batch_size, total_scan);
829 shrinkctl->nr_to_scan = nr_to_scan;
830 shrinkctl->nr_scanned = nr_to_scan;
831 ret = shrinker->scan_objects(shrinker, shrinkctl);
832 if (ret == SHRINK_STOP)
836 count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned);
837 total_scan -= shrinkctl->nr_scanned;
838 scanned += shrinkctl->nr_scanned;
844 * The deferred work is increased by any new work (delta) that wasn't
845 * done, decreased by old deferred work that was done now.
847 * And it is capped to two times of the freeable items.
849 next_deferred = max_t(long, (nr + delta - scanned), 0);
850 next_deferred = min(next_deferred, (2 * freeable));
853 * move the unused scan count back into the shrinker in a
854 * manner that handles concurrent updates.
856 new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
858 trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan);
863 static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
864 struct mem_cgroup *memcg, int priority)
866 struct shrinker_info *info;
867 unsigned long ret, freed = 0;
870 if (!mem_cgroup_online(memcg))
873 if (!down_read_trylock(&shrinker_rwsem))
876 info = shrinker_info_protected(memcg, nid);
880 for_each_set_bit(i, info->map, shrinker_nr_max) {
881 struct shrink_control sc = {
882 .gfp_mask = gfp_mask,
886 struct shrinker *shrinker;
888 shrinker = idr_find(&shrinker_idr, i);
889 if (unlikely(!shrinker || !(shrinker->flags & SHRINKER_REGISTERED))) {
891 clear_bit(i, info->map);
895 /* Call non-slab shrinkers even though kmem is disabled */
896 if (!memcg_kmem_enabled() &&
897 !(shrinker->flags & SHRINKER_NONSLAB))
900 ret = do_shrink_slab(&sc, shrinker, priority);
901 if (ret == SHRINK_EMPTY) {
902 clear_bit(i, info->map);
904 * After the shrinker reported that it had no objects to
905 * free, but before we cleared the corresponding bit in
906 * the memcg shrinker map, a new object might have been
907 * added. To make sure, we have the bit set in this
908 * case, we invoke the shrinker one more time and reset
909 * the bit if it reports that it is not empty anymore.
910 * The memory barrier here pairs with the barrier in
911 * set_shrinker_bit():
913 * list_lru_add() shrink_slab_memcg()
914 * list_add_tail() clear_bit()
916 * set_bit() do_shrink_slab()
918 smp_mb__after_atomic();
919 ret = do_shrink_slab(&sc, shrinker, priority);
920 if (ret == SHRINK_EMPTY)
923 set_shrinker_bit(memcg, nid, i);
927 if (rwsem_is_contended(&shrinker_rwsem)) {
933 up_read(&shrinker_rwsem);
936 #else /* CONFIG_MEMCG */
937 static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
938 struct mem_cgroup *memcg, int priority)
942 #endif /* CONFIG_MEMCG */
945 * shrink_slab - shrink slab caches
946 * @gfp_mask: allocation context
947 * @nid: node whose slab caches to target
948 * @memcg: memory cgroup whose slab caches to target
949 * @priority: the reclaim priority
951 * Call the shrink functions to age shrinkable caches.
953 * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set,
954 * unaware shrinkers will receive a node id of 0 instead.
956 * @memcg specifies the memory cgroup to target. Unaware shrinkers
957 * are called only if it is the root cgroup.
959 * @priority is sc->priority, we take the number of objects and >> by priority
960 * in order to get the scan target.
962 * Returns the number of reclaimed slab objects.
964 static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
965 struct mem_cgroup *memcg,
968 unsigned long ret, freed = 0;
969 struct shrinker *shrinker;
972 * The root memcg might be allocated even though memcg is disabled
973 * via "cgroup_disable=memory" boot parameter. This could make
974 * mem_cgroup_is_root() return false, then just run memcg slab
975 * shrink, but skip global shrink. This may result in premature
978 if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg))
979 return shrink_slab_memcg(gfp_mask, nid, memcg, priority);
981 if (!down_read_trylock(&shrinker_rwsem))
984 list_for_each_entry(shrinker, &shrinker_list, list) {
985 struct shrink_control sc = {
986 .gfp_mask = gfp_mask,
991 ret = do_shrink_slab(&sc, shrinker, priority);
992 if (ret == SHRINK_EMPTY)
996 * Bail out if someone want to register a new shrinker to
997 * prevent the registration from being stalled for long periods
998 * by parallel ongoing shrinking.
1000 if (rwsem_is_contended(&shrinker_rwsem)) {
1001 freed = freed ? : 1;
1006 up_read(&shrinker_rwsem);
1012 static void drop_slab_node(int nid)
1014 unsigned long freed;
1018 struct mem_cgroup *memcg = NULL;
1020 if (fatal_signal_pending(current))
1024 memcg = mem_cgroup_iter(NULL, NULL, NULL);
1026 freed += shrink_slab(GFP_KERNEL, nid, memcg, 0);
1027 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
1028 } while ((freed >> shift++) > 1);
1031 void drop_slab(void)
1035 for_each_online_node(nid)
1036 drop_slab_node(nid);
1039 static inline int is_page_cache_freeable(struct folio *folio)
1042 * A freeable page cache page is referenced only by the caller
1043 * that isolated the page, the page cache and optional buffer
1044 * heads at page->private.
1046 return folio_ref_count(folio) - folio_test_private(folio) ==
1047 1 + folio_nr_pages(folio);
1051 * We detected a synchronous write error writing a folio out. Probably
1052 * -ENOSPC. We need to propagate that into the address_space for a subsequent
1053 * fsync(), msync() or close().
1055 * The tricky part is that after writepage we cannot touch the mapping: nothing
1056 * prevents it from being freed up. But we have a ref on the folio and once
1057 * that folio is locked, the mapping is pinned.
1059 * We're allowed to run sleeping folio_lock() here because we know the caller has
1062 static void handle_write_error(struct address_space *mapping,
1063 struct folio *folio, int error)
1066 if (folio_mapping(folio) == mapping)
1067 mapping_set_error(mapping, error);
1068 folio_unlock(folio);
1071 static bool skip_throttle_noprogress(pg_data_t *pgdat)
1073 int reclaimable = 0, write_pending = 0;
1077 * If kswapd is disabled, reschedule if necessary but do not
1078 * throttle as the system is likely near OOM.
1080 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
1084 * If there are a lot of dirty/writeback pages then do not
1085 * throttle as throttling will occur when the pages cycle
1086 * towards the end of the LRU if still under writeback.
1088 for (i = 0; i < MAX_NR_ZONES; i++) {
1089 struct zone *zone = pgdat->node_zones + i;
1091 if (!managed_zone(zone))
1094 reclaimable += zone_reclaimable_pages(zone);
1095 write_pending += zone_page_state_snapshot(zone,
1096 NR_ZONE_WRITE_PENDING);
1098 if (2 * write_pending <= reclaimable)
1104 void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason)
1106 wait_queue_head_t *wqh = &pgdat->reclaim_wait[reason];
1111 * Do not throttle IO workers, kthreads other than kswapd or
1112 * workqueues. They may be required for reclaim to make
1113 * forward progress (e.g. journalling workqueues or kthreads).
1115 if (!current_is_kswapd() &&
1116 current->flags & (PF_IO_WORKER|PF_KTHREAD)) {
1122 * These figures are pulled out of thin air.
1123 * VMSCAN_THROTTLE_ISOLATED is a transient condition based on too many
1124 * parallel reclaimers which is a short-lived event so the timeout is
1125 * short. Failing to make progress or waiting on writeback are
1126 * potentially long-lived events so use a longer timeout. This is shaky
1127 * logic as a failure to make progress could be due to anything from
1128 * writeback to a slow device to excessive references pages at the tail
1129 * of the inactive LRU.
1132 case VMSCAN_THROTTLE_WRITEBACK:
1135 if (atomic_inc_return(&pgdat->nr_writeback_throttled) == 1) {
1136 WRITE_ONCE(pgdat->nr_reclaim_start,
1137 node_page_state(pgdat, NR_THROTTLED_WRITTEN));
1141 case VMSCAN_THROTTLE_CONGESTED:
1143 case VMSCAN_THROTTLE_NOPROGRESS:
1144 if (skip_throttle_noprogress(pgdat)) {
1152 case VMSCAN_THROTTLE_ISOLATED:
1161 prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
1162 ret = schedule_timeout(timeout);
1163 finish_wait(wqh, &wait);
1165 if (reason == VMSCAN_THROTTLE_WRITEBACK)
1166 atomic_dec(&pgdat->nr_writeback_throttled);
1168 trace_mm_vmscan_throttled(pgdat->node_id, jiffies_to_usecs(timeout),
1169 jiffies_to_usecs(timeout - ret),
1174 * Account for pages written if tasks are throttled waiting on dirty
1175 * pages to clean. If enough pages have been cleaned since throttling
1176 * started then wakeup the throttled tasks.
1178 void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio,
1181 unsigned long nr_written;
1183 node_stat_add_folio(folio, NR_THROTTLED_WRITTEN);
1186 * This is an inaccurate read as the per-cpu deltas may not
1187 * be synchronised. However, given that the system is
1188 * writeback throttled, it is not worth taking the penalty
1189 * of getting an accurate count. At worst, the throttle
1190 * timeout guarantees forward progress.
1192 nr_written = node_page_state(pgdat, NR_THROTTLED_WRITTEN) -
1193 READ_ONCE(pgdat->nr_reclaim_start);
1195 if (nr_written > SWAP_CLUSTER_MAX * nr_throttled)
1196 wake_up(&pgdat->reclaim_wait[VMSCAN_THROTTLE_WRITEBACK]);
1199 /* possible outcome of pageout() */
1201 /* failed to write page out, page is locked */
1203 /* move page to the active list, page is locked */
1205 /* page has been sent to the disk successfully, page is unlocked */
1207 /* page is clean and locked */
1212 * pageout is called by shrink_page_list() for each dirty page.
1213 * Calls ->writepage().
1215 static pageout_t pageout(struct folio *folio, struct address_space *mapping,
1216 struct swap_iocb **plug)
1219 * If the folio is dirty, only perform writeback if that write
1220 * will be non-blocking. To prevent this allocation from being
1221 * stalled by pagecache activity. But note that there may be
1222 * stalls if we need to run get_block(). We could test
1223 * PagePrivate for that.
1225 * If this process is currently in __generic_file_write_iter() against
1226 * this folio's queue, we can perform writeback even if that
1229 * If the folio is swapcache, write it back even if that would
1230 * block, for some throttling. This happens by accident, because
1231 * swap_backing_dev_info is bust: it doesn't reflect the
1232 * congestion state of the swapdevs. Easy to fix, if needed.
1234 if (!is_page_cache_freeable(folio))
1238 * Some data journaling orphaned folios can have
1239 * folio->mapping == NULL while being dirty with clean buffers.
1241 if (folio_test_private(folio)) {
1242 if (try_to_free_buffers(folio)) {
1243 folio_clear_dirty(folio);
1244 pr_info("%s: orphaned folio\n", __func__);
1250 if (mapping->a_ops->writepage == NULL)
1251 return PAGE_ACTIVATE;
1253 if (folio_clear_dirty_for_io(folio)) {
1255 struct writeback_control wbc = {
1256 .sync_mode = WB_SYNC_NONE,
1257 .nr_to_write = SWAP_CLUSTER_MAX,
1259 .range_end = LLONG_MAX,
1264 folio_set_reclaim(folio);
1265 res = mapping->a_ops->writepage(&folio->page, &wbc);
1267 handle_write_error(mapping, folio, res);
1268 if (res == AOP_WRITEPAGE_ACTIVATE) {
1269 folio_clear_reclaim(folio);
1270 return PAGE_ACTIVATE;
1273 if (!folio_test_writeback(folio)) {
1274 /* synchronous write or broken a_ops? */
1275 folio_clear_reclaim(folio);
1277 trace_mm_vmscan_write_folio(folio);
1278 node_stat_add_folio(folio, NR_VMSCAN_WRITE);
1279 return PAGE_SUCCESS;
1286 * Same as remove_mapping, but if the page is removed from the mapping, it
1287 * gets returned with a refcount of 0.
1289 static int __remove_mapping(struct address_space *mapping, struct folio *folio,
1290 bool reclaimed, struct mem_cgroup *target_memcg)
1293 void *shadow = NULL;
1295 BUG_ON(!folio_test_locked(folio));
1296 BUG_ON(mapping != folio_mapping(folio));
1298 if (!folio_test_swapcache(folio))
1299 spin_lock(&mapping->host->i_lock);
1300 xa_lock_irq(&mapping->i_pages);
1302 * The non racy check for a busy page.
1304 * Must be careful with the order of the tests. When someone has
1305 * a ref to the page, it may be possible that they dirty it then
1306 * drop the reference. So if PageDirty is tested before page_count
1307 * here, then the following race may occur:
1309 * get_user_pages(&page);
1310 * [user mapping goes away]
1312 * !PageDirty(page) [good]
1313 * SetPageDirty(page);
1315 * !page_count(page) [good, discard it]
1317 * [oops, our write_to data is lost]
1319 * Reversing the order of the tests ensures such a situation cannot
1320 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
1321 * load is not satisfied before that of page->_refcount.
1323 * Note that if SetPageDirty is always performed via set_page_dirty,
1324 * and thus under the i_pages lock, then this ordering is not required.
1326 refcount = 1 + folio_nr_pages(folio);
1327 if (!folio_ref_freeze(folio, refcount))
1329 /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */
1330 if (unlikely(folio_test_dirty(folio))) {
1331 folio_ref_unfreeze(folio, refcount);
1335 if (folio_test_swapcache(folio)) {
1336 swp_entry_t swap = folio_swap_entry(folio);
1338 /* get a shadow entry before mem_cgroup_swapout() clears folio_memcg() */
1339 if (reclaimed && !mapping_exiting(mapping))
1340 shadow = workingset_eviction(folio, target_memcg);
1341 mem_cgroup_swapout(folio, swap);
1342 __delete_from_swap_cache(folio, swap, shadow);
1343 xa_unlock_irq(&mapping->i_pages);
1344 put_swap_page(&folio->page, swap);
1346 void (*free_folio)(struct folio *);
1348 free_folio = mapping->a_ops->free_folio;
1350 * Remember a shadow entry for reclaimed file cache in
1351 * order to detect refaults, thus thrashing, later on.
1353 * But don't store shadows in an address space that is
1354 * already exiting. This is not just an optimization,
1355 * inode reclaim needs to empty out the radix tree or
1356 * the nodes are lost. Don't plant shadows behind its
1359 * We also don't store shadows for DAX mappings because the
1360 * only page cache pages found in these are zero pages
1361 * covering holes, and because we don't want to mix DAX
1362 * exceptional entries and shadow exceptional entries in the
1363 * same address_space.
1365 if (reclaimed && folio_is_file_lru(folio) &&
1366 !mapping_exiting(mapping) && !dax_mapping(mapping))
1367 shadow = workingset_eviction(folio, target_memcg);
1368 __filemap_remove_folio(folio, shadow);
1369 xa_unlock_irq(&mapping->i_pages);
1370 if (mapping_shrinkable(mapping))
1371 inode_add_lru(mapping->host);
1372 spin_unlock(&mapping->host->i_lock);
1381 xa_unlock_irq(&mapping->i_pages);
1382 if (!folio_test_swapcache(folio))
1383 spin_unlock(&mapping->host->i_lock);
1388 * remove_mapping() - Attempt to remove a folio from its mapping.
1389 * @mapping: The address space.
1390 * @folio: The folio to remove.
1392 * If the folio is dirty, under writeback or if someone else has a ref
1393 * on it, removal will fail.
1394 * Return: The number of pages removed from the mapping. 0 if the folio
1395 * could not be removed.
1396 * Context: The caller should have a single refcount on the folio and
1399 long remove_mapping(struct address_space *mapping, struct folio *folio)
1401 if (__remove_mapping(mapping, folio, false, NULL)) {
1403 * Unfreezing the refcount with 1 effectively
1404 * drops the pagecache ref for us without requiring another
1407 folio_ref_unfreeze(folio, 1);
1408 return folio_nr_pages(folio);
1414 * folio_putback_lru - Put previously isolated folio onto appropriate LRU list.
1415 * @folio: Folio to be returned to an LRU list.
1417 * Add previously isolated @folio to appropriate LRU list.
1418 * The folio may still be unevictable for other reasons.
1420 * Context: lru_lock must not be held, interrupts must be enabled.
1422 void folio_putback_lru(struct folio *folio)
1424 folio_add_lru(folio);
1425 folio_put(folio); /* drop ref from isolate */
1428 enum page_references {
1430 PAGEREF_RECLAIM_CLEAN,
1435 static enum page_references folio_check_references(struct folio *folio,
1436 struct scan_control *sc)
1438 int referenced_ptes, referenced_folio;
1439 unsigned long vm_flags;
1441 referenced_ptes = folio_referenced(folio, 1, sc->target_mem_cgroup,
1443 referenced_folio = folio_test_clear_referenced(folio);
1446 * The supposedly reclaimable folio was found to be in a VM_LOCKED vma.
1447 * Let the folio, now marked Mlocked, be moved to the unevictable list.
1449 if (vm_flags & VM_LOCKED)
1450 return PAGEREF_ACTIVATE;
1452 /* rmap lock contention: rotate */
1453 if (referenced_ptes == -1)
1454 return PAGEREF_KEEP;
1456 if (referenced_ptes) {
1458 * All mapped folios start out with page table
1459 * references from the instantiating fault, so we need
1460 * to look twice if a mapped file/anon folio is used more
1463 * Mark it and spare it for another trip around the
1464 * inactive list. Another page table reference will
1465 * lead to its activation.
1467 * Note: the mark is set for activated folios as well
1468 * so that recently deactivated but used folios are
1469 * quickly recovered.
1471 folio_set_referenced(folio);
1473 if (referenced_folio || referenced_ptes > 1)
1474 return PAGEREF_ACTIVATE;
1477 * Activate file-backed executable folios after first usage.
1479 if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio))
1480 return PAGEREF_ACTIVATE;
1482 return PAGEREF_KEEP;
1485 /* Reclaim if clean, defer dirty folios to writeback */
1486 if (referenced_folio && folio_is_file_lru(folio))
1487 return PAGEREF_RECLAIM_CLEAN;
1489 return PAGEREF_RECLAIM;
1492 /* Check if a page is dirty or under writeback */
1493 static void folio_check_dirty_writeback(struct folio *folio,
1494 bool *dirty, bool *writeback)
1496 struct address_space *mapping;
1499 * Anonymous pages are not handled by flushers and must be written
1500 * from reclaim context. Do not stall reclaim based on them.
1501 * MADV_FREE anonymous pages are put into inactive file list too.
1502 * They could be mistakenly treated as file lru. So further anon
1505 if (!folio_is_file_lru(folio) ||
1506 (folio_test_anon(folio) && !folio_test_swapbacked(folio))) {
1512 /* By default assume that the folio flags are accurate */
1513 *dirty = folio_test_dirty(folio);
1514 *writeback = folio_test_writeback(folio);
1516 /* Verify dirty/writeback state if the filesystem supports it */
1517 if (!folio_test_private(folio))
1520 mapping = folio_mapping(folio);
1521 if (mapping && mapping->a_ops->is_dirty_writeback)
1522 mapping->a_ops->is_dirty_writeback(folio, dirty, writeback);
1525 static struct page *alloc_demote_page(struct page *page, unsigned long node)
1527 struct migration_target_control mtc = {
1529 * Allocate from 'node', or fail quickly and quietly.
1530 * When this happens, 'page' will likely just be discarded
1531 * instead of migrated.
1533 .gfp_mask = (GFP_HIGHUSER_MOVABLE & ~__GFP_RECLAIM) |
1534 __GFP_THISNODE | __GFP_NOWARN |
1535 __GFP_NOMEMALLOC | GFP_NOWAIT,
1539 return alloc_migration_target(page, (unsigned long)&mtc);
1543 * Take pages on @demote_list and attempt to demote them to
1544 * another node. Pages which are not demoted are left on
1547 static unsigned int demote_page_list(struct list_head *demote_pages,
1548 struct pglist_data *pgdat)
1550 int target_nid = next_demotion_node(pgdat->node_id);
1551 unsigned int nr_succeeded;
1553 if (list_empty(demote_pages))
1556 if (target_nid == NUMA_NO_NODE)
1559 /* Demotion ignores all cpuset and mempolicy settings */
1560 migrate_pages(demote_pages, alloc_demote_page, NULL,
1561 target_nid, MIGRATE_ASYNC, MR_DEMOTION,
1564 if (current_is_kswapd())
1565 __count_vm_events(PGDEMOTE_KSWAPD, nr_succeeded);
1567 __count_vm_events(PGDEMOTE_DIRECT, nr_succeeded);
1569 return nr_succeeded;
1572 static bool may_enter_fs(struct folio *folio, gfp_t gfp_mask)
1574 if (gfp_mask & __GFP_FS)
1576 if (!folio_test_swapcache(folio) || !(gfp_mask & __GFP_IO))
1579 * We can "enter_fs" for swap-cache with only __GFP_IO
1580 * providing this isn't SWP_FS_OPS.
1581 * ->flags can be updated non-atomicially (scan_swap_map_slots),
1582 * but that will never affect SWP_FS_OPS, so the data_race
1585 return !data_race(folio_swap_flags(folio) & SWP_FS_OPS);
1589 * shrink_page_list() returns the number of reclaimed pages
1591 static unsigned int shrink_page_list(struct list_head *page_list,
1592 struct pglist_data *pgdat,
1593 struct scan_control *sc,
1594 struct reclaim_stat *stat,
1595 bool ignore_references)
1597 LIST_HEAD(ret_pages);
1598 LIST_HEAD(free_pages);
1599 LIST_HEAD(demote_pages);
1600 unsigned int nr_reclaimed = 0;
1601 unsigned int pgactivate = 0;
1602 bool do_demote_pass;
1603 struct swap_iocb *plug = NULL;
1605 memset(stat, 0, sizeof(*stat));
1607 do_demote_pass = can_demote(pgdat->node_id, sc);
1610 while (!list_empty(page_list)) {
1611 struct address_space *mapping;
1612 struct folio *folio;
1613 enum page_references references = PAGEREF_RECLAIM;
1614 bool dirty, writeback;
1615 unsigned int nr_pages;
1619 folio = lru_to_folio(page_list);
1620 list_del(&folio->lru);
1622 if (!folio_trylock(folio))
1625 VM_BUG_ON_FOLIO(folio_test_active(folio), folio);
1627 nr_pages = folio_nr_pages(folio);
1629 /* Account the number of base pages */
1630 sc->nr_scanned += nr_pages;
1632 if (unlikely(!folio_evictable(folio)))
1633 goto activate_locked;
1635 if (!sc->may_unmap && folio_mapped(folio))
1638 /* folio_update_gen() tried to promote this page? */
1639 if (lru_gen_enabled() && !ignore_references &&
1640 folio_mapped(folio) && folio_test_referenced(folio))
1644 * The number of dirty pages determines if a node is marked
1645 * reclaim_congested. kswapd will stall and start writing
1646 * folios if the tail of the LRU is all dirty unqueued folios.
1648 folio_check_dirty_writeback(folio, &dirty, &writeback);
1649 if (dirty || writeback)
1650 stat->nr_dirty += nr_pages;
1652 if (dirty && !writeback)
1653 stat->nr_unqueued_dirty += nr_pages;
1656 * Treat this folio as congested if folios are cycling
1657 * through the LRU so quickly that the folios marked
1658 * for immediate reclaim are making it to the end of
1659 * the LRU a second time.
1661 if (writeback && folio_test_reclaim(folio))
1662 stat->nr_congested += nr_pages;
1665 * If a folio at the tail of the LRU is under writeback, there
1666 * are three cases to consider.
1668 * 1) If reclaim is encountering an excessive number
1669 * of folios under writeback and this folio has both
1670 * the writeback and reclaim flags set, then it
1671 * indicates that folios are being queued for I/O but
1672 * are being recycled through the LRU before the I/O
1673 * can complete. Waiting on the folio itself risks an
1674 * indefinite stall if it is impossible to writeback
1675 * the folio due to I/O error or disconnected storage
1676 * so instead note that the LRU is being scanned too
1677 * quickly and the caller can stall after the folio
1678 * list has been processed.
1680 * 2) Global or new memcg reclaim encounters a folio that is
1681 * not marked for immediate reclaim, or the caller does not
1682 * have __GFP_FS (or __GFP_IO if it's simply going to swap,
1683 * not to fs). In this case mark the folio for immediate
1684 * reclaim and continue scanning.
1686 * Require may_enter_fs() because we would wait on fs, which
1687 * may not have submitted I/O yet. And the loop driver might
1688 * enter reclaim, and deadlock if it waits on a folio for
1689 * which it is needed to do the write (loop masks off
1690 * __GFP_IO|__GFP_FS for this reason); but more thought
1691 * would probably show more reasons.
1693 * 3) Legacy memcg encounters a folio that already has the
1694 * reclaim flag set. memcg does not have any dirty folio
1695 * throttling so we could easily OOM just because too many
1696 * folios are in writeback and there is nothing else to
1697 * reclaim. Wait for the writeback to complete.
1699 * In cases 1) and 2) we activate the folios to get them out of
1700 * the way while we continue scanning for clean folios on the
1701 * inactive list and refilling from the active list. The
1702 * observation here is that waiting for disk writes is more
1703 * expensive than potentially causing reloads down the line.
1704 * Since they're marked for immediate reclaim, they won't put
1705 * memory pressure on the cache working set any longer than it
1706 * takes to write them to disk.
1708 if (folio_test_writeback(folio)) {
1710 if (current_is_kswapd() &&
1711 folio_test_reclaim(folio) &&
1712 test_bit(PGDAT_WRITEBACK, &pgdat->flags)) {
1713 stat->nr_immediate += nr_pages;
1714 goto activate_locked;
1717 } else if (writeback_throttling_sane(sc) ||
1718 !folio_test_reclaim(folio) ||
1719 !may_enter_fs(folio, sc->gfp_mask)) {
1721 * This is slightly racy -
1722 * folio_end_writeback() might have
1723 * just cleared the reclaim flag, then
1724 * setting the reclaim flag here ends up
1725 * interpreted as the readahead flag - but
1726 * that does not matter enough to care.
1727 * What we do want is for this folio to
1728 * have the reclaim flag set next time
1729 * memcg reclaim reaches the tests above,
1730 * so it will then wait for writeback to
1731 * avoid OOM; and it's also appropriate
1732 * in global reclaim.
1734 folio_set_reclaim(folio);
1735 stat->nr_writeback += nr_pages;
1736 goto activate_locked;
1740 folio_unlock(folio);
1741 folio_wait_writeback(folio);
1742 /* then go back and try same folio again */
1743 list_add_tail(&folio->lru, page_list);
1748 if (!ignore_references)
1749 references = folio_check_references(folio, sc);
1751 switch (references) {
1752 case PAGEREF_ACTIVATE:
1753 goto activate_locked;
1755 stat->nr_ref_keep += nr_pages;
1757 case PAGEREF_RECLAIM:
1758 case PAGEREF_RECLAIM_CLEAN:
1759 ; /* try to reclaim the folio below */
1763 * Before reclaiming the folio, try to relocate
1764 * its contents to another node.
1766 if (do_demote_pass &&
1767 (thp_migration_supported() || !folio_test_large(folio))) {
1768 list_add(&folio->lru, &demote_pages);
1769 folio_unlock(folio);
1774 * Anonymous process memory has backing store?
1775 * Try to allocate it some swap space here.
1776 * Lazyfree folio could be freed directly
1778 if (folio_test_anon(folio) && folio_test_swapbacked(folio)) {
1779 if (!folio_test_swapcache(folio)) {
1780 if (!(sc->gfp_mask & __GFP_IO))
1782 if (folio_maybe_dma_pinned(folio))
1784 if (folio_test_large(folio)) {
1785 /* cannot split folio, skip it */
1786 if (!can_split_folio(folio, NULL))
1787 goto activate_locked;
1789 * Split folios without a PMD map right
1790 * away. Chances are some or all of the
1791 * tail pages can be freed without IO.
1793 if (!folio_entire_mapcount(folio) &&
1794 split_folio_to_list(folio,
1796 goto activate_locked;
1798 if (!add_to_swap(folio)) {
1799 if (!folio_test_large(folio))
1800 goto activate_locked_split;
1801 /* Fallback to swap normal pages */
1802 if (split_folio_to_list(folio,
1804 goto activate_locked;
1805 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1806 count_vm_event(THP_SWPOUT_FALLBACK);
1808 if (!add_to_swap(folio))
1809 goto activate_locked_split;
1812 } else if (folio_test_swapbacked(folio) &&
1813 folio_test_large(folio)) {
1814 /* Split shmem folio */
1815 if (split_folio_to_list(folio, page_list))
1820 * If the folio was split above, the tail pages will make
1821 * their own pass through this function and be accounted
1824 if ((nr_pages > 1) && !folio_test_large(folio)) {
1825 sc->nr_scanned -= (nr_pages - 1);
1830 * The folio is mapped into the page tables of one or more
1831 * processes. Try to unmap it here.
1833 if (folio_mapped(folio)) {
1834 enum ttu_flags flags = TTU_BATCH_FLUSH;
1835 bool was_swapbacked = folio_test_swapbacked(folio);
1837 if (folio_test_pmd_mappable(folio))
1838 flags |= TTU_SPLIT_HUGE_PMD;
1840 try_to_unmap(folio, flags);
1841 if (folio_mapped(folio)) {
1842 stat->nr_unmap_fail += nr_pages;
1843 if (!was_swapbacked &&
1844 folio_test_swapbacked(folio))
1845 stat->nr_lazyfree_fail += nr_pages;
1846 goto activate_locked;
1850 mapping = folio_mapping(folio);
1851 if (folio_test_dirty(folio)) {
1853 * Only kswapd can writeback filesystem folios
1854 * to avoid risk of stack overflow. But avoid
1855 * injecting inefficient single-folio I/O into
1856 * flusher writeback as much as possible: only
1857 * write folios when we've encountered many
1858 * dirty folios, and when we've already scanned
1859 * the rest of the LRU for clean folios and see
1860 * the same dirty folios again (with the reclaim
1863 if (folio_is_file_lru(folio) &&
1864 (!current_is_kswapd() ||
1865 !folio_test_reclaim(folio) ||
1866 !test_bit(PGDAT_DIRTY, &pgdat->flags))) {
1868 * Immediately reclaim when written back.
1869 * Similar in principle to deactivate_page()
1870 * except we already have the folio isolated
1871 * and know it's dirty
1873 node_stat_mod_folio(folio, NR_VMSCAN_IMMEDIATE,
1875 folio_set_reclaim(folio);
1877 goto activate_locked;
1880 if (references == PAGEREF_RECLAIM_CLEAN)
1882 if (!may_enter_fs(folio, sc->gfp_mask))
1884 if (!sc->may_writepage)
1888 * Folio is dirty. Flush the TLB if a writable entry
1889 * potentially exists to avoid CPU writes after I/O
1890 * starts and then write it out here.
1892 try_to_unmap_flush_dirty();
1893 switch (pageout(folio, mapping, &plug)) {
1897 goto activate_locked;
1899 stat->nr_pageout += nr_pages;
1901 if (folio_test_writeback(folio))
1903 if (folio_test_dirty(folio))
1907 * A synchronous write - probably a ramdisk. Go
1908 * ahead and try to reclaim the folio.
1910 if (!folio_trylock(folio))
1912 if (folio_test_dirty(folio) ||
1913 folio_test_writeback(folio))
1915 mapping = folio_mapping(folio);
1918 ; /* try to free the folio below */
1923 * If the folio has buffers, try to free the buffer
1924 * mappings associated with this folio. If we succeed
1925 * we try to free the folio as well.
1927 * We do this even if the folio is dirty.
1928 * filemap_release_folio() does not perform I/O, but it
1929 * is possible for a folio to have the dirty flag set,
1930 * but it is actually clean (all its buffers are clean).
1931 * This happens if the buffers were written out directly,
1932 * with submit_bh(). ext3 will do this, as well as
1933 * the blockdev mapping. filemap_release_folio() will
1934 * discover that cleanness and will drop the buffers
1935 * and mark the folio clean - it can be freed.
1937 * Rarely, folios can have buffers and no ->mapping.
1938 * These are the folios which were not successfully
1939 * invalidated in truncate_cleanup_folio(). We try to
1940 * drop those buffers here and if that worked, and the
1941 * folio is no longer mapped into process address space
1942 * (refcount == 1) it can be freed. Otherwise, leave
1943 * the folio on the LRU so it is swappable.
1945 if (folio_has_private(folio)) {
1946 if (!filemap_release_folio(folio, sc->gfp_mask))
1947 goto activate_locked;
1948 if (!mapping && folio_ref_count(folio) == 1) {
1949 folio_unlock(folio);
1950 if (folio_put_testzero(folio))
1954 * rare race with speculative reference.
1955 * the speculative reference will free
1956 * this folio shortly, so we may
1957 * increment nr_reclaimed here (and
1958 * leave it off the LRU).
1960 nr_reclaimed += nr_pages;
1966 if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) {
1967 /* follow __remove_mapping for reference */
1968 if (!folio_ref_freeze(folio, 1))
1971 * The folio has only one reference left, which is
1972 * from the isolation. After the caller puts the
1973 * folio back on the lru and drops the reference, the
1974 * folio will be freed anyway. It doesn't matter
1975 * which lru it goes on. So we don't bother checking
1976 * the dirty flag here.
1978 count_vm_events(PGLAZYFREED, nr_pages);
1979 count_memcg_folio_events(folio, PGLAZYFREED, nr_pages);
1980 } else if (!mapping || !__remove_mapping(mapping, folio, true,
1981 sc->target_mem_cgroup))
1984 folio_unlock(folio);
1987 * Folio may get swapped out as a whole, need to account
1990 nr_reclaimed += nr_pages;
1993 * Is there need to periodically free_page_list? It would
1994 * appear not as the counts should be low
1996 if (unlikely(folio_test_large(folio)))
1997 destroy_large_folio(folio);
1999 list_add(&folio->lru, &free_pages);
2002 activate_locked_split:
2004 * The tail pages that are failed to add into swap cache
2005 * reach here. Fixup nr_scanned and nr_pages.
2008 sc->nr_scanned -= (nr_pages - 1);
2012 /* Not a candidate for swapping, so reclaim swap space. */
2013 if (folio_test_swapcache(folio) &&
2014 (mem_cgroup_swap_full(&folio->page) ||
2015 folio_test_mlocked(folio)))
2016 try_to_free_swap(&folio->page);
2017 VM_BUG_ON_FOLIO(folio_test_active(folio), folio);
2018 if (!folio_test_mlocked(folio)) {
2019 int type = folio_is_file_lru(folio);
2020 folio_set_active(folio);
2021 stat->nr_activate[type] += nr_pages;
2022 count_memcg_folio_events(folio, PGACTIVATE, nr_pages);
2025 folio_unlock(folio);
2027 list_add(&folio->lru, &ret_pages);
2028 VM_BUG_ON_FOLIO(folio_test_lru(folio) ||
2029 folio_test_unevictable(folio), folio);
2031 /* 'page_list' is always empty here */
2033 /* Migrate folios selected for demotion */
2034 nr_reclaimed += demote_page_list(&demote_pages, pgdat);
2035 /* Folios that could not be demoted are still in @demote_pages */
2036 if (!list_empty(&demote_pages)) {
2037 /* Folios which weren't demoted go back on @page_list for retry: */
2038 list_splice_init(&demote_pages, page_list);
2039 do_demote_pass = false;
2043 pgactivate = stat->nr_activate[0] + stat->nr_activate[1];
2045 mem_cgroup_uncharge_list(&free_pages);
2046 try_to_unmap_flush();
2047 free_unref_page_list(&free_pages);
2049 list_splice(&ret_pages, page_list);
2050 count_vm_events(PGACTIVATE, pgactivate);
2053 swap_write_unplug(plug);
2054 return nr_reclaimed;
2057 unsigned int reclaim_clean_pages_from_list(struct zone *zone,
2058 struct list_head *folio_list)
2060 struct scan_control sc = {
2061 .gfp_mask = GFP_KERNEL,
2064 struct reclaim_stat stat;
2065 unsigned int nr_reclaimed;
2066 struct folio *folio, *next;
2067 LIST_HEAD(clean_folios);
2068 unsigned int noreclaim_flag;
2070 list_for_each_entry_safe(folio, next, folio_list, lru) {
2071 if (!folio_test_hugetlb(folio) && folio_is_file_lru(folio) &&
2072 !folio_test_dirty(folio) && !__folio_test_movable(folio) &&
2073 !folio_test_unevictable(folio)) {
2074 folio_clear_active(folio);
2075 list_move(&folio->lru, &clean_folios);
2080 * We should be safe here since we are only dealing with file pages and
2081 * we are not kswapd and therefore cannot write dirty file pages. But
2082 * call memalloc_noreclaim_save() anyway, just in case these conditions
2083 * change in the future.
2085 noreclaim_flag = memalloc_noreclaim_save();
2086 nr_reclaimed = shrink_page_list(&clean_folios, zone->zone_pgdat, &sc,
2088 memalloc_noreclaim_restore(noreclaim_flag);
2090 list_splice(&clean_folios, folio_list);
2091 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
2092 -(long)nr_reclaimed);
2094 * Since lazyfree pages are isolated from file LRU from the beginning,
2095 * they will rotate back to anonymous LRU in the end if it failed to
2096 * discard so isolated count will be mismatched.
2097 * Compensate the isolated count for both LRU lists.
2099 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON,
2100 stat.nr_lazyfree_fail);
2101 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
2102 -(long)stat.nr_lazyfree_fail);
2103 return nr_reclaimed;
2107 * Update LRU sizes after isolating pages. The LRU size updates must
2108 * be complete before mem_cgroup_update_lru_size due to a sanity check.
2110 static __always_inline void update_lru_sizes(struct lruvec *lruvec,
2111 enum lru_list lru, unsigned long *nr_zone_taken)
2115 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2116 if (!nr_zone_taken[zid])
2119 update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
2125 * Isolating page from the lruvec to fill in @dst list by nr_to_scan times.
2127 * lruvec->lru_lock is heavily contended. Some of the functions that
2128 * shrink the lists perform better by taking out a batch of pages
2129 * and working on them outside the LRU lock.
2131 * For pagecache intensive workloads, this function is the hottest
2132 * spot in the kernel (apart from copy_*_user functions).
2134 * Lru_lock must be held before calling this function.
2136 * @nr_to_scan: The number of eligible pages to look through on the list.
2137 * @lruvec: The LRU vector to pull pages from.
2138 * @dst: The temp list to put pages on to.
2139 * @nr_scanned: The number of pages that were scanned.
2140 * @sc: The scan_control struct for this reclaim session
2141 * @lru: LRU list id for isolating
2143 * returns how many pages were moved onto *@dst.
2145 static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
2146 struct lruvec *lruvec, struct list_head *dst,
2147 unsigned long *nr_scanned, struct scan_control *sc,
2150 struct list_head *src = &lruvec->lists[lru];
2151 unsigned long nr_taken = 0;
2152 unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 };
2153 unsigned long nr_skipped[MAX_NR_ZONES] = { 0, };
2154 unsigned long skipped = 0;
2155 unsigned long scan, total_scan, nr_pages;
2156 LIST_HEAD(folios_skipped);
2160 while (scan < nr_to_scan && !list_empty(src)) {
2161 struct list_head *move_to = src;
2162 struct folio *folio;
2164 folio = lru_to_folio(src);
2165 prefetchw_prev_lru_folio(folio, src, flags);
2167 nr_pages = folio_nr_pages(folio);
2168 total_scan += nr_pages;
2170 if (folio_zonenum(folio) > sc->reclaim_idx) {
2171 nr_skipped[folio_zonenum(folio)] += nr_pages;
2172 move_to = &folios_skipped;
2177 * Do not count skipped folios because that makes the function
2178 * return with no isolated folios if the LRU mostly contains
2179 * ineligible folios. This causes the VM to not reclaim any
2180 * folios, triggering a premature OOM.
2181 * Account all pages in a folio.
2185 if (!folio_test_lru(folio))
2187 if (!sc->may_unmap && folio_mapped(folio))
2191 * Be careful not to clear the lru flag until after we're
2192 * sure the folio is not being freed elsewhere -- the
2193 * folio release code relies on it.
2195 if (unlikely(!folio_try_get(folio)))
2198 if (!folio_test_clear_lru(folio)) {
2199 /* Another thread is already isolating this folio */
2204 nr_taken += nr_pages;
2205 nr_zone_taken[folio_zonenum(folio)] += nr_pages;
2208 list_move(&folio->lru, move_to);
2212 * Splice any skipped folios to the start of the LRU list. Note that
2213 * this disrupts the LRU order when reclaiming for lower zones but
2214 * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX
2215 * scanning would soon rescan the same folios to skip and waste lots
2218 if (!list_empty(&folios_skipped)) {
2221 list_splice(&folios_skipped, src);
2222 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2223 if (!nr_skipped[zid])
2226 __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]);
2227 skipped += nr_skipped[zid];
2230 *nr_scanned = total_scan;
2231 trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan,
2232 total_scan, skipped, nr_taken,
2233 sc->may_unmap ? 0 : ISOLATE_UNMAPPED, lru);
2234 update_lru_sizes(lruvec, lru, nr_zone_taken);
2239 * folio_isolate_lru() - Try to isolate a folio from its LRU list.
2240 * @folio: Folio to isolate from its LRU list.
2242 * Isolate a @folio from an LRU list and adjust the vmstat statistic
2243 * corresponding to whatever LRU list the folio was on.
2245 * The folio will have its LRU flag cleared. If it was found on the
2246 * active list, it will have the Active flag set. If it was found on the
2247 * unevictable list, it will have the Unevictable flag set. These flags
2248 * may need to be cleared by the caller before letting the page go.
2252 * (1) Must be called with an elevated refcount on the page. This is a
2253 * fundamental difference from isolate_lru_pages() (which is called
2254 * without a stable reference).
2255 * (2) The lru_lock must not be held.
2256 * (3) Interrupts must be enabled.
2258 * Return: 0 if the folio was removed from an LRU list.
2259 * -EBUSY if the folio was not on an LRU list.
2261 int folio_isolate_lru(struct folio *folio)
2265 VM_BUG_ON_FOLIO(!folio_ref_count(folio), folio);
2267 if (folio_test_clear_lru(folio)) {
2268 struct lruvec *lruvec;
2271 lruvec = folio_lruvec_lock_irq(folio);
2272 lruvec_del_folio(lruvec, folio);
2273 unlock_page_lruvec_irq(lruvec);
2281 * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
2282 * then get rescheduled. When there are massive number of tasks doing page
2283 * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
2284 * the LRU list will go small and be scanned faster than necessary, leading to
2285 * unnecessary swapping, thrashing and OOM.
2287 static int too_many_isolated(struct pglist_data *pgdat, int file,
2288 struct scan_control *sc)
2290 unsigned long inactive, isolated;
2293 if (current_is_kswapd())
2296 if (!writeback_throttling_sane(sc))
2300 inactive = node_page_state(pgdat, NR_INACTIVE_FILE);
2301 isolated = node_page_state(pgdat, NR_ISOLATED_FILE);
2303 inactive = node_page_state(pgdat, NR_INACTIVE_ANON);
2304 isolated = node_page_state(pgdat, NR_ISOLATED_ANON);
2308 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
2309 * won't get blocked by normal direct-reclaimers, forming a circular
2312 if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
2315 too_many = isolated > inactive;
2317 /* Wake up tasks throttled due to too_many_isolated. */
2319 wake_throttle_isolated(pgdat);
2325 * move_pages_to_lru() moves folios from private @list to appropriate LRU list.
2326 * On return, @list is reused as a list of folios to be freed by the caller.
2328 * Returns the number of pages moved to the given lruvec.
2330 static unsigned int move_pages_to_lru(struct lruvec *lruvec,
2331 struct list_head *list)
2333 int nr_pages, nr_moved = 0;
2334 LIST_HEAD(folios_to_free);
2336 while (!list_empty(list)) {
2337 struct folio *folio = lru_to_folio(list);
2339 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
2340 list_del(&folio->lru);
2341 if (unlikely(!folio_evictable(folio))) {
2342 spin_unlock_irq(&lruvec->lru_lock);
2343 folio_putback_lru(folio);
2344 spin_lock_irq(&lruvec->lru_lock);
2349 * The folio_set_lru needs to be kept here for list integrity.
2351 * #0 move_pages_to_lru #1 release_pages
2352 * if (!folio_put_testzero())
2353 * if (folio_put_testzero())
2354 * !lru //skip lru_lock
2356 * list_add(&folio->lru,)
2357 * list_add(&folio->lru,)
2359 folio_set_lru(folio);
2361 if (unlikely(folio_put_testzero(folio))) {
2362 __folio_clear_lru_flags(folio);
2364 if (unlikely(folio_test_large(folio))) {
2365 spin_unlock_irq(&lruvec->lru_lock);
2366 destroy_large_folio(folio);
2367 spin_lock_irq(&lruvec->lru_lock);
2369 list_add(&folio->lru, &folios_to_free);
2375 * All pages were isolated from the same lruvec (and isolation
2376 * inhibits memcg migration).
2378 VM_BUG_ON_FOLIO(!folio_matches_lruvec(folio, lruvec), folio);
2379 lruvec_add_folio(lruvec, folio);
2380 nr_pages = folio_nr_pages(folio);
2381 nr_moved += nr_pages;
2382 if (folio_test_active(folio))
2383 workingset_age_nonresident(lruvec, nr_pages);
2387 * To save our caller's stack, now use input list for pages to free.
2389 list_splice(&folios_to_free, list);
2395 * If a kernel thread (such as nfsd for loop-back mounts) services a backing
2396 * device by writing to the page cache it sets PF_LOCAL_THROTTLE. In this case
2397 * we should not throttle. Otherwise it is safe to do so.
2399 static int current_may_throttle(void)
2401 return !(current->flags & PF_LOCAL_THROTTLE);
2405 * shrink_inactive_list() is a helper for shrink_node(). It returns the number
2406 * of reclaimed pages
2408 static unsigned long
2409 shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
2410 struct scan_control *sc, enum lru_list lru)
2412 LIST_HEAD(page_list);
2413 unsigned long nr_scanned;
2414 unsigned int nr_reclaimed = 0;
2415 unsigned long nr_taken;
2416 struct reclaim_stat stat;
2417 bool file = is_file_lru(lru);
2418 enum vm_event_item item;
2419 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2420 bool stalled = false;
2422 while (unlikely(too_many_isolated(pgdat, file, sc))) {
2426 /* wait a bit for the reclaimer. */
2428 reclaim_throttle(pgdat, VMSCAN_THROTTLE_ISOLATED);
2430 /* We are about to die and free our memory. Return now. */
2431 if (fatal_signal_pending(current))
2432 return SWAP_CLUSTER_MAX;
2437 spin_lock_irq(&lruvec->lru_lock);
2439 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
2440 &nr_scanned, sc, lru);
2442 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
2443 item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT;
2444 if (!cgroup_reclaim(sc))
2445 __count_vm_events(item, nr_scanned);
2446 __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned);
2447 __count_vm_events(PGSCAN_ANON + file, nr_scanned);
2449 spin_unlock_irq(&lruvec->lru_lock);
2454 nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, &stat, false);
2456 spin_lock_irq(&lruvec->lru_lock);
2457 move_pages_to_lru(lruvec, &page_list);
2459 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
2460 item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT;
2461 if (!cgroup_reclaim(sc))
2462 __count_vm_events(item, nr_reclaimed);
2463 __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed);
2464 __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed);
2465 spin_unlock_irq(&lruvec->lru_lock);
2467 lru_note_cost(lruvec, file, stat.nr_pageout);
2468 mem_cgroup_uncharge_list(&page_list);
2469 free_unref_page_list(&page_list);
2472 * If dirty pages are scanned that are not queued for IO, it
2473 * implies that flushers are not doing their job. This can
2474 * happen when memory pressure pushes dirty pages to the end of
2475 * the LRU before the dirty limits are breached and the dirty
2476 * data has expired. It can also happen when the proportion of
2477 * dirty pages grows not through writes but through memory
2478 * pressure reclaiming all the clean cache. And in some cases,
2479 * the flushers simply cannot keep up with the allocation
2480 * rate. Nudge the flusher threads in case they are asleep.
2482 if (stat.nr_unqueued_dirty == nr_taken)
2483 wakeup_flusher_threads(WB_REASON_VMSCAN);
2485 sc->nr.dirty += stat.nr_dirty;
2486 sc->nr.congested += stat.nr_congested;
2487 sc->nr.unqueued_dirty += stat.nr_unqueued_dirty;
2488 sc->nr.writeback += stat.nr_writeback;
2489 sc->nr.immediate += stat.nr_immediate;
2490 sc->nr.taken += nr_taken;
2492 sc->nr.file_taken += nr_taken;
2494 trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
2495 nr_scanned, nr_reclaimed, &stat, sc->priority, file);
2496 return nr_reclaimed;
2500 * shrink_active_list() moves folios from the active LRU to the inactive LRU.
2502 * We move them the other way if the folio is referenced by one or more
2505 * If the folios are mostly unmapped, the processing is fast and it is
2506 * appropriate to hold lru_lock across the whole operation. But if
2507 * the folios are mapped, the processing is slow (folio_referenced()), so
2508 * we should drop lru_lock around each folio. It's impossible to balance
2509 * this, so instead we remove the folios from the LRU while processing them.
2510 * It is safe to rely on the active flag against the non-LRU folios in here
2511 * because nobody will play with that bit on a non-LRU folio.
2513 * The downside is that we have to touch folio->_refcount against each folio.
2514 * But we had to alter folio->flags anyway.
2516 static void shrink_active_list(unsigned long nr_to_scan,
2517 struct lruvec *lruvec,
2518 struct scan_control *sc,
2521 unsigned long nr_taken;
2522 unsigned long nr_scanned;
2523 unsigned long vm_flags;
2524 LIST_HEAD(l_hold); /* The folios which were snipped off */
2525 LIST_HEAD(l_active);
2526 LIST_HEAD(l_inactive);
2527 unsigned nr_deactivate, nr_activate;
2528 unsigned nr_rotated = 0;
2529 int file = is_file_lru(lru);
2530 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2534 spin_lock_irq(&lruvec->lru_lock);
2536 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
2537 &nr_scanned, sc, lru);
2539 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
2541 if (!cgroup_reclaim(sc))
2542 __count_vm_events(PGREFILL, nr_scanned);
2543 __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned);
2545 spin_unlock_irq(&lruvec->lru_lock);
2547 while (!list_empty(&l_hold)) {
2548 struct folio *folio;
2551 folio = lru_to_folio(&l_hold);
2552 list_del(&folio->lru);
2554 if (unlikely(!folio_evictable(folio))) {
2555 folio_putback_lru(folio);
2559 if (unlikely(buffer_heads_over_limit)) {
2560 if (folio_test_private(folio) && folio_trylock(folio)) {
2561 if (folio_test_private(folio))
2562 filemap_release_folio(folio, 0);
2563 folio_unlock(folio);
2567 /* Referenced or rmap lock contention: rotate */
2568 if (folio_referenced(folio, 0, sc->target_mem_cgroup,
2571 * Identify referenced, file-backed active folios and
2572 * give them one more trip around the active list. So
2573 * that executable code get better chances to stay in
2574 * memory under moderate memory pressure. Anon folios
2575 * are not likely to be evicted by use-once streaming
2576 * IO, plus JVM can create lots of anon VM_EXEC folios,
2577 * so we ignore them here.
2579 if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio)) {
2580 nr_rotated += folio_nr_pages(folio);
2581 list_add(&folio->lru, &l_active);
2586 folio_clear_active(folio); /* we are de-activating */
2587 folio_set_workingset(folio);
2588 list_add(&folio->lru, &l_inactive);
2592 * Move folios back to the lru list.
2594 spin_lock_irq(&lruvec->lru_lock);
2596 nr_activate = move_pages_to_lru(lruvec, &l_active);
2597 nr_deactivate = move_pages_to_lru(lruvec, &l_inactive);
2598 /* Keep all free folios in l_active list */
2599 list_splice(&l_inactive, &l_active);
2601 __count_vm_events(PGDEACTIVATE, nr_deactivate);
2602 __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate);
2604 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
2605 spin_unlock_irq(&lruvec->lru_lock);
2607 mem_cgroup_uncharge_list(&l_active);
2608 free_unref_page_list(&l_active);
2609 trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate,
2610 nr_deactivate, nr_rotated, sc->priority, file);
2613 static unsigned int reclaim_page_list(struct list_head *page_list,
2614 struct pglist_data *pgdat)
2616 struct reclaim_stat dummy_stat;
2617 unsigned int nr_reclaimed;
2618 struct folio *folio;
2619 struct scan_control sc = {
2620 .gfp_mask = GFP_KERNEL,
2627 nr_reclaimed = shrink_page_list(page_list, pgdat, &sc, &dummy_stat, false);
2628 while (!list_empty(page_list)) {
2629 folio = lru_to_folio(page_list);
2630 list_del(&folio->lru);
2631 folio_putback_lru(folio);
2634 return nr_reclaimed;
2637 unsigned long reclaim_pages(struct list_head *folio_list)
2640 unsigned int nr_reclaimed = 0;
2641 LIST_HEAD(node_folio_list);
2642 unsigned int noreclaim_flag;
2644 if (list_empty(folio_list))
2645 return nr_reclaimed;
2647 noreclaim_flag = memalloc_noreclaim_save();
2649 nid = folio_nid(lru_to_folio(folio_list));
2651 struct folio *folio = lru_to_folio(folio_list);
2653 if (nid == folio_nid(folio)) {
2654 folio_clear_active(folio);
2655 list_move(&folio->lru, &node_folio_list);
2659 nr_reclaimed += reclaim_page_list(&node_folio_list, NODE_DATA(nid));
2660 nid = folio_nid(lru_to_folio(folio_list));
2661 } while (!list_empty(folio_list));
2663 nr_reclaimed += reclaim_page_list(&node_folio_list, NODE_DATA(nid));
2665 memalloc_noreclaim_restore(noreclaim_flag);
2667 return nr_reclaimed;
2670 static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
2671 struct lruvec *lruvec, struct scan_control *sc)
2673 if (is_active_lru(lru)) {
2674 if (sc->may_deactivate & (1 << is_file_lru(lru)))
2675 shrink_active_list(nr_to_scan, lruvec, sc, lru);
2677 sc->skipped_deactivate = 1;
2681 return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
2685 * The inactive anon list should be small enough that the VM never has
2686 * to do too much work.
2688 * The inactive file list should be small enough to leave most memory
2689 * to the established workingset on the scan-resistant active list,
2690 * but large enough to avoid thrashing the aggregate readahead window.
2692 * Both inactive lists should also be large enough that each inactive
2693 * page has a chance to be referenced again before it is reclaimed.
2695 * If that fails and refaulting is observed, the inactive list grows.
2697 * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages
2698 * on this LRU, maintained by the pageout code. An inactive_ratio
2699 * of 3 means 3:1 or 25% of the pages are kept on the inactive list.
2702 * memory ratio inactive
2703 * -------------------------------------
2712 static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru)
2714 enum lru_list active_lru = inactive_lru + LRU_ACTIVE;
2715 unsigned long inactive, active;
2716 unsigned long inactive_ratio;
2719 inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru);
2720 active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru);
2722 gb = (inactive + active) >> (30 - PAGE_SHIFT);
2724 inactive_ratio = int_sqrt(10 * gb);
2728 return inactive * inactive_ratio < active;
2738 static void prepare_scan_count(pg_data_t *pgdat, struct scan_control *sc)
2741 struct lruvec *target_lruvec;
2743 if (lru_gen_enabled())
2746 target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat);
2749 * Flush the memory cgroup stats, so that we read accurate per-memcg
2750 * lruvec stats for heuristics.
2752 mem_cgroup_flush_stats();
2755 * Determine the scan balance between anon and file LRUs.
2757 spin_lock_irq(&target_lruvec->lru_lock);
2758 sc->anon_cost = target_lruvec->anon_cost;
2759 sc->file_cost = target_lruvec->file_cost;
2760 spin_unlock_irq(&target_lruvec->lru_lock);
2763 * Target desirable inactive:active list ratios for the anon
2764 * and file LRU lists.
2766 if (!sc->force_deactivate) {
2767 unsigned long refaults;
2770 * When refaults are being observed, it means a new
2771 * workingset is being established. Deactivate to get
2772 * rid of any stale active pages quickly.
2774 refaults = lruvec_page_state(target_lruvec,
2775 WORKINGSET_ACTIVATE_ANON);
2776 if (refaults != target_lruvec->refaults[WORKINGSET_ANON] ||
2777 inactive_is_low(target_lruvec, LRU_INACTIVE_ANON))
2778 sc->may_deactivate |= DEACTIVATE_ANON;
2780 sc->may_deactivate &= ~DEACTIVATE_ANON;
2782 refaults = lruvec_page_state(target_lruvec,
2783 WORKINGSET_ACTIVATE_FILE);
2784 if (refaults != target_lruvec->refaults[WORKINGSET_FILE] ||
2785 inactive_is_low(target_lruvec, LRU_INACTIVE_FILE))
2786 sc->may_deactivate |= DEACTIVATE_FILE;
2788 sc->may_deactivate &= ~DEACTIVATE_FILE;
2790 sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE;
2793 * If we have plenty of inactive file pages that aren't
2794 * thrashing, try to reclaim those first before touching
2797 file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE);
2798 if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE))
2799 sc->cache_trim_mode = 1;
2801 sc->cache_trim_mode = 0;
2804 * Prevent the reclaimer from falling into the cache trap: as
2805 * cache pages start out inactive, every cache fault will tip
2806 * the scan balance towards the file LRU. And as the file LRU
2807 * shrinks, so does the window for rotation from references.
2808 * This means we have a runaway feedback loop where a tiny
2809 * thrashing file LRU becomes infinitely more attractive than
2810 * anon pages. Try to detect this based on file LRU size.
2812 if (!cgroup_reclaim(sc)) {
2813 unsigned long total_high_wmark = 0;
2814 unsigned long free, anon;
2817 free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES);
2818 file = node_page_state(pgdat, NR_ACTIVE_FILE) +
2819 node_page_state(pgdat, NR_INACTIVE_FILE);
2821 for (z = 0; z < MAX_NR_ZONES; z++) {
2822 struct zone *zone = &pgdat->node_zones[z];
2824 if (!managed_zone(zone))
2827 total_high_wmark += high_wmark_pages(zone);
2831 * Consider anon: if that's low too, this isn't a
2832 * runaway file reclaim problem, but rather just
2833 * extreme pressure. Reclaim as per usual then.
2835 anon = node_page_state(pgdat, NR_INACTIVE_ANON);
2838 file + free <= total_high_wmark &&
2839 !(sc->may_deactivate & DEACTIVATE_ANON) &&
2840 anon >> sc->priority;
2845 * Determine how aggressively the anon and file LRU lists should be
2848 * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
2849 * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
2851 static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
2854 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2855 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
2856 unsigned long anon_cost, file_cost, total_cost;
2857 int swappiness = mem_cgroup_swappiness(memcg);
2858 u64 fraction[ANON_AND_FILE];
2859 u64 denominator = 0; /* gcc */
2860 enum scan_balance scan_balance;
2861 unsigned long ap, fp;
2864 /* If we have no swap space, do not bother scanning anon pages. */
2865 if (!sc->may_swap || !can_reclaim_anon_pages(memcg, pgdat->node_id, sc)) {
2866 scan_balance = SCAN_FILE;
2871 * Global reclaim will swap to prevent OOM even with no
2872 * swappiness, but memcg users want to use this knob to
2873 * disable swapping for individual groups completely when
2874 * using the memory controller's swap limit feature would be
2877 if (cgroup_reclaim(sc) && !swappiness) {
2878 scan_balance = SCAN_FILE;
2883 * Do not apply any pressure balancing cleverness when the
2884 * system is close to OOM, scan both anon and file equally
2885 * (unless the swappiness setting disagrees with swapping).
2887 if (!sc->priority && swappiness) {
2888 scan_balance = SCAN_EQUAL;
2893 * If the system is almost out of file pages, force-scan anon.
2895 if (sc->file_is_tiny) {
2896 scan_balance = SCAN_ANON;
2901 * If there is enough inactive page cache, we do not reclaim
2902 * anything from the anonymous working right now.
2904 if (sc->cache_trim_mode) {
2905 scan_balance = SCAN_FILE;
2909 scan_balance = SCAN_FRACT;
2911 * Calculate the pressure balance between anon and file pages.
2913 * The amount of pressure we put on each LRU is inversely
2914 * proportional to the cost of reclaiming each list, as
2915 * determined by the share of pages that are refaulting, times
2916 * the relative IO cost of bringing back a swapped out
2917 * anonymous page vs reloading a filesystem page (swappiness).
2919 * Although we limit that influence to ensure no list gets
2920 * left behind completely: at least a third of the pressure is
2921 * applied, before swappiness.
2923 * With swappiness at 100, anon and file have equal IO cost.
2925 total_cost = sc->anon_cost + sc->file_cost;
2926 anon_cost = total_cost + sc->anon_cost;
2927 file_cost = total_cost + sc->file_cost;
2928 total_cost = anon_cost + file_cost;
2930 ap = swappiness * (total_cost + 1);
2931 ap /= anon_cost + 1;
2933 fp = (200 - swappiness) * (total_cost + 1);
2934 fp /= file_cost + 1;
2938 denominator = ap + fp;
2940 for_each_evictable_lru(lru) {
2941 int file = is_file_lru(lru);
2942 unsigned long lruvec_size;
2943 unsigned long low, min;
2946 lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
2947 mem_cgroup_protection(sc->target_mem_cgroup, memcg,
2952 * Scale a cgroup's reclaim pressure by proportioning
2953 * its current usage to its memory.low or memory.min
2956 * This is important, as otherwise scanning aggression
2957 * becomes extremely binary -- from nothing as we
2958 * approach the memory protection threshold, to totally
2959 * nominal as we exceed it. This results in requiring
2960 * setting extremely liberal protection thresholds. It
2961 * also means we simply get no protection at all if we
2962 * set it too low, which is not ideal.
2964 * If there is any protection in place, we reduce scan
2965 * pressure by how much of the total memory used is
2966 * within protection thresholds.
2968 * There is one special case: in the first reclaim pass,
2969 * we skip over all groups that are within their low
2970 * protection. If that fails to reclaim enough pages to
2971 * satisfy the reclaim goal, we come back and override
2972 * the best-effort low protection. However, we still
2973 * ideally want to honor how well-behaved groups are in
2974 * that case instead of simply punishing them all
2975 * equally. As such, we reclaim them based on how much
2976 * memory they are using, reducing the scan pressure
2977 * again by how much of the total memory used is under
2980 unsigned long cgroup_size = mem_cgroup_size(memcg);
2981 unsigned long protection;
2983 /* memory.low scaling, make sure we retry before OOM */
2984 if (!sc->memcg_low_reclaim && low > min) {
2986 sc->memcg_low_skipped = 1;
2991 /* Avoid TOCTOU with earlier protection check */
2992 cgroup_size = max(cgroup_size, protection);
2994 scan = lruvec_size - lruvec_size * protection /
2998 * Minimally target SWAP_CLUSTER_MAX pages to keep
2999 * reclaim moving forwards, avoiding decrementing
3000 * sc->priority further than desirable.
3002 scan = max(scan, SWAP_CLUSTER_MAX);
3007 scan >>= sc->priority;
3010 * If the cgroup's already been deleted, make sure to
3011 * scrape out the remaining cache.
3013 if (!scan && !mem_cgroup_online(memcg))
3014 scan = min(lruvec_size, SWAP_CLUSTER_MAX);
3016 switch (scan_balance) {
3018 /* Scan lists relative to size */
3022 * Scan types proportional to swappiness and
3023 * their relative recent reclaim efficiency.
3024 * Make sure we don't miss the last page on
3025 * the offlined memory cgroups because of a
3028 scan = mem_cgroup_online(memcg) ?
3029 div64_u64(scan * fraction[file], denominator) :
3030 DIV64_U64_ROUND_UP(scan * fraction[file],
3035 /* Scan one type exclusively */
3036 if ((scan_balance == SCAN_FILE) != file)
3040 /* Look ma, no brain */
3049 * Anonymous LRU management is a waste if there is
3050 * ultimately no way to reclaim the memory.
3052 static bool can_age_anon_pages(struct pglist_data *pgdat,
3053 struct scan_control *sc)
3055 /* Aging the anon LRU is valuable if swap is present: */
3056 if (total_swap_pages > 0)
3059 /* Also valuable if anon pages can be demoted: */
3060 return can_demote(pgdat->node_id, sc);
3063 #ifdef CONFIG_LRU_GEN
3065 /******************************************************************************
3067 ******************************************************************************/
3069 #define LRU_REFS_FLAGS (BIT(PG_referenced) | BIT(PG_workingset))
3071 #define DEFINE_MAX_SEQ(lruvec) \
3072 unsigned long max_seq = READ_ONCE((lruvec)->lrugen.max_seq)
3074 #define DEFINE_MIN_SEQ(lruvec) \
3075 unsigned long min_seq[ANON_AND_FILE] = { \
3076 READ_ONCE((lruvec)->lrugen.min_seq[LRU_GEN_ANON]), \
3077 READ_ONCE((lruvec)->lrugen.min_seq[LRU_GEN_FILE]), \
3080 #define for_each_gen_type_zone(gen, type, zone) \
3081 for ((gen) = 0; (gen) < MAX_NR_GENS; (gen)++) \
3082 for ((type) = 0; (type) < ANON_AND_FILE; (type)++) \
3083 for ((zone) = 0; (zone) < MAX_NR_ZONES; (zone)++)
3085 static struct lruvec __maybe_unused *get_lruvec(struct mem_cgroup *memcg, int nid)
3087 struct pglist_data *pgdat = NODE_DATA(nid);
3091 struct lruvec *lruvec = &memcg->nodeinfo[nid]->lruvec;
3093 /* for hotadd_new_pgdat() */
3095 lruvec->pgdat = pgdat;
3100 VM_WARN_ON_ONCE(!mem_cgroup_disabled());
3102 return pgdat ? &pgdat->__lruvec : NULL;
3105 static int get_swappiness(struct lruvec *lruvec, struct scan_control *sc)
3107 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
3108 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
3110 if (!can_demote(pgdat->node_id, sc) &&
3111 mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH)
3114 return mem_cgroup_swappiness(memcg);
3117 static int get_nr_gens(struct lruvec *lruvec, int type)
3119 return lruvec->lrugen.max_seq - lruvec->lrugen.min_seq[type] + 1;
3122 static bool __maybe_unused seq_is_valid(struct lruvec *lruvec)
3124 /* see the comment on lru_gen_struct */
3125 return get_nr_gens(lruvec, LRU_GEN_FILE) >= MIN_NR_GENS &&
3126 get_nr_gens(lruvec, LRU_GEN_FILE) <= get_nr_gens(lruvec, LRU_GEN_ANON) &&
3127 get_nr_gens(lruvec, LRU_GEN_ANON) <= MAX_NR_GENS;
3130 /******************************************************************************
3131 * refault feedback loop
3132 ******************************************************************************/
3135 * A feedback loop based on Proportional-Integral-Derivative (PID) controller.
3137 * The P term is refaulted/(evicted+protected) from a tier in the generation
3138 * currently being evicted; the I term is the exponential moving average of the
3139 * P term over the generations previously evicted, using the smoothing factor
3140 * 1/2; the D term isn't supported.
3142 * The setpoint (SP) is always the first tier of one type; the process variable
3143 * (PV) is either any tier of the other type or any other tier of the same
3146 * The error is the difference between the SP and the PV; the correction is to
3147 * turn off protection when SP>PV or turn on protection when SP<PV.
3149 * For future optimizations:
3150 * 1. The D term may discount the other two terms over time so that long-lived
3151 * generations can resist stale information.
3154 unsigned long refaulted;
3155 unsigned long total;
3159 static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain,
3160 struct ctrl_pos *pos)
3162 struct lru_gen_struct *lrugen = &lruvec->lrugen;
3163 int hist = lru_hist_from_seq(lrugen->min_seq[type]);
3165 pos->refaulted = lrugen->avg_refaulted[type][tier] +
3166 atomic_long_read(&lrugen->refaulted[hist][type][tier]);
3167 pos->total = lrugen->avg_total[type][tier] +
3168 atomic_long_read(&lrugen->evicted[hist][type][tier]);
3170 pos->total += lrugen->protected[hist][type][tier - 1];
3174 static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover)
3177 struct lru_gen_struct *lrugen = &lruvec->lrugen;
3178 bool clear = carryover ? NR_HIST_GENS == 1 : NR_HIST_GENS > 1;
3179 unsigned long seq = carryover ? lrugen->min_seq[type] : lrugen->max_seq + 1;
3181 lockdep_assert_held(&lruvec->lru_lock);
3183 if (!carryover && !clear)
3186 hist = lru_hist_from_seq(seq);
3188 for (tier = 0; tier < MAX_NR_TIERS; tier++) {
3192 sum = lrugen->avg_refaulted[type][tier] +
3193 atomic_long_read(&lrugen->refaulted[hist][type][tier]);
3194 WRITE_ONCE(lrugen->avg_refaulted[type][tier], sum / 2);
3196 sum = lrugen->avg_total[type][tier] +
3197 atomic_long_read(&lrugen->evicted[hist][type][tier]);
3199 sum += lrugen->protected[hist][type][tier - 1];
3200 WRITE_ONCE(lrugen->avg_total[type][tier], sum / 2);
3204 atomic_long_set(&lrugen->refaulted[hist][type][tier], 0);
3205 atomic_long_set(&lrugen->evicted[hist][type][tier], 0);
3207 WRITE_ONCE(lrugen->protected[hist][type][tier - 1], 0);
3212 static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv)
3215 * Return true if the PV has a limited number of refaults or a lower
3216 * refaulted/total than the SP.
3218 return pv->refaulted < MIN_LRU_BATCH ||
3219 pv->refaulted * (sp->total + MIN_LRU_BATCH) * sp->gain <=
3220 (sp->refaulted + 1) * pv->total * pv->gain;
3223 /******************************************************************************
3225 ******************************************************************************/
3227 /* promote pages accessed through page tables */
3228 static int folio_update_gen(struct folio *folio, int gen)
3230 unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
3232 VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
3233 VM_WARN_ON_ONCE(!rcu_read_lock_held());
3236 /* lru_gen_del_folio() has isolated this page? */
3237 if (!(old_flags & LRU_GEN_MASK)) {
3238 /* for shrink_page_list() */
3239 new_flags = old_flags | BIT(PG_referenced);
3243 new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS);
3244 new_flags |= (gen + 1UL) << LRU_GEN_PGOFF;
3245 } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags));
3247 return ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
3250 /* protect pages accessed multiple times through file descriptors */
3251 static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio, bool reclaiming)
3253 int type = folio_is_file_lru(folio);
3254 struct lru_gen_struct *lrugen = &lruvec->lrugen;
3255 int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]);
3256 unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
3258 VM_WARN_ON_ONCE_FOLIO(!(old_flags & LRU_GEN_MASK), folio);
3261 new_gen = ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
3262 /* folio_update_gen() has promoted this page? */
3263 if (new_gen >= 0 && new_gen != old_gen)
3266 new_gen = (old_gen + 1) % MAX_NR_GENS;
3268 new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS);
3269 new_flags |= (new_gen + 1UL) << LRU_GEN_PGOFF;
3270 /* for folio_end_writeback() */
3272 new_flags |= BIT(PG_reclaim);
3273 } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags));
3275 lru_gen_update_size(lruvec, folio, old_gen, new_gen);
3280 static unsigned long get_pte_pfn(pte_t pte, struct vm_area_struct *vma, unsigned long addr)
3282 unsigned long pfn = pte_pfn(pte);
3284 VM_WARN_ON_ONCE(addr < vma->vm_start || addr >= vma->vm_end);
3286 if (!pte_present(pte) || is_zero_pfn(pfn))
3289 if (WARN_ON_ONCE(pte_devmap(pte) || pte_special(pte)))
3292 if (WARN_ON_ONCE(!pfn_valid(pfn)))
3298 static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *memcg,
3299 struct pglist_data *pgdat)
3301 struct folio *folio;
3303 /* try to avoid unnecessary memory loads */
3304 if (pfn < pgdat->node_start_pfn || pfn >= pgdat_end_pfn(pgdat))
3307 folio = pfn_folio(pfn);
3308 if (folio_nid(folio) != pgdat->node_id)
3311 if (folio_memcg_rcu(folio) != memcg)
3317 static void inc_min_seq(struct lruvec *lruvec, int type)
3319 struct lru_gen_struct *lrugen = &lruvec->lrugen;
3321 reset_ctrl_pos(lruvec, type, true);
3322 WRITE_ONCE(lrugen->min_seq[type], lrugen->min_seq[type] + 1);
3325 static bool try_to_inc_min_seq(struct lruvec *lruvec, bool can_swap)
3327 int gen, type, zone;
3328 bool success = false;
3329 struct lru_gen_struct *lrugen = &lruvec->lrugen;
3330 DEFINE_MIN_SEQ(lruvec);
3332 VM_WARN_ON_ONCE(!seq_is_valid(lruvec));
3334 /* find the oldest populated generation */
3335 for (type = !can_swap; type < ANON_AND_FILE; type++) {
3336 while (min_seq[type] + MIN_NR_GENS <= lrugen->max_seq) {
3337 gen = lru_gen_from_seq(min_seq[type]);
3339 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3340 if (!list_empty(&lrugen->lists[gen][type][zone]))
3350 /* see the comment on lru_gen_struct */
3352 min_seq[LRU_GEN_ANON] = min(min_seq[LRU_GEN_ANON], min_seq[LRU_GEN_FILE]);
3353 min_seq[LRU_GEN_FILE] = max(min_seq[LRU_GEN_ANON], lrugen->min_seq[LRU_GEN_FILE]);
3356 for (type = !can_swap; type < ANON_AND_FILE; type++) {
3357 if (min_seq[type] == lrugen->min_seq[type])
3360 reset_ctrl_pos(lruvec, type, true);
3361 WRITE_ONCE(lrugen->min_seq[type], min_seq[type]);
3368 static void inc_max_seq(struct lruvec *lruvec, unsigned long max_seq, bool can_swap)
3372 struct lru_gen_struct *lrugen = &lruvec->lrugen;
3374 spin_lock_irq(&lruvec->lru_lock);
3376 VM_WARN_ON_ONCE(!seq_is_valid(lruvec));
3378 if (max_seq != lrugen->max_seq)
3381 for (type = ANON_AND_FILE - 1; type >= 0; type--) {
3382 if (get_nr_gens(lruvec, type) != MAX_NR_GENS)
3385 VM_WARN_ON_ONCE(type == LRU_GEN_FILE || can_swap);
3387 inc_min_seq(lruvec, type);
3391 * Update the active/inactive LRU sizes for compatibility. Both sides of
3392 * the current max_seq need to be covered, since max_seq+1 can overlap
3393 * with min_seq[LRU_GEN_ANON] if swapping is constrained. And if they do
3394 * overlap, cold/hot inversion happens.
3396 prev = lru_gen_from_seq(lrugen->max_seq - 1);
3397 next = lru_gen_from_seq(lrugen->max_seq + 1);
3399 for (type = 0; type < ANON_AND_FILE; type++) {
3400 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3401 enum lru_list lru = type * LRU_INACTIVE_FILE;
3402 long delta = lrugen->nr_pages[prev][type][zone] -
3403 lrugen->nr_pages[next][type][zone];
3408 __update_lru_size(lruvec, lru, zone, delta);
3409 __update_lru_size(lruvec, lru + LRU_ACTIVE, zone, -delta);
3413 for (type = 0; type < ANON_AND_FILE; type++)
3414 reset_ctrl_pos(lruvec, type, false);
3416 /* make sure preceding modifications appear */
3417 smp_store_release(&lrugen->max_seq, lrugen->max_seq + 1);
3419 spin_unlock_irq(&lruvec->lru_lock);
3422 static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, unsigned long *min_seq,
3423 struct scan_control *sc, bool can_swap, unsigned long *nr_to_scan)
3425 int gen, type, zone;
3426 unsigned long old = 0;
3427 unsigned long young = 0;
3428 unsigned long total = 0;
3429 struct lru_gen_struct *lrugen = &lruvec->lrugen;
3430 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
3432 for (type = !can_swap; type < ANON_AND_FILE; type++) {
3435 for (seq = min_seq[type]; seq <= max_seq; seq++) {
3436 unsigned long size = 0;
3438 gen = lru_gen_from_seq(seq);
3440 for (zone = 0; zone < MAX_NR_ZONES; zone++)
3441 size += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L);
3446 else if (seq + MIN_NR_GENS == max_seq)
3451 /* try to scrape all its memory if this memcg was deleted */
3452 *nr_to_scan = mem_cgroup_online(memcg) ? (total >> sc->priority) : total;
3455 * The aging tries to be lazy to reduce the overhead, while the eviction
3456 * stalls when the number of generations reaches MIN_NR_GENS. Hence, the
3457 * ideal number of generations is MIN_NR_GENS+1.
3459 if (min_seq[!can_swap] + MIN_NR_GENS > max_seq)
3461 if (min_seq[!can_swap] + MIN_NR_GENS < max_seq)
3465 * It's also ideal to spread pages out evenly, i.e., 1/(MIN_NR_GENS+1)
3466 * of the total number of pages for each generation. A reasonable range
3467 * for this average portion is [1/MIN_NR_GENS, 1/(MIN_NR_GENS+2)]. The
3468 * aging cares about the upper bound of hot pages, while the eviction
3469 * cares about the lower bound of cold pages.
3471 if (young * MIN_NR_GENS > total)
3473 if (old * (MIN_NR_GENS + 2) < total)
3479 static void age_lruvec(struct lruvec *lruvec, struct scan_control *sc)
3482 unsigned long nr_to_scan;
3483 int swappiness = get_swappiness(lruvec, sc);
3484 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
3485 DEFINE_MAX_SEQ(lruvec);
3486 DEFINE_MIN_SEQ(lruvec);
3488 VM_WARN_ON_ONCE(sc->memcg_low_reclaim);
3490 mem_cgroup_calculate_protection(NULL, memcg);
3492 if (mem_cgroup_below_min(memcg))
3495 need_aging = should_run_aging(lruvec, max_seq, min_seq, sc, swappiness, &nr_to_scan);
3497 inc_max_seq(lruvec, max_seq, swappiness);
3500 static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc)
3502 struct mem_cgroup *memcg;
3504 VM_WARN_ON_ONCE(!current_is_kswapd());
3506 memcg = mem_cgroup_iter(NULL, NULL, NULL);
3508 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
3510 age_lruvec(lruvec, sc);
3513 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)));
3517 * This function exploits spatial locality when shrink_page_list() walks the
3518 * rmap. It scans the adjacent PTEs of a young PTE and promotes hot pages.
3520 void lru_gen_look_around(struct page_vma_mapped_walk *pvmw)
3524 unsigned long start;
3527 unsigned long bitmap[BITS_TO_LONGS(MIN_LRU_BATCH)] = {};
3528 struct folio *folio = pfn_folio(pvmw->pfn);
3529 struct mem_cgroup *memcg = folio_memcg(folio);
3530 struct pglist_data *pgdat = folio_pgdat(folio);
3531 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
3532 DEFINE_MAX_SEQ(lruvec);
3533 int old_gen, new_gen = lru_gen_from_seq(max_seq);
3535 lockdep_assert_held(pvmw->ptl);
3536 VM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio);
3538 if (spin_is_contended(pvmw->ptl))
3541 start = max(pvmw->address & PMD_MASK, pvmw->vma->vm_start);
3542 end = min(pvmw->address | ~PMD_MASK, pvmw->vma->vm_end - 1) + 1;
3544 if (end - start > MIN_LRU_BATCH * PAGE_SIZE) {
3545 if (pvmw->address - start < MIN_LRU_BATCH * PAGE_SIZE / 2)
3546 end = start + MIN_LRU_BATCH * PAGE_SIZE;
3547 else if (end - pvmw->address < MIN_LRU_BATCH * PAGE_SIZE / 2)
3548 start = end - MIN_LRU_BATCH * PAGE_SIZE;
3550 start = pvmw->address - MIN_LRU_BATCH * PAGE_SIZE / 2;
3551 end = pvmw->address + MIN_LRU_BATCH * PAGE_SIZE / 2;
3555 pte = pvmw->pte - (pvmw->address - start) / PAGE_SIZE;
3558 arch_enter_lazy_mmu_mode();
3560 for (i = 0, addr = start; addr != end; i++, addr += PAGE_SIZE) {
3563 pfn = get_pte_pfn(pte[i], pvmw->vma, addr);
3567 if (!pte_young(pte[i]))
3570 folio = get_pfn_folio(pfn, memcg, pgdat);
3574 if (!ptep_test_and_clear_young(pvmw->vma, addr, pte + i))
3575 VM_WARN_ON_ONCE(true);
3577 if (pte_dirty(pte[i]) && !folio_test_dirty(folio) &&
3578 !(folio_test_anon(folio) && folio_test_swapbacked(folio) &&
3579 !folio_test_swapcache(folio)))
3580 folio_mark_dirty(folio);
3582 old_gen = folio_lru_gen(folio);
3584 folio_set_referenced(folio);
3585 else if (old_gen != new_gen)
3586 __set_bit(i, bitmap);
3589 arch_leave_lazy_mmu_mode();
3592 if (bitmap_weight(bitmap, MIN_LRU_BATCH) < PAGEVEC_SIZE) {
3593 for_each_set_bit(i, bitmap, MIN_LRU_BATCH) {
3594 folio = pfn_folio(pte_pfn(pte[i]));
3595 folio_activate(folio);
3600 /* folio_update_gen() requires stable folio_memcg() */
3601 if (!mem_cgroup_trylock_pages(memcg))
3604 spin_lock_irq(&lruvec->lru_lock);
3605 new_gen = lru_gen_from_seq(lruvec->lrugen.max_seq);
3607 for_each_set_bit(i, bitmap, MIN_LRU_BATCH) {
3608 folio = pfn_folio(pte_pfn(pte[i]));
3609 if (folio_memcg_rcu(folio) != memcg)
3612 old_gen = folio_update_gen(folio, new_gen);
3613 if (old_gen < 0 || old_gen == new_gen)
3616 lru_gen_update_size(lruvec, folio, old_gen, new_gen);
3619 spin_unlock_irq(&lruvec->lru_lock);
3621 mem_cgroup_unlock_pages();
3624 /******************************************************************************
3626 ******************************************************************************/
3628 static bool sort_folio(struct lruvec *lruvec, struct folio *folio, int tier_idx)
3631 int gen = folio_lru_gen(folio);
3632 int type = folio_is_file_lru(folio);
3633 int zone = folio_zonenum(folio);
3634 int delta = folio_nr_pages(folio);
3635 int refs = folio_lru_refs(folio);
3636 int tier = lru_tier_from_refs(refs);
3637 struct lru_gen_struct *lrugen = &lruvec->lrugen;
3639 VM_WARN_ON_ONCE_FOLIO(gen >= MAX_NR_GENS, folio);
3642 if (!folio_evictable(folio)) {
3643 success = lru_gen_del_folio(lruvec, folio, true);
3644 VM_WARN_ON_ONCE_FOLIO(!success, folio);
3645 folio_set_unevictable(folio);
3646 lruvec_add_folio(lruvec, folio);
3647 __count_vm_events(UNEVICTABLE_PGCULLED, delta);
3651 /* dirty lazyfree */
3652 if (type == LRU_GEN_FILE && folio_test_anon(folio) && folio_test_dirty(folio)) {
3653 success = lru_gen_del_folio(lruvec, folio, true);
3654 VM_WARN_ON_ONCE_FOLIO(!success, folio);
3655 folio_set_swapbacked(folio);
3656 lruvec_add_folio_tail(lruvec, folio);
3661 if (gen != lru_gen_from_seq(lrugen->min_seq[type])) {
3662 list_move(&folio->lru, &lrugen->lists[gen][type][zone]);
3667 if (tier > tier_idx) {
3668 int hist = lru_hist_from_seq(lrugen->min_seq[type]);
3670 gen = folio_inc_gen(lruvec, folio, false);
3671 list_move_tail(&folio->lru, &lrugen->lists[gen][type][zone]);
3673 WRITE_ONCE(lrugen->protected[hist][type][tier - 1],
3674 lrugen->protected[hist][type][tier - 1] + delta);
3675 __mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta);
3679 /* waiting for writeback */
3680 if (folio_test_locked(folio) || folio_test_writeback(folio) ||
3681 (type == LRU_GEN_FILE && folio_test_dirty(folio))) {
3682 gen = folio_inc_gen(lruvec, folio, true);
3683 list_move(&folio->lru, &lrugen->lists[gen][type][zone]);
3690 static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct scan_control *sc)
3694 /* unmapping inhibited */
3695 if (!sc->may_unmap && folio_mapped(folio))
3698 /* swapping inhibited */
3699 if (!(sc->may_writepage && (sc->gfp_mask & __GFP_IO)) &&
3700 (folio_test_dirty(folio) ||
3701 (folio_test_anon(folio) && !folio_test_swapcache(folio))))
3704 /* raced with release_pages() */
3705 if (!folio_try_get(folio))
3708 /* raced with another isolation */
3709 if (!folio_test_clear_lru(folio)) {
3714 /* see the comment on MAX_NR_TIERS */
3715 if (!folio_test_referenced(folio))
3716 set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, 0);
3718 /* for shrink_page_list() */
3719 folio_clear_reclaim(folio);
3720 folio_clear_referenced(folio);
3722 success = lru_gen_del_folio(lruvec, folio, true);
3723 VM_WARN_ON_ONCE_FOLIO(!success, folio);
3728 static int scan_folios(struct lruvec *lruvec, struct scan_control *sc,
3729 int type, int tier, struct list_head *list)
3732 enum vm_event_item item;
3736 int remaining = MAX_LRU_BATCH;
3737 struct lru_gen_struct *lrugen = &lruvec->lrugen;
3738 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
3740 VM_WARN_ON_ONCE(!list_empty(list));
3742 if (get_nr_gens(lruvec, type) == MIN_NR_GENS)
3745 gen = lru_gen_from_seq(lrugen->min_seq[type]);
3747 for (zone = sc->reclaim_idx; zone >= 0; zone--) {
3750 struct list_head *head = &lrugen->lists[gen][type][zone];
3752 while (!list_empty(head)) {
3753 struct folio *folio = lru_to_folio(head);
3754 int delta = folio_nr_pages(folio);
3756 VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
3757 VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio);
3758 VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio);
3759 VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio);
3763 if (sort_folio(lruvec, folio, tier))
3765 else if (isolate_folio(lruvec, folio, sc)) {
3766 list_add(&folio->lru, list);
3769 list_move(&folio->lru, &moved);
3773 if (!--remaining || max(isolated, skipped) >= MIN_LRU_BATCH)
3778 list_splice(&moved, head);
3779 __count_zid_vm_events(PGSCAN_SKIP, zone, skipped);
3782 if (!remaining || isolated >= MIN_LRU_BATCH)
3786 item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT;
3787 if (!cgroup_reclaim(sc)) {
3788 __count_vm_events(item, isolated);
3789 __count_vm_events(PGREFILL, sorted);
3791 __count_memcg_events(memcg, item, isolated);
3792 __count_memcg_events(memcg, PGREFILL, sorted);
3793 __count_vm_events(PGSCAN_ANON + type, isolated);
3796 * There might not be eligible pages due to reclaim_idx, may_unmap and
3797 * may_writepage. Check the remaining to prevent livelock if it's not
3800 return isolated || !remaining ? scanned : 0;
3803 static int get_tier_idx(struct lruvec *lruvec, int type)
3806 struct ctrl_pos sp, pv;
3809 * To leave a margin for fluctuations, use a larger gain factor (1:2).
3810 * This value is chosen because any other tier would have at least twice
3811 * as many refaults as the first tier.
3813 read_ctrl_pos(lruvec, type, 0, 1, &sp);
3814 for (tier = 1; tier < MAX_NR_TIERS; tier++) {
3815 read_ctrl_pos(lruvec, type, tier, 2, &pv);
3816 if (!positive_ctrl_err(&sp, &pv))
3823 static int get_type_to_scan(struct lruvec *lruvec, int swappiness, int *tier_idx)
3826 struct ctrl_pos sp, pv;
3827 int gain[ANON_AND_FILE] = { swappiness, 200 - swappiness };
3830 * Compare the first tier of anon with that of file to determine which
3831 * type to scan. Also need to compare other tiers of the selected type
3832 * with the first tier of the other type to determine the last tier (of
3833 * the selected type) to evict.
3835 read_ctrl_pos(lruvec, LRU_GEN_ANON, 0, gain[LRU_GEN_ANON], &sp);
3836 read_ctrl_pos(lruvec, LRU_GEN_FILE, 0, gain[LRU_GEN_FILE], &pv);
3837 type = positive_ctrl_err(&sp, &pv);
3839 read_ctrl_pos(lruvec, !type, 0, gain[!type], &sp);
3840 for (tier = 1; tier < MAX_NR_TIERS; tier++) {
3841 read_ctrl_pos(lruvec, type, tier, gain[type], &pv);
3842 if (!positive_ctrl_err(&sp, &pv))
3846 *tier_idx = tier - 1;
3851 static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness,
3852 int *type_scanned, struct list_head *list)
3858 DEFINE_MIN_SEQ(lruvec);
3861 * Try to make the obvious choice first. When anon and file are both
3862 * available from the same generation, interpret swappiness 1 as file
3863 * first and 200 as anon first.
3866 type = LRU_GEN_FILE;
3867 else if (min_seq[LRU_GEN_ANON] < min_seq[LRU_GEN_FILE])
3868 type = LRU_GEN_ANON;
3869 else if (swappiness == 1)
3870 type = LRU_GEN_FILE;
3871 else if (swappiness == 200)
3872 type = LRU_GEN_ANON;
3874 type = get_type_to_scan(lruvec, swappiness, &tier);
3876 for (i = !swappiness; i < ANON_AND_FILE; i++) {
3878 tier = get_tier_idx(lruvec, type);
3880 scanned = scan_folios(lruvec, sc, type, tier, list);
3888 *type_scanned = type;
3893 static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness)
3899 struct folio *folio;
3900 enum vm_event_item item;
3901 struct reclaim_stat stat;
3902 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
3903 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
3905 spin_lock_irq(&lruvec->lru_lock);
3907 scanned = isolate_folios(lruvec, sc, swappiness, &type, &list);
3909 scanned += try_to_inc_min_seq(lruvec, swappiness);
3911 if (get_nr_gens(lruvec, !swappiness) == MIN_NR_GENS)
3914 spin_unlock_irq(&lruvec->lru_lock);
3916 if (list_empty(&list))
3919 reclaimed = shrink_page_list(&list, pgdat, sc, &stat, false);
3921 list_for_each_entry(folio, &list, lru) {
3922 /* restore LRU_REFS_FLAGS cleared by isolate_folio() */
3923 if (folio_test_workingset(folio))
3924 folio_set_referenced(folio);
3926 /* don't add rejected pages to the oldest generation */
3927 if (folio_test_reclaim(folio) &&
3928 (folio_test_dirty(folio) || folio_test_writeback(folio)))
3929 folio_clear_active(folio);
3931 folio_set_active(folio);
3934 spin_lock_irq(&lruvec->lru_lock);
3936 move_pages_to_lru(lruvec, &list);
3938 item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT;
3939 if (!cgroup_reclaim(sc))
3940 __count_vm_events(item, reclaimed);
3941 __count_memcg_events(memcg, item, reclaimed);
3942 __count_vm_events(PGSTEAL_ANON + type, reclaimed);
3944 spin_unlock_irq(&lruvec->lru_lock);
3946 mem_cgroup_uncharge_list(&list);
3947 free_unref_page_list(&list);
3949 sc->nr_reclaimed += reclaimed;
3954 static unsigned long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc,
3958 unsigned long nr_to_scan;
3959 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
3960 DEFINE_MAX_SEQ(lruvec);
3961 DEFINE_MIN_SEQ(lruvec);
3963 if (mem_cgroup_below_min(memcg) ||
3964 (mem_cgroup_below_low(memcg) && !sc->memcg_low_reclaim))
3967 need_aging = should_run_aging(lruvec, max_seq, min_seq, sc, can_swap, &nr_to_scan);
3971 /* skip the aging path at the default priority */
3972 if (sc->priority == DEF_PRIORITY)
3975 /* leave the work to lru_gen_age_node() */
3976 if (current_is_kswapd())
3979 inc_max_seq(lruvec, max_seq, can_swap);
3981 return min_seq[!can_swap] + MIN_NR_GENS <= max_seq ? nr_to_scan : 0;
3984 static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
3986 struct blk_plug plug;
3987 unsigned long scanned = 0;
3991 blk_start_plug(&plug);
3996 unsigned long nr_to_scan;
3999 swappiness = get_swappiness(lruvec, sc);
4000 else if (!cgroup_reclaim(sc) && get_swappiness(lruvec, sc))
4005 nr_to_scan = get_nr_to_scan(lruvec, sc, swappiness);
4009 delta = evict_folios(lruvec, sc, swappiness);
4014 if (scanned >= nr_to_scan)
4020 blk_finish_plug(&plug);
4023 /******************************************************************************
4025 ******************************************************************************/
4027 void lru_gen_init_lruvec(struct lruvec *lruvec)
4029 int gen, type, zone;
4030 struct lru_gen_struct *lrugen = &lruvec->lrugen;
4032 lrugen->max_seq = MIN_NR_GENS + 1;
4034 for_each_gen_type_zone(gen, type, zone)
4035 INIT_LIST_HEAD(&lrugen->lists[gen][type][zone]);
4039 void lru_gen_init_memcg(struct mem_cgroup *memcg)
4043 void lru_gen_exit_memcg(struct mem_cgroup *memcg)
4047 for_each_node(nid) {
4048 struct lruvec *lruvec = get_lruvec(memcg, nid);
4050 VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0,
4051 sizeof(lruvec->lrugen.nr_pages)));
4056 static int __init init_lru_gen(void)
4058 BUILD_BUG_ON(MIN_NR_GENS + 1 >= MAX_NR_GENS);
4059 BUILD_BUG_ON(BIT(LRU_GEN_WIDTH) <= MAX_NR_GENS);
4063 late_initcall(init_lru_gen);
4065 #else /* !CONFIG_LRU_GEN */
4067 static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc)
4071 static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
4075 #endif /* CONFIG_LRU_GEN */
4077 static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
4079 unsigned long nr[NR_LRU_LISTS];
4080 unsigned long targets[NR_LRU_LISTS];
4081 unsigned long nr_to_scan;
4083 unsigned long nr_reclaimed = 0;
4084 unsigned long nr_to_reclaim = sc->nr_to_reclaim;
4085 struct blk_plug plug;
4088 if (lru_gen_enabled()) {
4089 lru_gen_shrink_lruvec(lruvec, sc);
4093 get_scan_count(lruvec, sc, nr);
4095 /* Record the original scan target for proportional adjustments later */
4096 memcpy(targets, nr, sizeof(nr));
4099 * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal
4100 * event that can occur when there is little memory pressure e.g.
4101 * multiple streaming readers/writers. Hence, we do not abort scanning
4102 * when the requested number of pages are reclaimed when scanning at
4103 * DEF_PRIORITY on the assumption that the fact we are direct
4104 * reclaiming implies that kswapd is not keeping up and it is best to
4105 * do a batch of work at once. For memcg reclaim one check is made to
4106 * abort proportional reclaim if either the file or anon lru has already
4107 * dropped to zero at the first pass.
4109 scan_adjusted = (!cgroup_reclaim(sc) && !current_is_kswapd() &&
4110 sc->priority == DEF_PRIORITY);
4112 blk_start_plug(&plug);
4113 while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
4114 nr[LRU_INACTIVE_FILE]) {
4115 unsigned long nr_anon, nr_file, percentage;
4116 unsigned long nr_scanned;
4118 for_each_evictable_lru(lru) {
4120 nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX);
4121 nr[lru] -= nr_to_scan;
4123 nr_reclaimed += shrink_list(lru, nr_to_scan,
4130 if (nr_reclaimed < nr_to_reclaim || scan_adjusted)
4134 * For kswapd and memcg, reclaim at least the number of pages
4135 * requested. Ensure that the anon and file LRUs are scanned
4136 * proportionally what was requested by get_scan_count(). We
4137 * stop reclaiming one LRU and reduce the amount scanning
4138 * proportional to the original scan target.
4140 nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE];
4141 nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON];
4144 * It's just vindictive to attack the larger once the smaller
4145 * has gone to zero. And given the way we stop scanning the
4146 * smaller below, this makes sure that we only make one nudge
4147 * towards proportionality once we've got nr_to_reclaim.
4149 if (!nr_file || !nr_anon)
4152 if (nr_file > nr_anon) {
4153 unsigned long scan_target = targets[LRU_INACTIVE_ANON] +
4154 targets[LRU_ACTIVE_ANON] + 1;
4156 percentage = nr_anon * 100 / scan_target;
4158 unsigned long scan_target = targets[LRU_INACTIVE_FILE] +
4159 targets[LRU_ACTIVE_FILE] + 1;
4161 percentage = nr_file * 100 / scan_target;
4164 /* Stop scanning the smaller of the LRU */
4166 nr[lru + LRU_ACTIVE] = 0;
4169 * Recalculate the other LRU scan count based on its original
4170 * scan target and the percentage scanning already complete
4172 lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE;
4173 nr_scanned = targets[lru] - nr[lru];
4174 nr[lru] = targets[lru] * (100 - percentage) / 100;
4175 nr[lru] -= min(nr[lru], nr_scanned);
4178 nr_scanned = targets[lru] - nr[lru];
4179 nr[lru] = targets[lru] * (100 - percentage) / 100;
4180 nr[lru] -= min(nr[lru], nr_scanned);
4182 scan_adjusted = true;
4184 blk_finish_plug(&plug);
4185 sc->nr_reclaimed += nr_reclaimed;
4188 * Even if we did not try to evict anon pages at all, we want to
4189 * rebalance the anon lru active/inactive ratio.
4191 if (can_age_anon_pages(lruvec_pgdat(lruvec), sc) &&
4192 inactive_is_low(lruvec, LRU_INACTIVE_ANON))
4193 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
4194 sc, LRU_ACTIVE_ANON);
4197 /* Use reclaim/compaction for costly allocs or under memory pressure */
4198 static bool in_reclaim_compaction(struct scan_control *sc)
4200 if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
4201 (sc->order > PAGE_ALLOC_COSTLY_ORDER ||
4202 sc->priority < DEF_PRIORITY - 2))
4209 * Reclaim/compaction is used for high-order allocation requests. It reclaims
4210 * order-0 pages before compacting the zone. should_continue_reclaim() returns
4211 * true if more pages should be reclaimed such that when the page allocator
4212 * calls try_to_compact_pages() that it will have enough free pages to succeed.
4213 * It will give up earlier than that if there is difficulty reclaiming pages.
4215 static inline bool should_continue_reclaim(struct pglist_data *pgdat,
4216 unsigned long nr_reclaimed,
4217 struct scan_control *sc)
4219 unsigned long pages_for_compaction;
4220 unsigned long inactive_lru_pages;
4223 /* If not in reclaim/compaction mode, stop */
4224 if (!in_reclaim_compaction(sc))
4228 * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX
4229 * number of pages that were scanned. This will return to the caller
4230 * with the risk reclaim/compaction and the resulting allocation attempt
4231 * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL
4232 * allocations through requiring that the full LRU list has been scanned
4233 * first, by assuming that zero delta of sc->nr_scanned means full LRU
4234 * scan, but that approximation was wrong, and there were corner cases
4235 * where always a non-zero amount of pages were scanned.
4240 /* If compaction would go ahead or the allocation would succeed, stop */
4241 for (z = 0; z <= sc->reclaim_idx; z++) {
4242 struct zone *zone = &pgdat->node_zones[z];
4243 if (!managed_zone(zone))
4246 switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) {
4247 case COMPACT_SUCCESS:
4248 case COMPACT_CONTINUE:
4251 /* check next zone */
4257 * If we have not reclaimed enough pages for compaction and the
4258 * inactive lists are large enough, continue reclaiming
4260 pages_for_compaction = compact_gap(sc->order);
4261 inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE);
4262 if (can_reclaim_anon_pages(NULL, pgdat->node_id, sc))
4263 inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON);
4265 return inactive_lru_pages > pages_for_compaction;
4268 static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc)
4270 struct mem_cgroup *target_memcg = sc->target_mem_cgroup;
4271 struct mem_cgroup *memcg;
4273 memcg = mem_cgroup_iter(target_memcg, NULL, NULL);
4275 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
4276 unsigned long reclaimed;
4277 unsigned long scanned;
4280 * This loop can become CPU-bound when target memcgs
4281 * aren't eligible for reclaim - either because they
4282 * don't have any reclaimable pages, or because their
4283 * memory is explicitly protected. Avoid soft lockups.
4287 mem_cgroup_calculate_protection(target_memcg, memcg);
4289 if (mem_cgroup_below_min(memcg)) {
4292 * If there is no reclaimable memory, OOM.
4295 } else if (mem_cgroup_below_low(memcg)) {
4298 * Respect the protection only as long as
4299 * there is an unprotected supply
4300 * of reclaimable memory from other cgroups.
4302 if (!sc->memcg_low_reclaim) {
4303 sc->memcg_low_skipped = 1;
4306 memcg_memory_event(memcg, MEMCG_LOW);
4309 reclaimed = sc->nr_reclaimed;
4310 scanned = sc->nr_scanned;
4312 shrink_lruvec(lruvec, sc);
4314 shrink_slab(sc->gfp_mask, pgdat->node_id, memcg,
4317 /* Record the group's reclaim efficiency */
4319 vmpressure(sc->gfp_mask, memcg, false,
4320 sc->nr_scanned - scanned,
4321 sc->nr_reclaimed - reclaimed);
4323 } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL)));
4326 static void shrink_node(pg_data_t *pgdat, struct scan_control *sc)
4328 struct reclaim_state *reclaim_state = current->reclaim_state;
4329 unsigned long nr_reclaimed, nr_scanned;
4330 struct lruvec *target_lruvec;
4331 bool reclaimable = false;
4333 target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat);
4336 memset(&sc->nr, 0, sizeof(sc->nr));
4338 nr_reclaimed = sc->nr_reclaimed;
4339 nr_scanned = sc->nr_scanned;
4341 prepare_scan_count(pgdat, sc);
4343 shrink_node_memcgs(pgdat, sc);
4345 if (reclaim_state) {
4346 sc->nr_reclaimed += reclaim_state->reclaimed_slab;
4347 reclaim_state->reclaimed_slab = 0;
4350 /* Record the subtree's reclaim efficiency */
4352 vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true,
4353 sc->nr_scanned - nr_scanned,
4354 sc->nr_reclaimed - nr_reclaimed);
4356 if (sc->nr_reclaimed - nr_reclaimed)
4359 if (current_is_kswapd()) {
4361 * If reclaim is isolating dirty pages under writeback,
4362 * it implies that the long-lived page allocation rate
4363 * is exceeding the page laundering rate. Either the
4364 * global limits are not being effective at throttling
4365 * processes due to the page distribution throughout
4366 * zones or there is heavy usage of a slow backing
4367 * device. The only option is to throttle from reclaim
4368 * context which is not ideal as there is no guarantee
4369 * the dirtying process is throttled in the same way
4370 * balance_dirty_pages() manages.
4372 * Once a node is flagged PGDAT_WRITEBACK, kswapd will
4373 * count the number of pages under pages flagged for
4374 * immediate reclaim and stall if any are encountered
4375 * in the nr_immediate check below.
4377 if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken)
4378 set_bit(PGDAT_WRITEBACK, &pgdat->flags);
4380 /* Allow kswapd to start writing pages during reclaim.*/
4381 if (sc->nr.unqueued_dirty == sc->nr.file_taken)
4382 set_bit(PGDAT_DIRTY, &pgdat->flags);
4385 * If kswapd scans pages marked for immediate
4386 * reclaim and under writeback (nr_immediate), it
4387 * implies that pages are cycling through the LRU
4388 * faster than they are written so forcibly stall
4389 * until some pages complete writeback.
4391 if (sc->nr.immediate)
4392 reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK);
4396 * Tag a node/memcg as congested if all the dirty pages were marked
4397 * for writeback and immediate reclaim (counted in nr.congested).
4399 * Legacy memcg will stall in page writeback so avoid forcibly
4400 * stalling in reclaim_throttle().
4402 if ((current_is_kswapd() ||
4403 (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) &&
4404 sc->nr.dirty && sc->nr.dirty == sc->nr.congested)
4405 set_bit(LRUVEC_CONGESTED, &target_lruvec->flags);
4408 * Stall direct reclaim for IO completions if the lruvec is
4409 * node is congested. Allow kswapd to continue until it
4410 * starts encountering unqueued dirty pages or cycling through
4411 * the LRU too quickly.
4413 if (!current_is_kswapd() && current_may_throttle() &&
4414 !sc->hibernation_mode &&
4415 test_bit(LRUVEC_CONGESTED, &target_lruvec->flags))
4416 reclaim_throttle(pgdat, VMSCAN_THROTTLE_CONGESTED);
4418 if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed,
4423 * Kswapd gives up on balancing particular nodes after too
4424 * many failures to reclaim anything from them and goes to
4425 * sleep. On reclaim progress, reset the failure counter. A
4426 * successful direct reclaim run will revive a dormant kswapd.
4429 pgdat->kswapd_failures = 0;
4433 * Returns true if compaction should go ahead for a costly-order request, or
4434 * the allocation would already succeed without compaction. Return false if we
4435 * should reclaim first.
4437 static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
4439 unsigned long watermark;
4440 enum compact_result suitable;
4442 suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx);
4443 if (suitable == COMPACT_SUCCESS)
4444 /* Allocation should succeed already. Don't reclaim. */
4446 if (suitable == COMPACT_SKIPPED)
4447 /* Compaction cannot yet proceed. Do reclaim. */
4451 * Compaction is already possible, but it takes time to run and there
4452 * are potentially other callers using the pages just freed. So proceed
4453 * with reclaim to make a buffer of free pages available to give
4454 * compaction a reasonable chance of completing and allocating the page.
4455 * Note that we won't actually reclaim the whole buffer in one attempt
4456 * as the target watermark in should_continue_reclaim() is lower. But if
4457 * we are already above the high+gap watermark, don't reclaim at all.
4459 watermark = high_wmark_pages(zone) + compact_gap(sc->order);
4461 return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx);
4464 static void consider_reclaim_throttle(pg_data_t *pgdat, struct scan_control *sc)
4467 * If reclaim is making progress greater than 12% efficiency then
4468 * wake all the NOPROGRESS throttled tasks.
4470 if (sc->nr_reclaimed > (sc->nr_scanned >> 3)) {
4471 wait_queue_head_t *wqh;
4473 wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_NOPROGRESS];
4474 if (waitqueue_active(wqh))
4481 * Do not throttle kswapd or cgroup reclaim on NOPROGRESS as it will
4482 * throttle on VMSCAN_THROTTLE_WRITEBACK if there are too many pages
4483 * under writeback and marked for immediate reclaim at the tail of the
4486 if (current_is_kswapd() || cgroup_reclaim(sc))
4489 /* Throttle if making no progress at high prioities. */
4490 if (sc->priority == 1 && !sc->nr_reclaimed)
4491 reclaim_throttle(pgdat, VMSCAN_THROTTLE_NOPROGRESS);
4495 * This is the direct reclaim path, for page-allocating processes. We only
4496 * try to reclaim pages from zones which will satisfy the caller's allocation
4499 * If a zone is deemed to be full of pinned pages then just give it a light
4500 * scan then give up on it.
4502 static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
4506 unsigned long nr_soft_reclaimed;
4507 unsigned long nr_soft_scanned;
4509 pg_data_t *last_pgdat = NULL;
4510 pg_data_t *first_pgdat = NULL;
4513 * If the number of buffer_heads in the machine exceeds the maximum
4514 * allowed level, force direct reclaim to scan the highmem zone as
4515 * highmem pages could be pinning lowmem pages storing buffer_heads
4517 orig_mask = sc->gfp_mask;
4518 if (buffer_heads_over_limit) {
4519 sc->gfp_mask |= __GFP_HIGHMEM;
4520 sc->reclaim_idx = gfp_zone(sc->gfp_mask);
4523 for_each_zone_zonelist_nodemask(zone, z, zonelist,
4524 sc->reclaim_idx, sc->nodemask) {
4526 * Take care memory controller reclaiming has small influence
4529 if (!cgroup_reclaim(sc)) {
4530 if (!cpuset_zone_allowed(zone,
4531 GFP_KERNEL | __GFP_HARDWALL))
4535 * If we already have plenty of memory free for
4536 * compaction in this zone, don't free any more.
4537 * Even though compaction is invoked for any
4538 * non-zero order, only frequent costly order
4539 * reclamation is disruptive enough to become a
4540 * noticeable problem, like transparent huge
4543 if (IS_ENABLED(CONFIG_COMPACTION) &&
4544 sc->order > PAGE_ALLOC_COSTLY_ORDER &&
4545 compaction_ready(zone, sc)) {
4546 sc->compaction_ready = true;
4551 * Shrink each node in the zonelist once. If the
4552 * zonelist is ordered by zone (not the default) then a
4553 * node may be shrunk multiple times but in that case
4554 * the user prefers lower zones being preserved.
4556 if (zone->zone_pgdat == last_pgdat)
4560 * This steals pages from memory cgroups over softlimit
4561 * and returns the number of reclaimed pages and
4562 * scanned pages. This works for global memory pressure
4563 * and balancing, not for a memcg's limit.
4565 nr_soft_scanned = 0;
4566 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat,
4567 sc->order, sc->gfp_mask,
4569 sc->nr_reclaimed += nr_soft_reclaimed;
4570 sc->nr_scanned += nr_soft_scanned;
4571 /* need some check for avoid more shrink_zone() */
4575 first_pgdat = zone->zone_pgdat;
4577 /* See comment about same check for global reclaim above */
4578 if (zone->zone_pgdat == last_pgdat)
4580 last_pgdat = zone->zone_pgdat;
4581 shrink_node(zone->zone_pgdat, sc);
4585 consider_reclaim_throttle(first_pgdat, sc);
4588 * Restore to original mask to avoid the impact on the caller if we
4589 * promoted it to __GFP_HIGHMEM.
4591 sc->gfp_mask = orig_mask;
4594 static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat)
4596 struct lruvec *target_lruvec;
4597 unsigned long refaults;
4599 if (lru_gen_enabled())
4602 target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat);
4603 refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON);
4604 target_lruvec->refaults[WORKINGSET_ANON] = refaults;
4605 refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE);
4606 target_lruvec->refaults[WORKINGSET_FILE] = refaults;
4610 * This is the main entry point to direct page reclaim.
4612 * If a full scan of the inactive list fails to free enough memory then we
4613 * are "out of memory" and something needs to be killed.
4615 * If the caller is !__GFP_FS then the probability of a failure is reasonably
4616 * high - the zone may be full of dirty or under-writeback pages, which this
4617 * caller can't do much about. We kick the writeback threads and take explicit
4618 * naps in the hope that some of these pages can be written. But if the
4619 * allocating task holds filesystem locks which prevent writeout this might not
4620 * work, and the allocation attempt will fail.
4622 * returns: 0, if no pages reclaimed
4623 * else, the number of pages reclaimed
4625 static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
4626 struct scan_control *sc)
4628 int initial_priority = sc->priority;
4629 pg_data_t *last_pgdat;
4633 delayacct_freepages_start();
4635 if (!cgroup_reclaim(sc))
4636 __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1);
4640 vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup,
4643 shrink_zones(zonelist, sc);
4645 if (sc->nr_reclaimed >= sc->nr_to_reclaim)
4648 if (sc->compaction_ready)
4652 * If we're getting trouble reclaiming, start doing
4653 * writepage even in laptop mode.
4655 if (sc->priority < DEF_PRIORITY - 2)
4656 sc->may_writepage = 1;
4657 } while (--sc->priority >= 0);
4660 for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx,
4662 if (zone->zone_pgdat == last_pgdat)
4664 last_pgdat = zone->zone_pgdat;
4666 snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat);
4668 if (cgroup_reclaim(sc)) {
4669 struct lruvec *lruvec;
4671 lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup,
4673 clear_bit(LRUVEC_CONGESTED, &lruvec->flags);
4677 delayacct_freepages_end();
4679 if (sc->nr_reclaimed)
4680 return sc->nr_reclaimed;
4682 /* Aborted reclaim to try compaction? don't OOM, then */
4683 if (sc->compaction_ready)
4687 * We make inactive:active ratio decisions based on the node's
4688 * composition of memory, but a restrictive reclaim_idx or a
4689 * memory.low cgroup setting can exempt large amounts of
4690 * memory from reclaim. Neither of which are very common, so
4691 * instead of doing costly eligibility calculations of the
4692 * entire cgroup subtree up front, we assume the estimates are
4693 * good, and retry with forcible deactivation if that fails.
4695 if (sc->skipped_deactivate) {
4696 sc->priority = initial_priority;
4697 sc->force_deactivate = 1;
4698 sc->skipped_deactivate = 0;
4702 /* Untapped cgroup reserves? Don't OOM, retry. */
4703 if (sc->memcg_low_skipped) {
4704 sc->priority = initial_priority;
4705 sc->force_deactivate = 0;
4706 sc->memcg_low_reclaim = 1;
4707 sc->memcg_low_skipped = 0;
4714 static bool allow_direct_reclaim(pg_data_t *pgdat)
4717 unsigned long pfmemalloc_reserve = 0;
4718 unsigned long free_pages = 0;
4722 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
4725 for (i = 0; i <= ZONE_NORMAL; i++) {
4726 zone = &pgdat->node_zones[i];
4727 if (!managed_zone(zone))
4730 if (!zone_reclaimable_pages(zone))
4733 pfmemalloc_reserve += min_wmark_pages(zone);
4734 free_pages += zone_page_state(zone, NR_FREE_PAGES);
4737 /* If there are no reserves (unexpected config) then do not throttle */
4738 if (!pfmemalloc_reserve)
4741 wmark_ok = free_pages > pfmemalloc_reserve / 2;
4743 /* kswapd must be awake if processes are being throttled */
4744 if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
4745 if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL)
4746 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL);
4748 wake_up_interruptible(&pgdat->kswapd_wait);
4755 * Throttle direct reclaimers if backing storage is backed by the network
4756 * and the PFMEMALLOC reserve for the preferred node is getting dangerously
4757 * depleted. kswapd will continue to make progress and wake the processes
4758 * when the low watermark is reached.
4760 * Returns true if a fatal signal was delivered during throttling. If this
4761 * happens, the page allocator should not consider triggering the OOM killer.
4763 static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
4764 nodemask_t *nodemask)
4768 pg_data_t *pgdat = NULL;
4771 * Kernel threads should not be throttled as they may be indirectly
4772 * responsible for cleaning pages necessary for reclaim to make forward
4773 * progress. kjournald for example may enter direct reclaim while
4774 * committing a transaction where throttling it could forcing other
4775 * processes to block on log_wait_commit().
4777 if (current->flags & PF_KTHREAD)
4781 * If a fatal signal is pending, this process should not throttle.
4782 * It should return quickly so it can exit and free its memory
4784 if (fatal_signal_pending(current))
4788 * Check if the pfmemalloc reserves are ok by finding the first node
4789 * with a usable ZONE_NORMAL or lower zone. The expectation is that
4790 * GFP_KERNEL will be required for allocating network buffers when
4791 * swapping over the network so ZONE_HIGHMEM is unusable.
4793 * Throttling is based on the first usable node and throttled processes
4794 * wait on a queue until kswapd makes progress and wakes them. There
4795 * is an affinity then between processes waking up and where reclaim
4796 * progress has been made assuming the process wakes on the same node.
4797 * More importantly, processes running on remote nodes will not compete
4798 * for remote pfmemalloc reserves and processes on different nodes
4799 * should make reasonable progress.
4801 for_each_zone_zonelist_nodemask(zone, z, zonelist,
4802 gfp_zone(gfp_mask), nodemask) {
4803 if (zone_idx(zone) > ZONE_NORMAL)
4806 /* Throttle based on the first usable node */
4807 pgdat = zone->zone_pgdat;
4808 if (allow_direct_reclaim(pgdat))
4813 /* If no zone was usable by the allocation flags then do not throttle */
4817 /* Account for the throttling */
4818 count_vm_event(PGSCAN_DIRECT_THROTTLE);
4821 * If the caller cannot enter the filesystem, it's possible that it
4822 * is due to the caller holding an FS lock or performing a journal
4823 * transaction in the case of a filesystem like ext[3|4]. In this case,
4824 * it is not safe to block on pfmemalloc_wait as kswapd could be
4825 * blocked waiting on the same lock. Instead, throttle for up to a
4826 * second before continuing.
4828 if (!(gfp_mask & __GFP_FS))
4829 wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
4830 allow_direct_reclaim(pgdat), HZ);
4832 /* Throttle until kswapd wakes the process */
4833 wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
4834 allow_direct_reclaim(pgdat));
4836 if (fatal_signal_pending(current))
4843 unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
4844 gfp_t gfp_mask, nodemask_t *nodemask)
4846 unsigned long nr_reclaimed;
4847 struct scan_control sc = {
4848 .nr_to_reclaim = SWAP_CLUSTER_MAX,
4849 .gfp_mask = current_gfp_context(gfp_mask),
4850 .reclaim_idx = gfp_zone(gfp_mask),
4852 .nodemask = nodemask,
4853 .priority = DEF_PRIORITY,
4854 .may_writepage = !laptop_mode,
4860 * scan_control uses s8 fields for order, priority, and reclaim_idx.
4861 * Confirm they are large enough for max values.
4863 BUILD_BUG_ON(MAX_ORDER > S8_MAX);
4864 BUILD_BUG_ON(DEF_PRIORITY > S8_MAX);
4865 BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX);
4868 * Do not enter reclaim if fatal signal was delivered while throttled.
4869 * 1 is returned so that the page allocator does not OOM kill at this
4872 if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask))
4875 set_task_reclaim_state(current, &sc.reclaim_state);
4876 trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask);
4878 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
4880 trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
4881 set_task_reclaim_state(current, NULL);
4883 return nr_reclaimed;
4888 /* Only used by soft limit reclaim. Do not reuse for anything else. */
4889 unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg,
4890 gfp_t gfp_mask, bool noswap,
4892 unsigned long *nr_scanned)
4894 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
4895 struct scan_control sc = {
4896 .nr_to_reclaim = SWAP_CLUSTER_MAX,
4897 .target_mem_cgroup = memcg,
4898 .may_writepage = !laptop_mode,
4900 .reclaim_idx = MAX_NR_ZONES - 1,
4901 .may_swap = !noswap,
4904 WARN_ON_ONCE(!current->reclaim_state);
4906 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
4907 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
4909 trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
4913 * NOTE: Although we can get the priority field, using it
4914 * here is not a good idea, since it limits the pages we can scan.
4915 * if we don't reclaim here, the shrink_node from balance_pgdat
4916 * will pick up pages from other mem cgroup's as well. We hack
4917 * the priority and make it zero.
4919 shrink_lruvec(lruvec, &sc);
4921 trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
4923 *nr_scanned = sc.nr_scanned;
4925 return sc.nr_reclaimed;
4928 unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
4929 unsigned long nr_pages,
4931 unsigned int reclaim_options)
4933 unsigned long nr_reclaimed;
4934 unsigned int noreclaim_flag;
4935 struct scan_control sc = {
4936 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
4937 .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) |
4938 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
4939 .reclaim_idx = MAX_NR_ZONES - 1,
4940 .target_mem_cgroup = memcg,
4941 .priority = DEF_PRIORITY,
4942 .may_writepage = !laptop_mode,
4944 .may_swap = !!(reclaim_options & MEMCG_RECLAIM_MAY_SWAP),
4945 .proactive = !!(reclaim_options & MEMCG_RECLAIM_PROACTIVE),
4948 * Traverse the ZONELIST_FALLBACK zonelist of the current node to put
4949 * equal pressure on all the nodes. This is based on the assumption that
4950 * the reclaim does not bail out early.
4952 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
4954 set_task_reclaim_state(current, &sc.reclaim_state);
4955 trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask);
4956 noreclaim_flag = memalloc_noreclaim_save();
4958 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
4960 memalloc_noreclaim_restore(noreclaim_flag);
4961 trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
4962 set_task_reclaim_state(current, NULL);
4964 return nr_reclaimed;
4968 static void kswapd_age_node(struct pglist_data *pgdat, struct scan_control *sc)
4970 struct mem_cgroup *memcg;
4971 struct lruvec *lruvec;
4973 if (lru_gen_enabled()) {
4974 lru_gen_age_node(pgdat, sc);
4978 if (!can_age_anon_pages(pgdat, sc))
4981 lruvec = mem_cgroup_lruvec(NULL, pgdat);
4982 if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON))
4985 memcg = mem_cgroup_iter(NULL, NULL, NULL);
4987 lruvec = mem_cgroup_lruvec(memcg, pgdat);
4988 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
4989 sc, LRU_ACTIVE_ANON);
4990 memcg = mem_cgroup_iter(NULL, memcg, NULL);
4994 static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx)
5000 * Check for watermark boosts top-down as the higher zones
5001 * are more likely to be boosted. Both watermarks and boosts
5002 * should not be checked at the same time as reclaim would
5003 * start prematurely when there is no boosting and a lower
5006 for (i = highest_zoneidx; i >= 0; i--) {
5007 zone = pgdat->node_zones + i;
5008 if (!managed_zone(zone))
5011 if (zone->watermark_boost)
5019 * Returns true if there is an eligible zone balanced for the request order
5020 * and highest_zoneidx
5022 static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx)
5025 unsigned long mark = -1;
5029 * Check watermarks bottom-up as lower zones are more likely to
5032 for (i = 0; i <= highest_zoneidx; i++) {
5033 zone = pgdat->node_zones + i;
5035 if (!managed_zone(zone))
5038 if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
5039 mark = wmark_pages(zone, WMARK_PROMO);
5041 mark = high_wmark_pages(zone);
5042 if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx))
5047 * If a node has no managed zone within highest_zoneidx, it does not
5048 * need balancing by definition. This can happen if a zone-restricted
5049 * allocation tries to wake a remote kswapd.
5057 /* Clear pgdat state for congested, dirty or under writeback. */
5058 static void clear_pgdat_congested(pg_data_t *pgdat)
5060 struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat);
5062 clear_bit(LRUVEC_CONGESTED, &lruvec->flags);
5063 clear_bit(PGDAT_DIRTY, &pgdat->flags);
5064 clear_bit(PGDAT_WRITEBACK, &pgdat->flags);
5068 * Prepare kswapd for sleeping. This verifies that there are no processes
5069 * waiting in throttle_direct_reclaim() and that watermarks have been met.
5071 * Returns true if kswapd is ready to sleep
5073 static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order,
5074 int highest_zoneidx)
5077 * The throttled processes are normally woken up in balance_pgdat() as
5078 * soon as allow_direct_reclaim() is true. But there is a potential
5079 * race between when kswapd checks the watermarks and a process gets
5080 * throttled. There is also a potential race if processes get
5081 * throttled, kswapd wakes, a large process exits thereby balancing the
5082 * zones, which causes kswapd to exit balance_pgdat() before reaching
5083 * the wake up checks. If kswapd is going to sleep, no process should
5084 * be sleeping on pfmemalloc_wait, so wake them now if necessary. If
5085 * the wake up is premature, processes will wake kswapd and get
5086 * throttled again. The difference from wake ups in balance_pgdat() is
5087 * that here we are under prepare_to_wait().
5089 if (waitqueue_active(&pgdat->pfmemalloc_wait))
5090 wake_up_all(&pgdat->pfmemalloc_wait);
5092 /* Hopeless node, leave it to direct reclaim */
5093 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
5096 if (pgdat_balanced(pgdat, order, highest_zoneidx)) {
5097 clear_pgdat_congested(pgdat);
5105 * kswapd shrinks a node of pages that are at or below the highest usable
5106 * zone that is currently unbalanced.
5108 * Returns true if kswapd scanned at least the requested number of pages to
5109 * reclaim or if the lack of progress was due to pages under writeback.
5110 * This is used to determine if the scanning priority needs to be raised.
5112 static bool kswapd_shrink_node(pg_data_t *pgdat,
5113 struct scan_control *sc)
5118 /* Reclaim a number of pages proportional to the number of zones */
5119 sc->nr_to_reclaim = 0;
5120 for (z = 0; z <= sc->reclaim_idx; z++) {
5121 zone = pgdat->node_zones + z;
5122 if (!managed_zone(zone))
5125 sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX);
5129 * Historically care was taken to put equal pressure on all zones but
5130 * now pressure is applied based on node LRU order.
5132 shrink_node(pgdat, sc);
5135 * Fragmentation may mean that the system cannot be rebalanced for
5136 * high-order allocations. If twice the allocation size has been
5137 * reclaimed then recheck watermarks only at order-0 to prevent
5138 * excessive reclaim. Assume that a process requested a high-order
5139 * can direct reclaim/compact.
5141 if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order))
5144 return sc->nr_scanned >= sc->nr_to_reclaim;
5147 /* Page allocator PCP high watermark is lowered if reclaim is active. */
5149 update_reclaim_active(pg_data_t *pgdat, int highest_zoneidx, bool active)
5154 for (i = 0; i <= highest_zoneidx; i++) {
5155 zone = pgdat->node_zones + i;
5157 if (!managed_zone(zone))
5161 set_bit(ZONE_RECLAIM_ACTIVE, &zone->flags);
5163 clear_bit(ZONE_RECLAIM_ACTIVE, &zone->flags);
5168 set_reclaim_active(pg_data_t *pgdat, int highest_zoneidx)
5170 update_reclaim_active(pgdat, highest_zoneidx, true);
5174 clear_reclaim_active(pg_data_t *pgdat, int highest_zoneidx)
5176 update_reclaim_active(pgdat, highest_zoneidx, false);
5180 * For kswapd, balance_pgdat() will reclaim pages across a node from zones
5181 * that are eligible for use by the caller until at least one zone is
5184 * Returns the order kswapd finished reclaiming at.
5186 * kswapd scans the zones in the highmem->normal->dma direction. It skips
5187 * zones which have free_pages > high_wmark_pages(zone), but once a zone is
5188 * found to have free_pages <= high_wmark_pages(zone), any page in that zone
5189 * or lower is eligible for reclaim until at least one usable zone is
5192 static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx)
5195 unsigned long nr_soft_reclaimed;
5196 unsigned long nr_soft_scanned;
5197 unsigned long pflags;
5198 unsigned long nr_boost_reclaim;
5199 unsigned long zone_boosts[MAX_NR_ZONES] = { 0, };
5202 struct scan_control sc = {
5203 .gfp_mask = GFP_KERNEL,
5208 set_task_reclaim_state(current, &sc.reclaim_state);
5209 psi_memstall_enter(&pflags);
5210 __fs_reclaim_acquire(_THIS_IP_);
5212 count_vm_event(PAGEOUTRUN);
5215 * Account for the reclaim boost. Note that the zone boost is left in
5216 * place so that parallel allocations that are near the watermark will
5217 * stall or direct reclaim until kswapd is finished.
5219 nr_boost_reclaim = 0;
5220 for (i = 0; i <= highest_zoneidx; i++) {
5221 zone = pgdat->node_zones + i;
5222 if (!managed_zone(zone))
5225 nr_boost_reclaim += zone->watermark_boost;
5226 zone_boosts[i] = zone->watermark_boost;
5228 boosted = nr_boost_reclaim;
5231 set_reclaim_active(pgdat, highest_zoneidx);
5232 sc.priority = DEF_PRIORITY;
5234 unsigned long nr_reclaimed = sc.nr_reclaimed;
5235 bool raise_priority = true;
5239 sc.reclaim_idx = highest_zoneidx;
5242 * If the number of buffer_heads exceeds the maximum allowed
5243 * then consider reclaiming from all zones. This has a dual
5244 * purpose -- on 64-bit systems it is expected that
5245 * buffer_heads are stripped during active rotation. On 32-bit
5246 * systems, highmem pages can pin lowmem memory and shrinking
5247 * buffers can relieve lowmem pressure. Reclaim may still not
5248 * go ahead if all eligible zones for the original allocation
5249 * request are balanced to avoid excessive reclaim from kswapd.
5251 if (buffer_heads_over_limit) {
5252 for (i = MAX_NR_ZONES - 1; i >= 0; i--) {
5253 zone = pgdat->node_zones + i;
5254 if (!managed_zone(zone))
5263 * If the pgdat is imbalanced then ignore boosting and preserve
5264 * the watermarks for a later time and restart. Note that the
5265 * zone watermarks will be still reset at the end of balancing
5266 * on the grounds that the normal reclaim should be enough to
5267 * re-evaluate if boosting is required when kswapd next wakes.
5269 balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx);
5270 if (!balanced && nr_boost_reclaim) {
5271 nr_boost_reclaim = 0;
5276 * If boosting is not active then only reclaim if there are no
5277 * eligible zones. Note that sc.reclaim_idx is not used as
5278 * buffer_heads_over_limit may have adjusted it.
5280 if (!nr_boost_reclaim && balanced)
5283 /* Limit the priority of boosting to avoid reclaim writeback */
5284 if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2)
5285 raise_priority = false;
5288 * Do not writeback or swap pages for boosted reclaim. The
5289 * intent is to relieve pressure not issue sub-optimal IO
5290 * from reclaim context. If no pages are reclaimed, the
5291 * reclaim will be aborted.
5293 sc.may_writepage = !laptop_mode && !nr_boost_reclaim;
5294 sc.may_swap = !nr_boost_reclaim;
5297 * Do some background aging, to give pages a chance to be
5298 * referenced before reclaiming. All pages are rotated
5299 * regardless of classzone as this is about consistent aging.
5301 kswapd_age_node(pgdat, &sc);
5304 * If we're getting trouble reclaiming, start doing writepage
5305 * even in laptop mode.
5307 if (sc.priority < DEF_PRIORITY - 2)
5308 sc.may_writepage = 1;
5310 /* Call soft limit reclaim before calling shrink_node. */
5312 nr_soft_scanned = 0;
5313 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order,
5314 sc.gfp_mask, &nr_soft_scanned);
5315 sc.nr_reclaimed += nr_soft_reclaimed;
5318 * There should be no need to raise the scanning priority if
5319 * enough pages are already being scanned that that high
5320 * watermark would be met at 100% efficiency.
5322 if (kswapd_shrink_node(pgdat, &sc))
5323 raise_priority = false;
5326 * If the low watermark is met there is no need for processes
5327 * to be throttled on pfmemalloc_wait as they should not be
5328 * able to safely make forward progress. Wake them
5330 if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
5331 allow_direct_reclaim(pgdat))
5332 wake_up_all(&pgdat->pfmemalloc_wait);
5334 /* Check if kswapd should be suspending */
5335 __fs_reclaim_release(_THIS_IP_);
5336 ret = try_to_freeze();
5337 __fs_reclaim_acquire(_THIS_IP_);
5338 if (ret || kthread_should_stop())
5342 * Raise priority if scanning rate is too low or there was no
5343 * progress in reclaiming pages
5345 nr_reclaimed = sc.nr_reclaimed - nr_reclaimed;
5346 nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed);
5349 * If reclaim made no progress for a boost, stop reclaim as
5350 * IO cannot be queued and it could be an infinite loop in
5351 * extreme circumstances.
5353 if (nr_boost_reclaim && !nr_reclaimed)
5356 if (raise_priority || !nr_reclaimed)
5358 } while (sc.priority >= 1);
5360 if (!sc.nr_reclaimed)
5361 pgdat->kswapd_failures++;
5364 clear_reclaim_active(pgdat, highest_zoneidx);
5366 /* If reclaim was boosted, account for the reclaim done in this pass */
5368 unsigned long flags;
5370 for (i = 0; i <= highest_zoneidx; i++) {
5371 if (!zone_boosts[i])
5374 /* Increments are under the zone lock */
5375 zone = pgdat->node_zones + i;
5376 spin_lock_irqsave(&zone->lock, flags);
5377 zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]);
5378 spin_unlock_irqrestore(&zone->lock, flags);
5382 * As there is now likely space, wakeup kcompact to defragment
5385 wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx);
5388 snapshot_refaults(NULL, pgdat);
5389 __fs_reclaim_release(_THIS_IP_);
5390 psi_memstall_leave(&pflags);
5391 set_task_reclaim_state(current, NULL);
5394 * Return the order kswapd stopped reclaiming at as
5395 * prepare_kswapd_sleep() takes it into account. If another caller
5396 * entered the allocator slow path while kswapd was awake, order will
5397 * remain at the higher level.
5403 * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to
5404 * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is
5405 * not a valid index then either kswapd runs for first time or kswapd couldn't
5406 * sleep after previous reclaim attempt (node is still unbalanced). In that
5407 * case return the zone index of the previous kswapd reclaim cycle.
5409 static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,
5410 enum zone_type prev_highest_zoneidx)
5412 enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
5414 return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx;
5417 static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order,
5418 unsigned int highest_zoneidx)
5423 if (freezing(current) || kthread_should_stop())
5426 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
5429 * Try to sleep for a short interval. Note that kcompactd will only be
5430 * woken if it is possible to sleep for a short interval. This is
5431 * deliberate on the assumption that if reclaim cannot keep an
5432 * eligible zone balanced that it's also unlikely that compaction will
5435 if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
5437 * Compaction records what page blocks it recently failed to
5438 * isolate pages from and skips them in the future scanning.
5439 * When kswapd is going to sleep, it is reasonable to assume
5440 * that pages and compaction may succeed so reset the cache.
5442 reset_isolation_suitable(pgdat);
5445 * We have freed the memory, now we should compact it to make
5446 * allocation of the requested order possible.
5448 wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx);
5450 remaining = schedule_timeout(HZ/10);
5453 * If woken prematurely then reset kswapd_highest_zoneidx and
5454 * order. The values will either be from a wakeup request or
5455 * the previous request that slept prematurely.
5458 WRITE_ONCE(pgdat->kswapd_highest_zoneidx,
5459 kswapd_highest_zoneidx(pgdat,
5462 if (READ_ONCE(pgdat->kswapd_order) < reclaim_order)
5463 WRITE_ONCE(pgdat->kswapd_order, reclaim_order);
5466 finish_wait(&pgdat->kswapd_wait, &wait);
5467 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
5471 * After a short sleep, check if it was a premature sleep. If not, then
5472 * go fully to sleep until explicitly woken up.
5475 prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
5476 trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
5479 * vmstat counters are not perfectly accurate and the estimated
5480 * value for counters such as NR_FREE_PAGES can deviate from the
5481 * true value by nr_online_cpus * threshold. To avoid the zone
5482 * watermarks being breached while under pressure, we reduce the
5483 * per-cpu vmstat threshold while kswapd is awake and restore
5484 * them before going back to sleep.
5486 set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
5488 if (!kthread_should_stop())
5491 set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
5494 count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
5496 count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
5498 finish_wait(&pgdat->kswapd_wait, &wait);
5502 * The background pageout daemon, started as a kernel thread
5503 * from the init process.
5505 * This basically trickles out pages so that we have _some_
5506 * free memory available even if there is no other activity
5507 * that frees anything up. This is needed for things like routing
5508 * etc, where we otherwise might have all activity going on in
5509 * asynchronous contexts that cannot page things out.
5511 * If there are applications that are active memory-allocators
5512 * (most normal use), this basically shouldn't matter.
5514 static int kswapd(void *p)
5516 unsigned int alloc_order, reclaim_order;
5517 unsigned int highest_zoneidx = MAX_NR_ZONES - 1;
5518 pg_data_t *pgdat = (pg_data_t *)p;
5519 struct task_struct *tsk = current;
5520 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
5522 if (!cpumask_empty(cpumask))
5523 set_cpus_allowed_ptr(tsk, cpumask);
5526 * Tell the memory management that we're a "memory allocator",
5527 * and that if we need more memory we should get access to it
5528 * regardless (see "__alloc_pages()"). "kswapd" should
5529 * never get caught in the normal page freeing logic.
5531 * (Kswapd normally doesn't need memory anyway, but sometimes
5532 * you need a small amount of memory in order to be able to
5533 * page out something else, and this flag essentially protects
5534 * us from recursively trying to free more memory as we're
5535 * trying to free the first piece of memory in the first place).
5537 tsk->flags |= PF_MEMALLOC | PF_KSWAPD;
5540 WRITE_ONCE(pgdat->kswapd_order, 0);
5541 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
5542 atomic_set(&pgdat->nr_writeback_throttled, 0);
5546 alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order);
5547 highest_zoneidx = kswapd_highest_zoneidx(pgdat,
5551 kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order,
5554 /* Read the new order and highest_zoneidx */
5555 alloc_order = READ_ONCE(pgdat->kswapd_order);
5556 highest_zoneidx = kswapd_highest_zoneidx(pgdat,
5558 WRITE_ONCE(pgdat->kswapd_order, 0);
5559 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
5561 ret = try_to_freeze();
5562 if (kthread_should_stop())
5566 * We can speed up thawing tasks if we don't call balance_pgdat
5567 * after returning from the refrigerator
5573 * Reclaim begins at the requested order but if a high-order
5574 * reclaim fails then kswapd falls back to reclaiming for
5575 * order-0. If that happens, kswapd will consider sleeping
5576 * for the order it finished reclaiming at (reclaim_order)
5577 * but kcompactd is woken to compact for the original
5578 * request (alloc_order).
5580 trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx,
5582 reclaim_order = balance_pgdat(pgdat, alloc_order,
5584 if (reclaim_order < alloc_order)
5585 goto kswapd_try_sleep;
5588 tsk->flags &= ~(PF_MEMALLOC | PF_KSWAPD);
5594 * A zone is low on free memory or too fragmented for high-order memory. If
5595 * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's
5596 * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim
5597 * has failed or is not needed, still wake up kcompactd if only compaction is
5600 void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order,
5601 enum zone_type highest_zoneidx)
5604 enum zone_type curr_idx;
5606 if (!managed_zone(zone))
5609 if (!cpuset_zone_allowed(zone, gfp_flags))
5612 pgdat = zone->zone_pgdat;
5613 curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
5615 if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx)
5616 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx);
5618 if (READ_ONCE(pgdat->kswapd_order) < order)
5619 WRITE_ONCE(pgdat->kswapd_order, order);
5621 if (!waitqueue_active(&pgdat->kswapd_wait))
5624 /* Hopeless node, leave it to direct reclaim if possible */
5625 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ||
5626 (pgdat_balanced(pgdat, order, highest_zoneidx) &&
5627 !pgdat_watermark_boosted(pgdat, highest_zoneidx))) {
5629 * There may be plenty of free memory available, but it's too
5630 * fragmented for high-order allocations. Wake up kcompactd
5631 * and rely on compaction_suitable() to determine if it's
5632 * needed. If it fails, it will defer subsequent attempts to
5633 * ratelimit its work.
5635 if (!(gfp_flags & __GFP_DIRECT_RECLAIM))
5636 wakeup_kcompactd(pgdat, order, highest_zoneidx);
5640 trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order,
5642 wake_up_interruptible(&pgdat->kswapd_wait);
5645 #ifdef CONFIG_HIBERNATION
5647 * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
5650 * Rather than trying to age LRUs the aim is to preserve the overall
5651 * LRU order by reclaiming preferentially
5652 * inactive > active > active referenced > active mapped
5654 unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
5656 struct scan_control sc = {
5657 .nr_to_reclaim = nr_to_reclaim,
5658 .gfp_mask = GFP_HIGHUSER_MOVABLE,
5659 .reclaim_idx = MAX_NR_ZONES - 1,
5660 .priority = DEF_PRIORITY,
5664 .hibernation_mode = 1,
5666 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
5667 unsigned long nr_reclaimed;
5668 unsigned int noreclaim_flag;
5670 fs_reclaim_acquire(sc.gfp_mask);
5671 noreclaim_flag = memalloc_noreclaim_save();
5672 set_task_reclaim_state(current, &sc.reclaim_state);
5674 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
5676 set_task_reclaim_state(current, NULL);
5677 memalloc_noreclaim_restore(noreclaim_flag);
5678 fs_reclaim_release(sc.gfp_mask);
5680 return nr_reclaimed;
5682 #endif /* CONFIG_HIBERNATION */
5685 * This kswapd start function will be called by init and node-hot-add.
5687 void kswapd_run(int nid)
5689 pg_data_t *pgdat = NODE_DATA(nid);
5691 pgdat_kswapd_lock(pgdat);
5692 if (!pgdat->kswapd) {
5693 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
5694 if (IS_ERR(pgdat->kswapd)) {
5695 /* failure at boot is fatal */
5696 BUG_ON(system_state < SYSTEM_RUNNING);
5697 pr_err("Failed to start kswapd on node %d\n", nid);
5698 pgdat->kswapd = NULL;
5701 pgdat_kswapd_unlock(pgdat);
5705 * Called by memory hotplug when all memory in a node is offlined. Caller must
5706 * be holding mem_hotplug_begin/done().
5708 void kswapd_stop(int nid)
5710 pg_data_t *pgdat = NODE_DATA(nid);
5711 struct task_struct *kswapd;
5713 pgdat_kswapd_lock(pgdat);
5714 kswapd = pgdat->kswapd;
5716 kthread_stop(kswapd);
5717 pgdat->kswapd = NULL;
5719 pgdat_kswapd_unlock(pgdat);
5722 static int __init kswapd_init(void)
5727 for_each_node_state(nid, N_MEMORY)
5732 module_init(kswapd_init)
5738 * If non-zero call node_reclaim when the number of free pages falls below
5741 int node_reclaim_mode __read_mostly;
5744 * Priority for NODE_RECLAIM. This determines the fraction of pages
5745 * of a node considered for each zone_reclaim. 4 scans 1/16th of
5748 #define NODE_RECLAIM_PRIORITY 4
5751 * Percentage of pages in a zone that must be unmapped for node_reclaim to
5754 int sysctl_min_unmapped_ratio = 1;
5757 * If the number of slab pages in a zone grows beyond this percentage then
5758 * slab reclaim needs to occur.
5760 int sysctl_min_slab_ratio = 5;
5762 static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat)
5764 unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED);
5765 unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) +
5766 node_page_state(pgdat, NR_ACTIVE_FILE);
5769 * It's possible for there to be more file mapped pages than
5770 * accounted for by the pages on the file LRU lists because
5771 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
5773 return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
5776 /* Work out how many page cache pages we can reclaim in this reclaim_mode */
5777 static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat)
5779 unsigned long nr_pagecache_reclaimable;
5780 unsigned long delta = 0;
5783 * If RECLAIM_UNMAP is set, then all file pages are considered
5784 * potentially reclaimable. Otherwise, we have to worry about
5785 * pages like swapcache and node_unmapped_file_pages() provides
5788 if (node_reclaim_mode & RECLAIM_UNMAP)
5789 nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES);
5791 nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat);
5793 /* If we can't clean pages, remove dirty pages from consideration */
5794 if (!(node_reclaim_mode & RECLAIM_WRITE))
5795 delta += node_page_state(pgdat, NR_FILE_DIRTY);
5797 /* Watch for any possible underflows due to delta */
5798 if (unlikely(delta > nr_pagecache_reclaimable))
5799 delta = nr_pagecache_reclaimable;
5801 return nr_pagecache_reclaimable - delta;
5805 * Try to free up some pages from this node through reclaim.
5807 static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
5809 /* Minimum pages needed in order to stay on node */
5810 const unsigned long nr_pages = 1 << order;
5811 struct task_struct *p = current;
5812 unsigned int noreclaim_flag;
5813 struct scan_control sc = {
5814 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
5815 .gfp_mask = current_gfp_context(gfp_mask),
5817 .priority = NODE_RECLAIM_PRIORITY,
5818 .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE),
5819 .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP),
5821 .reclaim_idx = gfp_zone(gfp_mask),
5823 unsigned long pflags;
5825 trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order,
5829 psi_memstall_enter(&pflags);
5830 fs_reclaim_acquire(sc.gfp_mask);
5832 * We need to be able to allocate from the reserves for RECLAIM_UNMAP
5834 noreclaim_flag = memalloc_noreclaim_save();
5835 set_task_reclaim_state(p, &sc.reclaim_state);
5837 if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages ||
5838 node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) > pgdat->min_slab_pages) {
5840 * Free memory by calling shrink node with increasing
5841 * priorities until we have enough memory freed.
5844 shrink_node(pgdat, &sc);
5845 } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
5848 set_task_reclaim_state(p, NULL);
5849 memalloc_noreclaim_restore(noreclaim_flag);
5850 fs_reclaim_release(sc.gfp_mask);
5851 psi_memstall_leave(&pflags);
5853 trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed);
5855 return sc.nr_reclaimed >= nr_pages;
5858 int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
5863 * Node reclaim reclaims unmapped file backed pages and
5864 * slab pages if we are over the defined limits.
5866 * A small portion of unmapped file backed pages is needed for
5867 * file I/O otherwise pages read by file I/O will be immediately
5868 * thrown out if the node is overallocated. So we do not reclaim
5869 * if less than a specified percentage of the node is used by
5870 * unmapped file backed pages.
5872 if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages &&
5873 node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <=
5874 pgdat->min_slab_pages)
5875 return NODE_RECLAIM_FULL;
5878 * Do not scan if the allocation should not be delayed.
5880 if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC))
5881 return NODE_RECLAIM_NOSCAN;
5884 * Only run node reclaim on the local node or on nodes that do not
5885 * have associated processors. This will favor the local processor
5886 * over remote processors and spread off node memory allocations
5887 * as wide as possible.
5889 if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
5890 return NODE_RECLAIM_NOSCAN;
5892 if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
5893 return NODE_RECLAIM_NOSCAN;
5895 ret = __node_reclaim(pgdat, gfp_mask, order);
5896 clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags);
5899 count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
5905 void check_move_unevictable_pages(struct pagevec *pvec)
5907 struct folio_batch fbatch;
5910 folio_batch_init(&fbatch);
5911 for (i = 0; i < pvec->nr; i++) {
5912 struct page *page = pvec->pages[i];
5914 if (PageTransTail(page))
5916 folio_batch_add(&fbatch, page_folio(page));
5918 check_move_unevictable_folios(&fbatch);
5920 EXPORT_SYMBOL_GPL(check_move_unevictable_pages);
5923 * check_move_unevictable_folios - Move evictable folios to appropriate zone
5925 * @fbatch: Batch of lru folios to check.
5927 * Checks folios for evictability, if an evictable folio is in the unevictable
5928 * lru list, moves it to the appropriate evictable lru list. This function
5929 * should be only used for lru folios.
5931 void check_move_unevictable_folios(struct folio_batch *fbatch)
5933 struct lruvec *lruvec = NULL;
5938 for (i = 0; i < fbatch->nr; i++) {
5939 struct folio *folio = fbatch->folios[i];
5940 int nr_pages = folio_nr_pages(folio);
5942 pgscanned += nr_pages;
5944 /* block memcg migration while the folio moves between lrus */
5945 if (!folio_test_clear_lru(folio))
5948 lruvec = folio_lruvec_relock_irq(folio, lruvec);
5949 if (folio_evictable(folio) && folio_test_unevictable(folio)) {
5950 lruvec_del_folio(lruvec, folio);
5951 folio_clear_unevictable(folio);
5952 lruvec_add_folio(lruvec, folio);
5953 pgrescued += nr_pages;
5955 folio_set_lru(folio);
5959 __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
5960 __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
5961 unlock_page_lruvec_irq(lruvec);
5962 } else if (pgscanned) {
5963 count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
5966 EXPORT_SYMBOL_GPL(check_move_unevictable_folios);