cb349abcdb6c6cbf7085c8d7477f4fbf2d8b46b2
[openwrt/openwrt.git] /
1 From f3c93d2e37a3c56593d7ccf4f4bcf1b58426fdd8 Mon Sep 17 00:00:00 2001
2 From: Yu Zhao <yuzhao@google.com>
3 Date: Wed, 21 Dec 2022 21:19:02 -0700
4 Subject: [PATCH 04/19] BACKPORT: mm: multi-gen LRU: remove aging fairness
5 safeguard
6
7 Recall that the aging produces the youngest generation: first it scans
8 for accessed folios and updates their gen counters; then it increments
9 lrugen->max_seq.
10
11 The current aging fairness safeguard for kswapd uses two passes to
12 ensure the fairness to multiple eligible memcgs. On the first pass,
13 which is shared with the eviction, it checks whether all eligible
14 memcgs are low on cold folios. If so, it requires a second pass, on
15 which it ages all those memcgs at the same time.
16
17 With memcg LRU, the aging, while ensuring eventual fairness, will run
18 when necessary. Therefore the current aging fairness safeguard for
19 kswapd will not be needed.
20
21 Note that memcg LRU only applies to global reclaim. For memcg reclaim,
22 the aging can be unfair to different memcgs, i.e., their
23 lrugen->max_seq can be incremented at different paces.
24
25 Link: https://lkml.kernel.org/r/20221222041905.2431096-5-yuzhao@google.com
26 Signed-off-by: Yu Zhao <yuzhao@google.com>
27 Cc: Johannes Weiner <hannes@cmpxchg.org>
28 Cc: Jonathan Corbet <corbet@lwn.net>
29 Cc: Michael Larabel <Michael@MichaelLarabel.com>
30 Cc: Michal Hocko <mhocko@kernel.org>
31 Cc: Mike Rapoport <rppt@kernel.org>
32 Cc: Roman Gushchin <roman.gushchin@linux.dev>
33 Cc: Suren Baghdasaryan <surenb@google.com>
34 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
35 Bug: 274865848
36 (cherry picked from commit 7348cc91821b0cb24dfb00e578047f68299a50ab)
37 [TJ: Resolved conflicts with older function signatures for
38 min_cgroup_below_min / min_cgroup_below_low]
39 Change-Id: I6e36ecfbaaefbc0a56d9a9d5d7cbe404ed7f57a5
40 Signed-off-by: T.J. Mercier <tjmercier@google.com>
41 ---
42 mm/vmscan.c | 126 ++++++++++++++++++++++++----------------------------
43 1 file changed, 59 insertions(+), 67 deletions(-)
44
45 --- a/mm/vmscan.c
46 +++ b/mm/vmscan.c
47 @@ -136,7 +136,6 @@ struct scan_control {
48
49 #ifdef CONFIG_LRU_GEN
50 /* help kswapd make better choices among multiple memcgs */
51 - unsigned int memcgs_need_aging:1;
52 unsigned long last_reclaimed;
53 #endif
54
55 @@ -4457,7 +4456,7 @@ done:
56 return true;
57 }
58
59 -static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, unsigned long *min_seq,
60 +static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq,
61 struct scan_control *sc, bool can_swap, unsigned long *nr_to_scan)
62 {
63 int gen, type, zone;
64 @@ -4466,6 +4465,13 @@ static bool should_run_aging(struct lruv
65 unsigned long total = 0;
66 struct lru_gen_folio *lrugen = &lruvec->lrugen;
67 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
68 + DEFINE_MIN_SEQ(lruvec);
69 +
70 + /* whether this lruvec is completely out of cold folios */
71 + if (min_seq[!can_swap] + MIN_NR_GENS > max_seq) {
72 + *nr_to_scan = 0;
73 + return true;
74 + }
75
76 for (type = !can_swap; type < ANON_AND_FILE; type++) {
77 unsigned long seq;
78 @@ -4494,8 +4500,6 @@ static bool should_run_aging(struct lruv
79 * stalls when the number of generations reaches MIN_NR_GENS. Hence, the
80 * ideal number of generations is MIN_NR_GENS+1.
81 */
82 - if (min_seq[!can_swap] + MIN_NR_GENS > max_seq)
83 - return true;
84 if (min_seq[!can_swap] + MIN_NR_GENS < max_seq)
85 return false;
86
87 @@ -4514,40 +4518,54 @@ static bool should_run_aging(struct lruv
88 return false;
89 }
90
91 -static bool age_lruvec(struct lruvec *lruvec, struct scan_control *sc, unsigned long min_ttl)
92 +static bool lruvec_is_sizable(struct lruvec *lruvec, struct scan_control *sc)
93 {
94 - bool need_aging;
95 - unsigned long nr_to_scan;
96 - int swappiness = get_swappiness(lruvec, sc);
97 + int gen, type, zone;
98 + unsigned long total = 0;
99 + bool can_swap = get_swappiness(lruvec, sc);
100 + struct lru_gen_folio *lrugen = &lruvec->lrugen;
101 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
102 DEFINE_MAX_SEQ(lruvec);
103 DEFINE_MIN_SEQ(lruvec);
104
105 - VM_WARN_ON_ONCE(sc->memcg_low_reclaim);
106 + for (type = !can_swap; type < ANON_AND_FILE; type++) {
107 + unsigned long seq;
108
109 - mem_cgroup_calculate_protection(NULL, memcg);
110 + for (seq = min_seq[type]; seq <= max_seq; seq++) {
111 + gen = lru_gen_from_seq(seq);
112
113 - if (mem_cgroup_below_min(memcg))
114 - return false;
115 + for (zone = 0; zone < MAX_NR_ZONES; zone++)
116 + total += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L);
117 + }
118 + }
119
120 - need_aging = should_run_aging(lruvec, max_seq, min_seq, sc, swappiness, &nr_to_scan);
121 + /* whether the size is big enough to be helpful */
122 + return mem_cgroup_online(memcg) ? (total >> sc->priority) : total;
123 +}
124
125 - if (min_ttl) {
126 - int gen = lru_gen_from_seq(min_seq[LRU_GEN_FILE]);
127 - unsigned long birth = READ_ONCE(lruvec->lrugen.timestamps[gen]);
128 +static bool lruvec_is_reclaimable(struct lruvec *lruvec, struct scan_control *sc,
129 + unsigned long min_ttl)
130 +{
131 + int gen;
132 + unsigned long birth;
133 + struct mem_cgroup *memcg = lruvec_memcg(lruvec);
134 + DEFINE_MIN_SEQ(lruvec);
135
136 - if (time_is_after_jiffies(birth + min_ttl))
137 - return false;
138 + VM_WARN_ON_ONCE(sc->memcg_low_reclaim);
139
140 - /* the size is likely too small to be helpful */
141 - if (!nr_to_scan && sc->priority != DEF_PRIORITY)
142 - return false;
143 - }
144 + /* see the comment on lru_gen_folio */
145 + gen = lru_gen_from_seq(min_seq[LRU_GEN_FILE]);
146 + birth = READ_ONCE(lruvec->lrugen.timestamps[gen]);
147
148 - if (need_aging)
149 - try_to_inc_max_seq(lruvec, max_seq, sc, swappiness, false);
150 + if (time_is_after_jiffies(birth + min_ttl))
151 + return false;
152
153 - return true;
154 + if (!lruvec_is_sizable(lruvec, sc))
155 + return false;
156 +
157 + mem_cgroup_calculate_protection(NULL, memcg);
158 +
159 + return !mem_cgroup_below_min(memcg);
160 }
161
162 /* to protect the working set of the last N jiffies */
163 @@ -4556,46 +4574,32 @@ static unsigned long lru_gen_min_ttl __r
164 static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc)
165 {
166 struct mem_cgroup *memcg;
167 - bool success = false;
168 unsigned long min_ttl = READ_ONCE(lru_gen_min_ttl);
169
170 VM_WARN_ON_ONCE(!current_is_kswapd());
171
172 sc->last_reclaimed = sc->nr_reclaimed;
173
174 - /*
175 - * To reduce the chance of going into the aging path, which can be
176 - * costly, optimistically skip it if the flag below was cleared in the
177 - * eviction path. This improves the overall performance when multiple
178 - * memcgs are available.
179 - */
180 - if (!sc->memcgs_need_aging) {
181 - sc->memcgs_need_aging = true;
182 + /* check the order to exclude compaction-induced reclaim */
183 + if (!min_ttl || sc->order || sc->priority == DEF_PRIORITY)
184 return;
185 - }
186 -
187 - set_mm_walk(pgdat);
188
189 memcg = mem_cgroup_iter(NULL, NULL, NULL);
190 do {
191 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
192
193 - if (age_lruvec(lruvec, sc, min_ttl))
194 - success = true;
195 + if (lruvec_is_reclaimable(lruvec, sc, min_ttl)) {
196 + mem_cgroup_iter_break(NULL, memcg);
197 + return;
198 + }
199
200 cond_resched();
201 } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)));
202
203 - clear_mm_walk();
204 -
205 - /* check the order to exclude compaction-induced reclaim */
206 - if (success || !min_ttl || sc->order)
207 - return;
208 -
209 /*
210 * The main goal is to OOM kill if every generation from all memcgs is
211 * younger than min_ttl. However, another possibility is all memcgs are
212 - * either below min or empty.
213 + * either too small or below min.
214 */
215 if (mutex_trylock(&oom_lock)) {
216 struct oom_control oc = {
217 @@ -5113,33 +5117,27 @@ retry:
218 * reclaim.
219 */
220 static unsigned long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc,
221 - bool can_swap, bool *need_aging)
222 + bool can_swap)
223 {
224 unsigned long nr_to_scan;
225 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
226 DEFINE_MAX_SEQ(lruvec);
227 - DEFINE_MIN_SEQ(lruvec);
228
229 if (mem_cgroup_below_min(memcg) ||
230 (mem_cgroup_below_low(memcg) && !sc->memcg_low_reclaim))
231 return 0;
232
233 - *need_aging = should_run_aging(lruvec, max_seq, min_seq, sc, can_swap, &nr_to_scan);
234 - if (!*need_aging)
235 + if (!should_run_aging(lruvec, max_seq, sc, can_swap, &nr_to_scan))
236 return nr_to_scan;
237
238 /* skip the aging path at the default priority */
239 if (sc->priority == DEF_PRIORITY)
240 - goto done;
241 + return nr_to_scan;
242
243 - /* leave the work to lru_gen_age_node() */
244 - if (current_is_kswapd())
245 - return 0;
246 + try_to_inc_max_seq(lruvec, max_seq, sc, can_swap, false);
247
248 - if (try_to_inc_max_seq(lruvec, max_seq, sc, can_swap, false))
249 - return nr_to_scan;
250 -done:
251 - return min_seq[!can_swap] + MIN_NR_GENS <= max_seq ? nr_to_scan : 0;
252 + /* skip this lruvec as it's low on cold folios */
253 + return 0;
254 }
255
256 static unsigned long get_nr_to_reclaim(struct scan_control *sc)
257 @@ -5158,9 +5156,7 @@ static unsigned long get_nr_to_reclaim(s
258 static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
259 {
260 struct blk_plug plug;
261 - bool need_aging = false;
262 unsigned long scanned = 0;
263 - unsigned long reclaimed = sc->nr_reclaimed;
264 unsigned long nr_to_reclaim = get_nr_to_reclaim(sc);
265
266 lru_add_drain();
267 @@ -5181,13 +5177,13 @@ static void lru_gen_shrink_lruvec(struct
268 else
269 swappiness = 0;
270
271 - nr_to_scan = get_nr_to_scan(lruvec, sc, swappiness, &need_aging);
272 + nr_to_scan = get_nr_to_scan(lruvec, sc, swappiness);
273 if (!nr_to_scan)
274 - goto done;
275 + break;
276
277 delta = evict_folios(lruvec, sc, swappiness);
278 if (!delta)
279 - goto done;
280 + break;
281
282 scanned += delta;
283 if (scanned >= nr_to_scan)
284 @@ -5199,10 +5195,6 @@ static void lru_gen_shrink_lruvec(struct
285 cond_resched();
286 }
287
288 - /* see the comment in lru_gen_age_node() */
289 - if (sc->nr_reclaimed - reclaimed >= MIN_LRU_BATCH && !need_aging)
290 - sc->memcgs_need_aging = false;
291 -done:
292 clear_mm_walk();
293
294 blk_finish_plug(&plug);