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