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