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