]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - mm/vmscan.c
[PATCH] buglet in radix_tree_tag_set
[mirror_ubuntu-zesty-kernel.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>
16#include <linux/slab.h>
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>
22#include <linux/file.h>
23#include <linux/writeback.h>
24#include <linux/blkdev.h>
25#include <linux/buffer_head.h> /* for try_to_release_page(),
26 buffer_heads_over_limit */
27#include <linux/mm_inline.h>
28#include <linux/pagevec.h>
29#include <linux/backing-dev.h>
30#include <linux/rmap.h>
31#include <linux/topology.h>
32#include <linux/cpu.h>
33#include <linux/cpuset.h>
34#include <linux/notifier.h>
35#include <linux/rwsem.h>
248a0301 36#include <linux/delay.h>
1da177e4
LT
37
38#include <asm/tlbflush.h>
39#include <asm/div64.h>
40
41#include <linux/swapops.h>
42
0f8053a5
NP
43#include "internal.h"
44
1da177e4 45struct scan_control {
1da177e4
LT
46 /* Incremented by the number of inactive pages that were scanned */
47 unsigned long nr_scanned;
48
1da177e4
LT
49 unsigned long nr_mapped; /* From page_state */
50
1da177e4 51 /* This context's GFP mask */
6daa0e28 52 gfp_t gfp_mask;
1da177e4
LT
53
54 int may_writepage;
55
f1fd1067
CL
56 /* Can pages be swapped as part of reclaim? */
57 int may_swap;
58
1da177e4
LT
59 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
60 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
61 * In this context, it doesn't matter that we scan the
62 * whole list at once. */
63 int swap_cluster_max;
d6277db4
RW
64
65 int swappiness;
1da177e4
LT
66};
67
68/*
69 * The list of shrinker callbacks used by to apply pressure to
70 * ageable caches.
71 */
72struct shrinker {
73 shrinker_t shrinker;
74 struct list_head list;
75 int seeks; /* seeks to recreate an obj */
76 long nr; /* objs pending delete */
77};
78
79#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
80
81#ifdef ARCH_HAS_PREFETCH
82#define prefetch_prev_lru_page(_page, _base, _field) \
83 do { \
84 if ((_page)->lru.prev != _base) { \
85 struct page *prev; \
86 \
87 prev = lru_to_page(&(_page->lru)); \
88 prefetch(&prev->_field); \
89 } \
90 } while (0)
91#else
92#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
93#endif
94
95#ifdef ARCH_HAS_PREFETCHW
96#define prefetchw_prev_lru_page(_page, _base, _field) \
97 do { \
98 if ((_page)->lru.prev != _base) { \
99 struct page *prev; \
100 \
101 prev = lru_to_page(&(_page->lru)); \
102 prefetchw(&prev->_field); \
103 } \
104 } while (0)
105#else
106#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
107#endif
108
109/*
110 * From 0 .. 100. Higher means more swappy.
111 */
112int vm_swappiness = 60;
113static long total_memory;
114
115static LIST_HEAD(shrinker_list);
116static DECLARE_RWSEM(shrinker_rwsem);
117
118/*
119 * Add a shrinker callback to be called from the vm
120 */
121struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
122{
123 struct shrinker *shrinker;
124
125 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
126 if (shrinker) {
127 shrinker->shrinker = theshrinker;
128 shrinker->seeks = seeks;
129 shrinker->nr = 0;
130 down_write(&shrinker_rwsem);
131 list_add_tail(&shrinker->list, &shrinker_list);
132 up_write(&shrinker_rwsem);
133 }
134 return shrinker;
135}
136EXPORT_SYMBOL(set_shrinker);
137
138/*
139 * Remove one
140 */
141void remove_shrinker(struct shrinker *shrinker)
142{
143 down_write(&shrinker_rwsem);
144 list_del(&shrinker->list);
145 up_write(&shrinker_rwsem);
146 kfree(shrinker);
147}
148EXPORT_SYMBOL(remove_shrinker);
149
150#define SHRINK_BATCH 128
151/*
152 * Call the shrink functions to age shrinkable caches
153 *
154 * Here we assume it costs one seek to replace a lru page and that it also
155 * takes a seek to recreate a cache object. With this in mind we age equal
156 * percentages of the lru and ageable caches. This should balance the seeks
157 * generated by these structures.
158 *
159 * If the vm encounted mapped pages on the LRU it increase the pressure on
160 * slab to avoid swapping.
161 *
162 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
163 *
164 * `lru_pages' represents the number of on-LRU pages in all the zones which
165 * are eligible for the caller's allocation attempt. It is used for balancing
166 * slab reclaim versus page reclaim.
b15e0905 167 *
168 * Returns the number of slab objects which we shrunk.
1da177e4 169 */
69e05944
AM
170unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
171 unsigned long lru_pages)
1da177e4
LT
172{
173 struct shrinker *shrinker;
69e05944 174 unsigned long ret = 0;
1da177e4
LT
175
176 if (scanned == 0)
177 scanned = SWAP_CLUSTER_MAX;
178
179 if (!down_read_trylock(&shrinker_rwsem))
b15e0905 180 return 1; /* Assume we'll be able to shrink next time */
1da177e4
LT
181
182 list_for_each_entry(shrinker, &shrinker_list, list) {
183 unsigned long long delta;
184 unsigned long total_scan;
ea164d73 185 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
1da177e4
LT
186
187 delta = (4 * scanned) / shrinker->seeks;
ea164d73 188 delta *= max_pass;
1da177e4
LT
189 do_div(delta, lru_pages + 1);
190 shrinker->nr += delta;
ea164d73
AA
191 if (shrinker->nr < 0) {
192 printk(KERN_ERR "%s: nr=%ld\n",
193 __FUNCTION__, shrinker->nr);
194 shrinker->nr = max_pass;
195 }
196
197 /*
198 * Avoid risking looping forever due to too large nr value:
199 * never try to free more than twice the estimate number of
200 * freeable entries.
201 */
202 if (shrinker->nr > max_pass * 2)
203 shrinker->nr = max_pass * 2;
1da177e4
LT
204
205 total_scan = shrinker->nr;
206 shrinker->nr = 0;
207
208 while (total_scan >= SHRINK_BATCH) {
209 long this_scan = SHRINK_BATCH;
210 int shrink_ret;
b15e0905 211 int nr_before;
1da177e4 212
b15e0905 213 nr_before = (*shrinker->shrinker)(0, gfp_mask);
1da177e4
LT
214 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
215 if (shrink_ret == -1)
216 break;
b15e0905 217 if (shrink_ret < nr_before)
218 ret += nr_before - shrink_ret;
1da177e4
LT
219 mod_page_state(slabs_scanned, this_scan);
220 total_scan -= this_scan;
221
222 cond_resched();
223 }
224
225 shrinker->nr += total_scan;
226 }
227 up_read(&shrinker_rwsem);
b15e0905 228 return ret;
1da177e4
LT
229}
230
231/* Called without lock on whether page is mapped, so answer is unstable */
232static inline int page_mapping_inuse(struct page *page)
233{
234 struct address_space *mapping;
235
236 /* Page is in somebody's page tables. */
237 if (page_mapped(page))
238 return 1;
239
240 /* Be more reluctant to reclaim swapcache than pagecache */
241 if (PageSwapCache(page))
242 return 1;
243
244 mapping = page_mapping(page);
245 if (!mapping)
246 return 0;
247
248 /* File is mmap'd by somebody? */
249 return mapping_mapped(mapping);
250}
251
252static inline int is_page_cache_freeable(struct page *page)
253{
254 return page_count(page) - !!PagePrivate(page) == 2;
255}
256
257static int may_write_to_queue(struct backing_dev_info *bdi)
258{
930d9152 259 if (current->flags & PF_SWAPWRITE)
1da177e4
LT
260 return 1;
261 if (!bdi_write_congested(bdi))
262 return 1;
263 if (bdi == current->backing_dev_info)
264 return 1;
265 return 0;
266}
267
268/*
269 * We detected a synchronous write error writing a page out. Probably
270 * -ENOSPC. We need to propagate that into the address_space for a subsequent
271 * fsync(), msync() or close().
272 *
273 * The tricky part is that after writepage we cannot touch the mapping: nothing
274 * prevents it from being freed up. But we have a ref on the page and once
275 * that page is locked, the mapping is pinned.
276 *
277 * We're allowed to run sleeping lock_page() here because we know the caller has
278 * __GFP_FS.
279 */
280static void handle_write_error(struct address_space *mapping,
281 struct page *page, int error)
282{
283 lock_page(page);
284 if (page_mapping(page) == mapping) {
285 if (error == -ENOSPC)
286 set_bit(AS_ENOSPC, &mapping->flags);
287 else
288 set_bit(AS_EIO, &mapping->flags);
289 }
290 unlock_page(page);
291}
292
293/*
1742f19f
AM
294 * pageout is called by shrink_page_list() for each dirty page.
295 * Calls ->writepage().
1da177e4 296 */
b20a3503 297pageout_t pageout(struct page *page, struct address_space *mapping)
1da177e4
LT
298{
299 /*
300 * If the page is dirty, only perform writeback if that write
301 * will be non-blocking. To prevent this allocation from being
302 * stalled by pagecache activity. But note that there may be
303 * stalls if we need to run get_block(). We could test
304 * PagePrivate for that.
305 *
306 * If this process is currently in generic_file_write() against
307 * this page's queue, we can perform writeback even if that
308 * will block.
309 *
310 * If the page is swapcache, write it back even if that would
311 * block, for some throttling. This happens by accident, because
312 * swap_backing_dev_info is bust: it doesn't reflect the
313 * congestion state of the swapdevs. Easy to fix, if needed.
314 * See swapfile.c:page_queue_congested().
315 */
316 if (!is_page_cache_freeable(page))
317 return PAGE_KEEP;
318 if (!mapping) {
319 /*
320 * Some data journaling orphaned pages can have
321 * page->mapping == NULL while being dirty with clean buffers.
322 */
323aca6c 323 if (PagePrivate(page)) {
1da177e4
LT
324 if (try_to_free_buffers(page)) {
325 ClearPageDirty(page);
326 printk("%s: orphaned page\n", __FUNCTION__);
327 return PAGE_CLEAN;
328 }
329 }
330 return PAGE_KEEP;
331 }
332 if (mapping->a_ops->writepage == NULL)
333 return PAGE_ACTIVATE;
334 if (!may_write_to_queue(mapping->backing_dev_info))
335 return PAGE_KEEP;
336
337 if (clear_page_dirty_for_io(page)) {
338 int res;
339 struct writeback_control wbc = {
340 .sync_mode = WB_SYNC_NONE,
341 .nr_to_write = SWAP_CLUSTER_MAX,
342 .nonblocking = 1,
343 .for_reclaim = 1,
344 };
345
346 SetPageReclaim(page);
347 res = mapping->a_ops->writepage(page, &wbc);
348 if (res < 0)
349 handle_write_error(mapping, page, res);
994fc28c 350 if (res == AOP_WRITEPAGE_ACTIVATE) {
1da177e4
LT
351 ClearPageReclaim(page);
352 return PAGE_ACTIVATE;
353 }
354 if (!PageWriteback(page)) {
355 /* synchronous write or broken a_ops? */
356 ClearPageReclaim(page);
357 }
358
359 return PAGE_SUCCESS;
360 }
361
362 return PAGE_CLEAN;
363}
364
b20a3503 365int remove_mapping(struct address_space *mapping, struct page *page)
49d2e9cc
CL
366{
367 if (!mapping)
368 return 0; /* truncate got there first */
369
370 write_lock_irq(&mapping->tree_lock);
371
372 /*
373 * The non-racy check for busy page. It is critical to check
374 * PageDirty _after_ making sure that the page is freeable and
375 * not in use by anybody. (pagecache + us == 2)
376 */
377 if (unlikely(page_count(page) != 2))
378 goto cannot_free;
379 smp_rmb();
380 if (unlikely(PageDirty(page)))
381 goto cannot_free;
382
383 if (PageSwapCache(page)) {
384 swp_entry_t swap = { .val = page_private(page) };
385 __delete_from_swap_cache(page);
386 write_unlock_irq(&mapping->tree_lock);
387 swap_free(swap);
388 __put_page(page); /* The pagecache ref */
389 return 1;
390 }
391
392 __remove_from_page_cache(page);
393 write_unlock_irq(&mapping->tree_lock);
394 __put_page(page);
395 return 1;
396
397cannot_free:
398 write_unlock_irq(&mapping->tree_lock);
399 return 0;
400}
401
1da177e4 402/*
1742f19f 403 * shrink_page_list() returns the number of reclaimed pages
1da177e4 404 */
1742f19f
AM
405static unsigned long shrink_page_list(struct list_head *page_list,
406 struct scan_control *sc)
1da177e4
LT
407{
408 LIST_HEAD(ret_pages);
409 struct pagevec freed_pvec;
410 int pgactivate = 0;
05ff5137 411 unsigned long nr_reclaimed = 0;
1da177e4
LT
412
413 cond_resched();
414
415 pagevec_init(&freed_pvec, 1);
416 while (!list_empty(page_list)) {
417 struct address_space *mapping;
418 struct page *page;
419 int may_enter_fs;
420 int referenced;
421
422 cond_resched();
423
424 page = lru_to_page(page_list);
425 list_del(&page->lru);
426
427 if (TestSetPageLocked(page))
428 goto keep;
429
430 BUG_ON(PageActive(page));
431
432 sc->nr_scanned++;
80e43426
CL
433
434 if (!sc->may_swap && page_mapped(page))
435 goto keep_locked;
436
1da177e4
LT
437 /* Double the slab pressure for mapped and swapcache pages */
438 if (page_mapped(page) || PageSwapCache(page))
439 sc->nr_scanned++;
440
441 if (PageWriteback(page))
442 goto keep_locked;
443
f7b7fd8f 444 referenced = page_referenced(page, 1);
1da177e4
LT
445 /* In active use or really unfreeable? Activate it. */
446 if (referenced && page_mapping_inuse(page))
447 goto activate_locked;
448
449#ifdef CONFIG_SWAP
450 /*
451 * Anonymous process memory has backing store?
452 * Try to allocate it some swap space here.
453 */
6e5ef1a9 454 if (PageAnon(page) && !PageSwapCache(page))
1480a540 455 if (!add_to_swap(page, GFP_ATOMIC))
1da177e4 456 goto activate_locked;
1da177e4
LT
457#endif /* CONFIG_SWAP */
458
459 mapping = page_mapping(page);
460 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
461 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
462
463 /*
464 * The page is mapped into the page tables of one or more
465 * processes. Try to unmap it here.
466 */
467 if (page_mapped(page) && mapping) {
a48d07af 468 switch (try_to_unmap(page, 0)) {
1da177e4
LT
469 case SWAP_FAIL:
470 goto activate_locked;
471 case SWAP_AGAIN:
472 goto keep_locked;
473 case SWAP_SUCCESS:
474 ; /* try to free the page below */
475 }
476 }
477
478 if (PageDirty(page)) {
479 if (referenced)
480 goto keep_locked;
481 if (!may_enter_fs)
482 goto keep_locked;
52a8363e 483 if (!sc->may_writepage)
1da177e4
LT
484 goto keep_locked;
485
486 /* Page is dirty, try to write it out here */
487 switch(pageout(page, mapping)) {
488 case PAGE_KEEP:
489 goto keep_locked;
490 case PAGE_ACTIVATE:
491 goto activate_locked;
492 case PAGE_SUCCESS:
493 if (PageWriteback(page) || PageDirty(page))
494 goto keep;
495 /*
496 * A synchronous write - probably a ramdisk. Go
497 * ahead and try to reclaim the page.
498 */
499 if (TestSetPageLocked(page))
500 goto keep;
501 if (PageDirty(page) || PageWriteback(page))
502 goto keep_locked;
503 mapping = page_mapping(page);
504 case PAGE_CLEAN:
505 ; /* try to free the page below */
506 }
507 }
508
509 /*
510 * If the page has buffers, try to free the buffer mappings
511 * associated with this page. If we succeed we try to free
512 * the page as well.
513 *
514 * We do this even if the page is PageDirty().
515 * try_to_release_page() does not perform I/O, but it is
516 * possible for a page to have PageDirty set, but it is actually
517 * clean (all its buffers are clean). This happens if the
518 * buffers were written out directly, with submit_bh(). ext3
519 * will do this, as well as the blockdev mapping.
520 * try_to_release_page() will discover that cleanness and will
521 * drop the buffers and mark the page clean - it can be freed.
522 *
523 * Rarely, pages can have buffers and no ->mapping. These are
524 * the pages which were not successfully invalidated in
525 * truncate_complete_page(). We try to drop those buffers here
526 * and if that worked, and the page is no longer mapped into
527 * process address space (page_count == 1) it can be freed.
528 * Otherwise, leave the page on the LRU so it is swappable.
529 */
530 if (PagePrivate(page)) {
531 if (!try_to_release_page(page, sc->gfp_mask))
532 goto activate_locked;
533 if (!mapping && page_count(page) == 1)
534 goto free_it;
535 }
536
49d2e9cc
CL
537 if (!remove_mapping(mapping, page))
538 goto keep_locked;
1da177e4
LT
539
540free_it:
541 unlock_page(page);
05ff5137 542 nr_reclaimed++;
1da177e4
LT
543 if (!pagevec_add(&freed_pvec, page))
544 __pagevec_release_nonlru(&freed_pvec);
545 continue;
546
547activate_locked:
548 SetPageActive(page);
549 pgactivate++;
550keep_locked:
551 unlock_page(page);
552keep:
553 list_add(&page->lru, &ret_pages);
554 BUG_ON(PageLRU(page));
555 }
556 list_splice(&ret_pages, page_list);
557 if (pagevec_count(&freed_pvec))
558 __pagevec_release_nonlru(&freed_pvec);
559 mod_page_state(pgactivate, pgactivate);
05ff5137 560 return nr_reclaimed;
1da177e4
LT
561}
562
563/*
564 * zone->lru_lock is heavily contended. Some of the functions that
565 * shrink the lists perform better by taking out a batch of pages
566 * and working on them outside the LRU lock.
567 *
568 * For pagecache intensive workloads, this function is the hottest
569 * spot in the kernel (apart from copy_*_user functions).
570 *
571 * Appropriate locks must be held before calling this function.
572 *
573 * @nr_to_scan: The number of pages to look through on the list.
574 * @src: The LRU list to pull pages off.
575 * @dst: The temp list to put pages on to.
576 * @scanned: The number of pages that were scanned.
577 *
578 * returns how many pages were moved onto *@dst.
579 */
69e05944
AM
580static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
581 struct list_head *src, struct list_head *dst,
582 unsigned long *scanned)
1da177e4 583{
69e05944 584 unsigned long nr_taken = 0;
1da177e4 585 struct page *page;
c9b02d97 586 unsigned long scan;
1da177e4 587
c9b02d97 588 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
7c8ee9a8 589 struct list_head *target;
1da177e4
LT
590 page = lru_to_page(src);
591 prefetchw_prev_lru_page(page, src, flags);
592
8d438f96
NP
593 BUG_ON(!PageLRU(page));
594
053837fc 595 list_del(&page->lru);
7c8ee9a8
NP
596 target = src;
597 if (likely(get_page_unless_zero(page))) {
053837fc 598 /*
7c8ee9a8
NP
599 * Be careful not to clear PageLRU until after we're
600 * sure the page is not being freed elsewhere -- the
601 * page release code relies on it.
053837fc 602 */
7c8ee9a8
NP
603 ClearPageLRU(page);
604 target = dst;
605 nr_taken++;
606 } /* else it is being freed elsewhere */
46453a6e 607
7c8ee9a8 608 list_add(&page->lru, target);
1da177e4
LT
609 }
610
611 *scanned = scan;
612 return nr_taken;
613}
614
615/*
1742f19f
AM
616 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
617 * of reclaimed pages
1da177e4 618 */
1742f19f
AM
619static unsigned long shrink_inactive_list(unsigned long max_scan,
620 struct zone *zone, struct scan_control *sc)
1da177e4
LT
621{
622 LIST_HEAD(page_list);
623 struct pagevec pvec;
69e05944 624 unsigned long nr_scanned = 0;
05ff5137 625 unsigned long nr_reclaimed = 0;
1da177e4
LT
626
627 pagevec_init(&pvec, 1);
628
629 lru_add_drain();
630 spin_lock_irq(&zone->lru_lock);
69e05944 631 do {
1da177e4 632 struct page *page;
69e05944
AM
633 unsigned long nr_taken;
634 unsigned long nr_scan;
635 unsigned long nr_freed;
1da177e4
LT
636
637 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
638 &zone->inactive_list,
639 &page_list, &nr_scan);
640 zone->nr_inactive -= nr_taken;
641 zone->pages_scanned += nr_scan;
642 spin_unlock_irq(&zone->lru_lock);
643
69e05944 644 nr_scanned += nr_scan;
1742f19f 645 nr_freed = shrink_page_list(&page_list, sc);
05ff5137 646 nr_reclaimed += nr_freed;
a74609fa
NP
647 local_irq_disable();
648 if (current_is_kswapd()) {
649 __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
650 __mod_page_state(kswapd_steal, nr_freed);
651 } else
652 __mod_page_state_zone(zone, pgscan_direct, nr_scan);
653 __mod_page_state_zone(zone, pgsteal, nr_freed);
654
fb8d14e1
WF
655 if (nr_taken == 0)
656 goto done;
657
a74609fa 658 spin_lock(&zone->lru_lock);
1da177e4
LT
659 /*
660 * Put back any unfreeable pages.
661 */
662 while (!list_empty(&page_list)) {
663 page = lru_to_page(&page_list);
8d438f96
NP
664 BUG_ON(PageLRU(page));
665 SetPageLRU(page);
1da177e4
LT
666 list_del(&page->lru);
667 if (PageActive(page))
668 add_page_to_active_list(zone, page);
669 else
670 add_page_to_inactive_list(zone, page);
671 if (!pagevec_add(&pvec, page)) {
672 spin_unlock_irq(&zone->lru_lock);
673 __pagevec_release(&pvec);
674 spin_lock_irq(&zone->lru_lock);
675 }
676 }
69e05944 677 } while (nr_scanned < max_scan);
fb8d14e1 678 spin_unlock(&zone->lru_lock);
1da177e4 679done:
fb8d14e1 680 local_irq_enable();
1da177e4 681 pagevec_release(&pvec);
05ff5137 682 return nr_reclaimed;
1da177e4
LT
683}
684
685/*
686 * This moves pages from the active list to the inactive list.
687 *
688 * We move them the other way if the page is referenced by one or more
689 * processes, from rmap.
690 *
691 * If the pages are mostly unmapped, the processing is fast and it is
692 * appropriate to hold zone->lru_lock across the whole operation. But if
693 * the pages are mapped, the processing is slow (page_referenced()) so we
694 * should drop zone->lru_lock around each page. It's impossible to balance
695 * this, so instead we remove the pages from the LRU while processing them.
696 * It is safe to rely on PG_active against the non-LRU pages in here because
697 * nobody will play with that bit on a non-LRU page.
698 *
699 * The downside is that we have to touch page->_count against each page.
700 * But we had to alter page->flags anyway.
701 */
1742f19f
AM
702static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
703 struct scan_control *sc)
1da177e4 704{
69e05944 705 unsigned long pgmoved;
1da177e4 706 int pgdeactivate = 0;
69e05944 707 unsigned long pgscanned;
1da177e4
LT
708 LIST_HEAD(l_hold); /* The pages which were snipped off */
709 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
710 LIST_HEAD(l_active); /* Pages to go onto the active_list */
711 struct page *page;
712 struct pagevec pvec;
713 int reclaim_mapped = 0;
2903fb16 714
6e5ef1a9 715 if (sc->may_swap) {
2903fb16
CL
716 long mapped_ratio;
717 long distress;
718 long swap_tendency;
719
720 /*
721 * `distress' is a measure of how much trouble we're having
722 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
723 */
724 distress = 100 >> zone->prev_priority;
725
726 /*
727 * The point of this algorithm is to decide when to start
728 * reclaiming mapped memory instead of just pagecache. Work out
729 * how much memory
730 * is mapped.
731 */
732 mapped_ratio = (sc->nr_mapped * 100) / total_memory;
733
734 /*
735 * Now decide how much we really want to unmap some pages. The
736 * mapped ratio is downgraded - just because there's a lot of
737 * mapped memory doesn't necessarily mean that page reclaim
738 * isn't succeeding.
739 *
740 * The distress ratio is important - we don't want to start
741 * going oom.
742 *
743 * A 100% value of vm_swappiness overrides this algorithm
744 * altogether.
745 */
d6277db4 746 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
2903fb16
CL
747
748 /*
749 * Now use this metric to decide whether to start moving mapped
750 * memory onto the inactive list.
751 */
752 if (swap_tendency >= 100)
753 reclaim_mapped = 1;
754 }
1da177e4
LT
755
756 lru_add_drain();
757 spin_lock_irq(&zone->lru_lock);
758 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
759 &l_hold, &pgscanned);
760 zone->pages_scanned += pgscanned;
761 zone->nr_active -= pgmoved;
762 spin_unlock_irq(&zone->lru_lock);
763
1da177e4
LT
764 while (!list_empty(&l_hold)) {
765 cond_resched();
766 page = lru_to_page(&l_hold);
767 list_del(&page->lru);
768 if (page_mapped(page)) {
769 if (!reclaim_mapped ||
770 (total_swap_pages == 0 && PageAnon(page)) ||
f7b7fd8f 771 page_referenced(page, 0)) {
1da177e4
LT
772 list_add(&page->lru, &l_active);
773 continue;
774 }
775 }
776 list_add(&page->lru, &l_inactive);
777 }
778
779 pagevec_init(&pvec, 1);
780 pgmoved = 0;
781 spin_lock_irq(&zone->lru_lock);
782 while (!list_empty(&l_inactive)) {
783 page = lru_to_page(&l_inactive);
784 prefetchw_prev_lru_page(page, &l_inactive, flags);
8d438f96
NP
785 BUG_ON(PageLRU(page));
786 SetPageLRU(page);
4c84cacf
NP
787 BUG_ON(!PageActive(page));
788 ClearPageActive(page);
789
1da177e4
LT
790 list_move(&page->lru, &zone->inactive_list);
791 pgmoved++;
792 if (!pagevec_add(&pvec, page)) {
793 zone->nr_inactive += pgmoved;
794 spin_unlock_irq(&zone->lru_lock);
795 pgdeactivate += pgmoved;
796 pgmoved = 0;
797 if (buffer_heads_over_limit)
798 pagevec_strip(&pvec);
799 __pagevec_release(&pvec);
800 spin_lock_irq(&zone->lru_lock);
801 }
802 }
803 zone->nr_inactive += pgmoved;
804 pgdeactivate += pgmoved;
805 if (buffer_heads_over_limit) {
806 spin_unlock_irq(&zone->lru_lock);
807 pagevec_strip(&pvec);
808 spin_lock_irq(&zone->lru_lock);
809 }
810
811 pgmoved = 0;
812 while (!list_empty(&l_active)) {
813 page = lru_to_page(&l_active);
814 prefetchw_prev_lru_page(page, &l_active, flags);
8d438f96
NP
815 BUG_ON(PageLRU(page));
816 SetPageLRU(page);
1da177e4
LT
817 BUG_ON(!PageActive(page));
818 list_move(&page->lru, &zone->active_list);
819 pgmoved++;
820 if (!pagevec_add(&pvec, page)) {
821 zone->nr_active += pgmoved;
822 pgmoved = 0;
823 spin_unlock_irq(&zone->lru_lock);
824 __pagevec_release(&pvec);
825 spin_lock_irq(&zone->lru_lock);
826 }
827 }
828 zone->nr_active += pgmoved;
a74609fa
NP
829 spin_unlock(&zone->lru_lock);
830
831 __mod_page_state_zone(zone, pgrefill, pgscanned);
832 __mod_page_state(pgdeactivate, pgdeactivate);
833 local_irq_enable();
1da177e4 834
a74609fa 835 pagevec_release(&pvec);
1da177e4
LT
836}
837
838/*
839 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
840 */
05ff5137
AM
841static unsigned long shrink_zone(int priority, struct zone *zone,
842 struct scan_control *sc)
1da177e4
LT
843{
844 unsigned long nr_active;
845 unsigned long nr_inactive;
8695949a 846 unsigned long nr_to_scan;
05ff5137 847 unsigned long nr_reclaimed = 0;
1da177e4 848
53e9a615
MH
849 atomic_inc(&zone->reclaim_in_progress);
850
1da177e4
LT
851 /*
852 * Add one to `nr_to_scan' just to make sure that the kernel will
853 * slowly sift through the active list.
854 */
8695949a 855 zone->nr_scan_active += (zone->nr_active >> priority) + 1;
1da177e4
LT
856 nr_active = zone->nr_scan_active;
857 if (nr_active >= sc->swap_cluster_max)
858 zone->nr_scan_active = 0;
859 else
860 nr_active = 0;
861
8695949a 862 zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
1da177e4
LT
863 nr_inactive = zone->nr_scan_inactive;
864 if (nr_inactive >= sc->swap_cluster_max)
865 zone->nr_scan_inactive = 0;
866 else
867 nr_inactive = 0;
868
1da177e4
LT
869 while (nr_active || nr_inactive) {
870 if (nr_active) {
8695949a 871 nr_to_scan = min(nr_active,
1da177e4 872 (unsigned long)sc->swap_cluster_max);
8695949a 873 nr_active -= nr_to_scan;
1742f19f 874 shrink_active_list(nr_to_scan, zone, sc);
1da177e4
LT
875 }
876
877 if (nr_inactive) {
8695949a 878 nr_to_scan = min(nr_inactive,
1da177e4 879 (unsigned long)sc->swap_cluster_max);
8695949a 880 nr_inactive -= nr_to_scan;
1742f19f
AM
881 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
882 sc);
1da177e4
LT
883 }
884 }
885
886 throttle_vm_writeout();
53e9a615
MH
887
888 atomic_dec(&zone->reclaim_in_progress);
05ff5137 889 return nr_reclaimed;
1da177e4
LT
890}
891
892/*
893 * This is the direct reclaim path, for page-allocating processes. We only
894 * try to reclaim pages from zones which will satisfy the caller's allocation
895 * request.
896 *
897 * We reclaim from a zone even if that zone is over pages_high. Because:
898 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
899 * allocation or
900 * b) The zones may be over pages_high but they must go *over* pages_high to
901 * satisfy the `incremental min' zone defense algorithm.
902 *
903 * Returns the number of reclaimed pages.
904 *
905 * If a zone is deemed to be full of pinned pages then just give it a light
906 * scan then give up on it.
907 */
1742f19f 908static unsigned long shrink_zones(int priority, struct zone **zones,
05ff5137 909 struct scan_control *sc)
1da177e4 910{
05ff5137 911 unsigned long nr_reclaimed = 0;
1da177e4
LT
912 int i;
913
914 for (i = 0; zones[i] != NULL; i++) {
915 struct zone *zone = zones[i];
916
f3fe6512 917 if (!populated_zone(zone))
1da177e4
LT
918 continue;
919
9bf2229f 920 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1da177e4
LT
921 continue;
922
8695949a
CL
923 zone->temp_priority = priority;
924 if (zone->prev_priority > priority)
925 zone->prev_priority = priority;
1da177e4 926
8695949a 927 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1da177e4
LT
928 continue; /* Let kswapd poll it */
929
05ff5137 930 nr_reclaimed += shrink_zone(priority, zone, sc);
1da177e4 931 }
05ff5137 932 return nr_reclaimed;
1da177e4
LT
933}
934
935/*
936 * This is the main entry point to direct page reclaim.
937 *
938 * If a full scan of the inactive list fails to free enough memory then we
939 * are "out of memory" and something needs to be killed.
940 *
941 * If the caller is !__GFP_FS then the probability of a failure is reasonably
942 * high - the zone may be full of dirty or under-writeback pages, which this
943 * caller can't do much about. We kick pdflush and take explicit naps in the
944 * hope that some of these pages can be written. But if the allocating task
945 * holds filesystem locks which prevent writeout this might not work, and the
946 * allocation attempt will fail.
947 */
69e05944 948unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
1da177e4
LT
949{
950 int priority;
951 int ret = 0;
69e05944 952 unsigned long total_scanned = 0;
05ff5137 953 unsigned long nr_reclaimed = 0;
1da177e4 954 struct reclaim_state *reclaim_state = current->reclaim_state;
1da177e4
LT
955 unsigned long lru_pages = 0;
956 int i;
179e9639
AM
957 struct scan_control sc = {
958 .gfp_mask = gfp_mask,
959 .may_writepage = !laptop_mode,
960 .swap_cluster_max = SWAP_CLUSTER_MAX,
961 .may_swap = 1,
d6277db4 962 .swappiness = vm_swappiness,
179e9639 963 };
1da177e4
LT
964
965 inc_page_state(allocstall);
966
967 for (i = 0; zones[i] != NULL; i++) {
968 struct zone *zone = zones[i];
969
9bf2229f 970 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1da177e4
LT
971 continue;
972
973 zone->temp_priority = DEF_PRIORITY;
974 lru_pages += zone->nr_active + zone->nr_inactive;
975 }
976
977 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
978 sc.nr_mapped = read_page_state(nr_mapped);
979 sc.nr_scanned = 0;
f7b7fd8f
RR
980 if (!priority)
981 disable_swap_token();
1742f19f 982 nr_reclaimed += shrink_zones(priority, zones, &sc);
1da177e4
LT
983 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
984 if (reclaim_state) {
05ff5137 985 nr_reclaimed += reclaim_state->reclaimed_slab;
1da177e4
LT
986 reclaim_state->reclaimed_slab = 0;
987 }
988 total_scanned += sc.nr_scanned;
05ff5137 989 if (nr_reclaimed >= sc.swap_cluster_max) {
1da177e4
LT
990 ret = 1;
991 goto out;
992 }
993
994 /*
995 * Try to write back as many pages as we just scanned. This
996 * tends to cause slow streaming writers to write data to the
997 * disk smoothly, at the dirtying rate, which is nice. But
998 * that's undesirable in laptop mode, where we *want* lumpy
999 * writeout. So in laptop mode, write out the whole world.
1000 */
179e9639
AM
1001 if (total_scanned > sc.swap_cluster_max +
1002 sc.swap_cluster_max / 2) {
687a21ce 1003 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
1da177e4
LT
1004 sc.may_writepage = 1;
1005 }
1006
1007 /* Take a nap, wait for some writeback to complete */
1008 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1009 blk_congestion_wait(WRITE, HZ/10);
1010 }
1011out:
1012 for (i = 0; zones[i] != 0; i++) {
1013 struct zone *zone = zones[i];
1014
9bf2229f 1015 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1da177e4
LT
1016 continue;
1017
1018 zone->prev_priority = zone->temp_priority;
1019 }
1020 return ret;
1021}
1022
1023/*
1024 * For kswapd, balance_pgdat() will work across all this node's zones until
1025 * they are all at pages_high.
1026 *
1da177e4
LT
1027 * Returns the number of pages which were actually freed.
1028 *
1029 * There is special handling here for zones which are full of pinned pages.
1030 * This can happen if the pages are all mlocked, or if they are all used by
1031 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1032 * What we do is to detect the case where all pages in the zone have been
1033 * scanned twice and there has been zero successful reclaim. Mark the zone as
1034 * dead and from now on, only perform a short scan. Basically we're polling
1035 * the zone for when the problem goes away.
1036 *
1037 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1038 * zones which have free_pages > pages_high, but once a zone is found to have
1039 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1040 * of the number of free pages in the lower zones. This interoperates with
1041 * the page allocator fallback scheme to ensure that aging of pages is balanced
1042 * across the zones.
1043 */
d6277db4 1044static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
1da177e4 1045{
1da177e4
LT
1046 int all_zones_ok;
1047 int priority;
1048 int i;
69e05944 1049 unsigned long total_scanned;
05ff5137 1050 unsigned long nr_reclaimed;
1da177e4 1051 struct reclaim_state *reclaim_state = current->reclaim_state;
179e9639
AM
1052 struct scan_control sc = {
1053 .gfp_mask = GFP_KERNEL,
1054 .may_swap = 1,
d6277db4
RW
1055 .swap_cluster_max = SWAP_CLUSTER_MAX,
1056 .swappiness = vm_swappiness,
179e9639 1057 };
1da177e4
LT
1058
1059loop_again:
1060 total_scanned = 0;
05ff5137 1061 nr_reclaimed = 0;
c0bbbc73 1062 sc.may_writepage = !laptop_mode;
1da177e4
LT
1063 sc.nr_mapped = read_page_state(nr_mapped);
1064
1065 inc_page_state(pageoutrun);
1066
1067 for (i = 0; i < pgdat->nr_zones; i++) {
1068 struct zone *zone = pgdat->node_zones + i;
1069
1070 zone->temp_priority = DEF_PRIORITY;
1071 }
1072
1073 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1074 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1075 unsigned long lru_pages = 0;
1076
f7b7fd8f
RR
1077 /* The swap token gets in the way of swapout... */
1078 if (!priority)
1079 disable_swap_token();
1080
1da177e4
LT
1081 all_zones_ok = 1;
1082
d6277db4
RW
1083 /*
1084 * Scan in the highmem->dma direction for the highest
1085 * zone which needs scanning
1086 */
1087 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1088 struct zone *zone = pgdat->node_zones + i;
1da177e4 1089
d6277db4
RW
1090 if (!populated_zone(zone))
1091 continue;
1da177e4 1092
d6277db4
RW
1093 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1094 continue;
1da177e4 1095
d6277db4
RW
1096 if (!zone_watermark_ok(zone, order, zone->pages_high,
1097 0, 0)) {
1098 end_zone = i;
1099 goto scan;
1da177e4 1100 }
1da177e4 1101 }
d6277db4 1102 goto out;
1da177e4
LT
1103scan:
1104 for (i = 0; i <= end_zone; i++) {
1105 struct zone *zone = pgdat->node_zones + i;
1106
1107 lru_pages += zone->nr_active + zone->nr_inactive;
1108 }
1109
1110 /*
1111 * Now scan the zone in the dma->highmem direction, stopping
1112 * at the last zone which needs scanning.
1113 *
1114 * We do this because the page allocator works in the opposite
1115 * direction. This prevents the page allocator from allocating
1116 * pages behind kswapd's direction of progress, which would
1117 * cause too much scanning of the lower zones.
1118 */
1119 for (i = 0; i <= end_zone; i++) {
1120 struct zone *zone = pgdat->node_zones + i;
b15e0905 1121 int nr_slab;
1da177e4 1122
f3fe6512 1123 if (!populated_zone(zone))
1da177e4
LT
1124 continue;
1125
1126 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1127 continue;
1128
d6277db4
RW
1129 if (!zone_watermark_ok(zone, order, zone->pages_high,
1130 end_zone, 0))
1131 all_zones_ok = 0;
1da177e4
LT
1132 zone->temp_priority = priority;
1133 if (zone->prev_priority > priority)
1134 zone->prev_priority = priority;
1135 sc.nr_scanned = 0;
05ff5137 1136 nr_reclaimed += shrink_zone(priority, zone, &sc);
1da177e4 1137 reclaim_state->reclaimed_slab = 0;
b15e0905 1138 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1139 lru_pages);
05ff5137 1140 nr_reclaimed += reclaim_state->reclaimed_slab;
1da177e4
LT
1141 total_scanned += sc.nr_scanned;
1142 if (zone->all_unreclaimable)
1143 continue;
b15e0905 1144 if (nr_slab == 0 && zone->pages_scanned >=
1145 (zone->nr_active + zone->nr_inactive) * 4)
1da177e4
LT
1146 zone->all_unreclaimable = 1;
1147 /*
1148 * If we've done a decent amount of scanning and
1149 * the reclaim ratio is low, start doing writepage
1150 * even in laptop mode
1151 */
1152 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
05ff5137 1153 total_scanned > nr_reclaimed + nr_reclaimed / 2)
1da177e4
LT
1154 sc.may_writepage = 1;
1155 }
1da177e4
LT
1156 if (all_zones_ok)
1157 break; /* kswapd: all done */
1158 /*
1159 * OK, kswapd is getting into trouble. Take a nap, then take
1160 * another pass across the zones.
1161 */
1162 if (total_scanned && priority < DEF_PRIORITY - 2)
1163 blk_congestion_wait(WRITE, HZ/10);
1164
1165 /*
1166 * We do this so kswapd doesn't build up large priorities for
1167 * example when it is freeing in parallel with allocators. It
1168 * matches the direct reclaim path behaviour in terms of impact
1169 * on zone->*_priority.
1170 */
d6277db4 1171 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
1da177e4
LT
1172 break;
1173 }
1174out:
1175 for (i = 0; i < pgdat->nr_zones; i++) {
1176 struct zone *zone = pgdat->node_zones + i;
1177
1178 zone->prev_priority = zone->temp_priority;
1179 }
1180 if (!all_zones_ok) {
1181 cond_resched();
1182 goto loop_again;
1183 }
1184
05ff5137 1185 return nr_reclaimed;
1da177e4
LT
1186}
1187
1188/*
1189 * The background pageout daemon, started as a kernel thread
1190 * from the init process.
1191 *
1192 * This basically trickles out pages so that we have _some_
1193 * free memory available even if there is no other activity
1194 * that frees anything up. This is needed for things like routing
1195 * etc, where we otherwise might have all activity going on in
1196 * asynchronous contexts that cannot page things out.
1197 *
1198 * If there are applications that are active memory-allocators
1199 * (most normal use), this basically shouldn't matter.
1200 */
1201static int kswapd(void *p)
1202{
1203 unsigned long order;
1204 pg_data_t *pgdat = (pg_data_t*)p;
1205 struct task_struct *tsk = current;
1206 DEFINE_WAIT(wait);
1207 struct reclaim_state reclaim_state = {
1208 .reclaimed_slab = 0,
1209 };
1210 cpumask_t cpumask;
1211
1212 daemonize("kswapd%d", pgdat->node_id);
1213 cpumask = node_to_cpumask(pgdat->node_id);
1214 if (!cpus_empty(cpumask))
1215 set_cpus_allowed(tsk, cpumask);
1216 current->reclaim_state = &reclaim_state;
1217
1218 /*
1219 * Tell the memory management that we're a "memory allocator",
1220 * and that if we need more memory we should get access to it
1221 * regardless (see "__alloc_pages()"). "kswapd" should
1222 * never get caught in the normal page freeing logic.
1223 *
1224 * (Kswapd normally doesn't need memory anyway, but sometimes
1225 * you need a small amount of memory in order to be able to
1226 * page out something else, and this flag essentially protects
1227 * us from recursively trying to free more memory as we're
1228 * trying to free the first piece of memory in the first place).
1229 */
930d9152 1230 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
1da177e4
LT
1231
1232 order = 0;
1233 for ( ; ; ) {
1234 unsigned long new_order;
3e1d1d28
CL
1235
1236 try_to_freeze();
1da177e4
LT
1237
1238 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1239 new_order = pgdat->kswapd_max_order;
1240 pgdat->kswapd_max_order = 0;
1241 if (order < new_order) {
1242 /*
1243 * Don't sleep if someone wants a larger 'order'
1244 * allocation
1245 */
1246 order = new_order;
1247 } else {
1248 schedule();
1249 order = pgdat->kswapd_max_order;
1250 }
1251 finish_wait(&pgdat->kswapd_wait, &wait);
1252
d6277db4 1253 balance_pgdat(pgdat, order);
1da177e4
LT
1254 }
1255 return 0;
1256}
1257
1258/*
1259 * A zone is low on free memory, so wake its kswapd task to service it.
1260 */
1261void wakeup_kswapd(struct zone *zone, int order)
1262{
1263 pg_data_t *pgdat;
1264
f3fe6512 1265 if (!populated_zone(zone))
1da177e4
LT
1266 return;
1267
1268 pgdat = zone->zone_pgdat;
7fb1d9fc 1269 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
1da177e4
LT
1270 return;
1271 if (pgdat->kswapd_max_order < order)
1272 pgdat->kswapd_max_order = order;
9bf2229f 1273 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1da177e4 1274 return;
8d0986e2 1275 if (!waitqueue_active(&pgdat->kswapd_wait))
1da177e4 1276 return;
8d0986e2 1277 wake_up_interruptible(&pgdat->kswapd_wait);
1da177e4
LT
1278}
1279
1280#ifdef CONFIG_PM
1281/*
d6277db4
RW
1282 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1283 * from LRU lists system-wide, for given pass and priority, and returns the
1284 * number of reclaimed pages
1285 *
1286 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1287 */
1288static unsigned long shrink_all_zones(unsigned long nr_pages, int pass,
1289 int prio, struct scan_control *sc)
1290{
1291 struct zone *zone;
1292 unsigned long nr_to_scan, ret = 0;
1293
1294 for_each_zone(zone) {
1295
1296 if (!populated_zone(zone))
1297 continue;
1298
1299 if (zone->all_unreclaimable && prio != DEF_PRIORITY)
1300 continue;
1301
1302 /* For pass = 0 we don't shrink the active list */
1303 if (pass > 0) {
1304 zone->nr_scan_active += (zone->nr_active >> prio) + 1;
1305 if (zone->nr_scan_active >= nr_pages || pass > 3) {
1306 zone->nr_scan_active = 0;
1307 nr_to_scan = min(nr_pages, zone->nr_active);
1308 shrink_active_list(nr_to_scan, zone, sc);
1309 }
1310 }
1311
1312 zone->nr_scan_inactive += (zone->nr_inactive >> prio) + 1;
1313 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1314 zone->nr_scan_inactive = 0;
1315 nr_to_scan = min(nr_pages, zone->nr_inactive);
1316 ret += shrink_inactive_list(nr_to_scan, zone, sc);
1317 if (ret >= nr_pages)
1318 return ret;
1319 }
1320 }
1321
1322 return ret;
1323}
1324
1325/*
1326 * Try to free `nr_pages' of memory, system-wide, and return the number of
1327 * freed pages.
1328 *
1329 * Rather than trying to age LRUs the aim is to preserve the overall
1330 * LRU order by reclaiming preferentially
1331 * inactive > active > active referenced > active mapped
1da177e4 1332 */
69e05944 1333unsigned long shrink_all_memory(unsigned long nr_pages)
1da177e4 1334{
d6277db4 1335 unsigned long lru_pages, nr_slab;
69e05944 1336 unsigned long ret = 0;
d6277db4
RW
1337 int pass;
1338 struct reclaim_state reclaim_state;
1339 struct zone *zone;
1340 struct scan_control sc = {
1341 .gfp_mask = GFP_KERNEL,
1342 .may_swap = 0,
1343 .swap_cluster_max = nr_pages,
1344 .may_writepage = 1,
1345 .swappiness = vm_swappiness,
1da177e4
LT
1346 };
1347
1348 current->reclaim_state = &reclaim_state;
69e05944 1349
d6277db4
RW
1350 lru_pages = 0;
1351 for_each_zone(zone)
1352 lru_pages += zone->nr_active + zone->nr_inactive;
1353
1354 nr_slab = read_page_state(nr_slab);
1355 /* If slab caches are huge, it's better to hit them first */
1356 while (nr_slab >= lru_pages) {
1357 reclaim_state.reclaimed_slab = 0;
1358 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1359 if (!reclaim_state.reclaimed_slab)
1da177e4 1360 break;
d6277db4
RW
1361
1362 ret += reclaim_state.reclaimed_slab;
1363 if (ret >= nr_pages)
1364 goto out;
1365
1366 nr_slab -= reclaim_state.reclaimed_slab;
1da177e4 1367 }
d6277db4
RW
1368
1369 /*
1370 * We try to shrink LRUs in 5 passes:
1371 * 0 = Reclaim from inactive_list only
1372 * 1 = Reclaim from active list but don't reclaim mapped
1373 * 2 = 2nd pass of type 1
1374 * 3 = Reclaim mapped (normal reclaim)
1375 * 4 = 2nd pass of type 3
1376 */
1377 for (pass = 0; pass < 5; pass++) {
1378 int prio;
1379
1380 /* Needed for shrinking slab caches later on */
1381 if (!lru_pages)
1382 for_each_zone(zone) {
1383 lru_pages += zone->nr_active;
1384 lru_pages += zone->nr_inactive;
1385 }
1386
1387 /* Force reclaiming mapped pages in the passes #3 and #4 */
1388 if (pass > 2) {
1389 sc.may_swap = 1;
1390 sc.swappiness = 100;
1391 }
1392
1393 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1394 unsigned long nr_to_scan = nr_pages - ret;
1395
1396 sc.nr_mapped = read_page_state(nr_mapped);
1397 sc.nr_scanned = 0;
1398
1399 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1400 if (ret >= nr_pages)
1401 goto out;
1402
1403 reclaim_state.reclaimed_slab = 0;
1404 shrink_slab(sc.nr_scanned, sc.gfp_mask, lru_pages);
1405 ret += reclaim_state.reclaimed_slab;
1406 if (ret >= nr_pages)
1407 goto out;
1408
1409 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
1410 blk_congestion_wait(WRITE, HZ / 10);
1411 }
1412
1413 lru_pages = 0;
248a0301 1414 }
d6277db4
RW
1415
1416 /*
1417 * If ret = 0, we could not shrink LRUs, but there may be something
1418 * in slab caches
1419 */
1420 if (!ret)
1421 do {
1422 reclaim_state.reclaimed_slab = 0;
1423 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1424 ret += reclaim_state.reclaimed_slab;
1425 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
1426
1427out:
1da177e4 1428 current->reclaim_state = NULL;
d6277db4 1429
1da177e4
LT
1430 return ret;
1431}
1432#endif
1433
1434#ifdef CONFIG_HOTPLUG_CPU
1435/* It's optimal to keep kswapds on the same CPUs as their memory, but
1436 not required for correctness. So if the last cpu in a node goes
1437 away, we get changed to run anywhere: as the first one comes back,
1438 restore their cpu bindings. */
83d722f7 1439static int cpu_callback(struct notifier_block *nfb,
69e05944 1440 unsigned long action, void *hcpu)
1da177e4
LT
1441{
1442 pg_data_t *pgdat;
1443 cpumask_t mask;
1444
1445 if (action == CPU_ONLINE) {
ec936fc5 1446 for_each_online_pgdat(pgdat) {
1da177e4
LT
1447 mask = node_to_cpumask(pgdat->node_id);
1448 if (any_online_cpu(mask) != NR_CPUS)
1449 /* One of our CPUs online: restore mask */
1450 set_cpus_allowed(pgdat->kswapd, mask);
1451 }
1452 }
1453 return NOTIFY_OK;
1454}
1455#endif /* CONFIG_HOTPLUG_CPU */
1456
1457static int __init kswapd_init(void)
1458{
1459 pg_data_t *pgdat;
69e05944 1460
1da177e4 1461 swap_setup();
ec936fc5 1462 for_each_online_pgdat(pgdat) {
69e05944
AM
1463 pid_t pid;
1464
1465 pid = kernel_thread(kswapd, pgdat, CLONE_KERNEL);
1466 BUG_ON(pid < 0);
05eeae20 1467 read_lock(&tasklist_lock);
69e05944 1468 pgdat->kswapd = find_task_by_pid(pid);
05eeae20 1469 read_unlock(&tasklist_lock);
69e05944 1470 }
1da177e4
LT
1471 total_memory = nr_free_pagecache_pages();
1472 hotcpu_notifier(cpu_callback, 0);
1473 return 0;
1474}
1475
1476module_init(kswapd_init)
9eeff239
CL
1477
1478#ifdef CONFIG_NUMA
1479/*
1480 * Zone reclaim mode
1481 *
1482 * If non-zero call zone_reclaim when the number of free pages falls below
1483 * the watermarks.
1484 *
1485 * In the future we may add flags to the mode. However, the page allocator
1486 * should only have to check that zone_reclaim_mode != 0 before calling
1487 * zone_reclaim().
1488 */
1489int zone_reclaim_mode __read_mostly;
1490
1b2ffb78
CL
1491#define RECLAIM_OFF 0
1492#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1493#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1494#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
2a16e3f4 1495#define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
1b2ffb78 1496
9eeff239
CL
1497/*
1498 * Mininum time between zone reclaim scans
1499 */
2a11ff06 1500int zone_reclaim_interval __read_mostly = 30*HZ;
a92f7126
CL
1501
1502/*
1503 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1504 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1505 * a zone.
1506 */
1507#define ZONE_RECLAIM_PRIORITY 4
1508
9eeff239
CL
1509/*
1510 * Try to free up some pages from this zone through reclaim.
1511 */
179e9639 1512static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
9eeff239 1513{
7fb2d46d 1514 /* Minimum pages needed in order to stay on node */
69e05944 1515 const unsigned long nr_pages = 1 << order;
9eeff239
CL
1516 struct task_struct *p = current;
1517 struct reclaim_state reclaim_state;
8695949a 1518 int priority;
05ff5137 1519 unsigned long nr_reclaimed = 0;
179e9639
AM
1520 struct scan_control sc = {
1521 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1522 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
1523 .nr_mapped = read_page_state(nr_mapped),
69e05944
AM
1524 .swap_cluster_max = max_t(unsigned long, nr_pages,
1525 SWAP_CLUSTER_MAX),
179e9639 1526 .gfp_mask = gfp_mask,
d6277db4 1527 .swappiness = vm_swappiness,
179e9639 1528 };
9eeff239
CL
1529
1530 disable_swap_token();
9eeff239 1531 cond_resched();
d4f7796e
CL
1532 /*
1533 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1534 * and we also need to be able to write out pages for RECLAIM_WRITE
1535 * and RECLAIM_SWAP.
1536 */
1537 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
9eeff239
CL
1538 reclaim_state.reclaimed_slab = 0;
1539 p->reclaim_state = &reclaim_state;
c84db23c 1540
a92f7126
CL
1541 /*
1542 * Free memory by calling shrink zone with increasing priorities
1543 * until we have enough memory freed.
1544 */
8695949a 1545 priority = ZONE_RECLAIM_PRIORITY;
a92f7126 1546 do {
05ff5137 1547 nr_reclaimed += shrink_zone(priority, zone, &sc);
8695949a 1548 priority--;
05ff5137 1549 } while (priority >= 0 && nr_reclaimed < nr_pages);
c84db23c 1550
05ff5137 1551 if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
2a16e3f4 1552 /*
7fb2d46d
CL
1553 * shrink_slab() does not currently allow us to determine how
1554 * many pages were freed in this zone. So we just shake the slab
1555 * a bit and then go off node for this particular allocation
1556 * despite possibly having freed enough memory to allocate in
1557 * this zone. If we freed local memory then the next
1558 * allocations will be local again.
2a16e3f4
CL
1559 *
1560 * shrink_slab will free memory on all zones and may take
1561 * a long time.
1562 */
1563 shrink_slab(sc.nr_scanned, gfp_mask, order);
2a16e3f4
CL
1564 }
1565
9eeff239 1566 p->reclaim_state = NULL;
d4f7796e 1567 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
9eeff239 1568
7fb2d46d
CL
1569 if (nr_reclaimed == 0) {
1570 /*
1571 * We were unable to reclaim enough pages to stay on node. We
1572 * now allow off node accesses for a certain time period before
1573 * trying again to reclaim pages from the local zone.
1574 */
9eeff239 1575 zone->last_unsuccessful_zone_reclaim = jiffies;
7fb2d46d 1576 }
9eeff239 1577
05ff5137 1578 return nr_reclaimed >= nr_pages;
9eeff239 1579}
179e9639
AM
1580
1581int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1582{
1583 cpumask_t mask;
1584 int node_id;
1585
1586 /*
1587 * Do not reclaim if there was a recent unsuccessful attempt at zone
1588 * reclaim. In that case we let allocations go off node for the
1589 * zone_reclaim_interval. Otherwise we would scan for each off-node
1590 * page allocation.
1591 */
1592 if (time_before(jiffies,
1593 zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval))
1594 return 0;
1595
1596 /*
1597 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1598 * not have reclaimable pages and if we should not delay the allocation
1599 * then do not scan.
1600 */
1601 if (!(gfp_mask & __GFP_WAIT) ||
1602 zone->all_unreclaimable ||
1603 atomic_read(&zone->reclaim_in_progress) > 0 ||
1604 (current->flags & PF_MEMALLOC))
1605 return 0;
1606
1607 /*
1608 * Only run zone reclaim on the local zone or on zones that do not
1609 * have associated processors. This will favor the local processor
1610 * over remote processors and spread off node memory allocations
1611 * as wide as possible.
1612 */
1613 node_id = zone->zone_pgdat->node_id;
1614 mask = node_to_cpumask(node_id);
1615 if (!cpus_empty(mask) && node_id != numa_node_id())
1616 return 0;
1617 return __zone_reclaim(zone, gfp_mask, order);
1618}
9eeff239 1619#endif