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