ia64/linux-2.6.18-xen.hg

view mm/vmscan.c @ 452:c7ed6fe5dca0

kexec: dont initialise regions in reserve_memory()

There is no need to initialise efi_memmap_res and boot_param_res in
reserve_memory() for the initial xen domain as it is done in
machine_kexec_setup_resources() using values from the kexec hypercall.

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