MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / mm / vmscan.c
blob08361a25d2602142a1cfb44e6c75ca02706d5e6a
1 /*
2 * linux/mm/vmscan.c
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
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.
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/vmstat.h>
23 #include <linux/file.h>
24 #include <linux/writeback.h>
25 #include <linux/blkdev.h>
26 #include <linux/buffer_head.h> /* for try_to_release_page(),
27 buffer_heads_over_limit */
28 #include <linux/mm_inline.h>
29 #include <linux/pagevec.h>
30 #include <linux/backing-dev.h>
31 #include <linux/rmap.h>
32 #include <linux/topology.h>
33 #include <linux/cpu.h>
34 #include <linux/cpuset.h>
35 #include <linux/notifier.h>
36 #include <linux/rwsem.h>
37 #include <linux/delay.h>
38 #include <linux/kthread.h>
40 #include <asm/tlbflush.h>
41 #include <asm/div64.h>
43 #include <linux/swapops.h>
45 #include "internal.h"
47 struct scan_control {
48 /* Incremented by the number of inactive pages that were scanned */
49 unsigned long nr_scanned;
51 /* This context's GFP mask */
52 gfp_t gfp_mask;
54 int may_writepage;
56 /* Can pages be swapped as part of reclaim? */
57 int may_swap;
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;
65 int swappiness;
67 int all_unreclaimable;
71 * The list of shrinker callbacks used by to apply pressure to
72 * ageable caches.
74 struct shrinker {
75 shrinker_t shrinker;
76 struct list_head list;
77 int seeks; /* seeks to recreate an obj */
78 long nr; /* objs pending delete */
81 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
83 #ifdef ARCH_HAS_PREFETCH
84 #define prefetch_prev_lru_page(_page, _base, _field) \
85 do { \
86 if ((_page)->lru.prev != _base) { \
87 struct page *prev; \
89 prev = lru_to_page(&(_page->lru)); \
90 prefetch(&prev->_field); \
91 } \
92 } while (0)
93 #else
94 #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
95 #endif
97 #ifdef ARCH_HAS_PREFETCHW
98 #define prefetchw_prev_lru_page(_page, _base, _field) \
99 do { \
100 if ((_page)->lru.prev != _base) { \
101 struct page *prev; \
103 prev = lru_to_page(&(_page->lru)); \
104 prefetchw(&prev->_field); \
106 } while (0)
107 #else
108 #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
109 #endif
112 * From 0 .. 100. Higher means more swappy.
114 int vm_swappiness = 60;
115 long vm_total_pages; /* The total number of pages which the VM controls */
117 static LIST_HEAD(shrinker_list);
118 static DECLARE_RWSEM(shrinker_rwsem);
121 * Add a shrinker callback to be called from the vm
123 struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
125 struct shrinker *shrinker;
127 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
128 if (shrinker) {
129 shrinker->shrinker = theshrinker;
130 shrinker->seeks = seeks;
131 shrinker->nr = 0;
132 down_write(&shrinker_rwsem);
133 list_add_tail(&shrinker->list, &shrinker_list);
134 up_write(&shrinker_rwsem);
136 return shrinker;
138 EXPORT_SYMBOL(set_shrinker);
141 * Remove one
143 void remove_shrinker(struct shrinker *shrinker)
145 down_write(&shrinker_rwsem);
146 list_del(&shrinker->list);
147 up_write(&shrinker_rwsem);
148 kfree(shrinker);
150 EXPORT_SYMBOL(remove_shrinker);
152 #define SHRINK_BATCH 128
154 * Call the shrink functions to age shrinkable caches
156 * Here we assume it costs one seek to replace a lru page and that it also
157 * takes a seek to recreate a cache object. With this in mind we age equal
158 * percentages of the lru and ageable caches. This should balance the seeks
159 * generated by these structures.
161 * If the vm encounted mapped pages on the LRU it increase the pressure on
162 * slab to avoid swapping.
164 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
166 * `lru_pages' represents the number of on-LRU pages in all the zones which
167 * are eligible for the caller's allocation attempt. It is used for balancing
168 * slab reclaim versus page reclaim.
170 * Returns the number of slab objects which we shrunk.
172 unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
173 unsigned long lru_pages)
175 struct shrinker *shrinker;
176 unsigned long ret = 0;
178 if (scanned == 0)
179 scanned = SWAP_CLUSTER_MAX;
181 if (!down_read_trylock(&shrinker_rwsem))
182 return 1; /* Assume we'll be able to shrink next time */
184 list_for_each_entry(shrinker, &shrinker_list, list) {
185 unsigned long long delta;
186 unsigned long total_scan;
187 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
189 delta = (4 * scanned) / shrinker->seeks;
190 delta *= max_pass;
191 do_div(delta, lru_pages + 1);
192 shrinker->nr += delta;
193 if (shrinker->nr < 0) {
194 printk(KERN_ERR "%s: nr=%ld\n",
195 __FUNCTION__, shrinker->nr);
196 shrinker->nr = max_pass;
200 * Avoid risking looping forever due to too large nr value:
201 * never try to free more than twice the estimate number of
202 * freeable entries.
204 if (shrinker->nr > max_pass * 2)
205 shrinker->nr = max_pass * 2;
207 total_scan = shrinker->nr;
208 shrinker->nr = 0;
210 while (total_scan >= SHRINK_BATCH) {
211 long this_scan = SHRINK_BATCH;
212 int shrink_ret;
213 int nr_before;
215 nr_before = (*shrinker->shrinker)(0, gfp_mask);
216 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
217 if (shrink_ret == -1)
218 break;
219 if (shrink_ret < nr_before)
220 ret += nr_before - shrink_ret;
221 count_vm_events(SLABS_SCANNED, this_scan);
222 total_scan -= this_scan;
224 cond_resched();
227 shrinker->nr += total_scan;
229 up_read(&shrinker_rwsem);
230 return ret;
233 /* Called without lock on whether page is mapped, so answer is unstable */
234 static inline int page_mapping_inuse(struct page *page)
236 struct address_space *mapping;
238 /* Page is in somebody's page tables. */
239 if (page_mapped(page))
240 return 1;
242 /* Be more reluctant to reclaim swapcache than pagecache */
243 if (PageSwapCache(page))
244 return 1;
246 mapping = page_mapping(page);
247 if (!mapping)
248 return 0;
250 /* File is mmap'd by somebody? */
251 return mapping_mapped(mapping);
254 static inline int is_page_cache_freeable(struct page *page)
256 return page_count(page) - !!PagePrivate(page) == 2;
259 static int may_write_to_queue(struct backing_dev_info *bdi)
261 if (current->flags & PF_SWAPWRITE)
262 return 1;
263 if (!bdi_write_congested(bdi))
264 return 1;
265 if (bdi == current->backing_dev_info)
266 return 1;
267 return 0;
271 * We detected a synchronous write error writing a page out. Probably
272 * -ENOSPC. We need to propagate that into the address_space for a subsequent
273 * fsync(), msync() or close().
275 * The tricky part is that after writepage we cannot touch the mapping: nothing
276 * prevents it from being freed up. But we have a ref on the page and once
277 * that page is locked, the mapping is pinned.
279 * We're allowed to run sleeping lock_page() here because we know the caller has
280 * __GFP_FS.
282 static void handle_write_error(struct address_space *mapping,
283 struct page *page, int error)
285 lock_page(page);
286 if (page_mapping(page) == mapping) {
287 if (error == -ENOSPC)
288 set_bit(AS_ENOSPC, &mapping->flags);
289 else
290 set_bit(AS_EIO, &mapping->flags);
292 unlock_page(page);
295 /* possible outcome of pageout() */
296 typedef enum {
297 /* failed to write page out, page is locked */
298 PAGE_KEEP,
299 /* move page to the active list, page is locked */
300 PAGE_ACTIVATE,
301 /* page has been sent to the disk successfully, page is unlocked */
302 PAGE_SUCCESS,
303 /* page is clean and locked */
304 PAGE_CLEAN,
305 } pageout_t;
308 * pageout is called by shrink_page_list() for each dirty page.
309 * Calls ->writepage().
311 static pageout_t pageout(struct page *page, struct address_space *mapping)
314 * If the page is dirty, only perform writeback if that write
315 * will be non-blocking. To prevent this allocation from being
316 * stalled by pagecache activity. But note that there may be
317 * stalls if we need to run get_block(). We could test
318 * PagePrivate for that.
320 * If this process is currently in generic_file_write() against
321 * this page's queue, we can perform writeback even if that
322 * will block.
324 * If the page is swapcache, write it back even if that would
325 * block, for some throttling. This happens by accident, because
326 * swap_backing_dev_info is bust: it doesn't reflect the
327 * congestion state of the swapdevs. Easy to fix, if needed.
328 * See swapfile.c:page_queue_congested().
330 if (!is_page_cache_freeable(page))
331 return PAGE_KEEP;
332 if (!mapping) {
334 * Some data journaling orphaned pages can have
335 * page->mapping == NULL while being dirty with clean buffers.
337 if (PagePrivate(page)) {
338 if (try_to_free_buffers(page)) {
339 ClearPageDirty(page);
340 printk("%s: orphaned page\n", __FUNCTION__);
341 return PAGE_CLEAN;
344 return PAGE_KEEP;
346 if (mapping->a_ops->writepage == NULL)
347 return PAGE_ACTIVATE;
348 if (!may_write_to_queue(mapping->backing_dev_info))
349 return PAGE_KEEP;
351 if (clear_page_dirty_for_io(page)) {
352 int res;
353 struct writeback_control wbc = {
354 .sync_mode = WB_SYNC_NONE,
355 .nr_to_write = SWAP_CLUSTER_MAX,
356 .range_start = 0,
357 .range_end = LLONG_MAX,
358 .nonblocking = 1,
359 .for_reclaim = 1,
362 SetPageReclaim(page);
363 res = mapping->a_ops->writepage(page, &wbc);
364 if (res < 0)
365 handle_write_error(mapping, page, res);
366 if (res == AOP_WRITEPAGE_ACTIVATE) {
367 ClearPageReclaim(page);
368 return PAGE_ACTIVATE;
370 if (!PageWriteback(page)) {
371 /* synchronous write or broken a_ops? */
372 ClearPageReclaim(page);
374 inc_zone_page_state(page, NR_VMSCAN_WRITE);
375 return PAGE_SUCCESS;
378 return PAGE_CLEAN;
382 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
383 * someone else has a ref on the page, abort and return 0. If it was
384 * successfully detached, return 1. Assumes the caller has a single ref on
385 * this page.
387 int remove_mapping(struct address_space *mapping, struct page *page)
389 BUG_ON(!PageLocked(page));
390 BUG_ON(mapping != page_mapping(page));
392 write_lock_irq(&mapping->tree_lock);
394 * The non racy check for a busy page.
396 * Must be careful with the order of the tests. When someone has
397 * a ref to the page, it may be possible that they dirty it then
398 * drop the reference. So if PageDirty is tested before page_count
399 * here, then the following race may occur:
401 * get_user_pages(&page);
402 * [user mapping goes away]
403 * write_to(page);
404 * !PageDirty(page) [good]
405 * SetPageDirty(page);
406 * put_page(page);
407 * !page_count(page) [good, discard it]
409 * [oops, our write_to data is lost]
411 * Reversing the order of the tests ensures such a situation cannot
412 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
413 * load is not satisfied before that of page->_count.
415 * Note that if SetPageDirty is always performed via set_page_dirty,
416 * and thus under tree_lock, then this ordering is not required.
418 if (unlikely(page_count(page) != 2))
419 goto cannot_free;
420 smp_rmb();
421 if (unlikely(PageDirty(page)))
422 goto cannot_free;
424 if (PageSwapCache(page)) {
425 #if 0 // mask by Victor Yu. 03-15-2007
426 swp_entry_t swap = { .val = page_private(page) };
427 #else
428 swp_entry_t swap;
429 swap.val = page_private(page);
430 #endif
431 __delete_from_swap_cache(page);
432 write_unlock_irq(&mapping->tree_lock);
433 swap_free(swap);
434 __put_page(page); /* The pagecache ref */
435 return 1;
438 __remove_from_page_cache(page);
439 write_unlock_irq(&mapping->tree_lock);
440 __put_page(page);
441 return 1;
443 cannot_free:
444 write_unlock_irq(&mapping->tree_lock);
445 return 0;
449 * shrink_page_list() returns the number of reclaimed pages
451 static unsigned long shrink_page_list(struct list_head *page_list,
452 struct scan_control *sc)
454 LIST_HEAD(ret_pages);
455 struct pagevec freed_pvec;
456 int pgactivate = 0;
457 unsigned long nr_reclaimed = 0;
459 cond_resched();
461 pagevec_init(&freed_pvec, 1);
462 while (!list_empty(page_list)) {
463 struct address_space *mapping;
464 struct page *page;
465 int may_enter_fs;
466 int referenced;
468 cond_resched();
470 page = lru_to_page(page_list);
471 list_del(&page->lru);
473 if (TestSetPageLocked(page))
474 goto keep;
476 VM_BUG_ON(PageActive(page));
478 sc->nr_scanned++;
480 if (!sc->may_swap && page_mapped(page))
481 goto keep_locked;
483 /* Double the slab pressure for mapped and swapcache pages */
484 if (page_mapped(page) || PageSwapCache(page))
485 sc->nr_scanned++;
487 if (PageWriteback(page))
488 goto keep_locked;
490 referenced = page_referenced(page, 1);
491 /* In active use or really unfreeable? Activate it. */
492 if (referenced && page_mapping_inuse(page))
493 goto activate_locked;
495 #ifdef CONFIG_SWAP
497 * Anonymous process memory has backing store?
498 * Try to allocate it some swap space here.
500 if (PageAnon(page) && !PageSwapCache(page))
501 if (!add_to_swap(page, GFP_ATOMIC))
502 goto activate_locked;
503 #endif /* CONFIG_SWAP */
505 mapping = page_mapping(page);
506 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
507 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
510 * The page is mapped into the page tables of one or more
511 * processes. Try to unmap it here.
513 if (page_mapped(page) && mapping) {
514 switch (try_to_unmap(page, 0)) {
515 case SWAP_FAIL:
516 goto activate_locked;
517 case SWAP_AGAIN:
518 goto keep_locked;
519 case SWAP_SUCCESS:
520 ; /* try to free the page below */
524 if (PageDirty(page)) {
525 if (referenced)
526 goto keep_locked;
527 if (!may_enter_fs)
528 goto keep_locked;
529 if (!sc->may_writepage)
530 goto keep_locked;
532 /* Page is dirty, try to write it out here */
533 switch(pageout(page, mapping)) {
534 case PAGE_KEEP:
535 goto keep_locked;
536 case PAGE_ACTIVATE:
537 goto activate_locked;
538 case PAGE_SUCCESS:
539 if (PageWriteback(page) || PageDirty(page))
540 goto keep;
542 * A synchronous write - probably a ramdisk. Go
543 * ahead and try to reclaim the page.
545 if (TestSetPageLocked(page))
546 goto keep;
547 if (PageDirty(page) || PageWriteback(page))
548 goto keep_locked;
549 mapping = page_mapping(page);
550 case PAGE_CLEAN:
551 ; /* try to free the page below */
556 * If the page has buffers, try to free the buffer mappings
557 * associated with this page. If we succeed we try to free
558 * the page as well.
560 * We do this even if the page is PageDirty().
561 * try_to_release_page() does not perform I/O, but it is
562 * possible for a page to have PageDirty set, but it is actually
563 * clean (all its buffers are clean). This happens if the
564 * buffers were written out directly, with submit_bh(). ext3
565 * will do this, as well as the blockdev mapping.
566 * try_to_release_page() will discover that cleanness and will
567 * drop the buffers and mark the page clean - it can be freed.
569 * Rarely, pages can have buffers and no ->mapping. These are
570 * the pages which were not successfully invalidated in
571 * truncate_complete_page(). We try to drop those buffers here
572 * and if that worked, and the page is no longer mapped into
573 * process address space (page_count == 1) it can be freed.
574 * Otherwise, leave the page on the LRU so it is swappable.
576 if (PagePrivate(page)) {
577 if (!try_to_release_page(page, sc->gfp_mask))
578 goto activate_locked;
579 if (!mapping && page_count(page) == 1)
580 goto free_it;
583 if (!mapping || !remove_mapping(mapping, page))
584 goto keep_locked;
586 free_it:
587 unlock_page(page);
588 nr_reclaimed++;
589 if (!pagevec_add(&freed_pvec, page))
590 __pagevec_release_nonlru(&freed_pvec);
591 continue;
593 activate_locked:
594 SetPageActive(page);
595 pgactivate++;
596 keep_locked:
597 unlock_page(page);
598 keep:
599 list_add(&page->lru, &ret_pages);
600 VM_BUG_ON(PageLRU(page));
602 list_splice(&ret_pages, page_list);
603 if (pagevec_count(&freed_pvec))
604 __pagevec_release_nonlru(&freed_pvec);
605 count_vm_events(PGACTIVATE, pgactivate);
606 return nr_reclaimed;
610 * zone->lru_lock is heavily contended. Some of the functions that
611 * shrink the lists perform better by taking out a batch of pages
612 * and working on them outside the LRU lock.
614 * For pagecache intensive workloads, this function is the hottest
615 * spot in the kernel (apart from copy_*_user functions).
617 * Appropriate locks must be held before calling this function.
619 * @nr_to_scan: The number of pages to look through on the list.
620 * @src: The LRU list to pull pages off.
621 * @dst: The temp list to put pages on to.
622 * @scanned: The number of pages that were scanned.
624 * returns how many pages were moved onto *@dst.
626 static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
627 struct list_head *src, struct list_head *dst,
628 unsigned long *scanned)
630 unsigned long nr_taken = 0;
631 struct page *page;
632 unsigned long scan;
634 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
635 struct list_head *target;
636 page = lru_to_page(src);
637 prefetchw_prev_lru_page(page, src, flags);
639 VM_BUG_ON(!PageLRU(page));
641 list_del(&page->lru);
642 target = src;
643 if (likely(get_page_unless_zero(page))) {
645 * Be careful not to clear PageLRU until after we're
646 * sure the page is not being freed elsewhere -- the
647 * page release code relies on it.
649 ClearPageLRU(page);
650 target = dst;
651 nr_taken++;
652 } /* else it is being freed elsewhere */
654 list_add(&page->lru, target);
657 *scanned = scan;
658 return nr_taken;
662 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
663 * of reclaimed pages
665 static unsigned long shrink_inactive_list(unsigned long max_scan,
666 struct zone *zone, struct scan_control *sc)
668 LIST_HEAD(page_list);
669 struct pagevec pvec;
670 unsigned long nr_scanned = 0;
671 unsigned long nr_reclaimed = 0;
673 pagevec_init(&pvec, 1);
675 lru_add_drain();
676 spin_lock_irq(&zone->lru_lock);
677 do {
678 struct page *page;
679 unsigned long nr_taken;
680 unsigned long nr_scan;
681 unsigned long nr_freed;
683 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
684 &zone->inactive_list,
685 &page_list, &nr_scan);
686 zone->nr_inactive -= nr_taken;
687 zone->pages_scanned += nr_scan;
688 spin_unlock_irq(&zone->lru_lock);
690 nr_scanned += nr_scan;
691 nr_freed = shrink_page_list(&page_list, sc);
692 nr_reclaimed += nr_freed;
693 local_irq_disable();
694 if (current_is_kswapd()) {
695 __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
696 __count_vm_events(KSWAPD_STEAL, nr_freed);
697 } else
698 __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
699 __count_vm_events(PGACTIVATE, nr_freed);
701 if (nr_taken == 0)
702 goto done;
704 spin_lock(&zone->lru_lock);
706 * Put back any unfreeable pages.
708 while (!list_empty(&page_list)) {
709 page = lru_to_page(&page_list);
710 VM_BUG_ON(PageLRU(page));
711 SetPageLRU(page);
712 list_del(&page->lru);
713 if (PageActive(page))
714 add_page_to_active_list(zone, page);
715 else
716 add_page_to_inactive_list(zone, page);
717 if (!pagevec_add(&pvec, page)) {
718 spin_unlock_irq(&zone->lru_lock);
719 __pagevec_release(&pvec);
720 spin_lock_irq(&zone->lru_lock);
723 } while (nr_scanned < max_scan);
724 spin_unlock(&zone->lru_lock);
725 done:
726 local_irq_enable();
727 pagevec_release(&pvec);
728 return nr_reclaimed;
732 * We are about to scan this zone at a certain priority level. If that priority
733 * level is smaller (ie: more urgent) than the previous priority, then note
734 * that priority level within the zone. This is done so that when the next
735 * process comes in to scan this zone, it will immediately start out at this
736 * priority level rather than having to build up its own scanning priority.
737 * Here, this priority affects only the reclaim-mapped threshold.
739 static inline void note_zone_scanning_priority(struct zone *zone, int priority)
741 if (priority < zone->prev_priority)
742 zone->prev_priority = priority;
745 static inline int zone_is_near_oom(struct zone *zone)
747 return zone->pages_scanned >= (zone->nr_active + zone->nr_inactive)*3;
751 * This moves pages from the active list to the inactive list.
753 * We move them the other way if the page is referenced by one or more
754 * processes, from rmap.
756 * If the pages are mostly unmapped, the processing is fast and it is
757 * appropriate to hold zone->lru_lock across the whole operation. But if
758 * the pages are mapped, the processing is slow (page_referenced()) so we
759 * should drop zone->lru_lock around each page. It's impossible to balance
760 * this, so instead we remove the pages from the LRU while processing them.
761 * It is safe to rely on PG_active against the non-LRU pages in here because
762 * nobody will play with that bit on a non-LRU page.
764 * The downside is that we have to touch page->_count against each page.
765 * But we had to alter page->flags anyway.
767 static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
768 struct scan_control *sc, int priority)
770 unsigned long pgmoved;
771 int pgdeactivate = 0;
772 unsigned long pgscanned;
773 LIST_HEAD(l_hold); /* The pages which were snipped off */
774 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
775 LIST_HEAD(l_active); /* Pages to go onto the active_list */
776 struct page *page;
777 struct pagevec pvec;
778 int reclaim_mapped = 0;
780 if (sc->may_swap) {
781 long mapped_ratio;
782 long distress;
783 long swap_tendency;
785 if (zone_is_near_oom(zone))
786 goto force_reclaim_mapped;
789 * `distress' is a measure of how much trouble we're having
790 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
792 distress = 100 >> min(zone->prev_priority, priority);
795 * The point of this algorithm is to decide when to start
796 * reclaiming mapped memory instead of just pagecache. Work out
797 * how much memory
798 * is mapped.
800 mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
801 global_page_state(NR_ANON_PAGES)) * 100) /
802 vm_total_pages;
805 * Now decide how much we really want to unmap some pages. The
806 * mapped ratio is downgraded - just because there's a lot of
807 * mapped memory doesn't necessarily mean that page reclaim
808 * isn't succeeding.
810 * The distress ratio is important - we don't want to start
811 * going oom.
813 * A 100% value of vm_swappiness overrides this algorithm
814 * altogether.
816 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
819 * Now use this metric to decide whether to start moving mapped
820 * memory onto the inactive list.
822 if (swap_tendency >= 100)
823 force_reclaim_mapped:
824 reclaim_mapped = 1;
827 lru_add_drain();
828 spin_lock_irq(&zone->lru_lock);
829 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
830 &l_hold, &pgscanned);
831 zone->pages_scanned += pgscanned;
832 zone->nr_active -= pgmoved;
833 spin_unlock_irq(&zone->lru_lock);
835 while (!list_empty(&l_hold)) {
836 cond_resched();
837 page = lru_to_page(&l_hold);
838 list_del(&page->lru);
839 if (page_mapped(page)) {
840 if (!reclaim_mapped ||
841 (total_swap_pages == 0 && PageAnon(page)) ||
842 page_referenced(page, 0)) {
843 list_add(&page->lru, &l_active);
844 continue;
847 list_add(&page->lru, &l_inactive);
850 pagevec_init(&pvec, 1);
851 pgmoved = 0;
852 spin_lock_irq(&zone->lru_lock);
853 while (!list_empty(&l_inactive)) {
854 page = lru_to_page(&l_inactive);
855 prefetchw_prev_lru_page(page, &l_inactive, flags);
856 VM_BUG_ON(PageLRU(page));
857 SetPageLRU(page);
858 VM_BUG_ON(!PageActive(page));
859 ClearPageActive(page);
861 list_move(&page->lru, &zone->inactive_list);
862 pgmoved++;
863 if (!pagevec_add(&pvec, page)) {
864 zone->nr_inactive += pgmoved;
865 spin_unlock_irq(&zone->lru_lock);
866 pgdeactivate += pgmoved;
867 pgmoved = 0;
868 if (buffer_heads_over_limit)
869 pagevec_strip(&pvec);
870 __pagevec_release(&pvec);
871 spin_lock_irq(&zone->lru_lock);
874 zone->nr_inactive += pgmoved;
875 pgdeactivate += pgmoved;
876 if (buffer_heads_over_limit) {
877 spin_unlock_irq(&zone->lru_lock);
878 pagevec_strip(&pvec);
879 spin_lock_irq(&zone->lru_lock);
882 pgmoved = 0;
883 while (!list_empty(&l_active)) {
884 page = lru_to_page(&l_active);
885 prefetchw_prev_lru_page(page, &l_active, flags);
886 VM_BUG_ON(PageLRU(page));
887 SetPageLRU(page);
888 VM_BUG_ON(!PageActive(page));
889 list_move(&page->lru, &zone->active_list);
890 pgmoved++;
891 if (!pagevec_add(&pvec, page)) {
892 zone->nr_active += pgmoved;
893 pgmoved = 0;
894 spin_unlock_irq(&zone->lru_lock);
895 __pagevec_release(&pvec);
896 spin_lock_irq(&zone->lru_lock);
899 zone->nr_active += pgmoved;
901 __count_zone_vm_events(PGREFILL, zone, pgscanned);
902 __count_vm_events(PGDEACTIVATE, pgdeactivate);
903 spin_unlock_irq(&zone->lru_lock);
905 pagevec_release(&pvec);
909 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
911 static unsigned long shrink_zone(int priority, struct zone *zone,
912 struct scan_control *sc)
914 unsigned long nr_active;
915 unsigned long nr_inactive;
916 unsigned long nr_to_scan;
917 unsigned long nr_reclaimed = 0;
919 atomic_inc(&zone->reclaim_in_progress);
922 * Add one to `nr_to_scan' just to make sure that the kernel will
923 * slowly sift through the active list.
925 zone->nr_scan_active += (zone->nr_active >> priority) + 1;
926 nr_active = zone->nr_scan_active;
927 if (nr_active >= sc->swap_cluster_max)
928 zone->nr_scan_active = 0;
929 else
930 nr_active = 0;
932 zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
933 nr_inactive = zone->nr_scan_inactive;
934 if (nr_inactive >= sc->swap_cluster_max)
935 zone->nr_scan_inactive = 0;
936 else
937 nr_inactive = 0;
939 while (nr_active || nr_inactive) {
940 if (nr_active) {
941 nr_to_scan = min(nr_active,
942 (unsigned long)sc->swap_cluster_max);
943 nr_active -= nr_to_scan;
944 shrink_active_list(nr_to_scan, zone, sc, priority);
947 if (nr_inactive) {
948 nr_to_scan = min(nr_inactive,
949 (unsigned long)sc->swap_cluster_max);
950 nr_inactive -= nr_to_scan;
951 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
952 sc);
956 throttle_vm_writeout();
958 atomic_dec(&zone->reclaim_in_progress);
959 return nr_reclaimed;
963 * This is the direct reclaim path, for page-allocating processes. We only
964 * try to reclaim pages from zones which will satisfy the caller's allocation
965 * request.
967 * We reclaim from a zone even if that zone is over pages_high. Because:
968 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
969 * allocation or
970 * b) The zones may be over pages_high but they must go *over* pages_high to
971 * satisfy the `incremental min' zone defense algorithm.
973 * Returns the number of reclaimed pages.
975 * If a zone is deemed to be full of pinned pages then just give it a light
976 * scan then give up on it.
978 static unsigned long shrink_zones(int priority, struct zone **zones,
979 struct scan_control *sc)
981 unsigned long nr_reclaimed = 0;
982 int i;
984 sc->all_unreclaimable = 1;
985 for (i = 0; zones[i] != NULL; i++) {
986 struct zone *zone = zones[i];
988 if (!populated_zone(zone))
989 continue;
991 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
992 continue;
994 note_zone_scanning_priority(zone, priority);
996 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
997 continue; /* Let kswapd poll it */
999 sc->all_unreclaimable = 0;
1001 nr_reclaimed += shrink_zone(priority, zone, sc);
1003 return nr_reclaimed;
1007 * This is the main entry point to direct page reclaim.
1009 * If a full scan of the inactive list fails to free enough memory then we
1010 * are "out of memory" and something needs to be killed.
1012 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1013 * high - the zone may be full of dirty or under-writeback pages, which this
1014 * caller can't do much about. We kick pdflush and take explicit naps in the
1015 * hope that some of these pages can be written. But if the allocating task
1016 * holds filesystem locks which prevent writeout this might not work, and the
1017 * allocation attempt will fail.
1019 unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
1021 int priority;
1022 int ret = 0;
1023 unsigned long total_scanned = 0;
1024 unsigned long nr_reclaimed = 0;
1025 struct reclaim_state *reclaim_state = current->reclaim_state;
1026 unsigned long lru_pages = 0;
1027 int i;
1028 #if 0 // mask by Victor Yu. 03-14-2007
1029 struct scan_control sc = {
1030 .gfp_mask = gfp_mask,
1031 .may_writepage = !laptop_mode,
1032 .swap_cluster_max = SWAP_CLUSTER_MAX,
1033 .may_swap = 1,
1034 .swappiness = vm_swappiness,
1036 #else
1037 struct scan_control sc;
1038 sc.gfp_mask = gfp_mask;
1039 sc.may_writepage = !laptop_mode;
1040 sc.swap_cluster_max = SWAP_CLUSTER_MAX;
1041 sc.may_swap = 1;
1042 sc.swappiness = vm_swappiness;
1043 #endif
1045 count_vm_event(ALLOCSTALL);
1047 for (i = 0; zones[i] != NULL; i++) {
1048 struct zone *zone = zones[i];
1050 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1051 continue;
1053 lru_pages += zone->nr_active + zone->nr_inactive;
1056 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1057 sc.nr_scanned = 0;
1058 if (!priority)
1059 disable_swap_token();
1060 nr_reclaimed += shrink_zones(priority, zones, &sc);
1061 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
1062 if (reclaim_state) {
1063 nr_reclaimed += reclaim_state->reclaimed_slab;
1064 reclaim_state->reclaimed_slab = 0;
1066 total_scanned += sc.nr_scanned;
1067 if (nr_reclaimed >= sc.swap_cluster_max) {
1068 ret = 1;
1069 goto out;
1073 * Try to write back as many pages as we just scanned. This
1074 * tends to cause slow streaming writers to write data to the
1075 * disk smoothly, at the dirtying rate, which is nice. But
1076 * that's undesirable in laptop mode, where we *want* lumpy
1077 * writeout. So in laptop mode, write out the whole world.
1079 if (total_scanned > sc.swap_cluster_max +
1080 sc.swap_cluster_max / 2) {
1081 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
1082 sc.may_writepage = 1;
1085 /* Take a nap, wait for some writeback to complete */
1086 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1087 congestion_wait(WRITE, HZ/10);
1089 /* top priority shrink_caches still had more to do? don't OOM, then */
1090 if (!sc.all_unreclaimable)
1091 ret = 1;
1092 out:
1094 * Now that we've scanned all the zones at this priority level, note
1095 * that level within the zone so that the next thread which performs
1096 * scanning of this zone will immediately start out at this priority
1097 * level. This affects only the decision whether or not to bring
1098 * mapped pages onto the inactive list.
1100 if (priority < 0)
1101 priority = 0;
1102 for (i = 0; zones[i] != 0; i++) {
1103 struct zone *zone = zones[i];
1105 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1106 continue;
1108 zone->prev_priority = priority;
1110 return ret;
1114 * For kswapd, balance_pgdat() will work across all this node's zones until
1115 * they are all at pages_high.
1117 * Returns the number of pages which were actually freed.
1119 * There is special handling here for zones which are full of pinned pages.
1120 * This can happen if the pages are all mlocked, or if they are all used by
1121 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1122 * What we do is to detect the case where all pages in the zone have been
1123 * scanned twice and there has been zero successful reclaim. Mark the zone as
1124 * dead and from now on, only perform a short scan. Basically we're polling
1125 * the zone for when the problem goes away.
1127 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1128 * zones which have free_pages > pages_high, but once a zone is found to have
1129 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1130 * of the number of free pages in the lower zones. This interoperates with
1131 * the page allocator fallback scheme to ensure that aging of pages is balanced
1132 * across the zones.
1134 static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
1136 int all_zones_ok;
1137 int priority;
1138 int i;
1139 unsigned long total_scanned;
1140 unsigned long nr_reclaimed;
1141 struct reclaim_state *reclaim_state = current->reclaim_state;
1142 #if 0 // mask by Victor Yu. 03-14-2007
1143 struct scan_control sc = {
1144 .gfp_mask = GFP_KERNEL,
1145 .may_swap = 1,
1146 .swap_cluster_max = SWAP_CLUSTER_MAX,
1147 .swappiness = vm_swappiness,
1150 * temp_priority is used to remember the scanning priority at which
1151 * this zone was successfully refilled to free_pages == pages_high.
1153 int temp_priority[MAX_NR_ZONES];
1154 #else
1155 int temp_priority[MAX_NR_ZONES];
1156 struct scan_control sc;
1157 sc.gfp_mask = GFP_KERNEL;
1158 sc.may_swap = 1;
1159 sc.swap_cluster_max = SWAP_CLUSTER_MAX;
1160 sc.swappiness = vm_swappiness;
1161 #endif
1163 loop_again:
1164 total_scanned = 0;
1165 nr_reclaimed = 0;
1166 sc.may_writepage = !laptop_mode;
1167 count_vm_event(PAGEOUTRUN);
1169 for (i = 0; i < pgdat->nr_zones; i++)
1170 temp_priority[i] = DEF_PRIORITY;
1172 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1173 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1174 unsigned long lru_pages = 0;
1176 /* The swap token gets in the way of swapout... */
1177 if (!priority)
1178 disable_swap_token();
1180 all_zones_ok = 1;
1183 * Scan in the highmem->dma direction for the highest
1184 * zone which needs scanning
1186 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1187 struct zone *zone = pgdat->node_zones + i;
1189 if (!populated_zone(zone))
1190 continue;
1192 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1193 continue;
1195 if (!zone_watermark_ok(zone, order, zone->pages_high,
1196 0, 0)) {
1197 end_zone = i;
1198 goto scan;
1201 goto out;
1202 scan:
1203 for (i = 0; i <= end_zone; i++) {
1204 struct zone *zone = pgdat->node_zones + i;
1206 lru_pages += zone->nr_active + zone->nr_inactive;
1210 * Now scan the zone in the dma->highmem direction, stopping
1211 * at the last zone which needs scanning.
1213 * We do this because the page allocator works in the opposite
1214 * direction. This prevents the page allocator from allocating
1215 * pages behind kswapd's direction of progress, which would
1216 * cause too much scanning of the lower zones.
1218 for (i = 0; i <= end_zone; i++) {
1219 struct zone *zone = pgdat->node_zones + i;
1220 int nr_slab;
1222 if (!populated_zone(zone))
1223 continue;
1225 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1226 continue;
1228 if (!zone_watermark_ok(zone, order, zone->pages_high,
1229 end_zone, 0))
1230 all_zones_ok = 0;
1231 temp_priority[i] = priority;
1232 sc.nr_scanned = 0;
1233 note_zone_scanning_priority(zone, priority);
1234 nr_reclaimed += shrink_zone(priority, zone, &sc);
1235 reclaim_state->reclaimed_slab = 0;
1236 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1237 lru_pages);
1238 nr_reclaimed += reclaim_state->reclaimed_slab;
1239 total_scanned += sc.nr_scanned;
1240 if (zone->all_unreclaimable)
1241 continue;
1242 if (nr_slab == 0 && zone->pages_scanned >=
1243 (zone->nr_active + zone->nr_inactive) * 6)
1244 zone->all_unreclaimable = 1;
1246 * If we've done a decent amount of scanning and
1247 * the reclaim ratio is low, start doing writepage
1248 * even in laptop mode
1250 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
1251 total_scanned > nr_reclaimed + nr_reclaimed / 2)
1252 sc.may_writepage = 1;
1254 if (all_zones_ok)
1255 break; /* kswapd: all done */
1257 * OK, kswapd is getting into trouble. Take a nap, then take
1258 * another pass across the zones.
1260 if (total_scanned && priority < DEF_PRIORITY - 2)
1261 congestion_wait(WRITE, HZ/10);
1264 * We do this so kswapd doesn't build up large priorities for
1265 * example when it is freeing in parallel with allocators. It
1266 * matches the direct reclaim path behaviour in terms of impact
1267 * on zone->*_priority.
1269 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
1270 break;
1272 out:
1274 * Note within each zone the priority level at which this zone was
1275 * brought into a happy state. So that the next thread which scans this
1276 * zone will start out at that priority level.
1278 for (i = 0; i < pgdat->nr_zones; i++) {
1279 struct zone *zone = pgdat->node_zones + i;
1281 zone->prev_priority = temp_priority[i];
1283 if (!all_zones_ok) {
1284 cond_resched();
1285 goto loop_again;
1288 return nr_reclaimed;
1292 * The background pageout daemon, started as a kernel thread
1293 * from the init process.
1295 * This basically trickles out pages so that we have _some_
1296 * free memory available even if there is no other activity
1297 * that frees anything up. This is needed for things like routing
1298 * etc, where we otherwise might have all activity going on in
1299 * asynchronous contexts that cannot page things out.
1301 * If there are applications that are active memory-allocators
1302 * (most normal use), this basically shouldn't matter.
1304 static int kswapd(void *p)
1306 unsigned long order;
1307 pg_data_t *pgdat = (pg_data_t*)p;
1308 struct task_struct *tsk = current;
1309 DEFINE_WAIT(wait);
1310 struct reclaim_state reclaim_state = {
1311 .reclaimed_slab = 0,
1313 cpumask_t cpumask;
1315 cpumask = node_to_cpumask(pgdat->node_id);
1316 if (!cpus_empty(cpumask))
1317 set_cpus_allowed(tsk, cpumask);
1318 current->reclaim_state = &reclaim_state;
1321 * Tell the memory management that we're a "memory allocator",
1322 * and that if we need more memory we should get access to it
1323 * regardless (see "__alloc_pages()"). "kswapd" should
1324 * never get caught in the normal page freeing logic.
1326 * (Kswapd normally doesn't need memory anyway, but sometimes
1327 * you need a small amount of memory in order to be able to
1328 * page out something else, and this flag essentially protects
1329 * us from recursively trying to free more memory as we're
1330 * trying to free the first piece of memory in the first place).
1332 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
1334 order = 0;
1335 for ( ; ; ) {
1336 unsigned long new_order;
1338 try_to_freeze();
1340 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1341 new_order = pgdat->kswapd_max_order;
1342 pgdat->kswapd_max_order = 0;
1343 if (order < new_order) {
1345 * Don't sleep if someone wants a larger 'order'
1346 * allocation
1348 order = new_order;
1349 } else {
1350 schedule();
1351 order = pgdat->kswapd_max_order;
1353 finish_wait(&pgdat->kswapd_wait, &wait);
1355 balance_pgdat(pgdat, order);
1357 return 0;
1361 * A zone is low on free memory, so wake its kswapd task to service it.
1363 void wakeup_kswapd(struct zone *zone, int order)
1365 pg_data_t *pgdat;
1367 if (!populated_zone(zone))
1368 return;
1370 pgdat = zone->zone_pgdat;
1371 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
1372 return;
1373 if (pgdat->kswapd_max_order < order)
1374 pgdat->kswapd_max_order = order;
1375 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1376 return;
1377 if (!waitqueue_active(&pgdat->kswapd_wait))
1378 return;
1379 wake_up_interruptible(&pgdat->kswapd_wait);
1382 #ifdef CONFIG_PM
1384 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1385 * from LRU lists system-wide, for given pass and priority, and returns the
1386 * number of reclaimed pages
1388 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1390 static unsigned long shrink_all_zones(unsigned long nr_pages, int pass,
1391 int prio, struct scan_control *sc)
1393 struct zone *zone;
1394 unsigned long nr_to_scan, ret = 0;
1396 for_each_zone(zone) {
1398 if (!populated_zone(zone))
1399 continue;
1401 if (zone->all_unreclaimable && prio != DEF_PRIORITY)
1402 continue;
1404 /* For pass = 0 we don't shrink the active list */
1405 if (pass > 0) {
1406 zone->nr_scan_active += (zone->nr_active >> prio) + 1;
1407 if (zone->nr_scan_active >= nr_pages || pass > 3) {
1408 zone->nr_scan_active = 0;
1409 nr_to_scan = min(nr_pages, zone->nr_active);
1410 shrink_active_list(nr_to_scan, zone, sc, prio);
1414 zone->nr_scan_inactive += (zone->nr_inactive >> prio) + 1;
1415 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1416 zone->nr_scan_inactive = 0;
1417 nr_to_scan = min(nr_pages, zone->nr_inactive);
1418 ret += shrink_inactive_list(nr_to_scan, zone, sc);
1419 if (ret >= nr_pages)
1420 return ret;
1424 return ret;
1428 * Try to free `nr_pages' of memory, system-wide, and return the number of
1429 * freed pages.
1431 * Rather than trying to age LRUs the aim is to preserve the overall
1432 * LRU order by reclaiming preferentially
1433 * inactive > active > active referenced > active mapped
1435 unsigned long shrink_all_memory(unsigned long nr_pages)
1437 unsigned long lru_pages, nr_slab;
1438 unsigned long ret = 0;
1439 int pass;
1440 struct reclaim_state reclaim_state;
1441 struct zone *zone;
1442 #if 0 // mask by Victor Yu. 03-14-2007
1443 struct scan_control sc = {
1444 .gfp_mask = GFP_KERNEL,
1445 .may_swap = 0,
1446 .swap_cluster_max = nr_pages,
1447 .may_writepage = 1,
1448 .swappiness = vm_swappiness,
1450 #else
1451 struct scan_control sc;
1452 sc.gfp_mask = GFP_KERNEL;
1453 sc.may_swap = 0;
1454 sc.swap_cluster_max = nr_pages;
1455 sc.may_writepage = 1;
1456 sc.swappiness = vm_swappiness;
1457 #endif
1459 current->reclaim_state = &reclaim_state;
1461 lru_pages = 0;
1462 for_each_zone(zone)
1463 lru_pages += zone->nr_active + zone->nr_inactive;
1465 nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
1466 /* If slab caches are huge, it's better to hit them first */
1467 while (nr_slab >= lru_pages) {
1468 reclaim_state.reclaimed_slab = 0;
1469 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1470 if (!reclaim_state.reclaimed_slab)
1471 break;
1473 ret += reclaim_state.reclaimed_slab;
1474 if (ret >= nr_pages)
1475 goto out;
1477 nr_slab -= reclaim_state.reclaimed_slab;
1481 * We try to shrink LRUs in 5 passes:
1482 * 0 = Reclaim from inactive_list only
1483 * 1 = Reclaim from active list but don't reclaim mapped
1484 * 2 = 2nd pass of type 1
1485 * 3 = Reclaim mapped (normal reclaim)
1486 * 4 = 2nd pass of type 3
1488 for (pass = 0; pass < 5; pass++) {
1489 int prio;
1491 /* Needed for shrinking slab caches later on */
1492 if (!lru_pages)
1493 for_each_zone(zone) {
1494 lru_pages += zone->nr_active;
1495 lru_pages += zone->nr_inactive;
1498 /* Force reclaiming mapped pages in the passes #3 and #4 */
1499 if (pass > 2) {
1500 sc.may_swap = 1;
1501 sc.swappiness = 100;
1504 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1505 unsigned long nr_to_scan = nr_pages - ret;
1507 sc.nr_scanned = 0;
1508 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1509 if (ret >= nr_pages)
1510 goto out;
1512 reclaim_state.reclaimed_slab = 0;
1513 shrink_slab(sc.nr_scanned, sc.gfp_mask, lru_pages);
1514 ret += reclaim_state.reclaimed_slab;
1515 if (ret >= nr_pages)
1516 goto out;
1518 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
1519 congestion_wait(WRITE, HZ / 10);
1522 lru_pages = 0;
1526 * If ret = 0, we could not shrink LRUs, but there may be something
1527 * in slab caches
1529 if (!ret)
1530 do {
1531 reclaim_state.reclaimed_slab = 0;
1532 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1533 ret += reclaim_state.reclaimed_slab;
1534 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
1536 out:
1537 current->reclaim_state = NULL;
1539 return ret;
1541 #endif
1543 #ifdef CONFIG_HOTPLUG_CPU
1544 /* It's optimal to keep kswapds on the same CPUs as their memory, but
1545 not required for correctness. So if the last cpu in a node goes
1546 away, we get changed to run anywhere: as the first one comes back,
1547 restore their cpu bindings. */
1548 static int __devinit cpu_callback(struct notifier_block *nfb,
1549 unsigned long action, void *hcpu)
1551 pg_data_t *pgdat;
1552 cpumask_t mask;
1554 if (action == CPU_ONLINE) {
1555 for_each_online_pgdat(pgdat) {
1556 mask = node_to_cpumask(pgdat->node_id);
1557 if (any_online_cpu(mask) != NR_CPUS)
1558 /* One of our CPUs online: restore mask */
1559 set_cpus_allowed(pgdat->kswapd, mask);
1562 return NOTIFY_OK;
1564 #endif /* CONFIG_HOTPLUG_CPU */
1567 * This kswapd start function will be called by init and node-hot-add.
1568 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
1570 int kswapd_run(int nid)
1572 pg_data_t *pgdat = NODE_DATA(nid);
1573 int ret = 0;
1575 if (pgdat->kswapd)
1576 return 0;
1578 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
1579 if (IS_ERR(pgdat->kswapd)) {
1580 /* failure at boot is fatal */
1581 BUG_ON(system_state == SYSTEM_BOOTING);
1582 printk("Failed to start kswapd on node %d\n",nid);
1583 ret = -1;
1585 return ret;
1588 static int __init kswapd_init(void)
1590 int nid;
1592 swap_setup();
1593 for_each_online_node(nid)
1594 kswapd_run(nid);
1595 hotcpu_notifier(cpu_callback, 0);
1596 return 0;
1599 module_init(kswapd_init)
1601 #ifdef CONFIG_NUMA
1603 * Zone reclaim mode
1605 * If non-zero call zone_reclaim when the number of free pages falls below
1606 * the watermarks.
1608 int zone_reclaim_mode __read_mostly;
1610 #define RECLAIM_OFF 0
1611 #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1612 #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1613 #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1616 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1617 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1618 * a zone.
1620 #define ZONE_RECLAIM_PRIORITY 4
1623 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
1624 * occur.
1626 int sysctl_min_unmapped_ratio = 1;
1629 * If the number of slab pages in a zone grows beyond this percentage then
1630 * slab reclaim needs to occur.
1632 int sysctl_min_slab_ratio = 5;
1635 * Try to free up some pages from this zone through reclaim.
1637 static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1639 /* Minimum pages needed in order to stay on node */
1640 const unsigned long nr_pages = 1 << order;
1641 struct task_struct *p = current;
1642 struct reclaim_state reclaim_state;
1643 int priority;
1644 unsigned long nr_reclaimed = 0;
1645 #if 0 // mask by Victor Yu. 03-14-2007
1646 struct scan_control sc = {
1647 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1648 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
1649 .swap_cluster_max = max_t(unsigned long, nr_pages,
1650 SWAP_CLUSTER_MAX),
1651 .gfp_mask = gfp_mask,
1652 .swappiness = vm_swappiness,
1654 unsigned long slab_reclaimable;
1655 #else
1656 unsigned long slab_reclaimable;
1657 struct scan_control sc;
1658 sc.may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE);
1659 sc.may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP);
1660 sc.swap_cluster_max = max_t(unsigned long, nr_pages, SWAP_CLUSTER_MAX);
1661 sc.gfp_mask = gfp_mask;
1662 sc.swappiness = vm_swappiness;
1663 #endif
1665 disable_swap_token();
1666 cond_resched();
1668 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1669 * and we also need to be able to write out pages for RECLAIM_WRITE
1670 * and RECLAIM_SWAP.
1672 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
1673 reclaim_state.reclaimed_slab = 0;
1674 p->reclaim_state = &reclaim_state;
1676 if (zone_page_state(zone, NR_FILE_PAGES) -
1677 zone_page_state(zone, NR_FILE_MAPPED) >
1678 zone->min_unmapped_pages) {
1680 * Free memory by calling shrink zone with increasing
1681 * priorities until we have enough memory freed.
1683 priority = ZONE_RECLAIM_PRIORITY;
1684 do {
1685 note_zone_scanning_priority(zone, priority);
1686 nr_reclaimed += shrink_zone(priority, zone, &sc);
1687 priority--;
1688 } while (priority >= 0 && nr_reclaimed < nr_pages);
1691 slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1692 if (slab_reclaimable > zone->min_slab_pages) {
1694 * shrink_slab() does not currently allow us to determine how
1695 * many pages were freed in this zone. So we take the current
1696 * number of slab pages and shake the slab until it is reduced
1697 * by the same nr_pages that we used for reclaiming unmapped
1698 * pages.
1700 * Note that shrink_slab will free memory on all zones and may
1701 * take a long time.
1703 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
1704 zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
1705 slab_reclaimable - nr_pages)
1709 * Update nr_reclaimed by the number of slab pages we
1710 * reclaimed from this zone.
1712 nr_reclaimed += slab_reclaimable -
1713 zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1716 p->reclaim_state = NULL;
1717 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
1718 return nr_reclaimed >= nr_pages;
1721 int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1723 cpumask_t mask;
1724 int node_id;
1727 * Zone reclaim reclaims unmapped file backed pages and
1728 * slab pages if we are over the defined limits.
1730 * A small portion of unmapped file backed pages is needed for
1731 * file I/O otherwise pages read by file I/O will be immediately
1732 * thrown out if the zone is overallocated. So we do not reclaim
1733 * if less than a specified percentage of the zone is used by
1734 * unmapped file backed pages.
1736 if (zone_page_state(zone, NR_FILE_PAGES) -
1737 zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
1738 && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
1739 <= zone->min_slab_pages)
1740 return 0;
1743 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1744 * not have reclaimable pages and if we should not delay the allocation
1745 * then do not scan.
1747 if (!(gfp_mask & __GFP_WAIT) ||
1748 zone->all_unreclaimable ||
1749 atomic_read(&zone->reclaim_in_progress) > 0 ||
1750 (current->flags & PF_MEMALLOC))
1751 return 0;
1754 * Only run zone reclaim on the local zone or on zones that do not
1755 * have associated processors. This will favor the local processor
1756 * over remote processors and spread off node memory allocations
1757 * as wide as possible.
1759 node_id = zone_to_nid(zone);
1760 mask = node_to_cpumask(node_id);
1761 if (!cpus_empty(mask) && node_id != numa_node_id())
1762 return 0;
1763 return __zone_reclaim(zone, gfp_mask, order);
1765 #endif