]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
1da177e4 LT |
3 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds |
4 | * | |
5 | * Swap reorganised 29.12.95, Stephen Tweedie. | |
6 | * kswapd added: 7.1.96 sct | |
7 | * Removed kswapd_ctl limits, and swap out as many pages as needed | |
8 | * to bring the system back to freepages.high: 2.4.97, Rik van Riel. | |
9 | * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com). | |
10 | * Multiqueue VM started 5.8.00, Rik van Riel. | |
11 | */ | |
12 | ||
b1de0d13 MH |
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
14 | ||
1da177e4 | 15 | #include <linux/mm.h> |
5b3cc15a | 16 | #include <linux/sched/mm.h> |
1da177e4 | 17 | #include <linux/module.h> |
5a0e3ad6 | 18 | #include <linux/gfp.h> |
1da177e4 LT |
19 | #include <linux/kernel_stat.h> |
20 | #include <linux/swap.h> | |
21 | #include <linux/pagemap.h> | |
22 | #include <linux/init.h> | |
23 | #include <linux/highmem.h> | |
70ddf637 | 24 | #include <linux/vmpressure.h> |
e129b5c2 | 25 | #include <linux/vmstat.h> |
1da177e4 LT |
26 | #include <linux/file.h> |
27 | #include <linux/writeback.h> | |
28 | #include <linux/blkdev.h> | |
07f67a8d | 29 | #include <linux/buffer_head.h> /* for buffer_heads_over_limit */ |
1da177e4 | 30 | #include <linux/mm_inline.h> |
1da177e4 LT |
31 | #include <linux/backing-dev.h> |
32 | #include <linux/rmap.h> | |
33 | #include <linux/topology.h> | |
34 | #include <linux/cpu.h> | |
35 | #include <linux/cpuset.h> | |
3e7d3449 | 36 | #include <linux/compaction.h> |
1da177e4 | 37 | #include <linux/notifier.h> |
cf2e309e | 38 | #include <linux/mutex.h> |
248a0301 | 39 | #include <linux/delay.h> |
3218ae14 | 40 | #include <linux/kthread.h> |
7dfb7103 | 41 | #include <linux/freezer.h> |
66e1707b | 42 | #include <linux/memcontrol.h> |
26aa2d19 | 43 | #include <linux/migrate.h> |
873b4771 | 44 | #include <linux/delayacct.h> |
af936a16 | 45 | #include <linux/sysctl.h> |
91952440 | 46 | #include <linux/memory-tiers.h> |
929bea7c | 47 | #include <linux/oom.h> |
64e3d12f | 48 | #include <linux/pagevec.h> |
268bb0ce | 49 | #include <linux/prefetch.h> |
b1de0d13 | 50 | #include <linux/printk.h> |
f9fe48be | 51 | #include <linux/dax.h> |
eb414681 | 52 | #include <linux/psi.h> |
bd74fdae YZ |
53 | #include <linux/pagewalk.h> |
54 | #include <linux/shmem_fs.h> | |
354ed597 | 55 | #include <linux/ctype.h> |
d6c3af7d | 56 | #include <linux/debugfs.h> |
57e9cc50 | 57 | #include <linux/khugepaged.h> |
e4dde56c YZ |
58 | #include <linux/rculist_nulls.h> |
59 | #include <linux/random.h> | |
f95bdb70 | 60 | #include <linux/srcu.h> |
1da177e4 LT |
61 | |
62 | #include <asm/tlbflush.h> | |
63 | #include <asm/div64.h> | |
64 | ||
65 | #include <linux/swapops.h> | |
117aad1e | 66 | #include <linux/balloon_compaction.h> |
c574bbe9 | 67 | #include <linux/sched/sysctl.h> |
1da177e4 | 68 | |
0f8053a5 | 69 | #include "internal.h" |
014bb1de | 70 | #include "swap.h" |
0f8053a5 | 71 | |
33906bc5 MG |
72 | #define CREATE_TRACE_POINTS |
73 | #include <trace/events/vmscan.h> | |
74 | ||
1da177e4 | 75 | struct scan_control { |
22fba335 KM |
76 | /* How many pages shrink_list() should reclaim */ |
77 | unsigned long nr_to_reclaim; | |
78 | ||
ee814fe2 JW |
79 | /* |
80 | * Nodemask of nodes allowed by the caller. If NULL, all nodes | |
81 | * are scanned. | |
82 | */ | |
83 | nodemask_t *nodemask; | |
9e3b2f8c | 84 | |
f16015fb JW |
85 | /* |
86 | * The memory cgroup that hit its limit and as a result is the | |
87 | * primary target of this reclaim invocation. | |
88 | */ | |
89 | struct mem_cgroup *target_mem_cgroup; | |
66e1707b | 90 | |
7cf111bc JW |
91 | /* |
92 | * Scan pressure balancing between anon and file LRUs | |
93 | */ | |
94 | unsigned long anon_cost; | |
95 | unsigned long file_cost; | |
96 | ||
49fd9b6d | 97 | /* Can active folios be deactivated as part of reclaim? */ |
b91ac374 JW |
98 | #define DEACTIVATE_ANON 1 |
99 | #define DEACTIVATE_FILE 2 | |
100 | unsigned int may_deactivate:2; | |
101 | unsigned int force_deactivate:1; | |
102 | unsigned int skipped_deactivate:1; | |
103 | ||
1276ad68 | 104 | /* Writepage batching in laptop mode; RECLAIM_WRITE */ |
ee814fe2 JW |
105 | unsigned int may_writepage:1; |
106 | ||
49fd9b6d | 107 | /* Can mapped folios be reclaimed? */ |
ee814fe2 JW |
108 | unsigned int may_unmap:1; |
109 | ||
49fd9b6d | 110 | /* Can folios be swapped as part of reclaim? */ |
ee814fe2 JW |
111 | unsigned int may_swap:1; |
112 | ||
73b73bac YA |
113 | /* Proactive reclaim invoked by userspace through memory.reclaim */ |
114 | unsigned int proactive:1; | |
115 | ||
d6622f63 | 116 | /* |
f56ce412 JW |
117 | * Cgroup memory below memory.low is protected as long as we |
118 | * don't threaten to OOM. If any cgroup is reclaimed at | |
119 | * reduced force or passed over entirely due to its memory.low | |
120 | * setting (memcg_low_skipped), and nothing is reclaimed as a | |
121 | * result, then go back for one more cycle that reclaims the protected | |
122 | * memory (memcg_low_reclaim) to avert OOM. | |
d6622f63 YX |
123 | */ |
124 | unsigned int memcg_low_reclaim:1; | |
125 | unsigned int memcg_low_skipped:1; | |
241994ed | 126 | |
ee814fe2 JW |
127 | unsigned int hibernation_mode:1; |
128 | ||
129 | /* One of the zones is ready for compaction */ | |
130 | unsigned int compaction_ready:1; | |
131 | ||
b91ac374 JW |
132 | /* There is easily reclaimable cold cache in the current node */ |
133 | unsigned int cache_trim_mode:1; | |
134 | ||
49fd9b6d | 135 | /* The file folios on the current node are dangerously low */ |
53138cea JW |
136 | unsigned int file_is_tiny:1; |
137 | ||
26aa2d19 DH |
138 | /* Always discard instead of demoting to lower tier memory */ |
139 | unsigned int no_demotion:1; | |
140 | ||
bb451fdf GT |
141 | /* Allocation order */ |
142 | s8 order; | |
143 | ||
144 | /* Scan (total_size >> priority) pages at once */ | |
145 | s8 priority; | |
146 | ||
49fd9b6d | 147 | /* The highest zone to isolate folios for reclaim from */ |
bb451fdf GT |
148 | s8 reclaim_idx; |
149 | ||
150 | /* This context's GFP mask */ | |
151 | gfp_t gfp_mask; | |
152 | ||
ee814fe2 JW |
153 | /* Incremented by the number of inactive pages that were scanned */ |
154 | unsigned long nr_scanned; | |
155 | ||
156 | /* Number of pages freed so far during a call to shrink_zones() */ | |
157 | unsigned long nr_reclaimed; | |
d108c772 AR |
158 | |
159 | struct { | |
160 | unsigned int dirty; | |
161 | unsigned int unqueued_dirty; | |
162 | unsigned int congested; | |
163 | unsigned int writeback; | |
164 | unsigned int immediate; | |
165 | unsigned int file_taken; | |
166 | unsigned int taken; | |
167 | } nr; | |
e5ca8071 YS |
168 | |
169 | /* for recording the reclaimed slab by now */ | |
170 | struct reclaim_state reclaim_state; | |
1da177e4 LT |
171 | }; |
172 | ||
1da177e4 | 173 | #ifdef ARCH_HAS_PREFETCHW |
166e3d32 | 174 | #define prefetchw_prev_lru_folio(_folio, _base, _field) \ |
1da177e4 | 175 | do { \ |
166e3d32 MWO |
176 | if ((_folio)->lru.prev != _base) { \ |
177 | struct folio *prev; \ | |
1da177e4 | 178 | \ |
166e3d32 | 179 | prev = lru_to_folio(&(_folio->lru)); \ |
1da177e4 LT |
180 | prefetchw(&prev->_field); \ |
181 | } \ | |
182 | } while (0) | |
183 | #else | |
166e3d32 | 184 | #define prefetchw_prev_lru_folio(_folio, _base, _field) do { } while (0) |
1da177e4 LT |
185 | #endif |
186 | ||
187 | /* | |
c843966c | 188 | * From 0 .. 200. Higher means more swappy. |
1da177e4 LT |
189 | */ |
190 | int vm_swappiness = 60; | |
1da177e4 | 191 | |
0a432dcb YS |
192 | static void set_task_reclaim_state(struct task_struct *task, |
193 | struct reclaim_state *rs) | |
194 | { | |
195 | /* Check for an overwrite */ | |
196 | WARN_ON_ONCE(rs && task->reclaim_state); | |
197 | ||
198 | /* Check for the nulling of an already-nulled member */ | |
199 | WARN_ON_ONCE(!rs && !task->reclaim_state); | |
200 | ||
201 | task->reclaim_state = rs; | |
202 | } | |
203 | ||
5035ebc6 | 204 | LIST_HEAD(shrinker_list); |
cf2e309e | 205 | DEFINE_MUTEX(shrinker_mutex); |
f95bdb70 | 206 | DEFINE_SRCU(shrinker_srcu); |
475733dd | 207 | static atomic_t shrinker_srcu_generation = ATOMIC_INIT(0); |
1da177e4 | 208 | |
0a432dcb | 209 | #ifdef CONFIG_MEMCG |
a2fb1261 | 210 | static int shrinker_nr_max; |
2bfd3637 | 211 | |
3c6f17e6 | 212 | /* The shrinker_info is expanded in a batch of BITS_PER_LONG */ |
a2fb1261 YS |
213 | static inline int shrinker_map_size(int nr_items) |
214 | { | |
215 | return (DIV_ROUND_UP(nr_items, BITS_PER_LONG) * sizeof(unsigned long)); | |
216 | } | |
2bfd3637 | 217 | |
3c6f17e6 YS |
218 | static inline int shrinker_defer_size(int nr_items) |
219 | { | |
220 | return (round_up(nr_items, BITS_PER_LONG) * sizeof(atomic_long_t)); | |
221 | } | |
222 | ||
468ab843 YS |
223 | static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg, |
224 | int nid) | |
225 | { | |
caa05325 QZ |
226 | return srcu_dereference_check(memcg->nodeinfo[nid]->shrinker_info, |
227 | &shrinker_srcu, | |
cf2e309e | 228 | lockdep_is_held(&shrinker_mutex)); |
caa05325 QZ |
229 | } |
230 | ||
231 | static struct shrinker_info *shrinker_info_srcu(struct mem_cgroup *memcg, | |
232 | int nid) | |
233 | { | |
234 | return srcu_dereference(memcg->nodeinfo[nid]->shrinker_info, | |
235 | &shrinker_srcu); | |
236 | } | |
237 | ||
238 | static void free_shrinker_info_rcu(struct rcu_head *head) | |
239 | { | |
240 | kvfree(container_of(head, struct shrinker_info, rcu)); | |
468ab843 YS |
241 | } |
242 | ||
e4262c4f | 243 | static int expand_one_shrinker_info(struct mem_cgroup *memcg, |
3c6f17e6 | 244 | int map_size, int defer_size, |
42c9db39 QZ |
245 | int old_map_size, int old_defer_size, |
246 | int new_nr_max) | |
2bfd3637 | 247 | { |
e4262c4f | 248 | struct shrinker_info *new, *old; |
2bfd3637 YS |
249 | struct mem_cgroup_per_node *pn; |
250 | int nid; | |
3c6f17e6 | 251 | int size = map_size + defer_size; |
2bfd3637 | 252 | |
2bfd3637 YS |
253 | for_each_node(nid) { |
254 | pn = memcg->nodeinfo[nid]; | |
468ab843 | 255 | old = shrinker_info_protected(memcg, nid); |
2bfd3637 YS |
256 | /* Not yet online memcg */ |
257 | if (!old) | |
258 | return 0; | |
259 | ||
42c9db39 QZ |
260 | /* Already expanded this shrinker_info */ |
261 | if (new_nr_max <= old->map_nr_max) | |
262 | continue; | |
263 | ||
2bfd3637 YS |
264 | new = kvmalloc_node(sizeof(*new) + size, GFP_KERNEL, nid); |
265 | if (!new) | |
266 | return -ENOMEM; | |
267 | ||
3c6f17e6 YS |
268 | new->nr_deferred = (atomic_long_t *)(new + 1); |
269 | new->map = (void *)new->nr_deferred + defer_size; | |
42c9db39 | 270 | new->map_nr_max = new_nr_max; |
3c6f17e6 YS |
271 | |
272 | /* map: set all old bits, clear all new bits */ | |
273 | memset(new->map, (int)0xff, old_map_size); | |
274 | memset((void *)new->map + old_map_size, 0, map_size - old_map_size); | |
275 | /* nr_deferred: copy old values, clear all new values */ | |
276 | memcpy(new->nr_deferred, old->nr_deferred, old_defer_size); | |
277 | memset((void *)new->nr_deferred + old_defer_size, 0, | |
278 | defer_size - old_defer_size); | |
2bfd3637 | 279 | |
e4262c4f | 280 | rcu_assign_pointer(pn->shrinker_info, new); |
caa05325 | 281 | call_srcu(&shrinker_srcu, &old->rcu, free_shrinker_info_rcu); |
2bfd3637 YS |
282 | } |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
e4262c4f | 287 | void free_shrinker_info(struct mem_cgroup *memcg) |
2bfd3637 YS |
288 | { |
289 | struct mem_cgroup_per_node *pn; | |
e4262c4f | 290 | struct shrinker_info *info; |
2bfd3637 YS |
291 | int nid; |
292 | ||
2bfd3637 YS |
293 | for_each_node(nid) { |
294 | pn = memcg->nodeinfo[nid]; | |
e4262c4f YS |
295 | info = rcu_dereference_protected(pn->shrinker_info, true); |
296 | kvfree(info); | |
297 | rcu_assign_pointer(pn->shrinker_info, NULL); | |
2bfd3637 YS |
298 | } |
299 | } | |
300 | ||
e4262c4f | 301 | int alloc_shrinker_info(struct mem_cgroup *memcg) |
2bfd3637 | 302 | { |
e4262c4f | 303 | struct shrinker_info *info; |
2bfd3637 | 304 | int nid, size, ret = 0; |
3c6f17e6 | 305 | int map_size, defer_size = 0; |
2bfd3637 | 306 | |
cf2e309e | 307 | mutex_lock(&shrinker_mutex); |
3c6f17e6 YS |
308 | map_size = shrinker_map_size(shrinker_nr_max); |
309 | defer_size = shrinker_defer_size(shrinker_nr_max); | |
310 | size = map_size + defer_size; | |
2bfd3637 | 311 | for_each_node(nid) { |
e4262c4f YS |
312 | info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid); |
313 | if (!info) { | |
314 | free_shrinker_info(memcg); | |
2bfd3637 YS |
315 | ret = -ENOMEM; |
316 | break; | |
317 | } | |
3c6f17e6 YS |
318 | info->nr_deferred = (atomic_long_t *)(info + 1); |
319 | info->map = (void *)info->nr_deferred + defer_size; | |
42c9db39 | 320 | info->map_nr_max = shrinker_nr_max; |
e4262c4f | 321 | rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info); |
2bfd3637 | 322 | } |
cf2e309e | 323 | mutex_unlock(&shrinker_mutex); |
2bfd3637 YS |
324 | |
325 | return ret; | |
326 | } | |
327 | ||
e4262c4f | 328 | static int expand_shrinker_info(int new_id) |
2bfd3637 | 329 | { |
3c6f17e6 | 330 | int ret = 0; |
42c9db39 | 331 | int new_nr_max = round_up(new_id + 1, BITS_PER_LONG); |
3c6f17e6 YS |
332 | int map_size, defer_size = 0; |
333 | int old_map_size, old_defer_size = 0; | |
2bfd3637 YS |
334 | struct mem_cgroup *memcg; |
335 | ||
2bfd3637 | 336 | if (!root_mem_cgroup) |
d27cf2aa YS |
337 | goto out; |
338 | ||
cf2e309e | 339 | lockdep_assert_held(&shrinker_mutex); |
2bfd3637 | 340 | |
3c6f17e6 YS |
341 | map_size = shrinker_map_size(new_nr_max); |
342 | defer_size = shrinker_defer_size(new_nr_max); | |
343 | old_map_size = shrinker_map_size(shrinker_nr_max); | |
344 | old_defer_size = shrinker_defer_size(shrinker_nr_max); | |
345 | ||
2bfd3637 YS |
346 | memcg = mem_cgroup_iter(NULL, NULL, NULL); |
347 | do { | |
3c6f17e6 | 348 | ret = expand_one_shrinker_info(memcg, map_size, defer_size, |
42c9db39 QZ |
349 | old_map_size, old_defer_size, |
350 | new_nr_max); | |
2bfd3637 YS |
351 | if (ret) { |
352 | mem_cgroup_iter_break(NULL, memcg); | |
d27cf2aa | 353 | goto out; |
2bfd3637 YS |
354 | } |
355 | } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); | |
d27cf2aa | 356 | out: |
2bfd3637 | 357 | if (!ret) |
a2fb1261 | 358 | shrinker_nr_max = new_nr_max; |
d27cf2aa | 359 | |
2bfd3637 YS |
360 | return ret; |
361 | } | |
362 | ||
363 | void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) | |
364 | { | |
365 | if (shrinker_id >= 0 && memcg && !mem_cgroup_is_root(memcg)) { | |
e4262c4f | 366 | struct shrinker_info *info; |
caa05325 | 367 | int srcu_idx; |
2bfd3637 | 368 | |
caa05325 QZ |
369 | srcu_idx = srcu_read_lock(&shrinker_srcu); |
370 | info = shrinker_info_srcu(memcg, nid); | |
42c9db39 QZ |
371 | if (!WARN_ON_ONCE(shrinker_id >= info->map_nr_max)) { |
372 | /* Pairs with smp mb in shrink_slab() */ | |
373 | smp_mb__before_atomic(); | |
374 | set_bit(shrinker_id, info->map); | |
375 | } | |
caa05325 | 376 | srcu_read_unlock(&shrinker_srcu, srcu_idx); |
2bfd3637 YS |
377 | } |
378 | } | |
379 | ||
b4c2b231 | 380 | static DEFINE_IDR(shrinker_idr); |
b4c2b231 KT |
381 | |
382 | static int prealloc_memcg_shrinker(struct shrinker *shrinker) | |
383 | { | |
384 | int id, ret = -ENOMEM; | |
385 | ||
476b30a0 YS |
386 | if (mem_cgroup_disabled()) |
387 | return -ENOSYS; | |
388 | ||
cf2e309e | 389 | mutex_lock(&shrinker_mutex); |
41ca668a | 390 | id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL); |
b4c2b231 KT |
391 | if (id < 0) |
392 | goto unlock; | |
393 | ||
0a4465d3 | 394 | if (id >= shrinker_nr_max) { |
e4262c4f | 395 | if (expand_shrinker_info(id)) { |
0a4465d3 KT |
396 | idr_remove(&shrinker_idr, id); |
397 | goto unlock; | |
398 | } | |
0a4465d3 | 399 | } |
b4c2b231 KT |
400 | shrinker->id = id; |
401 | ret = 0; | |
402 | unlock: | |
cf2e309e | 403 | mutex_unlock(&shrinker_mutex); |
b4c2b231 KT |
404 | return ret; |
405 | } | |
406 | ||
407 | static void unregister_memcg_shrinker(struct shrinker *shrinker) | |
408 | { | |
409 | int id = shrinker->id; | |
410 | ||
411 | BUG_ON(id < 0); | |
412 | ||
cf2e309e | 413 | lockdep_assert_held(&shrinker_mutex); |
41ca668a | 414 | |
b4c2b231 | 415 | idr_remove(&shrinker_idr, id); |
b4c2b231 | 416 | } |
b4c2b231 | 417 | |
86750830 YS |
418 | static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker, |
419 | struct mem_cgroup *memcg) | |
420 | { | |
421 | struct shrinker_info *info; | |
422 | ||
caa05325 | 423 | info = shrinker_info_srcu(memcg, nid); |
86750830 YS |
424 | return atomic_long_xchg(&info->nr_deferred[shrinker->id], 0); |
425 | } | |
426 | ||
427 | static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker, | |
428 | struct mem_cgroup *memcg) | |
429 | { | |
430 | struct shrinker_info *info; | |
431 | ||
caa05325 | 432 | info = shrinker_info_srcu(memcg, nid); |
86750830 YS |
433 | return atomic_long_add_return(nr, &info->nr_deferred[shrinker->id]); |
434 | } | |
435 | ||
a178015c YS |
436 | void reparent_shrinker_deferred(struct mem_cgroup *memcg) |
437 | { | |
438 | int i, nid; | |
439 | long nr; | |
440 | struct mem_cgroup *parent; | |
441 | struct shrinker_info *child_info, *parent_info; | |
442 | ||
443 | parent = parent_mem_cgroup(memcg); | |
444 | if (!parent) | |
445 | parent = root_mem_cgroup; | |
446 | ||
447 | /* Prevent from concurrent shrinker_info expand */ | |
cf2e309e | 448 | mutex_lock(&shrinker_mutex); |
a178015c YS |
449 | for_each_node(nid) { |
450 | child_info = shrinker_info_protected(memcg, nid); | |
451 | parent_info = shrinker_info_protected(parent, nid); | |
42c9db39 | 452 | for (i = 0; i < child_info->map_nr_max; i++) { |
a178015c YS |
453 | nr = atomic_long_read(&child_info->nr_deferred[i]); |
454 | atomic_long_add(nr, &parent_info->nr_deferred[i]); | |
455 | } | |
456 | } | |
cf2e309e | 457 | mutex_unlock(&shrinker_mutex); |
a178015c YS |
458 | } |
459 | ||
b5ead35e | 460 | static bool cgroup_reclaim(struct scan_control *sc) |
89b5fae5 | 461 | { |
b5ead35e | 462 | return sc->target_mem_cgroup; |
89b5fae5 | 463 | } |
97c9341f | 464 | |
a579086c YZ |
465 | static bool global_reclaim(struct scan_control *sc) |
466 | { | |
467 | return !sc->target_mem_cgroup || mem_cgroup_is_root(sc->target_mem_cgroup); | |
468 | } | |
469 | ||
97c9341f | 470 | /** |
b5ead35e | 471 | * writeback_throttling_sane - is the usual dirty throttling mechanism available? |
97c9341f TH |
472 | * @sc: scan_control in question |
473 | * | |
474 | * The normal page dirty throttling mechanism in balance_dirty_pages() is | |
475 | * completely broken with the legacy memcg and direct stalling in | |
49fd9b6d | 476 | * shrink_folio_list() is used for throttling instead, which lacks all the |
97c9341f TH |
477 | * niceties such as fairness, adaptive pausing, bandwidth proportional |
478 | * allocation and configurability. | |
479 | * | |
480 | * This function tests whether the vmscan currently in progress can assume | |
481 | * that the normal dirty throttling mechanism is operational. | |
482 | */ | |
b5ead35e | 483 | static bool writeback_throttling_sane(struct scan_control *sc) |
97c9341f | 484 | { |
b5ead35e | 485 | if (!cgroup_reclaim(sc)) |
97c9341f TH |
486 | return true; |
487 | #ifdef CONFIG_CGROUP_WRITEBACK | |
69234ace | 488 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
97c9341f TH |
489 | return true; |
490 | #endif | |
491 | return false; | |
492 | } | |
91a45470 | 493 | #else |
0a432dcb YS |
494 | static int prealloc_memcg_shrinker(struct shrinker *shrinker) |
495 | { | |
476b30a0 | 496 | return -ENOSYS; |
0a432dcb YS |
497 | } |
498 | ||
499 | static void unregister_memcg_shrinker(struct shrinker *shrinker) | |
500 | { | |
501 | } | |
502 | ||
86750830 YS |
503 | static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker, |
504 | struct mem_cgroup *memcg) | |
505 | { | |
506 | return 0; | |
507 | } | |
508 | ||
509 | static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker, | |
510 | struct mem_cgroup *memcg) | |
511 | { | |
512 | return 0; | |
513 | } | |
514 | ||
b5ead35e | 515 | static bool cgroup_reclaim(struct scan_control *sc) |
89b5fae5 | 516 | { |
b5ead35e | 517 | return false; |
89b5fae5 | 518 | } |
97c9341f | 519 | |
a579086c YZ |
520 | static bool global_reclaim(struct scan_control *sc) |
521 | { | |
522 | return true; | |
523 | } | |
524 | ||
b5ead35e | 525 | static bool writeback_throttling_sane(struct scan_control *sc) |
97c9341f TH |
526 | { |
527 | return true; | |
528 | } | |
91a45470 KH |
529 | #endif |
530 | ||
583c27a1 YA |
531 | /* |
532 | * flush_reclaim_state(): add pages reclaimed outside of LRU-based reclaim to | |
533 | * scan_control->nr_reclaimed. | |
534 | */ | |
535 | static void flush_reclaim_state(struct scan_control *sc) | |
536 | { | |
537 | /* | |
538 | * Currently, reclaim_state->reclaimed includes three types of pages | |
539 | * freed outside of vmscan: | |
540 | * (1) Slab pages. | |
541 | * (2) Clean file pages from pruned inodes (on highmem systems). | |
542 | * (3) XFS freed buffer pages. | |
543 | * | |
544 | * For all of these cases, we cannot universally link the pages to a | |
545 | * single memcg. For example, a memcg-aware shrinker can free one object | |
546 | * charged to the target memcg, causing an entire page to be freed. | |
547 | * If we count the entire page as reclaimed from the memcg, we end up | |
548 | * overestimating the reclaimed amount (potentially under-reclaiming). | |
549 | * | |
550 | * Only count such pages for global reclaim to prevent under-reclaiming | |
551 | * from the target memcg; preventing unnecessary retries during memcg | |
552 | * charging and false positives from proactive reclaim. | |
553 | * | |
554 | * For uncommon cases where the freed pages were actually mostly | |
555 | * charged to the target memcg, we end up underestimating the reclaimed | |
556 | * amount. This should be fine. The freed pages will be uncharged | |
557 | * anyway, even if they are not counted here properly, and we will be | |
558 | * able to make forward progress in charging (which is usually in a | |
559 | * retry loop). | |
560 | * | |
561 | * We can go one step further, and report the uncharged objcg pages in | |
562 | * memcg reclaim, to make reporting more accurate and reduce | |
563 | * underestimation, but it's probably not worth the complexity for now. | |
564 | */ | |
565 | if (current->reclaim_state && global_reclaim(sc)) { | |
566 | sc->nr_reclaimed += current->reclaim_state->reclaimed; | |
567 | current->reclaim_state->reclaimed = 0; | |
568 | } | |
569 | } | |
570 | ||
86750830 YS |
571 | static long xchg_nr_deferred(struct shrinker *shrinker, |
572 | struct shrink_control *sc) | |
573 | { | |
574 | int nid = sc->nid; | |
575 | ||
576 | if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) | |
577 | nid = 0; | |
578 | ||
579 | if (sc->memcg && | |
580 | (shrinker->flags & SHRINKER_MEMCG_AWARE)) | |
581 | return xchg_nr_deferred_memcg(nid, shrinker, | |
582 | sc->memcg); | |
583 | ||
584 | return atomic_long_xchg(&shrinker->nr_deferred[nid], 0); | |
585 | } | |
586 | ||
587 | ||
588 | static long add_nr_deferred(long nr, struct shrinker *shrinker, | |
589 | struct shrink_control *sc) | |
590 | { | |
591 | int nid = sc->nid; | |
592 | ||
593 | if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) | |
594 | nid = 0; | |
595 | ||
596 | if (sc->memcg && | |
597 | (shrinker->flags & SHRINKER_MEMCG_AWARE)) | |
598 | return add_nr_deferred_memcg(nr, nid, shrinker, | |
599 | sc->memcg); | |
600 | ||
601 | return atomic_long_add_return(nr, &shrinker->nr_deferred[nid]); | |
602 | } | |
603 | ||
26aa2d19 DH |
604 | static bool can_demote(int nid, struct scan_control *sc) |
605 | { | |
20b51af1 HY |
606 | if (!numa_demotion_enabled) |
607 | return false; | |
3f1509c5 JW |
608 | if (sc && sc->no_demotion) |
609 | return false; | |
26aa2d19 DH |
610 | if (next_demotion_node(nid) == NUMA_NO_NODE) |
611 | return false; | |
612 | ||
20b51af1 | 613 | return true; |
26aa2d19 DH |
614 | } |
615 | ||
a2a36488 KB |
616 | static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, |
617 | int nid, | |
618 | struct scan_control *sc) | |
619 | { | |
620 | if (memcg == NULL) { | |
621 | /* | |
622 | * For non-memcg reclaim, is there | |
623 | * space in any swap device? | |
624 | */ | |
625 | if (get_nr_swap_pages() > 0) | |
626 | return true; | |
627 | } else { | |
628 | /* Is the memcg below its swap limit? */ | |
629 | if (mem_cgroup_get_nr_swap_pages(memcg) > 0) | |
630 | return true; | |
631 | } | |
632 | ||
633 | /* | |
634 | * The page can not be swapped. | |
635 | * | |
636 | * Can it be reclaimed from this node via demotion? | |
637 | */ | |
638 | return can_demote(nid, sc); | |
639 | } | |
640 | ||
5a1c84b4 | 641 | /* |
49fd9b6d | 642 | * This misses isolated folios which are not accounted for to save counters. |
5a1c84b4 | 643 | * As the data only determines if reclaim or compaction continues, it is |
49fd9b6d | 644 | * not expected that isolated folios will be a dominating factor. |
5a1c84b4 MG |
645 | */ |
646 | unsigned long zone_reclaimable_pages(struct zone *zone) | |
647 | { | |
648 | unsigned long nr; | |
649 | ||
650 | nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + | |
651 | zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); | |
a2a36488 | 652 | if (can_reclaim_anon_pages(NULL, zone_to_nid(zone), NULL)) |
5a1c84b4 MG |
653 | nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + |
654 | zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); | |
655 | ||
656 | return nr; | |
657 | } | |
658 | ||
fd538803 MH |
659 | /** |
660 | * lruvec_lru_size - Returns the number of pages on the given LRU list. | |
661 | * @lruvec: lru vector | |
662 | * @lru: lru to use | |
8b3a899a | 663 | * @zone_idx: zones to consider (use MAX_NR_ZONES - 1 for the whole LRU list) |
fd538803 | 664 | */ |
2091339d YZ |
665 | static unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, |
666 | int zone_idx) | |
c9f299d9 | 667 | { |
de3b0150 | 668 | unsigned long size = 0; |
fd538803 MH |
669 | int zid; |
670 | ||
8b3a899a | 671 | for (zid = 0; zid <= zone_idx; zid++) { |
fd538803 | 672 | struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; |
c9f299d9 | 673 | |
fd538803 MH |
674 | if (!managed_zone(zone)) |
675 | continue; | |
676 | ||
677 | if (!mem_cgroup_disabled()) | |
de3b0150 | 678 | size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid); |
fd538803 | 679 | else |
de3b0150 | 680 | size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru); |
fd538803 | 681 | } |
de3b0150 | 682 | return size; |
b4536f0c MH |
683 | } |
684 | ||
1da177e4 | 685 | /* |
1d3d4437 | 686 | * Add a shrinker callback to be called from the vm. |
1da177e4 | 687 | */ |
e33c267a | 688 | static int __prealloc_shrinker(struct shrinker *shrinker) |
1da177e4 | 689 | { |
476b30a0 YS |
690 | unsigned int size; |
691 | int err; | |
692 | ||
693 | if (shrinker->flags & SHRINKER_MEMCG_AWARE) { | |
694 | err = prealloc_memcg_shrinker(shrinker); | |
695 | if (err != -ENOSYS) | |
696 | return err; | |
1d3d4437 | 697 | |
476b30a0 YS |
698 | shrinker->flags &= ~SHRINKER_MEMCG_AWARE; |
699 | } | |
700 | ||
701 | size = sizeof(*shrinker->nr_deferred); | |
1d3d4437 GC |
702 | if (shrinker->flags & SHRINKER_NUMA_AWARE) |
703 | size *= nr_node_ids; | |
704 | ||
705 | shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); | |
706 | if (!shrinker->nr_deferred) | |
707 | return -ENOMEM; | |
b4c2b231 | 708 | |
8e04944f TH |
709 | return 0; |
710 | } | |
711 | ||
e33c267a RG |
712 | #ifdef CONFIG_SHRINKER_DEBUG |
713 | int prealloc_shrinker(struct shrinker *shrinker, const char *fmt, ...) | |
714 | { | |
715 | va_list ap; | |
716 | int err; | |
717 | ||
718 | va_start(ap, fmt); | |
719 | shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap); | |
720 | va_end(ap); | |
721 | if (!shrinker->name) | |
722 | return -ENOMEM; | |
723 | ||
724 | err = __prealloc_shrinker(shrinker); | |
14773bfa | 725 | if (err) { |
e33c267a | 726 | kfree_const(shrinker->name); |
14773bfa TH |
727 | shrinker->name = NULL; |
728 | } | |
e33c267a RG |
729 | |
730 | return err; | |
731 | } | |
732 | #else | |
733 | int prealloc_shrinker(struct shrinker *shrinker, const char *fmt, ...) | |
734 | { | |
735 | return __prealloc_shrinker(shrinker); | |
736 | } | |
737 | #endif | |
738 | ||
8e04944f TH |
739 | void free_prealloced_shrinker(struct shrinker *shrinker) |
740 | { | |
e33c267a RG |
741 | #ifdef CONFIG_SHRINKER_DEBUG |
742 | kfree_const(shrinker->name); | |
14773bfa | 743 | shrinker->name = NULL; |
e33c267a | 744 | #endif |
41ca668a | 745 | if (shrinker->flags & SHRINKER_MEMCG_AWARE) { |
cf2e309e | 746 | mutex_lock(&shrinker_mutex); |
b4c2b231 | 747 | unregister_memcg_shrinker(shrinker); |
cf2e309e | 748 | mutex_unlock(&shrinker_mutex); |
476b30a0 | 749 | return; |
41ca668a | 750 | } |
b4c2b231 | 751 | |
8e04944f TH |
752 | kfree(shrinker->nr_deferred); |
753 | shrinker->nr_deferred = NULL; | |
754 | } | |
1d3d4437 | 755 | |
8e04944f TH |
756 | void register_shrinker_prepared(struct shrinker *shrinker) |
757 | { | |
cf2e309e | 758 | mutex_lock(&shrinker_mutex); |
f95bdb70 | 759 | list_add_tail_rcu(&shrinker->list, &shrinker_list); |
41ca668a | 760 | shrinker->flags |= SHRINKER_REGISTERED; |
5035ebc6 | 761 | shrinker_debugfs_add(shrinker); |
cf2e309e | 762 | mutex_unlock(&shrinker_mutex); |
8e04944f TH |
763 | } |
764 | ||
e33c267a | 765 | static int __register_shrinker(struct shrinker *shrinker) |
8e04944f | 766 | { |
e33c267a | 767 | int err = __prealloc_shrinker(shrinker); |
8e04944f TH |
768 | |
769 | if (err) | |
770 | return err; | |
771 | register_shrinker_prepared(shrinker); | |
1d3d4437 | 772 | return 0; |
1da177e4 | 773 | } |
e33c267a RG |
774 | |
775 | #ifdef CONFIG_SHRINKER_DEBUG | |
776 | int register_shrinker(struct shrinker *shrinker, const char *fmt, ...) | |
777 | { | |
778 | va_list ap; | |
779 | int err; | |
780 | ||
781 | va_start(ap, fmt); | |
782 | shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap); | |
783 | va_end(ap); | |
784 | if (!shrinker->name) | |
785 | return -ENOMEM; | |
786 | ||
787 | err = __register_shrinker(shrinker); | |
14773bfa | 788 | if (err) { |
e33c267a | 789 | kfree_const(shrinker->name); |
14773bfa TH |
790 | shrinker->name = NULL; |
791 | } | |
e33c267a RG |
792 | return err; |
793 | } | |
794 | #else | |
795 | int register_shrinker(struct shrinker *shrinker, const char *fmt, ...) | |
796 | { | |
797 | return __register_shrinker(shrinker); | |
798 | } | |
799 | #endif | |
8e1f936b | 800 | EXPORT_SYMBOL(register_shrinker); |
1da177e4 LT |
801 | |
802 | /* | |
803 | * Remove one | |
804 | */ | |
8e1f936b | 805 | void unregister_shrinker(struct shrinker *shrinker) |
1da177e4 | 806 | { |
badc28d4 QZ |
807 | struct dentry *debugfs_entry; |
808 | ||
41ca668a | 809 | if (!(shrinker->flags & SHRINKER_REGISTERED)) |
bb422a73 | 810 | return; |
41ca668a | 811 | |
cf2e309e | 812 | mutex_lock(&shrinker_mutex); |
f95bdb70 | 813 | list_del_rcu(&shrinker->list); |
41ca668a YS |
814 | shrinker->flags &= ~SHRINKER_REGISTERED; |
815 | if (shrinker->flags & SHRINKER_MEMCG_AWARE) | |
816 | unregister_memcg_shrinker(shrinker); | |
badc28d4 | 817 | debugfs_entry = shrinker_debugfs_remove(shrinker); |
cf2e309e | 818 | mutex_unlock(&shrinker_mutex); |
41ca668a | 819 | |
475733dd | 820 | atomic_inc(&shrinker_srcu_generation); |
f95bdb70 QZ |
821 | synchronize_srcu(&shrinker_srcu); |
822 | ||
badc28d4 QZ |
823 | debugfs_remove_recursive(debugfs_entry); |
824 | ||
ae393321 | 825 | kfree(shrinker->nr_deferred); |
bb422a73 | 826 | shrinker->nr_deferred = NULL; |
1da177e4 | 827 | } |
8e1f936b | 828 | EXPORT_SYMBOL(unregister_shrinker); |
1da177e4 | 829 | |
880121be CK |
830 | /** |
831 | * synchronize_shrinkers - Wait for all running shrinkers to complete. | |
832 | * | |
1643db98 QZ |
833 | * This is useful to guarantee that all shrinker invocations have seen an |
834 | * update, before freeing memory. | |
880121be CK |
835 | */ |
836 | void synchronize_shrinkers(void) | |
837 | { | |
475733dd | 838 | atomic_inc(&shrinker_srcu_generation); |
f95bdb70 | 839 | synchronize_srcu(&shrinker_srcu); |
880121be CK |
840 | } |
841 | EXPORT_SYMBOL(synchronize_shrinkers); | |
842 | ||
1da177e4 | 843 | #define SHRINK_BATCH 128 |
1d3d4437 | 844 | |
cb731d6c | 845 | static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, |
9092c71b | 846 | struct shrinker *shrinker, int priority) |
1d3d4437 GC |
847 | { |
848 | unsigned long freed = 0; | |
849 | unsigned long long delta; | |
850 | long total_scan; | |
d5bc5fd3 | 851 | long freeable; |
1d3d4437 GC |
852 | long nr; |
853 | long new_nr; | |
1d3d4437 GC |
854 | long batch_size = shrinker->batch ? shrinker->batch |
855 | : SHRINK_BATCH; | |
5f33a080 | 856 | long scanned = 0, next_deferred; |
1d3d4437 | 857 | |
d5bc5fd3 | 858 | freeable = shrinker->count_objects(shrinker, shrinkctl); |
9b996468 KT |
859 | if (freeable == 0 || freeable == SHRINK_EMPTY) |
860 | return freeable; | |
1d3d4437 GC |
861 | |
862 | /* | |
863 | * copy the current shrinker scan count into a local variable | |
864 | * and zero it so that other concurrent shrinker invocations | |
865 | * don't also do this scanning work. | |
866 | */ | |
86750830 | 867 | nr = xchg_nr_deferred(shrinker, shrinkctl); |
1d3d4437 | 868 | |
4b85afbd JW |
869 | if (shrinker->seeks) { |
870 | delta = freeable >> priority; | |
871 | delta *= 4; | |
872 | do_div(delta, shrinker->seeks); | |
873 | } else { | |
874 | /* | |
875 | * These objects don't require any IO to create. Trim | |
876 | * them aggressively under memory pressure to keep | |
877 | * them from causing refetches in the IO caches. | |
878 | */ | |
879 | delta = freeable / 2; | |
880 | } | |
172b06c3 | 881 | |
18bb473e | 882 | total_scan = nr >> priority; |
1d3d4437 | 883 | total_scan += delta; |
18bb473e | 884 | total_scan = min(total_scan, (2 * freeable)); |
1d3d4437 GC |
885 | |
886 | trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, | |
9092c71b | 887 | freeable, delta, total_scan, priority); |
1d3d4437 | 888 | |
0b1fb40a VD |
889 | /* |
890 | * Normally, we should not scan less than batch_size objects in one | |
891 | * pass to avoid too frequent shrinker calls, but if the slab has less | |
892 | * than batch_size objects in total and we are really tight on memory, | |
893 | * we will try to reclaim all available objects, otherwise we can end | |
894 | * up failing allocations although there are plenty of reclaimable | |
895 | * objects spread over several slabs with usage less than the | |
896 | * batch_size. | |
897 | * | |
898 | * We detect the "tight on memory" situations by looking at the total | |
899 | * number of objects we want to scan (total_scan). If it is greater | |
d5bc5fd3 | 900 | * than the total number of objects on slab (freeable), we must be |
0b1fb40a VD |
901 | * scanning at high prio and therefore should try to reclaim as much as |
902 | * possible. | |
903 | */ | |
904 | while (total_scan >= batch_size || | |
d5bc5fd3 | 905 | total_scan >= freeable) { |
a0b02131 | 906 | unsigned long ret; |
0b1fb40a | 907 | unsigned long nr_to_scan = min(batch_size, total_scan); |
1d3d4437 | 908 | |
0b1fb40a | 909 | shrinkctl->nr_to_scan = nr_to_scan; |
d460acb5 | 910 | shrinkctl->nr_scanned = nr_to_scan; |
a0b02131 DC |
911 | ret = shrinker->scan_objects(shrinker, shrinkctl); |
912 | if (ret == SHRINK_STOP) | |
913 | break; | |
914 | freed += ret; | |
1d3d4437 | 915 | |
d460acb5 CW |
916 | count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); |
917 | total_scan -= shrinkctl->nr_scanned; | |
918 | scanned += shrinkctl->nr_scanned; | |
1d3d4437 GC |
919 | |
920 | cond_resched(); | |
921 | } | |
922 | ||
18bb473e YS |
923 | /* |
924 | * The deferred work is increased by any new work (delta) that wasn't | |
925 | * done, decreased by old deferred work that was done now. | |
926 | * | |
927 | * And it is capped to two times of the freeable items. | |
928 | */ | |
929 | next_deferred = max_t(long, (nr + delta - scanned), 0); | |
930 | next_deferred = min(next_deferred, (2 * freeable)); | |
931 | ||
1d3d4437 GC |
932 | /* |
933 | * move the unused scan count back into the shrinker in a | |
86750830 | 934 | * manner that handles concurrent updates. |
1d3d4437 | 935 | */ |
86750830 | 936 | new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl); |
1d3d4437 | 937 | |
8efb4b59 | 938 | trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan); |
1d3d4437 | 939 | return freed; |
1495f230 YH |
940 | } |
941 | ||
0a432dcb | 942 | #ifdef CONFIG_MEMCG |
b0dedc49 KT |
943 | static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, |
944 | struct mem_cgroup *memcg, int priority) | |
945 | { | |
e4262c4f | 946 | struct shrinker_info *info; |
b8e57efa | 947 | unsigned long ret, freed = 0; |
475733dd KT |
948 | int srcu_idx, generation; |
949 | int i = 0; | |
b0dedc49 | 950 | |
0a432dcb | 951 | if (!mem_cgroup_online(memcg)) |
b0dedc49 KT |
952 | return 0; |
953 | ||
475733dd | 954 | again: |
caa05325 QZ |
955 | srcu_idx = srcu_read_lock(&shrinker_srcu); |
956 | info = shrinker_info_srcu(memcg, nid); | |
e4262c4f | 957 | if (unlikely(!info)) |
b0dedc49 KT |
958 | goto unlock; |
959 | ||
475733dd KT |
960 | generation = atomic_read(&shrinker_srcu_generation); |
961 | for_each_set_bit_from(i, info->map, info->map_nr_max) { | |
b0dedc49 KT |
962 | struct shrink_control sc = { |
963 | .gfp_mask = gfp_mask, | |
964 | .nid = nid, | |
965 | .memcg = memcg, | |
966 | }; | |
967 | struct shrinker *shrinker; | |
968 | ||
969 | shrinker = idr_find(&shrinker_idr, i); | |
41ca668a | 970 | if (unlikely(!shrinker || !(shrinker->flags & SHRINKER_REGISTERED))) { |
7e010df5 | 971 | if (!shrinker) |
e4262c4f | 972 | clear_bit(i, info->map); |
b0dedc49 KT |
973 | continue; |
974 | } | |
975 | ||
0a432dcb | 976 | /* Call non-slab shrinkers even though kmem is disabled */ |
f7a449f7 | 977 | if (!memcg_kmem_online() && |
0a432dcb YS |
978 | !(shrinker->flags & SHRINKER_NONSLAB)) |
979 | continue; | |
980 | ||
b0dedc49 | 981 | ret = do_shrink_slab(&sc, shrinker, priority); |
f90280d6 | 982 | if (ret == SHRINK_EMPTY) { |
e4262c4f | 983 | clear_bit(i, info->map); |
f90280d6 KT |
984 | /* |
985 | * After the shrinker reported that it had no objects to | |
986 | * free, but before we cleared the corresponding bit in | |
987 | * the memcg shrinker map, a new object might have been | |
988 | * added. To make sure, we have the bit set in this | |
989 | * case, we invoke the shrinker one more time and reset | |
990 | * the bit if it reports that it is not empty anymore. | |
991 | * The memory barrier here pairs with the barrier in | |
2bfd3637 | 992 | * set_shrinker_bit(): |
f90280d6 KT |
993 | * |
994 | * list_lru_add() shrink_slab_memcg() | |
995 | * list_add_tail() clear_bit() | |
996 | * <MB> <MB> | |
997 | * set_bit() do_shrink_slab() | |
998 | */ | |
999 | smp_mb__after_atomic(); | |
1000 | ret = do_shrink_slab(&sc, shrinker, priority); | |
1001 | if (ret == SHRINK_EMPTY) | |
1002 | ret = 0; | |
1003 | else | |
2bfd3637 | 1004 | set_shrinker_bit(memcg, nid, i); |
f90280d6 | 1005 | } |
b0dedc49 | 1006 | freed += ret; |
475733dd KT |
1007 | if (atomic_read(&shrinker_srcu_generation) != generation) { |
1008 | srcu_read_unlock(&shrinker_srcu, srcu_idx); | |
1009 | i++; | |
1010 | goto again; | |
1011 | } | |
b0dedc49 KT |
1012 | } |
1013 | unlock: | |
caa05325 | 1014 | srcu_read_unlock(&shrinker_srcu, srcu_idx); |
b0dedc49 KT |
1015 | return freed; |
1016 | } | |
0a432dcb | 1017 | #else /* CONFIG_MEMCG */ |
b0dedc49 KT |
1018 | static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, |
1019 | struct mem_cgroup *memcg, int priority) | |
1020 | { | |
1021 | return 0; | |
1022 | } | |
0a432dcb | 1023 | #endif /* CONFIG_MEMCG */ |
b0dedc49 | 1024 | |
6b4f7799 | 1025 | /** |
cb731d6c | 1026 | * shrink_slab - shrink slab caches |
6b4f7799 JW |
1027 | * @gfp_mask: allocation context |
1028 | * @nid: node whose slab caches to target | |
cb731d6c | 1029 | * @memcg: memory cgroup whose slab caches to target |
9092c71b | 1030 | * @priority: the reclaim priority |
1da177e4 | 1031 | * |
6b4f7799 | 1032 | * Call the shrink functions to age shrinkable caches. |
1da177e4 | 1033 | * |
6b4f7799 JW |
1034 | * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, |
1035 | * unaware shrinkers will receive a node id of 0 instead. | |
1da177e4 | 1036 | * |
aeed1d32 VD |
1037 | * @memcg specifies the memory cgroup to target. Unaware shrinkers |
1038 | * are called only if it is the root cgroup. | |
cb731d6c | 1039 | * |
9092c71b JB |
1040 | * @priority is sc->priority, we take the number of objects and >> by priority |
1041 | * in order to get the scan target. | |
b15e0905 | 1042 | * |
6b4f7799 | 1043 | * Returns the number of reclaimed slab objects. |
1da177e4 | 1044 | */ |
cb731d6c VD |
1045 | static unsigned long shrink_slab(gfp_t gfp_mask, int nid, |
1046 | struct mem_cgroup *memcg, | |
9092c71b | 1047 | int priority) |
1da177e4 | 1048 | { |
b8e57efa | 1049 | unsigned long ret, freed = 0; |
1da177e4 | 1050 | struct shrinker *shrinker; |
475733dd | 1051 | int srcu_idx, generation; |
1da177e4 | 1052 | |
fa1e512f YS |
1053 | /* |
1054 | * The root memcg might be allocated even though memcg is disabled | |
1055 | * via "cgroup_disable=memory" boot parameter. This could make | |
1056 | * mem_cgroup_is_root() return false, then just run memcg slab | |
1057 | * shrink, but skip global shrink. This may result in premature | |
1058 | * oom. | |
1059 | */ | |
1060 | if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg)) | |
b0dedc49 | 1061 | return shrink_slab_memcg(gfp_mask, nid, memcg, priority); |
cb731d6c | 1062 | |
f95bdb70 | 1063 | srcu_idx = srcu_read_lock(&shrinker_srcu); |
1da177e4 | 1064 | |
475733dd | 1065 | generation = atomic_read(&shrinker_srcu_generation); |
f95bdb70 QZ |
1066 | list_for_each_entry_srcu(shrinker, &shrinker_list, list, |
1067 | srcu_read_lock_held(&shrinker_srcu)) { | |
6b4f7799 JW |
1068 | struct shrink_control sc = { |
1069 | .gfp_mask = gfp_mask, | |
1070 | .nid = nid, | |
cb731d6c | 1071 | .memcg = memcg, |
6b4f7799 | 1072 | }; |
ec97097b | 1073 | |
9b996468 KT |
1074 | ret = do_shrink_slab(&sc, shrinker, priority); |
1075 | if (ret == SHRINK_EMPTY) | |
1076 | ret = 0; | |
1077 | freed += ret; | |
475733dd KT |
1078 | |
1079 | if (atomic_read(&shrinker_srcu_generation) != generation) { | |
1080 | freed = freed ? : 1; | |
1081 | break; | |
1082 | } | |
1da177e4 | 1083 | } |
6b4f7799 | 1084 | |
f95bdb70 | 1085 | srcu_read_unlock(&shrinker_srcu, srcu_idx); |
f06590bd | 1086 | cond_resched(); |
24f7c6b9 | 1087 | return freed; |
1da177e4 LT |
1088 | } |
1089 | ||
e83b39d6 | 1090 | static unsigned long drop_slab_node(int nid) |
cb731d6c | 1091 | { |
e83b39d6 JK |
1092 | unsigned long freed = 0; |
1093 | struct mem_cgroup *memcg = NULL; | |
cb731d6c | 1094 | |
e83b39d6 | 1095 | memcg = mem_cgroup_iter(NULL, NULL, NULL); |
cb731d6c | 1096 | do { |
e83b39d6 JK |
1097 | freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); |
1098 | } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); | |
069c411d | 1099 | |
e83b39d6 | 1100 | return freed; |
cb731d6c VD |
1101 | } |
1102 | ||
1103 | void drop_slab(void) | |
1104 | { | |
1105 | int nid; | |
e83b39d6 JK |
1106 | int shift = 0; |
1107 | unsigned long freed; | |
1108 | ||
1109 | do { | |
1110 | freed = 0; | |
1111 | for_each_online_node(nid) { | |
1112 | if (fatal_signal_pending(current)) | |
1113 | return; | |
cb731d6c | 1114 | |
e83b39d6 JK |
1115 | freed += drop_slab_node(nid); |
1116 | } | |
1117 | } while ((freed >> shift++) > 1); | |
cb731d6c VD |
1118 | } |
1119 | ||
57e9cc50 JW |
1120 | static int reclaimer_offset(void) |
1121 | { | |
1122 | BUILD_BUG_ON(PGSTEAL_DIRECT - PGSTEAL_KSWAPD != | |
1123 | PGDEMOTE_DIRECT - PGDEMOTE_KSWAPD); | |
1124 | BUILD_BUG_ON(PGSTEAL_DIRECT - PGSTEAL_KSWAPD != | |
1125 | PGSCAN_DIRECT - PGSCAN_KSWAPD); | |
1126 | BUILD_BUG_ON(PGSTEAL_KHUGEPAGED - PGSTEAL_KSWAPD != | |
1127 | PGDEMOTE_KHUGEPAGED - PGDEMOTE_KSWAPD); | |
1128 | BUILD_BUG_ON(PGSTEAL_KHUGEPAGED - PGSTEAL_KSWAPD != | |
1129 | PGSCAN_KHUGEPAGED - PGSCAN_KSWAPD); | |
1130 | ||
1131 | if (current_is_kswapd()) | |
1132 | return 0; | |
1133 | if (current_is_khugepaged()) | |
1134 | return PGSTEAL_KHUGEPAGED - PGSTEAL_KSWAPD; | |
1135 | return PGSTEAL_DIRECT - PGSTEAL_KSWAPD; | |
1136 | } | |
1137 | ||
e0cd5e7f | 1138 | static inline int is_page_cache_freeable(struct folio *folio) |
1da177e4 | 1139 | { |
ceddc3a5 | 1140 | /* |
49fd9b6d MWO |
1141 | * A freeable page cache folio is referenced only by the caller |
1142 | * that isolated the folio, the page cache and optional filesystem | |
1143 | * private data at folio->private. | |
ceddc3a5 | 1144 | */ |
e0cd5e7f MWO |
1145 | return folio_ref_count(folio) - folio_test_private(folio) == |
1146 | 1 + folio_nr_pages(folio); | |
1da177e4 LT |
1147 | } |
1148 | ||
1da177e4 | 1149 | /* |
e0cd5e7f | 1150 | * We detected a synchronous write error writing a folio out. Probably |
1da177e4 LT |
1151 | * -ENOSPC. We need to propagate that into the address_space for a subsequent |
1152 | * fsync(), msync() or close(). | |
1153 | * | |
1154 | * The tricky part is that after writepage we cannot touch the mapping: nothing | |
e0cd5e7f MWO |
1155 | * prevents it from being freed up. But we have a ref on the folio and once |
1156 | * that folio is locked, the mapping is pinned. | |
1da177e4 | 1157 | * |
e0cd5e7f | 1158 | * We're allowed to run sleeping folio_lock() here because we know the caller has |
1da177e4 LT |
1159 | * __GFP_FS. |
1160 | */ | |
1161 | static void handle_write_error(struct address_space *mapping, | |
e0cd5e7f | 1162 | struct folio *folio, int error) |
1da177e4 | 1163 | { |
e0cd5e7f MWO |
1164 | folio_lock(folio); |
1165 | if (folio_mapping(folio) == mapping) | |
3e9f45bd | 1166 | mapping_set_error(mapping, error); |
e0cd5e7f | 1167 | folio_unlock(folio); |
1da177e4 LT |
1168 | } |
1169 | ||
1b4e3f26 MG |
1170 | static bool skip_throttle_noprogress(pg_data_t *pgdat) |
1171 | { | |
1172 | int reclaimable = 0, write_pending = 0; | |
1173 | int i; | |
1174 | ||
1175 | /* | |
1176 | * If kswapd is disabled, reschedule if necessary but do not | |
1177 | * throttle as the system is likely near OOM. | |
1178 | */ | |
1179 | if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) | |
1180 | return true; | |
1181 | ||
1182 | /* | |
49fd9b6d MWO |
1183 | * If there are a lot of dirty/writeback folios then do not |
1184 | * throttle as throttling will occur when the folios cycle | |
1b4e3f26 MG |
1185 | * towards the end of the LRU if still under writeback. |
1186 | */ | |
1187 | for (i = 0; i < MAX_NR_ZONES; i++) { | |
1188 | struct zone *zone = pgdat->node_zones + i; | |
1189 | ||
36c26128 | 1190 | if (!managed_zone(zone)) |
1b4e3f26 MG |
1191 | continue; |
1192 | ||
1193 | reclaimable += zone_reclaimable_pages(zone); | |
1194 | write_pending += zone_page_state_snapshot(zone, | |
1195 | NR_ZONE_WRITE_PENDING); | |
1196 | } | |
1197 | if (2 * write_pending <= reclaimable) | |
1198 | return true; | |
1199 | ||
1200 | return false; | |
1201 | } | |
1202 | ||
c3f4a9a2 | 1203 | void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason) |
8cd7c588 MG |
1204 | { |
1205 | wait_queue_head_t *wqh = &pgdat->reclaim_wait[reason]; | |
c3f4a9a2 | 1206 | long timeout, ret; |
8cd7c588 MG |
1207 | DEFINE_WAIT(wait); |
1208 | ||
1209 | /* | |
1210 | * Do not throttle IO workers, kthreads other than kswapd or | |
1211 | * workqueues. They may be required for reclaim to make | |
1212 | * forward progress (e.g. journalling workqueues or kthreads). | |
1213 | */ | |
1214 | if (!current_is_kswapd() && | |
b485c6f1 MG |
1215 | current->flags & (PF_IO_WORKER|PF_KTHREAD)) { |
1216 | cond_resched(); | |
8cd7c588 | 1217 | return; |
b485c6f1 | 1218 | } |
8cd7c588 | 1219 | |
c3f4a9a2 MG |
1220 | /* |
1221 | * These figures are pulled out of thin air. | |
1222 | * VMSCAN_THROTTLE_ISOLATED is a transient condition based on too many | |
1223 | * parallel reclaimers which is a short-lived event so the timeout is | |
1224 | * short. Failing to make progress or waiting on writeback are | |
1225 | * potentially long-lived events so use a longer timeout. This is shaky | |
1226 | * logic as a failure to make progress could be due to anything from | |
49fd9b6d | 1227 | * writeback to a slow device to excessive referenced folios at the tail |
c3f4a9a2 MG |
1228 | * of the inactive LRU. |
1229 | */ | |
1230 | switch(reason) { | |
1231 | case VMSCAN_THROTTLE_WRITEBACK: | |
1232 | timeout = HZ/10; | |
1233 | ||
1234 | if (atomic_inc_return(&pgdat->nr_writeback_throttled) == 1) { | |
1235 | WRITE_ONCE(pgdat->nr_reclaim_start, | |
1236 | node_page_state(pgdat, NR_THROTTLED_WRITTEN)); | |
1237 | } | |
1238 | ||
1239 | break; | |
1b4e3f26 MG |
1240 | case VMSCAN_THROTTLE_CONGESTED: |
1241 | fallthrough; | |
c3f4a9a2 | 1242 | case VMSCAN_THROTTLE_NOPROGRESS: |
1b4e3f26 MG |
1243 | if (skip_throttle_noprogress(pgdat)) { |
1244 | cond_resched(); | |
1245 | return; | |
1246 | } | |
1247 | ||
1248 | timeout = 1; | |
1249 | ||
c3f4a9a2 MG |
1250 | break; |
1251 | case VMSCAN_THROTTLE_ISOLATED: | |
1252 | timeout = HZ/50; | |
1253 | break; | |
1254 | default: | |
1255 | WARN_ON_ONCE(1); | |
1256 | timeout = HZ; | |
1257 | break; | |
8cd7c588 MG |
1258 | } |
1259 | ||
1260 | prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); | |
1261 | ret = schedule_timeout(timeout); | |
1262 | finish_wait(wqh, &wait); | |
d818fca1 | 1263 | |
c3f4a9a2 | 1264 | if (reason == VMSCAN_THROTTLE_WRITEBACK) |
d818fca1 | 1265 | atomic_dec(&pgdat->nr_writeback_throttled); |
8cd7c588 MG |
1266 | |
1267 | trace_mm_vmscan_throttled(pgdat->node_id, jiffies_to_usecs(timeout), | |
1268 | jiffies_to_usecs(timeout - ret), | |
1269 | reason); | |
1270 | } | |
1271 | ||
1272 | /* | |
49fd9b6d MWO |
1273 | * Account for folios written if tasks are throttled waiting on dirty |
1274 | * folios to clean. If enough folios have been cleaned since throttling | |
8cd7c588 MG |
1275 | * started then wakeup the throttled tasks. |
1276 | */ | |
512b7931 | 1277 | void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, |
8cd7c588 MG |
1278 | int nr_throttled) |
1279 | { | |
1280 | unsigned long nr_written; | |
1281 | ||
512b7931 | 1282 | node_stat_add_folio(folio, NR_THROTTLED_WRITTEN); |
8cd7c588 MG |
1283 | |
1284 | /* | |
1285 | * This is an inaccurate read as the per-cpu deltas may not | |
1286 | * be synchronised. However, given that the system is | |
1287 | * writeback throttled, it is not worth taking the penalty | |
1288 | * of getting an accurate count. At worst, the throttle | |
1289 | * timeout guarantees forward progress. | |
1290 | */ | |
1291 | nr_written = node_page_state(pgdat, NR_THROTTLED_WRITTEN) - | |
1292 | READ_ONCE(pgdat->nr_reclaim_start); | |
1293 | ||
1294 | if (nr_written > SWAP_CLUSTER_MAX * nr_throttled) | |
1295 | wake_up(&pgdat->reclaim_wait[VMSCAN_THROTTLE_WRITEBACK]); | |
1296 | } | |
1297 | ||
04e62a29 CL |
1298 | /* possible outcome of pageout() */ |
1299 | typedef enum { | |
49fd9b6d | 1300 | /* failed to write folio out, folio is locked */ |
04e62a29 | 1301 | PAGE_KEEP, |
49fd9b6d | 1302 | /* move folio to the active list, folio is locked */ |
04e62a29 | 1303 | PAGE_ACTIVATE, |
49fd9b6d | 1304 | /* folio has been sent to the disk successfully, folio is unlocked */ |
04e62a29 | 1305 | PAGE_SUCCESS, |
49fd9b6d | 1306 | /* folio is clean and locked */ |
04e62a29 CL |
1307 | PAGE_CLEAN, |
1308 | } pageout_t; | |
1309 | ||
1da177e4 | 1310 | /* |
49fd9b6d | 1311 | * pageout is called by shrink_folio_list() for each dirty folio. |
1742f19f | 1312 | * Calls ->writepage(). |
1da177e4 | 1313 | */ |
2282679f N |
1314 | static pageout_t pageout(struct folio *folio, struct address_space *mapping, |
1315 | struct swap_iocb **plug) | |
1da177e4 LT |
1316 | { |
1317 | /* | |
e0cd5e7f | 1318 | * If the folio is dirty, only perform writeback if that write |
1da177e4 LT |
1319 | * will be non-blocking. To prevent this allocation from being |
1320 | * stalled by pagecache activity. But note that there may be | |
1321 | * stalls if we need to run get_block(). We could test | |
1322 | * PagePrivate for that. | |
1323 | * | |
8174202b | 1324 | * If this process is currently in __generic_file_write_iter() against |
e0cd5e7f | 1325 | * this folio's queue, we can perform writeback even if that |
1da177e4 LT |
1326 | * will block. |
1327 | * | |
e0cd5e7f | 1328 | * If the folio is swapcache, write it back even if that would |
1da177e4 LT |
1329 | * block, for some throttling. This happens by accident, because |
1330 | * swap_backing_dev_info is bust: it doesn't reflect the | |
1331 | * congestion state of the swapdevs. Easy to fix, if needed. | |
1da177e4 | 1332 | */ |
e0cd5e7f | 1333 | if (!is_page_cache_freeable(folio)) |
1da177e4 LT |
1334 | return PAGE_KEEP; |
1335 | if (!mapping) { | |
1336 | /* | |
e0cd5e7f MWO |
1337 | * Some data journaling orphaned folios can have |
1338 | * folio->mapping == NULL while being dirty with clean buffers. | |
1da177e4 | 1339 | */ |
e0cd5e7f | 1340 | if (folio_test_private(folio)) { |
68189fef | 1341 | if (try_to_free_buffers(folio)) { |
e0cd5e7f MWO |
1342 | folio_clear_dirty(folio); |
1343 | pr_info("%s: orphaned folio\n", __func__); | |
1da177e4 LT |
1344 | return PAGE_CLEAN; |
1345 | } | |
1346 | } | |
1347 | return PAGE_KEEP; | |
1348 | } | |
1349 | if (mapping->a_ops->writepage == NULL) | |
1350 | return PAGE_ACTIVATE; | |
1da177e4 | 1351 | |
e0cd5e7f | 1352 | if (folio_clear_dirty_for_io(folio)) { |
1da177e4 LT |
1353 | int res; |
1354 | struct writeback_control wbc = { | |
1355 | .sync_mode = WB_SYNC_NONE, | |
1356 | .nr_to_write = SWAP_CLUSTER_MAX, | |
111ebb6e OH |
1357 | .range_start = 0, |
1358 | .range_end = LLONG_MAX, | |
1da177e4 | 1359 | .for_reclaim = 1, |
2282679f | 1360 | .swap_plug = plug, |
1da177e4 LT |
1361 | }; |
1362 | ||
e0cd5e7f MWO |
1363 | folio_set_reclaim(folio); |
1364 | res = mapping->a_ops->writepage(&folio->page, &wbc); | |
1da177e4 | 1365 | if (res < 0) |
e0cd5e7f | 1366 | handle_write_error(mapping, folio, res); |
994fc28c | 1367 | if (res == AOP_WRITEPAGE_ACTIVATE) { |
e0cd5e7f | 1368 | folio_clear_reclaim(folio); |
1da177e4 LT |
1369 | return PAGE_ACTIVATE; |
1370 | } | |
c661b078 | 1371 | |
e0cd5e7f | 1372 | if (!folio_test_writeback(folio)) { |
1da177e4 | 1373 | /* synchronous write or broken a_ops? */ |
e0cd5e7f | 1374 | folio_clear_reclaim(folio); |
1da177e4 | 1375 | } |
e0cd5e7f MWO |
1376 | trace_mm_vmscan_write_folio(folio); |
1377 | node_stat_add_folio(folio, NR_VMSCAN_WRITE); | |
1da177e4 LT |
1378 | return PAGE_SUCCESS; |
1379 | } | |
1380 | ||
1381 | return PAGE_CLEAN; | |
1382 | } | |
1383 | ||
a649fd92 | 1384 | /* |
49fd9b6d | 1385 | * Same as remove_mapping, but if the folio is removed from the mapping, it |
e286781d | 1386 | * gets returned with a refcount of 0. |
a649fd92 | 1387 | */ |
be7c07d6 | 1388 | static int __remove_mapping(struct address_space *mapping, struct folio *folio, |
b910718a | 1389 | bool reclaimed, struct mem_cgroup *target_memcg) |
49d2e9cc | 1390 | { |
bd4c82c2 | 1391 | int refcount; |
aae466b0 | 1392 | void *shadow = NULL; |
c4843a75 | 1393 | |
be7c07d6 MWO |
1394 | BUG_ON(!folio_test_locked(folio)); |
1395 | BUG_ON(mapping != folio_mapping(folio)); | |
49d2e9cc | 1396 | |
be7c07d6 | 1397 | if (!folio_test_swapcache(folio)) |
51b8c1fe | 1398 | spin_lock(&mapping->host->i_lock); |
30472509 | 1399 | xa_lock_irq(&mapping->i_pages); |
49d2e9cc | 1400 | /* |
49fd9b6d | 1401 | * The non racy check for a busy folio. |
0fd0e6b0 NP |
1402 | * |
1403 | * Must be careful with the order of the tests. When someone has | |
49fd9b6d MWO |
1404 | * a ref to the folio, it may be possible that they dirty it then |
1405 | * drop the reference. So if the dirty flag is tested before the | |
1406 | * refcount here, then the following race may occur: | |
0fd0e6b0 NP |
1407 | * |
1408 | * get_user_pages(&page); | |
1409 | * [user mapping goes away] | |
1410 | * write_to(page); | |
49fd9b6d MWO |
1411 | * !folio_test_dirty(folio) [good] |
1412 | * folio_set_dirty(folio); | |
1413 | * folio_put(folio); | |
1414 | * !refcount(folio) [good, discard it] | |
0fd0e6b0 NP |
1415 | * |
1416 | * [oops, our write_to data is lost] | |
1417 | * | |
1418 | * Reversing the order of the tests ensures such a situation cannot | |
49fd9b6d MWO |
1419 | * escape unnoticed. The smp_rmb is needed to ensure the folio->flags |
1420 | * load is not satisfied before that of folio->_refcount. | |
0fd0e6b0 | 1421 | * |
49fd9b6d | 1422 | * Note that if the dirty flag is always set via folio_mark_dirty, |
b93b0163 | 1423 | * and thus under the i_pages lock, then this ordering is not required. |
49d2e9cc | 1424 | */ |
be7c07d6 MWO |
1425 | refcount = 1 + folio_nr_pages(folio); |
1426 | if (!folio_ref_freeze(folio, refcount)) | |
49d2e9cc | 1427 | goto cannot_free; |
49fd9b6d | 1428 | /* note: atomic_cmpxchg in folio_ref_freeze provides the smp_rmb */ |
be7c07d6 MWO |
1429 | if (unlikely(folio_test_dirty(folio))) { |
1430 | folio_ref_unfreeze(folio, refcount); | |
49d2e9cc | 1431 | goto cannot_free; |
e286781d | 1432 | } |
49d2e9cc | 1433 | |
be7c07d6 MWO |
1434 | if (folio_test_swapcache(folio)) { |
1435 | swp_entry_t swap = folio_swap_entry(folio); | |
ac35a490 | 1436 | |
aae466b0 | 1437 | if (reclaimed && !mapping_exiting(mapping)) |
8927f647 | 1438 | shadow = workingset_eviction(folio, target_memcg); |
ceff9d33 | 1439 | __delete_from_swap_cache(folio, swap, shadow); |
c449deb2 | 1440 | mem_cgroup_swapout(folio, swap); |
30472509 | 1441 | xa_unlock_irq(&mapping->i_pages); |
4081f744 | 1442 | put_swap_folio(folio, swap); |
e286781d | 1443 | } else { |
d2329aa0 | 1444 | void (*free_folio)(struct folio *); |
6072d13c | 1445 | |
d2329aa0 | 1446 | free_folio = mapping->a_ops->free_folio; |
a528910e JW |
1447 | /* |
1448 | * Remember a shadow entry for reclaimed file cache in | |
1449 | * order to detect refaults, thus thrashing, later on. | |
1450 | * | |
1451 | * But don't store shadows in an address space that is | |
238c3046 | 1452 | * already exiting. This is not just an optimization, |
a528910e JW |
1453 | * inode reclaim needs to empty out the radix tree or |
1454 | * the nodes are lost. Don't plant shadows behind its | |
1455 | * back. | |
f9fe48be RZ |
1456 | * |
1457 | * We also don't store shadows for DAX mappings because the | |
49fd9b6d | 1458 | * only page cache folios found in these are zero pages |
f9fe48be RZ |
1459 | * covering holes, and because we don't want to mix DAX |
1460 | * exceptional entries and shadow exceptional entries in the | |
b93b0163 | 1461 | * same address_space. |
a528910e | 1462 | */ |
be7c07d6 | 1463 | if (reclaimed && folio_is_file_lru(folio) && |
f9fe48be | 1464 | !mapping_exiting(mapping) && !dax_mapping(mapping)) |
8927f647 MWO |
1465 | shadow = workingset_eviction(folio, target_memcg); |
1466 | __filemap_remove_folio(folio, shadow); | |
30472509 | 1467 | xa_unlock_irq(&mapping->i_pages); |
51b8c1fe JW |
1468 | if (mapping_shrinkable(mapping)) |
1469 | inode_add_lru(mapping->host); | |
1470 | spin_unlock(&mapping->host->i_lock); | |
6072d13c | 1471 | |
d2329aa0 MWO |
1472 | if (free_folio) |
1473 | free_folio(folio); | |
49d2e9cc CL |
1474 | } |
1475 | ||
49d2e9cc CL |
1476 | return 1; |
1477 | ||
1478 | cannot_free: | |
30472509 | 1479 | xa_unlock_irq(&mapping->i_pages); |
be7c07d6 | 1480 | if (!folio_test_swapcache(folio)) |
51b8c1fe | 1481 | spin_unlock(&mapping->host->i_lock); |
49d2e9cc CL |
1482 | return 0; |
1483 | } | |
1484 | ||
5100da38 MWO |
1485 | /** |
1486 | * remove_mapping() - Attempt to remove a folio from its mapping. | |
1487 | * @mapping: The address space. | |
1488 | * @folio: The folio to remove. | |
1489 | * | |
1490 | * If the folio is dirty, under writeback or if someone else has a ref | |
1491 | * on it, removal will fail. | |
1492 | * Return: The number of pages removed from the mapping. 0 if the folio | |
1493 | * could not be removed. | |
1494 | * Context: The caller should have a single refcount on the folio and | |
1495 | * hold its lock. | |
e286781d | 1496 | */ |
5100da38 | 1497 | long remove_mapping(struct address_space *mapping, struct folio *folio) |
e286781d | 1498 | { |
be7c07d6 | 1499 | if (__remove_mapping(mapping, folio, false, NULL)) { |
e286781d | 1500 | /* |
5100da38 | 1501 | * Unfreezing the refcount with 1 effectively |
e286781d NP |
1502 | * drops the pagecache ref for us without requiring another |
1503 | * atomic operation. | |
1504 | */ | |
be7c07d6 | 1505 | folio_ref_unfreeze(folio, 1); |
5100da38 | 1506 | return folio_nr_pages(folio); |
e286781d NP |
1507 | } |
1508 | return 0; | |
1509 | } | |
1510 | ||
894bc310 | 1511 | /** |
ca6d60f3 MWO |
1512 | * folio_putback_lru - Put previously isolated folio onto appropriate LRU list. |
1513 | * @folio: Folio to be returned to an LRU list. | |
894bc310 | 1514 | * |
ca6d60f3 MWO |
1515 | * Add previously isolated @folio to appropriate LRU list. |
1516 | * The folio may still be unevictable for other reasons. | |
894bc310 | 1517 | * |
ca6d60f3 | 1518 | * Context: lru_lock must not be held, interrupts must be enabled. |
894bc310 | 1519 | */ |
ca6d60f3 | 1520 | void folio_putback_lru(struct folio *folio) |
894bc310 | 1521 | { |
ca6d60f3 MWO |
1522 | folio_add_lru(folio); |
1523 | folio_put(folio); /* drop ref from isolate */ | |
894bc310 LS |
1524 | } |
1525 | ||
49fd9b6d MWO |
1526 | enum folio_references { |
1527 | FOLIOREF_RECLAIM, | |
1528 | FOLIOREF_RECLAIM_CLEAN, | |
1529 | FOLIOREF_KEEP, | |
1530 | FOLIOREF_ACTIVATE, | |
dfc8d636 JW |
1531 | }; |
1532 | ||
49fd9b6d | 1533 | static enum folio_references folio_check_references(struct folio *folio, |
dfc8d636 JW |
1534 | struct scan_control *sc) |
1535 | { | |
d92013d1 | 1536 | int referenced_ptes, referenced_folio; |
dfc8d636 | 1537 | unsigned long vm_flags; |
dfc8d636 | 1538 | |
b3ac0413 MWO |
1539 | referenced_ptes = folio_referenced(folio, 1, sc->target_mem_cgroup, |
1540 | &vm_flags); | |
d92013d1 | 1541 | referenced_folio = folio_test_clear_referenced(folio); |
dfc8d636 | 1542 | |
dfc8d636 | 1543 | /* |
d92013d1 MWO |
1544 | * The supposedly reclaimable folio was found to be in a VM_LOCKED vma. |
1545 | * Let the folio, now marked Mlocked, be moved to the unevictable list. | |
dfc8d636 JW |
1546 | */ |
1547 | if (vm_flags & VM_LOCKED) | |
49fd9b6d | 1548 | return FOLIOREF_ACTIVATE; |
dfc8d636 | 1549 | |
6d4675e6 MK |
1550 | /* rmap lock contention: rotate */ |
1551 | if (referenced_ptes == -1) | |
49fd9b6d | 1552 | return FOLIOREF_KEEP; |
6d4675e6 | 1553 | |
64574746 | 1554 | if (referenced_ptes) { |
64574746 | 1555 | /* |
d92013d1 | 1556 | * All mapped folios start out with page table |
64574746 | 1557 | * references from the instantiating fault, so we need |
9030fb0b | 1558 | * to look twice if a mapped file/anon folio is used more |
64574746 JW |
1559 | * than once. |
1560 | * | |
1561 | * Mark it and spare it for another trip around the | |
1562 | * inactive list. Another page table reference will | |
1563 | * lead to its activation. | |
1564 | * | |
d92013d1 MWO |
1565 | * Note: the mark is set for activated folios as well |
1566 | * so that recently deactivated but used folios are | |
64574746 JW |
1567 | * quickly recovered. |
1568 | */ | |
d92013d1 | 1569 | folio_set_referenced(folio); |
64574746 | 1570 | |
d92013d1 | 1571 | if (referenced_folio || referenced_ptes > 1) |
49fd9b6d | 1572 | return FOLIOREF_ACTIVATE; |
64574746 | 1573 | |
c909e993 | 1574 | /* |
d92013d1 | 1575 | * Activate file-backed executable folios after first usage. |
c909e993 | 1576 | */ |
f19a27e3 | 1577 | if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio)) |
49fd9b6d | 1578 | return FOLIOREF_ACTIVATE; |
c909e993 | 1579 | |
49fd9b6d | 1580 | return FOLIOREF_KEEP; |
64574746 | 1581 | } |
dfc8d636 | 1582 | |
d92013d1 | 1583 | /* Reclaim if clean, defer dirty folios to writeback */ |
f19a27e3 | 1584 | if (referenced_folio && folio_is_file_lru(folio)) |
49fd9b6d | 1585 | return FOLIOREF_RECLAIM_CLEAN; |
64574746 | 1586 | |
49fd9b6d | 1587 | return FOLIOREF_RECLAIM; |
dfc8d636 JW |
1588 | } |
1589 | ||
49fd9b6d | 1590 | /* Check if a folio is dirty or under writeback */ |
e20c41b1 | 1591 | static void folio_check_dirty_writeback(struct folio *folio, |
e2be15f6 MG |
1592 | bool *dirty, bool *writeback) |
1593 | { | |
b4597226 MG |
1594 | struct address_space *mapping; |
1595 | ||
e2be15f6 | 1596 | /* |
49fd9b6d | 1597 | * Anonymous folios are not handled by flushers and must be written |
32a331a7 | 1598 | * from reclaim context. Do not stall reclaim based on them. |
49fd9b6d | 1599 | * MADV_FREE anonymous folios are put into inactive file list too. |
32a331a7 ML |
1600 | * They could be mistakenly treated as file lru. So further anon |
1601 | * test is needed. | |
e2be15f6 | 1602 | */ |
e20c41b1 MWO |
1603 | if (!folio_is_file_lru(folio) || |
1604 | (folio_test_anon(folio) && !folio_test_swapbacked(folio))) { | |
e2be15f6 MG |
1605 | *dirty = false; |
1606 | *writeback = false; | |
1607 | return; | |
1608 | } | |
1609 | ||
e20c41b1 MWO |
1610 | /* By default assume that the folio flags are accurate */ |
1611 | *dirty = folio_test_dirty(folio); | |
1612 | *writeback = folio_test_writeback(folio); | |
b4597226 MG |
1613 | |
1614 | /* Verify dirty/writeback state if the filesystem supports it */ | |
e20c41b1 | 1615 | if (!folio_test_private(folio)) |
b4597226 MG |
1616 | return; |
1617 | ||
e20c41b1 | 1618 | mapping = folio_mapping(folio); |
b4597226 | 1619 | if (mapping && mapping->a_ops->is_dirty_writeback) |
520f301c | 1620 | mapping->a_ops->is_dirty_writeback(folio, dirty, writeback); |
e2be15f6 MG |
1621 | } |
1622 | ||
32008027 | 1623 | static struct page *alloc_demote_page(struct page *page, unsigned long private) |
26aa2d19 | 1624 | { |
32008027 JG |
1625 | struct page *target_page; |
1626 | nodemask_t *allowed_mask; | |
1627 | struct migration_target_control *mtc; | |
1628 | ||
1629 | mtc = (struct migration_target_control *)private; | |
1630 | ||
1631 | allowed_mask = mtc->nmask; | |
1632 | /* | |
1633 | * make sure we allocate from the target node first also trying to | |
1634 | * demote or reclaim pages from the target node via kswapd if we are | |
1635 | * low on free memory on target node. If we don't do this and if | |
1636 | * we have free memory on the slower(lower) memtier, we would start | |
1637 | * allocating pages from slower(lower) memory tiers without even forcing | |
1638 | * a demotion of cold pages from the target memtier. This can result | |
1639 | * in the kernel placing hot pages in slower(lower) memory tiers. | |
1640 | */ | |
1641 | mtc->nmask = NULL; | |
1642 | mtc->gfp_mask |= __GFP_THISNODE; | |
1643 | target_page = alloc_migration_target(page, (unsigned long)mtc); | |
1644 | if (target_page) | |
1645 | return target_page; | |
26aa2d19 | 1646 | |
32008027 JG |
1647 | mtc->gfp_mask &= ~__GFP_THISNODE; |
1648 | mtc->nmask = allowed_mask; | |
1649 | ||
1650 | return alloc_migration_target(page, (unsigned long)mtc); | |
26aa2d19 DH |
1651 | } |
1652 | ||
1653 | /* | |
49fd9b6d MWO |
1654 | * Take folios on @demote_folios and attempt to demote them to another node. |
1655 | * Folios which are not demoted are left on @demote_folios. | |
26aa2d19 | 1656 | */ |
49fd9b6d | 1657 | static unsigned int demote_folio_list(struct list_head *demote_folios, |
26aa2d19 DH |
1658 | struct pglist_data *pgdat) |
1659 | { | |
1660 | int target_nid = next_demotion_node(pgdat->node_id); | |
1661 | unsigned int nr_succeeded; | |
32008027 JG |
1662 | nodemask_t allowed_mask; |
1663 | ||
1664 | struct migration_target_control mtc = { | |
1665 | /* | |
1666 | * Allocate from 'node', or fail quickly and quietly. | |
1667 | * When this happens, 'page' will likely just be discarded | |
1668 | * instead of migrated. | |
1669 | */ | |
1670 | .gfp_mask = (GFP_HIGHUSER_MOVABLE & ~__GFP_RECLAIM) | __GFP_NOWARN | | |
1671 | __GFP_NOMEMALLOC | GFP_NOWAIT, | |
1672 | .nid = target_nid, | |
1673 | .nmask = &allowed_mask | |
1674 | }; | |
26aa2d19 | 1675 | |
49fd9b6d | 1676 | if (list_empty(demote_folios)) |
26aa2d19 DH |
1677 | return 0; |
1678 | ||
1679 | if (target_nid == NUMA_NO_NODE) | |
1680 | return 0; | |
1681 | ||
32008027 JG |
1682 | node_get_allowed_targets(pgdat, &allowed_mask); |
1683 | ||
26aa2d19 | 1684 | /* Demotion ignores all cpuset and mempolicy settings */ |
49fd9b6d | 1685 | migrate_pages(demote_folios, alloc_demote_page, NULL, |
32008027 JG |
1686 | (unsigned long)&mtc, MIGRATE_ASYNC, MR_DEMOTION, |
1687 | &nr_succeeded); | |
26aa2d19 | 1688 | |
57e9cc50 | 1689 | __count_vm_events(PGDEMOTE_KSWAPD + reclaimer_offset(), nr_succeeded); |
668e4147 | 1690 | |
26aa2d19 DH |
1691 | return nr_succeeded; |
1692 | } | |
1693 | ||
c28a0e96 | 1694 | static bool may_enter_fs(struct folio *folio, gfp_t gfp_mask) |
d791ea67 N |
1695 | { |
1696 | if (gfp_mask & __GFP_FS) | |
1697 | return true; | |
c28a0e96 | 1698 | if (!folio_test_swapcache(folio) || !(gfp_mask & __GFP_IO)) |
d791ea67 N |
1699 | return false; |
1700 | /* | |
1701 | * We can "enter_fs" for swap-cache with only __GFP_IO | |
1702 | * providing this isn't SWP_FS_OPS. | |
1703 | * ->flags can be updated non-atomicially (scan_swap_map_slots), | |
1704 | * but that will never affect SWP_FS_OPS, so the data_race | |
1705 | * is safe. | |
1706 | */ | |
b98c359f | 1707 | return !data_race(folio_swap_flags(folio) & SWP_FS_OPS); |
d791ea67 N |
1708 | } |
1709 | ||
1da177e4 | 1710 | /* |
49fd9b6d | 1711 | * shrink_folio_list() returns the number of reclaimed pages |
1da177e4 | 1712 | */ |
49fd9b6d MWO |
1713 | static unsigned int shrink_folio_list(struct list_head *folio_list, |
1714 | struct pglist_data *pgdat, struct scan_control *sc, | |
1715 | struct reclaim_stat *stat, bool ignore_references) | |
1716 | { | |
1717 | LIST_HEAD(ret_folios); | |
1718 | LIST_HEAD(free_folios); | |
1719 | LIST_HEAD(demote_folios); | |
730ec8c0 MS |
1720 | unsigned int nr_reclaimed = 0; |
1721 | unsigned int pgactivate = 0; | |
26aa2d19 | 1722 | bool do_demote_pass; |
2282679f | 1723 | struct swap_iocb *plug = NULL; |
1da177e4 | 1724 | |
060f005f | 1725 | memset(stat, 0, sizeof(*stat)); |
1da177e4 | 1726 | cond_resched(); |
26aa2d19 | 1727 | do_demote_pass = can_demote(pgdat->node_id, sc); |
1da177e4 | 1728 | |
26aa2d19 | 1729 | retry: |
49fd9b6d | 1730 | while (!list_empty(folio_list)) { |
1da177e4 | 1731 | struct address_space *mapping; |
be7c07d6 | 1732 | struct folio *folio; |
49fd9b6d | 1733 | enum folio_references references = FOLIOREF_RECLAIM; |
d791ea67 | 1734 | bool dirty, writeback; |
98879b3b | 1735 | unsigned int nr_pages; |
1da177e4 LT |
1736 | |
1737 | cond_resched(); | |
1738 | ||
49fd9b6d | 1739 | folio = lru_to_folio(folio_list); |
be7c07d6 | 1740 | list_del(&folio->lru); |
1da177e4 | 1741 | |
c28a0e96 | 1742 | if (!folio_trylock(folio)) |
1da177e4 LT |
1743 | goto keep; |
1744 | ||
c28a0e96 | 1745 | VM_BUG_ON_FOLIO(folio_test_active(folio), folio); |
1da177e4 | 1746 | |
c28a0e96 | 1747 | nr_pages = folio_nr_pages(folio); |
98879b3b | 1748 | |
c28a0e96 | 1749 | /* Account the number of base pages */ |
98879b3b | 1750 | sc->nr_scanned += nr_pages; |
80e43426 | 1751 | |
c28a0e96 | 1752 | if (unlikely(!folio_evictable(folio))) |
ad6b6704 | 1753 | goto activate_locked; |
894bc310 | 1754 | |
1bee2c16 | 1755 | if (!sc->may_unmap && folio_mapped(folio)) |
80e43426 CL |
1756 | goto keep_locked; |
1757 | ||
018ee47f YZ |
1758 | /* folio_update_gen() tried to promote this page? */ |
1759 | if (lru_gen_enabled() && !ignore_references && | |
1760 | folio_mapped(folio) && folio_test_referenced(folio)) | |
1761 | goto keep_locked; | |
1762 | ||
e2be15f6 | 1763 | /* |
894befec | 1764 | * The number of dirty pages determines if a node is marked |
8cd7c588 | 1765 | * reclaim_congested. kswapd will stall and start writing |
c28a0e96 | 1766 | * folios if the tail of the LRU is all dirty unqueued folios. |
e2be15f6 | 1767 | */ |
e20c41b1 | 1768 | folio_check_dirty_writeback(folio, &dirty, &writeback); |
e2be15f6 | 1769 | if (dirty || writeback) |
c79b7b96 | 1770 | stat->nr_dirty += nr_pages; |
e2be15f6 MG |
1771 | |
1772 | if (dirty && !writeback) | |
c79b7b96 | 1773 | stat->nr_unqueued_dirty += nr_pages; |
e2be15f6 | 1774 | |
d04e8acd | 1775 | /* |
c28a0e96 MWO |
1776 | * Treat this folio as congested if folios are cycling |
1777 | * through the LRU so quickly that the folios marked | |
1778 | * for immediate reclaim are making it to the end of | |
1779 | * the LRU a second time. | |
d04e8acd | 1780 | */ |
c28a0e96 | 1781 | if (writeback && folio_test_reclaim(folio)) |
c79b7b96 | 1782 | stat->nr_congested += nr_pages; |
e2be15f6 | 1783 | |
283aba9f | 1784 | /* |
d33e4e14 | 1785 | * If a folio at the tail of the LRU is under writeback, there |
283aba9f MG |
1786 | * are three cases to consider. |
1787 | * | |
c28a0e96 MWO |
1788 | * 1) If reclaim is encountering an excessive number |
1789 | * of folios under writeback and this folio has both | |
1790 | * the writeback and reclaim flags set, then it | |
d33e4e14 MWO |
1791 | * indicates that folios are being queued for I/O but |
1792 | * are being recycled through the LRU before the I/O | |
1793 | * can complete. Waiting on the folio itself risks an | |
1794 | * indefinite stall if it is impossible to writeback | |
1795 | * the folio due to I/O error or disconnected storage | |
1796 | * so instead note that the LRU is being scanned too | |
1797 | * quickly and the caller can stall after the folio | |
1798 | * list has been processed. | |
283aba9f | 1799 | * |
d33e4e14 | 1800 | * 2) Global or new memcg reclaim encounters a folio that is |
ecf5fc6e MH |
1801 | * not marked for immediate reclaim, or the caller does not |
1802 | * have __GFP_FS (or __GFP_IO if it's simply going to swap, | |
d33e4e14 | 1803 | * not to fs). In this case mark the folio for immediate |
97c9341f | 1804 | * reclaim and continue scanning. |
283aba9f | 1805 | * |
d791ea67 | 1806 | * Require may_enter_fs() because we would wait on fs, which |
d33e4e14 MWO |
1807 | * may not have submitted I/O yet. And the loop driver might |
1808 | * enter reclaim, and deadlock if it waits on a folio for | |
283aba9f MG |
1809 | * which it is needed to do the write (loop masks off |
1810 | * __GFP_IO|__GFP_FS for this reason); but more thought | |
1811 | * would probably show more reasons. | |
1812 | * | |
d33e4e14 MWO |
1813 | * 3) Legacy memcg encounters a folio that already has the |
1814 | * reclaim flag set. memcg does not have any dirty folio | |
283aba9f | 1815 | * throttling so we could easily OOM just because too many |
d33e4e14 | 1816 | * folios are in writeback and there is nothing else to |
283aba9f | 1817 | * reclaim. Wait for the writeback to complete. |
c55e8d03 | 1818 | * |
d33e4e14 MWO |
1819 | * In cases 1) and 2) we activate the folios to get them out of |
1820 | * the way while we continue scanning for clean folios on the | |
c55e8d03 JW |
1821 | * inactive list and refilling from the active list. The |
1822 | * observation here is that waiting for disk writes is more | |
1823 | * expensive than potentially causing reloads down the line. | |
1824 | * Since they're marked for immediate reclaim, they won't put | |
1825 | * memory pressure on the cache working set any longer than it | |
1826 | * takes to write them to disk. | |
283aba9f | 1827 | */ |
d33e4e14 | 1828 | if (folio_test_writeback(folio)) { |
283aba9f MG |
1829 | /* Case 1 above */ |
1830 | if (current_is_kswapd() && | |
d33e4e14 | 1831 | folio_test_reclaim(folio) && |
599d0c95 | 1832 | test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { |
c79b7b96 | 1833 | stat->nr_immediate += nr_pages; |
c55e8d03 | 1834 | goto activate_locked; |
283aba9f MG |
1835 | |
1836 | /* Case 2 above */ | |
b5ead35e | 1837 | } else if (writeback_throttling_sane(sc) || |
d33e4e14 | 1838 | !folio_test_reclaim(folio) || |
c28a0e96 | 1839 | !may_enter_fs(folio, sc->gfp_mask)) { |
c3b94f44 | 1840 | /* |
d33e4e14 | 1841 | * This is slightly racy - |
c28a0e96 MWO |
1842 | * folio_end_writeback() might have |
1843 | * just cleared the reclaim flag, then | |
1844 | * setting the reclaim flag here ends up | |
1845 | * interpreted as the readahead flag - but | |
1846 | * that does not matter enough to care. | |
1847 | * What we do want is for this folio to | |
1848 | * have the reclaim flag set next time | |
1849 | * memcg reclaim reaches the tests above, | |
1850 | * so it will then wait for writeback to | |
1851 | * avoid OOM; and it's also appropriate | |
d33e4e14 | 1852 | * in global reclaim. |
c3b94f44 | 1853 | */ |
d33e4e14 | 1854 | folio_set_reclaim(folio); |
c79b7b96 | 1855 | stat->nr_writeback += nr_pages; |
c55e8d03 | 1856 | goto activate_locked; |
283aba9f MG |
1857 | |
1858 | /* Case 3 above */ | |
1859 | } else { | |
d33e4e14 MWO |
1860 | folio_unlock(folio); |
1861 | folio_wait_writeback(folio); | |
1862 | /* then go back and try same folio again */ | |
49fd9b6d | 1863 | list_add_tail(&folio->lru, folio_list); |
7fadc820 | 1864 | continue; |
e62e384e | 1865 | } |
c661b078 | 1866 | } |
1da177e4 | 1867 | |
8940b34a | 1868 | if (!ignore_references) |
d92013d1 | 1869 | references = folio_check_references(folio, sc); |
02c6de8d | 1870 | |
dfc8d636 | 1871 | switch (references) { |
49fd9b6d | 1872 | case FOLIOREF_ACTIVATE: |
1da177e4 | 1873 | goto activate_locked; |
49fd9b6d | 1874 | case FOLIOREF_KEEP: |
98879b3b | 1875 | stat->nr_ref_keep += nr_pages; |
64574746 | 1876 | goto keep_locked; |
49fd9b6d MWO |
1877 | case FOLIOREF_RECLAIM: |
1878 | case FOLIOREF_RECLAIM_CLEAN: | |
c28a0e96 | 1879 | ; /* try to reclaim the folio below */ |
dfc8d636 | 1880 | } |
1da177e4 | 1881 | |
26aa2d19 | 1882 | /* |
c28a0e96 | 1883 | * Before reclaiming the folio, try to relocate |
26aa2d19 DH |
1884 | * its contents to another node. |
1885 | */ | |
1886 | if (do_demote_pass && | |
c28a0e96 | 1887 | (thp_migration_supported() || !folio_test_large(folio))) { |
49fd9b6d | 1888 | list_add(&folio->lru, &demote_folios); |
c28a0e96 | 1889 | folio_unlock(folio); |
26aa2d19 DH |
1890 | continue; |
1891 | } | |
1892 | ||
1da177e4 LT |
1893 | /* |
1894 | * Anonymous process memory has backing store? | |
1895 | * Try to allocate it some swap space here. | |
c28a0e96 | 1896 | * Lazyfree folio could be freed directly |
1da177e4 | 1897 | */ |
c28a0e96 MWO |
1898 | if (folio_test_anon(folio) && folio_test_swapbacked(folio)) { |
1899 | if (!folio_test_swapcache(folio)) { | |
bd4c82c2 HY |
1900 | if (!(sc->gfp_mask & __GFP_IO)) |
1901 | goto keep_locked; | |
d4b4084a | 1902 | if (folio_maybe_dma_pinned(folio)) |
feb889fb | 1903 | goto keep_locked; |
c28a0e96 MWO |
1904 | if (folio_test_large(folio)) { |
1905 | /* cannot split folio, skip it */ | |
d4b4084a | 1906 | if (!can_split_folio(folio, NULL)) |
bd4c82c2 HY |
1907 | goto activate_locked; |
1908 | /* | |
c28a0e96 | 1909 | * Split folios without a PMD map right |
bd4c82c2 HY |
1910 | * away. Chances are some or all of the |
1911 | * tail pages can be freed without IO. | |
1912 | */ | |
d4b4084a | 1913 | if (!folio_entire_mapcount(folio) && |
346cf613 | 1914 | split_folio_to_list(folio, |
49fd9b6d | 1915 | folio_list)) |
bd4c82c2 HY |
1916 | goto activate_locked; |
1917 | } | |
09c02e56 MWO |
1918 | if (!add_to_swap(folio)) { |
1919 | if (!folio_test_large(folio)) | |
98879b3b | 1920 | goto activate_locked_split; |
bd4c82c2 | 1921 | /* Fallback to swap normal pages */ |
346cf613 | 1922 | if (split_folio_to_list(folio, |
49fd9b6d | 1923 | folio_list)) |
bd4c82c2 | 1924 | goto activate_locked; |
fe490cc0 HY |
1925 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
1926 | count_vm_event(THP_SWPOUT_FALLBACK); | |
1927 | #endif | |
09c02e56 | 1928 | if (!add_to_swap(folio)) |
98879b3b | 1929 | goto activate_locked_split; |
bd4c82c2 | 1930 | } |
bd4c82c2 | 1931 | } |
c28a0e96 MWO |
1932 | } else if (folio_test_swapbacked(folio) && |
1933 | folio_test_large(folio)) { | |
1934 | /* Split shmem folio */ | |
49fd9b6d | 1935 | if (split_folio_to_list(folio, folio_list)) |
7751b2da | 1936 | goto keep_locked; |
e2be15f6 | 1937 | } |
1da177e4 | 1938 | |
98879b3b | 1939 | /* |
c28a0e96 MWO |
1940 | * If the folio was split above, the tail pages will make |
1941 | * their own pass through this function and be accounted | |
1942 | * then. | |
98879b3b | 1943 | */ |
c28a0e96 | 1944 | if ((nr_pages > 1) && !folio_test_large(folio)) { |
98879b3b YS |
1945 | sc->nr_scanned -= (nr_pages - 1); |
1946 | nr_pages = 1; | |
1947 | } | |
1948 | ||
1da177e4 | 1949 | /* |
1bee2c16 | 1950 | * The folio is mapped into the page tables of one or more |
1da177e4 LT |
1951 | * processes. Try to unmap it here. |
1952 | */ | |
1bee2c16 | 1953 | if (folio_mapped(folio)) { |
013339df | 1954 | enum ttu_flags flags = TTU_BATCH_FLUSH; |
1bee2c16 | 1955 | bool was_swapbacked = folio_test_swapbacked(folio); |
bd4c82c2 | 1956 | |
1bee2c16 | 1957 | if (folio_test_pmd_mappable(folio)) |
bd4c82c2 | 1958 | flags |= TTU_SPLIT_HUGE_PMD; |
1f318a9b | 1959 | |
869f7ee6 | 1960 | try_to_unmap(folio, flags); |
1bee2c16 | 1961 | if (folio_mapped(folio)) { |
98879b3b | 1962 | stat->nr_unmap_fail += nr_pages; |
1bee2c16 MWO |
1963 | if (!was_swapbacked && |
1964 | folio_test_swapbacked(folio)) | |
1f318a9b | 1965 | stat->nr_lazyfree_fail += nr_pages; |
1da177e4 | 1966 | goto activate_locked; |
1da177e4 LT |
1967 | } |
1968 | } | |
1969 | ||
5441d490 | 1970 | mapping = folio_mapping(folio); |
49bd2bf9 | 1971 | if (folio_test_dirty(folio)) { |
ee72886d | 1972 | /* |
49bd2bf9 | 1973 | * Only kswapd can writeback filesystem folios |
4eda4823 | 1974 | * to avoid risk of stack overflow. But avoid |
49bd2bf9 | 1975 | * injecting inefficient single-folio I/O into |
4eda4823 | 1976 | * flusher writeback as much as possible: only |
49bd2bf9 MWO |
1977 | * write folios when we've encountered many |
1978 | * dirty folios, and when we've already scanned | |
1979 | * the rest of the LRU for clean folios and see | |
1980 | * the same dirty folios again (with the reclaim | |
1981 | * flag set). | |
ee72886d | 1982 | */ |
49bd2bf9 MWO |
1983 | if (folio_is_file_lru(folio) && |
1984 | (!current_is_kswapd() || | |
1985 | !folio_test_reclaim(folio) || | |
4eda4823 | 1986 | !test_bit(PGDAT_DIRTY, &pgdat->flags))) { |
49ea7eb6 MG |
1987 | /* |
1988 | * Immediately reclaim when written back. | |
5a9e3474 | 1989 | * Similar in principle to folio_deactivate() |
49bd2bf9 | 1990 | * except we already have the folio isolated |
49ea7eb6 MG |
1991 | * and know it's dirty |
1992 | */ | |
49bd2bf9 MWO |
1993 | node_stat_mod_folio(folio, NR_VMSCAN_IMMEDIATE, |
1994 | nr_pages); | |
1995 | folio_set_reclaim(folio); | |
49ea7eb6 | 1996 | |
c55e8d03 | 1997 | goto activate_locked; |
ee72886d MG |
1998 | } |
1999 | ||
49fd9b6d | 2000 | if (references == FOLIOREF_RECLAIM_CLEAN) |
1da177e4 | 2001 | goto keep_locked; |
c28a0e96 | 2002 | if (!may_enter_fs(folio, sc->gfp_mask)) |
1da177e4 | 2003 | goto keep_locked; |
52a8363e | 2004 | if (!sc->may_writepage) |
1da177e4 LT |
2005 | goto keep_locked; |
2006 | ||
d950c947 | 2007 | /* |
49bd2bf9 MWO |
2008 | * Folio is dirty. Flush the TLB if a writable entry |
2009 | * potentially exists to avoid CPU writes after I/O | |
d950c947 MG |
2010 | * starts and then write it out here. |
2011 | */ | |
2012 | try_to_unmap_flush_dirty(); | |
2282679f | 2013 | switch (pageout(folio, mapping, &plug)) { |
1da177e4 LT |
2014 | case PAGE_KEEP: |
2015 | goto keep_locked; | |
2016 | case PAGE_ACTIVATE: | |
2017 | goto activate_locked; | |
2018 | case PAGE_SUCCESS: | |
c79b7b96 | 2019 | stat->nr_pageout += nr_pages; |
96f8bf4f | 2020 | |
49bd2bf9 | 2021 | if (folio_test_writeback(folio)) |
41ac1999 | 2022 | goto keep; |
49bd2bf9 | 2023 | if (folio_test_dirty(folio)) |
1da177e4 | 2024 | goto keep; |
7d3579e8 | 2025 | |
1da177e4 LT |
2026 | /* |
2027 | * A synchronous write - probably a ramdisk. Go | |
49bd2bf9 | 2028 | * ahead and try to reclaim the folio. |
1da177e4 | 2029 | */ |
49bd2bf9 | 2030 | if (!folio_trylock(folio)) |
1da177e4 | 2031 | goto keep; |
49bd2bf9 MWO |
2032 | if (folio_test_dirty(folio) || |
2033 | folio_test_writeback(folio)) | |
1da177e4 | 2034 | goto keep_locked; |
49bd2bf9 | 2035 | mapping = folio_mapping(folio); |
01359eb2 | 2036 | fallthrough; |
1da177e4 | 2037 | case PAGE_CLEAN: |
49bd2bf9 | 2038 | ; /* try to free the folio below */ |
1da177e4 LT |
2039 | } |
2040 | } | |
2041 | ||
2042 | /* | |
0a36111c MWO |
2043 | * If the folio has buffers, try to free the buffer |
2044 | * mappings associated with this folio. If we succeed | |
2045 | * we try to free the folio as well. | |
1da177e4 | 2046 | * |
0a36111c MWO |
2047 | * We do this even if the folio is dirty. |
2048 | * filemap_release_folio() does not perform I/O, but it | |
2049 | * is possible for a folio to have the dirty flag set, | |
2050 | * but it is actually clean (all its buffers are clean). | |
2051 | * This happens if the buffers were written out directly, | |
2052 | * with submit_bh(). ext3 will do this, as well as | |
2053 | * the blockdev mapping. filemap_release_folio() will | |
2054 | * discover that cleanness and will drop the buffers | |
2055 | * and mark the folio clean - it can be freed. | |
1da177e4 | 2056 | * |
0a36111c MWO |
2057 | * Rarely, folios can have buffers and no ->mapping. |
2058 | * These are the folios which were not successfully | |
2059 | * invalidated in truncate_cleanup_folio(). We try to | |
2060 | * drop those buffers here and if that worked, and the | |
2061 | * folio is no longer mapped into process address space | |
2062 | * (refcount == 1) it can be freed. Otherwise, leave | |
2063 | * the folio on the LRU so it is swappable. | |
1da177e4 | 2064 | */ |
0a36111c MWO |
2065 | if (folio_has_private(folio)) { |
2066 | if (!filemap_release_folio(folio, sc->gfp_mask)) | |
1da177e4 | 2067 | goto activate_locked; |
0a36111c MWO |
2068 | if (!mapping && folio_ref_count(folio) == 1) { |
2069 | folio_unlock(folio); | |
2070 | if (folio_put_testzero(folio)) | |
e286781d NP |
2071 | goto free_it; |
2072 | else { | |
2073 | /* | |
2074 | * rare race with speculative reference. | |
2075 | * the speculative reference will free | |
0a36111c | 2076 | * this folio shortly, so we may |
e286781d NP |
2077 | * increment nr_reclaimed here (and |
2078 | * leave it off the LRU). | |
2079 | */ | |
9aafcffc | 2080 | nr_reclaimed += nr_pages; |
e286781d NP |
2081 | continue; |
2082 | } | |
2083 | } | |
1da177e4 LT |
2084 | } |
2085 | ||
64daa5d8 | 2086 | if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) { |
802a3a92 | 2087 | /* follow __remove_mapping for reference */ |
64daa5d8 | 2088 | if (!folio_ref_freeze(folio, 1)) |
802a3a92 | 2089 | goto keep_locked; |
d17be2d9 | 2090 | /* |
64daa5d8 | 2091 | * The folio has only one reference left, which is |
d17be2d9 | 2092 | * from the isolation. After the caller puts the |
64daa5d8 MWO |
2093 | * folio back on the lru and drops the reference, the |
2094 | * folio will be freed anyway. It doesn't matter | |
2095 | * which lru it goes on. So we don't bother checking | |
2096 | * the dirty flag here. | |
d17be2d9 | 2097 | */ |
64daa5d8 MWO |
2098 | count_vm_events(PGLAZYFREED, nr_pages); |
2099 | count_memcg_folio_events(folio, PGLAZYFREED, nr_pages); | |
be7c07d6 | 2100 | } else if (!mapping || !__remove_mapping(mapping, folio, true, |
b910718a | 2101 | sc->target_mem_cgroup)) |
802a3a92 | 2102 | goto keep_locked; |
9a1ea439 | 2103 | |
c28a0e96 | 2104 | folio_unlock(folio); |
e286781d | 2105 | free_it: |
98879b3b | 2106 | /* |
c28a0e96 MWO |
2107 | * Folio may get swapped out as a whole, need to account |
2108 | * all pages in it. | |
98879b3b YS |
2109 | */ |
2110 | nr_reclaimed += nr_pages; | |
abe4c3b5 MG |
2111 | |
2112 | /* | |
49fd9b6d | 2113 | * Is there need to periodically free_folio_list? It would |
abe4c3b5 MG |
2114 | * appear not as the counts should be low |
2115 | */ | |
c28a0e96 | 2116 | if (unlikely(folio_test_large(folio))) |
5375336c | 2117 | destroy_large_folio(folio); |
7ae88534 | 2118 | else |
49fd9b6d | 2119 | list_add(&folio->lru, &free_folios); |
1da177e4 LT |
2120 | continue; |
2121 | ||
98879b3b YS |
2122 | activate_locked_split: |
2123 | /* | |
2124 | * The tail pages that are failed to add into swap cache | |
2125 | * reach here. Fixup nr_scanned and nr_pages. | |
2126 | */ | |
2127 | if (nr_pages > 1) { | |
2128 | sc->nr_scanned -= (nr_pages - 1); | |
2129 | nr_pages = 1; | |
2130 | } | |
1da177e4 | 2131 | activate_locked: |
68a22394 | 2132 | /* Not a candidate for swapping, so reclaim swap space. */ |
246b6480 | 2133 | if (folio_test_swapcache(folio) && |
9202d527 | 2134 | (mem_cgroup_swap_full(folio) || folio_test_mlocked(folio))) |
bdb0ed54 | 2135 | folio_free_swap(folio); |
246b6480 MWO |
2136 | VM_BUG_ON_FOLIO(folio_test_active(folio), folio); |
2137 | if (!folio_test_mlocked(folio)) { | |
2138 | int type = folio_is_file_lru(folio); | |
2139 | folio_set_active(folio); | |
98879b3b | 2140 | stat->nr_activate[type] += nr_pages; |
246b6480 | 2141 | count_memcg_folio_events(folio, PGACTIVATE, nr_pages); |
ad6b6704 | 2142 | } |
1da177e4 | 2143 | keep_locked: |
c28a0e96 | 2144 | folio_unlock(folio); |
1da177e4 | 2145 | keep: |
49fd9b6d | 2146 | list_add(&folio->lru, &ret_folios); |
c28a0e96 MWO |
2147 | VM_BUG_ON_FOLIO(folio_test_lru(folio) || |
2148 | folio_test_unevictable(folio), folio); | |
1da177e4 | 2149 | } |
49fd9b6d | 2150 | /* 'folio_list' is always empty here */ |
26aa2d19 | 2151 | |
c28a0e96 | 2152 | /* Migrate folios selected for demotion */ |
49fd9b6d MWO |
2153 | nr_reclaimed += demote_folio_list(&demote_folios, pgdat); |
2154 | /* Folios that could not be demoted are still in @demote_folios */ | |
2155 | if (!list_empty(&demote_folios)) { | |
6b426d07 | 2156 | /* Folios which weren't demoted go back on @folio_list */ |
49fd9b6d | 2157 | list_splice_init(&demote_folios, folio_list); |
6b426d07 MA |
2158 | |
2159 | /* | |
2160 | * goto retry to reclaim the undemoted folios in folio_list if | |
2161 | * desired. | |
2162 | * | |
2163 | * Reclaiming directly from top tier nodes is not often desired | |
2164 | * due to it breaking the LRU ordering: in general memory | |
2165 | * should be reclaimed from lower tier nodes and demoted from | |
2166 | * top tier nodes. | |
2167 | * | |
2168 | * However, disabling reclaim from top tier nodes entirely | |
2169 | * would cause ooms in edge scenarios where lower tier memory | |
2170 | * is unreclaimable for whatever reason, eg memory being | |
2171 | * mlocked or too hot to reclaim. We can disable reclaim | |
2172 | * from top tier nodes in proactive reclaim though as that is | |
2173 | * not real memory pressure. | |
2174 | */ | |
2175 | if (!sc->proactive) { | |
2176 | do_demote_pass = false; | |
2177 | goto retry; | |
2178 | } | |
26aa2d19 | 2179 | } |
abe4c3b5 | 2180 | |
98879b3b YS |
2181 | pgactivate = stat->nr_activate[0] + stat->nr_activate[1]; |
2182 | ||
49fd9b6d | 2183 | mem_cgroup_uncharge_list(&free_folios); |
72b252ae | 2184 | try_to_unmap_flush(); |
49fd9b6d | 2185 | free_unref_page_list(&free_folios); |
abe4c3b5 | 2186 | |
49fd9b6d | 2187 | list_splice(&ret_folios, folio_list); |
886cf190 | 2188 | count_vm_events(PGACTIVATE, pgactivate); |
060f005f | 2189 | |
2282679f N |
2190 | if (plug) |
2191 | swap_write_unplug(plug); | |
05ff5137 | 2192 | return nr_reclaimed; |
1da177e4 LT |
2193 | } |
2194 | ||
730ec8c0 | 2195 | unsigned int reclaim_clean_pages_from_list(struct zone *zone, |
49fd9b6d | 2196 | struct list_head *folio_list) |
02c6de8d MK |
2197 | { |
2198 | struct scan_control sc = { | |
2199 | .gfp_mask = GFP_KERNEL, | |
02c6de8d MK |
2200 | .may_unmap = 1, |
2201 | }; | |
1f318a9b | 2202 | struct reclaim_stat stat; |
730ec8c0 | 2203 | unsigned int nr_reclaimed; |
b8cecb93 MWO |
2204 | struct folio *folio, *next; |
2205 | LIST_HEAD(clean_folios); | |
2d2b8d2b | 2206 | unsigned int noreclaim_flag; |
02c6de8d | 2207 | |
b8cecb93 MWO |
2208 | list_for_each_entry_safe(folio, next, folio_list, lru) { |
2209 | if (!folio_test_hugetlb(folio) && folio_is_file_lru(folio) && | |
2210 | !folio_test_dirty(folio) && !__folio_test_movable(folio) && | |
2211 | !folio_test_unevictable(folio)) { | |
2212 | folio_clear_active(folio); | |
2213 | list_move(&folio->lru, &clean_folios); | |
02c6de8d MK |
2214 | } |
2215 | } | |
2216 | ||
2d2b8d2b YZ |
2217 | /* |
2218 | * We should be safe here since we are only dealing with file pages and | |
2219 | * we are not kswapd and therefore cannot write dirty file pages. But | |
2220 | * call memalloc_noreclaim_save() anyway, just in case these conditions | |
2221 | * change in the future. | |
2222 | */ | |
2223 | noreclaim_flag = memalloc_noreclaim_save(); | |
49fd9b6d | 2224 | nr_reclaimed = shrink_folio_list(&clean_folios, zone->zone_pgdat, &sc, |
013339df | 2225 | &stat, true); |
2d2b8d2b YZ |
2226 | memalloc_noreclaim_restore(noreclaim_flag); |
2227 | ||
b8cecb93 | 2228 | list_splice(&clean_folios, folio_list); |
2da9f630 NP |
2229 | mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, |
2230 | -(long)nr_reclaimed); | |
1f318a9b JK |
2231 | /* |
2232 | * Since lazyfree pages are isolated from file LRU from the beginning, | |
2233 | * they will rotate back to anonymous LRU in the end if it failed to | |
2234 | * discard so isolated count will be mismatched. | |
2235 | * Compensate the isolated count for both LRU lists. | |
2236 | */ | |
2237 | mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON, | |
2238 | stat.nr_lazyfree_fail); | |
2239 | mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, | |
2da9f630 | 2240 | -(long)stat.nr_lazyfree_fail); |
1f318a9b | 2241 | return nr_reclaimed; |
02c6de8d MK |
2242 | } |
2243 | ||
7ee36a14 MG |
2244 | /* |
2245 | * Update LRU sizes after isolating pages. The LRU size updates must | |
55b65a57 | 2246 | * be complete before mem_cgroup_update_lru_size due to a sanity check. |
7ee36a14 MG |
2247 | */ |
2248 | static __always_inline void update_lru_sizes(struct lruvec *lruvec, | |
b4536f0c | 2249 | enum lru_list lru, unsigned long *nr_zone_taken) |
7ee36a14 | 2250 | { |
7ee36a14 MG |
2251 | int zid; |
2252 | ||
7ee36a14 MG |
2253 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
2254 | if (!nr_zone_taken[zid]) | |
2255 | continue; | |
2256 | ||
a892cb6b | 2257 | update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); |
b4536f0c MH |
2258 | } |
2259 | ||
7ee36a14 MG |
2260 | } |
2261 | ||
f611fab7 | 2262 | /* |
15b44736 HD |
2263 | * Isolating page from the lruvec to fill in @dst list by nr_to_scan times. |
2264 | * | |
2265 | * lruvec->lru_lock is heavily contended. Some of the functions that | |
1da177e4 LT |
2266 | * shrink the lists perform better by taking out a batch of pages |
2267 | * and working on them outside the LRU lock. | |
2268 | * | |
2269 | * For pagecache intensive workloads, this function is the hottest | |
2270 | * spot in the kernel (apart from copy_*_user functions). | |
2271 | * | |
15b44736 | 2272 | * Lru_lock must be held before calling this function. |
1da177e4 | 2273 | * |
791b48b6 | 2274 | * @nr_to_scan: The number of eligible pages to look through on the list. |
5dc35979 | 2275 | * @lruvec: The LRU vector to pull pages from. |
1da177e4 | 2276 | * @dst: The temp list to put pages on to. |
f626012d | 2277 | * @nr_scanned: The number of pages that were scanned. |
fe2c2a10 | 2278 | * @sc: The scan_control struct for this reclaim session |
3cb99451 | 2279 | * @lru: LRU list id for isolating |
1da177e4 LT |
2280 | * |
2281 | * returns how many pages were moved onto *@dst. | |
2282 | */ | |
49fd9b6d | 2283 | static unsigned long isolate_lru_folios(unsigned long nr_to_scan, |
5dc35979 | 2284 | struct lruvec *lruvec, struct list_head *dst, |
fe2c2a10 | 2285 | unsigned long *nr_scanned, struct scan_control *sc, |
a9e7c39f | 2286 | enum lru_list lru) |
1da177e4 | 2287 | { |
75b00af7 | 2288 | struct list_head *src = &lruvec->lists[lru]; |
69e05944 | 2289 | unsigned long nr_taken = 0; |
599d0c95 | 2290 | unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; |
7cc30fcf | 2291 | unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; |
3db65812 | 2292 | unsigned long skipped = 0; |
791b48b6 | 2293 | unsigned long scan, total_scan, nr_pages; |
166e3d32 | 2294 | LIST_HEAD(folios_skipped); |
1da177e4 | 2295 | |
98879b3b | 2296 | total_scan = 0; |
791b48b6 | 2297 | scan = 0; |
98879b3b | 2298 | while (scan < nr_to_scan && !list_empty(src)) { |
89f6c88a | 2299 | struct list_head *move_to = src; |
166e3d32 | 2300 | struct folio *folio; |
5ad333eb | 2301 | |
166e3d32 MWO |
2302 | folio = lru_to_folio(src); |
2303 | prefetchw_prev_lru_folio(folio, src, flags); | |
1da177e4 | 2304 | |
166e3d32 | 2305 | nr_pages = folio_nr_pages(folio); |
98879b3b YS |
2306 | total_scan += nr_pages; |
2307 | ||
166e3d32 MWO |
2308 | if (folio_zonenum(folio) > sc->reclaim_idx) { |
2309 | nr_skipped[folio_zonenum(folio)] += nr_pages; | |
2310 | move_to = &folios_skipped; | |
89f6c88a | 2311 | goto move; |
b2e18757 MG |
2312 | } |
2313 | ||
791b48b6 | 2314 | /* |
166e3d32 MWO |
2315 | * Do not count skipped folios because that makes the function |
2316 | * return with no isolated folios if the LRU mostly contains | |
2317 | * ineligible folios. This causes the VM to not reclaim any | |
2318 | * folios, triggering a premature OOM. | |
2319 | * Account all pages in a folio. | |
791b48b6 | 2320 | */ |
98879b3b | 2321 | scan += nr_pages; |
89f6c88a | 2322 | |
166e3d32 | 2323 | if (!folio_test_lru(folio)) |
89f6c88a | 2324 | goto move; |
166e3d32 | 2325 | if (!sc->may_unmap && folio_mapped(folio)) |
89f6c88a HD |
2326 | goto move; |
2327 | ||
c2135f7c | 2328 | /* |
166e3d32 MWO |
2329 | * Be careful not to clear the lru flag until after we're |
2330 | * sure the folio is not being freed elsewhere -- the | |
2331 | * folio release code relies on it. | |
c2135f7c | 2332 | */ |
166e3d32 | 2333 | if (unlikely(!folio_try_get(folio))) |
89f6c88a | 2334 | goto move; |
5ad333eb | 2335 | |
166e3d32 MWO |
2336 | if (!folio_test_clear_lru(folio)) { |
2337 | /* Another thread is already isolating this folio */ | |
2338 | folio_put(folio); | |
89f6c88a | 2339 | goto move; |
5ad333eb | 2340 | } |
c2135f7c AS |
2341 | |
2342 | nr_taken += nr_pages; | |
166e3d32 | 2343 | nr_zone_taken[folio_zonenum(folio)] += nr_pages; |
89f6c88a HD |
2344 | move_to = dst; |
2345 | move: | |
166e3d32 | 2346 | list_move(&folio->lru, move_to); |
1da177e4 LT |
2347 | } |
2348 | ||
b2e18757 | 2349 | /* |
166e3d32 | 2350 | * Splice any skipped folios to the start of the LRU list. Note that |
b2e18757 MG |
2351 | * this disrupts the LRU order when reclaiming for lower zones but |
2352 | * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX | |
166e3d32 | 2353 | * scanning would soon rescan the same folios to skip and waste lots |
b2cb6826 | 2354 | * of cpu cycles. |
b2e18757 | 2355 | */ |
166e3d32 | 2356 | if (!list_empty(&folios_skipped)) { |
7cc30fcf MG |
2357 | int zid; |
2358 | ||
166e3d32 | 2359 | list_splice(&folios_skipped, src); |
7cc30fcf MG |
2360 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
2361 | if (!nr_skipped[zid]) | |
2362 | continue; | |
2363 | ||
2364 | __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); | |
1265e3a6 | 2365 | skipped += nr_skipped[zid]; |
7cc30fcf MG |
2366 | } |
2367 | } | |
791b48b6 | 2368 | *nr_scanned = total_scan; |
1265e3a6 | 2369 | trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, |
89f6c88a HD |
2370 | total_scan, skipped, nr_taken, |
2371 | sc->may_unmap ? 0 : ISOLATE_UNMAPPED, lru); | |
b4536f0c | 2372 | update_lru_sizes(lruvec, lru, nr_zone_taken); |
1da177e4 LT |
2373 | return nr_taken; |
2374 | } | |
2375 | ||
62695a84 | 2376 | /** |
d1d8a3b4 MWO |
2377 | * folio_isolate_lru() - Try to isolate a folio from its LRU list. |
2378 | * @folio: Folio to isolate from its LRU list. | |
62695a84 | 2379 | * |
d1d8a3b4 MWO |
2380 | * Isolate a @folio from an LRU list and adjust the vmstat statistic |
2381 | * corresponding to whatever LRU list the folio was on. | |
62695a84 | 2382 | * |
d1d8a3b4 MWO |
2383 | * The folio will have its LRU flag cleared. If it was found on the |
2384 | * active list, it will have the Active flag set. If it was found on the | |
2385 | * unevictable list, it will have the Unevictable flag set. These flags | |
894bc310 | 2386 | * may need to be cleared by the caller before letting the page go. |
62695a84 | 2387 | * |
d1d8a3b4 | 2388 | * Context: |
a5d09bed | 2389 | * |
49fd9b6d MWO |
2390 | * (1) Must be called with an elevated refcount on the folio. This is a |
2391 | * fundamental difference from isolate_lru_folios() (which is called | |
62695a84 | 2392 | * without a stable reference). |
d1d8a3b4 MWO |
2393 | * (2) The lru_lock must not be held. |
2394 | * (3) Interrupts must be enabled. | |
2395 | * | |
be2d5756 BW |
2396 | * Return: true if the folio was removed from an LRU list. |
2397 | * false if the folio was not on an LRU list. | |
62695a84 | 2398 | */ |
be2d5756 | 2399 | bool folio_isolate_lru(struct folio *folio) |
62695a84 | 2400 | { |
be2d5756 | 2401 | bool ret = false; |
62695a84 | 2402 | |
d1d8a3b4 | 2403 | VM_BUG_ON_FOLIO(!folio_ref_count(folio), folio); |
0c917313 | 2404 | |
d1d8a3b4 | 2405 | if (folio_test_clear_lru(folio)) { |
fa9add64 | 2406 | struct lruvec *lruvec; |
62695a84 | 2407 | |
d1d8a3b4 | 2408 | folio_get(folio); |
e809c3fe | 2409 | lruvec = folio_lruvec_lock_irq(folio); |
d1d8a3b4 | 2410 | lruvec_del_folio(lruvec, folio); |
6168d0da | 2411 | unlock_page_lruvec_irq(lruvec); |
be2d5756 | 2412 | ret = true; |
62695a84 | 2413 | } |
d25b5bd8 | 2414 | |
62695a84 NP |
2415 | return ret; |
2416 | } | |
2417 | ||
35cd7815 | 2418 | /* |
d37dd5dc | 2419 | * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and |
178821b8 | 2420 | * then get rescheduled. When there are massive number of tasks doing page |
d37dd5dc FW |
2421 | * allocation, such sleeping direct reclaimers may keep piling up on each CPU, |
2422 | * the LRU list will go small and be scanned faster than necessary, leading to | |
2423 | * unnecessary swapping, thrashing and OOM. | |
35cd7815 | 2424 | */ |
599d0c95 | 2425 | static int too_many_isolated(struct pglist_data *pgdat, int file, |
35cd7815 RR |
2426 | struct scan_control *sc) |
2427 | { | |
2428 | unsigned long inactive, isolated; | |
d818fca1 | 2429 | bool too_many; |
35cd7815 RR |
2430 | |
2431 | if (current_is_kswapd()) | |
2432 | return 0; | |
2433 | ||
b5ead35e | 2434 | if (!writeback_throttling_sane(sc)) |
35cd7815 RR |
2435 | return 0; |
2436 | ||
2437 | if (file) { | |
599d0c95 MG |
2438 | inactive = node_page_state(pgdat, NR_INACTIVE_FILE); |
2439 | isolated = node_page_state(pgdat, NR_ISOLATED_FILE); | |
35cd7815 | 2440 | } else { |
599d0c95 MG |
2441 | inactive = node_page_state(pgdat, NR_INACTIVE_ANON); |
2442 | isolated = node_page_state(pgdat, NR_ISOLATED_ANON); | |
35cd7815 RR |
2443 | } |
2444 | ||
3cf23841 FW |
2445 | /* |
2446 | * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they | |
2447 | * won't get blocked by normal direct-reclaimers, forming a circular | |
2448 | * deadlock. | |
2449 | */ | |
d0164adc | 2450 | if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) |
3cf23841 FW |
2451 | inactive >>= 3; |
2452 | ||
d818fca1 MG |
2453 | too_many = isolated > inactive; |
2454 | ||
2455 | /* Wake up tasks throttled due to too_many_isolated. */ | |
2456 | if (!too_many) | |
2457 | wake_throttle_isolated(pgdat); | |
2458 | ||
2459 | return too_many; | |
35cd7815 RR |
2460 | } |
2461 | ||
a222f341 | 2462 | /* |
49fd9b6d | 2463 | * move_folios_to_lru() moves folios from private @list to appropriate LRU list. |
ff00a170 | 2464 | * On return, @list is reused as a list of folios to be freed by the caller. |
a222f341 KT |
2465 | * |
2466 | * Returns the number of pages moved to the given lruvec. | |
2467 | */ | |
49fd9b6d MWO |
2468 | static unsigned int move_folios_to_lru(struct lruvec *lruvec, |
2469 | struct list_head *list) | |
66635629 | 2470 | { |
a222f341 | 2471 | int nr_pages, nr_moved = 0; |
ff00a170 | 2472 | LIST_HEAD(folios_to_free); |
66635629 | 2473 | |
a222f341 | 2474 | while (!list_empty(list)) { |
ff00a170 MWO |
2475 | struct folio *folio = lru_to_folio(list); |
2476 | ||
2477 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); | |
2478 | list_del(&folio->lru); | |
2479 | if (unlikely(!folio_evictable(folio))) { | |
6168d0da | 2480 | spin_unlock_irq(&lruvec->lru_lock); |
ff00a170 | 2481 | folio_putback_lru(folio); |
6168d0da | 2482 | spin_lock_irq(&lruvec->lru_lock); |
66635629 MG |
2483 | continue; |
2484 | } | |
fa9add64 | 2485 | |
3d06afab | 2486 | /* |
ff00a170 | 2487 | * The folio_set_lru needs to be kept here for list integrity. |
3d06afab | 2488 | * Otherwise: |
49fd9b6d | 2489 | * #0 move_folios_to_lru #1 release_pages |
ff00a170 MWO |
2490 | * if (!folio_put_testzero()) |
2491 | * if (folio_put_testzero()) | |
2492 | * !lru //skip lru_lock | |
2493 | * folio_set_lru() | |
2494 | * list_add(&folio->lru,) | |
2495 | * list_add(&folio->lru,) | |
3d06afab | 2496 | */ |
ff00a170 | 2497 | folio_set_lru(folio); |
a222f341 | 2498 | |
ff00a170 MWO |
2499 | if (unlikely(folio_put_testzero(folio))) { |
2500 | __folio_clear_lru_flags(folio); | |
2bcf8879 | 2501 | |
ff00a170 | 2502 | if (unlikely(folio_test_large(folio))) { |
6168d0da | 2503 | spin_unlock_irq(&lruvec->lru_lock); |
5375336c | 2504 | destroy_large_folio(folio); |
6168d0da | 2505 | spin_lock_irq(&lruvec->lru_lock); |
2bcf8879 | 2506 | } else |
ff00a170 | 2507 | list_add(&folio->lru, &folios_to_free); |
3d06afab AS |
2508 | |
2509 | continue; | |
66635629 | 2510 | } |
3d06afab | 2511 | |
afca9157 AS |
2512 | /* |
2513 | * All pages were isolated from the same lruvec (and isolation | |
2514 | * inhibits memcg migration). | |
2515 | */ | |
ff00a170 MWO |
2516 | VM_BUG_ON_FOLIO(!folio_matches_lruvec(folio, lruvec), folio); |
2517 | lruvec_add_folio(lruvec, folio); | |
2518 | nr_pages = folio_nr_pages(folio); | |
3d06afab | 2519 | nr_moved += nr_pages; |
ff00a170 | 2520 | if (folio_test_active(folio)) |
3d06afab | 2521 | workingset_age_nonresident(lruvec, nr_pages); |
66635629 | 2522 | } |
66635629 | 2523 | |
3f79768f HD |
2524 | /* |
2525 | * To save our caller's stack, now use input list for pages to free. | |
2526 | */ | |
ff00a170 | 2527 | list_splice(&folios_to_free, list); |
a222f341 KT |
2528 | |
2529 | return nr_moved; | |
66635629 MG |
2530 | } |
2531 | ||
399ba0b9 | 2532 | /* |
5829f7db ML |
2533 | * If a kernel thread (such as nfsd for loop-back mounts) services a backing |
2534 | * device by writing to the page cache it sets PF_LOCAL_THROTTLE. In this case | |
2535 | * we should not throttle. Otherwise it is safe to do so. | |
399ba0b9 N |
2536 | */ |
2537 | static int current_may_throttle(void) | |
2538 | { | |
b9b1335e | 2539 | return !(current->flags & PF_LOCAL_THROTTLE); |
399ba0b9 N |
2540 | } |
2541 | ||
1da177e4 | 2542 | /* |
b2e18757 | 2543 | * shrink_inactive_list() is a helper for shrink_node(). It returns the number |
1742f19f | 2544 | * of reclaimed pages |
1da177e4 | 2545 | */ |
49fd9b6d MWO |
2546 | static unsigned long shrink_inactive_list(unsigned long nr_to_scan, |
2547 | struct lruvec *lruvec, struct scan_control *sc, | |
2548 | enum lru_list lru) | |
1da177e4 | 2549 | { |
49fd9b6d | 2550 | LIST_HEAD(folio_list); |
e247dbce | 2551 | unsigned long nr_scanned; |
730ec8c0 | 2552 | unsigned int nr_reclaimed = 0; |
e247dbce | 2553 | unsigned long nr_taken; |
060f005f | 2554 | struct reclaim_stat stat; |
497a6c1b | 2555 | bool file = is_file_lru(lru); |
f46b7912 | 2556 | enum vm_event_item item; |
599d0c95 | 2557 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
db73ee0d | 2558 | bool stalled = false; |
78dc583d | 2559 | |
599d0c95 | 2560 | while (unlikely(too_many_isolated(pgdat, file, sc))) { |
db73ee0d MH |
2561 | if (stalled) |
2562 | return 0; | |
2563 | ||
2564 | /* wait a bit for the reclaimer. */ | |
db73ee0d | 2565 | stalled = true; |
c3f4a9a2 | 2566 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_ISOLATED); |
35cd7815 RR |
2567 | |
2568 | /* We are about to die and free our memory. Return now. */ | |
2569 | if (fatal_signal_pending(current)) | |
2570 | return SWAP_CLUSTER_MAX; | |
2571 | } | |
2572 | ||
1da177e4 | 2573 | lru_add_drain(); |
f80c0673 | 2574 | |
6168d0da | 2575 | spin_lock_irq(&lruvec->lru_lock); |
b35ea17b | 2576 | |
49fd9b6d | 2577 | nr_taken = isolate_lru_folios(nr_to_scan, lruvec, &folio_list, |
a9e7c39f | 2578 | &nr_scanned, sc, lru); |
95d918fc | 2579 | |
599d0c95 | 2580 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); |
57e9cc50 | 2581 | item = PGSCAN_KSWAPD + reclaimer_offset(); |
b5ead35e | 2582 | if (!cgroup_reclaim(sc)) |
f46b7912 KT |
2583 | __count_vm_events(item, nr_scanned); |
2584 | __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned); | |
497a6c1b JW |
2585 | __count_vm_events(PGSCAN_ANON + file, nr_scanned); |
2586 | ||
6168d0da | 2587 | spin_unlock_irq(&lruvec->lru_lock); |
b35ea17b | 2588 | |
d563c050 | 2589 | if (nr_taken == 0) |
66635629 | 2590 | return 0; |
5ad333eb | 2591 | |
49fd9b6d | 2592 | nr_reclaimed = shrink_folio_list(&folio_list, pgdat, sc, &stat, false); |
c661b078 | 2593 | |
6168d0da | 2594 | spin_lock_irq(&lruvec->lru_lock); |
49fd9b6d | 2595 | move_folios_to_lru(lruvec, &folio_list); |
497a6c1b JW |
2596 | |
2597 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); | |
57e9cc50 | 2598 | item = PGSTEAL_KSWAPD + reclaimer_offset(); |
b5ead35e | 2599 | if (!cgroup_reclaim(sc)) |
f46b7912 KT |
2600 | __count_vm_events(item, nr_reclaimed); |
2601 | __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed); | |
497a6c1b | 2602 | __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed); |
6168d0da | 2603 | spin_unlock_irq(&lruvec->lru_lock); |
3f79768f | 2604 | |
0538a82c | 2605 | lru_note_cost(lruvec, file, stat.nr_pageout, nr_scanned - nr_reclaimed); |
49fd9b6d MWO |
2606 | mem_cgroup_uncharge_list(&folio_list); |
2607 | free_unref_page_list(&folio_list); | |
e11da5b4 | 2608 | |
1c610d5f | 2609 | /* |
49fd9b6d | 2610 | * If dirty folios are scanned that are not queued for IO, it |
1c610d5f | 2611 | * implies that flushers are not doing their job. This can |
49fd9b6d | 2612 | * happen when memory pressure pushes dirty folios to the end of |
1c610d5f AR |
2613 | * the LRU before the dirty limits are breached and the dirty |
2614 | * data has expired. It can also happen when the proportion of | |
49fd9b6d | 2615 | * dirty folios grows not through writes but through memory |
1c610d5f AR |
2616 | * pressure reclaiming all the clean cache. And in some cases, |
2617 | * the flushers simply cannot keep up with the allocation | |
2618 | * rate. Nudge the flusher threads in case they are asleep. | |
2619 | */ | |
81a70c21 | 2620 | if (stat.nr_unqueued_dirty == nr_taken) { |
1c610d5f | 2621 | wakeup_flusher_threads(WB_REASON_VMSCAN); |
81a70c21 AK |
2622 | /* |
2623 | * For cgroupv1 dirty throttling is achieved by waking up | |
2624 | * the kernel flusher here and later waiting on folios | |
2625 | * which are in writeback to finish (see shrink_folio_list()). | |
2626 | * | |
2627 | * Flusher may not be able to issue writeback quickly | |
2628 | * enough for cgroupv1 writeback throttling to work | |
2629 | * on a large system. | |
2630 | */ | |
2631 | if (!writeback_throttling_sane(sc)) | |
2632 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); | |
2633 | } | |
1c610d5f | 2634 | |
d108c772 AR |
2635 | sc->nr.dirty += stat.nr_dirty; |
2636 | sc->nr.congested += stat.nr_congested; | |
2637 | sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; | |
2638 | sc->nr.writeback += stat.nr_writeback; | |
2639 | sc->nr.immediate += stat.nr_immediate; | |
2640 | sc->nr.taken += nr_taken; | |
2641 | if (file) | |
2642 | sc->nr.file_taken += nr_taken; | |
8e950282 | 2643 | |
599d0c95 | 2644 | trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, |
d51d1e64 | 2645 | nr_scanned, nr_reclaimed, &stat, sc->priority, file); |
05ff5137 | 2646 | return nr_reclaimed; |
1da177e4 LT |
2647 | } |
2648 | ||
15b44736 | 2649 | /* |
07f67a8d | 2650 | * shrink_active_list() moves folios from the active LRU to the inactive LRU. |
15b44736 | 2651 | * |
07f67a8d | 2652 | * We move them the other way if the folio is referenced by one or more |
15b44736 HD |
2653 | * processes. |
2654 | * | |
07f67a8d | 2655 | * If the folios are mostly unmapped, the processing is fast and it is |
15b44736 | 2656 | * appropriate to hold lru_lock across the whole operation. But if |
07f67a8d MWO |
2657 | * the folios are mapped, the processing is slow (folio_referenced()), so |
2658 | * we should drop lru_lock around each folio. It's impossible to balance | |
2659 | * this, so instead we remove the folios from the LRU while processing them. | |
2660 | * It is safe to rely on the active flag against the non-LRU folios in here | |
2661 | * because nobody will play with that bit on a non-LRU folio. | |
15b44736 | 2662 | * |
07f67a8d MWO |
2663 | * The downside is that we have to touch folio->_refcount against each folio. |
2664 | * But we had to alter folio->flags anyway. | |
15b44736 | 2665 | */ |
f626012d | 2666 | static void shrink_active_list(unsigned long nr_to_scan, |
1a93be0e | 2667 | struct lruvec *lruvec, |
f16015fb | 2668 | struct scan_control *sc, |
9e3b2f8c | 2669 | enum lru_list lru) |
1da177e4 | 2670 | { |
44c241f1 | 2671 | unsigned long nr_taken; |
f626012d | 2672 | unsigned long nr_scanned; |
6fe6b7e3 | 2673 | unsigned long vm_flags; |
07f67a8d | 2674 | LIST_HEAD(l_hold); /* The folios which were snipped off */ |
8cab4754 | 2675 | LIST_HEAD(l_active); |
b69408e8 | 2676 | LIST_HEAD(l_inactive); |
9d998b4f MH |
2677 | unsigned nr_deactivate, nr_activate; |
2678 | unsigned nr_rotated = 0; | |
3cb99451 | 2679 | int file = is_file_lru(lru); |
599d0c95 | 2680 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
1da177e4 LT |
2681 | |
2682 | lru_add_drain(); | |
f80c0673 | 2683 | |
6168d0da | 2684 | spin_lock_irq(&lruvec->lru_lock); |
925b7673 | 2685 | |
49fd9b6d | 2686 | nr_taken = isolate_lru_folios(nr_to_scan, lruvec, &l_hold, |
a9e7c39f | 2687 | &nr_scanned, sc, lru); |
89b5fae5 | 2688 | |
599d0c95 | 2689 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); |
1cfb419b | 2690 | |
912c0572 SB |
2691 | if (!cgroup_reclaim(sc)) |
2692 | __count_vm_events(PGREFILL, nr_scanned); | |
2fa2690c | 2693 | __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); |
9d5e6a9f | 2694 | |
6168d0da | 2695 | spin_unlock_irq(&lruvec->lru_lock); |
1da177e4 | 2696 | |
1da177e4 | 2697 | while (!list_empty(&l_hold)) { |
b3ac0413 | 2698 | struct folio *folio; |
b3ac0413 | 2699 | |
1da177e4 | 2700 | cond_resched(); |
b3ac0413 MWO |
2701 | folio = lru_to_folio(&l_hold); |
2702 | list_del(&folio->lru); | |
7e9cd484 | 2703 | |
07f67a8d MWO |
2704 | if (unlikely(!folio_evictable(folio))) { |
2705 | folio_putback_lru(folio); | |
894bc310 LS |
2706 | continue; |
2707 | } | |
2708 | ||
cc715d99 | 2709 | if (unlikely(buffer_heads_over_limit)) { |
36a3b14b MWO |
2710 | if (folio_test_private(folio) && folio_trylock(folio)) { |
2711 | if (folio_test_private(folio)) | |
07f67a8d MWO |
2712 | filemap_release_folio(folio, 0); |
2713 | folio_unlock(folio); | |
cc715d99 MG |
2714 | } |
2715 | } | |
2716 | ||
6d4675e6 | 2717 | /* Referenced or rmap lock contention: rotate */ |
b3ac0413 | 2718 | if (folio_referenced(folio, 0, sc->target_mem_cgroup, |
6d4675e6 | 2719 | &vm_flags) != 0) { |
8cab4754 | 2720 | /* |
07f67a8d | 2721 | * Identify referenced, file-backed active folios and |
8cab4754 WF |
2722 | * give them one more trip around the active list. So |
2723 | * that executable code get better chances to stay in | |
07f67a8d | 2724 | * memory under moderate memory pressure. Anon folios |
8cab4754 | 2725 | * are not likely to be evicted by use-once streaming |
07f67a8d | 2726 | * IO, plus JVM can create lots of anon VM_EXEC folios, |
8cab4754 WF |
2727 | * so we ignore them here. |
2728 | */ | |
07f67a8d MWO |
2729 | if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio)) { |
2730 | nr_rotated += folio_nr_pages(folio); | |
2731 | list_add(&folio->lru, &l_active); | |
8cab4754 WF |
2732 | continue; |
2733 | } | |
2734 | } | |
7e9cd484 | 2735 | |
07f67a8d MWO |
2736 | folio_clear_active(folio); /* we are de-activating */ |
2737 | folio_set_workingset(folio); | |
2738 | list_add(&folio->lru, &l_inactive); | |
1da177e4 LT |
2739 | } |
2740 | ||
b555749a | 2741 | /* |
07f67a8d | 2742 | * Move folios back to the lru list. |
b555749a | 2743 | */ |
6168d0da | 2744 | spin_lock_irq(&lruvec->lru_lock); |
556adecb | 2745 | |
49fd9b6d MWO |
2746 | nr_activate = move_folios_to_lru(lruvec, &l_active); |
2747 | nr_deactivate = move_folios_to_lru(lruvec, &l_inactive); | |
07f67a8d | 2748 | /* Keep all free folios in l_active list */ |
f372d89e | 2749 | list_splice(&l_inactive, &l_active); |
9851ac13 KT |
2750 | |
2751 | __count_vm_events(PGDEACTIVATE, nr_deactivate); | |
2752 | __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate); | |
2753 | ||
599d0c95 | 2754 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); |
6168d0da | 2755 | spin_unlock_irq(&lruvec->lru_lock); |
2bcf8879 | 2756 | |
0538a82c JW |
2757 | if (nr_rotated) |
2758 | lru_note_cost(lruvec, file, 0, nr_rotated); | |
f372d89e KT |
2759 | mem_cgroup_uncharge_list(&l_active); |
2760 | free_unref_page_list(&l_active); | |
9d998b4f MH |
2761 | trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, |
2762 | nr_deactivate, nr_rotated, sc->priority, file); | |
1da177e4 LT |
2763 | } |
2764 | ||
49fd9b6d | 2765 | static unsigned int reclaim_folio_list(struct list_head *folio_list, |
1fe47c0b | 2766 | struct pglist_data *pgdat) |
1a4e58cc | 2767 | { |
1a4e58cc | 2768 | struct reclaim_stat dummy_stat; |
1fe47c0b ML |
2769 | unsigned int nr_reclaimed; |
2770 | struct folio *folio; | |
1a4e58cc MK |
2771 | struct scan_control sc = { |
2772 | .gfp_mask = GFP_KERNEL, | |
1a4e58cc MK |
2773 | .may_writepage = 1, |
2774 | .may_unmap = 1, | |
2775 | .may_swap = 1, | |
26aa2d19 | 2776 | .no_demotion = 1, |
1a4e58cc MK |
2777 | }; |
2778 | ||
49fd9b6d MWO |
2779 | nr_reclaimed = shrink_folio_list(folio_list, pgdat, &sc, &dummy_stat, false); |
2780 | while (!list_empty(folio_list)) { | |
2781 | folio = lru_to_folio(folio_list); | |
1fe47c0b ML |
2782 | list_del(&folio->lru); |
2783 | folio_putback_lru(folio); | |
2784 | } | |
2785 | ||
2786 | return nr_reclaimed; | |
2787 | } | |
2788 | ||
a83f0551 | 2789 | unsigned long reclaim_pages(struct list_head *folio_list) |
1fe47c0b | 2790 | { |
ed657e55 | 2791 | int nid; |
1fe47c0b | 2792 | unsigned int nr_reclaimed = 0; |
a83f0551 | 2793 | LIST_HEAD(node_folio_list); |
1fe47c0b ML |
2794 | unsigned int noreclaim_flag; |
2795 | ||
a83f0551 | 2796 | if (list_empty(folio_list)) |
1ae65e27 WY |
2797 | return nr_reclaimed; |
2798 | ||
2d2b8d2b YZ |
2799 | noreclaim_flag = memalloc_noreclaim_save(); |
2800 | ||
a83f0551 | 2801 | nid = folio_nid(lru_to_folio(folio_list)); |
1ae65e27 | 2802 | do { |
a83f0551 | 2803 | struct folio *folio = lru_to_folio(folio_list); |
1a4e58cc | 2804 | |
a83f0551 MWO |
2805 | if (nid == folio_nid(folio)) { |
2806 | folio_clear_active(folio); | |
2807 | list_move(&folio->lru, &node_folio_list); | |
1a4e58cc MK |
2808 | continue; |
2809 | } | |
2810 | ||
49fd9b6d | 2811 | nr_reclaimed += reclaim_folio_list(&node_folio_list, NODE_DATA(nid)); |
a83f0551 MWO |
2812 | nid = folio_nid(lru_to_folio(folio_list)); |
2813 | } while (!list_empty(folio_list)); | |
1a4e58cc | 2814 | |
49fd9b6d | 2815 | nr_reclaimed += reclaim_folio_list(&node_folio_list, NODE_DATA(nid)); |
1a4e58cc | 2816 | |
2d2b8d2b YZ |
2817 | memalloc_noreclaim_restore(noreclaim_flag); |
2818 | ||
1a4e58cc MK |
2819 | return nr_reclaimed; |
2820 | } | |
2821 | ||
b91ac374 JW |
2822 | static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, |
2823 | struct lruvec *lruvec, struct scan_control *sc) | |
2824 | { | |
2825 | if (is_active_lru(lru)) { | |
2826 | if (sc->may_deactivate & (1 << is_file_lru(lru))) | |
2827 | shrink_active_list(nr_to_scan, lruvec, sc, lru); | |
2828 | else | |
2829 | sc->skipped_deactivate = 1; | |
2830 | return 0; | |
2831 | } | |
2832 | ||
2833 | return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); | |
2834 | } | |
2835 | ||
59dc76b0 RR |
2836 | /* |
2837 | * The inactive anon list should be small enough that the VM never has | |
2838 | * to do too much work. | |
14797e23 | 2839 | * |
59dc76b0 RR |
2840 | * The inactive file list should be small enough to leave most memory |
2841 | * to the established workingset on the scan-resistant active list, | |
2842 | * but large enough to avoid thrashing the aggregate readahead window. | |
56e49d21 | 2843 | * |
59dc76b0 | 2844 | * Both inactive lists should also be large enough that each inactive |
49fd9b6d | 2845 | * folio has a chance to be referenced again before it is reclaimed. |
56e49d21 | 2846 | * |
2a2e4885 JW |
2847 | * If that fails and refaulting is observed, the inactive list grows. |
2848 | * | |
49fd9b6d | 2849 | * The inactive_ratio is the target ratio of ACTIVE to INACTIVE folios |
3a50d14d | 2850 | * on this LRU, maintained by the pageout code. An inactive_ratio |
49fd9b6d | 2851 | * of 3 means 3:1 or 25% of the folios are kept on the inactive list. |
56e49d21 | 2852 | * |
59dc76b0 RR |
2853 | * total target max |
2854 | * memory ratio inactive | |
2855 | * ------------------------------------- | |
2856 | * 10MB 1 5MB | |
2857 | * 100MB 1 50MB | |
2858 | * 1GB 3 250MB | |
2859 | * 10GB 10 0.9GB | |
2860 | * 100GB 31 3GB | |
2861 | * 1TB 101 10GB | |
2862 | * 10TB 320 32GB | |
56e49d21 | 2863 | */ |
b91ac374 | 2864 | static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru) |
56e49d21 | 2865 | { |
b91ac374 | 2866 | enum lru_list active_lru = inactive_lru + LRU_ACTIVE; |
2a2e4885 JW |
2867 | unsigned long inactive, active; |
2868 | unsigned long inactive_ratio; | |
59dc76b0 | 2869 | unsigned long gb; |
e3790144 | 2870 | |
b91ac374 JW |
2871 | inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru); |
2872 | active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru); | |
f8d1a311 | 2873 | |
b91ac374 | 2874 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
4002570c | 2875 | if (gb) |
b91ac374 JW |
2876 | inactive_ratio = int_sqrt(10 * gb); |
2877 | else | |
2878 | inactive_ratio = 1; | |
fd538803 | 2879 | |
59dc76b0 | 2880 | return inactive * inactive_ratio < active; |
b39415b2 RR |
2881 | } |
2882 | ||
9a265114 JW |
2883 | enum scan_balance { |
2884 | SCAN_EQUAL, | |
2885 | SCAN_FRACT, | |
2886 | SCAN_ANON, | |
2887 | SCAN_FILE, | |
2888 | }; | |
2889 | ||
f1e1a7be YZ |
2890 | static void prepare_scan_count(pg_data_t *pgdat, struct scan_control *sc) |
2891 | { | |
2892 | unsigned long file; | |
2893 | struct lruvec *target_lruvec; | |
2894 | ||
ac35a490 YZ |
2895 | if (lru_gen_enabled()) |
2896 | return; | |
2897 | ||
f1e1a7be YZ |
2898 | target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); |
2899 | ||
2900 | /* | |
2901 | * Flush the memory cgroup stats, so that we read accurate per-memcg | |
2902 | * lruvec stats for heuristics. | |
2903 | */ | |
0d856cfe | 2904 | mem_cgroup_flush_stats(); |
f1e1a7be YZ |
2905 | |
2906 | /* | |
2907 | * Determine the scan balance between anon and file LRUs. | |
2908 | */ | |
2909 | spin_lock_irq(&target_lruvec->lru_lock); | |
2910 | sc->anon_cost = target_lruvec->anon_cost; | |
2911 | sc->file_cost = target_lruvec->file_cost; | |
2912 | spin_unlock_irq(&target_lruvec->lru_lock); | |
2913 | ||
2914 | /* | |
2915 | * Target desirable inactive:active list ratios for the anon | |
2916 | * and file LRU lists. | |
2917 | */ | |
2918 | if (!sc->force_deactivate) { | |
2919 | unsigned long refaults; | |
2920 | ||
2921 | /* | |
2922 | * When refaults are being observed, it means a new | |
2923 | * workingset is being established. Deactivate to get | |
2924 | * rid of any stale active pages quickly. | |
2925 | */ | |
2926 | refaults = lruvec_page_state(target_lruvec, | |
2927 | WORKINGSET_ACTIVATE_ANON); | |
2928 | if (refaults != target_lruvec->refaults[WORKINGSET_ANON] || | |
2929 | inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)) | |
2930 | sc->may_deactivate |= DEACTIVATE_ANON; | |
2931 | else | |
2932 | sc->may_deactivate &= ~DEACTIVATE_ANON; | |
2933 | ||
2934 | refaults = lruvec_page_state(target_lruvec, | |
2935 | WORKINGSET_ACTIVATE_FILE); | |
2936 | if (refaults != target_lruvec->refaults[WORKINGSET_FILE] || | |
2937 | inactive_is_low(target_lruvec, LRU_INACTIVE_FILE)) | |
2938 | sc->may_deactivate |= DEACTIVATE_FILE; | |
2939 | else | |
2940 | sc->may_deactivate &= ~DEACTIVATE_FILE; | |
2941 | } else | |
2942 | sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE; | |
2943 | ||
2944 | /* | |
2945 | * If we have plenty of inactive file pages that aren't | |
2946 | * thrashing, try to reclaim those first before touching | |
2947 | * anonymous pages. | |
2948 | */ | |
2949 | file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE); | |
2950 | if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE)) | |
2951 | sc->cache_trim_mode = 1; | |
2952 | else | |
2953 | sc->cache_trim_mode = 0; | |
2954 | ||
2955 | /* | |
2956 | * Prevent the reclaimer from falling into the cache trap: as | |
2957 | * cache pages start out inactive, every cache fault will tip | |
2958 | * the scan balance towards the file LRU. And as the file LRU | |
2959 | * shrinks, so does the window for rotation from references. | |
2960 | * This means we have a runaway feedback loop where a tiny | |
2961 | * thrashing file LRU becomes infinitely more attractive than | |
2962 | * anon pages. Try to detect this based on file LRU size. | |
2963 | */ | |
2964 | if (!cgroup_reclaim(sc)) { | |
2965 | unsigned long total_high_wmark = 0; | |
2966 | unsigned long free, anon; | |
2967 | int z; | |
2968 | ||
2969 | free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); | |
2970 | file = node_page_state(pgdat, NR_ACTIVE_FILE) + | |
2971 | node_page_state(pgdat, NR_INACTIVE_FILE); | |
2972 | ||
2973 | for (z = 0; z < MAX_NR_ZONES; z++) { | |
2974 | struct zone *zone = &pgdat->node_zones[z]; | |
2975 | ||
2976 | if (!managed_zone(zone)) | |
2977 | continue; | |
2978 | ||
2979 | total_high_wmark += high_wmark_pages(zone); | |
2980 | } | |
2981 | ||
2982 | /* | |
2983 | * Consider anon: if that's low too, this isn't a | |
2984 | * runaway file reclaim problem, but rather just | |
2985 | * extreme pressure. Reclaim as per usual then. | |
2986 | */ | |
2987 | anon = node_page_state(pgdat, NR_INACTIVE_ANON); | |
2988 | ||
2989 | sc->file_is_tiny = | |
2990 | file + free <= total_high_wmark && | |
2991 | !(sc->may_deactivate & DEACTIVATE_ANON) && | |
2992 | anon >> sc->priority; | |
2993 | } | |
2994 | } | |
2995 | ||
4f98a2fe RR |
2996 | /* |
2997 | * Determine how aggressively the anon and file LRU lists should be | |
02e458d8 | 2998 | * scanned. |
4f98a2fe | 2999 | * |
49fd9b6d MWO |
3000 | * nr[0] = anon inactive folios to scan; nr[1] = anon active folios to scan |
3001 | * nr[2] = file inactive folios to scan; nr[3] = file active folios to scan | |
4f98a2fe | 3002 | */ |
afaf07a6 JW |
3003 | static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, |
3004 | unsigned long *nr) | |
4f98a2fe | 3005 | { |
a2a36488 | 3006 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
afaf07a6 | 3007 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); |
d483a5dd | 3008 | unsigned long anon_cost, file_cost, total_cost; |
33377678 | 3009 | int swappiness = mem_cgroup_swappiness(memcg); |
ed017373 | 3010 | u64 fraction[ANON_AND_FILE]; |
9a265114 | 3011 | u64 denominator = 0; /* gcc */ |
9a265114 | 3012 | enum scan_balance scan_balance; |
4f98a2fe | 3013 | unsigned long ap, fp; |
4111304d | 3014 | enum lru_list lru; |
76a33fc3 | 3015 | |
49fd9b6d | 3016 | /* If we have no swap space, do not bother scanning anon folios. */ |
a2a36488 | 3017 | if (!sc->may_swap || !can_reclaim_anon_pages(memcg, pgdat->node_id, sc)) { |
9a265114 | 3018 | scan_balance = SCAN_FILE; |
76a33fc3 SL |
3019 | goto out; |
3020 | } | |
4f98a2fe | 3021 | |
10316b31 JW |
3022 | /* |
3023 | * Global reclaim will swap to prevent OOM even with no | |
3024 | * swappiness, but memcg users want to use this knob to | |
3025 | * disable swapping for individual groups completely when | |
3026 | * using the memory controller's swap limit feature would be | |
3027 | * too expensive. | |
3028 | */ | |
b5ead35e | 3029 | if (cgroup_reclaim(sc) && !swappiness) { |
9a265114 | 3030 | scan_balance = SCAN_FILE; |
10316b31 JW |
3031 | goto out; |
3032 | } | |
3033 | ||
3034 | /* | |
3035 | * Do not apply any pressure balancing cleverness when the | |
3036 | * system is close to OOM, scan both anon and file equally | |
3037 | * (unless the swappiness setting disagrees with swapping). | |
3038 | */ | |
02695175 | 3039 | if (!sc->priority && swappiness) { |
9a265114 | 3040 | scan_balance = SCAN_EQUAL; |
10316b31 JW |
3041 | goto out; |
3042 | } | |
3043 | ||
62376251 | 3044 | /* |
53138cea | 3045 | * If the system is almost out of file pages, force-scan anon. |
62376251 | 3046 | */ |
b91ac374 | 3047 | if (sc->file_is_tiny) { |
53138cea JW |
3048 | scan_balance = SCAN_ANON; |
3049 | goto out; | |
62376251 JW |
3050 | } |
3051 | ||
7c5bd705 | 3052 | /* |
b91ac374 JW |
3053 | * If there is enough inactive page cache, we do not reclaim |
3054 | * anything from the anonymous working right now. | |
7c5bd705 | 3055 | */ |
b91ac374 | 3056 | if (sc->cache_trim_mode) { |
9a265114 | 3057 | scan_balance = SCAN_FILE; |
7c5bd705 JW |
3058 | goto out; |
3059 | } | |
3060 | ||
9a265114 | 3061 | scan_balance = SCAN_FRACT; |
58c37f6e | 3062 | /* |
314b57fb JW |
3063 | * Calculate the pressure balance between anon and file pages. |
3064 | * | |
3065 | * The amount of pressure we put on each LRU is inversely | |
3066 | * proportional to the cost of reclaiming each list, as | |
3067 | * determined by the share of pages that are refaulting, times | |
3068 | * the relative IO cost of bringing back a swapped out | |
3069 | * anonymous page vs reloading a filesystem page (swappiness). | |
3070 | * | |
d483a5dd JW |
3071 | * Although we limit that influence to ensure no list gets |
3072 | * left behind completely: at least a third of the pressure is | |
3073 | * applied, before swappiness. | |
3074 | * | |
314b57fb | 3075 | * With swappiness at 100, anon and file have equal IO cost. |
58c37f6e | 3076 | */ |
d483a5dd JW |
3077 | total_cost = sc->anon_cost + sc->file_cost; |
3078 | anon_cost = total_cost + sc->anon_cost; | |
3079 | file_cost = total_cost + sc->file_cost; | |
3080 | total_cost = anon_cost + file_cost; | |
58c37f6e | 3081 | |
d483a5dd JW |
3082 | ap = swappiness * (total_cost + 1); |
3083 | ap /= anon_cost + 1; | |
4f98a2fe | 3084 | |
d483a5dd JW |
3085 | fp = (200 - swappiness) * (total_cost + 1); |
3086 | fp /= file_cost + 1; | |
4f98a2fe | 3087 | |
76a33fc3 SL |
3088 | fraction[0] = ap; |
3089 | fraction[1] = fp; | |
a4fe1631 | 3090 | denominator = ap + fp; |
76a33fc3 | 3091 | out: |
688035f7 JW |
3092 | for_each_evictable_lru(lru) { |
3093 | int file = is_file_lru(lru); | |
9783aa99 | 3094 | unsigned long lruvec_size; |
f56ce412 | 3095 | unsigned long low, min; |
688035f7 | 3096 | unsigned long scan; |
9783aa99 CD |
3097 | |
3098 | lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); | |
f56ce412 JW |
3099 | mem_cgroup_protection(sc->target_mem_cgroup, memcg, |
3100 | &min, &low); | |
9783aa99 | 3101 | |
f56ce412 | 3102 | if (min || low) { |
9783aa99 CD |
3103 | /* |
3104 | * Scale a cgroup's reclaim pressure by proportioning | |
3105 | * its current usage to its memory.low or memory.min | |
3106 | * setting. | |
3107 | * | |
3108 | * This is important, as otherwise scanning aggression | |
3109 | * becomes extremely binary -- from nothing as we | |
3110 | * approach the memory protection threshold, to totally | |
3111 | * nominal as we exceed it. This results in requiring | |
3112 | * setting extremely liberal protection thresholds. It | |
3113 | * also means we simply get no protection at all if we | |
3114 | * set it too low, which is not ideal. | |
1bc63fb1 CD |
3115 | * |
3116 | * If there is any protection in place, we reduce scan | |
3117 | * pressure by how much of the total memory used is | |
3118 | * within protection thresholds. | |
9783aa99 | 3119 | * |
9de7ca46 CD |
3120 | * There is one special case: in the first reclaim pass, |
3121 | * we skip over all groups that are within their low | |
3122 | * protection. If that fails to reclaim enough pages to | |
3123 | * satisfy the reclaim goal, we come back and override | |
3124 | * the best-effort low protection. However, we still | |
3125 | * ideally want to honor how well-behaved groups are in | |
3126 | * that case instead of simply punishing them all | |
3127 | * equally. As such, we reclaim them based on how much | |
1bc63fb1 CD |
3128 | * memory they are using, reducing the scan pressure |
3129 | * again by how much of the total memory used is under | |
3130 | * hard protection. | |
9783aa99 | 3131 | */ |
1bc63fb1 | 3132 | unsigned long cgroup_size = mem_cgroup_size(memcg); |
f56ce412 JW |
3133 | unsigned long protection; |
3134 | ||
3135 | /* memory.low scaling, make sure we retry before OOM */ | |
3136 | if (!sc->memcg_low_reclaim && low > min) { | |
3137 | protection = low; | |
3138 | sc->memcg_low_skipped = 1; | |
3139 | } else { | |
3140 | protection = min; | |
3141 | } | |
1bc63fb1 CD |
3142 | |
3143 | /* Avoid TOCTOU with earlier protection check */ | |
3144 | cgroup_size = max(cgroup_size, protection); | |
3145 | ||
3146 | scan = lruvec_size - lruvec_size * protection / | |
32d4f4b7 | 3147 | (cgroup_size + 1); |
9783aa99 CD |
3148 | |
3149 | /* | |
1bc63fb1 | 3150 | * Minimally target SWAP_CLUSTER_MAX pages to keep |
55b65a57 | 3151 | * reclaim moving forwards, avoiding decrementing |
9de7ca46 | 3152 | * sc->priority further than desirable. |
9783aa99 | 3153 | */ |
1bc63fb1 | 3154 | scan = max(scan, SWAP_CLUSTER_MAX); |
9783aa99 CD |
3155 | } else { |
3156 | scan = lruvec_size; | |
3157 | } | |
3158 | ||
3159 | scan >>= sc->priority; | |
6b4f7799 | 3160 | |
688035f7 JW |
3161 | /* |
3162 | * If the cgroup's already been deleted, make sure to | |
3163 | * scrape out the remaining cache. | |
3164 | */ | |
3165 | if (!scan && !mem_cgroup_online(memcg)) | |
9783aa99 | 3166 | scan = min(lruvec_size, SWAP_CLUSTER_MAX); |
6b4f7799 | 3167 | |
688035f7 JW |
3168 | switch (scan_balance) { |
3169 | case SCAN_EQUAL: | |
3170 | /* Scan lists relative to size */ | |
3171 | break; | |
3172 | case SCAN_FRACT: | |
9a265114 | 3173 | /* |
688035f7 JW |
3174 | * Scan types proportional to swappiness and |
3175 | * their relative recent reclaim efficiency. | |
76073c64 GS |
3176 | * Make sure we don't miss the last page on |
3177 | * the offlined memory cgroups because of a | |
3178 | * round-off error. | |
9a265114 | 3179 | */ |
76073c64 GS |
3180 | scan = mem_cgroup_online(memcg) ? |
3181 | div64_u64(scan * fraction[file], denominator) : | |
3182 | DIV64_U64_ROUND_UP(scan * fraction[file], | |
68600f62 | 3183 | denominator); |
688035f7 JW |
3184 | break; |
3185 | case SCAN_FILE: | |
3186 | case SCAN_ANON: | |
3187 | /* Scan one type exclusively */ | |
e072bff6 | 3188 | if ((scan_balance == SCAN_FILE) != file) |
688035f7 | 3189 | scan = 0; |
688035f7 JW |
3190 | break; |
3191 | default: | |
3192 | /* Look ma, no brain */ | |
3193 | BUG(); | |
9a265114 | 3194 | } |
688035f7 | 3195 | |
688035f7 | 3196 | nr[lru] = scan; |
76a33fc3 | 3197 | } |
6e08a369 | 3198 | } |
4f98a2fe | 3199 | |
2f368a9f DH |
3200 | /* |
3201 | * Anonymous LRU management is a waste if there is | |
3202 | * ultimately no way to reclaim the memory. | |
3203 | */ | |
3204 | static bool can_age_anon_pages(struct pglist_data *pgdat, | |
3205 | struct scan_control *sc) | |
3206 | { | |
3207 | /* Aging the anon LRU is valuable if swap is present: */ | |
3208 | if (total_swap_pages > 0) | |
3209 | return true; | |
3210 | ||
3211 | /* Also valuable if anon pages can be demoted: */ | |
3212 | return can_demote(pgdat->node_id, sc); | |
3213 | } | |
3214 | ||
ec1c86b2 YZ |
3215 | #ifdef CONFIG_LRU_GEN |
3216 | ||
354ed597 YZ |
3217 | #ifdef CONFIG_LRU_GEN_ENABLED |
3218 | DEFINE_STATIC_KEY_ARRAY_TRUE(lru_gen_caps, NR_LRU_GEN_CAPS); | |
3219 | #define get_cap(cap) static_branch_likely(&lru_gen_caps[cap]) | |
3220 | #else | |
3221 | DEFINE_STATIC_KEY_ARRAY_FALSE(lru_gen_caps, NR_LRU_GEN_CAPS); | |
3222 | #define get_cap(cap) static_branch_unlikely(&lru_gen_caps[cap]) | |
3223 | #endif | |
3224 | ||
ec1c86b2 YZ |
3225 | /****************************************************************************** |
3226 | * shorthand helpers | |
3227 | ******************************************************************************/ | |
3228 | ||
ac35a490 YZ |
3229 | #define LRU_REFS_FLAGS (BIT(PG_referenced) | BIT(PG_workingset)) |
3230 | ||
3231 | #define DEFINE_MAX_SEQ(lruvec) \ | |
3232 | unsigned long max_seq = READ_ONCE((lruvec)->lrugen.max_seq) | |
3233 | ||
3234 | #define DEFINE_MIN_SEQ(lruvec) \ | |
3235 | unsigned long min_seq[ANON_AND_FILE] = { \ | |
3236 | READ_ONCE((lruvec)->lrugen.min_seq[LRU_GEN_ANON]), \ | |
3237 | READ_ONCE((lruvec)->lrugen.min_seq[LRU_GEN_FILE]), \ | |
3238 | } | |
3239 | ||
ec1c86b2 YZ |
3240 | #define for_each_gen_type_zone(gen, type, zone) \ |
3241 | for ((gen) = 0; (gen) < MAX_NR_GENS; (gen)++) \ | |
3242 | for ((type) = 0; (type) < ANON_AND_FILE; (type)++) \ | |
3243 | for ((zone) = 0; (zone) < MAX_NR_ZONES; (zone)++) | |
3244 | ||
e4dde56c YZ |
3245 | #define get_memcg_gen(seq) ((seq) % MEMCG_NR_GENS) |
3246 | #define get_memcg_bin(bin) ((bin) % MEMCG_NR_BINS) | |
3247 | ||
bd74fdae | 3248 | static struct lruvec *get_lruvec(struct mem_cgroup *memcg, int nid) |
ec1c86b2 YZ |
3249 | { |
3250 | struct pglist_data *pgdat = NODE_DATA(nid); | |
3251 | ||
3252 | #ifdef CONFIG_MEMCG | |
3253 | if (memcg) { | |
3254 | struct lruvec *lruvec = &memcg->nodeinfo[nid]->lruvec; | |
3255 | ||
931b6a8b | 3256 | /* see the comment in mem_cgroup_lruvec() */ |
ec1c86b2 YZ |
3257 | if (!lruvec->pgdat) |
3258 | lruvec->pgdat = pgdat; | |
3259 | ||
3260 | return lruvec; | |
3261 | } | |
3262 | #endif | |
3263 | VM_WARN_ON_ONCE(!mem_cgroup_disabled()); | |
3264 | ||
931b6a8b | 3265 | return &pgdat->__lruvec; |
ec1c86b2 YZ |
3266 | } |
3267 | ||
ac35a490 YZ |
3268 | static int get_swappiness(struct lruvec *lruvec, struct scan_control *sc) |
3269 | { | |
3270 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
3271 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); | |
3272 | ||
e9d4e1ee YZ |
3273 | if (!sc->may_swap) |
3274 | return 0; | |
3275 | ||
ac35a490 YZ |
3276 | if (!can_demote(pgdat->node_id, sc) && |
3277 | mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH) | |
3278 | return 0; | |
3279 | ||
3280 | return mem_cgroup_swappiness(memcg); | |
3281 | } | |
3282 | ||
3283 | static int get_nr_gens(struct lruvec *lruvec, int type) | |
3284 | { | |
3285 | return lruvec->lrugen.max_seq - lruvec->lrugen.min_seq[type] + 1; | |
3286 | } | |
3287 | ||
3288 | static bool __maybe_unused seq_is_valid(struct lruvec *lruvec) | |
3289 | { | |
391655fe | 3290 | /* see the comment on lru_gen_folio */ |
ac35a490 YZ |
3291 | return get_nr_gens(lruvec, LRU_GEN_FILE) >= MIN_NR_GENS && |
3292 | get_nr_gens(lruvec, LRU_GEN_FILE) <= get_nr_gens(lruvec, LRU_GEN_ANON) && | |
3293 | get_nr_gens(lruvec, LRU_GEN_ANON) <= MAX_NR_GENS; | |
3294 | } | |
3295 | ||
ccbbbb85 A |
3296 | /****************************************************************************** |
3297 | * Bloom filters | |
3298 | ******************************************************************************/ | |
3299 | ||
3300 | /* | |
3301 | * Bloom filters with m=1<<15, k=2 and the false positive rates of ~1/5 when | |
3302 | * n=10,000 and ~1/2 when n=20,000, where, conventionally, m is the number of | |
3303 | * bits in a bitmap, k is the number of hash functions and n is the number of | |
3304 | * inserted items. | |
3305 | * | |
3306 | * Page table walkers use one of the two filters to reduce their search space. | |
3307 | * To get rid of non-leaf entries that no longer have enough leaf entries, the | |
3308 | * aging uses the double-buffering technique to flip to the other filter each | |
3309 | * time it produces a new generation. For non-leaf entries that have enough | |
3310 | * leaf entries, the aging carries them over to the next generation in | |
3311 | * walk_pmd_range(); the eviction also report them when walking the rmap | |
3312 | * in lru_gen_look_around(). | |
3313 | * | |
3314 | * For future optimizations: | |
3315 | * 1. It's not necessary to keep both filters all the time. The spare one can be | |
3316 | * freed after the RCU grace period and reallocated if needed again. | |
3317 | * 2. And when reallocating, it's worth scaling its size according to the number | |
3318 | * of inserted entries in the other filter, to reduce the memory overhead on | |
3319 | * small systems and false positives on large systems. | |
3320 | * 3. Jenkins' hash function is an alternative to Knuth's. | |
3321 | */ | |
3322 | #define BLOOM_FILTER_SHIFT 15 | |
3323 | ||
3324 | static inline int filter_gen_from_seq(unsigned long seq) | |
3325 | { | |
3326 | return seq % NR_BLOOM_FILTERS; | |
3327 | } | |
3328 | ||
3329 | static void get_item_key(void *item, int *key) | |
3330 | { | |
3331 | u32 hash = hash_ptr(item, BLOOM_FILTER_SHIFT * 2); | |
3332 | ||
3333 | BUILD_BUG_ON(BLOOM_FILTER_SHIFT * 2 > BITS_PER_TYPE(u32)); | |
3334 | ||
3335 | key[0] = hash & (BIT(BLOOM_FILTER_SHIFT) - 1); | |
3336 | key[1] = hash >> BLOOM_FILTER_SHIFT; | |
3337 | } | |
3338 | ||
3339 | static bool test_bloom_filter(struct lruvec *lruvec, unsigned long seq, void *item) | |
3340 | { | |
3341 | int key[2]; | |
3342 | unsigned long *filter; | |
3343 | int gen = filter_gen_from_seq(seq); | |
3344 | ||
3345 | filter = READ_ONCE(lruvec->mm_state.filters[gen]); | |
3346 | if (!filter) | |
3347 | return true; | |
3348 | ||
3349 | get_item_key(item, key); | |
3350 | ||
3351 | return test_bit(key[0], filter) && test_bit(key[1], filter); | |
3352 | } | |
3353 | ||
3354 | static void update_bloom_filter(struct lruvec *lruvec, unsigned long seq, void *item) | |
3355 | { | |
3356 | int key[2]; | |
3357 | unsigned long *filter; | |
3358 | int gen = filter_gen_from_seq(seq); | |
3359 | ||
3360 | filter = READ_ONCE(lruvec->mm_state.filters[gen]); | |
3361 | if (!filter) | |
3362 | return; | |
3363 | ||
3364 | get_item_key(item, key); | |
3365 | ||
3366 | if (!test_bit(key[0], filter)) | |
3367 | set_bit(key[0], filter); | |
3368 | if (!test_bit(key[1], filter)) | |
3369 | set_bit(key[1], filter); | |
3370 | } | |
3371 | ||
3372 | static void reset_bloom_filter(struct lruvec *lruvec, unsigned long seq) | |
3373 | { | |
3374 | unsigned long *filter; | |
3375 | int gen = filter_gen_from_seq(seq); | |
3376 | ||
3377 | filter = lruvec->mm_state.filters[gen]; | |
3378 | if (filter) { | |
3379 | bitmap_clear(filter, 0, BIT(BLOOM_FILTER_SHIFT)); | |
3380 | return; | |
3381 | } | |
3382 | ||
3383 | filter = bitmap_zalloc(BIT(BLOOM_FILTER_SHIFT), | |
3384 | __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN); | |
3385 | WRITE_ONCE(lruvec->mm_state.filters[gen], filter); | |
3386 | } | |
3387 | ||
bd74fdae YZ |
3388 | /****************************************************************************** |
3389 | * mm_struct list | |
3390 | ******************************************************************************/ | |
3391 | ||
3392 | static struct lru_gen_mm_list *get_mm_list(struct mem_cgroup *memcg) | |
3393 | { | |
3394 | static struct lru_gen_mm_list mm_list = { | |
3395 | .fifo = LIST_HEAD_INIT(mm_list.fifo), | |
3396 | .lock = __SPIN_LOCK_UNLOCKED(mm_list.lock), | |
3397 | }; | |
3398 | ||
3399 | #ifdef CONFIG_MEMCG | |
3400 | if (memcg) | |
3401 | return &memcg->mm_list; | |
3402 | #endif | |
3403 | VM_WARN_ON_ONCE(!mem_cgroup_disabled()); | |
3404 | ||
3405 | return &mm_list; | |
3406 | } | |
3407 | ||
3408 | void lru_gen_add_mm(struct mm_struct *mm) | |
3409 | { | |
3410 | int nid; | |
3411 | struct mem_cgroup *memcg = get_mem_cgroup_from_mm(mm); | |
3412 | struct lru_gen_mm_list *mm_list = get_mm_list(memcg); | |
3413 | ||
3414 | VM_WARN_ON_ONCE(!list_empty(&mm->lru_gen.list)); | |
3415 | #ifdef CONFIG_MEMCG | |
3416 | VM_WARN_ON_ONCE(mm->lru_gen.memcg); | |
3417 | mm->lru_gen.memcg = memcg; | |
3418 | #endif | |
3419 | spin_lock(&mm_list->lock); | |
3420 | ||
3421 | for_each_node_state(nid, N_MEMORY) { | |
3422 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
3423 | ||
bd74fdae YZ |
3424 | /* the first addition since the last iteration */ |
3425 | if (lruvec->mm_state.tail == &mm_list->fifo) | |
3426 | lruvec->mm_state.tail = &mm->lru_gen.list; | |
3427 | } | |
3428 | ||
3429 | list_add_tail(&mm->lru_gen.list, &mm_list->fifo); | |
3430 | ||
3431 | spin_unlock(&mm_list->lock); | |
3432 | } | |
3433 | ||
3434 | void lru_gen_del_mm(struct mm_struct *mm) | |
3435 | { | |
3436 | int nid; | |
3437 | struct lru_gen_mm_list *mm_list; | |
3438 | struct mem_cgroup *memcg = NULL; | |
3439 | ||
3440 | if (list_empty(&mm->lru_gen.list)) | |
3441 | return; | |
3442 | ||
3443 | #ifdef CONFIG_MEMCG | |
3444 | memcg = mm->lru_gen.memcg; | |
3445 | #endif | |
3446 | mm_list = get_mm_list(memcg); | |
3447 | ||
3448 | spin_lock(&mm_list->lock); | |
3449 | ||
3450 | for_each_node(nid) { | |
3451 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
3452 | ||
bd74fdae YZ |
3453 | /* where the last iteration ended (exclusive) */ |
3454 | if (lruvec->mm_state.tail == &mm->lru_gen.list) | |
3455 | lruvec->mm_state.tail = lruvec->mm_state.tail->next; | |
3456 | ||
3457 | /* where the current iteration continues (inclusive) */ | |
3458 | if (lruvec->mm_state.head != &mm->lru_gen.list) | |
3459 | continue; | |
3460 | ||
3461 | lruvec->mm_state.head = lruvec->mm_state.head->next; | |
3462 | /* the deletion ends the current iteration */ | |
3463 | if (lruvec->mm_state.head == &mm_list->fifo) | |
3464 | WRITE_ONCE(lruvec->mm_state.seq, lruvec->mm_state.seq + 1); | |
3465 | } | |
3466 | ||
3467 | list_del_init(&mm->lru_gen.list); | |
3468 | ||
3469 | spin_unlock(&mm_list->lock); | |
3470 | ||
3471 | #ifdef CONFIG_MEMCG | |
3472 | mem_cgroup_put(mm->lru_gen.memcg); | |
3473 | mm->lru_gen.memcg = NULL; | |
3474 | #endif | |
3475 | } | |
3476 | ||
3477 | #ifdef CONFIG_MEMCG | |
3478 | void lru_gen_migrate_mm(struct mm_struct *mm) | |
3479 | { | |
3480 | struct mem_cgroup *memcg; | |
3481 | struct task_struct *task = rcu_dereference_protected(mm->owner, true); | |
3482 | ||
3483 | VM_WARN_ON_ONCE(task->mm != mm); | |
3484 | lockdep_assert_held(&task->alloc_lock); | |
3485 | ||
3486 | /* for mm_update_next_owner() */ | |
3487 | if (mem_cgroup_disabled()) | |
3488 | return; | |
3489 | ||
de08eaa6 YZ |
3490 | /* migration can happen before addition */ |
3491 | if (!mm->lru_gen.memcg) | |
3492 | return; | |
3493 | ||
bd74fdae YZ |
3494 | rcu_read_lock(); |
3495 | memcg = mem_cgroup_from_task(task); | |
3496 | rcu_read_unlock(); | |
3497 | if (memcg == mm->lru_gen.memcg) | |
3498 | return; | |
3499 | ||
bd74fdae YZ |
3500 | VM_WARN_ON_ONCE(list_empty(&mm->lru_gen.list)); |
3501 | ||
3502 | lru_gen_del_mm(mm); | |
3503 | lru_gen_add_mm(mm); | |
3504 | } | |
3505 | #endif | |
3506 | ||
bd74fdae YZ |
3507 | static void reset_mm_stats(struct lruvec *lruvec, struct lru_gen_mm_walk *walk, bool last) |
3508 | { | |
3509 | int i; | |
3510 | int hist; | |
3511 | ||
3512 | lockdep_assert_held(&get_mm_list(lruvec_memcg(lruvec))->lock); | |
3513 | ||
3514 | if (walk) { | |
3515 | hist = lru_hist_from_seq(walk->max_seq); | |
3516 | ||
3517 | for (i = 0; i < NR_MM_STATS; i++) { | |
3518 | WRITE_ONCE(lruvec->mm_state.stats[hist][i], | |
3519 | lruvec->mm_state.stats[hist][i] + walk->mm_stats[i]); | |
3520 | walk->mm_stats[i] = 0; | |
3521 | } | |
3522 | } | |
3523 | ||
3524 | if (NR_HIST_GENS > 1 && last) { | |
3525 | hist = lru_hist_from_seq(lruvec->mm_state.seq + 1); | |
3526 | ||
3527 | for (i = 0; i < NR_MM_STATS; i++) | |
3528 | WRITE_ONCE(lruvec->mm_state.stats[hist][i], 0); | |
3529 | } | |
3530 | } | |
3531 | ||
3532 | static bool should_skip_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk) | |
3533 | { | |
3534 | int type; | |
3535 | unsigned long size = 0; | |
3536 | struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec); | |
3537 | int key = pgdat->node_id % BITS_PER_TYPE(mm->lru_gen.bitmap); | |
3538 | ||
3539 | if (!walk->force_scan && !test_bit(key, &mm->lru_gen.bitmap)) | |
3540 | return true; | |
3541 | ||
3542 | clear_bit(key, &mm->lru_gen.bitmap); | |
3543 | ||
3544 | for (type = !walk->can_swap; type < ANON_AND_FILE; type++) { | |
3545 | size += type ? get_mm_counter(mm, MM_FILEPAGES) : | |
3546 | get_mm_counter(mm, MM_ANONPAGES) + | |
3547 | get_mm_counter(mm, MM_SHMEMPAGES); | |
3548 | } | |
3549 | ||
3550 | if (size < MIN_LRU_BATCH) | |
3551 | return true; | |
3552 | ||
3553 | return !mmget_not_zero(mm); | |
3554 | } | |
3555 | ||
3556 | static bool iterate_mm_list(struct lruvec *lruvec, struct lru_gen_mm_walk *walk, | |
3557 | struct mm_struct **iter) | |
3558 | { | |
3559 | bool first = false; | |
3560 | bool last = true; | |
3561 | struct mm_struct *mm = NULL; | |
3562 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
3563 | struct lru_gen_mm_list *mm_list = get_mm_list(memcg); | |
3564 | struct lru_gen_mm_state *mm_state = &lruvec->mm_state; | |
3565 | ||
3566 | /* | |
3567 | * There are four interesting cases for this page table walker: | |
3568 | * 1. It tries to start a new iteration of mm_list with a stale max_seq; | |
3569 | * there is nothing left to do. | |
3570 | * 2. It's the first of the current generation, and it needs to reset | |
3571 | * the Bloom filter for the next generation. | |
3572 | * 3. It reaches the end of mm_list, and it needs to increment | |
3573 | * mm_state->seq; the iteration is done. | |
3574 | * 4. It's the last of the current generation, and it needs to reset the | |
3575 | * mm stats counters for the next generation. | |
3576 | */ | |
3577 | spin_lock(&mm_list->lock); | |
3578 | ||
3579 | VM_WARN_ON_ONCE(mm_state->seq + 1 < walk->max_seq); | |
3580 | VM_WARN_ON_ONCE(*iter && mm_state->seq > walk->max_seq); | |
3581 | VM_WARN_ON_ONCE(*iter && !mm_state->nr_walkers); | |
3582 | ||
3583 | if (walk->max_seq <= mm_state->seq) { | |
3584 | if (!*iter) | |
3585 | last = false; | |
3586 | goto done; | |
3587 | } | |
3588 | ||
3589 | if (!mm_state->nr_walkers) { | |
3590 | VM_WARN_ON_ONCE(mm_state->head && mm_state->head != &mm_list->fifo); | |
3591 | ||
3592 | mm_state->head = mm_list->fifo.next; | |
3593 | first = true; | |
3594 | } | |
3595 | ||
3596 | while (!mm && mm_state->head != &mm_list->fifo) { | |
3597 | mm = list_entry(mm_state->head, struct mm_struct, lru_gen.list); | |
3598 | ||
3599 | mm_state->head = mm_state->head->next; | |
3600 | ||
3601 | /* force scan for those added after the last iteration */ | |
3602 | if (!mm_state->tail || mm_state->tail == &mm->lru_gen.list) { | |
3603 | mm_state->tail = mm_state->head; | |
3604 | walk->force_scan = true; | |
3605 | } | |
3606 | ||
3607 | if (should_skip_mm(mm, walk)) | |
3608 | mm = NULL; | |
3609 | } | |
3610 | ||
3611 | if (mm_state->head == &mm_list->fifo) | |
3612 | WRITE_ONCE(mm_state->seq, mm_state->seq + 1); | |
3613 | done: | |
3614 | if (*iter && !mm) | |
3615 | mm_state->nr_walkers--; | |
3616 | if (!*iter && mm) | |
3617 | mm_state->nr_walkers++; | |
3618 | ||
3619 | if (mm_state->nr_walkers) | |
3620 | last = false; | |
3621 | ||
3622 | if (*iter || last) | |
3623 | reset_mm_stats(lruvec, walk, last); | |
3624 | ||
3625 | spin_unlock(&mm_list->lock); | |
3626 | ||
3627 | if (mm && first) | |
3628 | reset_bloom_filter(lruvec, walk->max_seq + 1); | |
3629 | ||
3630 | if (*iter) | |
3631 | mmput_async(*iter); | |
3632 | ||
3633 | *iter = mm; | |
3634 | ||
3635 | return last; | |
3636 | } | |
3637 | ||
3638 | static bool iterate_mm_list_nowalk(struct lruvec *lruvec, unsigned long max_seq) | |
3639 | { | |
3640 | bool success = false; | |
3641 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
3642 | struct lru_gen_mm_list *mm_list = get_mm_list(memcg); | |
3643 | struct lru_gen_mm_state *mm_state = &lruvec->mm_state; | |
3644 | ||
3645 | spin_lock(&mm_list->lock); | |
3646 | ||
3647 | VM_WARN_ON_ONCE(mm_state->seq + 1 < max_seq); | |
3648 | ||
3649 | if (max_seq > mm_state->seq && !mm_state->nr_walkers) { | |
3650 | VM_WARN_ON_ONCE(mm_state->head && mm_state->head != &mm_list->fifo); | |
3651 | ||
3652 | WRITE_ONCE(mm_state->seq, mm_state->seq + 1); | |
3653 | reset_mm_stats(lruvec, NULL, true); | |
3654 | success = true; | |
3655 | } | |
3656 | ||
3657 | spin_unlock(&mm_list->lock); | |
3658 | ||
3659 | return success; | |
3660 | } | |
3661 | ||
ac35a490 | 3662 | /****************************************************************************** |
32d32ef1 | 3663 | * PID controller |
ac35a490 YZ |
3664 | ******************************************************************************/ |
3665 | ||
3666 | /* | |
3667 | * A feedback loop based on Proportional-Integral-Derivative (PID) controller. | |
3668 | * | |
3669 | * The P term is refaulted/(evicted+protected) from a tier in the generation | |
3670 | * currently being evicted; the I term is the exponential moving average of the | |
3671 | * P term over the generations previously evicted, using the smoothing factor | |
3672 | * 1/2; the D term isn't supported. | |
3673 | * | |
3674 | * The setpoint (SP) is always the first tier of one type; the process variable | |
3675 | * (PV) is either any tier of the other type or any other tier of the same | |
3676 | * type. | |
3677 | * | |
3678 | * The error is the difference between the SP and the PV; the correction is to | |
3679 | * turn off protection when SP>PV or turn on protection when SP<PV. | |
3680 | * | |
3681 | * For future optimizations: | |
3682 | * 1. The D term may discount the other two terms over time so that long-lived | |
3683 | * generations can resist stale information. | |
3684 | */ | |
3685 | struct ctrl_pos { | |
3686 | unsigned long refaulted; | |
3687 | unsigned long total; | |
3688 | int gain; | |
3689 | }; | |
3690 | ||
3691 | static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain, | |
3692 | struct ctrl_pos *pos) | |
3693 | { | |
391655fe | 3694 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ac35a490 YZ |
3695 | int hist = lru_hist_from_seq(lrugen->min_seq[type]); |
3696 | ||
3697 | pos->refaulted = lrugen->avg_refaulted[type][tier] + | |
3698 | atomic_long_read(&lrugen->refaulted[hist][type][tier]); | |
3699 | pos->total = lrugen->avg_total[type][tier] + | |
3700 | atomic_long_read(&lrugen->evicted[hist][type][tier]); | |
3701 | if (tier) | |
3702 | pos->total += lrugen->protected[hist][type][tier - 1]; | |
3703 | pos->gain = gain; | |
3704 | } | |
3705 | ||
3706 | static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover) | |
3707 | { | |
3708 | int hist, tier; | |
391655fe | 3709 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ac35a490 YZ |
3710 | bool clear = carryover ? NR_HIST_GENS == 1 : NR_HIST_GENS > 1; |
3711 | unsigned long seq = carryover ? lrugen->min_seq[type] : lrugen->max_seq + 1; | |
3712 | ||
3713 | lockdep_assert_held(&lruvec->lru_lock); | |
3714 | ||
3715 | if (!carryover && !clear) | |
3716 | return; | |
3717 | ||
3718 | hist = lru_hist_from_seq(seq); | |
3719 | ||
3720 | for (tier = 0; tier < MAX_NR_TIERS; tier++) { | |
3721 | if (carryover) { | |
3722 | unsigned long sum; | |
3723 | ||
3724 | sum = lrugen->avg_refaulted[type][tier] + | |
3725 | atomic_long_read(&lrugen->refaulted[hist][type][tier]); | |
3726 | WRITE_ONCE(lrugen->avg_refaulted[type][tier], sum / 2); | |
3727 | ||
3728 | sum = lrugen->avg_total[type][tier] + | |
3729 | atomic_long_read(&lrugen->evicted[hist][type][tier]); | |
3730 | if (tier) | |
3731 | sum += lrugen->protected[hist][type][tier - 1]; | |
3732 | WRITE_ONCE(lrugen->avg_total[type][tier], sum / 2); | |
3733 | } | |
3734 | ||
3735 | if (clear) { | |
3736 | atomic_long_set(&lrugen->refaulted[hist][type][tier], 0); | |
3737 | atomic_long_set(&lrugen->evicted[hist][type][tier], 0); | |
3738 | if (tier) | |
3739 | WRITE_ONCE(lrugen->protected[hist][type][tier - 1], 0); | |
3740 | } | |
3741 | } | |
3742 | } | |
3743 | ||
3744 | static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv) | |
3745 | { | |
3746 | /* | |
3747 | * Return true if the PV has a limited number of refaults or a lower | |
3748 | * refaulted/total than the SP. | |
3749 | */ | |
3750 | return pv->refaulted < MIN_LRU_BATCH || | |
3751 | pv->refaulted * (sp->total + MIN_LRU_BATCH) * sp->gain <= | |
3752 | (sp->refaulted + 1) * pv->total * pv->gain; | |
3753 | } | |
3754 | ||
3755 | /****************************************************************************** | |
3756 | * the aging | |
3757 | ******************************************************************************/ | |
3758 | ||
018ee47f YZ |
3759 | /* promote pages accessed through page tables */ |
3760 | static int folio_update_gen(struct folio *folio, int gen) | |
3761 | { | |
3762 | unsigned long new_flags, old_flags = READ_ONCE(folio->flags); | |
3763 | ||
3764 | VM_WARN_ON_ONCE(gen >= MAX_NR_GENS); | |
3765 | VM_WARN_ON_ONCE(!rcu_read_lock_held()); | |
3766 | ||
3767 | do { | |
3768 | /* lru_gen_del_folio() has isolated this page? */ | |
3769 | if (!(old_flags & LRU_GEN_MASK)) { | |
49fd9b6d | 3770 | /* for shrink_folio_list() */ |
018ee47f YZ |
3771 | new_flags = old_flags | BIT(PG_referenced); |
3772 | continue; | |
3773 | } | |
3774 | ||
3775 | new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS); | |
3776 | new_flags |= (gen + 1UL) << LRU_GEN_PGOFF; | |
3777 | } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags)); | |
3778 | ||
3779 | return ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1; | |
3780 | } | |
3781 | ||
ac35a490 YZ |
3782 | /* protect pages accessed multiple times through file descriptors */ |
3783 | static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio, bool reclaiming) | |
3784 | { | |
3785 | int type = folio_is_file_lru(folio); | |
391655fe | 3786 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ac35a490 YZ |
3787 | int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]); |
3788 | unsigned long new_flags, old_flags = READ_ONCE(folio->flags); | |
3789 | ||
3790 | VM_WARN_ON_ONCE_FOLIO(!(old_flags & LRU_GEN_MASK), folio); | |
3791 | ||
3792 | do { | |
018ee47f YZ |
3793 | new_gen = ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1; |
3794 | /* folio_update_gen() has promoted this page? */ | |
3795 | if (new_gen >= 0 && new_gen != old_gen) | |
3796 | return new_gen; | |
3797 | ||
ac35a490 YZ |
3798 | new_gen = (old_gen + 1) % MAX_NR_GENS; |
3799 | ||
3800 | new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS); | |
3801 | new_flags |= (new_gen + 1UL) << LRU_GEN_PGOFF; | |
3802 | /* for folio_end_writeback() */ | |
3803 | if (reclaiming) | |
3804 | new_flags |= BIT(PG_reclaim); | |
3805 | } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags)); | |
3806 | ||
3807 | lru_gen_update_size(lruvec, folio, old_gen, new_gen); | |
3808 | ||
3809 | return new_gen; | |
3810 | } | |
3811 | ||
bd74fdae YZ |
3812 | static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, |
3813 | int old_gen, int new_gen) | |
3814 | { | |
3815 | int type = folio_is_file_lru(folio); | |
3816 | int zone = folio_zonenum(folio); | |
3817 | int delta = folio_nr_pages(folio); | |
3818 | ||
3819 | VM_WARN_ON_ONCE(old_gen >= MAX_NR_GENS); | |
3820 | VM_WARN_ON_ONCE(new_gen >= MAX_NR_GENS); | |
3821 | ||
3822 | walk->batched++; | |
3823 | ||
3824 | walk->nr_pages[old_gen][type][zone] -= delta; | |
3825 | walk->nr_pages[new_gen][type][zone] += delta; | |
3826 | } | |
3827 | ||
3828 | static void reset_batch_size(struct lruvec *lruvec, struct lru_gen_mm_walk *walk) | |
3829 | { | |
3830 | int gen, type, zone; | |
391655fe | 3831 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
bd74fdae YZ |
3832 | |
3833 | walk->batched = 0; | |
3834 | ||
3835 | for_each_gen_type_zone(gen, type, zone) { | |
3836 | enum lru_list lru = type * LRU_INACTIVE_FILE; | |
3837 | int delta = walk->nr_pages[gen][type][zone]; | |
3838 | ||
3839 | if (!delta) | |
3840 | continue; | |
3841 | ||
3842 | walk->nr_pages[gen][type][zone] = 0; | |
3843 | WRITE_ONCE(lrugen->nr_pages[gen][type][zone], | |
3844 | lrugen->nr_pages[gen][type][zone] + delta); | |
3845 | ||
3846 | if (lru_gen_is_active(lruvec, gen)) | |
3847 | lru += LRU_ACTIVE; | |
3848 | __update_lru_size(lruvec, lru, zone, delta); | |
3849 | } | |
3850 | } | |
3851 | ||
3852 | static int should_skip_vma(unsigned long start, unsigned long end, struct mm_walk *args) | |
3853 | { | |
3854 | struct address_space *mapping; | |
3855 | struct vm_area_struct *vma = args->vma; | |
3856 | struct lru_gen_mm_walk *walk = args->private; | |
3857 | ||
3858 | if (!vma_is_accessible(vma)) | |
3859 | return true; | |
3860 | ||
3861 | if (is_vm_hugetlb_page(vma)) | |
3862 | return true; | |
3863 | ||
8788f678 YZ |
3864 | if (!vma_has_recency(vma)) |
3865 | return true; | |
3866 | ||
3867 | if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) | |
bd74fdae YZ |
3868 | return true; |
3869 | ||
3870 | if (vma == get_gate_vma(vma->vm_mm)) | |
3871 | return true; | |
3872 | ||
3873 | if (vma_is_anonymous(vma)) | |
3874 | return !walk->can_swap; | |
3875 | ||
3876 | if (WARN_ON_ONCE(!vma->vm_file || !vma->vm_file->f_mapping)) | |
3877 | return true; | |
3878 | ||
3879 | mapping = vma->vm_file->f_mapping; | |
3880 | if (mapping_unevictable(mapping)) | |
3881 | return true; | |
3882 | ||
3883 | if (shmem_mapping(mapping)) | |
3884 | return !walk->can_swap; | |
3885 | ||
3886 | /* to exclude special mappings like dax, etc. */ | |
3887 | return !mapping->a_ops->read_folio; | |
3888 | } | |
3889 | ||
3890 | /* | |
3891 | * Some userspace memory allocators map many single-page VMAs. Instead of | |
3892 | * returning back to the PGD table for each of such VMAs, finish an entire PMD | |
3893 | * table to reduce zigzags and improve cache performance. | |
3894 | */ | |
3895 | static bool get_next_vma(unsigned long mask, unsigned long size, struct mm_walk *args, | |
3896 | unsigned long *vm_start, unsigned long *vm_end) | |
3897 | { | |
3898 | unsigned long start = round_up(*vm_end, size); | |
3899 | unsigned long end = (start | ~mask) + 1; | |
78ba531f | 3900 | VMA_ITERATOR(vmi, args->mm, start); |
bd74fdae YZ |
3901 | |
3902 | VM_WARN_ON_ONCE(mask & size); | |
3903 | VM_WARN_ON_ONCE((start & mask) != (*vm_start & mask)); | |
3904 | ||
78ba531f | 3905 | for_each_vma(vmi, args->vma) { |
bd74fdae YZ |
3906 | if (end && end <= args->vma->vm_start) |
3907 | return false; | |
3908 | ||
78ba531f | 3909 | if (should_skip_vma(args->vma->vm_start, args->vma->vm_end, args)) |
bd74fdae | 3910 | continue; |
bd74fdae YZ |
3911 | |
3912 | *vm_start = max(start, args->vma->vm_start); | |
3913 | *vm_end = min(end - 1, args->vma->vm_end - 1) + 1; | |
3914 | ||
3915 | return true; | |
3916 | } | |
3917 | ||
3918 | return false; | |
3919 | } | |
3920 | ||
018ee47f YZ |
3921 | static unsigned long get_pte_pfn(pte_t pte, struct vm_area_struct *vma, unsigned long addr) |
3922 | { | |
3923 | unsigned long pfn = pte_pfn(pte); | |
3924 | ||
3925 | VM_WARN_ON_ONCE(addr < vma->vm_start || addr >= vma->vm_end); | |
3926 | ||
3927 | if (!pte_present(pte) || is_zero_pfn(pfn)) | |
3928 | return -1; | |
3929 | ||
3930 | if (WARN_ON_ONCE(pte_devmap(pte) || pte_special(pte))) | |
3931 | return -1; | |
3932 | ||
3933 | if (WARN_ON_ONCE(!pfn_valid(pfn))) | |
3934 | return -1; | |
3935 | ||
3936 | return pfn; | |
3937 | } | |
3938 | ||
bd74fdae YZ |
3939 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG) |
3940 | static unsigned long get_pmd_pfn(pmd_t pmd, struct vm_area_struct *vma, unsigned long addr) | |
3941 | { | |
3942 | unsigned long pfn = pmd_pfn(pmd); | |
3943 | ||
3944 | VM_WARN_ON_ONCE(addr < vma->vm_start || addr >= vma->vm_end); | |
3945 | ||
3946 | if (!pmd_present(pmd) || is_huge_zero_pmd(pmd)) | |
3947 | return -1; | |
3948 | ||
3949 | if (WARN_ON_ONCE(pmd_devmap(pmd))) | |
3950 | return -1; | |
3951 | ||
3952 | if (WARN_ON_ONCE(!pfn_valid(pfn))) | |
3953 | return -1; | |
3954 | ||
3955 | return pfn; | |
3956 | } | |
3957 | #endif | |
3958 | ||
018ee47f | 3959 | static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *memcg, |
bd74fdae | 3960 | struct pglist_data *pgdat, bool can_swap) |
018ee47f YZ |
3961 | { |
3962 | struct folio *folio; | |
3963 | ||
3964 | /* try to avoid unnecessary memory loads */ | |
3965 | if (pfn < pgdat->node_start_pfn || pfn >= pgdat_end_pfn(pgdat)) | |
3966 | return NULL; | |
3967 | ||
3968 | folio = pfn_folio(pfn); | |
3969 | if (folio_nid(folio) != pgdat->node_id) | |
3970 | return NULL; | |
3971 | ||
3972 | if (folio_memcg_rcu(folio) != memcg) | |
3973 | return NULL; | |
3974 | ||
bd74fdae YZ |
3975 | /* file VMAs can contain anon pages from COW */ |
3976 | if (!folio_is_file_lru(folio) && !can_swap) | |
3977 | return NULL; | |
3978 | ||
018ee47f YZ |
3979 | return folio; |
3980 | } | |
3981 | ||
bd74fdae YZ |
3982 | static bool suitable_to_scan(int total, int young) |
3983 | { | |
3984 | int n = clamp_t(int, cache_line_size() / sizeof(pte_t), 2, 8); | |
3985 | ||
3986 | /* suitable if the average number of young PTEs per cacheline is >=1 */ | |
3987 | return young * n >= total; | |
3988 | } | |
3989 | ||
3990 | static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end, | |
3991 | struct mm_walk *args) | |
3992 | { | |
3993 | int i; | |
3994 | pte_t *pte; | |
3995 | spinlock_t *ptl; | |
3996 | unsigned long addr; | |
3997 | int total = 0; | |
3998 | int young = 0; | |
3999 | struct lru_gen_mm_walk *walk = args->private; | |
4000 | struct mem_cgroup *memcg = lruvec_memcg(walk->lruvec); | |
4001 | struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec); | |
4002 | int old_gen, new_gen = lru_gen_from_seq(walk->max_seq); | |
4003 | ||
4004 | VM_WARN_ON_ONCE(pmd_leaf(*pmd)); | |
4005 | ||
4006 | ptl = pte_lockptr(args->mm, pmd); | |
4007 | if (!spin_trylock(ptl)) | |
4008 | return false; | |
4009 | ||
4010 | arch_enter_lazy_mmu_mode(); | |
4011 | ||
4012 | pte = pte_offset_map(pmd, start & PMD_MASK); | |
4013 | restart: | |
4014 | for (i = pte_index(start), addr = start; addr != end; i++, addr += PAGE_SIZE) { | |
4015 | unsigned long pfn; | |
4016 | struct folio *folio; | |
4017 | ||
4018 | total++; | |
4019 | walk->mm_stats[MM_LEAF_TOTAL]++; | |
4020 | ||
4021 | pfn = get_pte_pfn(pte[i], args->vma, addr); | |
4022 | if (pfn == -1) | |
4023 | continue; | |
4024 | ||
4025 | if (!pte_young(pte[i])) { | |
4026 | walk->mm_stats[MM_LEAF_OLD]++; | |
4027 | continue; | |
4028 | } | |
4029 | ||
4030 | folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap); | |
4031 | if (!folio) | |
4032 | continue; | |
4033 | ||
4034 | if (!ptep_test_and_clear_young(args->vma, addr, pte + i)) | |
4035 | VM_WARN_ON_ONCE(true); | |
4036 | ||
4037 | young++; | |
4038 | walk->mm_stats[MM_LEAF_YOUNG]++; | |
4039 | ||
4040 | if (pte_dirty(pte[i]) && !folio_test_dirty(folio) && | |
4041 | !(folio_test_anon(folio) && folio_test_swapbacked(folio) && | |
4042 | !folio_test_swapcache(folio))) | |
4043 | folio_mark_dirty(folio); | |
4044 | ||
4045 | old_gen = folio_update_gen(folio, new_gen); | |
4046 | if (old_gen >= 0 && old_gen != new_gen) | |
4047 | update_batch_size(walk, folio, old_gen, new_gen); | |
4048 | } | |
4049 | ||
4050 | if (i < PTRS_PER_PTE && get_next_vma(PMD_MASK, PAGE_SIZE, args, &start, &end)) | |
4051 | goto restart; | |
4052 | ||
4053 | pte_unmap(pte); | |
4054 | ||
4055 | arch_leave_lazy_mmu_mode(); | |
4056 | spin_unlock(ptl); | |
4057 | ||
4058 | return suitable_to_scan(total, young); | |
4059 | } | |
4060 | ||
4061 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG) | |
b5ff4133 A |
4062 | static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area_struct *vma, |
4063 | struct mm_walk *args, unsigned long *bitmap, unsigned long *first) | |
bd74fdae YZ |
4064 | { |
4065 | int i; | |
4066 | pmd_t *pmd; | |
4067 | spinlock_t *ptl; | |
4068 | struct lru_gen_mm_walk *walk = args->private; | |
4069 | struct mem_cgroup *memcg = lruvec_memcg(walk->lruvec); | |
4070 | struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec); | |
4071 | int old_gen, new_gen = lru_gen_from_seq(walk->max_seq); | |
4072 | ||
4073 | VM_WARN_ON_ONCE(pud_leaf(*pud)); | |
4074 | ||
4075 | /* try to batch at most 1+MIN_LRU_BATCH+1 entries */ | |
b5ff4133 A |
4076 | if (*first == -1) { |
4077 | *first = addr; | |
4078 | bitmap_zero(bitmap, MIN_LRU_BATCH); | |
bd74fdae YZ |
4079 | return; |
4080 | } | |
4081 | ||
b5ff4133 | 4082 | i = addr == -1 ? 0 : pmd_index(addr) - pmd_index(*first); |
bd74fdae YZ |
4083 | if (i && i <= MIN_LRU_BATCH) { |
4084 | __set_bit(i - 1, bitmap); | |
4085 | return; | |
4086 | } | |
4087 | ||
b5ff4133 | 4088 | pmd = pmd_offset(pud, *first); |
bd74fdae YZ |
4089 | |
4090 | ptl = pmd_lockptr(args->mm, pmd); | |
4091 | if (!spin_trylock(ptl)) | |
4092 | goto done; | |
4093 | ||
4094 | arch_enter_lazy_mmu_mode(); | |
4095 | ||
4096 | do { | |
4097 | unsigned long pfn; | |
4098 | struct folio *folio; | |
b5ff4133 A |
4099 | |
4100 | /* don't round down the first address */ | |
4101 | addr = i ? (*first & PMD_MASK) + i * PMD_SIZE : *first; | |
bd74fdae YZ |
4102 | |
4103 | pfn = get_pmd_pfn(pmd[i], vma, addr); | |
4104 | if (pfn == -1) | |
4105 | goto next; | |
4106 | ||
4107 | if (!pmd_trans_huge(pmd[i])) { | |
b5ff4133 | 4108 | if (arch_has_hw_nonleaf_pmd_young() && get_cap(LRU_GEN_NONLEAF_YOUNG)) |
bd74fdae YZ |
4109 | pmdp_test_and_clear_young(vma, addr, pmd + i); |
4110 | goto next; | |
4111 | } | |
4112 | ||
4113 | folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap); | |
4114 | if (!folio) | |
4115 | goto next; | |
4116 | ||
4117 | if (!pmdp_test_and_clear_young(vma, addr, pmd + i)) | |
4118 | goto next; | |
4119 | ||
4120 | walk->mm_stats[MM_LEAF_YOUNG]++; | |
4121 | ||
4122 | if (pmd_dirty(pmd[i]) && !folio_test_dirty(folio) && | |
4123 | !(folio_test_anon(folio) && folio_test_swapbacked(folio) && | |
4124 | !folio_test_swapcache(folio))) | |
4125 | folio_mark_dirty(folio); | |
4126 | ||
4127 | old_gen = folio_update_gen(folio, new_gen); | |
4128 | if (old_gen >= 0 && old_gen != new_gen) | |
4129 | update_batch_size(walk, folio, old_gen, new_gen); | |
4130 | next: | |
4131 | i = i > MIN_LRU_BATCH ? 0 : find_next_bit(bitmap, MIN_LRU_BATCH, i) + 1; | |
4132 | } while (i <= MIN_LRU_BATCH); | |
4133 | ||
4134 | arch_leave_lazy_mmu_mode(); | |
4135 | spin_unlock(ptl); | |
4136 | done: | |
b5ff4133 | 4137 | *first = -1; |
bd74fdae YZ |
4138 | } |
4139 | #else | |
b5ff4133 A |
4140 | static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area_struct *vma, |
4141 | struct mm_walk *args, unsigned long *bitmap, unsigned long *first) | |
bd74fdae YZ |
4142 | { |
4143 | } | |
4144 | #endif | |
4145 | ||
4146 | static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end, | |
4147 | struct mm_walk *args) | |
4148 | { | |
4149 | int i; | |
4150 | pmd_t *pmd; | |
4151 | unsigned long next; | |
4152 | unsigned long addr; | |
4153 | struct vm_area_struct *vma; | |
b5ff4133 A |
4154 | unsigned long bitmap[BITS_TO_LONGS(MIN_LRU_BATCH)]; |
4155 | unsigned long first = -1; | |
bd74fdae | 4156 | struct lru_gen_mm_walk *walk = args->private; |
bd74fdae YZ |
4157 | |
4158 | VM_WARN_ON_ONCE(pud_leaf(*pud)); | |
4159 | ||
4160 | /* | |
4161 | * Finish an entire PMD in two passes: the first only reaches to PTE | |
4162 | * tables to avoid taking the PMD lock; the second, if necessary, takes | |
4163 | * the PMD lock to clear the accessed bit in PMD entries. | |
4164 | */ | |
4165 | pmd = pmd_offset(pud, start & PUD_MASK); | |
4166 | restart: | |
4167 | /* walk_pte_range() may call get_next_vma() */ | |
4168 | vma = args->vma; | |
4169 | for (i = pmd_index(start), addr = start; addr != end; i++, addr = next) { | |
dab6e717 | 4170 | pmd_t val = pmdp_get_lockless(pmd + i); |
bd74fdae YZ |
4171 | |
4172 | next = pmd_addr_end(addr, end); | |
4173 | ||
4174 | if (!pmd_present(val) || is_huge_zero_pmd(val)) { | |
4175 | walk->mm_stats[MM_LEAF_TOTAL]++; | |
4176 | continue; | |
4177 | } | |
4178 | ||
4179 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
4180 | if (pmd_trans_huge(val)) { | |
4181 | unsigned long pfn = pmd_pfn(val); | |
4182 | struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec); | |
4183 | ||
4184 | walk->mm_stats[MM_LEAF_TOTAL]++; | |
4185 | ||
4186 | if (!pmd_young(val)) { | |
4187 | walk->mm_stats[MM_LEAF_OLD]++; | |
4188 | continue; | |
4189 | } | |
4190 | ||
4191 | /* try to avoid unnecessary memory loads */ | |
4192 | if (pfn < pgdat->node_start_pfn || pfn >= pgdat_end_pfn(pgdat)) | |
4193 | continue; | |
4194 | ||
b5ff4133 | 4195 | walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first); |
bd74fdae YZ |
4196 | continue; |
4197 | } | |
4198 | #endif | |
4199 | walk->mm_stats[MM_NONLEAF_TOTAL]++; | |
4200 | ||
b5ff4133 | 4201 | if (arch_has_hw_nonleaf_pmd_young() && get_cap(LRU_GEN_NONLEAF_YOUNG)) { |
354ed597 YZ |
4202 | if (!pmd_young(val)) |
4203 | continue; | |
bd74fdae | 4204 | |
b5ff4133 | 4205 | walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first); |
354ed597 | 4206 | } |
4aaf269c | 4207 | |
bd74fdae YZ |
4208 | if (!walk->force_scan && !test_bloom_filter(walk->lruvec, walk->max_seq, pmd + i)) |
4209 | continue; | |
4210 | ||
4211 | walk->mm_stats[MM_NONLEAF_FOUND]++; | |
4212 | ||
4213 | if (!walk_pte_range(&val, addr, next, args)) | |
4214 | continue; | |
4215 | ||
4216 | walk->mm_stats[MM_NONLEAF_ADDED]++; | |
4217 | ||
4218 | /* carry over to the next generation */ | |
4219 | update_bloom_filter(walk->lruvec, walk->max_seq + 1, pmd + i); | |
4220 | } | |
4221 | ||
b5ff4133 | 4222 | walk_pmd_range_locked(pud, -1, vma, args, bitmap, &first); |
bd74fdae YZ |
4223 | |
4224 | if (i < PTRS_PER_PMD && get_next_vma(PUD_MASK, PMD_SIZE, args, &start, &end)) | |
4225 | goto restart; | |
4226 | } | |
4227 | ||
4228 | static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end, | |
4229 | struct mm_walk *args) | |
4230 | { | |
4231 | int i; | |
4232 | pud_t *pud; | |
4233 | unsigned long addr; | |
4234 | unsigned long next; | |
4235 | struct lru_gen_mm_walk *walk = args->private; | |
4236 | ||
4237 | VM_WARN_ON_ONCE(p4d_leaf(*p4d)); | |
4238 | ||
4239 | pud = pud_offset(p4d, start & P4D_MASK); | |
4240 | restart: | |
4241 | for (i = pud_index(start), addr = start; addr != end; i++, addr = next) { | |
4242 | pud_t val = READ_ONCE(pud[i]); | |
4243 | ||
4244 | next = pud_addr_end(addr, end); | |
4245 | ||
4246 | if (!pud_present(val) || WARN_ON_ONCE(pud_leaf(val))) | |
4247 | continue; | |
4248 | ||
4249 | walk_pmd_range(&val, addr, next, args); | |
4250 | ||
4251 | /* a racy check to curtail the waiting time */ | |
4252 | if (wq_has_sleeper(&walk->lruvec->mm_state.wait)) | |
4253 | return 1; | |
4254 | ||
4255 | if (need_resched() || walk->batched >= MAX_LRU_BATCH) { | |
4256 | end = (addr | ~PUD_MASK) + 1; | |
4257 | goto done; | |
4258 | } | |
4259 | } | |
4260 | ||
4261 | if (i < PTRS_PER_PUD && get_next_vma(P4D_MASK, PUD_SIZE, args, &start, &end)) | |
4262 | goto restart; | |
4263 | ||
4264 | end = round_up(end, P4D_SIZE); | |
4265 | done: | |
4266 | if (!end || !args->vma) | |
4267 | return 1; | |
4268 | ||
4269 | walk->next_addr = max(end, args->vma->vm_start); | |
4270 | ||
4271 | return -EAGAIN; | |
4272 | } | |
4273 | ||
4274 | static void walk_mm(struct lruvec *lruvec, struct mm_struct *mm, struct lru_gen_mm_walk *walk) | |
4275 | { | |
4276 | static const struct mm_walk_ops mm_walk_ops = { | |
4277 | .test_walk = should_skip_vma, | |
4278 | .p4d_entry = walk_pud_range, | |
4279 | }; | |
4280 | ||
4281 | int err; | |
4282 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
4283 | ||
4284 | walk->next_addr = FIRST_USER_ADDRESS; | |
4285 | ||
4286 | do { | |
4287 | err = -EBUSY; | |
4288 | ||
4289 | /* folio_update_gen() requires stable folio_memcg() */ | |
4290 | if (!mem_cgroup_trylock_pages(memcg)) | |
4291 | break; | |
4292 | ||
4293 | /* the caller might be holding the lock for write */ | |
4294 | if (mmap_read_trylock(mm)) { | |
4295 | err = walk_page_range(mm, walk->next_addr, ULONG_MAX, &mm_walk_ops, walk); | |
4296 | ||
4297 | mmap_read_unlock(mm); | |
4298 | } | |
4299 | ||
4300 | mem_cgroup_unlock_pages(); | |
4301 | ||
4302 | if (walk->batched) { | |
4303 | spin_lock_irq(&lruvec->lru_lock); | |
4304 | reset_batch_size(lruvec, walk); | |
4305 | spin_unlock_irq(&lruvec->lru_lock); | |
4306 | } | |
4307 | ||
4308 | cond_resched(); | |
4309 | } while (err == -EAGAIN); | |
4310 | } | |
4311 | ||
e9d4e1ee | 4312 | static struct lru_gen_mm_walk *set_mm_walk(struct pglist_data *pgdat, bool force_alloc) |
bd74fdae YZ |
4313 | { |
4314 | struct lru_gen_mm_walk *walk = current->reclaim_state->mm_walk; | |
4315 | ||
4316 | if (pgdat && current_is_kswapd()) { | |
4317 | VM_WARN_ON_ONCE(walk); | |
4318 | ||
4319 | walk = &pgdat->mm_walk; | |
e9d4e1ee | 4320 | } else if (!walk && force_alloc) { |
bd74fdae YZ |
4321 | VM_WARN_ON_ONCE(current_is_kswapd()); |
4322 | ||
4323 | walk = kzalloc(sizeof(*walk), __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN); | |
4324 | } | |
4325 | ||
4326 | current->reclaim_state->mm_walk = walk; | |
4327 | ||
4328 | return walk; | |
4329 | } | |
4330 | ||
4331 | static void clear_mm_walk(void) | |
4332 | { | |
4333 | struct lru_gen_mm_walk *walk = current->reclaim_state->mm_walk; | |
4334 | ||
4335 | VM_WARN_ON_ONCE(walk && memchr_inv(walk->nr_pages, 0, sizeof(walk->nr_pages))); | |
4336 | VM_WARN_ON_ONCE(walk && memchr_inv(walk->mm_stats, 0, sizeof(walk->mm_stats))); | |
4337 | ||
4338 | current->reclaim_state->mm_walk = NULL; | |
4339 | ||
4340 | if (!current_is_kswapd()) | |
4341 | kfree(walk); | |
4342 | } | |
4343 | ||
d6c3af7d | 4344 | static bool inc_min_seq(struct lruvec *lruvec, int type, bool can_swap) |
ac35a490 | 4345 | { |
d6c3af7d YZ |
4346 | int zone; |
4347 | int remaining = MAX_LRU_BATCH; | |
391655fe | 4348 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
d6c3af7d YZ |
4349 | int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]); |
4350 | ||
4351 | if (type == LRU_GEN_ANON && !can_swap) | |
4352 | goto done; | |
4353 | ||
4354 | /* prevent cold/hot inversion if force_scan is true */ | |
4355 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
6df1b221 | 4356 | struct list_head *head = &lrugen->folios[old_gen][type][zone]; |
d6c3af7d YZ |
4357 | |
4358 | while (!list_empty(head)) { | |
4359 | struct folio *folio = lru_to_folio(head); | |
4360 | ||
4361 | VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); | |
4362 | VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio); | |
4363 | VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio); | |
4364 | VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio); | |
ac35a490 | 4365 | |
d6c3af7d | 4366 | new_gen = folio_inc_gen(lruvec, folio, false); |
6df1b221 | 4367 | list_move_tail(&folio->lru, &lrugen->folios[new_gen][type][zone]); |
d6c3af7d YZ |
4368 | |
4369 | if (!--remaining) | |
4370 | return false; | |
4371 | } | |
4372 | } | |
4373 | done: | |
ac35a490 YZ |
4374 | reset_ctrl_pos(lruvec, type, true); |
4375 | WRITE_ONCE(lrugen->min_seq[type], lrugen->min_seq[type] + 1); | |
d6c3af7d YZ |
4376 | |
4377 | return true; | |
ac35a490 YZ |
4378 | } |
4379 | ||
4380 | static bool try_to_inc_min_seq(struct lruvec *lruvec, bool can_swap) | |
4381 | { | |
4382 | int gen, type, zone; | |
4383 | bool success = false; | |
391655fe | 4384 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ac35a490 YZ |
4385 | DEFINE_MIN_SEQ(lruvec); |
4386 | ||
4387 | VM_WARN_ON_ONCE(!seq_is_valid(lruvec)); | |
4388 | ||
4389 | /* find the oldest populated generation */ | |
4390 | for (type = !can_swap; type < ANON_AND_FILE; type++) { | |
4391 | while (min_seq[type] + MIN_NR_GENS <= lrugen->max_seq) { | |
4392 | gen = lru_gen_from_seq(min_seq[type]); | |
4393 | ||
4394 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
6df1b221 | 4395 | if (!list_empty(&lrugen->folios[gen][type][zone])) |
ac35a490 YZ |
4396 | goto next; |
4397 | } | |
4398 | ||
4399 | min_seq[type]++; | |
4400 | } | |
4401 | next: | |
4402 | ; | |
4403 | } | |
4404 | ||
391655fe | 4405 | /* see the comment on lru_gen_folio */ |
ac35a490 YZ |
4406 | if (can_swap) { |
4407 | min_seq[LRU_GEN_ANON] = min(min_seq[LRU_GEN_ANON], min_seq[LRU_GEN_FILE]); | |
4408 | min_seq[LRU_GEN_FILE] = max(min_seq[LRU_GEN_ANON], lrugen->min_seq[LRU_GEN_FILE]); | |
4409 | } | |
4410 | ||
4411 | for (type = !can_swap; type < ANON_AND_FILE; type++) { | |
4412 | if (min_seq[type] == lrugen->min_seq[type]) | |
4413 | continue; | |
4414 | ||
4415 | reset_ctrl_pos(lruvec, type, true); | |
4416 | WRITE_ONCE(lrugen->min_seq[type], min_seq[type]); | |
4417 | success = true; | |
4418 | } | |
4419 | ||
4420 | return success; | |
4421 | } | |
4422 | ||
d6c3af7d | 4423 | static void inc_max_seq(struct lruvec *lruvec, bool can_swap, bool force_scan) |
ac35a490 YZ |
4424 | { |
4425 | int prev, next; | |
4426 | int type, zone; | |
391655fe | 4427 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ac35a490 YZ |
4428 | |
4429 | spin_lock_irq(&lruvec->lru_lock); | |
4430 | ||
4431 | VM_WARN_ON_ONCE(!seq_is_valid(lruvec)); | |
4432 | ||
ac35a490 YZ |
4433 | for (type = ANON_AND_FILE - 1; type >= 0; type--) { |
4434 | if (get_nr_gens(lruvec, type) != MAX_NR_GENS) | |
4435 | continue; | |
4436 | ||
d6c3af7d | 4437 | VM_WARN_ON_ONCE(!force_scan && (type == LRU_GEN_FILE || can_swap)); |
ac35a490 | 4438 | |
d6c3af7d YZ |
4439 | while (!inc_min_seq(lruvec, type, can_swap)) { |
4440 | spin_unlock_irq(&lruvec->lru_lock); | |
4441 | cond_resched(); | |
4442 | spin_lock_irq(&lruvec->lru_lock); | |
4443 | } | |
ac35a490 YZ |
4444 | } |
4445 | ||
4446 | /* | |
4447 | * Update the active/inactive LRU sizes for compatibility. Both sides of | |
4448 | * the current max_seq need to be covered, since max_seq+1 can overlap | |
4449 | * with min_seq[LRU_GEN_ANON] if swapping is constrained. And if they do | |
4450 | * overlap, cold/hot inversion happens. | |
4451 | */ | |
4452 | prev = lru_gen_from_seq(lrugen->max_seq - 1); | |
4453 | next = lru_gen_from_seq(lrugen->max_seq + 1); | |
4454 | ||
4455 | for (type = 0; type < ANON_AND_FILE; type++) { | |
4456 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
4457 | enum lru_list lru = type * LRU_INACTIVE_FILE; | |
4458 | long delta = lrugen->nr_pages[prev][type][zone] - | |
4459 | lrugen->nr_pages[next][type][zone]; | |
4460 | ||
4461 | if (!delta) | |
4462 | continue; | |
4463 | ||
4464 | __update_lru_size(lruvec, lru, zone, delta); | |
4465 | __update_lru_size(lruvec, lru + LRU_ACTIVE, zone, -delta); | |
4466 | } | |
4467 | } | |
4468 | ||
4469 | for (type = 0; type < ANON_AND_FILE; type++) | |
4470 | reset_ctrl_pos(lruvec, type, false); | |
4471 | ||
1332a809 | 4472 | WRITE_ONCE(lrugen->timestamps[next], jiffies); |
ac35a490 YZ |
4473 | /* make sure preceding modifications appear */ |
4474 | smp_store_release(&lrugen->max_seq, lrugen->max_seq + 1); | |
bd74fdae | 4475 | |
ac35a490 YZ |
4476 | spin_unlock_irq(&lruvec->lru_lock); |
4477 | } | |
4478 | ||
bd74fdae | 4479 | static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long max_seq, |
d6c3af7d | 4480 | struct scan_control *sc, bool can_swap, bool force_scan) |
bd74fdae YZ |
4481 | { |
4482 | bool success; | |
4483 | struct lru_gen_mm_walk *walk; | |
4484 | struct mm_struct *mm = NULL; | |
391655fe | 4485 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
bd74fdae YZ |
4486 | |
4487 | VM_WARN_ON_ONCE(max_seq > READ_ONCE(lrugen->max_seq)); | |
4488 | ||
4489 | /* see the comment in iterate_mm_list() */ | |
4490 | if (max_seq <= READ_ONCE(lruvec->mm_state.seq)) { | |
4491 | success = false; | |
4492 | goto done; | |
4493 | } | |
4494 | ||
4495 | /* | |
4496 | * If the hardware doesn't automatically set the accessed bit, fallback | |
4497 | * to lru_gen_look_around(), which only clears the accessed bit in a | |
4498 | * handful of PTEs. Spreading the work out over a period of time usually | |
4499 | * is less efficient, but it avoids bursty page faults. | |
4500 | */ | |
f386e931 | 4501 | if (!arch_has_hw_pte_young() || !get_cap(LRU_GEN_MM_WALK)) { |
bd74fdae YZ |
4502 | success = iterate_mm_list_nowalk(lruvec, max_seq); |
4503 | goto done; | |
4504 | } | |
4505 | ||
e9d4e1ee | 4506 | walk = set_mm_walk(NULL, true); |
bd74fdae YZ |
4507 | if (!walk) { |
4508 | success = iterate_mm_list_nowalk(lruvec, max_seq); | |
4509 | goto done; | |
4510 | } | |
4511 | ||
4512 | walk->lruvec = lruvec; | |
4513 | walk->max_seq = max_seq; | |
4514 | walk->can_swap = can_swap; | |
d6c3af7d | 4515 | walk->force_scan = force_scan; |
bd74fdae YZ |
4516 | |
4517 | do { | |
4518 | success = iterate_mm_list(lruvec, walk, &mm); | |
4519 | if (mm) | |
4520 | walk_mm(lruvec, mm, walk); | |
4521 | ||
4522 | cond_resched(); | |
4523 | } while (mm); | |
4524 | done: | |
4525 | if (!success) { | |
4526 | if (sc->priority <= DEF_PRIORITY - 2) | |
4527 | wait_event_killable(lruvec->mm_state.wait, | |
4528 | max_seq < READ_ONCE(lrugen->max_seq)); | |
e4dde56c | 4529 | return false; |
bd74fdae YZ |
4530 | } |
4531 | ||
4532 | VM_WARN_ON_ONCE(max_seq != READ_ONCE(lrugen->max_seq)); | |
4533 | ||
d6c3af7d | 4534 | inc_max_seq(lruvec, can_swap, force_scan); |
bd74fdae YZ |
4535 | /* either this sees any waiters or they will see updated max_seq */ |
4536 | if (wq_has_sleeper(&lruvec->mm_state.wait)) | |
4537 | wake_up_all(&lruvec->mm_state.wait); | |
4538 | ||
bd74fdae YZ |
4539 | return true; |
4540 | } | |
4541 | ||
7b8144e6 A |
4542 | /****************************************************************************** |
4543 | * working set protection | |
4544 | ******************************************************************************/ | |
4545 | ||
7348cc91 | 4546 | static bool lruvec_is_sizable(struct lruvec *lruvec, struct scan_control *sc) |
ac35a490 | 4547 | { |
7348cc91 YZ |
4548 | int gen, type, zone; |
4549 | unsigned long total = 0; | |
4550 | bool can_swap = get_swappiness(lruvec, sc); | |
4551 | struct lru_gen_folio *lrugen = &lruvec->lrugen; | |
ac35a490 YZ |
4552 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); |
4553 | DEFINE_MAX_SEQ(lruvec); | |
4554 | DEFINE_MIN_SEQ(lruvec); | |
4555 | ||
7348cc91 YZ |
4556 | for (type = !can_swap; type < ANON_AND_FILE; type++) { |
4557 | unsigned long seq; | |
ac35a490 | 4558 | |
7348cc91 YZ |
4559 | for (seq = min_seq[type]; seq <= max_seq; seq++) { |
4560 | gen = lru_gen_from_seq(seq); | |
ac35a490 | 4561 | |
7348cc91 YZ |
4562 | for (zone = 0; zone < MAX_NR_ZONES; zone++) |
4563 | total += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); | |
4564 | } | |
4565 | } | |
ac35a490 | 4566 | |
7348cc91 YZ |
4567 | /* whether the size is big enough to be helpful */ |
4568 | return mem_cgroup_online(memcg) ? (total >> sc->priority) : total; | |
4569 | } | |
1332a809 | 4570 | |
7348cc91 YZ |
4571 | static bool lruvec_is_reclaimable(struct lruvec *lruvec, struct scan_control *sc, |
4572 | unsigned long min_ttl) | |
4573 | { | |
4574 | int gen; | |
4575 | unsigned long birth; | |
4576 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
4577 | DEFINE_MIN_SEQ(lruvec); | |
1332a809 | 4578 | |
7348cc91 YZ |
4579 | /* see the comment on lru_gen_folio */ |
4580 | gen = lru_gen_from_seq(min_seq[LRU_GEN_FILE]); | |
4581 | birth = READ_ONCE(lruvec->lrugen.timestamps[gen]); | |
1332a809 | 4582 | |
7348cc91 YZ |
4583 | if (time_is_after_jiffies(birth + min_ttl)) |
4584 | return false; | |
1332a809 | 4585 | |
7348cc91 YZ |
4586 | if (!lruvec_is_sizable(lruvec, sc)) |
4587 | return false; | |
4588 | ||
4589 | mem_cgroup_calculate_protection(NULL, memcg); | |
4590 | ||
4591 | return !mem_cgroup_below_min(NULL, memcg); | |
ac35a490 YZ |
4592 | } |
4593 | ||
1332a809 YZ |
4594 | /* to protect the working set of the last N jiffies */ |
4595 | static unsigned long lru_gen_min_ttl __read_mostly; | |
4596 | ||
ac35a490 YZ |
4597 | static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc) |
4598 | { | |
4599 | struct mem_cgroup *memcg; | |
1332a809 | 4600 | unsigned long min_ttl = READ_ONCE(lru_gen_min_ttl); |
ac35a490 YZ |
4601 | |
4602 | VM_WARN_ON_ONCE(!current_is_kswapd()); | |
4603 | ||
7348cc91 YZ |
4604 | /* check the order to exclude compaction-induced reclaim */ |
4605 | if (!min_ttl || sc->order || sc->priority == DEF_PRIORITY) | |
f76c8337 | 4606 | return; |
bd74fdae | 4607 | |
ac35a490 YZ |
4608 | memcg = mem_cgroup_iter(NULL, NULL, NULL); |
4609 | do { | |
4610 | struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); | |
4611 | ||
7348cc91 YZ |
4612 | if (lruvec_is_reclaimable(lruvec, sc, min_ttl)) { |
4613 | mem_cgroup_iter_break(NULL, memcg); | |
4614 | return; | |
4615 | } | |
ac35a490 YZ |
4616 | |
4617 | cond_resched(); | |
4618 | } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); | |
bd74fdae | 4619 | |
1332a809 YZ |
4620 | /* |
4621 | * The main goal is to OOM kill if every generation from all memcgs is | |
4622 | * younger than min_ttl. However, another possibility is all memcgs are | |
7348cc91 | 4623 | * either too small or below min. |
1332a809 YZ |
4624 | */ |
4625 | if (mutex_trylock(&oom_lock)) { | |
4626 | struct oom_control oc = { | |
4627 | .gfp_mask = sc->gfp_mask, | |
4628 | }; | |
4629 | ||
4630 | out_of_memory(&oc); | |
4631 | ||
4632 | mutex_unlock(&oom_lock); | |
4633 | } | |
ac35a490 YZ |
4634 | } |
4635 | ||
db19a43d A |
4636 | /****************************************************************************** |
4637 | * rmap/PT walk feedback | |
4638 | ******************************************************************************/ | |
4639 | ||
018ee47f | 4640 | /* |
49fd9b6d | 4641 | * This function exploits spatial locality when shrink_folio_list() walks the |
bd74fdae YZ |
4642 | * rmap. It scans the adjacent PTEs of a young PTE and promotes hot pages. If |
4643 | * the scan was done cacheline efficiently, it adds the PMD entry pointing to | |
4644 | * the PTE table to the Bloom filter. This forms a feedback loop between the | |
4645 | * eviction and the aging. | |
018ee47f YZ |
4646 | */ |
4647 | void lru_gen_look_around(struct page_vma_mapped_walk *pvmw) | |
4648 | { | |
4649 | int i; | |
018ee47f YZ |
4650 | unsigned long start; |
4651 | unsigned long end; | |
bd74fdae YZ |
4652 | struct lru_gen_mm_walk *walk; |
4653 | int young = 0; | |
abf08672 A |
4654 | pte_t *pte = pvmw->pte; |
4655 | unsigned long addr = pvmw->address; | |
018ee47f YZ |
4656 | struct folio *folio = pfn_folio(pvmw->pfn); |
4657 | struct mem_cgroup *memcg = folio_memcg(folio); | |
4658 | struct pglist_data *pgdat = folio_pgdat(folio); | |
4659 | struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); | |
4660 | DEFINE_MAX_SEQ(lruvec); | |
4661 | int old_gen, new_gen = lru_gen_from_seq(max_seq); | |
4662 | ||
4663 | lockdep_assert_held(pvmw->ptl); | |
4664 | VM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio); | |
4665 | ||
4666 | if (spin_is_contended(pvmw->ptl)) | |
4667 | return; | |
4668 | ||
bd74fdae YZ |
4669 | /* avoid taking the LRU lock under the PTL when possible */ |
4670 | walk = current->reclaim_state ? current->reclaim_state->mm_walk : NULL; | |
4671 | ||
abf08672 A |
4672 | start = max(addr & PMD_MASK, pvmw->vma->vm_start); |
4673 | end = min(addr | ~PMD_MASK, pvmw->vma->vm_end - 1) + 1; | |
018ee47f YZ |
4674 | |
4675 | if (end - start > MIN_LRU_BATCH * PAGE_SIZE) { | |
abf08672 | 4676 | if (addr - start < MIN_LRU_BATCH * PAGE_SIZE / 2) |
018ee47f | 4677 | end = start + MIN_LRU_BATCH * PAGE_SIZE; |
abf08672 | 4678 | else if (end - addr < MIN_LRU_BATCH * PAGE_SIZE / 2) |
018ee47f YZ |
4679 | start = end - MIN_LRU_BATCH * PAGE_SIZE; |
4680 | else { | |
abf08672 A |
4681 | start = addr - MIN_LRU_BATCH * PAGE_SIZE / 2; |
4682 | end = addr + MIN_LRU_BATCH * PAGE_SIZE / 2; | |
018ee47f YZ |
4683 | } |
4684 | } | |
4685 | ||
abf08672 A |
4686 | /* folio_update_gen() requires stable folio_memcg() */ |
4687 | if (!mem_cgroup_trylock_pages(memcg)) | |
4688 | return; | |
018ee47f | 4689 | |
018ee47f YZ |
4690 | arch_enter_lazy_mmu_mode(); |
4691 | ||
abf08672 A |
4692 | pte -= (addr - start) / PAGE_SIZE; |
4693 | ||
018ee47f YZ |
4694 | for (i = 0, addr = start; addr != end; i++, addr += PAGE_SIZE) { |
4695 | unsigned long pfn; | |
4696 | ||
4697 | pfn = get_pte_pfn(pte[i], pvmw->vma, addr); | |
4698 | if (pfn == -1) | |
4699 | continue; | |
4700 | ||
4701 | if (!pte_young(pte[i])) | |
4702 | continue; | |
4703 | ||
bd74fdae | 4704 | folio = get_pfn_folio(pfn, memcg, pgdat, !walk || walk->can_swap); |
018ee47f YZ |
4705 | if (!folio) |
4706 | continue; | |
4707 | ||
4708 | if (!ptep_test_and_clear_young(pvmw->vma, addr, pte + i)) | |
4709 | VM_WARN_ON_ONCE(true); | |
4710 | ||
bd74fdae YZ |
4711 | young++; |
4712 | ||
018ee47f YZ |
4713 | if (pte_dirty(pte[i]) && !folio_test_dirty(folio) && |
4714 | !(folio_test_anon(folio) && folio_test_swapbacked(folio) && | |
4715 | !folio_test_swapcache(folio))) | |
4716 | folio_mark_dirty(folio); | |
4717 | ||
abf08672 A |
4718 | if (walk) { |
4719 | old_gen = folio_update_gen(folio, new_gen); | |
4720 | if (old_gen >= 0 && old_gen != new_gen) | |
4721 | update_batch_size(walk, folio, old_gen, new_gen); | |
4722 | ||
4723 | continue; | |
4724 | } | |
4725 | ||
018ee47f YZ |
4726 | old_gen = folio_lru_gen(folio); |
4727 | if (old_gen < 0) | |
4728 | folio_set_referenced(folio); | |
4729 | else if (old_gen != new_gen) | |
abf08672 | 4730 | folio_activate(folio); |
018ee47f YZ |
4731 | } |
4732 | ||
4733 | arch_leave_lazy_mmu_mode(); | |
abf08672 | 4734 | mem_cgroup_unlock_pages(); |
018ee47f | 4735 | |
bd74fdae YZ |
4736 | /* feedback from rmap walkers to page table walkers */ |
4737 | if (suitable_to_scan(i, young)) | |
4738 | update_bloom_filter(lruvec, max_seq, pvmw->pmd); | |
018ee47f YZ |
4739 | } |
4740 | ||
36c7b4db A |
4741 | /****************************************************************************** |
4742 | * memcg LRU | |
4743 | ******************************************************************************/ | |
4744 | ||
4745 | /* see the comment on MEMCG_NR_GENS */ | |
4746 | enum { | |
4747 | MEMCG_LRU_NOP, | |
4748 | MEMCG_LRU_HEAD, | |
4749 | MEMCG_LRU_TAIL, | |
4750 | MEMCG_LRU_OLD, | |
4751 | MEMCG_LRU_YOUNG, | |
4752 | }; | |
4753 | ||
4754 | #ifdef CONFIG_MEMCG | |
4755 | ||
4756 | static int lru_gen_memcg_seg(struct lruvec *lruvec) | |
4757 | { | |
4758 | return READ_ONCE(lruvec->lrugen.seg); | |
4759 | } | |
4760 | ||
4761 | static void lru_gen_rotate_memcg(struct lruvec *lruvec, int op) | |
4762 | { | |
4763 | int seg; | |
4764 | int old, new; | |
4765 | int bin = get_random_u32_below(MEMCG_NR_BINS); | |
4766 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); | |
4767 | ||
4768 | spin_lock(&pgdat->memcg_lru.lock); | |
4769 | ||
4770 | VM_WARN_ON_ONCE(hlist_nulls_unhashed(&lruvec->lrugen.list)); | |
4771 | ||
4772 | seg = 0; | |
4773 | new = old = lruvec->lrugen.gen; | |
4774 | ||
4775 | /* see the comment on MEMCG_NR_GENS */ | |
4776 | if (op == MEMCG_LRU_HEAD) | |
4777 | seg = MEMCG_LRU_HEAD; | |
4778 | else if (op == MEMCG_LRU_TAIL) | |
4779 | seg = MEMCG_LRU_TAIL; | |
4780 | else if (op == MEMCG_LRU_OLD) | |
4781 | new = get_memcg_gen(pgdat->memcg_lru.seq); | |
4782 | else if (op == MEMCG_LRU_YOUNG) | |
4783 | new = get_memcg_gen(pgdat->memcg_lru.seq + 1); | |
4784 | else | |
4785 | VM_WARN_ON_ONCE(true); | |
4786 | ||
4787 | hlist_nulls_del_rcu(&lruvec->lrugen.list); | |
4788 | ||
4789 | if (op == MEMCG_LRU_HEAD || op == MEMCG_LRU_OLD) | |
4790 | hlist_nulls_add_head_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[new][bin]); | |
4791 | else | |
4792 | hlist_nulls_add_tail_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[new][bin]); | |
4793 | ||
4794 | pgdat->memcg_lru.nr_memcgs[old]--; | |
4795 | pgdat->memcg_lru.nr_memcgs[new]++; | |
4796 | ||
4797 | lruvec->lrugen.gen = new; | |
4798 | WRITE_ONCE(lruvec->lrugen.seg, seg); | |
4799 | ||
4800 | if (!pgdat->memcg_lru.nr_memcgs[old] && old == get_memcg_gen(pgdat->memcg_lru.seq)) | |
4801 | WRITE_ONCE(pgdat->memcg_lru.seq, pgdat->memcg_lru.seq + 1); | |
4802 | ||
4803 | spin_unlock(&pgdat->memcg_lru.lock); | |
4804 | } | |
4805 | ||
4806 | void lru_gen_online_memcg(struct mem_cgroup *memcg) | |
4807 | { | |
4808 | int gen; | |
4809 | int nid; | |
4810 | int bin = get_random_u32_below(MEMCG_NR_BINS); | |
4811 | ||
4812 | for_each_node(nid) { | |
4813 | struct pglist_data *pgdat = NODE_DATA(nid); | |
4814 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
4815 | ||
4816 | spin_lock(&pgdat->memcg_lru.lock); | |
4817 | ||
4818 | VM_WARN_ON_ONCE(!hlist_nulls_unhashed(&lruvec->lrugen.list)); | |
4819 | ||
4820 | gen = get_memcg_gen(pgdat->memcg_lru.seq); | |
4821 | ||
4822 | hlist_nulls_add_tail_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[gen][bin]); | |
4823 | pgdat->memcg_lru.nr_memcgs[gen]++; | |
4824 | ||
4825 | lruvec->lrugen.gen = gen; | |
4826 | ||
4827 | spin_unlock(&pgdat->memcg_lru.lock); | |
4828 | } | |
4829 | } | |
4830 | ||
4831 | void lru_gen_offline_memcg(struct mem_cgroup *memcg) | |
4832 | { | |
4833 | int nid; | |
4834 | ||
4835 | for_each_node(nid) { | |
4836 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
4837 | ||
4838 | lru_gen_rotate_memcg(lruvec, MEMCG_LRU_OLD); | |
4839 | } | |
4840 | } | |
4841 | ||
4842 | void lru_gen_release_memcg(struct mem_cgroup *memcg) | |
4843 | { | |
4844 | int gen; | |
4845 | int nid; | |
4846 | ||
4847 | for_each_node(nid) { | |
4848 | struct pglist_data *pgdat = NODE_DATA(nid); | |
4849 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
4850 | ||
4851 | spin_lock(&pgdat->memcg_lru.lock); | |
4852 | ||
4853 | VM_WARN_ON_ONCE(hlist_nulls_unhashed(&lruvec->lrugen.list)); | |
4854 | ||
4855 | gen = lruvec->lrugen.gen; | |
4856 | ||
4857 | hlist_nulls_del_rcu(&lruvec->lrugen.list); | |
4858 | pgdat->memcg_lru.nr_memcgs[gen]--; | |
4859 | ||
4860 | if (!pgdat->memcg_lru.nr_memcgs[gen] && gen == get_memcg_gen(pgdat->memcg_lru.seq)) | |
4861 | WRITE_ONCE(pgdat->memcg_lru.seq, pgdat->memcg_lru.seq + 1); | |
4862 | ||
4863 | spin_unlock(&pgdat->memcg_lru.lock); | |
4864 | } | |
4865 | } | |
4866 | ||
4867 | void lru_gen_soft_reclaim(struct lruvec *lruvec) | |
4868 | { | |
4869 | /* see the comment on MEMCG_NR_GENS */ | |
4870 | if (lru_gen_memcg_seg(lruvec) != MEMCG_LRU_HEAD) | |
4871 | lru_gen_rotate_memcg(lruvec, MEMCG_LRU_HEAD); | |
4872 | } | |
4873 | ||
4874 | #else /* !CONFIG_MEMCG */ | |
4875 | ||
4876 | static int lru_gen_memcg_seg(struct lruvec *lruvec) | |
4877 | { | |
4878 | return 0; | |
4879 | } | |
4880 | ||
4881 | #endif | |
4882 | ||
ac35a490 YZ |
4883 | /****************************************************************************** |
4884 | * the eviction | |
4885 | ******************************************************************************/ | |
4886 | ||
4887 | static bool sort_folio(struct lruvec *lruvec, struct folio *folio, int tier_idx) | |
4888 | { | |
4889 | bool success; | |
4890 | int gen = folio_lru_gen(folio); | |
4891 | int type = folio_is_file_lru(folio); | |
4892 | int zone = folio_zonenum(folio); | |
4893 | int delta = folio_nr_pages(folio); | |
4894 | int refs = folio_lru_refs(folio); | |
4895 | int tier = lru_tier_from_refs(refs); | |
391655fe | 4896 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ac35a490 YZ |
4897 | |
4898 | VM_WARN_ON_ONCE_FOLIO(gen >= MAX_NR_GENS, folio); | |
4899 | ||
4900 | /* unevictable */ | |
4901 | if (!folio_evictable(folio)) { | |
4902 | success = lru_gen_del_folio(lruvec, folio, true); | |
4903 | VM_WARN_ON_ONCE_FOLIO(!success, folio); | |
4904 | folio_set_unevictable(folio); | |
4905 | lruvec_add_folio(lruvec, folio); | |
4906 | __count_vm_events(UNEVICTABLE_PGCULLED, delta); | |
4907 | return true; | |
4908 | } | |
4909 | ||
4910 | /* dirty lazyfree */ | |
4911 | if (type == LRU_GEN_FILE && folio_test_anon(folio) && folio_test_dirty(folio)) { | |
4912 | success = lru_gen_del_folio(lruvec, folio, true); | |
4913 | VM_WARN_ON_ONCE_FOLIO(!success, folio); | |
4914 | folio_set_swapbacked(folio); | |
4915 | lruvec_add_folio_tail(lruvec, folio); | |
4916 | return true; | |
4917 | } | |
4918 | ||
018ee47f YZ |
4919 | /* promoted */ |
4920 | if (gen != lru_gen_from_seq(lrugen->min_seq[type])) { | |
6df1b221 | 4921 | list_move(&folio->lru, &lrugen->folios[gen][type][zone]); |
018ee47f YZ |
4922 | return true; |
4923 | } | |
4924 | ||
ac35a490 YZ |
4925 | /* protected */ |
4926 | if (tier > tier_idx) { | |
4927 | int hist = lru_hist_from_seq(lrugen->min_seq[type]); | |
4928 | ||
4929 | gen = folio_inc_gen(lruvec, folio, false); | |
6df1b221 | 4930 | list_move_tail(&folio->lru, &lrugen->folios[gen][type][zone]); |
ac35a490 YZ |
4931 | |
4932 | WRITE_ONCE(lrugen->protected[hist][type][tier - 1], | |
4933 | lrugen->protected[hist][type][tier - 1] + delta); | |
4934 | __mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta); | |
4935 | return true; | |
4936 | } | |
4937 | ||
4938 | /* waiting for writeback */ | |
4939 | if (folio_test_locked(folio) || folio_test_writeback(folio) || | |
4940 | (type == LRU_GEN_FILE && folio_test_dirty(folio))) { | |
4941 | gen = folio_inc_gen(lruvec, folio, true); | |
6df1b221 | 4942 | list_move(&folio->lru, &lrugen->folios[gen][type][zone]); |
ac35a490 YZ |
4943 | return true; |
4944 | } | |
4945 | ||
4946 | return false; | |
4947 | } | |
4948 | ||
4949 | static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct scan_control *sc) | |
4950 | { | |
4951 | bool success; | |
4952 | ||
ac35a490 | 4953 | /* swapping inhibited */ |
e9d4e1ee | 4954 | if (!(sc->gfp_mask & __GFP_IO) && |
ac35a490 YZ |
4955 | (folio_test_dirty(folio) || |
4956 | (folio_test_anon(folio) && !folio_test_swapcache(folio)))) | |
4957 | return false; | |
4958 | ||
4959 | /* raced with release_pages() */ | |
4960 | if (!folio_try_get(folio)) | |
4961 | return false; | |
4962 | ||
4963 | /* raced with another isolation */ | |
4964 | if (!folio_test_clear_lru(folio)) { | |
4965 | folio_put(folio); | |
4966 | return false; | |
4967 | } | |
4968 | ||
4969 | /* see the comment on MAX_NR_TIERS */ | |
4970 | if (!folio_test_referenced(folio)) | |
4971 | set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, 0); | |
4972 | ||
49fd9b6d | 4973 | /* for shrink_folio_list() */ |
ac35a490 YZ |
4974 | folio_clear_reclaim(folio); |
4975 | folio_clear_referenced(folio); | |
4976 | ||
4977 | success = lru_gen_del_folio(lruvec, folio, true); | |
4978 | VM_WARN_ON_ONCE_FOLIO(!success, folio); | |
4979 | ||
4980 | return true; | |
4981 | } | |
4982 | ||
4983 | static int scan_folios(struct lruvec *lruvec, struct scan_control *sc, | |
4984 | int type, int tier, struct list_head *list) | |
4985 | { | |
4986 | int gen, zone; | |
4987 | enum vm_event_item item; | |
4988 | int sorted = 0; | |
4989 | int scanned = 0; | |
4990 | int isolated = 0; | |
4991 | int remaining = MAX_LRU_BATCH; | |
391655fe | 4992 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ac35a490 YZ |
4993 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); |
4994 | ||
4995 | VM_WARN_ON_ONCE(!list_empty(list)); | |
4996 | ||
4997 | if (get_nr_gens(lruvec, type) == MIN_NR_GENS) | |
4998 | return 0; | |
4999 | ||
5000 | gen = lru_gen_from_seq(lrugen->min_seq[type]); | |
5001 | ||
5002 | for (zone = sc->reclaim_idx; zone >= 0; zone--) { | |
5003 | LIST_HEAD(moved); | |
5004 | int skipped = 0; | |
6df1b221 | 5005 | struct list_head *head = &lrugen->folios[gen][type][zone]; |
ac35a490 YZ |
5006 | |
5007 | while (!list_empty(head)) { | |
5008 | struct folio *folio = lru_to_folio(head); | |
5009 | int delta = folio_nr_pages(folio); | |
5010 | ||
5011 | VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); | |
5012 | VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio); | |
5013 | VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio); | |
5014 | VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio); | |
5015 | ||
5016 | scanned += delta; | |
5017 | ||
5018 | if (sort_folio(lruvec, folio, tier)) | |
5019 | sorted += delta; | |
5020 | else if (isolate_folio(lruvec, folio, sc)) { | |
5021 | list_add(&folio->lru, list); | |
5022 | isolated += delta; | |
5023 | } else { | |
5024 | list_move(&folio->lru, &moved); | |
5025 | skipped += delta; | |
5026 | } | |
5027 | ||
5028 | if (!--remaining || max(isolated, skipped) >= MIN_LRU_BATCH) | |
5029 | break; | |
5030 | } | |
5031 | ||
5032 | if (skipped) { | |
5033 | list_splice(&moved, head); | |
5034 | __count_zid_vm_events(PGSCAN_SKIP, zone, skipped); | |
5035 | } | |
5036 | ||
5037 | if (!remaining || isolated >= MIN_LRU_BATCH) | |
5038 | break; | |
5039 | } | |
5040 | ||
57e9cc50 | 5041 | item = PGSCAN_KSWAPD + reclaimer_offset(); |
ac35a490 YZ |
5042 | if (!cgroup_reclaim(sc)) { |
5043 | __count_vm_events(item, isolated); | |
5044 | __count_vm_events(PGREFILL, sorted); | |
5045 | } | |
5046 | __count_memcg_events(memcg, item, isolated); | |
5047 | __count_memcg_events(memcg, PGREFILL, sorted); | |
5048 | __count_vm_events(PGSCAN_ANON + type, isolated); | |
5049 | ||
5050 | /* | |
e9d4e1ee YZ |
5051 | * There might not be eligible folios due to reclaim_idx. Check the |
5052 | * remaining to prevent livelock if it's not making progress. | |
ac35a490 YZ |
5053 | */ |
5054 | return isolated || !remaining ? scanned : 0; | |
5055 | } | |
5056 | ||
5057 | static int get_tier_idx(struct lruvec *lruvec, int type) | |
5058 | { | |
5059 | int tier; | |
5060 | struct ctrl_pos sp, pv; | |
5061 | ||
5062 | /* | |
5063 | * To leave a margin for fluctuations, use a larger gain factor (1:2). | |
5064 | * This value is chosen because any other tier would have at least twice | |
5065 | * as many refaults as the first tier. | |
5066 | */ | |
5067 | read_ctrl_pos(lruvec, type, 0, 1, &sp); | |
5068 | for (tier = 1; tier < MAX_NR_TIERS; tier++) { | |
5069 | read_ctrl_pos(lruvec, type, tier, 2, &pv); | |
5070 | if (!positive_ctrl_err(&sp, &pv)) | |
5071 | break; | |
5072 | } | |
5073 | ||
5074 | return tier - 1; | |
5075 | } | |
5076 | ||
5077 | static int get_type_to_scan(struct lruvec *lruvec, int swappiness, int *tier_idx) | |
5078 | { | |
5079 | int type, tier; | |
5080 | struct ctrl_pos sp, pv; | |
5081 | int gain[ANON_AND_FILE] = { swappiness, 200 - swappiness }; | |
5082 | ||
5083 | /* | |
5084 | * Compare the first tier of anon with that of file to determine which | |
5085 | * type to scan. Also need to compare other tiers of the selected type | |
5086 | * with the first tier of the other type to determine the last tier (of | |
5087 | * the selected type) to evict. | |
5088 | */ | |
5089 | read_ctrl_pos(lruvec, LRU_GEN_ANON, 0, gain[LRU_GEN_ANON], &sp); | |
5090 | read_ctrl_pos(lruvec, LRU_GEN_FILE, 0, gain[LRU_GEN_FILE], &pv); | |
5091 | type = positive_ctrl_err(&sp, &pv); | |
5092 | ||
5093 | read_ctrl_pos(lruvec, !type, 0, gain[!type], &sp); | |
5094 | for (tier = 1; tier < MAX_NR_TIERS; tier++) { | |
5095 | read_ctrl_pos(lruvec, type, tier, gain[type], &pv); | |
5096 | if (!positive_ctrl_err(&sp, &pv)) | |
5097 | break; | |
5098 | } | |
5099 | ||
5100 | *tier_idx = tier - 1; | |
5101 | ||
5102 | return type; | |
5103 | } | |
5104 | ||
5105 | static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness, | |
5106 | int *type_scanned, struct list_head *list) | |
5107 | { | |
5108 | int i; | |
5109 | int type; | |
5110 | int scanned; | |
5111 | int tier = -1; | |
5112 | DEFINE_MIN_SEQ(lruvec); | |
5113 | ||
5114 | /* | |
5115 | * Try to make the obvious choice first. When anon and file are both | |
5116 | * available from the same generation, interpret swappiness 1 as file | |
5117 | * first and 200 as anon first. | |
5118 | */ | |
5119 | if (!swappiness) | |
5120 | type = LRU_GEN_FILE; | |
5121 | else if (min_seq[LRU_GEN_ANON] < min_seq[LRU_GEN_FILE]) | |
5122 | type = LRU_GEN_ANON; | |
5123 | else if (swappiness == 1) | |
5124 | type = LRU_GEN_FILE; | |
5125 | else if (swappiness == 200) | |
5126 | type = LRU_GEN_ANON; | |
5127 | else | |
5128 | type = get_type_to_scan(lruvec, swappiness, &tier); | |
5129 | ||
5130 | for (i = !swappiness; i < ANON_AND_FILE; i++) { | |
5131 | if (tier < 0) | |
5132 | tier = get_tier_idx(lruvec, type); | |
5133 | ||
5134 | scanned = scan_folios(lruvec, sc, type, tier, list); | |
5135 | if (scanned) | |
5136 | break; | |
5137 | ||
5138 | type = !type; | |
5139 | tier = -1; | |
5140 | } | |
5141 | ||
5142 | *type_scanned = type; | |
5143 | ||
5144 | return scanned; | |
5145 | } | |
5146 | ||
a579086c | 5147 | static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness) |
ac35a490 YZ |
5148 | { |
5149 | int type; | |
5150 | int scanned; | |
5151 | int reclaimed; | |
5152 | LIST_HEAD(list); | |
359a5e14 | 5153 | LIST_HEAD(clean); |
ac35a490 | 5154 | struct folio *folio; |
359a5e14 | 5155 | struct folio *next; |
ac35a490 YZ |
5156 | enum vm_event_item item; |
5157 | struct reclaim_stat stat; | |
bd74fdae | 5158 | struct lru_gen_mm_walk *walk; |
359a5e14 | 5159 | bool skip_retry = false; |
ac35a490 YZ |
5160 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); |
5161 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); | |
5162 | ||
5163 | spin_lock_irq(&lruvec->lru_lock); | |
5164 | ||
5165 | scanned = isolate_folios(lruvec, sc, swappiness, &type, &list); | |
5166 | ||
5167 | scanned += try_to_inc_min_seq(lruvec, swappiness); | |
5168 | ||
5169 | if (get_nr_gens(lruvec, !swappiness) == MIN_NR_GENS) | |
5170 | scanned = 0; | |
5171 | ||
5172 | spin_unlock_irq(&lruvec->lru_lock); | |
5173 | ||
5174 | if (list_empty(&list)) | |
5175 | return scanned; | |
359a5e14 | 5176 | retry: |
49fd9b6d | 5177 | reclaimed = shrink_folio_list(&list, pgdat, sc, &stat, false); |
359a5e14 | 5178 | sc->nr_reclaimed += reclaimed; |
ac35a490 | 5179 | |
359a5e14 YZ |
5180 | list_for_each_entry_safe_reverse(folio, next, &list, lru) { |
5181 | if (!folio_evictable(folio)) { | |
5182 | list_del(&folio->lru); | |
5183 | folio_putback_lru(folio); | |
5184 | continue; | |
5185 | } | |
ac35a490 | 5186 | |
ac35a490 | 5187 | if (folio_test_reclaim(folio) && |
359a5e14 YZ |
5188 | (folio_test_dirty(folio) || folio_test_writeback(folio))) { |
5189 | /* restore LRU_REFS_FLAGS cleared by isolate_folio() */ | |
5190 | if (folio_test_workingset(folio)) | |
5191 | folio_set_referenced(folio); | |
5192 | continue; | |
5193 | } | |
5194 | ||
5195 | if (skip_retry || folio_test_active(folio) || folio_test_referenced(folio) || | |
5196 | folio_mapped(folio) || folio_test_locked(folio) || | |
5197 | folio_test_dirty(folio) || folio_test_writeback(folio)) { | |
5198 | /* don't add rejected folios to the oldest generation */ | |
5199 | set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, | |
5200 | BIT(PG_active)); | |
5201 | continue; | |
5202 | } | |
5203 | ||
5204 | /* retry folios that may have missed folio_rotate_reclaimable() */ | |
5205 | list_move(&folio->lru, &clean); | |
5206 | sc->nr_scanned -= folio_nr_pages(folio); | |
ac35a490 YZ |
5207 | } |
5208 | ||
5209 | spin_lock_irq(&lruvec->lru_lock); | |
5210 | ||
49fd9b6d | 5211 | move_folios_to_lru(lruvec, &list); |
ac35a490 | 5212 | |
bd74fdae YZ |
5213 | walk = current->reclaim_state->mm_walk; |
5214 | if (walk && walk->batched) | |
5215 | reset_batch_size(lruvec, walk); | |
5216 | ||
57e9cc50 | 5217 | item = PGSTEAL_KSWAPD + reclaimer_offset(); |
ac35a490 YZ |
5218 | if (!cgroup_reclaim(sc)) |
5219 | __count_vm_events(item, reclaimed); | |
5220 | __count_memcg_events(memcg, item, reclaimed); | |
5221 | __count_vm_events(PGSTEAL_ANON + type, reclaimed); | |
5222 | ||
5223 | spin_unlock_irq(&lruvec->lru_lock); | |
5224 | ||
5225 | mem_cgroup_uncharge_list(&list); | |
5226 | free_unref_page_list(&list); | |
5227 | ||
359a5e14 YZ |
5228 | INIT_LIST_HEAD(&list); |
5229 | list_splice_init(&clean, &list); | |
5230 | ||
5231 | if (!list_empty(&list)) { | |
5232 | skip_retry = true; | |
5233 | goto retry; | |
5234 | } | |
ac35a490 YZ |
5235 | |
5236 | return scanned; | |
5237 | } | |
5238 | ||
77d4459a YZ |
5239 | static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, |
5240 | struct scan_control *sc, bool can_swap, unsigned long *nr_to_scan) | |
5241 | { | |
5242 | int gen, type, zone; | |
5243 | unsigned long old = 0; | |
5244 | unsigned long young = 0; | |
5245 | unsigned long total = 0; | |
5246 | struct lru_gen_folio *lrugen = &lruvec->lrugen; | |
5247 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
5248 | DEFINE_MIN_SEQ(lruvec); | |
5249 | ||
5250 | /* whether this lruvec is completely out of cold folios */ | |
5251 | if (min_seq[!can_swap] + MIN_NR_GENS > max_seq) { | |
5252 | *nr_to_scan = 0; | |
5253 | return true; | |
5254 | } | |
5255 | ||
5256 | for (type = !can_swap; type < ANON_AND_FILE; type++) { | |
5257 | unsigned long seq; | |
5258 | ||
5259 | for (seq = min_seq[type]; seq <= max_seq; seq++) { | |
5260 | unsigned long size = 0; | |
5261 | ||
5262 | gen = lru_gen_from_seq(seq); | |
5263 | ||
5264 | for (zone = 0; zone < MAX_NR_ZONES; zone++) | |
5265 | size += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); | |
5266 | ||
5267 | total += size; | |
5268 | if (seq == max_seq) | |
5269 | young += size; | |
5270 | else if (seq + MIN_NR_GENS == max_seq) | |
5271 | old += size; | |
5272 | } | |
5273 | } | |
5274 | ||
5275 | /* try to scrape all its memory if this memcg was deleted */ | |
5276 | *nr_to_scan = mem_cgroup_online(memcg) ? (total >> sc->priority) : total; | |
5277 | ||
5278 | /* | |
5279 | * The aging tries to be lazy to reduce the overhead, while the eviction | |
5280 | * stalls when the number of generations reaches MIN_NR_GENS. Hence, the | |
5281 | * ideal number of generations is MIN_NR_GENS+1. | |
5282 | */ | |
5283 | if (min_seq[!can_swap] + MIN_NR_GENS < max_seq) | |
5284 | return false; | |
5285 | ||
5286 | /* | |
5287 | * It's also ideal to spread pages out evenly, i.e., 1/(MIN_NR_GENS+1) | |
5288 | * of the total number of pages for each generation. A reasonable range | |
5289 | * for this average portion is [1/MIN_NR_GENS, 1/(MIN_NR_GENS+2)]. The | |
5290 | * aging cares about the upper bound of hot pages, while the eviction | |
5291 | * cares about the lower bound of cold pages. | |
5292 | */ | |
5293 | if (young * MIN_NR_GENS > total) | |
5294 | return true; | |
5295 | if (old * (MIN_NR_GENS + 2) < total) | |
5296 | return true; | |
5297 | ||
5298 | return false; | |
5299 | } | |
5300 | ||
bd74fdae YZ |
5301 | /* |
5302 | * For future optimizations: | |
5303 | * 1. Defer try_to_inc_max_seq() to workqueues to reduce latency for memcg | |
5304 | * reclaim. | |
5305 | */ | |
e4dde56c | 5306 | static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc, bool can_swap) |
ac35a490 | 5307 | { |
ac35a490 YZ |
5308 | unsigned long nr_to_scan; |
5309 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
5310 | DEFINE_MAX_SEQ(lruvec); | |
ac35a490 | 5311 | |
e9d4e1ee | 5312 | if (mem_cgroup_below_min(sc->target_mem_cgroup, memcg)) |
ac35a490 YZ |
5313 | return 0; |
5314 | ||
7348cc91 | 5315 | if (!should_run_aging(lruvec, max_seq, sc, can_swap, &nr_to_scan)) |
ac35a490 YZ |
5316 | return nr_to_scan; |
5317 | ||
5318 | /* skip the aging path at the default priority */ | |
5319 | if (sc->priority == DEF_PRIORITY) | |
7348cc91 | 5320 | return nr_to_scan; |
ac35a490 | 5321 | |
7348cc91 | 5322 | /* skip this lruvec as it's low on cold folios */ |
e4dde56c | 5323 | return try_to_inc_max_seq(lruvec, max_seq, sc, can_swap, false) ? -1 : 0; |
ac35a490 YZ |
5324 | } |
5325 | ||
a579086c | 5326 | static unsigned long get_nr_to_reclaim(struct scan_control *sc) |
f76c8337 | 5327 | { |
a579086c YZ |
5328 | /* don't abort memcg reclaim to ensure fairness */ |
5329 | if (!global_reclaim(sc)) | |
5330 | return -1; | |
f76c8337 | 5331 | |
a579086c | 5332 | return max(sc->nr_to_reclaim, compact_gap(sc->order)); |
f76c8337 YZ |
5333 | } |
5334 | ||
e4dde56c | 5335 | static bool try_to_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) |
ac35a490 | 5336 | { |
e4dde56c | 5337 | long nr_to_scan; |
ac35a490 | 5338 | unsigned long scanned = 0; |
a579086c | 5339 | unsigned long nr_to_reclaim = get_nr_to_reclaim(sc); |
e9d4e1ee YZ |
5340 | int swappiness = get_swappiness(lruvec, sc); |
5341 | ||
5342 | /* clean file folios are more likely to exist */ | |
5343 | if (swappiness && !(sc->gfp_mask & __GFP_IO)) | |
5344 | swappiness = 1; | |
ac35a490 | 5345 | |
ac35a490 YZ |
5346 | while (true) { |
5347 | int delta; | |
ac35a490 | 5348 | |
7348cc91 | 5349 | nr_to_scan = get_nr_to_scan(lruvec, sc, swappiness); |
e4dde56c | 5350 | if (nr_to_scan <= 0) |
7348cc91 | 5351 | break; |
ac35a490 | 5352 | |
a579086c | 5353 | delta = evict_folios(lruvec, sc, swappiness); |
ac35a490 | 5354 | if (!delta) |
7348cc91 | 5355 | break; |
ac35a490 YZ |
5356 | |
5357 | scanned += delta; | |
5358 | if (scanned >= nr_to_scan) | |
5359 | break; | |
5360 | ||
a579086c | 5361 | if (sc->nr_reclaimed >= nr_to_reclaim) |
f76c8337 YZ |
5362 | break; |
5363 | ||
ac35a490 YZ |
5364 | cond_resched(); |
5365 | } | |
5366 | ||
e4dde56c YZ |
5367 | /* whether try_to_inc_max_seq() was successful */ |
5368 | return nr_to_scan < 0; | |
5369 | } | |
5370 | ||
5371 | static int shrink_one(struct lruvec *lruvec, struct scan_control *sc) | |
5372 | { | |
5373 | bool success; | |
5374 | unsigned long scanned = sc->nr_scanned; | |
5375 | unsigned long reclaimed = sc->nr_reclaimed; | |
5376 | int seg = lru_gen_memcg_seg(lruvec); | |
5377 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
5378 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); | |
5379 | ||
5380 | /* see the comment on MEMCG_NR_GENS */ | |
5381 | if (!lruvec_is_sizable(lruvec, sc)) | |
5382 | return seg != MEMCG_LRU_TAIL ? MEMCG_LRU_TAIL : MEMCG_LRU_YOUNG; | |
5383 | ||
5384 | mem_cgroup_calculate_protection(NULL, memcg); | |
5385 | ||
5386 | if (mem_cgroup_below_min(NULL, memcg)) | |
5387 | return MEMCG_LRU_YOUNG; | |
5388 | ||
5389 | if (mem_cgroup_below_low(NULL, memcg)) { | |
5390 | /* see the comment on MEMCG_NR_GENS */ | |
5391 | if (seg != MEMCG_LRU_TAIL) | |
5392 | return MEMCG_LRU_TAIL; | |
5393 | ||
5394 | memcg_memory_event(memcg, MEMCG_LOW); | |
5395 | } | |
5396 | ||
5397 | success = try_to_shrink_lruvec(lruvec, sc); | |
5398 | ||
5399 | shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, sc->priority); | |
5400 | ||
5401 | if (!sc->proactive) | |
5402 | vmpressure(sc->gfp_mask, memcg, false, sc->nr_scanned - scanned, | |
5403 | sc->nr_reclaimed - reclaimed); | |
5404 | ||
583c27a1 | 5405 | flush_reclaim_state(sc); |
e4dde56c YZ |
5406 | |
5407 | return success ? MEMCG_LRU_YOUNG : 0; | |
5408 | } | |
5409 | ||
5410 | #ifdef CONFIG_MEMCG | |
5411 | ||
5412 | static void shrink_many(struct pglist_data *pgdat, struct scan_control *sc) | |
5413 | { | |
9f550d78 | 5414 | int op; |
e4dde56c YZ |
5415 | int gen; |
5416 | int bin; | |
5417 | int first_bin; | |
5418 | struct lruvec *lruvec; | |
5419 | struct lru_gen_folio *lrugen; | |
9f550d78 | 5420 | struct mem_cgroup *memcg; |
e4dde56c | 5421 | const struct hlist_nulls_node *pos; |
e4dde56c YZ |
5422 | unsigned long nr_to_reclaim = get_nr_to_reclaim(sc); |
5423 | ||
5424 | bin = first_bin = get_random_u32_below(MEMCG_NR_BINS); | |
5425 | restart: | |
9f550d78 YZ |
5426 | op = 0; |
5427 | memcg = NULL; | |
e4dde56c YZ |
5428 | gen = get_memcg_gen(READ_ONCE(pgdat->memcg_lru.seq)); |
5429 | ||
5430 | rcu_read_lock(); | |
5431 | ||
5432 | hlist_nulls_for_each_entry_rcu(lrugen, pos, &pgdat->memcg_lru.fifo[gen][bin], list) { | |
5433 | if (op) | |
5434 | lru_gen_rotate_memcg(lruvec, op); | |
5435 | ||
5436 | mem_cgroup_put(memcg); | |
5437 | ||
5438 | lruvec = container_of(lrugen, struct lruvec, lrugen); | |
5439 | memcg = lruvec_memcg(lruvec); | |
5440 | ||
5441 | if (!mem_cgroup_tryget(memcg)) { | |
5442 | op = 0; | |
5443 | memcg = NULL; | |
5444 | continue; | |
5445 | } | |
5446 | ||
5447 | rcu_read_unlock(); | |
5448 | ||
5449 | op = shrink_one(lruvec, sc); | |
5450 | ||
e4dde56c | 5451 | rcu_read_lock(); |
9f550d78 YZ |
5452 | |
5453 | if (sc->nr_reclaimed >= nr_to_reclaim) | |
5454 | break; | |
e4dde56c YZ |
5455 | } |
5456 | ||
5457 | rcu_read_unlock(); | |
5458 | ||
9f550d78 YZ |
5459 | if (op) |
5460 | lru_gen_rotate_memcg(lruvec, op); | |
5461 | ||
5462 | mem_cgroup_put(memcg); | |
5463 | ||
5464 | if (sc->nr_reclaimed >= nr_to_reclaim) | |
5465 | return; | |
5466 | ||
e4dde56c YZ |
5467 | /* restart if raced with lru_gen_rotate_memcg() */ |
5468 | if (gen != get_nulls_value(pos)) | |
5469 | goto restart; | |
5470 | ||
5471 | /* try the rest of the bins of the current generation */ | |
5472 | bin = get_memcg_bin(bin + 1); | |
5473 | if (bin != first_bin) | |
5474 | goto restart; | |
e4dde56c YZ |
5475 | } |
5476 | ||
5477 | static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) | |
5478 | { | |
5479 | struct blk_plug plug; | |
5480 | ||
5481 | VM_WARN_ON_ONCE(global_reclaim(sc)); | |
e9d4e1ee | 5482 | VM_WARN_ON_ONCE(!sc->may_writepage || !sc->may_unmap); |
e4dde56c YZ |
5483 | |
5484 | lru_add_drain(); | |
5485 | ||
5486 | blk_start_plug(&plug); | |
5487 | ||
e9d4e1ee | 5488 | set_mm_walk(NULL, sc->proactive); |
e4dde56c YZ |
5489 | |
5490 | if (try_to_shrink_lruvec(lruvec, sc)) | |
5491 | lru_gen_rotate_memcg(lruvec, MEMCG_LRU_YOUNG); | |
5492 | ||
5493 | clear_mm_walk(); | |
5494 | ||
5495 | blk_finish_plug(&plug); | |
5496 | } | |
5497 | ||
5498 | #else /* !CONFIG_MEMCG */ | |
5499 | ||
5500 | static void shrink_many(struct pglist_data *pgdat, struct scan_control *sc) | |
5501 | { | |
5502 | BUILD_BUG(); | |
5503 | } | |
5504 | ||
5505 | static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) | |
5506 | { | |
5507 | BUILD_BUG(); | |
5508 | } | |
5509 | ||
5510 | #endif | |
5511 | ||
5512 | static void set_initial_priority(struct pglist_data *pgdat, struct scan_control *sc) | |
5513 | { | |
5514 | int priority; | |
5515 | unsigned long reclaimable; | |
5516 | struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat); | |
5517 | ||
5518 | if (sc->priority != DEF_PRIORITY || sc->nr_to_reclaim < MIN_LRU_BATCH) | |
5519 | return; | |
5520 | /* | |
5521 | * Determine the initial priority based on ((total / MEMCG_NR_GENS) >> | |
5522 | * priority) * reclaimed_to_scanned_ratio = nr_to_reclaim, where the | |
5523 | * estimated reclaimed_to_scanned_ratio = inactive / total. | |
5524 | */ | |
5525 | reclaimable = node_page_state(pgdat, NR_INACTIVE_FILE); | |
5526 | if (get_swappiness(lruvec, sc)) | |
5527 | reclaimable += node_page_state(pgdat, NR_INACTIVE_ANON); | |
5528 | ||
5529 | reclaimable /= MEMCG_NR_GENS; | |
5530 | ||
5531 | /* round down reclaimable and round up sc->nr_to_reclaim */ | |
5532 | priority = fls_long(reclaimable) - 1 - fls_long(sc->nr_to_reclaim - 1); | |
5533 | ||
5534 | sc->priority = clamp(priority, 0, DEF_PRIORITY); | |
5535 | } | |
5536 | ||
5537 | static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *sc) | |
5538 | { | |
5539 | struct blk_plug plug; | |
5540 | unsigned long reclaimed = sc->nr_reclaimed; | |
5541 | ||
5542 | VM_WARN_ON_ONCE(!global_reclaim(sc)); | |
5543 | ||
e9d4e1ee YZ |
5544 | /* |
5545 | * Unmapped clean folios are already prioritized. Scanning for more of | |
5546 | * them is likely futile and can cause high reclaim latency when there | |
5547 | * is a large number of memcgs. | |
5548 | */ | |
5549 | if (!sc->may_writepage || !sc->may_unmap) | |
5550 | goto done; | |
5551 | ||
e4dde56c YZ |
5552 | lru_add_drain(); |
5553 | ||
5554 | blk_start_plug(&plug); | |
5555 | ||
e9d4e1ee | 5556 | set_mm_walk(pgdat, sc->proactive); |
e4dde56c YZ |
5557 | |
5558 | set_initial_priority(pgdat, sc); | |
5559 | ||
5560 | if (current_is_kswapd()) | |
5561 | sc->nr_reclaimed = 0; | |
5562 | ||
5563 | if (mem_cgroup_disabled()) | |
5564 | shrink_one(&pgdat->__lruvec, sc); | |
5565 | else | |
5566 | shrink_many(pgdat, sc); | |
5567 | ||
5568 | if (current_is_kswapd()) | |
5569 | sc->nr_reclaimed += reclaimed; | |
5570 | ||
bd74fdae YZ |
5571 | clear_mm_walk(); |
5572 | ||
ac35a490 | 5573 | blk_finish_plug(&plug); |
e9d4e1ee | 5574 | done: |
e4dde56c YZ |
5575 | /* kswapd should never fail */ |
5576 | pgdat->kswapd_failures = 0; | |
5577 | } | |
5578 | ||
354ed597 YZ |
5579 | /****************************************************************************** |
5580 | * state change | |
5581 | ******************************************************************************/ | |
5582 | ||
5583 | static bool __maybe_unused state_is_valid(struct lruvec *lruvec) | |
5584 | { | |
391655fe | 5585 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
354ed597 YZ |
5586 | |
5587 | if (lrugen->enabled) { | |
5588 | enum lru_list lru; | |
5589 | ||
5590 | for_each_evictable_lru(lru) { | |
5591 | if (!list_empty(&lruvec->lists[lru])) | |
5592 | return false; | |
5593 | } | |
5594 | } else { | |
5595 | int gen, type, zone; | |
5596 | ||
5597 | for_each_gen_type_zone(gen, type, zone) { | |
6df1b221 | 5598 | if (!list_empty(&lrugen->folios[gen][type][zone])) |
354ed597 YZ |
5599 | return false; |
5600 | } | |
5601 | } | |
5602 | ||
5603 | return true; | |
5604 | } | |
5605 | ||
5606 | static bool fill_evictable(struct lruvec *lruvec) | |
5607 | { | |
5608 | enum lru_list lru; | |
5609 | int remaining = MAX_LRU_BATCH; | |
5610 | ||
5611 | for_each_evictable_lru(lru) { | |
5612 | int type = is_file_lru(lru); | |
5613 | bool active = is_active_lru(lru); | |
5614 | struct list_head *head = &lruvec->lists[lru]; | |
5615 | ||
5616 | while (!list_empty(head)) { | |
5617 | bool success; | |
5618 | struct folio *folio = lru_to_folio(head); | |
5619 | ||
5620 | VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); | |
5621 | VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio) != active, folio); | |
5622 | VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio); | |
5623 | VM_WARN_ON_ONCE_FOLIO(folio_lru_gen(folio) != -1, folio); | |
5624 | ||
5625 | lruvec_del_folio(lruvec, folio); | |
5626 | success = lru_gen_add_folio(lruvec, folio, false); | |
5627 | VM_WARN_ON_ONCE(!success); | |
5628 | ||
5629 | if (!--remaining) | |
5630 | return false; | |
5631 | } | |
5632 | } | |
5633 | ||
5634 | return true; | |
5635 | } | |
5636 | ||
5637 | static bool drain_evictable(struct lruvec *lruvec) | |
5638 | { | |
5639 | int gen, type, zone; | |
5640 | int remaining = MAX_LRU_BATCH; | |
5641 | ||
5642 | for_each_gen_type_zone(gen, type, zone) { | |
6df1b221 | 5643 | struct list_head *head = &lruvec->lrugen.folios[gen][type][zone]; |
354ed597 YZ |
5644 | |
5645 | while (!list_empty(head)) { | |
5646 | bool success; | |
5647 | struct folio *folio = lru_to_folio(head); | |
5648 | ||
5649 | VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); | |
5650 | VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio); | |
5651 | VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio); | |
5652 | VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio); | |
5653 | ||
5654 | success = lru_gen_del_folio(lruvec, folio, false); | |
5655 | VM_WARN_ON_ONCE(!success); | |
5656 | lruvec_add_folio(lruvec, folio); | |
5657 | ||
5658 | if (!--remaining) | |
5659 | return false; | |
5660 | } | |
5661 | } | |
5662 | ||
5663 | return true; | |
5664 | } | |
5665 | ||
5666 | static void lru_gen_change_state(bool enabled) | |
5667 | { | |
5668 | static DEFINE_MUTEX(state_mutex); | |
5669 | ||
5670 | struct mem_cgroup *memcg; | |
5671 | ||
5672 | cgroup_lock(); | |
5673 | cpus_read_lock(); | |
5674 | get_online_mems(); | |
5675 | mutex_lock(&state_mutex); | |
5676 | ||
5677 | if (enabled == lru_gen_enabled()) | |
5678 | goto unlock; | |
5679 | ||
5680 | if (enabled) | |
5681 | static_branch_enable_cpuslocked(&lru_gen_caps[LRU_GEN_CORE]); | |
5682 | else | |
5683 | static_branch_disable_cpuslocked(&lru_gen_caps[LRU_GEN_CORE]); | |
5684 | ||
5685 | memcg = mem_cgroup_iter(NULL, NULL, NULL); | |
5686 | do { | |
5687 | int nid; | |
5688 | ||
5689 | for_each_node(nid) { | |
5690 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
5691 | ||
354ed597 YZ |
5692 | spin_lock_irq(&lruvec->lru_lock); |
5693 | ||
5694 | VM_WARN_ON_ONCE(!seq_is_valid(lruvec)); | |
5695 | VM_WARN_ON_ONCE(!state_is_valid(lruvec)); | |
5696 | ||
5697 | lruvec->lrugen.enabled = enabled; | |
5698 | ||
5699 | while (!(enabled ? fill_evictable(lruvec) : drain_evictable(lruvec))) { | |
5700 | spin_unlock_irq(&lruvec->lru_lock); | |
5701 | cond_resched(); | |
5702 | spin_lock_irq(&lruvec->lru_lock); | |
5703 | } | |
5704 | ||
5705 | spin_unlock_irq(&lruvec->lru_lock); | |
5706 | } | |
5707 | ||
5708 | cond_resched(); | |
5709 | } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); | |
5710 | unlock: | |
5711 | mutex_unlock(&state_mutex); | |
5712 | put_online_mems(); | |
5713 | cpus_read_unlock(); | |
5714 | cgroup_unlock(); | |
5715 | } | |
5716 | ||
5717 | /****************************************************************************** | |
5718 | * sysfs interface | |
5719 | ******************************************************************************/ | |
5720 | ||
9a52b2f3 | 5721 | static ssize_t min_ttl_ms_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
1332a809 | 5722 | { |
9a52b2f3 | 5723 | return sysfs_emit(buf, "%u\n", jiffies_to_msecs(READ_ONCE(lru_gen_min_ttl))); |
1332a809 YZ |
5724 | } |
5725 | ||
07017acb | 5726 | /* see Documentation/admin-guide/mm/multigen_lru.rst for details */ |
9a52b2f3 A |
5727 | static ssize_t min_ttl_ms_store(struct kobject *kobj, struct kobj_attribute *attr, |
5728 | const char *buf, size_t len) | |
1332a809 YZ |
5729 | { |
5730 | unsigned int msecs; | |
5731 | ||
5732 | if (kstrtouint(buf, 0, &msecs)) | |
5733 | return -EINVAL; | |
5734 | ||
5735 | WRITE_ONCE(lru_gen_min_ttl, msecs_to_jiffies(msecs)); | |
5736 | ||
5737 | return len; | |
5738 | } | |
5739 | ||
9a52b2f3 | 5740 | static struct kobj_attribute lru_gen_min_ttl_attr = __ATTR_RW(min_ttl_ms); |
1332a809 | 5741 | |
9a52b2f3 | 5742 | static ssize_t enabled_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
354ed597 YZ |
5743 | { |
5744 | unsigned int caps = 0; | |
5745 | ||
5746 | if (get_cap(LRU_GEN_CORE)) | |
5747 | caps |= BIT(LRU_GEN_CORE); | |
5748 | ||
5749 | if (arch_has_hw_pte_young() && get_cap(LRU_GEN_MM_WALK)) | |
5750 | caps |= BIT(LRU_GEN_MM_WALK); | |
5751 | ||
4aaf269c | 5752 | if (arch_has_hw_nonleaf_pmd_young() && get_cap(LRU_GEN_NONLEAF_YOUNG)) |
354ed597 YZ |
5753 | caps |= BIT(LRU_GEN_NONLEAF_YOUNG); |
5754 | ||
8ef9c32a | 5755 | return sysfs_emit(buf, "0x%04x\n", caps); |
354ed597 YZ |
5756 | } |
5757 | ||
07017acb | 5758 | /* see Documentation/admin-guide/mm/multigen_lru.rst for details */ |
9a52b2f3 | 5759 | static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, |
354ed597 YZ |
5760 | const char *buf, size_t len) |
5761 | { | |
5762 | int i; | |
5763 | unsigned int caps; | |
5764 | ||
5765 | if (tolower(*buf) == 'n') | |
5766 | caps = 0; | |
5767 | else if (tolower(*buf) == 'y') | |
5768 | caps = -1; | |
5769 | else if (kstrtouint(buf, 0, &caps)) | |
5770 | return -EINVAL; | |
5771 | ||
5772 | for (i = 0; i < NR_LRU_GEN_CAPS; i++) { | |
5773 | bool enabled = caps & BIT(i); | |
5774 | ||
5775 | if (i == LRU_GEN_CORE) | |
5776 | lru_gen_change_state(enabled); | |
5777 | else if (enabled) | |
5778 | static_branch_enable(&lru_gen_caps[i]); | |
5779 | else | |
5780 | static_branch_disable(&lru_gen_caps[i]); | |
5781 | } | |
5782 | ||
5783 | return len; | |
5784 | } | |
5785 | ||
9a52b2f3 | 5786 | static struct kobj_attribute lru_gen_enabled_attr = __ATTR_RW(enabled); |
354ed597 YZ |
5787 | |
5788 | static struct attribute *lru_gen_attrs[] = { | |
1332a809 | 5789 | &lru_gen_min_ttl_attr.attr, |
354ed597 YZ |
5790 | &lru_gen_enabled_attr.attr, |
5791 | NULL | |
5792 | }; | |
5793 | ||
9a52b2f3 | 5794 | static const struct attribute_group lru_gen_attr_group = { |
354ed597 YZ |
5795 | .name = "lru_gen", |
5796 | .attrs = lru_gen_attrs, | |
5797 | }; | |
5798 | ||
d6c3af7d YZ |
5799 | /****************************************************************************** |
5800 | * debugfs interface | |
5801 | ******************************************************************************/ | |
5802 | ||
5803 | static void *lru_gen_seq_start(struct seq_file *m, loff_t *pos) | |
5804 | { | |
5805 | struct mem_cgroup *memcg; | |
5806 | loff_t nr_to_skip = *pos; | |
5807 | ||
5808 | m->private = kvmalloc(PATH_MAX, GFP_KERNEL); | |
5809 | if (!m->private) | |
5810 | return ERR_PTR(-ENOMEM); | |
5811 | ||
5812 | memcg = mem_cgroup_iter(NULL, NULL, NULL); | |
5813 | do { | |
5814 | int nid; | |
5815 | ||
5816 | for_each_node_state(nid, N_MEMORY) { | |
5817 | if (!nr_to_skip--) | |
5818 | return get_lruvec(memcg, nid); | |
5819 | } | |
5820 | } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); | |
5821 | ||
5822 | return NULL; | |
5823 | } | |
5824 | ||
5825 | static void lru_gen_seq_stop(struct seq_file *m, void *v) | |
5826 | { | |
5827 | if (!IS_ERR_OR_NULL(v)) | |
5828 | mem_cgroup_iter_break(NULL, lruvec_memcg(v)); | |
5829 | ||
5830 | kvfree(m->private); | |
5831 | m->private = NULL; | |
5832 | } | |
5833 | ||
5834 | static void *lru_gen_seq_next(struct seq_file *m, void *v, loff_t *pos) | |
5835 | { | |
5836 | int nid = lruvec_pgdat(v)->node_id; | |
5837 | struct mem_cgroup *memcg = lruvec_memcg(v); | |
5838 | ||
5839 | ++*pos; | |
5840 | ||
5841 | nid = next_memory_node(nid); | |
5842 | if (nid == MAX_NUMNODES) { | |
5843 | memcg = mem_cgroup_iter(NULL, memcg, NULL); | |
5844 | if (!memcg) | |
5845 | return NULL; | |
5846 | ||
5847 | nid = first_memory_node; | |
5848 | } | |
5849 | ||
5850 | return get_lruvec(memcg, nid); | |
5851 | } | |
5852 | ||
5853 | static void lru_gen_seq_show_full(struct seq_file *m, struct lruvec *lruvec, | |
5854 | unsigned long max_seq, unsigned long *min_seq, | |
5855 | unsigned long seq) | |
5856 | { | |
5857 | int i; | |
5858 | int type, tier; | |
5859 | int hist = lru_hist_from_seq(seq); | |
391655fe | 5860 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
d6c3af7d YZ |
5861 | |
5862 | for (tier = 0; tier < MAX_NR_TIERS; tier++) { | |
5863 | seq_printf(m, " %10d", tier); | |
5864 | for (type = 0; type < ANON_AND_FILE; type++) { | |
5865 | const char *s = " "; | |
5866 | unsigned long n[3] = {}; | |
5867 | ||
5868 | if (seq == max_seq) { | |
5869 | s = "RT "; | |
5870 | n[0] = READ_ONCE(lrugen->avg_refaulted[type][tier]); | |
5871 | n[1] = READ_ONCE(lrugen->avg_total[type][tier]); | |
5872 | } else if (seq == min_seq[type] || NR_HIST_GENS > 1) { | |
5873 | s = "rep"; | |
5874 | n[0] = atomic_long_read(&lrugen->refaulted[hist][type][tier]); | |
5875 | n[1] = atomic_long_read(&lrugen->evicted[hist][type][tier]); | |
5876 | if (tier) | |
5877 | n[2] = READ_ONCE(lrugen->protected[hist][type][tier - 1]); | |
5878 | } | |
5879 | ||
5880 | for (i = 0; i < 3; i++) | |
5881 | seq_printf(m, " %10lu%c", n[i], s[i]); | |
5882 | } | |
5883 | seq_putc(m, '\n'); | |
5884 | } | |
5885 | ||
5886 | seq_puts(m, " "); | |
5887 | for (i = 0; i < NR_MM_STATS; i++) { | |
5888 | const char *s = " "; | |
5889 | unsigned long n = 0; | |
5890 | ||
5891 | if (seq == max_seq && NR_HIST_GENS == 1) { | |
5892 | s = "LOYNFA"; | |
5893 | n = READ_ONCE(lruvec->mm_state.stats[hist][i]); | |
5894 | } else if (seq != max_seq && NR_HIST_GENS > 1) { | |
5895 | s = "loynfa"; | |
5896 | n = READ_ONCE(lruvec->mm_state.stats[hist][i]); | |
5897 | } | |
5898 | ||
5899 | seq_printf(m, " %10lu%c", n, s[i]); | |
5900 | } | |
5901 | seq_putc(m, '\n'); | |
5902 | } | |
5903 | ||
07017acb | 5904 | /* see Documentation/admin-guide/mm/multigen_lru.rst for details */ |
d6c3af7d YZ |
5905 | static int lru_gen_seq_show(struct seq_file *m, void *v) |
5906 | { | |
5907 | unsigned long seq; | |
5908 | bool full = !debugfs_real_fops(m->file)->write; | |
5909 | struct lruvec *lruvec = v; | |
391655fe | 5910 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
d6c3af7d YZ |
5911 | int nid = lruvec_pgdat(lruvec)->node_id; |
5912 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
5913 | DEFINE_MAX_SEQ(lruvec); | |
5914 | DEFINE_MIN_SEQ(lruvec); | |
5915 | ||
5916 | if (nid == first_memory_node) { | |
5917 | const char *path = memcg ? m->private : ""; | |
5918 | ||
5919 | #ifdef CONFIG_MEMCG | |
5920 | if (memcg) | |
5921 | cgroup_path(memcg->css.cgroup, m->private, PATH_MAX); | |
5922 | #endif | |
5923 | seq_printf(m, "memcg %5hu %s\n", mem_cgroup_id(memcg), path); | |
5924 | } | |
5925 | ||
5926 | seq_printf(m, " node %5d\n", nid); | |
5927 | ||
5928 | if (!full) | |
5929 | seq = min_seq[LRU_GEN_ANON]; | |
5930 | else if (max_seq >= MAX_NR_GENS) | |
5931 | seq = max_seq - MAX_NR_GENS + 1; | |
5932 | else | |
5933 | seq = 0; | |
5934 | ||
5935 | for (; seq <= max_seq; seq++) { | |
5936 | int type, zone; | |
5937 | int gen = lru_gen_from_seq(seq); | |
5938 | unsigned long birth = READ_ONCE(lruvec->lrugen.timestamps[gen]); | |
5939 | ||
5940 | seq_printf(m, " %10lu %10u", seq, jiffies_to_msecs(jiffies - birth)); | |
5941 | ||
5942 | for (type = 0; type < ANON_AND_FILE; type++) { | |
5943 | unsigned long size = 0; | |
5944 | char mark = full && seq < min_seq[type] ? 'x' : ' '; | |
5945 | ||
5946 | for (zone = 0; zone < MAX_NR_ZONES; zone++) | |
5947 | size += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); | |
5948 | ||
5949 | seq_printf(m, " %10lu%c", size, mark); | |
5950 | } | |
5951 | ||
5952 | seq_putc(m, '\n'); | |
5953 | ||
5954 | if (full) | |
5955 | lru_gen_seq_show_full(m, lruvec, max_seq, min_seq, seq); | |
5956 | } | |
5957 | ||
5958 | return 0; | |
5959 | } | |
5960 | ||
5961 | static const struct seq_operations lru_gen_seq_ops = { | |
5962 | .start = lru_gen_seq_start, | |
5963 | .stop = lru_gen_seq_stop, | |
5964 | .next = lru_gen_seq_next, | |
5965 | .show = lru_gen_seq_show, | |
5966 | }; | |
5967 | ||
5968 | static int run_aging(struct lruvec *lruvec, unsigned long seq, struct scan_control *sc, | |
5969 | bool can_swap, bool force_scan) | |
5970 | { | |
5971 | DEFINE_MAX_SEQ(lruvec); | |
5972 | DEFINE_MIN_SEQ(lruvec); | |
5973 | ||
5974 | if (seq < max_seq) | |
5975 | return 0; | |
5976 | ||
5977 | if (seq > max_seq) | |
5978 | return -EINVAL; | |
5979 | ||
5980 | if (!force_scan && min_seq[!can_swap] + MAX_NR_GENS - 1 <= max_seq) | |
5981 | return -ERANGE; | |
5982 | ||
5983 | try_to_inc_max_seq(lruvec, max_seq, sc, can_swap, force_scan); | |
5984 | ||
5985 | return 0; | |
5986 | } | |
5987 | ||
5988 | static int run_eviction(struct lruvec *lruvec, unsigned long seq, struct scan_control *sc, | |
5989 | int swappiness, unsigned long nr_to_reclaim) | |
5990 | { | |
5991 | DEFINE_MAX_SEQ(lruvec); | |
5992 | ||
5993 | if (seq + MIN_NR_GENS > max_seq) | |
5994 | return -EINVAL; | |
5995 | ||
5996 | sc->nr_reclaimed = 0; | |
5997 | ||
5998 | while (!signal_pending(current)) { | |
5999 | DEFINE_MIN_SEQ(lruvec); | |
6000 | ||
6001 | if (seq < min_seq[!swappiness]) | |
6002 | return 0; | |
6003 | ||
6004 | if (sc->nr_reclaimed >= nr_to_reclaim) | |
6005 | return 0; | |
6006 | ||
a579086c | 6007 | if (!evict_folios(lruvec, sc, swappiness)) |
d6c3af7d YZ |
6008 | return 0; |
6009 | ||
6010 | cond_resched(); | |
6011 | } | |
6012 | ||
6013 | return -EINTR; | |
6014 | } | |
6015 | ||
6016 | static int run_cmd(char cmd, int memcg_id, int nid, unsigned long seq, | |
6017 | struct scan_control *sc, int swappiness, unsigned long opt) | |
6018 | { | |
6019 | struct lruvec *lruvec; | |
6020 | int err = -EINVAL; | |
6021 | struct mem_cgroup *memcg = NULL; | |
6022 | ||
6023 | if (nid < 0 || nid >= MAX_NUMNODES || !node_state(nid, N_MEMORY)) | |
6024 | return -EINVAL; | |
6025 | ||
6026 | if (!mem_cgroup_disabled()) { | |
6027 | rcu_read_lock(); | |
e4dde56c | 6028 | |
d6c3af7d | 6029 | memcg = mem_cgroup_from_id(memcg_id); |
e4dde56c | 6030 | if (!mem_cgroup_tryget(memcg)) |
d6c3af7d | 6031 | memcg = NULL; |
e4dde56c | 6032 | |
d6c3af7d YZ |
6033 | rcu_read_unlock(); |
6034 | ||
6035 | if (!memcg) | |
6036 | return -EINVAL; | |
6037 | } | |
6038 | ||
6039 | if (memcg_id != mem_cgroup_id(memcg)) | |
6040 | goto done; | |
6041 | ||
6042 | lruvec = get_lruvec(memcg, nid); | |
6043 | ||
6044 | if (swappiness < 0) | |
6045 | swappiness = get_swappiness(lruvec, sc); | |
6046 | else if (swappiness > 200) | |
6047 | goto done; | |
6048 | ||
6049 | switch (cmd) { | |
6050 | case '+': | |
6051 | err = run_aging(lruvec, seq, sc, swappiness, opt); | |
6052 | break; | |
6053 | case '-': | |
6054 | err = run_eviction(lruvec, seq, sc, swappiness, opt); | |
6055 | break; | |
6056 | } | |
6057 | done: | |
6058 | mem_cgroup_put(memcg); | |
6059 | ||
6060 | return err; | |
6061 | } | |
6062 | ||
07017acb | 6063 | /* see Documentation/admin-guide/mm/multigen_lru.rst for details */ |
d6c3af7d YZ |
6064 | static ssize_t lru_gen_seq_write(struct file *file, const char __user *src, |
6065 | size_t len, loff_t *pos) | |
6066 | { | |
6067 | void *buf; | |
6068 | char *cur, *next; | |
6069 | unsigned int flags; | |
6070 | struct blk_plug plug; | |
6071 | int err = -EINVAL; | |
6072 | struct scan_control sc = { | |
6073 | .may_writepage = true, | |
6074 | .may_unmap = true, | |
6075 | .may_swap = true, | |
6076 | .reclaim_idx = MAX_NR_ZONES - 1, | |
6077 | .gfp_mask = GFP_KERNEL, | |
6078 | }; | |
6079 | ||
6080 | buf = kvmalloc(len + 1, GFP_KERNEL); | |
6081 | if (!buf) | |
6082 | return -ENOMEM; | |
6083 | ||
6084 | if (copy_from_user(buf, src, len)) { | |
6085 | kvfree(buf); | |
6086 | return -EFAULT; | |
6087 | } | |
6088 | ||
6089 | set_task_reclaim_state(current, &sc.reclaim_state); | |
6090 | flags = memalloc_noreclaim_save(); | |
6091 | blk_start_plug(&plug); | |
e9d4e1ee | 6092 | if (!set_mm_walk(NULL, true)) { |
d6c3af7d YZ |
6093 | err = -ENOMEM; |
6094 | goto done; | |
6095 | } | |
6096 | ||
6097 | next = buf; | |
6098 | next[len] = '\0'; | |
6099 | ||
6100 | while ((cur = strsep(&next, ",;\n"))) { | |
6101 | int n; | |
6102 | int end; | |
6103 | char cmd; | |
6104 | unsigned int memcg_id; | |
6105 | unsigned int nid; | |
6106 | unsigned long seq; | |
6107 | unsigned int swappiness = -1; | |
6108 | unsigned long opt = -1; | |
6109 | ||
6110 | cur = skip_spaces(cur); | |
6111 | if (!*cur) | |
6112 | continue; | |
6113 | ||
6114 | n = sscanf(cur, "%c %u %u %lu %n %u %n %lu %n", &cmd, &memcg_id, &nid, | |
6115 | &seq, &end, &swappiness, &end, &opt, &end); | |
6116 | if (n < 4 || cur[end]) { | |
6117 | err = -EINVAL; | |
6118 | break; | |
6119 | } | |
6120 | ||
6121 | err = run_cmd(cmd, memcg_id, nid, seq, &sc, swappiness, opt); | |
6122 | if (err) | |
6123 | break; | |
6124 | } | |
6125 | done: | |
6126 | clear_mm_walk(); | |
6127 | blk_finish_plug(&plug); | |
6128 | memalloc_noreclaim_restore(flags); | |
6129 | set_task_reclaim_state(current, NULL); | |
6130 | ||
6131 | kvfree(buf); | |
6132 | ||
6133 | return err ? : len; | |
6134 | } | |
6135 | ||
6136 | static int lru_gen_seq_open(struct inode *inode, struct file *file) | |
6137 | { | |
6138 | return seq_open(file, &lru_gen_seq_ops); | |
6139 | } | |
6140 | ||
6141 | static const struct file_operations lru_gen_rw_fops = { | |
6142 | .open = lru_gen_seq_open, | |
6143 | .read = seq_read, | |
6144 | .write = lru_gen_seq_write, | |
6145 | .llseek = seq_lseek, | |
6146 | .release = seq_release, | |
6147 | }; | |
6148 | ||
6149 | static const struct file_operations lru_gen_ro_fops = { | |
6150 | .open = lru_gen_seq_open, | |
6151 | .read = seq_read, | |
6152 | .llseek = seq_lseek, | |
6153 | .release = seq_release, | |
6154 | }; | |
6155 | ||
ec1c86b2 YZ |
6156 | /****************************************************************************** |
6157 | * initialization | |
6158 | ******************************************************************************/ | |
6159 | ||
6160 | void lru_gen_init_lruvec(struct lruvec *lruvec) | |
6161 | { | |
1332a809 | 6162 | int i; |
ec1c86b2 | 6163 | int gen, type, zone; |
391655fe | 6164 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ec1c86b2 YZ |
6165 | |
6166 | lrugen->max_seq = MIN_NR_GENS + 1; | |
354ed597 | 6167 | lrugen->enabled = lru_gen_enabled(); |
ec1c86b2 | 6168 | |
1332a809 YZ |
6169 | for (i = 0; i <= MIN_NR_GENS + 1; i++) |
6170 | lrugen->timestamps[i] = jiffies; | |
6171 | ||
ec1c86b2 | 6172 | for_each_gen_type_zone(gen, type, zone) |
6df1b221 | 6173 | INIT_LIST_HEAD(&lrugen->folios[gen][type][zone]); |
bd74fdae YZ |
6174 | |
6175 | lruvec->mm_state.seq = MIN_NR_GENS; | |
6176 | init_waitqueue_head(&lruvec->mm_state.wait); | |
ec1c86b2 YZ |
6177 | } |
6178 | ||
6179 | #ifdef CONFIG_MEMCG | |
e4dde56c YZ |
6180 | |
6181 | void lru_gen_init_pgdat(struct pglist_data *pgdat) | |
6182 | { | |
6183 | int i, j; | |
6184 | ||
6185 | spin_lock_init(&pgdat->memcg_lru.lock); | |
6186 | ||
6187 | for (i = 0; i < MEMCG_NR_GENS; i++) { | |
6188 | for (j = 0; j < MEMCG_NR_BINS; j++) | |
6189 | INIT_HLIST_NULLS_HEAD(&pgdat->memcg_lru.fifo[i][j], i); | |
6190 | } | |
6191 | } | |
6192 | ||
ec1c86b2 YZ |
6193 | void lru_gen_init_memcg(struct mem_cgroup *memcg) |
6194 | { | |
bd74fdae YZ |
6195 | INIT_LIST_HEAD(&memcg->mm_list.fifo); |
6196 | spin_lock_init(&memcg->mm_list.lock); | |
ec1c86b2 YZ |
6197 | } |
6198 | ||
6199 | void lru_gen_exit_memcg(struct mem_cgroup *memcg) | |
6200 | { | |
bd74fdae | 6201 | int i; |
ec1c86b2 YZ |
6202 | int nid; |
6203 | ||
37cc9997 A |
6204 | VM_WARN_ON_ONCE(!list_empty(&memcg->mm_list.fifo)); |
6205 | ||
ec1c86b2 YZ |
6206 | for_each_node(nid) { |
6207 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
6208 | ||
37cc9997 | 6209 | VM_WARN_ON_ONCE(lruvec->mm_state.nr_walkers); |
ec1c86b2 YZ |
6210 | VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0, |
6211 | sizeof(lruvec->lrugen.nr_pages))); | |
bd74fdae | 6212 | |
37cc9997 A |
6213 | lruvec->lrugen.list.next = LIST_POISON1; |
6214 | ||
bd74fdae YZ |
6215 | for (i = 0; i < NR_BLOOM_FILTERS; i++) { |
6216 | bitmap_free(lruvec->mm_state.filters[i]); | |
6217 | lruvec->mm_state.filters[i] = NULL; | |
6218 | } | |
ec1c86b2 YZ |
6219 | } |
6220 | } | |
e4dde56c | 6221 | |
e4dde56c | 6222 | #endif /* CONFIG_MEMCG */ |
ec1c86b2 YZ |
6223 | |
6224 | static int __init init_lru_gen(void) | |
6225 | { | |
6226 | BUILD_BUG_ON(MIN_NR_GENS + 1 >= MAX_NR_GENS); | |
6227 | BUILD_BUG_ON(BIT(LRU_GEN_WIDTH) <= MAX_NR_GENS); | |
6228 | ||
354ed597 YZ |
6229 | if (sysfs_create_group(mm_kobj, &lru_gen_attr_group)) |
6230 | pr_err("lru_gen: failed to create sysfs group\n"); | |
6231 | ||
d6c3af7d YZ |
6232 | debugfs_create_file("lru_gen", 0644, NULL, NULL, &lru_gen_rw_fops); |
6233 | debugfs_create_file("lru_gen_full", 0444, NULL, NULL, &lru_gen_ro_fops); | |
6234 | ||
ec1c86b2 YZ |
6235 | return 0; |
6236 | }; | |
6237 | late_initcall(init_lru_gen); | |
6238 | ||
ac35a490 YZ |
6239 | #else /* !CONFIG_LRU_GEN */ |
6240 | ||
6241 | static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc) | |
6242 | { | |
6243 | } | |
6244 | ||
6245 | static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) | |
6246 | { | |
6247 | } | |
6248 | ||
e4dde56c YZ |
6249 | static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *sc) |
6250 | { | |
6251 | } | |
6252 | ||
ec1c86b2 YZ |
6253 | #endif /* CONFIG_LRU_GEN */ |
6254 | ||
afaf07a6 | 6255 | static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) |
9b4f98cd JW |
6256 | { |
6257 | unsigned long nr[NR_LRU_LISTS]; | |
e82e0561 | 6258 | unsigned long targets[NR_LRU_LISTS]; |
9b4f98cd JW |
6259 | unsigned long nr_to_scan; |
6260 | enum lru_list lru; | |
6261 | unsigned long nr_reclaimed = 0; | |
6262 | unsigned long nr_to_reclaim = sc->nr_to_reclaim; | |
f53af428 | 6263 | bool proportional_reclaim; |
9b4f98cd JW |
6264 | struct blk_plug plug; |
6265 | ||
e4dde56c | 6266 | if (lru_gen_enabled() && !global_reclaim(sc)) { |
ac35a490 YZ |
6267 | lru_gen_shrink_lruvec(lruvec, sc); |
6268 | return; | |
6269 | } | |
6270 | ||
afaf07a6 | 6271 | get_scan_count(lruvec, sc, nr); |
9b4f98cd | 6272 | |
e82e0561 MG |
6273 | /* Record the original scan target for proportional adjustments later */ |
6274 | memcpy(targets, nr, sizeof(nr)); | |
6275 | ||
1a501907 MG |
6276 | /* |
6277 | * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal | |
6278 | * event that can occur when there is little memory pressure e.g. | |
6279 | * multiple streaming readers/writers. Hence, we do not abort scanning | |
6280 | * when the requested number of pages are reclaimed when scanning at | |
6281 | * DEF_PRIORITY on the assumption that the fact we are direct | |
6282 | * reclaiming implies that kswapd is not keeping up and it is best to | |
6283 | * do a batch of work at once. For memcg reclaim one check is made to | |
6284 | * abort proportional reclaim if either the file or anon lru has already | |
6285 | * dropped to zero at the first pass. | |
6286 | */ | |
f53af428 JW |
6287 | proportional_reclaim = (!cgroup_reclaim(sc) && !current_is_kswapd() && |
6288 | sc->priority == DEF_PRIORITY); | |
1a501907 | 6289 | |
9b4f98cd JW |
6290 | blk_start_plug(&plug); |
6291 | while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || | |
6292 | nr[LRU_INACTIVE_FILE]) { | |
e82e0561 MG |
6293 | unsigned long nr_anon, nr_file, percentage; |
6294 | unsigned long nr_scanned; | |
6295 | ||
9b4f98cd JW |
6296 | for_each_evictable_lru(lru) { |
6297 | if (nr[lru]) { | |
6298 | nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); | |
6299 | nr[lru] -= nr_to_scan; | |
6300 | ||
6301 | nr_reclaimed += shrink_list(lru, nr_to_scan, | |
3b991208 | 6302 | lruvec, sc); |
9b4f98cd JW |
6303 | } |
6304 | } | |
e82e0561 | 6305 | |
bd041733 MH |
6306 | cond_resched(); |
6307 | ||
f53af428 | 6308 | if (nr_reclaimed < nr_to_reclaim || proportional_reclaim) |
e82e0561 MG |
6309 | continue; |
6310 | ||
e82e0561 MG |
6311 | /* |
6312 | * For kswapd and memcg, reclaim at least the number of pages | |
1a501907 | 6313 | * requested. Ensure that the anon and file LRUs are scanned |
e82e0561 MG |
6314 | * proportionally what was requested by get_scan_count(). We |
6315 | * stop reclaiming one LRU and reduce the amount scanning | |
6316 | * proportional to the original scan target. | |
6317 | */ | |
6318 | nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; | |
6319 | nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; | |
6320 | ||
1a501907 MG |
6321 | /* |
6322 | * It's just vindictive to attack the larger once the smaller | |
6323 | * has gone to zero. And given the way we stop scanning the | |
6324 | * smaller below, this makes sure that we only make one nudge | |
6325 | * towards proportionality once we've got nr_to_reclaim. | |
6326 | */ | |
6327 | if (!nr_file || !nr_anon) | |
6328 | break; | |
6329 | ||
e82e0561 MG |
6330 | if (nr_file > nr_anon) { |
6331 | unsigned long scan_target = targets[LRU_INACTIVE_ANON] + | |
6332 | targets[LRU_ACTIVE_ANON] + 1; | |
6333 | lru = LRU_BASE; | |
6334 | percentage = nr_anon * 100 / scan_target; | |
6335 | } else { | |
6336 | unsigned long scan_target = targets[LRU_INACTIVE_FILE] + | |
6337 | targets[LRU_ACTIVE_FILE] + 1; | |
6338 | lru = LRU_FILE; | |
6339 | percentage = nr_file * 100 / scan_target; | |
6340 | } | |
6341 | ||
6342 | /* Stop scanning the smaller of the LRU */ | |
6343 | nr[lru] = 0; | |
6344 | nr[lru + LRU_ACTIVE] = 0; | |
6345 | ||
6346 | /* | |
6347 | * Recalculate the other LRU scan count based on its original | |
6348 | * scan target and the percentage scanning already complete | |
6349 | */ | |
6350 | lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; | |
6351 | nr_scanned = targets[lru] - nr[lru]; | |
6352 | nr[lru] = targets[lru] * (100 - percentage) / 100; | |
6353 | nr[lru] -= min(nr[lru], nr_scanned); | |
6354 | ||
6355 | lru += LRU_ACTIVE; | |
6356 | nr_scanned = targets[lru] - nr[lru]; | |
6357 | nr[lru] = targets[lru] * (100 - percentage) / 100; | |
6358 | nr[lru] -= min(nr[lru], nr_scanned); | |
9b4f98cd JW |
6359 | } |
6360 | blk_finish_plug(&plug); | |
6361 | sc->nr_reclaimed += nr_reclaimed; | |
6362 | ||
6363 | /* | |
6364 | * Even if we did not try to evict anon pages at all, we want to | |
6365 | * rebalance the anon lru active/inactive ratio. | |
6366 | */ | |
2f368a9f DH |
6367 | if (can_age_anon_pages(lruvec_pgdat(lruvec), sc) && |
6368 | inactive_is_low(lruvec, LRU_INACTIVE_ANON)) | |
9b4f98cd JW |
6369 | shrink_active_list(SWAP_CLUSTER_MAX, lruvec, |
6370 | sc, LRU_ACTIVE_ANON); | |
9b4f98cd JW |
6371 | } |
6372 | ||
23b9da55 | 6373 | /* Use reclaim/compaction for costly allocs or under memory pressure */ |
9e3b2f8c | 6374 | static bool in_reclaim_compaction(struct scan_control *sc) |
23b9da55 | 6375 | { |
d84da3f9 | 6376 | if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && |
23b9da55 | 6377 | (sc->order > PAGE_ALLOC_COSTLY_ORDER || |
9e3b2f8c | 6378 | sc->priority < DEF_PRIORITY - 2)) |
23b9da55 MG |
6379 | return true; |
6380 | ||
6381 | return false; | |
6382 | } | |
6383 | ||
3e7d3449 | 6384 | /* |
23b9da55 MG |
6385 | * Reclaim/compaction is used for high-order allocation requests. It reclaims |
6386 | * order-0 pages before compacting the zone. should_continue_reclaim() returns | |
6387 | * true if more pages should be reclaimed such that when the page allocator | |
df3a45f9 | 6388 | * calls try_to_compact_pages() that it will have enough free pages to succeed. |
23b9da55 | 6389 | * It will give up earlier than that if there is difficulty reclaiming pages. |
3e7d3449 | 6390 | */ |
a9dd0a83 | 6391 | static inline bool should_continue_reclaim(struct pglist_data *pgdat, |
3e7d3449 | 6392 | unsigned long nr_reclaimed, |
3e7d3449 MG |
6393 | struct scan_control *sc) |
6394 | { | |
6395 | unsigned long pages_for_compaction; | |
6396 | unsigned long inactive_lru_pages; | |
a9dd0a83 | 6397 | int z; |
3e7d3449 MG |
6398 | |
6399 | /* If not in reclaim/compaction mode, stop */ | |
9e3b2f8c | 6400 | if (!in_reclaim_compaction(sc)) |
3e7d3449 MG |
6401 | return false; |
6402 | ||
5ee04716 VB |
6403 | /* |
6404 | * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX | |
6405 | * number of pages that were scanned. This will return to the caller | |
6406 | * with the risk reclaim/compaction and the resulting allocation attempt | |
6407 | * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL | |
6408 | * allocations through requiring that the full LRU list has been scanned | |
6409 | * first, by assuming that zero delta of sc->nr_scanned means full LRU | |
6410 | * scan, but that approximation was wrong, and there were corner cases | |
6411 | * where always a non-zero amount of pages were scanned. | |
6412 | */ | |
6413 | if (!nr_reclaimed) | |
6414 | return false; | |
3e7d3449 | 6415 | |
3e7d3449 | 6416 | /* If compaction would go ahead or the allocation would succeed, stop */ |
a9dd0a83 MG |
6417 | for (z = 0; z <= sc->reclaim_idx; z++) { |
6418 | struct zone *zone = &pgdat->node_zones[z]; | |
6aa303de | 6419 | if (!managed_zone(zone)) |
a9dd0a83 MG |
6420 | continue; |
6421 | ||
6422 | switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { | |
cf378319 | 6423 | case COMPACT_SUCCESS: |
a9dd0a83 MG |
6424 | case COMPACT_CONTINUE: |
6425 | return false; | |
6426 | default: | |
6427 | /* check next zone */ | |
6428 | ; | |
6429 | } | |
3e7d3449 | 6430 | } |
1c6c1597 HD |
6431 | |
6432 | /* | |
6433 | * If we have not reclaimed enough pages for compaction and the | |
6434 | * inactive lists are large enough, continue reclaiming | |
6435 | */ | |
6436 | pages_for_compaction = compact_gap(sc->order); | |
6437 | inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); | |
a2a36488 | 6438 | if (can_reclaim_anon_pages(NULL, pgdat->node_id, sc)) |
1c6c1597 HD |
6439 | inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); |
6440 | ||
5ee04716 | 6441 | return inactive_lru_pages > pages_for_compaction; |
3e7d3449 MG |
6442 | } |
6443 | ||
0f6a5cff | 6444 | static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) |
1da177e4 | 6445 | { |
0f6a5cff | 6446 | struct mem_cgroup *target_memcg = sc->target_mem_cgroup; |
d2af3397 | 6447 | struct mem_cgroup *memcg; |
1da177e4 | 6448 | |
0f6a5cff | 6449 | memcg = mem_cgroup_iter(target_memcg, NULL, NULL); |
d2af3397 | 6450 | do { |
afaf07a6 | 6451 | struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); |
d2af3397 JW |
6452 | unsigned long reclaimed; |
6453 | unsigned long scanned; | |
5660048c | 6454 | |
e3336cab XP |
6455 | /* |
6456 | * This loop can become CPU-bound when target memcgs | |
6457 | * aren't eligible for reclaim - either because they | |
6458 | * don't have any reclaimable pages, or because their | |
6459 | * memory is explicitly protected. Avoid soft lockups. | |
6460 | */ | |
6461 | cond_resched(); | |
6462 | ||
45c7f7e1 CD |
6463 | mem_cgroup_calculate_protection(target_memcg, memcg); |
6464 | ||
adb82130 | 6465 | if (mem_cgroup_below_min(target_memcg, memcg)) { |
d2af3397 JW |
6466 | /* |
6467 | * Hard protection. | |
6468 | * If there is no reclaimable memory, OOM. | |
6469 | */ | |
6470 | continue; | |
adb82130 | 6471 | } else if (mem_cgroup_below_low(target_memcg, memcg)) { |
d2af3397 JW |
6472 | /* |
6473 | * Soft protection. | |
6474 | * Respect the protection only as long as | |
6475 | * there is an unprotected supply | |
6476 | * of reclaimable memory from other cgroups. | |
6477 | */ | |
6478 | if (!sc->memcg_low_reclaim) { | |
6479 | sc->memcg_low_skipped = 1; | |
bf8d5d52 | 6480 | continue; |
241994ed | 6481 | } |
d2af3397 | 6482 | memcg_memory_event(memcg, MEMCG_LOW); |
d2af3397 | 6483 | } |
241994ed | 6484 | |
d2af3397 JW |
6485 | reclaimed = sc->nr_reclaimed; |
6486 | scanned = sc->nr_scanned; | |
afaf07a6 JW |
6487 | |
6488 | shrink_lruvec(lruvec, sc); | |
70ddf637 | 6489 | |
d2af3397 JW |
6490 | shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, |
6491 | sc->priority); | |
6b4f7799 | 6492 | |
d2af3397 | 6493 | /* Record the group's reclaim efficiency */ |
73b73bac YA |
6494 | if (!sc->proactive) |
6495 | vmpressure(sc->gfp_mask, memcg, false, | |
6496 | sc->nr_scanned - scanned, | |
6497 | sc->nr_reclaimed - reclaimed); | |
70ddf637 | 6498 | |
0f6a5cff JW |
6499 | } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL))); |
6500 | } | |
6501 | ||
6c9e0907 | 6502 | static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) |
0f6a5cff | 6503 | { |
54c4fe08 | 6504 | unsigned long nr_reclaimed, nr_scanned, nr_node_reclaimed; |
1b05117d | 6505 | struct lruvec *target_lruvec; |
0f6a5cff JW |
6506 | bool reclaimable = false; |
6507 | ||
e4dde56c YZ |
6508 | if (lru_gen_enabled() && global_reclaim(sc)) { |
6509 | lru_gen_shrink_node(pgdat, sc); | |
6510 | return; | |
6511 | } | |
6512 | ||
1b05117d JW |
6513 | target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); |
6514 | ||
0f6a5cff JW |
6515 | again: |
6516 | memset(&sc->nr, 0, sizeof(sc->nr)); | |
6517 | ||
6518 | nr_reclaimed = sc->nr_reclaimed; | |
6519 | nr_scanned = sc->nr_scanned; | |
6520 | ||
f1e1a7be | 6521 | prepare_scan_count(pgdat, sc); |
53138cea | 6522 | |
0f6a5cff | 6523 | shrink_node_memcgs(pgdat, sc); |
2344d7e4 | 6524 | |
583c27a1 | 6525 | flush_reclaim_state(sc); |
d108c772 | 6526 | |
54c4fe08 HX |
6527 | nr_node_reclaimed = sc->nr_reclaimed - nr_reclaimed; |
6528 | ||
d2af3397 | 6529 | /* Record the subtree's reclaim efficiency */ |
73b73bac YA |
6530 | if (!sc->proactive) |
6531 | vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, | |
54c4fe08 | 6532 | sc->nr_scanned - nr_scanned, nr_node_reclaimed); |
d108c772 | 6533 | |
54c4fe08 | 6534 | if (nr_node_reclaimed) |
d2af3397 | 6535 | reclaimable = true; |
d108c772 | 6536 | |
d2af3397 JW |
6537 | if (current_is_kswapd()) { |
6538 | /* | |
6539 | * If reclaim is isolating dirty pages under writeback, | |
6540 | * it implies that the long-lived page allocation rate | |
6541 | * is exceeding the page laundering rate. Either the | |
6542 | * global limits are not being effective at throttling | |
6543 | * processes due to the page distribution throughout | |
6544 | * zones or there is heavy usage of a slow backing | |
6545 | * device. The only option is to throttle from reclaim | |
6546 | * context which is not ideal as there is no guarantee | |
6547 | * the dirtying process is throttled in the same way | |
6548 | * balance_dirty_pages() manages. | |
6549 | * | |
6550 | * Once a node is flagged PGDAT_WRITEBACK, kswapd will | |
6551 | * count the number of pages under pages flagged for | |
6552 | * immediate reclaim and stall if any are encountered | |
6553 | * in the nr_immediate check below. | |
6554 | */ | |
6555 | if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) | |
6556 | set_bit(PGDAT_WRITEBACK, &pgdat->flags); | |
d108c772 | 6557 | |
d2af3397 JW |
6558 | /* Allow kswapd to start writing pages during reclaim.*/ |
6559 | if (sc->nr.unqueued_dirty == sc->nr.file_taken) | |
6560 | set_bit(PGDAT_DIRTY, &pgdat->flags); | |
e3c1ac58 | 6561 | |
d108c772 | 6562 | /* |
1eba09c1 | 6563 | * If kswapd scans pages marked for immediate |
d2af3397 JW |
6564 | * reclaim and under writeback (nr_immediate), it |
6565 | * implies that pages are cycling through the LRU | |
8cd7c588 MG |
6566 | * faster than they are written so forcibly stall |
6567 | * until some pages complete writeback. | |
d108c772 | 6568 | */ |
d2af3397 | 6569 | if (sc->nr.immediate) |
c3f4a9a2 | 6570 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); |
d2af3397 JW |
6571 | } |
6572 | ||
6573 | /* | |
8cd7c588 MG |
6574 | * Tag a node/memcg as congested if all the dirty pages were marked |
6575 | * for writeback and immediate reclaim (counted in nr.congested). | |
1b05117d | 6576 | * |
d2af3397 | 6577 | * Legacy memcg will stall in page writeback so avoid forcibly |
8cd7c588 | 6578 | * stalling in reclaim_throttle(). |
d2af3397 | 6579 | */ |
1b05117d JW |
6580 | if ((current_is_kswapd() || |
6581 | (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) && | |
d2af3397 | 6582 | sc->nr.dirty && sc->nr.dirty == sc->nr.congested) |
1b05117d | 6583 | set_bit(LRUVEC_CONGESTED, &target_lruvec->flags); |
d2af3397 JW |
6584 | |
6585 | /* | |
8cd7c588 MG |
6586 | * Stall direct reclaim for IO completions if the lruvec is |
6587 | * node is congested. Allow kswapd to continue until it | |
d2af3397 JW |
6588 | * starts encountering unqueued dirty pages or cycling through |
6589 | * the LRU too quickly. | |
6590 | */ | |
1b05117d JW |
6591 | if (!current_is_kswapd() && current_may_throttle() && |
6592 | !sc->hibernation_mode && | |
6593 | test_bit(LRUVEC_CONGESTED, &target_lruvec->flags)) | |
1b4e3f26 | 6594 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_CONGESTED); |
d108c772 | 6595 | |
54c4fe08 | 6596 | if (should_continue_reclaim(pgdat, nr_node_reclaimed, sc)) |
d2af3397 | 6597 | goto again; |
2344d7e4 | 6598 | |
c73322d0 JW |
6599 | /* |
6600 | * Kswapd gives up on balancing particular nodes after too | |
6601 | * many failures to reclaim anything from them and goes to | |
6602 | * sleep. On reclaim progress, reset the failure counter. A | |
6603 | * successful direct reclaim run will revive a dormant kswapd. | |
6604 | */ | |
6605 | if (reclaimable) | |
6606 | pgdat->kswapd_failures = 0; | |
f16015fb JW |
6607 | } |
6608 | ||
53853e2d | 6609 | /* |
fdd4c614 VB |
6610 | * Returns true if compaction should go ahead for a costly-order request, or |
6611 | * the allocation would already succeed without compaction. Return false if we | |
6612 | * should reclaim first. | |
53853e2d | 6613 | */ |
4f588331 | 6614 | static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) |
fe4b1b24 | 6615 | { |
31483b6a | 6616 | unsigned long watermark; |
fdd4c614 | 6617 | enum compact_result suitable; |
fe4b1b24 | 6618 | |
fdd4c614 VB |
6619 | suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); |
6620 | if (suitable == COMPACT_SUCCESS) | |
6621 | /* Allocation should succeed already. Don't reclaim. */ | |
6622 | return true; | |
6623 | if (suitable == COMPACT_SKIPPED) | |
6624 | /* Compaction cannot yet proceed. Do reclaim. */ | |
6625 | return false; | |
fe4b1b24 | 6626 | |
53853e2d | 6627 | /* |
fdd4c614 VB |
6628 | * Compaction is already possible, but it takes time to run and there |
6629 | * are potentially other callers using the pages just freed. So proceed | |
6630 | * with reclaim to make a buffer of free pages available to give | |
6631 | * compaction a reasonable chance of completing and allocating the page. | |
6632 | * Note that we won't actually reclaim the whole buffer in one attempt | |
6633 | * as the target watermark in should_continue_reclaim() is lower. But if | |
6634 | * we are already above the high+gap watermark, don't reclaim at all. | |
53853e2d | 6635 | */ |
fdd4c614 | 6636 | watermark = high_wmark_pages(zone) + compact_gap(sc->order); |
fe4b1b24 | 6637 | |
fdd4c614 | 6638 | return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); |
fe4b1b24 MG |
6639 | } |
6640 | ||
69392a40 MG |
6641 | static void consider_reclaim_throttle(pg_data_t *pgdat, struct scan_control *sc) |
6642 | { | |
66ce520b MG |
6643 | /* |
6644 | * If reclaim is making progress greater than 12% efficiency then | |
6645 | * wake all the NOPROGRESS throttled tasks. | |
6646 | */ | |
6647 | if (sc->nr_reclaimed > (sc->nr_scanned >> 3)) { | |
69392a40 MG |
6648 | wait_queue_head_t *wqh; |
6649 | ||
6650 | wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_NOPROGRESS]; | |
6651 | if (waitqueue_active(wqh)) | |
6652 | wake_up(wqh); | |
6653 | ||
6654 | return; | |
6655 | } | |
6656 | ||
6657 | /* | |
1b4e3f26 MG |
6658 | * Do not throttle kswapd or cgroup reclaim on NOPROGRESS as it will |
6659 | * throttle on VMSCAN_THROTTLE_WRITEBACK if there are too many pages | |
6660 | * under writeback and marked for immediate reclaim at the tail of the | |
6661 | * LRU. | |
69392a40 | 6662 | */ |
1b4e3f26 | 6663 | if (current_is_kswapd() || cgroup_reclaim(sc)) |
69392a40 MG |
6664 | return; |
6665 | ||
6666 | /* Throttle if making no progress at high prioities. */ | |
1b4e3f26 | 6667 | if (sc->priority == 1 && !sc->nr_reclaimed) |
c3f4a9a2 | 6668 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_NOPROGRESS); |
69392a40 MG |
6669 | } |
6670 | ||
1da177e4 LT |
6671 | /* |
6672 | * This is the direct reclaim path, for page-allocating processes. We only | |
6673 | * try to reclaim pages from zones which will satisfy the caller's allocation | |
6674 | * request. | |
6675 | * | |
1da177e4 LT |
6676 | * If a zone is deemed to be full of pinned pages then just give it a light |
6677 | * scan then give up on it. | |
6678 | */ | |
0a0337e0 | 6679 | static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) |
1da177e4 | 6680 | { |
dd1a239f | 6681 | struct zoneref *z; |
54a6eb5c | 6682 | struct zone *zone; |
0608f43d AM |
6683 | unsigned long nr_soft_reclaimed; |
6684 | unsigned long nr_soft_scanned; | |
619d0d76 | 6685 | gfp_t orig_mask; |
79dafcdc | 6686 | pg_data_t *last_pgdat = NULL; |
1b4e3f26 | 6687 | pg_data_t *first_pgdat = NULL; |
1cfb419b | 6688 | |
cc715d99 MG |
6689 | /* |
6690 | * If the number of buffer_heads in the machine exceeds the maximum | |
6691 | * allowed level, force direct reclaim to scan the highmem zone as | |
6692 | * highmem pages could be pinning lowmem pages storing buffer_heads | |
6693 | */ | |
619d0d76 | 6694 | orig_mask = sc->gfp_mask; |
b2e18757 | 6695 | if (buffer_heads_over_limit) { |
cc715d99 | 6696 | sc->gfp_mask |= __GFP_HIGHMEM; |
4f588331 | 6697 | sc->reclaim_idx = gfp_zone(sc->gfp_mask); |
b2e18757 | 6698 | } |
cc715d99 | 6699 | |
d4debc66 | 6700 | for_each_zone_zonelist_nodemask(zone, z, zonelist, |
b2e18757 | 6701 | sc->reclaim_idx, sc->nodemask) { |
1cfb419b KH |
6702 | /* |
6703 | * Take care memory controller reclaiming has small influence | |
6704 | * to global LRU. | |
6705 | */ | |
b5ead35e | 6706 | if (!cgroup_reclaim(sc)) { |
344736f2 VD |
6707 | if (!cpuset_zone_allowed(zone, |
6708 | GFP_KERNEL | __GFP_HARDWALL)) | |
1cfb419b | 6709 | continue; |
65ec02cb | 6710 | |
0b06496a JW |
6711 | /* |
6712 | * If we already have plenty of memory free for | |
6713 | * compaction in this zone, don't free any more. | |
6714 | * Even though compaction is invoked for any | |
6715 | * non-zero order, only frequent costly order | |
6716 | * reclamation is disruptive enough to become a | |
6717 | * noticeable problem, like transparent huge | |
6718 | * page allocations. | |
6719 | */ | |
6720 | if (IS_ENABLED(CONFIG_COMPACTION) && | |
6721 | sc->order > PAGE_ALLOC_COSTLY_ORDER && | |
4f588331 | 6722 | compaction_ready(zone, sc)) { |
0b06496a JW |
6723 | sc->compaction_ready = true; |
6724 | continue; | |
e0887c19 | 6725 | } |
0b06496a | 6726 | |
79dafcdc MG |
6727 | /* |
6728 | * Shrink each node in the zonelist once. If the | |
6729 | * zonelist is ordered by zone (not the default) then a | |
6730 | * node may be shrunk multiple times but in that case | |
6731 | * the user prefers lower zones being preserved. | |
6732 | */ | |
6733 | if (zone->zone_pgdat == last_pgdat) | |
6734 | continue; | |
6735 | ||
0608f43d AM |
6736 | /* |
6737 | * This steals pages from memory cgroups over softlimit | |
6738 | * and returns the number of reclaimed pages and | |
6739 | * scanned pages. This works for global memory pressure | |
6740 | * and balancing, not for a memcg's limit. | |
6741 | */ | |
6742 | nr_soft_scanned = 0; | |
ef8f2327 | 6743 | nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, |
0608f43d AM |
6744 | sc->order, sc->gfp_mask, |
6745 | &nr_soft_scanned); | |
6746 | sc->nr_reclaimed += nr_soft_reclaimed; | |
6747 | sc->nr_scanned += nr_soft_scanned; | |
ac34a1a3 | 6748 | /* need some check for avoid more shrink_zone() */ |
1cfb419b | 6749 | } |
408d8544 | 6750 | |
1b4e3f26 MG |
6751 | if (!first_pgdat) |
6752 | first_pgdat = zone->zone_pgdat; | |
6753 | ||
79dafcdc MG |
6754 | /* See comment about same check for global reclaim above */ |
6755 | if (zone->zone_pgdat == last_pgdat) | |
6756 | continue; | |
6757 | last_pgdat = zone->zone_pgdat; | |
970a39a3 | 6758 | shrink_node(zone->zone_pgdat, sc); |
1da177e4 | 6759 | } |
e0c23279 | 6760 | |
80082938 MG |
6761 | if (first_pgdat) |
6762 | consider_reclaim_throttle(first_pgdat, sc); | |
1b4e3f26 | 6763 | |
619d0d76 WY |
6764 | /* |
6765 | * Restore to original mask to avoid the impact on the caller if we | |
6766 | * promoted it to __GFP_HIGHMEM. | |
6767 | */ | |
6768 | sc->gfp_mask = orig_mask; | |
1da177e4 | 6769 | } |
4f98a2fe | 6770 | |
b910718a | 6771 | static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat) |
2a2e4885 | 6772 | { |
b910718a JW |
6773 | struct lruvec *target_lruvec; |
6774 | unsigned long refaults; | |
2a2e4885 | 6775 | |
ac35a490 YZ |
6776 | if (lru_gen_enabled()) |
6777 | return; | |
6778 | ||
b910718a | 6779 | target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat); |
170b04b7 | 6780 | refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON); |
e9c2dbc8 | 6781 | target_lruvec->refaults[WORKINGSET_ANON] = refaults; |
170b04b7 | 6782 | refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE); |
e9c2dbc8 | 6783 | target_lruvec->refaults[WORKINGSET_FILE] = refaults; |
2a2e4885 JW |
6784 | } |
6785 | ||
1da177e4 LT |
6786 | /* |
6787 | * This is the main entry point to direct page reclaim. | |
6788 | * | |
6789 | * If a full scan of the inactive list fails to free enough memory then we | |
6790 | * are "out of memory" and something needs to be killed. | |
6791 | * | |
6792 | * If the caller is !__GFP_FS then the probability of a failure is reasonably | |
6793 | * high - the zone may be full of dirty or under-writeback pages, which this | |
5b0830cb JA |
6794 | * caller can't do much about. We kick the writeback threads and take explicit |
6795 | * naps in the hope that some of these pages can be written. But if the | |
6796 | * allocating task holds filesystem locks which prevent writeout this might not | |
6797 | * work, and the allocation attempt will fail. | |
a41f24ea NA |
6798 | * |
6799 | * returns: 0, if no pages reclaimed | |
6800 | * else, the number of pages reclaimed | |
1da177e4 | 6801 | */ |
dac1d27b | 6802 | static unsigned long do_try_to_free_pages(struct zonelist *zonelist, |
3115cd91 | 6803 | struct scan_control *sc) |
1da177e4 | 6804 | { |
241994ed | 6805 | int initial_priority = sc->priority; |
2a2e4885 JW |
6806 | pg_data_t *last_pgdat; |
6807 | struct zoneref *z; | |
6808 | struct zone *zone; | |
241994ed | 6809 | retry: |
873b4771 KK |
6810 | delayacct_freepages_start(); |
6811 | ||
b5ead35e | 6812 | if (!cgroup_reclaim(sc)) |
7cc30fcf | 6813 | __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); |
1da177e4 | 6814 | |
9e3b2f8c | 6815 | do { |
73b73bac YA |
6816 | if (!sc->proactive) |
6817 | vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, | |
6818 | sc->priority); | |
66e1707b | 6819 | sc->nr_scanned = 0; |
0a0337e0 | 6820 | shrink_zones(zonelist, sc); |
c6a8a8c5 | 6821 | |
bb21c7ce | 6822 | if (sc->nr_reclaimed >= sc->nr_to_reclaim) |
0b06496a JW |
6823 | break; |
6824 | ||
6825 | if (sc->compaction_ready) | |
6826 | break; | |
1da177e4 | 6827 | |
0e50ce3b MK |
6828 | /* |
6829 | * If we're getting trouble reclaiming, start doing | |
6830 | * writepage even in laptop mode. | |
6831 | */ | |
6832 | if (sc->priority < DEF_PRIORITY - 2) | |
6833 | sc->may_writepage = 1; | |
0b06496a | 6834 | } while (--sc->priority >= 0); |
bb21c7ce | 6835 | |
2a2e4885 JW |
6836 | last_pgdat = NULL; |
6837 | for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, | |
6838 | sc->nodemask) { | |
6839 | if (zone->zone_pgdat == last_pgdat) | |
6840 | continue; | |
6841 | last_pgdat = zone->zone_pgdat; | |
1b05117d | 6842 | |
2a2e4885 | 6843 | snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); |
1b05117d JW |
6844 | |
6845 | if (cgroup_reclaim(sc)) { | |
6846 | struct lruvec *lruvec; | |
6847 | ||
6848 | lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, | |
6849 | zone->zone_pgdat); | |
6850 | clear_bit(LRUVEC_CONGESTED, &lruvec->flags); | |
6851 | } | |
2a2e4885 JW |
6852 | } |
6853 | ||
873b4771 KK |
6854 | delayacct_freepages_end(); |
6855 | ||
bb21c7ce KM |
6856 | if (sc->nr_reclaimed) |
6857 | return sc->nr_reclaimed; | |
6858 | ||
0cee34fd | 6859 | /* Aborted reclaim to try compaction? don't OOM, then */ |
0b06496a | 6860 | if (sc->compaction_ready) |
7335084d MG |
6861 | return 1; |
6862 | ||
b91ac374 JW |
6863 | /* |
6864 | * We make inactive:active ratio decisions based on the node's | |
6865 | * composition of memory, but a restrictive reclaim_idx or a | |
6866 | * memory.low cgroup setting can exempt large amounts of | |
6867 | * memory from reclaim. Neither of which are very common, so | |
6868 | * instead of doing costly eligibility calculations of the | |
6869 | * entire cgroup subtree up front, we assume the estimates are | |
6870 | * good, and retry with forcible deactivation if that fails. | |
6871 | */ | |
6872 | if (sc->skipped_deactivate) { | |
6873 | sc->priority = initial_priority; | |
6874 | sc->force_deactivate = 1; | |
6875 | sc->skipped_deactivate = 0; | |
6876 | goto retry; | |
6877 | } | |
6878 | ||
241994ed | 6879 | /* Untapped cgroup reserves? Don't OOM, retry. */ |
d6622f63 | 6880 | if (sc->memcg_low_skipped) { |
241994ed | 6881 | sc->priority = initial_priority; |
b91ac374 | 6882 | sc->force_deactivate = 0; |
d6622f63 YX |
6883 | sc->memcg_low_reclaim = 1; |
6884 | sc->memcg_low_skipped = 0; | |
241994ed JW |
6885 | goto retry; |
6886 | } | |
6887 | ||
bb21c7ce | 6888 | return 0; |
1da177e4 LT |
6889 | } |
6890 | ||
c73322d0 | 6891 | static bool allow_direct_reclaim(pg_data_t *pgdat) |
5515061d MG |
6892 | { |
6893 | struct zone *zone; | |
6894 | unsigned long pfmemalloc_reserve = 0; | |
6895 | unsigned long free_pages = 0; | |
6896 | int i; | |
6897 | bool wmark_ok; | |
6898 | ||
c73322d0 JW |
6899 | if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) |
6900 | return true; | |
6901 | ||
5515061d MG |
6902 | for (i = 0; i <= ZONE_NORMAL; i++) { |
6903 | zone = &pgdat->node_zones[i]; | |
d450abd8 JW |
6904 | if (!managed_zone(zone)) |
6905 | continue; | |
6906 | ||
6907 | if (!zone_reclaimable_pages(zone)) | |
675becce MG |
6908 | continue; |
6909 | ||
5515061d MG |
6910 | pfmemalloc_reserve += min_wmark_pages(zone); |
6911 | free_pages += zone_page_state(zone, NR_FREE_PAGES); | |
6912 | } | |
6913 | ||
675becce MG |
6914 | /* If there are no reserves (unexpected config) then do not throttle */ |
6915 | if (!pfmemalloc_reserve) | |
6916 | return true; | |
6917 | ||
5515061d MG |
6918 | wmark_ok = free_pages > pfmemalloc_reserve / 2; |
6919 | ||
6920 | /* kswapd must be awake if processes are being throttled */ | |
6921 | if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { | |
97a225e6 JK |
6922 | if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL) |
6923 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL); | |
5644e1fb | 6924 | |
5515061d MG |
6925 | wake_up_interruptible(&pgdat->kswapd_wait); |
6926 | } | |
6927 | ||
6928 | return wmark_ok; | |
6929 | } | |
6930 | ||
6931 | /* | |
6932 | * Throttle direct reclaimers if backing storage is backed by the network | |
6933 | * and the PFMEMALLOC reserve for the preferred node is getting dangerously | |
6934 | * depleted. kswapd will continue to make progress and wake the processes | |
50694c28 MG |
6935 | * when the low watermark is reached. |
6936 | * | |
6937 | * Returns true if a fatal signal was delivered during throttling. If this | |
6938 | * happens, the page allocator should not consider triggering the OOM killer. | |
5515061d | 6939 | */ |
50694c28 | 6940 | static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, |
5515061d MG |
6941 | nodemask_t *nodemask) |
6942 | { | |
675becce | 6943 | struct zoneref *z; |
5515061d | 6944 | struct zone *zone; |
675becce | 6945 | pg_data_t *pgdat = NULL; |
5515061d MG |
6946 | |
6947 | /* | |
6948 | * Kernel threads should not be throttled as they may be indirectly | |
6949 | * responsible for cleaning pages necessary for reclaim to make forward | |
6950 | * progress. kjournald for example may enter direct reclaim while | |
6951 | * committing a transaction where throttling it could forcing other | |
6952 | * processes to block on log_wait_commit(). | |
6953 | */ | |
6954 | if (current->flags & PF_KTHREAD) | |
50694c28 MG |
6955 | goto out; |
6956 | ||
6957 | /* | |
6958 | * If a fatal signal is pending, this process should not throttle. | |
6959 | * It should return quickly so it can exit and free its memory | |
6960 | */ | |
6961 | if (fatal_signal_pending(current)) | |
6962 | goto out; | |
5515061d | 6963 | |
675becce MG |
6964 | /* |
6965 | * Check if the pfmemalloc reserves are ok by finding the first node | |
6966 | * with a usable ZONE_NORMAL or lower zone. The expectation is that | |
6967 | * GFP_KERNEL will be required for allocating network buffers when | |
6968 | * swapping over the network so ZONE_HIGHMEM is unusable. | |
6969 | * | |
6970 | * Throttling is based on the first usable node and throttled processes | |
6971 | * wait on a queue until kswapd makes progress and wakes them. There | |
6972 | * is an affinity then between processes waking up and where reclaim | |
6973 | * progress has been made assuming the process wakes on the same node. | |
6974 | * More importantly, processes running on remote nodes will not compete | |
6975 | * for remote pfmemalloc reserves and processes on different nodes | |
6976 | * should make reasonable progress. | |
6977 | */ | |
6978 | for_each_zone_zonelist_nodemask(zone, z, zonelist, | |
17636faa | 6979 | gfp_zone(gfp_mask), nodemask) { |
675becce MG |
6980 | if (zone_idx(zone) > ZONE_NORMAL) |
6981 | continue; | |
6982 | ||
6983 | /* Throttle based on the first usable node */ | |
6984 | pgdat = zone->zone_pgdat; | |
c73322d0 | 6985 | if (allow_direct_reclaim(pgdat)) |
675becce MG |
6986 | goto out; |
6987 | break; | |
6988 | } | |
6989 | ||
6990 | /* If no zone was usable by the allocation flags then do not throttle */ | |
6991 | if (!pgdat) | |
50694c28 | 6992 | goto out; |
5515061d | 6993 | |
68243e76 MG |
6994 | /* Account for the throttling */ |
6995 | count_vm_event(PGSCAN_DIRECT_THROTTLE); | |
6996 | ||
5515061d MG |
6997 | /* |
6998 | * If the caller cannot enter the filesystem, it's possible that it | |
6999 | * is due to the caller holding an FS lock or performing a journal | |
7000 | * transaction in the case of a filesystem like ext[3|4]. In this case, | |
7001 | * it is not safe to block on pfmemalloc_wait as kswapd could be | |
7002 | * blocked waiting on the same lock. Instead, throttle for up to a | |
7003 | * second before continuing. | |
7004 | */ | |
2e786d9e | 7005 | if (!(gfp_mask & __GFP_FS)) |
5515061d | 7006 | wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, |
c73322d0 | 7007 | allow_direct_reclaim(pgdat), HZ); |
2e786d9e ML |
7008 | else |
7009 | /* Throttle until kswapd wakes the process */ | |
7010 | wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, | |
7011 | allow_direct_reclaim(pgdat)); | |
50694c28 | 7012 | |
50694c28 MG |
7013 | if (fatal_signal_pending(current)) |
7014 | return true; | |
7015 | ||
7016 | out: | |
7017 | return false; | |
5515061d MG |
7018 | } |
7019 | ||
dac1d27b | 7020 | unsigned long try_to_free_pages(struct zonelist *zonelist, int order, |
327c0e96 | 7021 | gfp_t gfp_mask, nodemask_t *nodemask) |
66e1707b | 7022 | { |
33906bc5 | 7023 | unsigned long nr_reclaimed; |
66e1707b | 7024 | struct scan_control sc = { |
ee814fe2 | 7025 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
f2f43e56 | 7026 | .gfp_mask = current_gfp_context(gfp_mask), |
b2e18757 | 7027 | .reclaim_idx = gfp_zone(gfp_mask), |
ee814fe2 JW |
7028 | .order = order, |
7029 | .nodemask = nodemask, | |
7030 | .priority = DEF_PRIORITY, | |
66e1707b | 7031 | .may_writepage = !laptop_mode, |
a6dc60f8 | 7032 | .may_unmap = 1, |
2e2e4259 | 7033 | .may_swap = 1, |
66e1707b BS |
7034 | }; |
7035 | ||
bb451fdf GT |
7036 | /* |
7037 | * scan_control uses s8 fields for order, priority, and reclaim_idx. | |
7038 | * Confirm they are large enough for max values. | |
7039 | */ | |
23baf831 | 7040 | BUILD_BUG_ON(MAX_ORDER >= S8_MAX); |
bb451fdf GT |
7041 | BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); |
7042 | BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); | |
7043 | ||
5515061d | 7044 | /* |
50694c28 MG |
7045 | * Do not enter reclaim if fatal signal was delivered while throttled. |
7046 | * 1 is returned so that the page allocator does not OOM kill at this | |
7047 | * point. | |
5515061d | 7048 | */ |
f2f43e56 | 7049 | if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) |
5515061d MG |
7050 | return 1; |
7051 | ||
1732d2b0 | 7052 | set_task_reclaim_state(current, &sc.reclaim_state); |
3481c37f | 7053 | trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask); |
33906bc5 | 7054 | |
3115cd91 | 7055 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
33906bc5 MG |
7056 | |
7057 | trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); | |
1732d2b0 | 7058 | set_task_reclaim_state(current, NULL); |
33906bc5 MG |
7059 | |
7060 | return nr_reclaimed; | |
66e1707b BS |
7061 | } |
7062 | ||
c255a458 | 7063 | #ifdef CONFIG_MEMCG |
66e1707b | 7064 | |
d2e5fb92 | 7065 | /* Only used by soft limit reclaim. Do not reuse for anything else. */ |
a9dd0a83 | 7066 | unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, |
4e416953 | 7067 | gfp_t gfp_mask, bool noswap, |
ef8f2327 | 7068 | pg_data_t *pgdat, |
0ae5e89c | 7069 | unsigned long *nr_scanned) |
4e416953 | 7070 | { |
afaf07a6 | 7071 | struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); |
4e416953 | 7072 | struct scan_control sc = { |
b8f5c566 | 7073 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
ee814fe2 | 7074 | .target_mem_cgroup = memcg, |
4e416953 BS |
7075 | .may_writepage = !laptop_mode, |
7076 | .may_unmap = 1, | |
b2e18757 | 7077 | .reclaim_idx = MAX_NR_ZONES - 1, |
4e416953 | 7078 | .may_swap = !noswap, |
4e416953 | 7079 | }; |
0ae5e89c | 7080 | |
d2e5fb92 MH |
7081 | WARN_ON_ONCE(!current->reclaim_state); |
7082 | ||
4e416953 BS |
7083 | sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | |
7084 | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); | |
bdce6d9e | 7085 | |
9e3b2f8c | 7086 | trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, |
3481c37f | 7087 | sc.gfp_mask); |
bdce6d9e | 7088 | |
4e416953 BS |
7089 | /* |
7090 | * NOTE: Although we can get the priority field, using it | |
7091 | * here is not a good idea, since it limits the pages we can scan. | |
a9dd0a83 | 7092 | * if we don't reclaim here, the shrink_node from balance_pgdat |
4e416953 BS |
7093 | * will pick up pages from other mem cgroup's as well. We hack |
7094 | * the priority and make it zero. | |
7095 | */ | |
afaf07a6 | 7096 | shrink_lruvec(lruvec, &sc); |
bdce6d9e KM |
7097 | |
7098 | trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); | |
7099 | ||
0ae5e89c | 7100 | *nr_scanned = sc.nr_scanned; |
0308f7cf | 7101 | |
4e416953 BS |
7102 | return sc.nr_reclaimed; |
7103 | } | |
7104 | ||
72835c86 | 7105 | unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, |
b70a2a21 | 7106 | unsigned long nr_pages, |
a7885eb8 | 7107 | gfp_t gfp_mask, |
55ab834a | 7108 | unsigned int reclaim_options) |
66e1707b | 7109 | { |
bdce6d9e | 7110 | unsigned long nr_reclaimed; |
499118e9 | 7111 | unsigned int noreclaim_flag; |
66e1707b | 7112 | struct scan_control sc = { |
b70a2a21 | 7113 | .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), |
7dea19f9 | 7114 | .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | |
a09ed5e0 | 7115 | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), |
b2e18757 | 7116 | .reclaim_idx = MAX_NR_ZONES - 1, |
ee814fe2 JW |
7117 | .target_mem_cgroup = memcg, |
7118 | .priority = DEF_PRIORITY, | |
7119 | .may_writepage = !laptop_mode, | |
7120 | .may_unmap = 1, | |
73b73bac YA |
7121 | .may_swap = !!(reclaim_options & MEMCG_RECLAIM_MAY_SWAP), |
7122 | .proactive = !!(reclaim_options & MEMCG_RECLAIM_PROACTIVE), | |
a09ed5e0 | 7123 | }; |
889976db | 7124 | /* |
fa40d1ee SB |
7125 | * Traverse the ZONELIST_FALLBACK zonelist of the current node to put |
7126 | * equal pressure on all the nodes. This is based on the assumption that | |
7127 | * the reclaim does not bail out early. | |
889976db | 7128 | */ |
fa40d1ee | 7129 | struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); |
889976db | 7130 | |
fa40d1ee | 7131 | set_task_reclaim_state(current, &sc.reclaim_state); |
3481c37f | 7132 | trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask); |
499118e9 | 7133 | noreclaim_flag = memalloc_noreclaim_save(); |
eb414681 | 7134 | |
3115cd91 | 7135 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
eb414681 | 7136 | |
499118e9 | 7137 | memalloc_noreclaim_restore(noreclaim_flag); |
bdce6d9e | 7138 | trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); |
1732d2b0 | 7139 | set_task_reclaim_state(current, NULL); |
bdce6d9e KM |
7140 | |
7141 | return nr_reclaimed; | |
66e1707b BS |
7142 | } |
7143 | #endif | |
7144 | ||
ac35a490 | 7145 | static void kswapd_age_node(struct pglist_data *pgdat, struct scan_control *sc) |
f16015fb | 7146 | { |
b95a2f2d | 7147 | struct mem_cgroup *memcg; |
b91ac374 | 7148 | struct lruvec *lruvec; |
f16015fb | 7149 | |
ac35a490 YZ |
7150 | if (lru_gen_enabled()) { |
7151 | lru_gen_age_node(pgdat, sc); | |
7152 | return; | |
7153 | } | |
7154 | ||
2f368a9f | 7155 | if (!can_age_anon_pages(pgdat, sc)) |
b95a2f2d JW |
7156 | return; |
7157 | ||
b91ac374 JW |
7158 | lruvec = mem_cgroup_lruvec(NULL, pgdat); |
7159 | if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON)) | |
7160 | return; | |
7161 | ||
b95a2f2d JW |
7162 | memcg = mem_cgroup_iter(NULL, NULL, NULL); |
7163 | do { | |
b91ac374 JW |
7164 | lruvec = mem_cgroup_lruvec(memcg, pgdat); |
7165 | shrink_active_list(SWAP_CLUSTER_MAX, lruvec, | |
7166 | sc, LRU_ACTIVE_ANON); | |
b95a2f2d JW |
7167 | memcg = mem_cgroup_iter(NULL, memcg, NULL); |
7168 | } while (memcg); | |
f16015fb JW |
7169 | } |
7170 | ||
97a225e6 | 7171 | static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx) |
1c30844d MG |
7172 | { |
7173 | int i; | |
7174 | struct zone *zone; | |
7175 | ||
7176 | /* | |
7177 | * Check for watermark boosts top-down as the higher zones | |
7178 | * are more likely to be boosted. Both watermarks and boosts | |
1eba09c1 | 7179 | * should not be checked at the same time as reclaim would |
1c30844d MG |
7180 | * start prematurely when there is no boosting and a lower |
7181 | * zone is balanced. | |
7182 | */ | |
97a225e6 | 7183 | for (i = highest_zoneidx; i >= 0; i--) { |
1c30844d MG |
7184 | zone = pgdat->node_zones + i; |
7185 | if (!managed_zone(zone)) | |
7186 | continue; | |
7187 | ||
7188 | if (zone->watermark_boost) | |
7189 | return true; | |
7190 | } | |
7191 | ||
7192 | return false; | |
7193 | } | |
7194 | ||
e716f2eb MG |
7195 | /* |
7196 | * Returns true if there is an eligible zone balanced for the request order | |
97a225e6 | 7197 | * and highest_zoneidx |
e716f2eb | 7198 | */ |
97a225e6 | 7199 | static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx) |
60cefed4 | 7200 | { |
e716f2eb MG |
7201 | int i; |
7202 | unsigned long mark = -1; | |
7203 | struct zone *zone; | |
60cefed4 | 7204 | |
1c30844d MG |
7205 | /* |
7206 | * Check watermarks bottom-up as lower zones are more likely to | |
7207 | * meet watermarks. | |
7208 | */ | |
97a225e6 | 7209 | for (i = 0; i <= highest_zoneidx; i++) { |
e716f2eb | 7210 | zone = pgdat->node_zones + i; |
6256c6b4 | 7211 | |
e716f2eb MG |
7212 | if (!managed_zone(zone)) |
7213 | continue; | |
7214 | ||
c574bbe9 HY |
7215 | if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) |
7216 | mark = wmark_pages(zone, WMARK_PROMO); | |
7217 | else | |
7218 | mark = high_wmark_pages(zone); | |
97a225e6 | 7219 | if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx)) |
e716f2eb MG |
7220 | return true; |
7221 | } | |
7222 | ||
7223 | /* | |
36c26128 | 7224 | * If a node has no managed zone within highest_zoneidx, it does not |
e716f2eb MG |
7225 | * need balancing by definition. This can happen if a zone-restricted |
7226 | * allocation tries to wake a remote kswapd. | |
7227 | */ | |
7228 | if (mark == -1) | |
7229 | return true; | |
7230 | ||
7231 | return false; | |
60cefed4 JW |
7232 | } |
7233 | ||
631b6e08 MG |
7234 | /* Clear pgdat state for congested, dirty or under writeback. */ |
7235 | static void clear_pgdat_congested(pg_data_t *pgdat) | |
7236 | { | |
1b05117d JW |
7237 | struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat); |
7238 | ||
7239 | clear_bit(LRUVEC_CONGESTED, &lruvec->flags); | |
631b6e08 MG |
7240 | clear_bit(PGDAT_DIRTY, &pgdat->flags); |
7241 | clear_bit(PGDAT_WRITEBACK, &pgdat->flags); | |
7242 | } | |
7243 | ||
5515061d MG |
7244 | /* |
7245 | * Prepare kswapd for sleeping. This verifies that there are no processes | |
7246 | * waiting in throttle_direct_reclaim() and that watermarks have been met. | |
7247 | * | |
7248 | * Returns true if kswapd is ready to sleep | |
7249 | */ | |
97a225e6 JK |
7250 | static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, |
7251 | int highest_zoneidx) | |
f50de2d3 | 7252 | { |
5515061d | 7253 | /* |
9e5e3661 | 7254 | * The throttled processes are normally woken up in balance_pgdat() as |
c73322d0 | 7255 | * soon as allow_direct_reclaim() is true. But there is a potential |
9e5e3661 VB |
7256 | * race between when kswapd checks the watermarks and a process gets |
7257 | * throttled. There is also a potential race if processes get | |
7258 | * throttled, kswapd wakes, a large process exits thereby balancing the | |
7259 | * zones, which causes kswapd to exit balance_pgdat() before reaching | |
7260 | * the wake up checks. If kswapd is going to sleep, no process should | |
7261 | * be sleeping on pfmemalloc_wait, so wake them now if necessary. If | |
7262 | * the wake up is premature, processes will wake kswapd and get | |
7263 | * throttled again. The difference from wake ups in balance_pgdat() is | |
7264 | * that here we are under prepare_to_wait(). | |
5515061d | 7265 | */ |
9e5e3661 VB |
7266 | if (waitqueue_active(&pgdat->pfmemalloc_wait)) |
7267 | wake_up_all(&pgdat->pfmemalloc_wait); | |
f50de2d3 | 7268 | |
c73322d0 JW |
7269 | /* Hopeless node, leave it to direct reclaim */ |
7270 | if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) | |
7271 | return true; | |
7272 | ||
97a225e6 | 7273 | if (pgdat_balanced(pgdat, order, highest_zoneidx)) { |
e716f2eb MG |
7274 | clear_pgdat_congested(pgdat); |
7275 | return true; | |
1d82de61 MG |
7276 | } |
7277 | ||
333b0a45 | 7278 | return false; |
f50de2d3 MG |
7279 | } |
7280 | ||
75485363 | 7281 | /* |
1d82de61 MG |
7282 | * kswapd shrinks a node of pages that are at or below the highest usable |
7283 | * zone that is currently unbalanced. | |
b8e83b94 MG |
7284 | * |
7285 | * Returns true if kswapd scanned at least the requested number of pages to | |
283aba9f MG |
7286 | * reclaim or if the lack of progress was due to pages under writeback. |
7287 | * This is used to determine if the scanning priority needs to be raised. | |
75485363 | 7288 | */ |
1d82de61 | 7289 | static bool kswapd_shrink_node(pg_data_t *pgdat, |
accf6242 | 7290 | struct scan_control *sc) |
75485363 | 7291 | { |
1d82de61 MG |
7292 | struct zone *zone; |
7293 | int z; | |
75485363 | 7294 | |
1d82de61 MG |
7295 | /* Reclaim a number of pages proportional to the number of zones */ |
7296 | sc->nr_to_reclaim = 0; | |
970a39a3 | 7297 | for (z = 0; z <= sc->reclaim_idx; z++) { |
1d82de61 | 7298 | zone = pgdat->node_zones + z; |
6aa303de | 7299 | if (!managed_zone(zone)) |
1d82de61 | 7300 | continue; |
7c954f6d | 7301 | |
1d82de61 MG |
7302 | sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); |
7303 | } | |
7c954f6d MG |
7304 | |
7305 | /* | |
1d82de61 MG |
7306 | * Historically care was taken to put equal pressure on all zones but |
7307 | * now pressure is applied based on node LRU order. | |
7c954f6d | 7308 | */ |
970a39a3 | 7309 | shrink_node(pgdat, sc); |
283aba9f | 7310 | |
7c954f6d | 7311 | /* |
1d82de61 MG |
7312 | * Fragmentation may mean that the system cannot be rebalanced for |
7313 | * high-order allocations. If twice the allocation size has been | |
7314 | * reclaimed then recheck watermarks only at order-0 to prevent | |
7315 | * excessive reclaim. Assume that a process requested a high-order | |
7316 | * can direct reclaim/compact. | |
7c954f6d | 7317 | */ |
9861a62c | 7318 | if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) |
1d82de61 | 7319 | sc->order = 0; |
7c954f6d | 7320 | |
b8e83b94 | 7321 | return sc->nr_scanned >= sc->nr_to_reclaim; |
75485363 MG |
7322 | } |
7323 | ||
c49c2c47 MG |
7324 | /* Page allocator PCP high watermark is lowered if reclaim is active. */ |
7325 | static inline void | |
7326 | update_reclaim_active(pg_data_t *pgdat, int highest_zoneidx, bool active) | |
7327 | { | |
7328 | int i; | |
7329 | struct zone *zone; | |
7330 | ||
7331 | for (i = 0; i <= highest_zoneidx; i++) { | |
7332 | zone = pgdat->node_zones + i; | |
7333 | ||
7334 | if (!managed_zone(zone)) | |
7335 | continue; | |
7336 | ||
7337 | if (active) | |
7338 | set_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); | |
7339 | else | |
7340 | clear_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); | |
7341 | } | |
7342 | } | |
7343 | ||
7344 | static inline void | |
7345 | set_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) | |
7346 | { | |
7347 | update_reclaim_active(pgdat, highest_zoneidx, true); | |
7348 | } | |
7349 | ||
7350 | static inline void | |
7351 | clear_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) | |
7352 | { | |
7353 | update_reclaim_active(pgdat, highest_zoneidx, false); | |
7354 | } | |
7355 | ||
1da177e4 | 7356 | /* |
1d82de61 MG |
7357 | * For kswapd, balance_pgdat() will reclaim pages across a node from zones |
7358 | * that are eligible for use by the caller until at least one zone is | |
7359 | * balanced. | |
1da177e4 | 7360 | * |
1d82de61 | 7361 | * Returns the order kswapd finished reclaiming at. |
1da177e4 LT |
7362 | * |
7363 | * kswapd scans the zones in the highmem->normal->dma direction. It skips | |
41858966 | 7364 | * zones which have free_pages > high_wmark_pages(zone), but once a zone is |
8bb4e7a2 | 7365 | * found to have free_pages <= high_wmark_pages(zone), any page in that zone |
1d82de61 MG |
7366 | * or lower is eligible for reclaim until at least one usable zone is |
7367 | * balanced. | |
1da177e4 | 7368 | */ |
97a225e6 | 7369 | static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) |
1da177e4 | 7370 | { |
1da177e4 | 7371 | int i; |
0608f43d AM |
7372 | unsigned long nr_soft_reclaimed; |
7373 | unsigned long nr_soft_scanned; | |
eb414681 | 7374 | unsigned long pflags; |
1c30844d MG |
7375 | unsigned long nr_boost_reclaim; |
7376 | unsigned long zone_boosts[MAX_NR_ZONES] = { 0, }; | |
7377 | bool boosted; | |
1d82de61 | 7378 | struct zone *zone; |
179e9639 AM |
7379 | struct scan_control sc = { |
7380 | .gfp_mask = GFP_KERNEL, | |
ee814fe2 | 7381 | .order = order, |
a6dc60f8 | 7382 | .may_unmap = 1, |
179e9639 | 7383 | }; |
93781325 | 7384 | |
1732d2b0 | 7385 | set_task_reclaim_state(current, &sc.reclaim_state); |
eb414681 | 7386 | psi_memstall_enter(&pflags); |
4f3eaf45 | 7387 | __fs_reclaim_acquire(_THIS_IP_); |
93781325 | 7388 | |
f8891e5e | 7389 | count_vm_event(PAGEOUTRUN); |
1da177e4 | 7390 | |
1c30844d MG |
7391 | /* |
7392 | * Account for the reclaim boost. Note that the zone boost is left in | |
7393 | * place so that parallel allocations that are near the watermark will | |
7394 | * stall or direct reclaim until kswapd is finished. | |
7395 | */ | |
7396 | nr_boost_reclaim = 0; | |
97a225e6 | 7397 | for (i = 0; i <= highest_zoneidx; i++) { |
1c30844d MG |
7398 | zone = pgdat->node_zones + i; |
7399 | if (!managed_zone(zone)) | |
7400 | continue; | |
7401 | ||
7402 | nr_boost_reclaim += zone->watermark_boost; | |
7403 | zone_boosts[i] = zone->watermark_boost; | |
7404 | } | |
7405 | boosted = nr_boost_reclaim; | |
7406 | ||
7407 | restart: | |
c49c2c47 | 7408 | set_reclaim_active(pgdat, highest_zoneidx); |
1c30844d | 7409 | sc.priority = DEF_PRIORITY; |
9e3b2f8c | 7410 | do { |
c73322d0 | 7411 | unsigned long nr_reclaimed = sc.nr_reclaimed; |
b8e83b94 | 7412 | bool raise_priority = true; |
1c30844d | 7413 | bool balanced; |
93781325 | 7414 | bool ret; |
b8e83b94 | 7415 | |
97a225e6 | 7416 | sc.reclaim_idx = highest_zoneidx; |
1da177e4 | 7417 | |
86c79f6b | 7418 | /* |
84c7a777 MG |
7419 | * If the number of buffer_heads exceeds the maximum allowed |
7420 | * then consider reclaiming from all zones. This has a dual | |
7421 | * purpose -- on 64-bit systems it is expected that | |
7422 | * buffer_heads are stripped during active rotation. On 32-bit | |
7423 | * systems, highmem pages can pin lowmem memory and shrinking | |
7424 | * buffers can relieve lowmem pressure. Reclaim may still not | |
7425 | * go ahead if all eligible zones for the original allocation | |
7426 | * request are balanced to avoid excessive reclaim from kswapd. | |
86c79f6b MG |
7427 | */ |
7428 | if (buffer_heads_over_limit) { | |
7429 | for (i = MAX_NR_ZONES - 1; i >= 0; i--) { | |
7430 | zone = pgdat->node_zones + i; | |
6aa303de | 7431 | if (!managed_zone(zone)) |
86c79f6b | 7432 | continue; |
cc715d99 | 7433 | |
970a39a3 | 7434 | sc.reclaim_idx = i; |
e1dbeda6 | 7435 | break; |
1da177e4 | 7436 | } |
1da177e4 | 7437 | } |
dafcb73e | 7438 | |
86c79f6b | 7439 | /* |
1c30844d MG |
7440 | * If the pgdat is imbalanced then ignore boosting and preserve |
7441 | * the watermarks for a later time and restart. Note that the | |
7442 | * zone watermarks will be still reset at the end of balancing | |
7443 | * on the grounds that the normal reclaim should be enough to | |
7444 | * re-evaluate if boosting is required when kswapd next wakes. | |
7445 | */ | |
97a225e6 | 7446 | balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx); |
1c30844d MG |
7447 | if (!balanced && nr_boost_reclaim) { |
7448 | nr_boost_reclaim = 0; | |
7449 | goto restart; | |
7450 | } | |
7451 | ||
7452 | /* | |
7453 | * If boosting is not active then only reclaim if there are no | |
7454 | * eligible zones. Note that sc.reclaim_idx is not used as | |
7455 | * buffer_heads_over_limit may have adjusted it. | |
86c79f6b | 7456 | */ |
1c30844d | 7457 | if (!nr_boost_reclaim && balanced) |
e716f2eb | 7458 | goto out; |
e1dbeda6 | 7459 | |
1c30844d MG |
7460 | /* Limit the priority of boosting to avoid reclaim writeback */ |
7461 | if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2) | |
7462 | raise_priority = false; | |
7463 | ||
7464 | /* | |
7465 | * Do not writeback or swap pages for boosted reclaim. The | |
7466 | * intent is to relieve pressure not issue sub-optimal IO | |
7467 | * from reclaim context. If no pages are reclaimed, the | |
7468 | * reclaim will be aborted. | |
7469 | */ | |
7470 | sc.may_writepage = !laptop_mode && !nr_boost_reclaim; | |
7471 | sc.may_swap = !nr_boost_reclaim; | |
1c30844d | 7472 | |
1d82de61 | 7473 | /* |
ac35a490 YZ |
7474 | * Do some background aging, to give pages a chance to be |
7475 | * referenced before reclaiming. All pages are rotated | |
7476 | * regardless of classzone as this is about consistent aging. | |
1d82de61 | 7477 | */ |
ac35a490 | 7478 | kswapd_age_node(pgdat, &sc); |
1d82de61 | 7479 | |
b7ea3c41 MG |
7480 | /* |
7481 | * If we're getting trouble reclaiming, start doing writepage | |
7482 | * even in laptop mode. | |
7483 | */ | |
047d72c3 | 7484 | if (sc.priority < DEF_PRIORITY - 2) |
b7ea3c41 MG |
7485 | sc.may_writepage = 1; |
7486 | ||
1d82de61 MG |
7487 | /* Call soft limit reclaim before calling shrink_node. */ |
7488 | sc.nr_scanned = 0; | |
7489 | nr_soft_scanned = 0; | |
ef8f2327 | 7490 | nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, |
1d82de61 MG |
7491 | sc.gfp_mask, &nr_soft_scanned); |
7492 | sc.nr_reclaimed += nr_soft_reclaimed; | |
7493 | ||
1da177e4 | 7494 | /* |
1d82de61 MG |
7495 | * There should be no need to raise the scanning priority if |
7496 | * enough pages are already being scanned that that high | |
7497 | * watermark would be met at 100% efficiency. | |
1da177e4 | 7498 | */ |
970a39a3 | 7499 | if (kswapd_shrink_node(pgdat, &sc)) |
1d82de61 | 7500 | raise_priority = false; |
5515061d MG |
7501 | |
7502 | /* | |
7503 | * If the low watermark is met there is no need for processes | |
7504 | * to be throttled on pfmemalloc_wait as they should not be | |
7505 | * able to safely make forward progress. Wake them | |
7506 | */ | |
7507 | if (waitqueue_active(&pgdat->pfmemalloc_wait) && | |
c73322d0 | 7508 | allow_direct_reclaim(pgdat)) |
cfc51155 | 7509 | wake_up_all(&pgdat->pfmemalloc_wait); |
5515061d | 7510 | |
b8e83b94 | 7511 | /* Check if kswapd should be suspending */ |
4f3eaf45 | 7512 | __fs_reclaim_release(_THIS_IP_); |
93781325 | 7513 | ret = try_to_freeze(); |
4f3eaf45 | 7514 | __fs_reclaim_acquire(_THIS_IP_); |
93781325 | 7515 | if (ret || kthread_should_stop()) |
b8e83b94 | 7516 | break; |
8357376d | 7517 | |
73ce02e9 | 7518 | /* |
b8e83b94 MG |
7519 | * Raise priority if scanning rate is too low or there was no |
7520 | * progress in reclaiming pages | |
73ce02e9 | 7521 | */ |
c73322d0 | 7522 | nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; |
1c30844d MG |
7523 | nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed); |
7524 | ||
7525 | /* | |
7526 | * If reclaim made no progress for a boost, stop reclaim as | |
7527 | * IO cannot be queued and it could be an infinite loop in | |
7528 | * extreme circumstances. | |
7529 | */ | |
7530 | if (nr_boost_reclaim && !nr_reclaimed) | |
7531 | break; | |
7532 | ||
c73322d0 | 7533 | if (raise_priority || !nr_reclaimed) |
b8e83b94 | 7534 | sc.priority--; |
1d82de61 | 7535 | } while (sc.priority >= 1); |
1da177e4 | 7536 | |
c73322d0 JW |
7537 | if (!sc.nr_reclaimed) |
7538 | pgdat->kswapd_failures++; | |
7539 | ||
b8e83b94 | 7540 | out: |
c49c2c47 MG |
7541 | clear_reclaim_active(pgdat, highest_zoneidx); |
7542 | ||
1c30844d MG |
7543 | /* If reclaim was boosted, account for the reclaim done in this pass */ |
7544 | if (boosted) { | |
7545 | unsigned long flags; | |
7546 | ||
97a225e6 | 7547 | for (i = 0; i <= highest_zoneidx; i++) { |
1c30844d MG |
7548 | if (!zone_boosts[i]) |
7549 | continue; | |
7550 | ||
7551 | /* Increments are under the zone lock */ | |
7552 | zone = pgdat->node_zones + i; | |
7553 | spin_lock_irqsave(&zone->lock, flags); | |
7554 | zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]); | |
7555 | spin_unlock_irqrestore(&zone->lock, flags); | |
7556 | } | |
7557 | ||
7558 | /* | |
7559 | * As there is now likely space, wakeup kcompact to defragment | |
7560 | * pageblocks. | |
7561 | */ | |
97a225e6 | 7562 | wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx); |
1c30844d MG |
7563 | } |
7564 | ||
2a2e4885 | 7565 | snapshot_refaults(NULL, pgdat); |
4f3eaf45 | 7566 | __fs_reclaim_release(_THIS_IP_); |
eb414681 | 7567 | psi_memstall_leave(&pflags); |
1732d2b0 | 7568 | set_task_reclaim_state(current, NULL); |
e5ca8071 | 7569 | |
0abdee2b | 7570 | /* |
1d82de61 MG |
7571 | * Return the order kswapd stopped reclaiming at as |
7572 | * prepare_kswapd_sleep() takes it into account. If another caller | |
7573 | * entered the allocator slow path while kswapd was awake, order will | |
7574 | * remain at the higher level. | |
0abdee2b | 7575 | */ |
1d82de61 | 7576 | return sc.order; |
1da177e4 LT |
7577 | } |
7578 | ||
e716f2eb | 7579 | /* |
97a225e6 JK |
7580 | * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to |
7581 | * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is | |
7582 | * not a valid index then either kswapd runs for first time or kswapd couldn't | |
7583 | * sleep after previous reclaim attempt (node is still unbalanced). In that | |
7584 | * case return the zone index of the previous kswapd reclaim cycle. | |
e716f2eb | 7585 | */ |
97a225e6 JK |
7586 | static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, |
7587 | enum zone_type prev_highest_zoneidx) | |
e716f2eb | 7588 | { |
97a225e6 | 7589 | enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); |
5644e1fb | 7590 | |
97a225e6 | 7591 | return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx; |
e716f2eb MG |
7592 | } |
7593 | ||
38087d9b | 7594 | static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, |
97a225e6 | 7595 | unsigned int highest_zoneidx) |
f0bc0a60 KM |
7596 | { |
7597 | long remaining = 0; | |
7598 | DEFINE_WAIT(wait); | |
7599 | ||
7600 | if (freezing(current) || kthread_should_stop()) | |
7601 | return; | |
7602 | ||
7603 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); | |
7604 | ||
333b0a45 SG |
7605 | /* |
7606 | * Try to sleep for a short interval. Note that kcompactd will only be | |
7607 | * woken if it is possible to sleep for a short interval. This is | |
7608 | * deliberate on the assumption that if reclaim cannot keep an | |
7609 | * eligible zone balanced that it's also unlikely that compaction will | |
7610 | * succeed. | |
7611 | */ | |
97a225e6 | 7612 | if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { |
fd901c95 VB |
7613 | /* |
7614 | * Compaction records what page blocks it recently failed to | |
7615 | * isolate pages from and skips them in the future scanning. | |
7616 | * When kswapd is going to sleep, it is reasonable to assume | |
7617 | * that pages and compaction may succeed so reset the cache. | |
7618 | */ | |
7619 | reset_isolation_suitable(pgdat); | |
7620 | ||
7621 | /* | |
7622 | * We have freed the memory, now we should compact it to make | |
7623 | * allocation of the requested order possible. | |
7624 | */ | |
97a225e6 | 7625 | wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx); |
fd901c95 | 7626 | |
f0bc0a60 | 7627 | remaining = schedule_timeout(HZ/10); |
38087d9b MG |
7628 | |
7629 | /* | |
97a225e6 | 7630 | * If woken prematurely then reset kswapd_highest_zoneidx and |
38087d9b MG |
7631 | * order. The values will either be from a wakeup request or |
7632 | * the previous request that slept prematurely. | |
7633 | */ | |
7634 | if (remaining) { | |
97a225e6 JK |
7635 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, |
7636 | kswapd_highest_zoneidx(pgdat, | |
7637 | highest_zoneidx)); | |
5644e1fb QC |
7638 | |
7639 | if (READ_ONCE(pgdat->kswapd_order) < reclaim_order) | |
7640 | WRITE_ONCE(pgdat->kswapd_order, reclaim_order); | |
38087d9b MG |
7641 | } |
7642 | ||
f0bc0a60 KM |
7643 | finish_wait(&pgdat->kswapd_wait, &wait); |
7644 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); | |
7645 | } | |
7646 | ||
7647 | /* | |
7648 | * After a short sleep, check if it was a premature sleep. If not, then | |
7649 | * go fully to sleep until explicitly woken up. | |
7650 | */ | |
d9f21d42 | 7651 | if (!remaining && |
97a225e6 | 7652 | prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { |
f0bc0a60 KM |
7653 | trace_mm_vmscan_kswapd_sleep(pgdat->node_id); |
7654 | ||
7655 | /* | |
7656 | * vmstat counters are not perfectly accurate and the estimated | |
7657 | * value for counters such as NR_FREE_PAGES can deviate from the | |
7658 | * true value by nr_online_cpus * threshold. To avoid the zone | |
7659 | * watermarks being breached while under pressure, we reduce the | |
7660 | * per-cpu vmstat threshold while kswapd is awake and restore | |
7661 | * them before going back to sleep. | |
7662 | */ | |
7663 | set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); | |
1c7e7f6c AK |
7664 | |
7665 | if (!kthread_should_stop()) | |
7666 | schedule(); | |
7667 | ||
f0bc0a60 KM |
7668 | set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); |
7669 | } else { | |
7670 | if (remaining) | |
7671 | count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); | |
7672 | else | |
7673 | count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); | |
7674 | } | |
7675 | finish_wait(&pgdat->kswapd_wait, &wait); | |
7676 | } | |
7677 | ||
1da177e4 LT |
7678 | /* |
7679 | * The background pageout daemon, started as a kernel thread | |
4f98a2fe | 7680 | * from the init process. |
1da177e4 LT |
7681 | * |
7682 | * This basically trickles out pages so that we have _some_ | |
7683 | * free memory available even if there is no other activity | |
7684 | * that frees anything up. This is needed for things like routing | |
7685 | * etc, where we otherwise might have all activity going on in | |
7686 | * asynchronous contexts that cannot page things out. | |
7687 | * | |
7688 | * If there are applications that are active memory-allocators | |
7689 | * (most normal use), this basically shouldn't matter. | |
7690 | */ | |
7691 | static int kswapd(void *p) | |
7692 | { | |
e716f2eb | 7693 | unsigned int alloc_order, reclaim_order; |
97a225e6 | 7694 | unsigned int highest_zoneidx = MAX_NR_ZONES - 1; |
68d68ff6 | 7695 | pg_data_t *pgdat = (pg_data_t *)p; |
1da177e4 | 7696 | struct task_struct *tsk = current; |
a70f7302 | 7697 | const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); |
1da177e4 | 7698 | |
174596a0 | 7699 | if (!cpumask_empty(cpumask)) |
c5f59f08 | 7700 | set_cpus_allowed_ptr(tsk, cpumask); |
1da177e4 LT |
7701 | |
7702 | /* | |
7703 | * Tell the memory management that we're a "memory allocator", | |
7704 | * and that if we need more memory we should get access to it | |
7705 | * regardless (see "__alloc_pages()"). "kswapd" should | |
7706 | * never get caught in the normal page freeing logic. | |
7707 | * | |
7708 | * (Kswapd normally doesn't need memory anyway, but sometimes | |
7709 | * you need a small amount of memory in order to be able to | |
7710 | * page out something else, and this flag essentially protects | |
7711 | * us from recursively trying to free more memory as we're | |
7712 | * trying to free the first piece of memory in the first place). | |
7713 | */ | |
b698f0a1 | 7714 | tsk->flags |= PF_MEMALLOC | PF_KSWAPD; |
83144186 | 7715 | set_freezable(); |
1da177e4 | 7716 | |
5644e1fb | 7717 | WRITE_ONCE(pgdat->kswapd_order, 0); |
97a225e6 | 7718 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); |
8cd7c588 | 7719 | atomic_set(&pgdat->nr_writeback_throttled, 0); |
1da177e4 | 7720 | for ( ; ; ) { |
6f6313d4 | 7721 | bool ret; |
3e1d1d28 | 7722 | |
5644e1fb | 7723 | alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order); |
97a225e6 JK |
7724 | highest_zoneidx = kswapd_highest_zoneidx(pgdat, |
7725 | highest_zoneidx); | |
e716f2eb | 7726 | |
38087d9b MG |
7727 | kswapd_try_sleep: |
7728 | kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, | |
97a225e6 | 7729 | highest_zoneidx); |
215ddd66 | 7730 | |
97a225e6 | 7731 | /* Read the new order and highest_zoneidx */ |
2b47a24c | 7732 | alloc_order = READ_ONCE(pgdat->kswapd_order); |
97a225e6 JK |
7733 | highest_zoneidx = kswapd_highest_zoneidx(pgdat, |
7734 | highest_zoneidx); | |
5644e1fb | 7735 | WRITE_ONCE(pgdat->kswapd_order, 0); |
97a225e6 | 7736 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); |
1da177e4 | 7737 | |
8fe23e05 DR |
7738 | ret = try_to_freeze(); |
7739 | if (kthread_should_stop()) | |
7740 | break; | |
7741 | ||
7742 | /* | |
7743 | * We can speed up thawing tasks if we don't call balance_pgdat | |
7744 | * after returning from the refrigerator | |
7745 | */ | |
38087d9b MG |
7746 | if (ret) |
7747 | continue; | |
7748 | ||
7749 | /* | |
7750 | * Reclaim begins at the requested order but if a high-order | |
7751 | * reclaim fails then kswapd falls back to reclaiming for | |
7752 | * order-0. If that happens, kswapd will consider sleeping | |
7753 | * for the order it finished reclaiming at (reclaim_order) | |
7754 | * but kcompactd is woken to compact for the original | |
7755 | * request (alloc_order). | |
7756 | */ | |
97a225e6 | 7757 | trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx, |
e5146b12 | 7758 | alloc_order); |
97a225e6 JK |
7759 | reclaim_order = balance_pgdat(pgdat, alloc_order, |
7760 | highest_zoneidx); | |
38087d9b MG |
7761 | if (reclaim_order < alloc_order) |
7762 | goto kswapd_try_sleep; | |
1da177e4 | 7763 | } |
b0a8cc58 | 7764 | |
b698f0a1 | 7765 | tsk->flags &= ~(PF_MEMALLOC | PF_KSWAPD); |
71abdc15 | 7766 | |
1da177e4 LT |
7767 | return 0; |
7768 | } | |
7769 | ||
7770 | /* | |
5ecd9d40 DR |
7771 | * A zone is low on free memory or too fragmented for high-order memory. If |
7772 | * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's | |
7773 | * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim | |
7774 | * has failed or is not needed, still wake up kcompactd if only compaction is | |
7775 | * needed. | |
1da177e4 | 7776 | */ |
5ecd9d40 | 7777 | void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, |
97a225e6 | 7778 | enum zone_type highest_zoneidx) |
1da177e4 LT |
7779 | { |
7780 | pg_data_t *pgdat; | |
5644e1fb | 7781 | enum zone_type curr_idx; |
1da177e4 | 7782 | |
6aa303de | 7783 | if (!managed_zone(zone)) |
1da177e4 LT |
7784 | return; |
7785 | ||
5ecd9d40 | 7786 | if (!cpuset_zone_allowed(zone, gfp_flags)) |
1da177e4 | 7787 | return; |
5644e1fb | 7788 | |
88f5acf8 | 7789 | pgdat = zone->zone_pgdat; |
97a225e6 | 7790 | curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); |
5644e1fb | 7791 | |
97a225e6 JK |
7792 | if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx) |
7793 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx); | |
5644e1fb QC |
7794 | |
7795 | if (READ_ONCE(pgdat->kswapd_order) < order) | |
7796 | WRITE_ONCE(pgdat->kswapd_order, order); | |
dffcac2c | 7797 | |
8d0986e2 | 7798 | if (!waitqueue_active(&pgdat->kswapd_wait)) |
1da177e4 | 7799 | return; |
e1a55637 | 7800 | |
5ecd9d40 DR |
7801 | /* Hopeless node, leave it to direct reclaim if possible */ |
7802 | if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || | |
97a225e6 JK |
7803 | (pgdat_balanced(pgdat, order, highest_zoneidx) && |
7804 | !pgdat_watermark_boosted(pgdat, highest_zoneidx))) { | |
5ecd9d40 DR |
7805 | /* |
7806 | * There may be plenty of free memory available, but it's too | |
7807 | * fragmented for high-order allocations. Wake up kcompactd | |
7808 | * and rely on compaction_suitable() to determine if it's | |
7809 | * needed. If it fails, it will defer subsequent attempts to | |
7810 | * ratelimit its work. | |
7811 | */ | |
7812 | if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) | |
97a225e6 | 7813 | wakeup_kcompactd(pgdat, order, highest_zoneidx); |
e716f2eb | 7814 | return; |
5ecd9d40 | 7815 | } |
88f5acf8 | 7816 | |
97a225e6 | 7817 | trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order, |
5ecd9d40 | 7818 | gfp_flags); |
8d0986e2 | 7819 | wake_up_interruptible(&pgdat->kswapd_wait); |
1da177e4 LT |
7820 | } |
7821 | ||
c6f37f12 | 7822 | #ifdef CONFIG_HIBERNATION |
1da177e4 | 7823 | /* |
7b51755c | 7824 | * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of |
d6277db4 RW |
7825 | * freed pages. |
7826 | * | |
7827 | * Rather than trying to age LRUs the aim is to preserve the overall | |
7828 | * LRU order by reclaiming preferentially | |
7829 | * inactive > active > active referenced > active mapped | |
1da177e4 | 7830 | */ |
7b51755c | 7831 | unsigned long shrink_all_memory(unsigned long nr_to_reclaim) |
1da177e4 | 7832 | { |
d6277db4 | 7833 | struct scan_control sc = { |
ee814fe2 | 7834 | .nr_to_reclaim = nr_to_reclaim, |
7b51755c | 7835 | .gfp_mask = GFP_HIGHUSER_MOVABLE, |
b2e18757 | 7836 | .reclaim_idx = MAX_NR_ZONES - 1, |
ee814fe2 | 7837 | .priority = DEF_PRIORITY, |
d6277db4 | 7838 | .may_writepage = 1, |
ee814fe2 JW |
7839 | .may_unmap = 1, |
7840 | .may_swap = 1, | |
7b51755c | 7841 | .hibernation_mode = 1, |
1da177e4 | 7842 | }; |
a09ed5e0 | 7843 | struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); |
7b51755c | 7844 | unsigned long nr_reclaimed; |
499118e9 | 7845 | unsigned int noreclaim_flag; |
1da177e4 | 7846 | |
d92a8cfc | 7847 | fs_reclaim_acquire(sc.gfp_mask); |
93781325 | 7848 | noreclaim_flag = memalloc_noreclaim_save(); |
1732d2b0 | 7849 | set_task_reclaim_state(current, &sc.reclaim_state); |
d6277db4 | 7850 | |
3115cd91 | 7851 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
d979677c | 7852 | |
1732d2b0 | 7853 | set_task_reclaim_state(current, NULL); |
499118e9 | 7854 | memalloc_noreclaim_restore(noreclaim_flag); |
93781325 | 7855 | fs_reclaim_release(sc.gfp_mask); |
d6277db4 | 7856 | |
7b51755c | 7857 | return nr_reclaimed; |
1da177e4 | 7858 | } |
c6f37f12 | 7859 | #endif /* CONFIG_HIBERNATION */ |
1da177e4 | 7860 | |
3218ae14 YG |
7861 | /* |
7862 | * This kswapd start function will be called by init and node-hot-add. | |
3218ae14 | 7863 | */ |
b87c517a | 7864 | void kswapd_run(int nid) |
3218ae14 YG |
7865 | { |
7866 | pg_data_t *pgdat = NODE_DATA(nid); | |
3218ae14 | 7867 | |
b4a0215e KW |
7868 | pgdat_kswapd_lock(pgdat); |
7869 | if (!pgdat->kswapd) { | |
7870 | pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); | |
7871 | if (IS_ERR(pgdat->kswapd)) { | |
7872 | /* failure at boot is fatal */ | |
7873 | BUG_ON(system_state < SYSTEM_RUNNING); | |
7874 | pr_err("Failed to start kswapd on node %d\n", nid); | |
7875 | pgdat->kswapd = NULL; | |
7876 | } | |
3218ae14 | 7877 | } |
b4a0215e | 7878 | pgdat_kswapd_unlock(pgdat); |
3218ae14 YG |
7879 | } |
7880 | ||
8fe23e05 | 7881 | /* |
d8adde17 | 7882 | * Called by memory hotplug when all memory in a node is offlined. Caller must |
e8da368a | 7883 | * be holding mem_hotplug_begin/done(). |
8fe23e05 DR |
7884 | */ |
7885 | void kswapd_stop(int nid) | |
7886 | { | |
b4a0215e KW |
7887 | pg_data_t *pgdat = NODE_DATA(nid); |
7888 | struct task_struct *kswapd; | |
8fe23e05 | 7889 | |
b4a0215e KW |
7890 | pgdat_kswapd_lock(pgdat); |
7891 | kswapd = pgdat->kswapd; | |
d8adde17 | 7892 | if (kswapd) { |
8fe23e05 | 7893 | kthread_stop(kswapd); |
b4a0215e | 7894 | pgdat->kswapd = NULL; |
d8adde17 | 7895 | } |
b4a0215e | 7896 | pgdat_kswapd_unlock(pgdat); |
8fe23e05 DR |
7897 | } |
7898 | ||
1da177e4 LT |
7899 | static int __init kswapd_init(void) |
7900 | { | |
6b700b5b | 7901 | int nid; |
69e05944 | 7902 | |
1da177e4 | 7903 | swap_setup(); |
48fb2e24 | 7904 | for_each_node_state(nid, N_MEMORY) |
3218ae14 | 7905 | kswapd_run(nid); |
1da177e4 LT |
7906 | return 0; |
7907 | } | |
7908 | ||
7909 | module_init(kswapd_init) | |
9eeff239 CL |
7910 | |
7911 | #ifdef CONFIG_NUMA | |
7912 | /* | |
a5f5f91d | 7913 | * Node reclaim mode |
9eeff239 | 7914 | * |
a5f5f91d | 7915 | * If non-zero call node_reclaim when the number of free pages falls below |
9eeff239 | 7916 | * the watermarks. |
9eeff239 | 7917 | */ |
a5f5f91d | 7918 | int node_reclaim_mode __read_mostly; |
9eeff239 | 7919 | |
a92f7126 | 7920 | /* |
a5f5f91d | 7921 | * Priority for NODE_RECLAIM. This determines the fraction of pages |
a92f7126 CL |
7922 | * of a node considered for each zone_reclaim. 4 scans 1/16th of |
7923 | * a zone. | |
7924 | */ | |
a5f5f91d | 7925 | #define NODE_RECLAIM_PRIORITY 4 |
a92f7126 | 7926 | |
9614634f | 7927 | /* |
a5f5f91d | 7928 | * Percentage of pages in a zone that must be unmapped for node_reclaim to |
9614634f CL |
7929 | * occur. |
7930 | */ | |
7931 | int sysctl_min_unmapped_ratio = 1; | |
7932 | ||
0ff38490 CL |
7933 | /* |
7934 | * If the number of slab pages in a zone grows beyond this percentage then | |
7935 | * slab reclaim needs to occur. | |
7936 | */ | |
7937 | int sysctl_min_slab_ratio = 5; | |
7938 | ||
11fb9989 | 7939 | static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) |
90afa5de | 7940 | { |
11fb9989 MG |
7941 | unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); |
7942 | unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + | |
7943 | node_page_state(pgdat, NR_ACTIVE_FILE); | |
90afa5de MG |
7944 | |
7945 | /* | |
7946 | * It's possible for there to be more file mapped pages than | |
7947 | * accounted for by the pages on the file LRU lists because | |
7948 | * tmpfs pages accounted for as ANON can also be FILE_MAPPED | |
7949 | */ | |
7950 | return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; | |
7951 | } | |
7952 | ||
7953 | /* Work out how many page cache pages we can reclaim in this reclaim_mode */ | |
a5f5f91d | 7954 | static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) |
90afa5de | 7955 | { |
d031a157 AM |
7956 | unsigned long nr_pagecache_reclaimable; |
7957 | unsigned long delta = 0; | |
90afa5de MG |
7958 | |
7959 | /* | |
95bbc0c7 | 7960 | * If RECLAIM_UNMAP is set, then all file pages are considered |
90afa5de | 7961 | * potentially reclaimable. Otherwise, we have to worry about |
11fb9989 | 7962 | * pages like swapcache and node_unmapped_file_pages() provides |
90afa5de MG |
7963 | * a better estimate |
7964 | */ | |
a5f5f91d MG |
7965 | if (node_reclaim_mode & RECLAIM_UNMAP) |
7966 | nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); | |
90afa5de | 7967 | else |
a5f5f91d | 7968 | nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); |
90afa5de MG |
7969 | |
7970 | /* If we can't clean pages, remove dirty pages from consideration */ | |
a5f5f91d MG |
7971 | if (!(node_reclaim_mode & RECLAIM_WRITE)) |
7972 | delta += node_page_state(pgdat, NR_FILE_DIRTY); | |
90afa5de MG |
7973 | |
7974 | /* Watch for any possible underflows due to delta */ | |
7975 | if (unlikely(delta > nr_pagecache_reclaimable)) | |
7976 | delta = nr_pagecache_reclaimable; | |
7977 | ||
7978 | return nr_pagecache_reclaimable - delta; | |
7979 | } | |
7980 | ||
9eeff239 | 7981 | /* |
a5f5f91d | 7982 | * Try to free up some pages from this node through reclaim. |
9eeff239 | 7983 | */ |
a5f5f91d | 7984 | static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) |
9eeff239 | 7985 | { |
7fb2d46d | 7986 | /* Minimum pages needed in order to stay on node */ |
69e05944 | 7987 | const unsigned long nr_pages = 1 << order; |
9eeff239 | 7988 | struct task_struct *p = current; |
499118e9 | 7989 | unsigned int noreclaim_flag; |
179e9639 | 7990 | struct scan_control sc = { |
62b726c1 | 7991 | .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), |
f2f43e56 | 7992 | .gfp_mask = current_gfp_context(gfp_mask), |
bd2f6199 | 7993 | .order = order, |
a5f5f91d MG |
7994 | .priority = NODE_RECLAIM_PRIORITY, |
7995 | .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), | |
7996 | .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), | |
ee814fe2 | 7997 | .may_swap = 1, |
f2f43e56 | 7998 | .reclaim_idx = gfp_zone(gfp_mask), |
179e9639 | 7999 | }; |
57f29762 | 8000 | unsigned long pflags; |
9eeff239 | 8001 | |
132bb8cf YS |
8002 | trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order, |
8003 | sc.gfp_mask); | |
8004 | ||
9eeff239 | 8005 | cond_resched(); |
57f29762 | 8006 | psi_memstall_enter(&pflags); |
93781325 | 8007 | fs_reclaim_acquire(sc.gfp_mask); |
d4f7796e | 8008 | /* |
95bbc0c7 | 8009 | * We need to be able to allocate from the reserves for RECLAIM_UNMAP |
d4f7796e | 8010 | */ |
499118e9 | 8011 | noreclaim_flag = memalloc_noreclaim_save(); |
1732d2b0 | 8012 | set_task_reclaim_state(p, &sc.reclaim_state); |
c84db23c | 8013 | |
d8ff6fde ML |
8014 | if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages || |
8015 | node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) > pgdat->min_slab_pages) { | |
0ff38490 | 8016 | /* |
894befec | 8017 | * Free memory by calling shrink node with increasing |
0ff38490 CL |
8018 | * priorities until we have enough memory freed. |
8019 | */ | |
0ff38490 | 8020 | do { |
970a39a3 | 8021 | shrink_node(pgdat, &sc); |
9e3b2f8c | 8022 | } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); |
0ff38490 | 8023 | } |
c84db23c | 8024 | |
1732d2b0 | 8025 | set_task_reclaim_state(p, NULL); |
499118e9 | 8026 | memalloc_noreclaim_restore(noreclaim_flag); |
93781325 | 8027 | fs_reclaim_release(sc.gfp_mask); |
57f29762 | 8028 | psi_memstall_leave(&pflags); |
132bb8cf YS |
8029 | |
8030 | trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed); | |
8031 | ||
a79311c1 | 8032 | return sc.nr_reclaimed >= nr_pages; |
9eeff239 | 8033 | } |
179e9639 | 8034 | |
a5f5f91d | 8035 | int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) |
179e9639 | 8036 | { |
d773ed6b | 8037 | int ret; |
179e9639 AM |
8038 | |
8039 | /* | |
a5f5f91d | 8040 | * Node reclaim reclaims unmapped file backed pages and |
0ff38490 | 8041 | * slab pages if we are over the defined limits. |
34aa1330 | 8042 | * |
9614634f CL |
8043 | * A small portion of unmapped file backed pages is needed for |
8044 | * file I/O otherwise pages read by file I/O will be immediately | |
a5f5f91d MG |
8045 | * thrown out if the node is overallocated. So we do not reclaim |
8046 | * if less than a specified percentage of the node is used by | |
9614634f | 8047 | * unmapped file backed pages. |
179e9639 | 8048 | */ |
a5f5f91d | 8049 | if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && |
d42f3245 RG |
8050 | node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <= |
8051 | pgdat->min_slab_pages) | |
a5f5f91d | 8052 | return NODE_RECLAIM_FULL; |
179e9639 AM |
8053 | |
8054 | /* | |
d773ed6b | 8055 | * Do not scan if the allocation should not be delayed. |
179e9639 | 8056 | */ |
d0164adc | 8057 | if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) |
a5f5f91d | 8058 | return NODE_RECLAIM_NOSCAN; |
179e9639 AM |
8059 | |
8060 | /* | |
a5f5f91d | 8061 | * Only run node reclaim on the local node or on nodes that do not |
179e9639 AM |
8062 | * have associated processors. This will favor the local processor |
8063 | * over remote processors and spread off node memory allocations | |
8064 | * as wide as possible. | |
8065 | */ | |
a5f5f91d MG |
8066 | if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) |
8067 | return NODE_RECLAIM_NOSCAN; | |
d773ed6b | 8068 | |
a5f5f91d MG |
8069 | if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) |
8070 | return NODE_RECLAIM_NOSCAN; | |
fa5e084e | 8071 | |
a5f5f91d MG |
8072 | ret = __node_reclaim(pgdat, gfp_mask, order); |
8073 | clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); | |
d773ed6b | 8074 | |
24cf7251 MG |
8075 | if (!ret) |
8076 | count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); | |
8077 | ||
d773ed6b | 8078 | return ret; |
179e9639 | 8079 | } |
9eeff239 | 8080 | #endif |
894bc310 | 8081 | |
77414d19 MWO |
8082 | void check_move_unevictable_pages(struct pagevec *pvec) |
8083 | { | |
8084 | struct folio_batch fbatch; | |
8085 | unsigned i; | |
8086 | ||
8087 | folio_batch_init(&fbatch); | |
8088 | for (i = 0; i < pvec->nr; i++) { | |
8089 | struct page *page = pvec->pages[i]; | |
8090 | ||
8091 | if (PageTransTail(page)) | |
8092 | continue; | |
8093 | folio_batch_add(&fbatch, page_folio(page)); | |
8094 | } | |
8095 | check_move_unevictable_folios(&fbatch); | |
8096 | } | |
8097 | EXPORT_SYMBOL_GPL(check_move_unevictable_pages); | |
8098 | ||
89e004ea | 8099 | /** |
77414d19 MWO |
8100 | * check_move_unevictable_folios - Move evictable folios to appropriate zone |
8101 | * lru list | |
8102 | * @fbatch: Batch of lru folios to check. | |
89e004ea | 8103 | * |
77414d19 | 8104 | * Checks folios for evictability, if an evictable folio is in the unevictable |
64e3d12f | 8105 | * lru list, moves it to the appropriate evictable lru list. This function |
77414d19 | 8106 | * should be only used for lru folios. |
89e004ea | 8107 | */ |
77414d19 | 8108 | void check_move_unevictable_folios(struct folio_batch *fbatch) |
89e004ea | 8109 | { |
6168d0da | 8110 | struct lruvec *lruvec = NULL; |
24513264 HD |
8111 | int pgscanned = 0; |
8112 | int pgrescued = 0; | |
8113 | int i; | |
89e004ea | 8114 | |
77414d19 MWO |
8115 | for (i = 0; i < fbatch->nr; i++) { |
8116 | struct folio *folio = fbatch->folios[i]; | |
8117 | int nr_pages = folio_nr_pages(folio); | |
8d8869ca | 8118 | |
8d8869ca | 8119 | pgscanned += nr_pages; |
89e004ea | 8120 | |
77414d19 MWO |
8121 | /* block memcg migration while the folio moves between lrus */ |
8122 | if (!folio_test_clear_lru(folio)) | |
d25b5bd8 AS |
8123 | continue; |
8124 | ||
0de340cb | 8125 | lruvec = folio_lruvec_relock_irq(folio, lruvec); |
77414d19 MWO |
8126 | if (folio_evictable(folio) && folio_test_unevictable(folio)) { |
8127 | lruvec_del_folio(lruvec, folio); | |
8128 | folio_clear_unevictable(folio); | |
8129 | lruvec_add_folio(lruvec, folio); | |
8d8869ca | 8130 | pgrescued += nr_pages; |
89e004ea | 8131 | } |
77414d19 | 8132 | folio_set_lru(folio); |
24513264 | 8133 | } |
89e004ea | 8134 | |
6168d0da | 8135 | if (lruvec) { |
24513264 HD |
8136 | __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); |
8137 | __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); | |
6168d0da | 8138 | unlock_page_lruvec_irq(lruvec); |
d25b5bd8 AS |
8139 | } else if (pgscanned) { |
8140 | count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); | |
89e004ea | 8141 | } |
89e004ea | 8142 | } |
77414d19 | 8143 | EXPORT_SYMBOL_GPL(check_move_unevictable_folios); |