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.
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/mutex.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/memory-tiers.h>
47 #include <linux/oom.h>
48 #include <linux/pagevec.h>
49 #include <linux/prefetch.h>
50 #include <linux/printk.h>
51 #include <linux/dax.h>
52 #include <linux/psi.h>
53 #include <linux/pagewalk.h>
54 #include <linux/shmem_fs.h>
55 #include <linux/ctype.h>
56 #include <linux/debugfs.h>
57 #include <linux/khugepaged.h>
58 #include <linux/rculist_nulls.h>
59 #include <linux/random.h>
60 #include <linux/srcu.h>
62 #include <asm/tlbflush.h>
63 #include <asm/div64.h>
65 #include <linux/swapops.h>
66 #include <linux/balloon_compaction.h>
67 #include <linux/sched/sysctl.h>
72 #define CREATE_TRACE_POINTS
73 #include <trace/events/vmscan.h>
76 /* How many pages shrink_list() should reclaim */
77 unsigned long nr_to_reclaim;
80 * Nodemask of nodes allowed by the caller. If NULL, all nodes
86 * The memory cgroup that hit its limit and as a result is the
87 * primary target of this reclaim invocation.
89 struct mem_cgroup *target_mem_cgroup;
92 * Scan pressure balancing between anon and file LRUs
94 unsigned long anon_cost;
95 unsigned long file_cost;
97 /* Can active folios be deactivated as part of reclaim? */
98 #define DEACTIVATE_ANON 1
99 #define DEACTIVATE_FILE 2
100 unsigned int may_deactivate:2;
101 unsigned int force_deactivate:1;
102 unsigned int skipped_deactivate:1;
104 /* Writepage batching in laptop mode; RECLAIM_WRITE */
105 unsigned int may_writepage:1;
107 /* Can mapped folios be reclaimed? */
108 unsigned int may_unmap:1;
110 /* Can folios be swapped as part of reclaim? */
111 unsigned int may_swap:1;
113 /* Proactive reclaim invoked by userspace through memory.reclaim */
114 unsigned int proactive:1;
117 * Cgroup memory below memory.low is protected as long as we
118 * don't threaten to OOM. If any cgroup is reclaimed at
119 * reduced force or passed over entirely due to its memory.low
120 * setting (memcg_low_skipped), and nothing is reclaimed as a
121 * result, then go back for one more cycle that reclaims the protected
122 * memory (memcg_low_reclaim) to avert OOM.
124 unsigned int memcg_low_reclaim:1;
125 unsigned int memcg_low_skipped:1;
127 unsigned int hibernation_mode:1;
129 /* One of the zones is ready for compaction */
130 unsigned int compaction_ready:1;
132 /* There is easily reclaimable cold cache in the current node */
133 unsigned int cache_trim_mode:1;
135 /* The file folios on the current node are dangerously low */
136 unsigned int file_is_tiny:1;
138 /* Always discard instead of demoting to lower tier memory */
139 unsigned int no_demotion:1;
141 /* Allocation order */
144 /* Scan (total_size >> priority) pages at once */
147 /* The highest zone to isolate folios for reclaim from */
150 /* This context's GFP mask */
153 /* Incremented by the number of inactive pages that were scanned */
154 unsigned long nr_scanned;
156 /* Number of pages freed so far during a call to shrink_zones() */
157 unsigned long nr_reclaimed;
161 unsigned int unqueued_dirty;
162 unsigned int congested;
163 unsigned int writeback;
164 unsigned int immediate;
165 unsigned int file_taken;
169 /* for recording the reclaimed slab by now */
170 struct reclaim_state reclaim_state;
173 #ifdef ARCH_HAS_PREFETCHW
174 #define prefetchw_prev_lru_folio(_folio, _base, _field) \
176 if ((_folio)->lru.prev != _base) { \
177 struct folio *prev; \
179 prev = lru_to_folio(&(_folio->lru)); \
180 prefetchw(&prev->_field); \
184 #define prefetchw_prev_lru_folio(_folio, _base, _field) do { } while (0)
188 * From 0 .. 200. Higher means more swappy.
190 int vm_swappiness = 60;
192 static void set_task_reclaim_state(struct task_struct *task,
193 struct reclaim_state *rs)
195 /* Check for an overwrite */
196 WARN_ON_ONCE(rs && task->reclaim_state);
198 /* Check for the nulling of an already-nulled member */
199 WARN_ON_ONCE(!rs && !task->reclaim_state);
201 task->reclaim_state = rs;
204 LIST_HEAD(shrinker_list);
205 DEFINE_MUTEX(shrinker_mutex);
206 DEFINE_SRCU(shrinker_srcu);
207 static atomic_t shrinker_srcu_generation = ATOMIC_INIT(0);
210 static int shrinker_nr_max;
212 /* The shrinker_info is expanded in a batch of BITS_PER_LONG */
213 static inline int shrinker_map_size(int nr_items)
215 return (DIV_ROUND_UP(nr_items, BITS_PER_LONG) * sizeof(unsigned long));
218 static inline int shrinker_defer_size(int nr_items)
220 return (round_up(nr_items, BITS_PER_LONG) * sizeof(atomic_long_t));
223 static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg,
226 return srcu_dereference_check(memcg->nodeinfo[nid]->shrinker_info,
228 lockdep_is_held(&shrinker_mutex));
231 static struct shrinker_info *shrinker_info_srcu(struct mem_cgroup *memcg,
234 return srcu_dereference(memcg->nodeinfo[nid]->shrinker_info,
238 static void free_shrinker_info_rcu(struct rcu_head *head)
240 kvfree(container_of(head, struct shrinker_info, rcu));
243 static int expand_one_shrinker_info(struct mem_cgroup *memcg,
244 int map_size, int defer_size,
245 int old_map_size, int old_defer_size,
248 struct shrinker_info *new, *old;
249 struct mem_cgroup_per_node *pn;
251 int size = map_size + defer_size;
254 pn = memcg->nodeinfo[nid];
255 old = shrinker_info_protected(memcg, nid);
256 /* Not yet online memcg */
260 /* Already expanded this shrinker_info */
261 if (new_nr_max <= old->map_nr_max)
264 new = kvmalloc_node(sizeof(*new) + size, GFP_KERNEL, nid);
268 new->nr_deferred = (atomic_long_t *)(new + 1);
269 new->map = (void *)new->nr_deferred + defer_size;
270 new->map_nr_max = new_nr_max;
272 /* map: set all old bits, clear all new bits */
273 memset(new->map, (int)0xff, old_map_size);
274 memset((void *)new->map + old_map_size, 0, map_size - old_map_size);
275 /* nr_deferred: copy old values, clear all new values */
276 memcpy(new->nr_deferred, old->nr_deferred, old_defer_size);
277 memset((void *)new->nr_deferred + old_defer_size, 0,
278 defer_size - old_defer_size);
280 rcu_assign_pointer(pn->shrinker_info, new);
281 call_srcu(&shrinker_srcu, &old->rcu, free_shrinker_info_rcu);
287 void free_shrinker_info(struct mem_cgroup *memcg)
289 struct mem_cgroup_per_node *pn;
290 struct shrinker_info *info;
294 pn = memcg->nodeinfo[nid];
295 info = rcu_dereference_protected(pn->shrinker_info, true);
297 rcu_assign_pointer(pn->shrinker_info, NULL);
301 int alloc_shrinker_info(struct mem_cgroup *memcg)
303 struct shrinker_info *info;
304 int nid, size, ret = 0;
305 int map_size, defer_size = 0;
307 mutex_lock(&shrinker_mutex);
308 map_size = shrinker_map_size(shrinker_nr_max);
309 defer_size = shrinker_defer_size(shrinker_nr_max);
310 size = map_size + defer_size;
312 info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid);
314 free_shrinker_info(memcg);
318 info->nr_deferred = (atomic_long_t *)(info + 1);
319 info->map = (void *)info->nr_deferred + defer_size;
320 info->map_nr_max = shrinker_nr_max;
321 rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info);
323 mutex_unlock(&shrinker_mutex);
328 static int expand_shrinker_info(int new_id)
331 int new_nr_max = round_up(new_id + 1, BITS_PER_LONG);
332 int map_size, defer_size = 0;
333 int old_map_size, old_defer_size = 0;
334 struct mem_cgroup *memcg;
336 if (!root_mem_cgroup)
339 lockdep_assert_held(&shrinker_mutex);
341 map_size = shrinker_map_size(new_nr_max);
342 defer_size = shrinker_defer_size(new_nr_max);
343 old_map_size = shrinker_map_size(shrinker_nr_max);
344 old_defer_size = shrinker_defer_size(shrinker_nr_max);
346 memcg = mem_cgroup_iter(NULL, NULL, NULL);
348 ret = expand_one_shrinker_info(memcg, map_size, defer_size,
349 old_map_size, old_defer_size,
352 mem_cgroup_iter_break(NULL, memcg);
355 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
358 shrinker_nr_max = new_nr_max;
363 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id)
365 if (shrinker_id >= 0 && memcg && !mem_cgroup_is_root(memcg)) {
366 struct shrinker_info *info;
369 srcu_idx = srcu_read_lock(&shrinker_srcu);
370 info = shrinker_info_srcu(memcg, nid);
371 if (!WARN_ON_ONCE(shrinker_id >= info->map_nr_max)) {
372 /* Pairs with smp mb in shrink_slab() */
373 smp_mb__before_atomic();
374 set_bit(shrinker_id, info->map);
376 srcu_read_unlock(&shrinker_srcu, srcu_idx);
380 static DEFINE_IDR(shrinker_idr);
382 static int prealloc_memcg_shrinker(struct shrinker *shrinker)
384 int id, ret = -ENOMEM;
386 if (mem_cgroup_disabled())
389 mutex_lock(&shrinker_mutex);
390 id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL);
394 if (id >= shrinker_nr_max) {
395 if (expand_shrinker_info(id)) {
396 idr_remove(&shrinker_idr, id);
403 mutex_unlock(&shrinker_mutex);
407 static void unregister_memcg_shrinker(struct shrinker *shrinker)
409 int id = shrinker->id;
413 lockdep_assert_held(&shrinker_mutex);
415 idr_remove(&shrinker_idr, id);
418 static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker,
419 struct mem_cgroup *memcg)
421 struct shrinker_info *info;
423 info = shrinker_info_srcu(memcg, nid);
424 return atomic_long_xchg(&info->nr_deferred[shrinker->id], 0);
427 static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker,
428 struct mem_cgroup *memcg)
430 struct shrinker_info *info;
432 info = shrinker_info_srcu(memcg, nid);
433 return atomic_long_add_return(nr, &info->nr_deferred[shrinker->id]);
436 void reparent_shrinker_deferred(struct mem_cgroup *memcg)
440 struct mem_cgroup *parent;
441 struct shrinker_info *child_info, *parent_info;
443 parent = parent_mem_cgroup(memcg);
445 parent = root_mem_cgroup;
447 /* Prevent from concurrent shrinker_info expand */
448 mutex_lock(&shrinker_mutex);
450 child_info = shrinker_info_protected(memcg, nid);
451 parent_info = shrinker_info_protected(parent, nid);
452 for (i = 0; i < child_info->map_nr_max; i++) {
453 nr = atomic_long_read(&child_info->nr_deferred[i]);
454 atomic_long_add(nr, &parent_info->nr_deferred[i]);
457 mutex_unlock(&shrinker_mutex);
460 static bool cgroup_reclaim(struct scan_control *sc)
462 return sc->target_mem_cgroup;
465 static bool global_reclaim(struct scan_control *sc)
467 return !sc->target_mem_cgroup || mem_cgroup_is_root(sc->target_mem_cgroup);
471 * writeback_throttling_sane - is the usual dirty throttling mechanism available?
472 * @sc: scan_control in question
474 * The normal page dirty throttling mechanism in balance_dirty_pages() is
475 * completely broken with the legacy memcg and direct stalling in
476 * shrink_folio_list() is used for throttling instead, which lacks all the
477 * niceties such as fairness, adaptive pausing, bandwidth proportional
478 * allocation and configurability.
480 * This function tests whether the vmscan currently in progress can assume
481 * that the normal dirty throttling mechanism is operational.
483 static bool writeback_throttling_sane(struct scan_control *sc)
485 if (!cgroup_reclaim(sc))
487 #ifdef CONFIG_CGROUP_WRITEBACK
488 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
494 static int prealloc_memcg_shrinker(struct shrinker *shrinker)
499 static void unregister_memcg_shrinker(struct shrinker *shrinker)
503 static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker,
504 struct mem_cgroup *memcg)
509 static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker,
510 struct mem_cgroup *memcg)
515 static bool cgroup_reclaim(struct scan_control *sc)
520 static bool global_reclaim(struct scan_control *sc)
525 static bool writeback_throttling_sane(struct scan_control *sc)
531 static long xchg_nr_deferred(struct shrinker *shrinker,
532 struct shrink_control *sc)
536 if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
540 (shrinker->flags & SHRINKER_MEMCG_AWARE))
541 return xchg_nr_deferred_memcg(nid, shrinker,
544 return atomic_long_xchg(&shrinker->nr_deferred[nid], 0);
548 static long add_nr_deferred(long nr, struct shrinker *shrinker,
549 struct shrink_control *sc)
553 if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
557 (shrinker->flags & SHRINKER_MEMCG_AWARE))
558 return add_nr_deferred_memcg(nr, nid, shrinker,
561 return atomic_long_add_return(nr, &shrinker->nr_deferred[nid]);
564 static bool can_demote(int nid, struct scan_control *sc)
566 if (!numa_demotion_enabled)
568 if (sc && sc->no_demotion)
570 if (next_demotion_node(nid) == NUMA_NO_NODE)
576 static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
578 struct scan_control *sc)
582 * For non-memcg reclaim, is there
583 * space in any swap device?
585 if (get_nr_swap_pages() > 0)
588 /* Is the memcg below its swap limit? */
589 if (mem_cgroup_get_nr_swap_pages(memcg) > 0)
594 * The page can not be swapped.
596 * Can it be reclaimed from this node via demotion?
598 return can_demote(nid, sc);
602 * This misses isolated folios which are not accounted for to save counters.
603 * As the data only determines if reclaim or compaction continues, it is
604 * not expected that isolated folios will be a dominating factor.
606 unsigned long zone_reclaimable_pages(struct zone *zone)
610 nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) +
611 zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE);
612 if (can_reclaim_anon_pages(NULL, zone_to_nid(zone), NULL))
613 nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) +
614 zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON);
620 * lruvec_lru_size - Returns the number of pages on the given LRU list.
621 * @lruvec: lru vector
623 * @zone_idx: zones to consider (use MAX_NR_ZONES - 1 for the whole LRU list)
625 static unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru,
628 unsigned long size = 0;
631 for (zid = 0; zid <= zone_idx; zid++) {
632 struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid];
634 if (!managed_zone(zone))
637 if (!mem_cgroup_disabled())
638 size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid);
640 size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru);
646 * Add a shrinker callback to be called from the vm.
648 static int __prealloc_shrinker(struct shrinker *shrinker)
653 if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
654 err = prealloc_memcg_shrinker(shrinker);
658 shrinker->flags &= ~SHRINKER_MEMCG_AWARE;
661 size = sizeof(*shrinker->nr_deferred);
662 if (shrinker->flags & SHRINKER_NUMA_AWARE)
665 shrinker->nr_deferred = kzalloc(size, GFP_KERNEL);
666 if (!shrinker->nr_deferred)
672 #ifdef CONFIG_SHRINKER_DEBUG
673 int prealloc_shrinker(struct shrinker *shrinker, const char *fmt, ...)
679 shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap);
684 err = __prealloc_shrinker(shrinker);
686 kfree_const(shrinker->name);
687 shrinker->name = NULL;
693 int prealloc_shrinker(struct shrinker *shrinker, const char *fmt, ...)
695 return __prealloc_shrinker(shrinker);
699 void free_prealloced_shrinker(struct shrinker *shrinker)
701 #ifdef CONFIG_SHRINKER_DEBUG
702 kfree_const(shrinker->name);
703 shrinker->name = NULL;
705 if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
706 mutex_lock(&shrinker_mutex);
707 unregister_memcg_shrinker(shrinker);
708 mutex_unlock(&shrinker_mutex);
712 kfree(shrinker->nr_deferred);
713 shrinker->nr_deferred = NULL;
716 void register_shrinker_prepared(struct shrinker *shrinker)
718 mutex_lock(&shrinker_mutex);
719 list_add_tail_rcu(&shrinker->list, &shrinker_list);
720 shrinker->flags |= SHRINKER_REGISTERED;
721 shrinker_debugfs_add(shrinker);
722 mutex_unlock(&shrinker_mutex);
725 static int __register_shrinker(struct shrinker *shrinker)
727 int err = __prealloc_shrinker(shrinker);
731 register_shrinker_prepared(shrinker);
735 #ifdef CONFIG_SHRINKER_DEBUG
736 int register_shrinker(struct shrinker *shrinker, const char *fmt, ...)
742 shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap);
747 err = __register_shrinker(shrinker);
749 kfree_const(shrinker->name);
750 shrinker->name = NULL;
755 int register_shrinker(struct shrinker *shrinker, const char *fmt, ...)
757 return __register_shrinker(shrinker);
760 EXPORT_SYMBOL(register_shrinker);
765 void unregister_shrinker(struct shrinker *shrinker)
767 struct dentry *debugfs_entry;
769 if (!(shrinker->flags & SHRINKER_REGISTERED))
772 mutex_lock(&shrinker_mutex);
773 list_del_rcu(&shrinker->list);
774 shrinker->flags &= ~SHRINKER_REGISTERED;
775 if (shrinker->flags & SHRINKER_MEMCG_AWARE)
776 unregister_memcg_shrinker(shrinker);
777 debugfs_entry = shrinker_debugfs_remove(shrinker);
778 mutex_unlock(&shrinker_mutex);
780 atomic_inc(&shrinker_srcu_generation);
781 synchronize_srcu(&shrinker_srcu);
783 debugfs_remove_recursive(debugfs_entry);
785 kfree(shrinker->nr_deferred);
786 shrinker->nr_deferred = NULL;
788 EXPORT_SYMBOL(unregister_shrinker);
791 * synchronize_shrinkers - Wait for all running shrinkers to complete.
793 * This is useful to guarantee that all shrinker invocations have seen an
794 * update, before freeing memory.
796 void synchronize_shrinkers(void)
798 atomic_inc(&shrinker_srcu_generation);
799 synchronize_srcu(&shrinker_srcu);
801 EXPORT_SYMBOL(synchronize_shrinkers);
803 #define SHRINK_BATCH 128
805 static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
806 struct shrinker *shrinker, int priority)
808 unsigned long freed = 0;
809 unsigned long long delta;
814 long batch_size = shrinker->batch ? shrinker->batch
816 long scanned = 0, next_deferred;
818 freeable = shrinker->count_objects(shrinker, shrinkctl);
819 if (freeable == 0 || freeable == SHRINK_EMPTY)
823 * copy the current shrinker scan count into a local variable
824 * and zero it so that other concurrent shrinker invocations
825 * don't also do this scanning work.
827 nr = xchg_nr_deferred(shrinker, shrinkctl);
829 if (shrinker->seeks) {
830 delta = freeable >> priority;
832 do_div(delta, shrinker->seeks);
835 * These objects don't require any IO to create. Trim
836 * them aggressively under memory pressure to keep
837 * them from causing refetches in the IO caches.
839 delta = freeable / 2;
842 total_scan = nr >> priority;
844 total_scan = min(total_scan, (2 * freeable));
846 trace_mm_shrink_slab_start(shrinker, shrinkctl, nr,
847 freeable, delta, total_scan, priority);
850 * Normally, we should not scan less than batch_size objects in one
851 * pass to avoid too frequent shrinker calls, but if the slab has less
852 * than batch_size objects in total and we are really tight on memory,
853 * we will try to reclaim all available objects, otherwise we can end
854 * up failing allocations although there are plenty of reclaimable
855 * objects spread over several slabs with usage less than the
858 * We detect the "tight on memory" situations by looking at the total
859 * number of objects we want to scan (total_scan). If it is greater
860 * than the total number of objects on slab (freeable), we must be
861 * scanning at high prio and therefore should try to reclaim as much as
864 while (total_scan >= batch_size ||
865 total_scan >= freeable) {
867 unsigned long nr_to_scan = min(batch_size, total_scan);
869 shrinkctl->nr_to_scan = nr_to_scan;
870 shrinkctl->nr_scanned = nr_to_scan;
871 ret = shrinker->scan_objects(shrinker, shrinkctl);
872 if (ret == SHRINK_STOP)
876 count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned);
877 total_scan -= shrinkctl->nr_scanned;
878 scanned += shrinkctl->nr_scanned;
884 * The deferred work is increased by any new work (delta) that wasn't
885 * done, decreased by old deferred work that was done now.
887 * And it is capped to two times of the freeable items.
889 next_deferred = max_t(long, (nr + delta - scanned), 0);
890 next_deferred = min(next_deferred, (2 * freeable));
893 * move the unused scan count back into the shrinker in a
894 * manner that handles concurrent updates.
896 new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
898 trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan);
903 static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
904 struct mem_cgroup *memcg, int priority)
906 struct shrinker_info *info;
907 unsigned long ret, freed = 0;
908 int srcu_idx, generation;
911 if (!mem_cgroup_online(memcg))
915 srcu_idx = srcu_read_lock(&shrinker_srcu);
916 info = shrinker_info_srcu(memcg, nid);
920 generation = atomic_read(&shrinker_srcu_generation);
921 for_each_set_bit_from(i, info->map, info->map_nr_max) {
922 struct shrink_control sc = {
923 .gfp_mask = gfp_mask,
927 struct shrinker *shrinker;
929 shrinker = idr_find(&shrinker_idr, i);
930 if (unlikely(!shrinker || !(shrinker->flags & SHRINKER_REGISTERED))) {
932 clear_bit(i, info->map);
936 /* Call non-slab shrinkers even though kmem is disabled */
937 if (!memcg_kmem_online() &&
938 !(shrinker->flags & SHRINKER_NONSLAB))
941 ret = do_shrink_slab(&sc, shrinker, priority);
942 if (ret == SHRINK_EMPTY) {
943 clear_bit(i, info->map);
945 * After the shrinker reported that it had no objects to
946 * free, but before we cleared the corresponding bit in
947 * the memcg shrinker map, a new object might have been
948 * added. To make sure, we have the bit set in this
949 * case, we invoke the shrinker one more time and reset
950 * the bit if it reports that it is not empty anymore.
951 * The memory barrier here pairs with the barrier in
952 * set_shrinker_bit():
954 * list_lru_add() shrink_slab_memcg()
955 * list_add_tail() clear_bit()
957 * set_bit() do_shrink_slab()
959 smp_mb__after_atomic();
960 ret = do_shrink_slab(&sc, shrinker, priority);
961 if (ret == SHRINK_EMPTY)
964 set_shrinker_bit(memcg, nid, i);
967 if (atomic_read(&shrinker_srcu_generation) != generation) {
968 srcu_read_unlock(&shrinker_srcu, srcu_idx);
974 srcu_read_unlock(&shrinker_srcu, srcu_idx);
977 #else /* CONFIG_MEMCG */
978 static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
979 struct mem_cgroup *memcg, int priority)
983 #endif /* CONFIG_MEMCG */
986 * shrink_slab - shrink slab caches
987 * @gfp_mask: allocation context
988 * @nid: node whose slab caches to target
989 * @memcg: memory cgroup whose slab caches to target
990 * @priority: the reclaim priority
992 * Call the shrink functions to age shrinkable caches.
994 * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set,
995 * unaware shrinkers will receive a node id of 0 instead.
997 * @memcg specifies the memory cgroup to target. Unaware shrinkers
998 * are called only if it is the root cgroup.
1000 * @priority is sc->priority, we take the number of objects and >> by priority
1001 * in order to get the scan target.
1003 * Returns the number of reclaimed slab objects.
1005 static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
1006 struct mem_cgroup *memcg,
1009 unsigned long ret, freed = 0;
1010 struct shrinker *shrinker;
1011 int srcu_idx, generation;
1014 * The root memcg might be allocated even though memcg is disabled
1015 * via "cgroup_disable=memory" boot parameter. This could make
1016 * mem_cgroup_is_root() return false, then just run memcg slab
1017 * shrink, but skip global shrink. This may result in premature
1020 if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg))
1021 return shrink_slab_memcg(gfp_mask, nid, memcg, priority);
1023 srcu_idx = srcu_read_lock(&shrinker_srcu);
1025 generation = atomic_read(&shrinker_srcu_generation);
1026 list_for_each_entry_srcu(shrinker, &shrinker_list, list,
1027 srcu_read_lock_held(&shrinker_srcu)) {
1028 struct shrink_control sc = {
1029 .gfp_mask = gfp_mask,
1034 ret = do_shrink_slab(&sc, shrinker, priority);
1035 if (ret == SHRINK_EMPTY)
1039 if (atomic_read(&shrinker_srcu_generation) != generation) {
1040 freed = freed ? : 1;
1045 srcu_read_unlock(&shrinker_srcu, srcu_idx);
1050 static unsigned long drop_slab_node(int nid)
1052 unsigned long freed = 0;
1053 struct mem_cgroup *memcg = NULL;
1055 memcg = mem_cgroup_iter(NULL, NULL, NULL);
1057 freed += shrink_slab(GFP_KERNEL, nid, memcg, 0);
1058 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
1063 void drop_slab(void)
1067 unsigned long freed;
1071 for_each_online_node(nid) {
1072 if (fatal_signal_pending(current))
1075 freed += drop_slab_node(nid);
1077 } while ((freed >> shift++) > 1);
1080 static int reclaimer_offset(void)
1082 BUILD_BUG_ON(PGSTEAL_DIRECT - PGSTEAL_KSWAPD !=
1083 PGDEMOTE_DIRECT - PGDEMOTE_KSWAPD);
1084 BUILD_BUG_ON(PGSTEAL_DIRECT - PGSTEAL_KSWAPD !=
1085 PGSCAN_DIRECT - PGSCAN_KSWAPD);
1086 BUILD_BUG_ON(PGSTEAL_KHUGEPAGED - PGSTEAL_KSWAPD !=
1087 PGDEMOTE_KHUGEPAGED - PGDEMOTE_KSWAPD);
1088 BUILD_BUG_ON(PGSTEAL_KHUGEPAGED - PGSTEAL_KSWAPD !=
1089 PGSCAN_KHUGEPAGED - PGSCAN_KSWAPD);
1091 if (current_is_kswapd())
1093 if (current_is_khugepaged())
1094 return PGSTEAL_KHUGEPAGED - PGSTEAL_KSWAPD;
1095 return PGSTEAL_DIRECT - PGSTEAL_KSWAPD;
1098 static inline int is_page_cache_freeable(struct folio *folio)
1101 * A freeable page cache folio is referenced only by the caller
1102 * that isolated the folio, the page cache and optional filesystem
1103 * private data at folio->private.
1105 return folio_ref_count(folio) - folio_test_private(folio) ==
1106 1 + folio_nr_pages(folio);
1110 * We detected a synchronous write error writing a folio out. Probably
1111 * -ENOSPC. We need to propagate that into the address_space for a subsequent
1112 * fsync(), msync() or close().
1114 * The tricky part is that after writepage we cannot touch the mapping: nothing
1115 * prevents it from being freed up. But we have a ref on the folio and once
1116 * that folio is locked, the mapping is pinned.
1118 * We're allowed to run sleeping folio_lock() here because we know the caller has
1121 static void handle_write_error(struct address_space *mapping,
1122 struct folio *folio, int error)
1125 if (folio_mapping(folio) == mapping)
1126 mapping_set_error(mapping, error);
1127 folio_unlock(folio);
1130 static bool skip_throttle_noprogress(pg_data_t *pgdat)
1132 int reclaimable = 0, write_pending = 0;
1136 * If kswapd is disabled, reschedule if necessary but do not
1137 * throttle as the system is likely near OOM.
1139 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
1143 * If there are a lot of dirty/writeback folios then do not
1144 * throttle as throttling will occur when the folios cycle
1145 * towards the end of the LRU if still under writeback.
1147 for (i = 0; i < MAX_NR_ZONES; i++) {
1148 struct zone *zone = pgdat->node_zones + i;
1150 if (!managed_zone(zone))
1153 reclaimable += zone_reclaimable_pages(zone);
1154 write_pending += zone_page_state_snapshot(zone,
1155 NR_ZONE_WRITE_PENDING);
1157 if (2 * write_pending <= reclaimable)
1163 void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason)
1165 wait_queue_head_t *wqh = &pgdat->reclaim_wait[reason];
1170 * Do not throttle IO workers, kthreads other than kswapd or
1171 * workqueues. They may be required for reclaim to make
1172 * forward progress (e.g. journalling workqueues or kthreads).
1174 if (!current_is_kswapd() &&
1175 current->flags & (PF_IO_WORKER|PF_KTHREAD)) {
1181 * These figures are pulled out of thin air.
1182 * VMSCAN_THROTTLE_ISOLATED is a transient condition based on too many
1183 * parallel reclaimers which is a short-lived event so the timeout is
1184 * short. Failing to make progress or waiting on writeback are
1185 * potentially long-lived events so use a longer timeout. This is shaky
1186 * logic as a failure to make progress could be due to anything from
1187 * writeback to a slow device to excessive referenced folios at the tail
1188 * of the inactive LRU.
1191 case VMSCAN_THROTTLE_WRITEBACK:
1194 if (atomic_inc_return(&pgdat->nr_writeback_throttled) == 1) {
1195 WRITE_ONCE(pgdat->nr_reclaim_start,
1196 node_page_state(pgdat, NR_THROTTLED_WRITTEN));
1200 case VMSCAN_THROTTLE_CONGESTED:
1202 case VMSCAN_THROTTLE_NOPROGRESS:
1203 if (skip_throttle_noprogress(pgdat)) {
1211 case VMSCAN_THROTTLE_ISOLATED:
1220 prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
1221 ret = schedule_timeout(timeout);
1222 finish_wait(wqh, &wait);
1224 if (reason == VMSCAN_THROTTLE_WRITEBACK)
1225 atomic_dec(&pgdat->nr_writeback_throttled);
1227 trace_mm_vmscan_throttled(pgdat->node_id, jiffies_to_usecs(timeout),
1228 jiffies_to_usecs(timeout - ret),
1233 * Account for folios written if tasks are throttled waiting on dirty
1234 * folios to clean. If enough folios have been cleaned since throttling
1235 * started then wakeup the throttled tasks.
1237 void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio,
1240 unsigned long nr_written;
1242 node_stat_add_folio(folio, NR_THROTTLED_WRITTEN);
1245 * This is an inaccurate read as the per-cpu deltas may not
1246 * be synchronised. However, given that the system is
1247 * writeback throttled, it is not worth taking the penalty
1248 * of getting an accurate count. At worst, the throttle
1249 * timeout guarantees forward progress.
1251 nr_written = node_page_state(pgdat, NR_THROTTLED_WRITTEN) -
1252 READ_ONCE(pgdat->nr_reclaim_start);
1254 if (nr_written > SWAP_CLUSTER_MAX * nr_throttled)
1255 wake_up(&pgdat->reclaim_wait[VMSCAN_THROTTLE_WRITEBACK]);
1258 /* possible outcome of pageout() */
1260 /* failed to write folio out, folio is locked */
1262 /* move folio to the active list, folio is locked */
1264 /* folio has been sent to the disk successfully, folio is unlocked */
1266 /* folio is clean and locked */
1271 * pageout is called by shrink_folio_list() for each dirty folio.
1272 * Calls ->writepage().
1274 static pageout_t pageout(struct folio *folio, struct address_space *mapping,
1275 struct swap_iocb **plug)
1278 * If the folio is dirty, only perform writeback if that write
1279 * will be non-blocking. To prevent this allocation from being
1280 * stalled by pagecache activity. But note that there may be
1281 * stalls if we need to run get_block(). We could test
1282 * PagePrivate for that.
1284 * If this process is currently in __generic_file_write_iter() against
1285 * this folio's queue, we can perform writeback even if that
1288 * If the folio is swapcache, write it back even if that would
1289 * block, for some throttling. This happens by accident, because
1290 * swap_backing_dev_info is bust: it doesn't reflect the
1291 * congestion state of the swapdevs. Easy to fix, if needed.
1293 if (!is_page_cache_freeable(folio))
1297 * Some data journaling orphaned folios can have
1298 * folio->mapping == NULL while being dirty with clean buffers.
1300 if (folio_test_private(folio)) {
1301 if (try_to_free_buffers(folio)) {
1302 folio_clear_dirty(folio);
1303 pr_info("%s: orphaned folio\n", __func__);
1309 if (mapping->a_ops->writepage == NULL)
1310 return PAGE_ACTIVATE;
1312 if (folio_clear_dirty_for_io(folio)) {
1314 struct writeback_control wbc = {
1315 .sync_mode = WB_SYNC_NONE,
1316 .nr_to_write = SWAP_CLUSTER_MAX,
1318 .range_end = LLONG_MAX,
1323 folio_set_reclaim(folio);
1324 res = mapping->a_ops->writepage(&folio->page, &wbc);
1326 handle_write_error(mapping, folio, res);
1327 if (res == AOP_WRITEPAGE_ACTIVATE) {
1328 folio_clear_reclaim(folio);
1329 return PAGE_ACTIVATE;
1332 if (!folio_test_writeback(folio)) {
1333 /* synchronous write or broken a_ops? */
1334 folio_clear_reclaim(folio);
1336 trace_mm_vmscan_write_folio(folio);
1337 node_stat_add_folio(folio, NR_VMSCAN_WRITE);
1338 return PAGE_SUCCESS;
1345 * Same as remove_mapping, but if the folio is removed from the mapping, it
1346 * gets returned with a refcount of 0.
1348 static int __remove_mapping(struct address_space *mapping, struct folio *folio,
1349 bool reclaimed, struct mem_cgroup *target_memcg)
1352 void *shadow = NULL;
1354 BUG_ON(!folio_test_locked(folio));
1355 BUG_ON(mapping != folio_mapping(folio));
1357 if (!folio_test_swapcache(folio))
1358 spin_lock(&mapping->host->i_lock);
1359 xa_lock_irq(&mapping->i_pages);
1361 * The non racy check for a busy folio.
1363 * Must be careful with the order of the tests. When someone has
1364 * a ref to the folio, it may be possible that they dirty it then
1365 * drop the reference. So if the dirty flag is tested before the
1366 * refcount here, then the following race may occur:
1368 * get_user_pages(&page);
1369 * [user mapping goes away]
1371 * !folio_test_dirty(folio) [good]
1372 * folio_set_dirty(folio);
1374 * !refcount(folio) [good, discard it]
1376 * [oops, our write_to data is lost]
1378 * Reversing the order of the tests ensures such a situation cannot
1379 * escape unnoticed. The smp_rmb is needed to ensure the folio->flags
1380 * load is not satisfied before that of folio->_refcount.
1382 * Note that if the dirty flag is always set via folio_mark_dirty,
1383 * and thus under the i_pages lock, then this ordering is not required.
1385 refcount = 1 + folio_nr_pages(folio);
1386 if (!folio_ref_freeze(folio, refcount))
1388 /* note: atomic_cmpxchg in folio_ref_freeze provides the smp_rmb */
1389 if (unlikely(folio_test_dirty(folio))) {
1390 folio_ref_unfreeze(folio, refcount);
1394 if (folio_test_swapcache(folio)) {
1395 swp_entry_t swap = folio_swap_entry(folio);
1397 if (reclaimed && !mapping_exiting(mapping))
1398 shadow = workingset_eviction(folio, target_memcg);
1399 __delete_from_swap_cache(folio, swap, shadow);
1400 mem_cgroup_swapout(folio, swap);
1401 xa_unlock_irq(&mapping->i_pages);
1402 put_swap_folio(folio, swap);
1404 void (*free_folio)(struct folio *);
1406 free_folio = mapping->a_ops->free_folio;
1408 * Remember a shadow entry for reclaimed file cache in
1409 * order to detect refaults, thus thrashing, later on.
1411 * But don't store shadows in an address space that is
1412 * already exiting. This is not just an optimization,
1413 * inode reclaim needs to empty out the radix tree or
1414 * the nodes are lost. Don't plant shadows behind its
1417 * We also don't store shadows for DAX mappings because the
1418 * only page cache folios found in these are zero pages
1419 * covering holes, and because we don't want to mix DAX
1420 * exceptional entries and shadow exceptional entries in the
1421 * same address_space.
1423 if (reclaimed && folio_is_file_lru(folio) &&
1424 !mapping_exiting(mapping) && !dax_mapping(mapping))
1425 shadow = workingset_eviction(folio, target_memcg);
1426 __filemap_remove_folio(folio, shadow);
1427 xa_unlock_irq(&mapping->i_pages);
1428 if (mapping_shrinkable(mapping))
1429 inode_add_lru(mapping->host);
1430 spin_unlock(&mapping->host->i_lock);
1439 xa_unlock_irq(&mapping->i_pages);
1440 if (!folio_test_swapcache(folio))
1441 spin_unlock(&mapping->host->i_lock);
1446 * remove_mapping() - Attempt to remove a folio from its mapping.
1447 * @mapping: The address space.
1448 * @folio: The folio to remove.
1450 * If the folio is dirty, under writeback or if someone else has a ref
1451 * on it, removal will fail.
1452 * Return: The number of pages removed from the mapping. 0 if the folio
1453 * could not be removed.
1454 * Context: The caller should have a single refcount on the folio and
1457 long remove_mapping(struct address_space *mapping, struct folio *folio)
1459 if (__remove_mapping(mapping, folio, false, NULL)) {
1461 * Unfreezing the refcount with 1 effectively
1462 * drops the pagecache ref for us without requiring another
1465 folio_ref_unfreeze(folio, 1);
1466 return folio_nr_pages(folio);
1472 * folio_putback_lru - Put previously isolated folio onto appropriate LRU list.
1473 * @folio: Folio to be returned to an LRU list.
1475 * Add previously isolated @folio to appropriate LRU list.
1476 * The folio may still be unevictable for other reasons.
1478 * Context: lru_lock must not be held, interrupts must be enabled.
1480 void folio_putback_lru(struct folio *folio)
1482 folio_add_lru(folio);
1483 folio_put(folio); /* drop ref from isolate */
1486 enum folio_references {
1488 FOLIOREF_RECLAIM_CLEAN,
1493 static enum folio_references folio_check_references(struct folio *folio,
1494 struct scan_control *sc)
1496 int referenced_ptes, referenced_folio;
1497 unsigned long vm_flags;
1499 referenced_ptes = folio_referenced(folio, 1, sc->target_mem_cgroup,
1501 referenced_folio = folio_test_clear_referenced(folio);
1504 * The supposedly reclaimable folio was found to be in a VM_LOCKED vma.
1505 * Let the folio, now marked Mlocked, be moved to the unevictable list.
1507 if (vm_flags & VM_LOCKED)
1508 return FOLIOREF_ACTIVATE;
1510 /* rmap lock contention: rotate */
1511 if (referenced_ptes == -1)
1512 return FOLIOREF_KEEP;
1514 if (referenced_ptes) {
1516 * All mapped folios start out with page table
1517 * references from the instantiating fault, so we need
1518 * to look twice if a mapped file/anon folio is used more
1521 * Mark it and spare it for another trip around the
1522 * inactive list. Another page table reference will
1523 * lead to its activation.
1525 * Note: the mark is set for activated folios as well
1526 * so that recently deactivated but used folios are
1527 * quickly recovered.
1529 folio_set_referenced(folio);
1531 if (referenced_folio || referenced_ptes > 1)
1532 return FOLIOREF_ACTIVATE;
1535 * Activate file-backed executable folios after first usage.
1537 if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio))
1538 return FOLIOREF_ACTIVATE;
1540 return FOLIOREF_KEEP;
1543 /* Reclaim if clean, defer dirty folios to writeback */
1544 if (referenced_folio && folio_is_file_lru(folio))
1545 return FOLIOREF_RECLAIM_CLEAN;
1547 return FOLIOREF_RECLAIM;
1550 /* Check if a folio is dirty or under writeback */
1551 static void folio_check_dirty_writeback(struct folio *folio,
1552 bool *dirty, bool *writeback)
1554 struct address_space *mapping;
1557 * Anonymous folios are not handled by flushers and must be written
1558 * from reclaim context. Do not stall reclaim based on them.
1559 * MADV_FREE anonymous folios are put into inactive file list too.
1560 * They could be mistakenly treated as file lru. So further anon
1563 if (!folio_is_file_lru(folio) ||
1564 (folio_test_anon(folio) && !folio_test_swapbacked(folio))) {
1570 /* By default assume that the folio flags are accurate */
1571 *dirty = folio_test_dirty(folio);
1572 *writeback = folio_test_writeback(folio);
1574 /* Verify dirty/writeback state if the filesystem supports it */
1575 if (!folio_test_private(folio))
1578 mapping = folio_mapping(folio);
1579 if (mapping && mapping->a_ops->is_dirty_writeback)
1580 mapping->a_ops->is_dirty_writeback(folio, dirty, writeback);
1583 static struct page *alloc_demote_page(struct page *page, unsigned long private)
1585 struct page *target_page;
1586 nodemask_t *allowed_mask;
1587 struct migration_target_control *mtc;
1589 mtc = (struct migration_target_control *)private;
1591 allowed_mask = mtc->nmask;
1593 * make sure we allocate from the target node first also trying to
1594 * demote or reclaim pages from the target node via kswapd if we are
1595 * low on free memory on target node. If we don't do this and if
1596 * we have free memory on the slower(lower) memtier, we would start
1597 * allocating pages from slower(lower) memory tiers without even forcing
1598 * a demotion of cold pages from the target memtier. This can result
1599 * in the kernel placing hot pages in slower(lower) memory tiers.
1602 mtc->gfp_mask |= __GFP_THISNODE;
1603 target_page = alloc_migration_target(page, (unsigned long)mtc);
1607 mtc->gfp_mask &= ~__GFP_THISNODE;
1608 mtc->nmask = allowed_mask;
1610 return alloc_migration_target(page, (unsigned long)mtc);
1614 * Take folios on @demote_folios and attempt to demote them to another node.
1615 * Folios which are not demoted are left on @demote_folios.
1617 static unsigned int demote_folio_list(struct list_head *demote_folios,
1618 struct pglist_data *pgdat)
1620 int target_nid = next_demotion_node(pgdat->node_id);
1621 unsigned int nr_succeeded;
1622 nodemask_t allowed_mask;
1624 struct migration_target_control mtc = {
1626 * Allocate from 'node', or fail quickly and quietly.
1627 * When this happens, 'page' will likely just be discarded
1628 * instead of migrated.
1630 .gfp_mask = (GFP_HIGHUSER_MOVABLE & ~__GFP_RECLAIM) | __GFP_NOWARN |
1631 __GFP_NOMEMALLOC | GFP_NOWAIT,
1633 .nmask = &allowed_mask
1636 if (list_empty(demote_folios))
1639 if (target_nid == NUMA_NO_NODE)
1642 node_get_allowed_targets(pgdat, &allowed_mask);
1644 /* Demotion ignores all cpuset and mempolicy settings */
1645 migrate_pages(demote_folios, alloc_demote_page, NULL,
1646 (unsigned long)&mtc, MIGRATE_ASYNC, MR_DEMOTION,
1649 __count_vm_events(PGDEMOTE_KSWAPD + reclaimer_offset(), nr_succeeded);
1651 return nr_succeeded;
1654 static bool may_enter_fs(struct folio *folio, gfp_t gfp_mask)
1656 if (gfp_mask & __GFP_FS)
1658 if (!folio_test_swapcache(folio) || !(gfp_mask & __GFP_IO))
1661 * We can "enter_fs" for swap-cache with only __GFP_IO
1662 * providing this isn't SWP_FS_OPS.
1663 * ->flags can be updated non-atomicially (scan_swap_map_slots),
1664 * but that will never affect SWP_FS_OPS, so the data_race
1667 return !data_race(folio_swap_flags(folio) & SWP_FS_OPS);
1671 * shrink_folio_list() returns the number of reclaimed pages
1673 static unsigned int shrink_folio_list(struct list_head *folio_list,
1674 struct pglist_data *pgdat, struct scan_control *sc,
1675 struct reclaim_stat *stat, bool ignore_references)
1677 LIST_HEAD(ret_folios);
1678 LIST_HEAD(free_folios);
1679 LIST_HEAD(demote_folios);
1680 unsigned int nr_reclaimed = 0;
1681 unsigned int pgactivate = 0;
1682 bool do_demote_pass;
1683 struct swap_iocb *plug = NULL;
1685 memset(stat, 0, sizeof(*stat));
1687 do_demote_pass = can_demote(pgdat->node_id, sc);
1690 while (!list_empty(folio_list)) {
1691 struct address_space *mapping;
1692 struct folio *folio;
1693 enum folio_references references = FOLIOREF_RECLAIM;
1694 bool dirty, writeback;
1695 unsigned int nr_pages;
1699 folio = lru_to_folio(folio_list);
1700 list_del(&folio->lru);
1702 if (!folio_trylock(folio))
1705 VM_BUG_ON_FOLIO(folio_test_active(folio), folio);
1707 nr_pages = folio_nr_pages(folio);
1709 /* Account the number of base pages */
1710 sc->nr_scanned += nr_pages;
1712 if (unlikely(!folio_evictable(folio)))
1713 goto activate_locked;
1715 if (!sc->may_unmap && folio_mapped(folio))
1718 /* folio_update_gen() tried to promote this page? */
1719 if (lru_gen_enabled() && !ignore_references &&
1720 folio_mapped(folio) && folio_test_referenced(folio))
1724 * The number of dirty pages determines if a node is marked
1725 * reclaim_congested. kswapd will stall and start writing
1726 * folios if the tail of the LRU is all dirty unqueued folios.
1728 folio_check_dirty_writeback(folio, &dirty, &writeback);
1729 if (dirty || writeback)
1730 stat->nr_dirty += nr_pages;
1732 if (dirty && !writeback)
1733 stat->nr_unqueued_dirty += nr_pages;
1736 * Treat this folio as congested if folios are cycling
1737 * through the LRU so quickly that the folios marked
1738 * for immediate reclaim are making it to the end of
1739 * the LRU a second time.
1741 if (writeback && folio_test_reclaim(folio))
1742 stat->nr_congested += nr_pages;
1745 * If a folio at the tail of the LRU is under writeback, there
1746 * are three cases to consider.
1748 * 1) If reclaim is encountering an excessive number
1749 * of folios under writeback and this folio has both
1750 * the writeback and reclaim flags set, then it
1751 * indicates that folios are being queued for I/O but
1752 * are being recycled through the LRU before the I/O
1753 * can complete. Waiting on the folio itself risks an
1754 * indefinite stall if it is impossible to writeback
1755 * the folio due to I/O error or disconnected storage
1756 * so instead note that the LRU is being scanned too
1757 * quickly and the caller can stall after the folio
1758 * list has been processed.
1760 * 2) Global or new memcg reclaim encounters a folio that is
1761 * not marked for immediate reclaim, or the caller does not
1762 * have __GFP_FS (or __GFP_IO if it's simply going to swap,
1763 * not to fs). In this case mark the folio for immediate
1764 * reclaim and continue scanning.
1766 * Require may_enter_fs() because we would wait on fs, which
1767 * may not have submitted I/O yet. And the loop driver might
1768 * enter reclaim, and deadlock if it waits on a folio for
1769 * which it is needed to do the write (loop masks off
1770 * __GFP_IO|__GFP_FS for this reason); but more thought
1771 * would probably show more reasons.
1773 * 3) Legacy memcg encounters a folio that already has the
1774 * reclaim flag set. memcg does not have any dirty folio
1775 * throttling so we could easily OOM just because too many
1776 * folios are in writeback and there is nothing else to
1777 * reclaim. Wait for the writeback to complete.
1779 * In cases 1) and 2) we activate the folios to get them out of
1780 * the way while we continue scanning for clean folios on the
1781 * inactive list and refilling from the active list. The
1782 * observation here is that waiting for disk writes is more
1783 * expensive than potentially causing reloads down the line.
1784 * Since they're marked for immediate reclaim, they won't put
1785 * memory pressure on the cache working set any longer than it
1786 * takes to write them to disk.
1788 if (folio_test_writeback(folio)) {
1790 if (current_is_kswapd() &&
1791 folio_test_reclaim(folio) &&
1792 test_bit(PGDAT_WRITEBACK, &pgdat->flags)) {
1793 stat->nr_immediate += nr_pages;
1794 goto activate_locked;
1797 } else if (writeback_throttling_sane(sc) ||
1798 !folio_test_reclaim(folio) ||
1799 !may_enter_fs(folio, sc->gfp_mask)) {
1801 * This is slightly racy -
1802 * folio_end_writeback() might have
1803 * just cleared the reclaim flag, then
1804 * setting the reclaim flag here ends up
1805 * interpreted as the readahead flag - but
1806 * that does not matter enough to care.
1807 * What we do want is for this folio to
1808 * have the reclaim flag set next time
1809 * memcg reclaim reaches the tests above,
1810 * so it will then wait for writeback to
1811 * avoid OOM; and it's also appropriate
1812 * in global reclaim.
1814 folio_set_reclaim(folio);
1815 stat->nr_writeback += nr_pages;
1816 goto activate_locked;
1820 folio_unlock(folio);
1821 folio_wait_writeback(folio);
1822 /* then go back and try same folio again */
1823 list_add_tail(&folio->lru, folio_list);
1828 if (!ignore_references)
1829 references = folio_check_references(folio, sc);
1831 switch (references) {
1832 case FOLIOREF_ACTIVATE:
1833 goto activate_locked;
1835 stat->nr_ref_keep += nr_pages;
1837 case FOLIOREF_RECLAIM:
1838 case FOLIOREF_RECLAIM_CLEAN:
1839 ; /* try to reclaim the folio below */
1843 * Before reclaiming the folio, try to relocate
1844 * its contents to another node.
1846 if (do_demote_pass &&
1847 (thp_migration_supported() || !folio_test_large(folio))) {
1848 list_add(&folio->lru, &demote_folios);
1849 folio_unlock(folio);
1854 * Anonymous process memory has backing store?
1855 * Try to allocate it some swap space here.
1856 * Lazyfree folio could be freed directly
1858 if (folio_test_anon(folio) && folio_test_swapbacked(folio)) {
1859 if (!folio_test_swapcache(folio)) {
1860 if (!(sc->gfp_mask & __GFP_IO))
1862 if (folio_maybe_dma_pinned(folio))
1864 if (folio_test_large(folio)) {
1865 /* cannot split folio, skip it */
1866 if (!can_split_folio(folio, NULL))
1867 goto activate_locked;
1869 * Split folios without a PMD map right
1870 * away. Chances are some or all of the
1871 * tail pages can be freed without IO.
1873 if (!folio_entire_mapcount(folio) &&
1874 split_folio_to_list(folio,
1876 goto activate_locked;
1878 if (!add_to_swap(folio)) {
1879 if (!folio_test_large(folio))
1880 goto activate_locked_split;
1881 /* Fallback to swap normal pages */
1882 if (split_folio_to_list(folio,
1884 goto activate_locked;
1885 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1886 count_vm_event(THP_SWPOUT_FALLBACK);
1888 if (!add_to_swap(folio))
1889 goto activate_locked_split;
1892 } else if (folio_test_swapbacked(folio) &&
1893 folio_test_large(folio)) {
1894 /* Split shmem folio */
1895 if (split_folio_to_list(folio, folio_list))
1900 * If the folio was split above, the tail pages will make
1901 * their own pass through this function and be accounted
1904 if ((nr_pages > 1) && !folio_test_large(folio)) {
1905 sc->nr_scanned -= (nr_pages - 1);
1910 * The folio is mapped into the page tables of one or more
1911 * processes. Try to unmap it here.
1913 if (folio_mapped(folio)) {
1914 enum ttu_flags flags = TTU_BATCH_FLUSH;
1915 bool was_swapbacked = folio_test_swapbacked(folio);
1917 if (folio_test_pmd_mappable(folio))
1918 flags |= TTU_SPLIT_HUGE_PMD;
1920 try_to_unmap(folio, flags);
1921 if (folio_mapped(folio)) {
1922 stat->nr_unmap_fail += nr_pages;
1923 if (!was_swapbacked &&
1924 folio_test_swapbacked(folio))
1925 stat->nr_lazyfree_fail += nr_pages;
1926 goto activate_locked;
1930 mapping = folio_mapping(folio);
1931 if (folio_test_dirty(folio)) {
1933 * Only kswapd can writeback filesystem folios
1934 * to avoid risk of stack overflow. But avoid
1935 * injecting inefficient single-folio I/O into
1936 * flusher writeback as much as possible: only
1937 * write folios when we've encountered many
1938 * dirty folios, and when we've already scanned
1939 * the rest of the LRU for clean folios and see
1940 * the same dirty folios again (with the reclaim
1943 if (folio_is_file_lru(folio) &&
1944 (!current_is_kswapd() ||
1945 !folio_test_reclaim(folio) ||
1946 !test_bit(PGDAT_DIRTY, &pgdat->flags))) {
1948 * Immediately reclaim when written back.
1949 * Similar in principle to folio_deactivate()
1950 * except we already have the folio isolated
1951 * and know it's dirty
1953 node_stat_mod_folio(folio, NR_VMSCAN_IMMEDIATE,
1955 folio_set_reclaim(folio);
1957 goto activate_locked;
1960 if (references == FOLIOREF_RECLAIM_CLEAN)
1962 if (!may_enter_fs(folio, sc->gfp_mask))
1964 if (!sc->may_writepage)
1968 * Folio is dirty. Flush the TLB if a writable entry
1969 * potentially exists to avoid CPU writes after I/O
1970 * starts and then write it out here.
1972 try_to_unmap_flush_dirty();
1973 switch (pageout(folio, mapping, &plug)) {
1977 goto activate_locked;
1979 stat->nr_pageout += nr_pages;
1981 if (folio_test_writeback(folio))
1983 if (folio_test_dirty(folio))
1987 * A synchronous write - probably a ramdisk. Go
1988 * ahead and try to reclaim the folio.
1990 if (!folio_trylock(folio))
1992 if (folio_test_dirty(folio) ||
1993 folio_test_writeback(folio))
1995 mapping = folio_mapping(folio);
1998 ; /* try to free the folio below */
2003 * If the folio has buffers, try to free the buffer
2004 * mappings associated with this folio. If we succeed
2005 * we try to free the folio as well.
2007 * We do this even if the folio is dirty.
2008 * filemap_release_folio() does not perform I/O, but it
2009 * is possible for a folio to have the dirty flag set,
2010 * but it is actually clean (all its buffers are clean).
2011 * This happens if the buffers were written out directly,
2012 * with submit_bh(). ext3 will do this, as well as
2013 * the blockdev mapping. filemap_release_folio() will
2014 * discover that cleanness and will drop the buffers
2015 * and mark the folio clean - it can be freed.
2017 * Rarely, folios can have buffers and no ->mapping.
2018 * These are the folios which were not successfully
2019 * invalidated in truncate_cleanup_folio(). We try to
2020 * drop those buffers here and if that worked, and the
2021 * folio is no longer mapped into process address space
2022 * (refcount == 1) it can be freed. Otherwise, leave
2023 * the folio on the LRU so it is swappable.
2025 if (folio_has_private(folio)) {
2026 if (!filemap_release_folio(folio, sc->gfp_mask))
2027 goto activate_locked;
2028 if (!mapping && folio_ref_count(folio) == 1) {
2029 folio_unlock(folio);
2030 if (folio_put_testzero(folio))
2034 * rare race with speculative reference.
2035 * the speculative reference will free
2036 * this folio shortly, so we may
2037 * increment nr_reclaimed here (and
2038 * leave it off the LRU).
2040 nr_reclaimed += nr_pages;
2046 if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) {
2047 /* follow __remove_mapping for reference */
2048 if (!folio_ref_freeze(folio, 1))
2051 * The folio has only one reference left, which is
2052 * from the isolation. After the caller puts the
2053 * folio back on the lru and drops the reference, the
2054 * folio will be freed anyway. It doesn't matter
2055 * which lru it goes on. So we don't bother checking
2056 * the dirty flag here.
2058 count_vm_events(PGLAZYFREED, nr_pages);
2059 count_memcg_folio_events(folio, PGLAZYFREED, nr_pages);
2060 } else if (!mapping || !__remove_mapping(mapping, folio, true,
2061 sc->target_mem_cgroup))
2064 folio_unlock(folio);
2067 * Folio may get swapped out as a whole, need to account
2070 nr_reclaimed += nr_pages;
2073 * Is there need to periodically free_folio_list? It would
2074 * appear not as the counts should be low
2076 if (unlikely(folio_test_large(folio)))
2077 destroy_large_folio(folio);
2079 list_add(&folio->lru, &free_folios);
2082 activate_locked_split:
2084 * The tail pages that are failed to add into swap cache
2085 * reach here. Fixup nr_scanned and nr_pages.
2088 sc->nr_scanned -= (nr_pages - 1);
2092 /* Not a candidate for swapping, so reclaim swap space. */
2093 if (folio_test_swapcache(folio) &&
2094 (mem_cgroup_swap_full(folio) || folio_test_mlocked(folio)))
2095 folio_free_swap(folio);
2096 VM_BUG_ON_FOLIO(folio_test_active(folio), folio);
2097 if (!folio_test_mlocked(folio)) {
2098 int type = folio_is_file_lru(folio);
2099 folio_set_active(folio);
2100 stat->nr_activate[type] += nr_pages;
2101 count_memcg_folio_events(folio, PGACTIVATE, nr_pages);
2104 folio_unlock(folio);
2106 list_add(&folio->lru, &ret_folios);
2107 VM_BUG_ON_FOLIO(folio_test_lru(folio) ||
2108 folio_test_unevictable(folio), folio);
2110 /* 'folio_list' is always empty here */
2112 /* Migrate folios selected for demotion */
2113 nr_reclaimed += demote_folio_list(&demote_folios, pgdat);
2114 /* Folios that could not be demoted are still in @demote_folios */
2115 if (!list_empty(&demote_folios)) {
2116 /* Folios which weren't demoted go back on @folio_list */
2117 list_splice_init(&demote_folios, folio_list);
2120 * goto retry to reclaim the undemoted folios in folio_list if
2123 * Reclaiming directly from top tier nodes is not often desired
2124 * due to it breaking the LRU ordering: in general memory
2125 * should be reclaimed from lower tier nodes and demoted from
2128 * However, disabling reclaim from top tier nodes entirely
2129 * would cause ooms in edge scenarios where lower tier memory
2130 * is unreclaimable for whatever reason, eg memory being
2131 * mlocked or too hot to reclaim. We can disable reclaim
2132 * from top tier nodes in proactive reclaim though as that is
2133 * not real memory pressure.
2135 if (!sc->proactive) {
2136 do_demote_pass = false;
2141 pgactivate = stat->nr_activate[0] + stat->nr_activate[1];
2143 mem_cgroup_uncharge_list(&free_folios);
2144 try_to_unmap_flush();
2145 free_unref_page_list(&free_folios);
2147 list_splice(&ret_folios, folio_list);
2148 count_vm_events(PGACTIVATE, pgactivate);
2151 swap_write_unplug(plug);
2152 return nr_reclaimed;
2155 unsigned int reclaim_clean_pages_from_list(struct zone *zone,
2156 struct list_head *folio_list)
2158 struct scan_control sc = {
2159 .gfp_mask = GFP_KERNEL,
2162 struct reclaim_stat stat;
2163 unsigned int nr_reclaimed;
2164 struct folio *folio, *next;
2165 LIST_HEAD(clean_folios);
2166 unsigned int noreclaim_flag;
2168 list_for_each_entry_safe(folio, next, folio_list, lru) {
2169 if (!folio_test_hugetlb(folio) && folio_is_file_lru(folio) &&
2170 !folio_test_dirty(folio) && !__folio_test_movable(folio) &&
2171 !folio_test_unevictable(folio)) {
2172 folio_clear_active(folio);
2173 list_move(&folio->lru, &clean_folios);
2178 * We should be safe here since we are only dealing with file pages and
2179 * we are not kswapd and therefore cannot write dirty file pages. But
2180 * call memalloc_noreclaim_save() anyway, just in case these conditions
2181 * change in the future.
2183 noreclaim_flag = memalloc_noreclaim_save();
2184 nr_reclaimed = shrink_folio_list(&clean_folios, zone->zone_pgdat, &sc,
2186 memalloc_noreclaim_restore(noreclaim_flag);
2188 list_splice(&clean_folios, folio_list);
2189 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
2190 -(long)nr_reclaimed);
2192 * Since lazyfree pages are isolated from file LRU from the beginning,
2193 * they will rotate back to anonymous LRU in the end if it failed to
2194 * discard so isolated count will be mismatched.
2195 * Compensate the isolated count for both LRU lists.
2197 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON,
2198 stat.nr_lazyfree_fail);
2199 mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
2200 -(long)stat.nr_lazyfree_fail);
2201 return nr_reclaimed;
2205 * Update LRU sizes after isolating pages. The LRU size updates must
2206 * be complete before mem_cgroup_update_lru_size due to a sanity check.
2208 static __always_inline void update_lru_sizes(struct lruvec *lruvec,
2209 enum lru_list lru, unsigned long *nr_zone_taken)
2213 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2214 if (!nr_zone_taken[zid])
2217 update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
2223 * Isolating page from the lruvec to fill in @dst list by nr_to_scan times.
2225 * lruvec->lru_lock is heavily contended. Some of the functions that
2226 * shrink the lists perform better by taking out a batch of pages
2227 * and working on them outside the LRU lock.
2229 * For pagecache intensive workloads, this function is the hottest
2230 * spot in the kernel (apart from copy_*_user functions).
2232 * Lru_lock must be held before calling this function.
2234 * @nr_to_scan: The number of eligible pages to look through on the list.
2235 * @lruvec: The LRU vector to pull pages from.
2236 * @dst: The temp list to put pages on to.
2237 * @nr_scanned: The number of pages that were scanned.
2238 * @sc: The scan_control struct for this reclaim session
2239 * @lru: LRU list id for isolating
2241 * returns how many pages were moved onto *@dst.
2243 static unsigned long isolate_lru_folios(unsigned long nr_to_scan,
2244 struct lruvec *lruvec, struct list_head *dst,
2245 unsigned long *nr_scanned, struct scan_control *sc,
2248 struct list_head *src = &lruvec->lists[lru];
2249 unsigned long nr_taken = 0;
2250 unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 };
2251 unsigned long nr_skipped[MAX_NR_ZONES] = { 0, };
2252 unsigned long skipped = 0;
2253 unsigned long scan, total_scan, nr_pages;
2254 LIST_HEAD(folios_skipped);
2258 while (scan < nr_to_scan && !list_empty(src)) {
2259 struct list_head *move_to = src;
2260 struct folio *folio;
2262 folio = lru_to_folio(src);
2263 prefetchw_prev_lru_folio(folio, src, flags);
2265 nr_pages = folio_nr_pages(folio);
2266 total_scan += nr_pages;
2268 if (folio_zonenum(folio) > sc->reclaim_idx) {
2269 nr_skipped[folio_zonenum(folio)] += nr_pages;
2270 move_to = &folios_skipped;
2275 * Do not count skipped folios because that makes the function
2276 * return with no isolated folios if the LRU mostly contains
2277 * ineligible folios. This causes the VM to not reclaim any
2278 * folios, triggering a premature OOM.
2279 * Account all pages in a folio.
2283 if (!folio_test_lru(folio))
2285 if (!sc->may_unmap && folio_mapped(folio))
2289 * Be careful not to clear the lru flag until after we're
2290 * sure the folio is not being freed elsewhere -- the
2291 * folio release code relies on it.
2293 if (unlikely(!folio_try_get(folio)))
2296 if (!folio_test_clear_lru(folio)) {
2297 /* Another thread is already isolating this folio */
2302 nr_taken += nr_pages;
2303 nr_zone_taken[folio_zonenum(folio)] += nr_pages;
2306 list_move(&folio->lru, move_to);
2310 * Splice any skipped folios to the start of the LRU list. Note that
2311 * this disrupts the LRU order when reclaiming for lower zones but
2312 * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX
2313 * scanning would soon rescan the same folios to skip and waste lots
2316 if (!list_empty(&folios_skipped)) {
2319 list_splice(&folios_skipped, src);
2320 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2321 if (!nr_skipped[zid])
2324 __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]);
2325 skipped += nr_skipped[zid];
2328 *nr_scanned = total_scan;
2329 trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan,
2330 total_scan, skipped, nr_taken,
2331 sc->may_unmap ? 0 : ISOLATE_UNMAPPED, lru);
2332 update_lru_sizes(lruvec, lru, nr_zone_taken);
2337 * folio_isolate_lru() - Try to isolate a folio from its LRU list.
2338 * @folio: Folio to isolate from its LRU list.
2340 * Isolate a @folio from an LRU list and adjust the vmstat statistic
2341 * corresponding to whatever LRU list the folio was on.
2343 * The folio will have its LRU flag cleared. If it was found on the
2344 * active list, it will have the Active flag set. If it was found on the
2345 * unevictable list, it will have the Unevictable flag set. These flags
2346 * may need to be cleared by the caller before letting the page go.
2350 * (1) Must be called with an elevated refcount on the folio. This is a
2351 * fundamental difference from isolate_lru_folios() (which is called
2352 * without a stable reference).
2353 * (2) The lru_lock must not be held.
2354 * (3) Interrupts must be enabled.
2356 * Return: true if the folio was removed from an LRU list.
2357 * false if the folio was not on an LRU list.
2359 bool folio_isolate_lru(struct folio *folio)
2363 VM_BUG_ON_FOLIO(!folio_ref_count(folio), folio);
2365 if (folio_test_clear_lru(folio)) {
2366 struct lruvec *lruvec;
2369 lruvec = folio_lruvec_lock_irq(folio);
2370 lruvec_del_folio(lruvec, folio);
2371 unlock_page_lruvec_irq(lruvec);
2379 * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
2380 * then get rescheduled. When there are massive number of tasks doing page
2381 * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
2382 * the LRU list will go small and be scanned faster than necessary, leading to
2383 * unnecessary swapping, thrashing and OOM.
2385 static int too_many_isolated(struct pglist_data *pgdat, int file,
2386 struct scan_control *sc)
2388 unsigned long inactive, isolated;
2391 if (current_is_kswapd())
2394 if (!writeback_throttling_sane(sc))
2398 inactive = node_page_state(pgdat, NR_INACTIVE_FILE);
2399 isolated = node_page_state(pgdat, NR_ISOLATED_FILE);
2401 inactive = node_page_state(pgdat, NR_INACTIVE_ANON);
2402 isolated = node_page_state(pgdat, NR_ISOLATED_ANON);
2406 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
2407 * won't get blocked by normal direct-reclaimers, forming a circular
2410 if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
2413 too_many = isolated > inactive;
2415 /* Wake up tasks throttled due to too_many_isolated. */
2417 wake_throttle_isolated(pgdat);
2423 * move_folios_to_lru() moves folios from private @list to appropriate LRU list.
2424 * On return, @list is reused as a list of folios to be freed by the caller.
2426 * Returns the number of pages moved to the given lruvec.
2428 static unsigned int move_folios_to_lru(struct lruvec *lruvec,
2429 struct list_head *list)
2431 int nr_pages, nr_moved = 0;
2432 LIST_HEAD(folios_to_free);
2434 while (!list_empty(list)) {
2435 struct folio *folio = lru_to_folio(list);
2437 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
2438 list_del(&folio->lru);
2439 if (unlikely(!folio_evictable(folio))) {
2440 spin_unlock_irq(&lruvec->lru_lock);
2441 folio_putback_lru(folio);
2442 spin_lock_irq(&lruvec->lru_lock);
2447 * The folio_set_lru needs to be kept here for list integrity.
2449 * #0 move_folios_to_lru #1 release_pages
2450 * if (!folio_put_testzero())
2451 * if (folio_put_testzero())
2452 * !lru //skip lru_lock
2454 * list_add(&folio->lru,)
2455 * list_add(&folio->lru,)
2457 folio_set_lru(folio);
2459 if (unlikely(folio_put_testzero(folio))) {
2460 __folio_clear_lru_flags(folio);
2462 if (unlikely(folio_test_large(folio))) {
2463 spin_unlock_irq(&lruvec->lru_lock);
2464 destroy_large_folio(folio);
2465 spin_lock_irq(&lruvec->lru_lock);
2467 list_add(&folio->lru, &folios_to_free);
2473 * All pages were isolated from the same lruvec (and isolation
2474 * inhibits memcg migration).
2476 VM_BUG_ON_FOLIO(!folio_matches_lruvec(folio, lruvec), folio);
2477 lruvec_add_folio(lruvec, folio);
2478 nr_pages = folio_nr_pages(folio);
2479 nr_moved += nr_pages;
2480 if (folio_test_active(folio))
2481 workingset_age_nonresident(lruvec, nr_pages);
2485 * To save our caller's stack, now use input list for pages to free.
2487 list_splice(&folios_to_free, list);
2493 * If a kernel thread (such as nfsd for loop-back mounts) services a backing
2494 * device by writing to the page cache it sets PF_LOCAL_THROTTLE. In this case
2495 * we should not throttle. Otherwise it is safe to do so.
2497 static int current_may_throttle(void)
2499 return !(current->flags & PF_LOCAL_THROTTLE);
2503 * shrink_inactive_list() is a helper for shrink_node(). It returns the number
2504 * of reclaimed pages
2506 static unsigned long shrink_inactive_list(unsigned long nr_to_scan,
2507 struct lruvec *lruvec, struct scan_control *sc,
2510 LIST_HEAD(folio_list);
2511 unsigned long nr_scanned;
2512 unsigned int nr_reclaimed = 0;
2513 unsigned long nr_taken;
2514 struct reclaim_stat stat;
2515 bool file = is_file_lru(lru);
2516 enum vm_event_item item;
2517 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2518 bool stalled = false;
2520 while (unlikely(too_many_isolated(pgdat, file, sc))) {
2524 /* wait a bit for the reclaimer. */
2526 reclaim_throttle(pgdat, VMSCAN_THROTTLE_ISOLATED);
2528 /* We are about to die and free our memory. Return now. */
2529 if (fatal_signal_pending(current))
2530 return SWAP_CLUSTER_MAX;
2535 spin_lock_irq(&lruvec->lru_lock);
2537 nr_taken = isolate_lru_folios(nr_to_scan, lruvec, &folio_list,
2538 &nr_scanned, sc, lru);
2540 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
2541 item = PGSCAN_KSWAPD + reclaimer_offset();
2542 if (!cgroup_reclaim(sc))
2543 __count_vm_events(item, nr_scanned);
2544 __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned);
2545 __count_vm_events(PGSCAN_ANON + file, nr_scanned);
2547 spin_unlock_irq(&lruvec->lru_lock);
2552 nr_reclaimed = shrink_folio_list(&folio_list, pgdat, sc, &stat, false);
2554 spin_lock_irq(&lruvec->lru_lock);
2555 move_folios_to_lru(lruvec, &folio_list);
2557 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
2558 item = PGSTEAL_KSWAPD + reclaimer_offset();
2559 if (!cgroup_reclaim(sc))
2560 __count_vm_events(item, nr_reclaimed);
2561 __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed);
2562 __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed);
2563 spin_unlock_irq(&lruvec->lru_lock);
2565 lru_note_cost(lruvec, file, stat.nr_pageout, nr_scanned - nr_reclaimed);
2566 mem_cgroup_uncharge_list(&folio_list);
2567 free_unref_page_list(&folio_list);
2570 * If dirty folios are scanned that are not queued for IO, it
2571 * implies that flushers are not doing their job. This can
2572 * happen when memory pressure pushes dirty folios to the end of
2573 * the LRU before the dirty limits are breached and the dirty
2574 * data has expired. It can also happen when the proportion of
2575 * dirty folios grows not through writes but through memory
2576 * pressure reclaiming all the clean cache. And in some cases,
2577 * the flushers simply cannot keep up with the allocation
2578 * rate. Nudge the flusher threads in case they are asleep.
2580 if (stat.nr_unqueued_dirty == nr_taken) {
2581 wakeup_flusher_threads(WB_REASON_VMSCAN);
2583 * For cgroupv1 dirty throttling is achieved by waking up
2584 * the kernel flusher here and later waiting on folios
2585 * which are in writeback to finish (see shrink_folio_list()).
2587 * Flusher may not be able to issue writeback quickly
2588 * enough for cgroupv1 writeback throttling to work
2589 * on a large system.
2591 if (!writeback_throttling_sane(sc))
2592 reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK);
2595 sc->nr.dirty += stat.nr_dirty;
2596 sc->nr.congested += stat.nr_congested;
2597 sc->nr.unqueued_dirty += stat.nr_unqueued_dirty;
2598 sc->nr.writeback += stat.nr_writeback;
2599 sc->nr.immediate += stat.nr_immediate;
2600 sc->nr.taken += nr_taken;
2602 sc->nr.file_taken += nr_taken;
2604 trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
2605 nr_scanned, nr_reclaimed, &stat, sc->priority, file);
2606 return nr_reclaimed;
2610 * shrink_active_list() moves folios from the active LRU to the inactive LRU.
2612 * We move them the other way if the folio is referenced by one or more
2615 * If the folios are mostly unmapped, the processing is fast and it is
2616 * appropriate to hold lru_lock across the whole operation. But if
2617 * the folios are mapped, the processing is slow (folio_referenced()), so
2618 * we should drop lru_lock around each folio. It's impossible to balance
2619 * this, so instead we remove the folios from the LRU while processing them.
2620 * It is safe to rely on the active flag against the non-LRU folios in here
2621 * because nobody will play with that bit on a non-LRU folio.
2623 * The downside is that we have to touch folio->_refcount against each folio.
2624 * But we had to alter folio->flags anyway.
2626 static void shrink_active_list(unsigned long nr_to_scan,
2627 struct lruvec *lruvec,
2628 struct scan_control *sc,
2631 unsigned long nr_taken;
2632 unsigned long nr_scanned;
2633 unsigned long vm_flags;
2634 LIST_HEAD(l_hold); /* The folios which were snipped off */
2635 LIST_HEAD(l_active);
2636 LIST_HEAD(l_inactive);
2637 unsigned nr_deactivate, nr_activate;
2638 unsigned nr_rotated = 0;
2639 int file = is_file_lru(lru);
2640 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2644 spin_lock_irq(&lruvec->lru_lock);
2646 nr_taken = isolate_lru_folios(nr_to_scan, lruvec, &l_hold,
2647 &nr_scanned, sc, lru);
2649 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
2651 if (!cgroup_reclaim(sc))
2652 __count_vm_events(PGREFILL, nr_scanned);
2653 __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned);
2655 spin_unlock_irq(&lruvec->lru_lock);
2657 while (!list_empty(&l_hold)) {
2658 struct folio *folio;
2661 folio = lru_to_folio(&l_hold);
2662 list_del(&folio->lru);
2664 if (unlikely(!folio_evictable(folio))) {
2665 folio_putback_lru(folio);
2669 if (unlikely(buffer_heads_over_limit)) {
2670 if (folio_test_private(folio) && folio_trylock(folio)) {
2671 if (folio_test_private(folio))
2672 filemap_release_folio(folio, 0);
2673 folio_unlock(folio);
2677 /* Referenced or rmap lock contention: rotate */
2678 if (folio_referenced(folio, 0, sc->target_mem_cgroup,
2681 * Identify referenced, file-backed active folios and
2682 * give them one more trip around the active list. So
2683 * that executable code get better chances to stay in
2684 * memory under moderate memory pressure. Anon folios
2685 * are not likely to be evicted by use-once streaming
2686 * IO, plus JVM can create lots of anon VM_EXEC folios,
2687 * so we ignore them here.
2689 if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio)) {
2690 nr_rotated += folio_nr_pages(folio);
2691 list_add(&folio->lru, &l_active);
2696 folio_clear_active(folio); /* we are de-activating */
2697 folio_set_workingset(folio);
2698 list_add(&folio->lru, &l_inactive);
2702 * Move folios back to the lru list.
2704 spin_lock_irq(&lruvec->lru_lock);
2706 nr_activate = move_folios_to_lru(lruvec, &l_active);
2707 nr_deactivate = move_folios_to_lru(lruvec, &l_inactive);
2708 /* Keep all free folios in l_active list */
2709 list_splice(&l_inactive, &l_active);
2711 __count_vm_events(PGDEACTIVATE, nr_deactivate);
2712 __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate);
2714 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
2715 spin_unlock_irq(&lruvec->lru_lock);
2718 lru_note_cost(lruvec, file, 0, nr_rotated);
2719 mem_cgroup_uncharge_list(&l_active);
2720 free_unref_page_list(&l_active);
2721 trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate,
2722 nr_deactivate, nr_rotated, sc->priority, file);
2725 static unsigned int reclaim_folio_list(struct list_head *folio_list,
2726 struct pglist_data *pgdat)
2728 struct reclaim_stat dummy_stat;
2729 unsigned int nr_reclaimed;
2730 struct folio *folio;
2731 struct scan_control sc = {
2732 .gfp_mask = GFP_KERNEL,
2739 nr_reclaimed = shrink_folio_list(folio_list, pgdat, &sc, &dummy_stat, false);
2740 while (!list_empty(folio_list)) {
2741 folio = lru_to_folio(folio_list);
2742 list_del(&folio->lru);
2743 folio_putback_lru(folio);
2746 return nr_reclaimed;
2749 unsigned long reclaim_pages(struct list_head *folio_list)
2752 unsigned int nr_reclaimed = 0;
2753 LIST_HEAD(node_folio_list);
2754 unsigned int noreclaim_flag;
2756 if (list_empty(folio_list))
2757 return nr_reclaimed;
2759 noreclaim_flag = memalloc_noreclaim_save();
2761 nid = folio_nid(lru_to_folio(folio_list));
2763 struct folio *folio = lru_to_folio(folio_list);
2765 if (nid == folio_nid(folio)) {
2766 folio_clear_active(folio);
2767 list_move(&folio->lru, &node_folio_list);
2771 nr_reclaimed += reclaim_folio_list(&node_folio_list, NODE_DATA(nid));
2772 nid = folio_nid(lru_to_folio(folio_list));
2773 } while (!list_empty(folio_list));
2775 nr_reclaimed += reclaim_folio_list(&node_folio_list, NODE_DATA(nid));
2777 memalloc_noreclaim_restore(noreclaim_flag);
2779 return nr_reclaimed;
2782 static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
2783 struct lruvec *lruvec, struct scan_control *sc)
2785 if (is_active_lru(lru)) {
2786 if (sc->may_deactivate & (1 << is_file_lru(lru)))
2787 shrink_active_list(nr_to_scan, lruvec, sc, lru);
2789 sc->skipped_deactivate = 1;
2793 return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
2797 * The inactive anon list should be small enough that the VM never has
2798 * to do too much work.
2800 * The inactive file list should be small enough to leave most memory
2801 * to the established workingset on the scan-resistant active list,
2802 * but large enough to avoid thrashing the aggregate readahead window.
2804 * Both inactive lists should also be large enough that each inactive
2805 * folio has a chance to be referenced again before it is reclaimed.
2807 * If that fails and refaulting is observed, the inactive list grows.
2809 * The inactive_ratio is the target ratio of ACTIVE to INACTIVE folios
2810 * on this LRU, maintained by the pageout code. An inactive_ratio
2811 * of 3 means 3:1 or 25% of the folios are kept on the inactive list.
2814 * memory ratio inactive
2815 * -------------------------------------
2824 static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru)
2826 enum lru_list active_lru = inactive_lru + LRU_ACTIVE;
2827 unsigned long inactive, active;
2828 unsigned long inactive_ratio;
2831 inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru);
2832 active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru);
2834 gb = (inactive + active) >> (30 - PAGE_SHIFT);
2836 inactive_ratio = int_sqrt(10 * gb);
2840 return inactive * inactive_ratio < active;
2850 static void prepare_scan_count(pg_data_t *pgdat, struct scan_control *sc)
2853 struct lruvec *target_lruvec;
2855 if (lru_gen_enabled())
2858 target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat);
2861 * Flush the memory cgroup stats, so that we read accurate per-memcg
2862 * lruvec stats for heuristics.
2864 mem_cgroup_flush_stats();
2867 * Determine the scan balance between anon and file LRUs.
2869 spin_lock_irq(&target_lruvec->lru_lock);
2870 sc->anon_cost = target_lruvec->anon_cost;
2871 sc->file_cost = target_lruvec->file_cost;
2872 spin_unlock_irq(&target_lruvec->lru_lock);
2875 * Target desirable inactive:active list ratios for the anon
2876 * and file LRU lists.
2878 if (!sc->force_deactivate) {
2879 unsigned long refaults;
2882 * When refaults are being observed, it means a new
2883 * workingset is being established. Deactivate to get
2884 * rid of any stale active pages quickly.
2886 refaults = lruvec_page_state(target_lruvec,
2887 WORKINGSET_ACTIVATE_ANON);
2888 if (refaults != target_lruvec->refaults[WORKINGSET_ANON] ||
2889 inactive_is_low(target_lruvec, LRU_INACTIVE_ANON))
2890 sc->may_deactivate |= DEACTIVATE_ANON;
2892 sc->may_deactivate &= ~DEACTIVATE_ANON;
2894 refaults = lruvec_page_state(target_lruvec,
2895 WORKINGSET_ACTIVATE_FILE);
2896 if (refaults != target_lruvec->refaults[WORKINGSET_FILE] ||
2897 inactive_is_low(target_lruvec, LRU_INACTIVE_FILE))
2898 sc->may_deactivate |= DEACTIVATE_FILE;
2900 sc->may_deactivate &= ~DEACTIVATE_FILE;
2902 sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE;
2905 * If we have plenty of inactive file pages that aren't
2906 * thrashing, try to reclaim those first before touching
2909 file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE);
2910 if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE))
2911 sc->cache_trim_mode = 1;
2913 sc->cache_trim_mode = 0;
2916 * Prevent the reclaimer from falling into the cache trap: as
2917 * cache pages start out inactive, every cache fault will tip
2918 * the scan balance towards the file LRU. And as the file LRU
2919 * shrinks, so does the window for rotation from references.
2920 * This means we have a runaway feedback loop where a tiny
2921 * thrashing file LRU becomes infinitely more attractive than
2922 * anon pages. Try to detect this based on file LRU size.
2924 if (!cgroup_reclaim(sc)) {
2925 unsigned long total_high_wmark = 0;
2926 unsigned long free, anon;
2929 free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES);
2930 file = node_page_state(pgdat, NR_ACTIVE_FILE) +
2931 node_page_state(pgdat, NR_INACTIVE_FILE);
2933 for (z = 0; z < MAX_NR_ZONES; z++) {
2934 struct zone *zone = &pgdat->node_zones[z];
2936 if (!managed_zone(zone))
2939 total_high_wmark += high_wmark_pages(zone);
2943 * Consider anon: if that's low too, this isn't a
2944 * runaway file reclaim problem, but rather just
2945 * extreme pressure. Reclaim as per usual then.
2947 anon = node_page_state(pgdat, NR_INACTIVE_ANON);
2950 file + free <= total_high_wmark &&
2951 !(sc->may_deactivate & DEACTIVATE_ANON) &&
2952 anon >> sc->priority;
2957 * Determine how aggressively the anon and file LRU lists should be
2960 * nr[0] = anon inactive folios to scan; nr[1] = anon active folios to scan
2961 * nr[2] = file inactive folios to scan; nr[3] = file active folios to scan
2963 static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
2966 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2967 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
2968 unsigned long anon_cost, file_cost, total_cost;
2969 int swappiness = mem_cgroup_swappiness(memcg);
2970 u64 fraction[ANON_AND_FILE];
2971 u64 denominator = 0; /* gcc */
2972 enum scan_balance scan_balance;
2973 unsigned long ap, fp;
2976 /* If we have no swap space, do not bother scanning anon folios. */
2977 if (!sc->may_swap || !can_reclaim_anon_pages(memcg, pgdat->node_id, sc)) {
2978 scan_balance = SCAN_FILE;
2983 * Global reclaim will swap to prevent OOM even with no
2984 * swappiness, but memcg users want to use this knob to
2985 * disable swapping for individual groups completely when
2986 * using the memory controller's swap limit feature would be
2989 if (cgroup_reclaim(sc) && !swappiness) {
2990 scan_balance = SCAN_FILE;
2995 * Do not apply any pressure balancing cleverness when the
2996 * system is close to OOM, scan both anon and file equally
2997 * (unless the swappiness setting disagrees with swapping).
2999 if (!sc->priority && swappiness) {
3000 scan_balance = SCAN_EQUAL;
3005 * If the system is almost out of file pages, force-scan anon.
3007 if (sc->file_is_tiny) {
3008 scan_balance = SCAN_ANON;
3013 * If there is enough inactive page cache, we do not reclaim
3014 * anything from the anonymous working right now.
3016 if (sc->cache_trim_mode) {
3017 scan_balance = SCAN_FILE;
3021 scan_balance = SCAN_FRACT;
3023 * Calculate the pressure balance between anon and file pages.
3025 * The amount of pressure we put on each LRU is inversely
3026 * proportional to the cost of reclaiming each list, as
3027 * determined by the share of pages that are refaulting, times
3028 * the relative IO cost of bringing back a swapped out
3029 * anonymous page vs reloading a filesystem page (swappiness).
3031 * Although we limit that influence to ensure no list gets
3032 * left behind completely: at least a third of the pressure is
3033 * applied, before swappiness.
3035 * With swappiness at 100, anon and file have equal IO cost.
3037 total_cost = sc->anon_cost + sc->file_cost;
3038 anon_cost = total_cost + sc->anon_cost;
3039 file_cost = total_cost + sc->file_cost;
3040 total_cost = anon_cost + file_cost;
3042 ap = swappiness * (total_cost + 1);
3043 ap /= anon_cost + 1;
3045 fp = (200 - swappiness) * (total_cost + 1);
3046 fp /= file_cost + 1;
3050 denominator = ap + fp;
3052 for_each_evictable_lru(lru) {
3053 int file = is_file_lru(lru);
3054 unsigned long lruvec_size;
3055 unsigned long low, min;
3058 lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
3059 mem_cgroup_protection(sc->target_mem_cgroup, memcg,
3064 * Scale a cgroup's reclaim pressure by proportioning
3065 * its current usage to its memory.low or memory.min
3068 * This is important, as otherwise scanning aggression
3069 * becomes extremely binary -- from nothing as we
3070 * approach the memory protection threshold, to totally
3071 * nominal as we exceed it. This results in requiring
3072 * setting extremely liberal protection thresholds. It
3073 * also means we simply get no protection at all if we
3074 * set it too low, which is not ideal.
3076 * If there is any protection in place, we reduce scan
3077 * pressure by how much of the total memory used is
3078 * within protection thresholds.
3080 * There is one special case: in the first reclaim pass,
3081 * we skip over all groups that are within their low
3082 * protection. If that fails to reclaim enough pages to
3083 * satisfy the reclaim goal, we come back and override
3084 * the best-effort low protection. However, we still
3085 * ideally want to honor how well-behaved groups are in
3086 * that case instead of simply punishing them all
3087 * equally. As such, we reclaim them based on how much
3088 * memory they are using, reducing the scan pressure
3089 * again by how much of the total memory used is under
3092 unsigned long cgroup_size = mem_cgroup_size(memcg);
3093 unsigned long protection;
3095 /* memory.low scaling, make sure we retry before OOM */
3096 if (!sc->memcg_low_reclaim && low > min) {
3098 sc->memcg_low_skipped = 1;
3103 /* Avoid TOCTOU with earlier protection check */
3104 cgroup_size = max(cgroup_size, protection);
3106 scan = lruvec_size - lruvec_size * protection /
3110 * Minimally target SWAP_CLUSTER_MAX pages to keep
3111 * reclaim moving forwards, avoiding decrementing
3112 * sc->priority further than desirable.
3114 scan = max(scan, SWAP_CLUSTER_MAX);
3119 scan >>= sc->priority;
3122 * If the cgroup's already been deleted, make sure to
3123 * scrape out the remaining cache.
3125 if (!scan && !mem_cgroup_online(memcg))
3126 scan = min(lruvec_size, SWAP_CLUSTER_MAX);
3128 switch (scan_balance) {
3130 /* Scan lists relative to size */
3134 * Scan types proportional to swappiness and
3135 * their relative recent reclaim efficiency.
3136 * Make sure we don't miss the last page on
3137 * the offlined memory cgroups because of a
3140 scan = mem_cgroup_online(memcg) ?
3141 div64_u64(scan * fraction[file], denominator) :
3142 DIV64_U64_ROUND_UP(scan * fraction[file],
3147 /* Scan one type exclusively */
3148 if ((scan_balance == SCAN_FILE) != file)
3152 /* Look ma, no brain */
3161 * Anonymous LRU management is a waste if there is
3162 * ultimately no way to reclaim the memory.
3164 static bool can_age_anon_pages(struct pglist_data *pgdat,
3165 struct scan_control *sc)
3167 /* Aging the anon LRU is valuable if swap is present: */
3168 if (total_swap_pages > 0)
3171 /* Also valuable if anon pages can be demoted: */
3172 return can_demote(pgdat->node_id, sc);
3175 #ifdef CONFIG_LRU_GEN
3177 #ifdef CONFIG_LRU_GEN_ENABLED
3178 DEFINE_STATIC_KEY_ARRAY_TRUE(lru_gen_caps, NR_LRU_GEN_CAPS);
3179 #define get_cap(cap) static_branch_likely(&lru_gen_caps[cap])
3181 DEFINE_STATIC_KEY_ARRAY_FALSE(lru_gen_caps, NR_LRU_GEN_CAPS);
3182 #define get_cap(cap) static_branch_unlikely(&lru_gen_caps[cap])
3185 /******************************************************************************
3187 ******************************************************************************/
3189 #define LRU_REFS_FLAGS (BIT(PG_referenced) | BIT(PG_workingset))
3191 #define DEFINE_MAX_SEQ(lruvec) \
3192 unsigned long max_seq = READ_ONCE((lruvec)->lrugen.max_seq)
3194 #define DEFINE_MIN_SEQ(lruvec) \
3195 unsigned long min_seq[ANON_AND_FILE] = { \
3196 READ_ONCE((lruvec)->lrugen.min_seq[LRU_GEN_ANON]), \
3197 READ_ONCE((lruvec)->lrugen.min_seq[LRU_GEN_FILE]), \
3200 #define for_each_gen_type_zone(gen, type, zone) \
3201 for ((gen) = 0; (gen) < MAX_NR_GENS; (gen)++) \
3202 for ((type) = 0; (type) < ANON_AND_FILE; (type)++) \
3203 for ((zone) = 0; (zone) < MAX_NR_ZONES; (zone)++)
3205 #define get_memcg_gen(seq) ((seq) % MEMCG_NR_GENS)
3206 #define get_memcg_bin(bin) ((bin) % MEMCG_NR_BINS)
3208 static struct lruvec *get_lruvec(struct mem_cgroup *memcg, int nid)
3210 struct pglist_data *pgdat = NODE_DATA(nid);
3214 struct lruvec *lruvec = &memcg->nodeinfo[nid]->lruvec;
3216 /* see the comment in mem_cgroup_lruvec() */
3218 lruvec->pgdat = pgdat;
3223 VM_WARN_ON_ONCE(!mem_cgroup_disabled());
3225 return &pgdat->__lruvec;
3228 static int get_swappiness(struct lruvec *lruvec, struct scan_control *sc)
3230 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
3231 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
3236 if (!can_demote(pgdat->node_id, sc) &&
3237 mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH)
3240 return mem_cgroup_swappiness(memcg);
3243 static int get_nr_gens(struct lruvec *lruvec, int type)
3245 return lruvec->lrugen.max_seq - lruvec->lrugen.min_seq[type] + 1;
3248 static bool __maybe_unused seq_is_valid(struct lruvec *lruvec)
3250 /* see the comment on lru_gen_folio */
3251 return get_nr_gens(lruvec, LRU_GEN_FILE) >= MIN_NR_GENS &&
3252 get_nr_gens(lruvec, LRU_GEN_FILE) <= get_nr_gens(lruvec, LRU_GEN_ANON) &&
3253 get_nr_gens(lruvec, LRU_GEN_ANON) <= MAX_NR_GENS;
3256 /******************************************************************************
3258 ******************************************************************************/
3261 * Bloom filters with m=1<<15, k=2 and the false positive rates of ~1/5 when
3262 * n=10,000 and ~1/2 when n=20,000, where, conventionally, m is the number of
3263 * bits in a bitmap, k is the number of hash functions and n is the number of
3266 * Page table walkers use one of the two filters to reduce their search space.
3267 * To get rid of non-leaf entries that no longer have enough leaf entries, the
3268 * aging uses the double-buffering technique to flip to the other filter each
3269 * time it produces a new generation. For non-leaf entries that have enough
3270 * leaf entries, the aging carries them over to the next generation in
3271 * walk_pmd_range(); the eviction also report them when walking the rmap
3272 * in lru_gen_look_around().
3274 * For future optimizations:
3275 * 1. It's not necessary to keep both filters all the time. The spare one can be
3276 * freed after the RCU grace period and reallocated if needed again.
3277 * 2. And when reallocating, it's worth scaling its size according to the number
3278 * of inserted entries in the other filter, to reduce the memory overhead on
3279 * small systems and false positives on large systems.
3280 * 3. Jenkins' hash function is an alternative to Knuth's.
3282 #define BLOOM_FILTER_SHIFT 15
3284 static inline int filter_gen_from_seq(unsigned long seq)
3286 return seq % NR_BLOOM_FILTERS;
3289 static void get_item_key(void *item, int *key)
3291 u32 hash = hash_ptr(item, BLOOM_FILTER_SHIFT * 2);
3293 BUILD_BUG_ON(BLOOM_FILTER_SHIFT * 2 > BITS_PER_TYPE(u32));
3295 key[0] = hash & (BIT(BLOOM_FILTER_SHIFT) - 1);
3296 key[1] = hash >> BLOOM_FILTER_SHIFT;
3299 static bool test_bloom_filter(struct lruvec *lruvec, unsigned long seq, void *item)
3302 unsigned long *filter;
3303 int gen = filter_gen_from_seq(seq);
3305 filter = READ_ONCE(lruvec->mm_state.filters[gen]);
3309 get_item_key(item, key);
3311 return test_bit(key[0], filter) && test_bit(key[1], filter);
3314 static void update_bloom_filter(struct lruvec *lruvec, unsigned long seq, void *item)
3317 unsigned long *filter;
3318 int gen = filter_gen_from_seq(seq);
3320 filter = READ_ONCE(lruvec->mm_state.filters[gen]);
3324 get_item_key(item, key);
3326 if (!test_bit(key[0], filter))
3327 set_bit(key[0], filter);
3328 if (!test_bit(key[1], filter))
3329 set_bit(key[1], filter);
3332 static void reset_bloom_filter(struct lruvec *lruvec, unsigned long seq)
3334 unsigned long *filter;
3335 int gen = filter_gen_from_seq(seq);
3337 filter = lruvec->mm_state.filters[gen];
3339 bitmap_clear(filter, 0, BIT(BLOOM_FILTER_SHIFT));
3343 filter = bitmap_zalloc(BIT(BLOOM_FILTER_SHIFT),
3344 __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN);
3345 WRITE_ONCE(lruvec->mm_state.filters[gen], filter);
3348 /******************************************************************************
3350 ******************************************************************************/
3352 static struct lru_gen_mm_list *get_mm_list(struct mem_cgroup *memcg)
3354 static struct lru_gen_mm_list mm_list = {
3355 .fifo = LIST_HEAD_INIT(mm_list.fifo),
3356 .lock = __SPIN_LOCK_UNLOCKED(mm_list.lock),
3361 return &memcg->mm_list;
3363 VM_WARN_ON_ONCE(!mem_cgroup_disabled());
3368 void lru_gen_add_mm(struct mm_struct *mm)
3371 struct mem_cgroup *memcg = get_mem_cgroup_from_mm(mm);
3372 struct lru_gen_mm_list *mm_list = get_mm_list(memcg);
3374 VM_WARN_ON_ONCE(!list_empty(&mm->lru_gen.list));
3376 VM_WARN_ON_ONCE(mm->lru_gen.memcg);
3377 mm->lru_gen.memcg = memcg;
3379 spin_lock(&mm_list->lock);
3381 for_each_node_state(nid, N_MEMORY) {
3382 struct lruvec *lruvec = get_lruvec(memcg, nid);
3384 /* the first addition since the last iteration */
3385 if (lruvec->mm_state.tail == &mm_list->fifo)
3386 lruvec->mm_state.tail = &mm->lru_gen.list;
3389 list_add_tail(&mm->lru_gen.list, &mm_list->fifo);
3391 spin_unlock(&mm_list->lock);
3394 void lru_gen_del_mm(struct mm_struct *mm)
3397 struct lru_gen_mm_list *mm_list;
3398 struct mem_cgroup *memcg = NULL;
3400 if (list_empty(&mm->lru_gen.list))
3404 memcg = mm->lru_gen.memcg;
3406 mm_list = get_mm_list(memcg);
3408 spin_lock(&mm_list->lock);
3410 for_each_node(nid) {
3411 struct lruvec *lruvec = get_lruvec(memcg, nid);
3413 /* where the last iteration ended (exclusive) */
3414 if (lruvec->mm_state.tail == &mm->lru_gen.list)
3415 lruvec->mm_state.tail = lruvec->mm_state.tail->next;
3417 /* where the current iteration continues (inclusive) */
3418 if (lruvec->mm_state.head != &mm->lru_gen.list)
3421 lruvec->mm_state.head = lruvec->mm_state.head->next;
3422 /* the deletion ends the current iteration */
3423 if (lruvec->mm_state.head == &mm_list->fifo)
3424 WRITE_ONCE(lruvec->mm_state.seq, lruvec->mm_state.seq + 1);
3427 list_del_init(&mm->lru_gen.list);
3429 spin_unlock(&mm_list->lock);
3432 mem_cgroup_put(mm->lru_gen.memcg);
3433 mm->lru_gen.memcg = NULL;
3438 void lru_gen_migrate_mm(struct mm_struct *mm)
3440 struct mem_cgroup *memcg;
3441 struct task_struct *task = rcu_dereference_protected(mm->owner, true);
3443 VM_WARN_ON_ONCE(task->mm != mm);
3444 lockdep_assert_held(&task->alloc_lock);
3446 /* for mm_update_next_owner() */
3447 if (mem_cgroup_disabled())
3450 /* migration can happen before addition */
3451 if (!mm->lru_gen.memcg)
3455 memcg = mem_cgroup_from_task(task);
3457 if (memcg == mm->lru_gen.memcg)
3460 VM_WARN_ON_ONCE(list_empty(&mm->lru_gen.list));
3467 static void reset_mm_stats(struct lruvec *lruvec, struct lru_gen_mm_walk *walk, bool last)
3472 lockdep_assert_held(&get_mm_list(lruvec_memcg(lruvec))->lock);
3475 hist = lru_hist_from_seq(walk->max_seq);
3477 for (i = 0; i < NR_MM_STATS; i++) {
3478 WRITE_ONCE(lruvec->mm_state.stats[hist][i],
3479 lruvec->mm_state.stats[hist][i] + walk->mm_stats[i]);
3480 walk->mm_stats[i] = 0;
3484 if (NR_HIST_GENS > 1 && last) {
3485 hist = lru_hist_from_seq(lruvec->mm_state.seq + 1);
3487 for (i = 0; i < NR_MM_STATS; i++)
3488 WRITE_ONCE(lruvec->mm_state.stats[hist][i], 0);
3492 static bool should_skip_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)
3495 unsigned long size = 0;
3496 struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
3497 int key = pgdat->node_id % BITS_PER_TYPE(mm->lru_gen.bitmap);
3499 if (!walk->force_scan && !test_bit(key, &mm->lru_gen.bitmap))
3502 clear_bit(key, &mm->lru_gen.bitmap);
3504 for (type = !walk->can_swap; type < ANON_AND_FILE; type++) {
3505 size += type ? get_mm_counter(mm, MM_FILEPAGES) :
3506 get_mm_counter(mm, MM_ANONPAGES) +
3507 get_mm_counter(mm, MM_SHMEMPAGES);
3510 if (size < MIN_LRU_BATCH)
3513 return !mmget_not_zero(mm);
3516 static bool iterate_mm_list(struct lruvec *lruvec, struct lru_gen_mm_walk *walk,
3517 struct mm_struct **iter)
3521 struct mm_struct *mm = NULL;
3522 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
3523 struct lru_gen_mm_list *mm_list = get_mm_list(memcg);
3524 struct lru_gen_mm_state *mm_state = &lruvec->mm_state;
3527 * There are four interesting cases for this page table walker:
3528 * 1. It tries to start a new iteration of mm_list with a stale max_seq;
3529 * there is nothing left to do.
3530 * 2. It's the first of the current generation, and it needs to reset
3531 * the Bloom filter for the next generation.
3532 * 3. It reaches the end of mm_list, and it needs to increment
3533 * mm_state->seq; the iteration is done.
3534 * 4. It's the last of the current generation, and it needs to reset the
3535 * mm stats counters for the next generation.
3537 spin_lock(&mm_list->lock);
3539 VM_WARN_ON_ONCE(mm_state->seq + 1 < walk->max_seq);
3540 VM_WARN_ON_ONCE(*iter && mm_state->seq > walk->max_seq);
3541 VM_WARN_ON_ONCE(*iter && !mm_state->nr_walkers);
3543 if (walk->max_seq <= mm_state->seq) {
3549 if (!mm_state->nr_walkers) {
3550 VM_WARN_ON_ONCE(mm_state->head && mm_state->head != &mm_list->fifo);
3552 mm_state->head = mm_list->fifo.next;
3556 while (!mm && mm_state->head != &mm_list->fifo) {
3557 mm = list_entry(mm_state->head, struct mm_struct, lru_gen.list);
3559 mm_state->head = mm_state->head->next;
3561 /* force scan for those added after the last iteration */
3562 if (!mm_state->tail || mm_state->tail == &mm->lru_gen.list) {
3563 mm_state->tail = mm_state->head;
3564 walk->force_scan = true;
3567 if (should_skip_mm(mm, walk))
3571 if (mm_state->head == &mm_list->fifo)
3572 WRITE_ONCE(mm_state->seq, mm_state->seq + 1);
3575 mm_state->nr_walkers--;
3577 mm_state->nr_walkers++;
3579 if (mm_state->nr_walkers)
3583 reset_mm_stats(lruvec, walk, last);
3585 spin_unlock(&mm_list->lock);
3588 reset_bloom_filter(lruvec, walk->max_seq + 1);
3598 static bool iterate_mm_list_nowalk(struct lruvec *lruvec, unsigned long max_seq)
3600 bool success = false;
3601 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
3602 struct lru_gen_mm_list *mm_list = get_mm_list(memcg);
3603 struct lru_gen_mm_state *mm_state = &lruvec->mm_state;
3605 spin_lock(&mm_list->lock);
3607 VM_WARN_ON_ONCE(mm_state->seq + 1 < max_seq);
3609 if (max_seq > mm_state->seq && !mm_state->nr_walkers) {
3610 VM_WARN_ON_ONCE(mm_state->head && mm_state->head != &mm_list->fifo);
3612 WRITE_ONCE(mm_state->seq, mm_state->seq + 1);
3613 reset_mm_stats(lruvec, NULL, true);
3617 spin_unlock(&mm_list->lock);
3622 /******************************************************************************
3624 ******************************************************************************/
3627 * A feedback loop based on Proportional-Integral-Derivative (PID) controller.
3629 * The P term is refaulted/(evicted+protected) from a tier in the generation
3630 * currently being evicted; the I term is the exponential moving average of the
3631 * P term over the generations previously evicted, using the smoothing factor
3632 * 1/2; the D term isn't supported.
3634 * The setpoint (SP) is always the first tier of one type; the process variable
3635 * (PV) is either any tier of the other type or any other tier of the same
3638 * The error is the difference between the SP and the PV; the correction is to
3639 * turn off protection when SP>PV or turn on protection when SP<PV.
3641 * For future optimizations:
3642 * 1. The D term may discount the other two terms over time so that long-lived
3643 * generations can resist stale information.
3646 unsigned long refaulted;
3647 unsigned long total;
3651 static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain,
3652 struct ctrl_pos *pos)
3654 struct lru_gen_folio *lrugen = &lruvec->lrugen;
3655 int hist = lru_hist_from_seq(lrugen->min_seq[type]);
3657 pos->refaulted = lrugen->avg_refaulted[type][tier] +
3658 atomic_long_read(&lrugen->refaulted[hist][type][tier]);
3659 pos->total = lrugen->avg_total[type][tier] +
3660 atomic_long_read(&lrugen->evicted[hist][type][tier]);
3662 pos->total += lrugen->protected[hist][type][tier - 1];
3666 static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover)
3669 struct lru_gen_folio *lrugen = &lruvec->lrugen;
3670 bool clear = carryover ? NR_HIST_GENS == 1 : NR_HIST_GENS > 1;
3671 unsigned long seq = carryover ? lrugen->min_seq[type] : lrugen->max_seq + 1;
3673 lockdep_assert_held(&lruvec->lru_lock);
3675 if (!carryover && !clear)
3678 hist = lru_hist_from_seq(seq);
3680 for (tier = 0; tier < MAX_NR_TIERS; tier++) {
3684 sum = lrugen->avg_refaulted[type][tier] +
3685 atomic_long_read(&lrugen->refaulted[hist][type][tier]);
3686 WRITE_ONCE(lrugen->avg_refaulted[type][tier], sum / 2);
3688 sum = lrugen->avg_total[type][tier] +
3689 atomic_long_read(&lrugen->evicted[hist][type][tier]);
3691 sum += lrugen->protected[hist][type][tier - 1];
3692 WRITE_ONCE(lrugen->avg_total[type][tier], sum / 2);
3696 atomic_long_set(&lrugen->refaulted[hist][type][tier], 0);
3697 atomic_long_set(&lrugen->evicted[hist][type][tier], 0);
3699 WRITE_ONCE(lrugen->protected[hist][type][tier - 1], 0);
3704 static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv)
3707 * Return true if the PV has a limited number of refaults or a lower
3708 * refaulted/total than the SP.
3710 return pv->refaulted < MIN_LRU_BATCH ||
3711 pv->refaulted * (sp->total + MIN_LRU_BATCH) * sp->gain <=
3712 (sp->refaulted + 1) * pv->total * pv->gain;
3715 /******************************************************************************
3717 ******************************************************************************/
3719 /* promote pages accessed through page tables */
3720 static int folio_update_gen(struct folio *folio, int gen)
3722 unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
3724 VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
3725 VM_WARN_ON_ONCE(!rcu_read_lock_held());
3728 /* lru_gen_del_folio() has isolated this page? */
3729 if (!(old_flags & LRU_GEN_MASK)) {
3730 /* for shrink_folio_list() */
3731 new_flags = old_flags | BIT(PG_referenced);
3735 new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS);
3736 new_flags |= (gen + 1UL) << LRU_GEN_PGOFF;
3737 } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags));
3739 return ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
3742 /* protect pages accessed multiple times through file descriptors */
3743 static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio, bool reclaiming)
3745 int type = folio_is_file_lru(folio);
3746 struct lru_gen_folio *lrugen = &lruvec->lrugen;
3747 int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]);
3748 unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
3750 VM_WARN_ON_ONCE_FOLIO(!(old_flags & LRU_GEN_MASK), folio);
3753 new_gen = ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
3754 /* folio_update_gen() has promoted this page? */
3755 if (new_gen >= 0 && new_gen != old_gen)
3758 new_gen = (old_gen + 1) % MAX_NR_GENS;
3760 new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS);
3761 new_flags |= (new_gen + 1UL) << LRU_GEN_PGOFF;
3762 /* for folio_end_writeback() */
3764 new_flags |= BIT(PG_reclaim);
3765 } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags));
3767 lru_gen_update_size(lruvec, folio, old_gen, new_gen);
3772 static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio,
3773 int old_gen, int new_gen)
3775 int type = folio_is_file_lru(folio);
3776 int zone = folio_zonenum(folio);
3777 int delta = folio_nr_pages(folio);
3779 VM_WARN_ON_ONCE(old_gen >= MAX_NR_GENS);
3780 VM_WARN_ON_ONCE(new_gen >= MAX_NR_GENS);
3784 walk->nr_pages[old_gen][type][zone] -= delta;
3785 walk->nr_pages[new_gen][type][zone] += delta;
3788 static void reset_batch_size(struct lruvec *lruvec, struct lru_gen_mm_walk *walk)
3790 int gen, type, zone;
3791 struct lru_gen_folio *lrugen = &lruvec->lrugen;
3795 for_each_gen_type_zone(gen, type, zone) {
3796 enum lru_list lru = type * LRU_INACTIVE_FILE;
3797 int delta = walk->nr_pages[gen][type][zone];
3802 walk->nr_pages[gen][type][zone] = 0;
3803 WRITE_ONCE(lrugen->nr_pages[gen][type][zone],
3804 lrugen->nr_pages[gen][type][zone] + delta);
3806 if (lru_gen_is_active(lruvec, gen))
3808 __update_lru_size(lruvec, lru, zone, delta);
3812 static int should_skip_vma(unsigned long start, unsigned long end, struct mm_walk *args)
3814 struct address_space *mapping;
3815 struct vm_area_struct *vma = args->vma;
3816 struct lru_gen_mm_walk *walk = args->private;
3818 if (!vma_is_accessible(vma))
3821 if (is_vm_hugetlb_page(vma))
3824 if (!vma_has_recency(vma))
3827 if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL))
3830 if (vma == get_gate_vma(vma->vm_mm))
3833 if (vma_is_anonymous(vma))
3834 return !walk->can_swap;
3836 if (WARN_ON_ONCE(!vma->vm_file || !vma->vm_file->f_mapping))
3839 mapping = vma->vm_file->f_mapping;
3840 if (mapping_unevictable(mapping))
3843 if (shmem_mapping(mapping))
3844 return !walk->can_swap;
3846 /* to exclude special mappings like dax, etc. */
3847 return !mapping->a_ops->read_folio;
3851 * Some userspace memory allocators map many single-page VMAs. Instead of
3852 * returning back to the PGD table for each of such VMAs, finish an entire PMD
3853 * table to reduce zigzags and improve cache performance.
3855 static bool get_next_vma(unsigned long mask, unsigned long size, struct mm_walk *args,
3856 unsigned long *vm_start, unsigned long *vm_end)
3858 unsigned long start = round_up(*vm_end, size);
3859 unsigned long end = (start | ~mask) + 1;
3860 VMA_ITERATOR(vmi, args->mm, start);
3862 VM_WARN_ON_ONCE(mask & size);
3863 VM_WARN_ON_ONCE((start & mask) != (*vm_start & mask));
3865 for_each_vma(vmi, args->vma) {
3866 if (end && end <= args->vma->vm_start)
3869 if (should_skip_vma(args->vma->vm_start, args->vma->vm_end, args))
3872 *vm_start = max(start, args->vma->vm_start);
3873 *vm_end = min(end - 1, args->vma->vm_end - 1) + 1;
3881 static unsigned long get_pte_pfn(pte_t pte, struct vm_area_struct *vma, unsigned long addr)
3883 unsigned long pfn = pte_pfn(pte);
3885 VM_WARN_ON_ONCE(addr < vma->vm_start || addr >= vma->vm_end);
3887 if (!pte_present(pte) || is_zero_pfn(pfn))
3890 if (WARN_ON_ONCE(pte_devmap(pte) || pte_special(pte)))
3893 if (WARN_ON_ONCE(!pfn_valid(pfn)))
3899 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG)
3900 static unsigned long get_pmd_pfn(pmd_t pmd, struct vm_area_struct *vma, unsigned long addr)
3902 unsigned long pfn = pmd_pfn(pmd);
3904 VM_WARN_ON_ONCE(addr < vma->vm_start || addr >= vma->vm_end);
3906 if (!pmd_present(pmd) || is_huge_zero_pmd(pmd))
3909 if (WARN_ON_ONCE(pmd_devmap(pmd)))
3912 if (WARN_ON_ONCE(!pfn_valid(pfn)))
3919 static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *memcg,
3920 struct pglist_data *pgdat, bool can_swap)
3922 struct folio *folio;
3924 /* try to avoid unnecessary memory loads */
3925 if (pfn < pgdat->node_start_pfn || pfn >= pgdat_end_pfn(pgdat))
3928 folio = pfn_folio(pfn);
3929 if (folio_nid(folio) != pgdat->node_id)
3932 if (folio_memcg_rcu(folio) != memcg)
3935 /* file VMAs can contain anon pages from COW */
3936 if (!folio_is_file_lru(folio) && !can_swap)
3942 static bool suitable_to_scan(int total, int young)
3944 int n = clamp_t(int, cache_line_size() / sizeof(pte_t), 2, 8);
3946 /* suitable if the average number of young PTEs per cacheline is >=1 */
3947 return young * n >= total;
3950 static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
3951 struct mm_walk *args)
3959 struct lru_gen_mm_walk *walk = args->private;
3960 struct mem_cgroup *memcg = lruvec_memcg(walk->lruvec);
3961 struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
3962 int old_gen, new_gen = lru_gen_from_seq(walk->max_seq);
3964 VM_WARN_ON_ONCE(pmd_leaf(*pmd));
3966 ptl = pte_lockptr(args->mm, pmd);
3967 if (!spin_trylock(ptl))
3970 arch_enter_lazy_mmu_mode();
3972 pte = pte_offset_map(pmd, start & PMD_MASK);
3974 for (i = pte_index(start), addr = start; addr != end; i++, addr += PAGE_SIZE) {
3976 struct folio *folio;
3979 walk->mm_stats[MM_LEAF_TOTAL]++;
3981 pfn = get_pte_pfn(pte[i], args->vma, addr);
3985 if (!pte_young(pte[i])) {
3986 walk->mm_stats[MM_LEAF_OLD]++;
3990 folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap);
3994 if (!ptep_test_and_clear_young(args->vma, addr, pte + i))
3995 VM_WARN_ON_ONCE(true);
3998 walk->mm_stats[MM_LEAF_YOUNG]++;
4000 if (pte_dirty(pte[i]) && !folio_test_dirty(folio) &&
4001 !(folio_test_anon(folio) && folio_test_swapbacked(folio) &&
4002 !folio_test_swapcache(folio)))
4003 folio_mark_dirty(folio);
4005 old_gen = folio_update_gen(folio, new_gen);
4006 if (old_gen >= 0 && old_gen != new_gen)
4007 update_batch_size(walk, folio, old_gen, new_gen);
4010 if (i < PTRS_PER_PTE && get_next_vma(PMD_MASK, PAGE_SIZE, args, &start, &end))
4015 arch_leave_lazy_mmu_mode();
4018 return suitable_to_scan(total, young);
4021 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG)
4022 static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area_struct *vma,
4023 struct mm_walk *args, unsigned long *bitmap, unsigned long *first)
4028 struct lru_gen_mm_walk *walk = args->private;
4029 struct mem_cgroup *memcg = lruvec_memcg(walk->lruvec);
4030 struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
4031 int old_gen, new_gen = lru_gen_from_seq(walk->max_seq);
4033 VM_WARN_ON_ONCE(pud_leaf(*pud));
4035 /* try to batch at most 1+MIN_LRU_BATCH+1 entries */
4038 bitmap_zero(bitmap, MIN_LRU_BATCH);
4042 i = addr == -1 ? 0 : pmd_index(addr) - pmd_index(*first);
4043 if (i && i <= MIN_LRU_BATCH) {
4044 __set_bit(i - 1, bitmap);
4048 pmd = pmd_offset(pud, *first);
4050 ptl = pmd_lockptr(args->mm, pmd);
4051 if (!spin_trylock(ptl))
4054 arch_enter_lazy_mmu_mode();
4058 struct folio *folio;
4060 /* don't round down the first address */
4061 addr = i ? (*first & PMD_MASK) + i * PMD_SIZE : *first;
4063 pfn = get_pmd_pfn(pmd[i], vma, addr);
4067 if (!pmd_trans_huge(pmd[i])) {
4068 if (arch_has_hw_nonleaf_pmd_young() && get_cap(LRU_GEN_NONLEAF_YOUNG))
4069 pmdp_test_and_clear_young(vma, addr, pmd + i);
4073 folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap);
4077 if (!pmdp_test_and_clear_young(vma, addr, pmd + i))
4080 walk->mm_stats[MM_LEAF_YOUNG]++;
4082 if (pmd_dirty(pmd[i]) && !folio_test_dirty(folio) &&
4083 !(folio_test_anon(folio) && folio_test_swapbacked(folio) &&
4084 !folio_test_swapcache(folio)))
4085 folio_mark_dirty(folio);
4087 old_gen = folio_update_gen(folio, new_gen);
4088 if (old_gen >= 0 && old_gen != new_gen)
4089 update_batch_size(walk, folio, old_gen, new_gen);
4091 i = i > MIN_LRU_BATCH ? 0 : find_next_bit(bitmap, MIN_LRU_BATCH, i) + 1;
4092 } while (i <= MIN_LRU_BATCH);
4094 arch_leave_lazy_mmu_mode();
4100 static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area_struct *vma,
4101 struct mm_walk *args, unsigned long *bitmap, unsigned long *first)
4106 static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,
4107 struct mm_walk *args)
4113 struct vm_area_struct *vma;
4114 unsigned long bitmap[BITS_TO_LONGS(MIN_LRU_BATCH)];
4115 unsigned long first = -1;
4116 struct lru_gen_mm_walk *walk = args->private;
4118 VM_WARN_ON_ONCE(pud_leaf(*pud));
4121 * Finish an entire PMD in two passes: the first only reaches to PTE
4122 * tables to avoid taking the PMD lock; the second, if necessary, takes
4123 * the PMD lock to clear the accessed bit in PMD entries.
4125 pmd = pmd_offset(pud, start & PUD_MASK);
4127 /* walk_pte_range() may call get_next_vma() */
4129 for (i = pmd_index(start), addr = start; addr != end; i++, addr = next) {
4130 pmd_t val = pmdp_get_lockless(pmd + i);
4132 next = pmd_addr_end(addr, end);
4134 if (!pmd_present(val) || is_huge_zero_pmd(val)) {
4135 walk->mm_stats[MM_LEAF_TOTAL]++;
4139 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
4140 if (pmd_trans_huge(val)) {
4141 unsigned long pfn = pmd_pfn(val);
4142 struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
4144 walk->mm_stats[MM_LEAF_TOTAL]++;
4146 if (!pmd_young(val)) {
4147 walk->mm_stats[MM_LEAF_OLD]++;
4151 /* try to avoid unnecessary memory loads */
4152 if (pfn < pgdat->node_start_pfn || pfn >= pgdat_end_pfn(pgdat))
4155 walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first);
4159 walk->mm_stats[MM_NONLEAF_TOTAL]++;
4161 if (arch_has_hw_nonleaf_pmd_young() && get_cap(LRU_GEN_NONLEAF_YOUNG)) {
4162 if (!pmd_young(val))
4165 walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first);
4168 if (!walk->force_scan && !test_bloom_filter(walk->lruvec, walk->max_seq, pmd + i))
4171 walk->mm_stats[MM_NONLEAF_FOUND]++;
4173 if (!walk_pte_range(&val, addr, next, args))
4176 walk->mm_stats[MM_NONLEAF_ADDED]++;
4178 /* carry over to the next generation */
4179 update_bloom_filter(walk->lruvec, walk->max_seq + 1, pmd + i);
4182 walk_pmd_range_locked(pud, -1, vma, args, bitmap, &first);
4184 if (i < PTRS_PER_PMD && get_next_vma(PUD_MASK, PMD_SIZE, args, &start, &end))
4188 static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,
4189 struct mm_walk *args)
4195 struct lru_gen_mm_walk *walk = args->private;
4197 VM_WARN_ON_ONCE(p4d_leaf(*p4d));
4199 pud = pud_offset(p4d, start & P4D_MASK);
4201 for (i = pud_index(start), addr = start; addr != end; i++, addr = next) {
4202 pud_t val = READ_ONCE(pud[i]);
4204 next = pud_addr_end(addr, end);
4206 if (!pud_present(val) || WARN_ON_ONCE(pud_leaf(val)))
4209 walk_pmd_range(&val, addr, next, args);
4211 /* a racy check to curtail the waiting time */
4212 if (wq_has_sleeper(&walk->lruvec->mm_state.wait))
4215 if (need_resched() || walk->batched >= MAX_LRU_BATCH) {
4216 end = (addr | ~PUD_MASK) + 1;
4221 if (i < PTRS_PER_PUD && get_next_vma(P4D_MASK, PUD_SIZE, args, &start, &end))
4224 end = round_up(end, P4D_SIZE);
4226 if (!end || !args->vma)
4229 walk->next_addr = max(end, args->vma->vm_start);
4234 static void walk_mm(struct lruvec *lruvec, struct mm_struct *mm, struct lru_gen_mm_walk *walk)
4236 static const struct mm_walk_ops mm_walk_ops = {
4237 .test_walk = should_skip_vma,
4238 .p4d_entry = walk_pud_range,
4242 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
4244 walk->next_addr = FIRST_USER_ADDRESS;
4249 /* folio_update_gen() requires stable folio_memcg() */
4250 if (!mem_cgroup_trylock_pages(memcg))
4253 /* the caller might be holding the lock for write */
4254 if (mmap_read_trylock(mm)) {
4255 err = walk_page_range(mm, walk->next_addr, ULONG_MAX, &mm_walk_ops, walk);
4257 mmap_read_unlock(mm);
4260 mem_cgroup_unlock_pages();
4262 if (walk->batched) {
4263 spin_lock_irq(&lruvec->lru_lock);
4264 reset_batch_size(lruvec, walk);
4265 spin_unlock_irq(&lruvec->lru_lock);
4269 } while (err == -EAGAIN);
4272 static struct lru_gen_mm_walk *set_mm_walk(struct pglist_data *pgdat, bool force_alloc)
4274 struct lru_gen_mm_walk *walk = current->reclaim_state->mm_walk;
4276 if (pgdat && current_is_kswapd()) {
4277 VM_WARN_ON_ONCE(walk);
4279 walk = &pgdat->mm_walk;
4280 } else if (!walk && force_alloc) {
4281 VM_WARN_ON_ONCE(current_is_kswapd());
4283 walk = kzalloc(sizeof(*walk), __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN);
4286 current->reclaim_state->mm_walk = walk;
4291 static void clear_mm_walk(void)
4293 struct lru_gen_mm_walk *walk = current->reclaim_state->mm_walk;
4295 VM_WARN_ON_ONCE(walk && memchr_inv(walk->nr_pages, 0, sizeof(walk->nr_pages)));
4296 VM_WARN_ON_ONCE(walk && memchr_inv(walk->mm_stats, 0, sizeof(walk->mm_stats)));
4298 current->reclaim_state->mm_walk = NULL;
4300 if (!current_is_kswapd())
4304 static bool inc_min_seq(struct lruvec *lruvec, int type, bool can_swap)
4307 int remaining = MAX_LRU_BATCH;
4308 struct lru_gen_folio *lrugen = &lruvec->lrugen;
4309 int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]);
4311 if (type == LRU_GEN_ANON && !can_swap)
4314 /* prevent cold/hot inversion if force_scan is true */
4315 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4316 struct list_head *head = &lrugen->folios[old_gen][type][zone];
4318 while (!list_empty(head)) {
4319 struct folio *folio = lru_to_folio(head);
4321 VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
4322 VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio);
4323 VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio);
4324 VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio);
4326 new_gen = folio_inc_gen(lruvec, folio, false);
4327 list_move_tail(&folio->lru, &lrugen->folios[new_gen][type][zone]);
4334 reset_ctrl_pos(lruvec, type, true);
4335 WRITE_ONCE(lrugen->min_seq[type], lrugen->min_seq[type] + 1);
4340 static bool try_to_inc_min_seq(struct lruvec *lruvec, bool can_swap)
4342 int gen, type, zone;
4343 bool success = false;
4344 struct lru_gen_folio *lrugen = &lruvec->lrugen;
4345 DEFINE_MIN_SEQ(lruvec);
4347 VM_WARN_ON_ONCE(!seq_is_valid(lruvec));
4349 /* find the oldest populated generation */
4350 for (type = !can_swap; type < ANON_AND_FILE; type++) {
4351 while (min_seq[type] + MIN_NR_GENS <= lrugen->max_seq) {
4352 gen = lru_gen_from_seq(min_seq[type]);
4354 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4355 if (!list_empty(&lrugen->folios[gen][type][zone]))
4365 /* see the comment on lru_gen_folio */
4367 min_seq[LRU_GEN_ANON] = min(min_seq[LRU_GEN_ANON], min_seq[LRU_GEN_FILE]);
4368 min_seq[LRU_GEN_FILE] = max(min_seq[LRU_GEN_ANON], lrugen->min_seq[LRU_GEN_FILE]);
4371 for (type = !can_swap; type < ANON_AND_FILE; type++) {
4372 if (min_seq[type] == lrugen->min_seq[type])
4375 reset_ctrl_pos(lruvec, type, true);
4376 WRITE_ONCE(lrugen->min_seq[type], min_seq[type]);
4383 static void inc_max_seq(struct lruvec *lruvec, bool can_swap, bool force_scan)
4387 struct lru_gen_folio *lrugen = &lruvec->lrugen;
4389 spin_lock_irq(&lruvec->lru_lock);
4391 VM_WARN_ON_ONCE(!seq_is_valid(lruvec));
4393 for (type = ANON_AND_FILE - 1; type >= 0; type--) {
4394 if (get_nr_gens(lruvec, type) != MAX_NR_GENS)
4397 VM_WARN_ON_ONCE(!force_scan && (type == LRU_GEN_FILE || can_swap));
4399 while (!inc_min_seq(lruvec, type, can_swap)) {
4400 spin_unlock_irq(&lruvec->lru_lock);
4402 spin_lock_irq(&lruvec->lru_lock);
4407 * Update the active/inactive LRU sizes for compatibility. Both sides of
4408 * the current max_seq need to be covered, since max_seq+1 can overlap
4409 * with min_seq[LRU_GEN_ANON] if swapping is constrained. And if they do
4410 * overlap, cold/hot inversion happens.
4412 prev = lru_gen_from_seq(lrugen->max_seq - 1);
4413 next = lru_gen_from_seq(lrugen->max_seq + 1);
4415 for (type = 0; type < ANON_AND_FILE; type++) {
4416 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4417 enum lru_list lru = type * LRU_INACTIVE_FILE;
4418 long delta = lrugen->nr_pages[prev][type][zone] -
4419 lrugen->nr_pages[next][type][zone];
4424 __update_lru_size(lruvec, lru, zone, delta);
4425 __update_lru_size(lruvec, lru + LRU_ACTIVE, zone, -delta);
4429 for (type = 0; type < ANON_AND_FILE; type++)
4430 reset_ctrl_pos(lruvec, type, false);
4432 WRITE_ONCE(lrugen->timestamps[next], jiffies);
4433 /* make sure preceding modifications appear */
4434 smp_store_release(&lrugen->max_seq, lrugen->max_seq + 1);
4436 spin_unlock_irq(&lruvec->lru_lock);
4439 static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long max_seq,
4440 struct scan_control *sc, bool can_swap, bool force_scan)
4443 struct lru_gen_mm_walk *walk;
4444 struct mm_struct *mm = NULL;
4445 struct lru_gen_folio *lrugen = &lruvec->lrugen;
4447 VM_WARN_ON_ONCE(max_seq > READ_ONCE(lrugen->max_seq));
4449 /* see the comment in iterate_mm_list() */
4450 if (max_seq <= READ_ONCE(lruvec->mm_state.seq)) {
4456 * If the hardware doesn't automatically set the accessed bit, fallback
4457 * to lru_gen_look_around(), which only clears the accessed bit in a
4458 * handful of PTEs. Spreading the work out over a period of time usually
4459 * is less efficient, but it avoids bursty page faults.
4461 if (!arch_has_hw_pte_young() || !get_cap(LRU_GEN_MM_WALK)) {
4462 success = iterate_mm_list_nowalk(lruvec, max_seq);
4466 walk = set_mm_walk(NULL, true);
4468 success = iterate_mm_list_nowalk(lruvec, max_seq);
4472 walk->lruvec = lruvec;
4473 walk->max_seq = max_seq;
4474 walk->can_swap = can_swap;
4475 walk->force_scan = force_scan;
4478 success = iterate_mm_list(lruvec, walk, &mm);
4480 walk_mm(lruvec, mm, walk);
4486 if (sc->priority <= DEF_PRIORITY - 2)
4487 wait_event_killable(lruvec->mm_state.wait,
4488 max_seq < READ_ONCE(lrugen->max_seq));
4492 VM_WARN_ON_ONCE(max_seq != READ_ONCE(lrugen->max_seq));
4494 inc_max_seq(lruvec, can_swap, force_scan);
4495 /* either this sees any waiters or they will see updated max_seq */
4496 if (wq_has_sleeper(&lruvec->mm_state.wait))
4497 wake_up_all(&lruvec->mm_state.wait);
4502 /******************************************************************************
4503 * working set protection
4504 ******************************************************************************/
4506 static bool lruvec_is_sizable(struct lruvec *lruvec, struct scan_control *sc)
4508 int gen, type, zone;
4509 unsigned long total = 0;
4510 bool can_swap = get_swappiness(lruvec, sc);
4511 struct lru_gen_folio *lrugen = &lruvec->lrugen;
4512 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
4513 DEFINE_MAX_SEQ(lruvec);
4514 DEFINE_MIN_SEQ(lruvec);
4516 for (type = !can_swap; type < ANON_AND_FILE; type++) {
4519 for (seq = min_seq[type]; seq <= max_seq; seq++) {
4520 gen = lru_gen_from_seq(seq);
4522 for (zone = 0; zone < MAX_NR_ZONES; zone++)
4523 total += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L);
4527 /* whether the size is big enough to be helpful */
4528 return mem_cgroup_online(memcg) ? (total >> sc->priority) : total;
4531 static bool lruvec_is_reclaimable(struct lruvec *lruvec, struct scan_control *sc,
4532 unsigned long min_ttl)
4535 unsigned long birth;
4536 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
4537 DEFINE_MIN_SEQ(lruvec);
4539 /* see the comment on lru_gen_folio */
4540 gen = lru_gen_from_seq(min_seq[LRU_GEN_FILE]);
4541 birth = READ_ONCE(lruvec->lrugen.timestamps[gen]);
4543 if (time_is_after_jiffies(birth + min_ttl))
4546 if (!lruvec_is_sizable(lruvec, sc))
4549 mem_cgroup_calculate_protection(NULL, memcg);
4551 return !mem_cgroup_below_min(NULL, memcg);
4554 /* to protect the working set of the last N jiffies */
4555 static unsigned long lru_gen_min_ttl __read_mostly;
4557 static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc)
4559 struct mem_cgroup *memcg;
4560 unsigned long min_ttl = READ_ONCE(lru_gen_min_ttl);
4562 VM_WARN_ON_ONCE(!current_is_kswapd());
4564 /* check the order to exclude compaction-induced reclaim */
4565 if (!min_ttl || sc->order || sc->priority == DEF_PRIORITY)
4568 memcg = mem_cgroup_iter(NULL, NULL, NULL);
4570 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
4572 if (lruvec_is_reclaimable(lruvec, sc, min_ttl)) {
4573 mem_cgroup_iter_break(NULL, memcg);
4578 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)));
4581 * The main goal is to OOM kill if every generation from all memcgs is
4582 * younger than min_ttl. However, another possibility is all memcgs are
4583 * either too small or below min.
4585 if (mutex_trylock(&oom_lock)) {
4586 struct oom_control oc = {
4587 .gfp_mask = sc->gfp_mask,
4592 mutex_unlock(&oom_lock);
4596 /******************************************************************************
4597 * rmap/PT walk feedback
4598 ******************************************************************************/
4601 * This function exploits spatial locality when shrink_folio_list() walks the
4602 * rmap. It scans the adjacent PTEs of a young PTE and promotes hot pages. If
4603 * the scan was done cacheline efficiently, it adds the PMD entry pointing to
4604 * the PTE table to the Bloom filter. This forms a feedback loop between the
4605 * eviction and the aging.
4607 void lru_gen_look_around(struct page_vma_mapped_walk *pvmw)
4610 unsigned long start;
4612 struct lru_gen_mm_walk *walk;
4614 pte_t *pte = pvmw->pte;
4615 unsigned long addr = pvmw->address;
4616 struct folio *folio = pfn_folio(pvmw->pfn);
4617 struct mem_cgroup *memcg = folio_memcg(folio);
4618 struct pglist_data *pgdat = folio_pgdat(folio);
4619 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
4620 DEFINE_MAX_SEQ(lruvec);
4621 int old_gen, new_gen = lru_gen_from_seq(max_seq);
4623 lockdep_assert_held(pvmw->ptl);
4624 VM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio);
4626 if (spin_is_contended(pvmw->ptl))
4629 /* avoid taking the LRU lock under the PTL when possible */
4630 walk = current->reclaim_state ? current->reclaim_state->mm_walk : NULL;
4632 start = max(addr & PMD_MASK, pvmw->vma->vm_start);
4633 end = min(addr | ~PMD_MASK, pvmw->vma->vm_end - 1) + 1;
4635 if (end - start > MIN_LRU_BATCH * PAGE_SIZE) {
4636 if (addr - start < MIN_LRU_BATCH * PAGE_SIZE / 2)
4637 end = start + MIN_LRU_BATCH * PAGE_SIZE;
4638 else if (end - addr < MIN_LRU_BATCH * PAGE_SIZE / 2)
4639 start = end - MIN_LRU_BATCH * PAGE_SIZE;
4641 start = addr - MIN_LRU_BATCH * PAGE_SIZE / 2;
4642 end = addr + MIN_LRU_BATCH * PAGE_SIZE / 2;
4646 /* folio_update_gen() requires stable folio_memcg() */
4647 if (!mem_cgroup_trylock_pages(memcg))
4650 arch_enter_lazy_mmu_mode();
4652 pte -= (addr - start) / PAGE_SIZE;
4654 for (i = 0, addr = start; addr != end; i++, addr += PAGE_SIZE) {
4657 pfn = get_pte_pfn(pte[i], pvmw->vma, addr);
4661 if (!pte_young(pte[i]))
4664 folio = get_pfn_folio(pfn, memcg, pgdat, !walk || walk->can_swap);
4668 if (!ptep_test_and_clear_young(pvmw->vma, addr, pte + i))
4669 VM_WARN_ON_ONCE(true);
4673 if (pte_dirty(pte[i]) && !folio_test_dirty(folio) &&
4674 !(folio_test_anon(folio) && folio_test_swapbacked(folio) &&
4675 !folio_test_swapcache(folio)))
4676 folio_mark_dirty(folio);
4679 old_gen = folio_update_gen(folio, new_gen);
4680 if (old_gen >= 0 && old_gen != new_gen)
4681 update_batch_size(walk, folio, old_gen, new_gen);
4686 old_gen = folio_lru_gen(folio);
4688 folio_set_referenced(folio);
4689 else if (old_gen != new_gen)
4690 folio_activate(folio);
4693 arch_leave_lazy_mmu_mode();
4694 mem_cgroup_unlock_pages();
4696 /* feedback from rmap walkers to page table walkers */
4697 if (suitable_to_scan(i, young))
4698 update_bloom_filter(lruvec, max_seq, pvmw->pmd);
4701 /******************************************************************************
4703 ******************************************************************************/
4705 /* see the comment on MEMCG_NR_GENS */
4716 static int lru_gen_memcg_seg(struct lruvec *lruvec)
4718 return READ_ONCE(lruvec->lrugen.seg);
4721 static void lru_gen_rotate_memcg(struct lruvec *lruvec, int op)
4725 int bin = get_random_u32_below(MEMCG_NR_BINS);
4726 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
4728 spin_lock(&pgdat->memcg_lru.lock);
4730 VM_WARN_ON_ONCE(hlist_nulls_unhashed(&lruvec->lrugen.list));
4733 new = old = lruvec->lrugen.gen;
4735 /* see the comment on MEMCG_NR_GENS */
4736 if (op == MEMCG_LRU_HEAD)
4737 seg = MEMCG_LRU_HEAD;
4738 else if (op == MEMCG_LRU_TAIL)
4739 seg = MEMCG_LRU_TAIL;
4740 else if (op == MEMCG_LRU_OLD)
4741 new = get_memcg_gen(pgdat->memcg_lru.seq);
4742 else if (op == MEMCG_LRU_YOUNG)
4743 new = get_memcg_gen(pgdat->memcg_lru.seq + 1);
4745 VM_WARN_ON_ONCE(true);
4747 hlist_nulls_del_rcu(&lruvec->lrugen.list);
4749 if (op == MEMCG_LRU_HEAD || op == MEMCG_LRU_OLD)
4750 hlist_nulls_add_head_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[new][bin]);
4752 hlist_nulls_add_tail_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[new][bin]);
4754 pgdat->memcg_lru.nr_memcgs[old]--;
4755 pgdat->memcg_lru.nr_memcgs[new]++;
4757 lruvec->lrugen.gen = new;
4758 WRITE_ONCE(lruvec->lrugen.seg, seg);
4760 if (!pgdat->memcg_lru.nr_memcgs[old] && old == get_memcg_gen(pgdat->memcg_lru.seq))
4761 WRITE_ONCE(pgdat->memcg_lru.seq, pgdat->memcg_lru.seq + 1);
4763 spin_unlock(&pgdat->memcg_lru.lock);
4766 void lru_gen_online_memcg(struct mem_cgroup *memcg)
4770 int bin = get_random_u32_below(MEMCG_NR_BINS);
4772 for_each_node(nid) {
4773 struct pglist_data *pgdat = NODE_DATA(nid);
4774 struct lruvec *lruvec = get_lruvec(memcg, nid);
4776 spin_lock(&pgdat->memcg_lru.lock);
4778 VM_WARN_ON_ONCE(!hlist_nulls_unhashed(&lruvec->lrugen.list));
4780 gen = get_memcg_gen(pgdat->memcg_lru.seq);
4782 hlist_nulls_add_tail_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[gen][bin]);
4783 pgdat->memcg_lru.nr_memcgs[gen]++;
4785 lruvec->lrugen.gen = gen;
4787 spin_unlock(&pgdat->memcg_lru.lock);
4791 void lru_gen_offline_memcg(struct mem_cgroup *memcg)
4795 for_each_node(nid) {
4796 struct lruvec *lruvec = get_lruvec(memcg, nid);
4798 lru_gen_rotate_memcg(lruvec, MEMCG_LRU_OLD);
4802 void lru_gen_release_memcg(struct mem_cgroup *memcg)
4807 for_each_node(nid) {
4808 struct pglist_data *pgdat = NODE_DATA(nid);
4809 struct lruvec *lruvec = get_lruvec(memcg, nid);
4811 spin_lock(&pgdat->memcg_lru.lock);
4813 VM_WARN_ON_ONCE(hlist_nulls_unhashed(&lruvec->lrugen.list));
4815 gen = lruvec->lrugen.gen;
4817 hlist_nulls_del_rcu(&lruvec->lrugen.list);
4818 pgdat->memcg_lru.nr_memcgs[gen]--;
4820 if (!pgdat->memcg_lru.nr_memcgs[gen] && gen == get_memcg_gen(pgdat->memcg_lru.seq))
4821 WRITE_ONCE(pgdat->memcg_lru.seq, pgdat->memcg_lru.seq + 1);
4823 spin_unlock(&pgdat->memcg_lru.lock);
4827 void lru_gen_soft_reclaim(struct lruvec *lruvec)
4829 /* see the comment on MEMCG_NR_GENS */
4830 if (lru_gen_memcg_seg(lruvec) != MEMCG_LRU_HEAD)
4831 lru_gen_rotate_memcg(lruvec, MEMCG_LRU_HEAD);
4834 #else /* !CONFIG_MEMCG */
4836 static int lru_gen_memcg_seg(struct lruvec *lruvec)
4843 /******************************************************************************
4845 ******************************************************************************/
4847 static bool sort_folio(struct lruvec *lruvec, struct folio *folio, int tier_idx)
4850 int gen = folio_lru_gen(folio);
4851 int type = folio_is_file_lru(folio);
4852 int zone = folio_zonenum(folio);
4853 int delta = folio_nr_pages(folio);
4854 int refs = folio_lru_refs(folio);
4855 int tier = lru_tier_from_refs(refs);
4856 struct lru_gen_folio *lrugen = &lruvec->lrugen;
4858 VM_WARN_ON_ONCE_FOLIO(gen >= MAX_NR_GENS, folio);
4861 if (!folio_evictable(folio)) {
4862 success = lru_gen_del_folio(lruvec, folio, true);
4863 VM_WARN_ON_ONCE_FOLIO(!success, folio);
4864 folio_set_unevictable(folio);
4865 lruvec_add_folio(lruvec, folio);
4866 __count_vm_events(UNEVICTABLE_PGCULLED, delta);
4870 /* dirty lazyfree */
4871 if (type == LRU_GEN_FILE && folio_test_anon(folio) && folio_test_dirty(folio)) {
4872 success = lru_gen_del_folio(lruvec, folio, true);
4873 VM_WARN_ON_ONCE_FOLIO(!success, folio);
4874 folio_set_swapbacked(folio);
4875 lruvec_add_folio_tail(lruvec, folio);
4880 if (gen != lru_gen_from_seq(lrugen->min_seq[type])) {
4881 list_move(&folio->lru, &lrugen->folios[gen][type][zone]);
4886 if (tier > tier_idx) {
4887 int hist = lru_hist_from_seq(lrugen->min_seq[type]);
4889 gen = folio_inc_gen(lruvec, folio, false);
4890 list_move_tail(&folio->lru, &lrugen->folios[gen][type][zone]);
4892 WRITE_ONCE(lrugen->protected[hist][type][tier - 1],
4893 lrugen->protected[hist][type][tier - 1] + delta);
4894 __mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta);
4898 /* waiting for writeback */
4899 if (folio_test_locked(folio) || folio_test_writeback(folio) ||
4900 (type == LRU_GEN_FILE && folio_test_dirty(folio))) {
4901 gen = folio_inc_gen(lruvec, folio, true);
4902 list_move(&folio->lru, &lrugen->folios[gen][type][zone]);
4909 static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct scan_control *sc)
4913 /* swapping inhibited */
4914 if (!(sc->gfp_mask & __GFP_IO) &&
4915 (folio_test_dirty(folio) ||
4916 (folio_test_anon(folio) && !folio_test_swapcache(folio))))
4919 /* raced with release_pages() */
4920 if (!folio_try_get(folio))
4923 /* raced with another isolation */
4924 if (!folio_test_clear_lru(folio)) {
4929 /* see the comment on MAX_NR_TIERS */
4930 if (!folio_test_referenced(folio))
4931 set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, 0);
4933 /* for shrink_folio_list() */
4934 folio_clear_reclaim(folio);
4935 folio_clear_referenced(folio);
4937 success = lru_gen_del_folio(lruvec, folio, true);
4938 VM_WARN_ON_ONCE_FOLIO(!success, folio);
4943 static int scan_folios(struct lruvec *lruvec, struct scan_control *sc,
4944 int type, int tier, struct list_head *list)
4947 enum vm_event_item item;
4951 int remaining = MAX_LRU_BATCH;
4952 struct lru_gen_folio *lrugen = &lruvec->lrugen;
4953 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
4955 VM_WARN_ON_ONCE(!list_empty(list));
4957 if (get_nr_gens(lruvec, type) == MIN_NR_GENS)
4960 gen = lru_gen_from_seq(lrugen->min_seq[type]);
4962 for (zone = sc->reclaim_idx; zone >= 0; zone--) {
4965 struct list_head *head = &lrugen->folios[gen][type][zone];
4967 while (!list_empty(head)) {
4968 struct folio *folio = lru_to_folio(head);
4969 int delta = folio_nr_pages(folio);
4971 VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
4972 VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio);
4973 VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio);
4974 VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio);
4978 if (sort_folio(lruvec, folio, tier))
4980 else if (isolate_folio(lruvec, folio, sc)) {
4981 list_add(&folio->lru, list);
4984 list_move(&folio->lru, &moved);
4988 if (!--remaining || max(isolated, skipped) >= MIN_LRU_BATCH)
4993 list_splice(&moved, head);
4994 __count_zid_vm_events(PGSCAN_SKIP, zone, skipped);
4997 if (!remaining || isolated >= MIN_LRU_BATCH)
5001 item = PGSCAN_KSWAPD + reclaimer_offset();
5002 if (!cgroup_reclaim(sc)) {
5003 __count_vm_events(item, isolated);
5004 __count_vm_events(PGREFILL, sorted);
5006 __count_memcg_events(memcg, item, isolated);
5007 __count_memcg_events(memcg, PGREFILL, sorted);
5008 __count_vm_events(PGSCAN_ANON + type, isolated);
5011 * There might not be eligible folios due to reclaim_idx. Check the
5012 * remaining to prevent livelock if it's not making progress.
5014 return isolated || !remaining ? scanned : 0;
5017 static int get_tier_idx(struct lruvec *lruvec, int type)
5020 struct ctrl_pos sp, pv;
5023 * To leave a margin for fluctuations, use a larger gain factor (1:2).
5024 * This value is chosen because any other tier would have at least twice
5025 * as many refaults as the first tier.
5027 read_ctrl_pos(lruvec, type, 0, 1, &sp);
5028 for (tier = 1; tier < MAX_NR_TIERS; tier++) {
5029 read_ctrl_pos(lruvec, type, tier, 2, &pv);
5030 if (!positive_ctrl_err(&sp, &pv))
5037 static int get_type_to_scan(struct lruvec *lruvec, int swappiness, int *tier_idx)
5040 struct ctrl_pos sp, pv;
5041 int gain[ANON_AND_FILE] = { swappiness, 200 - swappiness };
5044 * Compare the first tier of anon with that of file to determine which
5045 * type to scan. Also need to compare other tiers of the selected type
5046 * with the first tier of the other type to determine the last tier (of
5047 * the selected type) to evict.
5049 read_ctrl_pos(lruvec, LRU_GEN_ANON, 0, gain[LRU_GEN_ANON], &sp);
5050 read_ctrl_pos(lruvec, LRU_GEN_FILE, 0, gain[LRU_GEN_FILE], &pv);
5051 type = positive_ctrl_err(&sp, &pv);
5053 read_ctrl_pos(lruvec, !type, 0, gain[!type], &sp);
5054 for (tier = 1; tier < MAX_NR_TIERS; tier++) {
5055 read_ctrl_pos(lruvec, type, tier, gain[type], &pv);
5056 if (!positive_ctrl_err(&sp, &pv))
5060 *tier_idx = tier - 1;
5065 static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness,
5066 int *type_scanned, struct list_head *list)
5072 DEFINE_MIN_SEQ(lruvec);
5075 * Try to make the obvious choice first. When anon and file are both
5076 * available from the same generation, interpret swappiness 1 as file
5077 * first and 200 as anon first.
5080 type = LRU_GEN_FILE;
5081 else if (min_seq[LRU_GEN_ANON] < min_seq[LRU_GEN_FILE])
5082 type = LRU_GEN_ANON;
5083 else if (swappiness == 1)
5084 type = LRU_GEN_FILE;
5085 else if (swappiness == 200)
5086 type = LRU_GEN_ANON;
5088 type = get_type_to_scan(lruvec, swappiness, &tier);
5090 for (i = !swappiness; i < ANON_AND_FILE; i++) {
5092 tier = get_tier_idx(lruvec, type);
5094 scanned = scan_folios(lruvec, sc, type, tier, list);
5102 *type_scanned = type;
5107 static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness)
5114 struct folio *folio;
5116 enum vm_event_item item;
5117 struct reclaim_stat stat;
5118 struct lru_gen_mm_walk *walk;
5119 bool skip_retry = false;
5120 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
5121 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
5123 spin_lock_irq(&lruvec->lru_lock);
5125 scanned = isolate_folios(lruvec, sc, swappiness, &type, &list);
5127 scanned += try_to_inc_min_seq(lruvec, swappiness);
5129 if (get_nr_gens(lruvec, !swappiness) == MIN_NR_GENS)
5132 spin_unlock_irq(&lruvec->lru_lock);
5134 if (list_empty(&list))
5137 reclaimed = shrink_folio_list(&list, pgdat, sc, &stat, false);
5138 sc->nr_reclaimed += reclaimed;
5140 list_for_each_entry_safe_reverse(folio, next, &list, lru) {
5141 if (!folio_evictable(folio)) {
5142 list_del(&folio->lru);
5143 folio_putback_lru(folio);
5147 if (folio_test_reclaim(folio) &&
5148 (folio_test_dirty(folio) || folio_test_writeback(folio))) {
5149 /* restore LRU_REFS_FLAGS cleared by isolate_folio() */
5150 if (folio_test_workingset(folio))
5151 folio_set_referenced(folio);
5155 if (skip_retry || folio_test_active(folio) || folio_test_referenced(folio) ||
5156 folio_mapped(folio) || folio_test_locked(folio) ||
5157 folio_test_dirty(folio) || folio_test_writeback(folio)) {
5158 /* don't add rejected folios to the oldest generation */
5159 set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS,
5164 /* retry folios that may have missed folio_rotate_reclaimable() */
5165 list_move(&folio->lru, &clean);
5166 sc->nr_scanned -= folio_nr_pages(folio);
5169 spin_lock_irq(&lruvec->lru_lock);
5171 move_folios_to_lru(lruvec, &list);
5173 walk = current->reclaim_state->mm_walk;
5174 if (walk && walk->batched)
5175 reset_batch_size(lruvec, walk);
5177 item = PGSTEAL_KSWAPD + reclaimer_offset();
5178 if (!cgroup_reclaim(sc))
5179 __count_vm_events(item, reclaimed);
5180 __count_memcg_events(memcg, item, reclaimed);
5181 __count_vm_events(PGSTEAL_ANON + type, reclaimed);
5183 spin_unlock_irq(&lruvec->lru_lock);
5185 mem_cgroup_uncharge_list(&list);
5186 free_unref_page_list(&list);
5188 INIT_LIST_HEAD(&list);
5189 list_splice_init(&clean, &list);
5191 if (!list_empty(&list)) {
5199 static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq,
5200 struct scan_control *sc, bool can_swap, unsigned long *nr_to_scan)
5202 int gen, type, zone;
5203 unsigned long old = 0;
5204 unsigned long young = 0;
5205 unsigned long total = 0;
5206 struct lru_gen_folio *lrugen = &lruvec->lrugen;
5207 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
5208 DEFINE_MIN_SEQ(lruvec);
5210 /* whether this lruvec is completely out of cold folios */
5211 if (min_seq[!can_swap] + MIN_NR_GENS > max_seq) {
5216 for (type = !can_swap; type < ANON_AND_FILE; type++) {
5219 for (seq = min_seq[type]; seq <= max_seq; seq++) {
5220 unsigned long size = 0;
5222 gen = lru_gen_from_seq(seq);
5224 for (zone = 0; zone < MAX_NR_ZONES; zone++)
5225 size += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L);
5230 else if (seq + MIN_NR_GENS == max_seq)
5235 /* try to scrape all its memory if this memcg was deleted */
5236 *nr_to_scan = mem_cgroup_online(memcg) ? (total >> sc->priority) : total;
5239 * The aging tries to be lazy to reduce the overhead, while the eviction
5240 * stalls when the number of generations reaches MIN_NR_GENS. Hence, the
5241 * ideal number of generations is MIN_NR_GENS+1.
5243 if (min_seq[!can_swap] + MIN_NR_GENS < max_seq)
5247 * It's also ideal to spread pages out evenly, i.e., 1/(MIN_NR_GENS+1)
5248 * of the total number of pages for each generation. A reasonable range
5249 * for this average portion is [1/MIN_NR_GENS, 1/(MIN_NR_GENS+2)]. The
5250 * aging cares about the upper bound of hot pages, while the eviction
5251 * cares about the lower bound of cold pages.
5253 if (young * MIN_NR_GENS > total)
5255 if (old * (MIN_NR_GENS + 2) < total)
5262 * For future optimizations:
5263 * 1. Defer try_to_inc_max_seq() to workqueues to reduce latency for memcg
5266 static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc, bool can_swap)
5268 unsigned long nr_to_scan;
5269 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
5270 DEFINE_MAX_SEQ(lruvec);
5272 if (mem_cgroup_below_min(sc->target_mem_cgroup, memcg))
5275 if (!should_run_aging(lruvec, max_seq, sc, can_swap, &nr_to_scan))
5278 /* skip the aging path at the default priority */
5279 if (sc->priority == DEF_PRIORITY)
5282 /* skip this lruvec as it's low on cold folios */
5283 return try_to_inc_max_seq(lruvec, max_seq, sc, can_swap, false) ? -1 : 0;
5286 static unsigned long get_nr_to_reclaim(struct scan_control *sc)
5288 /* don't abort memcg reclaim to ensure fairness */
5289 if (!global_reclaim(sc))
5292 return max(sc->nr_to_reclaim, compact_gap(sc->order));
5295 static bool try_to_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
5298 unsigned long scanned = 0;
5299 unsigned long nr_to_reclaim = get_nr_to_reclaim(sc);
5300 int swappiness = get_swappiness(lruvec, sc);
5302 /* clean file folios are more likely to exist */
5303 if (swappiness && !(sc->gfp_mask & __GFP_IO))
5309 nr_to_scan = get_nr_to_scan(lruvec, sc, swappiness);
5310 if (nr_to_scan <= 0)
5313 delta = evict_folios(lruvec, sc, swappiness);
5318 if (scanned >= nr_to_scan)
5321 if (sc->nr_reclaimed >= nr_to_reclaim)
5327 /* whether try_to_inc_max_seq() was successful */
5328 return nr_to_scan < 0;
5331 static int shrink_one(struct lruvec *lruvec, struct scan_control *sc)
5334 unsigned long scanned = sc->nr_scanned;
5335 unsigned long reclaimed = sc->nr_reclaimed;
5336 int seg = lru_gen_memcg_seg(lruvec);
5337 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
5338 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
5340 /* see the comment on MEMCG_NR_GENS */
5341 if (!lruvec_is_sizable(lruvec, sc))
5342 return seg != MEMCG_LRU_TAIL ? MEMCG_LRU_TAIL : MEMCG_LRU_YOUNG;
5344 mem_cgroup_calculate_protection(NULL, memcg);
5346 if (mem_cgroup_below_min(NULL, memcg))
5347 return MEMCG_LRU_YOUNG;
5349 if (mem_cgroup_below_low(NULL, memcg)) {
5350 /* see the comment on MEMCG_NR_GENS */
5351 if (seg != MEMCG_LRU_TAIL)
5352 return MEMCG_LRU_TAIL;
5354 memcg_memory_event(memcg, MEMCG_LOW);
5357 success = try_to_shrink_lruvec(lruvec, sc);
5359 shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, sc->priority);
5362 vmpressure(sc->gfp_mask, memcg, false, sc->nr_scanned - scanned,
5363 sc->nr_reclaimed - reclaimed);
5365 sc->nr_reclaimed += current->reclaim_state->reclaimed_slab;
5366 current->reclaim_state->reclaimed_slab = 0;
5368 return success ? MEMCG_LRU_YOUNG : 0;
5373 static void shrink_many(struct pglist_data *pgdat, struct scan_control *sc)
5379 struct lruvec *lruvec;
5380 struct lru_gen_folio *lrugen;
5381 struct mem_cgroup *memcg;
5382 const struct hlist_nulls_node *pos;
5383 unsigned long nr_to_reclaim = get_nr_to_reclaim(sc);
5385 bin = first_bin = get_random_u32_below(MEMCG_NR_BINS);
5389 gen = get_memcg_gen(READ_ONCE(pgdat->memcg_lru.seq));
5393 hlist_nulls_for_each_entry_rcu(lrugen, pos, &pgdat->memcg_lru.fifo[gen][bin], list) {
5395 lru_gen_rotate_memcg(lruvec, op);
5397 mem_cgroup_put(memcg);
5399 lruvec = container_of(lrugen, struct lruvec, lrugen);
5400 memcg = lruvec_memcg(lruvec);
5402 if (!mem_cgroup_tryget(memcg)) {
5410 op = shrink_one(lruvec, sc);
5414 if (sc->nr_reclaimed >= nr_to_reclaim)
5421 lru_gen_rotate_memcg(lruvec, op);
5423 mem_cgroup_put(memcg);
5425 if (sc->nr_reclaimed >= nr_to_reclaim)
5428 /* restart if raced with lru_gen_rotate_memcg() */
5429 if (gen != get_nulls_value(pos))
5432 /* try the rest of the bins of the current generation */
5433 bin = get_memcg_bin(bin + 1);
5434 if (bin != first_bin)
5438 static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
5440 struct blk_plug plug;
5442 VM_WARN_ON_ONCE(global_reclaim(sc));
5443 VM_WARN_ON_ONCE(!sc->may_writepage || !sc->may_unmap);
5447 blk_start_plug(&plug);
5449 set_mm_walk(NULL, sc->proactive);
5451 if (try_to_shrink_lruvec(lruvec, sc))
5452 lru_gen_rotate_memcg(lruvec, MEMCG_LRU_YOUNG);
5456 blk_finish_plug(&plug);
5459 #else /* !CONFIG_MEMCG */
5461 static void shrink_many(struct pglist_data *pgdat, struct scan_control *sc)
5466 static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
5473 static void set_initial_priority(struct pglist_data *pgdat, struct scan_control *sc)
5476 unsigned long reclaimable;
5477 struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat);
5479 if (sc->priority != DEF_PRIORITY || sc->nr_to_reclaim < MIN_LRU_BATCH)
5482 * Determine the initial priority based on ((total / MEMCG_NR_GENS) >>
5483 * priority) * reclaimed_to_scanned_ratio = nr_to_reclaim, where the
5484 * estimated reclaimed_to_scanned_ratio = inactive / total.
5486 reclaimable = node_page_state(pgdat, NR_INACTIVE_FILE);
5487 if (get_swappiness(lruvec, sc))
5488 reclaimable += node_page_state(pgdat, NR_INACTIVE_ANON);
5490 reclaimable /= MEMCG_NR_GENS;
5492 /* round down reclaimable and round up sc->nr_to_reclaim */
5493 priority = fls_long(reclaimable) - 1 - fls_long(sc->nr_to_reclaim - 1);
5495 sc->priority = clamp(priority, 0, DEF_PRIORITY);
5498 static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *sc)
5500 struct blk_plug plug;
5501 unsigned long reclaimed = sc->nr_reclaimed;
5503 VM_WARN_ON_ONCE(!global_reclaim(sc));
5506 * Unmapped clean folios are already prioritized. Scanning for more of
5507 * them is likely futile and can cause high reclaim latency when there
5508 * is a large number of memcgs.
5510 if (!sc->may_writepage || !sc->may_unmap)
5515 blk_start_plug(&plug);
5517 set_mm_walk(pgdat, sc->proactive);
5519 set_initial_priority(pgdat, sc);
5521 if (current_is_kswapd())
5522 sc->nr_reclaimed = 0;
5524 if (mem_cgroup_disabled())
5525 shrink_one(&pgdat->__lruvec, sc);
5527 shrink_many(pgdat, sc);
5529 if (current_is_kswapd())
5530 sc->nr_reclaimed += reclaimed;
5534 blk_finish_plug(&plug);
5536 /* kswapd should never fail */
5537 pgdat->kswapd_failures = 0;
5540 /******************************************************************************
5542 ******************************************************************************/
5544 static bool __maybe_unused state_is_valid(struct lruvec *lruvec)
5546 struct lru_gen_folio *lrugen = &lruvec->lrugen;
5548 if (lrugen->enabled) {
5551 for_each_evictable_lru(lru) {
5552 if (!list_empty(&lruvec->lists[lru]))
5556 int gen, type, zone;
5558 for_each_gen_type_zone(gen, type, zone) {
5559 if (!list_empty(&lrugen->folios[gen][type][zone]))
5567 static bool fill_evictable(struct lruvec *lruvec)
5570 int remaining = MAX_LRU_BATCH;
5572 for_each_evictable_lru(lru) {
5573 int type = is_file_lru(lru);
5574 bool active = is_active_lru(lru);
5575 struct list_head *head = &lruvec->lists[lru];
5577 while (!list_empty(head)) {
5579 struct folio *folio = lru_to_folio(head);
5581 VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
5582 VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio) != active, folio);
5583 VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio);
5584 VM_WARN_ON_ONCE_FOLIO(folio_lru_gen(folio) != -1, folio);
5586 lruvec_del_folio(lruvec, folio);
5587 success = lru_gen_add_folio(lruvec, folio, false);
5588 VM_WARN_ON_ONCE(!success);
5598 static bool drain_evictable(struct lruvec *lruvec)
5600 int gen, type, zone;
5601 int remaining = MAX_LRU_BATCH;
5603 for_each_gen_type_zone(gen, type, zone) {
5604 struct list_head *head = &lruvec->lrugen.folios[gen][type][zone];
5606 while (!list_empty(head)) {
5608 struct folio *folio = lru_to_folio(head);
5610 VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
5611 VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio);
5612 VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio);
5613 VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio);
5615 success = lru_gen_del_folio(lruvec, folio, false);
5616 VM_WARN_ON_ONCE(!success);
5617 lruvec_add_folio(lruvec, folio);
5627 static void lru_gen_change_state(bool enabled)
5629 static DEFINE_MUTEX(state_mutex);
5631 struct mem_cgroup *memcg;
5636 mutex_lock(&state_mutex);
5638 if (enabled == lru_gen_enabled())
5642 static_branch_enable_cpuslocked(&lru_gen_caps[LRU_GEN_CORE]);
5644 static_branch_disable_cpuslocked(&lru_gen_caps[LRU_GEN_CORE]);
5646 memcg = mem_cgroup_iter(NULL, NULL, NULL);
5650 for_each_node(nid) {
5651 struct lruvec *lruvec = get_lruvec(memcg, nid);
5653 spin_lock_irq(&lruvec->lru_lock);
5655 VM_WARN_ON_ONCE(!seq_is_valid(lruvec));
5656 VM_WARN_ON_ONCE(!state_is_valid(lruvec));
5658 lruvec->lrugen.enabled = enabled;
5660 while (!(enabled ? fill_evictable(lruvec) : drain_evictable(lruvec))) {
5661 spin_unlock_irq(&lruvec->lru_lock);
5663 spin_lock_irq(&lruvec->lru_lock);
5666 spin_unlock_irq(&lruvec->lru_lock);
5670 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)));
5672 mutex_unlock(&state_mutex);
5678 /******************************************************************************
5680 ******************************************************************************/
5682 static ssize_t min_ttl_ms_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
5684 return sysfs_emit(buf, "%u\n", jiffies_to_msecs(READ_ONCE(lru_gen_min_ttl)));
5687 /* see Documentation/admin-guide/mm/multigen_lru.rst for details */
5688 static ssize_t min_ttl_ms_store(struct kobject *kobj, struct kobj_attribute *attr,
5689 const char *buf, size_t len)
5693 if (kstrtouint(buf, 0, &msecs))
5696 WRITE_ONCE(lru_gen_min_ttl, msecs_to_jiffies(msecs));
5701 static struct kobj_attribute lru_gen_min_ttl_attr = __ATTR_RW(min_ttl_ms);
5703 static ssize_t enabled_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
5705 unsigned int caps = 0;
5707 if (get_cap(LRU_GEN_CORE))
5708 caps |= BIT(LRU_GEN_CORE);
5710 if (arch_has_hw_pte_young() && get_cap(LRU_GEN_MM_WALK))
5711 caps |= BIT(LRU_GEN_MM_WALK);
5713 if (arch_has_hw_nonleaf_pmd_young() && get_cap(LRU_GEN_NONLEAF_YOUNG))
5714 caps |= BIT(LRU_GEN_NONLEAF_YOUNG);
5716 return sysfs_emit(buf, "0x%04x\n", caps);
5719 /* see Documentation/admin-guide/mm/multigen_lru.rst for details */
5720 static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
5721 const char *buf, size_t len)
5726 if (tolower(*buf) == 'n')
5728 else if (tolower(*buf) == 'y')
5730 else if (kstrtouint(buf, 0, &caps))
5733 for (i = 0; i < NR_LRU_GEN_CAPS; i++) {
5734 bool enabled = caps & BIT(i);
5736 if (i == LRU_GEN_CORE)
5737 lru_gen_change_state(enabled);
5739 static_branch_enable(&lru_gen_caps[i]);
5741 static_branch_disable(&lru_gen_caps[i]);
5747 static struct kobj_attribute lru_gen_enabled_attr = __ATTR_RW(enabled);
5749 static struct attribute *lru_gen_attrs[] = {
5750 &lru_gen_min_ttl_attr.attr,
5751 &lru_gen_enabled_attr.attr,
5755 static const struct attribute_group lru_gen_attr_group = {
5757 .attrs = lru_gen_attrs,
5760 /******************************************************************************
5762 ******************************************************************************/
5764 static void *lru_gen_seq_start(struct seq_file *m, loff_t *pos)
5766 struct mem_cgroup *memcg;
5767 loff_t nr_to_skip = *pos;
5769 m->private = kvmalloc(PATH_MAX, GFP_KERNEL);
5771 return ERR_PTR(-ENOMEM);
5773 memcg = mem_cgroup_iter(NULL, NULL, NULL);
5777 for_each_node_state(nid, N_MEMORY) {
5779 return get_lruvec(memcg, nid);
5781 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)));
5786 static void lru_gen_seq_stop(struct seq_file *m, void *v)
5788 if (!IS_ERR_OR_NULL(v))
5789 mem_cgroup_iter_break(NULL, lruvec_memcg(v));
5795 static void *lru_gen_seq_next(struct seq_file *m, void *v, loff_t *pos)
5797 int nid = lruvec_pgdat(v)->node_id;
5798 struct mem_cgroup *memcg = lruvec_memcg(v);
5802 nid = next_memory_node(nid);
5803 if (nid == MAX_NUMNODES) {
5804 memcg = mem_cgroup_iter(NULL, memcg, NULL);
5808 nid = first_memory_node;
5811 return get_lruvec(memcg, nid);
5814 static void lru_gen_seq_show_full(struct seq_file *m, struct lruvec *lruvec,
5815 unsigned long max_seq, unsigned long *min_seq,
5820 int hist = lru_hist_from_seq(seq);
5821 struct lru_gen_folio *lrugen = &lruvec->lrugen;
5823 for (tier = 0; tier < MAX_NR_TIERS; tier++) {
5824 seq_printf(m, " %10d", tier);
5825 for (type = 0; type < ANON_AND_FILE; type++) {
5826 const char *s = " ";
5827 unsigned long n[3] = {};
5829 if (seq == max_seq) {
5831 n[0] = READ_ONCE(lrugen->avg_refaulted[type][tier]);
5832 n[1] = READ_ONCE(lrugen->avg_total[type][tier]);
5833 } else if (seq == min_seq[type] || NR_HIST_GENS > 1) {
5835 n[0] = atomic_long_read(&lrugen->refaulted[hist][type][tier]);
5836 n[1] = atomic_long_read(&lrugen->evicted[hist][type][tier]);
5838 n[2] = READ_ONCE(lrugen->protected[hist][type][tier - 1]);
5841 for (i = 0; i < 3; i++)
5842 seq_printf(m, " %10lu%c", n[i], s[i]);
5848 for (i = 0; i < NR_MM_STATS; i++) {
5849 const char *s = " ";
5850 unsigned long n = 0;
5852 if (seq == max_seq && NR_HIST_GENS == 1) {
5854 n = READ_ONCE(lruvec->mm_state.stats[hist][i]);
5855 } else if (seq != max_seq && NR_HIST_GENS > 1) {
5857 n = READ_ONCE(lruvec->mm_state.stats[hist][i]);
5860 seq_printf(m, " %10lu%c", n, s[i]);
5865 /* see Documentation/admin-guide/mm/multigen_lru.rst for details */
5866 static int lru_gen_seq_show(struct seq_file *m, void *v)
5869 bool full = !debugfs_real_fops(m->file)->write;
5870 struct lruvec *lruvec = v;
5871 struct lru_gen_folio *lrugen = &lruvec->lrugen;
5872 int nid = lruvec_pgdat(lruvec)->node_id;
5873 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
5874 DEFINE_MAX_SEQ(lruvec);
5875 DEFINE_MIN_SEQ(lruvec);
5877 if (nid == first_memory_node) {
5878 const char *path = memcg ? m->private : "";
5882 cgroup_path(memcg->css.cgroup, m->private, PATH_MAX);
5884 seq_printf(m, "memcg %5hu %s\n", mem_cgroup_id(memcg), path);
5887 seq_printf(m, " node %5d\n", nid);
5890 seq = min_seq[LRU_GEN_ANON];
5891 else if (max_seq >= MAX_NR_GENS)
5892 seq = max_seq - MAX_NR_GENS + 1;
5896 for (; seq <= max_seq; seq++) {
5898 int gen = lru_gen_from_seq(seq);
5899 unsigned long birth = READ_ONCE(lruvec->lrugen.timestamps[gen]);
5901 seq_printf(m, " %10lu %10u", seq, jiffies_to_msecs(jiffies - birth));
5903 for (type = 0; type < ANON_AND_FILE; type++) {
5904 unsigned long size = 0;
5905 char mark = full && seq < min_seq[type] ? 'x' : ' ';
5907 for (zone = 0; zone < MAX_NR_ZONES; zone++)
5908 size += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L);
5910 seq_printf(m, " %10lu%c", size, mark);
5916 lru_gen_seq_show_full(m, lruvec, max_seq, min_seq, seq);
5922 static const struct seq_operations lru_gen_seq_ops = {
5923 .start = lru_gen_seq_start,
5924 .stop = lru_gen_seq_stop,
5925 .next = lru_gen_seq_next,
5926 .show = lru_gen_seq_show,
5929 static int run_aging(struct lruvec *lruvec, unsigned long seq, struct scan_control *sc,
5930 bool can_swap, bool force_scan)
5932 DEFINE_MAX_SEQ(lruvec);
5933 DEFINE_MIN_SEQ(lruvec);
5941 if (!force_scan && min_seq[!can_swap] + MAX_NR_GENS - 1 <= max_seq)
5944 try_to_inc_max_seq(lruvec, max_seq, sc, can_swap, force_scan);
5949 static int run_eviction(struct lruvec *lruvec, unsigned long seq, struct scan_control *sc,
5950 int swappiness, unsigned long nr_to_reclaim)
5952 DEFINE_MAX_SEQ(lruvec);
5954 if (seq + MIN_NR_GENS > max_seq)
5957 sc->nr_reclaimed = 0;
5959 while (!signal_pending(current)) {
5960 DEFINE_MIN_SEQ(lruvec);
5962 if (seq < min_seq[!swappiness])
5965 if (sc->nr_reclaimed >= nr_to_reclaim)
5968 if (!evict_folios(lruvec, sc, swappiness))
5977 static int run_cmd(char cmd, int memcg_id, int nid, unsigned long seq,
5978 struct scan_control *sc, int swappiness, unsigned long opt)
5980 struct lruvec *lruvec;
5982 struct mem_cgroup *memcg = NULL;
5984 if (nid < 0 || nid >= MAX_NUMNODES || !node_state(nid, N_MEMORY))
5987 if (!mem_cgroup_disabled()) {
5990 memcg = mem_cgroup_from_id(memcg_id);
5991 if (!mem_cgroup_tryget(memcg))
6000 if (memcg_id != mem_cgroup_id(memcg))
6003 lruvec = get_lruvec(memcg, nid);
6006 swappiness = get_swappiness(lruvec, sc);
6007 else if (swappiness > 200)
6012 err = run_aging(lruvec, seq, sc, swappiness, opt);
6015 err = run_eviction(lruvec, seq, sc, swappiness, opt);
6019 mem_cgroup_put(memcg);
6024 /* see Documentation/admin-guide/mm/multigen_lru.rst for details */
6025 static ssize_t lru_gen_seq_write(struct file *file, const char __user *src,
6026 size_t len, loff_t *pos)
6031 struct blk_plug plug;
6033 struct scan_control sc = {
6034 .may_writepage = true,
6037 .reclaim_idx = MAX_NR_ZONES - 1,
6038 .gfp_mask = GFP_KERNEL,
6041 buf = kvmalloc(len + 1, GFP_KERNEL);
6045 if (copy_from_user(buf, src, len)) {
6050 set_task_reclaim_state(current, &sc.reclaim_state);
6051 flags = memalloc_noreclaim_save();
6052 blk_start_plug(&plug);
6053 if (!set_mm_walk(NULL, true)) {
6061 while ((cur = strsep(&next, ",;\n"))) {
6065 unsigned int memcg_id;
6068 unsigned int swappiness = -1;
6069 unsigned long opt = -1;
6071 cur = skip_spaces(cur);
6075 n = sscanf(cur, "%c %u %u %lu %n %u %n %lu %n", &cmd, &memcg_id, &nid,
6076 &seq, &end, &swappiness, &end, &opt, &end);
6077 if (n < 4 || cur[end]) {
6082 err = run_cmd(cmd, memcg_id, nid, seq, &sc, swappiness, opt);
6088 blk_finish_plug(&plug);
6089 memalloc_noreclaim_restore(flags);
6090 set_task_reclaim_state(current, NULL);
6097 static int lru_gen_seq_open(struct inode *inode, struct file *file)
6099 return seq_open(file, &lru_gen_seq_ops);
6102 static const struct file_operations lru_gen_rw_fops = {
6103 .open = lru_gen_seq_open,
6105 .write = lru_gen_seq_write,
6106 .llseek = seq_lseek,
6107 .release = seq_release,
6110 static const struct file_operations lru_gen_ro_fops = {
6111 .open = lru_gen_seq_open,
6113 .llseek = seq_lseek,
6114 .release = seq_release,
6117 /******************************************************************************
6119 ******************************************************************************/
6121 void lru_gen_init_lruvec(struct lruvec *lruvec)
6124 int gen, type, zone;
6125 struct lru_gen_folio *lrugen = &lruvec->lrugen;
6127 lrugen->max_seq = MIN_NR_GENS + 1;
6128 lrugen->enabled = lru_gen_enabled();
6130 for (i = 0; i <= MIN_NR_GENS + 1; i++)
6131 lrugen->timestamps[i] = jiffies;
6133 for_each_gen_type_zone(gen, type, zone)
6134 INIT_LIST_HEAD(&lrugen->folios[gen][type][zone]);
6136 lruvec->mm_state.seq = MIN_NR_GENS;
6137 init_waitqueue_head(&lruvec->mm_state.wait);
6142 void lru_gen_init_pgdat(struct pglist_data *pgdat)
6146 spin_lock_init(&pgdat->memcg_lru.lock);
6148 for (i = 0; i < MEMCG_NR_GENS; i++) {
6149 for (j = 0; j < MEMCG_NR_BINS; j++)
6150 INIT_HLIST_NULLS_HEAD(&pgdat->memcg_lru.fifo[i][j], i);
6154 void lru_gen_init_memcg(struct mem_cgroup *memcg)
6156 INIT_LIST_HEAD(&memcg->mm_list.fifo);
6157 spin_lock_init(&memcg->mm_list.lock);
6160 void lru_gen_exit_memcg(struct mem_cgroup *memcg)
6165 VM_WARN_ON_ONCE(!list_empty(&memcg->mm_list.fifo));
6167 for_each_node(nid) {
6168 struct lruvec *lruvec = get_lruvec(memcg, nid);
6170 VM_WARN_ON_ONCE(lruvec->mm_state.nr_walkers);
6171 VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0,
6172 sizeof(lruvec->lrugen.nr_pages)));
6174 lruvec->lrugen.list.next = LIST_POISON1;
6176 for (i = 0; i < NR_BLOOM_FILTERS; i++) {
6177 bitmap_free(lruvec->mm_state.filters[i]);
6178 lruvec->mm_state.filters[i] = NULL;
6183 #endif /* CONFIG_MEMCG */
6185 static int __init init_lru_gen(void)
6187 BUILD_BUG_ON(MIN_NR_GENS + 1 >= MAX_NR_GENS);
6188 BUILD_BUG_ON(BIT(LRU_GEN_WIDTH) <= MAX_NR_GENS);
6190 if (sysfs_create_group(mm_kobj, &lru_gen_attr_group))
6191 pr_err("lru_gen: failed to create sysfs group\n");
6193 debugfs_create_file("lru_gen", 0644, NULL, NULL, &lru_gen_rw_fops);
6194 debugfs_create_file("lru_gen_full", 0444, NULL, NULL, &lru_gen_ro_fops);
6198 late_initcall(init_lru_gen);
6200 #else /* !CONFIG_LRU_GEN */
6202 static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc)
6206 static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
6210 static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *sc)
6214 #endif /* CONFIG_LRU_GEN */
6216 static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
6218 unsigned long nr[NR_LRU_LISTS];
6219 unsigned long targets[NR_LRU_LISTS];
6220 unsigned long nr_to_scan;
6222 unsigned long nr_reclaimed = 0;
6223 unsigned long nr_to_reclaim = sc->nr_to_reclaim;
6224 bool proportional_reclaim;
6225 struct blk_plug plug;
6227 if (lru_gen_enabled() && !global_reclaim(sc)) {
6228 lru_gen_shrink_lruvec(lruvec, sc);
6232 get_scan_count(lruvec, sc, nr);
6234 /* Record the original scan target for proportional adjustments later */
6235 memcpy(targets, nr, sizeof(nr));
6238 * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal
6239 * event that can occur when there is little memory pressure e.g.
6240 * multiple streaming readers/writers. Hence, we do not abort scanning
6241 * when the requested number of pages are reclaimed when scanning at
6242 * DEF_PRIORITY on the assumption that the fact we are direct
6243 * reclaiming implies that kswapd is not keeping up and it is best to
6244 * do a batch of work at once. For memcg reclaim one check is made to
6245 * abort proportional reclaim if either the file or anon lru has already
6246 * dropped to zero at the first pass.
6248 proportional_reclaim = (!cgroup_reclaim(sc) && !current_is_kswapd() &&
6249 sc->priority == DEF_PRIORITY);
6251 blk_start_plug(&plug);
6252 while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
6253 nr[LRU_INACTIVE_FILE]) {
6254 unsigned long nr_anon, nr_file, percentage;
6255 unsigned long nr_scanned;
6257 for_each_evictable_lru(lru) {
6259 nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX);
6260 nr[lru] -= nr_to_scan;
6262 nr_reclaimed += shrink_list(lru, nr_to_scan,
6269 if (nr_reclaimed < nr_to_reclaim || proportional_reclaim)
6273 * For kswapd and memcg, reclaim at least the number of pages
6274 * requested. Ensure that the anon and file LRUs are scanned
6275 * proportionally what was requested by get_scan_count(). We
6276 * stop reclaiming one LRU and reduce the amount scanning
6277 * proportional to the original scan target.
6279 nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE];
6280 nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON];
6283 * It's just vindictive to attack the larger once the smaller
6284 * has gone to zero. And given the way we stop scanning the
6285 * smaller below, this makes sure that we only make one nudge
6286 * towards proportionality once we've got nr_to_reclaim.
6288 if (!nr_file || !nr_anon)
6291 if (nr_file > nr_anon) {
6292 unsigned long scan_target = targets[LRU_INACTIVE_ANON] +
6293 targets[LRU_ACTIVE_ANON] + 1;
6295 percentage = nr_anon * 100 / scan_target;
6297 unsigned long scan_target = targets[LRU_INACTIVE_FILE] +
6298 targets[LRU_ACTIVE_FILE] + 1;
6300 percentage = nr_file * 100 / scan_target;
6303 /* Stop scanning the smaller of the LRU */
6305 nr[lru + LRU_ACTIVE] = 0;
6308 * Recalculate the other LRU scan count based on its original
6309 * scan target and the percentage scanning already complete
6311 lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE;
6312 nr_scanned = targets[lru] - nr[lru];
6313 nr[lru] = targets[lru] * (100 - percentage) / 100;
6314 nr[lru] -= min(nr[lru], nr_scanned);
6317 nr_scanned = targets[lru] - nr[lru];
6318 nr[lru] = targets[lru] * (100 - percentage) / 100;
6319 nr[lru] -= min(nr[lru], nr_scanned);
6321 blk_finish_plug(&plug);
6322 sc->nr_reclaimed += nr_reclaimed;
6325 * Even if we did not try to evict anon pages at all, we want to
6326 * rebalance the anon lru active/inactive ratio.
6328 if (can_age_anon_pages(lruvec_pgdat(lruvec), sc) &&
6329 inactive_is_low(lruvec, LRU_INACTIVE_ANON))
6330 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
6331 sc, LRU_ACTIVE_ANON);
6334 /* Use reclaim/compaction for costly allocs or under memory pressure */
6335 static bool in_reclaim_compaction(struct scan_control *sc)
6337 if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
6338 (sc->order > PAGE_ALLOC_COSTLY_ORDER ||
6339 sc->priority < DEF_PRIORITY - 2))
6346 * Reclaim/compaction is used for high-order allocation requests. It reclaims
6347 * order-0 pages before compacting the zone. should_continue_reclaim() returns
6348 * true if more pages should be reclaimed such that when the page allocator
6349 * calls try_to_compact_pages() that it will have enough free pages to succeed.
6350 * It will give up earlier than that if there is difficulty reclaiming pages.
6352 static inline bool should_continue_reclaim(struct pglist_data *pgdat,
6353 unsigned long nr_reclaimed,
6354 struct scan_control *sc)
6356 unsigned long pages_for_compaction;
6357 unsigned long inactive_lru_pages;
6360 /* If not in reclaim/compaction mode, stop */
6361 if (!in_reclaim_compaction(sc))
6365 * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX
6366 * number of pages that were scanned. This will return to the caller
6367 * with the risk reclaim/compaction and the resulting allocation attempt
6368 * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL
6369 * allocations through requiring that the full LRU list has been scanned
6370 * first, by assuming that zero delta of sc->nr_scanned means full LRU
6371 * scan, but that approximation was wrong, and there were corner cases
6372 * where always a non-zero amount of pages were scanned.
6377 /* If compaction would go ahead or the allocation would succeed, stop */
6378 for (z = 0; z <= sc->reclaim_idx; z++) {
6379 struct zone *zone = &pgdat->node_zones[z];
6380 if (!managed_zone(zone))
6383 switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) {
6384 case COMPACT_SUCCESS:
6385 case COMPACT_CONTINUE:
6388 /* check next zone */
6394 * If we have not reclaimed enough pages for compaction and the
6395 * inactive lists are large enough, continue reclaiming
6397 pages_for_compaction = compact_gap(sc->order);
6398 inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE);
6399 if (can_reclaim_anon_pages(NULL, pgdat->node_id, sc))
6400 inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON);
6402 return inactive_lru_pages > pages_for_compaction;
6405 static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc)
6407 struct mem_cgroup *target_memcg = sc->target_mem_cgroup;
6408 struct mem_cgroup *memcg;
6410 memcg = mem_cgroup_iter(target_memcg, NULL, NULL);
6412 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
6413 unsigned long reclaimed;
6414 unsigned long scanned;
6417 * This loop can become CPU-bound when target memcgs
6418 * aren't eligible for reclaim - either because they
6419 * don't have any reclaimable pages, or because their
6420 * memory is explicitly protected. Avoid soft lockups.
6424 mem_cgroup_calculate_protection(target_memcg, memcg);
6426 if (mem_cgroup_below_min(target_memcg, memcg)) {
6429 * If there is no reclaimable memory, OOM.
6432 } else if (mem_cgroup_below_low(target_memcg, memcg)) {
6435 * Respect the protection only as long as
6436 * there is an unprotected supply
6437 * of reclaimable memory from other cgroups.
6439 if (!sc->memcg_low_reclaim) {
6440 sc->memcg_low_skipped = 1;
6443 memcg_memory_event(memcg, MEMCG_LOW);
6446 reclaimed = sc->nr_reclaimed;
6447 scanned = sc->nr_scanned;
6449 shrink_lruvec(lruvec, sc);
6451 shrink_slab(sc->gfp_mask, pgdat->node_id, memcg,
6454 /* Record the group's reclaim efficiency */
6456 vmpressure(sc->gfp_mask, memcg, false,
6457 sc->nr_scanned - scanned,
6458 sc->nr_reclaimed - reclaimed);
6460 } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL)));
6463 static void shrink_node(pg_data_t *pgdat, struct scan_control *sc)
6465 struct reclaim_state *reclaim_state = current->reclaim_state;
6466 unsigned long nr_reclaimed, nr_scanned;
6467 struct lruvec *target_lruvec;
6468 bool reclaimable = false;
6470 if (lru_gen_enabled() && global_reclaim(sc)) {
6471 lru_gen_shrink_node(pgdat, sc);
6475 target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat);
6478 memset(&sc->nr, 0, sizeof(sc->nr));
6480 nr_reclaimed = sc->nr_reclaimed;
6481 nr_scanned = sc->nr_scanned;
6483 prepare_scan_count(pgdat, sc);
6485 shrink_node_memcgs(pgdat, sc);
6487 if (reclaim_state) {
6488 sc->nr_reclaimed += reclaim_state->reclaimed_slab;
6489 reclaim_state->reclaimed_slab = 0;
6492 /* Record the subtree's reclaim efficiency */
6494 vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true,
6495 sc->nr_scanned - nr_scanned,
6496 sc->nr_reclaimed - nr_reclaimed);
6498 if (sc->nr_reclaimed - nr_reclaimed)
6501 if (current_is_kswapd()) {
6503 * If reclaim is isolating dirty pages under writeback,
6504 * it implies that the long-lived page allocation rate
6505 * is exceeding the page laundering rate. Either the
6506 * global limits are not being effective at throttling
6507 * processes due to the page distribution throughout
6508 * zones or there is heavy usage of a slow backing
6509 * device. The only option is to throttle from reclaim
6510 * context which is not ideal as there is no guarantee
6511 * the dirtying process is throttled in the same way
6512 * balance_dirty_pages() manages.
6514 * Once a node is flagged PGDAT_WRITEBACK, kswapd will
6515 * count the number of pages under pages flagged for
6516 * immediate reclaim and stall if any are encountered
6517 * in the nr_immediate check below.
6519 if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken)
6520 set_bit(PGDAT_WRITEBACK, &pgdat->flags);
6522 /* Allow kswapd to start writing pages during reclaim.*/
6523 if (sc->nr.unqueued_dirty == sc->nr.file_taken)
6524 set_bit(PGDAT_DIRTY, &pgdat->flags);
6527 * If kswapd scans pages marked for immediate
6528 * reclaim and under writeback (nr_immediate), it
6529 * implies that pages are cycling through the LRU
6530 * faster than they are written so forcibly stall
6531 * until some pages complete writeback.
6533 if (sc->nr.immediate)
6534 reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK);
6538 * Tag a node/memcg as congested if all the dirty pages were marked
6539 * for writeback and immediate reclaim (counted in nr.congested).
6541 * Legacy memcg will stall in page writeback so avoid forcibly
6542 * stalling in reclaim_throttle().
6544 if ((current_is_kswapd() ||
6545 (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) &&
6546 sc->nr.dirty && sc->nr.dirty == sc->nr.congested)
6547 set_bit(LRUVEC_CONGESTED, &target_lruvec->flags);
6550 * Stall direct reclaim for IO completions if the lruvec is
6551 * node is congested. Allow kswapd to continue until it
6552 * starts encountering unqueued dirty pages or cycling through
6553 * the LRU too quickly.
6555 if (!current_is_kswapd() && current_may_throttle() &&
6556 !sc->hibernation_mode &&
6557 test_bit(LRUVEC_CONGESTED, &target_lruvec->flags))
6558 reclaim_throttle(pgdat, VMSCAN_THROTTLE_CONGESTED);
6560 if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed,
6565 * Kswapd gives up on balancing particular nodes after too
6566 * many failures to reclaim anything from them and goes to
6567 * sleep. On reclaim progress, reset the failure counter. A
6568 * successful direct reclaim run will revive a dormant kswapd.
6571 pgdat->kswapd_failures = 0;
6575 * Returns true if compaction should go ahead for a costly-order request, or
6576 * the allocation would already succeed without compaction. Return false if we
6577 * should reclaim first.
6579 static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
6581 unsigned long watermark;
6582 enum compact_result suitable;
6584 suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx);
6585 if (suitable == COMPACT_SUCCESS)
6586 /* Allocation should succeed already. Don't reclaim. */
6588 if (suitable == COMPACT_SKIPPED)
6589 /* Compaction cannot yet proceed. Do reclaim. */
6593 * Compaction is already possible, but it takes time to run and there
6594 * are potentially other callers using the pages just freed. So proceed
6595 * with reclaim to make a buffer of free pages available to give
6596 * compaction a reasonable chance of completing and allocating the page.
6597 * Note that we won't actually reclaim the whole buffer in one attempt
6598 * as the target watermark in should_continue_reclaim() is lower. But if
6599 * we are already above the high+gap watermark, don't reclaim at all.
6601 watermark = high_wmark_pages(zone) + compact_gap(sc->order);
6603 return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx);
6606 static void consider_reclaim_throttle(pg_data_t *pgdat, struct scan_control *sc)
6609 * If reclaim is making progress greater than 12% efficiency then
6610 * wake all the NOPROGRESS throttled tasks.
6612 if (sc->nr_reclaimed > (sc->nr_scanned >> 3)) {
6613 wait_queue_head_t *wqh;
6615 wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_NOPROGRESS];
6616 if (waitqueue_active(wqh))
6623 * Do not throttle kswapd or cgroup reclaim on NOPROGRESS as it will
6624 * throttle on VMSCAN_THROTTLE_WRITEBACK if there are too many pages
6625 * under writeback and marked for immediate reclaim at the tail of the
6628 if (current_is_kswapd() || cgroup_reclaim(sc))
6631 /* Throttle if making no progress at high prioities. */
6632 if (sc->priority == 1 && !sc->nr_reclaimed)
6633 reclaim_throttle(pgdat, VMSCAN_THROTTLE_NOPROGRESS);
6637 * This is the direct reclaim path, for page-allocating processes. We only
6638 * try to reclaim pages from zones which will satisfy the caller's allocation
6641 * If a zone is deemed to be full of pinned pages then just give it a light
6642 * scan then give up on it.
6644 static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
6648 unsigned long nr_soft_reclaimed;
6649 unsigned long nr_soft_scanned;
6651 pg_data_t *last_pgdat = NULL;
6652 pg_data_t *first_pgdat = NULL;
6655 * If the number of buffer_heads in the machine exceeds the maximum
6656 * allowed level, force direct reclaim to scan the highmem zone as
6657 * highmem pages could be pinning lowmem pages storing buffer_heads
6659 orig_mask = sc->gfp_mask;
6660 if (buffer_heads_over_limit) {
6661 sc->gfp_mask |= __GFP_HIGHMEM;
6662 sc->reclaim_idx = gfp_zone(sc->gfp_mask);
6665 for_each_zone_zonelist_nodemask(zone, z, zonelist,
6666 sc->reclaim_idx, sc->nodemask) {
6668 * Take care memory controller reclaiming has small influence
6671 if (!cgroup_reclaim(sc)) {
6672 if (!cpuset_zone_allowed(zone,
6673 GFP_KERNEL | __GFP_HARDWALL))
6677 * If we already have plenty of memory free for
6678 * compaction in this zone, don't free any more.
6679 * Even though compaction is invoked for any
6680 * non-zero order, only frequent costly order
6681 * reclamation is disruptive enough to become a
6682 * noticeable problem, like transparent huge
6685 if (IS_ENABLED(CONFIG_COMPACTION) &&
6686 sc->order > PAGE_ALLOC_COSTLY_ORDER &&
6687 compaction_ready(zone, sc)) {
6688 sc->compaction_ready = true;
6693 * Shrink each node in the zonelist once. If the
6694 * zonelist is ordered by zone (not the default) then a
6695 * node may be shrunk multiple times but in that case
6696 * the user prefers lower zones being preserved.
6698 if (zone->zone_pgdat == last_pgdat)
6702 * This steals pages from memory cgroups over softlimit
6703 * and returns the number of reclaimed pages and
6704 * scanned pages. This works for global memory pressure
6705 * and balancing, not for a memcg's limit.
6707 nr_soft_scanned = 0;
6708 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat,
6709 sc->order, sc->gfp_mask,
6711 sc->nr_reclaimed += nr_soft_reclaimed;
6712 sc->nr_scanned += nr_soft_scanned;
6713 /* need some check for avoid more shrink_zone() */
6717 first_pgdat = zone->zone_pgdat;
6719 /* See comment about same check for global reclaim above */
6720 if (zone->zone_pgdat == last_pgdat)
6722 last_pgdat = zone->zone_pgdat;
6723 shrink_node(zone->zone_pgdat, sc);
6727 consider_reclaim_throttle(first_pgdat, sc);
6730 * Restore to original mask to avoid the impact on the caller if we
6731 * promoted it to __GFP_HIGHMEM.
6733 sc->gfp_mask = orig_mask;
6736 static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat)
6738 struct lruvec *target_lruvec;
6739 unsigned long refaults;
6741 if (lru_gen_enabled())
6744 target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat);
6745 refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON);
6746 target_lruvec->refaults[WORKINGSET_ANON] = refaults;
6747 refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE);
6748 target_lruvec->refaults[WORKINGSET_FILE] = refaults;
6752 * This is the main entry point to direct page reclaim.
6754 * If a full scan of the inactive list fails to free enough memory then we
6755 * are "out of memory" and something needs to be killed.
6757 * If the caller is !__GFP_FS then the probability of a failure is reasonably
6758 * high - the zone may be full of dirty or under-writeback pages, which this
6759 * caller can't do much about. We kick the writeback threads and take explicit
6760 * naps in the hope that some of these pages can be written. But if the
6761 * allocating task holds filesystem locks which prevent writeout this might not
6762 * work, and the allocation attempt will fail.
6764 * returns: 0, if no pages reclaimed
6765 * else, the number of pages reclaimed
6767 static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
6768 struct scan_control *sc)
6770 int initial_priority = sc->priority;
6771 pg_data_t *last_pgdat;
6775 delayacct_freepages_start();
6777 if (!cgroup_reclaim(sc))
6778 __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1);
6782 vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup,
6785 shrink_zones(zonelist, sc);
6787 if (sc->nr_reclaimed >= sc->nr_to_reclaim)
6790 if (sc->compaction_ready)
6794 * If we're getting trouble reclaiming, start doing
6795 * writepage even in laptop mode.
6797 if (sc->priority < DEF_PRIORITY - 2)
6798 sc->may_writepage = 1;
6799 } while (--sc->priority >= 0);
6802 for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx,
6804 if (zone->zone_pgdat == last_pgdat)
6806 last_pgdat = zone->zone_pgdat;
6808 snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat);
6810 if (cgroup_reclaim(sc)) {
6811 struct lruvec *lruvec;
6813 lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup,
6815 clear_bit(LRUVEC_CONGESTED, &lruvec->flags);
6819 delayacct_freepages_end();
6821 if (sc->nr_reclaimed)
6822 return sc->nr_reclaimed;
6824 /* Aborted reclaim to try compaction? don't OOM, then */
6825 if (sc->compaction_ready)
6829 * We make inactive:active ratio decisions based on the node's
6830 * composition of memory, but a restrictive reclaim_idx or a
6831 * memory.low cgroup setting can exempt large amounts of
6832 * memory from reclaim. Neither of which are very common, so
6833 * instead of doing costly eligibility calculations of the
6834 * entire cgroup subtree up front, we assume the estimates are
6835 * good, and retry with forcible deactivation if that fails.
6837 if (sc->skipped_deactivate) {
6838 sc->priority = initial_priority;
6839 sc->force_deactivate = 1;
6840 sc->skipped_deactivate = 0;
6844 /* Untapped cgroup reserves? Don't OOM, retry. */
6845 if (sc->memcg_low_skipped) {
6846 sc->priority = initial_priority;
6847 sc->force_deactivate = 0;
6848 sc->memcg_low_reclaim = 1;
6849 sc->memcg_low_skipped = 0;
6856 static bool allow_direct_reclaim(pg_data_t *pgdat)
6859 unsigned long pfmemalloc_reserve = 0;
6860 unsigned long free_pages = 0;
6864 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
6867 for (i = 0; i <= ZONE_NORMAL; i++) {
6868 zone = &pgdat->node_zones[i];
6869 if (!managed_zone(zone))
6872 if (!zone_reclaimable_pages(zone))
6875 pfmemalloc_reserve += min_wmark_pages(zone);
6876 free_pages += zone_page_state(zone, NR_FREE_PAGES);
6879 /* If there are no reserves (unexpected config) then do not throttle */
6880 if (!pfmemalloc_reserve)
6883 wmark_ok = free_pages > pfmemalloc_reserve / 2;
6885 /* kswapd must be awake if processes are being throttled */
6886 if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
6887 if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL)
6888 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL);
6890 wake_up_interruptible(&pgdat->kswapd_wait);
6897 * Throttle direct reclaimers if backing storage is backed by the network
6898 * and the PFMEMALLOC reserve for the preferred node is getting dangerously
6899 * depleted. kswapd will continue to make progress and wake the processes
6900 * when the low watermark is reached.
6902 * Returns true if a fatal signal was delivered during throttling. If this
6903 * happens, the page allocator should not consider triggering the OOM killer.
6905 static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
6906 nodemask_t *nodemask)
6910 pg_data_t *pgdat = NULL;
6913 * Kernel threads should not be throttled as they may be indirectly
6914 * responsible for cleaning pages necessary for reclaim to make forward
6915 * progress. kjournald for example may enter direct reclaim while
6916 * committing a transaction where throttling it could forcing other
6917 * processes to block on log_wait_commit().
6919 if (current->flags & PF_KTHREAD)
6923 * If a fatal signal is pending, this process should not throttle.
6924 * It should return quickly so it can exit and free its memory
6926 if (fatal_signal_pending(current))
6930 * Check if the pfmemalloc reserves are ok by finding the first node
6931 * with a usable ZONE_NORMAL or lower zone. The expectation is that
6932 * GFP_KERNEL will be required for allocating network buffers when
6933 * swapping over the network so ZONE_HIGHMEM is unusable.
6935 * Throttling is based on the first usable node and throttled processes
6936 * wait on a queue until kswapd makes progress and wakes them. There
6937 * is an affinity then between processes waking up and where reclaim
6938 * progress has been made assuming the process wakes on the same node.
6939 * More importantly, processes running on remote nodes will not compete
6940 * for remote pfmemalloc reserves and processes on different nodes
6941 * should make reasonable progress.
6943 for_each_zone_zonelist_nodemask(zone, z, zonelist,
6944 gfp_zone(gfp_mask), nodemask) {
6945 if (zone_idx(zone) > ZONE_NORMAL)
6948 /* Throttle based on the first usable node */
6949 pgdat = zone->zone_pgdat;
6950 if (allow_direct_reclaim(pgdat))
6955 /* If no zone was usable by the allocation flags then do not throttle */
6959 /* Account for the throttling */
6960 count_vm_event(PGSCAN_DIRECT_THROTTLE);
6963 * If the caller cannot enter the filesystem, it's possible that it
6964 * is due to the caller holding an FS lock or performing a journal
6965 * transaction in the case of a filesystem like ext[3|4]. In this case,
6966 * it is not safe to block on pfmemalloc_wait as kswapd could be
6967 * blocked waiting on the same lock. Instead, throttle for up to a
6968 * second before continuing.
6970 if (!(gfp_mask & __GFP_FS))
6971 wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
6972 allow_direct_reclaim(pgdat), HZ);
6974 /* Throttle until kswapd wakes the process */
6975 wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
6976 allow_direct_reclaim(pgdat));
6978 if (fatal_signal_pending(current))
6985 unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
6986 gfp_t gfp_mask, nodemask_t *nodemask)
6988 unsigned long nr_reclaimed;
6989 struct scan_control sc = {
6990 .nr_to_reclaim = SWAP_CLUSTER_MAX,
6991 .gfp_mask = current_gfp_context(gfp_mask),
6992 .reclaim_idx = gfp_zone(gfp_mask),
6994 .nodemask = nodemask,
6995 .priority = DEF_PRIORITY,
6996 .may_writepage = !laptop_mode,
7002 * scan_control uses s8 fields for order, priority, and reclaim_idx.
7003 * Confirm they are large enough for max values.
7005 BUILD_BUG_ON(MAX_ORDER > S8_MAX);
7006 BUILD_BUG_ON(DEF_PRIORITY > S8_MAX);
7007 BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX);
7010 * Do not enter reclaim if fatal signal was delivered while throttled.
7011 * 1 is returned so that the page allocator does not OOM kill at this
7014 if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask))
7017 set_task_reclaim_state(current, &sc.reclaim_state);
7018 trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask);
7020 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
7022 trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
7023 set_task_reclaim_state(current, NULL);
7025 return nr_reclaimed;
7030 /* Only used by soft limit reclaim. Do not reuse for anything else. */
7031 unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg,
7032 gfp_t gfp_mask, bool noswap,
7034 unsigned long *nr_scanned)
7036 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
7037 struct scan_control sc = {
7038 .nr_to_reclaim = SWAP_CLUSTER_MAX,
7039 .target_mem_cgroup = memcg,
7040 .may_writepage = !laptop_mode,
7042 .reclaim_idx = MAX_NR_ZONES - 1,
7043 .may_swap = !noswap,
7046 WARN_ON_ONCE(!current->reclaim_state);
7048 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
7049 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
7051 trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
7055 * NOTE: Although we can get the priority field, using it
7056 * here is not a good idea, since it limits the pages we can scan.
7057 * if we don't reclaim here, the shrink_node from balance_pgdat
7058 * will pick up pages from other mem cgroup's as well. We hack
7059 * the priority and make it zero.
7061 shrink_lruvec(lruvec, &sc);
7063 trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
7065 *nr_scanned = sc.nr_scanned;
7067 return sc.nr_reclaimed;
7070 unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
7071 unsigned long nr_pages,
7073 unsigned int reclaim_options)
7075 unsigned long nr_reclaimed;
7076 unsigned int noreclaim_flag;
7077 struct scan_control sc = {
7078 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
7079 .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) |
7080 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
7081 .reclaim_idx = MAX_NR_ZONES - 1,
7082 .target_mem_cgroup = memcg,
7083 .priority = DEF_PRIORITY,
7084 .may_writepage = !laptop_mode,
7086 .may_swap = !!(reclaim_options & MEMCG_RECLAIM_MAY_SWAP),
7087 .proactive = !!(reclaim_options & MEMCG_RECLAIM_PROACTIVE),
7090 * Traverse the ZONELIST_FALLBACK zonelist of the current node to put
7091 * equal pressure on all the nodes. This is based on the assumption that
7092 * the reclaim does not bail out early.
7094 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
7096 set_task_reclaim_state(current, &sc.reclaim_state);
7097 trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask);
7098 noreclaim_flag = memalloc_noreclaim_save();
7100 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
7102 memalloc_noreclaim_restore(noreclaim_flag);
7103 trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
7104 set_task_reclaim_state(current, NULL);
7106 return nr_reclaimed;
7110 static void kswapd_age_node(struct pglist_data *pgdat, struct scan_control *sc)
7112 struct mem_cgroup *memcg;
7113 struct lruvec *lruvec;
7115 if (lru_gen_enabled()) {
7116 lru_gen_age_node(pgdat, sc);
7120 if (!can_age_anon_pages(pgdat, sc))
7123 lruvec = mem_cgroup_lruvec(NULL, pgdat);
7124 if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON))
7127 memcg = mem_cgroup_iter(NULL, NULL, NULL);
7129 lruvec = mem_cgroup_lruvec(memcg, pgdat);
7130 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
7131 sc, LRU_ACTIVE_ANON);
7132 memcg = mem_cgroup_iter(NULL, memcg, NULL);
7136 static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx)
7142 * Check for watermark boosts top-down as the higher zones
7143 * are more likely to be boosted. Both watermarks and boosts
7144 * should not be checked at the same time as reclaim would
7145 * start prematurely when there is no boosting and a lower
7148 for (i = highest_zoneidx; i >= 0; i--) {
7149 zone = pgdat->node_zones + i;
7150 if (!managed_zone(zone))
7153 if (zone->watermark_boost)
7161 * Returns true if there is an eligible zone balanced for the request order
7162 * and highest_zoneidx
7164 static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx)
7167 unsigned long mark = -1;
7171 * Check watermarks bottom-up as lower zones are more likely to
7174 for (i = 0; i <= highest_zoneidx; i++) {
7175 zone = pgdat->node_zones + i;
7177 if (!managed_zone(zone))
7180 if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
7181 mark = wmark_pages(zone, WMARK_PROMO);
7183 mark = high_wmark_pages(zone);
7184 if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx))
7189 * If a node has no managed zone within highest_zoneidx, it does not
7190 * need balancing by definition. This can happen if a zone-restricted
7191 * allocation tries to wake a remote kswapd.
7199 /* Clear pgdat state for congested, dirty or under writeback. */
7200 static void clear_pgdat_congested(pg_data_t *pgdat)
7202 struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat);
7204 clear_bit(LRUVEC_CONGESTED, &lruvec->flags);
7205 clear_bit(PGDAT_DIRTY, &pgdat->flags);
7206 clear_bit(PGDAT_WRITEBACK, &pgdat->flags);
7210 * Prepare kswapd for sleeping. This verifies that there are no processes
7211 * waiting in throttle_direct_reclaim() and that watermarks have been met.
7213 * Returns true if kswapd is ready to sleep
7215 static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order,
7216 int highest_zoneidx)
7219 * The throttled processes are normally woken up in balance_pgdat() as
7220 * soon as allow_direct_reclaim() is true. But there is a potential
7221 * race between when kswapd checks the watermarks and a process gets
7222 * throttled. There is also a potential race if processes get
7223 * throttled, kswapd wakes, a large process exits thereby balancing the
7224 * zones, which causes kswapd to exit balance_pgdat() before reaching
7225 * the wake up checks. If kswapd is going to sleep, no process should
7226 * be sleeping on pfmemalloc_wait, so wake them now if necessary. If
7227 * the wake up is premature, processes will wake kswapd and get
7228 * throttled again. The difference from wake ups in balance_pgdat() is
7229 * that here we are under prepare_to_wait().
7231 if (waitqueue_active(&pgdat->pfmemalloc_wait))
7232 wake_up_all(&pgdat->pfmemalloc_wait);
7234 /* Hopeless node, leave it to direct reclaim */
7235 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
7238 if (pgdat_balanced(pgdat, order, highest_zoneidx)) {
7239 clear_pgdat_congested(pgdat);
7247 * kswapd shrinks a node of pages that are at or below the highest usable
7248 * zone that is currently unbalanced.
7250 * Returns true if kswapd scanned at least the requested number of pages to
7251 * reclaim or if the lack of progress was due to pages under writeback.
7252 * This is used to determine if the scanning priority needs to be raised.
7254 static bool kswapd_shrink_node(pg_data_t *pgdat,
7255 struct scan_control *sc)
7260 /* Reclaim a number of pages proportional to the number of zones */
7261 sc->nr_to_reclaim = 0;
7262 for (z = 0; z <= sc->reclaim_idx; z++) {
7263 zone = pgdat->node_zones + z;
7264 if (!managed_zone(zone))
7267 sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX);
7271 * Historically care was taken to put equal pressure on all zones but
7272 * now pressure is applied based on node LRU order.
7274 shrink_node(pgdat, sc);
7277 * Fragmentation may mean that the system cannot be rebalanced for
7278 * high-order allocations. If twice the allocation size has been
7279 * reclaimed then recheck watermarks only at order-0 to prevent
7280 * excessive reclaim. Assume that a process requested a high-order
7281 * can direct reclaim/compact.
7283 if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order))
7286 return sc->nr_scanned >= sc->nr_to_reclaim;
7289 /* Page allocator PCP high watermark is lowered if reclaim is active. */
7291 update_reclaim_active(pg_data_t *pgdat, int highest_zoneidx, bool active)
7296 for (i = 0; i <= highest_zoneidx; i++) {
7297 zone = pgdat->node_zones + i;
7299 if (!managed_zone(zone))
7303 set_bit(ZONE_RECLAIM_ACTIVE, &zone->flags);
7305 clear_bit(ZONE_RECLAIM_ACTIVE, &zone->flags);
7310 set_reclaim_active(pg_data_t *pgdat, int highest_zoneidx)
7312 update_reclaim_active(pgdat, highest_zoneidx, true);
7316 clear_reclaim_active(pg_data_t *pgdat, int highest_zoneidx)
7318 update_reclaim_active(pgdat, highest_zoneidx, false);
7322 * For kswapd, balance_pgdat() will reclaim pages across a node from zones
7323 * that are eligible for use by the caller until at least one zone is
7326 * Returns the order kswapd finished reclaiming at.
7328 * kswapd scans the zones in the highmem->normal->dma direction. It skips
7329 * zones which have free_pages > high_wmark_pages(zone), but once a zone is
7330 * found to have free_pages <= high_wmark_pages(zone), any page in that zone
7331 * or lower is eligible for reclaim until at least one usable zone is
7334 static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx)
7337 unsigned long nr_soft_reclaimed;
7338 unsigned long nr_soft_scanned;
7339 unsigned long pflags;
7340 unsigned long nr_boost_reclaim;
7341 unsigned long zone_boosts[MAX_NR_ZONES] = { 0, };
7344 struct scan_control sc = {
7345 .gfp_mask = GFP_KERNEL,
7350 set_task_reclaim_state(current, &sc.reclaim_state);
7351 psi_memstall_enter(&pflags);
7352 __fs_reclaim_acquire(_THIS_IP_);
7354 count_vm_event(PAGEOUTRUN);
7357 * Account for the reclaim boost. Note that the zone boost is left in
7358 * place so that parallel allocations that are near the watermark will
7359 * stall or direct reclaim until kswapd is finished.
7361 nr_boost_reclaim = 0;
7362 for (i = 0; i <= highest_zoneidx; i++) {
7363 zone = pgdat->node_zones + i;
7364 if (!managed_zone(zone))
7367 nr_boost_reclaim += zone->watermark_boost;
7368 zone_boosts[i] = zone->watermark_boost;
7370 boosted = nr_boost_reclaim;
7373 set_reclaim_active(pgdat, highest_zoneidx);
7374 sc.priority = DEF_PRIORITY;
7376 unsigned long nr_reclaimed = sc.nr_reclaimed;
7377 bool raise_priority = true;
7381 sc.reclaim_idx = highest_zoneidx;
7384 * If the number of buffer_heads exceeds the maximum allowed
7385 * then consider reclaiming from all zones. This has a dual
7386 * purpose -- on 64-bit systems it is expected that
7387 * buffer_heads are stripped during active rotation. On 32-bit
7388 * systems, highmem pages can pin lowmem memory and shrinking
7389 * buffers can relieve lowmem pressure. Reclaim may still not
7390 * go ahead if all eligible zones for the original allocation
7391 * request are balanced to avoid excessive reclaim from kswapd.
7393 if (buffer_heads_over_limit) {
7394 for (i = MAX_NR_ZONES - 1; i >= 0; i--) {
7395 zone = pgdat->node_zones + i;
7396 if (!managed_zone(zone))
7405 * If the pgdat is imbalanced then ignore boosting and preserve
7406 * the watermarks for a later time and restart. Note that the
7407 * zone watermarks will be still reset at the end of balancing
7408 * on the grounds that the normal reclaim should be enough to
7409 * re-evaluate if boosting is required when kswapd next wakes.
7411 balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx);
7412 if (!balanced && nr_boost_reclaim) {
7413 nr_boost_reclaim = 0;
7418 * If boosting is not active then only reclaim if there are no
7419 * eligible zones. Note that sc.reclaim_idx is not used as
7420 * buffer_heads_over_limit may have adjusted it.
7422 if (!nr_boost_reclaim && balanced)
7425 /* Limit the priority of boosting to avoid reclaim writeback */
7426 if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2)
7427 raise_priority = false;
7430 * Do not writeback or swap pages for boosted reclaim. The
7431 * intent is to relieve pressure not issue sub-optimal IO
7432 * from reclaim context. If no pages are reclaimed, the
7433 * reclaim will be aborted.
7435 sc.may_writepage = !laptop_mode && !nr_boost_reclaim;
7436 sc.may_swap = !nr_boost_reclaim;
7439 * Do some background aging, to give pages a chance to be
7440 * referenced before reclaiming. All pages are rotated
7441 * regardless of classzone as this is about consistent aging.
7443 kswapd_age_node(pgdat, &sc);
7446 * If we're getting trouble reclaiming, start doing writepage
7447 * even in laptop mode.
7449 if (sc.priority < DEF_PRIORITY - 2)
7450 sc.may_writepage = 1;
7452 /* Call soft limit reclaim before calling shrink_node. */
7454 nr_soft_scanned = 0;
7455 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order,
7456 sc.gfp_mask, &nr_soft_scanned);
7457 sc.nr_reclaimed += nr_soft_reclaimed;
7460 * There should be no need to raise the scanning priority if
7461 * enough pages are already being scanned that that high
7462 * watermark would be met at 100% efficiency.
7464 if (kswapd_shrink_node(pgdat, &sc))
7465 raise_priority = false;
7468 * If the low watermark is met there is no need for processes
7469 * to be throttled on pfmemalloc_wait as they should not be
7470 * able to safely make forward progress. Wake them
7472 if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
7473 allow_direct_reclaim(pgdat))
7474 wake_up_all(&pgdat->pfmemalloc_wait);
7476 /* Check if kswapd should be suspending */
7477 __fs_reclaim_release(_THIS_IP_);
7478 ret = try_to_freeze();
7479 __fs_reclaim_acquire(_THIS_IP_);
7480 if (ret || kthread_should_stop())
7484 * Raise priority if scanning rate is too low or there was no
7485 * progress in reclaiming pages
7487 nr_reclaimed = sc.nr_reclaimed - nr_reclaimed;
7488 nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed);
7491 * If reclaim made no progress for a boost, stop reclaim as
7492 * IO cannot be queued and it could be an infinite loop in
7493 * extreme circumstances.
7495 if (nr_boost_reclaim && !nr_reclaimed)
7498 if (raise_priority || !nr_reclaimed)
7500 } while (sc.priority >= 1);
7502 if (!sc.nr_reclaimed)
7503 pgdat->kswapd_failures++;
7506 clear_reclaim_active(pgdat, highest_zoneidx);
7508 /* If reclaim was boosted, account for the reclaim done in this pass */
7510 unsigned long flags;
7512 for (i = 0; i <= highest_zoneidx; i++) {
7513 if (!zone_boosts[i])
7516 /* Increments are under the zone lock */
7517 zone = pgdat->node_zones + i;
7518 spin_lock_irqsave(&zone->lock, flags);
7519 zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]);
7520 spin_unlock_irqrestore(&zone->lock, flags);
7524 * As there is now likely space, wakeup kcompact to defragment
7527 wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx);
7530 snapshot_refaults(NULL, pgdat);
7531 __fs_reclaim_release(_THIS_IP_);
7532 psi_memstall_leave(&pflags);
7533 set_task_reclaim_state(current, NULL);
7536 * Return the order kswapd stopped reclaiming at as
7537 * prepare_kswapd_sleep() takes it into account. If another caller
7538 * entered the allocator slow path while kswapd was awake, order will
7539 * remain at the higher level.
7545 * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to
7546 * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is
7547 * not a valid index then either kswapd runs for first time or kswapd couldn't
7548 * sleep after previous reclaim attempt (node is still unbalanced). In that
7549 * case return the zone index of the previous kswapd reclaim cycle.
7551 static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,
7552 enum zone_type prev_highest_zoneidx)
7554 enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
7556 return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx;
7559 static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order,
7560 unsigned int highest_zoneidx)
7565 if (freezing(current) || kthread_should_stop())
7568 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
7571 * Try to sleep for a short interval. Note that kcompactd will only be
7572 * woken if it is possible to sleep for a short interval. This is
7573 * deliberate on the assumption that if reclaim cannot keep an
7574 * eligible zone balanced that it's also unlikely that compaction will
7577 if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
7579 * Compaction records what page blocks it recently failed to
7580 * isolate pages from and skips them in the future scanning.
7581 * When kswapd is going to sleep, it is reasonable to assume
7582 * that pages and compaction may succeed so reset the cache.
7584 reset_isolation_suitable(pgdat);
7587 * We have freed the memory, now we should compact it to make
7588 * allocation of the requested order possible.
7590 wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx);
7592 remaining = schedule_timeout(HZ/10);
7595 * If woken prematurely then reset kswapd_highest_zoneidx and
7596 * order. The values will either be from a wakeup request or
7597 * the previous request that slept prematurely.
7600 WRITE_ONCE(pgdat->kswapd_highest_zoneidx,
7601 kswapd_highest_zoneidx(pgdat,
7604 if (READ_ONCE(pgdat->kswapd_order) < reclaim_order)
7605 WRITE_ONCE(pgdat->kswapd_order, reclaim_order);
7608 finish_wait(&pgdat->kswapd_wait, &wait);
7609 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
7613 * After a short sleep, check if it was a premature sleep. If not, then
7614 * go fully to sleep until explicitly woken up.
7617 prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
7618 trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
7621 * vmstat counters are not perfectly accurate and the estimated
7622 * value for counters such as NR_FREE_PAGES can deviate from the
7623 * true value by nr_online_cpus * threshold. To avoid the zone
7624 * watermarks being breached while under pressure, we reduce the
7625 * per-cpu vmstat threshold while kswapd is awake and restore
7626 * them before going back to sleep.
7628 set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
7630 if (!kthread_should_stop())
7633 set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
7636 count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
7638 count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
7640 finish_wait(&pgdat->kswapd_wait, &wait);
7644 * The background pageout daemon, started as a kernel thread
7645 * from the init process.
7647 * This basically trickles out pages so that we have _some_
7648 * free memory available even if there is no other activity
7649 * that frees anything up. This is needed for things like routing
7650 * etc, where we otherwise might have all activity going on in
7651 * asynchronous contexts that cannot page things out.
7653 * If there are applications that are active memory-allocators
7654 * (most normal use), this basically shouldn't matter.
7656 static int kswapd(void *p)
7658 unsigned int alloc_order, reclaim_order;
7659 unsigned int highest_zoneidx = MAX_NR_ZONES - 1;
7660 pg_data_t *pgdat = (pg_data_t *)p;
7661 struct task_struct *tsk = current;
7662 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
7664 if (!cpumask_empty(cpumask))
7665 set_cpus_allowed_ptr(tsk, cpumask);
7668 * Tell the memory management that we're a "memory allocator",
7669 * and that if we need more memory we should get access to it
7670 * regardless (see "__alloc_pages()"). "kswapd" should
7671 * never get caught in the normal page freeing logic.
7673 * (Kswapd normally doesn't need memory anyway, but sometimes
7674 * you need a small amount of memory in order to be able to
7675 * page out something else, and this flag essentially protects
7676 * us from recursively trying to free more memory as we're
7677 * trying to free the first piece of memory in the first place).
7679 tsk->flags |= PF_MEMALLOC | PF_KSWAPD;
7682 WRITE_ONCE(pgdat->kswapd_order, 0);
7683 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
7684 atomic_set(&pgdat->nr_writeback_throttled, 0);
7688 alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order);
7689 highest_zoneidx = kswapd_highest_zoneidx(pgdat,
7693 kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order,
7696 /* Read the new order and highest_zoneidx */
7697 alloc_order = READ_ONCE(pgdat->kswapd_order);
7698 highest_zoneidx = kswapd_highest_zoneidx(pgdat,
7700 WRITE_ONCE(pgdat->kswapd_order, 0);
7701 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
7703 ret = try_to_freeze();
7704 if (kthread_should_stop())
7708 * We can speed up thawing tasks if we don't call balance_pgdat
7709 * after returning from the refrigerator
7715 * Reclaim begins at the requested order but if a high-order
7716 * reclaim fails then kswapd falls back to reclaiming for
7717 * order-0. If that happens, kswapd will consider sleeping
7718 * for the order it finished reclaiming at (reclaim_order)
7719 * but kcompactd is woken to compact for the original
7720 * request (alloc_order).
7722 trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx,
7724 reclaim_order = balance_pgdat(pgdat, alloc_order,
7726 if (reclaim_order < alloc_order)
7727 goto kswapd_try_sleep;
7730 tsk->flags &= ~(PF_MEMALLOC | PF_KSWAPD);
7736 * A zone is low on free memory or too fragmented for high-order memory. If
7737 * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's
7738 * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim
7739 * has failed or is not needed, still wake up kcompactd if only compaction is
7742 void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order,
7743 enum zone_type highest_zoneidx)
7746 enum zone_type curr_idx;
7748 if (!managed_zone(zone))
7751 if (!cpuset_zone_allowed(zone, gfp_flags))
7754 pgdat = zone->zone_pgdat;
7755 curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
7757 if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx)
7758 WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx);
7760 if (READ_ONCE(pgdat->kswapd_order) < order)
7761 WRITE_ONCE(pgdat->kswapd_order, order);
7763 if (!waitqueue_active(&pgdat->kswapd_wait))
7766 /* Hopeless node, leave it to direct reclaim if possible */
7767 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ||
7768 (pgdat_balanced(pgdat, order, highest_zoneidx) &&
7769 !pgdat_watermark_boosted(pgdat, highest_zoneidx))) {
7771 * There may be plenty of free memory available, but it's too
7772 * fragmented for high-order allocations. Wake up kcompactd
7773 * and rely on compaction_suitable() to determine if it's
7774 * needed. If it fails, it will defer subsequent attempts to
7775 * ratelimit its work.
7777 if (!(gfp_flags & __GFP_DIRECT_RECLAIM))
7778 wakeup_kcompactd(pgdat, order, highest_zoneidx);
7782 trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order,
7784 wake_up_interruptible(&pgdat->kswapd_wait);
7787 #ifdef CONFIG_HIBERNATION
7789 * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
7792 * Rather than trying to age LRUs the aim is to preserve the overall
7793 * LRU order by reclaiming preferentially
7794 * inactive > active > active referenced > active mapped
7796 unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
7798 struct scan_control sc = {
7799 .nr_to_reclaim = nr_to_reclaim,
7800 .gfp_mask = GFP_HIGHUSER_MOVABLE,
7801 .reclaim_idx = MAX_NR_ZONES - 1,
7802 .priority = DEF_PRIORITY,
7806 .hibernation_mode = 1,
7808 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
7809 unsigned long nr_reclaimed;
7810 unsigned int noreclaim_flag;
7812 fs_reclaim_acquire(sc.gfp_mask);
7813 noreclaim_flag = memalloc_noreclaim_save();
7814 set_task_reclaim_state(current, &sc.reclaim_state);
7816 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
7818 set_task_reclaim_state(current, NULL);
7819 memalloc_noreclaim_restore(noreclaim_flag);
7820 fs_reclaim_release(sc.gfp_mask);
7822 return nr_reclaimed;
7824 #endif /* CONFIG_HIBERNATION */
7827 * This kswapd start function will be called by init and node-hot-add.
7829 void kswapd_run(int nid)
7831 pg_data_t *pgdat = NODE_DATA(nid);
7833 pgdat_kswapd_lock(pgdat);
7834 if (!pgdat->kswapd) {
7835 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
7836 if (IS_ERR(pgdat->kswapd)) {
7837 /* failure at boot is fatal */
7838 BUG_ON(system_state < SYSTEM_RUNNING);
7839 pr_err("Failed to start kswapd on node %d\n", nid);
7840 pgdat->kswapd = NULL;
7843 pgdat_kswapd_unlock(pgdat);
7847 * Called by memory hotplug when all memory in a node is offlined. Caller must
7848 * be holding mem_hotplug_begin/done().
7850 void kswapd_stop(int nid)
7852 pg_data_t *pgdat = NODE_DATA(nid);
7853 struct task_struct *kswapd;
7855 pgdat_kswapd_lock(pgdat);
7856 kswapd = pgdat->kswapd;
7858 kthread_stop(kswapd);
7859 pgdat->kswapd = NULL;
7861 pgdat_kswapd_unlock(pgdat);
7864 static int __init kswapd_init(void)
7869 for_each_node_state(nid, N_MEMORY)
7874 module_init(kswapd_init)
7880 * If non-zero call node_reclaim when the number of free pages falls below
7883 int node_reclaim_mode __read_mostly;
7886 * Priority for NODE_RECLAIM. This determines the fraction of pages
7887 * of a node considered for each zone_reclaim. 4 scans 1/16th of
7890 #define NODE_RECLAIM_PRIORITY 4
7893 * Percentage of pages in a zone that must be unmapped for node_reclaim to
7896 int sysctl_min_unmapped_ratio = 1;
7899 * If the number of slab pages in a zone grows beyond this percentage then
7900 * slab reclaim needs to occur.
7902 int sysctl_min_slab_ratio = 5;
7904 static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat)
7906 unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED);
7907 unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) +
7908 node_page_state(pgdat, NR_ACTIVE_FILE);
7911 * It's possible for there to be more file mapped pages than
7912 * accounted for by the pages on the file LRU lists because
7913 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
7915 return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
7918 /* Work out how many page cache pages we can reclaim in this reclaim_mode */
7919 static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat)
7921 unsigned long nr_pagecache_reclaimable;
7922 unsigned long delta = 0;
7925 * If RECLAIM_UNMAP is set, then all file pages are considered
7926 * potentially reclaimable. Otherwise, we have to worry about
7927 * pages like swapcache and node_unmapped_file_pages() provides
7930 if (node_reclaim_mode & RECLAIM_UNMAP)
7931 nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES);
7933 nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat);
7935 /* If we can't clean pages, remove dirty pages from consideration */
7936 if (!(node_reclaim_mode & RECLAIM_WRITE))
7937 delta += node_page_state(pgdat, NR_FILE_DIRTY);
7939 /* Watch for any possible underflows due to delta */
7940 if (unlikely(delta > nr_pagecache_reclaimable))
7941 delta = nr_pagecache_reclaimable;
7943 return nr_pagecache_reclaimable - delta;
7947 * Try to free up some pages from this node through reclaim.
7949 static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
7951 /* Minimum pages needed in order to stay on node */
7952 const unsigned long nr_pages = 1 << order;
7953 struct task_struct *p = current;
7954 unsigned int noreclaim_flag;
7955 struct scan_control sc = {
7956 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
7957 .gfp_mask = current_gfp_context(gfp_mask),
7959 .priority = NODE_RECLAIM_PRIORITY,
7960 .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE),
7961 .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP),
7963 .reclaim_idx = gfp_zone(gfp_mask),
7965 unsigned long pflags;
7967 trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order,
7971 psi_memstall_enter(&pflags);
7972 fs_reclaim_acquire(sc.gfp_mask);
7974 * We need to be able to allocate from the reserves for RECLAIM_UNMAP
7976 noreclaim_flag = memalloc_noreclaim_save();
7977 set_task_reclaim_state(p, &sc.reclaim_state);
7979 if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages ||
7980 node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) > pgdat->min_slab_pages) {
7982 * Free memory by calling shrink node with increasing
7983 * priorities until we have enough memory freed.
7986 shrink_node(pgdat, &sc);
7987 } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
7990 set_task_reclaim_state(p, NULL);
7991 memalloc_noreclaim_restore(noreclaim_flag);
7992 fs_reclaim_release(sc.gfp_mask);
7993 psi_memstall_leave(&pflags);
7995 trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed);
7997 return sc.nr_reclaimed >= nr_pages;
8000 int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
8005 * Node reclaim reclaims unmapped file backed pages and
8006 * slab pages if we are over the defined limits.
8008 * A small portion of unmapped file backed pages is needed for
8009 * file I/O otherwise pages read by file I/O will be immediately
8010 * thrown out if the node is overallocated. So we do not reclaim
8011 * if less than a specified percentage of the node is used by
8012 * unmapped file backed pages.
8014 if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages &&
8015 node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <=
8016 pgdat->min_slab_pages)
8017 return NODE_RECLAIM_FULL;
8020 * Do not scan if the allocation should not be delayed.
8022 if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC))
8023 return NODE_RECLAIM_NOSCAN;
8026 * Only run node reclaim on the local node or on nodes that do not
8027 * have associated processors. This will favor the local processor
8028 * over remote processors and spread off node memory allocations
8029 * as wide as possible.
8031 if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
8032 return NODE_RECLAIM_NOSCAN;
8034 if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
8035 return NODE_RECLAIM_NOSCAN;
8037 ret = __node_reclaim(pgdat, gfp_mask, order);
8038 clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags);
8041 count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
8047 void check_move_unevictable_pages(struct pagevec *pvec)
8049 struct folio_batch fbatch;
8052 folio_batch_init(&fbatch);
8053 for (i = 0; i < pvec->nr; i++) {
8054 struct page *page = pvec->pages[i];
8056 if (PageTransTail(page))
8058 folio_batch_add(&fbatch, page_folio(page));
8060 check_move_unevictable_folios(&fbatch);
8062 EXPORT_SYMBOL_GPL(check_move_unevictable_pages);
8065 * check_move_unevictable_folios - Move evictable folios to appropriate zone
8067 * @fbatch: Batch of lru folios to check.
8069 * Checks folios for evictability, if an evictable folio is in the unevictable
8070 * lru list, moves it to the appropriate evictable lru list. This function
8071 * should be only used for lru folios.
8073 void check_move_unevictable_folios(struct folio_batch *fbatch)
8075 struct lruvec *lruvec = NULL;
8080 for (i = 0; i < fbatch->nr; i++) {
8081 struct folio *folio = fbatch->folios[i];
8082 int nr_pages = folio_nr_pages(folio);
8084 pgscanned += nr_pages;
8086 /* block memcg migration while the folio moves between lrus */
8087 if (!folio_test_clear_lru(folio))
8090 lruvec = folio_lruvec_relock_irq(folio, lruvec);
8091 if (folio_evictable(folio) && folio_test_unevictable(folio)) {
8092 lruvec_del_folio(lruvec, folio);
8093 folio_clear_unevictable(folio);
8094 lruvec_add_folio(lruvec, folio);
8095 pgrescued += nr_pages;
8097 folio_set_lru(folio);
8101 __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
8102 __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
8103 unlock_page_lruvec_irq(lruvec);
8104 } else if (pgscanned) {
8105 count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
8108 EXPORT_SYMBOL_GPL(check_move_unevictable_folios);