xref: /openbmc/linux/mm/vmscan.c (revision d5a9588c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
4  *
5  *  Swap reorganised 29.12.95, Stephen Tweedie.
6  *  kswapd added: 7.1.96  sct
7  *  Removed kswapd_ctl limits, and swap out as many pages as needed
8  *  to bring the system back to freepages.high: 2.4.97, Rik van Riel.
9  *  Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
10  *  Multiqueue VM started 5.8.00, Rik van Riel.
11  */
12 
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 
15 #include <linux/mm.h>
16 #include <linux/sched/mm.h>
17 #include <linux/module.h>
18 #include <linux/gfp.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/swap.h>
21 #include <linux/pagemap.h>
22 #include <linux/init.h>
23 #include <linux/highmem.h>
24 #include <linux/vmpressure.h>
25 #include <linux/vmstat.h>
26 #include <linux/file.h>
27 #include <linux/writeback.h>
28 #include <linux/blkdev.h>
29 #include <linux/buffer_head.h>	/* for buffer_heads_over_limit */
30 #include <linux/mm_inline.h>
31 #include <linux/backing-dev.h>
32 #include <linux/rmap.h>
33 #include <linux/topology.h>
34 #include <linux/cpu.h>
35 #include <linux/cpuset.h>
36 #include <linux/compaction.h>
37 #include <linux/notifier.h>
38 #include <linux/rwsem.h>
39 #include <linux/delay.h>
40 #include <linux/kthread.h>
41 #include <linux/freezer.h>
42 #include <linux/memcontrol.h>
43 #include <linux/migrate.h>
44 #include <linux/delayacct.h>
45 #include <linux/sysctl.h>
46 #include <linux/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 
61 #include <asm/tlbflush.h>
62 #include <asm/div64.h>
63 
64 #include <linux/swapops.h>
65 #include <linux/balloon_compaction.h>
66 #include <linux/sched/sysctl.h>
67 
68 #include "internal.h"
69 #include "swap.h"
70 
71 #define CREATE_TRACE_POINTS
72 #include <trace/events/vmscan.h>
73 
74 struct scan_control {
75 	/* How many pages shrink_list() should reclaim */
76 	unsigned long nr_to_reclaim;
77 
78 	/*
79 	 * Nodemask of nodes allowed by the caller. If NULL, all nodes
80 	 * are scanned.
81 	 */
82 	nodemask_t	*nodemask;
83 
84 	/*
85 	 * The memory cgroup that hit its limit and as a result is the
86 	 * primary target of this reclaim invocation.
87 	 */
88 	struct mem_cgroup *target_mem_cgroup;
89 
90 	/*
91 	 * Scan pressure balancing between anon and file LRUs
92 	 */
93 	unsigned long	anon_cost;
94 	unsigned long	file_cost;
95 
96 	/* Can active folios be deactivated as part of reclaim? */
97 #define DEACTIVATE_ANON 1
98 #define DEACTIVATE_FILE 2
99 	unsigned int may_deactivate:2;
100 	unsigned int force_deactivate:1;
101 	unsigned int skipped_deactivate:1;
102 
103 	/* Writepage batching in laptop mode; RECLAIM_WRITE */
104 	unsigned int may_writepage:1;
105 
106 	/* Can mapped folios be reclaimed? */
107 	unsigned int may_unmap:1;
108 
109 	/* Can folios be swapped as part of reclaim? */
110 	unsigned int may_swap:1;
111 
112 	/* Proactive reclaim invoked by userspace through memory.reclaim */
113 	unsigned int proactive:1;
114 
115 	/*
116 	 * Cgroup memory below memory.low is protected as long as we
117 	 * don't threaten to OOM. If any cgroup is reclaimed at
118 	 * reduced force or passed over entirely due to its memory.low
119 	 * setting (memcg_low_skipped), and nothing is reclaimed as a
120 	 * result, then go back for one more cycle that reclaims the protected
121 	 * memory (memcg_low_reclaim) to avert OOM.
122 	 */
123 	unsigned int memcg_low_reclaim:1;
124 	unsigned int memcg_low_skipped:1;
125 
126 	unsigned int hibernation_mode:1;
127 
128 	/* One of the zones is ready for compaction */
129 	unsigned int compaction_ready:1;
130 
131 	/* There is easily reclaimable cold cache in the current node */
132 	unsigned int cache_trim_mode:1;
133 
134 	/* The file folios on the current node are dangerously low */
135 	unsigned int file_is_tiny:1;
136 
137 	/* Always discard instead of demoting to lower tier memory */
138 	unsigned int no_demotion:1;
139 
140 	/* Allocation order */
141 	s8 order;
142 
143 	/* Scan (total_size >> priority) pages at once */
144 	s8 priority;
145 
146 	/* The highest zone to isolate folios for reclaim from */
147 	s8 reclaim_idx;
148 
149 	/* This context's GFP mask */
150 	gfp_t gfp_mask;
151 
152 	/* Incremented by the number of inactive pages that were scanned */
153 	unsigned long nr_scanned;
154 
155 	/* Number of pages freed so far during a call to shrink_zones() */
156 	unsigned long nr_reclaimed;
157 
158 	struct {
159 		unsigned int dirty;
160 		unsigned int unqueued_dirty;
161 		unsigned int congested;
162 		unsigned int writeback;
163 		unsigned int immediate;
164 		unsigned int file_taken;
165 		unsigned int taken;
166 	} nr;
167 
168 	/* for recording the reclaimed slab by now */
169 	struct reclaim_state reclaim_state;
170 };
171 
172 #ifdef ARCH_HAS_PREFETCHW
173 #define prefetchw_prev_lru_folio(_folio, _base, _field)			\
174 	do {								\
175 		if ((_folio)->lru.prev != _base) {			\
176 			struct folio *prev;				\
177 									\
178 			prev = lru_to_folio(&(_folio->lru));		\
179 			prefetchw(&prev->_field);			\
180 		}							\
181 	} while (0)
182 #else
183 #define prefetchw_prev_lru_folio(_folio, _base, _field) do { } while (0)
184 #endif
185 
186 /*
187  * From 0 .. 200.  Higher means more swappy.
188  */
189 int vm_swappiness = 60;
190 
191 LIST_HEAD(shrinker_list);
192 DECLARE_RWSEM(shrinker_rwsem);
193 
194 #ifdef CONFIG_MEMCG
195 static int shrinker_nr_max;
196 
197 /* The shrinker_info is expanded in a batch of BITS_PER_LONG */
198 static inline int shrinker_map_size(int nr_items)
199 {
200 	return (DIV_ROUND_UP(nr_items, BITS_PER_LONG) * sizeof(unsigned long));
201 }
202 
203 static inline int shrinker_defer_size(int nr_items)
204 {
205 	return (round_up(nr_items, BITS_PER_LONG) * sizeof(atomic_long_t));
206 }
207 
208 static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg,
209 						     int nid)
210 {
211 	return rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_info,
212 					 lockdep_is_held(&shrinker_rwsem));
213 }
214 
215 static int expand_one_shrinker_info(struct mem_cgroup *memcg,
216 				    int map_size, int defer_size,
217 				    int old_map_size, int old_defer_size,
218 				    int new_nr_max)
219 {
220 	struct shrinker_info *new, *old;
221 	struct mem_cgroup_per_node *pn;
222 	int nid;
223 	int size = map_size + defer_size;
224 
225 	for_each_node(nid) {
226 		pn = memcg->nodeinfo[nid];
227 		old = shrinker_info_protected(memcg, nid);
228 		/* Not yet online memcg */
229 		if (!old)
230 			return 0;
231 
232 		/* Already expanded this shrinker_info */
233 		if (new_nr_max <= old->map_nr_max)
234 			continue;
235 
236 		new = kvmalloc_node(sizeof(*new) + size, GFP_KERNEL, nid);
237 		if (!new)
238 			return -ENOMEM;
239 
240 		new->nr_deferred = (atomic_long_t *)(new + 1);
241 		new->map = (void *)new->nr_deferred + defer_size;
242 		new->map_nr_max = new_nr_max;
243 
244 		/* map: set all old bits, clear all new bits */
245 		memset(new->map, (int)0xff, old_map_size);
246 		memset((void *)new->map + old_map_size, 0, map_size - old_map_size);
247 		/* nr_deferred: copy old values, clear all new values */
248 		memcpy(new->nr_deferred, old->nr_deferred, old_defer_size);
249 		memset((void *)new->nr_deferred + old_defer_size, 0,
250 		       defer_size - old_defer_size);
251 
252 		rcu_assign_pointer(pn->shrinker_info, new);
253 		kvfree_rcu(old, rcu);
254 	}
255 
256 	return 0;
257 }
258 
259 void free_shrinker_info(struct mem_cgroup *memcg)
260 {
261 	struct mem_cgroup_per_node *pn;
262 	struct shrinker_info *info;
263 	int nid;
264 
265 	for_each_node(nid) {
266 		pn = memcg->nodeinfo[nid];
267 		info = rcu_dereference_protected(pn->shrinker_info, true);
268 		kvfree(info);
269 		rcu_assign_pointer(pn->shrinker_info, NULL);
270 	}
271 }
272 
273 int alloc_shrinker_info(struct mem_cgroup *memcg)
274 {
275 	struct shrinker_info *info;
276 	int nid, size, ret = 0;
277 	int map_size, defer_size = 0;
278 
279 	down_write(&shrinker_rwsem);
280 	map_size = shrinker_map_size(shrinker_nr_max);
281 	defer_size = shrinker_defer_size(shrinker_nr_max);
282 	size = map_size + defer_size;
283 	for_each_node(nid) {
284 		info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid);
285 		if (!info) {
286 			free_shrinker_info(memcg);
287 			ret = -ENOMEM;
288 			break;
289 		}
290 		info->nr_deferred = (atomic_long_t *)(info + 1);
291 		info->map = (void *)info->nr_deferred + defer_size;
292 		info->map_nr_max = shrinker_nr_max;
293 		rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info);
294 	}
295 	up_write(&shrinker_rwsem);
296 
297 	return ret;
298 }
299 
300 static int expand_shrinker_info(int new_id)
301 {
302 	int ret = 0;
303 	int new_nr_max = round_up(new_id + 1, BITS_PER_LONG);
304 	int map_size, defer_size = 0;
305 	int old_map_size, old_defer_size = 0;
306 	struct mem_cgroup *memcg;
307 
308 	if (!root_mem_cgroup)
309 		goto out;
310 
311 	lockdep_assert_held(&shrinker_rwsem);
312 
313 	map_size = shrinker_map_size(new_nr_max);
314 	defer_size = shrinker_defer_size(new_nr_max);
315 	old_map_size = shrinker_map_size(shrinker_nr_max);
316 	old_defer_size = shrinker_defer_size(shrinker_nr_max);
317 
318 	memcg = mem_cgroup_iter(NULL, NULL, NULL);
319 	do {
320 		ret = expand_one_shrinker_info(memcg, map_size, defer_size,
321 					       old_map_size, old_defer_size,
322 					       new_nr_max);
323 		if (ret) {
324 			mem_cgroup_iter_break(NULL, memcg);
325 			goto out;
326 		}
327 	} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
328 out:
329 	if (!ret)
330 		shrinker_nr_max = new_nr_max;
331 
332 	return ret;
333 }
334 
335 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id)
336 {
337 	if (shrinker_id >= 0 && memcg && !mem_cgroup_is_root(memcg)) {
338 		struct shrinker_info *info;
339 
340 		rcu_read_lock();
341 		info = rcu_dereference(memcg->nodeinfo[nid]->shrinker_info);
342 		if (!WARN_ON_ONCE(shrinker_id >= info->map_nr_max)) {
343 			/* Pairs with smp mb in shrink_slab() */
344 			smp_mb__before_atomic();
345 			set_bit(shrinker_id, info->map);
346 		}
347 		rcu_read_unlock();
348 	}
349 }
350 
351 static DEFINE_IDR(shrinker_idr);
352 
353 static int prealloc_memcg_shrinker(struct shrinker *shrinker)
354 {
355 	int id, ret = -ENOMEM;
356 
357 	if (mem_cgroup_disabled())
358 		return -ENOSYS;
359 
360 	down_write(&shrinker_rwsem);
361 	/* This may call shrinker, so it must use down_read_trylock() */
362 	id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL);
363 	if (id < 0)
364 		goto unlock;
365 
366 	if (id >= shrinker_nr_max) {
367 		if (expand_shrinker_info(id)) {
368 			idr_remove(&shrinker_idr, id);
369 			goto unlock;
370 		}
371 	}
372 	shrinker->id = id;
373 	ret = 0;
374 unlock:
375 	up_write(&shrinker_rwsem);
376 	return ret;
377 }
378 
379 static void unregister_memcg_shrinker(struct shrinker *shrinker)
380 {
381 	int id = shrinker->id;
382 
383 	BUG_ON(id < 0);
384 
385 	lockdep_assert_held(&shrinker_rwsem);
386 
387 	idr_remove(&shrinker_idr, id);
388 }
389 
390 static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker,
391 				   struct mem_cgroup *memcg)
392 {
393 	struct shrinker_info *info;
394 
395 	info = shrinker_info_protected(memcg, nid);
396 	return atomic_long_xchg(&info->nr_deferred[shrinker->id], 0);
397 }
398 
399 static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker,
400 				  struct mem_cgroup *memcg)
401 {
402 	struct shrinker_info *info;
403 
404 	info = shrinker_info_protected(memcg, nid);
405 	return atomic_long_add_return(nr, &info->nr_deferred[shrinker->id]);
406 }
407 
408 void reparent_shrinker_deferred(struct mem_cgroup *memcg)
409 {
410 	int i, nid;
411 	long nr;
412 	struct mem_cgroup *parent;
413 	struct shrinker_info *child_info, *parent_info;
414 
415 	parent = parent_mem_cgroup(memcg);
416 	if (!parent)
417 		parent = root_mem_cgroup;
418 
419 	/* Prevent from concurrent shrinker_info expand */
420 	down_read(&shrinker_rwsem);
421 	for_each_node(nid) {
422 		child_info = shrinker_info_protected(memcg, nid);
423 		parent_info = shrinker_info_protected(parent, nid);
424 		for (i = 0; i < child_info->map_nr_max; i++) {
425 			nr = atomic_long_read(&child_info->nr_deferred[i]);
426 			atomic_long_add(nr, &parent_info->nr_deferred[i]);
427 		}
428 	}
429 	up_read(&shrinker_rwsem);
430 }
431 
432 /* Returns true for reclaim through cgroup limits or cgroup interfaces. */
433 static bool cgroup_reclaim(struct scan_control *sc)
434 {
435 	return sc->target_mem_cgroup;
436 }
437 
438 /*
439  * Returns true for reclaim on the root cgroup. This is true for direct
440  * allocator reclaim and reclaim through cgroup interfaces on the root cgroup.
441  */
442 static bool root_reclaim(struct scan_control *sc)
443 {
444 	return !sc->target_mem_cgroup || mem_cgroup_is_root(sc->target_mem_cgroup);
445 }
446 
447 /**
448  * writeback_throttling_sane - is the usual dirty throttling mechanism available?
449  * @sc: scan_control in question
450  *
451  * The normal page dirty throttling mechanism in balance_dirty_pages() is
452  * completely broken with the legacy memcg and direct stalling in
453  * shrink_folio_list() is used for throttling instead, which lacks all the
454  * niceties such as fairness, adaptive pausing, bandwidth proportional
455  * allocation and configurability.
456  *
457  * This function tests whether the vmscan currently in progress can assume
458  * that the normal dirty throttling mechanism is operational.
459  */
460 static bool writeback_throttling_sane(struct scan_control *sc)
461 {
462 	if (!cgroup_reclaim(sc))
463 		return true;
464 #ifdef CONFIG_CGROUP_WRITEBACK
465 	if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
466 		return true;
467 #endif
468 	return false;
469 }
470 #else
471 static int prealloc_memcg_shrinker(struct shrinker *shrinker)
472 {
473 	return -ENOSYS;
474 }
475 
476 static void unregister_memcg_shrinker(struct shrinker *shrinker)
477 {
478 }
479 
480 static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker,
481 				   struct mem_cgroup *memcg)
482 {
483 	return 0;
484 }
485 
486 static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker,
487 				  struct mem_cgroup *memcg)
488 {
489 	return 0;
490 }
491 
492 static bool cgroup_reclaim(struct scan_control *sc)
493 {
494 	return false;
495 }
496 
497 static bool root_reclaim(struct scan_control *sc)
498 {
499 	return true;
500 }
501 
502 static bool writeback_throttling_sane(struct scan_control *sc)
503 {
504 	return true;
505 }
506 #endif
507 
508 static void set_task_reclaim_state(struct task_struct *task,
509 				   struct reclaim_state *rs)
510 {
511 	/* Check for an overwrite */
512 	WARN_ON_ONCE(rs && task->reclaim_state);
513 
514 	/* Check for the nulling of an already-nulled member */
515 	WARN_ON_ONCE(!rs && !task->reclaim_state);
516 
517 	task->reclaim_state = rs;
518 }
519 
520 /*
521  * flush_reclaim_state(): add pages reclaimed outside of LRU-based reclaim to
522  * scan_control->nr_reclaimed.
523  */
524 static void flush_reclaim_state(struct scan_control *sc)
525 {
526 	/*
527 	 * Currently, reclaim_state->reclaimed includes three types of pages
528 	 * freed outside of vmscan:
529 	 * (1) Slab pages.
530 	 * (2) Clean file pages from pruned inodes (on highmem systems).
531 	 * (3) XFS freed buffer pages.
532 	 *
533 	 * For all of these cases, we cannot universally link the pages to a
534 	 * single memcg. For example, a memcg-aware shrinker can free one object
535 	 * charged to the target memcg, causing an entire page to be freed.
536 	 * If we count the entire page as reclaimed from the memcg, we end up
537 	 * overestimating the reclaimed amount (potentially under-reclaiming).
538 	 *
539 	 * Only count such pages for global reclaim to prevent under-reclaiming
540 	 * from the target memcg; preventing unnecessary retries during memcg
541 	 * charging and false positives from proactive reclaim.
542 	 *
543 	 * For uncommon cases where the freed pages were actually mostly
544 	 * charged to the target memcg, we end up underestimating the reclaimed
545 	 * amount. This should be fine. The freed pages will be uncharged
546 	 * anyway, even if they are not counted here properly, and we will be
547 	 * able to make forward progress in charging (which is usually in a
548 	 * retry loop).
549 	 *
550 	 * We can go one step further, and report the uncharged objcg pages in
551 	 * memcg reclaim, to make reporting more accurate and reduce
552 	 * underestimation, but it's probably not worth the complexity for now.
553 	 */
554 	if (current->reclaim_state && root_reclaim(sc)) {
555 		sc->nr_reclaimed += current->reclaim_state->reclaimed;
556 		current->reclaim_state->reclaimed = 0;
557 	}
558 }
559 
560 static long xchg_nr_deferred(struct shrinker *shrinker,
561 			     struct shrink_control *sc)
562 {
563 	int nid = sc->nid;
564 
565 	if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
566 		nid = 0;
567 
568 	if (sc->memcg &&
569 	    (shrinker->flags & SHRINKER_MEMCG_AWARE))
570 		return xchg_nr_deferred_memcg(nid, shrinker,
571 					      sc->memcg);
572 
573 	return atomic_long_xchg(&shrinker->nr_deferred[nid], 0);
574 }
575 
576 
577 static long add_nr_deferred(long nr, struct shrinker *shrinker,
578 			    struct shrink_control *sc)
579 {
580 	int nid = sc->nid;
581 
582 	if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
583 		nid = 0;
584 
585 	if (sc->memcg &&
586 	    (shrinker->flags & SHRINKER_MEMCG_AWARE))
587 		return add_nr_deferred_memcg(nr, nid, shrinker,
588 					     sc->memcg);
589 
590 	return atomic_long_add_return(nr, &shrinker->nr_deferred[nid]);
591 }
592 
593 static bool can_demote(int nid, struct scan_control *sc)
594 {
595 	if (!numa_demotion_enabled)
596 		return false;
597 	if (sc && sc->no_demotion)
598 		return false;
599 	if (next_demotion_node(nid) == NUMA_NO_NODE)
600 		return false;
601 
602 	return true;
603 }
604 
605 static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
606 					  int nid,
607 					  struct scan_control *sc)
608 {
609 	if (memcg == NULL) {
610 		/*
611 		 * For non-memcg reclaim, is there
612 		 * space in any swap device?
613 		 */
614 		if (get_nr_swap_pages() > 0)
615 			return true;
616 	} else {
617 		/* Is the memcg below its swap limit? */
618 		if (mem_cgroup_get_nr_swap_pages(memcg) > 0)
619 			return true;
620 	}
621 
622 	/*
623 	 * The page can not be swapped.
624 	 *
625 	 * Can it be reclaimed from this node via demotion?
626 	 */
627 	return can_demote(nid, sc);
628 }
629 
630 /*
631  * This misses isolated folios which are not accounted for to save counters.
632  * As the data only determines if reclaim or compaction continues, it is
633  * not expected that isolated folios will be a dominating factor.
634  */
635 unsigned long zone_reclaimable_pages(struct zone *zone)
636 {
637 	unsigned long nr;
638 
639 	nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) +
640 		zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE);
641 	if (can_reclaim_anon_pages(NULL, zone_to_nid(zone), NULL))
642 		nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) +
643 			zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON);
644 
645 	return nr;
646 }
647 
648 /**
649  * lruvec_lru_size -  Returns the number of pages on the given LRU list.
650  * @lruvec: lru vector
651  * @lru: lru to use
652  * @zone_idx: zones to consider (use MAX_NR_ZONES - 1 for the whole LRU list)
653  */
654 static unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru,
655 				     int zone_idx)
656 {
657 	unsigned long size = 0;
658 	int zid;
659 
660 	for (zid = 0; zid <= zone_idx; zid++) {
661 		struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid];
662 
663 		if (!managed_zone(zone))
664 			continue;
665 
666 		if (!mem_cgroup_disabled())
667 			size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid);
668 		else
669 			size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru);
670 	}
671 	return size;
672 }
673 
674 /*
675  * Add a shrinker callback to be called from the vm.
676  */
677 static int __prealloc_shrinker(struct shrinker *shrinker)
678 {
679 	unsigned int size;
680 	int err;
681 
682 	if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
683 		err = prealloc_memcg_shrinker(shrinker);
684 		if (err != -ENOSYS)
685 			return err;
686 
687 		shrinker->flags &= ~SHRINKER_MEMCG_AWARE;
688 	}
689 
690 	size = sizeof(*shrinker->nr_deferred);
691 	if (shrinker->flags & SHRINKER_NUMA_AWARE)
692 		size *= nr_node_ids;
693 
694 	shrinker->nr_deferred = kzalloc(size, GFP_KERNEL);
695 	if (!shrinker->nr_deferred)
696 		return -ENOMEM;
697 
698 	return 0;
699 }
700 
701 #ifdef CONFIG_SHRINKER_DEBUG
702 int prealloc_shrinker(struct shrinker *shrinker, const char *fmt, ...)
703 {
704 	va_list ap;
705 	int err;
706 
707 	va_start(ap, fmt);
708 	shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap);
709 	va_end(ap);
710 	if (!shrinker->name)
711 		return -ENOMEM;
712 
713 	err = __prealloc_shrinker(shrinker);
714 	if (err) {
715 		kfree_const(shrinker->name);
716 		shrinker->name = NULL;
717 	}
718 
719 	return err;
720 }
721 #else
722 int prealloc_shrinker(struct shrinker *shrinker, const char *fmt, ...)
723 {
724 	return __prealloc_shrinker(shrinker);
725 }
726 #endif
727 
728 void free_prealloced_shrinker(struct shrinker *shrinker)
729 {
730 #ifdef CONFIG_SHRINKER_DEBUG
731 	kfree_const(shrinker->name);
732 	shrinker->name = NULL;
733 #endif
734 	if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
735 		down_write(&shrinker_rwsem);
736 		unregister_memcg_shrinker(shrinker);
737 		up_write(&shrinker_rwsem);
738 		return;
739 	}
740 
741 	kfree(shrinker->nr_deferred);
742 	shrinker->nr_deferred = NULL;
743 }
744 
745 void register_shrinker_prepared(struct shrinker *shrinker)
746 {
747 	down_write(&shrinker_rwsem);
748 	list_add_tail(&shrinker->list, &shrinker_list);
749 	shrinker->flags |= SHRINKER_REGISTERED;
750 	shrinker_debugfs_add(shrinker);
751 	up_write(&shrinker_rwsem);
752 }
753 
754 static int __register_shrinker(struct shrinker *shrinker)
755 {
756 	int err = __prealloc_shrinker(shrinker);
757 
758 	if (err)
759 		return err;
760 	register_shrinker_prepared(shrinker);
761 	return 0;
762 }
763 
764 #ifdef CONFIG_SHRINKER_DEBUG
765 int register_shrinker(struct shrinker *shrinker, const char *fmt, ...)
766 {
767 	va_list ap;
768 	int err;
769 
770 	va_start(ap, fmt);
771 	shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap);
772 	va_end(ap);
773 	if (!shrinker->name)
774 		return -ENOMEM;
775 
776 	err = __register_shrinker(shrinker);
777 	if (err) {
778 		kfree_const(shrinker->name);
779 		shrinker->name = NULL;
780 	}
781 	return err;
782 }
783 #else
784 int register_shrinker(struct shrinker *shrinker, const char *fmt, ...)
785 {
786 	return __register_shrinker(shrinker);
787 }
788 #endif
789 EXPORT_SYMBOL(register_shrinker);
790 
791 /*
792  * Remove one
793  */
794 void unregister_shrinker(struct shrinker *shrinker)
795 {
796 	struct dentry *debugfs_entry;
797 	int debugfs_id;
798 
799 	if (!(shrinker->flags & SHRINKER_REGISTERED))
800 		return;
801 
802 	down_write(&shrinker_rwsem);
803 	list_del(&shrinker->list);
804 	shrinker->flags &= ~SHRINKER_REGISTERED;
805 	if (shrinker->flags & SHRINKER_MEMCG_AWARE)
806 		unregister_memcg_shrinker(shrinker);
807 	debugfs_entry = shrinker_debugfs_detach(shrinker, &debugfs_id);
808 	up_write(&shrinker_rwsem);
809 
810 	shrinker_debugfs_remove(debugfs_entry, debugfs_id);
811 
812 	kfree(shrinker->nr_deferred);
813 	shrinker->nr_deferred = NULL;
814 }
815 EXPORT_SYMBOL(unregister_shrinker);
816 
817 /**
818  * synchronize_shrinkers - Wait for all running shrinkers to complete.
819  *
820  * This is equivalent to calling unregister_shrink() and register_shrinker(),
821  * but atomically and with less overhead. This is useful to guarantee that all
822  * shrinker invocations have seen an update, before freeing memory, similar to
823  * rcu.
824  */
825 void synchronize_shrinkers(void)
826 {
827 	down_write(&shrinker_rwsem);
828 	up_write(&shrinker_rwsem);
829 }
830 EXPORT_SYMBOL(synchronize_shrinkers);
831 
832 #define SHRINK_BATCH 128
833 
834 static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
835 				    struct shrinker *shrinker, int priority)
836 {
837 	unsigned long freed = 0;
838 	unsigned long long delta;
839 	long total_scan;
840 	long freeable;
841 	long nr;
842 	long new_nr;
843 	long batch_size = shrinker->batch ? shrinker->batch
844 					  : SHRINK_BATCH;
845 	long scanned = 0, next_deferred;
846 
847 	freeable = shrinker->count_objects(shrinker, shrinkctl);
848 	if (freeable == 0 || freeable == SHRINK_EMPTY)
849 		return freeable;
850 
851 	/*
852 	 * copy the current shrinker scan count into a local variable
853 	 * and zero it so that other concurrent shrinker invocations
854 	 * don't also do this scanning work.
855 	 */
856 	nr = xchg_nr_deferred(shrinker, shrinkctl);
857 
858 	if (shrinker->seeks) {
859 		delta = freeable >> priority;
860 		delta *= 4;
861 		do_div(delta, shrinker->seeks);
862 	} else {
863 		/*
864 		 * These objects don't require any IO to create. Trim
865 		 * them aggressively under memory pressure to keep
866 		 * them from causing refetches in the IO caches.
867 		 */
868 		delta = freeable / 2;
869 	}
870 
871 	total_scan = nr >> priority;
872 	total_scan += delta;
873 	total_scan = min(total_scan, (2 * freeable));
874 
875 	trace_mm_shrink_slab_start(shrinker, shrinkctl, nr,
876 				   freeable, delta, total_scan, priority);
877 
878 	/*
879 	 * Normally, we should not scan less than batch_size objects in one
880 	 * pass to avoid too frequent shrinker calls, but if the slab has less
881 	 * than batch_size objects in total and we are really tight on memory,
882 	 * we will try to reclaim all available objects, otherwise we can end
883 	 * up failing allocations although there are plenty of reclaimable
884 	 * objects spread over several slabs with usage less than the
885 	 * batch_size.
886 	 *
887 	 * We detect the "tight on memory" situations by looking at the total
888 	 * number of objects we want to scan (total_scan). If it is greater
889 	 * than the total number of objects on slab (freeable), we must be
890 	 * scanning at high prio and therefore should try to reclaim as much as
891 	 * possible.
892 	 */
893 	while (total_scan >= batch_size ||
894 	       total_scan >= freeable) {
895 		unsigned long ret;
896 		unsigned long nr_to_scan = min(batch_size, total_scan);
897 
898 		shrinkctl->nr_to_scan = nr_to_scan;
899 		shrinkctl->nr_scanned = nr_to_scan;
900 		ret = shrinker->scan_objects(shrinker, shrinkctl);
901 		if (ret == SHRINK_STOP)
902 			break;
903 		freed += ret;
904 
905 		count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned);
906 		total_scan -= shrinkctl->nr_scanned;
907 		scanned += shrinkctl->nr_scanned;
908 
909 		cond_resched();
910 	}
911 
912 	/*
913 	 * The deferred work is increased by any new work (delta) that wasn't
914 	 * done, decreased by old deferred work that was done now.
915 	 *
916 	 * And it is capped to two times of the freeable items.
917 	 */
918 	next_deferred = max_t(long, (nr + delta - scanned), 0);
919 	next_deferred = min(next_deferred, (2 * freeable));
920 
921 	/*
922 	 * move the unused scan count back into the shrinker in a
923 	 * manner that handles concurrent updates.
924 	 */
925 	new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
926 
927 	trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan);
928 	return freed;
929 }
930 
931 #ifdef CONFIG_MEMCG
932 static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
933 			struct mem_cgroup *memcg, int priority)
934 {
935 	struct shrinker_info *info;
936 	unsigned long ret, freed = 0;
937 	int i;
938 
939 	if (!mem_cgroup_online(memcg))
940 		return 0;
941 
942 	if (!down_read_trylock(&shrinker_rwsem))
943 		return 0;
944 
945 	info = shrinker_info_protected(memcg, nid);
946 	if (unlikely(!info))
947 		goto unlock;
948 
949 	for_each_set_bit(i, info->map, info->map_nr_max) {
950 		struct shrink_control sc = {
951 			.gfp_mask = gfp_mask,
952 			.nid = nid,
953 			.memcg = memcg,
954 		};
955 		struct shrinker *shrinker;
956 
957 		shrinker = idr_find(&shrinker_idr, i);
958 		if (unlikely(!shrinker || !(shrinker->flags & SHRINKER_REGISTERED))) {
959 			if (!shrinker)
960 				clear_bit(i, info->map);
961 			continue;
962 		}
963 
964 		/* Call non-slab shrinkers even though kmem is disabled */
965 		if (!memcg_kmem_online() &&
966 		    !(shrinker->flags & SHRINKER_NONSLAB))
967 			continue;
968 
969 		ret = do_shrink_slab(&sc, shrinker, priority);
970 		if (ret == SHRINK_EMPTY) {
971 			clear_bit(i, info->map);
972 			/*
973 			 * After the shrinker reported that it had no objects to
974 			 * free, but before we cleared the corresponding bit in
975 			 * the memcg shrinker map, a new object might have been
976 			 * added. To make sure, we have the bit set in this
977 			 * case, we invoke the shrinker one more time and reset
978 			 * the bit if it reports that it is not empty anymore.
979 			 * The memory barrier here pairs with the barrier in
980 			 * set_shrinker_bit():
981 			 *
982 			 * list_lru_add()     shrink_slab_memcg()
983 			 *   list_add_tail()    clear_bit()
984 			 *   <MB>               <MB>
985 			 *   set_bit()          do_shrink_slab()
986 			 */
987 			smp_mb__after_atomic();
988 			ret = do_shrink_slab(&sc, shrinker, priority);
989 			if (ret == SHRINK_EMPTY)
990 				ret = 0;
991 			else
992 				set_shrinker_bit(memcg, nid, i);
993 		}
994 		freed += ret;
995 
996 		if (rwsem_is_contended(&shrinker_rwsem)) {
997 			freed = freed ? : 1;
998 			break;
999 		}
1000 	}
1001 unlock:
1002 	up_read(&shrinker_rwsem);
1003 	return freed;
1004 }
1005 #else /* CONFIG_MEMCG */
1006 static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
1007 			struct mem_cgroup *memcg, int priority)
1008 {
1009 	return 0;
1010 }
1011 #endif /* CONFIG_MEMCG */
1012 
1013 /**
1014  * shrink_slab - shrink slab caches
1015  * @gfp_mask: allocation context
1016  * @nid: node whose slab caches to target
1017  * @memcg: memory cgroup whose slab caches to target
1018  * @priority: the reclaim priority
1019  *
1020  * Call the shrink functions to age shrinkable caches.
1021  *
1022  * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set,
1023  * unaware shrinkers will receive a node id of 0 instead.
1024  *
1025  * @memcg specifies the memory cgroup to target. Unaware shrinkers
1026  * are called only if it is the root cgroup.
1027  *
1028  * @priority is sc->priority, we take the number of objects and >> by priority
1029  * in order to get the scan target.
1030  *
1031  * Returns the number of reclaimed slab objects.
1032  */
1033 static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
1034 				 struct mem_cgroup *memcg,
1035 				 int priority)
1036 {
1037 	unsigned long ret, freed = 0;
1038 	struct shrinker *shrinker;
1039 
1040 	/*
1041 	 * The root memcg might be allocated even though memcg is disabled
1042 	 * via "cgroup_disable=memory" boot parameter.  This could make
1043 	 * mem_cgroup_is_root() return false, then just run memcg slab
1044 	 * shrink, but skip global shrink.  This may result in premature
1045 	 * oom.
1046 	 */
1047 	if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg))
1048 		return shrink_slab_memcg(gfp_mask, nid, memcg, priority);
1049 
1050 	if (!down_read_trylock(&shrinker_rwsem))
1051 		goto out;
1052 
1053 	list_for_each_entry(shrinker, &shrinker_list, list) {
1054 		struct shrink_control sc = {
1055 			.gfp_mask = gfp_mask,
1056 			.nid = nid,
1057 			.memcg = memcg,
1058 		};
1059 
1060 		ret = do_shrink_slab(&sc, shrinker, priority);
1061 		if (ret == SHRINK_EMPTY)
1062 			ret = 0;
1063 		freed += ret;
1064 		/*
1065 		 * Bail out if someone want to register a new shrinker to
1066 		 * prevent the registration from being stalled for long periods
1067 		 * by parallel ongoing shrinking.
1068 		 */
1069 		if (rwsem_is_contended(&shrinker_rwsem)) {
1070 			freed = freed ? : 1;
1071 			break;
1072 		}
1073 	}
1074 
1075 	up_read(&shrinker_rwsem);
1076 out:
1077 	cond_resched();
1078 	return freed;
1079 }
1080 
1081 static unsigned long drop_slab_node(int nid)
1082 {
1083 	unsigned long freed = 0;
1084 	struct mem_cgroup *memcg = NULL;
1085 
1086 	memcg = mem_cgroup_iter(NULL, NULL, NULL);
1087 	do {
1088 		freed += shrink_slab(GFP_KERNEL, nid, memcg, 0);
1089 	} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
1090 
1091 	return freed;
1092 }
1093 
1094 void drop_slab(void)
1095 {
1096 	int nid;
1097 	int shift = 0;
1098 	unsigned long freed;
1099 
1100 	do {
1101 		freed = 0;
1102 		for_each_online_node(nid) {
1103 			if (fatal_signal_pending(current))
1104 				return;
1105 
1106 			freed += drop_slab_node(nid);
1107 		}
1108 	} while ((freed >> shift++) > 1);
1109 }
1110 
1111 static int reclaimer_offset(void)
1112 {
1113 	BUILD_BUG_ON(PGSTEAL_DIRECT - PGSTEAL_KSWAPD !=
1114 			PGDEMOTE_DIRECT - PGDEMOTE_KSWAPD);
1115 	BUILD_BUG_ON(PGSTEAL_DIRECT - PGSTEAL_KSWAPD !=
1116 			PGSCAN_DIRECT - PGSCAN_KSWAPD);
1117 	BUILD_BUG_ON(PGSTEAL_KHUGEPAGED - PGSTEAL_KSWAPD !=
1118 			PGDEMOTE_KHUGEPAGED - PGDEMOTE_KSWAPD);
1119 	BUILD_BUG_ON(PGSTEAL_KHUGEPAGED - PGSTEAL_KSWAPD !=
1120 			PGSCAN_KHUGEPAGED - PGSCAN_KSWAPD);
1121 
1122 	if (current_is_kswapd())
1123 		return 0;
1124 	if (current_is_khugepaged())
1125 		return PGSTEAL_KHUGEPAGED - PGSTEAL_KSWAPD;
1126 	return PGSTEAL_DIRECT - PGSTEAL_KSWAPD;
1127 }
1128 
1129 static inline int is_page_cache_freeable(struct folio *folio)
1130 {
1131 	/*
1132 	 * A freeable page cache folio is referenced only by the caller
1133 	 * that isolated the folio, the page cache and optional filesystem
1134 	 * private data at folio->private.
1135 	 */
1136 	return folio_ref_count(folio) - folio_test_private(folio) ==
1137 		1 + folio_nr_pages(folio);
1138 }
1139 
1140 /*
1141  * We detected a synchronous write error writing a folio out.  Probably
1142  * -ENOSPC.  We need to propagate that into the address_space for a subsequent
1143  * fsync(), msync() or close().
1144  *
1145  * The tricky part is that after writepage we cannot touch the mapping: nothing
1146  * prevents it from being freed up.  But we have a ref on the folio and once
1147  * that folio is locked, the mapping is pinned.
1148  *
1149  * We're allowed to run sleeping folio_lock() here because we know the caller has
1150  * __GFP_FS.
1151  */
1152 static void handle_write_error(struct address_space *mapping,
1153 				struct folio *folio, int error)
1154 {
1155 	folio_lock(folio);
1156 	if (folio_mapping(folio) == mapping)
1157 		mapping_set_error(mapping, error);
1158 	folio_unlock(folio);
1159 }
1160 
1161 static bool skip_throttle_noprogress(pg_data_t *pgdat)
1162 {
1163 	int reclaimable = 0, write_pending = 0;
1164 	int i;
1165 
1166 	/*
1167 	 * If kswapd is disabled, reschedule if necessary but do not
1168 	 * throttle as the system is likely near OOM.
1169 	 */
1170 	if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
1171 		return true;
1172 
1173 	/*
1174 	 * If there are a lot of dirty/writeback folios then do not
1175 	 * throttle as throttling will occur when the folios cycle
1176 	 * towards the end of the LRU if still under writeback.
1177 	 */
1178 	for (i = 0; i < MAX_NR_ZONES; i++) {
1179 		struct zone *zone = pgdat->node_zones + i;
1180 
1181 		if (!managed_zone(zone))
1182 			continue;
1183 
1184 		reclaimable += zone_reclaimable_pages(zone);
1185 		write_pending += zone_page_state_snapshot(zone,
1186 						  NR_ZONE_WRITE_PENDING);
1187 	}
1188 	if (2 * write_pending <= reclaimable)
1189 		return true;
1190 
1191 	return false;
1192 }
1193 
1194 void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason)
1195 {
1196 	wait_queue_head_t *wqh = &pgdat->reclaim_wait[reason];
1197 	long timeout, ret;
1198 	DEFINE_WAIT(wait);
1199 
1200 	/*
1201 	 * Do not throttle user workers, kthreads other than kswapd or
1202 	 * workqueues. They may be required for reclaim to make
1203 	 * forward progress (e.g. journalling workqueues or kthreads).
1204 	 */
1205 	if (!current_is_kswapd() &&
1206 	    current->flags & (PF_USER_WORKER|PF_KTHREAD)) {
1207 		cond_resched();
1208 		return;
1209 	}
1210 
1211 	/*
1212 	 * These figures are pulled out of thin air.
1213 	 * VMSCAN_THROTTLE_ISOLATED is a transient condition based on too many
1214 	 * parallel reclaimers which is a short-lived event so the timeout is
1215 	 * short. Failing to make progress or waiting on writeback are
1216 	 * potentially long-lived events so use a longer timeout. This is shaky
1217 	 * logic as a failure to make progress could be due to anything from
1218 	 * writeback to a slow device to excessive referenced folios at the tail
1219 	 * of the inactive LRU.
1220 	 */
1221 	switch(reason) {
1222 	case VMSCAN_THROTTLE_WRITEBACK:
1223 		timeout = HZ/10;
1224 
1225 		if (atomic_inc_return(&pgdat->nr_writeback_throttled) == 1) {
1226 			WRITE_ONCE(pgdat->nr_reclaim_start,
1227 				node_page_state(pgdat, NR_THROTTLED_WRITTEN));
1228 		}
1229 
1230 		break;
1231 	case VMSCAN_THROTTLE_CONGESTED:
1232 		fallthrough;
1233 	case VMSCAN_THROTTLE_NOPROGRESS:
1234 		if (skip_throttle_noprogress(pgdat)) {
1235 			cond_resched();
1236 			return;
1237 		}
1238 
1239 		timeout = 1;
1240 
1241 		break;
1242 	case VMSCAN_THROTTLE_ISOLATED:
1243 		timeout = HZ/50;
1244 		break;
1245 	default:
1246 		WARN_ON_ONCE(1);
1247 		timeout = HZ;
1248 		break;
1249 	}
1250 
1251 	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
1252 	ret = schedule_timeout(timeout);
1253 	finish_wait(wqh, &wait);
1254 
1255 	if (reason == VMSCAN_THROTTLE_WRITEBACK)
1256 		atomic_dec(&pgdat->nr_writeback_throttled);
1257 
1258 	trace_mm_vmscan_throttled(pgdat->node_id, jiffies_to_usecs(timeout),
1259 				jiffies_to_usecs(timeout - ret),
1260 				reason);
1261 }
1262 
1263 /*
1264  * Account for folios written if tasks are throttled waiting on dirty
1265  * folios to clean. If enough folios have been cleaned since throttling
1266  * started then wakeup the throttled tasks.
1267  */
1268 void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio,
1269 							int nr_throttled)
1270 {
1271 	unsigned long nr_written;
1272 
1273 	node_stat_add_folio(folio, NR_THROTTLED_WRITTEN);
1274 
1275 	/*
1276 	 * This is an inaccurate read as the per-cpu deltas may not
1277 	 * be synchronised. However, given that the system is
1278 	 * writeback throttled, it is not worth taking the penalty
1279 	 * of getting an accurate count. At worst, the throttle
1280 	 * timeout guarantees forward progress.
1281 	 */
1282 	nr_written = node_page_state(pgdat, NR_THROTTLED_WRITTEN) -
1283 		READ_ONCE(pgdat->nr_reclaim_start);
1284 
1285 	if (nr_written > SWAP_CLUSTER_MAX * nr_throttled)
1286 		wake_up(&pgdat->reclaim_wait[VMSCAN_THROTTLE_WRITEBACK]);
1287 }
1288 
1289 /* possible outcome of pageout() */
1290 typedef enum {
1291 	/* failed to write folio out, folio is locked */
1292 	PAGE_KEEP,
1293 	/* move folio to the active list, folio is locked */
1294 	PAGE_ACTIVATE,
1295 	/* folio has been sent to the disk successfully, folio is unlocked */
1296 	PAGE_SUCCESS,
1297 	/* folio is clean and locked */
1298 	PAGE_CLEAN,
1299 } pageout_t;
1300 
1301 /*
1302  * pageout is called by shrink_folio_list() for each dirty folio.
1303  * Calls ->writepage().
1304  */
1305 static pageout_t pageout(struct folio *folio, struct address_space *mapping,
1306 			 struct swap_iocb **plug)
1307 {
1308 	/*
1309 	 * If the folio is dirty, only perform writeback if that write
1310 	 * will be non-blocking.  To prevent this allocation from being
1311 	 * stalled by pagecache activity.  But note that there may be
1312 	 * stalls if we need to run get_block().  We could test
1313 	 * PagePrivate for that.
1314 	 *
1315 	 * If this process is currently in __generic_file_write_iter() against
1316 	 * this folio's queue, we can perform writeback even if that
1317 	 * will block.
1318 	 *
1319 	 * If the folio is swapcache, write it back even if that would
1320 	 * block, for some throttling. This happens by accident, because
1321 	 * swap_backing_dev_info is bust: it doesn't reflect the
1322 	 * congestion state of the swapdevs.  Easy to fix, if needed.
1323 	 */
1324 	if (!is_page_cache_freeable(folio))
1325 		return PAGE_KEEP;
1326 	if (!mapping) {
1327 		/*
1328 		 * Some data journaling orphaned folios can have
1329 		 * folio->mapping == NULL while being dirty with clean buffers.
1330 		 */
1331 		if (folio_test_private(folio)) {
1332 			if (try_to_free_buffers(folio)) {
1333 				folio_clear_dirty(folio);
1334 				pr_info("%s: orphaned folio\n", __func__);
1335 				return PAGE_CLEAN;
1336 			}
1337 		}
1338 		return PAGE_KEEP;
1339 	}
1340 	if (mapping->a_ops->writepage == NULL)
1341 		return PAGE_ACTIVATE;
1342 
1343 	if (folio_clear_dirty_for_io(folio)) {
1344 		int res;
1345 		struct writeback_control wbc = {
1346 			.sync_mode = WB_SYNC_NONE,
1347 			.nr_to_write = SWAP_CLUSTER_MAX,
1348 			.range_start = 0,
1349 			.range_end = LLONG_MAX,
1350 			.for_reclaim = 1,
1351 			.swap_plug = plug,
1352 		};
1353 
1354 		folio_set_reclaim(folio);
1355 		res = mapping->a_ops->writepage(&folio->page, &wbc);
1356 		if (res < 0)
1357 			handle_write_error(mapping, folio, res);
1358 		if (res == AOP_WRITEPAGE_ACTIVATE) {
1359 			folio_clear_reclaim(folio);
1360 			return PAGE_ACTIVATE;
1361 		}
1362 
1363 		if (!folio_test_writeback(folio)) {
1364 			/* synchronous write or broken a_ops? */
1365 			folio_clear_reclaim(folio);
1366 		}
1367 		trace_mm_vmscan_write_folio(folio);
1368 		node_stat_add_folio(folio, NR_VMSCAN_WRITE);
1369 		return PAGE_SUCCESS;
1370 	}
1371 
1372 	return PAGE_CLEAN;
1373 }
1374 
1375 /*
1376  * Same as remove_mapping, but if the folio is removed from the mapping, it
1377  * gets returned with a refcount of 0.
1378  */
1379 static int __remove_mapping(struct address_space *mapping, struct folio *folio,
1380 			    bool reclaimed, struct mem_cgroup *target_memcg)
1381 {
1382 	int refcount;
1383 	void *shadow = NULL;
1384 
1385 	BUG_ON(!folio_test_locked(folio));
1386 	BUG_ON(mapping != folio_mapping(folio));
1387 
1388 	if (!folio_test_swapcache(folio))
1389 		spin_lock(&mapping->host->i_lock);
1390 	xa_lock_irq(&mapping->i_pages);
1391 	/*
1392 	 * The non racy check for a busy folio.
1393 	 *
1394 	 * Must be careful with the order of the tests. When someone has
1395 	 * a ref to the folio, it may be possible that they dirty it then
1396 	 * drop the reference. So if the dirty flag is tested before the
1397 	 * refcount here, then the following race may occur:
1398 	 *
1399 	 * get_user_pages(&page);
1400 	 * [user mapping goes away]
1401 	 * write_to(page);
1402 	 *				!folio_test_dirty(folio)    [good]
1403 	 * folio_set_dirty(folio);
1404 	 * folio_put(folio);
1405 	 *				!refcount(folio)   [good, discard it]
1406 	 *
1407 	 * [oops, our write_to data is lost]
1408 	 *
1409 	 * Reversing the order of the tests ensures such a situation cannot
1410 	 * escape unnoticed. The smp_rmb is needed to ensure the folio->flags
1411 	 * load is not satisfied before that of folio->_refcount.
1412 	 *
1413 	 * Note that if the dirty flag is always set via folio_mark_dirty,
1414 	 * and thus under the i_pages lock, then this ordering is not required.
1415 	 */
1416 	refcount = 1 + folio_nr_pages(folio);
1417 	if (!folio_ref_freeze(folio, refcount))
1418 		goto cannot_free;
1419 	/* note: atomic_cmpxchg in folio_ref_freeze provides the smp_rmb */
1420 	if (unlikely(folio_test_dirty(folio))) {
1421 		folio_ref_unfreeze(folio, refcount);
1422 		goto cannot_free;
1423 	}
1424 
1425 	if (folio_test_swapcache(folio)) {
1426 		swp_entry_t swap = folio->swap;
1427 
1428 		if (reclaimed && !mapping_exiting(mapping))
1429 			shadow = workingset_eviction(folio, target_memcg);
1430 		__delete_from_swap_cache(folio, swap, shadow);
1431 		mem_cgroup_swapout(folio, swap);
1432 		xa_unlock_irq(&mapping->i_pages);
1433 		put_swap_folio(folio, swap);
1434 	} else {
1435 		void (*free_folio)(struct folio *);
1436 
1437 		free_folio = mapping->a_ops->free_folio;
1438 		/*
1439 		 * Remember a shadow entry for reclaimed file cache in
1440 		 * order to detect refaults, thus thrashing, later on.
1441 		 *
1442 		 * But don't store shadows in an address space that is
1443 		 * already exiting.  This is not just an optimization,
1444 		 * inode reclaim needs to empty out the radix tree or
1445 		 * the nodes are lost.  Don't plant shadows behind its
1446 		 * back.
1447 		 *
1448 		 * We also don't store shadows for DAX mappings because the
1449 		 * only page cache folios found in these are zero pages
1450 		 * covering holes, and because we don't want to mix DAX
1451 		 * exceptional entries and shadow exceptional entries in the
1452 		 * same address_space.
1453 		 */
1454 		if (reclaimed && folio_is_file_lru(folio) &&
1455 		    !mapping_exiting(mapping) && !dax_mapping(mapping))
1456 			shadow = workingset_eviction(folio, target_memcg);
1457 		__filemap_remove_folio(folio, shadow);
1458 		xa_unlock_irq(&mapping->i_pages);
1459 		if (mapping_shrinkable(mapping))
1460 			inode_add_lru(mapping->host);
1461 		spin_unlock(&mapping->host->i_lock);
1462 
1463 		if (free_folio)
1464 			free_folio(folio);
1465 	}
1466 
1467 	return 1;
1468 
1469 cannot_free:
1470 	xa_unlock_irq(&mapping->i_pages);
1471 	if (!folio_test_swapcache(folio))
1472 		spin_unlock(&mapping->host->i_lock);
1473 	return 0;
1474 }
1475 
1476 /**
1477  * remove_mapping() - Attempt to remove a folio from its mapping.
1478  * @mapping: The address space.
1479  * @folio: The folio to remove.
1480  *
1481  * If the folio is dirty, under writeback or if someone else has a ref
1482  * on it, removal will fail.
1483  * Return: The number of pages removed from the mapping.  0 if the folio
1484  * could not be removed.
1485  * Context: The caller should have a single refcount on the folio and
1486  * hold its lock.
1487  */
1488 long remove_mapping(struct address_space *mapping, struct folio *folio)
1489 {
1490 	if (__remove_mapping(mapping, folio, false, NULL)) {
1491 		/*
1492 		 * Unfreezing the refcount with 1 effectively
1493 		 * drops the pagecache ref for us without requiring another
1494 		 * atomic operation.
1495 		 */
1496 		folio_ref_unfreeze(folio, 1);
1497 		return folio_nr_pages(folio);
1498 	}
1499 	return 0;
1500 }
1501 
1502 /**
1503  * folio_putback_lru - Put previously isolated folio onto appropriate LRU list.
1504  * @folio: Folio to be returned to an LRU list.
1505  *
1506  * Add previously isolated @folio to appropriate LRU list.
1507  * The folio may still be unevictable for other reasons.
1508  *
1509  * Context: lru_lock must not be held, interrupts must be enabled.
1510  */
1511 void folio_putback_lru(struct folio *folio)
1512 {
1513 	folio_add_lru(folio);
1514 	folio_put(folio);		/* drop ref from isolate */
1515 }
1516 
1517 enum folio_references {
1518 	FOLIOREF_RECLAIM,
1519 	FOLIOREF_RECLAIM_CLEAN,
1520 	FOLIOREF_KEEP,
1521 	FOLIOREF_ACTIVATE,
1522 };
1523 
1524 static enum folio_references folio_check_references(struct folio *folio,
1525 						  struct scan_control *sc)
1526 {
1527 	int referenced_ptes, referenced_folio;
1528 	unsigned long vm_flags;
1529 
1530 	referenced_ptes = folio_referenced(folio, 1, sc->target_mem_cgroup,
1531 					   &vm_flags);
1532 	referenced_folio = folio_test_clear_referenced(folio);
1533 
1534 	/*
1535 	 * The supposedly reclaimable folio was found to be in a VM_LOCKED vma.
1536 	 * Let the folio, now marked Mlocked, be moved to the unevictable list.
1537 	 */
1538 	if (vm_flags & VM_LOCKED)
1539 		return FOLIOREF_ACTIVATE;
1540 
1541 	/* rmap lock contention: rotate */
1542 	if (referenced_ptes == -1)
1543 		return FOLIOREF_KEEP;
1544 
1545 	if (referenced_ptes) {
1546 		/*
1547 		 * All mapped folios start out with page table
1548 		 * references from the instantiating fault, so we need
1549 		 * to look twice if a mapped file/anon folio is used more
1550 		 * than once.
1551 		 *
1552 		 * Mark it and spare it for another trip around the
1553 		 * inactive list.  Another page table reference will
1554 		 * lead to its activation.
1555 		 *
1556 		 * Note: the mark is set for activated folios as well
1557 		 * so that recently deactivated but used folios are
1558 		 * quickly recovered.
1559 		 */
1560 		folio_set_referenced(folio);
1561 
1562 		if (referenced_folio || referenced_ptes > 1)
1563 			return FOLIOREF_ACTIVATE;
1564 
1565 		/*
1566 		 * Activate file-backed executable folios after first usage.
1567 		 */
1568 		if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio))
1569 			return FOLIOREF_ACTIVATE;
1570 
1571 		return FOLIOREF_KEEP;
1572 	}
1573 
1574 	/* Reclaim if clean, defer dirty folios to writeback */
1575 	if (referenced_folio && folio_is_file_lru(folio))
1576 		return FOLIOREF_RECLAIM_CLEAN;
1577 
1578 	return FOLIOREF_RECLAIM;
1579 }
1580 
1581 /* Check if a folio is dirty or under writeback */
1582 static void folio_check_dirty_writeback(struct folio *folio,
1583 				       bool *dirty, bool *writeback)
1584 {
1585 	struct address_space *mapping;
1586 
1587 	/*
1588 	 * Anonymous folios are not handled by flushers and must be written
1589 	 * from reclaim context. Do not stall reclaim based on them.
1590 	 * MADV_FREE anonymous folios are put into inactive file list too.
1591 	 * They could be mistakenly treated as file lru. So further anon
1592 	 * test is needed.
1593 	 */
1594 	if (!folio_is_file_lru(folio) ||
1595 	    (folio_test_anon(folio) && !folio_test_swapbacked(folio))) {
1596 		*dirty = false;
1597 		*writeback = false;
1598 		return;
1599 	}
1600 
1601 	/* By default assume that the folio flags are accurate */
1602 	*dirty = folio_test_dirty(folio);
1603 	*writeback = folio_test_writeback(folio);
1604 
1605 	/* Verify dirty/writeback state if the filesystem supports it */
1606 	if (!folio_test_private(folio))
1607 		return;
1608 
1609 	mapping = folio_mapping(folio);
1610 	if (mapping && mapping->a_ops->is_dirty_writeback)
1611 		mapping->a_ops->is_dirty_writeback(folio, dirty, writeback);
1612 }
1613 
1614 static struct folio *alloc_demote_folio(struct folio *src,
1615 		unsigned long private)
1616 {
1617 	struct folio *dst;
1618 	nodemask_t *allowed_mask;
1619 	struct migration_target_control *mtc;
1620 
1621 	mtc = (struct migration_target_control *)private;
1622 
1623 	allowed_mask = mtc->nmask;
1624 	/*
1625 	 * make sure we allocate from the target node first also trying to
1626 	 * demote or reclaim pages from the target node via kswapd if we are
1627 	 * low on free memory on target node. If we don't do this and if
1628 	 * we have free memory on the slower(lower) memtier, we would start
1629 	 * allocating pages from slower(lower) memory tiers without even forcing
1630 	 * a demotion of cold pages from the target memtier. This can result
1631 	 * in the kernel placing hot pages in slower(lower) memory tiers.
1632 	 */
1633 	mtc->nmask = NULL;
1634 	mtc->gfp_mask |= __GFP_THISNODE;
1635 	dst = alloc_migration_target(src, (unsigned long)mtc);
1636 	if (dst)
1637 		return dst;
1638 
1639 	mtc->gfp_mask &= ~__GFP_THISNODE;
1640 	mtc->nmask = allowed_mask;
1641 
1642 	return alloc_migration_target(src, (unsigned long)mtc);
1643 }
1644 
1645 /*
1646  * Take folios on @demote_folios and attempt to demote them to another node.
1647  * Folios which are not demoted are left on @demote_folios.
1648  */
1649 static unsigned int demote_folio_list(struct list_head *demote_folios,
1650 				     struct pglist_data *pgdat)
1651 {
1652 	int target_nid = next_demotion_node(pgdat->node_id);
1653 	unsigned int nr_succeeded;
1654 	nodemask_t allowed_mask;
1655 
1656 	struct migration_target_control mtc = {
1657 		/*
1658 		 * Allocate from 'node', or fail quickly and quietly.
1659 		 * When this happens, 'page' will likely just be discarded
1660 		 * instead of migrated.
1661 		 */
1662 		.gfp_mask = (GFP_HIGHUSER_MOVABLE & ~__GFP_RECLAIM) | __GFP_NOWARN |
1663 			__GFP_NOMEMALLOC | GFP_NOWAIT,
1664 		.nid = target_nid,
1665 		.nmask = &allowed_mask
1666 	};
1667 
1668 	if (list_empty(demote_folios))
1669 		return 0;
1670 
1671 	if (target_nid == NUMA_NO_NODE)
1672 		return 0;
1673 
1674 	node_get_allowed_targets(pgdat, &allowed_mask);
1675 
1676 	/* Demotion ignores all cpuset and mempolicy settings */
1677 	migrate_pages(demote_folios, alloc_demote_folio, NULL,
1678 		      (unsigned long)&mtc, MIGRATE_ASYNC, MR_DEMOTION,
1679 		      &nr_succeeded);
1680 
1681 	__count_vm_events(PGDEMOTE_KSWAPD + reclaimer_offset(), nr_succeeded);
1682 
1683 	return nr_succeeded;
1684 }
1685 
1686 static bool may_enter_fs(struct folio *folio, gfp_t gfp_mask)
1687 {
1688 	if (gfp_mask & __GFP_FS)
1689 		return true;
1690 	if (!folio_test_swapcache(folio) || !(gfp_mask & __GFP_IO))
1691 		return false;
1692 	/*
1693 	 * We can "enter_fs" for swap-cache with only __GFP_IO
1694 	 * providing this isn't SWP_FS_OPS.
1695 	 * ->flags can be updated non-atomicially (scan_swap_map_slots),
1696 	 * but that will never affect SWP_FS_OPS, so the data_race
1697 	 * is safe.
1698 	 */
1699 	return !data_race(folio_swap_flags(folio) & SWP_FS_OPS);
1700 }
1701 
1702 /*
1703  * shrink_folio_list() returns the number of reclaimed pages
1704  */
1705 static unsigned int shrink_folio_list(struct list_head *folio_list,
1706 		struct pglist_data *pgdat, struct scan_control *sc,
1707 		struct reclaim_stat *stat, bool ignore_references)
1708 {
1709 	LIST_HEAD(ret_folios);
1710 	LIST_HEAD(free_folios);
1711 	LIST_HEAD(demote_folios);
1712 	unsigned int nr_reclaimed = 0;
1713 	unsigned int pgactivate = 0;
1714 	bool do_demote_pass;
1715 	struct swap_iocb *plug = NULL;
1716 
1717 	memset(stat, 0, sizeof(*stat));
1718 	cond_resched();
1719 	do_demote_pass = can_demote(pgdat->node_id, sc);
1720 
1721 retry:
1722 	while (!list_empty(folio_list)) {
1723 		struct address_space *mapping;
1724 		struct folio *folio;
1725 		enum folio_references references = FOLIOREF_RECLAIM;
1726 		bool dirty, writeback;
1727 		unsigned int nr_pages;
1728 
1729 		cond_resched();
1730 
1731 		folio = lru_to_folio(folio_list);
1732 		list_del(&folio->lru);
1733 
1734 		if (!folio_trylock(folio))
1735 			goto keep;
1736 
1737 		VM_BUG_ON_FOLIO(folio_test_active(folio), folio);
1738 
1739 		nr_pages = folio_nr_pages(folio);
1740 
1741 		/* Account the number of base pages */
1742 		sc->nr_scanned += nr_pages;
1743 
1744 		if (unlikely(!folio_evictable(folio)))
1745 			goto activate_locked;
1746 
1747 		if (!sc->may_unmap && folio_mapped(folio))
1748 			goto keep_locked;
1749 
1750 		/* folio_update_gen() tried to promote this page? */
1751 		if (lru_gen_enabled() && !ignore_references &&
1752 		    folio_mapped(folio) && folio_test_referenced(folio))
1753 			goto keep_locked;
1754 
1755 		/*
1756 		 * The number of dirty pages determines if a node is marked
1757 		 * reclaim_congested. kswapd will stall and start writing
1758 		 * folios if the tail of the LRU is all dirty unqueued folios.
1759 		 */
1760 		folio_check_dirty_writeback(folio, &dirty, &writeback);
1761 		if (dirty || writeback)
1762 			stat->nr_dirty += nr_pages;
1763 
1764 		if (dirty && !writeback)
1765 			stat->nr_unqueued_dirty += nr_pages;
1766 
1767 		/*
1768 		 * Treat this folio as congested if folios are cycling
1769 		 * through the LRU so quickly that the folios marked
1770 		 * for immediate reclaim are making it to the end of
1771 		 * the LRU a second time.
1772 		 */
1773 		if (writeback && folio_test_reclaim(folio))
1774 			stat->nr_congested += nr_pages;
1775 
1776 		/*
1777 		 * If a folio at the tail of the LRU is under writeback, there
1778 		 * are three cases to consider.
1779 		 *
1780 		 * 1) If reclaim is encountering an excessive number
1781 		 *    of folios under writeback and this folio has both
1782 		 *    the writeback and reclaim flags set, then it
1783 		 *    indicates that folios are being queued for I/O but
1784 		 *    are being recycled through the LRU before the I/O
1785 		 *    can complete. Waiting on the folio itself risks an
1786 		 *    indefinite stall if it is impossible to writeback
1787 		 *    the folio due to I/O error or disconnected storage
1788 		 *    so instead note that the LRU is being scanned too
1789 		 *    quickly and the caller can stall after the folio
1790 		 *    list has been processed.
1791 		 *
1792 		 * 2) Global or new memcg reclaim encounters a folio that is
1793 		 *    not marked for immediate reclaim, or the caller does not
1794 		 *    have __GFP_FS (or __GFP_IO if it's simply going to swap,
1795 		 *    not to fs). In this case mark the folio for immediate
1796 		 *    reclaim and continue scanning.
1797 		 *
1798 		 *    Require may_enter_fs() because we would wait on fs, which
1799 		 *    may not have submitted I/O yet. And the loop driver might
1800 		 *    enter reclaim, and deadlock if it waits on a folio for
1801 		 *    which it is needed to do the write (loop masks off
1802 		 *    __GFP_IO|__GFP_FS for this reason); but more thought
1803 		 *    would probably show more reasons.
1804 		 *
1805 		 * 3) Legacy memcg encounters a folio that already has the
1806 		 *    reclaim flag set. memcg does not have any dirty folio
1807 		 *    throttling so we could easily OOM just because too many
1808 		 *    folios are in writeback and there is nothing else to
1809 		 *    reclaim. Wait for the writeback to complete.
1810 		 *
1811 		 * In cases 1) and 2) we activate the folios to get them out of
1812 		 * the way while we continue scanning for clean folios on the
1813 		 * inactive list and refilling from the active list. The
1814 		 * observation here is that waiting for disk writes is more
1815 		 * expensive than potentially causing reloads down the line.
1816 		 * Since they're marked for immediate reclaim, they won't put
1817 		 * memory pressure on the cache working set any longer than it
1818 		 * takes to write them to disk.
1819 		 */
1820 		if (folio_test_writeback(folio)) {
1821 			/* Case 1 above */
1822 			if (current_is_kswapd() &&
1823 			    folio_test_reclaim(folio) &&
1824 			    test_bit(PGDAT_WRITEBACK, &pgdat->flags)) {
1825 				stat->nr_immediate += nr_pages;
1826 				goto activate_locked;
1827 
1828 			/* Case 2 above */
1829 			} else if (writeback_throttling_sane(sc) ||
1830 			    !folio_test_reclaim(folio) ||
1831 			    !may_enter_fs(folio, sc->gfp_mask)) {
1832 				/*
1833 				 * This is slightly racy -
1834 				 * folio_end_writeback() might have
1835 				 * just cleared the reclaim flag, then
1836 				 * setting the reclaim flag here ends up
1837 				 * interpreted as the readahead flag - but
1838 				 * that does not matter enough to care.
1839 				 * What we do want is for this folio to
1840 				 * have the reclaim flag set next time
1841 				 * memcg reclaim reaches the tests above,
1842 				 * so it will then wait for writeback to
1843 				 * avoid OOM; and it's also appropriate
1844 				 * in global reclaim.
1845 				 */
1846 				folio_set_reclaim(folio);
1847 				stat->nr_writeback += nr_pages;
1848 				goto activate_locked;
1849 
1850 			/* Case 3 above */
1851 			} else {
1852 				folio_unlock(folio);
1853 				folio_wait_writeback(folio);
1854 				/* then go back and try same folio again */
1855 				list_add_tail(&folio->lru, folio_list);
1856 				continue;
1857 			}
1858 		}
1859 
1860 		if (!ignore_references)
1861 			references = folio_check_references(folio, sc);
1862 
1863 		switch (references) {
1864 		case FOLIOREF_ACTIVATE:
1865 			goto activate_locked;
1866 		case FOLIOREF_KEEP:
1867 			stat->nr_ref_keep += nr_pages;
1868 			goto keep_locked;
1869 		case FOLIOREF_RECLAIM:
1870 		case FOLIOREF_RECLAIM_CLEAN:
1871 			; /* try to reclaim the folio below */
1872 		}
1873 
1874 		/*
1875 		 * Before reclaiming the folio, try to relocate
1876 		 * its contents to another node.
1877 		 */
1878 		if (do_demote_pass &&
1879 		    (thp_migration_supported() || !folio_test_large(folio))) {
1880 			list_add(&folio->lru, &demote_folios);
1881 			folio_unlock(folio);
1882 			continue;
1883 		}
1884 
1885 		/*
1886 		 * Anonymous process memory has backing store?
1887 		 * Try to allocate it some swap space here.
1888 		 * Lazyfree folio could be freed directly
1889 		 */
1890 		if (folio_test_anon(folio) && folio_test_swapbacked(folio)) {
1891 			if (!folio_test_swapcache(folio)) {
1892 				if (!(sc->gfp_mask & __GFP_IO))
1893 					goto keep_locked;
1894 				if (folio_maybe_dma_pinned(folio))
1895 					goto keep_locked;
1896 				if (folio_test_large(folio)) {
1897 					/* cannot split folio, skip it */
1898 					if (!can_split_folio(folio, NULL))
1899 						goto activate_locked;
1900 					/*
1901 					 * Split folios without a PMD map right
1902 					 * away. Chances are some or all of the
1903 					 * tail pages can be freed without IO.
1904 					 */
1905 					if (!folio_entire_mapcount(folio) &&
1906 					    split_folio_to_list(folio,
1907 								folio_list))
1908 						goto activate_locked;
1909 				}
1910 				if (!add_to_swap(folio)) {
1911 					if (!folio_test_large(folio))
1912 						goto activate_locked_split;
1913 					/* Fallback to swap normal pages */
1914 					if (split_folio_to_list(folio,
1915 								folio_list))
1916 						goto activate_locked;
1917 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1918 					count_vm_event(THP_SWPOUT_FALLBACK);
1919 #endif
1920 					if (!add_to_swap(folio))
1921 						goto activate_locked_split;
1922 				}
1923 			}
1924 		} else if (folio_test_swapbacked(folio) &&
1925 			   folio_test_large(folio)) {
1926 			/* Split shmem folio */
1927 			if (split_folio_to_list(folio, folio_list))
1928 				goto keep_locked;
1929 		}
1930 
1931 		/*
1932 		 * If the folio was split above, the tail pages will make
1933 		 * their own pass through this function and be accounted
1934 		 * then.
1935 		 */
1936 		if ((nr_pages > 1) && !folio_test_large(folio)) {
1937 			sc->nr_scanned -= (nr_pages - 1);
1938 			nr_pages = 1;
1939 		}
1940 
1941 		/*
1942 		 * The folio is mapped into the page tables of one or more
1943 		 * processes. Try to unmap it here.
1944 		 */
1945 		if (folio_mapped(folio)) {
1946 			enum ttu_flags flags = TTU_BATCH_FLUSH;
1947 			bool was_swapbacked = folio_test_swapbacked(folio);
1948 
1949 			if (folio_test_pmd_mappable(folio))
1950 				flags |= TTU_SPLIT_HUGE_PMD;
1951 
1952 			try_to_unmap(folio, flags);
1953 			if (folio_mapped(folio)) {
1954 				stat->nr_unmap_fail += nr_pages;
1955 				if (!was_swapbacked &&
1956 				    folio_test_swapbacked(folio))
1957 					stat->nr_lazyfree_fail += nr_pages;
1958 				goto activate_locked;
1959 			}
1960 		}
1961 
1962 		/*
1963 		 * Folio is unmapped now so it cannot be newly pinned anymore.
1964 		 * No point in trying to reclaim folio if it is pinned.
1965 		 * Furthermore we don't want to reclaim underlying fs metadata
1966 		 * if the folio is pinned and thus potentially modified by the
1967 		 * pinning process as that may upset the filesystem.
1968 		 */
1969 		if (folio_maybe_dma_pinned(folio))
1970 			goto activate_locked;
1971 
1972 		mapping = folio_mapping(folio);
1973 		if (folio_test_dirty(folio)) {
1974 			/*
1975 			 * Only kswapd can writeback filesystem folios
1976 			 * to avoid risk of stack overflow. But avoid
1977 			 * injecting inefficient single-folio I/O into
1978 			 * flusher writeback as much as possible: only
1979 			 * write folios when we've encountered many
1980 			 * dirty folios, and when we've already scanned
1981 			 * the rest of the LRU for clean folios and see
1982 			 * the same dirty folios again (with the reclaim
1983 			 * flag set).
1984 			 */
1985 			if (folio_is_file_lru(folio) &&
1986 			    (!current_is_kswapd() ||
1987 			     !folio_test_reclaim(folio) ||
1988 			     !test_bit(PGDAT_DIRTY, &pgdat->flags))) {
1989 				/*
1990 				 * Immediately reclaim when written back.
1991 				 * Similar in principle to folio_deactivate()
1992 				 * except we already have the folio isolated
1993 				 * and know it's dirty
1994 				 */
1995 				node_stat_mod_folio(folio, NR_VMSCAN_IMMEDIATE,
1996 						nr_pages);
1997 				folio_set_reclaim(folio);
1998 
1999 				goto activate_locked;
2000 			}
2001 
2002 			if (references == FOLIOREF_RECLAIM_CLEAN)
2003 				goto keep_locked;
2004 			if (!may_enter_fs(folio, sc->gfp_mask))
2005 				goto keep_locked;
2006 			if (!sc->may_writepage)
2007 				goto keep_locked;
2008 
2009 			/*
2010 			 * Folio is dirty. Flush the TLB if a writable entry
2011 			 * potentially exists to avoid CPU writes after I/O
2012 			 * starts and then write it out here.
2013 			 */
2014 			try_to_unmap_flush_dirty();
2015 			switch (pageout(folio, mapping, &plug)) {
2016 			case PAGE_KEEP:
2017 				goto keep_locked;
2018 			case PAGE_ACTIVATE:
2019 				goto activate_locked;
2020 			case PAGE_SUCCESS:
2021 				stat->nr_pageout += nr_pages;
2022 
2023 				if (folio_test_writeback(folio))
2024 					goto keep;
2025 				if (folio_test_dirty(folio))
2026 					goto keep;
2027 
2028 				/*
2029 				 * A synchronous write - probably a ramdisk.  Go
2030 				 * ahead and try to reclaim the folio.
2031 				 */
2032 				if (!folio_trylock(folio))
2033 					goto keep;
2034 				if (folio_test_dirty(folio) ||
2035 				    folio_test_writeback(folio))
2036 					goto keep_locked;
2037 				mapping = folio_mapping(folio);
2038 				fallthrough;
2039 			case PAGE_CLEAN:
2040 				; /* try to free the folio below */
2041 			}
2042 		}
2043 
2044 		/*
2045 		 * If the folio has buffers, try to free the buffer
2046 		 * mappings associated with this folio. If we succeed
2047 		 * we try to free the folio as well.
2048 		 *
2049 		 * We do this even if the folio is dirty.
2050 		 * filemap_release_folio() does not perform I/O, but it
2051 		 * is possible for a folio to have the dirty flag set,
2052 		 * but it is actually clean (all its buffers are clean).
2053 		 * This happens if the buffers were written out directly,
2054 		 * with submit_bh(). ext3 will do this, as well as
2055 		 * the blockdev mapping.  filemap_release_folio() will
2056 		 * discover that cleanness and will drop the buffers
2057 		 * and mark the folio clean - it can be freed.
2058 		 *
2059 		 * Rarely, folios can have buffers and no ->mapping.
2060 		 * These are the folios which were not successfully
2061 		 * invalidated in truncate_cleanup_folio().  We try to
2062 		 * drop those buffers here and if that worked, and the
2063 		 * folio is no longer mapped into process address space
2064 		 * (refcount == 1) it can be freed.  Otherwise, leave
2065 		 * the folio on the LRU so it is swappable.
2066 		 */
2067 		if (folio_needs_release(folio)) {
2068 			if (!filemap_release_folio(folio, sc->gfp_mask))
2069 				goto activate_locked;
2070 			if (!mapping && folio_ref_count(folio) == 1) {
2071 				folio_unlock(folio);
2072 				if (folio_put_testzero(folio))
2073 					goto free_it;
2074 				else {
2075 					/*
2076 					 * rare race with speculative reference.
2077 					 * the speculative reference will free
2078 					 * this folio shortly, so we may
2079 					 * increment nr_reclaimed here (and
2080 					 * leave it off the LRU).
2081 					 */
2082 					nr_reclaimed += nr_pages;
2083 					continue;
2084 				}
2085 			}
2086 		}
2087 
2088 		if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) {
2089 			/* follow __remove_mapping for reference */
2090 			if (!folio_ref_freeze(folio, 1))
2091 				goto keep_locked;
2092 			/*
2093 			 * The folio has only one reference left, which is
2094 			 * from the isolation. After the caller puts the
2095 			 * folio back on the lru and drops the reference, the
2096 			 * folio will be freed anyway. It doesn't matter
2097 			 * which lru it goes on. So we don't bother checking
2098 			 * the dirty flag here.
2099 			 */
2100 			count_vm_events(PGLAZYFREED, nr_pages);
2101 			count_memcg_folio_events(folio, PGLAZYFREED, nr_pages);
2102 		} else if (!mapping || !__remove_mapping(mapping, folio, true,
2103 							 sc->target_mem_cgroup))
2104 			goto keep_locked;
2105 
2106 		folio_unlock(folio);
2107 free_it:
2108 		/*
2109 		 * Folio may get swapped out as a whole, need to account
2110 		 * all pages in it.
2111 		 */
2112 		nr_reclaimed += nr_pages;
2113 
2114 		/*
2115 		 * Is there need to periodically free_folio_list? It would
2116 		 * appear not as the counts should be low
2117 		 */
2118 		if (unlikely(folio_test_large(folio)))
2119 			destroy_large_folio(folio);
2120 		else
2121 			list_add(&folio->lru, &free_folios);
2122 		continue;
2123 
2124 activate_locked_split:
2125 		/*
2126 		 * The tail pages that are failed to add into swap cache
2127 		 * reach here.  Fixup nr_scanned and nr_pages.
2128 		 */
2129 		if (nr_pages > 1) {
2130 			sc->nr_scanned -= (nr_pages - 1);
2131 			nr_pages = 1;
2132 		}
2133 activate_locked:
2134 		/* Not a candidate for swapping, so reclaim swap space. */
2135 		if (folio_test_swapcache(folio) &&
2136 		    (mem_cgroup_swap_full(folio) || folio_test_mlocked(folio)))
2137 			folio_free_swap(folio);
2138 		VM_BUG_ON_FOLIO(folio_test_active(folio), folio);
2139 		if (!folio_test_mlocked(folio)) {
2140 			int type = folio_is_file_lru(folio);
2141 			folio_set_active(folio);
2142 			stat->nr_activate[type] += nr_pages;
2143 			count_memcg_folio_events(folio, PGACTIVATE, nr_pages);
2144 		}
2145 keep_locked:
2146 		folio_unlock(folio);
2147 keep:
2148 		list_add(&folio->lru, &ret_folios);
2149 		VM_BUG_ON_FOLIO(folio_test_lru(folio) ||
2150 				folio_test_unevictable(folio), folio);
2151 	}
2152 	/* 'folio_list' is always empty here */
2153 
2154 	/* Migrate folios selected for demotion */
2155 	nr_reclaimed += demote_folio_list(&demote_folios, pgdat);
2156 	/* Folios that could not be demoted are still in @demote_folios */
2157 	if (!list_empty(&demote_folios)) {
2158 		/* Folios which weren't demoted go back on @folio_list */
2159 		list_splice_init(&demote_folios, folio_list);
2160 
2161 		/*
2162 		 * goto retry to reclaim the undemoted folios in folio_list if
2163 		 * desired.
2164 		 *
2165 		 * Reclaiming directly from top tier nodes is not often desired
2166 		 * due to it breaking the LRU ordering: in general memory
2167 		 * should be reclaimed from lower tier nodes and demoted from
2168 		 * top tier nodes.
2169 		 *
2170 		 * However, disabling reclaim from top tier nodes entirely
2171 		 * would cause ooms in edge scenarios where lower tier memory
2172 		 * is unreclaimable for whatever reason, eg memory being
2173 		 * mlocked or too hot to reclaim. We can disable reclaim
2174 		 * from top tier nodes in proactive reclaim though as that is
2175 		 * not real memory pressure.
2176 		 */
2177 		if (!sc->proactive) {
2178 			do_demote_pass = false;
2179 			goto retry;
2180 		}
2181 	}
2182 
2183 	pgactivate = stat->nr_activate[0] + stat->nr_activate[1];
2184 
2185 	mem_cgroup_uncharge_list(&free_folios);
2186 	try_to_unmap_flush();
2187 	free_unref_page_list(&free_folios);
2188 
2189 	list_splice(&ret_folios, folio_list);
2190 	count_vm_events(PGACTIVATE, pgactivate);
2191 
2192 	if (plug)
2193 		swap_write_unplug(plug);
2194 	return nr_reclaimed;
2195 }
2196 
2197 unsigned int reclaim_clean_pages_from_list(struct zone *zone,
2198 					   struct list_head *folio_list)
2199 {
2200 	struct scan_control sc = {
2201 		.gfp_mask = GFP_KERNEL,
2202 		.may_unmap = 1,
2203 	};
2204 	struct reclaim_stat stat;
2205 	unsigned int nr_reclaimed;
2206 	struct folio *folio, *next;
2207 	LIST_HEAD(clean_folios);
2208 	unsigned int noreclaim_flag;
2209 
2210 	list_for_each_entry_safe(folio, next, folio_list, lru) {
2211 		if (!folio_test_hugetlb(folio) && folio_is_file_lru(folio) &&
2212 		    !folio_test_dirty(folio) && !__folio_test_movable(folio) &&
2213 		    !folio_test_unevictable(folio)) {
2214 			folio_clear_active(folio);
2215 			list_move(&folio->lru, &clean_folios);
2216 		}
2217 	}
2218 
2219 	/*
2220 	 * We should be safe here since we are only dealing with file pages and
2221 	 * we are not kswapd and therefore cannot write dirty file pages. But
2222 	 * call memalloc_noreclaim_save() anyway, just in case these conditions
2223 	 * change in the future.
2224 	 */
2225 	noreclaim_flag = memalloc_noreclaim_save();
2226 	nr_reclaimed = shrink_folio_list(&clean_folios, zone->zone_pgdat, &sc,
2227 					&stat, true);
2228 	memalloc_noreclaim_restore(noreclaim_flag);
2229 
2230 	list_splice(&clean_folios, folio_list);
2231 	mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
2232 			    -(long)nr_reclaimed);
2233 	/*
2234 	 * Since lazyfree pages are isolated from file LRU from the beginning,
2235 	 * they will rotate back to anonymous LRU in the end if it failed to
2236 	 * discard so isolated count will be mismatched.
2237 	 * Compensate the isolated count for both LRU lists.
2238 	 */
2239 	mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON,
2240 			    stat.nr_lazyfree_fail);
2241 	mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
2242 			    -(long)stat.nr_lazyfree_fail);
2243 	return nr_reclaimed;
2244 }
2245 
2246 /*
2247  * Update LRU sizes after isolating pages. The LRU size updates must
2248  * be complete before mem_cgroup_update_lru_size due to a sanity check.
2249  */
2250 static __always_inline void update_lru_sizes(struct lruvec *lruvec,
2251 			enum lru_list lru, unsigned long *nr_zone_taken)
2252 {
2253 	int zid;
2254 
2255 	for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2256 		if (!nr_zone_taken[zid])
2257 			continue;
2258 
2259 		update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
2260 	}
2261 
2262 }
2263 
2264 #ifdef CONFIG_CMA
2265 /*
2266  * It is waste of effort to scan and reclaim CMA pages if it is not available
2267  * for current allocation context. Kswapd can not be enrolled as it can not
2268  * distinguish this scenario by using sc->gfp_mask = GFP_KERNEL
2269  */
2270 static bool skip_cma(struct folio *folio, struct scan_control *sc)
2271 {
2272 	return !current_is_kswapd() &&
2273 			gfp_migratetype(sc->gfp_mask) != MIGRATE_MOVABLE &&
2274 			get_pageblock_migratetype(&folio->page) == MIGRATE_CMA;
2275 }
2276 #else
2277 static bool skip_cma(struct folio *folio, struct scan_control *sc)
2278 {
2279 	return false;
2280 }
2281 #endif
2282 
2283 /*
2284  * Isolating page from the lruvec to fill in @dst list by nr_to_scan times.
2285  *
2286  * lruvec->lru_lock is heavily contended.  Some of the functions that
2287  * shrink the lists perform better by taking out a batch of pages
2288  * and working on them outside the LRU lock.
2289  *
2290  * For pagecache intensive workloads, this function is the hottest
2291  * spot in the kernel (apart from copy_*_user functions).
2292  *
2293  * Lru_lock must be held before calling this function.
2294  *
2295  * @nr_to_scan:	The number of eligible pages to look through on the list.
2296  * @lruvec:	The LRU vector to pull pages from.
2297  * @dst:	The temp list to put pages on to.
2298  * @nr_scanned:	The number of pages that were scanned.
2299  * @sc:		The scan_control struct for this reclaim session
2300  * @lru:	LRU list id for isolating
2301  *
2302  * returns how many pages were moved onto *@dst.
2303  */
2304 static unsigned long isolate_lru_folios(unsigned long nr_to_scan,
2305 		struct lruvec *lruvec, struct list_head *dst,
2306 		unsigned long *nr_scanned, struct scan_control *sc,
2307 		enum lru_list lru)
2308 {
2309 	struct list_head *src = &lruvec->lists[lru];
2310 	unsigned long nr_taken = 0;
2311 	unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 };
2312 	unsigned long nr_skipped[MAX_NR_ZONES] = { 0, };
2313 	unsigned long skipped = 0;
2314 	unsigned long scan, total_scan, nr_pages;
2315 	LIST_HEAD(folios_skipped);
2316 
2317 	total_scan = 0;
2318 	scan = 0;
2319 	while (scan < nr_to_scan && !list_empty(src)) {
2320 		struct list_head *move_to = src;
2321 		struct folio *folio;
2322 
2323 		folio = lru_to_folio(src);
2324 		prefetchw_prev_lru_folio(folio, src, flags);
2325 
2326 		nr_pages = folio_nr_pages(folio);
2327 		total_scan += nr_pages;
2328 
2329 		if (folio_zonenum(folio) > sc->reclaim_idx ||
2330 				skip_cma(folio, sc)) {
2331 			nr_skipped[folio_zonenum(folio)] += nr_pages;
2332 			move_to = &folios_skipped;
2333 			goto move;
2334 		}
2335 
2336 		/*
2337 		 * Do not count skipped folios because that makes the function
2338 		 * return with no isolated folios if the LRU mostly contains
2339 		 * ineligible folios.  This causes the VM to not reclaim any
2340 		 * folios, triggering a premature OOM.
2341 		 * Account all pages in a folio.
2342 		 */
2343 		scan += nr_pages;
2344 
2345 		if (!folio_test_lru(folio))
2346 			goto move;
2347 		if (!sc->may_unmap && folio_mapped(folio))
2348 			goto move;
2349 
2350 		/*
2351 		 * Be careful not to clear the lru flag until after we're
2352 		 * sure the folio is not being freed elsewhere -- the
2353 		 * folio release code relies on it.
2354 		 */
2355 		if (unlikely(!folio_try_get(folio)))
2356 			goto move;
2357 
2358 		if (!folio_test_clear_lru(folio)) {
2359 			/* Another thread is already isolating this folio */
2360 			folio_put(folio);
2361 			goto move;
2362 		}
2363 
2364 		nr_taken += nr_pages;
2365 		nr_zone_taken[folio_zonenum(folio)] += nr_pages;
2366 		move_to = dst;
2367 move:
2368 		list_move(&folio->lru, move_to);
2369 	}
2370 
2371 	/*
2372 	 * Splice any skipped folios to the start of the LRU list. Note that
2373 	 * this disrupts the LRU order when reclaiming for lower zones but
2374 	 * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX
2375 	 * scanning would soon rescan the same folios to skip and waste lots
2376 	 * of cpu cycles.
2377 	 */
2378 	if (!list_empty(&folios_skipped)) {
2379 		int zid;
2380 
2381 		list_splice(&folios_skipped, src);
2382 		for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2383 			if (!nr_skipped[zid])
2384 				continue;
2385 
2386 			__count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]);
2387 			skipped += nr_skipped[zid];
2388 		}
2389 	}
2390 	*nr_scanned = total_scan;
2391 	trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan,
2392 				    total_scan, skipped, nr_taken,
2393 				    sc->may_unmap ? 0 : ISOLATE_UNMAPPED, lru);
2394 	update_lru_sizes(lruvec, lru, nr_zone_taken);
2395 	return nr_taken;
2396 }
2397 
2398 /**
2399  * folio_isolate_lru() - Try to isolate a folio from its LRU list.
2400  * @folio: Folio to isolate from its LRU list.
2401  *
2402  * Isolate a @folio from an LRU list and adjust the vmstat statistic
2403  * corresponding to whatever LRU list the folio was on.
2404  *
2405  * The folio will have its LRU flag cleared.  If it was found on the
2406  * active list, it will have the Active flag set.  If it was found on the
2407  * unevictable list, it will have the Unevictable flag set.  These flags
2408  * may need to be cleared by the caller before letting the page go.
2409  *
2410  * Context:
2411  *
2412  * (1) Must be called with an elevated refcount on the folio. This is a
2413  *     fundamental difference from isolate_lru_folios() (which is called
2414  *     without a stable reference).
2415  * (2) The lru_lock must not be held.
2416  * (3) Interrupts must be enabled.
2417  *
2418  * Return: true if the folio was removed from an LRU list.
2419  * false if the folio was not on an LRU list.
2420  */
2421 bool folio_isolate_lru(struct folio *folio)
2422 {
2423 	bool ret = false;
2424 
2425 	VM_BUG_ON_FOLIO(!folio_ref_count(folio), folio);
2426 
2427 	if (folio_test_clear_lru(folio)) {
2428 		struct lruvec *lruvec;
2429 
2430 		folio_get(folio);
2431 		lruvec = folio_lruvec_lock_irq(folio);
2432 		lruvec_del_folio(lruvec, folio);
2433 		unlock_page_lruvec_irq(lruvec);
2434 		ret = true;
2435 	}
2436 
2437 	return ret;
2438 }
2439 
2440 /*
2441  * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
2442  * then get rescheduled. When there are massive number of tasks doing page
2443  * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
2444  * the LRU list will go small and be scanned faster than necessary, leading to
2445  * unnecessary swapping, thrashing and OOM.
2446  */
2447 static int too_many_isolated(struct pglist_data *pgdat, int file,
2448 		struct scan_control *sc)
2449 {
2450 	unsigned long inactive, isolated;
2451 	bool too_many;
2452 
2453 	if (current_is_kswapd())
2454 		return 0;
2455 
2456 	if (!writeback_throttling_sane(sc))
2457 		return 0;
2458 
2459 	if (file) {
2460 		inactive = node_page_state(pgdat, NR_INACTIVE_FILE);
2461 		isolated = node_page_state(pgdat, NR_ISOLATED_FILE);
2462 	} else {
2463 		inactive = node_page_state(pgdat, NR_INACTIVE_ANON);
2464 		isolated = node_page_state(pgdat, NR_ISOLATED_ANON);
2465 	}
2466 
2467 	/*
2468 	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
2469 	 * won't get blocked by normal direct-reclaimers, forming a circular
2470 	 * deadlock.
2471 	 */
2472 	if (gfp_has_io_fs(sc->gfp_mask))
2473 		inactive >>= 3;
2474 
2475 	too_many = isolated > inactive;
2476 
2477 	/* Wake up tasks throttled due to too_many_isolated. */
2478 	if (!too_many)
2479 		wake_throttle_isolated(pgdat);
2480 
2481 	return too_many;
2482 }
2483 
2484 /*
2485  * move_folios_to_lru() moves folios from private @list to appropriate LRU list.
2486  * On return, @list is reused as a list of folios to be freed by the caller.
2487  *
2488  * Returns the number of pages moved to the given lruvec.
2489  */
2490 static unsigned int move_folios_to_lru(struct lruvec *lruvec,
2491 		struct list_head *list)
2492 {
2493 	int nr_pages, nr_moved = 0;
2494 	LIST_HEAD(folios_to_free);
2495 
2496 	while (!list_empty(list)) {
2497 		struct folio *folio = lru_to_folio(list);
2498 
2499 		VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
2500 		list_del(&folio->lru);
2501 		if (unlikely(!folio_evictable(folio))) {
2502 			spin_unlock_irq(&lruvec->lru_lock);
2503 			folio_putback_lru(folio);
2504 			spin_lock_irq(&lruvec->lru_lock);
2505 			continue;
2506 		}
2507 
2508 		/*
2509 		 * The folio_set_lru needs to be kept here for list integrity.
2510 		 * Otherwise:
2511 		 *   #0 move_folios_to_lru             #1 release_pages
2512 		 *   if (!folio_put_testzero())
2513 		 *				      if (folio_put_testzero())
2514 		 *				        !lru //skip lru_lock
2515 		 *     folio_set_lru()
2516 		 *     list_add(&folio->lru,)
2517 		 *                                        list_add(&folio->lru,)
2518 		 */
2519 		folio_set_lru(folio);
2520 
2521 		if (unlikely(folio_put_testzero(folio))) {
2522 			__folio_clear_lru_flags(folio);
2523 
2524 			if (unlikely(folio_test_large(folio))) {
2525 				spin_unlock_irq(&lruvec->lru_lock);
2526 				destroy_large_folio(folio);
2527 				spin_lock_irq(&lruvec->lru_lock);
2528 			} else
2529 				list_add(&folio->lru, &folios_to_free);
2530 
2531 			continue;
2532 		}
2533 
2534 		/*
2535 		 * All pages were isolated from the same lruvec (and isolation
2536 		 * inhibits memcg migration).
2537 		 */
2538 		VM_BUG_ON_FOLIO(!folio_matches_lruvec(folio, lruvec), folio);
2539 		lruvec_add_folio(lruvec, folio);
2540 		nr_pages = folio_nr_pages(folio);
2541 		nr_moved += nr_pages;
2542 		if (folio_test_active(folio))
2543 			workingset_age_nonresident(lruvec, nr_pages);
2544 	}
2545 
2546 	/*
2547 	 * To save our caller's stack, now use input list for pages to free.
2548 	 */
2549 	list_splice(&folios_to_free, list);
2550 
2551 	return nr_moved;
2552 }
2553 
2554 /*
2555  * If a kernel thread (such as nfsd for loop-back mounts) services a backing
2556  * device by writing to the page cache it sets PF_LOCAL_THROTTLE. In this case
2557  * we should not throttle.  Otherwise it is safe to do so.
2558  */
2559 static int current_may_throttle(void)
2560 {
2561 	return !(current->flags & PF_LOCAL_THROTTLE);
2562 }
2563 
2564 /*
2565  * shrink_inactive_list() is a helper for shrink_node().  It returns the number
2566  * of reclaimed pages
2567  */
2568 static unsigned long shrink_inactive_list(unsigned long nr_to_scan,
2569 		struct lruvec *lruvec, struct scan_control *sc,
2570 		enum lru_list lru)
2571 {
2572 	LIST_HEAD(folio_list);
2573 	unsigned long nr_scanned;
2574 	unsigned int nr_reclaimed = 0;
2575 	unsigned long nr_taken;
2576 	struct reclaim_stat stat;
2577 	bool file = is_file_lru(lru);
2578 	enum vm_event_item item;
2579 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2580 	bool stalled = false;
2581 
2582 	while (unlikely(too_many_isolated(pgdat, file, sc))) {
2583 		if (stalled)
2584 			return 0;
2585 
2586 		/* wait a bit for the reclaimer. */
2587 		stalled = true;
2588 		reclaim_throttle(pgdat, VMSCAN_THROTTLE_ISOLATED);
2589 
2590 		/* We are about to die and free our memory. Return now. */
2591 		if (fatal_signal_pending(current))
2592 			return SWAP_CLUSTER_MAX;
2593 	}
2594 
2595 	lru_add_drain();
2596 
2597 	spin_lock_irq(&lruvec->lru_lock);
2598 
2599 	nr_taken = isolate_lru_folios(nr_to_scan, lruvec, &folio_list,
2600 				     &nr_scanned, sc, lru);
2601 
2602 	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
2603 	item = PGSCAN_KSWAPD + reclaimer_offset();
2604 	if (!cgroup_reclaim(sc))
2605 		__count_vm_events(item, nr_scanned);
2606 	__count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned);
2607 	__count_vm_events(PGSCAN_ANON + file, nr_scanned);
2608 
2609 	spin_unlock_irq(&lruvec->lru_lock);
2610 
2611 	if (nr_taken == 0)
2612 		return 0;
2613 
2614 	nr_reclaimed = shrink_folio_list(&folio_list, pgdat, sc, &stat, false);
2615 
2616 	spin_lock_irq(&lruvec->lru_lock);
2617 	move_folios_to_lru(lruvec, &folio_list);
2618 
2619 	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
2620 	item = PGSTEAL_KSWAPD + reclaimer_offset();
2621 	if (!cgroup_reclaim(sc))
2622 		__count_vm_events(item, nr_reclaimed);
2623 	__count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed);
2624 	__count_vm_events(PGSTEAL_ANON + file, nr_reclaimed);
2625 	spin_unlock_irq(&lruvec->lru_lock);
2626 
2627 	lru_note_cost(lruvec, file, stat.nr_pageout, nr_scanned - nr_reclaimed);
2628 	mem_cgroup_uncharge_list(&folio_list);
2629 	free_unref_page_list(&folio_list);
2630 
2631 	/*
2632 	 * If dirty folios are scanned that are not queued for IO, it
2633 	 * implies that flushers are not doing their job. This can
2634 	 * happen when memory pressure pushes dirty folios to the end of
2635 	 * the LRU before the dirty limits are breached and the dirty
2636 	 * data has expired. It can also happen when the proportion of
2637 	 * dirty folios grows not through writes but through memory
2638 	 * pressure reclaiming all the clean cache. And in some cases,
2639 	 * the flushers simply cannot keep up with the allocation
2640 	 * rate. Nudge the flusher threads in case they are asleep.
2641 	 */
2642 	if (stat.nr_unqueued_dirty == nr_taken) {
2643 		wakeup_flusher_threads(WB_REASON_VMSCAN);
2644 		/*
2645 		 * For cgroupv1 dirty throttling is achieved by waking up
2646 		 * the kernel flusher here and later waiting on folios
2647 		 * which are in writeback to finish (see shrink_folio_list()).
2648 		 *
2649 		 * Flusher may not be able to issue writeback quickly
2650 		 * enough for cgroupv1 writeback throttling to work
2651 		 * on a large system.
2652 		 */
2653 		if (!writeback_throttling_sane(sc))
2654 			reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK);
2655 	}
2656 
2657 	sc->nr.dirty += stat.nr_dirty;
2658 	sc->nr.congested += stat.nr_congested;
2659 	sc->nr.unqueued_dirty += stat.nr_unqueued_dirty;
2660 	sc->nr.writeback += stat.nr_writeback;
2661 	sc->nr.immediate += stat.nr_immediate;
2662 	sc->nr.taken += nr_taken;
2663 	if (file)
2664 		sc->nr.file_taken += nr_taken;
2665 
2666 	trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
2667 			nr_scanned, nr_reclaimed, &stat, sc->priority, file);
2668 	return nr_reclaimed;
2669 }
2670 
2671 /*
2672  * shrink_active_list() moves folios from the active LRU to the inactive LRU.
2673  *
2674  * We move them the other way if the folio is referenced by one or more
2675  * processes.
2676  *
2677  * If the folios are mostly unmapped, the processing is fast and it is
2678  * appropriate to hold lru_lock across the whole operation.  But if
2679  * the folios are mapped, the processing is slow (folio_referenced()), so
2680  * we should drop lru_lock around each folio.  It's impossible to balance
2681  * this, so instead we remove the folios from the LRU while processing them.
2682  * It is safe to rely on the active flag against the non-LRU folios in here
2683  * because nobody will play with that bit on a non-LRU folio.
2684  *
2685  * The downside is that we have to touch folio->_refcount against each folio.
2686  * But we had to alter folio->flags anyway.
2687  */
2688 static void shrink_active_list(unsigned long nr_to_scan,
2689 			       struct lruvec *lruvec,
2690 			       struct scan_control *sc,
2691 			       enum lru_list lru)
2692 {
2693 	unsigned long nr_taken;
2694 	unsigned long nr_scanned;
2695 	unsigned long vm_flags;
2696 	LIST_HEAD(l_hold);	/* The folios which were snipped off */
2697 	LIST_HEAD(l_active);
2698 	LIST_HEAD(l_inactive);
2699 	unsigned nr_deactivate, nr_activate;
2700 	unsigned nr_rotated = 0;
2701 	int file = is_file_lru(lru);
2702 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2703 
2704 	lru_add_drain();
2705 
2706 	spin_lock_irq(&lruvec->lru_lock);
2707 
2708 	nr_taken = isolate_lru_folios(nr_to_scan, lruvec, &l_hold,
2709 				     &nr_scanned, sc, lru);
2710 
2711 	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
2712 
2713 	if (!cgroup_reclaim(sc))
2714 		__count_vm_events(PGREFILL, nr_scanned);
2715 	__count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned);
2716 
2717 	spin_unlock_irq(&lruvec->lru_lock);
2718 
2719 	while (!list_empty(&l_hold)) {
2720 		struct folio *folio;
2721 
2722 		cond_resched();
2723 		folio = lru_to_folio(&l_hold);
2724 		list_del(&folio->lru);
2725 
2726 		if (unlikely(!folio_evictable(folio))) {
2727 			folio_putback_lru(folio);
2728 			continue;
2729 		}
2730 
2731 		if (unlikely(buffer_heads_over_limit)) {
2732 			if (folio_needs_release(folio) &&
2733 			    folio_trylock(folio)) {
2734 				filemap_release_folio(folio, 0);
2735 				folio_unlock(folio);
2736 			}
2737 		}
2738 
2739 		/* Referenced or rmap lock contention: rotate */
2740 		if (folio_referenced(folio, 0, sc->target_mem_cgroup,
2741 				     &vm_flags) != 0) {
2742 			/*
2743 			 * Identify referenced, file-backed active folios and
2744 			 * give them one more trip around the active list. So
2745 			 * that executable code get better chances to stay in
2746 			 * memory under moderate memory pressure.  Anon folios
2747 			 * are not likely to be evicted by use-once streaming
2748 			 * IO, plus JVM can create lots of anon VM_EXEC folios,
2749 			 * so we ignore them here.
2750 			 */
2751 			if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio)) {
2752 				nr_rotated += folio_nr_pages(folio);
2753 				list_add(&folio->lru, &l_active);
2754 				continue;
2755 			}
2756 		}
2757 
2758 		folio_clear_active(folio);	/* we are de-activating */
2759 		folio_set_workingset(folio);
2760 		list_add(&folio->lru, &l_inactive);
2761 	}
2762 
2763 	/*
2764 	 * Move folios back to the lru list.
2765 	 */
2766 	spin_lock_irq(&lruvec->lru_lock);
2767 
2768 	nr_activate = move_folios_to_lru(lruvec, &l_active);
2769 	nr_deactivate = move_folios_to_lru(lruvec, &l_inactive);
2770 	/* Keep all free folios in l_active list */
2771 	list_splice(&l_inactive, &l_active);
2772 
2773 	__count_vm_events(PGDEACTIVATE, nr_deactivate);
2774 	__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate);
2775 
2776 	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
2777 	spin_unlock_irq(&lruvec->lru_lock);
2778 
2779 	if (nr_rotated)
2780 		lru_note_cost(lruvec, file, 0, nr_rotated);
2781 	mem_cgroup_uncharge_list(&l_active);
2782 	free_unref_page_list(&l_active);
2783 	trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate,
2784 			nr_deactivate, nr_rotated, sc->priority, file);
2785 }
2786 
2787 static unsigned int reclaim_folio_list(struct list_head *folio_list,
2788 				      struct pglist_data *pgdat)
2789 {
2790 	struct reclaim_stat dummy_stat;
2791 	unsigned int nr_reclaimed;
2792 	struct folio *folio;
2793 	struct scan_control sc = {
2794 		.gfp_mask = GFP_KERNEL,
2795 		.may_writepage = 1,
2796 		.may_unmap = 1,
2797 		.may_swap = 1,
2798 		.no_demotion = 1,
2799 	};
2800 
2801 	nr_reclaimed = shrink_folio_list(folio_list, pgdat, &sc, &dummy_stat, false);
2802 	while (!list_empty(folio_list)) {
2803 		folio = lru_to_folio(folio_list);
2804 		list_del(&folio->lru);
2805 		folio_putback_lru(folio);
2806 	}
2807 
2808 	return nr_reclaimed;
2809 }
2810 
2811 unsigned long reclaim_pages(struct list_head *folio_list)
2812 {
2813 	int nid;
2814 	unsigned int nr_reclaimed = 0;
2815 	LIST_HEAD(node_folio_list);
2816 	unsigned int noreclaim_flag;
2817 
2818 	if (list_empty(folio_list))
2819 		return nr_reclaimed;
2820 
2821 	noreclaim_flag = memalloc_noreclaim_save();
2822 
2823 	nid = folio_nid(lru_to_folio(folio_list));
2824 	do {
2825 		struct folio *folio = lru_to_folio(folio_list);
2826 
2827 		if (nid == folio_nid(folio)) {
2828 			folio_clear_active(folio);
2829 			list_move(&folio->lru, &node_folio_list);
2830 			continue;
2831 		}
2832 
2833 		nr_reclaimed += reclaim_folio_list(&node_folio_list, NODE_DATA(nid));
2834 		nid = folio_nid(lru_to_folio(folio_list));
2835 	} while (!list_empty(folio_list));
2836 
2837 	nr_reclaimed += reclaim_folio_list(&node_folio_list, NODE_DATA(nid));
2838 
2839 	memalloc_noreclaim_restore(noreclaim_flag);
2840 
2841 	return nr_reclaimed;
2842 }
2843 
2844 static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
2845 				 struct lruvec *lruvec, struct scan_control *sc)
2846 {
2847 	if (is_active_lru(lru)) {
2848 		if (sc->may_deactivate & (1 << is_file_lru(lru)))
2849 			shrink_active_list(nr_to_scan, lruvec, sc, lru);
2850 		else
2851 			sc->skipped_deactivate = 1;
2852 		return 0;
2853 	}
2854 
2855 	return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
2856 }
2857 
2858 /*
2859  * The inactive anon list should be small enough that the VM never has
2860  * to do too much work.
2861  *
2862  * The inactive file list should be small enough to leave most memory
2863  * to the established workingset on the scan-resistant active list,
2864  * but large enough to avoid thrashing the aggregate readahead window.
2865  *
2866  * Both inactive lists should also be large enough that each inactive
2867  * folio has a chance to be referenced again before it is reclaimed.
2868  *
2869  * If that fails and refaulting is observed, the inactive list grows.
2870  *
2871  * The inactive_ratio is the target ratio of ACTIVE to INACTIVE folios
2872  * on this LRU, maintained by the pageout code. An inactive_ratio
2873  * of 3 means 3:1 or 25% of the folios are kept on the inactive list.
2874  *
2875  * total     target    max
2876  * memory    ratio     inactive
2877  * -------------------------------------
2878  *   10MB       1         5MB
2879  *  100MB       1        50MB
2880  *    1GB       3       250MB
2881  *   10GB      10       0.9GB
2882  *  100GB      31         3GB
2883  *    1TB     101        10GB
2884  *   10TB     320        32GB
2885  */
2886 static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru)
2887 {
2888 	enum lru_list active_lru = inactive_lru + LRU_ACTIVE;
2889 	unsigned long inactive, active;
2890 	unsigned long inactive_ratio;
2891 	unsigned long gb;
2892 
2893 	inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru);
2894 	active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru);
2895 
2896 	gb = (inactive + active) >> (30 - PAGE_SHIFT);
2897 	if (gb)
2898 		inactive_ratio = int_sqrt(10 * gb);
2899 	else
2900 		inactive_ratio = 1;
2901 
2902 	return inactive * inactive_ratio < active;
2903 }
2904 
2905 enum scan_balance {
2906 	SCAN_EQUAL,
2907 	SCAN_FRACT,
2908 	SCAN_ANON,
2909 	SCAN_FILE,
2910 };
2911 
2912 static void prepare_scan_count(pg_data_t *pgdat, struct scan_control *sc)
2913 {
2914 	unsigned long file;
2915 	struct lruvec *target_lruvec;
2916 
2917 	if (lru_gen_enabled())
2918 		return;
2919 
2920 	target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat);
2921 
2922 	/*
2923 	 * Flush the memory cgroup stats, so that we read accurate per-memcg
2924 	 * lruvec stats for heuristics.
2925 	 */
2926 	mem_cgroup_flush_stats();
2927 
2928 	/*
2929 	 * Determine the scan balance between anon and file LRUs.
2930 	 */
2931 	spin_lock_irq(&target_lruvec->lru_lock);
2932 	sc->anon_cost = target_lruvec->anon_cost;
2933 	sc->file_cost = target_lruvec->file_cost;
2934 	spin_unlock_irq(&target_lruvec->lru_lock);
2935 
2936 	/*
2937 	 * Target desirable inactive:active list ratios for the anon
2938 	 * and file LRU lists.
2939 	 */
2940 	if (!sc->force_deactivate) {
2941 		unsigned long refaults;
2942 
2943 		/*
2944 		 * When refaults are being observed, it means a new
2945 		 * workingset is being established. Deactivate to get
2946 		 * rid of any stale active pages quickly.
2947 		 */
2948 		refaults = lruvec_page_state(target_lruvec,
2949 				WORKINGSET_ACTIVATE_ANON);
2950 		if (refaults != target_lruvec->refaults[WORKINGSET_ANON] ||
2951 			inactive_is_low(target_lruvec, LRU_INACTIVE_ANON))
2952 			sc->may_deactivate |= DEACTIVATE_ANON;
2953 		else
2954 			sc->may_deactivate &= ~DEACTIVATE_ANON;
2955 
2956 		refaults = lruvec_page_state(target_lruvec,
2957 				WORKINGSET_ACTIVATE_FILE);
2958 		if (refaults != target_lruvec->refaults[WORKINGSET_FILE] ||
2959 		    inactive_is_low(target_lruvec, LRU_INACTIVE_FILE))
2960 			sc->may_deactivate |= DEACTIVATE_FILE;
2961 		else
2962 			sc->may_deactivate &= ~DEACTIVATE_FILE;
2963 	} else
2964 		sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE;
2965 
2966 	/*
2967 	 * If we have plenty of inactive file pages that aren't
2968 	 * thrashing, try to reclaim those first before touching
2969 	 * anonymous pages.
2970 	 */
2971 	file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE);
2972 	if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE))
2973 		sc->cache_trim_mode = 1;
2974 	else
2975 		sc->cache_trim_mode = 0;
2976 
2977 	/*
2978 	 * Prevent the reclaimer from falling into the cache trap: as
2979 	 * cache pages start out inactive, every cache fault will tip
2980 	 * the scan balance towards the file LRU.  And as the file LRU
2981 	 * shrinks, so does the window for rotation from references.
2982 	 * This means we have a runaway feedback loop where a tiny
2983 	 * thrashing file LRU becomes infinitely more attractive than
2984 	 * anon pages.  Try to detect this based on file LRU size.
2985 	 */
2986 	if (!cgroup_reclaim(sc)) {
2987 		unsigned long total_high_wmark = 0;
2988 		unsigned long free, anon;
2989 		int z;
2990 
2991 		free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES);
2992 		file = node_page_state(pgdat, NR_ACTIVE_FILE) +
2993 			   node_page_state(pgdat, NR_INACTIVE_FILE);
2994 
2995 		for (z = 0; z < MAX_NR_ZONES; z++) {
2996 			struct zone *zone = &pgdat->node_zones[z];
2997 
2998 			if (!managed_zone(zone))
2999 				continue;
3000 
3001 			total_high_wmark += high_wmark_pages(zone);
3002 		}
3003 
3004 		/*
3005 		 * Consider anon: if that's low too, this isn't a
3006 		 * runaway file reclaim problem, but rather just
3007 		 * extreme pressure. Reclaim as per usual then.
3008 		 */
3009 		anon = node_page_state(pgdat, NR_INACTIVE_ANON);
3010 
3011 		sc->file_is_tiny =
3012 			file + free <= total_high_wmark &&
3013 			!(sc->may_deactivate & DEACTIVATE_ANON) &&
3014 			anon >> sc->priority;
3015 	}
3016 }
3017 
3018 /*
3019  * Determine how aggressively the anon and file LRU lists should be
3020  * scanned.
3021  *
3022  * nr[0] = anon inactive folios to scan; nr[1] = anon active folios to scan
3023  * nr[2] = file inactive folios to scan; nr[3] = file active folios to scan
3024  */
3025 static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
3026 			   unsigned long *nr)
3027 {
3028 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
3029 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
3030 	unsigned long anon_cost, file_cost, total_cost;
3031 	int swappiness = mem_cgroup_swappiness(memcg);
3032 	u64 fraction[ANON_AND_FILE];
3033 	u64 denominator = 0;	/* gcc */
3034 	enum scan_balance scan_balance;
3035 	unsigned long ap, fp;
3036 	enum lru_list lru;
3037 
3038 	/* If we have no swap space, do not bother scanning anon folios. */
3039 	if (!sc->may_swap || !can_reclaim_anon_pages(memcg, pgdat->node_id, sc)) {
3040 		scan_balance = SCAN_FILE;
3041 		goto out;
3042 	}
3043 
3044 	/*
3045 	 * Global reclaim will swap to prevent OOM even with no
3046 	 * swappiness, but memcg users want to use this knob to
3047 	 * disable swapping for individual groups completely when
3048 	 * using the memory controller's swap limit feature would be
3049 	 * too expensive.
3050 	 */
3051 	if (cgroup_reclaim(sc) && !swappiness) {
3052 		scan_balance = SCAN_FILE;
3053 		goto out;
3054 	}
3055 
3056 	/*
3057 	 * Do not apply any pressure balancing cleverness when the
3058 	 * system is close to OOM, scan both anon and file equally
3059 	 * (unless the swappiness setting disagrees with swapping).
3060 	 */
3061 	if (!sc->priority && swappiness) {
3062 		scan_balance = SCAN_EQUAL;
3063 		goto out;
3064 	}
3065 
3066 	/*
3067 	 * If the system is almost out of file pages, force-scan anon.
3068 	 */
3069 	if (sc->file_is_tiny) {
3070 		scan_balance = SCAN_ANON;
3071 		goto out;
3072 	}
3073 
3074 	/*
3075 	 * If there is enough inactive page cache, we do not reclaim
3076 	 * anything from the anonymous working right now.
3077 	 */
3078 	if (sc->cache_trim_mode) {
3079 		scan_balance = SCAN_FILE;
3080 		goto out;
3081 	}
3082 
3083 	scan_balance = SCAN_FRACT;
3084 	/*
3085 	 * Calculate the pressure balance between anon and file pages.
3086 	 *
3087 	 * The amount of pressure we put on each LRU is inversely
3088 	 * proportional to the cost of reclaiming each list, as
3089 	 * determined by the share of pages that are refaulting, times
3090 	 * the relative IO cost of bringing back a swapped out
3091 	 * anonymous page vs reloading a filesystem page (swappiness).
3092 	 *
3093 	 * Although we limit that influence to ensure no list gets
3094 	 * left behind completely: at least a third of the pressure is
3095 	 * applied, before swappiness.
3096 	 *
3097 	 * With swappiness at 100, anon and file have equal IO cost.
3098 	 */
3099 	total_cost = sc->anon_cost + sc->file_cost;
3100 	anon_cost = total_cost + sc->anon_cost;
3101 	file_cost = total_cost + sc->file_cost;
3102 	total_cost = anon_cost + file_cost;
3103 
3104 	ap = swappiness * (total_cost + 1);
3105 	ap /= anon_cost + 1;
3106 
3107 	fp = (200 - swappiness) * (total_cost + 1);
3108 	fp /= file_cost + 1;
3109 
3110 	fraction[0] = ap;
3111 	fraction[1] = fp;
3112 	denominator = ap + fp;
3113 out:
3114 	for_each_evictable_lru(lru) {
3115 		int file = is_file_lru(lru);
3116 		unsigned long lruvec_size;
3117 		unsigned long low, min;
3118 		unsigned long scan;
3119 
3120 		lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
3121 		mem_cgroup_protection(sc->target_mem_cgroup, memcg,
3122 				      &min, &low);
3123 
3124 		if (min || low) {
3125 			/*
3126 			 * Scale a cgroup's reclaim pressure by proportioning
3127 			 * its current usage to its memory.low or memory.min
3128 			 * setting.
3129 			 *
3130 			 * This is important, as otherwise scanning aggression
3131 			 * becomes extremely binary -- from nothing as we
3132 			 * approach the memory protection threshold, to totally
3133 			 * nominal as we exceed it.  This results in requiring
3134 			 * setting extremely liberal protection thresholds. It
3135 			 * also means we simply get no protection at all if we
3136 			 * set it too low, which is not ideal.
3137 			 *
3138 			 * If there is any protection in place, we reduce scan
3139 			 * pressure by how much of the total memory used is
3140 			 * within protection thresholds.
3141 			 *
3142 			 * There is one special case: in the first reclaim pass,
3143 			 * we skip over all groups that are within their low
3144 			 * protection. If that fails to reclaim enough pages to
3145 			 * satisfy the reclaim goal, we come back and override
3146 			 * the best-effort low protection. However, we still
3147 			 * ideally want to honor how well-behaved groups are in
3148 			 * that case instead of simply punishing them all
3149 			 * equally. As such, we reclaim them based on how much
3150 			 * memory they are using, reducing the scan pressure
3151 			 * again by how much of the total memory used is under
3152 			 * hard protection.
3153 			 */
3154 			unsigned long cgroup_size = mem_cgroup_size(memcg);
3155 			unsigned long protection;
3156 
3157 			/* memory.low scaling, make sure we retry before OOM */
3158 			if (!sc->memcg_low_reclaim && low > min) {
3159 				protection = low;
3160 				sc->memcg_low_skipped = 1;
3161 			} else {
3162 				protection = min;
3163 			}
3164 
3165 			/* Avoid TOCTOU with earlier protection check */
3166 			cgroup_size = max(cgroup_size, protection);
3167 
3168 			scan = lruvec_size - lruvec_size * protection /
3169 				(cgroup_size + 1);
3170 
3171 			/*
3172 			 * Minimally target SWAP_CLUSTER_MAX pages to keep
3173 			 * reclaim moving forwards, avoiding decrementing
3174 			 * sc->priority further than desirable.
3175 			 */
3176 			scan = max(scan, SWAP_CLUSTER_MAX);
3177 		} else {
3178 			scan = lruvec_size;
3179 		}
3180 
3181 		scan >>= sc->priority;
3182 
3183 		/*
3184 		 * If the cgroup's already been deleted, make sure to
3185 		 * scrape out the remaining cache.
3186 		 */
3187 		if (!scan && !mem_cgroup_online(memcg))
3188 			scan = min(lruvec_size, SWAP_CLUSTER_MAX);
3189 
3190 		switch (scan_balance) {
3191 		case SCAN_EQUAL:
3192 			/* Scan lists relative to size */
3193 			break;
3194 		case SCAN_FRACT:
3195 			/*
3196 			 * Scan types proportional to swappiness and
3197 			 * their relative recent reclaim efficiency.
3198 			 * Make sure we don't miss the last page on
3199 			 * the offlined memory cgroups because of a
3200 			 * round-off error.
3201 			 */
3202 			scan = mem_cgroup_online(memcg) ?
3203 			       div64_u64(scan * fraction[file], denominator) :
3204 			       DIV64_U64_ROUND_UP(scan * fraction[file],
3205 						  denominator);
3206 			break;
3207 		case SCAN_FILE:
3208 		case SCAN_ANON:
3209 			/* Scan one type exclusively */
3210 			if ((scan_balance == SCAN_FILE) != file)
3211 				scan = 0;
3212 			break;
3213 		default:
3214 			/* Look ma, no brain */
3215 			BUG();
3216 		}
3217 
3218 		nr[lru] = scan;
3219 	}
3220 }
3221 
3222 /*
3223  * Anonymous LRU management is a waste if there is
3224  * ultimately no way to reclaim the memory.
3225  */
3226 static bool can_age_anon_pages(struct pglist_data *pgdat,
3227 			       struct scan_control *sc)
3228 {
3229 	/* Aging the anon LRU is valuable if swap is present: */
3230 	if (total_swap_pages > 0)
3231 		return true;
3232 
3233 	/* Also valuable if anon pages can be demoted: */
3234 	return can_demote(pgdat->node_id, sc);
3235 }
3236 
3237 #ifdef CONFIG_LRU_GEN
3238 
3239 #ifdef CONFIG_LRU_GEN_ENABLED
3240 DEFINE_STATIC_KEY_ARRAY_TRUE(lru_gen_caps, NR_LRU_GEN_CAPS);
3241 #define get_cap(cap)	static_branch_likely(&lru_gen_caps[cap])
3242 #else
3243 DEFINE_STATIC_KEY_ARRAY_FALSE(lru_gen_caps, NR_LRU_GEN_CAPS);
3244 #define get_cap(cap)	static_branch_unlikely(&lru_gen_caps[cap])
3245 #endif
3246 
3247 static bool should_walk_mmu(void)
3248 {
3249 	return arch_has_hw_pte_young() && get_cap(LRU_GEN_MM_WALK);
3250 }
3251 
3252 static bool should_clear_pmd_young(void)
3253 {
3254 	return arch_has_hw_nonleaf_pmd_young() && get_cap(LRU_GEN_NONLEAF_YOUNG);
3255 }
3256 
3257 /******************************************************************************
3258  *                          shorthand helpers
3259  ******************************************************************************/
3260 
3261 #define LRU_REFS_FLAGS	(BIT(PG_referenced) | BIT(PG_workingset))
3262 
3263 #define DEFINE_MAX_SEQ(lruvec)						\
3264 	unsigned long max_seq = READ_ONCE((lruvec)->lrugen.max_seq)
3265 
3266 #define DEFINE_MIN_SEQ(lruvec)						\
3267 	unsigned long min_seq[ANON_AND_FILE] = {			\
3268 		READ_ONCE((lruvec)->lrugen.min_seq[LRU_GEN_ANON]),	\
3269 		READ_ONCE((lruvec)->lrugen.min_seq[LRU_GEN_FILE]),	\
3270 	}
3271 
3272 #define for_each_gen_type_zone(gen, type, zone)				\
3273 	for ((gen) = 0; (gen) < MAX_NR_GENS; (gen)++)			\
3274 		for ((type) = 0; (type) < ANON_AND_FILE; (type)++)	\
3275 			for ((zone) = 0; (zone) < MAX_NR_ZONES; (zone)++)
3276 
3277 #define get_memcg_gen(seq)	((seq) % MEMCG_NR_GENS)
3278 #define get_memcg_bin(bin)	((bin) % MEMCG_NR_BINS)
3279 
3280 static struct lruvec *get_lruvec(struct mem_cgroup *memcg, int nid)
3281 {
3282 	struct pglist_data *pgdat = NODE_DATA(nid);
3283 
3284 #ifdef CONFIG_MEMCG
3285 	if (memcg) {
3286 		struct lruvec *lruvec = &memcg->nodeinfo[nid]->lruvec;
3287 
3288 		/* see the comment in mem_cgroup_lruvec() */
3289 		if (!lruvec->pgdat)
3290 			lruvec->pgdat = pgdat;
3291 
3292 		return lruvec;
3293 	}
3294 #endif
3295 	VM_WARN_ON_ONCE(!mem_cgroup_disabled());
3296 
3297 	return &pgdat->__lruvec;
3298 }
3299 
3300 static int get_swappiness(struct lruvec *lruvec, struct scan_control *sc)
3301 {
3302 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
3303 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
3304 
3305 	if (!sc->may_swap)
3306 		return 0;
3307 
3308 	if (!can_demote(pgdat->node_id, sc) &&
3309 	    mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH)
3310 		return 0;
3311 
3312 	return mem_cgroup_swappiness(memcg);
3313 }
3314 
3315 static int get_nr_gens(struct lruvec *lruvec, int type)
3316 {
3317 	return lruvec->lrugen.max_seq - lruvec->lrugen.min_seq[type] + 1;
3318 }
3319 
3320 static bool __maybe_unused seq_is_valid(struct lruvec *lruvec)
3321 {
3322 	/* see the comment on lru_gen_folio */
3323 	return get_nr_gens(lruvec, LRU_GEN_FILE) >= MIN_NR_GENS &&
3324 	       get_nr_gens(lruvec, LRU_GEN_FILE) <= get_nr_gens(lruvec, LRU_GEN_ANON) &&
3325 	       get_nr_gens(lruvec, LRU_GEN_ANON) <= MAX_NR_GENS;
3326 }
3327 
3328 /******************************************************************************
3329  *                          Bloom filters
3330  ******************************************************************************/
3331 
3332 /*
3333  * Bloom filters with m=1<<15, k=2 and the false positive rates of ~1/5 when
3334  * n=10,000 and ~1/2 when n=20,000, where, conventionally, m is the number of
3335  * bits in a bitmap, k is the number of hash functions and n is the number of
3336  * inserted items.
3337  *
3338  * Page table walkers use one of the two filters to reduce their search space.
3339  * To get rid of non-leaf entries that no longer have enough leaf entries, the
3340  * aging uses the double-buffering technique to flip to the other filter each
3341  * time it produces a new generation. For non-leaf entries that have enough
3342  * leaf entries, the aging carries them over to the next generation in
3343  * walk_pmd_range(); the eviction also report them when walking the rmap
3344  * in lru_gen_look_around().
3345  *
3346  * For future optimizations:
3347  * 1. It's not necessary to keep both filters all the time. The spare one can be
3348  *    freed after the RCU grace period and reallocated if needed again.
3349  * 2. And when reallocating, it's worth scaling its size according to the number
3350  *    of inserted entries in the other filter, to reduce the memory overhead on
3351  *    small systems and false positives on large systems.
3352  * 3. Jenkins' hash function is an alternative to Knuth's.
3353  */
3354 #define BLOOM_FILTER_SHIFT	15
3355 
3356 static inline int filter_gen_from_seq(unsigned long seq)
3357 {
3358 	return seq % NR_BLOOM_FILTERS;
3359 }
3360 
3361 static void get_item_key(void *item, int *key)
3362 {
3363 	u32 hash = hash_ptr(item, BLOOM_FILTER_SHIFT * 2);
3364 
3365 	BUILD_BUG_ON(BLOOM_FILTER_SHIFT * 2 > BITS_PER_TYPE(u32));
3366 
3367 	key[0] = hash & (BIT(BLOOM_FILTER_SHIFT) - 1);
3368 	key[1] = hash >> BLOOM_FILTER_SHIFT;
3369 }
3370 
3371 static bool test_bloom_filter(struct lruvec *lruvec, unsigned long seq, void *item)
3372 {
3373 	int key[2];
3374 	unsigned long *filter;
3375 	int gen = filter_gen_from_seq(seq);
3376 
3377 	filter = READ_ONCE(lruvec->mm_state.filters[gen]);
3378 	if (!filter)
3379 		return true;
3380 
3381 	get_item_key(item, key);
3382 
3383 	return test_bit(key[0], filter) && test_bit(key[1], filter);
3384 }
3385 
3386 static void update_bloom_filter(struct lruvec *lruvec, unsigned long seq, void *item)
3387 {
3388 	int key[2];
3389 	unsigned long *filter;
3390 	int gen = filter_gen_from_seq(seq);
3391 
3392 	filter = READ_ONCE(lruvec->mm_state.filters[gen]);
3393 	if (!filter)
3394 		return;
3395 
3396 	get_item_key(item, key);
3397 
3398 	if (!test_bit(key[0], filter))
3399 		set_bit(key[0], filter);
3400 	if (!test_bit(key[1], filter))
3401 		set_bit(key[1], filter);
3402 }
3403 
3404 static void reset_bloom_filter(struct lruvec *lruvec, unsigned long seq)
3405 {
3406 	unsigned long *filter;
3407 	int gen = filter_gen_from_seq(seq);
3408 
3409 	filter = lruvec->mm_state.filters[gen];
3410 	if (filter) {
3411 		bitmap_clear(filter, 0, BIT(BLOOM_FILTER_SHIFT));
3412 		return;
3413 	}
3414 
3415 	filter = bitmap_zalloc(BIT(BLOOM_FILTER_SHIFT),
3416 			       __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN);
3417 	WRITE_ONCE(lruvec->mm_state.filters[gen], filter);
3418 }
3419 
3420 /******************************************************************************
3421  *                          mm_struct list
3422  ******************************************************************************/
3423 
3424 static struct lru_gen_mm_list *get_mm_list(struct mem_cgroup *memcg)
3425 {
3426 	static struct lru_gen_mm_list mm_list = {
3427 		.fifo = LIST_HEAD_INIT(mm_list.fifo),
3428 		.lock = __SPIN_LOCK_UNLOCKED(mm_list.lock),
3429 	};
3430 
3431 #ifdef CONFIG_MEMCG
3432 	if (memcg)
3433 		return &memcg->mm_list;
3434 #endif
3435 	VM_WARN_ON_ONCE(!mem_cgroup_disabled());
3436 
3437 	return &mm_list;
3438 }
3439 
3440 void lru_gen_add_mm(struct mm_struct *mm)
3441 {
3442 	int nid;
3443 	struct mem_cgroup *memcg = get_mem_cgroup_from_mm(mm);
3444 	struct lru_gen_mm_list *mm_list = get_mm_list(memcg);
3445 
3446 	VM_WARN_ON_ONCE(!list_empty(&mm->lru_gen.list));
3447 #ifdef CONFIG_MEMCG
3448 	VM_WARN_ON_ONCE(mm->lru_gen.memcg);
3449 	mm->lru_gen.memcg = memcg;
3450 #endif
3451 	spin_lock(&mm_list->lock);
3452 
3453 	for_each_node_state(nid, N_MEMORY) {
3454 		struct lruvec *lruvec = get_lruvec(memcg, nid);
3455 
3456 		/* the first addition since the last iteration */
3457 		if (lruvec->mm_state.tail == &mm_list->fifo)
3458 			lruvec->mm_state.tail = &mm->lru_gen.list;
3459 	}
3460 
3461 	list_add_tail(&mm->lru_gen.list, &mm_list->fifo);
3462 
3463 	spin_unlock(&mm_list->lock);
3464 }
3465 
3466 void lru_gen_del_mm(struct mm_struct *mm)
3467 {
3468 	int nid;
3469 	struct lru_gen_mm_list *mm_list;
3470 	struct mem_cgroup *memcg = NULL;
3471 
3472 	if (list_empty(&mm->lru_gen.list))
3473 		return;
3474 
3475 #ifdef CONFIG_MEMCG
3476 	memcg = mm->lru_gen.memcg;
3477 #endif
3478 	mm_list = get_mm_list(memcg);
3479 
3480 	spin_lock(&mm_list->lock);
3481 
3482 	for_each_node(nid) {
3483 		struct lruvec *lruvec = get_lruvec(memcg, nid);
3484 
3485 		/* where the current iteration continues after */
3486 		if (lruvec->mm_state.head == &mm->lru_gen.list)
3487 			lruvec->mm_state.head = lruvec->mm_state.head->prev;
3488 
3489 		/* where the last iteration ended before */
3490 		if (lruvec->mm_state.tail == &mm->lru_gen.list)
3491 			lruvec->mm_state.tail = lruvec->mm_state.tail->next;
3492 	}
3493 
3494 	list_del_init(&mm->lru_gen.list);
3495 
3496 	spin_unlock(&mm_list->lock);
3497 
3498 #ifdef CONFIG_MEMCG
3499 	mem_cgroup_put(mm->lru_gen.memcg);
3500 	mm->lru_gen.memcg = NULL;
3501 #endif
3502 }
3503 
3504 #ifdef CONFIG_MEMCG
3505 void lru_gen_migrate_mm(struct mm_struct *mm)
3506 {
3507 	struct mem_cgroup *memcg;
3508 	struct task_struct *task = rcu_dereference_protected(mm->owner, true);
3509 
3510 	VM_WARN_ON_ONCE(task->mm != mm);
3511 	lockdep_assert_held(&task->alloc_lock);
3512 
3513 	/* for mm_update_next_owner() */
3514 	if (mem_cgroup_disabled())
3515 		return;
3516 
3517 	/* migration can happen before addition */
3518 	if (!mm->lru_gen.memcg)
3519 		return;
3520 
3521 	rcu_read_lock();
3522 	memcg = mem_cgroup_from_task(task);
3523 	rcu_read_unlock();
3524 	if (memcg == mm->lru_gen.memcg)
3525 		return;
3526 
3527 	VM_WARN_ON_ONCE(list_empty(&mm->lru_gen.list));
3528 
3529 	lru_gen_del_mm(mm);
3530 	lru_gen_add_mm(mm);
3531 }
3532 #endif
3533 
3534 static void reset_mm_stats(struct lruvec *lruvec, struct lru_gen_mm_walk *walk, bool last)
3535 {
3536 	int i;
3537 	int hist;
3538 
3539 	lockdep_assert_held(&get_mm_list(lruvec_memcg(lruvec))->lock);
3540 
3541 	if (walk) {
3542 		hist = lru_hist_from_seq(walk->max_seq);
3543 
3544 		for (i = 0; i < NR_MM_STATS; i++) {
3545 			WRITE_ONCE(lruvec->mm_state.stats[hist][i],
3546 				   lruvec->mm_state.stats[hist][i] + walk->mm_stats[i]);
3547 			walk->mm_stats[i] = 0;
3548 		}
3549 	}
3550 
3551 	if (NR_HIST_GENS > 1 && last) {
3552 		hist = lru_hist_from_seq(lruvec->mm_state.seq + 1);
3553 
3554 		for (i = 0; i < NR_MM_STATS; i++)
3555 			WRITE_ONCE(lruvec->mm_state.stats[hist][i], 0);
3556 	}
3557 }
3558 
3559 static bool should_skip_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)
3560 {
3561 	int type;
3562 	unsigned long size = 0;
3563 	struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
3564 	int key = pgdat->node_id % BITS_PER_TYPE(mm->lru_gen.bitmap);
3565 
3566 	if (!walk->force_scan && !test_bit(key, &mm->lru_gen.bitmap))
3567 		return true;
3568 
3569 	clear_bit(key, &mm->lru_gen.bitmap);
3570 
3571 	for (type = !walk->can_swap; type < ANON_AND_FILE; type++) {
3572 		size += type ? get_mm_counter(mm, MM_FILEPAGES) :
3573 			       get_mm_counter(mm, MM_ANONPAGES) +
3574 			       get_mm_counter(mm, MM_SHMEMPAGES);
3575 	}
3576 
3577 	if (size < MIN_LRU_BATCH)
3578 		return true;
3579 
3580 	return !mmget_not_zero(mm);
3581 }
3582 
3583 static bool iterate_mm_list(struct lruvec *lruvec, struct lru_gen_mm_walk *walk,
3584 			    struct mm_struct **iter)
3585 {
3586 	bool first = false;
3587 	bool last = false;
3588 	struct mm_struct *mm = NULL;
3589 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
3590 	struct lru_gen_mm_list *mm_list = get_mm_list(memcg);
3591 	struct lru_gen_mm_state *mm_state = &lruvec->mm_state;
3592 
3593 	/*
3594 	 * mm_state->seq is incremented after each iteration of mm_list. There
3595 	 * are three interesting cases for this page table walker:
3596 	 * 1. It tries to start a new iteration with a stale max_seq: there is
3597 	 *    nothing left to do.
3598 	 * 2. It started the next iteration: it needs to reset the Bloom filter
3599 	 *    so that a fresh set of PTE tables can be recorded.
3600 	 * 3. It ended the current iteration: it needs to reset the mm stats
3601 	 *    counters and tell its caller to increment max_seq.
3602 	 */
3603 	spin_lock(&mm_list->lock);
3604 
3605 	VM_WARN_ON_ONCE(mm_state->seq + 1 < walk->max_seq);
3606 
3607 	if (walk->max_seq <= mm_state->seq)
3608 		goto done;
3609 
3610 	if (!mm_state->head)
3611 		mm_state->head = &mm_list->fifo;
3612 
3613 	if (mm_state->head == &mm_list->fifo)
3614 		first = true;
3615 
3616 	do {
3617 		mm_state->head = mm_state->head->next;
3618 		if (mm_state->head == &mm_list->fifo) {
3619 			WRITE_ONCE(mm_state->seq, mm_state->seq + 1);
3620 			last = true;
3621 			break;
3622 		}
3623 
3624 		/* force scan for those added after the last iteration */
3625 		if (!mm_state->tail || mm_state->tail == mm_state->head) {
3626 			mm_state->tail = mm_state->head->next;
3627 			walk->force_scan = true;
3628 		}
3629 
3630 		mm = list_entry(mm_state->head, struct mm_struct, lru_gen.list);
3631 		if (should_skip_mm(mm, walk))
3632 			mm = NULL;
3633 	} while (!mm);
3634 done:
3635 	if (*iter || last)
3636 		reset_mm_stats(lruvec, walk, last);
3637 
3638 	spin_unlock(&mm_list->lock);
3639 
3640 	if (mm && first)
3641 		reset_bloom_filter(lruvec, walk->max_seq + 1);
3642 
3643 	if (*iter)
3644 		mmput_async(*iter);
3645 
3646 	*iter = mm;
3647 
3648 	return last;
3649 }
3650 
3651 static bool iterate_mm_list_nowalk(struct lruvec *lruvec, unsigned long max_seq)
3652 {
3653 	bool success = false;
3654 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
3655 	struct lru_gen_mm_list *mm_list = get_mm_list(memcg);
3656 	struct lru_gen_mm_state *mm_state = &lruvec->mm_state;
3657 
3658 	spin_lock(&mm_list->lock);
3659 
3660 	VM_WARN_ON_ONCE(mm_state->seq + 1 < max_seq);
3661 
3662 	if (max_seq > mm_state->seq) {
3663 		mm_state->head = NULL;
3664 		mm_state->tail = NULL;
3665 		WRITE_ONCE(mm_state->seq, mm_state->seq + 1);
3666 		reset_mm_stats(lruvec, NULL, true);
3667 		success = true;
3668 	}
3669 
3670 	spin_unlock(&mm_list->lock);
3671 
3672 	return success;
3673 }
3674 
3675 /******************************************************************************
3676  *                          PID controller
3677  ******************************************************************************/
3678 
3679 /*
3680  * A feedback loop based on Proportional-Integral-Derivative (PID) controller.
3681  *
3682  * The P term is refaulted/(evicted+protected) from a tier in the generation
3683  * currently being evicted; the I term is the exponential moving average of the
3684  * P term over the generations previously evicted, using the smoothing factor
3685  * 1/2; the D term isn't supported.
3686  *
3687  * The setpoint (SP) is always the first tier of one type; the process variable
3688  * (PV) is either any tier of the other type or any other tier of the same
3689  * type.
3690  *
3691  * The error is the difference between the SP and the PV; the correction is to
3692  * turn off protection when SP>PV or turn on protection when SP<PV.
3693  *
3694  * For future optimizations:
3695  * 1. The D term may discount the other two terms over time so that long-lived
3696  *    generations can resist stale information.
3697  */
3698 struct ctrl_pos {
3699 	unsigned long refaulted;
3700 	unsigned long total;
3701 	int gain;
3702 };
3703 
3704 static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain,
3705 			  struct ctrl_pos *pos)
3706 {
3707 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
3708 	int hist = lru_hist_from_seq(lrugen->min_seq[type]);
3709 
3710 	pos->refaulted = lrugen->avg_refaulted[type][tier] +
3711 			 atomic_long_read(&lrugen->refaulted[hist][type][tier]);
3712 	pos->total = lrugen->avg_total[type][tier] +
3713 		     atomic_long_read(&lrugen->evicted[hist][type][tier]);
3714 	if (tier)
3715 		pos->total += lrugen->protected[hist][type][tier - 1];
3716 	pos->gain = gain;
3717 }
3718 
3719 static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover)
3720 {
3721 	int hist, tier;
3722 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
3723 	bool clear = carryover ? NR_HIST_GENS == 1 : NR_HIST_GENS > 1;
3724 	unsigned long seq = carryover ? lrugen->min_seq[type] : lrugen->max_seq + 1;
3725 
3726 	lockdep_assert_held(&lruvec->lru_lock);
3727 
3728 	if (!carryover && !clear)
3729 		return;
3730 
3731 	hist = lru_hist_from_seq(seq);
3732 
3733 	for (tier = 0; tier < MAX_NR_TIERS; tier++) {
3734 		if (carryover) {
3735 			unsigned long sum;
3736 
3737 			sum = lrugen->avg_refaulted[type][tier] +
3738 			      atomic_long_read(&lrugen->refaulted[hist][type][tier]);
3739 			WRITE_ONCE(lrugen->avg_refaulted[type][tier], sum / 2);
3740 
3741 			sum = lrugen->avg_total[type][tier] +
3742 			      atomic_long_read(&lrugen->evicted[hist][type][tier]);
3743 			if (tier)
3744 				sum += lrugen->protected[hist][type][tier - 1];
3745 			WRITE_ONCE(lrugen->avg_total[type][tier], sum / 2);
3746 		}
3747 
3748 		if (clear) {
3749 			atomic_long_set(&lrugen->refaulted[hist][type][tier], 0);
3750 			atomic_long_set(&lrugen->evicted[hist][type][tier], 0);
3751 			if (tier)
3752 				WRITE_ONCE(lrugen->protected[hist][type][tier - 1], 0);
3753 		}
3754 	}
3755 }
3756 
3757 static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv)
3758 {
3759 	/*
3760 	 * Return true if the PV has a limited number of refaults or a lower
3761 	 * refaulted/total than the SP.
3762 	 */
3763 	return pv->refaulted < MIN_LRU_BATCH ||
3764 	       pv->refaulted * (sp->total + MIN_LRU_BATCH) * sp->gain <=
3765 	       (sp->refaulted + 1) * pv->total * pv->gain;
3766 }
3767 
3768 /******************************************************************************
3769  *                          the aging
3770  ******************************************************************************/
3771 
3772 /* promote pages accessed through page tables */
3773 static int folio_update_gen(struct folio *folio, int gen)
3774 {
3775 	unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
3776 
3777 	VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
3778 	VM_WARN_ON_ONCE(!rcu_read_lock_held());
3779 
3780 	do {
3781 		/* lru_gen_del_folio() has isolated this page? */
3782 		if (!(old_flags & LRU_GEN_MASK)) {
3783 			/* for shrink_folio_list() */
3784 			new_flags = old_flags | BIT(PG_referenced);
3785 			continue;
3786 		}
3787 
3788 		new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS);
3789 		new_flags |= (gen + 1UL) << LRU_GEN_PGOFF;
3790 	} while (!try_cmpxchg(&folio->flags, &old_flags, new_flags));
3791 
3792 	return ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
3793 }
3794 
3795 /* protect pages accessed multiple times through file descriptors */
3796 static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio, bool reclaiming)
3797 {
3798 	int type = folio_is_file_lru(folio);
3799 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
3800 	int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]);
3801 	unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
3802 
3803 	VM_WARN_ON_ONCE_FOLIO(!(old_flags & LRU_GEN_MASK), folio);
3804 
3805 	do {
3806 		new_gen = ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
3807 		/* folio_update_gen() has promoted this page? */
3808 		if (new_gen >= 0 && new_gen != old_gen)
3809 			return new_gen;
3810 
3811 		new_gen = (old_gen + 1) % MAX_NR_GENS;
3812 
3813 		new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS);
3814 		new_flags |= (new_gen + 1UL) << LRU_GEN_PGOFF;
3815 		/* for folio_end_writeback() */
3816 		if (reclaiming)
3817 			new_flags |= BIT(PG_reclaim);
3818 	} while (!try_cmpxchg(&folio->flags, &old_flags, new_flags));
3819 
3820 	lru_gen_update_size(lruvec, folio, old_gen, new_gen);
3821 
3822 	return new_gen;
3823 }
3824 
3825 static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio,
3826 			      int old_gen, int new_gen)
3827 {
3828 	int type = folio_is_file_lru(folio);
3829 	int zone = folio_zonenum(folio);
3830 	int delta = folio_nr_pages(folio);
3831 
3832 	VM_WARN_ON_ONCE(old_gen >= MAX_NR_GENS);
3833 	VM_WARN_ON_ONCE(new_gen >= MAX_NR_GENS);
3834 
3835 	walk->batched++;
3836 
3837 	walk->nr_pages[old_gen][type][zone] -= delta;
3838 	walk->nr_pages[new_gen][type][zone] += delta;
3839 }
3840 
3841 static void reset_batch_size(struct lruvec *lruvec, struct lru_gen_mm_walk *walk)
3842 {
3843 	int gen, type, zone;
3844 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
3845 
3846 	walk->batched = 0;
3847 
3848 	for_each_gen_type_zone(gen, type, zone) {
3849 		enum lru_list lru = type * LRU_INACTIVE_FILE;
3850 		int delta = walk->nr_pages[gen][type][zone];
3851 
3852 		if (!delta)
3853 			continue;
3854 
3855 		walk->nr_pages[gen][type][zone] = 0;
3856 		WRITE_ONCE(lrugen->nr_pages[gen][type][zone],
3857 			   lrugen->nr_pages[gen][type][zone] + delta);
3858 
3859 		if (lru_gen_is_active(lruvec, gen))
3860 			lru += LRU_ACTIVE;
3861 		__update_lru_size(lruvec, lru, zone, delta);
3862 	}
3863 }
3864 
3865 static int should_skip_vma(unsigned long start, unsigned long end, struct mm_walk *args)
3866 {
3867 	struct address_space *mapping;
3868 	struct vm_area_struct *vma = args->vma;
3869 	struct lru_gen_mm_walk *walk = args->private;
3870 
3871 	if (!vma_is_accessible(vma))
3872 		return true;
3873 
3874 	if (is_vm_hugetlb_page(vma))
3875 		return true;
3876 
3877 	if (!vma_has_recency(vma))
3878 		return true;
3879 
3880 	if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL))
3881 		return true;
3882 
3883 	if (vma == get_gate_vma(vma->vm_mm))
3884 		return true;
3885 
3886 	if (vma_is_anonymous(vma))
3887 		return !walk->can_swap;
3888 
3889 	if (WARN_ON_ONCE(!vma->vm_file || !vma->vm_file->f_mapping))
3890 		return true;
3891 
3892 	mapping = vma->vm_file->f_mapping;
3893 	if (mapping_unevictable(mapping))
3894 		return true;
3895 
3896 	if (shmem_mapping(mapping))
3897 		return !walk->can_swap;
3898 
3899 	/* to exclude special mappings like dax, etc. */
3900 	return !mapping->a_ops->read_folio;
3901 }
3902 
3903 /*
3904  * Some userspace memory allocators map many single-page VMAs. Instead of
3905  * returning back to the PGD table for each of such VMAs, finish an entire PMD
3906  * table to reduce zigzags and improve cache performance.
3907  */
3908 static bool get_next_vma(unsigned long mask, unsigned long size, struct mm_walk *args,
3909 			 unsigned long *vm_start, unsigned long *vm_end)
3910 {
3911 	unsigned long start = round_up(*vm_end, size);
3912 	unsigned long end = (start | ~mask) + 1;
3913 	VMA_ITERATOR(vmi, args->mm, start);
3914 
3915 	VM_WARN_ON_ONCE(mask & size);
3916 	VM_WARN_ON_ONCE((start & mask) != (*vm_start & mask));
3917 
3918 	for_each_vma(vmi, args->vma) {
3919 		if (end && end <= args->vma->vm_start)
3920 			return false;
3921 
3922 		if (should_skip_vma(args->vma->vm_start, args->vma->vm_end, args))
3923 			continue;
3924 
3925 		*vm_start = max(start, args->vma->vm_start);
3926 		*vm_end = min(end - 1, args->vma->vm_end - 1) + 1;
3927 
3928 		return true;
3929 	}
3930 
3931 	return false;
3932 }
3933 
3934 static unsigned long get_pte_pfn(pte_t pte, struct vm_area_struct *vma, unsigned long addr)
3935 {
3936 	unsigned long pfn = pte_pfn(pte);
3937 
3938 	VM_WARN_ON_ONCE(addr < vma->vm_start || addr >= vma->vm_end);
3939 
3940 	if (!pte_present(pte) || is_zero_pfn(pfn))
3941 		return -1;
3942 
3943 	if (WARN_ON_ONCE(pte_devmap(pte) || pte_special(pte)))
3944 		return -1;
3945 
3946 	if (WARN_ON_ONCE(!pfn_valid(pfn)))
3947 		return -1;
3948 
3949 	return pfn;
3950 }
3951 
3952 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG)
3953 static unsigned long get_pmd_pfn(pmd_t pmd, struct vm_area_struct *vma, unsigned long addr)
3954 {
3955 	unsigned long pfn = pmd_pfn(pmd);
3956 
3957 	VM_WARN_ON_ONCE(addr < vma->vm_start || addr >= vma->vm_end);
3958 
3959 	if (!pmd_present(pmd) || is_huge_zero_pmd(pmd))
3960 		return -1;
3961 
3962 	if (WARN_ON_ONCE(pmd_devmap(pmd)))
3963 		return -1;
3964 
3965 	if (WARN_ON_ONCE(!pfn_valid(pfn)))
3966 		return -1;
3967 
3968 	return pfn;
3969 }
3970 #endif
3971 
3972 static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *memcg,
3973 				   struct pglist_data *pgdat, bool can_swap)
3974 {
3975 	struct folio *folio;
3976 
3977 	/* try to avoid unnecessary memory loads */
3978 	if (pfn < pgdat->node_start_pfn || pfn >= pgdat_end_pfn(pgdat))
3979 		return NULL;
3980 
3981 	folio = pfn_folio(pfn);
3982 	if (folio_nid(folio) != pgdat->node_id)
3983 		return NULL;
3984 
3985 	if (folio_memcg_rcu(folio) != memcg)
3986 		return NULL;
3987 
3988 	/* file VMAs can contain anon pages from COW */
3989 	if (!folio_is_file_lru(folio) && !can_swap)
3990 		return NULL;
3991 
3992 	return folio;
3993 }
3994 
3995 static bool suitable_to_scan(int total, int young)
3996 {
3997 	int n = clamp_t(int, cache_line_size() / sizeof(pte_t), 2, 8);
3998 
3999 	/* suitable if the average number of young PTEs per cacheline is >=1 */
4000 	return young * n >= total;
4001 }
4002 
4003 static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
4004 			   struct mm_walk *args)
4005 {
4006 	int i;
4007 	pte_t *pte;
4008 	spinlock_t *ptl;
4009 	unsigned long addr;
4010 	int total = 0;
4011 	int young = 0;
4012 	struct lru_gen_mm_walk *walk = args->private;
4013 	struct mem_cgroup *memcg = lruvec_memcg(walk->lruvec);
4014 	struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
4015 	int old_gen, new_gen = lru_gen_from_seq(walk->max_seq);
4016 
4017 	pte = pte_offset_map_nolock(args->mm, pmd, start & PMD_MASK, &ptl);
4018 	if (!pte)
4019 		return false;
4020 	if (!spin_trylock(ptl)) {
4021 		pte_unmap(pte);
4022 		return false;
4023 	}
4024 
4025 	arch_enter_lazy_mmu_mode();
4026 restart:
4027 	for (i = pte_index(start), addr = start; addr != end; i++, addr += PAGE_SIZE) {
4028 		unsigned long pfn;
4029 		struct folio *folio;
4030 		pte_t ptent = ptep_get(pte + i);
4031 
4032 		total++;
4033 		walk->mm_stats[MM_LEAF_TOTAL]++;
4034 
4035 		pfn = get_pte_pfn(ptent, args->vma, addr);
4036 		if (pfn == -1)
4037 			continue;
4038 
4039 		if (!pte_young(ptent)) {
4040 			walk->mm_stats[MM_LEAF_OLD]++;
4041 			continue;
4042 		}
4043 
4044 		folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap);
4045 		if (!folio)
4046 			continue;
4047 
4048 		if (!ptep_test_and_clear_young(args->vma, addr, pte + i))
4049 			VM_WARN_ON_ONCE(true);
4050 
4051 		young++;
4052 		walk->mm_stats[MM_LEAF_YOUNG]++;
4053 
4054 		if (pte_dirty(ptent) && !folio_test_dirty(folio) &&
4055 		    !(folio_test_anon(folio) && folio_test_swapbacked(folio) &&
4056 		      !folio_test_swapcache(folio)))
4057 			folio_mark_dirty(folio);
4058 
4059 		old_gen = folio_update_gen(folio, new_gen);
4060 		if (old_gen >= 0 && old_gen != new_gen)
4061 			update_batch_size(walk, folio, old_gen, new_gen);
4062 	}
4063 
4064 	if (i < PTRS_PER_PTE && get_next_vma(PMD_MASK, PAGE_SIZE, args, &start, &end))
4065 		goto restart;
4066 
4067 	arch_leave_lazy_mmu_mode();
4068 	pte_unmap_unlock(pte, ptl);
4069 
4070 	return suitable_to_scan(total, young);
4071 }
4072 
4073 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG)
4074 static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area_struct *vma,
4075 				  struct mm_walk *args, unsigned long *bitmap, unsigned long *first)
4076 {
4077 	int i;
4078 	pmd_t *pmd;
4079 	spinlock_t *ptl;
4080 	struct lru_gen_mm_walk *walk = args->private;
4081 	struct mem_cgroup *memcg = lruvec_memcg(walk->lruvec);
4082 	struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
4083 	int old_gen, new_gen = lru_gen_from_seq(walk->max_seq);
4084 
4085 	VM_WARN_ON_ONCE(pud_leaf(*pud));
4086 
4087 	/* try to batch at most 1+MIN_LRU_BATCH+1 entries */
4088 	if (*first == -1) {
4089 		*first = addr;
4090 		bitmap_zero(bitmap, MIN_LRU_BATCH);
4091 		return;
4092 	}
4093 
4094 	i = addr == -1 ? 0 : pmd_index(addr) - pmd_index(*first);
4095 	if (i && i <= MIN_LRU_BATCH) {
4096 		__set_bit(i - 1, bitmap);
4097 		return;
4098 	}
4099 
4100 	pmd = pmd_offset(pud, *first);
4101 
4102 	ptl = pmd_lockptr(args->mm, pmd);
4103 	if (!spin_trylock(ptl))
4104 		goto done;
4105 
4106 	arch_enter_lazy_mmu_mode();
4107 
4108 	do {
4109 		unsigned long pfn;
4110 		struct folio *folio;
4111 
4112 		/* don't round down the first address */
4113 		addr = i ? (*first & PMD_MASK) + i * PMD_SIZE : *first;
4114 
4115 		pfn = get_pmd_pfn(pmd[i], vma, addr);
4116 		if (pfn == -1)
4117 			goto next;
4118 
4119 		if (!pmd_trans_huge(pmd[i])) {
4120 			if (should_clear_pmd_young())
4121 				pmdp_test_and_clear_young(vma, addr, pmd + i);
4122 			goto next;
4123 		}
4124 
4125 		folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap);
4126 		if (!folio)
4127 			goto next;
4128 
4129 		if (!pmdp_test_and_clear_young(vma, addr, pmd + i))
4130 			goto next;
4131 
4132 		walk->mm_stats[MM_LEAF_YOUNG]++;
4133 
4134 		if (pmd_dirty(pmd[i]) && !folio_test_dirty(folio) &&
4135 		    !(folio_test_anon(folio) && folio_test_swapbacked(folio) &&
4136 		      !folio_test_swapcache(folio)))
4137 			folio_mark_dirty(folio);
4138 
4139 		old_gen = folio_update_gen(folio, new_gen);
4140 		if (old_gen >= 0 && old_gen != new_gen)
4141 			update_batch_size(walk, folio, old_gen, new_gen);
4142 next:
4143 		i = i > MIN_LRU_BATCH ? 0 : find_next_bit(bitmap, MIN_LRU_BATCH, i) + 1;
4144 	} while (i <= MIN_LRU_BATCH);
4145 
4146 	arch_leave_lazy_mmu_mode();
4147 	spin_unlock(ptl);
4148 done:
4149 	*first = -1;
4150 }
4151 #else
4152 static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area_struct *vma,
4153 				  struct mm_walk *args, unsigned long *bitmap, unsigned long *first)
4154 {
4155 }
4156 #endif
4157 
4158 static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,
4159 			   struct mm_walk *args)
4160 {
4161 	int i;
4162 	pmd_t *pmd;
4163 	unsigned long next;
4164 	unsigned long addr;
4165 	struct vm_area_struct *vma;
4166 	DECLARE_BITMAP(bitmap, MIN_LRU_BATCH);
4167 	unsigned long first = -1;
4168 	struct lru_gen_mm_walk *walk = args->private;
4169 
4170 	VM_WARN_ON_ONCE(pud_leaf(*pud));
4171 
4172 	/*
4173 	 * Finish an entire PMD in two passes: the first only reaches to PTE
4174 	 * tables to avoid taking the PMD lock; the second, if necessary, takes
4175 	 * the PMD lock to clear the accessed bit in PMD entries.
4176 	 */
4177 	pmd = pmd_offset(pud, start & PUD_MASK);
4178 restart:
4179 	/* walk_pte_range() may call get_next_vma() */
4180 	vma = args->vma;
4181 	for (i = pmd_index(start), addr = start; addr != end; i++, addr = next) {
4182 		pmd_t val = pmdp_get_lockless(pmd + i);
4183 
4184 		next = pmd_addr_end(addr, end);
4185 
4186 		if (!pmd_present(val) || is_huge_zero_pmd(val)) {
4187 			walk->mm_stats[MM_LEAF_TOTAL]++;
4188 			continue;
4189 		}
4190 
4191 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
4192 		if (pmd_trans_huge(val)) {
4193 			unsigned long pfn = pmd_pfn(val);
4194 			struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
4195 
4196 			walk->mm_stats[MM_LEAF_TOTAL]++;
4197 
4198 			if (!pmd_young(val)) {
4199 				walk->mm_stats[MM_LEAF_OLD]++;
4200 				continue;
4201 			}
4202 
4203 			/* try to avoid unnecessary memory loads */
4204 			if (pfn < pgdat->node_start_pfn || pfn >= pgdat_end_pfn(pgdat))
4205 				continue;
4206 
4207 			walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first);
4208 			continue;
4209 		}
4210 #endif
4211 		walk->mm_stats[MM_NONLEAF_TOTAL]++;
4212 
4213 		if (should_clear_pmd_young()) {
4214 			if (!pmd_young(val))
4215 				continue;
4216 
4217 			walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first);
4218 		}
4219 
4220 		if (!walk->force_scan && !test_bloom_filter(walk->lruvec, walk->max_seq, pmd + i))
4221 			continue;
4222 
4223 		walk->mm_stats[MM_NONLEAF_FOUND]++;
4224 
4225 		if (!walk_pte_range(&val, addr, next, args))
4226 			continue;
4227 
4228 		walk->mm_stats[MM_NONLEAF_ADDED]++;
4229 
4230 		/* carry over to the next generation */
4231 		update_bloom_filter(walk->lruvec, walk->max_seq + 1, pmd + i);
4232 	}
4233 
4234 	walk_pmd_range_locked(pud, -1, vma, args, bitmap, &first);
4235 
4236 	if (i < PTRS_PER_PMD && get_next_vma(PUD_MASK, PMD_SIZE, args, &start, &end))
4237 		goto restart;
4238 }
4239 
4240 static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,
4241 			  struct mm_walk *args)
4242 {
4243 	int i;
4244 	pud_t *pud;
4245 	unsigned long addr;
4246 	unsigned long next;
4247 	struct lru_gen_mm_walk *walk = args->private;
4248 
4249 	VM_WARN_ON_ONCE(p4d_leaf(*p4d));
4250 
4251 	pud = pud_offset(p4d, start & P4D_MASK);
4252 restart:
4253 	for (i = pud_index(start), addr = start; addr != end; i++, addr = next) {
4254 		pud_t val = READ_ONCE(pud[i]);
4255 
4256 		next = pud_addr_end(addr, end);
4257 
4258 		if (!pud_present(val) || WARN_ON_ONCE(pud_leaf(val)))
4259 			continue;
4260 
4261 		walk_pmd_range(&val, addr, next, args);
4262 
4263 		if (need_resched() || walk->batched >= MAX_LRU_BATCH) {
4264 			end = (addr | ~PUD_MASK) + 1;
4265 			goto done;
4266 		}
4267 	}
4268 
4269 	if (i < PTRS_PER_PUD && get_next_vma(P4D_MASK, PUD_SIZE, args, &start, &end))
4270 		goto restart;
4271 
4272 	end = round_up(end, P4D_SIZE);
4273 done:
4274 	if (!end || !args->vma)
4275 		return 1;
4276 
4277 	walk->next_addr = max(end, args->vma->vm_start);
4278 
4279 	return -EAGAIN;
4280 }
4281 
4282 static void walk_mm(struct lruvec *lruvec, struct mm_struct *mm, struct lru_gen_mm_walk *walk)
4283 {
4284 	static const struct mm_walk_ops mm_walk_ops = {
4285 		.test_walk = should_skip_vma,
4286 		.p4d_entry = walk_pud_range,
4287 		.walk_lock = PGWALK_RDLOCK,
4288 	};
4289 
4290 	int err;
4291 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
4292 
4293 	walk->next_addr = FIRST_USER_ADDRESS;
4294 
4295 	do {
4296 		DEFINE_MAX_SEQ(lruvec);
4297 
4298 		err = -EBUSY;
4299 
4300 		/* another thread might have called inc_max_seq() */
4301 		if (walk->max_seq != max_seq)
4302 			break;
4303 
4304 		/* folio_update_gen() requires stable folio_memcg() */
4305 		if (!mem_cgroup_trylock_pages(memcg))
4306 			break;
4307 
4308 		/* the caller might be holding the lock for write */
4309 		if (mmap_read_trylock(mm)) {
4310 			err = walk_page_range(mm, walk->next_addr, ULONG_MAX, &mm_walk_ops, walk);
4311 
4312 			mmap_read_unlock(mm);
4313 		}
4314 
4315 		mem_cgroup_unlock_pages();
4316 
4317 		if (walk->batched) {
4318 			spin_lock_irq(&lruvec->lru_lock);
4319 			reset_batch_size(lruvec, walk);
4320 			spin_unlock_irq(&lruvec->lru_lock);
4321 		}
4322 
4323 		cond_resched();
4324 	} while (err == -EAGAIN);
4325 }
4326 
4327 static struct lru_gen_mm_walk *set_mm_walk(struct pglist_data *pgdat, bool force_alloc)
4328 {
4329 	struct lru_gen_mm_walk *walk = current->reclaim_state->mm_walk;
4330 
4331 	if (pgdat && current_is_kswapd()) {
4332 		VM_WARN_ON_ONCE(walk);
4333 
4334 		walk = &pgdat->mm_walk;
4335 	} else if (!walk && force_alloc) {
4336 		VM_WARN_ON_ONCE(current_is_kswapd());
4337 
4338 		walk = kzalloc(sizeof(*walk), __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN);
4339 	}
4340 
4341 	current->reclaim_state->mm_walk = walk;
4342 
4343 	return walk;
4344 }
4345 
4346 static void clear_mm_walk(void)
4347 {
4348 	struct lru_gen_mm_walk *walk = current->reclaim_state->mm_walk;
4349 
4350 	VM_WARN_ON_ONCE(walk && memchr_inv(walk->nr_pages, 0, sizeof(walk->nr_pages)));
4351 	VM_WARN_ON_ONCE(walk && memchr_inv(walk->mm_stats, 0, sizeof(walk->mm_stats)));
4352 
4353 	current->reclaim_state->mm_walk = NULL;
4354 
4355 	if (!current_is_kswapd())
4356 		kfree(walk);
4357 }
4358 
4359 static bool inc_min_seq(struct lruvec *lruvec, int type, bool can_swap)
4360 {
4361 	int zone;
4362 	int remaining = MAX_LRU_BATCH;
4363 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
4364 	int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]);
4365 
4366 	if (type == LRU_GEN_ANON && !can_swap)
4367 		goto done;
4368 
4369 	/* prevent cold/hot inversion if force_scan is true */
4370 	for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4371 		struct list_head *head = &lrugen->folios[old_gen][type][zone];
4372 
4373 		while (!list_empty(head)) {
4374 			struct folio *folio = lru_to_folio(head);
4375 
4376 			VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
4377 			VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio);
4378 			VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio);
4379 			VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio);
4380 
4381 			new_gen = folio_inc_gen(lruvec, folio, false);
4382 			list_move_tail(&folio->lru, &lrugen->folios[new_gen][type][zone]);
4383 
4384 			if (!--remaining)
4385 				return false;
4386 		}
4387 	}
4388 done:
4389 	reset_ctrl_pos(lruvec, type, true);
4390 	WRITE_ONCE(lrugen->min_seq[type], lrugen->min_seq[type] + 1);
4391 
4392 	return true;
4393 }
4394 
4395 static bool try_to_inc_min_seq(struct lruvec *lruvec, bool can_swap)
4396 {
4397 	int gen, type, zone;
4398 	bool success = false;
4399 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
4400 	DEFINE_MIN_SEQ(lruvec);
4401 
4402 	VM_WARN_ON_ONCE(!seq_is_valid(lruvec));
4403 
4404 	/* find the oldest populated generation */
4405 	for (type = !can_swap; type < ANON_AND_FILE; type++) {
4406 		while (min_seq[type] + MIN_NR_GENS <= lrugen->max_seq) {
4407 			gen = lru_gen_from_seq(min_seq[type]);
4408 
4409 			for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4410 				if (!list_empty(&lrugen->folios[gen][type][zone]))
4411 					goto next;
4412 			}
4413 
4414 			min_seq[type]++;
4415 		}
4416 next:
4417 		;
4418 	}
4419 
4420 	/* see the comment on lru_gen_folio */
4421 	if (can_swap) {
4422 		min_seq[LRU_GEN_ANON] = min(min_seq[LRU_GEN_ANON], min_seq[LRU_GEN_FILE]);
4423 		min_seq[LRU_GEN_FILE] = max(min_seq[LRU_GEN_ANON], lrugen->min_seq[LRU_GEN_FILE]);
4424 	}
4425 
4426 	for (type = !can_swap; type < ANON_AND_FILE; type++) {
4427 		if (min_seq[type] == lrugen->min_seq[type])
4428 			continue;
4429 
4430 		reset_ctrl_pos(lruvec, type, true);
4431 		WRITE_ONCE(lrugen->min_seq[type], min_seq[type]);
4432 		success = true;
4433 	}
4434 
4435 	return success;
4436 }
4437 
4438 static void inc_max_seq(struct lruvec *lruvec, bool can_swap, bool force_scan)
4439 {
4440 	int prev, next;
4441 	int type, zone;
4442 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
4443 restart:
4444 	spin_lock_irq(&lruvec->lru_lock);
4445 
4446 	VM_WARN_ON_ONCE(!seq_is_valid(lruvec));
4447 
4448 	for (type = ANON_AND_FILE - 1; type >= 0; type--) {
4449 		if (get_nr_gens(lruvec, type) != MAX_NR_GENS)
4450 			continue;
4451 
4452 		VM_WARN_ON_ONCE(!force_scan && (type == LRU_GEN_FILE || can_swap));
4453 
4454 		if (inc_min_seq(lruvec, type, can_swap))
4455 			continue;
4456 
4457 		spin_unlock_irq(&lruvec->lru_lock);
4458 		cond_resched();
4459 		goto restart;
4460 	}
4461 
4462 	/*
4463 	 * Update the active/inactive LRU sizes for compatibility. Both sides of
4464 	 * the current max_seq need to be covered, since max_seq+1 can overlap
4465 	 * with min_seq[LRU_GEN_ANON] if swapping is constrained. And if they do
4466 	 * overlap, cold/hot inversion happens.
4467 	 */
4468 	prev = lru_gen_from_seq(lrugen->max_seq - 1);
4469 	next = lru_gen_from_seq(lrugen->max_seq + 1);
4470 
4471 	for (type = 0; type < ANON_AND_FILE; type++) {
4472 		for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4473 			enum lru_list lru = type * LRU_INACTIVE_FILE;
4474 			long delta = lrugen->nr_pages[prev][type][zone] -
4475 				     lrugen->nr_pages[next][type][zone];
4476 
4477 			if (!delta)
4478 				continue;
4479 
4480 			__update_lru_size(lruvec, lru, zone, delta);
4481 			__update_lru_size(lruvec, lru + LRU_ACTIVE, zone, -delta);
4482 		}
4483 	}
4484 
4485 	for (type = 0; type < ANON_AND_FILE; type++)
4486 		reset_ctrl_pos(lruvec, type, false);
4487 
4488 	WRITE_ONCE(lrugen->timestamps[next], jiffies);
4489 	/* make sure preceding modifications appear */
4490 	smp_store_release(&lrugen->max_seq, lrugen->max_seq + 1);
4491 
4492 	spin_unlock_irq(&lruvec->lru_lock);
4493 }
4494 
4495 static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long max_seq,
4496 			       struct scan_control *sc, bool can_swap, bool force_scan)
4497 {
4498 	bool success;
4499 	struct lru_gen_mm_walk *walk;
4500 	struct mm_struct *mm = NULL;
4501 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
4502 
4503 	VM_WARN_ON_ONCE(max_seq > READ_ONCE(lrugen->max_seq));
4504 
4505 	/* see the comment in iterate_mm_list() */
4506 	if (max_seq <= READ_ONCE(lruvec->mm_state.seq)) {
4507 		success = false;
4508 		goto done;
4509 	}
4510 
4511 	/*
4512 	 * If the hardware doesn't automatically set the accessed bit, fallback
4513 	 * to lru_gen_look_around(), which only clears the accessed bit in a
4514 	 * handful of PTEs. Spreading the work out over a period of time usually
4515 	 * is less efficient, but it avoids bursty page faults.
4516 	 */
4517 	if (!should_walk_mmu()) {
4518 		success = iterate_mm_list_nowalk(lruvec, max_seq);
4519 		goto done;
4520 	}
4521 
4522 	walk = set_mm_walk(NULL, true);
4523 	if (!walk) {
4524 		success = iterate_mm_list_nowalk(lruvec, max_seq);
4525 		goto done;
4526 	}
4527 
4528 	walk->lruvec = lruvec;
4529 	walk->max_seq = max_seq;
4530 	walk->can_swap = can_swap;
4531 	walk->force_scan = force_scan;
4532 
4533 	do {
4534 		success = iterate_mm_list(lruvec, walk, &mm);
4535 		if (mm)
4536 			walk_mm(lruvec, mm, walk);
4537 	} while (mm);
4538 done:
4539 	if (success)
4540 		inc_max_seq(lruvec, can_swap, force_scan);
4541 
4542 	return success;
4543 }
4544 
4545 /******************************************************************************
4546  *                          working set protection
4547  ******************************************************************************/
4548 
4549 static void set_initial_priority(struct pglist_data *pgdat, struct scan_control *sc)
4550 {
4551 	int priority;
4552 	unsigned long reclaimable;
4553 
4554 	if (sc->priority != DEF_PRIORITY || sc->nr_to_reclaim < MIN_LRU_BATCH)
4555 		return;
4556 	/*
4557 	 * Determine the initial priority based on
4558 	 * (total >> priority) * reclaimed_to_scanned_ratio = nr_to_reclaim,
4559 	 * where reclaimed_to_scanned_ratio = inactive / total.
4560 	 */
4561 	reclaimable = node_page_state(pgdat, NR_INACTIVE_FILE);
4562 	if (can_reclaim_anon_pages(NULL, pgdat->node_id, sc))
4563 		reclaimable += node_page_state(pgdat, NR_INACTIVE_ANON);
4564 
4565 	/* round down reclaimable and round up sc->nr_to_reclaim */
4566 	priority = fls_long(reclaimable) - 1 - fls_long(sc->nr_to_reclaim - 1);
4567 
4568 	/*
4569 	 * The estimation is based on LRU pages only, so cap it to prevent
4570 	 * overshoots of shrinker objects by large margins.
4571 	 */
4572 	sc->priority = clamp(priority, DEF_PRIORITY / 2, DEF_PRIORITY);
4573 }
4574 
4575 static bool lruvec_is_sizable(struct lruvec *lruvec, struct scan_control *sc)
4576 {
4577 	int gen, type, zone;
4578 	unsigned long total = 0;
4579 	bool can_swap = get_swappiness(lruvec, sc);
4580 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
4581 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
4582 	DEFINE_MAX_SEQ(lruvec);
4583 	DEFINE_MIN_SEQ(lruvec);
4584 
4585 	for (type = !can_swap; type < ANON_AND_FILE; type++) {
4586 		unsigned long seq;
4587 
4588 		for (seq = min_seq[type]; seq <= max_seq; seq++) {
4589 			gen = lru_gen_from_seq(seq);
4590 
4591 			for (zone = 0; zone < MAX_NR_ZONES; zone++)
4592 				total += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L);
4593 		}
4594 	}
4595 
4596 	/* whether the size is big enough to be helpful */
4597 	return mem_cgroup_online(memcg) ? (total >> sc->priority) : total;
4598 }
4599 
4600 static bool lruvec_is_reclaimable(struct lruvec *lruvec, struct scan_control *sc,
4601 				  unsigned long min_ttl)
4602 {
4603 	int gen;
4604 	unsigned long birth;
4605 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
4606 	DEFINE_MIN_SEQ(lruvec);
4607 
4608 	if (mem_cgroup_below_min(NULL, memcg))
4609 		return false;
4610 
4611 	if (!lruvec_is_sizable(lruvec, sc))
4612 		return false;
4613 
4614 	/* see the comment on lru_gen_folio */
4615 	gen = lru_gen_from_seq(min_seq[LRU_GEN_FILE]);
4616 	birth = READ_ONCE(lruvec->lrugen.timestamps[gen]);
4617 
4618 	return time_is_before_jiffies(birth + min_ttl);
4619 }
4620 
4621 /* to protect the working set of the last N jiffies */
4622 static unsigned long lru_gen_min_ttl __read_mostly;
4623 
4624 static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc)
4625 {
4626 	struct mem_cgroup *memcg;
4627 	unsigned long min_ttl = READ_ONCE(lru_gen_min_ttl);
4628 	bool reclaimable = !min_ttl;
4629 
4630 	VM_WARN_ON_ONCE(!current_is_kswapd());
4631 
4632 	set_initial_priority(pgdat, sc);
4633 
4634 	memcg = mem_cgroup_iter(NULL, NULL, NULL);
4635 	do {
4636 		struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
4637 
4638 		mem_cgroup_calculate_protection(NULL, memcg);
4639 
4640 		if (!reclaimable)
4641 			reclaimable = lruvec_is_reclaimable(lruvec, sc, min_ttl);
4642 	} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)));
4643 
4644 	/*
4645 	 * The main goal is to OOM kill if every generation from all memcgs is
4646 	 * younger than min_ttl. However, another possibility is all memcgs are
4647 	 * either too small or below min.
4648 	 */
4649 	if (!reclaimable && mutex_trylock(&oom_lock)) {
4650 		struct oom_control oc = {
4651 			.gfp_mask = sc->gfp_mask,
4652 		};
4653 
4654 		out_of_memory(&oc);
4655 
4656 		mutex_unlock(&oom_lock);
4657 	}
4658 }
4659 
4660 /******************************************************************************
4661  *                          rmap/PT walk feedback
4662  ******************************************************************************/
4663 
4664 /*
4665  * This function exploits spatial locality when shrink_folio_list() walks the
4666  * rmap. It scans the adjacent PTEs of a young PTE and promotes hot pages. If
4667  * the scan was done cacheline efficiently, it adds the PMD entry pointing to
4668  * the PTE table to the Bloom filter. This forms a feedback loop between the
4669  * eviction and the aging.
4670  */
4671 void lru_gen_look_around(struct page_vma_mapped_walk *pvmw)
4672 {
4673 	int i;
4674 	unsigned long start;
4675 	unsigned long end;
4676 	struct lru_gen_mm_walk *walk;
4677 	int young = 0;
4678 	pte_t *pte = pvmw->pte;
4679 	unsigned long addr = pvmw->address;
4680 	struct vm_area_struct *vma = pvmw->vma;
4681 	struct folio *folio = pfn_folio(pvmw->pfn);
4682 	bool can_swap = !folio_is_file_lru(folio);
4683 	struct mem_cgroup *memcg = folio_memcg(folio);
4684 	struct pglist_data *pgdat = folio_pgdat(folio);
4685 	struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
4686 	DEFINE_MAX_SEQ(lruvec);
4687 	int old_gen, new_gen = lru_gen_from_seq(max_seq);
4688 
4689 	lockdep_assert_held(pvmw->ptl);
4690 	VM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio);
4691 
4692 	if (spin_is_contended(pvmw->ptl))
4693 		return;
4694 
4695 	/* exclude special VMAs containing anon pages from COW */
4696 	if (vma->vm_flags & VM_SPECIAL)
4697 		return;
4698 
4699 	/* avoid taking the LRU lock under the PTL when possible */
4700 	walk = current->reclaim_state ? current->reclaim_state->mm_walk : NULL;
4701 
4702 	start = max(addr & PMD_MASK, vma->vm_start);
4703 	end = min(addr | ~PMD_MASK, vma->vm_end - 1) + 1;
4704 
4705 	if (end - start > MIN_LRU_BATCH * PAGE_SIZE) {
4706 		if (addr - start < MIN_LRU_BATCH * PAGE_SIZE / 2)
4707 			end = start + MIN_LRU_BATCH * PAGE_SIZE;
4708 		else if (end - addr < MIN_LRU_BATCH * PAGE_SIZE / 2)
4709 			start = end - MIN_LRU_BATCH * PAGE_SIZE;
4710 		else {
4711 			start = addr - MIN_LRU_BATCH * PAGE_SIZE / 2;
4712 			end = addr + MIN_LRU_BATCH * PAGE_SIZE / 2;
4713 		}
4714 	}
4715 
4716 	/* folio_update_gen() requires stable folio_memcg() */
4717 	if (!mem_cgroup_trylock_pages(memcg))
4718 		return;
4719 
4720 	arch_enter_lazy_mmu_mode();
4721 
4722 	pte -= (addr - start) / PAGE_SIZE;
4723 
4724 	for (i = 0, addr = start; addr != end; i++, addr += PAGE_SIZE) {
4725 		unsigned long pfn;
4726 		pte_t ptent = ptep_get(pte + i);
4727 
4728 		pfn = get_pte_pfn(ptent, vma, addr);
4729 		if (pfn == -1)
4730 			continue;
4731 
4732 		if (!pte_young(ptent))
4733 			continue;
4734 
4735 		folio = get_pfn_folio(pfn, memcg, pgdat, can_swap);
4736 		if (!folio)
4737 			continue;
4738 
4739 		if (!ptep_test_and_clear_young(vma, addr, pte + i))
4740 			VM_WARN_ON_ONCE(true);
4741 
4742 		young++;
4743 
4744 		if (pte_dirty(ptent) && !folio_test_dirty(folio) &&
4745 		    !(folio_test_anon(folio) && folio_test_swapbacked(folio) &&
4746 		      !folio_test_swapcache(folio)))
4747 			folio_mark_dirty(folio);
4748 
4749 		if (walk) {
4750 			old_gen = folio_update_gen(folio, new_gen);
4751 			if (old_gen >= 0 && old_gen != new_gen)
4752 				update_batch_size(walk, folio, old_gen, new_gen);
4753 
4754 			continue;
4755 		}
4756 
4757 		old_gen = folio_lru_gen(folio);
4758 		if (old_gen < 0)
4759 			folio_set_referenced(folio);
4760 		else if (old_gen != new_gen)
4761 			folio_activate(folio);
4762 	}
4763 
4764 	arch_leave_lazy_mmu_mode();
4765 	mem_cgroup_unlock_pages();
4766 
4767 	/* feedback from rmap walkers to page table walkers */
4768 	if (suitable_to_scan(i, young))
4769 		update_bloom_filter(lruvec, max_seq, pvmw->pmd);
4770 }
4771 
4772 /******************************************************************************
4773  *                          memcg LRU
4774  ******************************************************************************/
4775 
4776 /* see the comment on MEMCG_NR_GENS */
4777 enum {
4778 	MEMCG_LRU_NOP,
4779 	MEMCG_LRU_HEAD,
4780 	MEMCG_LRU_TAIL,
4781 	MEMCG_LRU_OLD,
4782 	MEMCG_LRU_YOUNG,
4783 };
4784 
4785 #ifdef CONFIG_MEMCG
4786 
4787 static int lru_gen_memcg_seg(struct lruvec *lruvec)
4788 {
4789 	return READ_ONCE(lruvec->lrugen.seg);
4790 }
4791 
4792 static void lru_gen_rotate_memcg(struct lruvec *lruvec, int op)
4793 {
4794 	int seg;
4795 	int old, new;
4796 	unsigned long flags;
4797 	int bin = get_random_u32_below(MEMCG_NR_BINS);
4798 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
4799 
4800 	spin_lock_irqsave(&pgdat->memcg_lru.lock, flags);
4801 
4802 	VM_WARN_ON_ONCE(hlist_nulls_unhashed(&lruvec->lrugen.list));
4803 
4804 	seg = 0;
4805 	new = old = lruvec->lrugen.gen;
4806 
4807 	/* see the comment on MEMCG_NR_GENS */
4808 	if (op == MEMCG_LRU_HEAD)
4809 		seg = MEMCG_LRU_HEAD;
4810 	else if (op == MEMCG_LRU_TAIL)
4811 		seg = MEMCG_LRU_TAIL;
4812 	else if (op == MEMCG_LRU_OLD)
4813 		new = get_memcg_gen(pgdat->memcg_lru.seq);
4814 	else if (op == MEMCG_LRU_YOUNG)
4815 		new = get_memcg_gen(pgdat->memcg_lru.seq + 1);
4816 	else
4817 		VM_WARN_ON_ONCE(true);
4818 
4819 	WRITE_ONCE(lruvec->lrugen.seg, seg);
4820 	WRITE_ONCE(lruvec->lrugen.gen, new);
4821 
4822 	hlist_nulls_del_rcu(&lruvec->lrugen.list);
4823 
4824 	if (op == MEMCG_LRU_HEAD || op == MEMCG_LRU_OLD)
4825 		hlist_nulls_add_head_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[new][bin]);
4826 	else
4827 		hlist_nulls_add_tail_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[new][bin]);
4828 
4829 	pgdat->memcg_lru.nr_memcgs[old]--;
4830 	pgdat->memcg_lru.nr_memcgs[new]++;
4831 
4832 	if (!pgdat->memcg_lru.nr_memcgs[old] && old == get_memcg_gen(pgdat->memcg_lru.seq))
4833 		WRITE_ONCE(pgdat->memcg_lru.seq, pgdat->memcg_lru.seq + 1);
4834 
4835 	spin_unlock_irqrestore(&pgdat->memcg_lru.lock, flags);
4836 }
4837 
4838 void lru_gen_online_memcg(struct mem_cgroup *memcg)
4839 {
4840 	int gen;
4841 	int nid;
4842 	int bin = get_random_u32_below(MEMCG_NR_BINS);
4843 
4844 	for_each_node(nid) {
4845 		struct pglist_data *pgdat = NODE_DATA(nid);
4846 		struct lruvec *lruvec = get_lruvec(memcg, nid);
4847 
4848 		spin_lock_irq(&pgdat->memcg_lru.lock);
4849 
4850 		VM_WARN_ON_ONCE(!hlist_nulls_unhashed(&lruvec->lrugen.list));
4851 
4852 		gen = get_memcg_gen(pgdat->memcg_lru.seq);
4853 
4854 		lruvec->lrugen.gen = gen;
4855 
4856 		hlist_nulls_add_tail_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[gen][bin]);
4857 		pgdat->memcg_lru.nr_memcgs[gen]++;
4858 
4859 		spin_unlock_irq(&pgdat->memcg_lru.lock);
4860 	}
4861 }
4862 
4863 void lru_gen_offline_memcg(struct mem_cgroup *memcg)
4864 {
4865 	int nid;
4866 
4867 	for_each_node(nid) {
4868 		struct lruvec *lruvec = get_lruvec(memcg, nid);
4869 
4870 		lru_gen_rotate_memcg(lruvec, MEMCG_LRU_OLD);
4871 	}
4872 }
4873 
4874 void lru_gen_release_memcg(struct mem_cgroup *memcg)
4875 {
4876 	int gen;
4877 	int nid;
4878 
4879 	for_each_node(nid) {
4880 		struct pglist_data *pgdat = NODE_DATA(nid);
4881 		struct lruvec *lruvec = get_lruvec(memcg, nid);
4882 
4883 		spin_lock_irq(&pgdat->memcg_lru.lock);
4884 
4885 		if (hlist_nulls_unhashed(&lruvec->lrugen.list))
4886 			goto unlock;
4887 
4888 		gen = lruvec->lrugen.gen;
4889 
4890 		hlist_nulls_del_init_rcu(&lruvec->lrugen.list);
4891 		pgdat->memcg_lru.nr_memcgs[gen]--;
4892 
4893 		if (!pgdat->memcg_lru.nr_memcgs[gen] && gen == get_memcg_gen(pgdat->memcg_lru.seq))
4894 			WRITE_ONCE(pgdat->memcg_lru.seq, pgdat->memcg_lru.seq + 1);
4895 unlock:
4896 		spin_unlock_irq(&pgdat->memcg_lru.lock);
4897 	}
4898 }
4899 
4900 void lru_gen_soft_reclaim(struct mem_cgroup *memcg, int nid)
4901 {
4902 	struct lruvec *lruvec = get_lruvec(memcg, nid);
4903 
4904 	/* see the comment on MEMCG_NR_GENS */
4905 	if (lru_gen_memcg_seg(lruvec) != MEMCG_LRU_HEAD)
4906 		lru_gen_rotate_memcg(lruvec, MEMCG_LRU_HEAD);
4907 }
4908 
4909 #else /* !CONFIG_MEMCG */
4910 
4911 static int lru_gen_memcg_seg(struct lruvec *lruvec)
4912 {
4913 	return 0;
4914 }
4915 
4916 #endif
4917 
4918 /******************************************************************************
4919  *                          the eviction
4920  ******************************************************************************/
4921 
4922 static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_control *sc,
4923 		       int tier_idx)
4924 {
4925 	bool success;
4926 	int gen = folio_lru_gen(folio);
4927 	int type = folio_is_file_lru(folio);
4928 	int zone = folio_zonenum(folio);
4929 	int delta = folio_nr_pages(folio);
4930 	int refs = folio_lru_refs(folio);
4931 	int tier = lru_tier_from_refs(refs);
4932 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
4933 
4934 	VM_WARN_ON_ONCE_FOLIO(gen >= MAX_NR_GENS, folio);
4935 
4936 	/* unevictable */
4937 	if (!folio_evictable(folio)) {
4938 		success = lru_gen_del_folio(lruvec, folio, true);
4939 		VM_WARN_ON_ONCE_FOLIO(!success, folio);
4940 		folio_set_unevictable(folio);
4941 		lruvec_add_folio(lruvec, folio);
4942 		__count_vm_events(UNEVICTABLE_PGCULLED, delta);
4943 		return true;
4944 	}
4945 
4946 	/* dirty lazyfree */
4947 	if (type == LRU_GEN_FILE && folio_test_anon(folio) && folio_test_dirty(folio)) {
4948 		success = lru_gen_del_folio(lruvec, folio, true);
4949 		VM_WARN_ON_ONCE_FOLIO(!success, folio);
4950 		folio_set_swapbacked(folio);
4951 		lruvec_add_folio_tail(lruvec, folio);
4952 		return true;
4953 	}
4954 
4955 	/* promoted */
4956 	if (gen != lru_gen_from_seq(lrugen->min_seq[type])) {
4957 		list_move(&folio->lru, &lrugen->folios[gen][type][zone]);
4958 		return true;
4959 	}
4960 
4961 	/* protected */
4962 	if (tier > tier_idx || refs == BIT(LRU_REFS_WIDTH)) {
4963 		int hist = lru_hist_from_seq(lrugen->min_seq[type]);
4964 
4965 		gen = folio_inc_gen(lruvec, folio, false);
4966 		list_move_tail(&folio->lru, &lrugen->folios[gen][type][zone]);
4967 
4968 		WRITE_ONCE(lrugen->protected[hist][type][tier - 1],
4969 			   lrugen->protected[hist][type][tier - 1] + delta);
4970 		return true;
4971 	}
4972 
4973 	/* ineligible */
4974 	if (zone > sc->reclaim_idx || skip_cma(folio, sc)) {
4975 		gen = folio_inc_gen(lruvec, folio, false);
4976 		list_move_tail(&folio->lru, &lrugen->folios[gen][type][zone]);
4977 		return true;
4978 	}
4979 
4980 	/* waiting for writeback */
4981 	if (folio_test_locked(folio) || folio_test_writeback(folio) ||
4982 	    (type == LRU_GEN_FILE && folio_test_dirty(folio))) {
4983 		gen = folio_inc_gen(lruvec, folio, true);
4984 		list_move(&folio->lru, &lrugen->folios[gen][type][zone]);
4985 		return true;
4986 	}
4987 
4988 	return false;
4989 }
4990 
4991 static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct scan_control *sc)
4992 {
4993 	bool success;
4994 
4995 	/* swapping inhibited */
4996 	if (!(sc->gfp_mask & __GFP_IO) &&
4997 	    (folio_test_dirty(folio) ||
4998 	     (folio_test_anon(folio) && !folio_test_swapcache(folio))))
4999 		return false;
5000 
5001 	/* raced with release_pages() */
5002 	if (!folio_try_get(folio))
5003 		return false;
5004 
5005 	/* raced with another isolation */
5006 	if (!folio_test_clear_lru(folio)) {
5007 		folio_put(folio);
5008 		return false;
5009 	}
5010 
5011 	/* see the comment on MAX_NR_TIERS */
5012 	if (!folio_test_referenced(folio))
5013 		set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, 0);
5014 
5015 	/* for shrink_folio_list() */
5016 	folio_clear_reclaim(folio);
5017 	folio_clear_referenced(folio);
5018 
5019 	success = lru_gen_del_folio(lruvec, folio, true);
5020 	VM_WARN_ON_ONCE_FOLIO(!success, folio);
5021 
5022 	return true;
5023 }
5024 
5025 static int scan_folios(struct lruvec *lruvec, struct scan_control *sc,
5026 		       int type, int tier, struct list_head *list)
5027 {
5028 	int i;
5029 	int gen;
5030 	enum vm_event_item item;
5031 	int sorted = 0;
5032 	int scanned = 0;
5033 	int isolated = 0;
5034 	int remaining = MAX_LRU_BATCH;
5035 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
5036 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
5037 
5038 	VM_WARN_ON_ONCE(!list_empty(list));
5039 
5040 	if (get_nr_gens(lruvec, type) == MIN_NR_GENS)
5041 		return 0;
5042 
5043 	gen = lru_gen_from_seq(lrugen->min_seq[type]);
5044 
5045 	for (i = MAX_NR_ZONES; i > 0; i--) {
5046 		LIST_HEAD(moved);
5047 		int skipped = 0;
5048 		int zone = (sc->reclaim_idx + i) % MAX_NR_ZONES;
5049 		struct list_head *head = &lrugen->folios[gen][type][zone];
5050 
5051 		while (!list_empty(head)) {
5052 			struct folio *folio = lru_to_folio(head);
5053 			int delta = folio_nr_pages(folio);
5054 
5055 			VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
5056 			VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio);
5057 			VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio);
5058 			VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio);
5059 
5060 			scanned += delta;
5061 
5062 			if (sort_folio(lruvec, folio, sc, tier))
5063 				sorted += delta;
5064 			else if (isolate_folio(lruvec, folio, sc)) {
5065 				list_add(&folio->lru, list);
5066 				isolated += delta;
5067 			} else {
5068 				list_move(&folio->lru, &moved);
5069 				skipped += delta;
5070 			}
5071 
5072 			if (!--remaining || max(isolated, skipped) >= MIN_LRU_BATCH)
5073 				break;
5074 		}
5075 
5076 		if (skipped) {
5077 			list_splice(&moved, head);
5078 			__count_zid_vm_events(PGSCAN_SKIP, zone, skipped);
5079 		}
5080 
5081 		if (!remaining || isolated >= MIN_LRU_BATCH)
5082 			break;
5083 	}
5084 
5085 	item = PGSCAN_KSWAPD + reclaimer_offset();
5086 	if (!cgroup_reclaim(sc)) {
5087 		__count_vm_events(item, isolated);
5088 		__count_vm_events(PGREFILL, sorted);
5089 	}
5090 	__count_memcg_events(memcg, item, isolated);
5091 	__count_memcg_events(memcg, PGREFILL, sorted);
5092 	__count_vm_events(PGSCAN_ANON + type, isolated);
5093 
5094 	/*
5095 	 * There might not be eligible folios due to reclaim_idx. Check the
5096 	 * remaining to prevent livelock if it's not making progress.
5097 	 */
5098 	return isolated || !remaining ? scanned : 0;
5099 }
5100 
5101 static int get_tier_idx(struct lruvec *lruvec, int type)
5102 {
5103 	int tier;
5104 	struct ctrl_pos sp, pv;
5105 
5106 	/*
5107 	 * To leave a margin for fluctuations, use a larger gain factor (1:2).
5108 	 * This value is chosen because any other tier would have at least twice
5109 	 * as many refaults as the first tier.
5110 	 */
5111 	read_ctrl_pos(lruvec, type, 0, 1, &sp);
5112 	for (tier = 1; tier < MAX_NR_TIERS; tier++) {
5113 		read_ctrl_pos(lruvec, type, tier, 2, &pv);
5114 		if (!positive_ctrl_err(&sp, &pv))
5115 			break;
5116 	}
5117 
5118 	return tier - 1;
5119 }
5120 
5121 static int get_type_to_scan(struct lruvec *lruvec, int swappiness, int *tier_idx)
5122 {
5123 	int type, tier;
5124 	struct ctrl_pos sp, pv;
5125 	int gain[ANON_AND_FILE] = { swappiness, 200 - swappiness };
5126 
5127 	/*
5128 	 * Compare the first tier of anon with that of file to determine which
5129 	 * type to scan. Also need to compare other tiers of the selected type
5130 	 * with the first tier of the other type to determine the last tier (of
5131 	 * the selected type) to evict.
5132 	 */
5133 	read_ctrl_pos(lruvec, LRU_GEN_ANON, 0, gain[LRU_GEN_ANON], &sp);
5134 	read_ctrl_pos(lruvec, LRU_GEN_FILE, 0, gain[LRU_GEN_FILE], &pv);
5135 	type = positive_ctrl_err(&sp, &pv);
5136 
5137 	read_ctrl_pos(lruvec, !type, 0, gain[!type], &sp);
5138 	for (tier = 1; tier < MAX_NR_TIERS; tier++) {
5139 		read_ctrl_pos(lruvec, type, tier, gain[type], &pv);
5140 		if (!positive_ctrl_err(&sp, &pv))
5141 			break;
5142 	}
5143 
5144 	*tier_idx = tier - 1;
5145 
5146 	return type;
5147 }
5148 
5149 static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness,
5150 			  int *type_scanned, struct list_head *list)
5151 {
5152 	int i;
5153 	int type;
5154 	int scanned;
5155 	int tier = -1;
5156 	DEFINE_MIN_SEQ(lruvec);
5157 
5158 	/*
5159 	 * Try to make the obvious choice first. When anon and file are both
5160 	 * available from the same generation, interpret swappiness 1 as file
5161 	 * first and 200 as anon first.
5162 	 */
5163 	if (!swappiness)
5164 		type = LRU_GEN_FILE;
5165 	else if (min_seq[LRU_GEN_ANON] < min_seq[LRU_GEN_FILE])
5166 		type = LRU_GEN_ANON;
5167 	else if (swappiness == 1)
5168 		type = LRU_GEN_FILE;
5169 	else if (swappiness == 200)
5170 		type = LRU_GEN_ANON;
5171 	else
5172 		type = get_type_to_scan(lruvec, swappiness, &tier);
5173 
5174 	for (i = !swappiness; i < ANON_AND_FILE; i++) {
5175 		if (tier < 0)
5176 			tier = get_tier_idx(lruvec, type);
5177 
5178 		scanned = scan_folios(lruvec, sc, type, tier, list);
5179 		if (scanned)
5180 			break;
5181 
5182 		type = !type;
5183 		tier = -1;
5184 	}
5185 
5186 	*type_scanned = type;
5187 
5188 	return scanned;
5189 }
5190 
5191 static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness)
5192 {
5193 	int type;
5194 	int scanned;
5195 	int reclaimed;
5196 	LIST_HEAD(list);
5197 	LIST_HEAD(clean);
5198 	struct folio *folio;
5199 	struct folio *next;
5200 	enum vm_event_item item;
5201 	struct reclaim_stat stat;
5202 	struct lru_gen_mm_walk *walk;
5203 	bool skip_retry = false;
5204 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
5205 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
5206 
5207 	spin_lock_irq(&lruvec->lru_lock);
5208 
5209 	scanned = isolate_folios(lruvec, sc, swappiness, &type, &list);
5210 
5211 	scanned += try_to_inc_min_seq(lruvec, swappiness);
5212 
5213 	if (get_nr_gens(lruvec, !swappiness) == MIN_NR_GENS)
5214 		scanned = 0;
5215 
5216 	spin_unlock_irq(&lruvec->lru_lock);
5217 
5218 	if (list_empty(&list))
5219 		return scanned;
5220 retry:
5221 	reclaimed = shrink_folio_list(&list, pgdat, sc, &stat, false);
5222 	sc->nr_reclaimed += reclaimed;
5223 
5224 	list_for_each_entry_safe_reverse(folio, next, &list, lru) {
5225 		if (!folio_evictable(folio)) {
5226 			list_del(&folio->lru);
5227 			folio_putback_lru(folio);
5228 			continue;
5229 		}
5230 
5231 		if (folio_test_reclaim(folio) &&
5232 		    (folio_test_dirty(folio) || folio_test_writeback(folio))) {
5233 			/* restore LRU_REFS_FLAGS cleared by isolate_folio() */
5234 			if (folio_test_workingset(folio))
5235 				folio_set_referenced(folio);
5236 			continue;
5237 		}
5238 
5239 		if (skip_retry || folio_test_active(folio) || folio_test_referenced(folio) ||
5240 		    folio_mapped(folio) || folio_test_locked(folio) ||
5241 		    folio_test_dirty(folio) || folio_test_writeback(folio)) {
5242 			/* don't add rejected folios to the oldest generation */
5243 			set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS,
5244 				      BIT(PG_active));
5245 			continue;
5246 		}
5247 
5248 		/* retry folios that may have missed folio_rotate_reclaimable() */
5249 		list_move(&folio->lru, &clean);
5250 	}
5251 
5252 	spin_lock_irq(&lruvec->lru_lock);
5253 
5254 	move_folios_to_lru(lruvec, &list);
5255 
5256 	walk = current->reclaim_state->mm_walk;
5257 	if (walk && walk->batched)
5258 		reset_batch_size(lruvec, walk);
5259 
5260 	item = PGSTEAL_KSWAPD + reclaimer_offset();
5261 	if (!cgroup_reclaim(sc))
5262 		__count_vm_events(item, reclaimed);
5263 	__count_memcg_events(memcg, item, reclaimed);
5264 	__count_vm_events(PGSTEAL_ANON + type, reclaimed);
5265 
5266 	spin_unlock_irq(&lruvec->lru_lock);
5267 
5268 	mem_cgroup_uncharge_list(&list);
5269 	free_unref_page_list(&list);
5270 
5271 	INIT_LIST_HEAD(&list);
5272 	list_splice_init(&clean, &list);
5273 
5274 	if (!list_empty(&list)) {
5275 		skip_retry = true;
5276 		goto retry;
5277 	}
5278 
5279 	return scanned;
5280 }
5281 
5282 static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq,
5283 			     struct scan_control *sc, bool can_swap, unsigned long *nr_to_scan)
5284 {
5285 	int gen, type, zone;
5286 	unsigned long old = 0;
5287 	unsigned long young = 0;
5288 	unsigned long total = 0;
5289 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
5290 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
5291 	DEFINE_MIN_SEQ(lruvec);
5292 
5293 	/* whether this lruvec is completely out of cold folios */
5294 	if (min_seq[!can_swap] + MIN_NR_GENS > max_seq) {
5295 		*nr_to_scan = 0;
5296 		return true;
5297 	}
5298 
5299 	for (type = !can_swap; type < ANON_AND_FILE; type++) {
5300 		unsigned long seq;
5301 
5302 		for (seq = min_seq[type]; seq <= max_seq; seq++) {
5303 			unsigned long size = 0;
5304 
5305 			gen = lru_gen_from_seq(seq);
5306 
5307 			for (zone = 0; zone < MAX_NR_ZONES; zone++)
5308 				size += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L);
5309 
5310 			total += size;
5311 			if (seq == max_seq)
5312 				young += size;
5313 			else if (seq + MIN_NR_GENS == max_seq)
5314 				old += size;
5315 		}
5316 	}
5317 
5318 	/* try to scrape all its memory if this memcg was deleted */
5319 	if (!mem_cgroup_online(memcg)) {
5320 		*nr_to_scan = total;
5321 		return false;
5322 	}
5323 
5324 	*nr_to_scan = total >> sc->priority;
5325 
5326 	/*
5327 	 * The aging tries to be lazy to reduce the overhead, while the eviction
5328 	 * stalls when the number of generations reaches MIN_NR_GENS. Hence, the
5329 	 * ideal number of generations is MIN_NR_GENS+1.
5330 	 */
5331 	if (min_seq[!can_swap] + MIN_NR_GENS < max_seq)
5332 		return false;
5333 
5334 	/*
5335 	 * It's also ideal to spread pages out evenly, i.e., 1/(MIN_NR_GENS+1)
5336 	 * of the total number of pages for each generation. A reasonable range
5337 	 * for this average portion is [1/MIN_NR_GENS, 1/(MIN_NR_GENS+2)]. The
5338 	 * aging cares about the upper bound of hot pages, while the eviction
5339 	 * cares about the lower bound of cold pages.
5340 	 */
5341 	if (young * MIN_NR_GENS > total)
5342 		return true;
5343 	if (old * (MIN_NR_GENS + 2) < total)
5344 		return true;
5345 
5346 	return false;
5347 }
5348 
5349 /*
5350  * For future optimizations:
5351  * 1. Defer try_to_inc_max_seq() to workqueues to reduce latency for memcg
5352  *    reclaim.
5353  */
5354 static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc, bool can_swap)
5355 {
5356 	unsigned long nr_to_scan;
5357 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
5358 	DEFINE_MAX_SEQ(lruvec);
5359 
5360 	if (mem_cgroup_below_min(sc->target_mem_cgroup, memcg))
5361 		return -1;
5362 
5363 	if (!should_run_aging(lruvec, max_seq, sc, can_swap, &nr_to_scan))
5364 		return nr_to_scan;
5365 
5366 	/* skip the aging path at the default priority */
5367 	if (sc->priority == DEF_PRIORITY)
5368 		return nr_to_scan;
5369 
5370 	/* skip this lruvec as it's low on cold folios */
5371 	return try_to_inc_max_seq(lruvec, max_seq, sc, can_swap, false) ? -1 : 0;
5372 }
5373 
5374 static bool should_abort_scan(struct lruvec *lruvec, struct scan_control *sc)
5375 {
5376 	int i;
5377 	enum zone_watermarks mark;
5378 
5379 	/* don't abort memcg reclaim to ensure fairness */
5380 	if (!root_reclaim(sc))
5381 		return false;
5382 
5383 	if (sc->nr_reclaimed >= max(sc->nr_to_reclaim, compact_gap(sc->order)))
5384 		return true;
5385 
5386 	/* check the order to exclude compaction-induced reclaim */
5387 	if (!current_is_kswapd() || sc->order)
5388 		return false;
5389 
5390 	mark = sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING ?
5391 	       WMARK_PROMO : WMARK_HIGH;
5392 
5393 	for (i = 0; i <= sc->reclaim_idx; i++) {
5394 		struct zone *zone = lruvec_pgdat(lruvec)->node_zones + i;
5395 		unsigned long size = wmark_pages(zone, mark) + MIN_LRU_BATCH;
5396 
5397 		if (managed_zone(zone) && !zone_watermark_ok(zone, 0, size, sc->reclaim_idx, 0))
5398 			return false;
5399 	}
5400 
5401 	/* kswapd should abort if all eligible zones are safe */
5402 	return true;
5403 }
5404 
5405 static bool try_to_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
5406 {
5407 	long nr_to_scan;
5408 	unsigned long scanned = 0;
5409 	int swappiness = get_swappiness(lruvec, sc);
5410 
5411 	/* clean file folios are more likely to exist */
5412 	if (swappiness && !(sc->gfp_mask & __GFP_IO))
5413 		swappiness = 1;
5414 
5415 	while (true) {
5416 		int delta;
5417 
5418 		nr_to_scan = get_nr_to_scan(lruvec, sc, swappiness);
5419 		if (nr_to_scan <= 0)
5420 			break;
5421 
5422 		delta = evict_folios(lruvec, sc, swappiness);
5423 		if (!delta)
5424 			break;
5425 
5426 		scanned += delta;
5427 		if (scanned >= nr_to_scan)
5428 			break;
5429 
5430 		if (should_abort_scan(lruvec, sc))
5431 			break;
5432 
5433 		cond_resched();
5434 	}
5435 
5436 	/* whether this lruvec should be rotated */
5437 	return nr_to_scan < 0;
5438 }
5439 
5440 static int shrink_one(struct lruvec *lruvec, struct scan_control *sc)
5441 {
5442 	bool success;
5443 	unsigned long scanned = sc->nr_scanned;
5444 	unsigned long reclaimed = sc->nr_reclaimed;
5445 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
5446 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
5447 
5448 	/* lru_gen_age_node() called mem_cgroup_calculate_protection() */
5449 	if (mem_cgroup_below_min(NULL, memcg))
5450 		return MEMCG_LRU_YOUNG;
5451 
5452 	if (mem_cgroup_below_low(NULL, memcg)) {
5453 		/* see the comment on MEMCG_NR_GENS */
5454 		if (lru_gen_memcg_seg(lruvec) != MEMCG_LRU_TAIL)
5455 			return MEMCG_LRU_TAIL;
5456 
5457 		memcg_memory_event(memcg, MEMCG_LOW);
5458 	}
5459 
5460 	success = try_to_shrink_lruvec(lruvec, sc);
5461 
5462 	shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, sc->priority);
5463 
5464 	if (!sc->proactive)
5465 		vmpressure(sc->gfp_mask, memcg, false, sc->nr_scanned - scanned,
5466 			   sc->nr_reclaimed - reclaimed);
5467 
5468 	flush_reclaim_state(sc);
5469 
5470 	if (success && mem_cgroup_online(memcg))
5471 		return MEMCG_LRU_YOUNG;
5472 
5473 	if (!success && lruvec_is_sizable(lruvec, sc))
5474 		return 0;
5475 
5476 	/* one retry if offlined or too small */
5477 	return lru_gen_memcg_seg(lruvec) != MEMCG_LRU_TAIL ?
5478 	       MEMCG_LRU_TAIL : MEMCG_LRU_YOUNG;
5479 }
5480 
5481 #ifdef CONFIG_MEMCG
5482 
5483 static void shrink_many(struct pglist_data *pgdat, struct scan_control *sc)
5484 {
5485 	int op;
5486 	int gen;
5487 	int bin;
5488 	int first_bin;
5489 	struct lruvec *lruvec;
5490 	struct lru_gen_folio *lrugen;
5491 	struct mem_cgroup *memcg;
5492 	struct hlist_nulls_node *pos;
5493 
5494 	gen = get_memcg_gen(READ_ONCE(pgdat->memcg_lru.seq));
5495 	bin = first_bin = get_random_u32_below(MEMCG_NR_BINS);
5496 restart:
5497 	op = 0;
5498 	memcg = NULL;
5499 
5500 	rcu_read_lock();
5501 
5502 	hlist_nulls_for_each_entry_rcu(lrugen, pos, &pgdat->memcg_lru.fifo[gen][bin], list) {
5503 		if (op) {
5504 			lru_gen_rotate_memcg(lruvec, op);
5505 			op = 0;
5506 		}
5507 
5508 		mem_cgroup_put(memcg);
5509 		memcg = NULL;
5510 
5511 		if (gen != READ_ONCE(lrugen->gen))
5512 			continue;
5513 
5514 		lruvec = container_of(lrugen, struct lruvec, lrugen);
5515 		memcg = lruvec_memcg(lruvec);
5516 
5517 		if (!mem_cgroup_tryget(memcg)) {
5518 			lru_gen_release_memcg(memcg);
5519 			memcg = NULL;
5520 			continue;
5521 		}
5522 
5523 		rcu_read_unlock();
5524 
5525 		op = shrink_one(lruvec, sc);
5526 
5527 		rcu_read_lock();
5528 
5529 		if (should_abort_scan(lruvec, sc))
5530 			break;
5531 	}
5532 
5533 	rcu_read_unlock();
5534 
5535 	if (op)
5536 		lru_gen_rotate_memcg(lruvec, op);
5537 
5538 	mem_cgroup_put(memcg);
5539 
5540 	if (!is_a_nulls(pos))
5541 		return;
5542 
5543 	/* restart if raced with lru_gen_rotate_memcg() */
5544 	if (gen != get_nulls_value(pos))
5545 		goto restart;
5546 
5547 	/* try the rest of the bins of the current generation */
5548 	bin = get_memcg_bin(bin + 1);
5549 	if (bin != first_bin)
5550 		goto restart;
5551 }
5552 
5553 static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
5554 {
5555 	struct blk_plug plug;
5556 
5557 	VM_WARN_ON_ONCE(root_reclaim(sc));
5558 	VM_WARN_ON_ONCE(!sc->may_writepage || !sc->may_unmap);
5559 
5560 	lru_add_drain();
5561 
5562 	blk_start_plug(&plug);
5563 
5564 	set_mm_walk(NULL, sc->proactive);
5565 
5566 	if (try_to_shrink_lruvec(lruvec, sc))
5567 		lru_gen_rotate_memcg(lruvec, MEMCG_LRU_YOUNG);
5568 
5569 	clear_mm_walk();
5570 
5571 	blk_finish_plug(&plug);
5572 }
5573 
5574 #else /* !CONFIG_MEMCG */
5575 
5576 static void shrink_many(struct pglist_data *pgdat, struct scan_control *sc)
5577 {
5578 	BUILD_BUG();
5579 }
5580 
5581 static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
5582 {
5583 	BUILD_BUG();
5584 }
5585 
5586 #endif
5587 
5588 static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *sc)
5589 {
5590 	struct blk_plug plug;
5591 	unsigned long reclaimed = sc->nr_reclaimed;
5592 
5593 	VM_WARN_ON_ONCE(!root_reclaim(sc));
5594 
5595 	/*
5596 	 * Unmapped clean folios are already prioritized. Scanning for more of
5597 	 * them is likely futile and can cause high reclaim latency when there
5598 	 * is a large number of memcgs.
5599 	 */
5600 	if (!sc->may_writepage || !sc->may_unmap)
5601 		goto done;
5602 
5603 	lru_add_drain();
5604 
5605 	blk_start_plug(&plug);
5606 
5607 	set_mm_walk(pgdat, sc->proactive);
5608 
5609 	set_initial_priority(pgdat, sc);
5610 
5611 	if (current_is_kswapd())
5612 		sc->nr_reclaimed = 0;
5613 
5614 	if (mem_cgroup_disabled())
5615 		shrink_one(&pgdat->__lruvec, sc);
5616 	else
5617 		shrink_many(pgdat, sc);
5618 
5619 	if (current_is_kswapd())
5620 		sc->nr_reclaimed += reclaimed;
5621 
5622 	clear_mm_walk();
5623 
5624 	blk_finish_plug(&plug);
5625 done:
5626 	/* kswapd should never fail */
5627 	pgdat->kswapd_failures = 0;
5628 }
5629 
5630 /******************************************************************************
5631  *                          state change
5632  ******************************************************************************/
5633 
5634 static bool __maybe_unused state_is_valid(struct lruvec *lruvec)
5635 {
5636 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
5637 
5638 	if (lrugen->enabled) {
5639 		enum lru_list lru;
5640 
5641 		for_each_evictable_lru(lru) {
5642 			if (!list_empty(&lruvec->lists[lru]))
5643 				return false;
5644 		}
5645 	} else {
5646 		int gen, type, zone;
5647 
5648 		for_each_gen_type_zone(gen, type, zone) {
5649 			if (!list_empty(&lrugen->folios[gen][type][zone]))
5650 				return false;
5651 		}
5652 	}
5653 
5654 	return true;
5655 }
5656 
5657 static bool fill_evictable(struct lruvec *lruvec)
5658 {
5659 	enum lru_list lru;
5660 	int remaining = MAX_LRU_BATCH;
5661 
5662 	for_each_evictable_lru(lru) {
5663 		int type = is_file_lru(lru);
5664 		bool active = is_active_lru(lru);
5665 		struct list_head *head = &lruvec->lists[lru];
5666 
5667 		while (!list_empty(head)) {
5668 			bool success;
5669 			struct folio *folio = lru_to_folio(head);
5670 
5671 			VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
5672 			VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio) != active, folio);
5673 			VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio);
5674 			VM_WARN_ON_ONCE_FOLIO(folio_lru_gen(folio) != -1, folio);
5675 
5676 			lruvec_del_folio(lruvec, folio);
5677 			success = lru_gen_add_folio(lruvec, folio, false);
5678 			VM_WARN_ON_ONCE(!success);
5679 
5680 			if (!--remaining)
5681 				return false;
5682 		}
5683 	}
5684 
5685 	return true;
5686 }
5687 
5688 static bool drain_evictable(struct lruvec *lruvec)
5689 {
5690 	int gen, type, zone;
5691 	int remaining = MAX_LRU_BATCH;
5692 
5693 	for_each_gen_type_zone(gen, type, zone) {
5694 		struct list_head *head = &lruvec->lrugen.folios[gen][type][zone];
5695 
5696 		while (!list_empty(head)) {
5697 			bool success;
5698 			struct folio *folio = lru_to_folio(head);
5699 
5700 			VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
5701 			VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio);
5702 			VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio);
5703 			VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio);
5704 
5705 			success = lru_gen_del_folio(lruvec, folio, false);
5706 			VM_WARN_ON_ONCE(!success);
5707 			lruvec_add_folio(lruvec, folio);
5708 
5709 			if (!--remaining)
5710 				return false;
5711 		}
5712 	}
5713 
5714 	return true;
5715 }
5716 
5717 static void lru_gen_change_state(bool enabled)
5718 {
5719 	static DEFINE_MUTEX(state_mutex);
5720 
5721 	struct mem_cgroup *memcg;
5722 
5723 	cgroup_lock();
5724 	cpus_read_lock();
5725 	get_online_mems();
5726 	mutex_lock(&state_mutex);
5727 
5728 	if (enabled == lru_gen_enabled())
5729 		goto unlock;
5730 
5731 	if (enabled)
5732 		static_branch_enable_cpuslocked(&lru_gen_caps[LRU_GEN_CORE]);
5733 	else
5734 		static_branch_disable_cpuslocked(&lru_gen_caps[LRU_GEN_CORE]);
5735 
5736 	memcg = mem_cgroup_iter(NULL, NULL, NULL);
5737 	do {
5738 		int nid;
5739 
5740 		for_each_node(nid) {
5741 			struct lruvec *lruvec = get_lruvec(memcg, nid);
5742 
5743 			spin_lock_irq(&lruvec->lru_lock);
5744 
5745 			VM_WARN_ON_ONCE(!seq_is_valid(lruvec));
5746 			VM_WARN_ON_ONCE(!state_is_valid(lruvec));
5747 
5748 			lruvec->lrugen.enabled = enabled;
5749 
5750 			while (!(enabled ? fill_evictable(lruvec) : drain_evictable(lruvec))) {
5751 				spin_unlock_irq(&lruvec->lru_lock);
5752 				cond_resched();
5753 				spin_lock_irq(&lruvec->lru_lock);
5754 			}
5755 
5756 			spin_unlock_irq(&lruvec->lru_lock);
5757 		}
5758 
5759 		cond_resched();
5760 	} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)));
5761 unlock:
5762 	mutex_unlock(&state_mutex);
5763 	put_online_mems();
5764 	cpus_read_unlock();
5765 	cgroup_unlock();
5766 }
5767 
5768 /******************************************************************************
5769  *                          sysfs interface
5770  ******************************************************************************/
5771 
5772 static ssize_t min_ttl_ms_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
5773 {
5774 	return sysfs_emit(buf, "%u\n", jiffies_to_msecs(READ_ONCE(lru_gen_min_ttl)));
5775 }
5776 
5777 /* see Documentation/admin-guide/mm/multigen_lru.rst for details */
5778 static ssize_t min_ttl_ms_store(struct kobject *kobj, struct kobj_attribute *attr,
5779 				const char *buf, size_t len)
5780 {
5781 	unsigned int msecs;
5782 
5783 	if (kstrtouint(buf, 0, &msecs))
5784 		return -EINVAL;
5785 
5786 	WRITE_ONCE(lru_gen_min_ttl, msecs_to_jiffies(msecs));
5787 
5788 	return len;
5789 }
5790 
5791 static struct kobj_attribute lru_gen_min_ttl_attr = __ATTR_RW(min_ttl_ms);
5792 
5793 static ssize_t enabled_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
5794 {
5795 	unsigned int caps = 0;
5796 
5797 	if (get_cap(LRU_GEN_CORE))
5798 		caps |= BIT(LRU_GEN_CORE);
5799 
5800 	if (should_walk_mmu())
5801 		caps |= BIT(LRU_GEN_MM_WALK);
5802 
5803 	if (should_clear_pmd_young())
5804 		caps |= BIT(LRU_GEN_NONLEAF_YOUNG);
5805 
5806 	return sysfs_emit(buf, "0x%04x\n", caps);
5807 }
5808 
5809 /* see Documentation/admin-guide/mm/multigen_lru.rst for details */
5810 static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
5811 			     const char *buf, size_t len)
5812 {
5813 	int i;
5814 	unsigned int caps;
5815 
5816 	if (tolower(*buf) == 'n')
5817 		caps = 0;
5818 	else if (tolower(*buf) == 'y')
5819 		caps = -1;
5820 	else if (kstrtouint(buf, 0, &caps))
5821 		return -EINVAL;
5822 
5823 	for (i = 0; i < NR_LRU_GEN_CAPS; i++) {
5824 		bool enabled = caps & BIT(i);
5825 
5826 		if (i == LRU_GEN_CORE)
5827 			lru_gen_change_state(enabled);
5828 		else if (enabled)
5829 			static_branch_enable(&lru_gen_caps[i]);
5830 		else
5831 			static_branch_disable(&lru_gen_caps[i]);
5832 	}
5833 
5834 	return len;
5835 }
5836 
5837 static struct kobj_attribute lru_gen_enabled_attr = __ATTR_RW(enabled);
5838 
5839 static struct attribute *lru_gen_attrs[] = {
5840 	&lru_gen_min_ttl_attr.attr,
5841 	&lru_gen_enabled_attr.attr,
5842 	NULL
5843 };
5844 
5845 static const struct attribute_group lru_gen_attr_group = {
5846 	.name = "lru_gen",
5847 	.attrs = lru_gen_attrs,
5848 };
5849 
5850 /******************************************************************************
5851  *                          debugfs interface
5852  ******************************************************************************/
5853 
5854 static void *lru_gen_seq_start(struct seq_file *m, loff_t *pos)
5855 {
5856 	struct mem_cgroup *memcg;
5857 	loff_t nr_to_skip = *pos;
5858 
5859 	m->private = kvmalloc(PATH_MAX, GFP_KERNEL);
5860 	if (!m->private)
5861 		return ERR_PTR(-ENOMEM);
5862 
5863 	memcg = mem_cgroup_iter(NULL, NULL, NULL);
5864 	do {
5865 		int nid;
5866 
5867 		for_each_node_state(nid, N_MEMORY) {
5868 			if (!nr_to_skip--)
5869 				return get_lruvec(memcg, nid);
5870 		}
5871 	} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)));
5872 
5873 	return NULL;
5874 }
5875 
5876 static void lru_gen_seq_stop(struct seq_file *m, void *v)
5877 {
5878 	if (!IS_ERR_OR_NULL(v))
5879 		mem_cgroup_iter_break(NULL, lruvec_memcg(v));
5880 
5881 	kvfree(m->private);
5882 	m->private = NULL;
5883 }
5884 
5885 static void *lru_gen_seq_next(struct seq_file *m, void *v, loff_t *pos)
5886 {
5887 	int nid = lruvec_pgdat(v)->node_id;
5888 	struct mem_cgroup *memcg = lruvec_memcg(v);
5889 
5890 	++*pos;
5891 
5892 	nid = next_memory_node(nid);
5893 	if (nid == MAX_NUMNODES) {
5894 		memcg = mem_cgroup_iter(NULL, memcg, NULL);
5895 		if (!memcg)
5896 			return NULL;
5897 
5898 		nid = first_memory_node;
5899 	}
5900 
5901 	return get_lruvec(memcg, nid);
5902 }
5903 
5904 static void lru_gen_seq_show_full(struct seq_file *m, struct lruvec *lruvec,
5905 				  unsigned long max_seq, unsigned long *min_seq,
5906 				  unsigned long seq)
5907 {
5908 	int i;
5909 	int type, tier;
5910 	int hist = lru_hist_from_seq(seq);
5911 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
5912 
5913 	for (tier = 0; tier < MAX_NR_TIERS; tier++) {
5914 		seq_printf(m, "            %10d", tier);
5915 		for (type = 0; type < ANON_AND_FILE; type++) {
5916 			const char *s = "   ";
5917 			unsigned long n[3] = {};
5918 
5919 			if (seq == max_seq) {
5920 				s = "RT ";
5921 				n[0] = READ_ONCE(lrugen->avg_refaulted[type][tier]);
5922 				n[1] = READ_ONCE(lrugen->avg_total[type][tier]);
5923 			} else if (seq == min_seq[type] || NR_HIST_GENS > 1) {
5924 				s = "rep";
5925 				n[0] = atomic_long_read(&lrugen->refaulted[hist][type][tier]);
5926 				n[1] = atomic_long_read(&lrugen->evicted[hist][type][tier]);
5927 				if (tier)
5928 					n[2] = READ_ONCE(lrugen->protected[hist][type][tier - 1]);
5929 			}
5930 
5931 			for (i = 0; i < 3; i++)
5932 				seq_printf(m, " %10lu%c", n[i], s[i]);
5933 		}
5934 		seq_putc(m, '\n');
5935 	}
5936 
5937 	seq_puts(m, "                      ");
5938 	for (i = 0; i < NR_MM_STATS; i++) {
5939 		const char *s = "      ";
5940 		unsigned long n = 0;
5941 
5942 		if (seq == max_seq && NR_HIST_GENS == 1) {
5943 			s = "LOYNFA";
5944 			n = READ_ONCE(lruvec->mm_state.stats[hist][i]);
5945 		} else if (seq != max_seq && NR_HIST_GENS > 1) {
5946 			s = "loynfa";
5947 			n = READ_ONCE(lruvec->mm_state.stats[hist][i]);
5948 		}
5949 
5950 		seq_printf(m, " %10lu%c", n, s[i]);
5951 	}
5952 	seq_putc(m, '\n');
5953 }
5954 
5955 /* see Documentation/admin-guide/mm/multigen_lru.rst for details */
5956 static int lru_gen_seq_show(struct seq_file *m, void *v)
5957 {
5958 	unsigned long seq;
5959 	bool full = !debugfs_real_fops(m->file)->write;
5960 	struct lruvec *lruvec = v;
5961 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
5962 	int nid = lruvec_pgdat(lruvec)->node_id;
5963 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
5964 	DEFINE_MAX_SEQ(lruvec);
5965 	DEFINE_MIN_SEQ(lruvec);
5966 
5967 	if (nid == first_memory_node) {
5968 		const char *path = memcg ? m->private : "";
5969 
5970 #ifdef CONFIG_MEMCG
5971 		if (memcg)
5972 			cgroup_path(memcg->css.cgroup, m->private, PATH_MAX);
5973 #endif
5974 		seq_printf(m, "memcg %5hu %s\n", mem_cgroup_id(memcg), path);
5975 	}
5976 
5977 	seq_printf(m, " node %5d\n", nid);
5978 
5979 	if (!full)
5980 		seq = min_seq[LRU_GEN_ANON];
5981 	else if (max_seq >= MAX_NR_GENS)
5982 		seq = max_seq - MAX_NR_GENS + 1;
5983 	else
5984 		seq = 0;
5985 
5986 	for (; seq <= max_seq; seq++) {
5987 		int type, zone;
5988 		int gen = lru_gen_from_seq(seq);
5989 		unsigned long birth = READ_ONCE(lruvec->lrugen.timestamps[gen]);
5990 
5991 		seq_printf(m, " %10lu %10u", seq, jiffies_to_msecs(jiffies - birth));
5992 
5993 		for (type = 0; type < ANON_AND_FILE; type++) {
5994 			unsigned long size = 0;
5995 			char mark = full && seq < min_seq[type] ? 'x' : ' ';
5996 
5997 			for (zone = 0; zone < MAX_NR_ZONES; zone++)
5998 				size += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L);
5999 
6000 			seq_printf(m, " %10lu%c", size, mark);
6001 		}
6002 
6003 		seq_putc(m, '\n');
6004 
6005 		if (full)
6006 			lru_gen_seq_show_full(m, lruvec, max_seq, min_seq, seq);
6007 	}
6008 
6009 	return 0;
6010 }
6011 
6012 static const struct seq_operations lru_gen_seq_ops = {
6013 	.start = lru_gen_seq_start,
6014 	.stop = lru_gen_seq_stop,
6015 	.next = lru_gen_seq_next,
6016 	.show = lru_gen_seq_show,
6017 };
6018 
6019 static int run_aging(struct lruvec *lruvec, unsigned long seq, struct scan_control *sc,
6020 		     bool can_swap, bool force_scan)
6021 {
6022 	DEFINE_MAX_SEQ(lruvec);
6023 	DEFINE_MIN_SEQ(lruvec);
6024 
6025 	if (seq < max_seq)
6026 		return 0;
6027 
6028 	if (seq > max_seq)
6029 		return -EINVAL;
6030 
6031 	if (!force_scan && min_seq[!can_swap] + MAX_NR_GENS - 1 <= max_seq)
6032 		return -ERANGE;
6033 
6034 	try_to_inc_max_seq(lruvec, max_seq, sc, can_swap, force_scan);
6035 
6036 	return 0;
6037 }
6038 
6039 static int run_eviction(struct lruvec *lruvec, unsigned long seq, struct scan_control *sc,
6040 			int swappiness, unsigned long nr_to_reclaim)
6041 {
6042 	DEFINE_MAX_SEQ(lruvec);
6043 
6044 	if (seq + MIN_NR_GENS > max_seq)
6045 		return -EINVAL;
6046 
6047 	sc->nr_reclaimed = 0;
6048 
6049 	while (!signal_pending(current)) {
6050 		DEFINE_MIN_SEQ(lruvec);
6051 
6052 		if (seq < min_seq[!swappiness])
6053 			return 0;
6054 
6055 		if (sc->nr_reclaimed >= nr_to_reclaim)
6056 			return 0;
6057 
6058 		if (!evict_folios(lruvec, sc, swappiness))
6059 			return 0;
6060 
6061 		cond_resched();
6062 	}
6063 
6064 	return -EINTR;
6065 }
6066 
6067 static int run_cmd(char cmd, int memcg_id, int nid, unsigned long seq,
6068 		   struct scan_control *sc, int swappiness, unsigned long opt)
6069 {
6070 	struct lruvec *lruvec;
6071 	int err = -EINVAL;
6072 	struct mem_cgroup *memcg = NULL;
6073 
6074 	if (nid < 0 || nid >= MAX_NUMNODES || !node_state(nid, N_MEMORY))
6075 		return -EINVAL;
6076 
6077 	if (!mem_cgroup_disabled()) {
6078 		rcu_read_lock();
6079 
6080 		memcg = mem_cgroup_from_id(memcg_id);
6081 		if (!mem_cgroup_tryget(memcg))
6082 			memcg = NULL;
6083 
6084 		rcu_read_unlock();
6085 
6086 		if (!memcg)
6087 			return -EINVAL;
6088 	}
6089 
6090 	if (memcg_id != mem_cgroup_id(memcg))
6091 		goto done;
6092 
6093 	lruvec = get_lruvec(memcg, nid);
6094 
6095 	if (swappiness < 0)
6096 		swappiness = get_swappiness(lruvec, sc);
6097 	else if (swappiness > 200)
6098 		goto done;
6099 
6100 	switch (cmd) {
6101 	case '+':
6102 		err = run_aging(lruvec, seq, sc, swappiness, opt);
6103 		break;
6104 	case '-':
6105 		err = run_eviction(lruvec, seq, sc, swappiness, opt);
6106 		break;
6107 	}
6108 done:
6109 	mem_cgroup_put(memcg);
6110 
6111 	return err;
6112 }
6113 
6114 /* see Documentation/admin-guide/mm/multigen_lru.rst for details */
6115 static ssize_t lru_gen_seq_write(struct file *file, const char __user *src,
6116 				 size_t len, loff_t *pos)
6117 {
6118 	void *buf;
6119 	char *cur, *next;
6120 	unsigned int flags;
6121 	struct blk_plug plug;
6122 	int err = -EINVAL;
6123 	struct scan_control sc = {
6124 		.may_writepage = true,
6125 		.may_unmap = true,
6126 		.may_swap = true,
6127 		.reclaim_idx = MAX_NR_ZONES - 1,
6128 		.gfp_mask = GFP_KERNEL,
6129 	};
6130 
6131 	buf = kvmalloc(len + 1, GFP_KERNEL);
6132 	if (!buf)
6133 		return -ENOMEM;
6134 
6135 	if (copy_from_user(buf, src, len)) {
6136 		kvfree(buf);
6137 		return -EFAULT;
6138 	}
6139 
6140 	set_task_reclaim_state(current, &sc.reclaim_state);
6141 	flags = memalloc_noreclaim_save();
6142 	blk_start_plug(&plug);
6143 	if (!set_mm_walk(NULL, true)) {
6144 		err = -ENOMEM;
6145 		goto done;
6146 	}
6147 
6148 	next = buf;
6149 	next[len] = '\0';
6150 
6151 	while ((cur = strsep(&next, ",;\n"))) {
6152 		int n;
6153 		int end;
6154 		char cmd;
6155 		unsigned int memcg_id;
6156 		unsigned int nid;
6157 		unsigned long seq;
6158 		unsigned int swappiness = -1;
6159 		unsigned long opt = -1;
6160 
6161 		cur = skip_spaces(cur);
6162 		if (!*cur)
6163 			continue;
6164 
6165 		n = sscanf(cur, "%c %u %u %lu %n %u %n %lu %n", &cmd, &memcg_id, &nid,
6166 			   &seq, &end, &swappiness, &end, &opt, &end);
6167 		if (n < 4 || cur[end]) {
6168 			err = -EINVAL;
6169 			break;
6170 		}
6171 
6172 		err = run_cmd(cmd, memcg_id, nid, seq, &sc, swappiness, opt);
6173 		if (err)
6174 			break;
6175 	}
6176 done:
6177 	clear_mm_walk();
6178 	blk_finish_plug(&plug);
6179 	memalloc_noreclaim_restore(flags);
6180 	set_task_reclaim_state(current, NULL);
6181 
6182 	kvfree(buf);
6183 
6184 	return err ? : len;
6185 }
6186 
6187 static int lru_gen_seq_open(struct inode *inode, struct file *file)
6188 {
6189 	return seq_open(file, &lru_gen_seq_ops);
6190 }
6191 
6192 static const struct file_operations lru_gen_rw_fops = {
6193 	.open = lru_gen_seq_open,
6194 	.read = seq_read,
6195 	.write = lru_gen_seq_write,
6196 	.llseek = seq_lseek,
6197 	.release = seq_release,
6198 };
6199 
6200 static const struct file_operations lru_gen_ro_fops = {
6201 	.open = lru_gen_seq_open,
6202 	.read = seq_read,
6203 	.llseek = seq_lseek,
6204 	.release = seq_release,
6205 };
6206 
6207 /******************************************************************************
6208  *                          initialization
6209  ******************************************************************************/
6210 
6211 void lru_gen_init_lruvec(struct lruvec *lruvec)
6212 {
6213 	int i;
6214 	int gen, type, zone;
6215 	struct lru_gen_folio *lrugen = &lruvec->lrugen;
6216 
6217 	lrugen->max_seq = MIN_NR_GENS + 1;
6218 	lrugen->enabled = lru_gen_enabled();
6219 
6220 	for (i = 0; i <= MIN_NR_GENS + 1; i++)
6221 		lrugen->timestamps[i] = jiffies;
6222 
6223 	for_each_gen_type_zone(gen, type, zone)
6224 		INIT_LIST_HEAD(&lrugen->folios[gen][type][zone]);
6225 
6226 	lruvec->mm_state.seq = MIN_NR_GENS;
6227 }
6228 
6229 #ifdef CONFIG_MEMCG
6230 
6231 void lru_gen_init_pgdat(struct pglist_data *pgdat)
6232 {
6233 	int i, j;
6234 
6235 	spin_lock_init(&pgdat->memcg_lru.lock);
6236 
6237 	for (i = 0; i < MEMCG_NR_GENS; i++) {
6238 		for (j = 0; j < MEMCG_NR_BINS; j++)
6239 			INIT_HLIST_NULLS_HEAD(&pgdat->memcg_lru.fifo[i][j], i);
6240 	}
6241 }
6242 
6243 void lru_gen_init_memcg(struct mem_cgroup *memcg)
6244 {
6245 	INIT_LIST_HEAD(&memcg->mm_list.fifo);
6246 	spin_lock_init(&memcg->mm_list.lock);
6247 }
6248 
6249 void lru_gen_exit_memcg(struct mem_cgroup *memcg)
6250 {
6251 	int i;
6252 	int nid;
6253 
6254 	VM_WARN_ON_ONCE(!list_empty(&memcg->mm_list.fifo));
6255 
6256 	for_each_node(nid) {
6257 		struct lruvec *lruvec = get_lruvec(memcg, nid);
6258 
6259 		VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0,
6260 					   sizeof(lruvec->lrugen.nr_pages)));
6261 
6262 		lruvec->lrugen.list.next = LIST_POISON1;
6263 
6264 		for (i = 0; i < NR_BLOOM_FILTERS; i++) {
6265 			bitmap_free(lruvec->mm_state.filters[i]);
6266 			lruvec->mm_state.filters[i] = NULL;
6267 		}
6268 	}
6269 }
6270 
6271 #endif /* CONFIG_MEMCG */
6272 
6273 static int __init init_lru_gen(void)
6274 {
6275 	BUILD_BUG_ON(MIN_NR_GENS + 1 >= MAX_NR_GENS);
6276 	BUILD_BUG_ON(BIT(LRU_GEN_WIDTH) <= MAX_NR_GENS);
6277 
6278 	if (sysfs_create_group(mm_kobj, &lru_gen_attr_group))
6279 		pr_err("lru_gen: failed to create sysfs group\n");
6280 
6281 	debugfs_create_file("lru_gen", 0644, NULL, NULL, &lru_gen_rw_fops);
6282 	debugfs_create_file("lru_gen_full", 0444, NULL, NULL, &lru_gen_ro_fops);
6283 
6284 	return 0;
6285 };
6286 late_initcall(init_lru_gen);
6287 
6288 #else /* !CONFIG_LRU_GEN */
6289 
6290 static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc)
6291 {
6292 }
6293 
6294 static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
6295 {
6296 }
6297 
6298 static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *sc)
6299 {
6300 }
6301 
6302 #endif /* CONFIG_LRU_GEN */
6303 
6304 static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
6305 {
6306 	unsigned long nr[NR_LRU_LISTS];
6307 	unsigned long targets[NR_LRU_LISTS];
6308 	unsigned long nr_to_scan;
6309 	enum lru_list lru;
6310 	unsigned long nr_reclaimed = 0;
6311 	unsigned long nr_to_reclaim = sc->nr_to_reclaim;
6312 	bool proportional_reclaim;
6313 	struct blk_plug plug;
6314 
6315 	if (lru_gen_enabled() && !root_reclaim(sc)) {
6316 		lru_gen_shrink_lruvec(lruvec, sc);
6317 		return;
6318 	}
6319 
6320 	get_scan_count(lruvec, sc, nr);
6321 
6322 	/* Record the original scan target for proportional adjustments later */
6323 	memcpy(targets, nr, sizeof(nr));
6324 
6325 	/*
6326 	 * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal
6327 	 * event that can occur when there is little memory pressure e.g.
6328 	 * multiple streaming readers/writers. Hence, we do not abort scanning
6329 	 * when the requested number of pages are reclaimed when scanning at
6330 	 * DEF_PRIORITY on the assumption that the fact we are direct
6331 	 * reclaiming implies that kswapd is not keeping up and it is best to
6332 	 * do a batch of work at once. For memcg reclaim one check is made to
6333 	 * abort proportional reclaim if either the file or anon lru has already
6334 	 * dropped to zero at the first pass.
6335 	 */
6336 	proportional_reclaim = (!cgroup_reclaim(sc) && !current_is_kswapd() &&
6337 				sc->priority == DEF_PRIORITY);
6338 
6339 	blk_start_plug(&plug);
6340 	while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
6341 					nr[LRU_INACTIVE_FILE]) {
6342 		unsigned long nr_anon, nr_file, percentage;
6343 		unsigned long nr_scanned;
6344 
6345 		for_each_evictable_lru(lru) {
6346 			if (nr[lru]) {
6347 				nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX);
6348 				nr[lru] -= nr_to_scan;
6349 
6350 				nr_reclaimed += shrink_list(lru, nr_to_scan,
6351 							    lruvec, sc);
6352 			}
6353 		}
6354 
6355 		cond_resched();
6356 
6357 		if (nr_reclaimed < nr_to_reclaim || proportional_reclaim)
6358 			continue;
6359 
6360 		/*
6361 		 * For kswapd and memcg, reclaim at least the number of pages
6362 		 * requested. Ensure that the anon and file LRUs are scanned
6363 		 * proportionally what was requested by get_scan_count(). We
6364 		 * stop reclaiming one LRU and reduce the amount scanning
6365 		 * proportional to the original scan target.
6366 		 */
6367 		nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE];
6368 		nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON];
6369 
6370 		/*
6371 		 * It's just vindictive to attack the larger once the smaller
6372 		 * has gone to zero.  And given the way we stop scanning the
6373 		 * smaller below, this makes sure that we only make one nudge
6374 		 * towards proportionality once we've got nr_to_reclaim.
6375 		 */
6376 		if (!nr_file || !nr_anon)
6377 			break;
6378 
6379 		if (nr_file > nr_anon) {
6380 			unsigned long scan_target = targets[LRU_INACTIVE_ANON] +
6381 						targets[LRU_ACTIVE_ANON] + 1;
6382 			lru = LRU_BASE;
6383 			percentage = nr_anon * 100 / scan_target;
6384 		} else {
6385 			unsigned long scan_target = targets[LRU_INACTIVE_FILE] +
6386 						targets[LRU_ACTIVE_FILE] + 1;
6387 			lru = LRU_FILE;
6388 			percentage = nr_file * 100 / scan_target;
6389 		}
6390 
6391 		/* Stop scanning the smaller of the LRU */
6392 		nr[lru] = 0;
6393 		nr[lru + LRU_ACTIVE] = 0;
6394 
6395 		/*
6396 		 * Recalculate the other LRU scan count based on its original
6397 		 * scan target and the percentage scanning already complete
6398 		 */
6399 		lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE;
6400 		nr_scanned = targets[lru] - nr[lru];
6401 		nr[lru] = targets[lru] * (100 - percentage) / 100;
6402 		nr[lru] -= min(nr[lru], nr_scanned);
6403 
6404 		lru += LRU_ACTIVE;
6405 		nr_scanned = targets[lru] - nr[lru];
6406 		nr[lru] = targets[lru] * (100 - percentage) / 100;
6407 		nr[lru] -= min(nr[lru], nr_scanned);
6408 	}
6409 	blk_finish_plug(&plug);
6410 	sc->nr_reclaimed += nr_reclaimed;
6411 
6412 	/*
6413 	 * Even if we did not try to evict anon pages at all, we want to
6414 	 * rebalance the anon lru active/inactive ratio.
6415 	 */
6416 	if (can_age_anon_pages(lruvec_pgdat(lruvec), sc) &&
6417 	    inactive_is_low(lruvec, LRU_INACTIVE_ANON))
6418 		shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
6419 				   sc, LRU_ACTIVE_ANON);
6420 }
6421 
6422 /* Use reclaim/compaction for costly allocs or under memory pressure */
6423 static bool in_reclaim_compaction(struct scan_control *sc)
6424 {
6425 	if (gfp_compaction_allowed(sc->gfp_mask) && sc->order &&
6426 			(sc->order > PAGE_ALLOC_COSTLY_ORDER ||
6427 			 sc->priority < DEF_PRIORITY - 2))
6428 		return true;
6429 
6430 	return false;
6431 }
6432 
6433 /*
6434  * Reclaim/compaction is used for high-order allocation requests. It reclaims
6435  * order-0 pages before compacting the zone. should_continue_reclaim() returns
6436  * true if more pages should be reclaimed such that when the page allocator
6437  * calls try_to_compact_pages() that it will have enough free pages to succeed.
6438  * It will give up earlier than that if there is difficulty reclaiming pages.
6439  */
6440 static inline bool should_continue_reclaim(struct pglist_data *pgdat,
6441 					unsigned long nr_reclaimed,
6442 					struct scan_control *sc)
6443 {
6444 	unsigned long pages_for_compaction;
6445 	unsigned long inactive_lru_pages;
6446 	int z;
6447 
6448 	/* If not in reclaim/compaction mode, stop */
6449 	if (!in_reclaim_compaction(sc))
6450 		return false;
6451 
6452 	/*
6453 	 * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX
6454 	 * number of pages that were scanned. This will return to the caller
6455 	 * with the risk reclaim/compaction and the resulting allocation attempt
6456 	 * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL
6457 	 * allocations through requiring that the full LRU list has been scanned
6458 	 * first, by assuming that zero delta of sc->nr_scanned means full LRU
6459 	 * scan, but that approximation was wrong, and there were corner cases
6460 	 * where always a non-zero amount of pages were scanned.
6461 	 */
6462 	if (!nr_reclaimed)
6463 		return false;
6464 
6465 	/* If compaction would go ahead or the allocation would succeed, stop */
6466 	for (z = 0; z <= sc->reclaim_idx; z++) {
6467 		struct zone *zone = &pgdat->node_zones[z];
6468 		if (!managed_zone(zone))
6469 			continue;
6470 
6471 		/* Allocation can already succeed, nothing to do */
6472 		if (zone_watermark_ok(zone, sc->order, min_wmark_pages(zone),
6473 				      sc->reclaim_idx, 0))
6474 			return false;
6475 
6476 		if (compaction_suitable(zone, sc->order, sc->reclaim_idx))
6477 			return false;
6478 	}
6479 
6480 	/*
6481 	 * If we have not reclaimed enough pages for compaction and the
6482 	 * inactive lists are large enough, continue reclaiming
6483 	 */
6484 	pages_for_compaction = compact_gap(sc->order);
6485 	inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE);
6486 	if (can_reclaim_anon_pages(NULL, pgdat->node_id, sc))
6487 		inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON);
6488 
6489 	return inactive_lru_pages > pages_for_compaction;
6490 }
6491 
6492 static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc)
6493 {
6494 	struct mem_cgroup *target_memcg = sc->target_mem_cgroup;
6495 	struct mem_cgroup *memcg;
6496 
6497 	memcg = mem_cgroup_iter(target_memcg, NULL, NULL);
6498 	do {
6499 		struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
6500 		unsigned long reclaimed;
6501 		unsigned long scanned;
6502 
6503 		/*
6504 		 * This loop can become CPU-bound when target memcgs
6505 		 * aren't eligible for reclaim - either because they
6506 		 * don't have any reclaimable pages, or because their
6507 		 * memory is explicitly protected. Avoid soft lockups.
6508 		 */
6509 		cond_resched();
6510 
6511 		mem_cgroup_calculate_protection(target_memcg, memcg);
6512 
6513 		if (mem_cgroup_below_min(target_memcg, memcg)) {
6514 			/*
6515 			 * Hard protection.
6516 			 * If there is no reclaimable memory, OOM.
6517 			 */
6518 			continue;
6519 		} else if (mem_cgroup_below_low(target_memcg, memcg)) {
6520 			/*
6521 			 * Soft protection.
6522 			 * Respect the protection only as long as
6523 			 * there is an unprotected supply
6524 			 * of reclaimable memory from other cgroups.
6525 			 */
6526 			if (!sc->memcg_low_reclaim) {
6527 				sc->memcg_low_skipped = 1;
6528 				continue;
6529 			}
6530 			memcg_memory_event(memcg, MEMCG_LOW);
6531 		}
6532 
6533 		reclaimed = sc->nr_reclaimed;
6534 		scanned = sc->nr_scanned;
6535 
6536 		shrink_lruvec(lruvec, sc);
6537 
6538 		shrink_slab(sc->gfp_mask, pgdat->node_id, memcg,
6539 			    sc->priority);
6540 
6541 		/* Record the group's reclaim efficiency */
6542 		if (!sc->proactive)
6543 			vmpressure(sc->gfp_mask, memcg, false,
6544 				   sc->nr_scanned - scanned,
6545 				   sc->nr_reclaimed - reclaimed);
6546 
6547 	} while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL)));
6548 }
6549 
6550 static void shrink_node(pg_data_t *pgdat, struct scan_control *sc)
6551 {
6552 	unsigned long nr_reclaimed, nr_scanned, nr_node_reclaimed;
6553 	struct lruvec *target_lruvec;
6554 	bool reclaimable = false;
6555 
6556 	if (lru_gen_enabled() && root_reclaim(sc)) {
6557 		lru_gen_shrink_node(pgdat, sc);
6558 		return;
6559 	}
6560 
6561 	target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat);
6562 
6563 again:
6564 	memset(&sc->nr, 0, sizeof(sc->nr));
6565 
6566 	nr_reclaimed = sc->nr_reclaimed;
6567 	nr_scanned = sc->nr_scanned;
6568 
6569 	prepare_scan_count(pgdat, sc);
6570 
6571 	shrink_node_memcgs(pgdat, sc);
6572 
6573 	flush_reclaim_state(sc);
6574 
6575 	nr_node_reclaimed = sc->nr_reclaimed - nr_reclaimed;
6576 
6577 	/* Record the subtree's reclaim efficiency */
6578 	if (!sc->proactive)
6579 		vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true,
6580 			   sc->nr_scanned - nr_scanned, nr_node_reclaimed);
6581 
6582 	if (nr_node_reclaimed)
6583 		reclaimable = true;
6584 
6585 	if (current_is_kswapd()) {
6586 		/*
6587 		 * If reclaim is isolating dirty pages under writeback,
6588 		 * it implies that the long-lived page allocation rate
6589 		 * is exceeding the page laundering rate. Either the
6590 		 * global limits are not being effective at throttling
6591 		 * processes due to the page distribution throughout
6592 		 * zones or there is heavy usage of a slow backing
6593 		 * device. The only option is to throttle from reclaim
6594 		 * context which is not ideal as there is no guarantee
6595 		 * the dirtying process is throttled in the same way
6596 		 * balance_dirty_pages() manages.
6597 		 *
6598 		 * Once a node is flagged PGDAT_WRITEBACK, kswapd will
6599 		 * count the number of pages under pages flagged for
6600 		 * immediate reclaim and stall if any are encountered
6601 		 * in the nr_immediate check below.
6602 		 */
6603 		if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken)
6604 			set_bit(PGDAT_WRITEBACK, &pgdat->flags);
6605 
6606 		/* Allow kswapd to start writing pages during reclaim.*/
6607 		if (sc->nr.unqueued_dirty == sc->nr.file_taken)
6608 			set_bit(PGDAT_DIRTY, &pgdat->flags);
6609 
6610 		/*
6611 		 * If kswapd scans pages marked for immediate
6612 		 * reclaim and under writeback (nr_immediate), it
6613 		 * implies that pages are cycling through the LRU
6614 		 * faster than they are written so forcibly stall
6615 		 * until some pages complete writeback.
6616 		 */
6617 		if (sc->nr.immediate)
6618 			reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK);
6619 	}
6620 
6621 	/*
6622 	 * Tag a node/memcg as congested if all the dirty pages were marked
6623 	 * for writeback and immediate reclaim (counted in nr.congested).
6624 	 *
6625 	 * Legacy memcg will stall in page writeback so avoid forcibly
6626 	 * stalling in reclaim_throttle().
6627 	 */
6628 	if (sc->nr.dirty && sc->nr.dirty == sc->nr.congested) {
6629 		if (cgroup_reclaim(sc) && writeback_throttling_sane(sc))
6630 			set_bit(LRUVEC_CGROUP_CONGESTED, &target_lruvec->flags);
6631 
6632 		if (current_is_kswapd())
6633 			set_bit(LRUVEC_NODE_CONGESTED, &target_lruvec->flags);
6634 	}
6635 
6636 	/*
6637 	 * Stall direct reclaim for IO completions if the lruvec is
6638 	 * node is congested. Allow kswapd to continue until it
6639 	 * starts encountering unqueued dirty pages or cycling through
6640 	 * the LRU too quickly.
6641 	 */
6642 	if (!current_is_kswapd() && current_may_throttle() &&
6643 	    !sc->hibernation_mode &&
6644 	    (test_bit(LRUVEC_CGROUP_CONGESTED, &target_lruvec->flags) ||
6645 	     test_bit(LRUVEC_NODE_CONGESTED, &target_lruvec->flags)))
6646 		reclaim_throttle(pgdat, VMSCAN_THROTTLE_CONGESTED);
6647 
6648 	if (should_continue_reclaim(pgdat, nr_node_reclaimed, sc))
6649 		goto again;
6650 
6651 	/*
6652 	 * Kswapd gives up on balancing particular nodes after too
6653 	 * many failures to reclaim anything from them and goes to
6654 	 * sleep. On reclaim progress, reset the failure counter. A
6655 	 * successful direct reclaim run will revive a dormant kswapd.
6656 	 */
6657 	if (reclaimable)
6658 		pgdat->kswapd_failures = 0;
6659 }
6660 
6661 /*
6662  * Returns true if compaction should go ahead for a costly-order request, or
6663  * the allocation would already succeed without compaction. Return false if we
6664  * should reclaim first.
6665  */
6666 static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
6667 {
6668 	unsigned long watermark;
6669 
6670 	if (!gfp_compaction_allowed(sc->gfp_mask))
6671 		return false;
6672 
6673 	/* Allocation can already succeed, nothing to do */
6674 	if (zone_watermark_ok(zone, sc->order, min_wmark_pages(zone),
6675 			      sc->reclaim_idx, 0))
6676 		return true;
6677 
6678 	/* Compaction cannot yet proceed. Do reclaim. */
6679 	if (!compaction_suitable(zone, sc->order, sc->reclaim_idx))
6680 		return false;
6681 
6682 	/*
6683 	 * Compaction is already possible, but it takes time to run and there
6684 	 * are potentially other callers using the pages just freed. So proceed
6685 	 * with reclaim to make a buffer of free pages available to give
6686 	 * compaction a reasonable chance of completing and allocating the page.
6687 	 * Note that we won't actually reclaim the whole buffer in one attempt
6688 	 * as the target watermark in should_continue_reclaim() is lower. But if
6689 	 * we are already above the high+gap watermark, don't reclaim at all.
6690 	 */
6691 	watermark = high_wmark_pages(zone) + compact_gap(sc->order);
6692 
6693 	return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx);
6694 }
6695 
6696 static void consider_reclaim_throttle(pg_data_t *pgdat, struct scan_control *sc)
6697 {
6698 	/*
6699 	 * If reclaim is making progress greater than 12% efficiency then
6700 	 * wake all the NOPROGRESS throttled tasks.
6701 	 */
6702 	if (sc->nr_reclaimed > (sc->nr_scanned >> 3)) {
6703 		wait_queue_head_t *wqh;
6704 
6705 		wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_NOPROGRESS];
6706 		if (waitqueue_active(wqh))
6707 			wake_up(wqh);
6708 
6709 		return;
6710 	}
6711 
6712 	/*
6713 	 * Do not throttle kswapd or cgroup reclaim on NOPROGRESS as it will
6714 	 * throttle on VMSCAN_THROTTLE_WRITEBACK if there are too many pages
6715 	 * under writeback and marked for immediate reclaim at the tail of the
6716 	 * LRU.
6717 	 */
6718 	if (current_is_kswapd() || cgroup_reclaim(sc))
6719 		return;
6720 
6721 	/* Throttle if making no progress at high prioities. */
6722 	if (sc->priority == 1 && !sc->nr_reclaimed)
6723 		reclaim_throttle(pgdat, VMSCAN_THROTTLE_NOPROGRESS);
6724 }
6725 
6726 /*
6727  * This is the direct reclaim path, for page-allocating processes.  We only
6728  * try to reclaim pages from zones which will satisfy the caller's allocation
6729  * request.
6730  *
6731  * If a zone is deemed to be full of pinned pages then just give it a light
6732  * scan then give up on it.
6733  */
6734 static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
6735 {
6736 	struct zoneref *z;
6737 	struct zone *zone;
6738 	unsigned long nr_soft_reclaimed;
6739 	unsigned long nr_soft_scanned;
6740 	gfp_t orig_mask;
6741 	pg_data_t *last_pgdat = NULL;
6742 	pg_data_t *first_pgdat = NULL;
6743 
6744 	/*
6745 	 * If the number of buffer_heads in the machine exceeds the maximum
6746 	 * allowed level, force direct reclaim to scan the highmem zone as
6747 	 * highmem pages could be pinning lowmem pages storing buffer_heads
6748 	 */
6749 	orig_mask = sc->gfp_mask;
6750 	if (buffer_heads_over_limit) {
6751 		sc->gfp_mask |= __GFP_HIGHMEM;
6752 		sc->reclaim_idx = gfp_zone(sc->gfp_mask);
6753 	}
6754 
6755 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
6756 					sc->reclaim_idx, sc->nodemask) {
6757 		/*
6758 		 * Take care memory controller reclaiming has small influence
6759 		 * to global LRU.
6760 		 */
6761 		if (!cgroup_reclaim(sc)) {
6762 			if (!cpuset_zone_allowed(zone,
6763 						 GFP_KERNEL | __GFP_HARDWALL))
6764 				continue;
6765 
6766 			/*
6767 			 * If we already have plenty of memory free for
6768 			 * compaction in this zone, don't free any more.
6769 			 * Even though compaction is invoked for any
6770 			 * non-zero order, only frequent costly order
6771 			 * reclamation is disruptive enough to become a
6772 			 * noticeable problem, like transparent huge
6773 			 * page allocations.
6774 			 */
6775 			if (IS_ENABLED(CONFIG_COMPACTION) &&
6776 			    sc->order > PAGE_ALLOC_COSTLY_ORDER &&
6777 			    compaction_ready(zone, sc)) {
6778 				sc->compaction_ready = true;
6779 				continue;
6780 			}
6781 
6782 			/*
6783 			 * Shrink each node in the zonelist once. If the
6784 			 * zonelist is ordered by zone (not the default) then a
6785 			 * node may be shrunk multiple times but in that case
6786 			 * the user prefers lower zones being preserved.
6787 			 */
6788 			if (zone->zone_pgdat == last_pgdat)
6789 				continue;
6790 
6791 			/*
6792 			 * This steals pages from memory cgroups over softlimit
6793 			 * and returns the number of reclaimed pages and
6794 			 * scanned pages. This works for global memory pressure
6795 			 * and balancing, not for a memcg's limit.
6796 			 */
6797 			nr_soft_scanned = 0;
6798 			nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat,
6799 						sc->order, sc->gfp_mask,
6800 						&nr_soft_scanned);
6801 			sc->nr_reclaimed += nr_soft_reclaimed;
6802 			sc->nr_scanned += nr_soft_scanned;
6803 			/* need some check for avoid more shrink_zone() */
6804 		}
6805 
6806 		if (!first_pgdat)
6807 			first_pgdat = zone->zone_pgdat;
6808 
6809 		/* See comment about same check for global reclaim above */
6810 		if (zone->zone_pgdat == last_pgdat)
6811 			continue;
6812 		last_pgdat = zone->zone_pgdat;
6813 		shrink_node(zone->zone_pgdat, sc);
6814 	}
6815 
6816 	if (first_pgdat)
6817 		consider_reclaim_throttle(first_pgdat, sc);
6818 
6819 	/*
6820 	 * Restore to original mask to avoid the impact on the caller if we
6821 	 * promoted it to __GFP_HIGHMEM.
6822 	 */
6823 	sc->gfp_mask = orig_mask;
6824 }
6825 
6826 static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat)
6827 {
6828 	struct lruvec *target_lruvec;
6829 	unsigned long refaults;
6830 
6831 	if (lru_gen_enabled())
6832 		return;
6833 
6834 	target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat);
6835 	refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON);
6836 	target_lruvec->refaults[WORKINGSET_ANON] = refaults;
6837 	refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE);
6838 	target_lruvec->refaults[WORKINGSET_FILE] = refaults;
6839 }
6840 
6841 /*
6842  * This is the main entry point to direct page reclaim.
6843  *
6844  * If a full scan of the inactive list fails to free enough memory then we
6845  * are "out of memory" and something needs to be killed.
6846  *
6847  * If the caller is !__GFP_FS then the probability of a failure is reasonably
6848  * high - the zone may be full of dirty or under-writeback pages, which this
6849  * caller can't do much about.  We kick the writeback threads and take explicit
6850  * naps in the hope that some of these pages can be written.  But if the
6851  * allocating task holds filesystem locks which prevent writeout this might not
6852  * work, and the allocation attempt will fail.
6853  *
6854  * returns:	0, if no pages reclaimed
6855  * 		else, the number of pages reclaimed
6856  */
6857 static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
6858 					  struct scan_control *sc)
6859 {
6860 	int initial_priority = sc->priority;
6861 	pg_data_t *last_pgdat;
6862 	struct zoneref *z;
6863 	struct zone *zone;
6864 retry:
6865 	delayacct_freepages_start();
6866 
6867 	if (!cgroup_reclaim(sc))
6868 		__count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1);
6869 
6870 	do {
6871 		if (!sc->proactive)
6872 			vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup,
6873 					sc->priority);
6874 		sc->nr_scanned = 0;
6875 		shrink_zones(zonelist, sc);
6876 
6877 		if (sc->nr_reclaimed >= sc->nr_to_reclaim)
6878 			break;
6879 
6880 		if (sc->compaction_ready)
6881 			break;
6882 
6883 		/*
6884 		 * If we're getting trouble reclaiming, start doing
6885 		 * writepage even in laptop mode.
6886 		 */
6887 		if (sc->priority < DEF_PRIORITY - 2)
6888 			sc->may_writepage = 1;
6889 	} while (--sc->priority >= 0);
6890 
6891 	last_pgdat = NULL;
6892 	for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx,
6893 					sc->nodemask) {
6894 		if (zone->zone_pgdat == last_pgdat)
6895 			continue;
6896 		last_pgdat = zone->zone_pgdat;
6897 
6898 		snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat);
6899 
6900 		if (cgroup_reclaim(sc)) {
6901 			struct lruvec *lruvec;
6902 
6903 			lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup,
6904 						   zone->zone_pgdat);
6905 			clear_bit(LRUVEC_CGROUP_CONGESTED, &lruvec->flags);
6906 		}
6907 	}
6908 
6909 	delayacct_freepages_end();
6910 
6911 	if (sc->nr_reclaimed)
6912 		return sc->nr_reclaimed;
6913 
6914 	/* Aborted reclaim to try compaction? don't OOM, then */
6915 	if (sc->compaction_ready)
6916 		return 1;
6917 
6918 	/*
6919 	 * We make inactive:active ratio decisions based on the node's
6920 	 * composition of memory, but a restrictive reclaim_idx or a
6921 	 * memory.low cgroup setting can exempt large amounts of
6922 	 * memory from reclaim. Neither of which are very common, so
6923 	 * instead of doing costly eligibility calculations of the
6924 	 * entire cgroup subtree up front, we assume the estimates are
6925 	 * good, and retry with forcible deactivation if that fails.
6926 	 */
6927 	if (sc->skipped_deactivate) {
6928 		sc->priority = initial_priority;
6929 		sc->force_deactivate = 1;
6930 		sc->skipped_deactivate = 0;
6931 		goto retry;
6932 	}
6933 
6934 	/* Untapped cgroup reserves?  Don't OOM, retry. */
6935 	if (sc->memcg_low_skipped) {
6936 		sc->priority = initial_priority;
6937 		sc->force_deactivate = 0;
6938 		sc->memcg_low_reclaim = 1;
6939 		sc->memcg_low_skipped = 0;
6940 		goto retry;
6941 	}
6942 
6943 	return 0;
6944 }
6945 
6946 static bool allow_direct_reclaim(pg_data_t *pgdat)
6947 {
6948 	struct zone *zone;
6949 	unsigned long pfmemalloc_reserve = 0;
6950 	unsigned long free_pages = 0;
6951 	int i;
6952 	bool wmark_ok;
6953 
6954 	if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
6955 		return true;
6956 
6957 	for (i = 0; i <= ZONE_NORMAL; i++) {
6958 		zone = &pgdat->node_zones[i];
6959 		if (!managed_zone(zone))
6960 			continue;
6961 
6962 		if (!zone_reclaimable_pages(zone))
6963 			continue;
6964 
6965 		pfmemalloc_reserve += min_wmark_pages(zone);
6966 		free_pages += zone_page_state_snapshot(zone, NR_FREE_PAGES);
6967 	}
6968 
6969 	/* If there are no reserves (unexpected config) then do not throttle */
6970 	if (!pfmemalloc_reserve)
6971 		return true;
6972 
6973 	wmark_ok = free_pages > pfmemalloc_reserve / 2;
6974 
6975 	/* kswapd must be awake if processes are being throttled */
6976 	if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
6977 		if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL)
6978 			WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL);
6979 
6980 		wake_up_interruptible(&pgdat->kswapd_wait);
6981 	}
6982 
6983 	return wmark_ok;
6984 }
6985 
6986 /*
6987  * Throttle direct reclaimers if backing storage is backed by the network
6988  * and the PFMEMALLOC reserve for the preferred node is getting dangerously
6989  * depleted. kswapd will continue to make progress and wake the processes
6990  * when the low watermark is reached.
6991  *
6992  * Returns true if a fatal signal was delivered during throttling. If this
6993  * happens, the page allocator should not consider triggering the OOM killer.
6994  */
6995 static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
6996 					nodemask_t *nodemask)
6997 {
6998 	struct zoneref *z;
6999 	struct zone *zone;
7000 	pg_data_t *pgdat = NULL;
7001 
7002 	/*
7003 	 * Kernel threads should not be throttled as they may be indirectly
7004 	 * responsible for cleaning pages necessary for reclaim to make forward
7005 	 * progress. kjournald for example may enter direct reclaim while
7006 	 * committing a transaction where throttling it could forcing other
7007 	 * processes to block on log_wait_commit().
7008 	 */
7009 	if (current->flags & PF_KTHREAD)
7010 		goto out;
7011 
7012 	/*
7013 	 * If a fatal signal is pending, this process should not throttle.
7014 	 * It should return quickly so it can exit and free its memory
7015 	 */
7016 	if (fatal_signal_pending(current))
7017 		goto out;
7018 
7019 	/*
7020 	 * Check if the pfmemalloc reserves are ok by finding the first node
7021 	 * with a usable ZONE_NORMAL or lower zone. The expectation is that
7022 	 * GFP_KERNEL will be required for allocating network buffers when
7023 	 * swapping over the network so ZONE_HIGHMEM is unusable.
7024 	 *
7025 	 * Throttling is based on the first usable node and throttled processes
7026 	 * wait on a queue until kswapd makes progress and wakes them. There
7027 	 * is an affinity then between processes waking up and where reclaim
7028 	 * progress has been made assuming the process wakes on the same node.
7029 	 * More importantly, processes running on remote nodes will not compete
7030 	 * for remote pfmemalloc reserves and processes on different nodes
7031 	 * should make reasonable progress.
7032 	 */
7033 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
7034 					gfp_zone(gfp_mask), nodemask) {
7035 		if (zone_idx(zone) > ZONE_NORMAL)
7036 			continue;
7037 
7038 		/* Throttle based on the first usable node */
7039 		pgdat = zone->zone_pgdat;
7040 		if (allow_direct_reclaim(pgdat))
7041 			goto out;
7042 		break;
7043 	}
7044 
7045 	/* If no zone was usable by the allocation flags then do not throttle */
7046 	if (!pgdat)
7047 		goto out;
7048 
7049 	/* Account for the throttling */
7050 	count_vm_event(PGSCAN_DIRECT_THROTTLE);
7051 
7052 	/*
7053 	 * If the caller cannot enter the filesystem, it's possible that it
7054 	 * is due to the caller holding an FS lock or performing a journal
7055 	 * transaction in the case of a filesystem like ext[3|4]. In this case,
7056 	 * it is not safe to block on pfmemalloc_wait as kswapd could be
7057 	 * blocked waiting on the same lock. Instead, throttle for up to a
7058 	 * second before continuing.
7059 	 */
7060 	if (!(gfp_mask & __GFP_FS))
7061 		wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
7062 			allow_direct_reclaim(pgdat), HZ);
7063 	else
7064 		/* Throttle until kswapd wakes the process */
7065 		wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
7066 			allow_direct_reclaim(pgdat));
7067 
7068 	if (fatal_signal_pending(current))
7069 		return true;
7070 
7071 out:
7072 	return false;
7073 }
7074 
7075 unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
7076 				gfp_t gfp_mask, nodemask_t *nodemask)
7077 {
7078 	unsigned long nr_reclaimed;
7079 	struct scan_control sc = {
7080 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
7081 		.gfp_mask = current_gfp_context(gfp_mask),
7082 		.reclaim_idx = gfp_zone(gfp_mask),
7083 		.order = order,
7084 		.nodemask = nodemask,
7085 		.priority = DEF_PRIORITY,
7086 		.may_writepage = !laptop_mode,
7087 		.may_unmap = 1,
7088 		.may_swap = 1,
7089 	};
7090 
7091 	/*
7092 	 * scan_control uses s8 fields for order, priority, and reclaim_idx.
7093 	 * Confirm they are large enough for max values.
7094 	 */
7095 	BUILD_BUG_ON(MAX_ORDER >= S8_MAX);
7096 	BUILD_BUG_ON(DEF_PRIORITY > S8_MAX);
7097 	BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX);
7098 
7099 	/*
7100 	 * Do not enter reclaim if fatal signal was delivered while throttled.
7101 	 * 1 is returned so that the page allocator does not OOM kill at this
7102 	 * point.
7103 	 */
7104 	if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask))
7105 		return 1;
7106 
7107 	set_task_reclaim_state(current, &sc.reclaim_state);
7108 	trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask);
7109 
7110 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
7111 
7112 	trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
7113 	set_task_reclaim_state(current, NULL);
7114 
7115 	return nr_reclaimed;
7116 }
7117 
7118 #ifdef CONFIG_MEMCG
7119 
7120 /* Only used by soft limit reclaim. Do not reuse for anything else. */
7121 unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg,
7122 						gfp_t gfp_mask, bool noswap,
7123 						pg_data_t *pgdat,
7124 						unsigned long *nr_scanned)
7125 {
7126 	struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
7127 	struct scan_control sc = {
7128 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
7129 		.target_mem_cgroup = memcg,
7130 		.may_writepage = !laptop_mode,
7131 		.may_unmap = 1,
7132 		.reclaim_idx = MAX_NR_ZONES - 1,
7133 		.may_swap = !noswap,
7134 	};
7135 
7136 	WARN_ON_ONCE(!current->reclaim_state);
7137 
7138 	sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
7139 			(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
7140 
7141 	trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
7142 						      sc.gfp_mask);
7143 
7144 	/*
7145 	 * NOTE: Although we can get the priority field, using it
7146 	 * here is not a good idea, since it limits the pages we can scan.
7147 	 * if we don't reclaim here, the shrink_node from balance_pgdat
7148 	 * will pick up pages from other mem cgroup's as well. We hack
7149 	 * the priority and make it zero.
7150 	 */
7151 	shrink_lruvec(lruvec, &sc);
7152 
7153 	trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
7154 
7155 	*nr_scanned = sc.nr_scanned;
7156 
7157 	return sc.nr_reclaimed;
7158 }
7159 
7160 unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
7161 					   unsigned long nr_pages,
7162 					   gfp_t gfp_mask,
7163 					   unsigned int reclaim_options)
7164 {
7165 	unsigned long nr_reclaimed;
7166 	unsigned int noreclaim_flag;
7167 	struct scan_control sc = {
7168 		.nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
7169 		.gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) |
7170 				(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
7171 		.reclaim_idx = MAX_NR_ZONES - 1,
7172 		.target_mem_cgroup = memcg,
7173 		.priority = DEF_PRIORITY,
7174 		.may_writepage = !laptop_mode,
7175 		.may_unmap = 1,
7176 		.may_swap = !!(reclaim_options & MEMCG_RECLAIM_MAY_SWAP),
7177 		.proactive = !!(reclaim_options & MEMCG_RECLAIM_PROACTIVE),
7178 	};
7179 	/*
7180 	 * Traverse the ZONELIST_FALLBACK zonelist of the current node to put
7181 	 * equal pressure on all the nodes. This is based on the assumption that
7182 	 * the reclaim does not bail out early.
7183 	 */
7184 	struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
7185 
7186 	set_task_reclaim_state(current, &sc.reclaim_state);
7187 	trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask);
7188 	noreclaim_flag = memalloc_noreclaim_save();
7189 
7190 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
7191 
7192 	memalloc_noreclaim_restore(noreclaim_flag);
7193 	trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
7194 	set_task_reclaim_state(current, NULL);
7195 
7196 	return nr_reclaimed;
7197 }
7198 #endif
7199 
7200 static void kswapd_age_node(struct pglist_data *pgdat, struct scan_control *sc)
7201 {
7202 	struct mem_cgroup *memcg;
7203 	struct lruvec *lruvec;
7204 
7205 	if (lru_gen_enabled()) {
7206 		lru_gen_age_node(pgdat, sc);
7207 		return;
7208 	}
7209 
7210 	if (!can_age_anon_pages(pgdat, sc))
7211 		return;
7212 
7213 	lruvec = mem_cgroup_lruvec(NULL, pgdat);
7214 	if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON))
7215 		return;
7216 
7217 	memcg = mem_cgroup_iter(NULL, NULL, NULL);
7218 	do {
7219 		lruvec = mem_cgroup_lruvec(memcg, pgdat);
7220 		shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
7221 				   sc, LRU_ACTIVE_ANON);
7222 		memcg = mem_cgroup_iter(NULL, memcg, NULL);
7223 	} while (memcg);
7224 }
7225 
7226 static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx)
7227 {
7228 	int i;
7229 	struct zone *zone;
7230 
7231 	/*
7232 	 * Check for watermark boosts top-down as the higher zones
7233 	 * are more likely to be boosted. Both watermarks and boosts
7234 	 * should not be checked at the same time as reclaim would
7235 	 * start prematurely when there is no boosting and a lower
7236 	 * zone is balanced.
7237 	 */
7238 	for (i = highest_zoneidx; i >= 0; i--) {
7239 		zone = pgdat->node_zones + i;
7240 		if (!managed_zone(zone))
7241 			continue;
7242 
7243 		if (zone->watermark_boost)
7244 			return true;
7245 	}
7246 
7247 	return false;
7248 }
7249 
7250 /*
7251  * Returns true if there is an eligible zone balanced for the request order
7252  * and highest_zoneidx
7253  */
7254 static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx)
7255 {
7256 	int i;
7257 	unsigned long mark = -1;
7258 	struct zone *zone;
7259 
7260 	/*
7261 	 * Check watermarks bottom-up as lower zones are more likely to
7262 	 * meet watermarks.
7263 	 */
7264 	for (i = 0; i <= highest_zoneidx; i++) {
7265 		zone = pgdat->node_zones + i;
7266 
7267 		if (!managed_zone(zone))
7268 			continue;
7269 
7270 		if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
7271 			mark = wmark_pages(zone, WMARK_PROMO);
7272 		else
7273 			mark = high_wmark_pages(zone);
7274 		if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx))
7275 			return true;
7276 	}
7277 
7278 	/*
7279 	 * If a node has no managed zone within highest_zoneidx, it does not
7280 	 * need balancing by definition. This can happen if a zone-restricted
7281 	 * allocation tries to wake a remote kswapd.
7282 	 */
7283 	if (mark == -1)
7284 		return true;
7285 
7286 	return false;
7287 }
7288 
7289 /* Clear pgdat state for congested, dirty or under writeback. */
7290 static void clear_pgdat_congested(pg_data_t *pgdat)
7291 {
7292 	struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat);
7293 
7294 	clear_bit(LRUVEC_NODE_CONGESTED, &lruvec->flags);
7295 	clear_bit(LRUVEC_CGROUP_CONGESTED, &lruvec->flags);
7296 	clear_bit(PGDAT_DIRTY, &pgdat->flags);
7297 	clear_bit(PGDAT_WRITEBACK, &pgdat->flags);
7298 }
7299 
7300 /*
7301  * Prepare kswapd for sleeping. This verifies that there are no processes
7302  * waiting in throttle_direct_reclaim() and that watermarks have been met.
7303  *
7304  * Returns true if kswapd is ready to sleep
7305  */
7306 static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order,
7307 				int highest_zoneidx)
7308 {
7309 	/*
7310 	 * The throttled processes are normally woken up in balance_pgdat() as
7311 	 * soon as allow_direct_reclaim() is true. But there is a potential
7312 	 * race between when kswapd checks the watermarks and a process gets
7313 	 * throttled. There is also a potential race if processes get
7314 	 * throttled, kswapd wakes, a large process exits thereby balancing the
7315 	 * zones, which causes kswapd to exit balance_pgdat() before reaching
7316 	 * the wake up checks. If kswapd is going to sleep, no process should
7317 	 * be sleeping on pfmemalloc_wait, so wake them now if necessary. If
7318 	 * the wake up is premature, processes will wake kswapd and get
7319 	 * throttled again. The difference from wake ups in balance_pgdat() is
7320 	 * that here we are under prepare_to_wait().
7321 	 */
7322 	if (waitqueue_active(&pgdat->pfmemalloc_wait))
7323 		wake_up_all(&pgdat->pfmemalloc_wait);
7324 
7325 	/* Hopeless node, leave it to direct reclaim */
7326 	if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
7327 		return true;
7328 
7329 	if (pgdat_balanced(pgdat, order, highest_zoneidx)) {
7330 		clear_pgdat_congested(pgdat);
7331 		return true;
7332 	}
7333 
7334 	return false;
7335 }
7336 
7337 /*
7338  * kswapd shrinks a node of pages that are at or below the highest usable
7339  * zone that is currently unbalanced.
7340  *
7341  * Returns true if kswapd scanned at least the requested number of pages to
7342  * reclaim or if the lack of progress was due to pages under writeback.
7343  * This is used to determine if the scanning priority needs to be raised.
7344  */
7345 static bool kswapd_shrink_node(pg_data_t *pgdat,
7346 			       struct scan_control *sc)
7347 {
7348 	struct zone *zone;
7349 	int z;
7350 	unsigned long nr_reclaimed = sc->nr_reclaimed;
7351 
7352 	/* Reclaim a number of pages proportional to the number of zones */
7353 	sc->nr_to_reclaim = 0;
7354 	for (z = 0; z <= sc->reclaim_idx; z++) {
7355 		zone = pgdat->node_zones + z;
7356 		if (!managed_zone(zone))
7357 			continue;
7358 
7359 		sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX);
7360 	}
7361 
7362 	/*
7363 	 * Historically care was taken to put equal pressure on all zones but
7364 	 * now pressure is applied based on node LRU order.
7365 	 */
7366 	shrink_node(pgdat, sc);
7367 
7368 	/*
7369 	 * Fragmentation may mean that the system cannot be rebalanced for
7370 	 * high-order allocations. If twice the allocation size has been
7371 	 * reclaimed then recheck watermarks only at order-0 to prevent
7372 	 * excessive reclaim. Assume that a process requested a high-order
7373 	 * can direct reclaim/compact.
7374 	 */
7375 	if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order))
7376 		sc->order = 0;
7377 
7378 	/* account for progress from mm_account_reclaimed_pages() */
7379 	return max(sc->nr_scanned, sc->nr_reclaimed - nr_reclaimed) >= sc->nr_to_reclaim;
7380 }
7381 
7382 /* Page allocator PCP high watermark is lowered if reclaim is active. */
7383 static inline void
7384 update_reclaim_active(pg_data_t *pgdat, int highest_zoneidx, bool active)
7385 {
7386 	int i;
7387 	struct zone *zone;
7388 
7389 	for (i = 0; i <= highest_zoneidx; i++) {
7390 		zone = pgdat->node_zones + i;
7391 
7392 		if (!managed_zone(zone))
7393 			continue;
7394 
7395 		if (active)
7396 			set_bit(ZONE_RECLAIM_ACTIVE, &zone->flags);
7397 		else
7398 			clear_bit(ZONE_RECLAIM_ACTIVE, &zone->flags);
7399 	}
7400 }
7401 
7402 static inline void
7403 set_reclaim_active(pg_data_t *pgdat, int highest_zoneidx)
7404 {
7405 	update_reclaim_active(pgdat, highest_zoneidx, true);
7406 }
7407 
7408 static inline void
7409 clear_reclaim_active(pg_data_t *pgdat, int highest_zoneidx)
7410 {
7411 	update_reclaim_active(pgdat, highest_zoneidx, false);
7412 }
7413 
7414 /*
7415  * For kswapd, balance_pgdat() will reclaim pages across a node from zones
7416  * that are eligible for use by the caller until at least one zone is
7417  * balanced.
7418  *
7419  * Returns the order kswapd finished reclaiming at.
7420  *
7421  * kswapd scans the zones in the highmem->normal->dma direction.  It skips
7422  * zones which have free_pages > high_wmark_pages(zone), but once a zone is
7423  * found to have free_pages <= high_wmark_pages(zone), any page in that zone
7424  * or lower is eligible for reclaim until at least one usable zone is
7425  * balanced.
7426  */
7427 static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx)
7428 {
7429 	int i;
7430 	unsigned long nr_soft_reclaimed;
7431 	unsigned long nr_soft_scanned;
7432 	unsigned long pflags;
7433 	unsigned long nr_boost_reclaim;
7434 	unsigned long zone_boosts[MAX_NR_ZONES] = { 0, };
7435 	bool boosted;
7436 	struct zone *zone;
7437 	struct scan_control sc = {
7438 		.gfp_mask = GFP_KERNEL,
7439 		.order = order,
7440 		.may_unmap = 1,
7441 	};
7442 
7443 	set_task_reclaim_state(current, &sc.reclaim_state);
7444 	psi_memstall_enter(&pflags);
7445 	__fs_reclaim_acquire(_THIS_IP_);
7446 
7447 	count_vm_event(PAGEOUTRUN);
7448 
7449 	/*
7450 	 * Account for the reclaim boost. Note that the zone boost is left in
7451 	 * place so that parallel allocations that are near the watermark will
7452 	 * stall or direct reclaim until kswapd is finished.
7453 	 */
7454 	nr_boost_reclaim = 0;
7455 	for (i = 0; i <= highest_zoneidx; i++) {
7456 		zone = pgdat->node_zones + i;
7457 		if (!managed_zone(zone))
7458 			continue;
7459 
7460 		nr_boost_reclaim += zone->watermark_boost;
7461 		zone_boosts[i] = zone->watermark_boost;
7462 	}
7463 	boosted = nr_boost_reclaim;
7464 
7465 restart:
7466 	set_reclaim_active(pgdat, highest_zoneidx);
7467 	sc.priority = DEF_PRIORITY;
7468 	do {
7469 		unsigned long nr_reclaimed = sc.nr_reclaimed;
7470 		bool raise_priority = true;
7471 		bool balanced;
7472 		bool ret;
7473 
7474 		sc.reclaim_idx = highest_zoneidx;
7475 
7476 		/*
7477 		 * If the number of buffer_heads exceeds the maximum allowed
7478 		 * then consider reclaiming from all zones. This has a dual
7479 		 * purpose -- on 64-bit systems it is expected that
7480 		 * buffer_heads are stripped during active rotation. On 32-bit
7481 		 * systems, highmem pages can pin lowmem memory and shrinking
7482 		 * buffers can relieve lowmem pressure. Reclaim may still not
7483 		 * go ahead if all eligible zones for the original allocation
7484 		 * request are balanced to avoid excessive reclaim from kswapd.
7485 		 */
7486 		if (buffer_heads_over_limit) {
7487 			for (i = MAX_NR_ZONES - 1; i >= 0; i--) {
7488 				zone = pgdat->node_zones + i;
7489 				if (!managed_zone(zone))
7490 					continue;
7491 
7492 				sc.reclaim_idx = i;
7493 				break;
7494 			}
7495 		}
7496 
7497 		/*
7498 		 * If the pgdat is imbalanced then ignore boosting and preserve
7499 		 * the watermarks for a later time and restart. Note that the
7500 		 * zone watermarks will be still reset at the end of balancing
7501 		 * on the grounds that the normal reclaim should be enough to
7502 		 * re-evaluate if boosting is required when kswapd next wakes.
7503 		 */
7504 		balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx);
7505 		if (!balanced && nr_boost_reclaim) {
7506 			nr_boost_reclaim = 0;
7507 			goto restart;
7508 		}
7509 
7510 		/*
7511 		 * If boosting is not active then only reclaim if there are no
7512 		 * eligible zones. Note that sc.reclaim_idx is not used as
7513 		 * buffer_heads_over_limit may have adjusted it.
7514 		 */
7515 		if (!nr_boost_reclaim && balanced)
7516 			goto out;
7517 
7518 		/* Limit the priority of boosting to avoid reclaim writeback */
7519 		if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2)
7520 			raise_priority = false;
7521 
7522 		/*
7523 		 * Do not writeback or swap pages for boosted reclaim. The
7524 		 * intent is to relieve pressure not issue sub-optimal IO
7525 		 * from reclaim context. If no pages are reclaimed, the
7526 		 * reclaim will be aborted.
7527 		 */
7528 		sc.may_writepage = !laptop_mode && !nr_boost_reclaim;
7529 		sc.may_swap = !nr_boost_reclaim;
7530 
7531 		/*
7532 		 * Do some background aging, to give pages a chance to be
7533 		 * referenced before reclaiming. All pages are rotated
7534 		 * regardless of classzone as this is about consistent aging.
7535 		 */
7536 		kswapd_age_node(pgdat, &sc);
7537 
7538 		/*
7539 		 * If we're getting trouble reclaiming, start doing writepage
7540 		 * even in laptop mode.
7541 		 */
7542 		if (sc.priority < DEF_PRIORITY - 2)
7543 			sc.may_writepage = 1;
7544 
7545 		/* Call soft limit reclaim before calling shrink_node. */
7546 		sc.nr_scanned = 0;
7547 		nr_soft_scanned = 0;
7548 		nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order,
7549 						sc.gfp_mask, &nr_soft_scanned);
7550 		sc.nr_reclaimed += nr_soft_reclaimed;
7551 
7552 		/*
7553 		 * There should be no need to raise the scanning priority if
7554 		 * enough pages are already being scanned that that high
7555 		 * watermark would be met at 100% efficiency.
7556 		 */
7557 		if (kswapd_shrink_node(pgdat, &sc))
7558 			raise_priority = false;
7559 
7560 		/*
7561 		 * If the low watermark is met there is no need for processes
7562 		 * to be throttled on pfmemalloc_wait as they should not be
7563 		 * able to safely make forward progress. Wake them
7564 		 */
7565 		if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
7566 				allow_direct_reclaim(pgdat))
7567 			wake_up_all(&pgdat->pfmemalloc_wait);
7568 
7569 		/* Check if kswapd should be suspending */
7570 		__fs_reclaim_release(_THIS_IP_);
7571 		ret = try_to_freeze();
7572 		__fs_reclaim_acquire(_THIS_IP_);
7573 		if (ret || kthread_should_stop())
7574 			break;
7575 
7576 		/*
7577 		 * Raise priority if scanning rate is too low or there was no
7578 		 * progress in reclaiming pages
7579 		 */
7580 		nr_reclaimed = sc.nr_reclaimed - nr_reclaimed;
7581 		nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed);
7582 
7583 		/*
7584 		 * If reclaim made no progress for a boost, stop reclaim as
7585 		 * IO cannot be queued and it could be an infinite loop in
7586 		 * extreme circumstances.
7587 		 */
7588 		if (nr_boost_reclaim && !nr_reclaimed)
7589 			break;
7590 
7591 		if (raise_priority || !nr_reclaimed)
7592 			sc.priority--;
7593 	} while (sc.priority >= 1);
7594 
7595 	if (!sc.nr_reclaimed)
7596 		pgdat->kswapd_failures++;
7597 
7598 out:
7599 	clear_reclaim_active(pgdat, highest_zoneidx);
7600 
7601 	/* If reclaim was boosted, account for the reclaim done in this pass */
7602 	if (boosted) {
7603 		unsigned long flags;
7604 
7605 		for (i = 0; i <= highest_zoneidx; i++) {
7606 			if (!zone_boosts[i])
7607 				continue;
7608 
7609 			/* Increments are under the zone lock */
7610 			zone = pgdat->node_zones + i;
7611 			spin_lock_irqsave(&zone->lock, flags);
7612 			zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]);
7613 			spin_unlock_irqrestore(&zone->lock, flags);
7614 		}
7615 
7616 		/*
7617 		 * As there is now likely space, wakeup kcompact to defragment
7618 		 * pageblocks.
7619 		 */
7620 		wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx);
7621 	}
7622 
7623 	snapshot_refaults(NULL, pgdat);
7624 	__fs_reclaim_release(_THIS_IP_);
7625 	psi_memstall_leave(&pflags);
7626 	set_task_reclaim_state(current, NULL);
7627 
7628 	/*
7629 	 * Return the order kswapd stopped reclaiming at as
7630 	 * prepare_kswapd_sleep() takes it into account. If another caller
7631 	 * entered the allocator slow path while kswapd was awake, order will
7632 	 * remain at the higher level.
7633 	 */
7634 	return sc.order;
7635 }
7636 
7637 /*
7638  * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to
7639  * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is
7640  * not a valid index then either kswapd runs for first time or kswapd couldn't
7641  * sleep after previous reclaim attempt (node is still unbalanced). In that
7642  * case return the zone index of the previous kswapd reclaim cycle.
7643  */
7644 static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,
7645 					   enum zone_type prev_highest_zoneidx)
7646 {
7647 	enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
7648 
7649 	return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx;
7650 }
7651 
7652 static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order,
7653 				unsigned int highest_zoneidx)
7654 {
7655 	long remaining = 0;
7656 	DEFINE_WAIT(wait);
7657 
7658 	if (freezing(current) || kthread_should_stop())
7659 		return;
7660 
7661 	prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
7662 
7663 	/*
7664 	 * Try to sleep for a short interval. Note that kcompactd will only be
7665 	 * woken if it is possible to sleep for a short interval. This is
7666 	 * deliberate on the assumption that if reclaim cannot keep an
7667 	 * eligible zone balanced that it's also unlikely that compaction will
7668 	 * succeed.
7669 	 */
7670 	if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
7671 		/*
7672 		 * Compaction records what page blocks it recently failed to
7673 		 * isolate pages from and skips them in the future scanning.
7674 		 * When kswapd is going to sleep, it is reasonable to assume
7675 		 * that pages and compaction may succeed so reset the cache.
7676 		 */
7677 		reset_isolation_suitable(pgdat);
7678 
7679 		/*
7680 		 * We have freed the memory, now we should compact it to make
7681 		 * allocation of the requested order possible.
7682 		 */
7683 		wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx);
7684 
7685 		remaining = schedule_timeout(HZ/10);
7686 
7687 		/*
7688 		 * If woken prematurely then reset kswapd_highest_zoneidx and
7689 		 * order. The values will either be from a wakeup request or
7690 		 * the previous request that slept prematurely.
7691 		 */
7692 		if (remaining) {
7693 			WRITE_ONCE(pgdat->kswapd_highest_zoneidx,
7694 					kswapd_highest_zoneidx(pgdat,
7695 							highest_zoneidx));
7696 
7697 			if (READ_ONCE(pgdat->kswapd_order) < reclaim_order)
7698 				WRITE_ONCE(pgdat->kswapd_order, reclaim_order);
7699 		}
7700 
7701 		finish_wait(&pgdat->kswapd_wait, &wait);
7702 		prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
7703 	}
7704 
7705 	/*
7706 	 * After a short sleep, check if it was a premature sleep. If not, then
7707 	 * go fully to sleep until explicitly woken up.
7708 	 */
7709 	if (!remaining &&
7710 	    prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
7711 		trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
7712 
7713 		/*
7714 		 * vmstat counters are not perfectly accurate and the estimated
7715 		 * value for counters such as NR_FREE_PAGES can deviate from the
7716 		 * true value by nr_online_cpus * threshold. To avoid the zone
7717 		 * watermarks being breached while under pressure, we reduce the
7718 		 * per-cpu vmstat threshold while kswapd is awake and restore
7719 		 * them before going back to sleep.
7720 		 */
7721 		set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
7722 
7723 		if (!kthread_should_stop())
7724 			schedule();
7725 
7726 		set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
7727 	} else {
7728 		if (remaining)
7729 			count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
7730 		else
7731 			count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
7732 	}
7733 	finish_wait(&pgdat->kswapd_wait, &wait);
7734 }
7735 
7736 /*
7737  * The background pageout daemon, started as a kernel thread
7738  * from the init process.
7739  *
7740  * This basically trickles out pages so that we have _some_
7741  * free memory available even if there is no other activity
7742  * that frees anything up. This is needed for things like routing
7743  * etc, where we otherwise might have all activity going on in
7744  * asynchronous contexts that cannot page things out.
7745  *
7746  * If there are applications that are active memory-allocators
7747  * (most normal use), this basically shouldn't matter.
7748  */
7749 static int kswapd(void *p)
7750 {
7751 	unsigned int alloc_order, reclaim_order;
7752 	unsigned int highest_zoneidx = MAX_NR_ZONES - 1;
7753 	pg_data_t *pgdat = (pg_data_t *)p;
7754 	struct task_struct *tsk = current;
7755 	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
7756 
7757 	if (!cpumask_empty(cpumask))
7758 		set_cpus_allowed_ptr(tsk, cpumask);
7759 
7760 	/*
7761 	 * Tell the memory management that we're a "memory allocator",
7762 	 * and that if we need more memory we should get access to it
7763 	 * regardless (see "__alloc_pages()"). "kswapd" should
7764 	 * never get caught in the normal page freeing logic.
7765 	 *
7766 	 * (Kswapd normally doesn't need memory anyway, but sometimes
7767 	 * you need a small amount of memory in order to be able to
7768 	 * page out something else, and this flag essentially protects
7769 	 * us from recursively trying to free more memory as we're
7770 	 * trying to free the first piece of memory in the first place).
7771 	 */
7772 	tsk->flags |= PF_MEMALLOC | PF_KSWAPD;
7773 	set_freezable();
7774 
7775 	WRITE_ONCE(pgdat->kswapd_order, 0);
7776 	WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
7777 	atomic_set(&pgdat->nr_writeback_throttled, 0);
7778 	for ( ; ; ) {
7779 		bool ret;
7780 
7781 		alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order);
7782 		highest_zoneidx = kswapd_highest_zoneidx(pgdat,
7783 							highest_zoneidx);
7784 
7785 kswapd_try_sleep:
7786 		kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order,
7787 					highest_zoneidx);
7788 
7789 		/* Read the new order and highest_zoneidx */
7790 		alloc_order = READ_ONCE(pgdat->kswapd_order);
7791 		highest_zoneidx = kswapd_highest_zoneidx(pgdat,
7792 							highest_zoneidx);
7793 		WRITE_ONCE(pgdat->kswapd_order, 0);
7794 		WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
7795 
7796 		ret = try_to_freeze();
7797 		if (kthread_should_stop())
7798 			break;
7799 
7800 		/*
7801 		 * We can speed up thawing tasks if we don't call balance_pgdat
7802 		 * after returning from the refrigerator
7803 		 */
7804 		if (ret)
7805 			continue;
7806 
7807 		/*
7808 		 * Reclaim begins at the requested order but if a high-order
7809 		 * reclaim fails then kswapd falls back to reclaiming for
7810 		 * order-0. If that happens, kswapd will consider sleeping
7811 		 * for the order it finished reclaiming at (reclaim_order)
7812 		 * but kcompactd is woken to compact for the original
7813 		 * request (alloc_order).
7814 		 */
7815 		trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx,
7816 						alloc_order);
7817 		reclaim_order = balance_pgdat(pgdat, alloc_order,
7818 						highest_zoneidx);
7819 		if (reclaim_order < alloc_order)
7820 			goto kswapd_try_sleep;
7821 	}
7822 
7823 	tsk->flags &= ~(PF_MEMALLOC | PF_KSWAPD);
7824 
7825 	return 0;
7826 }
7827 
7828 /*
7829  * A zone is low on free memory or too fragmented for high-order memory.  If
7830  * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's
7831  * pgdat.  It will wake up kcompactd after reclaiming memory.  If kswapd reclaim
7832  * has failed or is not needed, still wake up kcompactd if only compaction is
7833  * needed.
7834  */
7835 void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order,
7836 		   enum zone_type highest_zoneidx)
7837 {
7838 	pg_data_t *pgdat;
7839 	enum zone_type curr_idx;
7840 
7841 	if (!managed_zone(zone))
7842 		return;
7843 
7844 	if (!cpuset_zone_allowed(zone, gfp_flags))
7845 		return;
7846 
7847 	pgdat = zone->zone_pgdat;
7848 	curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
7849 
7850 	if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx)
7851 		WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx);
7852 
7853 	if (READ_ONCE(pgdat->kswapd_order) < order)
7854 		WRITE_ONCE(pgdat->kswapd_order, order);
7855 
7856 	if (!waitqueue_active(&pgdat->kswapd_wait))
7857 		return;
7858 
7859 	/* Hopeless node, leave it to direct reclaim if possible */
7860 	if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ||
7861 	    (pgdat_balanced(pgdat, order, highest_zoneidx) &&
7862 	     !pgdat_watermark_boosted(pgdat, highest_zoneidx))) {
7863 		/*
7864 		 * There may be plenty of free memory available, but it's too
7865 		 * fragmented for high-order allocations.  Wake up kcompactd
7866 		 * and rely on compaction_suitable() to determine if it's
7867 		 * needed.  If it fails, it will defer subsequent attempts to
7868 		 * ratelimit its work.
7869 		 */
7870 		if (!(gfp_flags & __GFP_DIRECT_RECLAIM))
7871 			wakeup_kcompactd(pgdat, order, highest_zoneidx);
7872 		return;
7873 	}
7874 
7875 	trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order,
7876 				      gfp_flags);
7877 	wake_up_interruptible(&pgdat->kswapd_wait);
7878 }
7879 
7880 #ifdef CONFIG_HIBERNATION
7881 /*
7882  * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
7883  * freed pages.
7884  *
7885  * Rather than trying to age LRUs the aim is to preserve the overall
7886  * LRU order by reclaiming preferentially
7887  * inactive > active > active referenced > active mapped
7888  */
7889 unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
7890 {
7891 	struct scan_control sc = {
7892 		.nr_to_reclaim = nr_to_reclaim,
7893 		.gfp_mask = GFP_HIGHUSER_MOVABLE,
7894 		.reclaim_idx = MAX_NR_ZONES - 1,
7895 		.priority = DEF_PRIORITY,
7896 		.may_writepage = 1,
7897 		.may_unmap = 1,
7898 		.may_swap = 1,
7899 		.hibernation_mode = 1,
7900 	};
7901 	struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
7902 	unsigned long nr_reclaimed;
7903 	unsigned int noreclaim_flag;
7904 
7905 	fs_reclaim_acquire(sc.gfp_mask);
7906 	noreclaim_flag = memalloc_noreclaim_save();
7907 	set_task_reclaim_state(current, &sc.reclaim_state);
7908 
7909 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
7910 
7911 	set_task_reclaim_state(current, NULL);
7912 	memalloc_noreclaim_restore(noreclaim_flag);
7913 	fs_reclaim_release(sc.gfp_mask);
7914 
7915 	return nr_reclaimed;
7916 }
7917 #endif /* CONFIG_HIBERNATION */
7918 
7919 /*
7920  * This kswapd start function will be called by init and node-hot-add.
7921  */
7922 void __meminit kswapd_run(int nid)
7923 {
7924 	pg_data_t *pgdat = NODE_DATA(nid);
7925 
7926 	pgdat_kswapd_lock(pgdat);
7927 	if (!pgdat->kswapd) {
7928 		pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
7929 		if (IS_ERR(pgdat->kswapd)) {
7930 			/* failure at boot is fatal */
7931 			BUG_ON(system_state < SYSTEM_RUNNING);
7932 			pr_err("Failed to start kswapd on node %d\n", nid);
7933 			pgdat->kswapd = NULL;
7934 		}
7935 	}
7936 	pgdat_kswapd_unlock(pgdat);
7937 }
7938 
7939 /*
7940  * Called by memory hotplug when all memory in a node is offlined.  Caller must
7941  * be holding mem_hotplug_begin/done().
7942  */
7943 void __meminit kswapd_stop(int nid)
7944 {
7945 	pg_data_t *pgdat = NODE_DATA(nid);
7946 	struct task_struct *kswapd;
7947 
7948 	pgdat_kswapd_lock(pgdat);
7949 	kswapd = pgdat->kswapd;
7950 	if (kswapd) {
7951 		kthread_stop(kswapd);
7952 		pgdat->kswapd = NULL;
7953 	}
7954 	pgdat_kswapd_unlock(pgdat);
7955 }
7956 
7957 static int __init kswapd_init(void)
7958 {
7959 	int nid;
7960 
7961 	swap_setup();
7962 	for_each_node_state(nid, N_MEMORY)
7963  		kswapd_run(nid);
7964 	return 0;
7965 }
7966 
7967 module_init(kswapd_init)
7968 
7969 #ifdef CONFIG_NUMA
7970 /*
7971  * Node reclaim mode
7972  *
7973  * If non-zero call node_reclaim when the number of free pages falls below
7974  * the watermarks.
7975  */
7976 int node_reclaim_mode __read_mostly;
7977 
7978 /*
7979  * Priority for NODE_RECLAIM. This determines the fraction of pages
7980  * of a node considered for each zone_reclaim. 4 scans 1/16th of
7981  * a zone.
7982  */
7983 #define NODE_RECLAIM_PRIORITY 4
7984 
7985 /*
7986  * Percentage of pages in a zone that must be unmapped for node_reclaim to
7987  * occur.
7988  */
7989 int sysctl_min_unmapped_ratio = 1;
7990 
7991 /*
7992  * If the number of slab pages in a zone grows beyond this percentage then
7993  * slab reclaim needs to occur.
7994  */
7995 int sysctl_min_slab_ratio = 5;
7996 
7997 static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat)
7998 {
7999 	unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED);
8000 	unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) +
8001 		node_page_state(pgdat, NR_ACTIVE_FILE);
8002 
8003 	/*
8004 	 * It's possible for there to be more file mapped pages than
8005 	 * accounted for by the pages on the file LRU lists because
8006 	 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
8007 	 */
8008 	return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
8009 }
8010 
8011 /* Work out how many page cache pages we can reclaim in this reclaim_mode */
8012 static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat)
8013 {
8014 	unsigned long nr_pagecache_reclaimable;
8015 	unsigned long delta = 0;
8016 
8017 	/*
8018 	 * If RECLAIM_UNMAP is set, then all file pages are considered
8019 	 * potentially reclaimable. Otherwise, we have to worry about
8020 	 * pages like swapcache and node_unmapped_file_pages() provides
8021 	 * a better estimate
8022 	 */
8023 	if (node_reclaim_mode & RECLAIM_UNMAP)
8024 		nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES);
8025 	else
8026 		nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat);
8027 
8028 	/* If we can't clean pages, remove dirty pages from consideration */
8029 	if (!(node_reclaim_mode & RECLAIM_WRITE))
8030 		delta += node_page_state(pgdat, NR_FILE_DIRTY);
8031 
8032 	/* Watch for any possible underflows due to delta */
8033 	if (unlikely(delta > nr_pagecache_reclaimable))
8034 		delta = nr_pagecache_reclaimable;
8035 
8036 	return nr_pagecache_reclaimable - delta;
8037 }
8038 
8039 /*
8040  * Try to free up some pages from this node through reclaim.
8041  */
8042 static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
8043 {
8044 	/* Minimum pages needed in order to stay on node */
8045 	const unsigned long nr_pages = 1 << order;
8046 	struct task_struct *p = current;
8047 	unsigned int noreclaim_flag;
8048 	struct scan_control sc = {
8049 		.nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
8050 		.gfp_mask = current_gfp_context(gfp_mask),
8051 		.order = order,
8052 		.priority = NODE_RECLAIM_PRIORITY,
8053 		.may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE),
8054 		.may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP),
8055 		.may_swap = 1,
8056 		.reclaim_idx = gfp_zone(gfp_mask),
8057 	};
8058 	unsigned long pflags;
8059 
8060 	trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order,
8061 					   sc.gfp_mask);
8062 
8063 	cond_resched();
8064 	psi_memstall_enter(&pflags);
8065 	fs_reclaim_acquire(sc.gfp_mask);
8066 	/*
8067 	 * We need to be able to allocate from the reserves for RECLAIM_UNMAP
8068 	 */
8069 	noreclaim_flag = memalloc_noreclaim_save();
8070 	set_task_reclaim_state(p, &sc.reclaim_state);
8071 
8072 	if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages ||
8073 	    node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) > pgdat->min_slab_pages) {
8074 		/*
8075 		 * Free memory by calling shrink node with increasing
8076 		 * priorities until we have enough memory freed.
8077 		 */
8078 		do {
8079 			shrink_node(pgdat, &sc);
8080 		} while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
8081 	}
8082 
8083 	set_task_reclaim_state(p, NULL);
8084 	memalloc_noreclaim_restore(noreclaim_flag);
8085 	fs_reclaim_release(sc.gfp_mask);
8086 	psi_memstall_leave(&pflags);
8087 
8088 	trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed);
8089 
8090 	return sc.nr_reclaimed >= nr_pages;
8091 }
8092 
8093 int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
8094 {
8095 	int ret;
8096 
8097 	/*
8098 	 * Node reclaim reclaims unmapped file backed pages and
8099 	 * slab pages if we are over the defined limits.
8100 	 *
8101 	 * A small portion of unmapped file backed pages is needed for
8102 	 * file I/O otherwise pages read by file I/O will be immediately
8103 	 * thrown out if the node is overallocated. So we do not reclaim
8104 	 * if less than a specified percentage of the node is used by
8105 	 * unmapped file backed pages.
8106 	 */
8107 	if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages &&
8108 	    node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <=
8109 	    pgdat->min_slab_pages)
8110 		return NODE_RECLAIM_FULL;
8111 
8112 	/*
8113 	 * Do not scan if the allocation should not be delayed.
8114 	 */
8115 	if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC))
8116 		return NODE_RECLAIM_NOSCAN;
8117 
8118 	/*
8119 	 * Only run node reclaim on the local node or on nodes that do not
8120 	 * have associated processors. This will favor the local processor
8121 	 * over remote processors and spread off node memory allocations
8122 	 * as wide as possible.
8123 	 */
8124 	if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
8125 		return NODE_RECLAIM_NOSCAN;
8126 
8127 	if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
8128 		return NODE_RECLAIM_NOSCAN;
8129 
8130 	ret = __node_reclaim(pgdat, gfp_mask, order);
8131 	clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags);
8132 
8133 	if (!ret)
8134 		count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
8135 
8136 	return ret;
8137 }
8138 #endif
8139 
8140 /**
8141  * check_move_unevictable_folios - Move evictable folios to appropriate zone
8142  * lru list
8143  * @fbatch: Batch of lru folios to check.
8144  *
8145  * Checks folios for evictability, if an evictable folio is in the unevictable
8146  * lru list, moves it to the appropriate evictable lru list. This function
8147  * should be only used for lru folios.
8148  */
8149 void check_move_unevictable_folios(struct folio_batch *fbatch)
8150 {
8151 	struct lruvec *lruvec = NULL;
8152 	int pgscanned = 0;
8153 	int pgrescued = 0;
8154 	int i;
8155 
8156 	for (i = 0; i < fbatch->nr; i++) {
8157 		struct folio *folio = fbatch->folios[i];
8158 		int nr_pages = folio_nr_pages(folio);
8159 
8160 		pgscanned += nr_pages;
8161 
8162 		/* block memcg migration while the folio moves between lrus */
8163 		if (!folio_test_clear_lru(folio))
8164 			continue;
8165 
8166 		lruvec = folio_lruvec_relock_irq(folio, lruvec);
8167 		if (folio_evictable(folio) && folio_test_unevictable(folio)) {
8168 			lruvec_del_folio(lruvec, folio);
8169 			folio_clear_unevictable(folio);
8170 			lruvec_add_folio(lruvec, folio);
8171 			pgrescued += nr_pages;
8172 		}
8173 		folio_set_lru(folio);
8174 	}
8175 
8176 	if (lruvec) {
8177 		__count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
8178 		__count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
8179 		unlock_page_lruvec_irq(lruvec);
8180 	} else if (pgscanned) {
8181 		count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
8182 	}
8183 }
8184 EXPORT_SYMBOL_GPL(check_move_unevictable_folios);
8185