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
6 Among the flags in scan_control:
7 1. sc->may_swap, which indicates swap constraint due to memsw.max, is
9 2. sc->proactive, which indicates reclaim by memory.reclaim, may not
10 opportunistically skip the aging path, since it is considered less
12 3. !(sc->gfp_mask & __GFP_IO), which indicates IO constraint, lowers
13 swappiness to prioritize file LRU, since clean file folios are more
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.
20 The rest are handled by the existing code.
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>
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>
39 mm/vmscan.c | 55 +++++++++++++++++++++++++++--------------------------
40 1 file changed, 28 insertions(+), 27 deletions(-)
44 @@ -3185,6 +3185,9 @@ static int get_swappiness(struct lruvec
45 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
46 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
51 if (!can_demote(pgdat->node_id, sc) &&
52 mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH)
54 @@ -4223,7 +4226,7 @@ static void walk_mm(struct lruvec *lruve
55 } while (err == -EAGAIN);
58 -static struct lru_gen_mm_walk *set_mm_walk(struct pglist_data *pgdat)
59 +static struct lru_gen_mm_walk *set_mm_walk(struct pglist_data *pgdat, bool force_alloc)
61 struct lru_gen_mm_walk *walk = current->reclaim_state->mm_walk;
63 @@ -4231,7 +4234,7 @@ static struct lru_gen_mm_walk *set_mm_wa
64 VM_WARN_ON_ONCE(walk);
66 walk = &pgdat->mm_walk;
67 - } else if (!pgdat && !walk) {
68 + } else if (!walk && force_alloc) {
69 VM_WARN_ON_ONCE(current_is_kswapd());
71 walk = kzalloc(sizeof(*walk), __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN);
72 @@ -4417,7 +4420,7 @@ static bool try_to_inc_max_seq(struct lr
76 - walk = set_mm_walk(NULL);
77 + walk = set_mm_walk(NULL, true);
79 success = iterate_mm_list_nowalk(lruvec, max_seq);
81 @@ -4486,8 +4489,6 @@ static bool lruvec_is_reclaimable(struct
82 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
83 DEFINE_MIN_SEQ(lruvec);
85 - VM_WARN_ON_ONCE(sc->memcg_low_reclaim);
87 /* see the comment on lru_gen_folio */
88 gen = lru_gen_from_seq(min_seq[LRU_GEN_FILE]);
89 birth = READ_ONCE(lruvec->lrugen.timestamps[gen]);
90 @@ -4743,12 +4744,8 @@ static bool isolate_folio(struct lruvec
94 - /* unmapping inhibited */
95 - if (!sc->may_unmap && folio_mapped(folio))
98 /* swapping inhibited */
99 - if (!(sc->may_writepage && (sc->gfp_mask & __GFP_IO)) &&
100 + if (!(sc->gfp_mask & __GFP_IO) &&
101 (folio_test_dirty(folio) ||
102 (folio_test_anon(folio) && !folio_test_swapcache(folio))))
104 @@ -4845,9 +4842,8 @@ static int scan_folios(struct lruvec *lr
105 __count_vm_events(PGSCAN_ANON + type, isolated);
108 - * There might not be eligible pages due to reclaim_idx, may_unmap and
109 - * may_writepage. Check the remaining to prevent livelock if it's not
111 + * There might not be eligible folios due to reclaim_idx. Check the
112 + * remaining to prevent livelock if it's not making progress.
114 return isolated || !remaining ? scanned : 0;
116 @@ -5107,8 +5103,7 @@ static long get_nr_to_scan(struct lruvec
117 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
118 DEFINE_MAX_SEQ(lruvec);
120 - if (mem_cgroup_below_min(memcg) ||
121 - (mem_cgroup_below_low(memcg) && !sc->memcg_low_reclaim))
122 + if (mem_cgroup_below_min(memcg))
125 if (!should_run_aging(lruvec, max_seq, sc, can_swap, &nr_to_scan))
126 @@ -5136,17 +5131,14 @@ static bool try_to_shrink_lruvec(struct
128 unsigned long scanned = 0;
129 unsigned long nr_to_reclaim = get_nr_to_reclaim(sc);
130 + int swappiness = get_swappiness(lruvec, sc);
132 + /* clean file folios are more likely to exist */
133 + if (swappiness && !(sc->gfp_mask & __GFP_IO))
141 - swappiness = get_swappiness(lruvec, sc);
142 - else if (!cgroup_reclaim(sc) && get_swappiness(lruvec, sc))
147 nr_to_scan = get_nr_to_scan(lruvec, sc, swappiness);
149 @@ -5277,12 +5269,13 @@ static void lru_gen_shrink_lruvec(struct
150 struct blk_plug plug;
152 VM_WARN_ON_ONCE(global_reclaim(sc));
153 + VM_WARN_ON_ONCE(!sc->may_writepage || !sc->may_unmap);
157 blk_start_plug(&plug);
159 - set_mm_walk(lruvec_pgdat(lruvec));
160 + set_mm_walk(NULL, sc->proactive);
162 if (try_to_shrink_lruvec(lruvec, sc))
163 lru_gen_rotate_memcg(lruvec, MEMCG_LRU_YOUNG);
164 @@ -5338,11 +5331,19 @@ static void lru_gen_shrink_node(struct p
166 VM_WARN_ON_ONCE(!global_reclaim(sc));
169 + * Unmapped clean folios are already prioritized. Scanning for more of
170 + * them is likely futile and can cause high reclaim latency when there
171 + * is a large number of memcgs.
173 + if (!sc->may_writepage || !sc->may_unmap)
178 blk_start_plug(&plug);
180 - set_mm_walk(pgdat);
181 + set_mm_walk(pgdat, sc->proactive);
183 set_initial_priority(pgdat, sc);
185 @@ -5360,7 +5361,7 @@ static void lru_gen_shrink_node(struct p
188 blk_finish_plug(&plug);
191 /* kswapd should never fail */
192 pgdat->kswapd_failures = 0;
194 @@ -5932,7 +5933,7 @@ static ssize_t lru_gen_seq_write(struct
195 set_task_reclaim_state(current, &sc.reclaim_state);
196 flags = memalloc_noreclaim_save();
197 blk_start_plug(&plug);
198 - if (!set_mm_walk(NULL)) {
199 + if (!set_mm_walk(NULL, true)) {