d60ddb9dccafed5e31d564f7963d2b2613d4c883
[openwrt/staging/neocturne.git] /
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 --- a/mm/vmscan.c
43 +++ b/mm/vmscan.c
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);
47
48 + if (!sc->may_swap)
49 + return 0;
50 +
51 if (!can_demote(pgdat->node_id, sc) &&
52 mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH)
53 return 0;
54 @@ -4223,7 +4226,7 @@ static void walk_mm(struct lruvec *lruve
55 } while (err == -EAGAIN);
56 }
57
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)
60 {
61 struct lru_gen_mm_walk *walk = current->reclaim_state->mm_walk;
62
63 @@ -4231,7 +4234,7 @@ static struct lru_gen_mm_walk *set_mm_wa
64 VM_WARN_ON_ONCE(walk);
65
66 walk = &pgdat->mm_walk;
67 - } else if (!pgdat && !walk) {
68 + } else if (!walk && force_alloc) {
69 VM_WARN_ON_ONCE(current_is_kswapd());
70
71 walk = kzalloc(sizeof(*walk), __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN);
72 @@ -4419,7 +4422,7 @@ static bool try_to_inc_max_seq(struct lr
73 goto done;
74 }
75
76 - walk = set_mm_walk(NULL);
77 + walk = set_mm_walk(NULL, true);
78 if (!walk) {
79 success = iterate_mm_list_nowalk(lruvec, max_seq);
80 goto done;
81 @@ -4488,8 +4491,6 @@ static bool lruvec_is_reclaimable(struct
82 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
83 DEFINE_MIN_SEQ(lruvec);
84
85 - VM_WARN_ON_ONCE(sc->memcg_low_reclaim);
86 -
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 @@ -4753,12 +4754,8 @@ static bool isolate_folio(struct lruvec
91 {
92 bool success;
93
94 - /* unmapping inhibited */
95 - if (!sc->may_unmap && folio_mapped(folio))
96 - return false;
97 -
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))))
103 return false;
104 @@ -4857,9 +4854,8 @@ static int scan_folios(struct lruvec *lr
105 __count_vm_events(PGSCAN_ANON + type, isolated);
106
107 /*
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
110 - * making progress.
111 + * There might not be eligible folios due to reclaim_idx. Check the
112 + * remaining to prevent livelock if it's not making progress.
113 */
114 return isolated || !remaining ? scanned : 0;
115 }
116 @@ -5119,8 +5115,7 @@ static long get_nr_to_scan(struct lruvec
117 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
118 DEFINE_MAX_SEQ(lruvec);
119
120 - if (mem_cgroup_below_min(memcg) ||
121 - (mem_cgroup_below_low(memcg) && !sc->memcg_low_reclaim))
122 + if (mem_cgroup_below_min(memcg))
123 return 0;
124
125 if (!should_run_aging(lruvec, max_seq, sc, can_swap, &nr_to_scan))
126 @@ -5148,17 +5143,14 @@ static bool try_to_shrink_lruvec(struct
127 long nr_to_scan;
128 unsigned long scanned = 0;
129 unsigned long nr_to_reclaim = get_nr_to_reclaim(sc);
130 + int swappiness = get_swappiness(lruvec, sc);
131 +
132 + /* clean file folios are more likely to exist */
133 + if (swappiness && !(sc->gfp_mask & __GFP_IO))
134 + swappiness = 1;
135
136 while (true) {
137 int delta;
138 - int swappiness;
139 -
140 - if (sc->may_swap)
141 - swappiness = get_swappiness(lruvec, sc);
142 - else if (!cgroup_reclaim(sc) && get_swappiness(lruvec, sc))
143 - swappiness = 1;
144 - else
145 - swappiness = 0;
146
147 nr_to_scan = get_nr_to_scan(lruvec, sc, swappiness);
148 if (nr_to_scan <= 0)
149 @@ -5289,12 +5281,13 @@ static void lru_gen_shrink_lruvec(struct
150 struct blk_plug plug;
151
152 VM_WARN_ON_ONCE(global_reclaim(sc));
153 + VM_WARN_ON_ONCE(!sc->may_writepage || !sc->may_unmap);
154
155 lru_add_drain();
156
157 blk_start_plug(&plug);
158
159 - set_mm_walk(lruvec_pgdat(lruvec));
160 + set_mm_walk(NULL, sc->proactive);
161
162 if (try_to_shrink_lruvec(lruvec, sc))
163 lru_gen_rotate_memcg(lruvec, MEMCG_LRU_YOUNG);
164 @@ -5350,11 +5343,19 @@ static void lru_gen_shrink_node(struct p
165
166 VM_WARN_ON_ONCE(!global_reclaim(sc));
167
168 + /*
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.
172 + */
173 + if (!sc->may_writepage || !sc->may_unmap)
174 + goto done;
175 +
176 lru_add_drain();
177
178 blk_start_plug(&plug);
179
180 - set_mm_walk(pgdat);
181 + set_mm_walk(pgdat, sc->proactive);
182
183 set_initial_priority(pgdat, sc);
184
185 @@ -5372,7 +5373,7 @@ static void lru_gen_shrink_node(struct p
186 clear_mm_walk();
187
188 blk_finish_plug(&plug);
189 -
190 +done:
191 /* kswapd should never fail */
192 pgdat->kswapd_failures = 0;
193 }
194 @@ -5944,7 +5945,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)) {
200 err = -ENOMEM;
201 goto done;
202 }