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> | |
29 | #include <linux/buffer_head.h> /* for try_to_release_page(), | |
30 | buffer_heads_over_limit */ | |
31 | #include <linux/mm_inline.h> | |
1da177e4 LT |
32 | #include <linux/backing-dev.h> |
33 | #include <linux/rmap.h> | |
34 | #include <linux/topology.h> | |
35 | #include <linux/cpu.h> | |
36 | #include <linux/cpuset.h> | |
3e7d3449 | 37 | #include <linux/compaction.h> |
1da177e4 LT |
38 | #include <linux/notifier.h> |
39 | #include <linux/rwsem.h> | |
248a0301 | 40 | #include <linux/delay.h> |
3218ae14 | 41 | #include <linux/kthread.h> |
7dfb7103 | 42 | #include <linux/freezer.h> |
66e1707b | 43 | #include <linux/memcontrol.h> |
26aa2d19 | 44 | #include <linux/migrate.h> |
873b4771 | 45 | #include <linux/delayacct.h> |
af936a16 | 46 | #include <linux/sysctl.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> |
1da177e4 LT |
53 | |
54 | #include <asm/tlbflush.h> | |
55 | #include <asm/div64.h> | |
56 | ||
57 | #include <linux/swapops.h> | |
117aad1e | 58 | #include <linux/balloon_compaction.h> |
c574bbe9 | 59 | #include <linux/sched/sysctl.h> |
1da177e4 | 60 | |
0f8053a5 | 61 | #include "internal.h" |
014bb1de | 62 | #include "swap.h" |
0f8053a5 | 63 | |
33906bc5 MG |
64 | #define CREATE_TRACE_POINTS |
65 | #include <trace/events/vmscan.h> | |
66 | ||
1da177e4 | 67 | struct scan_control { |
22fba335 KM |
68 | /* How many pages shrink_list() should reclaim */ |
69 | unsigned long nr_to_reclaim; | |
70 | ||
ee814fe2 JW |
71 | /* |
72 | * Nodemask of nodes allowed by the caller. If NULL, all nodes | |
73 | * are scanned. | |
74 | */ | |
75 | nodemask_t *nodemask; | |
9e3b2f8c | 76 | |
f16015fb JW |
77 | /* |
78 | * The memory cgroup that hit its limit and as a result is the | |
79 | * primary target of this reclaim invocation. | |
80 | */ | |
81 | struct mem_cgroup *target_mem_cgroup; | |
66e1707b | 82 | |
7cf111bc JW |
83 | /* |
84 | * Scan pressure balancing between anon and file LRUs | |
85 | */ | |
86 | unsigned long anon_cost; | |
87 | unsigned long file_cost; | |
88 | ||
b91ac374 JW |
89 | /* Can active pages be deactivated as part of reclaim? */ |
90 | #define DEACTIVATE_ANON 1 | |
91 | #define DEACTIVATE_FILE 2 | |
92 | unsigned int may_deactivate:2; | |
93 | unsigned int force_deactivate:1; | |
94 | unsigned int skipped_deactivate:1; | |
95 | ||
1276ad68 | 96 | /* Writepage batching in laptop mode; RECLAIM_WRITE */ |
ee814fe2 JW |
97 | unsigned int may_writepage:1; |
98 | ||
99 | /* Can mapped pages be reclaimed? */ | |
100 | unsigned int may_unmap:1; | |
101 | ||
102 | /* Can pages be swapped as part of reclaim? */ | |
103 | unsigned int may_swap:1; | |
104 | ||
d6622f63 | 105 | /* |
f56ce412 JW |
106 | * Cgroup memory below memory.low is protected as long as we |
107 | * don't threaten to OOM. If any cgroup is reclaimed at | |
108 | * reduced force or passed over entirely due to its memory.low | |
109 | * setting (memcg_low_skipped), and nothing is reclaimed as a | |
110 | * result, then go back for one more cycle that reclaims the protected | |
111 | * memory (memcg_low_reclaim) to avert OOM. | |
d6622f63 YX |
112 | */ |
113 | unsigned int memcg_low_reclaim:1; | |
114 | unsigned int memcg_low_skipped:1; | |
241994ed | 115 | |
ee814fe2 JW |
116 | unsigned int hibernation_mode:1; |
117 | ||
118 | /* One of the zones is ready for compaction */ | |
119 | unsigned int compaction_ready:1; | |
120 | ||
b91ac374 JW |
121 | /* There is easily reclaimable cold cache in the current node */ |
122 | unsigned int cache_trim_mode:1; | |
123 | ||
53138cea JW |
124 | /* The file pages on the current node are dangerously low */ |
125 | unsigned int file_is_tiny:1; | |
126 | ||
26aa2d19 DH |
127 | /* Always discard instead of demoting to lower tier memory */ |
128 | unsigned int no_demotion:1; | |
129 | ||
bb451fdf GT |
130 | /* Allocation order */ |
131 | s8 order; | |
132 | ||
133 | /* Scan (total_size >> priority) pages at once */ | |
134 | s8 priority; | |
135 | ||
136 | /* The highest zone to isolate pages for reclaim from */ | |
137 | s8 reclaim_idx; | |
138 | ||
139 | /* This context's GFP mask */ | |
140 | gfp_t gfp_mask; | |
141 | ||
ee814fe2 JW |
142 | /* Incremented by the number of inactive pages that were scanned */ |
143 | unsigned long nr_scanned; | |
144 | ||
145 | /* Number of pages freed so far during a call to shrink_zones() */ | |
146 | unsigned long nr_reclaimed; | |
d108c772 AR |
147 | |
148 | struct { | |
149 | unsigned int dirty; | |
150 | unsigned int unqueued_dirty; | |
151 | unsigned int congested; | |
152 | unsigned int writeback; | |
153 | unsigned int immediate; | |
154 | unsigned int file_taken; | |
155 | unsigned int taken; | |
156 | } nr; | |
e5ca8071 YS |
157 | |
158 | /* for recording the reclaimed slab by now */ | |
159 | struct reclaim_state reclaim_state; | |
1da177e4 LT |
160 | }; |
161 | ||
1da177e4 LT |
162 | #ifdef ARCH_HAS_PREFETCHW |
163 | #define prefetchw_prev_lru_page(_page, _base, _field) \ | |
164 | do { \ | |
165 | if ((_page)->lru.prev != _base) { \ | |
166 | struct page *prev; \ | |
167 | \ | |
168 | prev = lru_to_page(&(_page->lru)); \ | |
169 | prefetchw(&prev->_field); \ | |
170 | } \ | |
171 | } while (0) | |
172 | #else | |
173 | #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) | |
174 | #endif | |
175 | ||
176 | /* | |
c843966c | 177 | * From 0 .. 200. Higher means more swappy. |
1da177e4 LT |
178 | */ |
179 | int vm_swappiness = 60; | |
1da177e4 | 180 | |
0a432dcb YS |
181 | static void set_task_reclaim_state(struct task_struct *task, |
182 | struct reclaim_state *rs) | |
183 | { | |
184 | /* Check for an overwrite */ | |
185 | WARN_ON_ONCE(rs && task->reclaim_state); | |
186 | ||
187 | /* Check for the nulling of an already-nulled member */ | |
188 | WARN_ON_ONCE(!rs && !task->reclaim_state); | |
189 | ||
190 | task->reclaim_state = rs; | |
191 | } | |
192 | ||
1da177e4 LT |
193 | static LIST_HEAD(shrinker_list); |
194 | static DECLARE_RWSEM(shrinker_rwsem); | |
195 | ||
0a432dcb | 196 | #ifdef CONFIG_MEMCG |
a2fb1261 | 197 | static int shrinker_nr_max; |
2bfd3637 | 198 | |
3c6f17e6 | 199 | /* The shrinker_info is expanded in a batch of BITS_PER_LONG */ |
a2fb1261 YS |
200 | static inline int shrinker_map_size(int nr_items) |
201 | { | |
202 | return (DIV_ROUND_UP(nr_items, BITS_PER_LONG) * sizeof(unsigned long)); | |
203 | } | |
2bfd3637 | 204 | |
3c6f17e6 YS |
205 | static inline int shrinker_defer_size(int nr_items) |
206 | { | |
207 | return (round_up(nr_items, BITS_PER_LONG) * sizeof(atomic_long_t)); | |
208 | } | |
209 | ||
468ab843 YS |
210 | static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg, |
211 | int nid) | |
212 | { | |
213 | return rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_info, | |
214 | lockdep_is_held(&shrinker_rwsem)); | |
215 | } | |
216 | ||
e4262c4f | 217 | static int expand_one_shrinker_info(struct mem_cgroup *memcg, |
3c6f17e6 YS |
218 | int map_size, int defer_size, |
219 | int old_map_size, int old_defer_size) | |
2bfd3637 | 220 | { |
e4262c4f | 221 | struct shrinker_info *new, *old; |
2bfd3637 YS |
222 | struct mem_cgroup_per_node *pn; |
223 | int nid; | |
3c6f17e6 | 224 | int size = map_size + defer_size; |
2bfd3637 | 225 | |
2bfd3637 YS |
226 | for_each_node(nid) { |
227 | pn = memcg->nodeinfo[nid]; | |
468ab843 | 228 | old = shrinker_info_protected(memcg, nid); |
2bfd3637 YS |
229 | /* Not yet online memcg */ |
230 | if (!old) | |
231 | return 0; | |
232 | ||
233 | new = kvmalloc_node(sizeof(*new) + size, GFP_KERNEL, nid); | |
234 | if (!new) | |
235 | return -ENOMEM; | |
236 | ||
3c6f17e6 YS |
237 | new->nr_deferred = (atomic_long_t *)(new + 1); |
238 | new->map = (void *)new->nr_deferred + defer_size; | |
239 | ||
240 | /* map: set all old bits, clear all new bits */ | |
241 | memset(new->map, (int)0xff, old_map_size); | |
242 | memset((void *)new->map + old_map_size, 0, map_size - old_map_size); | |
243 | /* nr_deferred: copy old values, clear all new values */ | |
244 | memcpy(new->nr_deferred, old->nr_deferred, old_defer_size); | |
245 | memset((void *)new->nr_deferred + old_defer_size, 0, | |
246 | defer_size - old_defer_size); | |
2bfd3637 | 247 | |
e4262c4f | 248 | rcu_assign_pointer(pn->shrinker_info, new); |
72673e86 | 249 | kvfree_rcu(old, rcu); |
2bfd3637 YS |
250 | } |
251 | ||
252 | return 0; | |
253 | } | |
254 | ||
e4262c4f | 255 | void free_shrinker_info(struct mem_cgroup *memcg) |
2bfd3637 YS |
256 | { |
257 | struct mem_cgroup_per_node *pn; | |
e4262c4f | 258 | struct shrinker_info *info; |
2bfd3637 YS |
259 | int nid; |
260 | ||
2bfd3637 YS |
261 | for_each_node(nid) { |
262 | pn = memcg->nodeinfo[nid]; | |
e4262c4f YS |
263 | info = rcu_dereference_protected(pn->shrinker_info, true); |
264 | kvfree(info); | |
265 | rcu_assign_pointer(pn->shrinker_info, NULL); | |
2bfd3637 YS |
266 | } |
267 | } | |
268 | ||
e4262c4f | 269 | int alloc_shrinker_info(struct mem_cgroup *memcg) |
2bfd3637 | 270 | { |
e4262c4f | 271 | struct shrinker_info *info; |
2bfd3637 | 272 | int nid, size, ret = 0; |
3c6f17e6 | 273 | int map_size, defer_size = 0; |
2bfd3637 | 274 | |
d27cf2aa | 275 | down_write(&shrinker_rwsem); |
3c6f17e6 YS |
276 | map_size = shrinker_map_size(shrinker_nr_max); |
277 | defer_size = shrinker_defer_size(shrinker_nr_max); | |
278 | size = map_size + defer_size; | |
2bfd3637 | 279 | for_each_node(nid) { |
e4262c4f YS |
280 | info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid); |
281 | if (!info) { | |
282 | free_shrinker_info(memcg); | |
2bfd3637 YS |
283 | ret = -ENOMEM; |
284 | break; | |
285 | } | |
3c6f17e6 YS |
286 | info->nr_deferred = (atomic_long_t *)(info + 1); |
287 | info->map = (void *)info->nr_deferred + defer_size; | |
e4262c4f | 288 | rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info); |
2bfd3637 | 289 | } |
d27cf2aa | 290 | up_write(&shrinker_rwsem); |
2bfd3637 YS |
291 | |
292 | return ret; | |
293 | } | |
294 | ||
3c6f17e6 YS |
295 | static inline bool need_expand(int nr_max) |
296 | { | |
297 | return round_up(nr_max, BITS_PER_LONG) > | |
298 | round_up(shrinker_nr_max, BITS_PER_LONG); | |
299 | } | |
300 | ||
e4262c4f | 301 | static int expand_shrinker_info(int new_id) |
2bfd3637 | 302 | { |
3c6f17e6 | 303 | int ret = 0; |
a2fb1261 | 304 | int new_nr_max = new_id + 1; |
3c6f17e6 YS |
305 | int map_size, defer_size = 0; |
306 | int old_map_size, old_defer_size = 0; | |
2bfd3637 YS |
307 | struct mem_cgroup *memcg; |
308 | ||
3c6f17e6 | 309 | if (!need_expand(new_nr_max)) |
a2fb1261 | 310 | goto out; |
2bfd3637 | 311 | |
2bfd3637 | 312 | if (!root_mem_cgroup) |
d27cf2aa YS |
313 | goto out; |
314 | ||
315 | lockdep_assert_held(&shrinker_rwsem); | |
2bfd3637 | 316 | |
3c6f17e6 YS |
317 | map_size = shrinker_map_size(new_nr_max); |
318 | defer_size = shrinker_defer_size(new_nr_max); | |
319 | old_map_size = shrinker_map_size(shrinker_nr_max); | |
320 | old_defer_size = shrinker_defer_size(shrinker_nr_max); | |
321 | ||
2bfd3637 YS |
322 | memcg = mem_cgroup_iter(NULL, NULL, NULL); |
323 | do { | |
3c6f17e6 YS |
324 | ret = expand_one_shrinker_info(memcg, map_size, defer_size, |
325 | old_map_size, old_defer_size); | |
2bfd3637 YS |
326 | if (ret) { |
327 | mem_cgroup_iter_break(NULL, memcg); | |
d27cf2aa | 328 | goto out; |
2bfd3637 YS |
329 | } |
330 | } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); | |
d27cf2aa | 331 | out: |
2bfd3637 | 332 | if (!ret) |
a2fb1261 | 333 | shrinker_nr_max = new_nr_max; |
d27cf2aa | 334 | |
2bfd3637 YS |
335 | return ret; |
336 | } | |
337 | ||
338 | void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) | |
339 | { | |
340 | if (shrinker_id >= 0 && memcg && !mem_cgroup_is_root(memcg)) { | |
e4262c4f | 341 | struct shrinker_info *info; |
2bfd3637 YS |
342 | |
343 | rcu_read_lock(); | |
e4262c4f | 344 | info = rcu_dereference(memcg->nodeinfo[nid]->shrinker_info); |
2bfd3637 YS |
345 | /* Pairs with smp mb in shrink_slab() */ |
346 | smp_mb__before_atomic(); | |
e4262c4f | 347 | set_bit(shrinker_id, info->map); |
2bfd3637 YS |
348 | rcu_read_unlock(); |
349 | } | |
350 | } | |
351 | ||
b4c2b231 | 352 | static DEFINE_IDR(shrinker_idr); |
b4c2b231 KT |
353 | |
354 | static int prealloc_memcg_shrinker(struct shrinker *shrinker) | |
355 | { | |
356 | int id, ret = -ENOMEM; | |
357 | ||
476b30a0 YS |
358 | if (mem_cgroup_disabled()) |
359 | return -ENOSYS; | |
360 | ||
b4c2b231 KT |
361 | down_write(&shrinker_rwsem); |
362 | /* This may call shrinker, so it must use down_read_trylock() */ | |
41ca668a | 363 | id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL); |
b4c2b231 KT |
364 | if (id < 0) |
365 | goto unlock; | |
366 | ||
0a4465d3 | 367 | if (id >= shrinker_nr_max) { |
e4262c4f | 368 | if (expand_shrinker_info(id)) { |
0a4465d3 KT |
369 | idr_remove(&shrinker_idr, id); |
370 | goto unlock; | |
371 | } | |
0a4465d3 | 372 | } |
b4c2b231 KT |
373 | shrinker->id = id; |
374 | ret = 0; | |
375 | unlock: | |
376 | up_write(&shrinker_rwsem); | |
377 | return ret; | |
378 | } | |
379 | ||
380 | static void unregister_memcg_shrinker(struct shrinker *shrinker) | |
381 | { | |
382 | int id = shrinker->id; | |
383 | ||
384 | BUG_ON(id < 0); | |
385 | ||
41ca668a YS |
386 | lockdep_assert_held(&shrinker_rwsem); |
387 | ||
b4c2b231 | 388 | idr_remove(&shrinker_idr, id); |
b4c2b231 | 389 | } |
b4c2b231 | 390 | |
86750830 YS |
391 | static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker, |
392 | struct mem_cgroup *memcg) | |
393 | { | |
394 | struct shrinker_info *info; | |
395 | ||
396 | info = shrinker_info_protected(memcg, nid); | |
397 | return atomic_long_xchg(&info->nr_deferred[shrinker->id], 0); | |
398 | } | |
399 | ||
400 | static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker, | |
401 | struct mem_cgroup *memcg) | |
402 | { | |
403 | struct shrinker_info *info; | |
404 | ||
405 | info = shrinker_info_protected(memcg, nid); | |
406 | return atomic_long_add_return(nr, &info->nr_deferred[shrinker->id]); | |
407 | } | |
408 | ||
a178015c YS |
409 | void reparent_shrinker_deferred(struct mem_cgroup *memcg) |
410 | { | |
411 | int i, nid; | |
412 | long nr; | |
413 | struct mem_cgroup *parent; | |
414 | struct shrinker_info *child_info, *parent_info; | |
415 | ||
416 | parent = parent_mem_cgroup(memcg); | |
417 | if (!parent) | |
418 | parent = root_mem_cgroup; | |
419 | ||
420 | /* Prevent from concurrent shrinker_info expand */ | |
421 | down_read(&shrinker_rwsem); | |
422 | for_each_node(nid) { | |
423 | child_info = shrinker_info_protected(memcg, nid); | |
424 | parent_info = shrinker_info_protected(parent, nid); | |
425 | for (i = 0; i < shrinker_nr_max; i++) { | |
426 | nr = atomic_long_read(&child_info->nr_deferred[i]); | |
427 | atomic_long_add(nr, &parent_info->nr_deferred[i]); | |
428 | } | |
429 | } | |
430 | up_read(&shrinker_rwsem); | |
431 | } | |
432 | ||
b5ead35e | 433 | static bool cgroup_reclaim(struct scan_control *sc) |
89b5fae5 | 434 | { |
b5ead35e | 435 | return sc->target_mem_cgroup; |
89b5fae5 | 436 | } |
97c9341f TH |
437 | |
438 | /** | |
b5ead35e | 439 | * writeback_throttling_sane - is the usual dirty throttling mechanism available? |
97c9341f TH |
440 | * @sc: scan_control in question |
441 | * | |
442 | * The normal page dirty throttling mechanism in balance_dirty_pages() is | |
443 | * completely broken with the legacy memcg and direct stalling in | |
444 | * shrink_page_list() is used for throttling instead, which lacks all the | |
445 | * niceties such as fairness, adaptive pausing, bandwidth proportional | |
446 | * allocation and configurability. | |
447 | * | |
448 | * This function tests whether the vmscan currently in progress can assume | |
449 | * that the normal dirty throttling mechanism is operational. | |
450 | */ | |
b5ead35e | 451 | static bool writeback_throttling_sane(struct scan_control *sc) |
97c9341f | 452 | { |
b5ead35e | 453 | if (!cgroup_reclaim(sc)) |
97c9341f TH |
454 | return true; |
455 | #ifdef CONFIG_CGROUP_WRITEBACK | |
69234ace | 456 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
97c9341f TH |
457 | return true; |
458 | #endif | |
459 | return false; | |
460 | } | |
91a45470 | 461 | #else |
0a432dcb YS |
462 | static int prealloc_memcg_shrinker(struct shrinker *shrinker) |
463 | { | |
476b30a0 | 464 | return -ENOSYS; |
0a432dcb YS |
465 | } |
466 | ||
467 | static void unregister_memcg_shrinker(struct shrinker *shrinker) | |
468 | { | |
469 | } | |
470 | ||
86750830 YS |
471 | static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker, |
472 | struct mem_cgroup *memcg) | |
473 | { | |
474 | return 0; | |
475 | } | |
476 | ||
477 | static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker, | |
478 | struct mem_cgroup *memcg) | |
479 | { | |
480 | return 0; | |
481 | } | |
482 | ||
b5ead35e | 483 | static bool cgroup_reclaim(struct scan_control *sc) |
89b5fae5 | 484 | { |
b5ead35e | 485 | return false; |
89b5fae5 | 486 | } |
97c9341f | 487 | |
b5ead35e | 488 | static bool writeback_throttling_sane(struct scan_control *sc) |
97c9341f TH |
489 | { |
490 | return true; | |
491 | } | |
91a45470 KH |
492 | #endif |
493 | ||
86750830 YS |
494 | static long xchg_nr_deferred(struct shrinker *shrinker, |
495 | struct shrink_control *sc) | |
496 | { | |
497 | int nid = sc->nid; | |
498 | ||
499 | if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) | |
500 | nid = 0; | |
501 | ||
502 | if (sc->memcg && | |
503 | (shrinker->flags & SHRINKER_MEMCG_AWARE)) | |
504 | return xchg_nr_deferred_memcg(nid, shrinker, | |
505 | sc->memcg); | |
506 | ||
507 | return atomic_long_xchg(&shrinker->nr_deferred[nid], 0); | |
508 | } | |
509 | ||
510 | ||
511 | static long add_nr_deferred(long nr, struct shrinker *shrinker, | |
512 | struct shrink_control *sc) | |
513 | { | |
514 | int nid = sc->nid; | |
515 | ||
516 | if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) | |
517 | nid = 0; | |
518 | ||
519 | if (sc->memcg && | |
520 | (shrinker->flags & SHRINKER_MEMCG_AWARE)) | |
521 | return add_nr_deferred_memcg(nr, nid, shrinker, | |
522 | sc->memcg); | |
523 | ||
524 | return atomic_long_add_return(nr, &shrinker->nr_deferred[nid]); | |
525 | } | |
526 | ||
26aa2d19 DH |
527 | static bool can_demote(int nid, struct scan_control *sc) |
528 | { | |
20b51af1 HY |
529 | if (!numa_demotion_enabled) |
530 | return false; | |
3a235693 DH |
531 | if (sc) { |
532 | if (sc->no_demotion) | |
533 | return false; | |
534 | /* It is pointless to do demotion in memcg reclaim */ | |
535 | if (cgroup_reclaim(sc)) | |
536 | return false; | |
537 | } | |
26aa2d19 DH |
538 | if (next_demotion_node(nid) == NUMA_NO_NODE) |
539 | return false; | |
540 | ||
20b51af1 | 541 | return true; |
26aa2d19 DH |
542 | } |
543 | ||
a2a36488 KB |
544 | static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, |
545 | int nid, | |
546 | struct scan_control *sc) | |
547 | { | |
548 | if (memcg == NULL) { | |
549 | /* | |
550 | * For non-memcg reclaim, is there | |
551 | * space in any swap device? | |
552 | */ | |
553 | if (get_nr_swap_pages() > 0) | |
554 | return true; | |
555 | } else { | |
556 | /* Is the memcg below its swap limit? */ | |
557 | if (mem_cgroup_get_nr_swap_pages(memcg) > 0) | |
558 | return true; | |
559 | } | |
560 | ||
561 | /* | |
562 | * The page can not be swapped. | |
563 | * | |
564 | * Can it be reclaimed from this node via demotion? | |
565 | */ | |
566 | return can_demote(nid, sc); | |
567 | } | |
568 | ||
5a1c84b4 MG |
569 | /* |
570 | * This misses isolated pages which are not accounted for to save counters. | |
571 | * As the data only determines if reclaim or compaction continues, it is | |
572 | * not expected that isolated pages will be a dominating factor. | |
573 | */ | |
574 | unsigned long zone_reclaimable_pages(struct zone *zone) | |
575 | { | |
576 | unsigned long nr; | |
577 | ||
578 | nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + | |
579 | zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); | |
a2a36488 | 580 | if (can_reclaim_anon_pages(NULL, zone_to_nid(zone), NULL)) |
5a1c84b4 MG |
581 | nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + |
582 | zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); | |
583 | ||
584 | return nr; | |
585 | } | |
586 | ||
fd538803 MH |
587 | /** |
588 | * lruvec_lru_size - Returns the number of pages on the given LRU list. | |
589 | * @lruvec: lru vector | |
590 | * @lru: lru to use | |
8b3a899a | 591 | * @zone_idx: zones to consider (use MAX_NR_ZONES - 1 for the whole LRU list) |
fd538803 | 592 | */ |
2091339d YZ |
593 | static unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, |
594 | int zone_idx) | |
c9f299d9 | 595 | { |
de3b0150 | 596 | unsigned long size = 0; |
fd538803 MH |
597 | int zid; |
598 | ||
8b3a899a | 599 | for (zid = 0; zid <= zone_idx; zid++) { |
fd538803 | 600 | struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; |
c9f299d9 | 601 | |
fd538803 MH |
602 | if (!managed_zone(zone)) |
603 | continue; | |
604 | ||
605 | if (!mem_cgroup_disabled()) | |
de3b0150 | 606 | size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid); |
fd538803 | 607 | else |
de3b0150 | 608 | size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru); |
fd538803 | 609 | } |
de3b0150 | 610 | return size; |
b4536f0c MH |
611 | } |
612 | ||
1da177e4 | 613 | /* |
1d3d4437 | 614 | * Add a shrinker callback to be called from the vm. |
1da177e4 | 615 | */ |
8e04944f | 616 | int prealloc_shrinker(struct shrinker *shrinker) |
1da177e4 | 617 | { |
476b30a0 YS |
618 | unsigned int size; |
619 | int err; | |
620 | ||
621 | if (shrinker->flags & SHRINKER_MEMCG_AWARE) { | |
622 | err = prealloc_memcg_shrinker(shrinker); | |
623 | if (err != -ENOSYS) | |
624 | return err; | |
1d3d4437 | 625 | |
476b30a0 YS |
626 | shrinker->flags &= ~SHRINKER_MEMCG_AWARE; |
627 | } | |
628 | ||
629 | size = sizeof(*shrinker->nr_deferred); | |
1d3d4437 GC |
630 | if (shrinker->flags & SHRINKER_NUMA_AWARE) |
631 | size *= nr_node_ids; | |
632 | ||
633 | shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); | |
634 | if (!shrinker->nr_deferred) | |
635 | return -ENOMEM; | |
b4c2b231 | 636 | |
8e04944f TH |
637 | return 0; |
638 | } | |
639 | ||
640 | void free_prealloced_shrinker(struct shrinker *shrinker) | |
641 | { | |
41ca668a YS |
642 | if (shrinker->flags & SHRINKER_MEMCG_AWARE) { |
643 | down_write(&shrinker_rwsem); | |
b4c2b231 | 644 | unregister_memcg_shrinker(shrinker); |
41ca668a | 645 | up_write(&shrinker_rwsem); |
476b30a0 | 646 | return; |
41ca668a | 647 | } |
b4c2b231 | 648 | |
8e04944f TH |
649 | kfree(shrinker->nr_deferred); |
650 | shrinker->nr_deferred = NULL; | |
651 | } | |
1d3d4437 | 652 | |
8e04944f TH |
653 | void register_shrinker_prepared(struct shrinker *shrinker) |
654 | { | |
8e1f936b RR |
655 | down_write(&shrinker_rwsem); |
656 | list_add_tail(&shrinker->list, &shrinker_list); | |
41ca668a | 657 | shrinker->flags |= SHRINKER_REGISTERED; |
8e1f936b | 658 | up_write(&shrinker_rwsem); |
8e04944f TH |
659 | } |
660 | ||
661 | int register_shrinker(struct shrinker *shrinker) | |
662 | { | |
663 | int err = prealloc_shrinker(shrinker); | |
664 | ||
665 | if (err) | |
666 | return err; | |
667 | register_shrinker_prepared(shrinker); | |
1d3d4437 | 668 | return 0; |
1da177e4 | 669 | } |
8e1f936b | 670 | EXPORT_SYMBOL(register_shrinker); |
1da177e4 LT |
671 | |
672 | /* | |
673 | * Remove one | |
674 | */ | |
8e1f936b | 675 | void unregister_shrinker(struct shrinker *shrinker) |
1da177e4 | 676 | { |
41ca668a | 677 | if (!(shrinker->flags & SHRINKER_REGISTERED)) |
bb422a73 | 678 | return; |
41ca668a | 679 | |
1da177e4 LT |
680 | down_write(&shrinker_rwsem); |
681 | list_del(&shrinker->list); | |
41ca668a YS |
682 | shrinker->flags &= ~SHRINKER_REGISTERED; |
683 | if (shrinker->flags & SHRINKER_MEMCG_AWARE) | |
684 | unregister_memcg_shrinker(shrinker); | |
1da177e4 | 685 | up_write(&shrinker_rwsem); |
41ca668a | 686 | |
ae393321 | 687 | kfree(shrinker->nr_deferred); |
bb422a73 | 688 | shrinker->nr_deferred = NULL; |
1da177e4 | 689 | } |
8e1f936b | 690 | EXPORT_SYMBOL(unregister_shrinker); |
1da177e4 | 691 | |
880121be CK |
692 | /** |
693 | * synchronize_shrinkers - Wait for all running shrinkers to complete. | |
694 | * | |
695 | * This is equivalent to calling unregister_shrink() and register_shrinker(), | |
696 | * but atomically and with less overhead. This is useful to guarantee that all | |
697 | * shrinker invocations have seen an update, before freeing memory, similar to | |
698 | * rcu. | |
699 | */ | |
700 | void synchronize_shrinkers(void) | |
701 | { | |
702 | down_write(&shrinker_rwsem); | |
703 | up_write(&shrinker_rwsem); | |
704 | } | |
705 | EXPORT_SYMBOL(synchronize_shrinkers); | |
706 | ||
1da177e4 | 707 | #define SHRINK_BATCH 128 |
1d3d4437 | 708 | |
cb731d6c | 709 | static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, |
9092c71b | 710 | struct shrinker *shrinker, int priority) |
1d3d4437 GC |
711 | { |
712 | unsigned long freed = 0; | |
713 | unsigned long long delta; | |
714 | long total_scan; | |
d5bc5fd3 | 715 | long freeable; |
1d3d4437 GC |
716 | long nr; |
717 | long new_nr; | |
1d3d4437 GC |
718 | long batch_size = shrinker->batch ? shrinker->batch |
719 | : SHRINK_BATCH; | |
5f33a080 | 720 | long scanned = 0, next_deferred; |
1d3d4437 | 721 | |
d5bc5fd3 | 722 | freeable = shrinker->count_objects(shrinker, shrinkctl); |
9b996468 KT |
723 | if (freeable == 0 || freeable == SHRINK_EMPTY) |
724 | return freeable; | |
1d3d4437 GC |
725 | |
726 | /* | |
727 | * copy the current shrinker scan count into a local variable | |
728 | * and zero it so that other concurrent shrinker invocations | |
729 | * don't also do this scanning work. | |
730 | */ | |
86750830 | 731 | nr = xchg_nr_deferred(shrinker, shrinkctl); |
1d3d4437 | 732 | |
4b85afbd JW |
733 | if (shrinker->seeks) { |
734 | delta = freeable >> priority; | |
735 | delta *= 4; | |
736 | do_div(delta, shrinker->seeks); | |
737 | } else { | |
738 | /* | |
739 | * These objects don't require any IO to create. Trim | |
740 | * them aggressively under memory pressure to keep | |
741 | * them from causing refetches in the IO caches. | |
742 | */ | |
743 | delta = freeable / 2; | |
744 | } | |
172b06c3 | 745 | |
18bb473e | 746 | total_scan = nr >> priority; |
1d3d4437 | 747 | total_scan += delta; |
18bb473e | 748 | total_scan = min(total_scan, (2 * freeable)); |
1d3d4437 GC |
749 | |
750 | trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, | |
9092c71b | 751 | freeable, delta, total_scan, priority); |
1d3d4437 | 752 | |
0b1fb40a VD |
753 | /* |
754 | * Normally, we should not scan less than batch_size objects in one | |
755 | * pass to avoid too frequent shrinker calls, but if the slab has less | |
756 | * than batch_size objects in total and we are really tight on memory, | |
757 | * we will try to reclaim all available objects, otherwise we can end | |
758 | * up failing allocations although there are plenty of reclaimable | |
759 | * objects spread over several slabs with usage less than the | |
760 | * batch_size. | |
761 | * | |
762 | * We detect the "tight on memory" situations by looking at the total | |
763 | * number of objects we want to scan (total_scan). If it is greater | |
d5bc5fd3 | 764 | * than the total number of objects on slab (freeable), we must be |
0b1fb40a VD |
765 | * scanning at high prio and therefore should try to reclaim as much as |
766 | * possible. | |
767 | */ | |
768 | while (total_scan >= batch_size || | |
d5bc5fd3 | 769 | total_scan >= freeable) { |
a0b02131 | 770 | unsigned long ret; |
0b1fb40a | 771 | unsigned long nr_to_scan = min(batch_size, total_scan); |
1d3d4437 | 772 | |
0b1fb40a | 773 | shrinkctl->nr_to_scan = nr_to_scan; |
d460acb5 | 774 | shrinkctl->nr_scanned = nr_to_scan; |
a0b02131 DC |
775 | ret = shrinker->scan_objects(shrinker, shrinkctl); |
776 | if (ret == SHRINK_STOP) | |
777 | break; | |
778 | freed += ret; | |
1d3d4437 | 779 | |
d460acb5 CW |
780 | count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); |
781 | total_scan -= shrinkctl->nr_scanned; | |
782 | scanned += shrinkctl->nr_scanned; | |
1d3d4437 GC |
783 | |
784 | cond_resched(); | |
785 | } | |
786 | ||
18bb473e YS |
787 | /* |
788 | * The deferred work is increased by any new work (delta) that wasn't | |
789 | * done, decreased by old deferred work that was done now. | |
790 | * | |
791 | * And it is capped to two times of the freeable items. | |
792 | */ | |
793 | next_deferred = max_t(long, (nr + delta - scanned), 0); | |
794 | next_deferred = min(next_deferred, (2 * freeable)); | |
795 | ||
1d3d4437 GC |
796 | /* |
797 | * move the unused scan count back into the shrinker in a | |
86750830 | 798 | * manner that handles concurrent updates. |
1d3d4437 | 799 | */ |
86750830 | 800 | new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl); |
1d3d4437 | 801 | |
8efb4b59 | 802 | trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan); |
1d3d4437 | 803 | return freed; |
1495f230 YH |
804 | } |
805 | ||
0a432dcb | 806 | #ifdef CONFIG_MEMCG |
b0dedc49 KT |
807 | static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, |
808 | struct mem_cgroup *memcg, int priority) | |
809 | { | |
e4262c4f | 810 | struct shrinker_info *info; |
b8e57efa KT |
811 | unsigned long ret, freed = 0; |
812 | int i; | |
b0dedc49 | 813 | |
0a432dcb | 814 | if (!mem_cgroup_online(memcg)) |
b0dedc49 KT |
815 | return 0; |
816 | ||
817 | if (!down_read_trylock(&shrinker_rwsem)) | |
818 | return 0; | |
819 | ||
468ab843 | 820 | info = shrinker_info_protected(memcg, nid); |
e4262c4f | 821 | if (unlikely(!info)) |
b0dedc49 KT |
822 | goto unlock; |
823 | ||
e4262c4f | 824 | for_each_set_bit(i, info->map, shrinker_nr_max) { |
b0dedc49 KT |
825 | struct shrink_control sc = { |
826 | .gfp_mask = gfp_mask, | |
827 | .nid = nid, | |
828 | .memcg = memcg, | |
829 | }; | |
830 | struct shrinker *shrinker; | |
831 | ||
832 | shrinker = idr_find(&shrinker_idr, i); | |
41ca668a | 833 | if (unlikely(!shrinker || !(shrinker->flags & SHRINKER_REGISTERED))) { |
7e010df5 | 834 | if (!shrinker) |
e4262c4f | 835 | clear_bit(i, info->map); |
b0dedc49 KT |
836 | continue; |
837 | } | |
838 | ||
0a432dcb YS |
839 | /* Call non-slab shrinkers even though kmem is disabled */ |
840 | if (!memcg_kmem_enabled() && | |
841 | !(shrinker->flags & SHRINKER_NONSLAB)) | |
842 | continue; | |
843 | ||
b0dedc49 | 844 | ret = do_shrink_slab(&sc, shrinker, priority); |
f90280d6 | 845 | if (ret == SHRINK_EMPTY) { |
e4262c4f | 846 | clear_bit(i, info->map); |
f90280d6 KT |
847 | /* |
848 | * After the shrinker reported that it had no objects to | |
849 | * free, but before we cleared the corresponding bit in | |
850 | * the memcg shrinker map, a new object might have been | |
851 | * added. To make sure, we have the bit set in this | |
852 | * case, we invoke the shrinker one more time and reset | |
853 | * the bit if it reports that it is not empty anymore. | |
854 | * The memory barrier here pairs with the barrier in | |
2bfd3637 | 855 | * set_shrinker_bit(): |
f90280d6 KT |
856 | * |
857 | * list_lru_add() shrink_slab_memcg() | |
858 | * list_add_tail() clear_bit() | |
859 | * <MB> <MB> | |
860 | * set_bit() do_shrink_slab() | |
861 | */ | |
862 | smp_mb__after_atomic(); | |
863 | ret = do_shrink_slab(&sc, shrinker, priority); | |
864 | if (ret == SHRINK_EMPTY) | |
865 | ret = 0; | |
866 | else | |
2bfd3637 | 867 | set_shrinker_bit(memcg, nid, i); |
f90280d6 | 868 | } |
b0dedc49 KT |
869 | freed += ret; |
870 | ||
871 | if (rwsem_is_contended(&shrinker_rwsem)) { | |
872 | freed = freed ? : 1; | |
873 | break; | |
874 | } | |
875 | } | |
876 | unlock: | |
877 | up_read(&shrinker_rwsem); | |
878 | return freed; | |
879 | } | |
0a432dcb | 880 | #else /* CONFIG_MEMCG */ |
b0dedc49 KT |
881 | static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, |
882 | struct mem_cgroup *memcg, int priority) | |
883 | { | |
884 | return 0; | |
885 | } | |
0a432dcb | 886 | #endif /* CONFIG_MEMCG */ |
b0dedc49 | 887 | |
6b4f7799 | 888 | /** |
cb731d6c | 889 | * shrink_slab - shrink slab caches |
6b4f7799 JW |
890 | * @gfp_mask: allocation context |
891 | * @nid: node whose slab caches to target | |
cb731d6c | 892 | * @memcg: memory cgroup whose slab caches to target |
9092c71b | 893 | * @priority: the reclaim priority |
1da177e4 | 894 | * |
6b4f7799 | 895 | * Call the shrink functions to age shrinkable caches. |
1da177e4 | 896 | * |
6b4f7799 JW |
897 | * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, |
898 | * unaware shrinkers will receive a node id of 0 instead. | |
1da177e4 | 899 | * |
aeed1d32 VD |
900 | * @memcg specifies the memory cgroup to target. Unaware shrinkers |
901 | * are called only if it is the root cgroup. | |
cb731d6c | 902 | * |
9092c71b JB |
903 | * @priority is sc->priority, we take the number of objects and >> by priority |
904 | * in order to get the scan target. | |
b15e0905 | 905 | * |
6b4f7799 | 906 | * Returns the number of reclaimed slab objects. |
1da177e4 | 907 | */ |
cb731d6c VD |
908 | static unsigned long shrink_slab(gfp_t gfp_mask, int nid, |
909 | struct mem_cgroup *memcg, | |
9092c71b | 910 | int priority) |
1da177e4 | 911 | { |
b8e57efa | 912 | unsigned long ret, freed = 0; |
1da177e4 LT |
913 | struct shrinker *shrinker; |
914 | ||
fa1e512f YS |
915 | /* |
916 | * The root memcg might be allocated even though memcg is disabled | |
917 | * via "cgroup_disable=memory" boot parameter. This could make | |
918 | * mem_cgroup_is_root() return false, then just run memcg slab | |
919 | * shrink, but skip global shrink. This may result in premature | |
920 | * oom. | |
921 | */ | |
922 | if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg)) | |
b0dedc49 | 923 | return shrink_slab_memcg(gfp_mask, nid, memcg, priority); |
cb731d6c | 924 | |
e830c63a | 925 | if (!down_read_trylock(&shrinker_rwsem)) |
f06590bd | 926 | goto out; |
1da177e4 LT |
927 | |
928 | list_for_each_entry(shrinker, &shrinker_list, list) { | |
6b4f7799 JW |
929 | struct shrink_control sc = { |
930 | .gfp_mask = gfp_mask, | |
931 | .nid = nid, | |
cb731d6c | 932 | .memcg = memcg, |
6b4f7799 | 933 | }; |
ec97097b | 934 | |
9b996468 KT |
935 | ret = do_shrink_slab(&sc, shrinker, priority); |
936 | if (ret == SHRINK_EMPTY) | |
937 | ret = 0; | |
938 | freed += ret; | |
e496612c MK |
939 | /* |
940 | * Bail out if someone want to register a new shrinker to | |
55b65a57 | 941 | * prevent the registration from being stalled for long periods |
e496612c MK |
942 | * by parallel ongoing shrinking. |
943 | */ | |
944 | if (rwsem_is_contended(&shrinker_rwsem)) { | |
945 | freed = freed ? : 1; | |
946 | break; | |
947 | } | |
1da177e4 | 948 | } |
6b4f7799 | 949 | |
1da177e4 | 950 | up_read(&shrinker_rwsem); |
f06590bd MK |
951 | out: |
952 | cond_resched(); | |
24f7c6b9 | 953 | return freed; |
1da177e4 LT |
954 | } |
955 | ||
e4b424b7 | 956 | static void drop_slab_node(int nid) |
cb731d6c VD |
957 | { |
958 | unsigned long freed; | |
1399af7e | 959 | int shift = 0; |
cb731d6c VD |
960 | |
961 | do { | |
962 | struct mem_cgroup *memcg = NULL; | |
963 | ||
069c411d CZ |
964 | if (fatal_signal_pending(current)) |
965 | return; | |
966 | ||
cb731d6c | 967 | freed = 0; |
aeed1d32 | 968 | memcg = mem_cgroup_iter(NULL, NULL, NULL); |
cb731d6c | 969 | do { |
9092c71b | 970 | freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); |
cb731d6c | 971 | } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); |
1399af7e | 972 | } while ((freed >> shift++) > 1); |
cb731d6c VD |
973 | } |
974 | ||
975 | void drop_slab(void) | |
976 | { | |
977 | int nid; | |
978 | ||
979 | for_each_online_node(nid) | |
980 | drop_slab_node(nid); | |
981 | } | |
982 | ||
e0cd5e7f | 983 | static inline int is_page_cache_freeable(struct folio *folio) |
1da177e4 | 984 | { |
ceddc3a5 JW |
985 | /* |
986 | * A freeable page cache page is referenced only by the caller | |
67891fff MW |
987 | * that isolated the page, the page cache and optional buffer |
988 | * heads at page->private. | |
ceddc3a5 | 989 | */ |
e0cd5e7f MWO |
990 | return folio_ref_count(folio) - folio_test_private(folio) == |
991 | 1 + folio_nr_pages(folio); | |
1da177e4 LT |
992 | } |
993 | ||
1da177e4 | 994 | /* |
e0cd5e7f | 995 | * We detected a synchronous write error writing a folio out. Probably |
1da177e4 LT |
996 | * -ENOSPC. We need to propagate that into the address_space for a subsequent |
997 | * fsync(), msync() or close(). | |
998 | * | |
999 | * The tricky part is that after writepage we cannot touch the mapping: nothing | |
e0cd5e7f MWO |
1000 | * prevents it from being freed up. But we have a ref on the folio and once |
1001 | * that folio is locked, the mapping is pinned. | |
1da177e4 | 1002 | * |
e0cd5e7f | 1003 | * We're allowed to run sleeping folio_lock() here because we know the caller has |
1da177e4 LT |
1004 | * __GFP_FS. |
1005 | */ | |
1006 | static void handle_write_error(struct address_space *mapping, | |
e0cd5e7f | 1007 | struct folio *folio, int error) |
1da177e4 | 1008 | { |
e0cd5e7f MWO |
1009 | folio_lock(folio); |
1010 | if (folio_mapping(folio) == mapping) | |
3e9f45bd | 1011 | mapping_set_error(mapping, error); |
e0cd5e7f | 1012 | folio_unlock(folio); |
1da177e4 LT |
1013 | } |
1014 | ||
1b4e3f26 MG |
1015 | static bool skip_throttle_noprogress(pg_data_t *pgdat) |
1016 | { | |
1017 | int reclaimable = 0, write_pending = 0; | |
1018 | int i; | |
1019 | ||
1020 | /* | |
1021 | * If kswapd is disabled, reschedule if necessary but do not | |
1022 | * throttle as the system is likely near OOM. | |
1023 | */ | |
1024 | if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) | |
1025 | return true; | |
1026 | ||
1027 | /* | |
1028 | * If there are a lot of dirty/writeback pages then do not | |
1029 | * throttle as throttling will occur when the pages cycle | |
1030 | * towards the end of the LRU if still under writeback. | |
1031 | */ | |
1032 | for (i = 0; i < MAX_NR_ZONES; i++) { | |
1033 | struct zone *zone = pgdat->node_zones + i; | |
1034 | ||
36c26128 | 1035 | if (!managed_zone(zone)) |
1b4e3f26 MG |
1036 | continue; |
1037 | ||
1038 | reclaimable += zone_reclaimable_pages(zone); | |
1039 | write_pending += zone_page_state_snapshot(zone, | |
1040 | NR_ZONE_WRITE_PENDING); | |
1041 | } | |
1042 | if (2 * write_pending <= reclaimable) | |
1043 | return true; | |
1044 | ||
1045 | return false; | |
1046 | } | |
1047 | ||
c3f4a9a2 | 1048 | void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason) |
8cd7c588 MG |
1049 | { |
1050 | wait_queue_head_t *wqh = &pgdat->reclaim_wait[reason]; | |
c3f4a9a2 | 1051 | long timeout, ret; |
8cd7c588 MG |
1052 | DEFINE_WAIT(wait); |
1053 | ||
1054 | /* | |
1055 | * Do not throttle IO workers, kthreads other than kswapd or | |
1056 | * workqueues. They may be required for reclaim to make | |
1057 | * forward progress (e.g. journalling workqueues or kthreads). | |
1058 | */ | |
1059 | if (!current_is_kswapd() && | |
b485c6f1 MG |
1060 | current->flags & (PF_IO_WORKER|PF_KTHREAD)) { |
1061 | cond_resched(); | |
8cd7c588 | 1062 | return; |
b485c6f1 | 1063 | } |
8cd7c588 | 1064 | |
c3f4a9a2 MG |
1065 | /* |
1066 | * These figures are pulled out of thin air. | |
1067 | * VMSCAN_THROTTLE_ISOLATED is a transient condition based on too many | |
1068 | * parallel reclaimers which is a short-lived event so the timeout is | |
1069 | * short. Failing to make progress or waiting on writeback are | |
1070 | * potentially long-lived events so use a longer timeout. This is shaky | |
1071 | * logic as a failure to make progress could be due to anything from | |
1072 | * writeback to a slow device to excessive references pages at the tail | |
1073 | * of the inactive LRU. | |
1074 | */ | |
1075 | switch(reason) { | |
1076 | case VMSCAN_THROTTLE_WRITEBACK: | |
1077 | timeout = HZ/10; | |
1078 | ||
1079 | if (atomic_inc_return(&pgdat->nr_writeback_throttled) == 1) { | |
1080 | WRITE_ONCE(pgdat->nr_reclaim_start, | |
1081 | node_page_state(pgdat, NR_THROTTLED_WRITTEN)); | |
1082 | } | |
1083 | ||
1084 | break; | |
1b4e3f26 MG |
1085 | case VMSCAN_THROTTLE_CONGESTED: |
1086 | fallthrough; | |
c3f4a9a2 | 1087 | case VMSCAN_THROTTLE_NOPROGRESS: |
1b4e3f26 MG |
1088 | if (skip_throttle_noprogress(pgdat)) { |
1089 | cond_resched(); | |
1090 | return; | |
1091 | } | |
1092 | ||
1093 | timeout = 1; | |
1094 | ||
c3f4a9a2 MG |
1095 | break; |
1096 | case VMSCAN_THROTTLE_ISOLATED: | |
1097 | timeout = HZ/50; | |
1098 | break; | |
1099 | default: | |
1100 | WARN_ON_ONCE(1); | |
1101 | timeout = HZ; | |
1102 | break; | |
8cd7c588 MG |
1103 | } |
1104 | ||
1105 | prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); | |
1106 | ret = schedule_timeout(timeout); | |
1107 | finish_wait(wqh, &wait); | |
d818fca1 | 1108 | |
c3f4a9a2 | 1109 | if (reason == VMSCAN_THROTTLE_WRITEBACK) |
d818fca1 | 1110 | atomic_dec(&pgdat->nr_writeback_throttled); |
8cd7c588 MG |
1111 | |
1112 | trace_mm_vmscan_throttled(pgdat->node_id, jiffies_to_usecs(timeout), | |
1113 | jiffies_to_usecs(timeout - ret), | |
1114 | reason); | |
1115 | } | |
1116 | ||
1117 | /* | |
1118 | * Account for pages written if tasks are throttled waiting on dirty | |
1119 | * pages to clean. If enough pages have been cleaned since throttling | |
1120 | * started then wakeup the throttled tasks. | |
1121 | */ | |
512b7931 | 1122 | void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, |
8cd7c588 MG |
1123 | int nr_throttled) |
1124 | { | |
1125 | unsigned long nr_written; | |
1126 | ||
512b7931 | 1127 | node_stat_add_folio(folio, NR_THROTTLED_WRITTEN); |
8cd7c588 MG |
1128 | |
1129 | /* | |
1130 | * This is an inaccurate read as the per-cpu deltas may not | |
1131 | * be synchronised. However, given that the system is | |
1132 | * writeback throttled, it is not worth taking the penalty | |
1133 | * of getting an accurate count. At worst, the throttle | |
1134 | * timeout guarantees forward progress. | |
1135 | */ | |
1136 | nr_written = node_page_state(pgdat, NR_THROTTLED_WRITTEN) - | |
1137 | READ_ONCE(pgdat->nr_reclaim_start); | |
1138 | ||
1139 | if (nr_written > SWAP_CLUSTER_MAX * nr_throttled) | |
1140 | wake_up(&pgdat->reclaim_wait[VMSCAN_THROTTLE_WRITEBACK]); | |
1141 | } | |
1142 | ||
04e62a29 CL |
1143 | /* possible outcome of pageout() */ |
1144 | typedef enum { | |
1145 | /* failed to write page out, page is locked */ | |
1146 | PAGE_KEEP, | |
1147 | /* move page to the active list, page is locked */ | |
1148 | PAGE_ACTIVATE, | |
1149 | /* page has been sent to the disk successfully, page is unlocked */ | |
1150 | PAGE_SUCCESS, | |
1151 | /* page is clean and locked */ | |
1152 | PAGE_CLEAN, | |
1153 | } pageout_t; | |
1154 | ||
1da177e4 | 1155 | /* |
1742f19f AM |
1156 | * pageout is called by shrink_page_list() for each dirty page. |
1157 | * Calls ->writepage(). | |
1da177e4 | 1158 | */ |
2282679f N |
1159 | static pageout_t pageout(struct folio *folio, struct address_space *mapping, |
1160 | struct swap_iocb **plug) | |
1da177e4 LT |
1161 | { |
1162 | /* | |
e0cd5e7f | 1163 | * If the folio is dirty, only perform writeback if that write |
1da177e4 LT |
1164 | * will be non-blocking. To prevent this allocation from being |
1165 | * stalled by pagecache activity. But note that there may be | |
1166 | * stalls if we need to run get_block(). We could test | |
1167 | * PagePrivate for that. | |
1168 | * | |
8174202b | 1169 | * If this process is currently in __generic_file_write_iter() against |
e0cd5e7f | 1170 | * this folio's queue, we can perform writeback even if that |
1da177e4 LT |
1171 | * will block. |
1172 | * | |
e0cd5e7f | 1173 | * If the folio is swapcache, write it back even if that would |
1da177e4 LT |
1174 | * block, for some throttling. This happens by accident, because |
1175 | * swap_backing_dev_info is bust: it doesn't reflect the | |
1176 | * congestion state of the swapdevs. Easy to fix, if needed. | |
1da177e4 | 1177 | */ |
e0cd5e7f | 1178 | if (!is_page_cache_freeable(folio)) |
1da177e4 LT |
1179 | return PAGE_KEEP; |
1180 | if (!mapping) { | |
1181 | /* | |
e0cd5e7f MWO |
1182 | * Some data journaling orphaned folios can have |
1183 | * folio->mapping == NULL while being dirty with clean buffers. | |
1da177e4 | 1184 | */ |
e0cd5e7f MWO |
1185 | if (folio_test_private(folio)) { |
1186 | if (try_to_free_buffers(&folio->page)) { | |
1187 | folio_clear_dirty(folio); | |
1188 | pr_info("%s: orphaned folio\n", __func__); | |
1da177e4 LT |
1189 | return PAGE_CLEAN; |
1190 | } | |
1191 | } | |
1192 | return PAGE_KEEP; | |
1193 | } | |
1194 | if (mapping->a_ops->writepage == NULL) | |
1195 | return PAGE_ACTIVATE; | |
1da177e4 | 1196 | |
e0cd5e7f | 1197 | if (folio_clear_dirty_for_io(folio)) { |
1da177e4 LT |
1198 | int res; |
1199 | struct writeback_control wbc = { | |
1200 | .sync_mode = WB_SYNC_NONE, | |
1201 | .nr_to_write = SWAP_CLUSTER_MAX, | |
111ebb6e OH |
1202 | .range_start = 0, |
1203 | .range_end = LLONG_MAX, | |
1da177e4 | 1204 | .for_reclaim = 1, |
2282679f | 1205 | .swap_plug = plug, |
1da177e4 LT |
1206 | }; |
1207 | ||
e0cd5e7f MWO |
1208 | folio_set_reclaim(folio); |
1209 | res = mapping->a_ops->writepage(&folio->page, &wbc); | |
1da177e4 | 1210 | if (res < 0) |
e0cd5e7f | 1211 | handle_write_error(mapping, folio, res); |
994fc28c | 1212 | if (res == AOP_WRITEPAGE_ACTIVATE) { |
e0cd5e7f | 1213 | folio_clear_reclaim(folio); |
1da177e4 LT |
1214 | return PAGE_ACTIVATE; |
1215 | } | |
c661b078 | 1216 | |
e0cd5e7f | 1217 | if (!folio_test_writeback(folio)) { |
1da177e4 | 1218 | /* synchronous write or broken a_ops? */ |
e0cd5e7f | 1219 | folio_clear_reclaim(folio); |
1da177e4 | 1220 | } |
e0cd5e7f MWO |
1221 | trace_mm_vmscan_write_folio(folio); |
1222 | node_stat_add_folio(folio, NR_VMSCAN_WRITE); | |
1da177e4 LT |
1223 | return PAGE_SUCCESS; |
1224 | } | |
1225 | ||
1226 | return PAGE_CLEAN; | |
1227 | } | |
1228 | ||
a649fd92 | 1229 | /* |
e286781d NP |
1230 | * Same as remove_mapping, but if the page is removed from the mapping, it |
1231 | * gets returned with a refcount of 0. | |
a649fd92 | 1232 | */ |
be7c07d6 | 1233 | static int __remove_mapping(struct address_space *mapping, struct folio *folio, |
b910718a | 1234 | bool reclaimed, struct mem_cgroup *target_memcg) |
49d2e9cc | 1235 | { |
bd4c82c2 | 1236 | int refcount; |
aae466b0 | 1237 | void *shadow = NULL; |
c4843a75 | 1238 | |
be7c07d6 MWO |
1239 | BUG_ON(!folio_test_locked(folio)); |
1240 | BUG_ON(mapping != folio_mapping(folio)); | |
49d2e9cc | 1241 | |
be7c07d6 | 1242 | if (!folio_test_swapcache(folio)) |
51b8c1fe | 1243 | spin_lock(&mapping->host->i_lock); |
30472509 | 1244 | xa_lock_irq(&mapping->i_pages); |
49d2e9cc | 1245 | /* |
0fd0e6b0 NP |
1246 | * The non racy check for a busy page. |
1247 | * | |
1248 | * Must be careful with the order of the tests. When someone has | |
1249 | * a ref to the page, it may be possible that they dirty it then | |
1250 | * drop the reference. So if PageDirty is tested before page_count | |
1251 | * here, then the following race may occur: | |
1252 | * | |
1253 | * get_user_pages(&page); | |
1254 | * [user mapping goes away] | |
1255 | * write_to(page); | |
1256 | * !PageDirty(page) [good] | |
1257 | * SetPageDirty(page); | |
1258 | * put_page(page); | |
1259 | * !page_count(page) [good, discard it] | |
1260 | * | |
1261 | * [oops, our write_to data is lost] | |
1262 | * | |
1263 | * Reversing the order of the tests ensures such a situation cannot | |
1264 | * escape unnoticed. The smp_rmb is needed to ensure the page->flags | |
0139aa7b | 1265 | * load is not satisfied before that of page->_refcount. |
0fd0e6b0 NP |
1266 | * |
1267 | * Note that if SetPageDirty is always performed via set_page_dirty, | |
b93b0163 | 1268 | * and thus under the i_pages lock, then this ordering is not required. |
49d2e9cc | 1269 | */ |
be7c07d6 MWO |
1270 | refcount = 1 + folio_nr_pages(folio); |
1271 | if (!folio_ref_freeze(folio, refcount)) | |
49d2e9cc | 1272 | goto cannot_free; |
1c4c3b99 | 1273 | /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */ |
be7c07d6 MWO |
1274 | if (unlikely(folio_test_dirty(folio))) { |
1275 | folio_ref_unfreeze(folio, refcount); | |
49d2e9cc | 1276 | goto cannot_free; |
e286781d | 1277 | } |
49d2e9cc | 1278 | |
be7c07d6 MWO |
1279 | if (folio_test_swapcache(folio)) { |
1280 | swp_entry_t swap = folio_swap_entry(folio); | |
3ecb0087 | 1281 | mem_cgroup_swapout(folio, swap); |
aae466b0 | 1282 | if (reclaimed && !mapping_exiting(mapping)) |
8927f647 | 1283 | shadow = workingset_eviction(folio, target_memcg); |
be7c07d6 | 1284 | __delete_from_swap_cache(&folio->page, swap, shadow); |
30472509 | 1285 | xa_unlock_irq(&mapping->i_pages); |
be7c07d6 | 1286 | put_swap_page(&folio->page, swap); |
e286781d | 1287 | } else { |
6072d13c LT |
1288 | void (*freepage)(struct page *); |
1289 | ||
1290 | freepage = mapping->a_ops->freepage; | |
a528910e JW |
1291 | /* |
1292 | * Remember a shadow entry for reclaimed file cache in | |
1293 | * order to detect refaults, thus thrashing, later on. | |
1294 | * | |
1295 | * But don't store shadows in an address space that is | |
238c3046 | 1296 | * already exiting. This is not just an optimization, |
a528910e JW |
1297 | * inode reclaim needs to empty out the radix tree or |
1298 | * the nodes are lost. Don't plant shadows behind its | |
1299 | * back. | |
f9fe48be RZ |
1300 | * |
1301 | * We also don't store shadows for DAX mappings because the | |
1302 | * only page cache pages found in these are zero pages | |
1303 | * covering holes, and because we don't want to mix DAX | |
1304 | * exceptional entries and shadow exceptional entries in the | |
b93b0163 | 1305 | * same address_space. |
a528910e | 1306 | */ |
be7c07d6 | 1307 | if (reclaimed && folio_is_file_lru(folio) && |
f9fe48be | 1308 | !mapping_exiting(mapping) && !dax_mapping(mapping)) |
8927f647 MWO |
1309 | shadow = workingset_eviction(folio, target_memcg); |
1310 | __filemap_remove_folio(folio, shadow); | |
30472509 | 1311 | xa_unlock_irq(&mapping->i_pages); |
51b8c1fe JW |
1312 | if (mapping_shrinkable(mapping)) |
1313 | inode_add_lru(mapping->host); | |
1314 | spin_unlock(&mapping->host->i_lock); | |
6072d13c LT |
1315 | |
1316 | if (freepage != NULL) | |
be7c07d6 | 1317 | freepage(&folio->page); |
49d2e9cc CL |
1318 | } |
1319 | ||
49d2e9cc CL |
1320 | return 1; |
1321 | ||
1322 | cannot_free: | |
30472509 | 1323 | xa_unlock_irq(&mapping->i_pages); |
be7c07d6 | 1324 | if (!folio_test_swapcache(folio)) |
51b8c1fe | 1325 | spin_unlock(&mapping->host->i_lock); |
49d2e9cc CL |
1326 | return 0; |
1327 | } | |
1328 | ||
5100da38 MWO |
1329 | /** |
1330 | * remove_mapping() - Attempt to remove a folio from its mapping. | |
1331 | * @mapping: The address space. | |
1332 | * @folio: The folio to remove. | |
1333 | * | |
1334 | * If the folio is dirty, under writeback or if someone else has a ref | |
1335 | * on it, removal will fail. | |
1336 | * Return: The number of pages removed from the mapping. 0 if the folio | |
1337 | * could not be removed. | |
1338 | * Context: The caller should have a single refcount on the folio and | |
1339 | * hold its lock. | |
e286781d | 1340 | */ |
5100da38 | 1341 | long remove_mapping(struct address_space *mapping, struct folio *folio) |
e286781d | 1342 | { |
be7c07d6 | 1343 | if (__remove_mapping(mapping, folio, false, NULL)) { |
e286781d | 1344 | /* |
5100da38 | 1345 | * Unfreezing the refcount with 1 effectively |
e286781d NP |
1346 | * drops the pagecache ref for us without requiring another |
1347 | * atomic operation. | |
1348 | */ | |
be7c07d6 | 1349 | folio_ref_unfreeze(folio, 1); |
5100da38 | 1350 | return folio_nr_pages(folio); |
e286781d NP |
1351 | } |
1352 | return 0; | |
1353 | } | |
1354 | ||
894bc310 | 1355 | /** |
ca6d60f3 MWO |
1356 | * folio_putback_lru - Put previously isolated folio onto appropriate LRU list. |
1357 | * @folio: Folio to be returned to an LRU list. | |
894bc310 | 1358 | * |
ca6d60f3 MWO |
1359 | * Add previously isolated @folio to appropriate LRU list. |
1360 | * The folio may still be unevictable for other reasons. | |
894bc310 | 1361 | * |
ca6d60f3 | 1362 | * Context: lru_lock must not be held, interrupts must be enabled. |
894bc310 | 1363 | */ |
ca6d60f3 | 1364 | void folio_putback_lru(struct folio *folio) |
894bc310 | 1365 | { |
ca6d60f3 MWO |
1366 | folio_add_lru(folio); |
1367 | folio_put(folio); /* drop ref from isolate */ | |
894bc310 LS |
1368 | } |
1369 | ||
dfc8d636 JW |
1370 | enum page_references { |
1371 | PAGEREF_RECLAIM, | |
1372 | PAGEREF_RECLAIM_CLEAN, | |
64574746 | 1373 | PAGEREF_KEEP, |
dfc8d636 JW |
1374 | PAGEREF_ACTIVATE, |
1375 | }; | |
1376 | ||
d92013d1 | 1377 | static enum page_references folio_check_references(struct folio *folio, |
dfc8d636 JW |
1378 | struct scan_control *sc) |
1379 | { | |
d92013d1 | 1380 | int referenced_ptes, referenced_folio; |
dfc8d636 | 1381 | unsigned long vm_flags; |
dfc8d636 | 1382 | |
b3ac0413 MWO |
1383 | referenced_ptes = folio_referenced(folio, 1, sc->target_mem_cgroup, |
1384 | &vm_flags); | |
d92013d1 | 1385 | referenced_folio = folio_test_clear_referenced(folio); |
dfc8d636 | 1386 | |
dfc8d636 | 1387 | /* |
d92013d1 MWO |
1388 | * The supposedly reclaimable folio was found to be in a VM_LOCKED vma. |
1389 | * Let the folio, now marked Mlocked, be moved to the unevictable list. | |
dfc8d636 JW |
1390 | */ |
1391 | if (vm_flags & VM_LOCKED) | |
47d4f3ee | 1392 | return PAGEREF_ACTIVATE; |
dfc8d636 | 1393 | |
64574746 | 1394 | if (referenced_ptes) { |
64574746 | 1395 | /* |
d92013d1 | 1396 | * All mapped folios start out with page table |
64574746 | 1397 | * references from the instantiating fault, so we need |
9030fb0b | 1398 | * to look twice if a mapped file/anon folio is used more |
64574746 JW |
1399 | * than once. |
1400 | * | |
1401 | * Mark it and spare it for another trip around the | |
1402 | * inactive list. Another page table reference will | |
1403 | * lead to its activation. | |
1404 | * | |
d92013d1 MWO |
1405 | * Note: the mark is set for activated folios as well |
1406 | * so that recently deactivated but used folios are | |
64574746 JW |
1407 | * quickly recovered. |
1408 | */ | |
d92013d1 | 1409 | folio_set_referenced(folio); |
64574746 | 1410 | |
d92013d1 | 1411 | if (referenced_folio || referenced_ptes > 1) |
64574746 JW |
1412 | return PAGEREF_ACTIVATE; |
1413 | ||
c909e993 | 1414 | /* |
d92013d1 | 1415 | * Activate file-backed executable folios after first usage. |
c909e993 | 1416 | */ |
f19a27e3 | 1417 | if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio)) |
c909e993 KK |
1418 | return PAGEREF_ACTIVATE; |
1419 | ||
64574746 JW |
1420 | return PAGEREF_KEEP; |
1421 | } | |
dfc8d636 | 1422 | |
d92013d1 | 1423 | /* Reclaim if clean, defer dirty folios to writeback */ |
f19a27e3 | 1424 | if (referenced_folio && folio_is_file_lru(folio)) |
64574746 JW |
1425 | return PAGEREF_RECLAIM_CLEAN; |
1426 | ||
1427 | return PAGEREF_RECLAIM; | |
dfc8d636 JW |
1428 | } |
1429 | ||
e2be15f6 | 1430 | /* Check if a page is dirty or under writeback */ |
e20c41b1 | 1431 | static void folio_check_dirty_writeback(struct folio *folio, |
e2be15f6 MG |
1432 | bool *dirty, bool *writeback) |
1433 | { | |
b4597226 MG |
1434 | struct address_space *mapping; |
1435 | ||
e2be15f6 MG |
1436 | /* |
1437 | * Anonymous pages are not handled by flushers and must be written | |
32a331a7 ML |
1438 | * from reclaim context. Do not stall reclaim based on them. |
1439 | * MADV_FREE anonymous pages are put into inactive file list too. | |
1440 | * They could be mistakenly treated as file lru. So further anon | |
1441 | * test is needed. | |
e2be15f6 | 1442 | */ |
e20c41b1 MWO |
1443 | if (!folio_is_file_lru(folio) || |
1444 | (folio_test_anon(folio) && !folio_test_swapbacked(folio))) { | |
e2be15f6 MG |
1445 | *dirty = false; |
1446 | *writeback = false; | |
1447 | return; | |
1448 | } | |
1449 | ||
e20c41b1 MWO |
1450 | /* By default assume that the folio flags are accurate */ |
1451 | *dirty = folio_test_dirty(folio); | |
1452 | *writeback = folio_test_writeback(folio); | |
b4597226 MG |
1453 | |
1454 | /* Verify dirty/writeback state if the filesystem supports it */ | |
e20c41b1 | 1455 | if (!folio_test_private(folio)) |
b4597226 MG |
1456 | return; |
1457 | ||
e20c41b1 | 1458 | mapping = folio_mapping(folio); |
b4597226 | 1459 | if (mapping && mapping->a_ops->is_dirty_writeback) |
e20c41b1 | 1460 | mapping->a_ops->is_dirty_writeback(&folio->page, dirty, writeback); |
e2be15f6 MG |
1461 | } |
1462 | ||
26aa2d19 DH |
1463 | static struct page *alloc_demote_page(struct page *page, unsigned long node) |
1464 | { | |
1465 | struct migration_target_control mtc = { | |
1466 | /* | |
1467 | * Allocate from 'node', or fail quickly and quietly. | |
1468 | * When this happens, 'page' will likely just be discarded | |
1469 | * instead of migrated. | |
1470 | */ | |
1471 | .gfp_mask = (GFP_HIGHUSER_MOVABLE & ~__GFP_RECLAIM) | | |
1472 | __GFP_THISNODE | __GFP_NOWARN | | |
1473 | __GFP_NOMEMALLOC | GFP_NOWAIT, | |
1474 | .nid = node | |
1475 | }; | |
1476 | ||
1477 | return alloc_migration_target(page, (unsigned long)&mtc); | |
1478 | } | |
1479 | ||
1480 | /* | |
1481 | * Take pages on @demote_list and attempt to demote them to | |
1482 | * another node. Pages which are not demoted are left on | |
1483 | * @demote_pages. | |
1484 | */ | |
1485 | static unsigned int demote_page_list(struct list_head *demote_pages, | |
1486 | struct pglist_data *pgdat) | |
1487 | { | |
1488 | int target_nid = next_demotion_node(pgdat->node_id); | |
1489 | unsigned int nr_succeeded; | |
26aa2d19 DH |
1490 | |
1491 | if (list_empty(demote_pages)) | |
1492 | return 0; | |
1493 | ||
1494 | if (target_nid == NUMA_NO_NODE) | |
1495 | return 0; | |
1496 | ||
1497 | /* Demotion ignores all cpuset and mempolicy settings */ | |
cb75463c | 1498 | migrate_pages(demote_pages, alloc_demote_page, NULL, |
26aa2d19 DH |
1499 | target_nid, MIGRATE_ASYNC, MR_DEMOTION, |
1500 | &nr_succeeded); | |
1501 | ||
668e4147 YS |
1502 | if (current_is_kswapd()) |
1503 | __count_vm_events(PGDEMOTE_KSWAPD, nr_succeeded); | |
1504 | else | |
1505 | __count_vm_events(PGDEMOTE_DIRECT, nr_succeeded); | |
1506 | ||
26aa2d19 DH |
1507 | return nr_succeeded; |
1508 | } | |
1509 | ||
d791ea67 N |
1510 | static bool may_enter_fs(struct page *page, gfp_t gfp_mask) |
1511 | { | |
1512 | if (gfp_mask & __GFP_FS) | |
1513 | return true; | |
1514 | if (!PageSwapCache(page) || !(gfp_mask & __GFP_IO)) | |
1515 | return false; | |
1516 | /* | |
1517 | * We can "enter_fs" for swap-cache with only __GFP_IO | |
1518 | * providing this isn't SWP_FS_OPS. | |
1519 | * ->flags can be updated non-atomicially (scan_swap_map_slots), | |
1520 | * but that will never affect SWP_FS_OPS, so the data_race | |
1521 | * is safe. | |
1522 | */ | |
1523 | return !data_race(page_swap_flags(page) & SWP_FS_OPS); | |
1524 | } | |
1525 | ||
1da177e4 | 1526 | /* |
1742f19f | 1527 | * shrink_page_list() returns the number of reclaimed pages |
1da177e4 | 1528 | */ |
730ec8c0 MS |
1529 | static unsigned int shrink_page_list(struct list_head *page_list, |
1530 | struct pglist_data *pgdat, | |
1531 | struct scan_control *sc, | |
730ec8c0 MS |
1532 | struct reclaim_stat *stat, |
1533 | bool ignore_references) | |
1da177e4 LT |
1534 | { |
1535 | LIST_HEAD(ret_pages); | |
abe4c3b5 | 1536 | LIST_HEAD(free_pages); |
26aa2d19 | 1537 | LIST_HEAD(demote_pages); |
730ec8c0 MS |
1538 | unsigned int nr_reclaimed = 0; |
1539 | unsigned int pgactivate = 0; | |
26aa2d19 | 1540 | bool do_demote_pass; |
2282679f | 1541 | struct swap_iocb *plug = NULL; |
1da177e4 | 1542 | |
060f005f | 1543 | memset(stat, 0, sizeof(*stat)); |
1da177e4 | 1544 | cond_resched(); |
26aa2d19 | 1545 | do_demote_pass = can_demote(pgdat->node_id, sc); |
1da177e4 | 1546 | |
26aa2d19 | 1547 | retry: |
1da177e4 LT |
1548 | while (!list_empty(page_list)) { |
1549 | struct address_space *mapping; | |
1550 | struct page *page; | |
be7c07d6 | 1551 | struct folio *folio; |
8940b34a | 1552 | enum page_references references = PAGEREF_RECLAIM; |
d791ea67 | 1553 | bool dirty, writeback; |
98879b3b | 1554 | unsigned int nr_pages; |
1da177e4 LT |
1555 | |
1556 | cond_resched(); | |
1557 | ||
be7c07d6 MWO |
1558 | folio = lru_to_folio(page_list); |
1559 | list_del(&folio->lru); | |
1560 | page = &folio->page; | |
1da177e4 | 1561 | |
529ae9aa | 1562 | if (!trylock_page(page)) |
1da177e4 LT |
1563 | goto keep; |
1564 | ||
309381fe | 1565 | VM_BUG_ON_PAGE(PageActive(page), page); |
1da177e4 | 1566 | |
d8c6546b | 1567 | nr_pages = compound_nr(page); |
98879b3b YS |
1568 | |
1569 | /* Account the number of base pages even though THP */ | |
1570 | sc->nr_scanned += nr_pages; | |
80e43426 | 1571 | |
39b5f29a | 1572 | if (unlikely(!page_evictable(page))) |
ad6b6704 | 1573 | goto activate_locked; |
894bc310 | 1574 | |
1bee2c16 | 1575 | if (!sc->may_unmap && folio_mapped(folio)) |
80e43426 CL |
1576 | goto keep_locked; |
1577 | ||
e2be15f6 | 1578 | /* |
894befec | 1579 | * The number of dirty pages determines if a node is marked |
8cd7c588 MG |
1580 | * reclaim_congested. kswapd will stall and start writing |
1581 | * pages if the tail of the LRU is all dirty unqueued pages. | |
e2be15f6 | 1582 | */ |
e20c41b1 | 1583 | folio_check_dirty_writeback(folio, &dirty, &writeback); |
e2be15f6 | 1584 | if (dirty || writeback) |
c79b7b96 | 1585 | stat->nr_dirty += nr_pages; |
e2be15f6 MG |
1586 | |
1587 | if (dirty && !writeback) | |
c79b7b96 | 1588 | stat->nr_unqueued_dirty += nr_pages; |
e2be15f6 | 1589 | |
d04e8acd MG |
1590 | /* |
1591 | * Treat this page as congested if the underlying BDI is or if | |
1592 | * pages are cycling through the LRU so quickly that the | |
1593 | * pages marked for immediate reclaim are making it to the | |
1594 | * end of the LRU a second time. | |
1595 | */ | |
e2be15f6 | 1596 | mapping = page_mapping(page); |
fe55d563 | 1597 | if (writeback && PageReclaim(page)) |
c79b7b96 | 1598 | stat->nr_congested += nr_pages; |
e2be15f6 | 1599 | |
283aba9f | 1600 | /* |
d33e4e14 | 1601 | * If a folio at the tail of the LRU is under writeback, there |
283aba9f MG |
1602 | * are three cases to consider. |
1603 | * | |
d33e4e14 MWO |
1604 | * 1) If reclaim is encountering an excessive number of folios |
1605 | * under writeback and this folio is both under | |
1606 | * writeback and has the reclaim flag set then it | |
1607 | * indicates that folios are being queued for I/O but | |
1608 | * are being recycled through the LRU before the I/O | |
1609 | * can complete. Waiting on the folio itself risks an | |
1610 | * indefinite stall if it is impossible to writeback | |
1611 | * the folio due to I/O error or disconnected storage | |
1612 | * so instead note that the LRU is being scanned too | |
1613 | * quickly and the caller can stall after the folio | |
1614 | * list has been processed. | |
283aba9f | 1615 | * |
d33e4e14 | 1616 | * 2) Global or new memcg reclaim encounters a folio that is |
ecf5fc6e MH |
1617 | * not marked for immediate reclaim, or the caller does not |
1618 | * have __GFP_FS (or __GFP_IO if it's simply going to swap, | |
d33e4e14 | 1619 | * not to fs). In this case mark the folio for immediate |
97c9341f | 1620 | * reclaim and continue scanning. |
283aba9f | 1621 | * |
d791ea67 | 1622 | * Require may_enter_fs() because we would wait on fs, which |
d33e4e14 MWO |
1623 | * may not have submitted I/O yet. And the loop driver might |
1624 | * enter reclaim, and deadlock if it waits on a folio for | |
283aba9f MG |
1625 | * which it is needed to do the write (loop masks off |
1626 | * __GFP_IO|__GFP_FS for this reason); but more thought | |
1627 | * would probably show more reasons. | |
1628 | * | |
d33e4e14 MWO |
1629 | * 3) Legacy memcg encounters a folio that already has the |
1630 | * reclaim flag set. memcg does not have any dirty folio | |
283aba9f | 1631 | * throttling so we could easily OOM just because too many |
d33e4e14 | 1632 | * folios are in writeback and there is nothing else to |
283aba9f | 1633 | * reclaim. Wait for the writeback to complete. |
c55e8d03 | 1634 | * |
d33e4e14 MWO |
1635 | * In cases 1) and 2) we activate the folios to get them out of |
1636 | * the way while we continue scanning for clean folios on the | |
c55e8d03 JW |
1637 | * inactive list and refilling from the active list. The |
1638 | * observation here is that waiting for disk writes is more | |
1639 | * expensive than potentially causing reloads down the line. | |
1640 | * Since they're marked for immediate reclaim, they won't put | |
1641 | * memory pressure on the cache working set any longer than it | |
1642 | * takes to write them to disk. | |
283aba9f | 1643 | */ |
d33e4e14 | 1644 | if (folio_test_writeback(folio)) { |
283aba9f MG |
1645 | /* Case 1 above */ |
1646 | if (current_is_kswapd() && | |
d33e4e14 | 1647 | folio_test_reclaim(folio) && |
599d0c95 | 1648 | test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { |
c79b7b96 | 1649 | stat->nr_immediate += nr_pages; |
c55e8d03 | 1650 | goto activate_locked; |
283aba9f MG |
1651 | |
1652 | /* Case 2 above */ | |
b5ead35e | 1653 | } else if (writeback_throttling_sane(sc) || |
d33e4e14 MWO |
1654 | !folio_test_reclaim(folio) || |
1655 | !may_enter_fs(page, sc->gfp_mask)) { | |
c3b94f44 | 1656 | /* |
d33e4e14 MWO |
1657 | * This is slightly racy - |
1658 | * folio_end_writeback() might have just | |
1659 | * cleared the reclaim flag, then setting | |
1660 | * reclaim here ends up interpreted as | |
1661 | * the readahead flag - but that does | |
1662 | * not matter enough to care. What we | |
1663 | * do want is for this folio to have | |
1664 | * the reclaim flag set next time memcg | |
1665 | * reclaim reaches the tests above, so | |
1666 | * it will then folio_wait_writeback() | |
1667 | * to avoid OOM; and it's also appropriate | |
1668 | * in global reclaim. | |
c3b94f44 | 1669 | */ |
d33e4e14 | 1670 | folio_set_reclaim(folio); |
c79b7b96 | 1671 | stat->nr_writeback += nr_pages; |
c55e8d03 | 1672 | goto activate_locked; |
283aba9f MG |
1673 | |
1674 | /* Case 3 above */ | |
1675 | } else { | |
d33e4e14 MWO |
1676 | folio_unlock(folio); |
1677 | folio_wait_writeback(folio); | |
1678 | /* then go back and try same folio again */ | |
1679 | list_add_tail(&folio->lru, page_list); | |
7fadc820 | 1680 | continue; |
e62e384e | 1681 | } |
c661b078 | 1682 | } |
1da177e4 | 1683 | |
8940b34a | 1684 | if (!ignore_references) |
d92013d1 | 1685 | references = folio_check_references(folio, sc); |
02c6de8d | 1686 | |
dfc8d636 JW |
1687 | switch (references) { |
1688 | case PAGEREF_ACTIVATE: | |
1da177e4 | 1689 | goto activate_locked; |
64574746 | 1690 | case PAGEREF_KEEP: |
98879b3b | 1691 | stat->nr_ref_keep += nr_pages; |
64574746 | 1692 | goto keep_locked; |
dfc8d636 JW |
1693 | case PAGEREF_RECLAIM: |
1694 | case PAGEREF_RECLAIM_CLEAN: | |
1695 | ; /* try to reclaim the page below */ | |
1696 | } | |
1da177e4 | 1697 | |
26aa2d19 DH |
1698 | /* |
1699 | * Before reclaiming the page, try to relocate | |
1700 | * its contents to another node. | |
1701 | */ | |
1702 | if (do_demote_pass && | |
1703 | (thp_migration_supported() || !PageTransHuge(page))) { | |
1704 | list_add(&page->lru, &demote_pages); | |
1705 | unlock_page(page); | |
1706 | continue; | |
1707 | } | |
1708 | ||
1da177e4 LT |
1709 | /* |
1710 | * Anonymous process memory has backing store? | |
1711 | * Try to allocate it some swap space here. | |
802a3a92 | 1712 | * Lazyfree page could be freed directly |
1da177e4 | 1713 | */ |
bd4c82c2 HY |
1714 | if (PageAnon(page) && PageSwapBacked(page)) { |
1715 | if (!PageSwapCache(page)) { | |
1716 | if (!(sc->gfp_mask & __GFP_IO)) | |
1717 | goto keep_locked; | |
d4b4084a | 1718 | if (folio_maybe_dma_pinned(folio)) |
feb889fb | 1719 | goto keep_locked; |
bd4c82c2 HY |
1720 | if (PageTransHuge(page)) { |
1721 | /* cannot split THP, skip it */ | |
d4b4084a | 1722 | if (!can_split_folio(folio, NULL)) |
bd4c82c2 HY |
1723 | goto activate_locked; |
1724 | /* | |
1725 | * Split pages without a PMD map right | |
1726 | * away. Chances are some or all of the | |
1727 | * tail pages can be freed without IO. | |
1728 | */ | |
d4b4084a | 1729 | if (!folio_entire_mapcount(folio) && |
346cf613 MWO |
1730 | split_folio_to_list(folio, |
1731 | page_list)) | |
bd4c82c2 HY |
1732 | goto activate_locked; |
1733 | } | |
1734 | if (!add_to_swap(page)) { | |
1735 | if (!PageTransHuge(page)) | |
98879b3b | 1736 | goto activate_locked_split; |
bd4c82c2 | 1737 | /* Fallback to swap normal pages */ |
346cf613 MWO |
1738 | if (split_folio_to_list(folio, |
1739 | page_list)) | |
bd4c82c2 | 1740 | goto activate_locked; |
fe490cc0 HY |
1741 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
1742 | count_vm_event(THP_SWPOUT_FALLBACK); | |
1743 | #endif | |
bd4c82c2 | 1744 | if (!add_to_swap(page)) |
98879b3b | 1745 | goto activate_locked_split; |
bd4c82c2 | 1746 | } |
0f074658 | 1747 | |
bd4c82c2 HY |
1748 | /* Adding to swap updated mapping */ |
1749 | mapping = page_mapping(page); | |
1750 | } | |
820c4e2e MWO |
1751 | } else if (PageSwapBacked(page) && PageTransHuge(page)) { |
1752 | /* Split shmem THP */ | |
346cf613 | 1753 | if (split_folio_to_list(folio, page_list)) |
7751b2da | 1754 | goto keep_locked; |
e2be15f6 | 1755 | } |
1da177e4 | 1756 | |
98879b3b YS |
1757 | /* |
1758 | * THP may get split above, need minus tail pages and update | |
1759 | * nr_pages to avoid accounting tail pages twice. | |
1760 | * | |
1761 | * The tail pages that are added into swap cache successfully | |
1762 | * reach here. | |
1763 | */ | |
1764 | if ((nr_pages > 1) && !PageTransHuge(page)) { | |
1765 | sc->nr_scanned -= (nr_pages - 1); | |
1766 | nr_pages = 1; | |
1767 | } | |
1768 | ||
1da177e4 | 1769 | /* |
1bee2c16 | 1770 | * The folio is mapped into the page tables of one or more |
1da177e4 LT |
1771 | * processes. Try to unmap it here. |
1772 | */ | |
1bee2c16 | 1773 | if (folio_mapped(folio)) { |
013339df | 1774 | enum ttu_flags flags = TTU_BATCH_FLUSH; |
1bee2c16 | 1775 | bool was_swapbacked = folio_test_swapbacked(folio); |
bd4c82c2 | 1776 | |
1bee2c16 | 1777 | if (folio_test_pmd_mappable(folio)) |
bd4c82c2 | 1778 | flags |= TTU_SPLIT_HUGE_PMD; |
1f318a9b | 1779 | |
869f7ee6 | 1780 | try_to_unmap(folio, flags); |
1bee2c16 | 1781 | if (folio_mapped(folio)) { |
98879b3b | 1782 | stat->nr_unmap_fail += nr_pages; |
1bee2c16 MWO |
1783 | if (!was_swapbacked && |
1784 | folio_test_swapbacked(folio)) | |
1f318a9b | 1785 | stat->nr_lazyfree_fail += nr_pages; |
1da177e4 | 1786 | goto activate_locked; |
1da177e4 LT |
1787 | } |
1788 | } | |
1789 | ||
1790 | if (PageDirty(page)) { | |
ee72886d | 1791 | /* |
4eda4823 JW |
1792 | * Only kswapd can writeback filesystem pages |
1793 | * to avoid risk of stack overflow. But avoid | |
1794 | * injecting inefficient single-page IO into | |
1795 | * flusher writeback as much as possible: only | |
1796 | * write pages when we've encountered many | |
1797 | * dirty pages, and when we've already scanned | |
1798 | * the rest of the LRU for clean pages and see | |
1799 | * the same dirty pages again (PageReclaim). | |
ee72886d | 1800 | */ |
9de4f22a | 1801 | if (page_is_file_lru(page) && |
4eda4823 JW |
1802 | (!current_is_kswapd() || !PageReclaim(page) || |
1803 | !test_bit(PGDAT_DIRTY, &pgdat->flags))) { | |
49ea7eb6 MG |
1804 | /* |
1805 | * Immediately reclaim when written back. | |
1806 | * Similar in principal to deactivate_page() | |
1807 | * except we already have the page isolated | |
1808 | * and know it's dirty | |
1809 | */ | |
c4a25635 | 1810 | inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); |
49ea7eb6 MG |
1811 | SetPageReclaim(page); |
1812 | ||
c55e8d03 | 1813 | goto activate_locked; |
ee72886d MG |
1814 | } |
1815 | ||
dfc8d636 | 1816 | if (references == PAGEREF_RECLAIM_CLEAN) |
1da177e4 | 1817 | goto keep_locked; |
d791ea67 | 1818 | if (!may_enter_fs(page, sc->gfp_mask)) |
1da177e4 | 1819 | goto keep_locked; |
52a8363e | 1820 | if (!sc->may_writepage) |
1da177e4 LT |
1821 | goto keep_locked; |
1822 | ||
d950c947 MG |
1823 | /* |
1824 | * Page is dirty. Flush the TLB if a writable entry | |
1825 | * potentially exists to avoid CPU writes after IO | |
1826 | * starts and then write it out here. | |
1827 | */ | |
1828 | try_to_unmap_flush_dirty(); | |
2282679f | 1829 | switch (pageout(folio, mapping, &plug)) { |
1da177e4 LT |
1830 | case PAGE_KEEP: |
1831 | goto keep_locked; | |
1832 | case PAGE_ACTIVATE: | |
1833 | goto activate_locked; | |
1834 | case PAGE_SUCCESS: | |
c79b7b96 | 1835 | stat->nr_pageout += nr_pages; |
96f8bf4f | 1836 | |
7d3579e8 | 1837 | if (PageWriteback(page)) |
41ac1999 | 1838 | goto keep; |
7d3579e8 | 1839 | if (PageDirty(page)) |
1da177e4 | 1840 | goto keep; |
7d3579e8 | 1841 | |
1da177e4 LT |
1842 | /* |
1843 | * A synchronous write - probably a ramdisk. Go | |
1844 | * ahead and try to reclaim the page. | |
1845 | */ | |
529ae9aa | 1846 | if (!trylock_page(page)) |
1da177e4 LT |
1847 | goto keep; |
1848 | if (PageDirty(page) || PageWriteback(page)) | |
1849 | goto keep_locked; | |
1850 | mapping = page_mapping(page); | |
01359eb2 | 1851 | fallthrough; |
1da177e4 LT |
1852 | case PAGE_CLEAN: |
1853 | ; /* try to free the page below */ | |
1854 | } | |
1855 | } | |
1856 | ||
1857 | /* | |
1858 | * If the page has buffers, try to free the buffer mappings | |
1859 | * associated with this page. If we succeed we try to free | |
1860 | * the page as well. | |
1861 | * | |
1862 | * We do this even if the page is PageDirty(). | |
1863 | * try_to_release_page() does not perform I/O, but it is | |
1864 | * possible for a page to have PageDirty set, but it is actually | |
1865 | * clean (all its buffers are clean). This happens if the | |
1866 | * buffers were written out directly, with submit_bh(). ext3 | |
894bc310 | 1867 | * will do this, as well as the blockdev mapping. |
1da177e4 LT |
1868 | * try_to_release_page() will discover that cleanness and will |
1869 | * drop the buffers and mark the page clean - it can be freed. | |
1870 | * | |
1871 | * Rarely, pages can have buffers and no ->mapping. These are | |
1872 | * the pages which were not successfully invalidated in | |
d12b8951 | 1873 | * truncate_cleanup_page(). We try to drop those buffers here |
1da177e4 LT |
1874 | * and if that worked, and the page is no longer mapped into |
1875 | * process address space (page_count == 1) it can be freed. | |
1876 | * Otherwise, leave the page on the LRU so it is swappable. | |
1877 | */ | |
266cf658 | 1878 | if (page_has_private(page)) { |
1da177e4 LT |
1879 | if (!try_to_release_page(page, sc->gfp_mask)) |
1880 | goto activate_locked; | |
e286781d NP |
1881 | if (!mapping && page_count(page) == 1) { |
1882 | unlock_page(page); | |
1883 | if (put_page_testzero(page)) | |
1884 | goto free_it; | |
1885 | else { | |
1886 | /* | |
1887 | * rare race with speculative reference. | |
1888 | * the speculative reference will free | |
1889 | * this page shortly, so we may | |
1890 | * increment nr_reclaimed here (and | |
1891 | * leave it off the LRU). | |
1892 | */ | |
9aafcffc | 1893 | nr_reclaimed += nr_pages; |
e286781d NP |
1894 | continue; |
1895 | } | |
1896 | } | |
1da177e4 LT |
1897 | } |
1898 | ||
802a3a92 SL |
1899 | if (PageAnon(page) && !PageSwapBacked(page)) { |
1900 | /* follow __remove_mapping for reference */ | |
1901 | if (!page_ref_freeze(page, 1)) | |
1902 | goto keep_locked; | |
d17be2d9 ML |
1903 | /* |
1904 | * The page has only one reference left, which is | |
1905 | * from the isolation. After the caller puts the | |
1906 | * page back on lru and drops the reference, the | |
1907 | * page will be freed anyway. It doesn't matter | |
1908 | * which lru it goes. So we don't bother checking | |
1909 | * PageDirty here. | |
1910 | */ | |
802a3a92 | 1911 | count_vm_event(PGLAZYFREED); |
2262185c | 1912 | count_memcg_page_event(page, PGLAZYFREED); |
be7c07d6 | 1913 | } else if (!mapping || !__remove_mapping(mapping, folio, true, |
b910718a | 1914 | sc->target_mem_cgroup)) |
802a3a92 | 1915 | goto keep_locked; |
9a1ea439 HD |
1916 | |
1917 | unlock_page(page); | |
e286781d | 1918 | free_it: |
98879b3b YS |
1919 | /* |
1920 | * THP may get swapped out in a whole, need account | |
1921 | * all base pages. | |
1922 | */ | |
1923 | nr_reclaimed += nr_pages; | |
abe4c3b5 MG |
1924 | |
1925 | /* | |
1926 | * Is there need to periodically free_page_list? It would | |
1927 | * appear not as the counts should be low | |
1928 | */ | |
7ae88534 | 1929 | if (unlikely(PageTransHuge(page))) |
ff45fc3c | 1930 | destroy_compound_page(page); |
7ae88534 | 1931 | else |
bd4c82c2 | 1932 | list_add(&page->lru, &free_pages); |
1da177e4 LT |
1933 | continue; |
1934 | ||
98879b3b YS |
1935 | activate_locked_split: |
1936 | /* | |
1937 | * The tail pages that are failed to add into swap cache | |
1938 | * reach here. Fixup nr_scanned and nr_pages. | |
1939 | */ | |
1940 | if (nr_pages > 1) { | |
1941 | sc->nr_scanned -= (nr_pages - 1); | |
1942 | nr_pages = 1; | |
1943 | } | |
1da177e4 | 1944 | activate_locked: |
68a22394 | 1945 | /* Not a candidate for swapping, so reclaim swap space. */ |
ad6b6704 MK |
1946 | if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || |
1947 | PageMlocked(page))) | |
a2c43eed | 1948 | try_to_free_swap(page); |
309381fe | 1949 | VM_BUG_ON_PAGE(PageActive(page), page); |
ad6b6704 | 1950 | if (!PageMlocked(page)) { |
9de4f22a | 1951 | int type = page_is_file_lru(page); |
ad6b6704 | 1952 | SetPageActive(page); |
98879b3b | 1953 | stat->nr_activate[type] += nr_pages; |
2262185c | 1954 | count_memcg_page_event(page, PGACTIVATE); |
ad6b6704 | 1955 | } |
1da177e4 LT |
1956 | keep_locked: |
1957 | unlock_page(page); | |
1958 | keep: | |
1959 | list_add(&page->lru, &ret_pages); | |
309381fe | 1960 | VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); |
1da177e4 | 1961 | } |
26aa2d19 DH |
1962 | /* 'page_list' is always empty here */ |
1963 | ||
1964 | /* Migrate pages selected for demotion */ | |
1965 | nr_reclaimed += demote_page_list(&demote_pages, pgdat); | |
1966 | /* Pages that could not be demoted are still in @demote_pages */ | |
1967 | if (!list_empty(&demote_pages)) { | |
1968 | /* Pages which failed to demoted go back on @page_list for retry: */ | |
1969 | list_splice_init(&demote_pages, page_list); | |
1970 | do_demote_pass = false; | |
1971 | goto retry; | |
1972 | } | |
abe4c3b5 | 1973 | |
98879b3b YS |
1974 | pgactivate = stat->nr_activate[0] + stat->nr_activate[1]; |
1975 | ||
747db954 | 1976 | mem_cgroup_uncharge_list(&free_pages); |
72b252ae | 1977 | try_to_unmap_flush(); |
2d4894b5 | 1978 | free_unref_page_list(&free_pages); |
abe4c3b5 | 1979 | |
1da177e4 | 1980 | list_splice(&ret_pages, page_list); |
886cf190 | 1981 | count_vm_events(PGACTIVATE, pgactivate); |
060f005f | 1982 | |
2282679f N |
1983 | if (plug) |
1984 | swap_write_unplug(plug); | |
05ff5137 | 1985 | return nr_reclaimed; |
1da177e4 LT |
1986 | } |
1987 | ||
730ec8c0 | 1988 | unsigned int reclaim_clean_pages_from_list(struct zone *zone, |
02c6de8d MK |
1989 | struct list_head *page_list) |
1990 | { | |
1991 | struct scan_control sc = { | |
1992 | .gfp_mask = GFP_KERNEL, | |
02c6de8d MK |
1993 | .may_unmap = 1, |
1994 | }; | |
1f318a9b | 1995 | struct reclaim_stat stat; |
730ec8c0 | 1996 | unsigned int nr_reclaimed; |
02c6de8d MK |
1997 | struct page *page, *next; |
1998 | LIST_HEAD(clean_pages); | |
2d2b8d2b | 1999 | unsigned int noreclaim_flag; |
02c6de8d MK |
2000 | |
2001 | list_for_each_entry_safe(page, next, page_list, lru) { | |
ae37c7ff OS |
2002 | if (!PageHuge(page) && page_is_file_lru(page) && |
2003 | !PageDirty(page) && !__PageMovable(page) && | |
2004 | !PageUnevictable(page)) { | |
02c6de8d MK |
2005 | ClearPageActive(page); |
2006 | list_move(&page->lru, &clean_pages); | |
2007 | } | |
2008 | } | |
2009 | ||
2d2b8d2b YZ |
2010 | /* |
2011 | * We should be safe here since we are only dealing with file pages and | |
2012 | * we are not kswapd and therefore cannot write dirty file pages. But | |
2013 | * call memalloc_noreclaim_save() anyway, just in case these conditions | |
2014 | * change in the future. | |
2015 | */ | |
2016 | noreclaim_flag = memalloc_noreclaim_save(); | |
1f318a9b | 2017 | nr_reclaimed = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, |
013339df | 2018 | &stat, true); |
2d2b8d2b YZ |
2019 | memalloc_noreclaim_restore(noreclaim_flag); |
2020 | ||
02c6de8d | 2021 | list_splice(&clean_pages, page_list); |
2da9f630 NP |
2022 | mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, |
2023 | -(long)nr_reclaimed); | |
1f318a9b JK |
2024 | /* |
2025 | * Since lazyfree pages are isolated from file LRU from the beginning, | |
2026 | * they will rotate back to anonymous LRU in the end if it failed to | |
2027 | * discard so isolated count will be mismatched. | |
2028 | * Compensate the isolated count for both LRU lists. | |
2029 | */ | |
2030 | mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON, | |
2031 | stat.nr_lazyfree_fail); | |
2032 | mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, | |
2da9f630 | 2033 | -(long)stat.nr_lazyfree_fail); |
1f318a9b | 2034 | return nr_reclaimed; |
02c6de8d MK |
2035 | } |
2036 | ||
7ee36a14 MG |
2037 | /* |
2038 | * Update LRU sizes after isolating pages. The LRU size updates must | |
55b65a57 | 2039 | * be complete before mem_cgroup_update_lru_size due to a sanity check. |
7ee36a14 MG |
2040 | */ |
2041 | static __always_inline void update_lru_sizes(struct lruvec *lruvec, | |
b4536f0c | 2042 | enum lru_list lru, unsigned long *nr_zone_taken) |
7ee36a14 | 2043 | { |
7ee36a14 MG |
2044 | int zid; |
2045 | ||
7ee36a14 MG |
2046 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
2047 | if (!nr_zone_taken[zid]) | |
2048 | continue; | |
2049 | ||
a892cb6b | 2050 | update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); |
b4536f0c MH |
2051 | } |
2052 | ||
7ee36a14 MG |
2053 | } |
2054 | ||
f611fab7 | 2055 | /* |
15b44736 HD |
2056 | * Isolating page from the lruvec to fill in @dst list by nr_to_scan times. |
2057 | * | |
2058 | * lruvec->lru_lock is heavily contended. Some of the functions that | |
1da177e4 LT |
2059 | * shrink the lists perform better by taking out a batch of pages |
2060 | * and working on them outside the LRU lock. | |
2061 | * | |
2062 | * For pagecache intensive workloads, this function is the hottest | |
2063 | * spot in the kernel (apart from copy_*_user functions). | |
2064 | * | |
15b44736 | 2065 | * Lru_lock must be held before calling this function. |
1da177e4 | 2066 | * |
791b48b6 | 2067 | * @nr_to_scan: The number of eligible pages to look through on the list. |
5dc35979 | 2068 | * @lruvec: The LRU vector to pull pages from. |
1da177e4 | 2069 | * @dst: The temp list to put pages on to. |
f626012d | 2070 | * @nr_scanned: The number of pages that were scanned. |
fe2c2a10 | 2071 | * @sc: The scan_control struct for this reclaim session |
3cb99451 | 2072 | * @lru: LRU list id for isolating |
1da177e4 LT |
2073 | * |
2074 | * returns how many pages were moved onto *@dst. | |
2075 | */ | |
69e05944 | 2076 | static unsigned long isolate_lru_pages(unsigned long nr_to_scan, |
5dc35979 | 2077 | struct lruvec *lruvec, struct list_head *dst, |
fe2c2a10 | 2078 | unsigned long *nr_scanned, struct scan_control *sc, |
a9e7c39f | 2079 | enum lru_list lru) |
1da177e4 | 2080 | { |
75b00af7 | 2081 | struct list_head *src = &lruvec->lists[lru]; |
69e05944 | 2082 | unsigned long nr_taken = 0; |
599d0c95 | 2083 | unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; |
7cc30fcf | 2084 | unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; |
3db65812 | 2085 | unsigned long skipped = 0; |
791b48b6 | 2086 | unsigned long scan, total_scan, nr_pages; |
b2e18757 | 2087 | LIST_HEAD(pages_skipped); |
1da177e4 | 2088 | |
98879b3b | 2089 | total_scan = 0; |
791b48b6 | 2090 | scan = 0; |
98879b3b | 2091 | while (scan < nr_to_scan && !list_empty(src)) { |
89f6c88a | 2092 | struct list_head *move_to = src; |
5ad333eb | 2093 | struct page *page; |
5ad333eb | 2094 | |
1da177e4 LT |
2095 | page = lru_to_page(src); |
2096 | prefetchw_prev_lru_page(page, src, flags); | |
2097 | ||
d8c6546b | 2098 | nr_pages = compound_nr(page); |
98879b3b YS |
2099 | total_scan += nr_pages; |
2100 | ||
b2e18757 | 2101 | if (page_zonenum(page) > sc->reclaim_idx) { |
98879b3b | 2102 | nr_skipped[page_zonenum(page)] += nr_pages; |
89f6c88a HD |
2103 | move_to = &pages_skipped; |
2104 | goto move; | |
b2e18757 MG |
2105 | } |
2106 | ||
791b48b6 MK |
2107 | /* |
2108 | * Do not count skipped pages because that makes the function | |
2109 | * return with no isolated pages if the LRU mostly contains | |
2110 | * ineligible pages. This causes the VM to not reclaim any | |
2111 | * pages, triggering a premature OOM. | |
89f6c88a | 2112 | * Account all tail pages of THP. |
791b48b6 | 2113 | */ |
98879b3b | 2114 | scan += nr_pages; |
89f6c88a HD |
2115 | |
2116 | if (!PageLRU(page)) | |
2117 | goto move; | |
2118 | if (!sc->may_unmap && page_mapped(page)) | |
2119 | goto move; | |
2120 | ||
c2135f7c AS |
2121 | /* |
2122 | * Be careful not to clear PageLRU until after we're | |
2123 | * sure the page is not being freed elsewhere -- the | |
2124 | * page release code relies on it. | |
2125 | */ | |
89f6c88a HD |
2126 | if (unlikely(!get_page_unless_zero(page))) |
2127 | goto move; | |
5ad333eb | 2128 | |
c2135f7c AS |
2129 | if (!TestClearPageLRU(page)) { |
2130 | /* Another thread is already isolating this page */ | |
2131 | put_page(page); | |
89f6c88a | 2132 | goto move; |
5ad333eb | 2133 | } |
c2135f7c AS |
2134 | |
2135 | nr_taken += nr_pages; | |
2136 | nr_zone_taken[page_zonenum(page)] += nr_pages; | |
89f6c88a HD |
2137 | move_to = dst; |
2138 | move: | |
2139 | list_move(&page->lru, move_to); | |
1da177e4 LT |
2140 | } |
2141 | ||
b2e18757 MG |
2142 | /* |
2143 | * Splice any skipped pages to the start of the LRU list. Note that | |
2144 | * this disrupts the LRU order when reclaiming for lower zones but | |
2145 | * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX | |
b2cb6826 ML |
2146 | * scanning would soon rescan the same pages to skip and waste lots |
2147 | * of cpu cycles. | |
b2e18757 | 2148 | */ |
7cc30fcf MG |
2149 | if (!list_empty(&pages_skipped)) { |
2150 | int zid; | |
2151 | ||
3db65812 | 2152 | list_splice(&pages_skipped, src); |
7cc30fcf MG |
2153 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
2154 | if (!nr_skipped[zid]) | |
2155 | continue; | |
2156 | ||
2157 | __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); | |
1265e3a6 | 2158 | skipped += nr_skipped[zid]; |
7cc30fcf MG |
2159 | } |
2160 | } | |
791b48b6 | 2161 | *nr_scanned = total_scan; |
1265e3a6 | 2162 | trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, |
89f6c88a HD |
2163 | total_scan, skipped, nr_taken, |
2164 | sc->may_unmap ? 0 : ISOLATE_UNMAPPED, lru); | |
b4536f0c | 2165 | update_lru_sizes(lruvec, lru, nr_zone_taken); |
1da177e4 LT |
2166 | return nr_taken; |
2167 | } | |
2168 | ||
62695a84 | 2169 | /** |
d1d8a3b4 MWO |
2170 | * folio_isolate_lru() - Try to isolate a folio from its LRU list. |
2171 | * @folio: Folio to isolate from its LRU list. | |
62695a84 | 2172 | * |
d1d8a3b4 MWO |
2173 | * Isolate a @folio from an LRU list and adjust the vmstat statistic |
2174 | * corresponding to whatever LRU list the folio was on. | |
62695a84 | 2175 | * |
d1d8a3b4 MWO |
2176 | * The folio will have its LRU flag cleared. If it was found on the |
2177 | * active list, it will have the Active flag set. If it was found on the | |
2178 | * unevictable list, it will have the Unevictable flag set. These flags | |
894bc310 | 2179 | * may need to be cleared by the caller before letting the page go. |
62695a84 | 2180 | * |
d1d8a3b4 | 2181 | * Context: |
a5d09bed | 2182 | * |
62695a84 | 2183 | * (1) Must be called with an elevated refcount on the page. This is a |
d1d8a3b4 | 2184 | * fundamental difference from isolate_lru_pages() (which is called |
62695a84 | 2185 | * without a stable reference). |
d1d8a3b4 MWO |
2186 | * (2) The lru_lock must not be held. |
2187 | * (3) Interrupts must be enabled. | |
2188 | * | |
2189 | * Return: 0 if the folio was removed from an LRU list. | |
2190 | * -EBUSY if the folio was not on an LRU list. | |
62695a84 | 2191 | */ |
d1d8a3b4 | 2192 | int folio_isolate_lru(struct folio *folio) |
62695a84 NP |
2193 | { |
2194 | int ret = -EBUSY; | |
2195 | ||
d1d8a3b4 | 2196 | VM_BUG_ON_FOLIO(!folio_ref_count(folio), folio); |
0c917313 | 2197 | |
d1d8a3b4 | 2198 | if (folio_test_clear_lru(folio)) { |
fa9add64 | 2199 | struct lruvec *lruvec; |
62695a84 | 2200 | |
d1d8a3b4 | 2201 | folio_get(folio); |
e809c3fe | 2202 | lruvec = folio_lruvec_lock_irq(folio); |
d1d8a3b4 | 2203 | lruvec_del_folio(lruvec, folio); |
6168d0da | 2204 | unlock_page_lruvec_irq(lruvec); |
d25b5bd8 | 2205 | ret = 0; |
62695a84 | 2206 | } |
d25b5bd8 | 2207 | |
62695a84 NP |
2208 | return ret; |
2209 | } | |
2210 | ||
35cd7815 | 2211 | /* |
d37dd5dc | 2212 | * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and |
178821b8 | 2213 | * then get rescheduled. When there are massive number of tasks doing page |
d37dd5dc FW |
2214 | * allocation, such sleeping direct reclaimers may keep piling up on each CPU, |
2215 | * the LRU list will go small and be scanned faster than necessary, leading to | |
2216 | * unnecessary swapping, thrashing and OOM. | |
35cd7815 | 2217 | */ |
599d0c95 | 2218 | static int too_many_isolated(struct pglist_data *pgdat, int file, |
35cd7815 RR |
2219 | struct scan_control *sc) |
2220 | { | |
2221 | unsigned long inactive, isolated; | |
d818fca1 | 2222 | bool too_many; |
35cd7815 RR |
2223 | |
2224 | if (current_is_kswapd()) | |
2225 | return 0; | |
2226 | ||
b5ead35e | 2227 | if (!writeback_throttling_sane(sc)) |
35cd7815 RR |
2228 | return 0; |
2229 | ||
2230 | if (file) { | |
599d0c95 MG |
2231 | inactive = node_page_state(pgdat, NR_INACTIVE_FILE); |
2232 | isolated = node_page_state(pgdat, NR_ISOLATED_FILE); | |
35cd7815 | 2233 | } else { |
599d0c95 MG |
2234 | inactive = node_page_state(pgdat, NR_INACTIVE_ANON); |
2235 | isolated = node_page_state(pgdat, NR_ISOLATED_ANON); | |
35cd7815 RR |
2236 | } |
2237 | ||
3cf23841 FW |
2238 | /* |
2239 | * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they | |
2240 | * won't get blocked by normal direct-reclaimers, forming a circular | |
2241 | * deadlock. | |
2242 | */ | |
d0164adc | 2243 | if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) |
3cf23841 FW |
2244 | inactive >>= 3; |
2245 | ||
d818fca1 MG |
2246 | too_many = isolated > inactive; |
2247 | ||
2248 | /* Wake up tasks throttled due to too_many_isolated. */ | |
2249 | if (!too_many) | |
2250 | wake_throttle_isolated(pgdat); | |
2251 | ||
2252 | return too_many; | |
35cd7815 RR |
2253 | } |
2254 | ||
a222f341 | 2255 | /* |
15b44736 HD |
2256 | * move_pages_to_lru() moves pages from private @list to appropriate LRU list. |
2257 | * On return, @list is reused as a list of pages to be freed by the caller. | |
a222f341 KT |
2258 | * |
2259 | * Returns the number of pages moved to the given lruvec. | |
2260 | */ | |
9ef56b78 MS |
2261 | static unsigned int move_pages_to_lru(struct lruvec *lruvec, |
2262 | struct list_head *list) | |
66635629 | 2263 | { |
a222f341 | 2264 | int nr_pages, nr_moved = 0; |
3f79768f | 2265 | LIST_HEAD(pages_to_free); |
a222f341 | 2266 | struct page *page; |
66635629 | 2267 | |
a222f341 KT |
2268 | while (!list_empty(list)) { |
2269 | page = lru_to_page(list); | |
309381fe | 2270 | VM_BUG_ON_PAGE(PageLRU(page), page); |
3d06afab | 2271 | list_del(&page->lru); |
39b5f29a | 2272 | if (unlikely(!page_evictable(page))) { |
6168d0da | 2273 | spin_unlock_irq(&lruvec->lru_lock); |
66635629 | 2274 | putback_lru_page(page); |
6168d0da | 2275 | spin_lock_irq(&lruvec->lru_lock); |
66635629 MG |
2276 | continue; |
2277 | } | |
fa9add64 | 2278 | |
3d06afab AS |
2279 | /* |
2280 | * The SetPageLRU needs to be kept here for list integrity. | |
2281 | * Otherwise: | |
2282 | * #0 move_pages_to_lru #1 release_pages | |
2283 | * if !put_page_testzero | |
2284 | * if (put_page_testzero()) | |
2285 | * !PageLRU //skip lru_lock | |
2286 | * SetPageLRU() | |
2287 | * list_add(&page->lru,) | |
2288 | * list_add(&page->lru,) | |
2289 | */ | |
7a608572 | 2290 | SetPageLRU(page); |
a222f341 | 2291 | |
3d06afab | 2292 | if (unlikely(put_page_testzero(page))) { |
87560179 | 2293 | __clear_page_lru_flags(page); |
2bcf8879 HD |
2294 | |
2295 | if (unlikely(PageCompound(page))) { | |
6168d0da | 2296 | spin_unlock_irq(&lruvec->lru_lock); |
ff45fc3c | 2297 | destroy_compound_page(page); |
6168d0da | 2298 | spin_lock_irq(&lruvec->lru_lock); |
2bcf8879 HD |
2299 | } else |
2300 | list_add(&page->lru, &pages_to_free); | |
3d06afab AS |
2301 | |
2302 | continue; | |
66635629 | 2303 | } |
3d06afab | 2304 | |
afca9157 AS |
2305 | /* |
2306 | * All pages were isolated from the same lruvec (and isolation | |
2307 | * inhibits memcg migration). | |
2308 | */ | |
0de340cb | 2309 | VM_BUG_ON_PAGE(!folio_matches_lruvec(page_folio(page), lruvec), page); |
3a9c9788 | 2310 | add_page_to_lru_list(page, lruvec); |
3d06afab | 2311 | nr_pages = thp_nr_pages(page); |
3d06afab AS |
2312 | nr_moved += nr_pages; |
2313 | if (PageActive(page)) | |
2314 | workingset_age_nonresident(lruvec, nr_pages); | |
66635629 | 2315 | } |
66635629 | 2316 | |
3f79768f HD |
2317 | /* |
2318 | * To save our caller's stack, now use input list for pages to free. | |
2319 | */ | |
a222f341 KT |
2320 | list_splice(&pages_to_free, list); |
2321 | ||
2322 | return nr_moved; | |
66635629 MG |
2323 | } |
2324 | ||
399ba0b9 | 2325 | /* |
5829f7db ML |
2326 | * If a kernel thread (such as nfsd for loop-back mounts) services a backing |
2327 | * device by writing to the page cache it sets PF_LOCAL_THROTTLE. In this case | |
2328 | * we should not throttle. Otherwise it is safe to do so. | |
399ba0b9 N |
2329 | */ |
2330 | static int current_may_throttle(void) | |
2331 | { | |
b9b1335e | 2332 | return !(current->flags & PF_LOCAL_THROTTLE); |
399ba0b9 N |
2333 | } |
2334 | ||
1da177e4 | 2335 | /* |
b2e18757 | 2336 | * shrink_inactive_list() is a helper for shrink_node(). It returns the number |
1742f19f | 2337 | * of reclaimed pages |
1da177e4 | 2338 | */ |
9ef56b78 | 2339 | static unsigned long |
1a93be0e | 2340 | shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, |
9e3b2f8c | 2341 | struct scan_control *sc, enum lru_list lru) |
1da177e4 LT |
2342 | { |
2343 | LIST_HEAD(page_list); | |
e247dbce | 2344 | unsigned long nr_scanned; |
730ec8c0 | 2345 | unsigned int nr_reclaimed = 0; |
e247dbce | 2346 | unsigned long nr_taken; |
060f005f | 2347 | struct reclaim_stat stat; |
497a6c1b | 2348 | bool file = is_file_lru(lru); |
f46b7912 | 2349 | enum vm_event_item item; |
599d0c95 | 2350 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
db73ee0d | 2351 | bool stalled = false; |
78dc583d | 2352 | |
599d0c95 | 2353 | while (unlikely(too_many_isolated(pgdat, file, sc))) { |
db73ee0d MH |
2354 | if (stalled) |
2355 | return 0; | |
2356 | ||
2357 | /* wait a bit for the reclaimer. */ | |
db73ee0d | 2358 | stalled = true; |
c3f4a9a2 | 2359 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_ISOLATED); |
35cd7815 RR |
2360 | |
2361 | /* We are about to die and free our memory. Return now. */ | |
2362 | if (fatal_signal_pending(current)) | |
2363 | return SWAP_CLUSTER_MAX; | |
2364 | } | |
2365 | ||
1da177e4 | 2366 | lru_add_drain(); |
f80c0673 | 2367 | |
6168d0da | 2368 | spin_lock_irq(&lruvec->lru_lock); |
b35ea17b | 2369 | |
5dc35979 | 2370 | nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, |
a9e7c39f | 2371 | &nr_scanned, sc, lru); |
95d918fc | 2372 | |
599d0c95 | 2373 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); |
f46b7912 | 2374 | item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT; |
b5ead35e | 2375 | if (!cgroup_reclaim(sc)) |
f46b7912 KT |
2376 | __count_vm_events(item, nr_scanned); |
2377 | __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned); | |
497a6c1b JW |
2378 | __count_vm_events(PGSCAN_ANON + file, nr_scanned); |
2379 | ||
6168d0da | 2380 | spin_unlock_irq(&lruvec->lru_lock); |
b35ea17b | 2381 | |
d563c050 | 2382 | if (nr_taken == 0) |
66635629 | 2383 | return 0; |
5ad333eb | 2384 | |
013339df | 2385 | nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, &stat, false); |
c661b078 | 2386 | |
6168d0da | 2387 | spin_lock_irq(&lruvec->lru_lock); |
497a6c1b JW |
2388 | move_pages_to_lru(lruvec, &page_list); |
2389 | ||
2390 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); | |
f46b7912 | 2391 | item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT; |
b5ead35e | 2392 | if (!cgroup_reclaim(sc)) |
f46b7912 KT |
2393 | __count_vm_events(item, nr_reclaimed); |
2394 | __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed); | |
497a6c1b | 2395 | __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed); |
6168d0da | 2396 | spin_unlock_irq(&lruvec->lru_lock); |
3f79768f | 2397 | |
75cc3c91 | 2398 | lru_note_cost(lruvec, file, stat.nr_pageout); |
747db954 | 2399 | mem_cgroup_uncharge_list(&page_list); |
2d4894b5 | 2400 | free_unref_page_list(&page_list); |
e11da5b4 | 2401 | |
1c610d5f AR |
2402 | /* |
2403 | * If dirty pages are scanned that are not queued for IO, it | |
2404 | * implies that flushers are not doing their job. This can | |
2405 | * happen when memory pressure pushes dirty pages to the end of | |
2406 | * the LRU before the dirty limits are breached and the dirty | |
2407 | * data has expired. It can also happen when the proportion of | |
2408 | * dirty pages grows not through writes but through memory | |
2409 | * pressure reclaiming all the clean cache. And in some cases, | |
2410 | * the flushers simply cannot keep up with the allocation | |
2411 | * rate. Nudge the flusher threads in case they are asleep. | |
2412 | */ | |
2413 | if (stat.nr_unqueued_dirty == nr_taken) | |
2414 | wakeup_flusher_threads(WB_REASON_VMSCAN); | |
2415 | ||
d108c772 AR |
2416 | sc->nr.dirty += stat.nr_dirty; |
2417 | sc->nr.congested += stat.nr_congested; | |
2418 | sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; | |
2419 | sc->nr.writeback += stat.nr_writeback; | |
2420 | sc->nr.immediate += stat.nr_immediate; | |
2421 | sc->nr.taken += nr_taken; | |
2422 | if (file) | |
2423 | sc->nr.file_taken += nr_taken; | |
8e950282 | 2424 | |
599d0c95 | 2425 | trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, |
d51d1e64 | 2426 | nr_scanned, nr_reclaimed, &stat, sc->priority, file); |
05ff5137 | 2427 | return nr_reclaimed; |
1da177e4 LT |
2428 | } |
2429 | ||
15b44736 HD |
2430 | /* |
2431 | * shrink_active_list() moves pages from the active LRU to the inactive LRU. | |
2432 | * | |
2433 | * We move them the other way if the page is referenced by one or more | |
2434 | * processes. | |
2435 | * | |
2436 | * If the pages are mostly unmapped, the processing is fast and it is | |
2437 | * appropriate to hold lru_lock across the whole operation. But if | |
b3ac0413 | 2438 | * the pages are mapped, the processing is slow (folio_referenced()), so |
15b44736 HD |
2439 | * we should drop lru_lock around each page. It's impossible to balance |
2440 | * this, so instead we remove the pages from the LRU while processing them. | |
2441 | * It is safe to rely on PG_active against the non-LRU pages in here because | |
2442 | * nobody will play with that bit on a non-LRU page. | |
2443 | * | |
2444 | * The downside is that we have to touch page->_refcount against each page. | |
2445 | * But we had to alter page->flags anyway. | |
2446 | */ | |
f626012d | 2447 | static void shrink_active_list(unsigned long nr_to_scan, |
1a93be0e | 2448 | struct lruvec *lruvec, |
f16015fb | 2449 | struct scan_control *sc, |
9e3b2f8c | 2450 | enum lru_list lru) |
1da177e4 | 2451 | { |
44c241f1 | 2452 | unsigned long nr_taken; |
f626012d | 2453 | unsigned long nr_scanned; |
6fe6b7e3 | 2454 | unsigned long vm_flags; |
1da177e4 | 2455 | LIST_HEAD(l_hold); /* The pages which were snipped off */ |
8cab4754 | 2456 | LIST_HEAD(l_active); |
b69408e8 | 2457 | LIST_HEAD(l_inactive); |
9d998b4f MH |
2458 | unsigned nr_deactivate, nr_activate; |
2459 | unsigned nr_rotated = 0; | |
3cb99451 | 2460 | int file = is_file_lru(lru); |
599d0c95 | 2461 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
1da177e4 LT |
2462 | |
2463 | lru_add_drain(); | |
f80c0673 | 2464 | |
6168d0da | 2465 | spin_lock_irq(&lruvec->lru_lock); |
925b7673 | 2466 | |
5dc35979 | 2467 | nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, |
a9e7c39f | 2468 | &nr_scanned, sc, lru); |
89b5fae5 | 2469 | |
599d0c95 | 2470 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); |
1cfb419b | 2471 | |
912c0572 SB |
2472 | if (!cgroup_reclaim(sc)) |
2473 | __count_vm_events(PGREFILL, nr_scanned); | |
2fa2690c | 2474 | __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); |
9d5e6a9f | 2475 | |
6168d0da | 2476 | spin_unlock_irq(&lruvec->lru_lock); |
1da177e4 | 2477 | |
1da177e4 | 2478 | while (!list_empty(&l_hold)) { |
b3ac0413 MWO |
2479 | struct folio *folio; |
2480 | struct page *page; | |
2481 | ||
1da177e4 | 2482 | cond_resched(); |
b3ac0413 MWO |
2483 | folio = lru_to_folio(&l_hold); |
2484 | list_del(&folio->lru); | |
2485 | page = &folio->page; | |
7e9cd484 | 2486 | |
39b5f29a | 2487 | if (unlikely(!page_evictable(page))) { |
894bc310 LS |
2488 | putback_lru_page(page); |
2489 | continue; | |
2490 | } | |
2491 | ||
cc715d99 MG |
2492 | if (unlikely(buffer_heads_over_limit)) { |
2493 | if (page_has_private(page) && trylock_page(page)) { | |
2494 | if (page_has_private(page)) | |
2495 | try_to_release_page(page, 0); | |
2496 | unlock_page(page); | |
2497 | } | |
2498 | } | |
2499 | ||
b3ac0413 MWO |
2500 | if (folio_referenced(folio, 0, sc->target_mem_cgroup, |
2501 | &vm_flags)) { | |
8cab4754 WF |
2502 | /* |
2503 | * Identify referenced, file-backed active pages and | |
2504 | * give them one more trip around the active list. So | |
2505 | * that executable code get better chances to stay in | |
2506 | * memory under moderate memory pressure. Anon pages | |
2507 | * are not likely to be evicted by use-once streaming | |
2508 | * IO, plus JVM can create lots of anon VM_EXEC pages, | |
2509 | * so we ignore them here. | |
2510 | */ | |
9de4f22a | 2511 | if ((vm_flags & VM_EXEC) && page_is_file_lru(page)) { |
6c357848 | 2512 | nr_rotated += thp_nr_pages(page); |
8cab4754 WF |
2513 | list_add(&page->lru, &l_active); |
2514 | continue; | |
2515 | } | |
2516 | } | |
7e9cd484 | 2517 | |
5205e56e | 2518 | ClearPageActive(page); /* we are de-activating */ |
1899ad18 | 2519 | SetPageWorkingset(page); |
1da177e4 LT |
2520 | list_add(&page->lru, &l_inactive); |
2521 | } | |
2522 | ||
b555749a | 2523 | /* |
8cab4754 | 2524 | * Move pages back to the lru list. |
b555749a | 2525 | */ |
6168d0da | 2526 | spin_lock_irq(&lruvec->lru_lock); |
556adecb | 2527 | |
a222f341 KT |
2528 | nr_activate = move_pages_to_lru(lruvec, &l_active); |
2529 | nr_deactivate = move_pages_to_lru(lruvec, &l_inactive); | |
f372d89e KT |
2530 | /* Keep all free pages in l_active list */ |
2531 | list_splice(&l_inactive, &l_active); | |
9851ac13 KT |
2532 | |
2533 | __count_vm_events(PGDEACTIVATE, nr_deactivate); | |
2534 | __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate); | |
2535 | ||
599d0c95 | 2536 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); |
6168d0da | 2537 | spin_unlock_irq(&lruvec->lru_lock); |
2bcf8879 | 2538 | |
f372d89e KT |
2539 | mem_cgroup_uncharge_list(&l_active); |
2540 | free_unref_page_list(&l_active); | |
9d998b4f MH |
2541 | trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, |
2542 | nr_deactivate, nr_rotated, sc->priority, file); | |
1da177e4 LT |
2543 | } |
2544 | ||
1fe47c0b ML |
2545 | static unsigned int reclaim_page_list(struct list_head *page_list, |
2546 | struct pglist_data *pgdat) | |
1a4e58cc | 2547 | { |
1a4e58cc | 2548 | struct reclaim_stat dummy_stat; |
1fe47c0b ML |
2549 | unsigned int nr_reclaimed; |
2550 | struct folio *folio; | |
1a4e58cc MK |
2551 | struct scan_control sc = { |
2552 | .gfp_mask = GFP_KERNEL, | |
1a4e58cc MK |
2553 | .may_writepage = 1, |
2554 | .may_unmap = 1, | |
2555 | .may_swap = 1, | |
26aa2d19 | 2556 | .no_demotion = 1, |
1a4e58cc MK |
2557 | }; |
2558 | ||
1fe47c0b ML |
2559 | nr_reclaimed = shrink_page_list(page_list, pgdat, &sc, &dummy_stat, false); |
2560 | while (!list_empty(page_list)) { | |
2561 | folio = lru_to_folio(page_list); | |
2562 | list_del(&folio->lru); | |
2563 | folio_putback_lru(folio); | |
2564 | } | |
2565 | ||
2566 | return nr_reclaimed; | |
2567 | } | |
2568 | ||
2569 | unsigned long reclaim_pages(struct list_head *page_list) | |
2570 | { | |
ed657e55 | 2571 | int nid; |
1fe47c0b ML |
2572 | unsigned int nr_reclaimed = 0; |
2573 | LIST_HEAD(node_page_list); | |
2574 | struct page *page; | |
2575 | unsigned int noreclaim_flag; | |
2576 | ||
1ae65e27 WY |
2577 | if (list_empty(page_list)) |
2578 | return nr_reclaimed; | |
2579 | ||
2d2b8d2b YZ |
2580 | noreclaim_flag = memalloc_noreclaim_save(); |
2581 | ||
ed657e55 | 2582 | nid = page_to_nid(lru_to_page(page_list)); |
1ae65e27 | 2583 | do { |
1a4e58cc | 2584 | page = lru_to_page(page_list); |
1a4e58cc MK |
2585 | |
2586 | if (nid == page_to_nid(page)) { | |
2587 | ClearPageActive(page); | |
2588 | list_move(&page->lru, &node_page_list); | |
2589 | continue; | |
2590 | } | |
2591 | ||
1fe47c0b | 2592 | nr_reclaimed += reclaim_page_list(&node_page_list, NODE_DATA(nid)); |
ed657e55 | 2593 | nid = page_to_nid(lru_to_page(page_list)); |
1ae65e27 | 2594 | } while (!list_empty(page_list)); |
1a4e58cc | 2595 | |
1ae65e27 | 2596 | nr_reclaimed += reclaim_page_list(&node_page_list, NODE_DATA(nid)); |
1a4e58cc | 2597 | |
2d2b8d2b YZ |
2598 | memalloc_noreclaim_restore(noreclaim_flag); |
2599 | ||
1a4e58cc MK |
2600 | return nr_reclaimed; |
2601 | } | |
2602 | ||
b91ac374 JW |
2603 | static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, |
2604 | struct lruvec *lruvec, struct scan_control *sc) | |
2605 | { | |
2606 | if (is_active_lru(lru)) { | |
2607 | if (sc->may_deactivate & (1 << is_file_lru(lru))) | |
2608 | shrink_active_list(nr_to_scan, lruvec, sc, lru); | |
2609 | else | |
2610 | sc->skipped_deactivate = 1; | |
2611 | return 0; | |
2612 | } | |
2613 | ||
2614 | return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); | |
2615 | } | |
2616 | ||
59dc76b0 RR |
2617 | /* |
2618 | * The inactive anon list should be small enough that the VM never has | |
2619 | * to do too much work. | |
14797e23 | 2620 | * |
59dc76b0 RR |
2621 | * The inactive file list should be small enough to leave most memory |
2622 | * to the established workingset on the scan-resistant active list, | |
2623 | * but large enough to avoid thrashing the aggregate readahead window. | |
56e49d21 | 2624 | * |
59dc76b0 RR |
2625 | * Both inactive lists should also be large enough that each inactive |
2626 | * page has a chance to be referenced again before it is reclaimed. | |
56e49d21 | 2627 | * |
2a2e4885 JW |
2628 | * If that fails and refaulting is observed, the inactive list grows. |
2629 | * | |
59dc76b0 | 2630 | * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages |
3a50d14d | 2631 | * on this LRU, maintained by the pageout code. An inactive_ratio |
59dc76b0 | 2632 | * of 3 means 3:1 or 25% of the pages are kept on the inactive list. |
56e49d21 | 2633 | * |
59dc76b0 RR |
2634 | * total target max |
2635 | * memory ratio inactive | |
2636 | * ------------------------------------- | |
2637 | * 10MB 1 5MB | |
2638 | * 100MB 1 50MB | |
2639 | * 1GB 3 250MB | |
2640 | * 10GB 10 0.9GB | |
2641 | * 100GB 31 3GB | |
2642 | * 1TB 101 10GB | |
2643 | * 10TB 320 32GB | |
56e49d21 | 2644 | */ |
b91ac374 | 2645 | static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru) |
56e49d21 | 2646 | { |
b91ac374 | 2647 | enum lru_list active_lru = inactive_lru + LRU_ACTIVE; |
2a2e4885 JW |
2648 | unsigned long inactive, active; |
2649 | unsigned long inactive_ratio; | |
59dc76b0 | 2650 | unsigned long gb; |
e3790144 | 2651 | |
b91ac374 JW |
2652 | inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru); |
2653 | active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru); | |
f8d1a311 | 2654 | |
b91ac374 | 2655 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
4002570c | 2656 | if (gb) |
b91ac374 JW |
2657 | inactive_ratio = int_sqrt(10 * gb); |
2658 | else | |
2659 | inactive_ratio = 1; | |
fd538803 | 2660 | |
59dc76b0 | 2661 | return inactive * inactive_ratio < active; |
b39415b2 RR |
2662 | } |
2663 | ||
9a265114 JW |
2664 | enum scan_balance { |
2665 | SCAN_EQUAL, | |
2666 | SCAN_FRACT, | |
2667 | SCAN_ANON, | |
2668 | SCAN_FILE, | |
2669 | }; | |
2670 | ||
4f98a2fe RR |
2671 | /* |
2672 | * Determine how aggressively the anon and file LRU lists should be | |
02e458d8 | 2673 | * scanned. |
4f98a2fe | 2674 | * |
be7bd59d WL |
2675 | * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan |
2676 | * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan | |
4f98a2fe | 2677 | */ |
afaf07a6 JW |
2678 | static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, |
2679 | unsigned long *nr) | |
4f98a2fe | 2680 | { |
a2a36488 | 2681 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
afaf07a6 | 2682 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); |
d483a5dd | 2683 | unsigned long anon_cost, file_cost, total_cost; |
33377678 | 2684 | int swappiness = mem_cgroup_swappiness(memcg); |
ed017373 | 2685 | u64 fraction[ANON_AND_FILE]; |
9a265114 | 2686 | u64 denominator = 0; /* gcc */ |
9a265114 | 2687 | enum scan_balance scan_balance; |
4f98a2fe | 2688 | unsigned long ap, fp; |
4111304d | 2689 | enum lru_list lru; |
76a33fc3 SL |
2690 | |
2691 | /* If we have no swap space, do not bother scanning anon pages. */ | |
a2a36488 | 2692 | if (!sc->may_swap || !can_reclaim_anon_pages(memcg, pgdat->node_id, sc)) { |
9a265114 | 2693 | scan_balance = SCAN_FILE; |
76a33fc3 SL |
2694 | goto out; |
2695 | } | |
4f98a2fe | 2696 | |
10316b31 JW |
2697 | /* |
2698 | * Global reclaim will swap to prevent OOM even with no | |
2699 | * swappiness, but memcg users want to use this knob to | |
2700 | * disable swapping for individual groups completely when | |
2701 | * using the memory controller's swap limit feature would be | |
2702 | * too expensive. | |
2703 | */ | |
b5ead35e | 2704 | if (cgroup_reclaim(sc) && !swappiness) { |
9a265114 | 2705 | scan_balance = SCAN_FILE; |
10316b31 JW |
2706 | goto out; |
2707 | } | |
2708 | ||
2709 | /* | |
2710 | * Do not apply any pressure balancing cleverness when the | |
2711 | * system is close to OOM, scan both anon and file equally | |
2712 | * (unless the swappiness setting disagrees with swapping). | |
2713 | */ | |
02695175 | 2714 | if (!sc->priority && swappiness) { |
9a265114 | 2715 | scan_balance = SCAN_EQUAL; |
10316b31 JW |
2716 | goto out; |
2717 | } | |
2718 | ||
62376251 | 2719 | /* |
53138cea | 2720 | * If the system is almost out of file pages, force-scan anon. |
62376251 | 2721 | */ |
b91ac374 | 2722 | if (sc->file_is_tiny) { |
53138cea JW |
2723 | scan_balance = SCAN_ANON; |
2724 | goto out; | |
62376251 JW |
2725 | } |
2726 | ||
7c5bd705 | 2727 | /* |
b91ac374 JW |
2728 | * If there is enough inactive page cache, we do not reclaim |
2729 | * anything from the anonymous working right now. | |
7c5bd705 | 2730 | */ |
b91ac374 | 2731 | if (sc->cache_trim_mode) { |
9a265114 | 2732 | scan_balance = SCAN_FILE; |
7c5bd705 JW |
2733 | goto out; |
2734 | } | |
2735 | ||
9a265114 | 2736 | scan_balance = SCAN_FRACT; |
58c37f6e | 2737 | /* |
314b57fb JW |
2738 | * Calculate the pressure balance between anon and file pages. |
2739 | * | |
2740 | * The amount of pressure we put on each LRU is inversely | |
2741 | * proportional to the cost of reclaiming each list, as | |
2742 | * determined by the share of pages that are refaulting, times | |
2743 | * the relative IO cost of bringing back a swapped out | |
2744 | * anonymous page vs reloading a filesystem page (swappiness). | |
2745 | * | |
d483a5dd JW |
2746 | * Although we limit that influence to ensure no list gets |
2747 | * left behind completely: at least a third of the pressure is | |
2748 | * applied, before swappiness. | |
2749 | * | |
314b57fb | 2750 | * With swappiness at 100, anon and file have equal IO cost. |
58c37f6e | 2751 | */ |
d483a5dd JW |
2752 | total_cost = sc->anon_cost + sc->file_cost; |
2753 | anon_cost = total_cost + sc->anon_cost; | |
2754 | file_cost = total_cost + sc->file_cost; | |
2755 | total_cost = anon_cost + file_cost; | |
58c37f6e | 2756 | |
d483a5dd JW |
2757 | ap = swappiness * (total_cost + 1); |
2758 | ap /= anon_cost + 1; | |
4f98a2fe | 2759 | |
d483a5dd JW |
2760 | fp = (200 - swappiness) * (total_cost + 1); |
2761 | fp /= file_cost + 1; | |
4f98a2fe | 2762 | |
76a33fc3 SL |
2763 | fraction[0] = ap; |
2764 | fraction[1] = fp; | |
a4fe1631 | 2765 | denominator = ap + fp; |
76a33fc3 | 2766 | out: |
688035f7 JW |
2767 | for_each_evictable_lru(lru) { |
2768 | int file = is_file_lru(lru); | |
9783aa99 | 2769 | unsigned long lruvec_size; |
f56ce412 | 2770 | unsigned long low, min; |
688035f7 | 2771 | unsigned long scan; |
9783aa99 CD |
2772 | |
2773 | lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); | |
f56ce412 JW |
2774 | mem_cgroup_protection(sc->target_mem_cgroup, memcg, |
2775 | &min, &low); | |
9783aa99 | 2776 | |
f56ce412 | 2777 | if (min || low) { |
9783aa99 CD |
2778 | /* |
2779 | * Scale a cgroup's reclaim pressure by proportioning | |
2780 | * its current usage to its memory.low or memory.min | |
2781 | * setting. | |
2782 | * | |
2783 | * This is important, as otherwise scanning aggression | |
2784 | * becomes extremely binary -- from nothing as we | |
2785 | * approach the memory protection threshold, to totally | |
2786 | * nominal as we exceed it. This results in requiring | |
2787 | * setting extremely liberal protection thresholds. It | |
2788 | * also means we simply get no protection at all if we | |
2789 | * set it too low, which is not ideal. | |
1bc63fb1 CD |
2790 | * |
2791 | * If there is any protection in place, we reduce scan | |
2792 | * pressure by how much of the total memory used is | |
2793 | * within protection thresholds. | |
9783aa99 | 2794 | * |
9de7ca46 CD |
2795 | * There is one special case: in the first reclaim pass, |
2796 | * we skip over all groups that are within their low | |
2797 | * protection. If that fails to reclaim enough pages to | |
2798 | * satisfy the reclaim goal, we come back and override | |
2799 | * the best-effort low protection. However, we still | |
2800 | * ideally want to honor how well-behaved groups are in | |
2801 | * that case instead of simply punishing them all | |
2802 | * equally. As such, we reclaim them based on how much | |
1bc63fb1 CD |
2803 | * memory they are using, reducing the scan pressure |
2804 | * again by how much of the total memory used is under | |
2805 | * hard protection. | |
9783aa99 | 2806 | */ |
1bc63fb1 | 2807 | unsigned long cgroup_size = mem_cgroup_size(memcg); |
f56ce412 JW |
2808 | unsigned long protection; |
2809 | ||
2810 | /* memory.low scaling, make sure we retry before OOM */ | |
2811 | if (!sc->memcg_low_reclaim && low > min) { | |
2812 | protection = low; | |
2813 | sc->memcg_low_skipped = 1; | |
2814 | } else { | |
2815 | protection = min; | |
2816 | } | |
1bc63fb1 CD |
2817 | |
2818 | /* Avoid TOCTOU with earlier protection check */ | |
2819 | cgroup_size = max(cgroup_size, protection); | |
2820 | ||
2821 | scan = lruvec_size - lruvec_size * protection / | |
32d4f4b7 | 2822 | (cgroup_size + 1); |
9783aa99 CD |
2823 | |
2824 | /* | |
1bc63fb1 | 2825 | * Minimally target SWAP_CLUSTER_MAX pages to keep |
55b65a57 | 2826 | * reclaim moving forwards, avoiding decrementing |
9de7ca46 | 2827 | * sc->priority further than desirable. |
9783aa99 | 2828 | */ |
1bc63fb1 | 2829 | scan = max(scan, SWAP_CLUSTER_MAX); |
9783aa99 CD |
2830 | } else { |
2831 | scan = lruvec_size; | |
2832 | } | |
2833 | ||
2834 | scan >>= sc->priority; | |
6b4f7799 | 2835 | |
688035f7 JW |
2836 | /* |
2837 | * If the cgroup's already been deleted, make sure to | |
2838 | * scrape out the remaining cache. | |
2839 | */ | |
2840 | if (!scan && !mem_cgroup_online(memcg)) | |
9783aa99 | 2841 | scan = min(lruvec_size, SWAP_CLUSTER_MAX); |
6b4f7799 | 2842 | |
688035f7 JW |
2843 | switch (scan_balance) { |
2844 | case SCAN_EQUAL: | |
2845 | /* Scan lists relative to size */ | |
2846 | break; | |
2847 | case SCAN_FRACT: | |
9a265114 | 2848 | /* |
688035f7 JW |
2849 | * Scan types proportional to swappiness and |
2850 | * their relative recent reclaim efficiency. | |
76073c64 GS |
2851 | * Make sure we don't miss the last page on |
2852 | * the offlined memory cgroups because of a | |
2853 | * round-off error. | |
9a265114 | 2854 | */ |
76073c64 GS |
2855 | scan = mem_cgroup_online(memcg) ? |
2856 | div64_u64(scan * fraction[file], denominator) : | |
2857 | DIV64_U64_ROUND_UP(scan * fraction[file], | |
68600f62 | 2858 | denominator); |
688035f7 JW |
2859 | break; |
2860 | case SCAN_FILE: | |
2861 | case SCAN_ANON: | |
2862 | /* Scan one type exclusively */ | |
e072bff6 | 2863 | if ((scan_balance == SCAN_FILE) != file) |
688035f7 | 2864 | scan = 0; |
688035f7 JW |
2865 | break; |
2866 | default: | |
2867 | /* Look ma, no brain */ | |
2868 | BUG(); | |
9a265114 | 2869 | } |
688035f7 | 2870 | |
688035f7 | 2871 | nr[lru] = scan; |
76a33fc3 | 2872 | } |
6e08a369 | 2873 | } |
4f98a2fe | 2874 | |
2f368a9f DH |
2875 | /* |
2876 | * Anonymous LRU management is a waste if there is | |
2877 | * ultimately no way to reclaim the memory. | |
2878 | */ | |
2879 | static bool can_age_anon_pages(struct pglist_data *pgdat, | |
2880 | struct scan_control *sc) | |
2881 | { | |
2882 | /* Aging the anon LRU is valuable if swap is present: */ | |
2883 | if (total_swap_pages > 0) | |
2884 | return true; | |
2885 | ||
2886 | /* Also valuable if anon pages can be demoted: */ | |
2887 | return can_demote(pgdat->node_id, sc); | |
2888 | } | |
2889 | ||
afaf07a6 | 2890 | static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) |
9b4f98cd JW |
2891 | { |
2892 | unsigned long nr[NR_LRU_LISTS]; | |
e82e0561 | 2893 | unsigned long targets[NR_LRU_LISTS]; |
9b4f98cd JW |
2894 | unsigned long nr_to_scan; |
2895 | enum lru_list lru; | |
2896 | unsigned long nr_reclaimed = 0; | |
2897 | unsigned long nr_to_reclaim = sc->nr_to_reclaim; | |
2898 | struct blk_plug plug; | |
1a501907 | 2899 | bool scan_adjusted; |
9b4f98cd | 2900 | |
afaf07a6 | 2901 | get_scan_count(lruvec, sc, nr); |
9b4f98cd | 2902 | |
e82e0561 MG |
2903 | /* Record the original scan target for proportional adjustments later */ |
2904 | memcpy(targets, nr, sizeof(nr)); | |
2905 | ||
1a501907 MG |
2906 | /* |
2907 | * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal | |
2908 | * event that can occur when there is little memory pressure e.g. | |
2909 | * multiple streaming readers/writers. Hence, we do not abort scanning | |
2910 | * when the requested number of pages are reclaimed when scanning at | |
2911 | * DEF_PRIORITY on the assumption that the fact we are direct | |
2912 | * reclaiming implies that kswapd is not keeping up and it is best to | |
2913 | * do a batch of work at once. For memcg reclaim one check is made to | |
2914 | * abort proportional reclaim if either the file or anon lru has already | |
2915 | * dropped to zero at the first pass. | |
2916 | */ | |
b5ead35e | 2917 | scan_adjusted = (!cgroup_reclaim(sc) && !current_is_kswapd() && |
1a501907 MG |
2918 | sc->priority == DEF_PRIORITY); |
2919 | ||
9b4f98cd JW |
2920 | blk_start_plug(&plug); |
2921 | while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || | |
2922 | nr[LRU_INACTIVE_FILE]) { | |
e82e0561 MG |
2923 | unsigned long nr_anon, nr_file, percentage; |
2924 | unsigned long nr_scanned; | |
2925 | ||
9b4f98cd JW |
2926 | for_each_evictable_lru(lru) { |
2927 | if (nr[lru]) { | |
2928 | nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); | |
2929 | nr[lru] -= nr_to_scan; | |
2930 | ||
2931 | nr_reclaimed += shrink_list(lru, nr_to_scan, | |
3b991208 | 2932 | lruvec, sc); |
9b4f98cd JW |
2933 | } |
2934 | } | |
e82e0561 | 2935 | |
bd041733 MH |
2936 | cond_resched(); |
2937 | ||
e82e0561 MG |
2938 | if (nr_reclaimed < nr_to_reclaim || scan_adjusted) |
2939 | continue; | |
2940 | ||
e82e0561 MG |
2941 | /* |
2942 | * For kswapd and memcg, reclaim at least the number of pages | |
1a501907 | 2943 | * requested. Ensure that the anon and file LRUs are scanned |
e82e0561 MG |
2944 | * proportionally what was requested by get_scan_count(). We |
2945 | * stop reclaiming one LRU and reduce the amount scanning | |
2946 | * proportional to the original scan target. | |
2947 | */ | |
2948 | nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; | |
2949 | nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; | |
2950 | ||
1a501907 MG |
2951 | /* |
2952 | * It's just vindictive to attack the larger once the smaller | |
2953 | * has gone to zero. And given the way we stop scanning the | |
2954 | * smaller below, this makes sure that we only make one nudge | |
2955 | * towards proportionality once we've got nr_to_reclaim. | |
2956 | */ | |
2957 | if (!nr_file || !nr_anon) | |
2958 | break; | |
2959 | ||
e82e0561 MG |
2960 | if (nr_file > nr_anon) { |
2961 | unsigned long scan_target = targets[LRU_INACTIVE_ANON] + | |
2962 | targets[LRU_ACTIVE_ANON] + 1; | |
2963 | lru = LRU_BASE; | |
2964 | percentage = nr_anon * 100 / scan_target; | |
2965 | } else { | |
2966 | unsigned long scan_target = targets[LRU_INACTIVE_FILE] + | |
2967 | targets[LRU_ACTIVE_FILE] + 1; | |
2968 | lru = LRU_FILE; | |
2969 | percentage = nr_file * 100 / scan_target; | |
2970 | } | |
2971 | ||
2972 | /* Stop scanning the smaller of the LRU */ | |
2973 | nr[lru] = 0; | |
2974 | nr[lru + LRU_ACTIVE] = 0; | |
2975 | ||
2976 | /* | |
2977 | * Recalculate the other LRU scan count based on its original | |
2978 | * scan target and the percentage scanning already complete | |
2979 | */ | |
2980 | lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; | |
2981 | nr_scanned = targets[lru] - nr[lru]; | |
2982 | nr[lru] = targets[lru] * (100 - percentage) / 100; | |
2983 | nr[lru] -= min(nr[lru], nr_scanned); | |
2984 | ||
2985 | lru += LRU_ACTIVE; | |
2986 | nr_scanned = targets[lru] - nr[lru]; | |
2987 | nr[lru] = targets[lru] * (100 - percentage) / 100; | |
2988 | nr[lru] -= min(nr[lru], nr_scanned); | |
2989 | ||
2990 | scan_adjusted = true; | |
9b4f98cd JW |
2991 | } |
2992 | blk_finish_plug(&plug); | |
2993 | sc->nr_reclaimed += nr_reclaimed; | |
2994 | ||
2995 | /* | |
2996 | * Even if we did not try to evict anon pages at all, we want to | |
2997 | * rebalance the anon lru active/inactive ratio. | |
2998 | */ | |
2f368a9f DH |
2999 | if (can_age_anon_pages(lruvec_pgdat(lruvec), sc) && |
3000 | inactive_is_low(lruvec, LRU_INACTIVE_ANON)) | |
9b4f98cd JW |
3001 | shrink_active_list(SWAP_CLUSTER_MAX, lruvec, |
3002 | sc, LRU_ACTIVE_ANON); | |
9b4f98cd JW |
3003 | } |
3004 | ||
23b9da55 | 3005 | /* Use reclaim/compaction for costly allocs or under memory pressure */ |
9e3b2f8c | 3006 | static bool in_reclaim_compaction(struct scan_control *sc) |
23b9da55 | 3007 | { |
d84da3f9 | 3008 | if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && |
23b9da55 | 3009 | (sc->order > PAGE_ALLOC_COSTLY_ORDER || |
9e3b2f8c | 3010 | sc->priority < DEF_PRIORITY - 2)) |
23b9da55 MG |
3011 | return true; |
3012 | ||
3013 | return false; | |
3014 | } | |
3015 | ||
3e7d3449 | 3016 | /* |
23b9da55 MG |
3017 | * Reclaim/compaction is used for high-order allocation requests. It reclaims |
3018 | * order-0 pages before compacting the zone. should_continue_reclaim() returns | |
3019 | * true if more pages should be reclaimed such that when the page allocator | |
df3a45f9 | 3020 | * calls try_to_compact_pages() that it will have enough free pages to succeed. |
23b9da55 | 3021 | * It will give up earlier than that if there is difficulty reclaiming pages. |
3e7d3449 | 3022 | */ |
a9dd0a83 | 3023 | static inline bool should_continue_reclaim(struct pglist_data *pgdat, |
3e7d3449 | 3024 | unsigned long nr_reclaimed, |
3e7d3449 MG |
3025 | struct scan_control *sc) |
3026 | { | |
3027 | unsigned long pages_for_compaction; | |
3028 | unsigned long inactive_lru_pages; | |
a9dd0a83 | 3029 | int z; |
3e7d3449 MG |
3030 | |
3031 | /* If not in reclaim/compaction mode, stop */ | |
9e3b2f8c | 3032 | if (!in_reclaim_compaction(sc)) |
3e7d3449 MG |
3033 | return false; |
3034 | ||
5ee04716 VB |
3035 | /* |
3036 | * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX | |
3037 | * number of pages that were scanned. This will return to the caller | |
3038 | * with the risk reclaim/compaction and the resulting allocation attempt | |
3039 | * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL | |
3040 | * allocations through requiring that the full LRU list has been scanned | |
3041 | * first, by assuming that zero delta of sc->nr_scanned means full LRU | |
3042 | * scan, but that approximation was wrong, and there were corner cases | |
3043 | * where always a non-zero amount of pages were scanned. | |
3044 | */ | |
3045 | if (!nr_reclaimed) | |
3046 | return false; | |
3e7d3449 | 3047 | |
3e7d3449 | 3048 | /* If compaction would go ahead or the allocation would succeed, stop */ |
a9dd0a83 MG |
3049 | for (z = 0; z <= sc->reclaim_idx; z++) { |
3050 | struct zone *zone = &pgdat->node_zones[z]; | |
6aa303de | 3051 | if (!managed_zone(zone)) |
a9dd0a83 MG |
3052 | continue; |
3053 | ||
3054 | switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { | |
cf378319 | 3055 | case COMPACT_SUCCESS: |
a9dd0a83 MG |
3056 | case COMPACT_CONTINUE: |
3057 | return false; | |
3058 | default: | |
3059 | /* check next zone */ | |
3060 | ; | |
3061 | } | |
3e7d3449 | 3062 | } |
1c6c1597 HD |
3063 | |
3064 | /* | |
3065 | * If we have not reclaimed enough pages for compaction and the | |
3066 | * inactive lists are large enough, continue reclaiming | |
3067 | */ | |
3068 | pages_for_compaction = compact_gap(sc->order); | |
3069 | inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); | |
a2a36488 | 3070 | if (can_reclaim_anon_pages(NULL, pgdat->node_id, sc)) |
1c6c1597 HD |
3071 | inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); |
3072 | ||
5ee04716 | 3073 | return inactive_lru_pages > pages_for_compaction; |
3e7d3449 MG |
3074 | } |
3075 | ||
0f6a5cff | 3076 | static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) |
1da177e4 | 3077 | { |
0f6a5cff | 3078 | struct mem_cgroup *target_memcg = sc->target_mem_cgroup; |
d2af3397 | 3079 | struct mem_cgroup *memcg; |
1da177e4 | 3080 | |
0f6a5cff | 3081 | memcg = mem_cgroup_iter(target_memcg, NULL, NULL); |
d2af3397 | 3082 | do { |
afaf07a6 | 3083 | struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); |
d2af3397 JW |
3084 | unsigned long reclaimed; |
3085 | unsigned long scanned; | |
5660048c | 3086 | |
e3336cab XP |
3087 | /* |
3088 | * This loop can become CPU-bound when target memcgs | |
3089 | * aren't eligible for reclaim - either because they | |
3090 | * don't have any reclaimable pages, or because their | |
3091 | * memory is explicitly protected. Avoid soft lockups. | |
3092 | */ | |
3093 | cond_resched(); | |
3094 | ||
45c7f7e1 CD |
3095 | mem_cgroup_calculate_protection(target_memcg, memcg); |
3096 | ||
3097 | if (mem_cgroup_below_min(memcg)) { | |
d2af3397 JW |
3098 | /* |
3099 | * Hard protection. | |
3100 | * If there is no reclaimable memory, OOM. | |
3101 | */ | |
3102 | continue; | |
45c7f7e1 | 3103 | } else if (mem_cgroup_below_low(memcg)) { |
d2af3397 JW |
3104 | /* |
3105 | * Soft protection. | |
3106 | * Respect the protection only as long as | |
3107 | * there is an unprotected supply | |
3108 | * of reclaimable memory from other cgroups. | |
3109 | */ | |
3110 | if (!sc->memcg_low_reclaim) { | |
3111 | sc->memcg_low_skipped = 1; | |
bf8d5d52 | 3112 | continue; |
241994ed | 3113 | } |
d2af3397 | 3114 | memcg_memory_event(memcg, MEMCG_LOW); |
d2af3397 | 3115 | } |
241994ed | 3116 | |
d2af3397 JW |
3117 | reclaimed = sc->nr_reclaimed; |
3118 | scanned = sc->nr_scanned; | |
afaf07a6 JW |
3119 | |
3120 | shrink_lruvec(lruvec, sc); | |
70ddf637 | 3121 | |
d2af3397 JW |
3122 | shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, |
3123 | sc->priority); | |
6b4f7799 | 3124 | |
d2af3397 JW |
3125 | /* Record the group's reclaim efficiency */ |
3126 | vmpressure(sc->gfp_mask, memcg, false, | |
3127 | sc->nr_scanned - scanned, | |
3128 | sc->nr_reclaimed - reclaimed); | |
70ddf637 | 3129 | |
0f6a5cff JW |
3130 | } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL))); |
3131 | } | |
3132 | ||
6c9e0907 | 3133 | static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) |
0f6a5cff JW |
3134 | { |
3135 | struct reclaim_state *reclaim_state = current->reclaim_state; | |
0f6a5cff | 3136 | unsigned long nr_reclaimed, nr_scanned; |
1b05117d | 3137 | struct lruvec *target_lruvec; |
0f6a5cff | 3138 | bool reclaimable = false; |
b91ac374 | 3139 | unsigned long file; |
0f6a5cff | 3140 | |
1b05117d JW |
3141 | target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); |
3142 | ||
0f6a5cff | 3143 | again: |
aa48e47e SB |
3144 | /* |
3145 | * Flush the memory cgroup stats, so that we read accurate per-memcg | |
3146 | * lruvec stats for heuristics. | |
3147 | */ | |
3148 | mem_cgroup_flush_stats(); | |
3149 | ||
0f6a5cff JW |
3150 | memset(&sc->nr, 0, sizeof(sc->nr)); |
3151 | ||
3152 | nr_reclaimed = sc->nr_reclaimed; | |
3153 | nr_scanned = sc->nr_scanned; | |
3154 | ||
7cf111bc JW |
3155 | /* |
3156 | * Determine the scan balance between anon and file LRUs. | |
3157 | */ | |
6168d0da | 3158 | spin_lock_irq(&target_lruvec->lru_lock); |
7cf111bc JW |
3159 | sc->anon_cost = target_lruvec->anon_cost; |
3160 | sc->file_cost = target_lruvec->file_cost; | |
6168d0da | 3161 | spin_unlock_irq(&target_lruvec->lru_lock); |
7cf111bc | 3162 | |
b91ac374 JW |
3163 | /* |
3164 | * Target desirable inactive:active list ratios for the anon | |
3165 | * and file LRU lists. | |
3166 | */ | |
3167 | if (!sc->force_deactivate) { | |
3168 | unsigned long refaults; | |
3169 | ||
170b04b7 JK |
3170 | refaults = lruvec_page_state(target_lruvec, |
3171 | WORKINGSET_ACTIVATE_ANON); | |
3172 | if (refaults != target_lruvec->refaults[0] || | |
3173 | inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)) | |
b91ac374 JW |
3174 | sc->may_deactivate |= DEACTIVATE_ANON; |
3175 | else | |
3176 | sc->may_deactivate &= ~DEACTIVATE_ANON; | |
3177 | ||
3178 | /* | |
3179 | * When refaults are being observed, it means a new | |
3180 | * workingset is being established. Deactivate to get | |
3181 | * rid of any stale active pages quickly. | |
3182 | */ | |
3183 | refaults = lruvec_page_state(target_lruvec, | |
170b04b7 JK |
3184 | WORKINGSET_ACTIVATE_FILE); |
3185 | if (refaults != target_lruvec->refaults[1] || | |
b91ac374 JW |
3186 | inactive_is_low(target_lruvec, LRU_INACTIVE_FILE)) |
3187 | sc->may_deactivate |= DEACTIVATE_FILE; | |
3188 | else | |
3189 | sc->may_deactivate &= ~DEACTIVATE_FILE; | |
3190 | } else | |
3191 | sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE; | |
3192 | ||
3193 | /* | |
3194 | * If we have plenty of inactive file pages that aren't | |
3195 | * thrashing, try to reclaim those first before touching | |
3196 | * anonymous pages. | |
3197 | */ | |
3198 | file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE); | |
3199 | if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE)) | |
3200 | sc->cache_trim_mode = 1; | |
3201 | else | |
3202 | sc->cache_trim_mode = 0; | |
3203 | ||
53138cea JW |
3204 | /* |
3205 | * Prevent the reclaimer from falling into the cache trap: as | |
3206 | * cache pages start out inactive, every cache fault will tip | |
3207 | * the scan balance towards the file LRU. And as the file LRU | |
3208 | * shrinks, so does the window for rotation from references. | |
3209 | * This means we have a runaway feedback loop where a tiny | |
3210 | * thrashing file LRU becomes infinitely more attractive than | |
3211 | * anon pages. Try to detect this based on file LRU size. | |
3212 | */ | |
3213 | if (!cgroup_reclaim(sc)) { | |
53138cea | 3214 | unsigned long total_high_wmark = 0; |
b91ac374 JW |
3215 | unsigned long free, anon; |
3216 | int z; | |
53138cea JW |
3217 | |
3218 | free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); | |
3219 | file = node_page_state(pgdat, NR_ACTIVE_FILE) + | |
3220 | node_page_state(pgdat, NR_INACTIVE_FILE); | |
3221 | ||
3222 | for (z = 0; z < MAX_NR_ZONES; z++) { | |
3223 | struct zone *zone = &pgdat->node_zones[z]; | |
3224 | if (!managed_zone(zone)) | |
3225 | continue; | |
3226 | ||
3227 | total_high_wmark += high_wmark_pages(zone); | |
3228 | } | |
3229 | ||
b91ac374 JW |
3230 | /* |
3231 | * Consider anon: if that's low too, this isn't a | |
3232 | * runaway file reclaim problem, but rather just | |
3233 | * extreme pressure. Reclaim as per usual then. | |
3234 | */ | |
3235 | anon = node_page_state(pgdat, NR_INACTIVE_ANON); | |
3236 | ||
3237 | sc->file_is_tiny = | |
3238 | file + free <= total_high_wmark && | |
3239 | !(sc->may_deactivate & DEACTIVATE_ANON) && | |
3240 | anon >> sc->priority; | |
53138cea JW |
3241 | } |
3242 | ||
0f6a5cff | 3243 | shrink_node_memcgs(pgdat, sc); |
2344d7e4 | 3244 | |
d2af3397 JW |
3245 | if (reclaim_state) { |
3246 | sc->nr_reclaimed += reclaim_state->reclaimed_slab; | |
3247 | reclaim_state->reclaimed_slab = 0; | |
3248 | } | |
d108c772 | 3249 | |
d2af3397 | 3250 | /* Record the subtree's reclaim efficiency */ |
1b05117d | 3251 | vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, |
d2af3397 JW |
3252 | sc->nr_scanned - nr_scanned, |
3253 | sc->nr_reclaimed - nr_reclaimed); | |
d108c772 | 3254 | |
d2af3397 JW |
3255 | if (sc->nr_reclaimed - nr_reclaimed) |
3256 | reclaimable = true; | |
d108c772 | 3257 | |
d2af3397 JW |
3258 | if (current_is_kswapd()) { |
3259 | /* | |
3260 | * If reclaim is isolating dirty pages under writeback, | |
3261 | * it implies that the long-lived page allocation rate | |
3262 | * is exceeding the page laundering rate. Either the | |
3263 | * global limits are not being effective at throttling | |
3264 | * processes due to the page distribution throughout | |
3265 | * zones or there is heavy usage of a slow backing | |
3266 | * device. The only option is to throttle from reclaim | |
3267 | * context which is not ideal as there is no guarantee | |
3268 | * the dirtying process is throttled in the same way | |
3269 | * balance_dirty_pages() manages. | |
3270 | * | |
3271 | * Once a node is flagged PGDAT_WRITEBACK, kswapd will | |
3272 | * count the number of pages under pages flagged for | |
3273 | * immediate reclaim and stall if any are encountered | |
3274 | * in the nr_immediate check below. | |
3275 | */ | |
3276 | if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) | |
3277 | set_bit(PGDAT_WRITEBACK, &pgdat->flags); | |
d108c772 | 3278 | |
d2af3397 JW |
3279 | /* Allow kswapd to start writing pages during reclaim.*/ |
3280 | if (sc->nr.unqueued_dirty == sc->nr.file_taken) | |
3281 | set_bit(PGDAT_DIRTY, &pgdat->flags); | |
e3c1ac58 | 3282 | |
d108c772 | 3283 | /* |
1eba09c1 | 3284 | * If kswapd scans pages marked for immediate |
d2af3397 JW |
3285 | * reclaim and under writeback (nr_immediate), it |
3286 | * implies that pages are cycling through the LRU | |
8cd7c588 MG |
3287 | * faster than they are written so forcibly stall |
3288 | * until some pages complete writeback. | |
d108c772 | 3289 | */ |
d2af3397 | 3290 | if (sc->nr.immediate) |
c3f4a9a2 | 3291 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); |
d2af3397 JW |
3292 | } |
3293 | ||
3294 | /* | |
8cd7c588 MG |
3295 | * Tag a node/memcg as congested if all the dirty pages were marked |
3296 | * for writeback and immediate reclaim (counted in nr.congested). | |
1b05117d | 3297 | * |
d2af3397 | 3298 | * Legacy memcg will stall in page writeback so avoid forcibly |
8cd7c588 | 3299 | * stalling in reclaim_throttle(). |
d2af3397 | 3300 | */ |
1b05117d JW |
3301 | if ((current_is_kswapd() || |
3302 | (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) && | |
d2af3397 | 3303 | sc->nr.dirty && sc->nr.dirty == sc->nr.congested) |
1b05117d | 3304 | set_bit(LRUVEC_CONGESTED, &target_lruvec->flags); |
d2af3397 JW |
3305 | |
3306 | /* | |
8cd7c588 MG |
3307 | * Stall direct reclaim for IO completions if the lruvec is |
3308 | * node is congested. Allow kswapd to continue until it | |
d2af3397 JW |
3309 | * starts encountering unqueued dirty pages or cycling through |
3310 | * the LRU too quickly. | |
3311 | */ | |
1b05117d JW |
3312 | if (!current_is_kswapd() && current_may_throttle() && |
3313 | !sc->hibernation_mode && | |
3314 | test_bit(LRUVEC_CONGESTED, &target_lruvec->flags)) | |
1b4e3f26 | 3315 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_CONGESTED); |
d108c772 | 3316 | |
d2af3397 JW |
3317 | if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, |
3318 | sc)) | |
3319 | goto again; | |
2344d7e4 | 3320 | |
c73322d0 JW |
3321 | /* |
3322 | * Kswapd gives up on balancing particular nodes after too | |
3323 | * many failures to reclaim anything from them and goes to | |
3324 | * sleep. On reclaim progress, reset the failure counter. A | |
3325 | * successful direct reclaim run will revive a dormant kswapd. | |
3326 | */ | |
3327 | if (reclaimable) | |
3328 | pgdat->kswapd_failures = 0; | |
f16015fb JW |
3329 | } |
3330 | ||
53853e2d | 3331 | /* |
fdd4c614 VB |
3332 | * Returns true if compaction should go ahead for a costly-order request, or |
3333 | * the allocation would already succeed without compaction. Return false if we | |
3334 | * should reclaim first. | |
53853e2d | 3335 | */ |
4f588331 | 3336 | static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) |
fe4b1b24 | 3337 | { |
31483b6a | 3338 | unsigned long watermark; |
fdd4c614 | 3339 | enum compact_result suitable; |
fe4b1b24 | 3340 | |
fdd4c614 VB |
3341 | suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); |
3342 | if (suitable == COMPACT_SUCCESS) | |
3343 | /* Allocation should succeed already. Don't reclaim. */ | |
3344 | return true; | |
3345 | if (suitable == COMPACT_SKIPPED) | |
3346 | /* Compaction cannot yet proceed. Do reclaim. */ | |
3347 | return false; | |
fe4b1b24 | 3348 | |
53853e2d | 3349 | /* |
fdd4c614 VB |
3350 | * Compaction is already possible, but it takes time to run and there |
3351 | * are potentially other callers using the pages just freed. So proceed | |
3352 | * with reclaim to make a buffer of free pages available to give | |
3353 | * compaction a reasonable chance of completing and allocating the page. | |
3354 | * Note that we won't actually reclaim the whole buffer in one attempt | |
3355 | * as the target watermark in should_continue_reclaim() is lower. But if | |
3356 | * we are already above the high+gap watermark, don't reclaim at all. | |
53853e2d | 3357 | */ |
fdd4c614 | 3358 | watermark = high_wmark_pages(zone) + compact_gap(sc->order); |
fe4b1b24 | 3359 | |
fdd4c614 | 3360 | return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); |
fe4b1b24 MG |
3361 | } |
3362 | ||
69392a40 MG |
3363 | static void consider_reclaim_throttle(pg_data_t *pgdat, struct scan_control *sc) |
3364 | { | |
66ce520b MG |
3365 | /* |
3366 | * If reclaim is making progress greater than 12% efficiency then | |
3367 | * wake all the NOPROGRESS throttled tasks. | |
3368 | */ | |
3369 | if (sc->nr_reclaimed > (sc->nr_scanned >> 3)) { | |
69392a40 MG |
3370 | wait_queue_head_t *wqh; |
3371 | ||
3372 | wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_NOPROGRESS]; | |
3373 | if (waitqueue_active(wqh)) | |
3374 | wake_up(wqh); | |
3375 | ||
3376 | return; | |
3377 | } | |
3378 | ||
3379 | /* | |
1b4e3f26 MG |
3380 | * Do not throttle kswapd or cgroup reclaim on NOPROGRESS as it will |
3381 | * throttle on VMSCAN_THROTTLE_WRITEBACK if there are too many pages | |
3382 | * under writeback and marked for immediate reclaim at the tail of the | |
3383 | * LRU. | |
69392a40 | 3384 | */ |
1b4e3f26 | 3385 | if (current_is_kswapd() || cgroup_reclaim(sc)) |
69392a40 MG |
3386 | return; |
3387 | ||
3388 | /* Throttle if making no progress at high prioities. */ | |
1b4e3f26 | 3389 | if (sc->priority == 1 && !sc->nr_reclaimed) |
c3f4a9a2 | 3390 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_NOPROGRESS); |
69392a40 MG |
3391 | } |
3392 | ||
1da177e4 LT |
3393 | /* |
3394 | * This is the direct reclaim path, for page-allocating processes. We only | |
3395 | * try to reclaim pages from zones which will satisfy the caller's allocation | |
3396 | * request. | |
3397 | * | |
1da177e4 LT |
3398 | * If a zone is deemed to be full of pinned pages then just give it a light |
3399 | * scan then give up on it. | |
3400 | */ | |
0a0337e0 | 3401 | static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) |
1da177e4 | 3402 | { |
dd1a239f | 3403 | struct zoneref *z; |
54a6eb5c | 3404 | struct zone *zone; |
0608f43d AM |
3405 | unsigned long nr_soft_reclaimed; |
3406 | unsigned long nr_soft_scanned; | |
619d0d76 | 3407 | gfp_t orig_mask; |
79dafcdc | 3408 | pg_data_t *last_pgdat = NULL; |
1b4e3f26 | 3409 | pg_data_t *first_pgdat = NULL; |
1cfb419b | 3410 | |
cc715d99 MG |
3411 | /* |
3412 | * If the number of buffer_heads in the machine exceeds the maximum | |
3413 | * allowed level, force direct reclaim to scan the highmem zone as | |
3414 | * highmem pages could be pinning lowmem pages storing buffer_heads | |
3415 | */ | |
619d0d76 | 3416 | orig_mask = sc->gfp_mask; |
b2e18757 | 3417 | if (buffer_heads_over_limit) { |
cc715d99 | 3418 | sc->gfp_mask |= __GFP_HIGHMEM; |
4f588331 | 3419 | sc->reclaim_idx = gfp_zone(sc->gfp_mask); |
b2e18757 | 3420 | } |
cc715d99 | 3421 | |
d4debc66 | 3422 | for_each_zone_zonelist_nodemask(zone, z, zonelist, |
b2e18757 | 3423 | sc->reclaim_idx, sc->nodemask) { |
1cfb419b KH |
3424 | /* |
3425 | * Take care memory controller reclaiming has small influence | |
3426 | * to global LRU. | |
3427 | */ | |
b5ead35e | 3428 | if (!cgroup_reclaim(sc)) { |
344736f2 VD |
3429 | if (!cpuset_zone_allowed(zone, |
3430 | GFP_KERNEL | __GFP_HARDWALL)) | |
1cfb419b | 3431 | continue; |
65ec02cb | 3432 | |
0b06496a JW |
3433 | /* |
3434 | * If we already have plenty of memory free for | |
3435 | * compaction in this zone, don't free any more. | |
3436 | * Even though compaction is invoked for any | |
3437 | * non-zero order, only frequent costly order | |
3438 | * reclamation is disruptive enough to become a | |
3439 | * noticeable problem, like transparent huge | |
3440 | * page allocations. | |
3441 | */ | |
3442 | if (IS_ENABLED(CONFIG_COMPACTION) && | |
3443 | sc->order > PAGE_ALLOC_COSTLY_ORDER && | |
4f588331 | 3444 | compaction_ready(zone, sc)) { |
0b06496a JW |
3445 | sc->compaction_ready = true; |
3446 | continue; | |
e0887c19 | 3447 | } |
0b06496a | 3448 | |
79dafcdc MG |
3449 | /* |
3450 | * Shrink each node in the zonelist once. If the | |
3451 | * zonelist is ordered by zone (not the default) then a | |
3452 | * node may be shrunk multiple times but in that case | |
3453 | * the user prefers lower zones being preserved. | |
3454 | */ | |
3455 | if (zone->zone_pgdat == last_pgdat) | |
3456 | continue; | |
3457 | ||
0608f43d AM |
3458 | /* |
3459 | * This steals pages from memory cgroups over softlimit | |
3460 | * and returns the number of reclaimed pages and | |
3461 | * scanned pages. This works for global memory pressure | |
3462 | * and balancing, not for a memcg's limit. | |
3463 | */ | |
3464 | nr_soft_scanned = 0; | |
ef8f2327 | 3465 | nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, |
0608f43d AM |
3466 | sc->order, sc->gfp_mask, |
3467 | &nr_soft_scanned); | |
3468 | sc->nr_reclaimed += nr_soft_reclaimed; | |
3469 | sc->nr_scanned += nr_soft_scanned; | |
ac34a1a3 | 3470 | /* need some check for avoid more shrink_zone() */ |
1cfb419b | 3471 | } |
408d8544 | 3472 | |
1b4e3f26 MG |
3473 | if (!first_pgdat) |
3474 | first_pgdat = zone->zone_pgdat; | |
3475 | ||
79dafcdc MG |
3476 | /* See comment about same check for global reclaim above */ |
3477 | if (zone->zone_pgdat == last_pgdat) | |
3478 | continue; | |
3479 | last_pgdat = zone->zone_pgdat; | |
970a39a3 | 3480 | shrink_node(zone->zone_pgdat, sc); |
1da177e4 | 3481 | } |
e0c23279 | 3482 | |
80082938 MG |
3483 | if (first_pgdat) |
3484 | consider_reclaim_throttle(first_pgdat, sc); | |
1b4e3f26 | 3485 | |
619d0d76 WY |
3486 | /* |
3487 | * Restore to original mask to avoid the impact on the caller if we | |
3488 | * promoted it to __GFP_HIGHMEM. | |
3489 | */ | |
3490 | sc->gfp_mask = orig_mask; | |
1da177e4 | 3491 | } |
4f98a2fe | 3492 | |
b910718a | 3493 | static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat) |
2a2e4885 | 3494 | { |
b910718a JW |
3495 | struct lruvec *target_lruvec; |
3496 | unsigned long refaults; | |
2a2e4885 | 3497 | |
b910718a | 3498 | target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat); |
170b04b7 JK |
3499 | refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON); |
3500 | target_lruvec->refaults[0] = refaults; | |
3501 | refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE); | |
3502 | target_lruvec->refaults[1] = refaults; | |
2a2e4885 JW |
3503 | } |
3504 | ||
1da177e4 LT |
3505 | /* |
3506 | * This is the main entry point to direct page reclaim. | |
3507 | * | |
3508 | * If a full scan of the inactive list fails to free enough memory then we | |
3509 | * are "out of memory" and something needs to be killed. | |
3510 | * | |
3511 | * If the caller is !__GFP_FS then the probability of a failure is reasonably | |
3512 | * high - the zone may be full of dirty or under-writeback pages, which this | |
5b0830cb JA |
3513 | * caller can't do much about. We kick the writeback threads and take explicit |
3514 | * naps in the hope that some of these pages can be written. But if the | |
3515 | * allocating task holds filesystem locks which prevent writeout this might not | |
3516 | * work, and the allocation attempt will fail. | |
a41f24ea NA |
3517 | * |
3518 | * returns: 0, if no pages reclaimed | |
3519 | * else, the number of pages reclaimed | |
1da177e4 | 3520 | */ |
dac1d27b | 3521 | static unsigned long do_try_to_free_pages(struct zonelist *zonelist, |
3115cd91 | 3522 | struct scan_control *sc) |
1da177e4 | 3523 | { |
241994ed | 3524 | int initial_priority = sc->priority; |
2a2e4885 JW |
3525 | pg_data_t *last_pgdat; |
3526 | struct zoneref *z; | |
3527 | struct zone *zone; | |
241994ed | 3528 | retry: |
873b4771 KK |
3529 | delayacct_freepages_start(); |
3530 | ||
b5ead35e | 3531 | if (!cgroup_reclaim(sc)) |
7cc30fcf | 3532 | __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); |
1da177e4 | 3533 | |
9e3b2f8c | 3534 | do { |
70ddf637 AV |
3535 | vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, |
3536 | sc->priority); | |
66e1707b | 3537 | sc->nr_scanned = 0; |
0a0337e0 | 3538 | shrink_zones(zonelist, sc); |
c6a8a8c5 | 3539 | |
bb21c7ce | 3540 | if (sc->nr_reclaimed >= sc->nr_to_reclaim) |
0b06496a JW |
3541 | break; |
3542 | ||
3543 | if (sc->compaction_ready) | |
3544 | break; | |
1da177e4 | 3545 | |
0e50ce3b MK |
3546 | /* |
3547 | * If we're getting trouble reclaiming, start doing | |
3548 | * writepage even in laptop mode. | |
3549 | */ | |
3550 | if (sc->priority < DEF_PRIORITY - 2) | |
3551 | sc->may_writepage = 1; | |
0b06496a | 3552 | } while (--sc->priority >= 0); |
bb21c7ce | 3553 | |
2a2e4885 JW |
3554 | last_pgdat = NULL; |
3555 | for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, | |
3556 | sc->nodemask) { | |
3557 | if (zone->zone_pgdat == last_pgdat) | |
3558 | continue; | |
3559 | last_pgdat = zone->zone_pgdat; | |
1b05117d | 3560 | |
2a2e4885 | 3561 | snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); |
1b05117d JW |
3562 | |
3563 | if (cgroup_reclaim(sc)) { | |
3564 | struct lruvec *lruvec; | |
3565 | ||
3566 | lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, | |
3567 | zone->zone_pgdat); | |
3568 | clear_bit(LRUVEC_CONGESTED, &lruvec->flags); | |
3569 | } | |
2a2e4885 JW |
3570 | } |
3571 | ||
873b4771 KK |
3572 | delayacct_freepages_end(); |
3573 | ||
bb21c7ce KM |
3574 | if (sc->nr_reclaimed) |
3575 | return sc->nr_reclaimed; | |
3576 | ||
0cee34fd | 3577 | /* Aborted reclaim to try compaction? don't OOM, then */ |
0b06496a | 3578 | if (sc->compaction_ready) |
7335084d MG |
3579 | return 1; |
3580 | ||
b91ac374 JW |
3581 | /* |
3582 | * We make inactive:active ratio decisions based on the node's | |
3583 | * composition of memory, but a restrictive reclaim_idx or a | |
3584 | * memory.low cgroup setting can exempt large amounts of | |
3585 | * memory from reclaim. Neither of which are very common, so | |
3586 | * instead of doing costly eligibility calculations of the | |
3587 | * entire cgroup subtree up front, we assume the estimates are | |
3588 | * good, and retry with forcible deactivation if that fails. | |
3589 | */ | |
3590 | if (sc->skipped_deactivate) { | |
3591 | sc->priority = initial_priority; | |
3592 | sc->force_deactivate = 1; | |
3593 | sc->skipped_deactivate = 0; | |
3594 | goto retry; | |
3595 | } | |
3596 | ||
241994ed | 3597 | /* Untapped cgroup reserves? Don't OOM, retry. */ |
d6622f63 | 3598 | if (sc->memcg_low_skipped) { |
241994ed | 3599 | sc->priority = initial_priority; |
b91ac374 | 3600 | sc->force_deactivate = 0; |
d6622f63 YX |
3601 | sc->memcg_low_reclaim = 1; |
3602 | sc->memcg_low_skipped = 0; | |
241994ed JW |
3603 | goto retry; |
3604 | } | |
3605 | ||
bb21c7ce | 3606 | return 0; |
1da177e4 LT |
3607 | } |
3608 | ||
c73322d0 | 3609 | static bool allow_direct_reclaim(pg_data_t *pgdat) |
5515061d MG |
3610 | { |
3611 | struct zone *zone; | |
3612 | unsigned long pfmemalloc_reserve = 0; | |
3613 | unsigned long free_pages = 0; | |
3614 | int i; | |
3615 | bool wmark_ok; | |
3616 | ||
c73322d0 JW |
3617 | if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) |
3618 | return true; | |
3619 | ||
5515061d MG |
3620 | for (i = 0; i <= ZONE_NORMAL; i++) { |
3621 | zone = &pgdat->node_zones[i]; | |
d450abd8 JW |
3622 | if (!managed_zone(zone)) |
3623 | continue; | |
3624 | ||
3625 | if (!zone_reclaimable_pages(zone)) | |
675becce MG |
3626 | continue; |
3627 | ||
5515061d MG |
3628 | pfmemalloc_reserve += min_wmark_pages(zone); |
3629 | free_pages += zone_page_state(zone, NR_FREE_PAGES); | |
3630 | } | |
3631 | ||
675becce MG |
3632 | /* If there are no reserves (unexpected config) then do not throttle */ |
3633 | if (!pfmemalloc_reserve) | |
3634 | return true; | |
3635 | ||
5515061d MG |
3636 | wmark_ok = free_pages > pfmemalloc_reserve / 2; |
3637 | ||
3638 | /* kswapd must be awake if processes are being throttled */ | |
3639 | if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { | |
97a225e6 JK |
3640 | if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL) |
3641 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL); | |
5644e1fb | 3642 | |
5515061d MG |
3643 | wake_up_interruptible(&pgdat->kswapd_wait); |
3644 | } | |
3645 | ||
3646 | return wmark_ok; | |
3647 | } | |
3648 | ||
3649 | /* | |
3650 | * Throttle direct reclaimers if backing storage is backed by the network | |
3651 | * and the PFMEMALLOC reserve for the preferred node is getting dangerously | |
3652 | * depleted. kswapd will continue to make progress and wake the processes | |
50694c28 MG |
3653 | * when the low watermark is reached. |
3654 | * | |
3655 | * Returns true if a fatal signal was delivered during throttling. If this | |
3656 | * happens, the page allocator should not consider triggering the OOM killer. | |
5515061d | 3657 | */ |
50694c28 | 3658 | static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, |
5515061d MG |
3659 | nodemask_t *nodemask) |
3660 | { | |
675becce | 3661 | struct zoneref *z; |
5515061d | 3662 | struct zone *zone; |
675becce | 3663 | pg_data_t *pgdat = NULL; |
5515061d MG |
3664 | |
3665 | /* | |
3666 | * Kernel threads should not be throttled as they may be indirectly | |
3667 | * responsible for cleaning pages necessary for reclaim to make forward | |
3668 | * progress. kjournald for example may enter direct reclaim while | |
3669 | * committing a transaction where throttling it could forcing other | |
3670 | * processes to block on log_wait_commit(). | |
3671 | */ | |
3672 | if (current->flags & PF_KTHREAD) | |
50694c28 MG |
3673 | goto out; |
3674 | ||
3675 | /* | |
3676 | * If a fatal signal is pending, this process should not throttle. | |
3677 | * It should return quickly so it can exit and free its memory | |
3678 | */ | |
3679 | if (fatal_signal_pending(current)) | |
3680 | goto out; | |
5515061d | 3681 | |
675becce MG |
3682 | /* |
3683 | * Check if the pfmemalloc reserves are ok by finding the first node | |
3684 | * with a usable ZONE_NORMAL or lower zone. The expectation is that | |
3685 | * GFP_KERNEL will be required for allocating network buffers when | |
3686 | * swapping over the network so ZONE_HIGHMEM is unusable. | |
3687 | * | |
3688 | * Throttling is based on the first usable node and throttled processes | |
3689 | * wait on a queue until kswapd makes progress and wakes them. There | |
3690 | * is an affinity then between processes waking up and where reclaim | |
3691 | * progress has been made assuming the process wakes on the same node. | |
3692 | * More importantly, processes running on remote nodes will not compete | |
3693 | * for remote pfmemalloc reserves and processes on different nodes | |
3694 | * should make reasonable progress. | |
3695 | */ | |
3696 | for_each_zone_zonelist_nodemask(zone, z, zonelist, | |
17636faa | 3697 | gfp_zone(gfp_mask), nodemask) { |
675becce MG |
3698 | if (zone_idx(zone) > ZONE_NORMAL) |
3699 | continue; | |
3700 | ||
3701 | /* Throttle based on the first usable node */ | |
3702 | pgdat = zone->zone_pgdat; | |
c73322d0 | 3703 | if (allow_direct_reclaim(pgdat)) |
675becce MG |
3704 | goto out; |
3705 | break; | |
3706 | } | |
3707 | ||
3708 | /* If no zone was usable by the allocation flags then do not throttle */ | |
3709 | if (!pgdat) | |
50694c28 | 3710 | goto out; |
5515061d | 3711 | |
68243e76 MG |
3712 | /* Account for the throttling */ |
3713 | count_vm_event(PGSCAN_DIRECT_THROTTLE); | |
3714 | ||
5515061d MG |
3715 | /* |
3716 | * If the caller cannot enter the filesystem, it's possible that it | |
3717 | * is due to the caller holding an FS lock or performing a journal | |
3718 | * transaction in the case of a filesystem like ext[3|4]. In this case, | |
3719 | * it is not safe to block on pfmemalloc_wait as kswapd could be | |
3720 | * blocked waiting on the same lock. Instead, throttle for up to a | |
3721 | * second before continuing. | |
3722 | */ | |
2e786d9e | 3723 | if (!(gfp_mask & __GFP_FS)) |
5515061d | 3724 | wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, |
c73322d0 | 3725 | allow_direct_reclaim(pgdat), HZ); |
2e786d9e ML |
3726 | else |
3727 | /* Throttle until kswapd wakes the process */ | |
3728 | wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, | |
3729 | allow_direct_reclaim(pgdat)); | |
50694c28 | 3730 | |
50694c28 MG |
3731 | if (fatal_signal_pending(current)) |
3732 | return true; | |
3733 | ||
3734 | out: | |
3735 | return false; | |
5515061d MG |
3736 | } |
3737 | ||
dac1d27b | 3738 | unsigned long try_to_free_pages(struct zonelist *zonelist, int order, |
327c0e96 | 3739 | gfp_t gfp_mask, nodemask_t *nodemask) |
66e1707b | 3740 | { |
33906bc5 | 3741 | unsigned long nr_reclaimed; |
66e1707b | 3742 | struct scan_control sc = { |
ee814fe2 | 3743 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
f2f43e56 | 3744 | .gfp_mask = current_gfp_context(gfp_mask), |
b2e18757 | 3745 | .reclaim_idx = gfp_zone(gfp_mask), |
ee814fe2 JW |
3746 | .order = order, |
3747 | .nodemask = nodemask, | |
3748 | .priority = DEF_PRIORITY, | |
66e1707b | 3749 | .may_writepage = !laptop_mode, |
a6dc60f8 | 3750 | .may_unmap = 1, |
2e2e4259 | 3751 | .may_swap = 1, |
66e1707b BS |
3752 | }; |
3753 | ||
bb451fdf GT |
3754 | /* |
3755 | * scan_control uses s8 fields for order, priority, and reclaim_idx. | |
3756 | * Confirm they are large enough for max values. | |
3757 | */ | |
3758 | BUILD_BUG_ON(MAX_ORDER > S8_MAX); | |
3759 | BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); | |
3760 | BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); | |
3761 | ||
5515061d | 3762 | /* |
50694c28 MG |
3763 | * Do not enter reclaim if fatal signal was delivered while throttled. |
3764 | * 1 is returned so that the page allocator does not OOM kill at this | |
3765 | * point. | |
5515061d | 3766 | */ |
f2f43e56 | 3767 | if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) |
5515061d MG |
3768 | return 1; |
3769 | ||
1732d2b0 | 3770 | set_task_reclaim_state(current, &sc.reclaim_state); |
3481c37f | 3771 | trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask); |
33906bc5 | 3772 | |
3115cd91 | 3773 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
33906bc5 MG |
3774 | |
3775 | trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); | |
1732d2b0 | 3776 | set_task_reclaim_state(current, NULL); |
33906bc5 MG |
3777 | |
3778 | return nr_reclaimed; | |
66e1707b BS |
3779 | } |
3780 | ||
c255a458 | 3781 | #ifdef CONFIG_MEMCG |
66e1707b | 3782 | |
d2e5fb92 | 3783 | /* Only used by soft limit reclaim. Do not reuse for anything else. */ |
a9dd0a83 | 3784 | unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, |
4e416953 | 3785 | gfp_t gfp_mask, bool noswap, |
ef8f2327 | 3786 | pg_data_t *pgdat, |
0ae5e89c | 3787 | unsigned long *nr_scanned) |
4e416953 | 3788 | { |
afaf07a6 | 3789 | struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); |
4e416953 | 3790 | struct scan_control sc = { |
b8f5c566 | 3791 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
ee814fe2 | 3792 | .target_mem_cgroup = memcg, |
4e416953 BS |
3793 | .may_writepage = !laptop_mode, |
3794 | .may_unmap = 1, | |
b2e18757 | 3795 | .reclaim_idx = MAX_NR_ZONES - 1, |
4e416953 | 3796 | .may_swap = !noswap, |
4e416953 | 3797 | }; |
0ae5e89c | 3798 | |
d2e5fb92 MH |
3799 | WARN_ON_ONCE(!current->reclaim_state); |
3800 | ||
4e416953 BS |
3801 | sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | |
3802 | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); | |
bdce6d9e | 3803 | |
9e3b2f8c | 3804 | trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, |
3481c37f | 3805 | sc.gfp_mask); |
bdce6d9e | 3806 | |
4e416953 BS |
3807 | /* |
3808 | * NOTE: Although we can get the priority field, using it | |
3809 | * here is not a good idea, since it limits the pages we can scan. | |
a9dd0a83 | 3810 | * if we don't reclaim here, the shrink_node from balance_pgdat |
4e416953 BS |
3811 | * will pick up pages from other mem cgroup's as well. We hack |
3812 | * the priority and make it zero. | |
3813 | */ | |
afaf07a6 | 3814 | shrink_lruvec(lruvec, &sc); |
bdce6d9e KM |
3815 | |
3816 | trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); | |
3817 | ||
0ae5e89c | 3818 | *nr_scanned = sc.nr_scanned; |
0308f7cf | 3819 | |
4e416953 BS |
3820 | return sc.nr_reclaimed; |
3821 | } | |
3822 | ||
72835c86 | 3823 | unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, |
b70a2a21 | 3824 | unsigned long nr_pages, |
a7885eb8 | 3825 | gfp_t gfp_mask, |
b70a2a21 | 3826 | bool may_swap) |
66e1707b | 3827 | { |
bdce6d9e | 3828 | unsigned long nr_reclaimed; |
499118e9 | 3829 | unsigned int noreclaim_flag; |
66e1707b | 3830 | struct scan_control sc = { |
b70a2a21 | 3831 | .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), |
7dea19f9 | 3832 | .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | |
a09ed5e0 | 3833 | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), |
b2e18757 | 3834 | .reclaim_idx = MAX_NR_ZONES - 1, |
ee814fe2 JW |
3835 | .target_mem_cgroup = memcg, |
3836 | .priority = DEF_PRIORITY, | |
3837 | .may_writepage = !laptop_mode, | |
3838 | .may_unmap = 1, | |
b70a2a21 | 3839 | .may_swap = may_swap, |
a09ed5e0 | 3840 | }; |
889976db | 3841 | /* |
fa40d1ee SB |
3842 | * Traverse the ZONELIST_FALLBACK zonelist of the current node to put |
3843 | * equal pressure on all the nodes. This is based on the assumption that | |
3844 | * the reclaim does not bail out early. | |
889976db | 3845 | */ |
fa40d1ee | 3846 | struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); |
889976db | 3847 | |
fa40d1ee | 3848 | set_task_reclaim_state(current, &sc.reclaim_state); |
3481c37f | 3849 | trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask); |
499118e9 | 3850 | noreclaim_flag = memalloc_noreclaim_save(); |
eb414681 | 3851 | |
3115cd91 | 3852 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
eb414681 | 3853 | |
499118e9 | 3854 | memalloc_noreclaim_restore(noreclaim_flag); |
bdce6d9e | 3855 | trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); |
1732d2b0 | 3856 | set_task_reclaim_state(current, NULL); |
bdce6d9e KM |
3857 | |
3858 | return nr_reclaimed; | |
66e1707b BS |
3859 | } |
3860 | #endif | |
3861 | ||
1d82de61 | 3862 | static void age_active_anon(struct pglist_data *pgdat, |
ef8f2327 | 3863 | struct scan_control *sc) |
f16015fb | 3864 | { |
b95a2f2d | 3865 | struct mem_cgroup *memcg; |
b91ac374 | 3866 | struct lruvec *lruvec; |
f16015fb | 3867 | |
2f368a9f | 3868 | if (!can_age_anon_pages(pgdat, sc)) |
b95a2f2d JW |
3869 | return; |
3870 | ||
b91ac374 JW |
3871 | lruvec = mem_cgroup_lruvec(NULL, pgdat); |
3872 | if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON)) | |
3873 | return; | |
3874 | ||
b95a2f2d JW |
3875 | memcg = mem_cgroup_iter(NULL, NULL, NULL); |
3876 | do { | |
b91ac374 JW |
3877 | lruvec = mem_cgroup_lruvec(memcg, pgdat); |
3878 | shrink_active_list(SWAP_CLUSTER_MAX, lruvec, | |
3879 | sc, LRU_ACTIVE_ANON); | |
b95a2f2d JW |
3880 | memcg = mem_cgroup_iter(NULL, memcg, NULL); |
3881 | } while (memcg); | |
f16015fb JW |
3882 | } |
3883 | ||
97a225e6 | 3884 | static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx) |
1c30844d MG |
3885 | { |
3886 | int i; | |
3887 | struct zone *zone; | |
3888 | ||
3889 | /* | |
3890 | * Check for watermark boosts top-down as the higher zones | |
3891 | * are more likely to be boosted. Both watermarks and boosts | |
1eba09c1 | 3892 | * should not be checked at the same time as reclaim would |
1c30844d MG |
3893 | * start prematurely when there is no boosting and a lower |
3894 | * zone is balanced. | |
3895 | */ | |
97a225e6 | 3896 | for (i = highest_zoneidx; i >= 0; i--) { |
1c30844d MG |
3897 | zone = pgdat->node_zones + i; |
3898 | if (!managed_zone(zone)) | |
3899 | continue; | |
3900 | ||
3901 | if (zone->watermark_boost) | |
3902 | return true; | |
3903 | } | |
3904 | ||
3905 | return false; | |
3906 | } | |
3907 | ||
e716f2eb MG |
3908 | /* |
3909 | * Returns true if there is an eligible zone balanced for the request order | |
97a225e6 | 3910 | * and highest_zoneidx |
e716f2eb | 3911 | */ |
97a225e6 | 3912 | static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx) |
60cefed4 | 3913 | { |
e716f2eb MG |
3914 | int i; |
3915 | unsigned long mark = -1; | |
3916 | struct zone *zone; | |
60cefed4 | 3917 | |
1c30844d MG |
3918 | /* |
3919 | * Check watermarks bottom-up as lower zones are more likely to | |
3920 | * meet watermarks. | |
3921 | */ | |
97a225e6 | 3922 | for (i = 0; i <= highest_zoneidx; i++) { |
e716f2eb | 3923 | zone = pgdat->node_zones + i; |
6256c6b4 | 3924 | |
e716f2eb MG |
3925 | if (!managed_zone(zone)) |
3926 | continue; | |
3927 | ||
c574bbe9 HY |
3928 | if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) |
3929 | mark = wmark_pages(zone, WMARK_PROMO); | |
3930 | else | |
3931 | mark = high_wmark_pages(zone); | |
97a225e6 | 3932 | if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx)) |
e716f2eb MG |
3933 | return true; |
3934 | } | |
3935 | ||
3936 | /* | |
36c26128 | 3937 | * If a node has no managed zone within highest_zoneidx, it does not |
e716f2eb MG |
3938 | * need balancing by definition. This can happen if a zone-restricted |
3939 | * allocation tries to wake a remote kswapd. | |
3940 | */ | |
3941 | if (mark == -1) | |
3942 | return true; | |
3943 | ||
3944 | return false; | |
60cefed4 JW |
3945 | } |
3946 | ||
631b6e08 MG |
3947 | /* Clear pgdat state for congested, dirty or under writeback. */ |
3948 | static void clear_pgdat_congested(pg_data_t *pgdat) | |
3949 | { | |
1b05117d JW |
3950 | struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat); |
3951 | ||
3952 | clear_bit(LRUVEC_CONGESTED, &lruvec->flags); | |
631b6e08 MG |
3953 | clear_bit(PGDAT_DIRTY, &pgdat->flags); |
3954 | clear_bit(PGDAT_WRITEBACK, &pgdat->flags); | |
3955 | } | |
3956 | ||
5515061d MG |
3957 | /* |
3958 | * Prepare kswapd for sleeping. This verifies that there are no processes | |
3959 | * waiting in throttle_direct_reclaim() and that watermarks have been met. | |
3960 | * | |
3961 | * Returns true if kswapd is ready to sleep | |
3962 | */ | |
97a225e6 JK |
3963 | static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, |
3964 | int highest_zoneidx) | |
f50de2d3 | 3965 | { |
5515061d | 3966 | /* |
9e5e3661 | 3967 | * The throttled processes are normally woken up in balance_pgdat() as |
c73322d0 | 3968 | * soon as allow_direct_reclaim() is true. But there is a potential |
9e5e3661 VB |
3969 | * race between when kswapd checks the watermarks and a process gets |
3970 | * throttled. There is also a potential race if processes get | |
3971 | * throttled, kswapd wakes, a large process exits thereby balancing the | |
3972 | * zones, which causes kswapd to exit balance_pgdat() before reaching | |
3973 | * the wake up checks. If kswapd is going to sleep, no process should | |
3974 | * be sleeping on pfmemalloc_wait, so wake them now if necessary. If | |
3975 | * the wake up is premature, processes will wake kswapd and get | |
3976 | * throttled again. The difference from wake ups in balance_pgdat() is | |
3977 | * that here we are under prepare_to_wait(). | |
5515061d | 3978 | */ |
9e5e3661 VB |
3979 | if (waitqueue_active(&pgdat->pfmemalloc_wait)) |
3980 | wake_up_all(&pgdat->pfmemalloc_wait); | |
f50de2d3 | 3981 | |
c73322d0 JW |
3982 | /* Hopeless node, leave it to direct reclaim */ |
3983 | if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) | |
3984 | return true; | |
3985 | ||
97a225e6 | 3986 | if (pgdat_balanced(pgdat, order, highest_zoneidx)) { |
e716f2eb MG |
3987 | clear_pgdat_congested(pgdat); |
3988 | return true; | |
1d82de61 MG |
3989 | } |
3990 | ||
333b0a45 | 3991 | return false; |
f50de2d3 MG |
3992 | } |
3993 | ||
75485363 | 3994 | /* |
1d82de61 MG |
3995 | * kswapd shrinks a node of pages that are at or below the highest usable |
3996 | * zone that is currently unbalanced. | |
b8e83b94 MG |
3997 | * |
3998 | * Returns true if kswapd scanned at least the requested number of pages to | |
283aba9f MG |
3999 | * reclaim or if the lack of progress was due to pages under writeback. |
4000 | * This is used to determine if the scanning priority needs to be raised. | |
75485363 | 4001 | */ |
1d82de61 | 4002 | static bool kswapd_shrink_node(pg_data_t *pgdat, |
accf6242 | 4003 | struct scan_control *sc) |
75485363 | 4004 | { |
1d82de61 MG |
4005 | struct zone *zone; |
4006 | int z; | |
75485363 | 4007 | |
1d82de61 MG |
4008 | /* Reclaim a number of pages proportional to the number of zones */ |
4009 | sc->nr_to_reclaim = 0; | |
970a39a3 | 4010 | for (z = 0; z <= sc->reclaim_idx; z++) { |
1d82de61 | 4011 | zone = pgdat->node_zones + z; |
6aa303de | 4012 | if (!managed_zone(zone)) |
1d82de61 | 4013 | continue; |
7c954f6d | 4014 | |
1d82de61 MG |
4015 | sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); |
4016 | } | |
7c954f6d MG |
4017 | |
4018 | /* | |
1d82de61 MG |
4019 | * Historically care was taken to put equal pressure on all zones but |
4020 | * now pressure is applied based on node LRU order. | |
7c954f6d | 4021 | */ |
970a39a3 | 4022 | shrink_node(pgdat, sc); |
283aba9f | 4023 | |
7c954f6d | 4024 | /* |
1d82de61 MG |
4025 | * Fragmentation may mean that the system cannot be rebalanced for |
4026 | * high-order allocations. If twice the allocation size has been | |
4027 | * reclaimed then recheck watermarks only at order-0 to prevent | |
4028 | * excessive reclaim. Assume that a process requested a high-order | |
4029 | * can direct reclaim/compact. | |
7c954f6d | 4030 | */ |
9861a62c | 4031 | if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) |
1d82de61 | 4032 | sc->order = 0; |
7c954f6d | 4033 | |
b8e83b94 | 4034 | return sc->nr_scanned >= sc->nr_to_reclaim; |
75485363 MG |
4035 | } |
4036 | ||
c49c2c47 MG |
4037 | /* Page allocator PCP high watermark is lowered if reclaim is active. */ |
4038 | static inline void | |
4039 | update_reclaim_active(pg_data_t *pgdat, int highest_zoneidx, bool active) | |
4040 | { | |
4041 | int i; | |
4042 | struct zone *zone; | |
4043 | ||
4044 | for (i = 0; i <= highest_zoneidx; i++) { | |
4045 | zone = pgdat->node_zones + i; | |
4046 | ||
4047 | if (!managed_zone(zone)) | |
4048 | continue; | |
4049 | ||
4050 | if (active) | |
4051 | set_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); | |
4052 | else | |
4053 | clear_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); | |
4054 | } | |
4055 | } | |
4056 | ||
4057 | static inline void | |
4058 | set_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) | |
4059 | { | |
4060 | update_reclaim_active(pgdat, highest_zoneidx, true); | |
4061 | } | |
4062 | ||
4063 | static inline void | |
4064 | clear_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) | |
4065 | { | |
4066 | update_reclaim_active(pgdat, highest_zoneidx, false); | |
4067 | } | |
4068 | ||
1da177e4 | 4069 | /* |
1d82de61 MG |
4070 | * For kswapd, balance_pgdat() will reclaim pages across a node from zones |
4071 | * that are eligible for use by the caller until at least one zone is | |
4072 | * balanced. | |
1da177e4 | 4073 | * |
1d82de61 | 4074 | * Returns the order kswapd finished reclaiming at. |
1da177e4 LT |
4075 | * |
4076 | * kswapd scans the zones in the highmem->normal->dma direction. It skips | |
41858966 | 4077 | * zones which have free_pages > high_wmark_pages(zone), but once a zone is |
8bb4e7a2 | 4078 | * found to have free_pages <= high_wmark_pages(zone), any page in that zone |
1d82de61 MG |
4079 | * or lower is eligible for reclaim until at least one usable zone is |
4080 | * balanced. | |
1da177e4 | 4081 | */ |
97a225e6 | 4082 | static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) |
1da177e4 | 4083 | { |
1da177e4 | 4084 | int i; |
0608f43d AM |
4085 | unsigned long nr_soft_reclaimed; |
4086 | unsigned long nr_soft_scanned; | |
eb414681 | 4087 | unsigned long pflags; |
1c30844d MG |
4088 | unsigned long nr_boost_reclaim; |
4089 | unsigned long zone_boosts[MAX_NR_ZONES] = { 0, }; | |
4090 | bool boosted; | |
1d82de61 | 4091 | struct zone *zone; |
179e9639 AM |
4092 | struct scan_control sc = { |
4093 | .gfp_mask = GFP_KERNEL, | |
ee814fe2 | 4094 | .order = order, |
a6dc60f8 | 4095 | .may_unmap = 1, |
179e9639 | 4096 | }; |
93781325 | 4097 | |
1732d2b0 | 4098 | set_task_reclaim_state(current, &sc.reclaim_state); |
eb414681 | 4099 | psi_memstall_enter(&pflags); |
4f3eaf45 | 4100 | __fs_reclaim_acquire(_THIS_IP_); |
93781325 | 4101 | |
f8891e5e | 4102 | count_vm_event(PAGEOUTRUN); |
1da177e4 | 4103 | |
1c30844d MG |
4104 | /* |
4105 | * Account for the reclaim boost. Note that the zone boost is left in | |
4106 | * place so that parallel allocations that are near the watermark will | |
4107 | * stall or direct reclaim until kswapd is finished. | |
4108 | */ | |
4109 | nr_boost_reclaim = 0; | |
97a225e6 | 4110 | for (i = 0; i <= highest_zoneidx; i++) { |
1c30844d MG |
4111 | zone = pgdat->node_zones + i; |
4112 | if (!managed_zone(zone)) | |
4113 | continue; | |
4114 | ||
4115 | nr_boost_reclaim += zone->watermark_boost; | |
4116 | zone_boosts[i] = zone->watermark_boost; | |
4117 | } | |
4118 | boosted = nr_boost_reclaim; | |
4119 | ||
4120 | restart: | |
c49c2c47 | 4121 | set_reclaim_active(pgdat, highest_zoneidx); |
1c30844d | 4122 | sc.priority = DEF_PRIORITY; |
9e3b2f8c | 4123 | do { |
c73322d0 | 4124 | unsigned long nr_reclaimed = sc.nr_reclaimed; |
b8e83b94 | 4125 | bool raise_priority = true; |
1c30844d | 4126 | bool balanced; |
93781325 | 4127 | bool ret; |
b8e83b94 | 4128 | |
97a225e6 | 4129 | sc.reclaim_idx = highest_zoneidx; |
1da177e4 | 4130 | |
86c79f6b | 4131 | /* |
84c7a777 MG |
4132 | * If the number of buffer_heads exceeds the maximum allowed |
4133 | * then consider reclaiming from all zones. This has a dual | |
4134 | * purpose -- on 64-bit systems it is expected that | |
4135 | * buffer_heads are stripped during active rotation. On 32-bit | |
4136 | * systems, highmem pages can pin lowmem memory and shrinking | |
4137 | * buffers can relieve lowmem pressure. Reclaim may still not | |
4138 | * go ahead if all eligible zones for the original allocation | |
4139 | * request are balanced to avoid excessive reclaim from kswapd. | |
86c79f6b MG |
4140 | */ |
4141 | if (buffer_heads_over_limit) { | |
4142 | for (i = MAX_NR_ZONES - 1; i >= 0; i--) { | |
4143 | zone = pgdat->node_zones + i; | |
6aa303de | 4144 | if (!managed_zone(zone)) |
86c79f6b | 4145 | continue; |
cc715d99 | 4146 | |
970a39a3 | 4147 | sc.reclaim_idx = i; |
e1dbeda6 | 4148 | break; |
1da177e4 | 4149 | } |
1da177e4 | 4150 | } |
dafcb73e | 4151 | |
86c79f6b | 4152 | /* |
1c30844d MG |
4153 | * If the pgdat is imbalanced then ignore boosting and preserve |
4154 | * the watermarks for a later time and restart. Note that the | |
4155 | * zone watermarks will be still reset at the end of balancing | |
4156 | * on the grounds that the normal reclaim should be enough to | |
4157 | * re-evaluate if boosting is required when kswapd next wakes. | |
4158 | */ | |
97a225e6 | 4159 | balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx); |
1c30844d MG |
4160 | if (!balanced && nr_boost_reclaim) { |
4161 | nr_boost_reclaim = 0; | |
4162 | goto restart; | |
4163 | } | |
4164 | ||
4165 | /* | |
4166 | * If boosting is not active then only reclaim if there are no | |
4167 | * eligible zones. Note that sc.reclaim_idx is not used as | |
4168 | * buffer_heads_over_limit may have adjusted it. | |
86c79f6b | 4169 | */ |
1c30844d | 4170 | if (!nr_boost_reclaim && balanced) |
e716f2eb | 4171 | goto out; |
e1dbeda6 | 4172 | |
1c30844d MG |
4173 | /* Limit the priority of boosting to avoid reclaim writeback */ |
4174 | if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2) | |
4175 | raise_priority = false; | |
4176 | ||
4177 | /* | |
4178 | * Do not writeback or swap pages for boosted reclaim. The | |
4179 | * intent is to relieve pressure not issue sub-optimal IO | |
4180 | * from reclaim context. If no pages are reclaimed, the | |
4181 | * reclaim will be aborted. | |
4182 | */ | |
4183 | sc.may_writepage = !laptop_mode && !nr_boost_reclaim; | |
4184 | sc.may_swap = !nr_boost_reclaim; | |
1c30844d | 4185 | |
1d82de61 MG |
4186 | /* |
4187 | * Do some background aging of the anon list, to give | |
4188 | * pages a chance to be referenced before reclaiming. All | |
4189 | * pages are rotated regardless of classzone as this is | |
4190 | * about consistent aging. | |
4191 | */ | |
ef8f2327 | 4192 | age_active_anon(pgdat, &sc); |
1d82de61 | 4193 | |
b7ea3c41 MG |
4194 | /* |
4195 | * If we're getting trouble reclaiming, start doing writepage | |
4196 | * even in laptop mode. | |
4197 | */ | |
047d72c3 | 4198 | if (sc.priority < DEF_PRIORITY - 2) |
b7ea3c41 MG |
4199 | sc.may_writepage = 1; |
4200 | ||
1d82de61 MG |
4201 | /* Call soft limit reclaim before calling shrink_node. */ |
4202 | sc.nr_scanned = 0; | |
4203 | nr_soft_scanned = 0; | |
ef8f2327 | 4204 | nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, |
1d82de61 MG |
4205 | sc.gfp_mask, &nr_soft_scanned); |
4206 | sc.nr_reclaimed += nr_soft_reclaimed; | |
4207 | ||
1da177e4 | 4208 | /* |
1d82de61 MG |
4209 | * There should be no need to raise the scanning priority if |
4210 | * enough pages are already being scanned that that high | |
4211 | * watermark would be met at 100% efficiency. | |
1da177e4 | 4212 | */ |
970a39a3 | 4213 | if (kswapd_shrink_node(pgdat, &sc)) |
1d82de61 | 4214 | raise_priority = false; |
5515061d MG |
4215 | |
4216 | /* | |
4217 | * If the low watermark is met there is no need for processes | |
4218 | * to be throttled on pfmemalloc_wait as they should not be | |
4219 | * able to safely make forward progress. Wake them | |
4220 | */ | |
4221 | if (waitqueue_active(&pgdat->pfmemalloc_wait) && | |
c73322d0 | 4222 | allow_direct_reclaim(pgdat)) |
cfc51155 | 4223 | wake_up_all(&pgdat->pfmemalloc_wait); |
5515061d | 4224 | |
b8e83b94 | 4225 | /* Check if kswapd should be suspending */ |
4f3eaf45 | 4226 | __fs_reclaim_release(_THIS_IP_); |
93781325 | 4227 | ret = try_to_freeze(); |
4f3eaf45 | 4228 | __fs_reclaim_acquire(_THIS_IP_); |
93781325 | 4229 | if (ret || kthread_should_stop()) |
b8e83b94 | 4230 | break; |
8357376d | 4231 | |
73ce02e9 | 4232 | /* |
b8e83b94 MG |
4233 | * Raise priority if scanning rate is too low or there was no |
4234 | * progress in reclaiming pages | |
73ce02e9 | 4235 | */ |
c73322d0 | 4236 | nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; |
1c30844d MG |
4237 | nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed); |
4238 | ||
4239 | /* | |
4240 | * If reclaim made no progress for a boost, stop reclaim as | |
4241 | * IO cannot be queued and it could be an infinite loop in | |
4242 | * extreme circumstances. | |
4243 | */ | |
4244 | if (nr_boost_reclaim && !nr_reclaimed) | |
4245 | break; | |
4246 | ||
c73322d0 | 4247 | if (raise_priority || !nr_reclaimed) |
b8e83b94 | 4248 | sc.priority--; |
1d82de61 | 4249 | } while (sc.priority >= 1); |
1da177e4 | 4250 | |
c73322d0 JW |
4251 | if (!sc.nr_reclaimed) |
4252 | pgdat->kswapd_failures++; | |
4253 | ||
b8e83b94 | 4254 | out: |
c49c2c47 MG |
4255 | clear_reclaim_active(pgdat, highest_zoneidx); |
4256 | ||
1c30844d MG |
4257 | /* If reclaim was boosted, account for the reclaim done in this pass */ |
4258 | if (boosted) { | |
4259 | unsigned long flags; | |
4260 | ||
97a225e6 | 4261 | for (i = 0; i <= highest_zoneidx; i++) { |
1c30844d MG |
4262 | if (!zone_boosts[i]) |
4263 | continue; | |
4264 | ||
4265 | /* Increments are under the zone lock */ | |
4266 | zone = pgdat->node_zones + i; | |
4267 | spin_lock_irqsave(&zone->lock, flags); | |
4268 | zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]); | |
4269 | spin_unlock_irqrestore(&zone->lock, flags); | |
4270 | } | |
4271 | ||
4272 | /* | |
4273 | * As there is now likely space, wakeup kcompact to defragment | |
4274 | * pageblocks. | |
4275 | */ | |
97a225e6 | 4276 | wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx); |
1c30844d MG |
4277 | } |
4278 | ||
2a2e4885 | 4279 | snapshot_refaults(NULL, pgdat); |
4f3eaf45 | 4280 | __fs_reclaim_release(_THIS_IP_); |
eb414681 | 4281 | psi_memstall_leave(&pflags); |
1732d2b0 | 4282 | set_task_reclaim_state(current, NULL); |
e5ca8071 | 4283 | |
0abdee2b | 4284 | /* |
1d82de61 MG |
4285 | * Return the order kswapd stopped reclaiming at as |
4286 | * prepare_kswapd_sleep() takes it into account. If another caller | |
4287 | * entered the allocator slow path while kswapd was awake, order will | |
4288 | * remain at the higher level. | |
0abdee2b | 4289 | */ |
1d82de61 | 4290 | return sc.order; |
1da177e4 LT |
4291 | } |
4292 | ||
e716f2eb | 4293 | /* |
97a225e6 JK |
4294 | * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to |
4295 | * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is | |
4296 | * not a valid index then either kswapd runs for first time or kswapd couldn't | |
4297 | * sleep after previous reclaim attempt (node is still unbalanced). In that | |
4298 | * case return the zone index of the previous kswapd reclaim cycle. | |
e716f2eb | 4299 | */ |
97a225e6 JK |
4300 | static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, |
4301 | enum zone_type prev_highest_zoneidx) | |
e716f2eb | 4302 | { |
97a225e6 | 4303 | enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); |
5644e1fb | 4304 | |
97a225e6 | 4305 | return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx; |
e716f2eb MG |
4306 | } |
4307 | ||
38087d9b | 4308 | static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, |
97a225e6 | 4309 | unsigned int highest_zoneidx) |
f0bc0a60 KM |
4310 | { |
4311 | long remaining = 0; | |
4312 | DEFINE_WAIT(wait); | |
4313 | ||
4314 | if (freezing(current) || kthread_should_stop()) | |
4315 | return; | |
4316 | ||
4317 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); | |
4318 | ||
333b0a45 SG |
4319 | /* |
4320 | * Try to sleep for a short interval. Note that kcompactd will only be | |
4321 | * woken if it is possible to sleep for a short interval. This is | |
4322 | * deliberate on the assumption that if reclaim cannot keep an | |
4323 | * eligible zone balanced that it's also unlikely that compaction will | |
4324 | * succeed. | |
4325 | */ | |
97a225e6 | 4326 | if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { |
fd901c95 VB |
4327 | /* |
4328 | * Compaction records what page blocks it recently failed to | |
4329 | * isolate pages from and skips them in the future scanning. | |
4330 | * When kswapd is going to sleep, it is reasonable to assume | |
4331 | * that pages and compaction may succeed so reset the cache. | |
4332 | */ | |
4333 | reset_isolation_suitable(pgdat); | |
4334 | ||
4335 | /* | |
4336 | * We have freed the memory, now we should compact it to make | |
4337 | * allocation of the requested order possible. | |
4338 | */ | |
97a225e6 | 4339 | wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx); |
fd901c95 | 4340 | |
f0bc0a60 | 4341 | remaining = schedule_timeout(HZ/10); |
38087d9b MG |
4342 | |
4343 | /* | |
97a225e6 | 4344 | * If woken prematurely then reset kswapd_highest_zoneidx and |
38087d9b MG |
4345 | * order. The values will either be from a wakeup request or |
4346 | * the previous request that slept prematurely. | |
4347 | */ | |
4348 | if (remaining) { | |
97a225e6 JK |
4349 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, |
4350 | kswapd_highest_zoneidx(pgdat, | |
4351 | highest_zoneidx)); | |
5644e1fb QC |
4352 | |
4353 | if (READ_ONCE(pgdat->kswapd_order) < reclaim_order) | |
4354 | WRITE_ONCE(pgdat->kswapd_order, reclaim_order); | |
38087d9b MG |
4355 | } |
4356 | ||
f0bc0a60 KM |
4357 | finish_wait(&pgdat->kswapd_wait, &wait); |
4358 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); | |
4359 | } | |
4360 | ||
4361 | /* | |
4362 | * After a short sleep, check if it was a premature sleep. If not, then | |
4363 | * go fully to sleep until explicitly woken up. | |
4364 | */ | |
d9f21d42 | 4365 | if (!remaining && |
97a225e6 | 4366 | prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { |
f0bc0a60 KM |
4367 | trace_mm_vmscan_kswapd_sleep(pgdat->node_id); |
4368 | ||
4369 | /* | |
4370 | * vmstat counters are not perfectly accurate and the estimated | |
4371 | * value for counters such as NR_FREE_PAGES can deviate from the | |
4372 | * true value by nr_online_cpus * threshold. To avoid the zone | |
4373 | * watermarks being breached while under pressure, we reduce the | |
4374 | * per-cpu vmstat threshold while kswapd is awake and restore | |
4375 | * them before going back to sleep. | |
4376 | */ | |
4377 | set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); | |
1c7e7f6c AK |
4378 | |
4379 | if (!kthread_should_stop()) | |
4380 | schedule(); | |
4381 | ||
f0bc0a60 KM |
4382 | set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); |
4383 | } else { | |
4384 | if (remaining) | |
4385 | count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); | |
4386 | else | |
4387 | count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); | |
4388 | } | |
4389 | finish_wait(&pgdat->kswapd_wait, &wait); | |
4390 | } | |
4391 | ||
1da177e4 LT |
4392 | /* |
4393 | * The background pageout daemon, started as a kernel thread | |
4f98a2fe | 4394 | * from the init process. |
1da177e4 LT |
4395 | * |
4396 | * This basically trickles out pages so that we have _some_ | |
4397 | * free memory available even if there is no other activity | |
4398 | * that frees anything up. This is needed for things like routing | |
4399 | * etc, where we otherwise might have all activity going on in | |
4400 | * asynchronous contexts that cannot page things out. | |
4401 | * | |
4402 | * If there are applications that are active memory-allocators | |
4403 | * (most normal use), this basically shouldn't matter. | |
4404 | */ | |
4405 | static int kswapd(void *p) | |
4406 | { | |
e716f2eb | 4407 | unsigned int alloc_order, reclaim_order; |
97a225e6 | 4408 | unsigned int highest_zoneidx = MAX_NR_ZONES - 1; |
68d68ff6 | 4409 | pg_data_t *pgdat = (pg_data_t *)p; |
1da177e4 | 4410 | struct task_struct *tsk = current; |
a70f7302 | 4411 | const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); |
1da177e4 | 4412 | |
174596a0 | 4413 | if (!cpumask_empty(cpumask)) |
c5f59f08 | 4414 | set_cpus_allowed_ptr(tsk, cpumask); |
1da177e4 LT |
4415 | |
4416 | /* | |
4417 | * Tell the memory management that we're a "memory allocator", | |
4418 | * and that if we need more memory we should get access to it | |
4419 | * regardless (see "__alloc_pages()"). "kswapd" should | |
4420 | * never get caught in the normal page freeing logic. | |
4421 | * | |
4422 | * (Kswapd normally doesn't need memory anyway, but sometimes | |
4423 | * you need a small amount of memory in order to be able to | |
4424 | * page out something else, and this flag essentially protects | |
4425 | * us from recursively trying to free more memory as we're | |
4426 | * trying to free the first piece of memory in the first place). | |
4427 | */ | |
b698f0a1 | 4428 | tsk->flags |= PF_MEMALLOC | PF_KSWAPD; |
83144186 | 4429 | set_freezable(); |
1da177e4 | 4430 | |
5644e1fb | 4431 | WRITE_ONCE(pgdat->kswapd_order, 0); |
97a225e6 | 4432 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); |
8cd7c588 | 4433 | atomic_set(&pgdat->nr_writeback_throttled, 0); |
1da177e4 | 4434 | for ( ; ; ) { |
6f6313d4 | 4435 | bool ret; |
3e1d1d28 | 4436 | |
5644e1fb | 4437 | alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order); |
97a225e6 JK |
4438 | highest_zoneidx = kswapd_highest_zoneidx(pgdat, |
4439 | highest_zoneidx); | |
e716f2eb | 4440 | |
38087d9b MG |
4441 | kswapd_try_sleep: |
4442 | kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, | |
97a225e6 | 4443 | highest_zoneidx); |
215ddd66 | 4444 | |
97a225e6 | 4445 | /* Read the new order and highest_zoneidx */ |
2b47a24c | 4446 | alloc_order = READ_ONCE(pgdat->kswapd_order); |
97a225e6 JK |
4447 | highest_zoneidx = kswapd_highest_zoneidx(pgdat, |
4448 | highest_zoneidx); | |
5644e1fb | 4449 | WRITE_ONCE(pgdat->kswapd_order, 0); |
97a225e6 | 4450 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); |
1da177e4 | 4451 | |
8fe23e05 DR |
4452 | ret = try_to_freeze(); |
4453 | if (kthread_should_stop()) | |
4454 | break; | |
4455 | ||
4456 | /* | |
4457 | * We can speed up thawing tasks if we don't call balance_pgdat | |
4458 | * after returning from the refrigerator | |
4459 | */ | |
38087d9b MG |
4460 | if (ret) |
4461 | continue; | |
4462 | ||
4463 | /* | |
4464 | * Reclaim begins at the requested order but if a high-order | |
4465 | * reclaim fails then kswapd falls back to reclaiming for | |
4466 | * order-0. If that happens, kswapd will consider sleeping | |
4467 | * for the order it finished reclaiming at (reclaim_order) | |
4468 | * but kcompactd is woken to compact for the original | |
4469 | * request (alloc_order). | |
4470 | */ | |
97a225e6 | 4471 | trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx, |
e5146b12 | 4472 | alloc_order); |
97a225e6 JK |
4473 | reclaim_order = balance_pgdat(pgdat, alloc_order, |
4474 | highest_zoneidx); | |
38087d9b MG |
4475 | if (reclaim_order < alloc_order) |
4476 | goto kswapd_try_sleep; | |
1da177e4 | 4477 | } |
b0a8cc58 | 4478 | |
b698f0a1 | 4479 | tsk->flags &= ~(PF_MEMALLOC | PF_KSWAPD); |
71abdc15 | 4480 | |
1da177e4 LT |
4481 | return 0; |
4482 | } | |
4483 | ||
4484 | /* | |
5ecd9d40 DR |
4485 | * A zone is low on free memory or too fragmented for high-order memory. If |
4486 | * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's | |
4487 | * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim | |
4488 | * has failed or is not needed, still wake up kcompactd if only compaction is | |
4489 | * needed. | |
1da177e4 | 4490 | */ |
5ecd9d40 | 4491 | void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, |
97a225e6 | 4492 | enum zone_type highest_zoneidx) |
1da177e4 LT |
4493 | { |
4494 | pg_data_t *pgdat; | |
5644e1fb | 4495 | enum zone_type curr_idx; |
1da177e4 | 4496 | |
6aa303de | 4497 | if (!managed_zone(zone)) |
1da177e4 LT |
4498 | return; |
4499 | ||
5ecd9d40 | 4500 | if (!cpuset_zone_allowed(zone, gfp_flags)) |
1da177e4 | 4501 | return; |
5644e1fb | 4502 | |
88f5acf8 | 4503 | pgdat = zone->zone_pgdat; |
97a225e6 | 4504 | curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); |
5644e1fb | 4505 | |
97a225e6 JK |
4506 | if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx) |
4507 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx); | |
5644e1fb QC |
4508 | |
4509 | if (READ_ONCE(pgdat->kswapd_order) < order) | |
4510 | WRITE_ONCE(pgdat->kswapd_order, order); | |
dffcac2c | 4511 | |
8d0986e2 | 4512 | if (!waitqueue_active(&pgdat->kswapd_wait)) |
1da177e4 | 4513 | return; |
e1a55637 | 4514 | |
5ecd9d40 DR |
4515 | /* Hopeless node, leave it to direct reclaim if possible */ |
4516 | if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || | |
97a225e6 JK |
4517 | (pgdat_balanced(pgdat, order, highest_zoneidx) && |
4518 | !pgdat_watermark_boosted(pgdat, highest_zoneidx))) { | |
5ecd9d40 DR |
4519 | /* |
4520 | * There may be plenty of free memory available, but it's too | |
4521 | * fragmented for high-order allocations. Wake up kcompactd | |
4522 | * and rely on compaction_suitable() to determine if it's | |
4523 | * needed. If it fails, it will defer subsequent attempts to | |
4524 | * ratelimit its work. | |
4525 | */ | |
4526 | if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) | |
97a225e6 | 4527 | wakeup_kcompactd(pgdat, order, highest_zoneidx); |
e716f2eb | 4528 | return; |
5ecd9d40 | 4529 | } |
88f5acf8 | 4530 | |
97a225e6 | 4531 | trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order, |
5ecd9d40 | 4532 | gfp_flags); |
8d0986e2 | 4533 | wake_up_interruptible(&pgdat->kswapd_wait); |
1da177e4 LT |
4534 | } |
4535 | ||
c6f37f12 | 4536 | #ifdef CONFIG_HIBERNATION |
1da177e4 | 4537 | /* |
7b51755c | 4538 | * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of |
d6277db4 RW |
4539 | * freed pages. |
4540 | * | |
4541 | * Rather than trying to age LRUs the aim is to preserve the overall | |
4542 | * LRU order by reclaiming preferentially | |
4543 | * inactive > active > active referenced > active mapped | |
1da177e4 | 4544 | */ |
7b51755c | 4545 | unsigned long shrink_all_memory(unsigned long nr_to_reclaim) |
1da177e4 | 4546 | { |
d6277db4 | 4547 | struct scan_control sc = { |
ee814fe2 | 4548 | .nr_to_reclaim = nr_to_reclaim, |
7b51755c | 4549 | .gfp_mask = GFP_HIGHUSER_MOVABLE, |
b2e18757 | 4550 | .reclaim_idx = MAX_NR_ZONES - 1, |
ee814fe2 | 4551 | .priority = DEF_PRIORITY, |
d6277db4 | 4552 | .may_writepage = 1, |
ee814fe2 JW |
4553 | .may_unmap = 1, |
4554 | .may_swap = 1, | |
7b51755c | 4555 | .hibernation_mode = 1, |
1da177e4 | 4556 | }; |
a09ed5e0 | 4557 | struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); |
7b51755c | 4558 | unsigned long nr_reclaimed; |
499118e9 | 4559 | unsigned int noreclaim_flag; |
1da177e4 | 4560 | |
d92a8cfc | 4561 | fs_reclaim_acquire(sc.gfp_mask); |
93781325 | 4562 | noreclaim_flag = memalloc_noreclaim_save(); |
1732d2b0 | 4563 | set_task_reclaim_state(current, &sc.reclaim_state); |
d6277db4 | 4564 | |
3115cd91 | 4565 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
d979677c | 4566 | |
1732d2b0 | 4567 | set_task_reclaim_state(current, NULL); |
499118e9 | 4568 | memalloc_noreclaim_restore(noreclaim_flag); |
93781325 | 4569 | fs_reclaim_release(sc.gfp_mask); |
d6277db4 | 4570 | |
7b51755c | 4571 | return nr_reclaimed; |
1da177e4 | 4572 | } |
c6f37f12 | 4573 | #endif /* CONFIG_HIBERNATION */ |
1da177e4 | 4574 | |
3218ae14 YG |
4575 | /* |
4576 | * This kswapd start function will be called by init and node-hot-add. | |
3218ae14 | 4577 | */ |
b87c517a | 4578 | void kswapd_run(int nid) |
3218ae14 YG |
4579 | { |
4580 | pg_data_t *pgdat = NODE_DATA(nid); | |
3218ae14 YG |
4581 | |
4582 | if (pgdat->kswapd) | |
b87c517a | 4583 | return; |
3218ae14 YG |
4584 | |
4585 | pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); | |
4586 | if (IS_ERR(pgdat->kswapd)) { | |
4587 | /* failure at boot is fatal */ | |
c6202adf | 4588 | BUG_ON(system_state < SYSTEM_RUNNING); |
d5dc0ad9 | 4589 | pr_err("Failed to start kswapd on node %d\n", nid); |
d72515b8 | 4590 | pgdat->kswapd = NULL; |
3218ae14 | 4591 | } |
3218ae14 YG |
4592 | } |
4593 | ||
8fe23e05 | 4594 | /* |
d8adde17 | 4595 | * Called by memory hotplug when all memory in a node is offlined. Caller must |
bfc8c901 | 4596 | * hold mem_hotplug_begin/end(). |
8fe23e05 DR |
4597 | */ |
4598 | void kswapd_stop(int nid) | |
4599 | { | |
4600 | struct task_struct *kswapd = NODE_DATA(nid)->kswapd; | |
4601 | ||
d8adde17 | 4602 | if (kswapd) { |
8fe23e05 | 4603 | kthread_stop(kswapd); |
d8adde17 JL |
4604 | NODE_DATA(nid)->kswapd = NULL; |
4605 | } | |
8fe23e05 DR |
4606 | } |
4607 | ||
1da177e4 LT |
4608 | static int __init kswapd_init(void) |
4609 | { | |
6b700b5b | 4610 | int nid; |
69e05944 | 4611 | |
1da177e4 | 4612 | swap_setup(); |
48fb2e24 | 4613 | for_each_node_state(nid, N_MEMORY) |
3218ae14 | 4614 | kswapd_run(nid); |
1da177e4 LT |
4615 | return 0; |
4616 | } | |
4617 | ||
4618 | module_init(kswapd_init) | |
9eeff239 CL |
4619 | |
4620 | #ifdef CONFIG_NUMA | |
4621 | /* | |
a5f5f91d | 4622 | * Node reclaim mode |
9eeff239 | 4623 | * |
a5f5f91d | 4624 | * If non-zero call node_reclaim when the number of free pages falls below |
9eeff239 | 4625 | * the watermarks. |
9eeff239 | 4626 | */ |
a5f5f91d | 4627 | int node_reclaim_mode __read_mostly; |
9eeff239 | 4628 | |
a92f7126 | 4629 | /* |
a5f5f91d | 4630 | * Priority for NODE_RECLAIM. This determines the fraction of pages |
a92f7126 CL |
4631 | * of a node considered for each zone_reclaim. 4 scans 1/16th of |
4632 | * a zone. | |
4633 | */ | |
a5f5f91d | 4634 | #define NODE_RECLAIM_PRIORITY 4 |
a92f7126 | 4635 | |
9614634f | 4636 | /* |
a5f5f91d | 4637 | * Percentage of pages in a zone that must be unmapped for node_reclaim to |
9614634f CL |
4638 | * occur. |
4639 | */ | |
4640 | int sysctl_min_unmapped_ratio = 1; | |
4641 | ||
0ff38490 CL |
4642 | /* |
4643 | * If the number of slab pages in a zone grows beyond this percentage then | |
4644 | * slab reclaim needs to occur. | |
4645 | */ | |
4646 | int sysctl_min_slab_ratio = 5; | |
4647 | ||
11fb9989 | 4648 | static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) |
90afa5de | 4649 | { |
11fb9989 MG |
4650 | unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); |
4651 | unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + | |
4652 | node_page_state(pgdat, NR_ACTIVE_FILE); | |
90afa5de MG |
4653 | |
4654 | /* | |
4655 | * It's possible for there to be more file mapped pages than | |
4656 | * accounted for by the pages on the file LRU lists because | |
4657 | * tmpfs pages accounted for as ANON can also be FILE_MAPPED | |
4658 | */ | |
4659 | return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; | |
4660 | } | |
4661 | ||
4662 | /* Work out how many page cache pages we can reclaim in this reclaim_mode */ | |
a5f5f91d | 4663 | static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) |
90afa5de | 4664 | { |
d031a157 AM |
4665 | unsigned long nr_pagecache_reclaimable; |
4666 | unsigned long delta = 0; | |
90afa5de MG |
4667 | |
4668 | /* | |
95bbc0c7 | 4669 | * If RECLAIM_UNMAP is set, then all file pages are considered |
90afa5de | 4670 | * potentially reclaimable. Otherwise, we have to worry about |
11fb9989 | 4671 | * pages like swapcache and node_unmapped_file_pages() provides |
90afa5de MG |
4672 | * a better estimate |
4673 | */ | |
a5f5f91d MG |
4674 | if (node_reclaim_mode & RECLAIM_UNMAP) |
4675 | nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); | |
90afa5de | 4676 | else |
a5f5f91d | 4677 | nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); |
90afa5de MG |
4678 | |
4679 | /* If we can't clean pages, remove dirty pages from consideration */ | |
a5f5f91d MG |
4680 | if (!(node_reclaim_mode & RECLAIM_WRITE)) |
4681 | delta += node_page_state(pgdat, NR_FILE_DIRTY); | |
90afa5de MG |
4682 | |
4683 | /* Watch for any possible underflows due to delta */ | |
4684 | if (unlikely(delta > nr_pagecache_reclaimable)) | |
4685 | delta = nr_pagecache_reclaimable; | |
4686 | ||
4687 | return nr_pagecache_reclaimable - delta; | |
4688 | } | |
4689 | ||
9eeff239 | 4690 | /* |
a5f5f91d | 4691 | * Try to free up some pages from this node through reclaim. |
9eeff239 | 4692 | */ |
a5f5f91d | 4693 | static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) |
9eeff239 | 4694 | { |
7fb2d46d | 4695 | /* Minimum pages needed in order to stay on node */ |
69e05944 | 4696 | const unsigned long nr_pages = 1 << order; |
9eeff239 | 4697 | struct task_struct *p = current; |
499118e9 | 4698 | unsigned int noreclaim_flag; |
179e9639 | 4699 | struct scan_control sc = { |
62b726c1 | 4700 | .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), |
f2f43e56 | 4701 | .gfp_mask = current_gfp_context(gfp_mask), |
bd2f6199 | 4702 | .order = order, |
a5f5f91d MG |
4703 | .priority = NODE_RECLAIM_PRIORITY, |
4704 | .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), | |
4705 | .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), | |
ee814fe2 | 4706 | .may_swap = 1, |
f2f43e56 | 4707 | .reclaim_idx = gfp_zone(gfp_mask), |
179e9639 | 4708 | }; |
57f29762 | 4709 | unsigned long pflags; |
9eeff239 | 4710 | |
132bb8cf YS |
4711 | trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order, |
4712 | sc.gfp_mask); | |
4713 | ||
9eeff239 | 4714 | cond_resched(); |
57f29762 | 4715 | psi_memstall_enter(&pflags); |
93781325 | 4716 | fs_reclaim_acquire(sc.gfp_mask); |
d4f7796e | 4717 | /* |
95bbc0c7 | 4718 | * We need to be able to allocate from the reserves for RECLAIM_UNMAP |
d4f7796e | 4719 | */ |
499118e9 | 4720 | noreclaim_flag = memalloc_noreclaim_save(); |
1732d2b0 | 4721 | set_task_reclaim_state(p, &sc.reclaim_state); |
c84db23c | 4722 | |
d8ff6fde ML |
4723 | if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages || |
4724 | node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) > pgdat->min_slab_pages) { | |
0ff38490 | 4725 | /* |
894befec | 4726 | * Free memory by calling shrink node with increasing |
0ff38490 CL |
4727 | * priorities until we have enough memory freed. |
4728 | */ | |
0ff38490 | 4729 | do { |
970a39a3 | 4730 | shrink_node(pgdat, &sc); |
9e3b2f8c | 4731 | } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); |
0ff38490 | 4732 | } |
c84db23c | 4733 | |
1732d2b0 | 4734 | set_task_reclaim_state(p, NULL); |
499118e9 | 4735 | memalloc_noreclaim_restore(noreclaim_flag); |
93781325 | 4736 | fs_reclaim_release(sc.gfp_mask); |
57f29762 | 4737 | psi_memstall_leave(&pflags); |
132bb8cf YS |
4738 | |
4739 | trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed); | |
4740 | ||
a79311c1 | 4741 | return sc.nr_reclaimed >= nr_pages; |
9eeff239 | 4742 | } |
179e9639 | 4743 | |
a5f5f91d | 4744 | int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) |
179e9639 | 4745 | { |
d773ed6b | 4746 | int ret; |
179e9639 AM |
4747 | |
4748 | /* | |
a5f5f91d | 4749 | * Node reclaim reclaims unmapped file backed pages and |
0ff38490 | 4750 | * slab pages if we are over the defined limits. |
34aa1330 | 4751 | * |
9614634f CL |
4752 | * A small portion of unmapped file backed pages is needed for |
4753 | * file I/O otherwise pages read by file I/O will be immediately | |
a5f5f91d MG |
4754 | * thrown out if the node is overallocated. So we do not reclaim |
4755 | * if less than a specified percentage of the node is used by | |
9614634f | 4756 | * unmapped file backed pages. |
179e9639 | 4757 | */ |
a5f5f91d | 4758 | if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && |
d42f3245 RG |
4759 | node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <= |
4760 | pgdat->min_slab_pages) | |
a5f5f91d | 4761 | return NODE_RECLAIM_FULL; |
179e9639 AM |
4762 | |
4763 | /* | |
d773ed6b | 4764 | * Do not scan if the allocation should not be delayed. |
179e9639 | 4765 | */ |
d0164adc | 4766 | if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) |
a5f5f91d | 4767 | return NODE_RECLAIM_NOSCAN; |
179e9639 AM |
4768 | |
4769 | /* | |
a5f5f91d | 4770 | * Only run node reclaim on the local node or on nodes that do not |
179e9639 AM |
4771 | * have associated processors. This will favor the local processor |
4772 | * over remote processors and spread off node memory allocations | |
4773 | * as wide as possible. | |
4774 | */ | |
a5f5f91d MG |
4775 | if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) |
4776 | return NODE_RECLAIM_NOSCAN; | |
d773ed6b | 4777 | |
a5f5f91d MG |
4778 | if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) |
4779 | return NODE_RECLAIM_NOSCAN; | |
fa5e084e | 4780 | |
a5f5f91d MG |
4781 | ret = __node_reclaim(pgdat, gfp_mask, order); |
4782 | clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); | |
d773ed6b | 4783 | |
24cf7251 MG |
4784 | if (!ret) |
4785 | count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); | |
4786 | ||
d773ed6b | 4787 | return ret; |
179e9639 | 4788 | } |
9eeff239 | 4789 | #endif |
894bc310 | 4790 | |
89e004ea | 4791 | /** |
64e3d12f KHY |
4792 | * check_move_unevictable_pages - check pages for evictability and move to |
4793 | * appropriate zone lru list | |
4794 | * @pvec: pagevec with lru pages to check | |
89e004ea | 4795 | * |
64e3d12f KHY |
4796 | * Checks pages for evictability, if an evictable page is in the unevictable |
4797 | * lru list, moves it to the appropriate evictable lru list. This function | |
4798 | * should be only used for lru pages. | |
89e004ea | 4799 | */ |
64e3d12f | 4800 | void check_move_unevictable_pages(struct pagevec *pvec) |
89e004ea | 4801 | { |
6168d0da | 4802 | struct lruvec *lruvec = NULL; |
24513264 HD |
4803 | int pgscanned = 0; |
4804 | int pgrescued = 0; | |
4805 | int i; | |
89e004ea | 4806 | |
64e3d12f KHY |
4807 | for (i = 0; i < pvec->nr; i++) { |
4808 | struct page *page = pvec->pages[i]; | |
0de340cb | 4809 | struct folio *folio = page_folio(page); |
8d8869ca HD |
4810 | int nr_pages; |
4811 | ||
4812 | if (PageTransTail(page)) | |
4813 | continue; | |
4814 | ||
4815 | nr_pages = thp_nr_pages(page); | |
4816 | pgscanned += nr_pages; | |
89e004ea | 4817 | |
d25b5bd8 AS |
4818 | /* block memcg migration during page moving between lru */ |
4819 | if (!TestClearPageLRU(page)) | |
4820 | continue; | |
4821 | ||
0de340cb | 4822 | lruvec = folio_lruvec_relock_irq(folio, lruvec); |
d25b5bd8 | 4823 | if (page_evictable(page) && PageUnevictable(page)) { |
46ae6b2c | 4824 | del_page_from_lru_list(page, lruvec); |
24513264 | 4825 | ClearPageUnevictable(page); |
3a9c9788 | 4826 | add_page_to_lru_list(page, lruvec); |
8d8869ca | 4827 | pgrescued += nr_pages; |
89e004ea | 4828 | } |
d25b5bd8 | 4829 | SetPageLRU(page); |
24513264 | 4830 | } |
89e004ea | 4831 | |
6168d0da | 4832 | if (lruvec) { |
24513264 HD |
4833 | __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); |
4834 | __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); | |
6168d0da | 4835 | unlock_page_lruvec_irq(lruvec); |
d25b5bd8 AS |
4836 | } else if (pgscanned) { |
4837 | count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); | |
89e004ea | 4838 | } |
89e004ea | 4839 | } |
64e3d12f | 4840 | EXPORT_SYMBOL_GPL(check_move_unevictable_pages); |