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