kernel: 6.1: Synchronize MGLRU patches with upstream
[openwrt/staging/stintel.git] / target / linux / generic / backport-6.1 / 020-v6.3-07-BACKPORT-mm-multi-gen-LRU-clarify-scan_control-flags.patch
1 From 11b14ee8cbbbebd8204609076a9327a1171cd253 Mon Sep 17 00:00:00 2001
2 From: Yu Zhao <yuzhao@google.com>
3 Date: Wed, 21 Dec 2022 21:19:05 -0700
4 Subject: [PATCH 07/19] BACKPORT: mm: multi-gen LRU: clarify scan_control flags
5
6 Among the flags in scan_control:
7 1. sc->may_swap, which indicates swap constraint due to memsw.max, is
8 supported as usual.
9 2. sc->proactive, which indicates reclaim by memory.reclaim, may not
10 opportunistically skip the aging path, since it is considered less
11 latency sensitive.
12 3. !(sc->gfp_mask & __GFP_IO), which indicates IO constraint, lowers
13 swappiness to prioritize file LRU, since clean file folios are more
14 likely to exist.
15 4. sc->may_writepage and sc->may_unmap, which indicates opportunistic
16 reclaim, are rejected, since unmapped clean folios are already
17 prioritized. Scanning for more of them is likely futile and can
18 cause high reclaim latency when there is a large number of memcgs.
19
20 The rest are handled by the existing code.
21
22 Link: https://lkml.kernel.org/r/20221222041905.2431096-8-yuzhao@google.com
23 Signed-off-by: Yu Zhao <yuzhao@google.com>
24 Cc: Johannes Weiner <hannes@cmpxchg.org>
25 Cc: Jonathan Corbet <corbet@lwn.net>
26 Cc: Michael Larabel <Michael@MichaelLarabel.com>
27 Cc: Michal Hocko <mhocko@kernel.org>
28 Cc: Mike Rapoport <rppt@kernel.org>
29 Cc: Roman Gushchin <roman.gushchin@linux.dev>
30 Cc: Suren Baghdasaryan <surenb@google.com>
31 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
32 Bug: 274865848
33 (cherry picked from commit e9d4e1ee788097484606c32122f146d802a9c5fb)
34 [TJ: Resolved conflict with older function signature for min_cgroup_below_min, and over
35 cdded861182142ac4488a4d64c571107aeb77f53 ("ANDROID: MGLRU: Don't skip anon reclaim if swap low")]
36 Change-Id: Ic2e779eaf4e91a3921831b4e2fa10c740dc59d50
37 Signed-off-by: T.J. Mercier <tjmercier@google.com>
38 ---
39 mm/vmscan.c | 55 +++++++++++++++++++++++++++--------------------------
40 1 file changed, 28 insertions(+), 27 deletions(-)
41
42 diff --git a/mm/vmscan.c b/mm/vmscan.c
43 index 65eb28448f216..0a0e1250ffc87 100644
44 --- a/mm/vmscan.c
45 +++ b/mm/vmscan.c
46 @@ -3185,6 +3185,9 @@ static int get_swappiness(struct lruvec *lruvec, struct scan_control *sc)
47 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
48 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
49
50 + if (!sc->may_swap)
51 + return 0;
52 +
53 if (!can_demote(pgdat->node_id, sc) &&
54 mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH)
55 return 0;
56 @@ -4223,7 +4226,7 @@ static void walk_mm(struct lruvec *lruvec, struct mm_struct *mm, struct lru_gen_
57 } while (err == -EAGAIN);
58 }
59
60 -static struct lru_gen_mm_walk *set_mm_walk(struct pglist_data *pgdat)
61 +static struct lru_gen_mm_walk *set_mm_walk(struct pglist_data *pgdat, bool force_alloc)
62 {
63 struct lru_gen_mm_walk *walk = current->reclaim_state->mm_walk;
64
65 @@ -4231,7 +4234,7 @@ static struct lru_gen_mm_walk *set_mm_walk(struct pglist_data *pgdat)
66 VM_WARN_ON_ONCE(walk);
67
68 walk = &pgdat->mm_walk;
69 - } else if (!pgdat && !walk) {
70 + } else if (!walk && force_alloc) {
71 VM_WARN_ON_ONCE(current_is_kswapd());
72
73 walk = kzalloc(sizeof(*walk), __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN);
74 @@ -4417,7 +4420,7 @@ static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long max_seq,
75 goto done;
76 }
77
78 - walk = set_mm_walk(NULL);
79 + walk = set_mm_walk(NULL, true);
80 if (!walk) {
81 success = iterate_mm_list_nowalk(lruvec, max_seq);
82 goto done;
83 @@ -4486,8 +4489,6 @@ static bool lruvec_is_reclaimable(struct lruvec *lruvec, struct scan_control *sc
84 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
85 DEFINE_MIN_SEQ(lruvec);
86
87 - VM_WARN_ON_ONCE(sc->memcg_low_reclaim);
88 -
89 /* see the comment on lru_gen_folio */
90 gen = lru_gen_from_seq(min_seq[LRU_GEN_FILE]);
91 birth = READ_ONCE(lruvec->lrugen.timestamps[gen]);
92 @@ -4743,12 +4744,8 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca
93 {
94 bool success;
95
96 - /* unmapping inhibited */
97 - if (!sc->may_unmap && folio_mapped(folio))
98 - return false;
99 -
100 /* swapping inhibited */
101 - if (!(sc->may_writepage && (sc->gfp_mask & __GFP_IO)) &&
102 + if (!(sc->gfp_mask & __GFP_IO) &&
103 (folio_test_dirty(folio) ||
104 (folio_test_anon(folio) && !folio_test_swapcache(folio))))
105 return false;
106 @@ -4845,9 +4842,8 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc,
107 __count_vm_events(PGSCAN_ANON + type, isolated);
108
109 /*
110 - * There might not be eligible pages due to reclaim_idx, may_unmap and
111 - * may_writepage. Check the remaining to prevent livelock if it's not
112 - * making progress.
113 + * There might not be eligible folios due to reclaim_idx. Check the
114 + * remaining to prevent livelock if it's not making progress.
115 */
116 return isolated || !remaining ? scanned : 0;
117 }
118 @@ -5107,8 +5103,7 @@ static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc, bool
119 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
120 DEFINE_MAX_SEQ(lruvec);
121
122 - if (mem_cgroup_below_min(memcg) ||
123 - (mem_cgroup_below_low(memcg) && !sc->memcg_low_reclaim))
124 + if (mem_cgroup_below_min(memcg))
125 return 0;
126
127 if (!should_run_aging(lruvec, max_seq, sc, can_swap, &nr_to_scan))
128 @@ -5136,17 +5131,14 @@ static bool try_to_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
129 long nr_to_scan;
130 unsigned long scanned = 0;
131 unsigned long nr_to_reclaim = get_nr_to_reclaim(sc);
132 + int swappiness = get_swappiness(lruvec, sc);
133 +
134 + /* clean file folios are more likely to exist */
135 + if (swappiness && !(sc->gfp_mask & __GFP_IO))
136 + swappiness = 1;
137
138 while (true) {
139 int delta;
140 - int swappiness;
141 -
142 - if (sc->may_swap)
143 - swappiness = get_swappiness(lruvec, sc);
144 - else if (!cgroup_reclaim(sc) && get_swappiness(lruvec, sc))
145 - swappiness = 1;
146 - else
147 - swappiness = 0;
148
149 nr_to_scan = get_nr_to_scan(lruvec, sc, swappiness);
150 if (nr_to_scan <= 0)
151 @@ -5277,12 +5269,13 @@ static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc
152 struct blk_plug plug;
153
154 VM_WARN_ON_ONCE(global_reclaim(sc));
155 + VM_WARN_ON_ONCE(!sc->may_writepage || !sc->may_unmap);
156
157 lru_add_drain();
158
159 blk_start_plug(&plug);
160
161 - set_mm_walk(lruvec_pgdat(lruvec));
162 + set_mm_walk(NULL, sc->proactive);
163
164 if (try_to_shrink_lruvec(lruvec, sc))
165 lru_gen_rotate_memcg(lruvec, MEMCG_LRU_YOUNG);
166 @@ -5338,11 +5331,19 @@ static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *
167
168 VM_WARN_ON_ONCE(!global_reclaim(sc));
169
170 + /*
171 + * Unmapped clean folios are already prioritized. Scanning for more of
172 + * them is likely futile and can cause high reclaim latency when there
173 + * is a large number of memcgs.
174 + */
175 + if (!sc->may_writepage || !sc->may_unmap)
176 + goto done;
177 +
178 lru_add_drain();
179
180 blk_start_plug(&plug);
181
182 - set_mm_walk(pgdat);
183 + set_mm_walk(pgdat, sc->proactive);
184
185 set_initial_priority(pgdat, sc);
186
187 @@ -5360,7 +5361,7 @@ static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *
188 clear_mm_walk();
189
190 blk_finish_plug(&plug);
191 -
192 +done:
193 /* kswapd should never fail */
194 pgdat->kswapd_failures = 0;
195 }
196 @@ -5932,7 +5933,7 @@ static ssize_t lru_gen_seq_write(struct file *file, const char __user *src,
197 set_task_reclaim_state(current, &sc.reclaim_state);
198 flags = memalloc_noreclaim_save();
199 blk_start_plug(&plug);
200 - if (!set_mm_walk(NULL)) {
201 + if (!set_mm_walk(NULL, true)) {
202 err = -ENOMEM;
203 goto done;
204 }
205 --
206 2.40.1
207