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