Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/mm/vmscan.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds | |
5 | * | |
6 | * Swap reorganised 29.12.95, Stephen Tweedie. | |
7 | * kswapd added: 7.1.96 sct | |
8 | * Removed kswapd_ctl limits, and swap out as many pages as needed | |
9 | * to bring the system back to freepages.high: 2.4.97, Rik van Riel. | |
10 | * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com). | |
11 | * Multiqueue VM started 5.8.00, Rik van Riel. | |
12 | */ | |
13 | ||
b1de0d13 MH |
14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
15 | ||
1da177e4 LT |
16 | #include <linux/mm.h> |
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> |
268bb0ce | 47 | #include <linux/prefetch.h> |
b1de0d13 | 48 | #include <linux/printk.h> |
f9fe48be | 49 | #include <linux/dax.h> |
1da177e4 LT |
50 | |
51 | #include <asm/tlbflush.h> | |
52 | #include <asm/div64.h> | |
53 | ||
54 | #include <linux/swapops.h> | |
117aad1e | 55 | #include <linux/balloon_compaction.h> |
1da177e4 | 56 | |
0f8053a5 NP |
57 | #include "internal.h" |
58 | ||
33906bc5 MG |
59 | #define CREATE_TRACE_POINTS |
60 | #include <trace/events/vmscan.h> | |
61 | ||
1da177e4 | 62 | struct scan_control { |
22fba335 KM |
63 | /* How many pages shrink_list() should reclaim */ |
64 | unsigned long nr_to_reclaim; | |
65 | ||
1da177e4 | 66 | /* This context's GFP mask */ |
6daa0e28 | 67 | gfp_t gfp_mask; |
1da177e4 | 68 | |
ee814fe2 | 69 | /* Allocation order */ |
5ad333eb | 70 | int order; |
66e1707b | 71 | |
ee814fe2 JW |
72 | /* |
73 | * Nodemask of nodes allowed by the caller. If NULL, all nodes | |
74 | * are scanned. | |
75 | */ | |
76 | nodemask_t *nodemask; | |
9e3b2f8c | 77 | |
f16015fb JW |
78 | /* |
79 | * The memory cgroup that hit its limit and as a result is the | |
80 | * primary target of this reclaim invocation. | |
81 | */ | |
82 | struct mem_cgroup *target_mem_cgroup; | |
66e1707b | 83 | |
ee814fe2 JW |
84 | /* Scan (total_size >> priority) pages at once */ |
85 | int priority; | |
86 | ||
b2e18757 MG |
87 | /* The highest zone to isolate pages for reclaim from */ |
88 | enum zone_type reclaim_idx; | |
89 | ||
ee814fe2 JW |
90 | unsigned int may_writepage:1; |
91 | ||
92 | /* Can mapped pages be reclaimed? */ | |
93 | unsigned int may_unmap:1; | |
94 | ||
95 | /* Can pages be swapped as part of reclaim? */ | |
96 | unsigned int may_swap:1; | |
97 | ||
241994ed JW |
98 | /* Can cgroups be reclaimed below their normal consumption range? */ |
99 | unsigned int may_thrash:1; | |
100 | ||
ee814fe2 JW |
101 | unsigned int hibernation_mode:1; |
102 | ||
103 | /* One of the zones is ready for compaction */ | |
104 | unsigned int compaction_ready:1; | |
105 | ||
106 | /* Incremented by the number of inactive pages that were scanned */ | |
107 | unsigned long nr_scanned; | |
108 | ||
109 | /* Number of pages freed so far during a call to shrink_zones() */ | |
110 | unsigned long nr_reclaimed; | |
1da177e4 LT |
111 | }; |
112 | ||
1da177e4 LT |
113 | #ifdef ARCH_HAS_PREFETCH |
114 | #define prefetch_prev_lru_page(_page, _base, _field) \ | |
115 | do { \ | |
116 | if ((_page)->lru.prev != _base) { \ | |
117 | struct page *prev; \ | |
118 | \ | |
119 | prev = lru_to_page(&(_page->lru)); \ | |
120 | prefetch(&prev->_field); \ | |
121 | } \ | |
122 | } while (0) | |
123 | #else | |
124 | #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) | |
125 | #endif | |
126 | ||
127 | #ifdef ARCH_HAS_PREFETCHW | |
128 | #define prefetchw_prev_lru_page(_page, _base, _field) \ | |
129 | do { \ | |
130 | if ((_page)->lru.prev != _base) { \ | |
131 | struct page *prev; \ | |
132 | \ | |
133 | prev = lru_to_page(&(_page->lru)); \ | |
134 | prefetchw(&prev->_field); \ | |
135 | } \ | |
136 | } while (0) | |
137 | #else | |
138 | #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) | |
139 | #endif | |
140 | ||
141 | /* | |
142 | * From 0 .. 100. Higher means more swappy. | |
143 | */ | |
144 | int vm_swappiness = 60; | |
d0480be4 WSH |
145 | /* |
146 | * The total number of pages which are beyond the high watermark within all | |
147 | * zones. | |
148 | */ | |
149 | unsigned long vm_total_pages; | |
1da177e4 LT |
150 | |
151 | static LIST_HEAD(shrinker_list); | |
152 | static DECLARE_RWSEM(shrinker_rwsem); | |
153 | ||
c255a458 | 154 | #ifdef CONFIG_MEMCG |
89b5fae5 JW |
155 | static bool global_reclaim(struct scan_control *sc) |
156 | { | |
f16015fb | 157 | return !sc->target_mem_cgroup; |
89b5fae5 | 158 | } |
97c9341f TH |
159 | |
160 | /** | |
161 | * sane_reclaim - is the usual dirty throttling mechanism operational? | |
162 | * @sc: scan_control in question | |
163 | * | |
164 | * The normal page dirty throttling mechanism in balance_dirty_pages() is | |
165 | * completely broken with the legacy memcg and direct stalling in | |
166 | * shrink_page_list() is used for throttling instead, which lacks all the | |
167 | * niceties such as fairness, adaptive pausing, bandwidth proportional | |
168 | * allocation and configurability. | |
169 | * | |
170 | * This function tests whether the vmscan currently in progress can assume | |
171 | * that the normal dirty throttling mechanism is operational. | |
172 | */ | |
173 | static bool sane_reclaim(struct scan_control *sc) | |
174 | { | |
175 | struct mem_cgroup *memcg = sc->target_mem_cgroup; | |
176 | ||
177 | if (!memcg) | |
178 | return true; | |
179 | #ifdef CONFIG_CGROUP_WRITEBACK | |
69234ace | 180 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
97c9341f TH |
181 | return true; |
182 | #endif | |
183 | return false; | |
184 | } | |
91a45470 | 185 | #else |
89b5fae5 JW |
186 | static bool global_reclaim(struct scan_control *sc) |
187 | { | |
188 | return true; | |
189 | } | |
97c9341f TH |
190 | |
191 | static bool sane_reclaim(struct scan_control *sc) | |
192 | { | |
193 | return true; | |
194 | } | |
91a45470 KH |
195 | #endif |
196 | ||
599d0c95 MG |
197 | unsigned long pgdat_reclaimable_pages(struct pglist_data *pgdat) |
198 | { | |
199 | unsigned long nr; | |
200 | ||
201 | nr = node_page_state_snapshot(pgdat, NR_ACTIVE_FILE) + | |
202 | node_page_state_snapshot(pgdat, NR_INACTIVE_FILE) + | |
203 | node_page_state_snapshot(pgdat, NR_ISOLATED_FILE); | |
6e543d57 LD |
204 | |
205 | if (get_nr_swap_pages() > 0) | |
599d0c95 MG |
206 | nr += node_page_state_snapshot(pgdat, NR_ACTIVE_ANON) + |
207 | node_page_state_snapshot(pgdat, NR_INACTIVE_ANON) + | |
208 | node_page_state_snapshot(pgdat, NR_ISOLATED_ANON); | |
6e543d57 LD |
209 | |
210 | return nr; | |
211 | } | |
212 | ||
599d0c95 | 213 | bool pgdat_reclaimable(struct pglist_data *pgdat) |
6e543d57 | 214 | { |
599d0c95 MG |
215 | return node_page_state_snapshot(pgdat, NR_PAGES_SCANNED) < |
216 | pgdat_reclaimable_pages(pgdat) * 6; | |
6e543d57 LD |
217 | } |
218 | ||
23047a96 | 219 | unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru) |
c9f299d9 | 220 | { |
c3c787e8 | 221 | if (!mem_cgroup_disabled()) |
4d7dcca2 | 222 | return mem_cgroup_get_lru_size(lruvec, lru); |
a3d8e054 | 223 | |
599d0c95 | 224 | return node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru); |
c9f299d9 KM |
225 | } |
226 | ||
1da177e4 | 227 | /* |
1d3d4437 | 228 | * Add a shrinker callback to be called from the vm. |
1da177e4 | 229 | */ |
1d3d4437 | 230 | int register_shrinker(struct shrinker *shrinker) |
1da177e4 | 231 | { |
1d3d4437 GC |
232 | size_t size = sizeof(*shrinker->nr_deferred); |
233 | ||
1d3d4437 GC |
234 | if (shrinker->flags & SHRINKER_NUMA_AWARE) |
235 | size *= nr_node_ids; | |
236 | ||
237 | shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); | |
238 | if (!shrinker->nr_deferred) | |
239 | return -ENOMEM; | |
240 | ||
8e1f936b RR |
241 | down_write(&shrinker_rwsem); |
242 | list_add_tail(&shrinker->list, &shrinker_list); | |
243 | up_write(&shrinker_rwsem); | |
1d3d4437 | 244 | return 0; |
1da177e4 | 245 | } |
8e1f936b | 246 | EXPORT_SYMBOL(register_shrinker); |
1da177e4 LT |
247 | |
248 | /* | |
249 | * Remove one | |
250 | */ | |
8e1f936b | 251 | void unregister_shrinker(struct shrinker *shrinker) |
1da177e4 LT |
252 | { |
253 | down_write(&shrinker_rwsem); | |
254 | list_del(&shrinker->list); | |
255 | up_write(&shrinker_rwsem); | |
ae393321 | 256 | kfree(shrinker->nr_deferred); |
1da177e4 | 257 | } |
8e1f936b | 258 | EXPORT_SYMBOL(unregister_shrinker); |
1da177e4 LT |
259 | |
260 | #define SHRINK_BATCH 128 | |
1d3d4437 | 261 | |
cb731d6c VD |
262 | static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, |
263 | struct shrinker *shrinker, | |
264 | unsigned long nr_scanned, | |
265 | unsigned long nr_eligible) | |
1d3d4437 GC |
266 | { |
267 | unsigned long freed = 0; | |
268 | unsigned long long delta; | |
269 | long total_scan; | |
d5bc5fd3 | 270 | long freeable; |
1d3d4437 GC |
271 | long nr; |
272 | long new_nr; | |
273 | int nid = shrinkctl->nid; | |
274 | long batch_size = shrinker->batch ? shrinker->batch | |
275 | : SHRINK_BATCH; | |
276 | ||
d5bc5fd3 VD |
277 | freeable = shrinker->count_objects(shrinker, shrinkctl); |
278 | if (freeable == 0) | |
1d3d4437 GC |
279 | return 0; |
280 | ||
281 | /* | |
282 | * copy the current shrinker scan count into a local variable | |
283 | * and zero it so that other concurrent shrinker invocations | |
284 | * don't also do this scanning work. | |
285 | */ | |
286 | nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); | |
287 | ||
288 | total_scan = nr; | |
6b4f7799 | 289 | delta = (4 * nr_scanned) / shrinker->seeks; |
d5bc5fd3 | 290 | delta *= freeable; |
6b4f7799 | 291 | do_div(delta, nr_eligible + 1); |
1d3d4437 GC |
292 | total_scan += delta; |
293 | if (total_scan < 0) { | |
8612c663 | 294 | pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n", |
a0b02131 | 295 | shrinker->scan_objects, total_scan); |
d5bc5fd3 | 296 | total_scan = freeable; |
1d3d4437 GC |
297 | } |
298 | ||
299 | /* | |
300 | * We need to avoid excessive windup on filesystem shrinkers | |
301 | * due to large numbers of GFP_NOFS allocations causing the | |
302 | * shrinkers to return -1 all the time. This results in a large | |
303 | * nr being built up so when a shrink that can do some work | |
304 | * comes along it empties the entire cache due to nr >>> | |
d5bc5fd3 | 305 | * freeable. This is bad for sustaining a working set in |
1d3d4437 GC |
306 | * memory. |
307 | * | |
308 | * Hence only allow the shrinker to scan the entire cache when | |
309 | * a large delta change is calculated directly. | |
310 | */ | |
d5bc5fd3 VD |
311 | if (delta < freeable / 4) |
312 | total_scan = min(total_scan, freeable / 2); | |
1d3d4437 GC |
313 | |
314 | /* | |
315 | * Avoid risking looping forever due to too large nr value: | |
316 | * never try to free more than twice the estimate number of | |
317 | * freeable entries. | |
318 | */ | |
d5bc5fd3 VD |
319 | if (total_scan > freeable * 2) |
320 | total_scan = freeable * 2; | |
1d3d4437 GC |
321 | |
322 | trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, | |
6b4f7799 JW |
323 | nr_scanned, nr_eligible, |
324 | freeable, delta, total_scan); | |
1d3d4437 | 325 | |
0b1fb40a VD |
326 | /* |
327 | * Normally, we should not scan less than batch_size objects in one | |
328 | * pass to avoid too frequent shrinker calls, but if the slab has less | |
329 | * than batch_size objects in total and we are really tight on memory, | |
330 | * we will try to reclaim all available objects, otherwise we can end | |
331 | * up failing allocations although there are plenty of reclaimable | |
332 | * objects spread over several slabs with usage less than the | |
333 | * batch_size. | |
334 | * | |
335 | * We detect the "tight on memory" situations by looking at the total | |
336 | * number of objects we want to scan (total_scan). If it is greater | |
d5bc5fd3 | 337 | * than the total number of objects on slab (freeable), we must be |
0b1fb40a VD |
338 | * scanning at high prio and therefore should try to reclaim as much as |
339 | * possible. | |
340 | */ | |
341 | while (total_scan >= batch_size || | |
d5bc5fd3 | 342 | total_scan >= freeable) { |
a0b02131 | 343 | unsigned long ret; |
0b1fb40a | 344 | unsigned long nr_to_scan = min(batch_size, total_scan); |
1d3d4437 | 345 | |
0b1fb40a | 346 | shrinkctl->nr_to_scan = nr_to_scan; |
a0b02131 DC |
347 | ret = shrinker->scan_objects(shrinker, shrinkctl); |
348 | if (ret == SHRINK_STOP) | |
349 | break; | |
350 | freed += ret; | |
1d3d4437 | 351 | |
0b1fb40a VD |
352 | count_vm_events(SLABS_SCANNED, nr_to_scan); |
353 | total_scan -= nr_to_scan; | |
1d3d4437 GC |
354 | |
355 | cond_resched(); | |
356 | } | |
357 | ||
358 | /* | |
359 | * move the unused scan count back into the shrinker in a | |
360 | * manner that handles concurrent updates. If we exhausted the | |
361 | * scan, there is no need to do an update. | |
362 | */ | |
363 | if (total_scan > 0) | |
364 | new_nr = atomic_long_add_return(total_scan, | |
365 | &shrinker->nr_deferred[nid]); | |
366 | else | |
367 | new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); | |
368 | ||
df9024a8 | 369 | trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan); |
1d3d4437 | 370 | return freed; |
1495f230 YH |
371 | } |
372 | ||
6b4f7799 | 373 | /** |
cb731d6c | 374 | * shrink_slab - shrink slab caches |
6b4f7799 JW |
375 | * @gfp_mask: allocation context |
376 | * @nid: node whose slab caches to target | |
cb731d6c | 377 | * @memcg: memory cgroup whose slab caches to target |
6b4f7799 JW |
378 | * @nr_scanned: pressure numerator |
379 | * @nr_eligible: pressure denominator | |
1da177e4 | 380 | * |
6b4f7799 | 381 | * Call the shrink functions to age shrinkable caches. |
1da177e4 | 382 | * |
6b4f7799 JW |
383 | * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, |
384 | * unaware shrinkers will receive a node id of 0 instead. | |
1da177e4 | 385 | * |
cb731d6c VD |
386 | * @memcg specifies the memory cgroup to target. If it is not NULL, |
387 | * only shrinkers with SHRINKER_MEMCG_AWARE set will be called to scan | |
0fc9f58a VD |
388 | * objects from the memory cgroup specified. Otherwise, only unaware |
389 | * shrinkers are called. | |
cb731d6c | 390 | * |
6b4f7799 JW |
391 | * @nr_scanned and @nr_eligible form a ratio that indicate how much of |
392 | * the available objects should be scanned. Page reclaim for example | |
393 | * passes the number of pages scanned and the number of pages on the | |
394 | * LRU lists that it considered on @nid, plus a bias in @nr_scanned | |
395 | * when it encountered mapped pages. The ratio is further biased by | |
396 | * the ->seeks setting of the shrink function, which indicates the | |
397 | * cost to recreate an object relative to that of an LRU page. | |
b15e0905 | 398 | * |
6b4f7799 | 399 | * Returns the number of reclaimed slab objects. |
1da177e4 | 400 | */ |
cb731d6c VD |
401 | static unsigned long shrink_slab(gfp_t gfp_mask, int nid, |
402 | struct mem_cgroup *memcg, | |
403 | unsigned long nr_scanned, | |
404 | unsigned long nr_eligible) | |
1da177e4 LT |
405 | { |
406 | struct shrinker *shrinker; | |
24f7c6b9 | 407 | unsigned long freed = 0; |
1da177e4 | 408 | |
0fc9f58a | 409 | if (memcg && (!memcg_kmem_enabled() || !mem_cgroup_online(memcg))) |
cb731d6c VD |
410 | return 0; |
411 | ||
6b4f7799 JW |
412 | if (nr_scanned == 0) |
413 | nr_scanned = SWAP_CLUSTER_MAX; | |
1da177e4 | 414 | |
f06590bd | 415 | if (!down_read_trylock(&shrinker_rwsem)) { |
24f7c6b9 DC |
416 | /* |
417 | * If we would return 0, our callers would understand that we | |
418 | * have nothing else to shrink and give up trying. By returning | |
419 | * 1 we keep it going and assume we'll be able to shrink next | |
420 | * time. | |
421 | */ | |
422 | freed = 1; | |
f06590bd MK |
423 | goto out; |
424 | } | |
1da177e4 LT |
425 | |
426 | list_for_each_entry(shrinker, &shrinker_list, list) { | |
6b4f7799 JW |
427 | struct shrink_control sc = { |
428 | .gfp_mask = gfp_mask, | |
429 | .nid = nid, | |
cb731d6c | 430 | .memcg = memcg, |
6b4f7799 | 431 | }; |
ec97097b | 432 | |
0fc9f58a VD |
433 | /* |
434 | * If kernel memory accounting is disabled, we ignore | |
435 | * SHRINKER_MEMCG_AWARE flag and call all shrinkers | |
436 | * passing NULL for memcg. | |
437 | */ | |
438 | if (memcg_kmem_enabled() && | |
439 | !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE)) | |
cb731d6c VD |
440 | continue; |
441 | ||
6b4f7799 JW |
442 | if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) |
443 | sc.nid = 0; | |
1da177e4 | 444 | |
cb731d6c | 445 | freed += do_shrink_slab(&sc, shrinker, nr_scanned, nr_eligible); |
1da177e4 | 446 | } |
6b4f7799 | 447 | |
1da177e4 | 448 | up_read(&shrinker_rwsem); |
f06590bd MK |
449 | out: |
450 | cond_resched(); | |
24f7c6b9 | 451 | return freed; |
1da177e4 LT |
452 | } |
453 | ||
cb731d6c VD |
454 | void drop_slab_node(int nid) |
455 | { | |
456 | unsigned long freed; | |
457 | ||
458 | do { | |
459 | struct mem_cgroup *memcg = NULL; | |
460 | ||
461 | freed = 0; | |
462 | do { | |
463 | freed += shrink_slab(GFP_KERNEL, nid, memcg, | |
464 | 1000, 1000); | |
465 | } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); | |
466 | } while (freed > 10); | |
467 | } | |
468 | ||
469 | void drop_slab(void) | |
470 | { | |
471 | int nid; | |
472 | ||
473 | for_each_online_node(nid) | |
474 | drop_slab_node(nid); | |
475 | } | |
476 | ||
1da177e4 LT |
477 | static inline int is_page_cache_freeable(struct page *page) |
478 | { | |
ceddc3a5 JW |
479 | /* |
480 | * A freeable page cache page is referenced only by the caller | |
481 | * that isolated the page, the page cache radix tree and | |
482 | * optional buffer heads at page->private. | |
483 | */ | |
edcf4748 | 484 | return page_count(page) - page_has_private(page) == 2; |
1da177e4 LT |
485 | } |
486 | ||
703c2708 | 487 | static int may_write_to_inode(struct inode *inode, struct scan_control *sc) |
1da177e4 | 488 | { |
930d9152 | 489 | if (current->flags & PF_SWAPWRITE) |
1da177e4 | 490 | return 1; |
703c2708 | 491 | if (!inode_write_congested(inode)) |
1da177e4 | 492 | return 1; |
703c2708 | 493 | if (inode_to_bdi(inode) == current->backing_dev_info) |
1da177e4 LT |
494 | return 1; |
495 | return 0; | |
496 | } | |
497 | ||
498 | /* | |
499 | * We detected a synchronous write error writing a page out. Probably | |
500 | * -ENOSPC. We need to propagate that into the address_space for a subsequent | |
501 | * fsync(), msync() or close(). | |
502 | * | |
503 | * The tricky part is that after writepage we cannot touch the mapping: nothing | |
504 | * prevents it from being freed up. But we have a ref on the page and once | |
505 | * that page is locked, the mapping is pinned. | |
506 | * | |
507 | * We're allowed to run sleeping lock_page() here because we know the caller has | |
508 | * __GFP_FS. | |
509 | */ | |
510 | static void handle_write_error(struct address_space *mapping, | |
511 | struct page *page, int error) | |
512 | { | |
7eaceacc | 513 | lock_page(page); |
3e9f45bd GC |
514 | if (page_mapping(page) == mapping) |
515 | mapping_set_error(mapping, error); | |
1da177e4 LT |
516 | unlock_page(page); |
517 | } | |
518 | ||
04e62a29 CL |
519 | /* possible outcome of pageout() */ |
520 | typedef enum { | |
521 | /* failed to write page out, page is locked */ | |
522 | PAGE_KEEP, | |
523 | /* move page to the active list, page is locked */ | |
524 | PAGE_ACTIVATE, | |
525 | /* page has been sent to the disk successfully, page is unlocked */ | |
526 | PAGE_SUCCESS, | |
527 | /* page is clean and locked */ | |
528 | PAGE_CLEAN, | |
529 | } pageout_t; | |
530 | ||
1da177e4 | 531 | /* |
1742f19f AM |
532 | * pageout is called by shrink_page_list() for each dirty page. |
533 | * Calls ->writepage(). | |
1da177e4 | 534 | */ |
c661b078 | 535 | static pageout_t pageout(struct page *page, struct address_space *mapping, |
7d3579e8 | 536 | struct scan_control *sc) |
1da177e4 LT |
537 | { |
538 | /* | |
539 | * If the page is dirty, only perform writeback if that write | |
540 | * will be non-blocking. To prevent this allocation from being | |
541 | * stalled by pagecache activity. But note that there may be | |
542 | * stalls if we need to run get_block(). We could test | |
543 | * PagePrivate for that. | |
544 | * | |
8174202b | 545 | * If this process is currently in __generic_file_write_iter() against |
1da177e4 LT |
546 | * this page's queue, we can perform writeback even if that |
547 | * will block. | |
548 | * | |
549 | * If the page is swapcache, write it back even if that would | |
550 | * block, for some throttling. This happens by accident, because | |
551 | * swap_backing_dev_info is bust: it doesn't reflect the | |
552 | * congestion state of the swapdevs. Easy to fix, if needed. | |
1da177e4 LT |
553 | */ |
554 | if (!is_page_cache_freeable(page)) | |
555 | return PAGE_KEEP; | |
556 | if (!mapping) { | |
557 | /* | |
558 | * Some data journaling orphaned pages can have | |
559 | * page->mapping == NULL while being dirty with clean buffers. | |
560 | */ | |
266cf658 | 561 | if (page_has_private(page)) { |
1da177e4 LT |
562 | if (try_to_free_buffers(page)) { |
563 | ClearPageDirty(page); | |
b1de0d13 | 564 | pr_info("%s: orphaned page\n", __func__); |
1da177e4 LT |
565 | return PAGE_CLEAN; |
566 | } | |
567 | } | |
568 | return PAGE_KEEP; | |
569 | } | |
570 | if (mapping->a_ops->writepage == NULL) | |
571 | return PAGE_ACTIVATE; | |
703c2708 | 572 | if (!may_write_to_inode(mapping->host, sc)) |
1da177e4 LT |
573 | return PAGE_KEEP; |
574 | ||
575 | if (clear_page_dirty_for_io(page)) { | |
576 | int res; | |
577 | struct writeback_control wbc = { | |
578 | .sync_mode = WB_SYNC_NONE, | |
579 | .nr_to_write = SWAP_CLUSTER_MAX, | |
111ebb6e OH |
580 | .range_start = 0, |
581 | .range_end = LLONG_MAX, | |
1da177e4 LT |
582 | .for_reclaim = 1, |
583 | }; | |
584 | ||
585 | SetPageReclaim(page); | |
586 | res = mapping->a_ops->writepage(page, &wbc); | |
587 | if (res < 0) | |
588 | handle_write_error(mapping, page, res); | |
994fc28c | 589 | if (res == AOP_WRITEPAGE_ACTIVATE) { |
1da177e4 LT |
590 | ClearPageReclaim(page); |
591 | return PAGE_ACTIVATE; | |
592 | } | |
c661b078 | 593 | |
1da177e4 LT |
594 | if (!PageWriteback(page)) { |
595 | /* synchronous write or broken a_ops? */ | |
596 | ClearPageReclaim(page); | |
597 | } | |
3aa23851 | 598 | trace_mm_vmscan_writepage(page); |
c4a25635 | 599 | inc_node_page_state(page, NR_VMSCAN_WRITE); |
1da177e4 LT |
600 | return PAGE_SUCCESS; |
601 | } | |
602 | ||
603 | return PAGE_CLEAN; | |
604 | } | |
605 | ||
a649fd92 | 606 | /* |
e286781d NP |
607 | * Same as remove_mapping, but if the page is removed from the mapping, it |
608 | * gets returned with a refcount of 0. | |
a649fd92 | 609 | */ |
a528910e JW |
610 | static int __remove_mapping(struct address_space *mapping, struct page *page, |
611 | bool reclaimed) | |
49d2e9cc | 612 | { |
c4843a75 | 613 | unsigned long flags; |
c4843a75 | 614 | |
28e4d965 NP |
615 | BUG_ON(!PageLocked(page)); |
616 | BUG_ON(mapping != page_mapping(page)); | |
49d2e9cc | 617 | |
c4843a75 | 618 | spin_lock_irqsave(&mapping->tree_lock, flags); |
49d2e9cc | 619 | /* |
0fd0e6b0 NP |
620 | * The non racy check for a busy page. |
621 | * | |
622 | * Must be careful with the order of the tests. When someone has | |
623 | * a ref to the page, it may be possible that they dirty it then | |
624 | * drop the reference. So if PageDirty is tested before page_count | |
625 | * here, then the following race may occur: | |
626 | * | |
627 | * get_user_pages(&page); | |
628 | * [user mapping goes away] | |
629 | * write_to(page); | |
630 | * !PageDirty(page) [good] | |
631 | * SetPageDirty(page); | |
632 | * put_page(page); | |
633 | * !page_count(page) [good, discard it] | |
634 | * | |
635 | * [oops, our write_to data is lost] | |
636 | * | |
637 | * Reversing the order of the tests ensures such a situation cannot | |
638 | * escape unnoticed. The smp_rmb is needed to ensure the page->flags | |
0139aa7b | 639 | * load is not satisfied before that of page->_refcount. |
0fd0e6b0 NP |
640 | * |
641 | * Note that if SetPageDirty is always performed via set_page_dirty, | |
642 | * and thus under tree_lock, then this ordering is not required. | |
49d2e9cc | 643 | */ |
fe896d18 | 644 | if (!page_ref_freeze(page, 2)) |
49d2e9cc | 645 | goto cannot_free; |
e286781d NP |
646 | /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ |
647 | if (unlikely(PageDirty(page))) { | |
fe896d18 | 648 | page_ref_unfreeze(page, 2); |
49d2e9cc | 649 | goto cannot_free; |
e286781d | 650 | } |
49d2e9cc CL |
651 | |
652 | if (PageSwapCache(page)) { | |
653 | swp_entry_t swap = { .val = page_private(page) }; | |
0a31bc97 | 654 | mem_cgroup_swapout(page, swap); |
49d2e9cc | 655 | __delete_from_swap_cache(page); |
c4843a75 | 656 | spin_unlock_irqrestore(&mapping->tree_lock, flags); |
0a31bc97 | 657 | swapcache_free(swap); |
e286781d | 658 | } else { |
6072d13c | 659 | void (*freepage)(struct page *); |
a528910e | 660 | void *shadow = NULL; |
6072d13c LT |
661 | |
662 | freepage = mapping->a_ops->freepage; | |
a528910e JW |
663 | /* |
664 | * Remember a shadow entry for reclaimed file cache in | |
665 | * order to detect refaults, thus thrashing, later on. | |
666 | * | |
667 | * But don't store shadows in an address space that is | |
668 | * already exiting. This is not just an optizimation, | |
669 | * inode reclaim needs to empty out the radix tree or | |
670 | * the nodes are lost. Don't plant shadows behind its | |
671 | * back. | |
f9fe48be RZ |
672 | * |
673 | * We also don't store shadows for DAX mappings because the | |
674 | * only page cache pages found in these are zero pages | |
675 | * covering holes, and because we don't want to mix DAX | |
676 | * exceptional entries and shadow exceptional entries in the | |
677 | * same page_tree. | |
a528910e JW |
678 | */ |
679 | if (reclaimed && page_is_file_cache(page) && | |
f9fe48be | 680 | !mapping_exiting(mapping) && !dax_mapping(mapping)) |
a528910e | 681 | shadow = workingset_eviction(mapping, page); |
62cccb8c | 682 | __delete_from_page_cache(page, shadow); |
c4843a75 | 683 | spin_unlock_irqrestore(&mapping->tree_lock, flags); |
6072d13c LT |
684 | |
685 | if (freepage != NULL) | |
686 | freepage(page); | |
49d2e9cc CL |
687 | } |
688 | ||
49d2e9cc CL |
689 | return 1; |
690 | ||
691 | cannot_free: | |
c4843a75 | 692 | spin_unlock_irqrestore(&mapping->tree_lock, flags); |
49d2e9cc CL |
693 | return 0; |
694 | } | |
695 | ||
e286781d NP |
696 | /* |
697 | * Attempt to detach a locked page from its ->mapping. If it is dirty or if | |
698 | * someone else has a ref on the page, abort and return 0. If it was | |
699 | * successfully detached, return 1. Assumes the caller has a single ref on | |
700 | * this page. | |
701 | */ | |
702 | int remove_mapping(struct address_space *mapping, struct page *page) | |
703 | { | |
a528910e | 704 | if (__remove_mapping(mapping, page, false)) { |
e286781d NP |
705 | /* |
706 | * Unfreezing the refcount with 1 rather than 2 effectively | |
707 | * drops the pagecache ref for us without requiring another | |
708 | * atomic operation. | |
709 | */ | |
fe896d18 | 710 | page_ref_unfreeze(page, 1); |
e286781d NP |
711 | return 1; |
712 | } | |
713 | return 0; | |
714 | } | |
715 | ||
894bc310 LS |
716 | /** |
717 | * putback_lru_page - put previously isolated page onto appropriate LRU list | |
718 | * @page: page to be put back to appropriate lru list | |
719 | * | |
720 | * Add previously isolated @page to appropriate LRU list. | |
721 | * Page may still be unevictable for other reasons. | |
722 | * | |
723 | * lru_lock must not be held, interrupts must be enabled. | |
724 | */ | |
894bc310 LS |
725 | void putback_lru_page(struct page *page) |
726 | { | |
0ec3b74c | 727 | bool is_unevictable; |
bbfd28ee | 728 | int was_unevictable = PageUnevictable(page); |
894bc310 | 729 | |
309381fe | 730 | VM_BUG_ON_PAGE(PageLRU(page), page); |
894bc310 LS |
731 | |
732 | redo: | |
733 | ClearPageUnevictable(page); | |
734 | ||
39b5f29a | 735 | if (page_evictable(page)) { |
894bc310 LS |
736 | /* |
737 | * For evictable pages, we can use the cache. | |
738 | * In event of a race, worst case is we end up with an | |
739 | * unevictable page on [in]active list. | |
740 | * We know how to handle that. | |
741 | */ | |
0ec3b74c | 742 | is_unevictable = false; |
c53954a0 | 743 | lru_cache_add(page); |
894bc310 LS |
744 | } else { |
745 | /* | |
746 | * Put unevictable pages directly on zone's unevictable | |
747 | * list. | |
748 | */ | |
0ec3b74c | 749 | is_unevictable = true; |
894bc310 | 750 | add_page_to_unevictable_list(page); |
6a7b9548 | 751 | /* |
21ee9f39 MK |
752 | * When racing with an mlock or AS_UNEVICTABLE clearing |
753 | * (page is unlocked) make sure that if the other thread | |
754 | * does not observe our setting of PG_lru and fails | |
24513264 | 755 | * isolation/check_move_unevictable_pages, |
21ee9f39 | 756 | * we see PG_mlocked/AS_UNEVICTABLE cleared below and move |
6a7b9548 JW |
757 | * the page back to the evictable list. |
758 | * | |
21ee9f39 | 759 | * The other side is TestClearPageMlocked() or shmem_lock(). |
6a7b9548 JW |
760 | */ |
761 | smp_mb(); | |
894bc310 | 762 | } |
894bc310 LS |
763 | |
764 | /* | |
765 | * page's status can change while we move it among lru. If an evictable | |
766 | * page is on unevictable list, it never be freed. To avoid that, | |
767 | * check after we added it to the list, again. | |
768 | */ | |
0ec3b74c | 769 | if (is_unevictable && page_evictable(page)) { |
894bc310 LS |
770 | if (!isolate_lru_page(page)) { |
771 | put_page(page); | |
772 | goto redo; | |
773 | } | |
774 | /* This means someone else dropped this page from LRU | |
775 | * So, it will be freed or putback to LRU again. There is | |
776 | * nothing to do here. | |
777 | */ | |
778 | } | |
779 | ||
0ec3b74c | 780 | if (was_unevictable && !is_unevictable) |
bbfd28ee | 781 | count_vm_event(UNEVICTABLE_PGRESCUED); |
0ec3b74c | 782 | else if (!was_unevictable && is_unevictable) |
bbfd28ee LS |
783 | count_vm_event(UNEVICTABLE_PGCULLED); |
784 | ||
894bc310 LS |
785 | put_page(page); /* drop ref from isolate */ |
786 | } | |
787 | ||
dfc8d636 JW |
788 | enum page_references { |
789 | PAGEREF_RECLAIM, | |
790 | PAGEREF_RECLAIM_CLEAN, | |
64574746 | 791 | PAGEREF_KEEP, |
dfc8d636 JW |
792 | PAGEREF_ACTIVATE, |
793 | }; | |
794 | ||
795 | static enum page_references page_check_references(struct page *page, | |
796 | struct scan_control *sc) | |
797 | { | |
64574746 | 798 | int referenced_ptes, referenced_page; |
dfc8d636 | 799 | unsigned long vm_flags; |
dfc8d636 | 800 | |
c3ac9a8a JW |
801 | referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, |
802 | &vm_flags); | |
64574746 | 803 | referenced_page = TestClearPageReferenced(page); |
dfc8d636 | 804 | |
dfc8d636 JW |
805 | /* |
806 | * Mlock lost the isolation race with us. Let try_to_unmap() | |
807 | * move the page to the unevictable list. | |
808 | */ | |
809 | if (vm_flags & VM_LOCKED) | |
810 | return PAGEREF_RECLAIM; | |
811 | ||
64574746 | 812 | if (referenced_ptes) { |
e4898273 | 813 | if (PageSwapBacked(page)) |
64574746 JW |
814 | return PAGEREF_ACTIVATE; |
815 | /* | |
816 | * All mapped pages start out with page table | |
817 | * references from the instantiating fault, so we need | |
818 | * to look twice if a mapped file page is used more | |
819 | * than once. | |
820 | * | |
821 | * Mark it and spare it for another trip around the | |
822 | * inactive list. Another page table reference will | |
823 | * lead to its activation. | |
824 | * | |
825 | * Note: the mark is set for activated pages as well | |
826 | * so that recently deactivated but used pages are | |
827 | * quickly recovered. | |
828 | */ | |
829 | SetPageReferenced(page); | |
830 | ||
34dbc67a | 831 | if (referenced_page || referenced_ptes > 1) |
64574746 JW |
832 | return PAGEREF_ACTIVATE; |
833 | ||
c909e993 KK |
834 | /* |
835 | * Activate file-backed executable pages after first usage. | |
836 | */ | |
837 | if (vm_flags & VM_EXEC) | |
838 | return PAGEREF_ACTIVATE; | |
839 | ||
64574746 JW |
840 | return PAGEREF_KEEP; |
841 | } | |
dfc8d636 JW |
842 | |
843 | /* Reclaim if clean, defer dirty pages to writeback */ | |
2e30244a | 844 | if (referenced_page && !PageSwapBacked(page)) |
64574746 JW |
845 | return PAGEREF_RECLAIM_CLEAN; |
846 | ||
847 | return PAGEREF_RECLAIM; | |
dfc8d636 JW |
848 | } |
849 | ||
e2be15f6 MG |
850 | /* Check if a page is dirty or under writeback */ |
851 | static void page_check_dirty_writeback(struct page *page, | |
852 | bool *dirty, bool *writeback) | |
853 | { | |
b4597226 MG |
854 | struct address_space *mapping; |
855 | ||
e2be15f6 MG |
856 | /* |
857 | * Anonymous pages are not handled by flushers and must be written | |
858 | * from reclaim context. Do not stall reclaim based on them | |
859 | */ | |
860 | if (!page_is_file_cache(page)) { | |
861 | *dirty = false; | |
862 | *writeback = false; | |
863 | return; | |
864 | } | |
865 | ||
866 | /* By default assume that the page flags are accurate */ | |
867 | *dirty = PageDirty(page); | |
868 | *writeback = PageWriteback(page); | |
b4597226 MG |
869 | |
870 | /* Verify dirty/writeback state if the filesystem supports it */ | |
871 | if (!page_has_private(page)) | |
872 | return; | |
873 | ||
874 | mapping = page_mapping(page); | |
875 | if (mapping && mapping->a_ops->is_dirty_writeback) | |
876 | mapping->a_ops->is_dirty_writeback(page, dirty, writeback); | |
e2be15f6 MG |
877 | } |
878 | ||
1da177e4 | 879 | /* |
1742f19f | 880 | * shrink_page_list() returns the number of reclaimed pages |
1da177e4 | 881 | */ |
1742f19f | 882 | static unsigned long shrink_page_list(struct list_head *page_list, |
599d0c95 | 883 | struct pglist_data *pgdat, |
f84f6e2b | 884 | struct scan_control *sc, |
02c6de8d | 885 | enum ttu_flags ttu_flags, |
8e950282 | 886 | unsigned long *ret_nr_dirty, |
d43006d5 | 887 | unsigned long *ret_nr_unqueued_dirty, |
8e950282 | 888 | unsigned long *ret_nr_congested, |
02c6de8d | 889 | unsigned long *ret_nr_writeback, |
b1a6f21e | 890 | unsigned long *ret_nr_immediate, |
02c6de8d | 891 | bool force_reclaim) |
1da177e4 LT |
892 | { |
893 | LIST_HEAD(ret_pages); | |
abe4c3b5 | 894 | LIST_HEAD(free_pages); |
1da177e4 | 895 | int pgactivate = 0; |
d43006d5 | 896 | unsigned long nr_unqueued_dirty = 0; |
0e093d99 MG |
897 | unsigned long nr_dirty = 0; |
898 | unsigned long nr_congested = 0; | |
05ff5137 | 899 | unsigned long nr_reclaimed = 0; |
92df3a72 | 900 | unsigned long nr_writeback = 0; |
b1a6f21e | 901 | unsigned long nr_immediate = 0; |
1da177e4 LT |
902 | |
903 | cond_resched(); | |
904 | ||
1da177e4 LT |
905 | while (!list_empty(page_list)) { |
906 | struct address_space *mapping; | |
907 | struct page *page; | |
908 | int may_enter_fs; | |
02c6de8d | 909 | enum page_references references = PAGEREF_RECLAIM_CLEAN; |
e2be15f6 | 910 | bool dirty, writeback; |
854e9ed0 MK |
911 | bool lazyfree = false; |
912 | int ret = SWAP_SUCCESS; | |
1da177e4 LT |
913 | |
914 | cond_resched(); | |
915 | ||
916 | page = lru_to_page(page_list); | |
917 | list_del(&page->lru); | |
918 | ||
529ae9aa | 919 | if (!trylock_page(page)) |
1da177e4 LT |
920 | goto keep; |
921 | ||
309381fe | 922 | VM_BUG_ON_PAGE(PageActive(page), page); |
1da177e4 LT |
923 | |
924 | sc->nr_scanned++; | |
80e43426 | 925 | |
39b5f29a | 926 | if (unlikely(!page_evictable(page))) |
b291f000 | 927 | goto cull_mlocked; |
894bc310 | 928 | |
a6dc60f8 | 929 | if (!sc->may_unmap && page_mapped(page)) |
80e43426 CL |
930 | goto keep_locked; |
931 | ||
1da177e4 LT |
932 | /* Double the slab pressure for mapped and swapcache pages */ |
933 | if (page_mapped(page) || PageSwapCache(page)) | |
934 | sc->nr_scanned++; | |
935 | ||
c661b078 AW |
936 | may_enter_fs = (sc->gfp_mask & __GFP_FS) || |
937 | (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); | |
938 | ||
e2be15f6 MG |
939 | /* |
940 | * The number of dirty pages determines if a zone is marked | |
941 | * reclaim_congested which affects wait_iff_congested. kswapd | |
942 | * will stall and start writing pages if the tail of the LRU | |
943 | * is all dirty unqueued pages. | |
944 | */ | |
945 | page_check_dirty_writeback(page, &dirty, &writeback); | |
946 | if (dirty || writeback) | |
947 | nr_dirty++; | |
948 | ||
949 | if (dirty && !writeback) | |
950 | nr_unqueued_dirty++; | |
951 | ||
d04e8acd MG |
952 | /* |
953 | * Treat this page as congested if the underlying BDI is or if | |
954 | * pages are cycling through the LRU so quickly that the | |
955 | * pages marked for immediate reclaim are making it to the | |
956 | * end of the LRU a second time. | |
957 | */ | |
e2be15f6 | 958 | mapping = page_mapping(page); |
1da58ee2 | 959 | if (((dirty || writeback) && mapping && |
703c2708 | 960 | inode_write_congested(mapping->host)) || |
d04e8acd | 961 | (writeback && PageReclaim(page))) |
e2be15f6 MG |
962 | nr_congested++; |
963 | ||
283aba9f MG |
964 | /* |
965 | * If a page at the tail of the LRU is under writeback, there | |
966 | * are three cases to consider. | |
967 | * | |
968 | * 1) If reclaim is encountering an excessive number of pages | |
969 | * under writeback and this page is both under writeback and | |
970 | * PageReclaim then it indicates that pages are being queued | |
971 | * for IO but are being recycled through the LRU before the | |
972 | * IO can complete. Waiting on the page itself risks an | |
973 | * indefinite stall if it is impossible to writeback the | |
974 | * page due to IO error or disconnected storage so instead | |
b1a6f21e MG |
975 | * note that the LRU is being scanned too quickly and the |
976 | * caller can stall after page list has been processed. | |
283aba9f | 977 | * |
97c9341f | 978 | * 2) Global or new memcg reclaim encounters a page that is |
ecf5fc6e MH |
979 | * not marked for immediate reclaim, or the caller does not |
980 | * have __GFP_FS (or __GFP_IO if it's simply going to swap, | |
981 | * not to fs). In this case mark the page for immediate | |
97c9341f | 982 | * reclaim and continue scanning. |
283aba9f | 983 | * |
ecf5fc6e MH |
984 | * Require may_enter_fs because we would wait on fs, which |
985 | * may not have submitted IO yet. And the loop driver might | |
283aba9f MG |
986 | * enter reclaim, and deadlock if it waits on a page for |
987 | * which it is needed to do the write (loop masks off | |
988 | * __GFP_IO|__GFP_FS for this reason); but more thought | |
989 | * would probably show more reasons. | |
990 | * | |
7fadc820 | 991 | * 3) Legacy memcg encounters a page that is already marked |
283aba9f MG |
992 | * PageReclaim. memcg does not have any dirty pages |
993 | * throttling so we could easily OOM just because too many | |
994 | * pages are in writeback and there is nothing else to | |
995 | * reclaim. Wait for the writeback to complete. | |
996 | */ | |
c661b078 | 997 | if (PageWriteback(page)) { |
283aba9f MG |
998 | /* Case 1 above */ |
999 | if (current_is_kswapd() && | |
1000 | PageReclaim(page) && | |
599d0c95 | 1001 | test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { |
b1a6f21e MG |
1002 | nr_immediate++; |
1003 | goto keep_locked; | |
283aba9f MG |
1004 | |
1005 | /* Case 2 above */ | |
97c9341f | 1006 | } else if (sane_reclaim(sc) || |
ecf5fc6e | 1007 | !PageReclaim(page) || !may_enter_fs) { |
c3b94f44 HD |
1008 | /* |
1009 | * This is slightly racy - end_page_writeback() | |
1010 | * might have just cleared PageReclaim, then | |
1011 | * setting PageReclaim here end up interpreted | |
1012 | * as PageReadahead - but that does not matter | |
1013 | * enough to care. What we do want is for this | |
1014 | * page to have PageReclaim set next time memcg | |
1015 | * reclaim reaches the tests above, so it will | |
1016 | * then wait_on_page_writeback() to avoid OOM; | |
1017 | * and it's also appropriate in global reclaim. | |
1018 | */ | |
1019 | SetPageReclaim(page); | |
e62e384e | 1020 | nr_writeback++; |
c3b94f44 | 1021 | goto keep_locked; |
283aba9f MG |
1022 | |
1023 | /* Case 3 above */ | |
1024 | } else { | |
7fadc820 | 1025 | unlock_page(page); |
283aba9f | 1026 | wait_on_page_writeback(page); |
7fadc820 HD |
1027 | /* then go back and try same page again */ |
1028 | list_add_tail(&page->lru, page_list); | |
1029 | continue; | |
e62e384e | 1030 | } |
c661b078 | 1031 | } |
1da177e4 | 1032 | |
02c6de8d MK |
1033 | if (!force_reclaim) |
1034 | references = page_check_references(page, sc); | |
1035 | ||
dfc8d636 JW |
1036 | switch (references) { |
1037 | case PAGEREF_ACTIVATE: | |
1da177e4 | 1038 | goto activate_locked; |
64574746 JW |
1039 | case PAGEREF_KEEP: |
1040 | goto keep_locked; | |
dfc8d636 JW |
1041 | case PAGEREF_RECLAIM: |
1042 | case PAGEREF_RECLAIM_CLEAN: | |
1043 | ; /* try to reclaim the page below */ | |
1044 | } | |
1da177e4 | 1045 | |
1da177e4 LT |
1046 | /* |
1047 | * Anonymous process memory has backing store? | |
1048 | * Try to allocate it some swap space here. | |
1049 | */ | |
b291f000 | 1050 | if (PageAnon(page) && !PageSwapCache(page)) { |
63eb6b93 HD |
1051 | if (!(sc->gfp_mask & __GFP_IO)) |
1052 | goto keep_locked; | |
5bc7b8ac | 1053 | if (!add_to_swap(page, page_list)) |
1da177e4 | 1054 | goto activate_locked; |
854e9ed0 | 1055 | lazyfree = true; |
63eb6b93 | 1056 | may_enter_fs = 1; |
1da177e4 | 1057 | |
e2be15f6 MG |
1058 | /* Adding to swap updated mapping */ |
1059 | mapping = page_mapping(page); | |
7751b2da KS |
1060 | } else if (unlikely(PageTransHuge(page))) { |
1061 | /* Split file THP */ | |
1062 | if (split_huge_page_to_list(page, page_list)) | |
1063 | goto keep_locked; | |
e2be15f6 | 1064 | } |
1da177e4 | 1065 | |
7751b2da KS |
1066 | VM_BUG_ON_PAGE(PageTransHuge(page), page); |
1067 | ||
1da177e4 LT |
1068 | /* |
1069 | * The page is mapped into the page tables of one or more | |
1070 | * processes. Try to unmap it here. | |
1071 | */ | |
1072 | if (page_mapped(page) && mapping) { | |
854e9ed0 MK |
1073 | switch (ret = try_to_unmap(page, lazyfree ? |
1074 | (ttu_flags | TTU_BATCH_FLUSH | TTU_LZFREE) : | |
1075 | (ttu_flags | TTU_BATCH_FLUSH))) { | |
1da177e4 LT |
1076 | case SWAP_FAIL: |
1077 | goto activate_locked; | |
1078 | case SWAP_AGAIN: | |
1079 | goto keep_locked; | |
b291f000 NP |
1080 | case SWAP_MLOCK: |
1081 | goto cull_mlocked; | |
854e9ed0 MK |
1082 | case SWAP_LZFREE: |
1083 | goto lazyfree; | |
1da177e4 LT |
1084 | case SWAP_SUCCESS: |
1085 | ; /* try to free the page below */ | |
1086 | } | |
1087 | } | |
1088 | ||
1089 | if (PageDirty(page)) { | |
ee72886d MG |
1090 | /* |
1091 | * Only kswapd can writeback filesystem pages to | |
d43006d5 MG |
1092 | * avoid risk of stack overflow but only writeback |
1093 | * if many dirty pages have been encountered. | |
ee72886d | 1094 | */ |
f84f6e2b | 1095 | if (page_is_file_cache(page) && |
9e3b2f8c | 1096 | (!current_is_kswapd() || |
599d0c95 | 1097 | !test_bit(PGDAT_DIRTY, &pgdat->flags))) { |
49ea7eb6 MG |
1098 | /* |
1099 | * Immediately reclaim when written back. | |
1100 | * Similar in principal to deactivate_page() | |
1101 | * except we already have the page isolated | |
1102 | * and know it's dirty | |
1103 | */ | |
c4a25635 | 1104 | inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); |
49ea7eb6 MG |
1105 | SetPageReclaim(page); |
1106 | ||
ee72886d MG |
1107 | goto keep_locked; |
1108 | } | |
1109 | ||
dfc8d636 | 1110 | if (references == PAGEREF_RECLAIM_CLEAN) |
1da177e4 | 1111 | goto keep_locked; |
4dd4b920 | 1112 | if (!may_enter_fs) |
1da177e4 | 1113 | goto keep_locked; |
52a8363e | 1114 | if (!sc->may_writepage) |
1da177e4 LT |
1115 | goto keep_locked; |
1116 | ||
d950c947 MG |
1117 | /* |
1118 | * Page is dirty. Flush the TLB if a writable entry | |
1119 | * potentially exists to avoid CPU writes after IO | |
1120 | * starts and then write it out here. | |
1121 | */ | |
1122 | try_to_unmap_flush_dirty(); | |
7d3579e8 | 1123 | switch (pageout(page, mapping, sc)) { |
1da177e4 LT |
1124 | case PAGE_KEEP: |
1125 | goto keep_locked; | |
1126 | case PAGE_ACTIVATE: | |
1127 | goto activate_locked; | |
1128 | case PAGE_SUCCESS: | |
7d3579e8 | 1129 | if (PageWriteback(page)) |
41ac1999 | 1130 | goto keep; |
7d3579e8 | 1131 | if (PageDirty(page)) |
1da177e4 | 1132 | goto keep; |
7d3579e8 | 1133 | |
1da177e4 LT |
1134 | /* |
1135 | * A synchronous write - probably a ramdisk. Go | |
1136 | * ahead and try to reclaim the page. | |
1137 | */ | |
529ae9aa | 1138 | if (!trylock_page(page)) |
1da177e4 LT |
1139 | goto keep; |
1140 | if (PageDirty(page) || PageWriteback(page)) | |
1141 | goto keep_locked; | |
1142 | mapping = page_mapping(page); | |
1143 | case PAGE_CLEAN: | |
1144 | ; /* try to free the page below */ | |
1145 | } | |
1146 | } | |
1147 | ||
1148 | /* | |
1149 | * If the page has buffers, try to free the buffer mappings | |
1150 | * associated with this page. If we succeed we try to free | |
1151 | * the page as well. | |
1152 | * | |
1153 | * We do this even if the page is PageDirty(). | |
1154 | * try_to_release_page() does not perform I/O, but it is | |
1155 | * possible for a page to have PageDirty set, but it is actually | |
1156 | * clean (all its buffers are clean). This happens if the | |
1157 | * buffers were written out directly, with submit_bh(). ext3 | |
894bc310 | 1158 | * will do this, as well as the blockdev mapping. |
1da177e4 LT |
1159 | * try_to_release_page() will discover that cleanness and will |
1160 | * drop the buffers and mark the page clean - it can be freed. | |
1161 | * | |
1162 | * Rarely, pages can have buffers and no ->mapping. These are | |
1163 | * the pages which were not successfully invalidated in | |
1164 | * truncate_complete_page(). We try to drop those buffers here | |
1165 | * and if that worked, and the page is no longer mapped into | |
1166 | * process address space (page_count == 1) it can be freed. | |
1167 | * Otherwise, leave the page on the LRU so it is swappable. | |
1168 | */ | |
266cf658 | 1169 | if (page_has_private(page)) { |
1da177e4 LT |
1170 | if (!try_to_release_page(page, sc->gfp_mask)) |
1171 | goto activate_locked; | |
e286781d NP |
1172 | if (!mapping && page_count(page) == 1) { |
1173 | unlock_page(page); | |
1174 | if (put_page_testzero(page)) | |
1175 | goto free_it; | |
1176 | else { | |
1177 | /* | |
1178 | * rare race with speculative reference. | |
1179 | * the speculative reference will free | |
1180 | * this page shortly, so we may | |
1181 | * increment nr_reclaimed here (and | |
1182 | * leave it off the LRU). | |
1183 | */ | |
1184 | nr_reclaimed++; | |
1185 | continue; | |
1186 | } | |
1187 | } | |
1da177e4 LT |
1188 | } |
1189 | ||
854e9ed0 | 1190 | lazyfree: |
a528910e | 1191 | if (!mapping || !__remove_mapping(mapping, page, true)) |
49d2e9cc | 1192 | goto keep_locked; |
1da177e4 | 1193 | |
a978d6f5 NP |
1194 | /* |
1195 | * At this point, we have no other references and there is | |
1196 | * no way to pick any more up (removed from LRU, removed | |
1197 | * from pagecache). Can use non-atomic bitops now (and | |
1198 | * we obviously don't have to worry about waking up a process | |
1199 | * waiting on the page lock, because there are no references. | |
1200 | */ | |
48c935ad | 1201 | __ClearPageLocked(page); |
e286781d | 1202 | free_it: |
854e9ed0 MK |
1203 | if (ret == SWAP_LZFREE) |
1204 | count_vm_event(PGLAZYFREED); | |
1205 | ||
05ff5137 | 1206 | nr_reclaimed++; |
abe4c3b5 MG |
1207 | |
1208 | /* | |
1209 | * Is there need to periodically free_page_list? It would | |
1210 | * appear not as the counts should be low | |
1211 | */ | |
1212 | list_add(&page->lru, &free_pages); | |
1da177e4 LT |
1213 | continue; |
1214 | ||
b291f000 | 1215 | cull_mlocked: |
63d6c5ad HD |
1216 | if (PageSwapCache(page)) |
1217 | try_to_free_swap(page); | |
b291f000 | 1218 | unlock_page(page); |
c54839a7 | 1219 | list_add(&page->lru, &ret_pages); |
b291f000 NP |
1220 | continue; |
1221 | ||
1da177e4 | 1222 | activate_locked: |
68a22394 | 1223 | /* Not a candidate for swapping, so reclaim swap space. */ |
5ccc5aba | 1224 | if (PageSwapCache(page) && mem_cgroup_swap_full(page)) |
a2c43eed | 1225 | try_to_free_swap(page); |
309381fe | 1226 | VM_BUG_ON_PAGE(PageActive(page), page); |
1da177e4 LT |
1227 | SetPageActive(page); |
1228 | pgactivate++; | |
1229 | keep_locked: | |
1230 | unlock_page(page); | |
1231 | keep: | |
1232 | list_add(&page->lru, &ret_pages); | |
309381fe | 1233 | VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); |
1da177e4 | 1234 | } |
abe4c3b5 | 1235 | |
747db954 | 1236 | mem_cgroup_uncharge_list(&free_pages); |
72b252ae | 1237 | try_to_unmap_flush(); |
b745bc85 | 1238 | free_hot_cold_page_list(&free_pages, true); |
abe4c3b5 | 1239 | |
1da177e4 | 1240 | list_splice(&ret_pages, page_list); |
f8891e5e | 1241 | count_vm_events(PGACTIVATE, pgactivate); |
0a31bc97 | 1242 | |
8e950282 MG |
1243 | *ret_nr_dirty += nr_dirty; |
1244 | *ret_nr_congested += nr_congested; | |
d43006d5 | 1245 | *ret_nr_unqueued_dirty += nr_unqueued_dirty; |
92df3a72 | 1246 | *ret_nr_writeback += nr_writeback; |
b1a6f21e | 1247 | *ret_nr_immediate += nr_immediate; |
05ff5137 | 1248 | return nr_reclaimed; |
1da177e4 LT |
1249 | } |
1250 | ||
02c6de8d MK |
1251 | unsigned long reclaim_clean_pages_from_list(struct zone *zone, |
1252 | struct list_head *page_list) | |
1253 | { | |
1254 | struct scan_control sc = { | |
1255 | .gfp_mask = GFP_KERNEL, | |
1256 | .priority = DEF_PRIORITY, | |
1257 | .may_unmap = 1, | |
1258 | }; | |
8e950282 | 1259 | unsigned long ret, dummy1, dummy2, dummy3, dummy4, dummy5; |
02c6de8d MK |
1260 | struct page *page, *next; |
1261 | LIST_HEAD(clean_pages); | |
1262 | ||
1263 | list_for_each_entry_safe(page, next, page_list, lru) { | |
117aad1e | 1264 | if (page_is_file_cache(page) && !PageDirty(page) && |
b1123ea6 | 1265 | !__PageMovable(page)) { |
02c6de8d MK |
1266 | ClearPageActive(page); |
1267 | list_move(&page->lru, &clean_pages); | |
1268 | } | |
1269 | } | |
1270 | ||
599d0c95 | 1271 | ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, |
8e950282 MG |
1272 | TTU_UNMAP|TTU_IGNORE_ACCESS, |
1273 | &dummy1, &dummy2, &dummy3, &dummy4, &dummy5, true); | |
02c6de8d | 1274 | list_splice(&clean_pages, page_list); |
599d0c95 | 1275 | mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret); |
02c6de8d MK |
1276 | return ret; |
1277 | } | |
1278 | ||
5ad333eb AW |
1279 | /* |
1280 | * Attempt to remove the specified page from its LRU. Only take this page | |
1281 | * if it is of the appropriate PageActive status. Pages which are being | |
1282 | * freed elsewhere are also ignored. | |
1283 | * | |
1284 | * page: page to consider | |
1285 | * mode: one of the LRU isolation modes defined above | |
1286 | * | |
1287 | * returns 0 on success, -ve errno on failure. | |
1288 | */ | |
f3fd4a61 | 1289 | int __isolate_lru_page(struct page *page, isolate_mode_t mode) |
5ad333eb AW |
1290 | { |
1291 | int ret = -EINVAL; | |
1292 | ||
1293 | /* Only take pages on the LRU. */ | |
1294 | if (!PageLRU(page)) | |
1295 | return ret; | |
1296 | ||
e46a2879 MK |
1297 | /* Compaction should not handle unevictable pages but CMA can do so */ |
1298 | if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) | |
894bc310 LS |
1299 | return ret; |
1300 | ||
5ad333eb | 1301 | ret = -EBUSY; |
08e552c6 | 1302 | |
c8244935 MG |
1303 | /* |
1304 | * To minimise LRU disruption, the caller can indicate that it only | |
1305 | * wants to isolate pages it will be able to operate on without | |
1306 | * blocking - clean pages for the most part. | |
1307 | * | |
1308 | * ISOLATE_CLEAN means that only clean pages should be isolated. This | |
1309 | * is used by reclaim when it is cannot write to backing storage | |
1310 | * | |
1311 | * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages | |
1312 | * that it is possible to migrate without blocking | |
1313 | */ | |
1314 | if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) { | |
1315 | /* All the caller can do on PageWriteback is block */ | |
1316 | if (PageWriteback(page)) | |
1317 | return ret; | |
1318 | ||
1319 | if (PageDirty(page)) { | |
1320 | struct address_space *mapping; | |
1321 | ||
1322 | /* ISOLATE_CLEAN means only clean pages */ | |
1323 | if (mode & ISOLATE_CLEAN) | |
1324 | return ret; | |
1325 | ||
1326 | /* | |
1327 | * Only pages without mappings or that have a | |
1328 | * ->migratepage callback are possible to migrate | |
1329 | * without blocking | |
1330 | */ | |
1331 | mapping = page_mapping(page); | |
1332 | if (mapping && !mapping->a_ops->migratepage) | |
1333 | return ret; | |
1334 | } | |
1335 | } | |
39deaf85 | 1336 | |
f80c0673 MK |
1337 | if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) |
1338 | return ret; | |
1339 | ||
5ad333eb AW |
1340 | if (likely(get_page_unless_zero(page))) { |
1341 | /* | |
1342 | * Be careful not to clear PageLRU until after we're | |
1343 | * sure the page is not being freed elsewhere -- the | |
1344 | * page release code relies on it. | |
1345 | */ | |
1346 | ClearPageLRU(page); | |
1347 | ret = 0; | |
1348 | } | |
1349 | ||
1350 | return ret; | |
1351 | } | |
1352 | ||
7ee36a14 MG |
1353 | |
1354 | /* | |
1355 | * Update LRU sizes after isolating pages. The LRU size updates must | |
1356 | * be complete before mem_cgroup_update_lru_size due to a santity check. | |
1357 | */ | |
1358 | static __always_inline void update_lru_sizes(struct lruvec *lruvec, | |
1359 | enum lru_list lru, unsigned long *nr_zone_taken, | |
1360 | unsigned long nr_taken) | |
1361 | { | |
7ee36a14 MG |
1362 | int zid; |
1363 | ||
7ee36a14 MG |
1364 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
1365 | if (!nr_zone_taken[zid]) | |
1366 | continue; | |
1367 | ||
1368 | __update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); | |
1369 | } | |
7ee36a14 MG |
1370 | |
1371 | #ifdef CONFIG_MEMCG | |
1372 | mem_cgroup_update_lru_size(lruvec, lru, -nr_taken); | |
1373 | #endif | |
1374 | } | |
1375 | ||
1da177e4 | 1376 | /* |
a52633d8 | 1377 | * zone_lru_lock is heavily contended. Some of the functions that |
1da177e4 LT |
1378 | * shrink the lists perform better by taking out a batch of pages |
1379 | * and working on them outside the LRU lock. | |
1380 | * | |
1381 | * For pagecache intensive workloads, this function is the hottest | |
1382 | * spot in the kernel (apart from copy_*_user functions). | |
1383 | * | |
1384 | * Appropriate locks must be held before calling this function. | |
1385 | * | |
1386 | * @nr_to_scan: The number of pages to look through on the list. | |
5dc35979 | 1387 | * @lruvec: The LRU vector to pull pages from. |
1da177e4 | 1388 | * @dst: The temp list to put pages on to. |
f626012d | 1389 | * @nr_scanned: The number of pages that were scanned. |
fe2c2a10 | 1390 | * @sc: The scan_control struct for this reclaim session |
5ad333eb | 1391 | * @mode: One of the LRU isolation modes |
3cb99451 | 1392 | * @lru: LRU list id for isolating |
1da177e4 LT |
1393 | * |
1394 | * returns how many pages were moved onto *@dst. | |
1395 | */ | |
69e05944 | 1396 | static unsigned long isolate_lru_pages(unsigned long nr_to_scan, |
5dc35979 | 1397 | struct lruvec *lruvec, struct list_head *dst, |
fe2c2a10 | 1398 | unsigned long *nr_scanned, struct scan_control *sc, |
3cb99451 | 1399 | isolate_mode_t mode, enum lru_list lru) |
1da177e4 | 1400 | { |
75b00af7 | 1401 | struct list_head *src = &lruvec->lists[lru]; |
69e05944 | 1402 | unsigned long nr_taken = 0; |
599d0c95 | 1403 | unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; |
7cc30fcf | 1404 | unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; |
599d0c95 | 1405 | unsigned long scan, nr_pages; |
b2e18757 | 1406 | LIST_HEAD(pages_skipped); |
1da177e4 | 1407 | |
0b802f10 VD |
1408 | for (scan = 0; scan < nr_to_scan && nr_taken < nr_to_scan && |
1409 | !list_empty(src); scan++) { | |
5ad333eb | 1410 | struct page *page; |
5ad333eb | 1411 | |
1da177e4 LT |
1412 | page = lru_to_page(src); |
1413 | prefetchw_prev_lru_page(page, src, flags); | |
1414 | ||
309381fe | 1415 | VM_BUG_ON_PAGE(!PageLRU(page), page); |
8d438f96 | 1416 | |
b2e18757 MG |
1417 | if (page_zonenum(page) > sc->reclaim_idx) { |
1418 | list_move(&page->lru, &pages_skipped); | |
7cc30fcf | 1419 | nr_skipped[page_zonenum(page)]++; |
b2e18757 MG |
1420 | continue; |
1421 | } | |
1422 | ||
f3fd4a61 | 1423 | switch (__isolate_lru_page(page, mode)) { |
5ad333eb | 1424 | case 0: |
599d0c95 MG |
1425 | nr_pages = hpage_nr_pages(page); |
1426 | nr_taken += nr_pages; | |
1427 | nr_zone_taken[page_zonenum(page)] += nr_pages; | |
5ad333eb | 1428 | list_move(&page->lru, dst); |
5ad333eb AW |
1429 | break; |
1430 | ||
1431 | case -EBUSY: | |
1432 | /* else it is being freed elsewhere */ | |
1433 | list_move(&page->lru, src); | |
1434 | continue; | |
46453a6e | 1435 | |
5ad333eb AW |
1436 | default: |
1437 | BUG(); | |
1438 | } | |
1da177e4 LT |
1439 | } |
1440 | ||
b2e18757 MG |
1441 | /* |
1442 | * Splice any skipped pages to the start of the LRU list. Note that | |
1443 | * this disrupts the LRU order when reclaiming for lower zones but | |
1444 | * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX | |
1445 | * scanning would soon rescan the same pages to skip and put the | |
1446 | * system at risk of premature OOM. | |
1447 | */ | |
7cc30fcf MG |
1448 | if (!list_empty(&pages_skipped)) { |
1449 | int zid; | |
1450 | ||
b2e18757 | 1451 | list_splice(&pages_skipped, src); |
7cc30fcf MG |
1452 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
1453 | if (!nr_skipped[zid]) | |
1454 | continue; | |
1455 | ||
1456 | __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); | |
1457 | } | |
1458 | } | |
f626012d | 1459 | *nr_scanned = scan; |
e5146b12 | 1460 | trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, scan, |
75b00af7 | 1461 | nr_taken, mode, is_file_lru(lru)); |
7ee36a14 | 1462 | update_lru_sizes(lruvec, lru, nr_zone_taken, nr_taken); |
1da177e4 LT |
1463 | return nr_taken; |
1464 | } | |
1465 | ||
62695a84 NP |
1466 | /** |
1467 | * isolate_lru_page - tries to isolate a page from its LRU list | |
1468 | * @page: page to isolate from its LRU list | |
1469 | * | |
1470 | * Isolates a @page from an LRU list, clears PageLRU and adjusts the | |
1471 | * vmstat statistic corresponding to whatever LRU list the page was on. | |
1472 | * | |
1473 | * Returns 0 if the page was removed from an LRU list. | |
1474 | * Returns -EBUSY if the page was not on an LRU list. | |
1475 | * | |
1476 | * The returned page will have PageLRU() cleared. If it was found on | |
894bc310 LS |
1477 | * the active list, it will have PageActive set. If it was found on |
1478 | * the unevictable list, it will have the PageUnevictable bit set. That flag | |
1479 | * may need to be cleared by the caller before letting the page go. | |
62695a84 NP |
1480 | * |
1481 | * The vmstat statistic corresponding to the list on which the page was | |
1482 | * found will be decremented. | |
1483 | * | |
1484 | * Restrictions: | |
1485 | * (1) Must be called with an elevated refcount on the page. This is a | |
1486 | * fundamentnal difference from isolate_lru_pages (which is called | |
1487 | * without a stable reference). | |
1488 | * (2) the lru_lock must not be held. | |
1489 | * (3) interrupts must be enabled. | |
1490 | */ | |
1491 | int isolate_lru_page(struct page *page) | |
1492 | { | |
1493 | int ret = -EBUSY; | |
1494 | ||
309381fe | 1495 | VM_BUG_ON_PAGE(!page_count(page), page); |
cf2a82ee | 1496 | WARN_RATELIMIT(PageTail(page), "trying to isolate tail page"); |
0c917313 | 1497 | |
62695a84 NP |
1498 | if (PageLRU(page)) { |
1499 | struct zone *zone = page_zone(page); | |
fa9add64 | 1500 | struct lruvec *lruvec; |
62695a84 | 1501 | |
a52633d8 | 1502 | spin_lock_irq(zone_lru_lock(zone)); |
599d0c95 | 1503 | lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat); |
0c917313 | 1504 | if (PageLRU(page)) { |
894bc310 | 1505 | int lru = page_lru(page); |
0c917313 | 1506 | get_page(page); |
62695a84 | 1507 | ClearPageLRU(page); |
fa9add64 HD |
1508 | del_page_from_lru_list(page, lruvec, lru); |
1509 | ret = 0; | |
62695a84 | 1510 | } |
a52633d8 | 1511 | spin_unlock_irq(zone_lru_lock(zone)); |
62695a84 NP |
1512 | } |
1513 | return ret; | |
1514 | } | |
1515 | ||
35cd7815 | 1516 | /* |
d37dd5dc FW |
1517 | * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and |
1518 | * then get resheduled. When there are massive number of tasks doing page | |
1519 | * allocation, such sleeping direct reclaimers may keep piling up on each CPU, | |
1520 | * the LRU list will go small and be scanned faster than necessary, leading to | |
1521 | * unnecessary swapping, thrashing and OOM. | |
35cd7815 | 1522 | */ |
599d0c95 | 1523 | static int too_many_isolated(struct pglist_data *pgdat, int file, |
35cd7815 RR |
1524 | struct scan_control *sc) |
1525 | { | |
1526 | unsigned long inactive, isolated; | |
1527 | ||
1528 | if (current_is_kswapd()) | |
1529 | return 0; | |
1530 | ||
97c9341f | 1531 | if (!sane_reclaim(sc)) |
35cd7815 RR |
1532 | return 0; |
1533 | ||
1534 | if (file) { | |
599d0c95 MG |
1535 | inactive = node_page_state(pgdat, NR_INACTIVE_FILE); |
1536 | isolated = node_page_state(pgdat, NR_ISOLATED_FILE); | |
35cd7815 | 1537 | } else { |
599d0c95 MG |
1538 | inactive = node_page_state(pgdat, NR_INACTIVE_ANON); |
1539 | isolated = node_page_state(pgdat, NR_ISOLATED_ANON); | |
35cd7815 RR |
1540 | } |
1541 | ||
3cf23841 FW |
1542 | /* |
1543 | * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they | |
1544 | * won't get blocked by normal direct-reclaimers, forming a circular | |
1545 | * deadlock. | |
1546 | */ | |
d0164adc | 1547 | if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) |
3cf23841 FW |
1548 | inactive >>= 3; |
1549 | ||
35cd7815 RR |
1550 | return isolated > inactive; |
1551 | } | |
1552 | ||
66635629 | 1553 | static noinline_for_stack void |
75b00af7 | 1554 | putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) |
66635629 | 1555 | { |
27ac81d8 | 1556 | struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; |
599d0c95 | 1557 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
3f79768f | 1558 | LIST_HEAD(pages_to_free); |
66635629 | 1559 | |
66635629 MG |
1560 | /* |
1561 | * Put back any unfreeable pages. | |
1562 | */ | |
66635629 | 1563 | while (!list_empty(page_list)) { |
3f79768f | 1564 | struct page *page = lru_to_page(page_list); |
66635629 | 1565 | int lru; |
3f79768f | 1566 | |
309381fe | 1567 | VM_BUG_ON_PAGE(PageLRU(page), page); |
66635629 | 1568 | list_del(&page->lru); |
39b5f29a | 1569 | if (unlikely(!page_evictable(page))) { |
599d0c95 | 1570 | spin_unlock_irq(&pgdat->lru_lock); |
66635629 | 1571 | putback_lru_page(page); |
599d0c95 | 1572 | spin_lock_irq(&pgdat->lru_lock); |
66635629 MG |
1573 | continue; |
1574 | } | |
fa9add64 | 1575 | |
599d0c95 | 1576 | lruvec = mem_cgroup_page_lruvec(page, pgdat); |
fa9add64 | 1577 | |
7a608572 | 1578 | SetPageLRU(page); |
66635629 | 1579 | lru = page_lru(page); |
fa9add64 HD |
1580 | add_page_to_lru_list(page, lruvec, lru); |
1581 | ||
66635629 MG |
1582 | if (is_active_lru(lru)) { |
1583 | int file = is_file_lru(lru); | |
9992af10 RR |
1584 | int numpages = hpage_nr_pages(page); |
1585 | reclaim_stat->recent_rotated[file] += numpages; | |
66635629 | 1586 | } |
2bcf8879 HD |
1587 | if (put_page_testzero(page)) { |
1588 | __ClearPageLRU(page); | |
1589 | __ClearPageActive(page); | |
fa9add64 | 1590 | del_page_from_lru_list(page, lruvec, lru); |
2bcf8879 HD |
1591 | |
1592 | if (unlikely(PageCompound(page))) { | |
599d0c95 | 1593 | spin_unlock_irq(&pgdat->lru_lock); |
747db954 | 1594 | mem_cgroup_uncharge(page); |
2bcf8879 | 1595 | (*get_compound_page_dtor(page))(page); |
599d0c95 | 1596 | spin_lock_irq(&pgdat->lru_lock); |
2bcf8879 HD |
1597 | } else |
1598 | list_add(&page->lru, &pages_to_free); | |
66635629 MG |
1599 | } |
1600 | } | |
66635629 | 1601 | |
3f79768f HD |
1602 | /* |
1603 | * To save our caller's stack, now use input list for pages to free. | |
1604 | */ | |
1605 | list_splice(&pages_to_free, page_list); | |
66635629 MG |
1606 | } |
1607 | ||
399ba0b9 N |
1608 | /* |
1609 | * If a kernel thread (such as nfsd for loop-back mounts) services | |
1610 | * a backing device by writing to the page cache it sets PF_LESS_THROTTLE. | |
1611 | * In that case we should only throttle if the backing device it is | |
1612 | * writing to is congested. In other cases it is safe to throttle. | |
1613 | */ | |
1614 | static int current_may_throttle(void) | |
1615 | { | |
1616 | return !(current->flags & PF_LESS_THROTTLE) || | |
1617 | current->backing_dev_info == NULL || | |
1618 | bdi_write_congested(current->backing_dev_info); | |
1619 | } | |
1620 | ||
1da177e4 | 1621 | /* |
b2e18757 | 1622 | * shrink_inactive_list() is a helper for shrink_node(). It returns the number |
1742f19f | 1623 | * of reclaimed pages |
1da177e4 | 1624 | */ |
66635629 | 1625 | static noinline_for_stack unsigned long |
1a93be0e | 1626 | shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, |
9e3b2f8c | 1627 | struct scan_control *sc, enum lru_list lru) |
1da177e4 LT |
1628 | { |
1629 | LIST_HEAD(page_list); | |
e247dbce | 1630 | unsigned long nr_scanned; |
05ff5137 | 1631 | unsigned long nr_reclaimed = 0; |
e247dbce | 1632 | unsigned long nr_taken; |
8e950282 MG |
1633 | unsigned long nr_dirty = 0; |
1634 | unsigned long nr_congested = 0; | |
e2be15f6 | 1635 | unsigned long nr_unqueued_dirty = 0; |
92df3a72 | 1636 | unsigned long nr_writeback = 0; |
b1a6f21e | 1637 | unsigned long nr_immediate = 0; |
f3fd4a61 | 1638 | isolate_mode_t isolate_mode = 0; |
3cb99451 | 1639 | int file = is_file_lru(lru); |
599d0c95 | 1640 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
1a93be0e | 1641 | struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; |
78dc583d | 1642 | |
599d0c95 | 1643 | while (unlikely(too_many_isolated(pgdat, file, sc))) { |
58355c78 | 1644 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
35cd7815 RR |
1645 | |
1646 | /* We are about to die and free our memory. Return now. */ | |
1647 | if (fatal_signal_pending(current)) | |
1648 | return SWAP_CLUSTER_MAX; | |
1649 | } | |
1650 | ||
1da177e4 | 1651 | lru_add_drain(); |
f80c0673 MK |
1652 | |
1653 | if (!sc->may_unmap) | |
61317289 | 1654 | isolate_mode |= ISOLATE_UNMAPPED; |
f80c0673 | 1655 | if (!sc->may_writepage) |
61317289 | 1656 | isolate_mode |= ISOLATE_CLEAN; |
f80c0673 | 1657 | |
599d0c95 | 1658 | spin_lock_irq(&pgdat->lru_lock); |
b35ea17b | 1659 | |
5dc35979 KK |
1660 | nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, |
1661 | &nr_scanned, sc, isolate_mode, lru); | |
95d918fc | 1662 | |
599d0c95 | 1663 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); |
9d5e6a9f | 1664 | reclaim_stat->recent_scanned[file] += nr_taken; |
95d918fc | 1665 | |
89b5fae5 | 1666 | if (global_reclaim(sc)) { |
599d0c95 | 1667 | __mod_node_page_state(pgdat, NR_PAGES_SCANNED, nr_scanned); |
e247dbce | 1668 | if (current_is_kswapd()) |
599d0c95 | 1669 | __count_vm_events(PGSCAN_KSWAPD, nr_scanned); |
e247dbce | 1670 | else |
599d0c95 | 1671 | __count_vm_events(PGSCAN_DIRECT, nr_scanned); |
e247dbce | 1672 | } |
599d0c95 | 1673 | spin_unlock_irq(&pgdat->lru_lock); |
b35ea17b | 1674 | |
d563c050 | 1675 | if (nr_taken == 0) |
66635629 | 1676 | return 0; |
5ad333eb | 1677 | |
599d0c95 | 1678 | nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, TTU_UNMAP, |
8e950282 MG |
1679 | &nr_dirty, &nr_unqueued_dirty, &nr_congested, |
1680 | &nr_writeback, &nr_immediate, | |
1681 | false); | |
c661b078 | 1682 | |
599d0c95 | 1683 | spin_lock_irq(&pgdat->lru_lock); |
3f79768f | 1684 | |
904249aa YH |
1685 | if (global_reclaim(sc)) { |
1686 | if (current_is_kswapd()) | |
599d0c95 | 1687 | __count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed); |
904249aa | 1688 | else |
599d0c95 | 1689 | __count_vm_events(PGSTEAL_DIRECT, nr_reclaimed); |
904249aa | 1690 | } |
a74609fa | 1691 | |
27ac81d8 | 1692 | putback_inactive_pages(lruvec, &page_list); |
3f79768f | 1693 | |
599d0c95 | 1694 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); |
3f79768f | 1695 | |
599d0c95 | 1696 | spin_unlock_irq(&pgdat->lru_lock); |
3f79768f | 1697 | |
747db954 | 1698 | mem_cgroup_uncharge_list(&page_list); |
b745bc85 | 1699 | free_hot_cold_page_list(&page_list, true); |
e11da5b4 | 1700 | |
92df3a72 MG |
1701 | /* |
1702 | * If reclaim is isolating dirty pages under writeback, it implies | |
1703 | * that the long-lived page allocation rate is exceeding the page | |
1704 | * laundering rate. Either the global limits are not being effective | |
1705 | * at throttling processes due to the page distribution throughout | |
1706 | * zones or there is heavy usage of a slow backing device. The | |
1707 | * only option is to throttle from reclaim context which is not ideal | |
1708 | * as there is no guarantee the dirtying process is throttled in the | |
1709 | * same way balance_dirty_pages() manages. | |
1710 | * | |
8e950282 MG |
1711 | * Once a zone is flagged ZONE_WRITEBACK, kswapd will count the number |
1712 | * of pages under pages flagged for immediate reclaim and stall if any | |
1713 | * are encountered in the nr_immediate check below. | |
92df3a72 | 1714 | */ |
918fc718 | 1715 | if (nr_writeback && nr_writeback == nr_taken) |
599d0c95 | 1716 | set_bit(PGDAT_WRITEBACK, &pgdat->flags); |
92df3a72 | 1717 | |
d43006d5 | 1718 | /* |
97c9341f TH |
1719 | * Legacy memcg will stall in page writeback so avoid forcibly |
1720 | * stalling here. | |
d43006d5 | 1721 | */ |
97c9341f | 1722 | if (sane_reclaim(sc)) { |
8e950282 MG |
1723 | /* |
1724 | * Tag a zone as congested if all the dirty pages scanned were | |
1725 | * backed by a congested BDI and wait_iff_congested will stall. | |
1726 | */ | |
1727 | if (nr_dirty && nr_dirty == nr_congested) | |
599d0c95 | 1728 | set_bit(PGDAT_CONGESTED, &pgdat->flags); |
8e950282 | 1729 | |
b1a6f21e MG |
1730 | /* |
1731 | * If dirty pages are scanned that are not queued for IO, it | |
1732 | * implies that flushers are not keeping up. In this case, flag | |
599d0c95 | 1733 | * the pgdat PGDAT_DIRTY and kswapd will start writing pages from |
57054651 | 1734 | * reclaim context. |
b1a6f21e MG |
1735 | */ |
1736 | if (nr_unqueued_dirty == nr_taken) | |
599d0c95 | 1737 | set_bit(PGDAT_DIRTY, &pgdat->flags); |
b1a6f21e MG |
1738 | |
1739 | /* | |
b738d764 LT |
1740 | * If kswapd scans pages marked marked for immediate |
1741 | * reclaim and under writeback (nr_immediate), it implies | |
1742 | * that pages are cycling through the LRU faster than | |
b1a6f21e MG |
1743 | * they are written so also forcibly stall. |
1744 | */ | |
b738d764 | 1745 | if (nr_immediate && current_may_throttle()) |
b1a6f21e | 1746 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
e2be15f6 | 1747 | } |
d43006d5 | 1748 | |
8e950282 MG |
1749 | /* |
1750 | * Stall direct reclaim for IO completions if underlying BDIs or zone | |
1751 | * is congested. Allow kswapd to continue until it starts encountering | |
1752 | * unqueued dirty pages or cycling through the LRU too quickly. | |
1753 | */ | |
399ba0b9 N |
1754 | if (!sc->hibernation_mode && !current_is_kswapd() && |
1755 | current_may_throttle()) | |
599d0c95 | 1756 | wait_iff_congested(pgdat, BLK_RW_ASYNC, HZ/10); |
8e950282 | 1757 | |
599d0c95 MG |
1758 | trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, |
1759 | nr_scanned, nr_reclaimed, | |
ba5e9579 | 1760 | sc->priority, file); |
05ff5137 | 1761 | return nr_reclaimed; |
1da177e4 LT |
1762 | } |
1763 | ||
1764 | /* | |
1765 | * This moves pages from the active list to the inactive list. | |
1766 | * | |
1767 | * We move them the other way if the page is referenced by one or more | |
1768 | * processes, from rmap. | |
1769 | * | |
1770 | * If the pages are mostly unmapped, the processing is fast and it is | |
a52633d8 | 1771 | * appropriate to hold zone_lru_lock across the whole operation. But if |
1da177e4 | 1772 | * the pages are mapped, the processing is slow (page_referenced()) so we |
a52633d8 | 1773 | * should drop zone_lru_lock around each page. It's impossible to balance |
1da177e4 LT |
1774 | * this, so instead we remove the pages from the LRU while processing them. |
1775 | * It is safe to rely on PG_active against the non-LRU pages in here because | |
1776 | * nobody will play with that bit on a non-LRU page. | |
1777 | * | |
0139aa7b | 1778 | * The downside is that we have to touch page->_refcount against each page. |
1da177e4 LT |
1779 | * But we had to alter page->flags anyway. |
1780 | */ | |
1cfb419b | 1781 | |
fa9add64 | 1782 | static void move_active_pages_to_lru(struct lruvec *lruvec, |
3eb4140f | 1783 | struct list_head *list, |
2bcf8879 | 1784 | struct list_head *pages_to_free, |
3eb4140f WF |
1785 | enum lru_list lru) |
1786 | { | |
599d0c95 | 1787 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
3eb4140f | 1788 | unsigned long pgmoved = 0; |
3eb4140f | 1789 | struct page *page; |
fa9add64 | 1790 | int nr_pages; |
3eb4140f | 1791 | |
3eb4140f WF |
1792 | while (!list_empty(list)) { |
1793 | page = lru_to_page(list); | |
599d0c95 | 1794 | lruvec = mem_cgroup_page_lruvec(page, pgdat); |
3eb4140f | 1795 | |
309381fe | 1796 | VM_BUG_ON_PAGE(PageLRU(page), page); |
3eb4140f WF |
1797 | SetPageLRU(page); |
1798 | ||
fa9add64 | 1799 | nr_pages = hpage_nr_pages(page); |
599d0c95 | 1800 | update_lru_size(lruvec, lru, page_zonenum(page), nr_pages); |
925b7673 | 1801 | list_move(&page->lru, &lruvec->lists[lru]); |
fa9add64 | 1802 | pgmoved += nr_pages; |
3eb4140f | 1803 | |
2bcf8879 HD |
1804 | if (put_page_testzero(page)) { |
1805 | __ClearPageLRU(page); | |
1806 | __ClearPageActive(page); | |
fa9add64 | 1807 | del_page_from_lru_list(page, lruvec, lru); |
2bcf8879 HD |
1808 | |
1809 | if (unlikely(PageCompound(page))) { | |
599d0c95 | 1810 | spin_unlock_irq(&pgdat->lru_lock); |
747db954 | 1811 | mem_cgroup_uncharge(page); |
2bcf8879 | 1812 | (*get_compound_page_dtor(page))(page); |
599d0c95 | 1813 | spin_lock_irq(&pgdat->lru_lock); |
2bcf8879 HD |
1814 | } else |
1815 | list_add(&page->lru, pages_to_free); | |
3eb4140f WF |
1816 | } |
1817 | } | |
9d5e6a9f | 1818 | |
3eb4140f WF |
1819 | if (!is_active_lru(lru)) |
1820 | __count_vm_events(PGDEACTIVATE, pgmoved); | |
1821 | } | |
1cfb419b | 1822 | |
f626012d | 1823 | static void shrink_active_list(unsigned long nr_to_scan, |
1a93be0e | 1824 | struct lruvec *lruvec, |
f16015fb | 1825 | struct scan_control *sc, |
9e3b2f8c | 1826 | enum lru_list lru) |
1da177e4 | 1827 | { |
44c241f1 | 1828 | unsigned long nr_taken; |
f626012d | 1829 | unsigned long nr_scanned; |
6fe6b7e3 | 1830 | unsigned long vm_flags; |
1da177e4 | 1831 | LIST_HEAD(l_hold); /* The pages which were snipped off */ |
8cab4754 | 1832 | LIST_HEAD(l_active); |
b69408e8 | 1833 | LIST_HEAD(l_inactive); |
1da177e4 | 1834 | struct page *page; |
1a93be0e | 1835 | struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; |
44c241f1 | 1836 | unsigned long nr_rotated = 0; |
f3fd4a61 | 1837 | isolate_mode_t isolate_mode = 0; |
3cb99451 | 1838 | int file = is_file_lru(lru); |
599d0c95 | 1839 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
1da177e4 LT |
1840 | |
1841 | lru_add_drain(); | |
f80c0673 MK |
1842 | |
1843 | if (!sc->may_unmap) | |
61317289 | 1844 | isolate_mode |= ISOLATE_UNMAPPED; |
f80c0673 | 1845 | if (!sc->may_writepage) |
61317289 | 1846 | isolate_mode |= ISOLATE_CLEAN; |
f80c0673 | 1847 | |
599d0c95 | 1848 | spin_lock_irq(&pgdat->lru_lock); |
925b7673 | 1849 | |
5dc35979 KK |
1850 | nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, |
1851 | &nr_scanned, sc, isolate_mode, lru); | |
89b5fae5 | 1852 | |
599d0c95 | 1853 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); |
b7c46d15 | 1854 | reclaim_stat->recent_scanned[file] += nr_taken; |
1cfb419b | 1855 | |
9d5e6a9f | 1856 | if (global_reclaim(sc)) |
599d0c95 MG |
1857 | __mod_node_page_state(pgdat, NR_PAGES_SCANNED, nr_scanned); |
1858 | __count_vm_events(PGREFILL, nr_scanned); | |
9d5e6a9f | 1859 | |
599d0c95 | 1860 | spin_unlock_irq(&pgdat->lru_lock); |
1da177e4 | 1861 | |
1da177e4 LT |
1862 | while (!list_empty(&l_hold)) { |
1863 | cond_resched(); | |
1864 | page = lru_to_page(&l_hold); | |
1865 | list_del(&page->lru); | |
7e9cd484 | 1866 | |
39b5f29a | 1867 | if (unlikely(!page_evictable(page))) { |
894bc310 LS |
1868 | putback_lru_page(page); |
1869 | continue; | |
1870 | } | |
1871 | ||
cc715d99 MG |
1872 | if (unlikely(buffer_heads_over_limit)) { |
1873 | if (page_has_private(page) && trylock_page(page)) { | |
1874 | if (page_has_private(page)) | |
1875 | try_to_release_page(page, 0); | |
1876 | unlock_page(page); | |
1877 | } | |
1878 | } | |
1879 | ||
c3ac9a8a JW |
1880 | if (page_referenced(page, 0, sc->target_mem_cgroup, |
1881 | &vm_flags)) { | |
9992af10 | 1882 | nr_rotated += hpage_nr_pages(page); |
8cab4754 WF |
1883 | /* |
1884 | * Identify referenced, file-backed active pages and | |
1885 | * give them one more trip around the active list. So | |
1886 | * that executable code get better chances to stay in | |
1887 | * memory under moderate memory pressure. Anon pages | |
1888 | * are not likely to be evicted by use-once streaming | |
1889 | * IO, plus JVM can create lots of anon VM_EXEC pages, | |
1890 | * so we ignore them here. | |
1891 | */ | |
41e20983 | 1892 | if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { |
8cab4754 WF |
1893 | list_add(&page->lru, &l_active); |
1894 | continue; | |
1895 | } | |
1896 | } | |
7e9cd484 | 1897 | |
5205e56e | 1898 | ClearPageActive(page); /* we are de-activating */ |
1da177e4 LT |
1899 | list_add(&page->lru, &l_inactive); |
1900 | } | |
1901 | ||
b555749a | 1902 | /* |
8cab4754 | 1903 | * Move pages back to the lru list. |
b555749a | 1904 | */ |
599d0c95 | 1905 | spin_lock_irq(&pgdat->lru_lock); |
556adecb | 1906 | /* |
8cab4754 WF |
1907 | * Count referenced pages from currently used mappings as rotated, |
1908 | * even though only some of them are actually re-activated. This | |
1909 | * helps balance scan pressure between file and anonymous pages in | |
7c0db9e9 | 1910 | * get_scan_count. |
7e9cd484 | 1911 | */ |
b7c46d15 | 1912 | reclaim_stat->recent_rotated[file] += nr_rotated; |
556adecb | 1913 | |
fa9add64 HD |
1914 | move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru); |
1915 | move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE); | |
599d0c95 MG |
1916 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); |
1917 | spin_unlock_irq(&pgdat->lru_lock); | |
2bcf8879 | 1918 | |
747db954 | 1919 | mem_cgroup_uncharge_list(&l_hold); |
b745bc85 | 1920 | free_hot_cold_page_list(&l_hold, true); |
1da177e4 LT |
1921 | } |
1922 | ||
59dc76b0 RR |
1923 | /* |
1924 | * The inactive anon list should be small enough that the VM never has | |
1925 | * to do too much work. | |
14797e23 | 1926 | * |
59dc76b0 RR |
1927 | * The inactive file list should be small enough to leave most memory |
1928 | * to the established workingset on the scan-resistant active list, | |
1929 | * but large enough to avoid thrashing the aggregate readahead window. | |
56e49d21 | 1930 | * |
59dc76b0 RR |
1931 | * Both inactive lists should also be large enough that each inactive |
1932 | * page has a chance to be referenced again before it is reclaimed. | |
56e49d21 | 1933 | * |
59dc76b0 RR |
1934 | * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages |
1935 | * on this LRU, maintained by the pageout code. A zone->inactive_ratio | |
1936 | * of 3 means 3:1 or 25% of the pages are kept on the inactive list. | |
56e49d21 | 1937 | * |
59dc76b0 RR |
1938 | * total target max |
1939 | * memory ratio inactive | |
1940 | * ------------------------------------- | |
1941 | * 10MB 1 5MB | |
1942 | * 100MB 1 50MB | |
1943 | * 1GB 3 250MB | |
1944 | * 10GB 10 0.9GB | |
1945 | * 100GB 31 3GB | |
1946 | * 1TB 101 10GB | |
1947 | * 10TB 320 32GB | |
56e49d21 | 1948 | */ |
59dc76b0 | 1949 | static bool inactive_list_is_low(struct lruvec *lruvec, bool file) |
56e49d21 | 1950 | { |
59dc76b0 | 1951 | unsigned long inactive_ratio; |
e3790144 JW |
1952 | unsigned long inactive; |
1953 | unsigned long active; | |
59dc76b0 | 1954 | unsigned long gb; |
e3790144 | 1955 | |
59dc76b0 RR |
1956 | /* |
1957 | * If we don't have swap space, anonymous page deactivation | |
1958 | * is pointless. | |
1959 | */ | |
1960 | if (!file && !total_swap_pages) | |
1961 | return false; | |
56e49d21 | 1962 | |
59dc76b0 RR |
1963 | inactive = lruvec_lru_size(lruvec, file * LRU_FILE); |
1964 | active = lruvec_lru_size(lruvec, file * LRU_FILE + LRU_ACTIVE); | |
56e49d21 | 1965 | |
59dc76b0 RR |
1966 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
1967 | if (gb) | |
1968 | inactive_ratio = int_sqrt(10 * gb); | |
b39415b2 | 1969 | else |
59dc76b0 RR |
1970 | inactive_ratio = 1; |
1971 | ||
1972 | return inactive * inactive_ratio < active; | |
b39415b2 RR |
1973 | } |
1974 | ||
4f98a2fe | 1975 | static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, |
1a93be0e | 1976 | struct lruvec *lruvec, struct scan_control *sc) |
b69408e8 | 1977 | { |
b39415b2 | 1978 | if (is_active_lru(lru)) { |
59dc76b0 | 1979 | if (inactive_list_is_low(lruvec, is_file_lru(lru))) |
1a93be0e | 1980 | shrink_active_list(nr_to_scan, lruvec, sc, lru); |
556adecb RR |
1981 | return 0; |
1982 | } | |
1983 | ||
1a93be0e | 1984 | return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); |
4f98a2fe RR |
1985 | } |
1986 | ||
9a265114 JW |
1987 | enum scan_balance { |
1988 | SCAN_EQUAL, | |
1989 | SCAN_FRACT, | |
1990 | SCAN_ANON, | |
1991 | SCAN_FILE, | |
1992 | }; | |
1993 | ||
4f98a2fe RR |
1994 | /* |
1995 | * Determine how aggressively the anon and file LRU lists should be | |
1996 | * scanned. The relative value of each set of LRU lists is determined | |
1997 | * by looking at the fraction of the pages scanned we did rotate back | |
1998 | * onto the active list instead of evict. | |
1999 | * | |
be7bd59d WL |
2000 | * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan |
2001 | * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan | |
4f98a2fe | 2002 | */ |
33377678 | 2003 | static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, |
6b4f7799 JW |
2004 | struct scan_control *sc, unsigned long *nr, |
2005 | unsigned long *lru_pages) | |
4f98a2fe | 2006 | { |
33377678 | 2007 | int swappiness = mem_cgroup_swappiness(memcg); |
9a265114 JW |
2008 | struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; |
2009 | u64 fraction[2]; | |
2010 | u64 denominator = 0; /* gcc */ | |
599d0c95 | 2011 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
4f98a2fe | 2012 | unsigned long anon_prio, file_prio; |
9a265114 | 2013 | enum scan_balance scan_balance; |
0bf1457f | 2014 | unsigned long anon, file; |
9a265114 | 2015 | bool force_scan = false; |
4f98a2fe | 2016 | unsigned long ap, fp; |
4111304d | 2017 | enum lru_list lru; |
6f04f48d SS |
2018 | bool some_scanned; |
2019 | int pass; | |
246e87a9 | 2020 | |
f11c0ca5 JW |
2021 | /* |
2022 | * If the zone or memcg is small, nr[l] can be 0. This | |
2023 | * results in no scanning on this priority and a potential | |
2024 | * priority drop. Global direct reclaim can go to the next | |
2025 | * zone and tends to have no problems. Global kswapd is for | |
2026 | * zone balancing and it needs to scan a minimum amount. When | |
2027 | * reclaiming for a memcg, a priority drop can cause high | |
2028 | * latencies, so it's better to scan a minimum amount there as | |
2029 | * well. | |
2030 | */ | |
90cbc250 | 2031 | if (current_is_kswapd()) { |
599d0c95 | 2032 | if (!pgdat_reclaimable(pgdat)) |
90cbc250 | 2033 | force_scan = true; |
eb01aaab | 2034 | if (!mem_cgroup_online(memcg)) |
90cbc250 VD |
2035 | force_scan = true; |
2036 | } | |
89b5fae5 | 2037 | if (!global_reclaim(sc)) |
a4d3e9e7 | 2038 | force_scan = true; |
76a33fc3 SL |
2039 | |
2040 | /* If we have no swap space, do not bother scanning anon pages. */ | |
d8b38438 | 2041 | if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) { |
9a265114 | 2042 | scan_balance = SCAN_FILE; |
76a33fc3 SL |
2043 | goto out; |
2044 | } | |
4f98a2fe | 2045 | |
10316b31 JW |
2046 | /* |
2047 | * Global reclaim will swap to prevent OOM even with no | |
2048 | * swappiness, but memcg users want to use this knob to | |
2049 | * disable swapping for individual groups completely when | |
2050 | * using the memory controller's swap limit feature would be | |
2051 | * too expensive. | |
2052 | */ | |
02695175 | 2053 | if (!global_reclaim(sc) && !swappiness) { |
9a265114 | 2054 | scan_balance = SCAN_FILE; |
10316b31 JW |
2055 | goto out; |
2056 | } | |
2057 | ||
2058 | /* | |
2059 | * Do not apply any pressure balancing cleverness when the | |
2060 | * system is close to OOM, scan both anon and file equally | |
2061 | * (unless the swappiness setting disagrees with swapping). | |
2062 | */ | |
02695175 | 2063 | if (!sc->priority && swappiness) { |
9a265114 | 2064 | scan_balance = SCAN_EQUAL; |
10316b31 JW |
2065 | goto out; |
2066 | } | |
2067 | ||
62376251 JW |
2068 | /* |
2069 | * Prevent the reclaimer from falling into the cache trap: as | |
2070 | * cache pages start out inactive, every cache fault will tip | |
2071 | * the scan balance towards the file LRU. And as the file LRU | |
2072 | * shrinks, so does the window for rotation from references. | |
2073 | * This means we have a runaway feedback loop where a tiny | |
2074 | * thrashing file LRU becomes infinitely more attractive than | |
2075 | * anon pages. Try to detect this based on file LRU size. | |
2076 | */ | |
2077 | if (global_reclaim(sc)) { | |
599d0c95 MG |
2078 | unsigned long pgdatfile; |
2079 | unsigned long pgdatfree; | |
2080 | int z; | |
2081 | unsigned long total_high_wmark = 0; | |
2ab051e1 | 2082 | |
599d0c95 MG |
2083 | pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); |
2084 | pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) + | |
2085 | node_page_state(pgdat, NR_INACTIVE_FILE); | |
2086 | ||
2087 | for (z = 0; z < MAX_NR_ZONES; z++) { | |
2088 | struct zone *zone = &pgdat->node_zones[z]; | |
2089 | if (!populated_zone(zone)) | |
2090 | continue; | |
2091 | ||
2092 | total_high_wmark += high_wmark_pages(zone); | |
2093 | } | |
62376251 | 2094 | |
599d0c95 | 2095 | if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) { |
62376251 JW |
2096 | scan_balance = SCAN_ANON; |
2097 | goto out; | |
2098 | } | |
2099 | } | |
2100 | ||
7c5bd705 | 2101 | /* |
316bda0e VD |
2102 | * If there is enough inactive page cache, i.e. if the size of the |
2103 | * inactive list is greater than that of the active list *and* the | |
2104 | * inactive list actually has some pages to scan on this priority, we | |
2105 | * do not reclaim anything from the anonymous working set right now. | |
2106 | * Without the second condition we could end up never scanning an | |
2107 | * lruvec even if it has plenty of old anonymous pages unless the | |
2108 | * system is under heavy pressure. | |
7c5bd705 | 2109 | */ |
59dc76b0 | 2110 | if (!inactive_list_is_low(lruvec, true) && |
23047a96 | 2111 | lruvec_lru_size(lruvec, LRU_INACTIVE_FILE) >> sc->priority) { |
9a265114 | 2112 | scan_balance = SCAN_FILE; |
7c5bd705 JW |
2113 | goto out; |
2114 | } | |
2115 | ||
9a265114 JW |
2116 | scan_balance = SCAN_FRACT; |
2117 | ||
58c37f6e KM |
2118 | /* |
2119 | * With swappiness at 100, anonymous and file have the same priority. | |
2120 | * This scanning priority is essentially the inverse of IO cost. | |
2121 | */ | |
02695175 | 2122 | anon_prio = swappiness; |
75b00af7 | 2123 | file_prio = 200 - anon_prio; |
58c37f6e | 2124 | |
4f98a2fe RR |
2125 | /* |
2126 | * OK, so we have swap space and a fair amount of page cache | |
2127 | * pages. We use the recently rotated / recently scanned | |
2128 | * ratios to determine how valuable each cache is. | |
2129 | * | |
2130 | * Because workloads change over time (and to avoid overflow) | |
2131 | * we keep these statistics as a floating average, which ends | |
2132 | * up weighing recent references more than old ones. | |
2133 | * | |
2134 | * anon in [0], file in [1] | |
2135 | */ | |
2ab051e1 | 2136 | |
23047a96 JW |
2137 | anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON) + |
2138 | lruvec_lru_size(lruvec, LRU_INACTIVE_ANON); | |
2139 | file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE) + | |
2140 | lruvec_lru_size(lruvec, LRU_INACTIVE_FILE); | |
2ab051e1 | 2141 | |
599d0c95 | 2142 | spin_lock_irq(&pgdat->lru_lock); |
6e901571 | 2143 | if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { |
6e901571 KM |
2144 | reclaim_stat->recent_scanned[0] /= 2; |
2145 | reclaim_stat->recent_rotated[0] /= 2; | |
4f98a2fe RR |
2146 | } |
2147 | ||
6e901571 | 2148 | if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { |
6e901571 KM |
2149 | reclaim_stat->recent_scanned[1] /= 2; |
2150 | reclaim_stat->recent_rotated[1] /= 2; | |
4f98a2fe RR |
2151 | } |
2152 | ||
4f98a2fe | 2153 | /* |
00d8089c RR |
2154 | * The amount of pressure on anon vs file pages is inversely |
2155 | * proportional to the fraction of recently scanned pages on | |
2156 | * each list that were recently referenced and in active use. | |
4f98a2fe | 2157 | */ |
fe35004f | 2158 | ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); |
6e901571 | 2159 | ap /= reclaim_stat->recent_rotated[0] + 1; |
4f98a2fe | 2160 | |
fe35004f | 2161 | fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); |
6e901571 | 2162 | fp /= reclaim_stat->recent_rotated[1] + 1; |
599d0c95 | 2163 | spin_unlock_irq(&pgdat->lru_lock); |
4f98a2fe | 2164 | |
76a33fc3 SL |
2165 | fraction[0] = ap; |
2166 | fraction[1] = fp; | |
2167 | denominator = ap + fp + 1; | |
2168 | out: | |
6f04f48d SS |
2169 | some_scanned = false; |
2170 | /* Only use force_scan on second pass. */ | |
2171 | for (pass = 0; !some_scanned && pass < 2; pass++) { | |
6b4f7799 | 2172 | *lru_pages = 0; |
6f04f48d SS |
2173 | for_each_evictable_lru(lru) { |
2174 | int file = is_file_lru(lru); | |
2175 | unsigned long size; | |
2176 | unsigned long scan; | |
6e08a369 | 2177 | |
23047a96 | 2178 | size = lruvec_lru_size(lruvec, lru); |
6f04f48d | 2179 | scan = size >> sc->priority; |
9a265114 | 2180 | |
6f04f48d SS |
2181 | if (!scan && pass && force_scan) |
2182 | scan = min(size, SWAP_CLUSTER_MAX); | |
9a265114 | 2183 | |
6f04f48d SS |
2184 | switch (scan_balance) { |
2185 | case SCAN_EQUAL: | |
2186 | /* Scan lists relative to size */ | |
2187 | break; | |
2188 | case SCAN_FRACT: | |
2189 | /* | |
2190 | * Scan types proportional to swappiness and | |
2191 | * their relative recent reclaim efficiency. | |
2192 | */ | |
2193 | scan = div64_u64(scan * fraction[file], | |
2194 | denominator); | |
2195 | break; | |
2196 | case SCAN_FILE: | |
2197 | case SCAN_ANON: | |
2198 | /* Scan one type exclusively */ | |
6b4f7799 JW |
2199 | if ((scan_balance == SCAN_FILE) != file) { |
2200 | size = 0; | |
6f04f48d | 2201 | scan = 0; |
6b4f7799 | 2202 | } |
6f04f48d SS |
2203 | break; |
2204 | default: | |
2205 | /* Look ma, no brain */ | |
2206 | BUG(); | |
2207 | } | |
6b4f7799 JW |
2208 | |
2209 | *lru_pages += size; | |
6f04f48d | 2210 | nr[lru] = scan; |
6b4f7799 | 2211 | |
9a265114 | 2212 | /* |
6f04f48d SS |
2213 | * Skip the second pass and don't force_scan, |
2214 | * if we found something to scan. | |
9a265114 | 2215 | */ |
6f04f48d | 2216 | some_scanned |= !!scan; |
9a265114 | 2217 | } |
76a33fc3 | 2218 | } |
6e08a369 | 2219 | } |
4f98a2fe | 2220 | |
72b252ae MG |
2221 | #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH |
2222 | static void init_tlb_ubc(void) | |
2223 | { | |
2224 | /* | |
2225 | * This deliberately does not clear the cpumask as it's expensive | |
2226 | * and unnecessary. If there happens to be data in there then the | |
2227 | * first SWAP_CLUSTER_MAX pages will send an unnecessary IPI and | |
2228 | * then will be cleared. | |
2229 | */ | |
2230 | current->tlb_ubc.flush_required = false; | |
2231 | } | |
2232 | #else | |
2233 | static inline void init_tlb_ubc(void) | |
2234 | { | |
2235 | } | |
2236 | #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ | |
2237 | ||
9b4f98cd | 2238 | /* |
a9dd0a83 | 2239 | * This is a basic per-node page freer. Used by both kswapd and direct reclaim. |
9b4f98cd | 2240 | */ |
a9dd0a83 | 2241 | static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg, |
33377678 | 2242 | struct scan_control *sc, unsigned long *lru_pages) |
9b4f98cd | 2243 | { |
ef8f2327 | 2244 | struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); |
9b4f98cd | 2245 | unsigned long nr[NR_LRU_LISTS]; |
e82e0561 | 2246 | unsigned long targets[NR_LRU_LISTS]; |
9b4f98cd JW |
2247 | unsigned long nr_to_scan; |
2248 | enum lru_list lru; | |
2249 | unsigned long nr_reclaimed = 0; | |
2250 | unsigned long nr_to_reclaim = sc->nr_to_reclaim; | |
2251 | struct blk_plug plug; | |
1a501907 | 2252 | bool scan_adjusted; |
9b4f98cd | 2253 | |
33377678 | 2254 | get_scan_count(lruvec, memcg, sc, nr, lru_pages); |
9b4f98cd | 2255 | |
e82e0561 MG |
2256 | /* Record the original scan target for proportional adjustments later */ |
2257 | memcpy(targets, nr, sizeof(nr)); | |
2258 | ||
1a501907 MG |
2259 | /* |
2260 | * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal | |
2261 | * event that can occur when there is little memory pressure e.g. | |
2262 | * multiple streaming readers/writers. Hence, we do not abort scanning | |
2263 | * when the requested number of pages are reclaimed when scanning at | |
2264 | * DEF_PRIORITY on the assumption that the fact we are direct | |
2265 | * reclaiming implies that kswapd is not keeping up and it is best to | |
2266 | * do a batch of work at once. For memcg reclaim one check is made to | |
2267 | * abort proportional reclaim if either the file or anon lru has already | |
2268 | * dropped to zero at the first pass. | |
2269 | */ | |
2270 | scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() && | |
2271 | sc->priority == DEF_PRIORITY); | |
2272 | ||
72b252ae MG |
2273 | init_tlb_ubc(); |
2274 | ||
9b4f98cd JW |
2275 | blk_start_plug(&plug); |
2276 | while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || | |
2277 | nr[LRU_INACTIVE_FILE]) { | |
e82e0561 MG |
2278 | unsigned long nr_anon, nr_file, percentage; |
2279 | unsigned long nr_scanned; | |
2280 | ||
9b4f98cd JW |
2281 | for_each_evictable_lru(lru) { |
2282 | if (nr[lru]) { | |
2283 | nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); | |
2284 | nr[lru] -= nr_to_scan; | |
2285 | ||
2286 | nr_reclaimed += shrink_list(lru, nr_to_scan, | |
2287 | lruvec, sc); | |
2288 | } | |
2289 | } | |
e82e0561 MG |
2290 | |
2291 | if (nr_reclaimed < nr_to_reclaim || scan_adjusted) | |
2292 | continue; | |
2293 | ||
e82e0561 MG |
2294 | /* |
2295 | * For kswapd and memcg, reclaim at least the number of pages | |
1a501907 | 2296 | * requested. Ensure that the anon and file LRUs are scanned |
e82e0561 MG |
2297 | * proportionally what was requested by get_scan_count(). We |
2298 | * stop reclaiming one LRU and reduce the amount scanning | |
2299 | * proportional to the original scan target. | |
2300 | */ | |
2301 | nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; | |
2302 | nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; | |
2303 | ||
1a501907 MG |
2304 | /* |
2305 | * It's just vindictive to attack the larger once the smaller | |
2306 | * has gone to zero. And given the way we stop scanning the | |
2307 | * smaller below, this makes sure that we only make one nudge | |
2308 | * towards proportionality once we've got nr_to_reclaim. | |
2309 | */ | |
2310 | if (!nr_file || !nr_anon) | |
2311 | break; | |
2312 | ||
e82e0561 MG |
2313 | if (nr_file > nr_anon) { |
2314 | unsigned long scan_target = targets[LRU_INACTIVE_ANON] + | |
2315 | targets[LRU_ACTIVE_ANON] + 1; | |
2316 | lru = LRU_BASE; | |
2317 | percentage = nr_anon * 100 / scan_target; | |
2318 | } else { | |
2319 | unsigned long scan_target = targets[LRU_INACTIVE_FILE] + | |
2320 | targets[LRU_ACTIVE_FILE] + 1; | |
2321 | lru = LRU_FILE; | |
2322 | percentage = nr_file * 100 / scan_target; | |
2323 | } | |
2324 | ||
2325 | /* Stop scanning the smaller of the LRU */ | |
2326 | nr[lru] = 0; | |
2327 | nr[lru + LRU_ACTIVE] = 0; | |
2328 | ||
2329 | /* | |
2330 | * Recalculate the other LRU scan count based on its original | |
2331 | * scan target and the percentage scanning already complete | |
2332 | */ | |
2333 | lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; | |
2334 | nr_scanned = targets[lru] - nr[lru]; | |
2335 | nr[lru] = targets[lru] * (100 - percentage) / 100; | |
2336 | nr[lru] -= min(nr[lru], nr_scanned); | |
2337 | ||
2338 | lru += LRU_ACTIVE; | |
2339 | nr_scanned = targets[lru] - nr[lru]; | |
2340 | nr[lru] = targets[lru] * (100 - percentage) / 100; | |
2341 | nr[lru] -= min(nr[lru], nr_scanned); | |
2342 | ||
2343 | scan_adjusted = true; | |
9b4f98cd JW |
2344 | } |
2345 | blk_finish_plug(&plug); | |
2346 | sc->nr_reclaimed += nr_reclaimed; | |
2347 | ||
2348 | /* | |
2349 | * Even if we did not try to evict anon pages at all, we want to | |
2350 | * rebalance the anon lru active/inactive ratio. | |
2351 | */ | |
59dc76b0 | 2352 | if (inactive_list_is_low(lruvec, false)) |
9b4f98cd JW |
2353 | shrink_active_list(SWAP_CLUSTER_MAX, lruvec, |
2354 | sc, LRU_ACTIVE_ANON); | |
2355 | ||
2356 | throttle_vm_writeout(sc->gfp_mask); | |
2357 | } | |
2358 | ||
23b9da55 | 2359 | /* Use reclaim/compaction for costly allocs or under memory pressure */ |
9e3b2f8c | 2360 | static bool in_reclaim_compaction(struct scan_control *sc) |
23b9da55 | 2361 | { |
d84da3f9 | 2362 | if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && |
23b9da55 | 2363 | (sc->order > PAGE_ALLOC_COSTLY_ORDER || |
9e3b2f8c | 2364 | sc->priority < DEF_PRIORITY - 2)) |
23b9da55 MG |
2365 | return true; |
2366 | ||
2367 | return false; | |
2368 | } | |
2369 | ||
3e7d3449 | 2370 | /* |
23b9da55 MG |
2371 | * Reclaim/compaction is used for high-order allocation requests. It reclaims |
2372 | * order-0 pages before compacting the zone. should_continue_reclaim() returns | |
2373 | * true if more pages should be reclaimed such that when the page allocator | |
2374 | * calls try_to_compact_zone() that it will have enough free pages to succeed. | |
2375 | * It will give up earlier than that if there is difficulty reclaiming pages. | |
3e7d3449 | 2376 | */ |
a9dd0a83 | 2377 | static inline bool should_continue_reclaim(struct pglist_data *pgdat, |
3e7d3449 MG |
2378 | unsigned long nr_reclaimed, |
2379 | unsigned long nr_scanned, | |
2380 | struct scan_control *sc) | |
2381 | { | |
2382 | unsigned long pages_for_compaction; | |
2383 | unsigned long inactive_lru_pages; | |
a9dd0a83 | 2384 | int z; |
3e7d3449 MG |
2385 | |
2386 | /* If not in reclaim/compaction mode, stop */ | |
9e3b2f8c | 2387 | if (!in_reclaim_compaction(sc)) |
3e7d3449 MG |
2388 | return false; |
2389 | ||
2876592f MG |
2390 | /* Consider stopping depending on scan and reclaim activity */ |
2391 | if (sc->gfp_mask & __GFP_REPEAT) { | |
2392 | /* | |
2393 | * For __GFP_REPEAT allocations, stop reclaiming if the | |
2394 | * full LRU list has been scanned and we are still failing | |
2395 | * to reclaim pages. This full LRU scan is potentially | |
2396 | * expensive but a __GFP_REPEAT caller really wants to succeed | |
2397 | */ | |
2398 | if (!nr_reclaimed && !nr_scanned) | |
2399 | return false; | |
2400 | } else { | |
2401 | /* | |
2402 | * For non-__GFP_REPEAT allocations which can presumably | |
2403 | * fail without consequence, stop if we failed to reclaim | |
2404 | * any pages from the last SWAP_CLUSTER_MAX number of | |
2405 | * pages that were scanned. This will return to the | |
2406 | * caller faster at the risk reclaim/compaction and | |
2407 | * the resulting allocation attempt fails | |
2408 | */ | |
2409 | if (!nr_reclaimed) | |
2410 | return false; | |
2411 | } | |
3e7d3449 MG |
2412 | |
2413 | /* | |
2414 | * If we have not reclaimed enough pages for compaction and the | |
2415 | * inactive lists are large enough, continue reclaiming | |
2416 | */ | |
2417 | pages_for_compaction = (2UL << sc->order); | |
a9dd0a83 | 2418 | inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); |
ec8acf20 | 2419 | if (get_nr_swap_pages() > 0) |
a9dd0a83 | 2420 | inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); |
3e7d3449 MG |
2421 | if (sc->nr_reclaimed < pages_for_compaction && |
2422 | inactive_lru_pages > pages_for_compaction) | |
2423 | return true; | |
2424 | ||
2425 | /* If compaction would go ahead or the allocation would succeed, stop */ | |
a9dd0a83 MG |
2426 | for (z = 0; z <= sc->reclaim_idx; z++) { |
2427 | struct zone *zone = &pgdat->node_zones[z]; | |
2428 | if (!populated_zone(zone)) | |
2429 | continue; | |
2430 | ||
2431 | switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { | |
2432 | case COMPACT_PARTIAL: | |
2433 | case COMPACT_CONTINUE: | |
2434 | return false; | |
2435 | default: | |
2436 | /* check next zone */ | |
2437 | ; | |
2438 | } | |
3e7d3449 | 2439 | } |
a9dd0a83 | 2440 | return true; |
3e7d3449 MG |
2441 | } |
2442 | ||
970a39a3 | 2443 | static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) |
1da177e4 | 2444 | { |
cb731d6c | 2445 | struct reclaim_state *reclaim_state = current->reclaim_state; |
f0fdc5e8 | 2446 | unsigned long nr_reclaimed, nr_scanned; |
2344d7e4 | 2447 | bool reclaimable = false; |
1da177e4 | 2448 | |
9b4f98cd JW |
2449 | do { |
2450 | struct mem_cgroup *root = sc->target_mem_cgroup; | |
2451 | struct mem_cgroup_reclaim_cookie reclaim = { | |
ef8f2327 | 2452 | .pgdat = pgdat, |
9b4f98cd JW |
2453 | .priority = sc->priority, |
2454 | }; | |
a9dd0a83 | 2455 | unsigned long node_lru_pages = 0; |
694fbc0f | 2456 | struct mem_cgroup *memcg; |
3e7d3449 | 2457 | |
9b4f98cd JW |
2458 | nr_reclaimed = sc->nr_reclaimed; |
2459 | nr_scanned = sc->nr_scanned; | |
1da177e4 | 2460 | |
694fbc0f AM |
2461 | memcg = mem_cgroup_iter(root, NULL, &reclaim); |
2462 | do { | |
6b4f7799 | 2463 | unsigned long lru_pages; |
8e8ae645 | 2464 | unsigned long reclaimed; |
cb731d6c | 2465 | unsigned long scanned; |
5660048c | 2466 | |
241994ed JW |
2467 | if (mem_cgroup_low(root, memcg)) { |
2468 | if (!sc->may_thrash) | |
2469 | continue; | |
2470 | mem_cgroup_events(memcg, MEMCG_LOW, 1); | |
2471 | } | |
2472 | ||
8e8ae645 | 2473 | reclaimed = sc->nr_reclaimed; |
cb731d6c | 2474 | scanned = sc->nr_scanned; |
f9be23d6 | 2475 | |
a9dd0a83 MG |
2476 | shrink_node_memcg(pgdat, memcg, sc, &lru_pages); |
2477 | node_lru_pages += lru_pages; | |
f16015fb | 2478 | |
b2e18757 | 2479 | if (!global_reclaim(sc)) |
a9dd0a83 | 2480 | shrink_slab(sc->gfp_mask, pgdat->node_id, |
cb731d6c VD |
2481 | memcg, sc->nr_scanned - scanned, |
2482 | lru_pages); | |
2483 | ||
8e8ae645 JW |
2484 | /* Record the group's reclaim efficiency */ |
2485 | vmpressure(sc->gfp_mask, memcg, false, | |
2486 | sc->nr_scanned - scanned, | |
2487 | sc->nr_reclaimed - reclaimed); | |
2488 | ||
9b4f98cd | 2489 | /* |
a394cb8e MH |
2490 | * Direct reclaim and kswapd have to scan all memory |
2491 | * cgroups to fulfill the overall scan target for the | |
a9dd0a83 | 2492 | * node. |
a394cb8e MH |
2493 | * |
2494 | * Limit reclaim, on the other hand, only cares about | |
2495 | * nr_to_reclaim pages to be reclaimed and it will | |
2496 | * retry with decreasing priority if one round over the | |
2497 | * whole hierarchy is not sufficient. | |
9b4f98cd | 2498 | */ |
a394cb8e MH |
2499 | if (!global_reclaim(sc) && |
2500 | sc->nr_reclaimed >= sc->nr_to_reclaim) { | |
9b4f98cd JW |
2501 | mem_cgroup_iter_break(root, memcg); |
2502 | break; | |
2503 | } | |
241994ed | 2504 | } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim))); |
70ddf637 | 2505 | |
6b4f7799 JW |
2506 | /* |
2507 | * Shrink the slab caches in the same proportion that | |
2508 | * the eligible LRU pages were scanned. | |
2509 | */ | |
b2e18757 | 2510 | if (global_reclaim(sc)) |
a9dd0a83 | 2511 | shrink_slab(sc->gfp_mask, pgdat->node_id, NULL, |
cb731d6c | 2512 | sc->nr_scanned - nr_scanned, |
a9dd0a83 | 2513 | node_lru_pages); |
cb731d6c VD |
2514 | |
2515 | if (reclaim_state) { | |
2516 | sc->nr_reclaimed += reclaim_state->reclaimed_slab; | |
2517 | reclaim_state->reclaimed_slab = 0; | |
6b4f7799 JW |
2518 | } |
2519 | ||
8e8ae645 JW |
2520 | /* Record the subtree's reclaim efficiency */ |
2521 | vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, | |
70ddf637 AV |
2522 | sc->nr_scanned - nr_scanned, |
2523 | sc->nr_reclaimed - nr_reclaimed); | |
2524 | ||
2344d7e4 JW |
2525 | if (sc->nr_reclaimed - nr_reclaimed) |
2526 | reclaimable = true; | |
2527 | ||
a9dd0a83 | 2528 | } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, |
9b4f98cd | 2529 | sc->nr_scanned - nr_scanned, sc)); |
2344d7e4 JW |
2530 | |
2531 | return reclaimable; | |
f16015fb JW |
2532 | } |
2533 | ||
53853e2d VB |
2534 | /* |
2535 | * Returns true if compaction should go ahead for a high-order request, or | |
2536 | * the high-order allocation would succeed without compaction. | |
2537 | */ | |
4f588331 | 2538 | static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) |
fe4b1b24 | 2539 | { |
31483b6a | 2540 | unsigned long watermark; |
fe4b1b24 MG |
2541 | bool watermark_ok; |
2542 | ||
fe4b1b24 MG |
2543 | /* |
2544 | * Compaction takes time to run and there are potentially other | |
2545 | * callers using the pages just freed. Continue reclaiming until | |
2546 | * there is a buffer of free pages available to give compaction | |
2547 | * a reasonable chance of completing and allocating the page | |
2548 | */ | |
4f588331 MG |
2549 | watermark = high_wmark_pages(zone) + (2UL << sc->order); |
2550 | watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); | |
fe4b1b24 MG |
2551 | |
2552 | /* | |
2553 | * If compaction is deferred, reclaim up to a point where | |
2554 | * compaction will have a chance of success when re-enabled | |
2555 | */ | |
4f588331 | 2556 | if (compaction_deferred(zone, sc->order)) |
fe4b1b24 MG |
2557 | return watermark_ok; |
2558 | ||
53853e2d VB |
2559 | /* |
2560 | * If compaction is not ready to start and allocation is not likely | |
2561 | * to succeed without it, then keep reclaiming. | |
2562 | */ | |
4f588331 | 2563 | if (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx) == COMPACT_SKIPPED) |
fe4b1b24 MG |
2564 | return false; |
2565 | ||
2566 | return watermark_ok; | |
2567 | } | |
2568 | ||
1da177e4 LT |
2569 | /* |
2570 | * This is the direct reclaim path, for page-allocating processes. We only | |
2571 | * try to reclaim pages from zones which will satisfy the caller's allocation | |
2572 | * request. | |
2573 | * | |
1da177e4 LT |
2574 | * If a zone is deemed to be full of pinned pages then just give it a light |
2575 | * scan then give up on it. | |
2576 | */ | |
0a0337e0 | 2577 | static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) |
1da177e4 | 2578 | { |
dd1a239f | 2579 | struct zoneref *z; |
54a6eb5c | 2580 | struct zone *zone; |
0608f43d AM |
2581 | unsigned long nr_soft_reclaimed; |
2582 | unsigned long nr_soft_scanned; | |
619d0d76 | 2583 | gfp_t orig_mask; |
79dafcdc | 2584 | pg_data_t *last_pgdat = NULL; |
1cfb419b | 2585 | |
cc715d99 MG |
2586 | /* |
2587 | * If the number of buffer_heads in the machine exceeds the maximum | |
2588 | * allowed level, force direct reclaim to scan the highmem zone as | |
2589 | * highmem pages could be pinning lowmem pages storing buffer_heads | |
2590 | */ | |
619d0d76 | 2591 | orig_mask = sc->gfp_mask; |
b2e18757 | 2592 | if (buffer_heads_over_limit) { |
cc715d99 | 2593 | sc->gfp_mask |= __GFP_HIGHMEM; |
4f588331 | 2594 | sc->reclaim_idx = gfp_zone(sc->gfp_mask); |
b2e18757 | 2595 | } |
cc715d99 | 2596 | |
d4debc66 | 2597 | for_each_zone_zonelist_nodemask(zone, z, zonelist, |
b2e18757 | 2598 | sc->reclaim_idx, sc->nodemask) { |
1cfb419b KH |
2599 | /* |
2600 | * Take care memory controller reclaiming has small influence | |
2601 | * to global LRU. | |
2602 | */ | |
89b5fae5 | 2603 | if (global_reclaim(sc)) { |
344736f2 VD |
2604 | if (!cpuset_zone_allowed(zone, |
2605 | GFP_KERNEL | __GFP_HARDWALL)) | |
1cfb419b | 2606 | continue; |
65ec02cb | 2607 | |
6e543d57 | 2608 | if (sc->priority != DEF_PRIORITY && |
599d0c95 | 2609 | !pgdat_reclaimable(zone->zone_pgdat)) |
1cfb419b | 2610 | continue; /* Let kswapd poll it */ |
0b06496a JW |
2611 | |
2612 | /* | |
2613 | * If we already have plenty of memory free for | |
2614 | * compaction in this zone, don't free any more. | |
2615 | * Even though compaction is invoked for any | |
2616 | * non-zero order, only frequent costly order | |
2617 | * reclamation is disruptive enough to become a | |
2618 | * noticeable problem, like transparent huge | |
2619 | * page allocations. | |
2620 | */ | |
2621 | if (IS_ENABLED(CONFIG_COMPACTION) && | |
2622 | sc->order > PAGE_ALLOC_COSTLY_ORDER && | |
4f588331 | 2623 | compaction_ready(zone, sc)) { |
0b06496a JW |
2624 | sc->compaction_ready = true; |
2625 | continue; | |
e0887c19 | 2626 | } |
0b06496a | 2627 | |
79dafcdc MG |
2628 | /* |
2629 | * Shrink each node in the zonelist once. If the | |
2630 | * zonelist is ordered by zone (not the default) then a | |
2631 | * node may be shrunk multiple times but in that case | |
2632 | * the user prefers lower zones being preserved. | |
2633 | */ | |
2634 | if (zone->zone_pgdat == last_pgdat) | |
2635 | continue; | |
2636 | ||
0608f43d AM |
2637 | /* |
2638 | * This steals pages from memory cgroups over softlimit | |
2639 | * and returns the number of reclaimed pages and | |
2640 | * scanned pages. This works for global memory pressure | |
2641 | * and balancing, not for a memcg's limit. | |
2642 | */ | |
2643 | nr_soft_scanned = 0; | |
ef8f2327 | 2644 | nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, |
0608f43d AM |
2645 | sc->order, sc->gfp_mask, |
2646 | &nr_soft_scanned); | |
2647 | sc->nr_reclaimed += nr_soft_reclaimed; | |
2648 | sc->nr_scanned += nr_soft_scanned; | |
ac34a1a3 | 2649 | /* need some check for avoid more shrink_zone() */ |
1cfb419b | 2650 | } |
408d8544 | 2651 | |
79dafcdc MG |
2652 | /* See comment about same check for global reclaim above */ |
2653 | if (zone->zone_pgdat == last_pgdat) | |
2654 | continue; | |
2655 | last_pgdat = zone->zone_pgdat; | |
970a39a3 | 2656 | shrink_node(zone->zone_pgdat, sc); |
1da177e4 | 2657 | } |
e0c23279 | 2658 | |
619d0d76 WY |
2659 | /* |
2660 | * Restore to original mask to avoid the impact on the caller if we | |
2661 | * promoted it to __GFP_HIGHMEM. | |
2662 | */ | |
2663 | sc->gfp_mask = orig_mask; | |
1da177e4 | 2664 | } |
4f98a2fe | 2665 | |
1da177e4 LT |
2666 | /* |
2667 | * This is the main entry point to direct page reclaim. | |
2668 | * | |
2669 | * If a full scan of the inactive list fails to free enough memory then we | |
2670 | * are "out of memory" and something needs to be killed. | |
2671 | * | |
2672 | * If the caller is !__GFP_FS then the probability of a failure is reasonably | |
2673 | * high - the zone may be full of dirty or under-writeback pages, which this | |
5b0830cb JA |
2674 | * caller can't do much about. We kick the writeback threads and take explicit |
2675 | * naps in the hope that some of these pages can be written. But if the | |
2676 | * allocating task holds filesystem locks which prevent writeout this might not | |
2677 | * work, and the allocation attempt will fail. | |
a41f24ea NA |
2678 | * |
2679 | * returns: 0, if no pages reclaimed | |
2680 | * else, the number of pages reclaimed | |
1da177e4 | 2681 | */ |
dac1d27b | 2682 | static unsigned long do_try_to_free_pages(struct zonelist *zonelist, |
3115cd91 | 2683 | struct scan_control *sc) |
1da177e4 | 2684 | { |
241994ed | 2685 | int initial_priority = sc->priority; |
69e05944 | 2686 | unsigned long total_scanned = 0; |
22fba335 | 2687 | unsigned long writeback_threshold; |
241994ed | 2688 | retry: |
873b4771 KK |
2689 | delayacct_freepages_start(); |
2690 | ||
89b5fae5 | 2691 | if (global_reclaim(sc)) |
7cc30fcf | 2692 | __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); |
1da177e4 | 2693 | |
9e3b2f8c | 2694 | do { |
70ddf637 AV |
2695 | vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, |
2696 | sc->priority); | |
66e1707b | 2697 | sc->nr_scanned = 0; |
0a0337e0 | 2698 | shrink_zones(zonelist, sc); |
c6a8a8c5 | 2699 | |
66e1707b | 2700 | total_scanned += sc->nr_scanned; |
bb21c7ce | 2701 | if (sc->nr_reclaimed >= sc->nr_to_reclaim) |
0b06496a JW |
2702 | break; |
2703 | ||
2704 | if (sc->compaction_ready) | |
2705 | break; | |
1da177e4 | 2706 | |
0e50ce3b MK |
2707 | /* |
2708 | * If we're getting trouble reclaiming, start doing | |
2709 | * writepage even in laptop mode. | |
2710 | */ | |
2711 | if (sc->priority < DEF_PRIORITY - 2) | |
2712 | sc->may_writepage = 1; | |
2713 | ||
1da177e4 LT |
2714 | /* |
2715 | * Try to write back as many pages as we just scanned. This | |
2716 | * tends to cause slow streaming writers to write data to the | |
2717 | * disk smoothly, at the dirtying rate, which is nice. But | |
2718 | * that's undesirable in laptop mode, where we *want* lumpy | |
2719 | * writeout. So in laptop mode, write out the whole world. | |
2720 | */ | |
22fba335 KM |
2721 | writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2; |
2722 | if (total_scanned > writeback_threshold) { | |
0e175a18 CW |
2723 | wakeup_flusher_threads(laptop_mode ? 0 : total_scanned, |
2724 | WB_REASON_TRY_TO_FREE_PAGES); | |
66e1707b | 2725 | sc->may_writepage = 1; |
1da177e4 | 2726 | } |
0b06496a | 2727 | } while (--sc->priority >= 0); |
bb21c7ce | 2728 | |
873b4771 KK |
2729 | delayacct_freepages_end(); |
2730 | ||
bb21c7ce KM |
2731 | if (sc->nr_reclaimed) |
2732 | return sc->nr_reclaimed; | |
2733 | ||
0cee34fd | 2734 | /* Aborted reclaim to try compaction? don't OOM, then */ |
0b06496a | 2735 | if (sc->compaction_ready) |
7335084d MG |
2736 | return 1; |
2737 | ||
241994ed JW |
2738 | /* Untapped cgroup reserves? Don't OOM, retry. */ |
2739 | if (!sc->may_thrash) { | |
2740 | sc->priority = initial_priority; | |
2741 | sc->may_thrash = 1; | |
2742 | goto retry; | |
2743 | } | |
2744 | ||
bb21c7ce | 2745 | return 0; |
1da177e4 LT |
2746 | } |
2747 | ||
5515061d MG |
2748 | static bool pfmemalloc_watermark_ok(pg_data_t *pgdat) |
2749 | { | |
2750 | struct zone *zone; | |
2751 | unsigned long pfmemalloc_reserve = 0; | |
2752 | unsigned long free_pages = 0; | |
2753 | int i; | |
2754 | bool wmark_ok; | |
2755 | ||
2756 | for (i = 0; i <= ZONE_NORMAL; i++) { | |
2757 | zone = &pgdat->node_zones[i]; | |
f012a84a | 2758 | if (!populated_zone(zone) || |
599d0c95 | 2759 | pgdat_reclaimable_pages(pgdat) == 0) |
675becce MG |
2760 | continue; |
2761 | ||
5515061d MG |
2762 | pfmemalloc_reserve += min_wmark_pages(zone); |
2763 | free_pages += zone_page_state(zone, NR_FREE_PAGES); | |
2764 | } | |
2765 | ||
675becce MG |
2766 | /* If there are no reserves (unexpected config) then do not throttle */ |
2767 | if (!pfmemalloc_reserve) | |
2768 | return true; | |
2769 | ||
5515061d MG |
2770 | wmark_ok = free_pages > pfmemalloc_reserve / 2; |
2771 | ||
2772 | /* kswapd must be awake if processes are being throttled */ | |
2773 | if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { | |
38087d9b | 2774 | pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx, |
5515061d MG |
2775 | (enum zone_type)ZONE_NORMAL); |
2776 | wake_up_interruptible(&pgdat->kswapd_wait); | |
2777 | } | |
2778 | ||
2779 | return wmark_ok; | |
2780 | } | |
2781 | ||
2782 | /* | |
2783 | * Throttle direct reclaimers if backing storage is backed by the network | |
2784 | * and the PFMEMALLOC reserve for the preferred node is getting dangerously | |
2785 | * depleted. kswapd will continue to make progress and wake the processes | |
50694c28 MG |
2786 | * when the low watermark is reached. |
2787 | * | |
2788 | * Returns true if a fatal signal was delivered during throttling. If this | |
2789 | * happens, the page allocator should not consider triggering the OOM killer. | |
5515061d | 2790 | */ |
50694c28 | 2791 | static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, |
5515061d MG |
2792 | nodemask_t *nodemask) |
2793 | { | |
675becce | 2794 | struct zoneref *z; |
5515061d | 2795 | struct zone *zone; |
675becce | 2796 | pg_data_t *pgdat = NULL; |
5515061d MG |
2797 | |
2798 | /* | |
2799 | * Kernel threads should not be throttled as they may be indirectly | |
2800 | * responsible for cleaning pages necessary for reclaim to make forward | |
2801 | * progress. kjournald for example may enter direct reclaim while | |
2802 | * committing a transaction where throttling it could forcing other | |
2803 | * processes to block on log_wait_commit(). | |
2804 | */ | |
2805 | if (current->flags & PF_KTHREAD) | |
50694c28 MG |
2806 | goto out; |
2807 | ||
2808 | /* | |
2809 | * If a fatal signal is pending, this process should not throttle. | |
2810 | * It should return quickly so it can exit and free its memory | |
2811 | */ | |
2812 | if (fatal_signal_pending(current)) | |
2813 | goto out; | |
5515061d | 2814 | |
675becce MG |
2815 | /* |
2816 | * Check if the pfmemalloc reserves are ok by finding the first node | |
2817 | * with a usable ZONE_NORMAL or lower zone. The expectation is that | |
2818 | * GFP_KERNEL will be required for allocating network buffers when | |
2819 | * swapping over the network so ZONE_HIGHMEM is unusable. | |
2820 | * | |
2821 | * Throttling is based on the first usable node and throttled processes | |
2822 | * wait on a queue until kswapd makes progress and wakes them. There | |
2823 | * is an affinity then between processes waking up and where reclaim | |
2824 | * progress has been made assuming the process wakes on the same node. | |
2825 | * More importantly, processes running on remote nodes will not compete | |
2826 | * for remote pfmemalloc reserves and processes on different nodes | |
2827 | * should make reasonable progress. | |
2828 | */ | |
2829 | for_each_zone_zonelist_nodemask(zone, z, zonelist, | |
17636faa | 2830 | gfp_zone(gfp_mask), nodemask) { |
675becce MG |
2831 | if (zone_idx(zone) > ZONE_NORMAL) |
2832 | continue; | |
2833 | ||
2834 | /* Throttle based on the first usable node */ | |
2835 | pgdat = zone->zone_pgdat; | |
2836 | if (pfmemalloc_watermark_ok(pgdat)) | |
2837 | goto out; | |
2838 | break; | |
2839 | } | |
2840 | ||
2841 | /* If no zone was usable by the allocation flags then do not throttle */ | |
2842 | if (!pgdat) | |
50694c28 | 2843 | goto out; |
5515061d | 2844 | |
68243e76 MG |
2845 | /* Account for the throttling */ |
2846 | count_vm_event(PGSCAN_DIRECT_THROTTLE); | |
2847 | ||
5515061d MG |
2848 | /* |
2849 | * If the caller cannot enter the filesystem, it's possible that it | |
2850 | * is due to the caller holding an FS lock or performing a journal | |
2851 | * transaction in the case of a filesystem like ext[3|4]. In this case, | |
2852 | * it is not safe to block on pfmemalloc_wait as kswapd could be | |
2853 | * blocked waiting on the same lock. Instead, throttle for up to a | |
2854 | * second before continuing. | |
2855 | */ | |
2856 | if (!(gfp_mask & __GFP_FS)) { | |
2857 | wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, | |
2858 | pfmemalloc_watermark_ok(pgdat), HZ); | |
50694c28 MG |
2859 | |
2860 | goto check_pending; | |
5515061d MG |
2861 | } |
2862 | ||
2863 | /* Throttle until kswapd wakes the process */ | |
2864 | wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, | |
2865 | pfmemalloc_watermark_ok(pgdat)); | |
50694c28 MG |
2866 | |
2867 | check_pending: | |
2868 | if (fatal_signal_pending(current)) | |
2869 | return true; | |
2870 | ||
2871 | out: | |
2872 | return false; | |
5515061d MG |
2873 | } |
2874 | ||
dac1d27b | 2875 | unsigned long try_to_free_pages(struct zonelist *zonelist, int order, |
327c0e96 | 2876 | gfp_t gfp_mask, nodemask_t *nodemask) |
66e1707b | 2877 | { |
33906bc5 | 2878 | unsigned long nr_reclaimed; |
66e1707b | 2879 | struct scan_control sc = { |
ee814fe2 | 2880 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
21caf2fc | 2881 | .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), |
b2e18757 | 2882 | .reclaim_idx = gfp_zone(gfp_mask), |
ee814fe2 JW |
2883 | .order = order, |
2884 | .nodemask = nodemask, | |
2885 | .priority = DEF_PRIORITY, | |
66e1707b | 2886 | .may_writepage = !laptop_mode, |
a6dc60f8 | 2887 | .may_unmap = 1, |
2e2e4259 | 2888 | .may_swap = 1, |
66e1707b BS |
2889 | }; |
2890 | ||
5515061d | 2891 | /* |
50694c28 MG |
2892 | * Do not enter reclaim if fatal signal was delivered while throttled. |
2893 | * 1 is returned so that the page allocator does not OOM kill at this | |
2894 | * point. | |
5515061d | 2895 | */ |
50694c28 | 2896 | if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask)) |
5515061d MG |
2897 | return 1; |
2898 | ||
33906bc5 MG |
2899 | trace_mm_vmscan_direct_reclaim_begin(order, |
2900 | sc.may_writepage, | |
e5146b12 MG |
2901 | gfp_mask, |
2902 | sc.reclaim_idx); | |
33906bc5 | 2903 | |
3115cd91 | 2904 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
33906bc5 MG |
2905 | |
2906 | trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); | |
2907 | ||
2908 | return nr_reclaimed; | |
66e1707b BS |
2909 | } |
2910 | ||
c255a458 | 2911 | #ifdef CONFIG_MEMCG |
66e1707b | 2912 | |
a9dd0a83 | 2913 | unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, |
4e416953 | 2914 | gfp_t gfp_mask, bool noswap, |
ef8f2327 | 2915 | pg_data_t *pgdat, |
0ae5e89c | 2916 | unsigned long *nr_scanned) |
4e416953 BS |
2917 | { |
2918 | struct scan_control sc = { | |
b8f5c566 | 2919 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
ee814fe2 | 2920 | .target_mem_cgroup = memcg, |
4e416953 BS |
2921 | .may_writepage = !laptop_mode, |
2922 | .may_unmap = 1, | |
b2e18757 | 2923 | .reclaim_idx = MAX_NR_ZONES - 1, |
4e416953 | 2924 | .may_swap = !noswap, |
4e416953 | 2925 | }; |
6b4f7799 | 2926 | unsigned long lru_pages; |
0ae5e89c | 2927 | |
4e416953 BS |
2928 | sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | |
2929 | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); | |
bdce6d9e | 2930 | |
9e3b2f8c | 2931 | trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, |
bdce6d9e | 2932 | sc.may_writepage, |
e5146b12 MG |
2933 | sc.gfp_mask, |
2934 | sc.reclaim_idx); | |
bdce6d9e | 2935 | |
4e416953 BS |
2936 | /* |
2937 | * NOTE: Although we can get the priority field, using it | |
2938 | * here is not a good idea, since it limits the pages we can scan. | |
a9dd0a83 | 2939 | * if we don't reclaim here, the shrink_node from balance_pgdat |
4e416953 BS |
2940 | * will pick up pages from other mem cgroup's as well. We hack |
2941 | * the priority and make it zero. | |
2942 | */ | |
ef8f2327 | 2943 | shrink_node_memcg(pgdat, memcg, &sc, &lru_pages); |
bdce6d9e KM |
2944 | |
2945 | trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); | |
2946 | ||
0ae5e89c | 2947 | *nr_scanned = sc.nr_scanned; |
4e416953 BS |
2948 | return sc.nr_reclaimed; |
2949 | } | |
2950 | ||
72835c86 | 2951 | unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, |
b70a2a21 | 2952 | unsigned long nr_pages, |
a7885eb8 | 2953 | gfp_t gfp_mask, |
b70a2a21 | 2954 | bool may_swap) |
66e1707b | 2955 | { |
4e416953 | 2956 | struct zonelist *zonelist; |
bdce6d9e | 2957 | unsigned long nr_reclaimed; |
889976db | 2958 | int nid; |
66e1707b | 2959 | struct scan_control sc = { |
b70a2a21 | 2960 | .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), |
a09ed5e0 YH |
2961 | .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | |
2962 | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), | |
b2e18757 | 2963 | .reclaim_idx = MAX_NR_ZONES - 1, |
ee814fe2 JW |
2964 | .target_mem_cgroup = memcg, |
2965 | .priority = DEF_PRIORITY, | |
2966 | .may_writepage = !laptop_mode, | |
2967 | .may_unmap = 1, | |
b70a2a21 | 2968 | .may_swap = may_swap, |
a09ed5e0 | 2969 | }; |
66e1707b | 2970 | |
889976db YH |
2971 | /* |
2972 | * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't | |
2973 | * take care of from where we get pages. So the node where we start the | |
2974 | * scan does not need to be the current node. | |
2975 | */ | |
72835c86 | 2976 | nid = mem_cgroup_select_victim_node(memcg); |
889976db YH |
2977 | |
2978 | zonelist = NODE_DATA(nid)->node_zonelists; | |
bdce6d9e KM |
2979 | |
2980 | trace_mm_vmscan_memcg_reclaim_begin(0, | |
2981 | sc.may_writepage, | |
e5146b12 MG |
2982 | sc.gfp_mask, |
2983 | sc.reclaim_idx); | |
bdce6d9e | 2984 | |
3115cd91 | 2985 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
bdce6d9e KM |
2986 | |
2987 | trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); | |
2988 | ||
2989 | return nr_reclaimed; | |
66e1707b BS |
2990 | } |
2991 | #endif | |
2992 | ||
1d82de61 | 2993 | static void age_active_anon(struct pglist_data *pgdat, |
ef8f2327 | 2994 | struct scan_control *sc) |
f16015fb | 2995 | { |
b95a2f2d | 2996 | struct mem_cgroup *memcg; |
f16015fb | 2997 | |
b95a2f2d JW |
2998 | if (!total_swap_pages) |
2999 | return; | |
3000 | ||
3001 | memcg = mem_cgroup_iter(NULL, NULL, NULL); | |
3002 | do { | |
ef8f2327 | 3003 | struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); |
b95a2f2d | 3004 | |
59dc76b0 | 3005 | if (inactive_list_is_low(lruvec, false)) |
1a93be0e | 3006 | shrink_active_list(SWAP_CLUSTER_MAX, lruvec, |
9e3b2f8c | 3007 | sc, LRU_ACTIVE_ANON); |
b95a2f2d JW |
3008 | |
3009 | memcg = mem_cgroup_iter(NULL, memcg, NULL); | |
3010 | } while (memcg); | |
f16015fb JW |
3011 | } |
3012 | ||
31483b6a | 3013 | static bool zone_balanced(struct zone *zone, int order, int classzone_idx) |
60cefed4 | 3014 | { |
31483b6a | 3015 | unsigned long mark = high_wmark_pages(zone); |
60cefed4 | 3016 | |
6256c6b4 MG |
3017 | if (!zone_watermark_ok_safe(zone, order, mark, classzone_idx)) |
3018 | return false; | |
3019 | ||
3020 | /* | |
3021 | * If any eligible zone is balanced then the node is not considered | |
3022 | * to be congested or dirty | |
3023 | */ | |
3024 | clear_bit(PGDAT_CONGESTED, &zone->zone_pgdat->flags); | |
3025 | clear_bit(PGDAT_DIRTY, &zone->zone_pgdat->flags); | |
3026 | ||
3027 | return true; | |
60cefed4 JW |
3028 | } |
3029 | ||
5515061d MG |
3030 | /* |
3031 | * Prepare kswapd for sleeping. This verifies that there are no processes | |
3032 | * waiting in throttle_direct_reclaim() and that watermarks have been met. | |
3033 | * | |
3034 | * Returns true if kswapd is ready to sleep | |
3035 | */ | |
d9f21d42 | 3036 | static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx) |
f50de2d3 | 3037 | { |
1d82de61 MG |
3038 | int i; |
3039 | ||
5515061d | 3040 | /* |
9e5e3661 VB |
3041 | * The throttled processes are normally woken up in balance_pgdat() as |
3042 | * soon as pfmemalloc_watermark_ok() is true. But there is a potential | |
3043 | * race between when kswapd checks the watermarks and a process gets | |
3044 | * throttled. There is also a potential race if processes get | |
3045 | * throttled, kswapd wakes, a large process exits thereby balancing the | |
3046 | * zones, which causes kswapd to exit balance_pgdat() before reaching | |
3047 | * the wake up checks. If kswapd is going to sleep, no process should | |
3048 | * be sleeping on pfmemalloc_wait, so wake them now if necessary. If | |
3049 | * the wake up is premature, processes will wake kswapd and get | |
3050 | * throttled again. The difference from wake ups in balance_pgdat() is | |
3051 | * that here we are under prepare_to_wait(). | |
5515061d | 3052 | */ |
9e5e3661 VB |
3053 | if (waitqueue_active(&pgdat->pfmemalloc_wait)) |
3054 | wake_up_all(&pgdat->pfmemalloc_wait); | |
f50de2d3 | 3055 | |
1d82de61 MG |
3056 | for (i = 0; i <= classzone_idx; i++) { |
3057 | struct zone *zone = pgdat->node_zones + i; | |
3058 | ||
3059 | if (!populated_zone(zone)) | |
3060 | continue; | |
3061 | ||
38087d9b MG |
3062 | if (!zone_balanced(zone, order, classzone_idx)) |
3063 | return false; | |
1d82de61 MG |
3064 | } |
3065 | ||
38087d9b | 3066 | return true; |
f50de2d3 MG |
3067 | } |
3068 | ||
75485363 | 3069 | /* |
1d82de61 MG |
3070 | * kswapd shrinks a node of pages that are at or below the highest usable |
3071 | * zone that is currently unbalanced. | |
b8e83b94 MG |
3072 | * |
3073 | * Returns true if kswapd scanned at least the requested number of pages to | |
283aba9f MG |
3074 | * reclaim or if the lack of progress was due to pages under writeback. |
3075 | * This is used to determine if the scanning priority needs to be raised. | |
75485363 | 3076 | */ |
1d82de61 | 3077 | static bool kswapd_shrink_node(pg_data_t *pgdat, |
accf6242 | 3078 | struct scan_control *sc) |
75485363 | 3079 | { |
1d82de61 MG |
3080 | struct zone *zone; |
3081 | int z; | |
75485363 | 3082 | |
1d82de61 MG |
3083 | /* Reclaim a number of pages proportional to the number of zones */ |
3084 | sc->nr_to_reclaim = 0; | |
970a39a3 | 3085 | for (z = 0; z <= sc->reclaim_idx; z++) { |
1d82de61 MG |
3086 | zone = pgdat->node_zones + z; |
3087 | if (!populated_zone(zone)) | |
3088 | continue; | |
7c954f6d | 3089 | |
1d82de61 MG |
3090 | sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); |
3091 | } | |
7c954f6d MG |
3092 | |
3093 | /* | |
1d82de61 MG |
3094 | * Historically care was taken to put equal pressure on all zones but |
3095 | * now pressure is applied based on node LRU order. | |
7c954f6d | 3096 | */ |
970a39a3 | 3097 | shrink_node(pgdat, sc); |
283aba9f | 3098 | |
7c954f6d | 3099 | /* |
1d82de61 MG |
3100 | * Fragmentation may mean that the system cannot be rebalanced for |
3101 | * high-order allocations. If twice the allocation size has been | |
3102 | * reclaimed then recheck watermarks only at order-0 to prevent | |
3103 | * excessive reclaim. Assume that a process requested a high-order | |
3104 | * can direct reclaim/compact. | |
7c954f6d | 3105 | */ |
1d82de61 MG |
3106 | if (sc->order && sc->nr_reclaimed >= 2UL << sc->order) |
3107 | sc->order = 0; | |
7c954f6d | 3108 | |
b8e83b94 | 3109 | return sc->nr_scanned >= sc->nr_to_reclaim; |
75485363 MG |
3110 | } |
3111 | ||
1da177e4 | 3112 | /* |
1d82de61 MG |
3113 | * For kswapd, balance_pgdat() will reclaim pages across a node from zones |
3114 | * that are eligible for use by the caller until at least one zone is | |
3115 | * balanced. | |
1da177e4 | 3116 | * |
1d82de61 | 3117 | * Returns the order kswapd finished reclaiming at. |
1da177e4 LT |
3118 | * |
3119 | * kswapd scans the zones in the highmem->normal->dma direction. It skips | |
41858966 | 3120 | * zones which have free_pages > high_wmark_pages(zone), but once a zone is |
1d82de61 MG |
3121 | * found to have free_pages <= high_wmark_pages(zone), any page is that zone |
3122 | * or lower is eligible for reclaim until at least one usable zone is | |
3123 | * balanced. | |
1da177e4 | 3124 | */ |
accf6242 | 3125 | static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx) |
1da177e4 | 3126 | { |
1da177e4 | 3127 | int i; |
0608f43d AM |
3128 | unsigned long nr_soft_reclaimed; |
3129 | unsigned long nr_soft_scanned; | |
1d82de61 | 3130 | struct zone *zone; |
179e9639 AM |
3131 | struct scan_control sc = { |
3132 | .gfp_mask = GFP_KERNEL, | |
ee814fe2 | 3133 | .order = order, |
b8e83b94 | 3134 | .priority = DEF_PRIORITY, |
ee814fe2 | 3135 | .may_writepage = !laptop_mode, |
a6dc60f8 | 3136 | .may_unmap = 1, |
2e2e4259 | 3137 | .may_swap = 1, |
179e9639 | 3138 | }; |
f8891e5e | 3139 | count_vm_event(PAGEOUTRUN); |
1da177e4 | 3140 | |
9e3b2f8c | 3141 | do { |
b8e83b94 MG |
3142 | bool raise_priority = true; |
3143 | ||
3144 | sc.nr_reclaimed = 0; | |
84c7a777 | 3145 | sc.reclaim_idx = classzone_idx; |
1da177e4 | 3146 | |
86c79f6b | 3147 | /* |
84c7a777 MG |
3148 | * If the number of buffer_heads exceeds the maximum allowed |
3149 | * then consider reclaiming from all zones. This has a dual | |
3150 | * purpose -- on 64-bit systems it is expected that | |
3151 | * buffer_heads are stripped during active rotation. On 32-bit | |
3152 | * systems, highmem pages can pin lowmem memory and shrinking | |
3153 | * buffers can relieve lowmem pressure. Reclaim may still not | |
3154 | * go ahead if all eligible zones for the original allocation | |
3155 | * request are balanced to avoid excessive reclaim from kswapd. | |
86c79f6b MG |
3156 | */ |
3157 | if (buffer_heads_over_limit) { | |
3158 | for (i = MAX_NR_ZONES - 1; i >= 0; i--) { | |
3159 | zone = pgdat->node_zones + i; | |
3160 | if (!populated_zone(zone)) | |
3161 | continue; | |
cc715d99 | 3162 | |
970a39a3 | 3163 | sc.reclaim_idx = i; |
e1dbeda6 | 3164 | break; |
1da177e4 | 3165 | } |
1da177e4 | 3166 | } |
dafcb73e | 3167 | |
86c79f6b MG |
3168 | /* |
3169 | * Only reclaim if there are no eligible zones. Check from | |
3170 | * high to low zone as allocations prefer higher zones. | |
3171 | * Scanning from low to high zone would allow congestion to be | |
3172 | * cleared during a very small window when a small low | |
3173 | * zone was balanced even under extreme pressure when the | |
84c7a777 MG |
3174 | * overall node may be congested. Note that sc.reclaim_idx |
3175 | * is not used as buffer_heads_over_limit may have adjusted | |
3176 | * it. | |
86c79f6b | 3177 | */ |
84c7a777 | 3178 | for (i = classzone_idx; i >= 0; i--) { |
86c79f6b MG |
3179 | zone = pgdat->node_zones + i; |
3180 | if (!populated_zone(zone)) | |
3181 | continue; | |
3182 | ||
84c7a777 | 3183 | if (zone_balanced(zone, sc.order, classzone_idx)) |
86c79f6b MG |
3184 | goto out; |
3185 | } | |
e1dbeda6 | 3186 | |
1d82de61 MG |
3187 | /* |
3188 | * Do some background aging of the anon list, to give | |
3189 | * pages a chance to be referenced before reclaiming. All | |
3190 | * pages are rotated regardless of classzone as this is | |
3191 | * about consistent aging. | |
3192 | */ | |
ef8f2327 | 3193 | age_active_anon(pgdat, &sc); |
1d82de61 | 3194 | |
b7ea3c41 MG |
3195 | /* |
3196 | * If we're getting trouble reclaiming, start doing writepage | |
3197 | * even in laptop mode. | |
3198 | */ | |
1d82de61 | 3199 | if (sc.priority < DEF_PRIORITY - 2 || !pgdat_reclaimable(pgdat)) |
b7ea3c41 MG |
3200 | sc.may_writepage = 1; |
3201 | ||
1d82de61 MG |
3202 | /* Call soft limit reclaim before calling shrink_node. */ |
3203 | sc.nr_scanned = 0; | |
3204 | nr_soft_scanned = 0; | |
ef8f2327 | 3205 | nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, |
1d82de61 MG |
3206 | sc.gfp_mask, &nr_soft_scanned); |
3207 | sc.nr_reclaimed += nr_soft_reclaimed; | |
3208 | ||
1da177e4 | 3209 | /* |
1d82de61 MG |
3210 | * There should be no need to raise the scanning priority if |
3211 | * enough pages are already being scanned that that high | |
3212 | * watermark would be met at 100% efficiency. | |
1da177e4 | 3213 | */ |
970a39a3 | 3214 | if (kswapd_shrink_node(pgdat, &sc)) |
1d82de61 | 3215 | raise_priority = false; |
5515061d MG |
3216 | |
3217 | /* | |
3218 | * If the low watermark is met there is no need for processes | |
3219 | * to be throttled on pfmemalloc_wait as they should not be | |
3220 | * able to safely make forward progress. Wake them | |
3221 | */ | |
3222 | if (waitqueue_active(&pgdat->pfmemalloc_wait) && | |
3223 | pfmemalloc_watermark_ok(pgdat)) | |
cfc51155 | 3224 | wake_up_all(&pgdat->pfmemalloc_wait); |
5515061d | 3225 | |
b8e83b94 MG |
3226 | /* Check if kswapd should be suspending */ |
3227 | if (try_to_freeze() || kthread_should_stop()) | |
3228 | break; | |
8357376d | 3229 | |
73ce02e9 | 3230 | /* |
b8e83b94 MG |
3231 | * Raise priority if scanning rate is too low or there was no |
3232 | * progress in reclaiming pages | |
73ce02e9 | 3233 | */ |
b8e83b94 MG |
3234 | if (raise_priority || !sc.nr_reclaimed) |
3235 | sc.priority--; | |
1d82de61 | 3236 | } while (sc.priority >= 1); |
1da177e4 | 3237 | |
b8e83b94 | 3238 | out: |
0abdee2b | 3239 | /* |
1d82de61 MG |
3240 | * Return the order kswapd stopped reclaiming at as |
3241 | * prepare_kswapd_sleep() takes it into account. If another caller | |
3242 | * entered the allocator slow path while kswapd was awake, order will | |
3243 | * remain at the higher level. | |
0abdee2b | 3244 | */ |
1d82de61 | 3245 | return sc.order; |
1da177e4 LT |
3246 | } |
3247 | ||
38087d9b MG |
3248 | static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, |
3249 | unsigned int classzone_idx) | |
f0bc0a60 KM |
3250 | { |
3251 | long remaining = 0; | |
3252 | DEFINE_WAIT(wait); | |
3253 | ||
3254 | if (freezing(current) || kthread_should_stop()) | |
3255 | return; | |
3256 | ||
3257 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); | |
3258 | ||
3259 | /* Try to sleep for a short interval */ | |
d9f21d42 | 3260 | if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { |
fd901c95 VB |
3261 | /* |
3262 | * Compaction records what page blocks it recently failed to | |
3263 | * isolate pages from and skips them in the future scanning. | |
3264 | * When kswapd is going to sleep, it is reasonable to assume | |
3265 | * that pages and compaction may succeed so reset the cache. | |
3266 | */ | |
3267 | reset_isolation_suitable(pgdat); | |
3268 | ||
3269 | /* | |
3270 | * We have freed the memory, now we should compact it to make | |
3271 | * allocation of the requested order possible. | |
3272 | */ | |
38087d9b | 3273 | wakeup_kcompactd(pgdat, alloc_order, classzone_idx); |
fd901c95 | 3274 | |
f0bc0a60 | 3275 | remaining = schedule_timeout(HZ/10); |
38087d9b MG |
3276 | |
3277 | /* | |
3278 | * If woken prematurely then reset kswapd_classzone_idx and | |
3279 | * order. The values will either be from a wakeup request or | |
3280 | * the previous request that slept prematurely. | |
3281 | */ | |
3282 | if (remaining) { | |
3283 | pgdat->kswapd_classzone_idx = max(pgdat->kswapd_classzone_idx, classzone_idx); | |
3284 | pgdat->kswapd_order = max(pgdat->kswapd_order, reclaim_order); | |
3285 | } | |
3286 | ||
f0bc0a60 KM |
3287 | finish_wait(&pgdat->kswapd_wait, &wait); |
3288 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); | |
3289 | } | |
3290 | ||
3291 | /* | |
3292 | * After a short sleep, check if it was a premature sleep. If not, then | |
3293 | * go fully to sleep until explicitly woken up. | |
3294 | */ | |
d9f21d42 MG |
3295 | if (!remaining && |
3296 | prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { | |
f0bc0a60 KM |
3297 | trace_mm_vmscan_kswapd_sleep(pgdat->node_id); |
3298 | ||
3299 | /* | |
3300 | * vmstat counters are not perfectly accurate and the estimated | |
3301 | * value for counters such as NR_FREE_PAGES can deviate from the | |
3302 | * true value by nr_online_cpus * threshold. To avoid the zone | |
3303 | * watermarks being breached while under pressure, we reduce the | |
3304 | * per-cpu vmstat threshold while kswapd is awake and restore | |
3305 | * them before going back to sleep. | |
3306 | */ | |
3307 | set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); | |
1c7e7f6c AK |
3308 | |
3309 | if (!kthread_should_stop()) | |
3310 | schedule(); | |
3311 | ||
f0bc0a60 KM |
3312 | set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); |
3313 | } else { | |
3314 | if (remaining) | |
3315 | count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); | |
3316 | else | |
3317 | count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); | |
3318 | } | |
3319 | finish_wait(&pgdat->kswapd_wait, &wait); | |
3320 | } | |
3321 | ||
1da177e4 LT |
3322 | /* |
3323 | * The background pageout daemon, started as a kernel thread | |
4f98a2fe | 3324 | * from the init process. |
1da177e4 LT |
3325 | * |
3326 | * This basically trickles out pages so that we have _some_ | |
3327 | * free memory available even if there is no other activity | |
3328 | * that frees anything up. This is needed for things like routing | |
3329 | * etc, where we otherwise might have all activity going on in | |
3330 | * asynchronous contexts that cannot page things out. | |
3331 | * | |
3332 | * If there are applications that are active memory-allocators | |
3333 | * (most normal use), this basically shouldn't matter. | |
3334 | */ | |
3335 | static int kswapd(void *p) | |
3336 | { | |
38087d9b | 3337 | unsigned int alloc_order, reclaim_order, classzone_idx; |
1da177e4 LT |
3338 | pg_data_t *pgdat = (pg_data_t*)p; |
3339 | struct task_struct *tsk = current; | |
f0bc0a60 | 3340 | |
1da177e4 LT |
3341 | struct reclaim_state reclaim_state = { |
3342 | .reclaimed_slab = 0, | |
3343 | }; | |
a70f7302 | 3344 | const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); |
1da177e4 | 3345 | |
cf40bd16 NP |
3346 | lockdep_set_current_reclaim_state(GFP_KERNEL); |
3347 | ||
174596a0 | 3348 | if (!cpumask_empty(cpumask)) |
c5f59f08 | 3349 | set_cpus_allowed_ptr(tsk, cpumask); |
1da177e4 LT |
3350 | current->reclaim_state = &reclaim_state; |
3351 | ||
3352 | /* | |
3353 | * Tell the memory management that we're a "memory allocator", | |
3354 | * and that if we need more memory we should get access to it | |
3355 | * regardless (see "__alloc_pages()"). "kswapd" should | |
3356 | * never get caught in the normal page freeing logic. | |
3357 | * | |
3358 | * (Kswapd normally doesn't need memory anyway, but sometimes | |
3359 | * you need a small amount of memory in order to be able to | |
3360 | * page out something else, and this flag essentially protects | |
3361 | * us from recursively trying to free more memory as we're | |
3362 | * trying to free the first piece of memory in the first place). | |
3363 | */ | |
930d9152 | 3364 | tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; |
83144186 | 3365 | set_freezable(); |
1da177e4 | 3366 | |
38087d9b MG |
3367 | pgdat->kswapd_order = alloc_order = reclaim_order = 0; |
3368 | pgdat->kswapd_classzone_idx = classzone_idx = 0; | |
1da177e4 | 3369 | for ( ; ; ) { |
6f6313d4 | 3370 | bool ret; |
3e1d1d28 | 3371 | |
38087d9b MG |
3372 | kswapd_try_sleep: |
3373 | kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, | |
3374 | classzone_idx); | |
215ddd66 | 3375 | |
38087d9b MG |
3376 | /* Read the new order and classzone_idx */ |
3377 | alloc_order = reclaim_order = pgdat->kswapd_order; | |
3378 | classzone_idx = pgdat->kswapd_classzone_idx; | |
3379 | pgdat->kswapd_order = 0; | |
3380 | pgdat->kswapd_classzone_idx = 0; | |
1da177e4 | 3381 | |
8fe23e05 DR |
3382 | ret = try_to_freeze(); |
3383 | if (kthread_should_stop()) | |
3384 | break; | |
3385 | ||
3386 | /* | |
3387 | * We can speed up thawing tasks if we don't call balance_pgdat | |
3388 | * after returning from the refrigerator | |
3389 | */ | |
38087d9b MG |
3390 | if (ret) |
3391 | continue; | |
3392 | ||
3393 | /* | |
3394 | * Reclaim begins at the requested order but if a high-order | |
3395 | * reclaim fails then kswapd falls back to reclaiming for | |
3396 | * order-0. If that happens, kswapd will consider sleeping | |
3397 | * for the order it finished reclaiming at (reclaim_order) | |
3398 | * but kcompactd is woken to compact for the original | |
3399 | * request (alloc_order). | |
3400 | */ | |
e5146b12 MG |
3401 | trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx, |
3402 | alloc_order); | |
38087d9b MG |
3403 | reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx); |
3404 | if (reclaim_order < alloc_order) | |
3405 | goto kswapd_try_sleep; | |
1d82de61 | 3406 | |
38087d9b MG |
3407 | alloc_order = reclaim_order = pgdat->kswapd_order; |
3408 | classzone_idx = pgdat->kswapd_classzone_idx; | |
1da177e4 | 3409 | } |
b0a8cc58 | 3410 | |
71abdc15 | 3411 | tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); |
b0a8cc58 | 3412 | current->reclaim_state = NULL; |
71abdc15 JW |
3413 | lockdep_clear_current_reclaim_state(); |
3414 | ||
1da177e4 LT |
3415 | return 0; |
3416 | } | |
3417 | ||
3418 | /* | |
3419 | * A zone is low on free memory, so wake its kswapd task to service it. | |
3420 | */ | |
99504748 | 3421 | void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) |
1da177e4 LT |
3422 | { |
3423 | pg_data_t *pgdat; | |
e1a55637 | 3424 | int z; |
1da177e4 | 3425 | |
f3fe6512 | 3426 | if (!populated_zone(zone)) |
1da177e4 LT |
3427 | return; |
3428 | ||
344736f2 | 3429 | if (!cpuset_zone_allowed(zone, GFP_KERNEL | __GFP_HARDWALL)) |
1da177e4 | 3430 | return; |
88f5acf8 | 3431 | pgdat = zone->zone_pgdat; |
38087d9b MG |
3432 | pgdat->kswapd_classzone_idx = max(pgdat->kswapd_classzone_idx, classzone_idx); |
3433 | pgdat->kswapd_order = max(pgdat->kswapd_order, order); | |
8d0986e2 | 3434 | if (!waitqueue_active(&pgdat->kswapd_wait)) |
1da177e4 | 3435 | return; |
e1a55637 MG |
3436 | |
3437 | /* Only wake kswapd if all zones are unbalanced */ | |
3438 | for (z = 0; z <= classzone_idx; z++) { | |
3439 | zone = pgdat->node_zones + z; | |
3440 | if (!populated_zone(zone)) | |
3441 | continue; | |
3442 | ||
3443 | if (zone_balanced(zone, order, classzone_idx)) | |
3444 | return; | |
3445 | } | |
88f5acf8 MG |
3446 | |
3447 | trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order); | |
8d0986e2 | 3448 | wake_up_interruptible(&pgdat->kswapd_wait); |
1da177e4 LT |
3449 | } |
3450 | ||
c6f37f12 | 3451 | #ifdef CONFIG_HIBERNATION |
1da177e4 | 3452 | /* |
7b51755c | 3453 | * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of |
d6277db4 RW |
3454 | * freed pages. |
3455 | * | |
3456 | * Rather than trying to age LRUs the aim is to preserve the overall | |
3457 | * LRU order by reclaiming preferentially | |
3458 | * inactive > active > active referenced > active mapped | |
1da177e4 | 3459 | */ |
7b51755c | 3460 | unsigned long shrink_all_memory(unsigned long nr_to_reclaim) |
1da177e4 | 3461 | { |
d6277db4 | 3462 | struct reclaim_state reclaim_state; |
d6277db4 | 3463 | struct scan_control sc = { |
ee814fe2 | 3464 | .nr_to_reclaim = nr_to_reclaim, |
7b51755c | 3465 | .gfp_mask = GFP_HIGHUSER_MOVABLE, |
b2e18757 | 3466 | .reclaim_idx = MAX_NR_ZONES - 1, |
ee814fe2 | 3467 | .priority = DEF_PRIORITY, |
d6277db4 | 3468 | .may_writepage = 1, |
ee814fe2 JW |
3469 | .may_unmap = 1, |
3470 | .may_swap = 1, | |
7b51755c | 3471 | .hibernation_mode = 1, |
1da177e4 | 3472 | }; |
a09ed5e0 | 3473 | struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); |
7b51755c KM |
3474 | struct task_struct *p = current; |
3475 | unsigned long nr_reclaimed; | |
1da177e4 | 3476 | |
7b51755c KM |
3477 | p->flags |= PF_MEMALLOC; |
3478 | lockdep_set_current_reclaim_state(sc.gfp_mask); | |
3479 | reclaim_state.reclaimed_slab = 0; | |
3480 | p->reclaim_state = &reclaim_state; | |
d6277db4 | 3481 | |
3115cd91 | 3482 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
d979677c | 3483 | |
7b51755c KM |
3484 | p->reclaim_state = NULL; |
3485 | lockdep_clear_current_reclaim_state(); | |
3486 | p->flags &= ~PF_MEMALLOC; | |
d6277db4 | 3487 | |
7b51755c | 3488 | return nr_reclaimed; |
1da177e4 | 3489 | } |
c6f37f12 | 3490 | #endif /* CONFIG_HIBERNATION */ |
1da177e4 | 3491 | |
1da177e4 LT |
3492 | /* It's optimal to keep kswapds on the same CPUs as their memory, but |
3493 | not required for correctness. So if the last cpu in a node goes | |
3494 | away, we get changed to run anywhere: as the first one comes back, | |
3495 | restore their cpu bindings. */ | |
fcb35a9b GKH |
3496 | static int cpu_callback(struct notifier_block *nfb, unsigned long action, |
3497 | void *hcpu) | |
1da177e4 | 3498 | { |
58c0a4a7 | 3499 | int nid; |
1da177e4 | 3500 | |
8bb78442 | 3501 | if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { |
48fb2e24 | 3502 | for_each_node_state(nid, N_MEMORY) { |
c5f59f08 | 3503 | pg_data_t *pgdat = NODE_DATA(nid); |
a70f7302 RR |
3504 | const struct cpumask *mask; |
3505 | ||
3506 | mask = cpumask_of_node(pgdat->node_id); | |
c5f59f08 | 3507 | |
3e597945 | 3508 | if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) |
1da177e4 | 3509 | /* One of our CPUs online: restore mask */ |
c5f59f08 | 3510 | set_cpus_allowed_ptr(pgdat->kswapd, mask); |
1da177e4 LT |
3511 | } |
3512 | } | |
3513 | return NOTIFY_OK; | |
3514 | } | |
1da177e4 | 3515 | |
3218ae14 YG |
3516 | /* |
3517 | * This kswapd start function will be called by init and node-hot-add. | |
3518 | * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. | |
3519 | */ | |
3520 | int kswapd_run(int nid) | |
3521 | { | |
3522 | pg_data_t *pgdat = NODE_DATA(nid); | |
3523 | int ret = 0; | |
3524 | ||
3525 | if (pgdat->kswapd) | |
3526 | return 0; | |
3527 | ||
3528 | pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); | |
3529 | if (IS_ERR(pgdat->kswapd)) { | |
3530 | /* failure at boot is fatal */ | |
3531 | BUG_ON(system_state == SYSTEM_BOOTING); | |
d5dc0ad9 GS |
3532 | pr_err("Failed to start kswapd on node %d\n", nid); |
3533 | ret = PTR_ERR(pgdat->kswapd); | |
d72515b8 | 3534 | pgdat->kswapd = NULL; |
3218ae14 YG |
3535 | } |
3536 | return ret; | |
3537 | } | |
3538 | ||
8fe23e05 | 3539 | /* |
d8adde17 | 3540 | * Called by memory hotplug when all memory in a node is offlined. Caller must |
bfc8c901 | 3541 | * hold mem_hotplug_begin/end(). |
8fe23e05 DR |
3542 | */ |
3543 | void kswapd_stop(int nid) | |
3544 | { | |
3545 | struct task_struct *kswapd = NODE_DATA(nid)->kswapd; | |
3546 | ||
d8adde17 | 3547 | if (kswapd) { |
8fe23e05 | 3548 | kthread_stop(kswapd); |
d8adde17 JL |
3549 | NODE_DATA(nid)->kswapd = NULL; |
3550 | } | |
8fe23e05 DR |
3551 | } |
3552 | ||
1da177e4 LT |
3553 | static int __init kswapd_init(void) |
3554 | { | |
3218ae14 | 3555 | int nid; |
69e05944 | 3556 | |
1da177e4 | 3557 | swap_setup(); |
48fb2e24 | 3558 | for_each_node_state(nid, N_MEMORY) |
3218ae14 | 3559 | kswapd_run(nid); |
1da177e4 LT |
3560 | hotcpu_notifier(cpu_callback, 0); |
3561 | return 0; | |
3562 | } | |
3563 | ||
3564 | module_init(kswapd_init) | |
9eeff239 CL |
3565 | |
3566 | #ifdef CONFIG_NUMA | |
3567 | /* | |
a5f5f91d | 3568 | * Node reclaim mode |
9eeff239 | 3569 | * |
a5f5f91d | 3570 | * If non-zero call node_reclaim when the number of free pages falls below |
9eeff239 | 3571 | * the watermarks. |
9eeff239 | 3572 | */ |
a5f5f91d | 3573 | int node_reclaim_mode __read_mostly; |
9eeff239 | 3574 | |
1b2ffb78 | 3575 | #define RECLAIM_OFF 0 |
7d03431c | 3576 | #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ |
1b2ffb78 | 3577 | #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ |
95bbc0c7 | 3578 | #define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */ |
1b2ffb78 | 3579 | |
a92f7126 | 3580 | /* |
a5f5f91d | 3581 | * Priority for NODE_RECLAIM. This determines the fraction of pages |
a92f7126 CL |
3582 | * of a node considered for each zone_reclaim. 4 scans 1/16th of |
3583 | * a zone. | |
3584 | */ | |
a5f5f91d | 3585 | #define NODE_RECLAIM_PRIORITY 4 |
a92f7126 | 3586 | |
9614634f | 3587 | /* |
a5f5f91d | 3588 | * Percentage of pages in a zone that must be unmapped for node_reclaim to |
9614634f CL |
3589 | * occur. |
3590 | */ | |
3591 | int sysctl_min_unmapped_ratio = 1; | |
3592 | ||
0ff38490 CL |
3593 | /* |
3594 | * If the number of slab pages in a zone grows beyond this percentage then | |
3595 | * slab reclaim needs to occur. | |
3596 | */ | |
3597 | int sysctl_min_slab_ratio = 5; | |
3598 | ||
11fb9989 | 3599 | static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) |
90afa5de | 3600 | { |
11fb9989 MG |
3601 | unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); |
3602 | unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + | |
3603 | node_page_state(pgdat, NR_ACTIVE_FILE); | |
90afa5de MG |
3604 | |
3605 | /* | |
3606 | * It's possible for there to be more file mapped pages than | |
3607 | * accounted for by the pages on the file LRU lists because | |
3608 | * tmpfs pages accounted for as ANON can also be FILE_MAPPED | |
3609 | */ | |
3610 | return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; | |
3611 | } | |
3612 | ||
3613 | /* Work out how many page cache pages we can reclaim in this reclaim_mode */ | |
a5f5f91d | 3614 | static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) |
90afa5de | 3615 | { |
d031a157 AM |
3616 | unsigned long nr_pagecache_reclaimable; |
3617 | unsigned long delta = 0; | |
90afa5de MG |
3618 | |
3619 | /* | |
95bbc0c7 | 3620 | * If RECLAIM_UNMAP is set, then all file pages are considered |
90afa5de | 3621 | * potentially reclaimable. Otherwise, we have to worry about |
11fb9989 | 3622 | * pages like swapcache and node_unmapped_file_pages() provides |
90afa5de MG |
3623 | * a better estimate |
3624 | */ | |
a5f5f91d MG |
3625 | if (node_reclaim_mode & RECLAIM_UNMAP) |
3626 | nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); | |
90afa5de | 3627 | else |
a5f5f91d | 3628 | nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); |
90afa5de MG |
3629 | |
3630 | /* If we can't clean pages, remove dirty pages from consideration */ | |
a5f5f91d MG |
3631 | if (!(node_reclaim_mode & RECLAIM_WRITE)) |
3632 | delta += node_page_state(pgdat, NR_FILE_DIRTY); | |
90afa5de MG |
3633 | |
3634 | /* Watch for any possible underflows due to delta */ | |
3635 | if (unlikely(delta > nr_pagecache_reclaimable)) | |
3636 | delta = nr_pagecache_reclaimable; | |
3637 | ||
3638 | return nr_pagecache_reclaimable - delta; | |
3639 | } | |
3640 | ||
9eeff239 | 3641 | /* |
a5f5f91d | 3642 | * Try to free up some pages from this node through reclaim. |
9eeff239 | 3643 | */ |
a5f5f91d | 3644 | static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) |
9eeff239 | 3645 | { |
7fb2d46d | 3646 | /* Minimum pages needed in order to stay on node */ |
69e05944 | 3647 | const unsigned long nr_pages = 1 << order; |
9eeff239 CL |
3648 | struct task_struct *p = current; |
3649 | struct reclaim_state reclaim_state; | |
a5f5f91d | 3650 | int classzone_idx = gfp_zone(gfp_mask); |
179e9639 | 3651 | struct scan_control sc = { |
62b726c1 | 3652 | .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), |
21caf2fc | 3653 | .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), |
bd2f6199 | 3654 | .order = order, |
a5f5f91d MG |
3655 | .priority = NODE_RECLAIM_PRIORITY, |
3656 | .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), | |
3657 | .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), | |
ee814fe2 | 3658 | .may_swap = 1, |
a5f5f91d | 3659 | .reclaim_idx = classzone_idx, |
179e9639 | 3660 | }; |
9eeff239 | 3661 | |
9eeff239 | 3662 | cond_resched(); |
d4f7796e | 3663 | /* |
95bbc0c7 | 3664 | * We need to be able to allocate from the reserves for RECLAIM_UNMAP |
d4f7796e | 3665 | * and we also need to be able to write out pages for RECLAIM_WRITE |
95bbc0c7 | 3666 | * and RECLAIM_UNMAP. |
d4f7796e CL |
3667 | */ |
3668 | p->flags |= PF_MEMALLOC | PF_SWAPWRITE; | |
76ca542d | 3669 | lockdep_set_current_reclaim_state(gfp_mask); |
9eeff239 CL |
3670 | reclaim_state.reclaimed_slab = 0; |
3671 | p->reclaim_state = &reclaim_state; | |
c84db23c | 3672 | |
a5f5f91d | 3673 | if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { |
0ff38490 CL |
3674 | /* |
3675 | * Free memory by calling shrink zone with increasing | |
3676 | * priorities until we have enough memory freed. | |
3677 | */ | |
0ff38490 | 3678 | do { |
970a39a3 | 3679 | shrink_node(pgdat, &sc); |
9e3b2f8c | 3680 | } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); |
0ff38490 | 3681 | } |
c84db23c | 3682 | |
9eeff239 | 3683 | p->reclaim_state = NULL; |
d4f7796e | 3684 | current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); |
76ca542d | 3685 | lockdep_clear_current_reclaim_state(); |
a79311c1 | 3686 | return sc.nr_reclaimed >= nr_pages; |
9eeff239 | 3687 | } |
179e9639 | 3688 | |
a5f5f91d | 3689 | int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) |
179e9639 | 3690 | { |
d773ed6b | 3691 | int ret; |
179e9639 AM |
3692 | |
3693 | /* | |
a5f5f91d | 3694 | * Node reclaim reclaims unmapped file backed pages and |
0ff38490 | 3695 | * slab pages if we are over the defined limits. |
34aa1330 | 3696 | * |
9614634f CL |
3697 | * A small portion of unmapped file backed pages is needed for |
3698 | * file I/O otherwise pages read by file I/O will be immediately | |
a5f5f91d MG |
3699 | * thrown out if the node is overallocated. So we do not reclaim |
3700 | * if less than a specified percentage of the node is used by | |
9614634f | 3701 | * unmapped file backed pages. |
179e9639 | 3702 | */ |
a5f5f91d MG |
3703 | if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && |
3704 | sum_zone_node_page_state(pgdat->node_id, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages) | |
3705 | return NODE_RECLAIM_FULL; | |
179e9639 | 3706 | |
a5f5f91d MG |
3707 | if (!pgdat_reclaimable(pgdat)) |
3708 | return NODE_RECLAIM_FULL; | |
d773ed6b | 3709 | |
179e9639 | 3710 | /* |
d773ed6b | 3711 | * Do not scan if the allocation should not be delayed. |
179e9639 | 3712 | */ |
d0164adc | 3713 | if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) |
a5f5f91d | 3714 | return NODE_RECLAIM_NOSCAN; |
179e9639 AM |
3715 | |
3716 | /* | |
a5f5f91d | 3717 | * Only run node reclaim on the local node or on nodes that do not |
179e9639 AM |
3718 | * have associated processors. This will favor the local processor |
3719 | * over remote processors and spread off node memory allocations | |
3720 | * as wide as possible. | |
3721 | */ | |
a5f5f91d MG |
3722 | if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) |
3723 | return NODE_RECLAIM_NOSCAN; | |
d773ed6b | 3724 | |
a5f5f91d MG |
3725 | if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) |
3726 | return NODE_RECLAIM_NOSCAN; | |
fa5e084e | 3727 | |
a5f5f91d MG |
3728 | ret = __node_reclaim(pgdat, gfp_mask, order); |
3729 | clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); | |
d773ed6b | 3730 | |
24cf7251 MG |
3731 | if (!ret) |
3732 | count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); | |
3733 | ||
d773ed6b | 3734 | return ret; |
179e9639 | 3735 | } |
9eeff239 | 3736 | #endif |
894bc310 | 3737 | |
894bc310 LS |
3738 | /* |
3739 | * page_evictable - test whether a page is evictable | |
3740 | * @page: the page to test | |
894bc310 LS |
3741 | * |
3742 | * Test whether page is evictable--i.e., should be placed on active/inactive | |
39b5f29a | 3743 | * lists vs unevictable list. |
894bc310 LS |
3744 | * |
3745 | * Reasons page might not be evictable: | |
ba9ddf49 | 3746 | * (1) page's mapping marked unevictable |
b291f000 | 3747 | * (2) page is part of an mlocked VMA |
ba9ddf49 | 3748 | * |
894bc310 | 3749 | */ |
39b5f29a | 3750 | int page_evictable(struct page *page) |
894bc310 | 3751 | { |
39b5f29a | 3752 | return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); |
894bc310 | 3753 | } |
89e004ea | 3754 | |
85046579 | 3755 | #ifdef CONFIG_SHMEM |
89e004ea | 3756 | /** |
24513264 HD |
3757 | * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list |
3758 | * @pages: array of pages to check | |
3759 | * @nr_pages: number of pages to check | |
89e004ea | 3760 | * |
24513264 | 3761 | * Checks pages for evictability and moves them to the appropriate lru list. |
85046579 HD |
3762 | * |
3763 | * This function is only used for SysV IPC SHM_UNLOCK. | |
89e004ea | 3764 | */ |
24513264 | 3765 | void check_move_unevictable_pages(struct page **pages, int nr_pages) |
89e004ea | 3766 | { |
925b7673 | 3767 | struct lruvec *lruvec; |
785b99fe | 3768 | struct pglist_data *pgdat = NULL; |
24513264 HD |
3769 | int pgscanned = 0; |
3770 | int pgrescued = 0; | |
3771 | int i; | |
89e004ea | 3772 | |
24513264 HD |
3773 | for (i = 0; i < nr_pages; i++) { |
3774 | struct page *page = pages[i]; | |
785b99fe | 3775 | struct pglist_data *pagepgdat = page_pgdat(page); |
89e004ea | 3776 | |
24513264 | 3777 | pgscanned++; |
785b99fe MG |
3778 | if (pagepgdat != pgdat) { |
3779 | if (pgdat) | |
3780 | spin_unlock_irq(&pgdat->lru_lock); | |
3781 | pgdat = pagepgdat; | |
3782 | spin_lock_irq(&pgdat->lru_lock); | |
24513264 | 3783 | } |
785b99fe | 3784 | lruvec = mem_cgroup_page_lruvec(page, pgdat); |
89e004ea | 3785 | |
24513264 HD |
3786 | if (!PageLRU(page) || !PageUnevictable(page)) |
3787 | continue; | |
89e004ea | 3788 | |
39b5f29a | 3789 | if (page_evictable(page)) { |
24513264 HD |
3790 | enum lru_list lru = page_lru_base_type(page); |
3791 | ||
309381fe | 3792 | VM_BUG_ON_PAGE(PageActive(page), page); |
24513264 | 3793 | ClearPageUnevictable(page); |
fa9add64 HD |
3794 | del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); |
3795 | add_page_to_lru_list(page, lruvec, lru); | |
24513264 | 3796 | pgrescued++; |
89e004ea | 3797 | } |
24513264 | 3798 | } |
89e004ea | 3799 | |
785b99fe | 3800 | if (pgdat) { |
24513264 HD |
3801 | __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); |
3802 | __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); | |
785b99fe | 3803 | spin_unlock_irq(&pgdat->lru_lock); |
89e004ea | 3804 | } |
89e004ea | 3805 | } |
85046579 | 3806 | #endif /* CONFIG_SHMEM */ |