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