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> | |
e129b5c2 | 22 | #include <linux/vmstat.h> |
1da177e4 LT |
23 | #include <linux/file.h> |
24 | #include <linux/writeback.h> | |
25 | #include <linux/blkdev.h> | |
26 | #include <linux/buffer_head.h> /* for try_to_release_page(), | |
27 | buffer_heads_over_limit */ | |
28 | #include <linux/mm_inline.h> | |
29 | #include <linux/pagevec.h> | |
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> | |
35 | #include <linux/notifier.h> | |
36 | #include <linux/rwsem.h> | |
248a0301 | 37 | #include <linux/delay.h> |
3218ae14 | 38 | #include <linux/kthread.h> |
7dfb7103 | 39 | #include <linux/freezer.h> |
66e1707b | 40 | #include <linux/memcontrol.h> |
873b4771 | 41 | #include <linux/delayacct.h> |
af936a16 | 42 | #include <linux/sysctl.h> |
1da177e4 LT |
43 | |
44 | #include <asm/tlbflush.h> | |
45 | #include <asm/div64.h> | |
46 | ||
47 | #include <linux/swapops.h> | |
48 | ||
0f8053a5 NP |
49 | #include "internal.h" |
50 | ||
33906bc5 MG |
51 | #define CREATE_TRACE_POINTS |
52 | #include <trace/events/vmscan.h> | |
53 | ||
7d3579e8 KM |
54 | enum lumpy_mode { |
55 | LUMPY_MODE_NONE, | |
56 | LUMPY_MODE_ASYNC, | |
57 | LUMPY_MODE_SYNC, | |
58 | }; | |
59 | ||
1da177e4 | 60 | struct scan_control { |
1da177e4 LT |
61 | /* Incremented by the number of inactive pages that were scanned */ |
62 | unsigned long nr_scanned; | |
63 | ||
a79311c1 RR |
64 | /* Number of pages freed so far during a call to shrink_zones() */ |
65 | unsigned long nr_reclaimed; | |
66 | ||
22fba335 KM |
67 | /* How many pages shrink_list() should reclaim */ |
68 | unsigned long nr_to_reclaim; | |
69 | ||
7b51755c KM |
70 | unsigned long hibernation_mode; |
71 | ||
1da177e4 | 72 | /* This context's GFP mask */ |
6daa0e28 | 73 | gfp_t gfp_mask; |
1da177e4 LT |
74 | |
75 | int may_writepage; | |
76 | ||
a6dc60f8 JW |
77 | /* Can mapped pages be reclaimed? */ |
78 | int may_unmap; | |
f1fd1067 | 79 | |
2e2e4259 KM |
80 | /* Can pages be swapped as part of reclaim? */ |
81 | int may_swap; | |
82 | ||
d6277db4 | 83 | int swappiness; |
408d8544 | 84 | |
5ad333eb | 85 | int order; |
66e1707b | 86 | |
5f53e762 | 87 | /* |
415b54e3 NK |
88 | * Intend to reclaim enough continuous memory rather than reclaim |
89 | * enough amount of memory. i.e, mode for high order allocation. | |
5f53e762 | 90 | */ |
7d3579e8 | 91 | enum lumpy_mode lumpy_reclaim_mode; |
5f53e762 | 92 | |
66e1707b BS |
93 | /* Which cgroup do we reclaim from */ |
94 | struct mem_cgroup *mem_cgroup; | |
95 | ||
327c0e96 KH |
96 | /* |
97 | * Nodemask of nodes allowed by the caller. If NULL, all nodes | |
98 | * are scanned. | |
99 | */ | |
100 | nodemask_t *nodemask; | |
1da177e4 LT |
101 | }; |
102 | ||
1da177e4 LT |
103 | #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) |
104 | ||
105 | #ifdef ARCH_HAS_PREFETCH | |
106 | #define prefetch_prev_lru_page(_page, _base, _field) \ | |
107 | do { \ | |
108 | if ((_page)->lru.prev != _base) { \ | |
109 | struct page *prev; \ | |
110 | \ | |
111 | prev = lru_to_page(&(_page->lru)); \ | |
112 | prefetch(&prev->_field); \ | |
113 | } \ | |
114 | } while (0) | |
115 | #else | |
116 | #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) | |
117 | #endif | |
118 | ||
119 | #ifdef ARCH_HAS_PREFETCHW | |
120 | #define prefetchw_prev_lru_page(_page, _base, _field) \ | |
121 | do { \ | |
122 | if ((_page)->lru.prev != _base) { \ | |
123 | struct page *prev; \ | |
124 | \ | |
125 | prev = lru_to_page(&(_page->lru)); \ | |
126 | prefetchw(&prev->_field); \ | |
127 | } \ | |
128 | } while (0) | |
129 | #else | |
130 | #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) | |
131 | #endif | |
132 | ||
133 | /* | |
134 | * From 0 .. 100. Higher means more swappy. | |
135 | */ | |
136 | int vm_swappiness = 60; | |
bd1e22b8 | 137 | long vm_total_pages; /* The total number of pages which the VM controls */ |
1da177e4 LT |
138 | |
139 | static LIST_HEAD(shrinker_list); | |
140 | static DECLARE_RWSEM(shrinker_rwsem); | |
141 | ||
00f0b825 | 142 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR |
e72e2bd6 | 143 | #define scanning_global_lru(sc) (!(sc)->mem_cgroup) |
91a45470 | 144 | #else |
e72e2bd6 | 145 | #define scanning_global_lru(sc) (1) |
91a45470 KH |
146 | #endif |
147 | ||
6e901571 KM |
148 | static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone, |
149 | struct scan_control *sc) | |
150 | { | |
e72e2bd6 | 151 | if (!scanning_global_lru(sc)) |
3e2f41f1 KM |
152 | return mem_cgroup_get_reclaim_stat(sc->mem_cgroup, zone); |
153 | ||
6e901571 KM |
154 | return &zone->reclaim_stat; |
155 | } | |
156 | ||
0b217676 VL |
157 | static unsigned long zone_nr_lru_pages(struct zone *zone, |
158 | struct scan_control *sc, enum lru_list lru) | |
c9f299d9 | 159 | { |
e72e2bd6 | 160 | if (!scanning_global_lru(sc)) |
a3d8e054 KM |
161 | return mem_cgroup_zone_nr_pages(sc->mem_cgroup, zone, lru); |
162 | ||
c9f299d9 KM |
163 | return zone_page_state(zone, NR_LRU_BASE + lru); |
164 | } | |
165 | ||
166 | ||
1da177e4 LT |
167 | /* |
168 | * Add a shrinker callback to be called from the vm | |
169 | */ | |
8e1f936b | 170 | void register_shrinker(struct shrinker *shrinker) |
1da177e4 | 171 | { |
8e1f936b RR |
172 | shrinker->nr = 0; |
173 | down_write(&shrinker_rwsem); | |
174 | list_add_tail(&shrinker->list, &shrinker_list); | |
175 | up_write(&shrinker_rwsem); | |
1da177e4 | 176 | } |
8e1f936b | 177 | EXPORT_SYMBOL(register_shrinker); |
1da177e4 LT |
178 | |
179 | /* | |
180 | * Remove one | |
181 | */ | |
8e1f936b | 182 | void unregister_shrinker(struct shrinker *shrinker) |
1da177e4 LT |
183 | { |
184 | down_write(&shrinker_rwsem); | |
185 | list_del(&shrinker->list); | |
186 | up_write(&shrinker_rwsem); | |
1da177e4 | 187 | } |
8e1f936b | 188 | EXPORT_SYMBOL(unregister_shrinker); |
1da177e4 LT |
189 | |
190 | #define SHRINK_BATCH 128 | |
191 | /* | |
192 | * Call the shrink functions to age shrinkable caches | |
193 | * | |
194 | * Here we assume it costs one seek to replace a lru page and that it also | |
195 | * takes a seek to recreate a cache object. With this in mind we age equal | |
196 | * percentages of the lru and ageable caches. This should balance the seeks | |
197 | * generated by these structures. | |
198 | * | |
183ff22b | 199 | * If the vm encountered mapped pages on the LRU it increase the pressure on |
1da177e4 LT |
200 | * slab to avoid swapping. |
201 | * | |
202 | * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. | |
203 | * | |
204 | * `lru_pages' represents the number of on-LRU pages in all the zones which | |
205 | * are eligible for the caller's allocation attempt. It is used for balancing | |
206 | * slab reclaim versus page reclaim. | |
b15e0905 | 207 | * |
208 | * Returns the number of slab objects which we shrunk. | |
1da177e4 | 209 | */ |
69e05944 AM |
210 | unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask, |
211 | unsigned long lru_pages) | |
1da177e4 LT |
212 | { |
213 | struct shrinker *shrinker; | |
69e05944 | 214 | unsigned long ret = 0; |
1da177e4 LT |
215 | |
216 | if (scanned == 0) | |
217 | scanned = SWAP_CLUSTER_MAX; | |
218 | ||
219 | if (!down_read_trylock(&shrinker_rwsem)) | |
b15e0905 | 220 | return 1; /* Assume we'll be able to shrink next time */ |
1da177e4 LT |
221 | |
222 | list_for_each_entry(shrinker, &shrinker_list, list) { | |
223 | unsigned long long delta; | |
224 | unsigned long total_scan; | |
7f8275d0 | 225 | unsigned long max_pass; |
1da177e4 | 226 | |
7f8275d0 | 227 | max_pass = (*shrinker->shrink)(shrinker, 0, gfp_mask); |
1da177e4 | 228 | delta = (4 * scanned) / shrinker->seeks; |
ea164d73 | 229 | delta *= max_pass; |
1da177e4 LT |
230 | do_div(delta, lru_pages + 1); |
231 | shrinker->nr += delta; | |
ea164d73 | 232 | if (shrinker->nr < 0) { |
88c3bd70 DR |
233 | printk(KERN_ERR "shrink_slab: %pF negative objects to " |
234 | "delete nr=%ld\n", | |
235 | shrinker->shrink, shrinker->nr); | |
ea164d73 AA |
236 | shrinker->nr = max_pass; |
237 | } | |
238 | ||
239 | /* | |
240 | * Avoid risking looping forever due to too large nr value: | |
241 | * never try to free more than twice the estimate number of | |
242 | * freeable entries. | |
243 | */ | |
244 | if (shrinker->nr > max_pass * 2) | |
245 | shrinker->nr = max_pass * 2; | |
1da177e4 LT |
246 | |
247 | total_scan = shrinker->nr; | |
248 | shrinker->nr = 0; | |
249 | ||
250 | while (total_scan >= SHRINK_BATCH) { | |
251 | long this_scan = SHRINK_BATCH; | |
252 | int shrink_ret; | |
b15e0905 | 253 | int nr_before; |
1da177e4 | 254 | |
7f8275d0 DC |
255 | nr_before = (*shrinker->shrink)(shrinker, 0, gfp_mask); |
256 | shrink_ret = (*shrinker->shrink)(shrinker, this_scan, | |
257 | gfp_mask); | |
1da177e4 LT |
258 | if (shrink_ret == -1) |
259 | break; | |
b15e0905 | 260 | if (shrink_ret < nr_before) |
261 | ret += nr_before - shrink_ret; | |
f8891e5e | 262 | count_vm_events(SLABS_SCANNED, this_scan); |
1da177e4 LT |
263 | total_scan -= this_scan; |
264 | ||
265 | cond_resched(); | |
266 | } | |
267 | ||
268 | shrinker->nr += total_scan; | |
269 | } | |
270 | up_read(&shrinker_rwsem); | |
b15e0905 | 271 | return ret; |
1da177e4 LT |
272 | } |
273 | ||
7d3579e8 KM |
274 | static void set_lumpy_reclaim_mode(int priority, struct scan_control *sc, |
275 | bool sync) | |
276 | { | |
277 | enum lumpy_mode mode = sync ? LUMPY_MODE_SYNC : LUMPY_MODE_ASYNC; | |
278 | ||
279 | /* | |
280 | * Some reclaim have alredy been failed. No worth to try synchronous | |
281 | * lumpy reclaim. | |
282 | */ | |
283 | if (sync && sc->lumpy_reclaim_mode == LUMPY_MODE_NONE) | |
284 | return; | |
285 | ||
286 | /* | |
287 | * If we need a large contiguous chunk of memory, or have | |
288 | * trouble getting a small set of contiguous pages, we | |
289 | * will reclaim both active and inactive pages. | |
290 | */ | |
291 | if (sc->order > PAGE_ALLOC_COSTLY_ORDER) | |
292 | sc->lumpy_reclaim_mode = mode; | |
293 | else if (sc->order && priority < DEF_PRIORITY - 2) | |
294 | sc->lumpy_reclaim_mode = mode; | |
295 | else | |
296 | sc->lumpy_reclaim_mode = LUMPY_MODE_NONE; | |
297 | } | |
298 | ||
299 | static void disable_lumpy_reclaim_mode(struct scan_control *sc) | |
300 | { | |
301 | sc->lumpy_reclaim_mode = LUMPY_MODE_NONE; | |
302 | } | |
303 | ||
1da177e4 LT |
304 | static inline int is_page_cache_freeable(struct page *page) |
305 | { | |
ceddc3a5 JW |
306 | /* |
307 | * A freeable page cache page is referenced only by the caller | |
308 | * that isolated the page, the page cache radix tree and | |
309 | * optional buffer heads at page->private. | |
310 | */ | |
edcf4748 | 311 | return page_count(page) - page_has_private(page) == 2; |
1da177e4 LT |
312 | } |
313 | ||
7d3579e8 KM |
314 | static int may_write_to_queue(struct backing_dev_info *bdi, |
315 | struct scan_control *sc) | |
1da177e4 | 316 | { |
930d9152 | 317 | if (current->flags & PF_SWAPWRITE) |
1da177e4 LT |
318 | return 1; |
319 | if (!bdi_write_congested(bdi)) | |
320 | return 1; | |
321 | if (bdi == current->backing_dev_info) | |
322 | return 1; | |
7d3579e8 KM |
323 | |
324 | /* lumpy reclaim for hugepage often need a lot of write */ | |
325 | if (sc->order > PAGE_ALLOC_COSTLY_ORDER) | |
326 | return 1; | |
1da177e4 LT |
327 | return 0; |
328 | } | |
329 | ||
330 | /* | |
331 | * We detected a synchronous write error writing a page out. Probably | |
332 | * -ENOSPC. We need to propagate that into the address_space for a subsequent | |
333 | * fsync(), msync() or close(). | |
334 | * | |
335 | * The tricky part is that after writepage we cannot touch the mapping: nothing | |
336 | * prevents it from being freed up. But we have a ref on the page and once | |
337 | * that page is locked, the mapping is pinned. | |
338 | * | |
339 | * We're allowed to run sleeping lock_page() here because we know the caller has | |
340 | * __GFP_FS. | |
341 | */ | |
342 | static void handle_write_error(struct address_space *mapping, | |
343 | struct page *page, int error) | |
344 | { | |
a6aa62a0 | 345 | lock_page_nosync(page); |
3e9f45bd GC |
346 | if (page_mapping(page) == mapping) |
347 | mapping_set_error(mapping, error); | |
1da177e4 LT |
348 | unlock_page(page); |
349 | } | |
350 | ||
04e62a29 CL |
351 | /* possible outcome of pageout() */ |
352 | typedef enum { | |
353 | /* failed to write page out, page is locked */ | |
354 | PAGE_KEEP, | |
355 | /* move page to the active list, page is locked */ | |
356 | PAGE_ACTIVATE, | |
357 | /* page has been sent to the disk successfully, page is unlocked */ | |
358 | PAGE_SUCCESS, | |
359 | /* page is clean and locked */ | |
360 | PAGE_CLEAN, | |
361 | } pageout_t; | |
362 | ||
1da177e4 | 363 | /* |
1742f19f AM |
364 | * pageout is called by shrink_page_list() for each dirty page. |
365 | * Calls ->writepage(). | |
1da177e4 | 366 | */ |
c661b078 | 367 | static pageout_t pageout(struct page *page, struct address_space *mapping, |
7d3579e8 | 368 | struct scan_control *sc) |
1da177e4 LT |
369 | { |
370 | /* | |
371 | * If the page is dirty, only perform writeback if that write | |
372 | * will be non-blocking. To prevent this allocation from being | |
373 | * stalled by pagecache activity. But note that there may be | |
374 | * stalls if we need to run get_block(). We could test | |
375 | * PagePrivate for that. | |
376 | * | |
6aceb53b | 377 | * If this process is currently in __generic_file_aio_write() against |
1da177e4 LT |
378 | * this page's queue, we can perform writeback even if that |
379 | * will block. | |
380 | * | |
381 | * If the page is swapcache, write it back even if that would | |
382 | * block, for some throttling. This happens by accident, because | |
383 | * swap_backing_dev_info is bust: it doesn't reflect the | |
384 | * congestion state of the swapdevs. Easy to fix, if needed. | |
1da177e4 LT |
385 | */ |
386 | if (!is_page_cache_freeable(page)) | |
387 | return PAGE_KEEP; | |
388 | if (!mapping) { | |
389 | /* | |
390 | * Some data journaling orphaned pages can have | |
391 | * page->mapping == NULL while being dirty with clean buffers. | |
392 | */ | |
266cf658 | 393 | if (page_has_private(page)) { |
1da177e4 LT |
394 | if (try_to_free_buffers(page)) { |
395 | ClearPageDirty(page); | |
d40cee24 | 396 | printk("%s: orphaned page\n", __func__); |
1da177e4 LT |
397 | return PAGE_CLEAN; |
398 | } | |
399 | } | |
400 | return PAGE_KEEP; | |
401 | } | |
402 | if (mapping->a_ops->writepage == NULL) | |
403 | return PAGE_ACTIVATE; | |
0e093d99 | 404 | if (!may_write_to_queue(mapping->backing_dev_info, sc)) |
1da177e4 LT |
405 | return PAGE_KEEP; |
406 | ||
407 | if (clear_page_dirty_for_io(page)) { | |
408 | int res; | |
409 | struct writeback_control wbc = { | |
410 | .sync_mode = WB_SYNC_NONE, | |
411 | .nr_to_write = SWAP_CLUSTER_MAX, | |
111ebb6e OH |
412 | .range_start = 0, |
413 | .range_end = LLONG_MAX, | |
1da177e4 LT |
414 | .for_reclaim = 1, |
415 | }; | |
416 | ||
417 | SetPageReclaim(page); | |
418 | res = mapping->a_ops->writepage(page, &wbc); | |
419 | if (res < 0) | |
420 | handle_write_error(mapping, page, res); | |
994fc28c | 421 | if (res == AOP_WRITEPAGE_ACTIVATE) { |
1da177e4 LT |
422 | ClearPageReclaim(page); |
423 | return PAGE_ACTIVATE; | |
424 | } | |
c661b078 AW |
425 | |
426 | /* | |
427 | * Wait on writeback if requested to. This happens when | |
428 | * direct reclaiming a large contiguous area and the | |
429 | * first attempt to free a range of pages fails. | |
430 | */ | |
7d3579e8 KM |
431 | if (PageWriteback(page) && |
432 | sc->lumpy_reclaim_mode == LUMPY_MODE_SYNC) | |
c661b078 AW |
433 | wait_on_page_writeback(page); |
434 | ||
1da177e4 LT |
435 | if (!PageWriteback(page)) { |
436 | /* synchronous write or broken a_ops? */ | |
437 | ClearPageReclaim(page); | |
438 | } | |
755f0225 | 439 | trace_mm_vmscan_writepage(page, |
7d3579e8 | 440 | trace_reclaim_flags(page, sc->lumpy_reclaim_mode)); |
e129b5c2 | 441 | inc_zone_page_state(page, NR_VMSCAN_WRITE); |
1da177e4 LT |
442 | return PAGE_SUCCESS; |
443 | } | |
444 | ||
445 | return PAGE_CLEAN; | |
446 | } | |
447 | ||
a649fd92 | 448 | /* |
e286781d NP |
449 | * Same as remove_mapping, but if the page is removed from the mapping, it |
450 | * gets returned with a refcount of 0. | |
a649fd92 | 451 | */ |
e286781d | 452 | static int __remove_mapping(struct address_space *mapping, struct page *page) |
49d2e9cc | 453 | { |
28e4d965 NP |
454 | BUG_ON(!PageLocked(page)); |
455 | BUG_ON(mapping != page_mapping(page)); | |
49d2e9cc | 456 | |
19fd6231 | 457 | spin_lock_irq(&mapping->tree_lock); |
49d2e9cc | 458 | /* |
0fd0e6b0 NP |
459 | * The non racy check for a busy page. |
460 | * | |
461 | * Must be careful with the order of the tests. When someone has | |
462 | * a ref to the page, it may be possible that they dirty it then | |
463 | * drop the reference. So if PageDirty is tested before page_count | |
464 | * here, then the following race may occur: | |
465 | * | |
466 | * get_user_pages(&page); | |
467 | * [user mapping goes away] | |
468 | * write_to(page); | |
469 | * !PageDirty(page) [good] | |
470 | * SetPageDirty(page); | |
471 | * put_page(page); | |
472 | * !page_count(page) [good, discard it] | |
473 | * | |
474 | * [oops, our write_to data is lost] | |
475 | * | |
476 | * Reversing the order of the tests ensures such a situation cannot | |
477 | * escape unnoticed. The smp_rmb is needed to ensure the page->flags | |
478 | * load is not satisfied before that of page->_count. | |
479 | * | |
480 | * Note that if SetPageDirty is always performed via set_page_dirty, | |
481 | * and thus under tree_lock, then this ordering is not required. | |
49d2e9cc | 482 | */ |
e286781d | 483 | if (!page_freeze_refs(page, 2)) |
49d2e9cc | 484 | goto cannot_free; |
e286781d NP |
485 | /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ |
486 | if (unlikely(PageDirty(page))) { | |
487 | page_unfreeze_refs(page, 2); | |
49d2e9cc | 488 | goto cannot_free; |
e286781d | 489 | } |
49d2e9cc CL |
490 | |
491 | if (PageSwapCache(page)) { | |
492 | swp_entry_t swap = { .val = page_private(page) }; | |
493 | __delete_from_swap_cache(page); | |
19fd6231 | 494 | spin_unlock_irq(&mapping->tree_lock); |
cb4b86ba | 495 | swapcache_free(swap, page); |
e286781d | 496 | } else { |
6072d13c LT |
497 | void (*freepage)(struct page *); |
498 | ||
499 | freepage = mapping->a_ops->freepage; | |
500 | ||
e286781d | 501 | __remove_from_page_cache(page); |
19fd6231 | 502 | spin_unlock_irq(&mapping->tree_lock); |
e767e056 | 503 | mem_cgroup_uncharge_cache_page(page); |
6072d13c LT |
504 | |
505 | if (freepage != NULL) | |
506 | freepage(page); | |
49d2e9cc CL |
507 | } |
508 | ||
49d2e9cc CL |
509 | return 1; |
510 | ||
511 | cannot_free: | |
19fd6231 | 512 | spin_unlock_irq(&mapping->tree_lock); |
49d2e9cc CL |
513 | return 0; |
514 | } | |
515 | ||
e286781d NP |
516 | /* |
517 | * Attempt to detach a locked page from its ->mapping. If it is dirty or if | |
518 | * someone else has a ref on the page, abort and return 0. If it was | |
519 | * successfully detached, return 1. Assumes the caller has a single ref on | |
520 | * this page. | |
521 | */ | |
522 | int remove_mapping(struct address_space *mapping, struct page *page) | |
523 | { | |
524 | if (__remove_mapping(mapping, page)) { | |
525 | /* | |
526 | * Unfreezing the refcount with 1 rather than 2 effectively | |
527 | * drops the pagecache ref for us without requiring another | |
528 | * atomic operation. | |
529 | */ | |
530 | page_unfreeze_refs(page, 1); | |
531 | return 1; | |
532 | } | |
533 | return 0; | |
534 | } | |
535 | ||
894bc310 LS |
536 | /** |
537 | * putback_lru_page - put previously isolated page onto appropriate LRU list | |
538 | * @page: page to be put back to appropriate lru list | |
539 | * | |
540 | * Add previously isolated @page to appropriate LRU list. | |
541 | * Page may still be unevictable for other reasons. | |
542 | * | |
543 | * lru_lock must not be held, interrupts must be enabled. | |
544 | */ | |
894bc310 LS |
545 | void putback_lru_page(struct page *page) |
546 | { | |
547 | int lru; | |
548 | int active = !!TestClearPageActive(page); | |
bbfd28ee | 549 | int was_unevictable = PageUnevictable(page); |
894bc310 LS |
550 | |
551 | VM_BUG_ON(PageLRU(page)); | |
552 | ||
553 | redo: | |
554 | ClearPageUnevictable(page); | |
555 | ||
556 | if (page_evictable(page, NULL)) { | |
557 | /* | |
558 | * For evictable pages, we can use the cache. | |
559 | * In event of a race, worst case is we end up with an | |
560 | * unevictable page on [in]active list. | |
561 | * We know how to handle that. | |
562 | */ | |
401a8e1c | 563 | lru = active + page_lru_base_type(page); |
894bc310 LS |
564 | lru_cache_add_lru(page, lru); |
565 | } else { | |
566 | /* | |
567 | * Put unevictable pages directly on zone's unevictable | |
568 | * list. | |
569 | */ | |
570 | lru = LRU_UNEVICTABLE; | |
571 | add_page_to_unevictable_list(page); | |
6a7b9548 JW |
572 | /* |
573 | * When racing with an mlock clearing (page is | |
574 | * unlocked), make sure that if the other thread does | |
575 | * not observe our setting of PG_lru and fails | |
576 | * isolation, we see PG_mlocked cleared below and move | |
577 | * the page back to the evictable list. | |
578 | * | |
579 | * The other side is TestClearPageMlocked(). | |
580 | */ | |
581 | smp_mb(); | |
894bc310 | 582 | } |
894bc310 LS |
583 | |
584 | /* | |
585 | * page's status can change while we move it among lru. If an evictable | |
586 | * page is on unevictable list, it never be freed. To avoid that, | |
587 | * check after we added it to the list, again. | |
588 | */ | |
589 | if (lru == LRU_UNEVICTABLE && page_evictable(page, NULL)) { | |
590 | if (!isolate_lru_page(page)) { | |
591 | put_page(page); | |
592 | goto redo; | |
593 | } | |
594 | /* This means someone else dropped this page from LRU | |
595 | * So, it will be freed or putback to LRU again. There is | |
596 | * nothing to do here. | |
597 | */ | |
598 | } | |
599 | ||
bbfd28ee LS |
600 | if (was_unevictable && lru != LRU_UNEVICTABLE) |
601 | count_vm_event(UNEVICTABLE_PGRESCUED); | |
602 | else if (!was_unevictable && lru == LRU_UNEVICTABLE) | |
603 | count_vm_event(UNEVICTABLE_PGCULLED); | |
604 | ||
894bc310 LS |
605 | put_page(page); /* drop ref from isolate */ |
606 | } | |
607 | ||
dfc8d636 JW |
608 | enum page_references { |
609 | PAGEREF_RECLAIM, | |
610 | PAGEREF_RECLAIM_CLEAN, | |
64574746 | 611 | PAGEREF_KEEP, |
dfc8d636 JW |
612 | PAGEREF_ACTIVATE, |
613 | }; | |
614 | ||
615 | static enum page_references page_check_references(struct page *page, | |
616 | struct scan_control *sc) | |
617 | { | |
64574746 | 618 | int referenced_ptes, referenced_page; |
dfc8d636 | 619 | unsigned long vm_flags; |
dfc8d636 | 620 | |
64574746 JW |
621 | referenced_ptes = page_referenced(page, 1, sc->mem_cgroup, &vm_flags); |
622 | referenced_page = TestClearPageReferenced(page); | |
dfc8d636 JW |
623 | |
624 | /* Lumpy reclaim - ignore references */ | |
7d3579e8 | 625 | if (sc->lumpy_reclaim_mode != LUMPY_MODE_NONE) |
dfc8d636 JW |
626 | return PAGEREF_RECLAIM; |
627 | ||
628 | /* | |
629 | * Mlock lost the isolation race with us. Let try_to_unmap() | |
630 | * move the page to the unevictable list. | |
631 | */ | |
632 | if (vm_flags & VM_LOCKED) | |
633 | return PAGEREF_RECLAIM; | |
634 | ||
64574746 JW |
635 | if (referenced_ptes) { |
636 | if (PageAnon(page)) | |
637 | return PAGEREF_ACTIVATE; | |
638 | /* | |
639 | * All mapped pages start out with page table | |
640 | * references from the instantiating fault, so we need | |
641 | * to look twice if a mapped file page is used more | |
642 | * than once. | |
643 | * | |
644 | * Mark it and spare it for another trip around the | |
645 | * inactive list. Another page table reference will | |
646 | * lead to its activation. | |
647 | * | |
648 | * Note: the mark is set for activated pages as well | |
649 | * so that recently deactivated but used pages are | |
650 | * quickly recovered. | |
651 | */ | |
652 | SetPageReferenced(page); | |
653 | ||
654 | if (referenced_page) | |
655 | return PAGEREF_ACTIVATE; | |
656 | ||
657 | return PAGEREF_KEEP; | |
658 | } | |
dfc8d636 JW |
659 | |
660 | /* Reclaim if clean, defer dirty pages to writeback */ | |
2e30244a | 661 | if (referenced_page && !PageSwapBacked(page)) |
64574746 JW |
662 | return PAGEREF_RECLAIM_CLEAN; |
663 | ||
664 | return PAGEREF_RECLAIM; | |
dfc8d636 JW |
665 | } |
666 | ||
abe4c3b5 MG |
667 | static noinline_for_stack void free_page_list(struct list_head *free_pages) |
668 | { | |
669 | struct pagevec freed_pvec; | |
670 | struct page *page, *tmp; | |
671 | ||
672 | pagevec_init(&freed_pvec, 1); | |
673 | ||
674 | list_for_each_entry_safe(page, tmp, free_pages, lru) { | |
675 | list_del(&page->lru); | |
676 | if (!pagevec_add(&freed_pvec, page)) { | |
677 | __pagevec_free(&freed_pvec); | |
678 | pagevec_reinit(&freed_pvec); | |
679 | } | |
680 | } | |
681 | ||
682 | pagevec_free(&freed_pvec); | |
683 | } | |
684 | ||
1da177e4 | 685 | /* |
1742f19f | 686 | * shrink_page_list() returns the number of reclaimed pages |
1da177e4 | 687 | */ |
1742f19f | 688 | static unsigned long shrink_page_list(struct list_head *page_list, |
0e093d99 | 689 | struct zone *zone, |
7d3579e8 | 690 | struct scan_control *sc) |
1da177e4 LT |
691 | { |
692 | LIST_HEAD(ret_pages); | |
abe4c3b5 | 693 | LIST_HEAD(free_pages); |
1da177e4 | 694 | int pgactivate = 0; |
0e093d99 MG |
695 | unsigned long nr_dirty = 0; |
696 | unsigned long nr_congested = 0; | |
05ff5137 | 697 | unsigned long nr_reclaimed = 0; |
1da177e4 LT |
698 | |
699 | cond_resched(); | |
700 | ||
1da177e4 | 701 | while (!list_empty(page_list)) { |
dfc8d636 | 702 | enum page_references references; |
1da177e4 LT |
703 | struct address_space *mapping; |
704 | struct page *page; | |
705 | int may_enter_fs; | |
1da177e4 LT |
706 | |
707 | cond_resched(); | |
708 | ||
709 | page = lru_to_page(page_list); | |
710 | list_del(&page->lru); | |
711 | ||
529ae9aa | 712 | if (!trylock_page(page)) |
1da177e4 LT |
713 | goto keep; |
714 | ||
725d704e | 715 | VM_BUG_ON(PageActive(page)); |
0e093d99 | 716 | VM_BUG_ON(page_zone(page) != zone); |
1da177e4 LT |
717 | |
718 | sc->nr_scanned++; | |
80e43426 | 719 | |
b291f000 NP |
720 | if (unlikely(!page_evictable(page, NULL))) |
721 | goto cull_mlocked; | |
894bc310 | 722 | |
a6dc60f8 | 723 | if (!sc->may_unmap && page_mapped(page)) |
80e43426 CL |
724 | goto keep_locked; |
725 | ||
1da177e4 LT |
726 | /* Double the slab pressure for mapped and swapcache pages */ |
727 | if (page_mapped(page) || PageSwapCache(page)) | |
728 | sc->nr_scanned++; | |
729 | ||
c661b078 AW |
730 | may_enter_fs = (sc->gfp_mask & __GFP_FS) || |
731 | (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); | |
732 | ||
733 | if (PageWriteback(page)) { | |
734 | /* | |
735 | * Synchronous reclaim is performed in two passes, | |
736 | * first an asynchronous pass over the list to | |
737 | * start parallel writeback, and a second synchronous | |
738 | * pass to wait for the IO to complete. Wait here | |
739 | * for any page for which writeback has already | |
740 | * started. | |
741 | */ | |
7d3579e8 KM |
742 | if (sc->lumpy_reclaim_mode == LUMPY_MODE_SYNC && |
743 | may_enter_fs) | |
c661b078 | 744 | wait_on_page_writeback(page); |
7d3579e8 KM |
745 | else { |
746 | unlock_page(page); | |
747 | goto keep_lumpy; | |
748 | } | |
c661b078 | 749 | } |
1da177e4 | 750 | |
dfc8d636 JW |
751 | references = page_check_references(page, sc); |
752 | switch (references) { | |
753 | case PAGEREF_ACTIVATE: | |
1da177e4 | 754 | goto activate_locked; |
64574746 JW |
755 | case PAGEREF_KEEP: |
756 | goto keep_locked; | |
dfc8d636 JW |
757 | case PAGEREF_RECLAIM: |
758 | case PAGEREF_RECLAIM_CLEAN: | |
759 | ; /* try to reclaim the page below */ | |
760 | } | |
1da177e4 | 761 | |
1da177e4 LT |
762 | /* |
763 | * Anonymous process memory has backing store? | |
764 | * Try to allocate it some swap space here. | |
765 | */ | |
b291f000 | 766 | if (PageAnon(page) && !PageSwapCache(page)) { |
63eb6b93 HD |
767 | if (!(sc->gfp_mask & __GFP_IO)) |
768 | goto keep_locked; | |
ac47b003 | 769 | if (!add_to_swap(page)) |
1da177e4 | 770 | goto activate_locked; |
63eb6b93 | 771 | may_enter_fs = 1; |
b291f000 | 772 | } |
1da177e4 LT |
773 | |
774 | mapping = page_mapping(page); | |
1da177e4 LT |
775 | |
776 | /* | |
777 | * The page is mapped into the page tables of one or more | |
778 | * processes. Try to unmap it here. | |
779 | */ | |
780 | if (page_mapped(page) && mapping) { | |
14fa31b8 | 781 | switch (try_to_unmap(page, TTU_UNMAP)) { |
1da177e4 LT |
782 | case SWAP_FAIL: |
783 | goto activate_locked; | |
784 | case SWAP_AGAIN: | |
785 | goto keep_locked; | |
b291f000 NP |
786 | case SWAP_MLOCK: |
787 | goto cull_mlocked; | |
1da177e4 LT |
788 | case SWAP_SUCCESS: |
789 | ; /* try to free the page below */ | |
790 | } | |
791 | } | |
792 | ||
793 | if (PageDirty(page)) { | |
0e093d99 MG |
794 | nr_dirty++; |
795 | ||
dfc8d636 | 796 | if (references == PAGEREF_RECLAIM_CLEAN) |
1da177e4 | 797 | goto keep_locked; |
4dd4b920 | 798 | if (!may_enter_fs) |
1da177e4 | 799 | goto keep_locked; |
52a8363e | 800 | if (!sc->may_writepage) |
1da177e4 LT |
801 | goto keep_locked; |
802 | ||
803 | /* Page is dirty, try to write it out here */ | |
7d3579e8 | 804 | switch (pageout(page, mapping, sc)) { |
1da177e4 | 805 | case PAGE_KEEP: |
0e093d99 | 806 | nr_congested++; |
1da177e4 LT |
807 | goto keep_locked; |
808 | case PAGE_ACTIVATE: | |
809 | goto activate_locked; | |
810 | case PAGE_SUCCESS: | |
7d3579e8 KM |
811 | if (PageWriteback(page)) |
812 | goto keep_lumpy; | |
813 | if (PageDirty(page)) | |
1da177e4 | 814 | goto keep; |
7d3579e8 | 815 | |
1da177e4 LT |
816 | /* |
817 | * A synchronous write - probably a ramdisk. Go | |
818 | * ahead and try to reclaim the page. | |
819 | */ | |
529ae9aa | 820 | if (!trylock_page(page)) |
1da177e4 LT |
821 | goto keep; |
822 | if (PageDirty(page) || PageWriteback(page)) | |
823 | goto keep_locked; | |
824 | mapping = page_mapping(page); | |
825 | case PAGE_CLEAN: | |
826 | ; /* try to free the page below */ | |
827 | } | |
828 | } | |
829 | ||
830 | /* | |
831 | * If the page has buffers, try to free the buffer mappings | |
832 | * associated with this page. If we succeed we try to free | |
833 | * the page as well. | |
834 | * | |
835 | * We do this even if the page is PageDirty(). | |
836 | * try_to_release_page() does not perform I/O, but it is | |
837 | * possible for a page to have PageDirty set, but it is actually | |
838 | * clean (all its buffers are clean). This happens if the | |
839 | * buffers were written out directly, with submit_bh(). ext3 | |
894bc310 | 840 | * will do this, as well as the blockdev mapping. |
1da177e4 LT |
841 | * try_to_release_page() will discover that cleanness and will |
842 | * drop the buffers and mark the page clean - it can be freed. | |
843 | * | |
844 | * Rarely, pages can have buffers and no ->mapping. These are | |
845 | * the pages which were not successfully invalidated in | |
846 | * truncate_complete_page(). We try to drop those buffers here | |
847 | * and if that worked, and the page is no longer mapped into | |
848 | * process address space (page_count == 1) it can be freed. | |
849 | * Otherwise, leave the page on the LRU so it is swappable. | |
850 | */ | |
266cf658 | 851 | if (page_has_private(page)) { |
1da177e4 LT |
852 | if (!try_to_release_page(page, sc->gfp_mask)) |
853 | goto activate_locked; | |
e286781d NP |
854 | if (!mapping && page_count(page) == 1) { |
855 | unlock_page(page); | |
856 | if (put_page_testzero(page)) | |
857 | goto free_it; | |
858 | else { | |
859 | /* | |
860 | * rare race with speculative reference. | |
861 | * the speculative reference will free | |
862 | * this page shortly, so we may | |
863 | * increment nr_reclaimed here (and | |
864 | * leave it off the LRU). | |
865 | */ | |
866 | nr_reclaimed++; | |
867 | continue; | |
868 | } | |
869 | } | |
1da177e4 LT |
870 | } |
871 | ||
e286781d | 872 | if (!mapping || !__remove_mapping(mapping, page)) |
49d2e9cc | 873 | goto keep_locked; |
1da177e4 | 874 | |
a978d6f5 NP |
875 | /* |
876 | * At this point, we have no other references and there is | |
877 | * no way to pick any more up (removed from LRU, removed | |
878 | * from pagecache). Can use non-atomic bitops now (and | |
879 | * we obviously don't have to worry about waking up a process | |
880 | * waiting on the page lock, because there are no references. | |
881 | */ | |
882 | __clear_page_locked(page); | |
e286781d | 883 | free_it: |
05ff5137 | 884 | nr_reclaimed++; |
abe4c3b5 MG |
885 | |
886 | /* | |
887 | * Is there need to periodically free_page_list? It would | |
888 | * appear not as the counts should be low | |
889 | */ | |
890 | list_add(&page->lru, &free_pages); | |
1da177e4 LT |
891 | continue; |
892 | ||
b291f000 | 893 | cull_mlocked: |
63d6c5ad HD |
894 | if (PageSwapCache(page)) |
895 | try_to_free_swap(page); | |
b291f000 NP |
896 | unlock_page(page); |
897 | putback_lru_page(page); | |
7d3579e8 | 898 | disable_lumpy_reclaim_mode(sc); |
b291f000 NP |
899 | continue; |
900 | ||
1da177e4 | 901 | activate_locked: |
68a22394 RR |
902 | /* Not a candidate for swapping, so reclaim swap space. */ |
903 | if (PageSwapCache(page) && vm_swap_full()) | |
a2c43eed | 904 | try_to_free_swap(page); |
894bc310 | 905 | VM_BUG_ON(PageActive(page)); |
1da177e4 LT |
906 | SetPageActive(page); |
907 | pgactivate++; | |
908 | keep_locked: | |
909 | unlock_page(page); | |
910 | keep: | |
7d3579e8 KM |
911 | disable_lumpy_reclaim_mode(sc); |
912 | keep_lumpy: | |
1da177e4 | 913 | list_add(&page->lru, &ret_pages); |
b291f000 | 914 | VM_BUG_ON(PageLRU(page) || PageUnevictable(page)); |
1da177e4 | 915 | } |
abe4c3b5 | 916 | |
0e093d99 MG |
917 | /* |
918 | * Tag a zone as congested if all the dirty pages encountered were | |
919 | * backed by a congested BDI. In this case, reclaimers should just | |
920 | * back off and wait for congestion to clear because further reclaim | |
921 | * will encounter the same problem | |
922 | */ | |
1dce071e | 923 | if (nr_dirty == nr_congested && nr_dirty != 0) |
0e093d99 MG |
924 | zone_set_flag(zone, ZONE_CONGESTED); |
925 | ||
abe4c3b5 MG |
926 | free_page_list(&free_pages); |
927 | ||
1da177e4 | 928 | list_splice(&ret_pages, page_list); |
f8891e5e | 929 | count_vm_events(PGACTIVATE, pgactivate); |
05ff5137 | 930 | return nr_reclaimed; |
1da177e4 LT |
931 | } |
932 | ||
5ad333eb AW |
933 | /* |
934 | * Attempt to remove the specified page from its LRU. Only take this page | |
935 | * if it is of the appropriate PageActive status. Pages which are being | |
936 | * freed elsewhere are also ignored. | |
937 | * | |
938 | * page: page to consider | |
939 | * mode: one of the LRU isolation modes defined above | |
940 | * | |
941 | * returns 0 on success, -ve errno on failure. | |
942 | */ | |
4f98a2fe | 943 | int __isolate_lru_page(struct page *page, int mode, int file) |
5ad333eb AW |
944 | { |
945 | int ret = -EINVAL; | |
946 | ||
947 | /* Only take pages on the LRU. */ | |
948 | if (!PageLRU(page)) | |
949 | return ret; | |
950 | ||
951 | /* | |
952 | * When checking the active state, we need to be sure we are | |
953 | * dealing with comparible boolean values. Take the logical not | |
954 | * of each. | |
955 | */ | |
956 | if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode)) | |
957 | return ret; | |
958 | ||
6c0b1351 | 959 | if (mode != ISOLATE_BOTH && page_is_file_cache(page) != file) |
4f98a2fe RR |
960 | return ret; |
961 | ||
894bc310 LS |
962 | /* |
963 | * When this function is being called for lumpy reclaim, we | |
964 | * initially look into all LRU pages, active, inactive and | |
965 | * unevictable; only give shrink_page_list evictable pages. | |
966 | */ | |
967 | if (PageUnevictable(page)) | |
968 | return ret; | |
969 | ||
5ad333eb | 970 | ret = -EBUSY; |
08e552c6 | 971 | |
5ad333eb AW |
972 | if (likely(get_page_unless_zero(page))) { |
973 | /* | |
974 | * Be careful not to clear PageLRU until after we're | |
975 | * sure the page is not being freed elsewhere -- the | |
976 | * page release code relies on it. | |
977 | */ | |
978 | ClearPageLRU(page); | |
979 | ret = 0; | |
980 | } | |
981 | ||
982 | return ret; | |
983 | } | |
984 | ||
1da177e4 LT |
985 | /* |
986 | * zone->lru_lock is heavily contended. Some of the functions that | |
987 | * shrink the lists perform better by taking out a batch of pages | |
988 | * and working on them outside the LRU lock. | |
989 | * | |
990 | * For pagecache intensive workloads, this function is the hottest | |
991 | * spot in the kernel (apart from copy_*_user functions). | |
992 | * | |
993 | * Appropriate locks must be held before calling this function. | |
994 | * | |
995 | * @nr_to_scan: The number of pages to look through on the list. | |
996 | * @src: The LRU list to pull pages off. | |
997 | * @dst: The temp list to put pages on to. | |
998 | * @scanned: The number of pages that were scanned. | |
5ad333eb AW |
999 | * @order: The caller's attempted allocation order |
1000 | * @mode: One of the LRU isolation modes | |
4f98a2fe | 1001 | * @file: True [1] if isolating file [!anon] pages |
1da177e4 LT |
1002 | * |
1003 | * returns how many pages were moved onto *@dst. | |
1004 | */ | |
69e05944 AM |
1005 | static unsigned long isolate_lru_pages(unsigned long nr_to_scan, |
1006 | struct list_head *src, struct list_head *dst, | |
4f98a2fe | 1007 | unsigned long *scanned, int order, int mode, int file) |
1da177e4 | 1008 | { |
69e05944 | 1009 | unsigned long nr_taken = 0; |
a8a94d15 MG |
1010 | unsigned long nr_lumpy_taken = 0; |
1011 | unsigned long nr_lumpy_dirty = 0; | |
1012 | unsigned long nr_lumpy_failed = 0; | |
c9b02d97 | 1013 | unsigned long scan; |
1da177e4 | 1014 | |
c9b02d97 | 1015 | for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { |
5ad333eb AW |
1016 | struct page *page; |
1017 | unsigned long pfn; | |
1018 | unsigned long end_pfn; | |
1019 | unsigned long page_pfn; | |
1020 | int zone_id; | |
1021 | ||
1da177e4 LT |
1022 | page = lru_to_page(src); |
1023 | prefetchw_prev_lru_page(page, src, flags); | |
1024 | ||
725d704e | 1025 | VM_BUG_ON(!PageLRU(page)); |
8d438f96 | 1026 | |
4f98a2fe | 1027 | switch (__isolate_lru_page(page, mode, file)) { |
5ad333eb AW |
1028 | case 0: |
1029 | list_move(&page->lru, dst); | |
2ffebca6 | 1030 | mem_cgroup_del_lru(page); |
7c8ee9a8 | 1031 | nr_taken++; |
5ad333eb AW |
1032 | break; |
1033 | ||
1034 | case -EBUSY: | |
1035 | /* else it is being freed elsewhere */ | |
1036 | list_move(&page->lru, src); | |
2ffebca6 | 1037 | mem_cgroup_rotate_lru_list(page, page_lru(page)); |
5ad333eb | 1038 | continue; |
46453a6e | 1039 | |
5ad333eb AW |
1040 | default: |
1041 | BUG(); | |
1042 | } | |
1043 | ||
1044 | if (!order) | |
1045 | continue; | |
1046 | ||
1047 | /* | |
1048 | * Attempt to take all pages in the order aligned region | |
1049 | * surrounding the tag page. Only take those pages of | |
1050 | * the same active state as that tag page. We may safely | |
1051 | * round the target page pfn down to the requested order | |
1052 | * as the mem_map is guarenteed valid out to MAX_ORDER, | |
1053 | * where that page is in a different zone we will detect | |
1054 | * it from its zone id and abort this block scan. | |
1055 | */ | |
1056 | zone_id = page_zone_id(page); | |
1057 | page_pfn = page_to_pfn(page); | |
1058 | pfn = page_pfn & ~((1 << order) - 1); | |
1059 | end_pfn = pfn + (1 << order); | |
1060 | for (; pfn < end_pfn; pfn++) { | |
1061 | struct page *cursor_page; | |
1062 | ||
1063 | /* The target page is in the block, ignore it. */ | |
1064 | if (unlikely(pfn == page_pfn)) | |
1065 | continue; | |
1066 | ||
1067 | /* Avoid holes within the zone. */ | |
1068 | if (unlikely(!pfn_valid_within(pfn))) | |
1069 | break; | |
1070 | ||
1071 | cursor_page = pfn_to_page(pfn); | |
4f98a2fe | 1072 | |
5ad333eb AW |
1073 | /* Check that we have not crossed a zone boundary. */ |
1074 | if (unlikely(page_zone_id(cursor_page) != zone_id)) | |
08fc468f | 1075 | break; |
de2e7567 MK |
1076 | |
1077 | /* | |
1078 | * If we don't have enough swap space, reclaiming of | |
1079 | * anon page which don't already have a swap slot is | |
1080 | * pointless. | |
1081 | */ | |
1082 | if (nr_swap_pages <= 0 && PageAnon(cursor_page) && | |
08fc468f KM |
1083 | !PageSwapCache(cursor_page)) |
1084 | break; | |
de2e7567 | 1085 | |
ee993b13 | 1086 | if (__isolate_lru_page(cursor_page, mode, file) == 0) { |
5ad333eb | 1087 | list_move(&cursor_page->lru, dst); |
cb4cbcf6 | 1088 | mem_cgroup_del_lru(cursor_page); |
5ad333eb | 1089 | nr_taken++; |
a8a94d15 MG |
1090 | nr_lumpy_taken++; |
1091 | if (PageDirty(cursor_page)) | |
1092 | nr_lumpy_dirty++; | |
5ad333eb | 1093 | scan++; |
a8a94d15 | 1094 | } else { |
08fc468f KM |
1095 | /* the page is freed already. */ |
1096 | if (!page_count(cursor_page)) | |
1097 | continue; | |
1098 | break; | |
5ad333eb AW |
1099 | } |
1100 | } | |
08fc468f KM |
1101 | |
1102 | /* If we break out of the loop above, lumpy reclaim failed */ | |
1103 | if (pfn < end_pfn) | |
1104 | nr_lumpy_failed++; | |
1da177e4 LT |
1105 | } |
1106 | ||
1107 | *scanned = scan; | |
a8a94d15 MG |
1108 | |
1109 | trace_mm_vmscan_lru_isolate(order, | |
1110 | nr_to_scan, scan, | |
1111 | nr_taken, | |
1112 | nr_lumpy_taken, nr_lumpy_dirty, nr_lumpy_failed, | |
1113 | mode); | |
1da177e4 LT |
1114 | return nr_taken; |
1115 | } | |
1116 | ||
66e1707b BS |
1117 | static unsigned long isolate_pages_global(unsigned long nr, |
1118 | struct list_head *dst, | |
1119 | unsigned long *scanned, int order, | |
1120 | int mode, struct zone *z, | |
4f98a2fe | 1121 | int active, int file) |
66e1707b | 1122 | { |
4f98a2fe | 1123 | int lru = LRU_BASE; |
66e1707b | 1124 | if (active) |
4f98a2fe RR |
1125 | lru += LRU_ACTIVE; |
1126 | if (file) | |
1127 | lru += LRU_FILE; | |
1128 | return isolate_lru_pages(nr, &z->lru[lru].list, dst, scanned, order, | |
b7c46d15 | 1129 | mode, file); |
66e1707b BS |
1130 | } |
1131 | ||
5ad333eb AW |
1132 | /* |
1133 | * clear_active_flags() is a helper for shrink_active_list(), clearing | |
1134 | * any active bits from the pages in the list. | |
1135 | */ | |
4f98a2fe RR |
1136 | static unsigned long clear_active_flags(struct list_head *page_list, |
1137 | unsigned int *count) | |
5ad333eb AW |
1138 | { |
1139 | int nr_active = 0; | |
4f98a2fe | 1140 | int lru; |
5ad333eb AW |
1141 | struct page *page; |
1142 | ||
4f98a2fe | 1143 | list_for_each_entry(page, page_list, lru) { |
401a8e1c | 1144 | lru = page_lru_base_type(page); |
5ad333eb | 1145 | if (PageActive(page)) { |
4f98a2fe | 1146 | lru += LRU_ACTIVE; |
5ad333eb AW |
1147 | ClearPageActive(page); |
1148 | nr_active++; | |
1149 | } | |
1489fa14 MG |
1150 | if (count) |
1151 | count[lru]++; | |
4f98a2fe | 1152 | } |
5ad333eb AW |
1153 | |
1154 | return nr_active; | |
1155 | } | |
1156 | ||
62695a84 NP |
1157 | /** |
1158 | * isolate_lru_page - tries to isolate a page from its LRU list | |
1159 | * @page: page to isolate from its LRU list | |
1160 | * | |
1161 | * Isolates a @page from an LRU list, clears PageLRU and adjusts the | |
1162 | * vmstat statistic corresponding to whatever LRU list the page was on. | |
1163 | * | |
1164 | * Returns 0 if the page was removed from an LRU list. | |
1165 | * Returns -EBUSY if the page was not on an LRU list. | |
1166 | * | |
1167 | * The returned page will have PageLRU() cleared. If it was found on | |
894bc310 LS |
1168 | * the active list, it will have PageActive set. If it was found on |
1169 | * the unevictable list, it will have the PageUnevictable bit set. That flag | |
1170 | * may need to be cleared by the caller before letting the page go. | |
62695a84 NP |
1171 | * |
1172 | * The vmstat statistic corresponding to the list on which the page was | |
1173 | * found will be decremented. | |
1174 | * | |
1175 | * Restrictions: | |
1176 | * (1) Must be called with an elevated refcount on the page. This is a | |
1177 | * fundamentnal difference from isolate_lru_pages (which is called | |
1178 | * without a stable reference). | |
1179 | * (2) the lru_lock must not be held. | |
1180 | * (3) interrupts must be enabled. | |
1181 | */ | |
1182 | int isolate_lru_page(struct page *page) | |
1183 | { | |
1184 | int ret = -EBUSY; | |
1185 | ||
1186 | if (PageLRU(page)) { | |
1187 | struct zone *zone = page_zone(page); | |
1188 | ||
1189 | spin_lock_irq(&zone->lru_lock); | |
1190 | if (PageLRU(page) && get_page_unless_zero(page)) { | |
894bc310 | 1191 | int lru = page_lru(page); |
62695a84 NP |
1192 | ret = 0; |
1193 | ClearPageLRU(page); | |
4f98a2fe | 1194 | |
4f98a2fe | 1195 | del_page_from_lru_list(zone, page, lru); |
62695a84 NP |
1196 | } |
1197 | spin_unlock_irq(&zone->lru_lock); | |
1198 | } | |
1199 | return ret; | |
1200 | } | |
1201 | ||
35cd7815 RR |
1202 | /* |
1203 | * Are there way too many processes in the direct reclaim path already? | |
1204 | */ | |
1205 | static int too_many_isolated(struct zone *zone, int file, | |
1206 | struct scan_control *sc) | |
1207 | { | |
1208 | unsigned long inactive, isolated; | |
1209 | ||
1210 | if (current_is_kswapd()) | |
1211 | return 0; | |
1212 | ||
1213 | if (!scanning_global_lru(sc)) | |
1214 | return 0; | |
1215 | ||
1216 | if (file) { | |
1217 | inactive = zone_page_state(zone, NR_INACTIVE_FILE); | |
1218 | isolated = zone_page_state(zone, NR_ISOLATED_FILE); | |
1219 | } else { | |
1220 | inactive = zone_page_state(zone, NR_INACTIVE_ANON); | |
1221 | isolated = zone_page_state(zone, NR_ISOLATED_ANON); | |
1222 | } | |
1223 | ||
1224 | return isolated > inactive; | |
1225 | } | |
1226 | ||
66635629 MG |
1227 | /* |
1228 | * TODO: Try merging with migrations version of putback_lru_pages | |
1229 | */ | |
1230 | static noinline_for_stack void | |
1489fa14 | 1231 | putback_lru_pages(struct zone *zone, struct scan_control *sc, |
66635629 MG |
1232 | unsigned long nr_anon, unsigned long nr_file, |
1233 | struct list_head *page_list) | |
1234 | { | |
1235 | struct page *page; | |
1236 | struct pagevec pvec; | |
1489fa14 | 1237 | struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc); |
66635629 MG |
1238 | |
1239 | pagevec_init(&pvec, 1); | |
1240 | ||
1241 | /* | |
1242 | * Put back any unfreeable pages. | |
1243 | */ | |
1244 | spin_lock(&zone->lru_lock); | |
1245 | while (!list_empty(page_list)) { | |
1246 | int lru; | |
1247 | page = lru_to_page(page_list); | |
1248 | VM_BUG_ON(PageLRU(page)); | |
1249 | list_del(&page->lru); | |
1250 | if (unlikely(!page_evictable(page, NULL))) { | |
1251 | spin_unlock_irq(&zone->lru_lock); | |
1252 | putback_lru_page(page); | |
1253 | spin_lock_irq(&zone->lru_lock); | |
1254 | continue; | |
1255 | } | |
1256 | SetPageLRU(page); | |
1257 | lru = page_lru(page); | |
1258 | add_page_to_lru_list(zone, page, lru); | |
1259 | if (is_active_lru(lru)) { | |
1260 | int file = is_file_lru(lru); | |
1261 | reclaim_stat->recent_rotated[file]++; | |
1262 | } | |
1263 | if (!pagevec_add(&pvec, page)) { | |
1264 | spin_unlock_irq(&zone->lru_lock); | |
1265 | __pagevec_release(&pvec); | |
1266 | spin_lock_irq(&zone->lru_lock); | |
1267 | } | |
1268 | } | |
1269 | __mod_zone_page_state(zone, NR_ISOLATED_ANON, -nr_anon); | |
1270 | __mod_zone_page_state(zone, NR_ISOLATED_FILE, -nr_file); | |
1271 | ||
1272 | spin_unlock_irq(&zone->lru_lock); | |
1273 | pagevec_release(&pvec); | |
1274 | } | |
1275 | ||
1489fa14 MG |
1276 | static noinline_for_stack void update_isolated_counts(struct zone *zone, |
1277 | struct scan_control *sc, | |
1278 | unsigned long *nr_anon, | |
1279 | unsigned long *nr_file, | |
1280 | struct list_head *isolated_list) | |
1281 | { | |
1282 | unsigned long nr_active; | |
1283 | unsigned int count[NR_LRU_LISTS] = { 0, }; | |
1284 | struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc); | |
1285 | ||
1286 | nr_active = clear_active_flags(isolated_list, count); | |
1287 | __count_vm_events(PGDEACTIVATE, nr_active); | |
1288 | ||
1289 | __mod_zone_page_state(zone, NR_ACTIVE_FILE, | |
1290 | -count[LRU_ACTIVE_FILE]); | |
1291 | __mod_zone_page_state(zone, NR_INACTIVE_FILE, | |
1292 | -count[LRU_INACTIVE_FILE]); | |
1293 | __mod_zone_page_state(zone, NR_ACTIVE_ANON, | |
1294 | -count[LRU_ACTIVE_ANON]); | |
1295 | __mod_zone_page_state(zone, NR_INACTIVE_ANON, | |
1296 | -count[LRU_INACTIVE_ANON]); | |
1297 | ||
1298 | *nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON]; | |
1299 | *nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE]; | |
1300 | __mod_zone_page_state(zone, NR_ISOLATED_ANON, *nr_anon); | |
1301 | __mod_zone_page_state(zone, NR_ISOLATED_FILE, *nr_file); | |
1302 | ||
1303 | reclaim_stat->recent_scanned[0] += *nr_anon; | |
1304 | reclaim_stat->recent_scanned[1] += *nr_file; | |
1305 | } | |
1306 | ||
e31f3698 WF |
1307 | /* |
1308 | * Returns true if the caller should wait to clean dirty/writeback pages. | |
1309 | * | |
1310 | * If we are direct reclaiming for contiguous pages and we do not reclaim | |
1311 | * everything in the list, try again and wait for writeback IO to complete. | |
1312 | * This will stall high-order allocations noticeably. Only do that when really | |
1313 | * need to free the pages under high memory pressure. | |
1314 | */ | |
1315 | static inline bool should_reclaim_stall(unsigned long nr_taken, | |
1316 | unsigned long nr_freed, | |
1317 | int priority, | |
1318 | struct scan_control *sc) | |
1319 | { | |
1320 | int lumpy_stall_priority; | |
1321 | ||
1322 | /* kswapd should not stall on sync IO */ | |
1323 | if (current_is_kswapd()) | |
1324 | return false; | |
1325 | ||
1326 | /* Only stall on lumpy reclaim */ | |
7d3579e8 | 1327 | if (sc->lumpy_reclaim_mode == LUMPY_MODE_NONE) |
e31f3698 WF |
1328 | return false; |
1329 | ||
1330 | /* If we have relaimed everything on the isolated list, no stall */ | |
1331 | if (nr_freed == nr_taken) | |
1332 | return false; | |
1333 | ||
1334 | /* | |
1335 | * For high-order allocations, there are two stall thresholds. | |
1336 | * High-cost allocations stall immediately where as lower | |
1337 | * order allocations such as stacks require the scanning | |
1338 | * priority to be much higher before stalling. | |
1339 | */ | |
1340 | if (sc->order > PAGE_ALLOC_COSTLY_ORDER) | |
1341 | lumpy_stall_priority = DEF_PRIORITY; | |
1342 | else | |
1343 | lumpy_stall_priority = DEF_PRIORITY / 3; | |
1344 | ||
1345 | return priority <= lumpy_stall_priority; | |
1346 | } | |
1347 | ||
1da177e4 | 1348 | /* |
1742f19f AM |
1349 | * shrink_inactive_list() is a helper for shrink_zone(). It returns the number |
1350 | * of reclaimed pages | |
1da177e4 | 1351 | */ |
66635629 MG |
1352 | static noinline_for_stack unsigned long |
1353 | shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone, | |
1354 | struct scan_control *sc, int priority, int file) | |
1da177e4 LT |
1355 | { |
1356 | LIST_HEAD(page_list); | |
e247dbce | 1357 | unsigned long nr_scanned; |
05ff5137 | 1358 | unsigned long nr_reclaimed = 0; |
e247dbce | 1359 | unsigned long nr_taken; |
e247dbce KM |
1360 | unsigned long nr_anon; |
1361 | unsigned long nr_file; | |
78dc583d | 1362 | |
35cd7815 | 1363 | while (unlikely(too_many_isolated(zone, file, sc))) { |
58355c78 | 1364 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
35cd7815 RR |
1365 | |
1366 | /* We are about to die and free our memory. Return now. */ | |
1367 | if (fatal_signal_pending(current)) | |
1368 | return SWAP_CLUSTER_MAX; | |
1369 | } | |
1370 | ||
7d3579e8 | 1371 | set_lumpy_reclaim_mode(priority, sc, false); |
1da177e4 LT |
1372 | lru_add_drain(); |
1373 | spin_lock_irq(&zone->lru_lock); | |
b35ea17b | 1374 | |
e247dbce KM |
1375 | if (scanning_global_lru(sc)) { |
1376 | nr_taken = isolate_pages_global(nr_to_scan, | |
1377 | &page_list, &nr_scanned, sc->order, | |
7d3579e8 KM |
1378 | sc->lumpy_reclaim_mode == LUMPY_MODE_NONE ? |
1379 | ISOLATE_INACTIVE : ISOLATE_BOTH, | |
e247dbce KM |
1380 | zone, 0, file); |
1381 | zone->pages_scanned += nr_scanned; | |
1382 | if (current_is_kswapd()) | |
1383 | __count_zone_vm_events(PGSCAN_KSWAPD, zone, | |
1384 | nr_scanned); | |
1385 | else | |
1386 | __count_zone_vm_events(PGSCAN_DIRECT, zone, | |
1387 | nr_scanned); | |
1388 | } else { | |
1389 | nr_taken = mem_cgroup_isolate_pages(nr_to_scan, | |
1390 | &page_list, &nr_scanned, sc->order, | |
7d3579e8 KM |
1391 | sc->lumpy_reclaim_mode == LUMPY_MODE_NONE ? |
1392 | ISOLATE_INACTIVE : ISOLATE_BOTH, | |
e247dbce KM |
1393 | zone, sc->mem_cgroup, |
1394 | 0, file); | |
1395 | /* | |
1396 | * mem_cgroup_isolate_pages() keeps track of | |
1397 | * scanned pages on its own. | |
1398 | */ | |
1399 | } | |
b35ea17b | 1400 | |
66635629 MG |
1401 | if (nr_taken == 0) { |
1402 | spin_unlock_irq(&zone->lru_lock); | |
1403 | return 0; | |
1404 | } | |
5ad333eb | 1405 | |
1489fa14 | 1406 | update_isolated_counts(zone, sc, &nr_anon, &nr_file, &page_list); |
1da177e4 | 1407 | |
e247dbce | 1408 | spin_unlock_irq(&zone->lru_lock); |
c661b078 | 1409 | |
0e093d99 | 1410 | nr_reclaimed = shrink_page_list(&page_list, zone, sc); |
c661b078 | 1411 | |
e31f3698 WF |
1412 | /* Check if we should syncronously wait for writeback */ |
1413 | if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) { | |
7d3579e8 | 1414 | set_lumpy_reclaim_mode(priority, sc, true); |
0e093d99 | 1415 | nr_reclaimed += shrink_page_list(&page_list, zone, sc); |
e247dbce | 1416 | } |
b35ea17b | 1417 | |
e247dbce KM |
1418 | local_irq_disable(); |
1419 | if (current_is_kswapd()) | |
1420 | __count_vm_events(KSWAPD_STEAL, nr_reclaimed); | |
1421 | __count_zone_vm_events(PGSTEAL, zone, nr_reclaimed); | |
a74609fa | 1422 | |
1489fa14 | 1423 | putback_lru_pages(zone, sc, nr_anon, nr_file, &page_list); |
e11da5b4 MG |
1424 | |
1425 | trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id, | |
1426 | zone_idx(zone), | |
1427 | nr_scanned, nr_reclaimed, | |
1428 | priority, | |
1429 | trace_shrink_flags(file, sc->lumpy_reclaim_mode)); | |
05ff5137 | 1430 | return nr_reclaimed; |
1da177e4 LT |
1431 | } |
1432 | ||
1433 | /* | |
1434 | * This moves pages from the active list to the inactive list. | |
1435 | * | |
1436 | * We move them the other way if the page is referenced by one or more | |
1437 | * processes, from rmap. | |
1438 | * | |
1439 | * If the pages are mostly unmapped, the processing is fast and it is | |
1440 | * appropriate to hold zone->lru_lock across the whole operation. But if | |
1441 | * the pages are mapped, the processing is slow (page_referenced()) so we | |
1442 | * should drop zone->lru_lock around each page. It's impossible to balance | |
1443 | * this, so instead we remove the pages from the LRU while processing them. | |
1444 | * It is safe to rely on PG_active against the non-LRU pages in here because | |
1445 | * nobody will play with that bit on a non-LRU page. | |
1446 | * | |
1447 | * The downside is that we have to touch page->_count against each page. | |
1448 | * But we had to alter page->flags anyway. | |
1449 | */ | |
1cfb419b | 1450 | |
3eb4140f WF |
1451 | static void move_active_pages_to_lru(struct zone *zone, |
1452 | struct list_head *list, | |
1453 | enum lru_list lru) | |
1454 | { | |
1455 | unsigned long pgmoved = 0; | |
1456 | struct pagevec pvec; | |
1457 | struct page *page; | |
1458 | ||
1459 | pagevec_init(&pvec, 1); | |
1460 | ||
1461 | while (!list_empty(list)) { | |
1462 | page = lru_to_page(list); | |
3eb4140f WF |
1463 | |
1464 | VM_BUG_ON(PageLRU(page)); | |
1465 | SetPageLRU(page); | |
1466 | ||
3eb4140f WF |
1467 | list_move(&page->lru, &zone->lru[lru].list); |
1468 | mem_cgroup_add_lru_list(page, lru); | |
1469 | pgmoved++; | |
1470 | ||
1471 | if (!pagevec_add(&pvec, page) || list_empty(list)) { | |
1472 | spin_unlock_irq(&zone->lru_lock); | |
1473 | if (buffer_heads_over_limit) | |
1474 | pagevec_strip(&pvec); | |
1475 | __pagevec_release(&pvec); | |
1476 | spin_lock_irq(&zone->lru_lock); | |
1477 | } | |
1478 | } | |
1479 | __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved); | |
1480 | if (!is_active_lru(lru)) | |
1481 | __count_vm_events(PGDEACTIVATE, pgmoved); | |
1482 | } | |
1cfb419b | 1483 | |
1742f19f | 1484 | static void shrink_active_list(unsigned long nr_pages, struct zone *zone, |
4f98a2fe | 1485 | struct scan_control *sc, int priority, int file) |
1da177e4 | 1486 | { |
44c241f1 | 1487 | unsigned long nr_taken; |
69e05944 | 1488 | unsigned long pgscanned; |
6fe6b7e3 | 1489 | unsigned long vm_flags; |
1da177e4 | 1490 | LIST_HEAD(l_hold); /* The pages which were snipped off */ |
8cab4754 | 1491 | LIST_HEAD(l_active); |
b69408e8 | 1492 | LIST_HEAD(l_inactive); |
1da177e4 | 1493 | struct page *page; |
6e901571 | 1494 | struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc); |
44c241f1 | 1495 | unsigned long nr_rotated = 0; |
1da177e4 LT |
1496 | |
1497 | lru_add_drain(); | |
1498 | spin_lock_irq(&zone->lru_lock); | |
e72e2bd6 | 1499 | if (scanning_global_lru(sc)) { |
8b25c6d2 JW |
1500 | nr_taken = isolate_pages_global(nr_pages, &l_hold, |
1501 | &pgscanned, sc->order, | |
1502 | ISOLATE_ACTIVE, zone, | |
1503 | 1, file); | |
1cfb419b | 1504 | zone->pages_scanned += pgscanned; |
8b25c6d2 JW |
1505 | } else { |
1506 | nr_taken = mem_cgroup_isolate_pages(nr_pages, &l_hold, | |
1507 | &pgscanned, sc->order, | |
1508 | ISOLATE_ACTIVE, zone, | |
1509 | sc->mem_cgroup, 1, file); | |
1510 | /* | |
1511 | * mem_cgroup_isolate_pages() keeps track of | |
1512 | * scanned pages on its own. | |
1513 | */ | |
4f98a2fe | 1514 | } |
8b25c6d2 | 1515 | |
b7c46d15 | 1516 | reclaim_stat->recent_scanned[file] += nr_taken; |
1cfb419b | 1517 | |
3eb4140f | 1518 | __count_zone_vm_events(PGREFILL, zone, pgscanned); |
4f98a2fe | 1519 | if (file) |
44c241f1 | 1520 | __mod_zone_page_state(zone, NR_ACTIVE_FILE, -nr_taken); |
4f98a2fe | 1521 | else |
44c241f1 | 1522 | __mod_zone_page_state(zone, NR_ACTIVE_ANON, -nr_taken); |
a731286d | 1523 | __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); |
1da177e4 LT |
1524 | spin_unlock_irq(&zone->lru_lock); |
1525 | ||
1da177e4 LT |
1526 | while (!list_empty(&l_hold)) { |
1527 | cond_resched(); | |
1528 | page = lru_to_page(&l_hold); | |
1529 | list_del(&page->lru); | |
7e9cd484 | 1530 | |
894bc310 LS |
1531 | if (unlikely(!page_evictable(page, NULL))) { |
1532 | putback_lru_page(page); | |
1533 | continue; | |
1534 | } | |
1535 | ||
64574746 | 1536 | if (page_referenced(page, 0, sc->mem_cgroup, &vm_flags)) { |
44c241f1 | 1537 | nr_rotated++; |
8cab4754 WF |
1538 | /* |
1539 | * Identify referenced, file-backed active pages and | |
1540 | * give them one more trip around the active list. So | |
1541 | * that executable code get better chances to stay in | |
1542 | * memory under moderate memory pressure. Anon pages | |
1543 | * are not likely to be evicted by use-once streaming | |
1544 | * IO, plus JVM can create lots of anon VM_EXEC pages, | |
1545 | * so we ignore them here. | |
1546 | */ | |
41e20983 | 1547 | if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { |
8cab4754 WF |
1548 | list_add(&page->lru, &l_active); |
1549 | continue; | |
1550 | } | |
1551 | } | |
7e9cd484 | 1552 | |
5205e56e | 1553 | ClearPageActive(page); /* we are de-activating */ |
1da177e4 LT |
1554 | list_add(&page->lru, &l_inactive); |
1555 | } | |
1556 | ||
b555749a | 1557 | /* |
8cab4754 | 1558 | * Move pages back to the lru list. |
b555749a | 1559 | */ |
2a1dc509 | 1560 | spin_lock_irq(&zone->lru_lock); |
556adecb | 1561 | /* |
8cab4754 WF |
1562 | * Count referenced pages from currently used mappings as rotated, |
1563 | * even though only some of them are actually re-activated. This | |
1564 | * helps balance scan pressure between file and anonymous pages in | |
1565 | * get_scan_ratio. | |
7e9cd484 | 1566 | */ |
b7c46d15 | 1567 | reclaim_stat->recent_rotated[file] += nr_rotated; |
556adecb | 1568 | |
3eb4140f WF |
1569 | move_active_pages_to_lru(zone, &l_active, |
1570 | LRU_ACTIVE + file * LRU_FILE); | |
1571 | move_active_pages_to_lru(zone, &l_inactive, | |
1572 | LRU_BASE + file * LRU_FILE); | |
a731286d | 1573 | __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); |
f8891e5e | 1574 | spin_unlock_irq(&zone->lru_lock); |
1da177e4 LT |
1575 | } |
1576 | ||
74e3f3c3 | 1577 | #ifdef CONFIG_SWAP |
14797e23 | 1578 | static int inactive_anon_is_low_global(struct zone *zone) |
f89eb90e KM |
1579 | { |
1580 | unsigned long active, inactive; | |
1581 | ||
1582 | active = zone_page_state(zone, NR_ACTIVE_ANON); | |
1583 | inactive = zone_page_state(zone, NR_INACTIVE_ANON); | |
1584 | ||
1585 | if (inactive * zone->inactive_ratio < active) | |
1586 | return 1; | |
1587 | ||
1588 | return 0; | |
1589 | } | |
1590 | ||
14797e23 KM |
1591 | /** |
1592 | * inactive_anon_is_low - check if anonymous pages need to be deactivated | |
1593 | * @zone: zone to check | |
1594 | * @sc: scan control of this context | |
1595 | * | |
1596 | * Returns true if the zone does not have enough inactive anon pages, | |
1597 | * meaning some active anon pages need to be deactivated. | |
1598 | */ | |
1599 | static int inactive_anon_is_low(struct zone *zone, struct scan_control *sc) | |
1600 | { | |
1601 | int low; | |
1602 | ||
74e3f3c3 MK |
1603 | /* |
1604 | * If we don't have swap space, anonymous page deactivation | |
1605 | * is pointless. | |
1606 | */ | |
1607 | if (!total_swap_pages) | |
1608 | return 0; | |
1609 | ||
e72e2bd6 | 1610 | if (scanning_global_lru(sc)) |
14797e23 KM |
1611 | low = inactive_anon_is_low_global(zone); |
1612 | else | |
c772be93 | 1613 | low = mem_cgroup_inactive_anon_is_low(sc->mem_cgroup); |
14797e23 KM |
1614 | return low; |
1615 | } | |
74e3f3c3 MK |
1616 | #else |
1617 | static inline int inactive_anon_is_low(struct zone *zone, | |
1618 | struct scan_control *sc) | |
1619 | { | |
1620 | return 0; | |
1621 | } | |
1622 | #endif | |
14797e23 | 1623 | |
56e49d21 RR |
1624 | static int inactive_file_is_low_global(struct zone *zone) |
1625 | { | |
1626 | unsigned long active, inactive; | |
1627 | ||
1628 | active = zone_page_state(zone, NR_ACTIVE_FILE); | |
1629 | inactive = zone_page_state(zone, NR_INACTIVE_FILE); | |
1630 | ||
1631 | return (active > inactive); | |
1632 | } | |
1633 | ||
1634 | /** | |
1635 | * inactive_file_is_low - check if file pages need to be deactivated | |
1636 | * @zone: zone to check | |
1637 | * @sc: scan control of this context | |
1638 | * | |
1639 | * When the system is doing streaming IO, memory pressure here | |
1640 | * ensures that active file pages get deactivated, until more | |
1641 | * than half of the file pages are on the inactive list. | |
1642 | * | |
1643 | * Once we get to that situation, protect the system's working | |
1644 | * set from being evicted by disabling active file page aging. | |
1645 | * | |
1646 | * This uses a different ratio than the anonymous pages, because | |
1647 | * the page cache uses a use-once replacement algorithm. | |
1648 | */ | |
1649 | static int inactive_file_is_low(struct zone *zone, struct scan_control *sc) | |
1650 | { | |
1651 | int low; | |
1652 | ||
1653 | if (scanning_global_lru(sc)) | |
1654 | low = inactive_file_is_low_global(zone); | |
1655 | else | |
1656 | low = mem_cgroup_inactive_file_is_low(sc->mem_cgroup); | |
1657 | return low; | |
1658 | } | |
1659 | ||
b39415b2 RR |
1660 | static int inactive_list_is_low(struct zone *zone, struct scan_control *sc, |
1661 | int file) | |
1662 | { | |
1663 | if (file) | |
1664 | return inactive_file_is_low(zone, sc); | |
1665 | else | |
1666 | return inactive_anon_is_low(zone, sc); | |
1667 | } | |
1668 | ||
4f98a2fe | 1669 | static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, |
b69408e8 CL |
1670 | struct zone *zone, struct scan_control *sc, int priority) |
1671 | { | |
4f98a2fe RR |
1672 | int file = is_file_lru(lru); |
1673 | ||
b39415b2 RR |
1674 | if (is_active_lru(lru)) { |
1675 | if (inactive_list_is_low(zone, sc, file)) | |
1676 | shrink_active_list(nr_to_scan, zone, sc, priority, file); | |
556adecb RR |
1677 | return 0; |
1678 | } | |
1679 | ||
33c120ed | 1680 | return shrink_inactive_list(nr_to_scan, zone, sc, priority, file); |
4f98a2fe RR |
1681 | } |
1682 | ||
76a33fc3 SL |
1683 | /* |
1684 | * Smallish @nr_to_scan's are deposited in @nr_saved_scan, | |
1685 | * until we collected @swap_cluster_max pages to scan. | |
1686 | */ | |
1687 | static unsigned long nr_scan_try_batch(unsigned long nr_to_scan, | |
1688 | unsigned long *nr_saved_scan) | |
1689 | { | |
1690 | unsigned long nr; | |
1691 | ||
1692 | *nr_saved_scan += nr_to_scan; | |
1693 | nr = *nr_saved_scan; | |
1694 | ||
1695 | if (nr >= SWAP_CLUSTER_MAX) | |
1696 | *nr_saved_scan = 0; | |
1697 | else | |
1698 | nr = 0; | |
1699 | ||
1700 | return nr; | |
1701 | } | |
1702 | ||
4f98a2fe RR |
1703 | /* |
1704 | * Determine how aggressively the anon and file LRU lists should be | |
1705 | * scanned. The relative value of each set of LRU lists is determined | |
1706 | * by looking at the fraction of the pages scanned we did rotate back | |
1707 | * onto the active list instead of evict. | |
1708 | * | |
76a33fc3 | 1709 | * nr[0] = anon pages to scan; nr[1] = file pages to scan |
4f98a2fe | 1710 | */ |
76a33fc3 SL |
1711 | static void get_scan_count(struct zone *zone, struct scan_control *sc, |
1712 | unsigned long *nr, int priority) | |
4f98a2fe RR |
1713 | { |
1714 | unsigned long anon, file, free; | |
1715 | unsigned long anon_prio, file_prio; | |
1716 | unsigned long ap, fp; | |
6e901571 | 1717 | struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc); |
76a33fc3 SL |
1718 | u64 fraction[2], denominator; |
1719 | enum lru_list l; | |
1720 | int noswap = 0; | |
1721 | ||
1722 | /* If we have no swap space, do not bother scanning anon pages. */ | |
1723 | if (!sc->may_swap || (nr_swap_pages <= 0)) { | |
1724 | noswap = 1; | |
1725 | fraction[0] = 0; | |
1726 | fraction[1] = 1; | |
1727 | denominator = 1; | |
1728 | goto out; | |
1729 | } | |
4f98a2fe | 1730 | |
0b217676 VL |
1731 | anon = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_ANON) + |
1732 | zone_nr_lru_pages(zone, sc, LRU_INACTIVE_ANON); | |
1733 | file = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_FILE) + | |
1734 | zone_nr_lru_pages(zone, sc, LRU_INACTIVE_FILE); | |
b962716b | 1735 | |
e72e2bd6 | 1736 | if (scanning_global_lru(sc)) { |
eeee9a8c KM |
1737 | free = zone_page_state(zone, NR_FREE_PAGES); |
1738 | /* If we have very few page cache pages, | |
1739 | force-scan anon pages. */ | |
41858966 | 1740 | if (unlikely(file + free <= high_wmark_pages(zone))) { |
76a33fc3 SL |
1741 | fraction[0] = 1; |
1742 | fraction[1] = 0; | |
1743 | denominator = 1; | |
1744 | goto out; | |
eeee9a8c | 1745 | } |
4f98a2fe RR |
1746 | } |
1747 | ||
58c37f6e KM |
1748 | /* |
1749 | * With swappiness at 100, anonymous and file have the same priority. | |
1750 | * This scanning priority is essentially the inverse of IO cost. | |
1751 | */ | |
1752 | anon_prio = sc->swappiness; | |
1753 | file_prio = 200 - sc->swappiness; | |
1754 | ||
4f98a2fe RR |
1755 | /* |
1756 | * OK, so we have swap space and a fair amount of page cache | |
1757 | * pages. We use the recently rotated / recently scanned | |
1758 | * ratios to determine how valuable each cache is. | |
1759 | * | |
1760 | * Because workloads change over time (and to avoid overflow) | |
1761 | * we keep these statistics as a floating average, which ends | |
1762 | * up weighing recent references more than old ones. | |
1763 | * | |
1764 | * anon in [0], file in [1] | |
1765 | */ | |
58c37f6e | 1766 | spin_lock_irq(&zone->lru_lock); |
6e901571 | 1767 | if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { |
6e901571 KM |
1768 | reclaim_stat->recent_scanned[0] /= 2; |
1769 | reclaim_stat->recent_rotated[0] /= 2; | |
4f98a2fe RR |
1770 | } |
1771 | ||
6e901571 | 1772 | if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { |
6e901571 KM |
1773 | reclaim_stat->recent_scanned[1] /= 2; |
1774 | reclaim_stat->recent_rotated[1] /= 2; | |
4f98a2fe RR |
1775 | } |
1776 | ||
4f98a2fe | 1777 | /* |
00d8089c RR |
1778 | * The amount of pressure on anon vs file pages is inversely |
1779 | * proportional to the fraction of recently scanned pages on | |
1780 | * each list that were recently referenced and in active use. | |
4f98a2fe | 1781 | */ |
6e901571 KM |
1782 | ap = (anon_prio + 1) * (reclaim_stat->recent_scanned[0] + 1); |
1783 | ap /= reclaim_stat->recent_rotated[0] + 1; | |
4f98a2fe | 1784 | |
6e901571 KM |
1785 | fp = (file_prio + 1) * (reclaim_stat->recent_scanned[1] + 1); |
1786 | fp /= reclaim_stat->recent_rotated[1] + 1; | |
58c37f6e | 1787 | spin_unlock_irq(&zone->lru_lock); |
4f98a2fe | 1788 | |
76a33fc3 SL |
1789 | fraction[0] = ap; |
1790 | fraction[1] = fp; | |
1791 | denominator = ap + fp + 1; | |
1792 | out: | |
1793 | for_each_evictable_lru(l) { | |
1794 | int file = is_file_lru(l); | |
1795 | unsigned long scan; | |
6e08a369 | 1796 | |
76a33fc3 SL |
1797 | scan = zone_nr_lru_pages(zone, sc, l); |
1798 | if (priority || noswap) { | |
1799 | scan >>= priority; | |
1800 | scan = div64_u64(scan * fraction[file], denominator); | |
1801 | } | |
1802 | nr[l] = nr_scan_try_batch(scan, | |
1803 | &reclaim_stat->nr_saved_scan[l]); | |
1804 | } | |
6e08a369 | 1805 | } |
4f98a2fe | 1806 | |
1da177e4 LT |
1807 | /* |
1808 | * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. | |
1809 | */ | |
a79311c1 | 1810 | static void shrink_zone(int priority, struct zone *zone, |
05ff5137 | 1811 | struct scan_control *sc) |
1da177e4 | 1812 | { |
b69408e8 | 1813 | unsigned long nr[NR_LRU_LISTS]; |
8695949a | 1814 | unsigned long nr_to_scan; |
b69408e8 | 1815 | enum lru_list l; |
01dbe5c9 | 1816 | unsigned long nr_reclaimed = sc->nr_reclaimed; |
22fba335 | 1817 | unsigned long nr_to_reclaim = sc->nr_to_reclaim; |
e0f79b8f | 1818 | |
76a33fc3 | 1819 | get_scan_count(zone, sc, nr, priority); |
1da177e4 | 1820 | |
556adecb RR |
1821 | while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || |
1822 | nr[LRU_INACTIVE_FILE]) { | |
894bc310 | 1823 | for_each_evictable_lru(l) { |
b69408e8 | 1824 | if (nr[l]) { |
ece74b2e KM |
1825 | nr_to_scan = min_t(unsigned long, |
1826 | nr[l], SWAP_CLUSTER_MAX); | |
b69408e8 | 1827 | nr[l] -= nr_to_scan; |
1da177e4 | 1828 | |
01dbe5c9 KM |
1829 | nr_reclaimed += shrink_list(l, nr_to_scan, |
1830 | zone, sc, priority); | |
b69408e8 | 1831 | } |
1da177e4 | 1832 | } |
a79311c1 RR |
1833 | /* |
1834 | * On large memory systems, scan >> priority can become | |
1835 | * really large. This is fine for the starting priority; | |
1836 | * we want to put equal scanning pressure on each zone. | |
1837 | * However, if the VM has a harder time of freeing pages, | |
1838 | * with multiple processes reclaiming pages, the total | |
1839 | * freeing target can get unreasonably large. | |
1840 | */ | |
338fde90 | 1841 | if (nr_reclaimed >= nr_to_reclaim && priority < DEF_PRIORITY) |
a79311c1 | 1842 | break; |
1da177e4 LT |
1843 | } |
1844 | ||
01dbe5c9 KM |
1845 | sc->nr_reclaimed = nr_reclaimed; |
1846 | ||
556adecb RR |
1847 | /* |
1848 | * Even if we did not try to evict anon pages at all, we want to | |
1849 | * rebalance the anon lru active/inactive ratio. | |
1850 | */ | |
74e3f3c3 | 1851 | if (inactive_anon_is_low(zone, sc)) |
556adecb RR |
1852 | shrink_active_list(SWAP_CLUSTER_MAX, zone, sc, priority, 0); |
1853 | ||
232ea4d6 | 1854 | throttle_vm_writeout(sc->gfp_mask); |
1da177e4 LT |
1855 | } |
1856 | ||
1857 | /* | |
1858 | * This is the direct reclaim path, for page-allocating processes. We only | |
1859 | * try to reclaim pages from zones which will satisfy the caller's allocation | |
1860 | * request. | |
1861 | * | |
41858966 MG |
1862 | * We reclaim from a zone even if that zone is over high_wmark_pages(zone). |
1863 | * Because: | |
1da177e4 LT |
1864 | * a) The caller may be trying to free *extra* pages to satisfy a higher-order |
1865 | * allocation or | |
41858966 MG |
1866 | * b) The target zone may be at high_wmark_pages(zone) but the lower zones |
1867 | * must go *over* high_wmark_pages(zone) to satisfy the `incremental min' | |
1868 | * zone defense algorithm. | |
1da177e4 | 1869 | * |
1da177e4 LT |
1870 | * If a zone is deemed to be full of pinned pages then just give it a light |
1871 | * scan then give up on it. | |
1872 | */ | |
d1908362 | 1873 | static void shrink_zones(int priority, struct zonelist *zonelist, |
05ff5137 | 1874 | struct scan_control *sc) |
1da177e4 | 1875 | { |
dd1a239f | 1876 | struct zoneref *z; |
54a6eb5c | 1877 | struct zone *zone; |
1cfb419b | 1878 | |
d4debc66 MG |
1879 | for_each_zone_zonelist_nodemask(zone, z, zonelist, |
1880 | gfp_zone(sc->gfp_mask), sc->nodemask) { | |
f3fe6512 | 1881 | if (!populated_zone(zone)) |
1da177e4 | 1882 | continue; |
1cfb419b KH |
1883 | /* |
1884 | * Take care memory controller reclaiming has small influence | |
1885 | * to global LRU. | |
1886 | */ | |
e72e2bd6 | 1887 | if (scanning_global_lru(sc)) { |
1cfb419b KH |
1888 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
1889 | continue; | |
93e4a89a | 1890 | if (zone->all_unreclaimable && priority != DEF_PRIORITY) |
1cfb419b | 1891 | continue; /* Let kswapd poll it */ |
1cfb419b | 1892 | } |
408d8544 | 1893 | |
a79311c1 | 1894 | shrink_zone(priority, zone, sc); |
1da177e4 | 1895 | } |
d1908362 MK |
1896 | } |
1897 | ||
1898 | static bool zone_reclaimable(struct zone *zone) | |
1899 | { | |
1900 | return zone->pages_scanned < zone_reclaimable_pages(zone) * 6; | |
1901 | } | |
1902 | ||
1903 | /* | |
1904 | * As hibernation is going on, kswapd is freezed so that it can't mark | |
1905 | * the zone into all_unreclaimable. It can't handle OOM during hibernation. | |
1906 | * So let's check zone's unreclaimable in direct reclaim as well as kswapd. | |
1907 | */ | |
1908 | static bool all_unreclaimable(struct zonelist *zonelist, | |
1909 | struct scan_control *sc) | |
1910 | { | |
1911 | struct zoneref *z; | |
1912 | struct zone *zone; | |
1913 | bool all_unreclaimable = true; | |
1914 | ||
1915 | for_each_zone_zonelist_nodemask(zone, z, zonelist, | |
1916 | gfp_zone(sc->gfp_mask), sc->nodemask) { | |
1917 | if (!populated_zone(zone)) | |
1918 | continue; | |
1919 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) | |
1920 | continue; | |
1921 | if (zone_reclaimable(zone)) { | |
1922 | all_unreclaimable = false; | |
1923 | break; | |
1924 | } | |
1925 | } | |
1926 | ||
bb21c7ce | 1927 | return all_unreclaimable; |
1da177e4 | 1928 | } |
4f98a2fe | 1929 | |
1da177e4 LT |
1930 | /* |
1931 | * This is the main entry point to direct page reclaim. | |
1932 | * | |
1933 | * If a full scan of the inactive list fails to free enough memory then we | |
1934 | * are "out of memory" and something needs to be killed. | |
1935 | * | |
1936 | * If the caller is !__GFP_FS then the probability of a failure is reasonably | |
1937 | * high - the zone may be full of dirty or under-writeback pages, which this | |
5b0830cb JA |
1938 | * caller can't do much about. We kick the writeback threads and take explicit |
1939 | * naps in the hope that some of these pages can be written. But if the | |
1940 | * allocating task holds filesystem locks which prevent writeout this might not | |
1941 | * work, and the allocation attempt will fail. | |
a41f24ea NA |
1942 | * |
1943 | * returns: 0, if no pages reclaimed | |
1944 | * else, the number of pages reclaimed | |
1da177e4 | 1945 | */ |
dac1d27b | 1946 | static unsigned long do_try_to_free_pages(struct zonelist *zonelist, |
dd1a239f | 1947 | struct scan_control *sc) |
1da177e4 LT |
1948 | { |
1949 | int priority; | |
69e05944 | 1950 | unsigned long total_scanned = 0; |
1da177e4 | 1951 | struct reclaim_state *reclaim_state = current->reclaim_state; |
dd1a239f | 1952 | struct zoneref *z; |
54a6eb5c | 1953 | struct zone *zone; |
22fba335 | 1954 | unsigned long writeback_threshold; |
1da177e4 | 1955 | |
c0ff7453 | 1956 | get_mems_allowed(); |
873b4771 KK |
1957 | delayacct_freepages_start(); |
1958 | ||
e72e2bd6 | 1959 | if (scanning_global_lru(sc)) |
1cfb419b | 1960 | count_vm_event(ALLOCSTALL); |
1da177e4 LT |
1961 | |
1962 | for (priority = DEF_PRIORITY; priority >= 0; priority--) { | |
66e1707b | 1963 | sc->nr_scanned = 0; |
f7b7fd8f RR |
1964 | if (!priority) |
1965 | disable_swap_token(); | |
d1908362 | 1966 | shrink_zones(priority, zonelist, sc); |
66e1707b BS |
1967 | /* |
1968 | * Don't shrink slabs when reclaiming memory from | |
1969 | * over limit cgroups | |
1970 | */ | |
e72e2bd6 | 1971 | if (scanning_global_lru(sc)) { |
c6a8a8c5 | 1972 | unsigned long lru_pages = 0; |
d4debc66 MG |
1973 | for_each_zone_zonelist(zone, z, zonelist, |
1974 | gfp_zone(sc->gfp_mask)) { | |
c6a8a8c5 KM |
1975 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
1976 | continue; | |
1977 | ||
1978 | lru_pages += zone_reclaimable_pages(zone); | |
1979 | } | |
1980 | ||
dd1a239f | 1981 | shrink_slab(sc->nr_scanned, sc->gfp_mask, lru_pages); |
91a45470 | 1982 | if (reclaim_state) { |
a79311c1 | 1983 | sc->nr_reclaimed += reclaim_state->reclaimed_slab; |
91a45470 KH |
1984 | reclaim_state->reclaimed_slab = 0; |
1985 | } | |
1da177e4 | 1986 | } |
66e1707b | 1987 | total_scanned += sc->nr_scanned; |
bb21c7ce | 1988 | if (sc->nr_reclaimed >= sc->nr_to_reclaim) |
1da177e4 | 1989 | goto out; |
1da177e4 LT |
1990 | |
1991 | /* | |
1992 | * Try to write back as many pages as we just scanned. This | |
1993 | * tends to cause slow streaming writers to write data to the | |
1994 | * disk smoothly, at the dirtying rate, which is nice. But | |
1995 | * that's undesirable in laptop mode, where we *want* lumpy | |
1996 | * writeout. So in laptop mode, write out the whole world. | |
1997 | */ | |
22fba335 KM |
1998 | writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2; |
1999 | if (total_scanned > writeback_threshold) { | |
03ba3782 | 2000 | wakeup_flusher_threads(laptop_mode ? 0 : total_scanned); |
66e1707b | 2001 | sc->may_writepage = 1; |
1da177e4 LT |
2002 | } |
2003 | ||
2004 | /* Take a nap, wait for some writeback to complete */ | |
7b51755c | 2005 | if (!sc->hibernation_mode && sc->nr_scanned && |
0e093d99 MG |
2006 | priority < DEF_PRIORITY - 2) { |
2007 | struct zone *preferred_zone; | |
2008 | ||
2009 | first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask), | |
2010 | NULL, &preferred_zone); | |
2011 | wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10); | |
2012 | } | |
1da177e4 | 2013 | } |
bb21c7ce | 2014 | |
1da177e4 | 2015 | out: |
873b4771 | 2016 | delayacct_freepages_end(); |
c0ff7453 | 2017 | put_mems_allowed(); |
873b4771 | 2018 | |
bb21c7ce KM |
2019 | if (sc->nr_reclaimed) |
2020 | return sc->nr_reclaimed; | |
2021 | ||
2022 | /* top priority shrink_zones still had more to do? don't OOM, then */ | |
d1908362 | 2023 | if (scanning_global_lru(sc) && !all_unreclaimable(zonelist, sc)) |
bb21c7ce KM |
2024 | return 1; |
2025 | ||
2026 | return 0; | |
1da177e4 LT |
2027 | } |
2028 | ||
dac1d27b | 2029 | unsigned long try_to_free_pages(struct zonelist *zonelist, int order, |
327c0e96 | 2030 | gfp_t gfp_mask, nodemask_t *nodemask) |
66e1707b | 2031 | { |
33906bc5 | 2032 | unsigned long nr_reclaimed; |
66e1707b BS |
2033 | struct scan_control sc = { |
2034 | .gfp_mask = gfp_mask, | |
2035 | .may_writepage = !laptop_mode, | |
22fba335 | 2036 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
a6dc60f8 | 2037 | .may_unmap = 1, |
2e2e4259 | 2038 | .may_swap = 1, |
66e1707b BS |
2039 | .swappiness = vm_swappiness, |
2040 | .order = order, | |
2041 | .mem_cgroup = NULL, | |
327c0e96 | 2042 | .nodemask = nodemask, |
66e1707b BS |
2043 | }; |
2044 | ||
33906bc5 MG |
2045 | trace_mm_vmscan_direct_reclaim_begin(order, |
2046 | sc.may_writepage, | |
2047 | gfp_mask); | |
2048 | ||
2049 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); | |
2050 | ||
2051 | trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); | |
2052 | ||
2053 | return nr_reclaimed; | |
66e1707b BS |
2054 | } |
2055 | ||
00f0b825 | 2056 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR |
66e1707b | 2057 | |
4e416953 BS |
2058 | unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *mem, |
2059 | gfp_t gfp_mask, bool noswap, | |
2060 | unsigned int swappiness, | |
14fec796 | 2061 | struct zone *zone) |
4e416953 BS |
2062 | { |
2063 | struct scan_control sc = { | |
b8f5c566 | 2064 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
4e416953 BS |
2065 | .may_writepage = !laptop_mode, |
2066 | .may_unmap = 1, | |
2067 | .may_swap = !noswap, | |
4e416953 BS |
2068 | .swappiness = swappiness, |
2069 | .order = 0, | |
2070 | .mem_cgroup = mem, | |
4e416953 | 2071 | }; |
4e416953 BS |
2072 | sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | |
2073 | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); | |
bdce6d9e KM |
2074 | |
2075 | trace_mm_vmscan_memcg_softlimit_reclaim_begin(0, | |
2076 | sc.may_writepage, | |
2077 | sc.gfp_mask); | |
2078 | ||
4e416953 BS |
2079 | /* |
2080 | * NOTE: Although we can get the priority field, using it | |
2081 | * here is not a good idea, since it limits the pages we can scan. | |
2082 | * if we don't reclaim here, the shrink_zone from balance_pgdat | |
2083 | * will pick up pages from other mem cgroup's as well. We hack | |
2084 | * the priority and make it zero. | |
2085 | */ | |
2086 | shrink_zone(0, zone, &sc); | |
bdce6d9e KM |
2087 | |
2088 | trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); | |
2089 | ||
4e416953 BS |
2090 | return sc.nr_reclaimed; |
2091 | } | |
2092 | ||
e1a1cd59 | 2093 | unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont, |
a7885eb8 KM |
2094 | gfp_t gfp_mask, |
2095 | bool noswap, | |
2096 | unsigned int swappiness) | |
66e1707b | 2097 | { |
4e416953 | 2098 | struct zonelist *zonelist; |
bdce6d9e | 2099 | unsigned long nr_reclaimed; |
66e1707b | 2100 | struct scan_control sc = { |
66e1707b | 2101 | .may_writepage = !laptop_mode, |
a6dc60f8 | 2102 | .may_unmap = 1, |
2e2e4259 | 2103 | .may_swap = !noswap, |
22fba335 | 2104 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
a7885eb8 | 2105 | .swappiness = swappiness, |
66e1707b BS |
2106 | .order = 0, |
2107 | .mem_cgroup = mem_cont, | |
327c0e96 | 2108 | .nodemask = NULL, /* we don't care the placement */ |
66e1707b | 2109 | }; |
66e1707b | 2110 | |
dd1a239f MG |
2111 | sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | |
2112 | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); | |
2113 | zonelist = NODE_DATA(numa_node_id())->node_zonelists; | |
bdce6d9e KM |
2114 | |
2115 | trace_mm_vmscan_memcg_reclaim_begin(0, | |
2116 | sc.may_writepage, | |
2117 | sc.gfp_mask); | |
2118 | ||
2119 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); | |
2120 | ||
2121 | trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); | |
2122 | ||
2123 | return nr_reclaimed; | |
66e1707b BS |
2124 | } |
2125 | #endif | |
2126 | ||
f50de2d3 | 2127 | /* is kswapd sleeping prematurely? */ |
bb3ab596 | 2128 | static int sleeping_prematurely(pg_data_t *pgdat, int order, long remaining) |
f50de2d3 | 2129 | { |
bb3ab596 | 2130 | int i; |
f50de2d3 MG |
2131 | |
2132 | /* If a direct reclaimer woke kswapd within HZ/10, it's premature */ | |
2133 | if (remaining) | |
2134 | return 1; | |
2135 | ||
2136 | /* If after HZ/10, a zone is below the high mark, it's premature */ | |
bb3ab596 KM |
2137 | for (i = 0; i < pgdat->nr_zones; i++) { |
2138 | struct zone *zone = pgdat->node_zones + i; | |
2139 | ||
2140 | if (!populated_zone(zone)) | |
2141 | continue; | |
2142 | ||
93e4a89a | 2143 | if (zone->all_unreclaimable) |
de3fab39 KM |
2144 | continue; |
2145 | ||
88f5acf8 | 2146 | if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone), |
f50de2d3 MG |
2147 | 0, 0)) |
2148 | return 1; | |
bb3ab596 | 2149 | } |
f50de2d3 MG |
2150 | |
2151 | return 0; | |
2152 | } | |
2153 | ||
1da177e4 LT |
2154 | /* |
2155 | * For kswapd, balance_pgdat() will work across all this node's zones until | |
41858966 | 2156 | * they are all at high_wmark_pages(zone). |
1da177e4 | 2157 | * |
1da177e4 LT |
2158 | * Returns the number of pages which were actually freed. |
2159 | * | |
2160 | * There is special handling here for zones which are full of pinned pages. | |
2161 | * This can happen if the pages are all mlocked, or if they are all used by | |
2162 | * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. | |
2163 | * What we do is to detect the case where all pages in the zone have been | |
2164 | * scanned twice and there has been zero successful reclaim. Mark the zone as | |
2165 | * dead and from now on, only perform a short scan. Basically we're polling | |
2166 | * the zone for when the problem goes away. | |
2167 | * | |
2168 | * kswapd scans the zones in the highmem->normal->dma direction. It skips | |
41858966 MG |
2169 | * zones which have free_pages > high_wmark_pages(zone), but once a zone is |
2170 | * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the | |
2171 | * lower zones regardless of the number of free pages in the lower zones. This | |
2172 | * interoperates with the page allocator fallback scheme to ensure that aging | |
2173 | * of pages is balanced across the zones. | |
1da177e4 | 2174 | */ |
d6277db4 | 2175 | static unsigned long balance_pgdat(pg_data_t *pgdat, int order) |
1da177e4 | 2176 | { |
1da177e4 LT |
2177 | int all_zones_ok; |
2178 | int priority; | |
2179 | int i; | |
69e05944 | 2180 | unsigned long total_scanned; |
1da177e4 | 2181 | struct reclaim_state *reclaim_state = current->reclaim_state; |
179e9639 AM |
2182 | struct scan_control sc = { |
2183 | .gfp_mask = GFP_KERNEL, | |
a6dc60f8 | 2184 | .may_unmap = 1, |
2e2e4259 | 2185 | .may_swap = 1, |
22fba335 KM |
2186 | /* |
2187 | * kswapd doesn't want to be bailed out while reclaim. because | |
2188 | * we want to put equal scanning pressure on each zone. | |
2189 | */ | |
2190 | .nr_to_reclaim = ULONG_MAX, | |
d6277db4 | 2191 | .swappiness = vm_swappiness, |
5ad333eb | 2192 | .order = order, |
66e1707b | 2193 | .mem_cgroup = NULL, |
179e9639 | 2194 | }; |
1da177e4 LT |
2195 | loop_again: |
2196 | total_scanned = 0; | |
a79311c1 | 2197 | sc.nr_reclaimed = 0; |
c0bbbc73 | 2198 | sc.may_writepage = !laptop_mode; |
f8891e5e | 2199 | count_vm_event(PAGEOUTRUN); |
1da177e4 | 2200 | |
1da177e4 LT |
2201 | for (priority = DEF_PRIORITY; priority >= 0; priority--) { |
2202 | int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ | |
2203 | unsigned long lru_pages = 0; | |
bb3ab596 | 2204 | int has_under_min_watermark_zone = 0; |
1da177e4 | 2205 | |
f7b7fd8f RR |
2206 | /* The swap token gets in the way of swapout... */ |
2207 | if (!priority) | |
2208 | disable_swap_token(); | |
2209 | ||
1da177e4 LT |
2210 | all_zones_ok = 1; |
2211 | ||
d6277db4 RW |
2212 | /* |
2213 | * Scan in the highmem->dma direction for the highest | |
2214 | * zone which needs scanning | |
2215 | */ | |
2216 | for (i = pgdat->nr_zones - 1; i >= 0; i--) { | |
2217 | struct zone *zone = pgdat->node_zones + i; | |
1da177e4 | 2218 | |
d6277db4 RW |
2219 | if (!populated_zone(zone)) |
2220 | continue; | |
1da177e4 | 2221 | |
93e4a89a | 2222 | if (zone->all_unreclaimable && priority != DEF_PRIORITY) |
d6277db4 | 2223 | continue; |
1da177e4 | 2224 | |
556adecb RR |
2225 | /* |
2226 | * Do some background aging of the anon list, to give | |
2227 | * pages a chance to be referenced before reclaiming. | |
2228 | */ | |
14797e23 | 2229 | if (inactive_anon_is_low(zone, &sc)) |
556adecb RR |
2230 | shrink_active_list(SWAP_CLUSTER_MAX, zone, |
2231 | &sc, priority, 0); | |
2232 | ||
88f5acf8 | 2233 | if (!zone_watermark_ok_safe(zone, order, |
41858966 | 2234 | high_wmark_pages(zone), 0, 0)) { |
d6277db4 | 2235 | end_zone = i; |
e1dbeda6 | 2236 | break; |
1da177e4 | 2237 | } |
1da177e4 | 2238 | } |
e1dbeda6 AM |
2239 | if (i < 0) |
2240 | goto out; | |
2241 | ||
1da177e4 LT |
2242 | for (i = 0; i <= end_zone; i++) { |
2243 | struct zone *zone = pgdat->node_zones + i; | |
2244 | ||
adea02a1 | 2245 | lru_pages += zone_reclaimable_pages(zone); |
1da177e4 LT |
2246 | } |
2247 | ||
2248 | /* | |
2249 | * Now scan the zone in the dma->highmem direction, stopping | |
2250 | * at the last zone which needs scanning. | |
2251 | * | |
2252 | * We do this because the page allocator works in the opposite | |
2253 | * direction. This prevents the page allocator from allocating | |
2254 | * pages behind kswapd's direction of progress, which would | |
2255 | * cause too much scanning of the lower zones. | |
2256 | */ | |
2257 | for (i = 0; i <= end_zone; i++) { | |
2258 | struct zone *zone = pgdat->node_zones + i; | |
b15e0905 | 2259 | int nr_slab; |
1da177e4 | 2260 | |
f3fe6512 | 2261 | if (!populated_zone(zone)) |
1da177e4 LT |
2262 | continue; |
2263 | ||
93e4a89a | 2264 | if (zone->all_unreclaimable && priority != DEF_PRIORITY) |
1da177e4 LT |
2265 | continue; |
2266 | ||
1da177e4 | 2267 | sc.nr_scanned = 0; |
4e416953 | 2268 | |
4e416953 BS |
2269 | /* |
2270 | * Call soft limit reclaim before calling shrink_zone. | |
2271 | * For now we ignore the return value | |
2272 | */ | |
00918b6a KM |
2273 | mem_cgroup_soft_limit_reclaim(zone, order, sc.gfp_mask); |
2274 | ||
32a4330d RR |
2275 | /* |
2276 | * We put equal pressure on every zone, unless one | |
2277 | * zone has way too many pages free already. | |
2278 | */ | |
88f5acf8 | 2279 | if (!zone_watermark_ok_safe(zone, order, |
41858966 | 2280 | 8*high_wmark_pages(zone), end_zone, 0)) |
a79311c1 | 2281 | shrink_zone(priority, zone, &sc); |
1da177e4 | 2282 | reclaim_state->reclaimed_slab = 0; |
b15e0905 | 2283 | nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL, |
2284 | lru_pages); | |
a79311c1 | 2285 | sc.nr_reclaimed += reclaim_state->reclaimed_slab; |
1da177e4 | 2286 | total_scanned += sc.nr_scanned; |
93e4a89a | 2287 | if (zone->all_unreclaimable) |
1da177e4 | 2288 | continue; |
d1908362 | 2289 | if (nr_slab == 0 && !zone_reclaimable(zone)) |
93e4a89a | 2290 | zone->all_unreclaimable = 1; |
1da177e4 LT |
2291 | /* |
2292 | * If we've done a decent amount of scanning and | |
2293 | * the reclaim ratio is low, start doing writepage | |
2294 | * even in laptop mode | |
2295 | */ | |
2296 | if (total_scanned > SWAP_CLUSTER_MAX * 2 && | |
a79311c1 | 2297 | total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2) |
1da177e4 | 2298 | sc.may_writepage = 1; |
bb3ab596 | 2299 | |
88f5acf8 | 2300 | if (!zone_watermark_ok_safe(zone, order, |
45973d74 MK |
2301 | high_wmark_pages(zone), end_zone, 0)) { |
2302 | all_zones_ok = 0; | |
2303 | /* | |
2304 | * We are still under min water mark. This | |
2305 | * means that we have a GFP_ATOMIC allocation | |
2306 | * failure risk. Hurry up! | |
2307 | */ | |
88f5acf8 | 2308 | if (!zone_watermark_ok_safe(zone, order, |
45973d74 MK |
2309 | min_wmark_pages(zone), end_zone, 0)) |
2310 | has_under_min_watermark_zone = 1; | |
0e093d99 MG |
2311 | } else { |
2312 | /* | |
2313 | * If a zone reaches its high watermark, | |
2314 | * consider it to be no longer congested. It's | |
2315 | * possible there are dirty pages backed by | |
2316 | * congested BDIs but as pressure is relieved, | |
2317 | * spectulatively avoid congestion waits | |
2318 | */ | |
2319 | zone_clear_flag(zone, ZONE_CONGESTED); | |
45973d74 | 2320 | } |
bb3ab596 | 2321 | |
1da177e4 | 2322 | } |
1da177e4 LT |
2323 | if (all_zones_ok) |
2324 | break; /* kswapd: all done */ | |
2325 | /* | |
2326 | * OK, kswapd is getting into trouble. Take a nap, then take | |
2327 | * another pass across the zones. | |
2328 | */ | |
bb3ab596 KM |
2329 | if (total_scanned && (priority < DEF_PRIORITY - 2)) { |
2330 | if (has_under_min_watermark_zone) | |
2331 | count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT); | |
2332 | else | |
2333 | congestion_wait(BLK_RW_ASYNC, HZ/10); | |
2334 | } | |
1da177e4 LT |
2335 | |
2336 | /* | |
2337 | * We do this so kswapd doesn't build up large priorities for | |
2338 | * example when it is freeing in parallel with allocators. It | |
2339 | * matches the direct reclaim path behaviour in terms of impact | |
2340 | * on zone->*_priority. | |
2341 | */ | |
a79311c1 | 2342 | if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX) |
1da177e4 LT |
2343 | break; |
2344 | } | |
2345 | out: | |
1da177e4 LT |
2346 | if (!all_zones_ok) { |
2347 | cond_resched(); | |
8357376d RW |
2348 | |
2349 | try_to_freeze(); | |
2350 | ||
73ce02e9 KM |
2351 | /* |
2352 | * Fragmentation may mean that the system cannot be | |
2353 | * rebalanced for high-order allocations in all zones. | |
2354 | * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX, | |
2355 | * it means the zones have been fully scanned and are still | |
2356 | * not balanced. For high-order allocations, there is | |
2357 | * little point trying all over again as kswapd may | |
2358 | * infinite loop. | |
2359 | * | |
2360 | * Instead, recheck all watermarks at order-0 as they | |
2361 | * are the most important. If watermarks are ok, kswapd will go | |
2362 | * back to sleep. High-order users can still perform direct | |
2363 | * reclaim if they wish. | |
2364 | */ | |
2365 | if (sc.nr_reclaimed < SWAP_CLUSTER_MAX) | |
2366 | order = sc.order = 0; | |
2367 | ||
1da177e4 LT |
2368 | goto loop_again; |
2369 | } | |
2370 | ||
a79311c1 | 2371 | return sc.nr_reclaimed; |
1da177e4 LT |
2372 | } |
2373 | ||
f0bc0a60 KM |
2374 | static void kswapd_try_to_sleep(pg_data_t *pgdat, int order) |
2375 | { | |
2376 | long remaining = 0; | |
2377 | DEFINE_WAIT(wait); | |
2378 | ||
2379 | if (freezing(current) || kthread_should_stop()) | |
2380 | return; | |
2381 | ||
2382 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); | |
2383 | ||
2384 | /* Try to sleep for a short interval */ | |
2385 | if (!sleeping_prematurely(pgdat, order, remaining)) { | |
2386 | remaining = schedule_timeout(HZ/10); | |
2387 | finish_wait(&pgdat->kswapd_wait, &wait); | |
2388 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); | |
2389 | } | |
2390 | ||
2391 | /* | |
2392 | * After a short sleep, check if it was a premature sleep. If not, then | |
2393 | * go fully to sleep until explicitly woken up. | |
2394 | */ | |
2395 | if (!sleeping_prematurely(pgdat, order, remaining)) { | |
2396 | trace_mm_vmscan_kswapd_sleep(pgdat->node_id); | |
2397 | ||
2398 | /* | |
2399 | * vmstat counters are not perfectly accurate and the estimated | |
2400 | * value for counters such as NR_FREE_PAGES can deviate from the | |
2401 | * true value by nr_online_cpus * threshold. To avoid the zone | |
2402 | * watermarks being breached while under pressure, we reduce the | |
2403 | * per-cpu vmstat threshold while kswapd is awake and restore | |
2404 | * them before going back to sleep. | |
2405 | */ | |
2406 | set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); | |
2407 | schedule(); | |
2408 | set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); | |
2409 | } else { | |
2410 | if (remaining) | |
2411 | count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); | |
2412 | else | |
2413 | count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); | |
2414 | } | |
2415 | finish_wait(&pgdat->kswapd_wait, &wait); | |
2416 | } | |
2417 | ||
1da177e4 LT |
2418 | /* |
2419 | * The background pageout daemon, started as a kernel thread | |
4f98a2fe | 2420 | * from the init process. |
1da177e4 LT |
2421 | * |
2422 | * This basically trickles out pages so that we have _some_ | |
2423 | * free memory available even if there is no other activity | |
2424 | * that frees anything up. This is needed for things like routing | |
2425 | * etc, where we otherwise might have all activity going on in | |
2426 | * asynchronous contexts that cannot page things out. | |
2427 | * | |
2428 | * If there are applications that are active memory-allocators | |
2429 | * (most normal use), this basically shouldn't matter. | |
2430 | */ | |
2431 | static int kswapd(void *p) | |
2432 | { | |
2433 | unsigned long order; | |
2434 | pg_data_t *pgdat = (pg_data_t*)p; | |
2435 | struct task_struct *tsk = current; | |
f0bc0a60 | 2436 | |
1da177e4 LT |
2437 | struct reclaim_state reclaim_state = { |
2438 | .reclaimed_slab = 0, | |
2439 | }; | |
a70f7302 | 2440 | const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); |
1da177e4 | 2441 | |
cf40bd16 NP |
2442 | lockdep_set_current_reclaim_state(GFP_KERNEL); |
2443 | ||
174596a0 | 2444 | if (!cpumask_empty(cpumask)) |
c5f59f08 | 2445 | set_cpus_allowed_ptr(tsk, cpumask); |
1da177e4 LT |
2446 | current->reclaim_state = &reclaim_state; |
2447 | ||
2448 | /* | |
2449 | * Tell the memory management that we're a "memory allocator", | |
2450 | * and that if we need more memory we should get access to it | |
2451 | * regardless (see "__alloc_pages()"). "kswapd" should | |
2452 | * never get caught in the normal page freeing logic. | |
2453 | * | |
2454 | * (Kswapd normally doesn't need memory anyway, but sometimes | |
2455 | * you need a small amount of memory in order to be able to | |
2456 | * page out something else, and this flag essentially protects | |
2457 | * us from recursively trying to free more memory as we're | |
2458 | * trying to free the first piece of memory in the first place). | |
2459 | */ | |
930d9152 | 2460 | tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; |
83144186 | 2461 | set_freezable(); |
1da177e4 LT |
2462 | |
2463 | order = 0; | |
2464 | for ( ; ; ) { | |
2465 | unsigned long new_order; | |
8fe23e05 | 2466 | int ret; |
3e1d1d28 | 2467 | |
1da177e4 LT |
2468 | new_order = pgdat->kswapd_max_order; |
2469 | pgdat->kswapd_max_order = 0; | |
2470 | if (order < new_order) { | |
2471 | /* | |
2472 | * Don't sleep if someone wants a larger 'order' | |
2473 | * allocation | |
2474 | */ | |
2475 | order = new_order; | |
2476 | } else { | |
f0bc0a60 | 2477 | kswapd_try_to_sleep(pgdat, order); |
1da177e4 LT |
2478 | order = pgdat->kswapd_max_order; |
2479 | } | |
1da177e4 | 2480 | |
8fe23e05 DR |
2481 | ret = try_to_freeze(); |
2482 | if (kthread_should_stop()) | |
2483 | break; | |
2484 | ||
2485 | /* | |
2486 | * We can speed up thawing tasks if we don't call balance_pgdat | |
2487 | * after returning from the refrigerator | |
2488 | */ | |
33906bc5 MG |
2489 | if (!ret) { |
2490 | trace_mm_vmscan_kswapd_wake(pgdat->node_id, order); | |
b1296cc4 | 2491 | balance_pgdat(pgdat, order); |
33906bc5 | 2492 | } |
1da177e4 LT |
2493 | } |
2494 | return 0; | |
2495 | } | |
2496 | ||
2497 | /* | |
2498 | * A zone is low on free memory, so wake its kswapd task to service it. | |
2499 | */ | |
2500 | void wakeup_kswapd(struct zone *zone, int order) | |
2501 | { | |
2502 | pg_data_t *pgdat; | |
2503 | ||
f3fe6512 | 2504 | if (!populated_zone(zone)) |
1da177e4 LT |
2505 | return; |
2506 | ||
88f5acf8 | 2507 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
1da177e4 | 2508 | return; |
88f5acf8 | 2509 | pgdat = zone->zone_pgdat; |
1da177e4 LT |
2510 | if (pgdat->kswapd_max_order < order) |
2511 | pgdat->kswapd_max_order = order; | |
8d0986e2 | 2512 | if (!waitqueue_active(&pgdat->kswapd_wait)) |
1da177e4 | 2513 | return; |
88f5acf8 MG |
2514 | if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0)) |
2515 | return; | |
2516 | ||
2517 | trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order); | |
8d0986e2 | 2518 | wake_up_interruptible(&pgdat->kswapd_wait); |
1da177e4 LT |
2519 | } |
2520 | ||
adea02a1 WF |
2521 | /* |
2522 | * The reclaimable count would be mostly accurate. | |
2523 | * The less reclaimable pages may be | |
2524 | * - mlocked pages, which will be moved to unevictable list when encountered | |
2525 | * - mapped pages, which may require several travels to be reclaimed | |
2526 | * - dirty pages, which is not "instantly" reclaimable | |
2527 | */ | |
2528 | unsigned long global_reclaimable_pages(void) | |
4f98a2fe | 2529 | { |
adea02a1 WF |
2530 | int nr; |
2531 | ||
2532 | nr = global_page_state(NR_ACTIVE_FILE) + | |
2533 | global_page_state(NR_INACTIVE_FILE); | |
2534 | ||
2535 | if (nr_swap_pages > 0) | |
2536 | nr += global_page_state(NR_ACTIVE_ANON) + | |
2537 | global_page_state(NR_INACTIVE_ANON); | |
2538 | ||
2539 | return nr; | |
2540 | } | |
2541 | ||
2542 | unsigned long zone_reclaimable_pages(struct zone *zone) | |
2543 | { | |
2544 | int nr; | |
2545 | ||
2546 | nr = zone_page_state(zone, NR_ACTIVE_FILE) + | |
2547 | zone_page_state(zone, NR_INACTIVE_FILE); | |
2548 | ||
2549 | if (nr_swap_pages > 0) | |
2550 | nr += zone_page_state(zone, NR_ACTIVE_ANON) + | |
2551 | zone_page_state(zone, NR_INACTIVE_ANON); | |
2552 | ||
2553 | return nr; | |
4f98a2fe RR |
2554 | } |
2555 | ||
c6f37f12 | 2556 | #ifdef CONFIG_HIBERNATION |
1da177e4 | 2557 | /* |
7b51755c | 2558 | * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of |
d6277db4 RW |
2559 | * freed pages. |
2560 | * | |
2561 | * Rather than trying to age LRUs the aim is to preserve the overall | |
2562 | * LRU order by reclaiming preferentially | |
2563 | * inactive > active > active referenced > active mapped | |
1da177e4 | 2564 | */ |
7b51755c | 2565 | unsigned long shrink_all_memory(unsigned long nr_to_reclaim) |
1da177e4 | 2566 | { |
d6277db4 | 2567 | struct reclaim_state reclaim_state; |
d6277db4 | 2568 | struct scan_control sc = { |
7b51755c KM |
2569 | .gfp_mask = GFP_HIGHUSER_MOVABLE, |
2570 | .may_swap = 1, | |
2571 | .may_unmap = 1, | |
d6277db4 | 2572 | .may_writepage = 1, |
7b51755c KM |
2573 | .nr_to_reclaim = nr_to_reclaim, |
2574 | .hibernation_mode = 1, | |
2575 | .swappiness = vm_swappiness, | |
2576 | .order = 0, | |
1da177e4 | 2577 | }; |
7b51755c KM |
2578 | struct zonelist * zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); |
2579 | struct task_struct *p = current; | |
2580 | unsigned long nr_reclaimed; | |
1da177e4 | 2581 | |
7b51755c KM |
2582 | p->flags |= PF_MEMALLOC; |
2583 | lockdep_set_current_reclaim_state(sc.gfp_mask); | |
2584 | reclaim_state.reclaimed_slab = 0; | |
2585 | p->reclaim_state = &reclaim_state; | |
d6277db4 | 2586 | |
7b51755c | 2587 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
d979677c | 2588 | |
7b51755c KM |
2589 | p->reclaim_state = NULL; |
2590 | lockdep_clear_current_reclaim_state(); | |
2591 | p->flags &= ~PF_MEMALLOC; | |
d6277db4 | 2592 | |
7b51755c | 2593 | return nr_reclaimed; |
1da177e4 | 2594 | } |
c6f37f12 | 2595 | #endif /* CONFIG_HIBERNATION */ |
1da177e4 | 2596 | |
1da177e4 LT |
2597 | /* It's optimal to keep kswapds on the same CPUs as their memory, but |
2598 | not required for correctness. So if the last cpu in a node goes | |
2599 | away, we get changed to run anywhere: as the first one comes back, | |
2600 | restore their cpu bindings. */ | |
9c7b216d | 2601 | static int __devinit cpu_callback(struct notifier_block *nfb, |
69e05944 | 2602 | unsigned long action, void *hcpu) |
1da177e4 | 2603 | { |
58c0a4a7 | 2604 | int nid; |
1da177e4 | 2605 | |
8bb78442 | 2606 | if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { |
58c0a4a7 | 2607 | for_each_node_state(nid, N_HIGH_MEMORY) { |
c5f59f08 | 2608 | pg_data_t *pgdat = NODE_DATA(nid); |
a70f7302 RR |
2609 | const struct cpumask *mask; |
2610 | ||
2611 | mask = cpumask_of_node(pgdat->node_id); | |
c5f59f08 | 2612 | |
3e597945 | 2613 | if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) |
1da177e4 | 2614 | /* One of our CPUs online: restore mask */ |
c5f59f08 | 2615 | set_cpus_allowed_ptr(pgdat->kswapd, mask); |
1da177e4 LT |
2616 | } |
2617 | } | |
2618 | return NOTIFY_OK; | |
2619 | } | |
1da177e4 | 2620 | |
3218ae14 YG |
2621 | /* |
2622 | * This kswapd start function will be called by init and node-hot-add. | |
2623 | * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. | |
2624 | */ | |
2625 | int kswapd_run(int nid) | |
2626 | { | |
2627 | pg_data_t *pgdat = NODE_DATA(nid); | |
2628 | int ret = 0; | |
2629 | ||
2630 | if (pgdat->kswapd) | |
2631 | return 0; | |
2632 | ||
2633 | pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); | |
2634 | if (IS_ERR(pgdat->kswapd)) { | |
2635 | /* failure at boot is fatal */ | |
2636 | BUG_ON(system_state == SYSTEM_BOOTING); | |
2637 | printk("Failed to start kswapd on node %d\n",nid); | |
2638 | ret = -1; | |
2639 | } | |
2640 | return ret; | |
2641 | } | |
2642 | ||
8fe23e05 DR |
2643 | /* |
2644 | * Called by memory hotplug when all memory in a node is offlined. | |
2645 | */ | |
2646 | void kswapd_stop(int nid) | |
2647 | { | |
2648 | struct task_struct *kswapd = NODE_DATA(nid)->kswapd; | |
2649 | ||
2650 | if (kswapd) | |
2651 | kthread_stop(kswapd); | |
2652 | } | |
2653 | ||
1da177e4 LT |
2654 | static int __init kswapd_init(void) |
2655 | { | |
3218ae14 | 2656 | int nid; |
69e05944 | 2657 | |
1da177e4 | 2658 | swap_setup(); |
9422ffba | 2659 | for_each_node_state(nid, N_HIGH_MEMORY) |
3218ae14 | 2660 | kswapd_run(nid); |
1da177e4 LT |
2661 | hotcpu_notifier(cpu_callback, 0); |
2662 | return 0; | |
2663 | } | |
2664 | ||
2665 | module_init(kswapd_init) | |
9eeff239 CL |
2666 | |
2667 | #ifdef CONFIG_NUMA | |
2668 | /* | |
2669 | * Zone reclaim mode | |
2670 | * | |
2671 | * If non-zero call zone_reclaim when the number of free pages falls below | |
2672 | * the watermarks. | |
9eeff239 CL |
2673 | */ |
2674 | int zone_reclaim_mode __read_mostly; | |
2675 | ||
1b2ffb78 | 2676 | #define RECLAIM_OFF 0 |
7d03431c | 2677 | #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ |
1b2ffb78 CL |
2678 | #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ |
2679 | #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ | |
2680 | ||
a92f7126 CL |
2681 | /* |
2682 | * Priority for ZONE_RECLAIM. This determines the fraction of pages | |
2683 | * of a node considered for each zone_reclaim. 4 scans 1/16th of | |
2684 | * a zone. | |
2685 | */ | |
2686 | #define ZONE_RECLAIM_PRIORITY 4 | |
2687 | ||
9614634f CL |
2688 | /* |
2689 | * Percentage of pages in a zone that must be unmapped for zone_reclaim to | |
2690 | * occur. | |
2691 | */ | |
2692 | int sysctl_min_unmapped_ratio = 1; | |
2693 | ||
0ff38490 CL |
2694 | /* |
2695 | * If the number of slab pages in a zone grows beyond this percentage then | |
2696 | * slab reclaim needs to occur. | |
2697 | */ | |
2698 | int sysctl_min_slab_ratio = 5; | |
2699 | ||
90afa5de MG |
2700 | static inline unsigned long zone_unmapped_file_pages(struct zone *zone) |
2701 | { | |
2702 | unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED); | |
2703 | unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) + | |
2704 | zone_page_state(zone, NR_ACTIVE_FILE); | |
2705 | ||
2706 | /* | |
2707 | * It's possible for there to be more file mapped pages than | |
2708 | * accounted for by the pages on the file LRU lists because | |
2709 | * tmpfs pages accounted for as ANON can also be FILE_MAPPED | |
2710 | */ | |
2711 | return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; | |
2712 | } | |
2713 | ||
2714 | /* Work out how many page cache pages we can reclaim in this reclaim_mode */ | |
2715 | static long zone_pagecache_reclaimable(struct zone *zone) | |
2716 | { | |
2717 | long nr_pagecache_reclaimable; | |
2718 | long delta = 0; | |
2719 | ||
2720 | /* | |
2721 | * If RECLAIM_SWAP is set, then all file pages are considered | |
2722 | * potentially reclaimable. Otherwise, we have to worry about | |
2723 | * pages like swapcache and zone_unmapped_file_pages() provides | |
2724 | * a better estimate | |
2725 | */ | |
2726 | if (zone_reclaim_mode & RECLAIM_SWAP) | |
2727 | nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES); | |
2728 | else | |
2729 | nr_pagecache_reclaimable = zone_unmapped_file_pages(zone); | |
2730 | ||
2731 | /* If we can't clean pages, remove dirty pages from consideration */ | |
2732 | if (!(zone_reclaim_mode & RECLAIM_WRITE)) | |
2733 | delta += zone_page_state(zone, NR_FILE_DIRTY); | |
2734 | ||
2735 | /* Watch for any possible underflows due to delta */ | |
2736 | if (unlikely(delta > nr_pagecache_reclaimable)) | |
2737 | delta = nr_pagecache_reclaimable; | |
2738 | ||
2739 | return nr_pagecache_reclaimable - delta; | |
2740 | } | |
2741 | ||
9eeff239 CL |
2742 | /* |
2743 | * Try to free up some pages from this zone through reclaim. | |
2744 | */ | |
179e9639 | 2745 | static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) |
9eeff239 | 2746 | { |
7fb2d46d | 2747 | /* Minimum pages needed in order to stay on node */ |
69e05944 | 2748 | const unsigned long nr_pages = 1 << order; |
9eeff239 CL |
2749 | struct task_struct *p = current; |
2750 | struct reclaim_state reclaim_state; | |
8695949a | 2751 | int priority; |
179e9639 AM |
2752 | struct scan_control sc = { |
2753 | .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), | |
a6dc60f8 | 2754 | .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP), |
2e2e4259 | 2755 | .may_swap = 1, |
22fba335 KM |
2756 | .nr_to_reclaim = max_t(unsigned long, nr_pages, |
2757 | SWAP_CLUSTER_MAX), | |
179e9639 | 2758 | .gfp_mask = gfp_mask, |
d6277db4 | 2759 | .swappiness = vm_swappiness, |
bd2f6199 | 2760 | .order = order, |
179e9639 | 2761 | }; |
15748048 | 2762 | unsigned long nr_slab_pages0, nr_slab_pages1; |
9eeff239 | 2763 | |
9eeff239 | 2764 | cond_resched(); |
d4f7796e CL |
2765 | /* |
2766 | * We need to be able to allocate from the reserves for RECLAIM_SWAP | |
2767 | * and we also need to be able to write out pages for RECLAIM_WRITE | |
2768 | * and RECLAIM_SWAP. | |
2769 | */ | |
2770 | p->flags |= PF_MEMALLOC | PF_SWAPWRITE; | |
76ca542d | 2771 | lockdep_set_current_reclaim_state(gfp_mask); |
9eeff239 CL |
2772 | reclaim_state.reclaimed_slab = 0; |
2773 | p->reclaim_state = &reclaim_state; | |
c84db23c | 2774 | |
90afa5de | 2775 | if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) { |
0ff38490 CL |
2776 | /* |
2777 | * Free memory by calling shrink zone with increasing | |
2778 | * priorities until we have enough memory freed. | |
2779 | */ | |
2780 | priority = ZONE_RECLAIM_PRIORITY; | |
2781 | do { | |
a79311c1 | 2782 | shrink_zone(priority, zone, &sc); |
0ff38490 | 2783 | priority--; |
a79311c1 | 2784 | } while (priority >= 0 && sc.nr_reclaimed < nr_pages); |
0ff38490 | 2785 | } |
c84db23c | 2786 | |
15748048 KM |
2787 | nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); |
2788 | if (nr_slab_pages0 > zone->min_slab_pages) { | |
2a16e3f4 | 2789 | /* |
7fb2d46d | 2790 | * shrink_slab() does not currently allow us to determine how |
0ff38490 CL |
2791 | * many pages were freed in this zone. So we take the current |
2792 | * number of slab pages and shake the slab until it is reduced | |
2793 | * by the same nr_pages that we used for reclaiming unmapped | |
2794 | * pages. | |
2a16e3f4 | 2795 | * |
0ff38490 CL |
2796 | * Note that shrink_slab will free memory on all zones and may |
2797 | * take a long time. | |
2a16e3f4 | 2798 | */ |
4dc4b3d9 KM |
2799 | for (;;) { |
2800 | unsigned long lru_pages = zone_reclaimable_pages(zone); | |
2801 | ||
2802 | /* No reclaimable slab or very low memory pressure */ | |
2803 | if (!shrink_slab(sc.nr_scanned, gfp_mask, lru_pages)) | |
2804 | break; | |
2805 | ||
2806 | /* Freed enough memory */ | |
2807 | nr_slab_pages1 = zone_page_state(zone, | |
2808 | NR_SLAB_RECLAIMABLE); | |
2809 | if (nr_slab_pages1 + nr_pages <= nr_slab_pages0) | |
2810 | break; | |
2811 | } | |
83e33a47 CL |
2812 | |
2813 | /* | |
2814 | * Update nr_reclaimed by the number of slab pages we | |
2815 | * reclaimed from this zone. | |
2816 | */ | |
15748048 KM |
2817 | nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); |
2818 | if (nr_slab_pages1 < nr_slab_pages0) | |
2819 | sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1; | |
2a16e3f4 CL |
2820 | } |
2821 | ||
9eeff239 | 2822 | p->reclaim_state = NULL; |
d4f7796e | 2823 | current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); |
76ca542d | 2824 | lockdep_clear_current_reclaim_state(); |
a79311c1 | 2825 | return sc.nr_reclaimed >= nr_pages; |
9eeff239 | 2826 | } |
179e9639 AM |
2827 | |
2828 | int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) | |
2829 | { | |
179e9639 | 2830 | int node_id; |
d773ed6b | 2831 | int ret; |
179e9639 AM |
2832 | |
2833 | /* | |
0ff38490 CL |
2834 | * Zone reclaim reclaims unmapped file backed pages and |
2835 | * slab pages if we are over the defined limits. | |
34aa1330 | 2836 | * |
9614634f CL |
2837 | * A small portion of unmapped file backed pages is needed for |
2838 | * file I/O otherwise pages read by file I/O will be immediately | |
2839 | * thrown out if the zone is overallocated. So we do not reclaim | |
2840 | * if less than a specified percentage of the zone is used by | |
2841 | * unmapped file backed pages. | |
179e9639 | 2842 | */ |
90afa5de MG |
2843 | if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages && |
2844 | zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages) | |
fa5e084e | 2845 | return ZONE_RECLAIM_FULL; |
179e9639 | 2846 | |
93e4a89a | 2847 | if (zone->all_unreclaimable) |
fa5e084e | 2848 | return ZONE_RECLAIM_FULL; |
d773ed6b | 2849 | |
179e9639 | 2850 | /* |
d773ed6b | 2851 | * Do not scan if the allocation should not be delayed. |
179e9639 | 2852 | */ |
d773ed6b | 2853 | if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC)) |
fa5e084e | 2854 | return ZONE_RECLAIM_NOSCAN; |
179e9639 AM |
2855 | |
2856 | /* | |
2857 | * Only run zone reclaim on the local zone or on zones that do not | |
2858 | * have associated processors. This will favor the local processor | |
2859 | * over remote processors and spread off node memory allocations | |
2860 | * as wide as possible. | |
2861 | */ | |
89fa3024 | 2862 | node_id = zone_to_nid(zone); |
37c0708d | 2863 | if (node_state(node_id, N_CPU) && node_id != numa_node_id()) |
fa5e084e | 2864 | return ZONE_RECLAIM_NOSCAN; |
d773ed6b DR |
2865 | |
2866 | if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED)) | |
fa5e084e MG |
2867 | return ZONE_RECLAIM_NOSCAN; |
2868 | ||
d773ed6b DR |
2869 | ret = __zone_reclaim(zone, gfp_mask, order); |
2870 | zone_clear_flag(zone, ZONE_RECLAIM_LOCKED); | |
2871 | ||
24cf7251 MG |
2872 | if (!ret) |
2873 | count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); | |
2874 | ||
d773ed6b | 2875 | return ret; |
179e9639 | 2876 | } |
9eeff239 | 2877 | #endif |
894bc310 | 2878 | |
894bc310 LS |
2879 | /* |
2880 | * page_evictable - test whether a page is evictable | |
2881 | * @page: the page to test | |
2882 | * @vma: the VMA in which the page is or will be mapped, may be NULL | |
2883 | * | |
2884 | * Test whether page is evictable--i.e., should be placed on active/inactive | |
b291f000 NP |
2885 | * lists vs unevictable list. The vma argument is !NULL when called from the |
2886 | * fault path to determine how to instantate a new page. | |
894bc310 LS |
2887 | * |
2888 | * Reasons page might not be evictable: | |
ba9ddf49 | 2889 | * (1) page's mapping marked unevictable |
b291f000 | 2890 | * (2) page is part of an mlocked VMA |
ba9ddf49 | 2891 | * |
894bc310 LS |
2892 | */ |
2893 | int page_evictable(struct page *page, struct vm_area_struct *vma) | |
2894 | { | |
2895 | ||
ba9ddf49 LS |
2896 | if (mapping_unevictable(page_mapping(page))) |
2897 | return 0; | |
2898 | ||
b291f000 NP |
2899 | if (PageMlocked(page) || (vma && is_mlocked_vma(vma, page))) |
2900 | return 0; | |
894bc310 LS |
2901 | |
2902 | return 1; | |
2903 | } | |
89e004ea LS |
2904 | |
2905 | /** | |
2906 | * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list | |
2907 | * @page: page to check evictability and move to appropriate lru list | |
2908 | * @zone: zone page is in | |
2909 | * | |
2910 | * Checks a page for evictability and moves the page to the appropriate | |
2911 | * zone lru list. | |
2912 | * | |
2913 | * Restrictions: zone->lru_lock must be held, page must be on LRU and must | |
2914 | * have PageUnevictable set. | |
2915 | */ | |
2916 | static void check_move_unevictable_page(struct page *page, struct zone *zone) | |
2917 | { | |
2918 | VM_BUG_ON(PageActive(page)); | |
2919 | ||
2920 | retry: | |
2921 | ClearPageUnevictable(page); | |
2922 | if (page_evictable(page, NULL)) { | |
401a8e1c | 2923 | enum lru_list l = page_lru_base_type(page); |
af936a16 | 2924 | |
89e004ea LS |
2925 | __dec_zone_state(zone, NR_UNEVICTABLE); |
2926 | list_move(&page->lru, &zone->lru[l].list); | |
08e552c6 | 2927 | mem_cgroup_move_lists(page, LRU_UNEVICTABLE, l); |
89e004ea LS |
2928 | __inc_zone_state(zone, NR_INACTIVE_ANON + l); |
2929 | __count_vm_event(UNEVICTABLE_PGRESCUED); | |
2930 | } else { | |
2931 | /* | |
2932 | * rotate unevictable list | |
2933 | */ | |
2934 | SetPageUnevictable(page); | |
2935 | list_move(&page->lru, &zone->lru[LRU_UNEVICTABLE].list); | |
08e552c6 | 2936 | mem_cgroup_rotate_lru_list(page, LRU_UNEVICTABLE); |
89e004ea LS |
2937 | if (page_evictable(page, NULL)) |
2938 | goto retry; | |
2939 | } | |
2940 | } | |
2941 | ||
2942 | /** | |
2943 | * scan_mapping_unevictable_pages - scan an address space for evictable pages | |
2944 | * @mapping: struct address_space to scan for evictable pages | |
2945 | * | |
2946 | * Scan all pages in mapping. Check unevictable pages for | |
2947 | * evictability and move them to the appropriate zone lru list. | |
2948 | */ | |
2949 | void scan_mapping_unevictable_pages(struct address_space *mapping) | |
2950 | { | |
2951 | pgoff_t next = 0; | |
2952 | pgoff_t end = (i_size_read(mapping->host) + PAGE_CACHE_SIZE - 1) >> | |
2953 | PAGE_CACHE_SHIFT; | |
2954 | struct zone *zone; | |
2955 | struct pagevec pvec; | |
2956 | ||
2957 | if (mapping->nrpages == 0) | |
2958 | return; | |
2959 | ||
2960 | pagevec_init(&pvec, 0); | |
2961 | while (next < end && | |
2962 | pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) { | |
2963 | int i; | |
2964 | int pg_scanned = 0; | |
2965 | ||
2966 | zone = NULL; | |
2967 | ||
2968 | for (i = 0; i < pagevec_count(&pvec); i++) { | |
2969 | struct page *page = pvec.pages[i]; | |
2970 | pgoff_t page_index = page->index; | |
2971 | struct zone *pagezone = page_zone(page); | |
2972 | ||
2973 | pg_scanned++; | |
2974 | if (page_index > next) | |
2975 | next = page_index; | |
2976 | next++; | |
2977 | ||
2978 | if (pagezone != zone) { | |
2979 | if (zone) | |
2980 | spin_unlock_irq(&zone->lru_lock); | |
2981 | zone = pagezone; | |
2982 | spin_lock_irq(&zone->lru_lock); | |
2983 | } | |
2984 | ||
2985 | if (PageLRU(page) && PageUnevictable(page)) | |
2986 | check_move_unevictable_page(page, zone); | |
2987 | } | |
2988 | if (zone) | |
2989 | spin_unlock_irq(&zone->lru_lock); | |
2990 | pagevec_release(&pvec); | |
2991 | ||
2992 | count_vm_events(UNEVICTABLE_PGSCANNED, pg_scanned); | |
2993 | } | |
2994 | ||
2995 | } | |
af936a16 LS |
2996 | |
2997 | /** | |
2998 | * scan_zone_unevictable_pages - check unevictable list for evictable pages | |
2999 | * @zone - zone of which to scan the unevictable list | |
3000 | * | |
3001 | * Scan @zone's unevictable LRU lists to check for pages that have become | |
3002 | * evictable. Move those that have to @zone's inactive list where they | |
3003 | * become candidates for reclaim, unless shrink_inactive_zone() decides | |
3004 | * to reactivate them. Pages that are still unevictable are rotated | |
3005 | * back onto @zone's unevictable list. | |
3006 | */ | |
3007 | #define SCAN_UNEVICTABLE_BATCH_SIZE 16UL /* arbitrary lock hold batch size */ | |
14b90b22 | 3008 | static void scan_zone_unevictable_pages(struct zone *zone) |
af936a16 LS |
3009 | { |
3010 | struct list_head *l_unevictable = &zone->lru[LRU_UNEVICTABLE].list; | |
3011 | unsigned long scan; | |
3012 | unsigned long nr_to_scan = zone_page_state(zone, NR_UNEVICTABLE); | |
3013 | ||
3014 | while (nr_to_scan > 0) { | |
3015 | unsigned long batch_size = min(nr_to_scan, | |
3016 | SCAN_UNEVICTABLE_BATCH_SIZE); | |
3017 | ||
3018 | spin_lock_irq(&zone->lru_lock); | |
3019 | for (scan = 0; scan < batch_size; scan++) { | |
3020 | struct page *page = lru_to_page(l_unevictable); | |
3021 | ||
3022 | if (!trylock_page(page)) | |
3023 | continue; | |
3024 | ||
3025 | prefetchw_prev_lru_page(page, l_unevictable, flags); | |
3026 | ||
3027 | if (likely(PageLRU(page) && PageUnevictable(page))) | |
3028 | check_move_unevictable_page(page, zone); | |
3029 | ||
3030 | unlock_page(page); | |
3031 | } | |
3032 | spin_unlock_irq(&zone->lru_lock); | |
3033 | ||
3034 | nr_to_scan -= batch_size; | |
3035 | } | |
3036 | } | |
3037 | ||
3038 | ||
3039 | /** | |
3040 | * scan_all_zones_unevictable_pages - scan all unevictable lists for evictable pages | |
3041 | * | |
3042 | * A really big hammer: scan all zones' unevictable LRU lists to check for | |
3043 | * pages that have become evictable. Move those back to the zones' | |
3044 | * inactive list where they become candidates for reclaim. | |
3045 | * This occurs when, e.g., we have unswappable pages on the unevictable lists, | |
3046 | * and we add swap to the system. As such, it runs in the context of a task | |
3047 | * that has possibly/probably made some previously unevictable pages | |
3048 | * evictable. | |
3049 | */ | |
ff30153b | 3050 | static void scan_all_zones_unevictable_pages(void) |
af936a16 LS |
3051 | { |
3052 | struct zone *zone; | |
3053 | ||
3054 | for_each_zone(zone) { | |
3055 | scan_zone_unevictable_pages(zone); | |
3056 | } | |
3057 | } | |
3058 | ||
3059 | /* | |
3060 | * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of | |
3061 | * all nodes' unevictable lists for evictable pages | |
3062 | */ | |
3063 | unsigned long scan_unevictable_pages; | |
3064 | ||
3065 | int scan_unevictable_handler(struct ctl_table *table, int write, | |
8d65af78 | 3066 | void __user *buffer, |
af936a16 LS |
3067 | size_t *length, loff_t *ppos) |
3068 | { | |
8d65af78 | 3069 | proc_doulongvec_minmax(table, write, buffer, length, ppos); |
af936a16 LS |
3070 | |
3071 | if (write && *(unsigned long *)table->data) | |
3072 | scan_all_zones_unevictable_pages(); | |
3073 | ||
3074 | scan_unevictable_pages = 0; | |
3075 | return 0; | |
3076 | } | |
3077 | ||
e4455abb | 3078 | #ifdef CONFIG_NUMA |
af936a16 LS |
3079 | /* |
3080 | * per node 'scan_unevictable_pages' attribute. On demand re-scan of | |
3081 | * a specified node's per zone unevictable lists for evictable pages. | |
3082 | */ | |
3083 | ||
3084 | static ssize_t read_scan_unevictable_node(struct sys_device *dev, | |
3085 | struct sysdev_attribute *attr, | |
3086 | char *buf) | |
3087 | { | |
3088 | return sprintf(buf, "0\n"); /* always zero; should fit... */ | |
3089 | } | |
3090 | ||
3091 | static ssize_t write_scan_unevictable_node(struct sys_device *dev, | |
3092 | struct sysdev_attribute *attr, | |
3093 | const char *buf, size_t count) | |
3094 | { | |
3095 | struct zone *node_zones = NODE_DATA(dev->id)->node_zones; | |
3096 | struct zone *zone; | |
3097 | unsigned long res; | |
3098 | unsigned long req = strict_strtoul(buf, 10, &res); | |
3099 | ||
3100 | if (!req) | |
3101 | return 1; /* zero is no-op */ | |
3102 | ||
3103 | for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) { | |
3104 | if (!populated_zone(zone)) | |
3105 | continue; | |
3106 | scan_zone_unevictable_pages(zone); | |
3107 | } | |
3108 | return 1; | |
3109 | } | |
3110 | ||
3111 | ||
3112 | static SYSDEV_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR, | |
3113 | read_scan_unevictable_node, | |
3114 | write_scan_unevictable_node); | |
3115 | ||
3116 | int scan_unevictable_register_node(struct node *node) | |
3117 | { | |
3118 | return sysdev_create_file(&node->sysdev, &attr_scan_unevictable_pages); | |
3119 | } | |
3120 | ||
3121 | void scan_unevictable_unregister_node(struct node *node) | |
3122 | { | |
3123 | sysdev_remove_file(&node->sysdev, &attr_scan_unevictable_pages); | |
3124 | } | |
e4455abb | 3125 | #endif |