d214898a6d07f2ab23207d86c1ad7ee5962fa0e0
[openwrt/staging/blogic.git] /
1 From ce45f1c4b32cf69b166f56ef5bc6c761e06ed4e5 Mon Sep 17 00:00:00 2001
2 From: Yu Zhao <yuzhao@google.com>
3 Date: Wed, 21 Dec 2022 21:19:01 -0700
4 Subject: [PATCH 23/29] mm: multi-gen LRU: remove eviction fairness safeguard
5
6 Recall that the eviction consumes the oldest generation: first it
7 bucket-sorts folios whose gen counters were updated by the aging and
8 reclaims the rest; then it increments lrugen->min_seq.
9
10 The current eviction fairness safeguard for global reclaim has a
11 dilemma: when there are multiple eligible memcgs, should it continue
12 or stop upon meeting the reclaim goal? If it continues, it overshoots
13 and increases direct reclaim latency; if it stops, it loses fairness
14 between memcgs it has taken memory away from and those it has yet to.
15
16 With memcg LRU, the eviction, while ensuring eventual fairness, will
17 stop upon meeting its goal. Therefore the current eviction fairness
18 safeguard for global reclaim will not be needed.
19
20 Note that memcg LRU only applies to global reclaim. For memcg reclaim,
21 the eviction will continue, even if it is overshooting. This becomes
22 unconditional due to code simplification.
23
24 Link: https://lkml.kernel.org/r/20221222041905.2431096-4-yuzhao@google.com
25 Signed-off-by: Yu Zhao <yuzhao@google.com>
26 Cc: Johannes Weiner <hannes@cmpxchg.org>
27 Cc: Jonathan Corbet <corbet@lwn.net>
28 Cc: Michael Larabel <Michael@MichaelLarabel.com>
29 Cc: Michal Hocko <mhocko@kernel.org>
30 Cc: Mike Rapoport <rppt@kernel.org>
31 Cc: Roman Gushchin <roman.gushchin@linux.dev>
32 Cc: Suren Baghdasaryan <surenb@google.com>
33 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
34 ---
35 mm/vmscan.c | 82 +++++++++++++++--------------------------------------
36 1 file changed, 23 insertions(+), 59 deletions(-)
37
38 --- a/mm/vmscan.c
39 +++ b/mm/vmscan.c
40 @@ -448,6 +448,11 @@ static bool cgroup_reclaim(struct scan_c
41 return sc->target_mem_cgroup;
42 }
43
44 +static bool global_reclaim(struct scan_control *sc)
45 +{
46 + return !sc->target_mem_cgroup || mem_cgroup_is_root(sc->target_mem_cgroup);
47 +}
48 +
49 /**
50 * writeback_throttling_sane - is the usual dirty throttling mechanism available?
51 * @sc: scan_control in question
52 @@ -498,6 +503,11 @@ static bool cgroup_reclaim(struct scan_c
53 return false;
54 }
55
56 +static bool global_reclaim(struct scan_control *sc)
57 +{
58 + return true;
59 +}
60 +
61 static bool writeback_throttling_sane(struct scan_control *sc)
62 {
63 return true;
64 @@ -4996,8 +5006,7 @@ static int isolate_folios(struct lruvec
65 return scanned;
66 }
67
68 -static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness,
69 - bool *need_swapping)
70 +static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness)
71 {
72 int type;
73 int scanned;
74 @@ -5086,9 +5095,6 @@ retry:
75 goto retry;
76 }
77
78 - if (need_swapping && type == LRU_GEN_ANON)
79 - *need_swapping = true;
80 -
81 return scanned;
82 }
83
84 @@ -5127,67 +5133,26 @@ done:
85 return min_seq[!can_swap] + MIN_NR_GENS <= max_seq ? nr_to_scan : 0;
86 }
87
88 -static bool should_abort_scan(struct lruvec *lruvec, unsigned long seq,
89 - struct scan_control *sc, bool need_swapping)
90 +static unsigned long get_nr_to_reclaim(struct scan_control *sc)
91 {
92 - int i;
93 - DEFINE_MAX_SEQ(lruvec);
94 -
95 - if (!current_is_kswapd()) {
96 - /* age each memcg at most once to ensure fairness */
97 - if (max_seq - seq > 1)
98 - return true;
99 -
100 - /* over-swapping can increase allocation latency */
101 - if (sc->nr_reclaimed >= sc->nr_to_reclaim && need_swapping)
102 - return true;
103 -
104 - /* give this thread a chance to exit and free its memory */
105 - if (fatal_signal_pending(current)) {
106 - sc->nr_reclaimed += MIN_LRU_BATCH;
107 - return true;
108 - }
109 -
110 - if (cgroup_reclaim(sc))
111 - return false;
112 - } else if (sc->nr_reclaimed - sc->last_reclaimed < sc->nr_to_reclaim)
113 - return false;
114 -
115 - /* keep scanning at low priorities to ensure fairness */
116 - if (sc->priority > DEF_PRIORITY - 2)
117 - return false;
118 -
119 - /*
120 - * A minimum amount of work was done under global memory pressure. For
121 - * kswapd, it may be overshooting. For direct reclaim, the allocation
122 - * may succeed if all suitable zones are somewhat safe. In either case,
123 - * it's better to stop now, and restart later if necessary.
124 - */
125 - for (i = 0; i <= sc->reclaim_idx; i++) {
126 - unsigned long wmark;
127 - struct zone *zone = lruvec_pgdat(lruvec)->node_zones + i;
128 -
129 - if (!managed_zone(zone))
130 - continue;
131 -
132 - wmark = current_is_kswapd() ? high_wmark_pages(zone) : low_wmark_pages(zone);
133 - if (wmark > zone_page_state(zone, NR_FREE_PAGES))
134 - return false;
135 - }
136 + /* don't abort memcg reclaim to ensure fairness */
137 + if (!global_reclaim(sc))
138 + return -1;
139
140 - sc->nr_reclaimed += MIN_LRU_BATCH;
141 + /* discount the previous progress for kswapd */
142 + if (current_is_kswapd())
143 + return sc->nr_to_reclaim + sc->last_reclaimed;
144
145 - return true;
146 + return max(sc->nr_to_reclaim, compact_gap(sc->order));
147 }
148
149 static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
150 {
151 struct blk_plug plug;
152 bool need_aging = false;
153 - bool need_swapping = false;
154 unsigned long scanned = 0;
155 unsigned long reclaimed = sc->nr_reclaimed;
156 - DEFINE_MAX_SEQ(lruvec);
157 + unsigned long nr_to_reclaim = get_nr_to_reclaim(sc);
158
159 lru_add_drain();
160
161 @@ -5211,7 +5176,7 @@ static void lru_gen_shrink_lruvec(struct
162 if (!nr_to_scan)
163 goto done;
164
165 - delta = evict_folios(lruvec, sc, swappiness, &need_swapping);
166 + delta = evict_folios(lruvec, sc, swappiness);
167 if (!delta)
168 goto done;
169
170 @@ -5219,7 +5184,7 @@ static void lru_gen_shrink_lruvec(struct
171 if (scanned >= nr_to_scan)
172 break;
173
174 - if (should_abort_scan(lruvec, max_seq, sc, need_swapping))
175 + if (sc->nr_reclaimed >= nr_to_reclaim)
176 break;
177
178 cond_resched();
179 @@ -5669,7 +5634,7 @@ static int run_eviction(struct lruvec *l
180 if (sc->nr_reclaimed >= nr_to_reclaim)
181 return 0;
182
183 - if (!evict_folios(lruvec, sc, swappiness, NULL))
184 + if (!evict_folios(lruvec, sc, swappiness))
185 return 0;
186
187 cond_resched();