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