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.
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>
37 #include <asm/tlbflush.h>
38 #include <asm/div64.h>
40 #include <linux/swapops.h>
42 /* possible outcome of pageout() */
44 /* failed to write page out, page is locked */
46 /* move page to the active list, page is locked */
48 /* page has been sent to the disk successfully, page is unlocked */
50 /* page is clean and locked */
55 /* Ask refill_inactive_zone, or shrink_cache to scan this many pages */
56 unsigned long nr_to_scan;
58 /* Incremented by the number of inactive pages that were scanned */
59 unsigned long nr_scanned;
61 /* Incremented by the number of pages reclaimed */
62 unsigned long nr_reclaimed;
64 unsigned long nr_mapped; /* From page_state */
66 /* Ask shrink_caches, or shrink_zone to scan at this priority */
67 unsigned int priority;
69 /* This context's GFP mask */
74 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
75 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
76 * In this context, it doesn't matter that we scan the
77 * whole list at once. */
82 * The list of shrinker callbacks used by to apply pressure to
87 struct list_head list;
88 int seeks; /* seeks to recreate an obj */
89 long nr; /* objs pending delete */
92 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
94 #ifdef ARCH_HAS_PREFETCH
95 #define prefetch_prev_lru_page(_page, _base, _field) \
97 if ((_page)->lru.prev != _base) { \
100 prev = lru_to_page(&(_page->lru)); \
101 prefetch(&prev->_field); \
105 #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
108 #ifdef ARCH_HAS_PREFETCHW
109 #define prefetchw_prev_lru_page(_page, _base, _field) \
111 if ((_page)->lru.prev != _base) { \
114 prev = lru_to_page(&(_page->lru)); \
115 prefetchw(&prev->_field); \
119 #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
123 * From 0 .. 100. Higher means more swappy.
125 int vm_swappiness = 60;
126 static long total_memory;
128 static LIST_HEAD(shrinker_list);
129 static DECLARE_RWSEM(shrinker_rwsem);
132 * Add a shrinker callback to be called from the vm
134 struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
136 struct shrinker *shrinker;
138 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
140 shrinker->shrinker = theshrinker;
141 shrinker->seeks = seeks;
143 down_write(&shrinker_rwsem);
144 list_add_tail(&shrinker->list, &shrinker_list);
145 up_write(&shrinker_rwsem);
149 EXPORT_SYMBOL(set_shrinker);
154 void remove_shrinker(struct shrinker *shrinker)
156 down_write(&shrinker_rwsem);
157 list_del(&shrinker->list);
158 up_write(&shrinker_rwsem);
161 EXPORT_SYMBOL(remove_shrinker);
163 #define SHRINK_BATCH 128
165 * Call the shrink functions to age shrinkable caches
167 * Here we assume it costs one seek to replace a lru page and that it also
168 * takes a seek to recreate a cache object. With this in mind we age equal
169 * percentages of the lru and ageable caches. This should balance the seeks
170 * generated by these structures.
172 * If the vm encounted mapped pages on the LRU it increase the pressure on
173 * slab to avoid swapping.
175 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
177 * `lru_pages' represents the number of on-LRU pages in all the zones which
178 * are eligible for the caller's allocation attempt. It is used for balancing
179 * slab reclaim versus page reclaim.
181 * Returns the number of slab objects which we shrunk.
183 static int shrink_slab(unsigned long scanned, gfp_t gfp_mask,
184 unsigned long lru_pages)
186 struct shrinker *shrinker;
190 scanned = SWAP_CLUSTER_MAX;
192 if (!down_read_trylock(&shrinker_rwsem))
193 return 1; /* Assume we'll be able to shrink next time */
195 list_for_each_entry(shrinker, &shrinker_list, list) {
196 unsigned long long delta;
197 unsigned long total_scan;
198 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
200 delta = (4 * scanned) / shrinker->seeks;
202 do_div(delta, lru_pages + 1);
203 shrinker->nr += delta;
204 if (shrinker->nr < 0) {
205 printk(KERN_ERR "%s: nr=%ld\n",
206 __FUNCTION__, shrinker->nr);
207 shrinker->nr = max_pass;
211 * Avoid risking looping forever due to too large nr value:
212 * never try to free more than twice the estimate number of
215 if (shrinker->nr > max_pass * 2)
216 shrinker->nr = max_pass * 2;
218 total_scan = shrinker->nr;
221 while (total_scan >= SHRINK_BATCH) {
222 long this_scan = SHRINK_BATCH;
226 nr_before = (*shrinker->shrinker)(0, gfp_mask);
227 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
228 if (shrink_ret == -1)
230 if (shrink_ret < nr_before)
231 ret += nr_before - shrink_ret;
232 mod_page_state(slabs_scanned, this_scan);
233 total_scan -= this_scan;
238 shrinker->nr += total_scan;
240 up_read(&shrinker_rwsem);
244 /* Called without lock on whether page is mapped, so answer is unstable */
245 static inline int page_mapping_inuse(struct page *page)
247 struct address_space *mapping;
249 /* Page is in somebody's page tables. */
250 if (page_mapped(page))
253 /* Be more reluctant to reclaim swapcache than pagecache */
254 if (PageSwapCache(page))
257 mapping = page_mapping(page);
261 /* File is mmap'd by somebody? */
262 return mapping_mapped(mapping);
265 static inline int is_page_cache_freeable(struct page *page)
267 return page_count(page) - !!PagePrivate(page) == 2;
270 static int may_write_to_queue(struct backing_dev_info *bdi)
272 if (current_is_kswapd())
274 if (current_is_pdflush()) /* This is unlikely, but why not... */
276 if (!bdi_write_congested(bdi))
278 if (bdi == current->backing_dev_info)
284 * We detected a synchronous write error writing a page out. Probably
285 * -ENOSPC. We need to propagate that into the address_space for a subsequent
286 * fsync(), msync() or close().
288 * The tricky part is that after writepage we cannot touch the mapping: nothing
289 * prevents it from being freed up. But we have a ref on the page and once
290 * that page is locked, the mapping is pinned.
292 * We're allowed to run sleeping lock_page() here because we know the caller has
295 static void handle_write_error(struct address_space *mapping,
296 struct page *page, int error)
299 if (page_mapping(page) == mapping) {
300 if (error == -ENOSPC)
301 set_bit(AS_ENOSPC, &mapping->flags);
303 set_bit(AS_EIO, &mapping->flags);
309 * pageout is called by shrink_list() for each dirty page. 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
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))
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__);
346 if (mapping->a_ops->writepage == NULL)
347 return PAGE_ACTIVATE;
348 if (!may_write_to_queue(mapping->backing_dev_info))
351 if (clear_page_dirty_for_io(page)) {
353 struct writeback_control wbc = {
354 .sync_mode = WB_SYNC_NONE,
355 .nr_to_write = SWAP_CLUSTER_MAX,
360 SetPageReclaim(page);
361 res = mapping->a_ops->writepage(page, &wbc);
363 handle_write_error(mapping, page, res);
364 if (res == AOP_WRITEPAGE_ACTIVATE) {
365 ClearPageReclaim(page);
366 return PAGE_ACTIVATE;
368 if (!PageWriteback(page)) {
369 /* synchronous write or broken a_ops? */
370 ClearPageReclaim(page);
380 * shrink_list adds the number of reclaimed pages to sc->nr_reclaimed
382 static int shrink_list(struct list_head *page_list, struct scan_control *sc)
384 LIST_HEAD(ret_pages);
385 struct pagevec freed_pvec;
391 pagevec_init(&freed_pvec, 1);
392 while (!list_empty(page_list)) {
393 struct address_space *mapping;
400 page = lru_to_page(page_list);
401 list_del(&page->lru);
403 if (TestSetPageLocked(page))
406 BUG_ON(PageActive(page));
409 /* Double the slab pressure for mapped and swapcache pages */
410 if (page_mapped(page) || PageSwapCache(page))
413 if (PageWriteback(page))
416 referenced = page_referenced(page, 1);
417 /* In active use or really unfreeable? Activate it. */
418 if (referenced && page_mapping_inuse(page))
419 goto activate_locked;
423 * Anonymous process memory has backing store?
424 * Try to allocate it some swap space here.
426 if (PageAnon(page) && !PageSwapCache(page)) {
427 if (!add_to_swap(page))
428 goto activate_locked;
430 #endif /* CONFIG_SWAP */
432 mapping = page_mapping(page);
433 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
434 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
437 * The page is mapped into the page tables of one or more
438 * processes. Try to unmap it here.
440 if (page_mapped(page) && mapping) {
441 switch (try_to_unmap(page)) {
443 goto activate_locked;
447 ; /* try to free the page below */
451 if (PageDirty(page)) {
456 if (laptop_mode && !sc->may_writepage)
459 /* Page is dirty, try to write it out here */
460 switch(pageout(page, mapping)) {
464 goto activate_locked;
466 if (PageWriteback(page) || PageDirty(page))
469 * A synchronous write - probably a ramdisk. Go
470 * ahead and try to reclaim the page.
472 if (TestSetPageLocked(page))
474 if (PageDirty(page) || PageWriteback(page))
476 mapping = page_mapping(page);
478 ; /* try to free the page below */
483 * If the page has buffers, try to free the buffer mappings
484 * associated with this page. If we succeed we try to free
487 * We do this even if the page is PageDirty().
488 * try_to_release_page() does not perform I/O, but it is
489 * possible for a page to have PageDirty set, but it is actually
490 * clean (all its buffers are clean). This happens if the
491 * buffers were written out directly, with submit_bh(). ext3
492 * will do this, as well as the blockdev mapping.
493 * try_to_release_page() will discover that cleanness and will
494 * drop the buffers and mark the page clean - it can be freed.
496 * Rarely, pages can have buffers and no ->mapping. These are
497 * the pages which were not successfully invalidated in
498 * truncate_complete_page(). We try to drop those buffers here
499 * and if that worked, and the page is no longer mapped into
500 * process address space (page_count == 1) it can be freed.
501 * Otherwise, leave the page on the LRU so it is swappable.
503 if (PagePrivate(page)) {
504 if (!try_to_release_page(page, sc->gfp_mask))
505 goto activate_locked;
506 if (!mapping && page_count(page) == 1)
511 goto keep_locked; /* truncate got there first */
513 write_lock_irq(&mapping->tree_lock);
516 * The non-racy check for busy page. It is critical to check
517 * PageDirty _after_ making sure that the page is freeable and
518 * not in use by anybody. (pagecache + us == 2)
520 if (unlikely(page_count(page) != 2))
523 if (unlikely(PageDirty(page)))
527 if (PageSwapCache(page)) {
528 swp_entry_t swap = { .val = page_private(page) };
529 __delete_from_swap_cache(page);
530 write_unlock_irq(&mapping->tree_lock);
532 __put_page(page); /* The pagecache ref */
535 #endif /* CONFIG_SWAP */
537 __remove_from_page_cache(page);
538 write_unlock_irq(&mapping->tree_lock);
544 if (!pagevec_add(&freed_pvec, page))
545 __pagevec_release_nonlru(&freed_pvec);
549 write_unlock_irq(&mapping->tree_lock);
558 list_add(&page->lru, &ret_pages);
559 BUG_ON(PageLRU(page));
561 list_splice(&ret_pages, page_list);
562 if (pagevec_count(&freed_pvec))
563 __pagevec_release_nonlru(&freed_pvec);
564 mod_page_state(pgactivate, pgactivate);
565 sc->nr_reclaimed += reclaimed;
570 * zone->lru_lock is heavily contended. Some of the functions that
571 * shrink the lists perform better by taking out a batch of pages
572 * and working on them outside the LRU lock.
574 * For pagecache intensive workloads, this function is the hottest
575 * spot in the kernel (apart from copy_*_user functions).
577 * Appropriate locks must be held before calling this function.
579 * @nr_to_scan: The number of pages to look through on the list.
580 * @src: The LRU list to pull pages off.
581 * @dst: The temp list to put pages on to.
582 * @scanned: The number of pages that were scanned.
584 * returns how many pages were moved onto *@dst.
586 static int isolate_lru_pages(int nr_to_scan, struct list_head *src,
587 struct list_head *dst, int *scanned)
593 while (scan++ < nr_to_scan && !list_empty(src)) {
594 page = lru_to_page(src);
595 prefetchw_prev_lru_page(page, src, flags);
597 if (!TestClearPageLRU(page))
599 list_del(&page->lru);
600 if (get_page_testone(page)) {
602 * It is being freed elsewhere
606 list_add(&page->lru, src);
609 list_add(&page->lru, dst);
619 * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed
621 static void shrink_cache(struct zone *zone, struct scan_control *sc)
623 LIST_HEAD(page_list);
625 int max_scan = sc->nr_to_scan;
627 pagevec_init(&pvec, 1);
630 spin_lock_irq(&zone->lru_lock);
631 while (max_scan > 0) {
637 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
638 &zone->inactive_list,
639 &page_list, &nr_scan);
640 zone->nr_inactive -= nr_taken;
641 zone->pages_scanned += nr_scan;
642 spin_unlock_irq(&zone->lru_lock);
648 nr_freed = shrink_list(&page_list, sc);
651 if (current_is_kswapd()) {
652 __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
653 __mod_page_state(kswapd_steal, nr_freed);
655 __mod_page_state_zone(zone, pgscan_direct, nr_scan);
656 __mod_page_state_zone(zone, pgsteal, nr_freed);
658 spin_lock(&zone->lru_lock);
660 * Put back any unfreeable pages.
662 while (!list_empty(&page_list)) {
663 page = lru_to_page(&page_list);
664 if (TestSetPageLRU(page))
666 list_del(&page->lru);
667 if (PageActive(page))
668 add_page_to_active_list(zone, page);
670 add_page_to_inactive_list(zone, page);
671 if (!pagevec_add(&pvec, page)) {
672 spin_unlock_irq(&zone->lru_lock);
673 __pagevec_release(&pvec);
674 spin_lock_irq(&zone->lru_lock);
678 spin_unlock_irq(&zone->lru_lock);
680 pagevec_release(&pvec);
684 * This moves pages from the active list to the inactive list.
686 * We move them the other way if the page is referenced by one or more
687 * processes, from rmap.
689 * If the pages are mostly unmapped, the processing is fast and it is
690 * appropriate to hold zone->lru_lock across the whole operation. But if
691 * the pages are mapped, the processing is slow (page_referenced()) so we
692 * should drop zone->lru_lock around each page. It's impossible to balance
693 * this, so instead we remove the pages from the LRU while processing them.
694 * It is safe to rely on PG_active against the non-LRU pages in here because
695 * nobody will play with that bit on a non-LRU page.
697 * The downside is that we have to touch page->_count against each page.
698 * But we had to alter page->flags anyway.
701 refill_inactive_zone(struct zone *zone, struct scan_control *sc)
704 int pgdeactivate = 0;
706 int nr_pages = sc->nr_to_scan;
707 LIST_HEAD(l_hold); /* The pages which were snipped off */
708 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
709 LIST_HEAD(l_active); /* Pages to go onto the active_list */
712 int reclaim_mapped = 0;
718 spin_lock_irq(&zone->lru_lock);
719 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
720 &l_hold, &pgscanned);
721 zone->pages_scanned += pgscanned;
722 zone->nr_active -= pgmoved;
723 spin_unlock_irq(&zone->lru_lock);
726 * `distress' is a measure of how much trouble we're having reclaiming
727 * pages. 0 -> no problems. 100 -> great trouble.
729 distress = 100 >> zone->prev_priority;
732 * The point of this algorithm is to decide when to start reclaiming
733 * mapped memory instead of just pagecache. Work out how much memory
736 mapped_ratio = (sc->nr_mapped * 100) / total_memory;
739 * Now decide how much we really want to unmap some pages. The mapped
740 * ratio is downgraded - just because there's a lot of mapped memory
741 * doesn't necessarily mean that page reclaim isn't succeeding.
743 * The distress ratio is important - we don't want to start going oom.
745 * A 100% value of vm_swappiness overrides this algorithm altogether.
747 swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
750 * Now use this metric to decide whether to start moving mapped memory
751 * onto the inactive list.
753 if (swap_tendency >= 100)
756 while (!list_empty(&l_hold)) {
758 page = lru_to_page(&l_hold);
759 list_del(&page->lru);
760 if (page_mapped(page)) {
761 if (!reclaim_mapped ||
762 (total_swap_pages == 0 && PageAnon(page)) ||
763 page_referenced(page, 0)) {
764 list_add(&page->lru, &l_active);
768 list_add(&page->lru, &l_inactive);
771 pagevec_init(&pvec, 1);
773 spin_lock_irq(&zone->lru_lock);
774 while (!list_empty(&l_inactive)) {
775 page = lru_to_page(&l_inactive);
776 prefetchw_prev_lru_page(page, &l_inactive, flags);
777 if (TestSetPageLRU(page))
779 if (!TestClearPageActive(page))
781 list_move(&page->lru, &zone->inactive_list);
783 if (!pagevec_add(&pvec, page)) {
784 zone->nr_inactive += pgmoved;
785 spin_unlock_irq(&zone->lru_lock);
786 pgdeactivate += pgmoved;
788 if (buffer_heads_over_limit)
789 pagevec_strip(&pvec);
790 __pagevec_release(&pvec);
791 spin_lock_irq(&zone->lru_lock);
794 zone->nr_inactive += pgmoved;
795 pgdeactivate += pgmoved;
796 if (buffer_heads_over_limit) {
797 spin_unlock_irq(&zone->lru_lock);
798 pagevec_strip(&pvec);
799 spin_lock_irq(&zone->lru_lock);
803 while (!list_empty(&l_active)) {
804 page = lru_to_page(&l_active);
805 prefetchw_prev_lru_page(page, &l_active, flags);
806 if (TestSetPageLRU(page))
808 BUG_ON(!PageActive(page));
809 list_move(&page->lru, &zone->active_list);
811 if (!pagevec_add(&pvec, page)) {
812 zone->nr_active += pgmoved;
814 spin_unlock_irq(&zone->lru_lock);
815 __pagevec_release(&pvec);
816 spin_lock_irq(&zone->lru_lock);
819 zone->nr_active += pgmoved;
820 spin_unlock(&zone->lru_lock);
822 __mod_page_state_zone(zone, pgrefill, pgscanned);
823 __mod_page_state(pgdeactivate, pgdeactivate);
826 pagevec_release(&pvec);
830 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
833 shrink_zone(struct zone *zone, struct scan_control *sc)
835 unsigned long nr_active;
836 unsigned long nr_inactive;
838 atomic_inc(&zone->reclaim_in_progress);
841 * Add one to `nr_to_scan' just to make sure that the kernel will
842 * slowly sift through the active list.
844 zone->nr_scan_active += (zone->nr_active >> sc->priority) + 1;
845 nr_active = zone->nr_scan_active;
846 if (nr_active >= sc->swap_cluster_max)
847 zone->nr_scan_active = 0;
851 zone->nr_scan_inactive += (zone->nr_inactive >> sc->priority) + 1;
852 nr_inactive = zone->nr_scan_inactive;
853 if (nr_inactive >= sc->swap_cluster_max)
854 zone->nr_scan_inactive = 0;
858 while (nr_active || nr_inactive) {
860 sc->nr_to_scan = min(nr_active,
861 (unsigned long)sc->swap_cluster_max);
862 nr_active -= sc->nr_to_scan;
863 refill_inactive_zone(zone, sc);
867 sc->nr_to_scan = min(nr_inactive,
868 (unsigned long)sc->swap_cluster_max);
869 nr_inactive -= sc->nr_to_scan;
870 shrink_cache(zone, sc);
874 throttle_vm_writeout();
876 atomic_dec(&zone->reclaim_in_progress);
880 * This is the direct reclaim path, for page-allocating processes. We only
881 * try to reclaim pages from zones which will satisfy the caller's allocation
884 * We reclaim from a zone even if that zone is over pages_high. Because:
885 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
887 * b) The zones may be over pages_high but they must go *over* pages_high to
888 * satisfy the `incremental min' zone defense algorithm.
890 * Returns the number of reclaimed pages.
892 * If a zone is deemed to be full of pinned pages then just give it a light
893 * scan then give up on it.
896 shrink_caches(struct zone **zones, struct scan_control *sc)
900 for (i = 0; zones[i] != NULL; i++) {
901 struct zone *zone = zones[i];
903 if (!populated_zone(zone))
906 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
909 zone->temp_priority = sc->priority;
910 if (zone->prev_priority > sc->priority)
911 zone->prev_priority = sc->priority;
913 if (zone->all_unreclaimable && sc->priority != DEF_PRIORITY)
914 continue; /* Let kswapd poll it */
916 shrink_zone(zone, sc);
921 * This is the main entry point to direct page reclaim.
923 * If a full scan of the inactive list fails to free enough memory then we
924 * are "out of memory" and something needs to be killed.
926 * If the caller is !__GFP_FS then the probability of a failure is reasonably
927 * high - the zone may be full of dirty or under-writeback pages, which this
928 * caller can't do much about. We kick pdflush and take explicit naps in the
929 * hope that some of these pages can be written. But if the allocating task
930 * holds filesystem locks which prevent writeout this might not work, and the
931 * allocation attempt will fail.
933 int try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
937 int total_scanned = 0, total_reclaimed = 0;
938 struct reclaim_state *reclaim_state = current->reclaim_state;
939 struct scan_control sc;
940 unsigned long lru_pages = 0;
943 sc.gfp_mask = gfp_mask;
944 sc.may_writepage = 0;
946 inc_page_state(allocstall);
948 for (i = 0; zones[i] != NULL; i++) {
949 struct zone *zone = zones[i];
951 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
954 zone->temp_priority = DEF_PRIORITY;
955 lru_pages += zone->nr_active + zone->nr_inactive;
958 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
959 sc.nr_mapped = read_page_state(nr_mapped);
962 sc.priority = priority;
963 sc.swap_cluster_max = SWAP_CLUSTER_MAX;
965 disable_swap_token();
966 shrink_caches(zones, &sc);
967 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
969 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
970 reclaim_state->reclaimed_slab = 0;
972 total_scanned += sc.nr_scanned;
973 total_reclaimed += sc.nr_reclaimed;
974 if (total_reclaimed >= sc.swap_cluster_max) {
980 * Try to write back as many pages as we just scanned. This
981 * tends to cause slow streaming writers to write data to the
982 * disk smoothly, at the dirtying rate, which is nice. But
983 * that's undesirable in laptop mode, where we *want* lumpy
984 * writeout. So in laptop mode, write out the whole world.
986 if (total_scanned > sc.swap_cluster_max + sc.swap_cluster_max/2) {
987 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
988 sc.may_writepage = 1;
991 /* Take a nap, wait for some writeback to complete */
992 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
993 blk_congestion_wait(WRITE, HZ/10);
996 for (i = 0; zones[i] != 0; i++) {
997 struct zone *zone = zones[i];
999 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1002 zone->prev_priority = zone->temp_priority;
1008 * For kswapd, balance_pgdat() will work across all this node's zones until
1009 * they are all at pages_high.
1011 * If `nr_pages' is non-zero then it is the number of pages which are to be
1012 * reclaimed, regardless of the zone occupancies. This is a software suspend
1015 * Returns the number of pages which were actually freed.
1017 * There is special handling here for zones which are full of pinned pages.
1018 * This can happen if the pages are all mlocked, or if they are all used by
1019 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1020 * What we do is to detect the case where all pages in the zone have been
1021 * scanned twice and there has been zero successful reclaim. Mark the zone as
1022 * dead and from now on, only perform a short scan. Basically we're polling
1023 * the zone for when the problem goes away.
1025 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1026 * zones which have free_pages > pages_high, but once a zone is found to have
1027 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1028 * of the number of free pages in the lower zones. This interoperates with
1029 * the page allocator fallback scheme to ensure that aging of pages is balanced
1032 static int balance_pgdat(pg_data_t *pgdat, int nr_pages, int order)
1034 int to_free = nr_pages;
1038 int total_scanned, total_reclaimed;
1039 struct reclaim_state *reclaim_state = current->reclaim_state;
1040 struct scan_control sc;
1044 total_reclaimed = 0;
1045 sc.gfp_mask = GFP_KERNEL;
1046 sc.may_writepage = 0;
1047 sc.nr_mapped = read_page_state(nr_mapped);
1049 inc_page_state(pageoutrun);
1051 for (i = 0; i < pgdat->nr_zones; i++) {
1052 struct zone *zone = pgdat->node_zones + i;
1054 zone->temp_priority = DEF_PRIORITY;
1057 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1058 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1059 unsigned long lru_pages = 0;
1061 /* The swap token gets in the way of swapout... */
1063 disable_swap_token();
1067 if (nr_pages == 0) {
1069 * Scan in the highmem->dma direction for the highest
1070 * zone which needs scanning
1072 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1073 struct zone *zone = pgdat->node_zones + i;
1075 if (!populated_zone(zone))
1078 if (zone->all_unreclaimable &&
1079 priority != DEF_PRIORITY)
1082 if (!zone_watermark_ok(zone, order,
1083 zone->pages_high, 0, 0)) {
1090 end_zone = pgdat->nr_zones - 1;
1093 for (i = 0; i <= end_zone; i++) {
1094 struct zone *zone = pgdat->node_zones + i;
1096 lru_pages += zone->nr_active + zone->nr_inactive;
1100 * Now scan the zone in the dma->highmem direction, stopping
1101 * at the last zone which needs scanning.
1103 * We do this because the page allocator works in the opposite
1104 * direction. This prevents the page allocator from allocating
1105 * pages behind kswapd's direction of progress, which would
1106 * cause too much scanning of the lower zones.
1108 for (i = 0; i <= end_zone; i++) {
1109 struct zone *zone = pgdat->node_zones + i;
1112 if (!populated_zone(zone))
1115 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1118 if (nr_pages == 0) { /* Not software suspend */
1119 if (!zone_watermark_ok(zone, order,
1120 zone->pages_high, end_zone, 0))
1123 zone->temp_priority = priority;
1124 if (zone->prev_priority > priority)
1125 zone->prev_priority = priority;
1127 sc.nr_reclaimed = 0;
1128 sc.priority = priority;
1129 sc.swap_cluster_max = nr_pages? nr_pages : SWAP_CLUSTER_MAX;
1130 atomic_inc(&zone->reclaim_in_progress);
1131 shrink_zone(zone, &sc);
1132 atomic_dec(&zone->reclaim_in_progress);
1133 reclaim_state->reclaimed_slab = 0;
1134 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1136 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
1137 total_reclaimed += sc.nr_reclaimed;
1138 total_scanned += sc.nr_scanned;
1139 if (zone->all_unreclaimable)
1141 if (nr_slab == 0 && zone->pages_scanned >=
1142 (zone->nr_active + zone->nr_inactive) * 4)
1143 zone->all_unreclaimable = 1;
1145 * If we've done a decent amount of scanning and
1146 * the reclaim ratio is low, start doing writepage
1147 * even in laptop mode
1149 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
1150 total_scanned > total_reclaimed+total_reclaimed/2)
1151 sc.may_writepage = 1;
1153 if (nr_pages && to_free > total_reclaimed)
1154 continue; /* swsusp: need to do more work */
1156 break; /* kswapd: all done */
1158 * OK, kswapd is getting into trouble. Take a nap, then take
1159 * another pass across the zones.
1161 if (total_scanned && priority < DEF_PRIORITY - 2)
1162 blk_congestion_wait(WRITE, HZ/10);
1165 * We do this so kswapd doesn't build up large priorities for
1166 * example when it is freeing in parallel with allocators. It
1167 * matches the direct reclaim path behaviour in terms of impact
1168 * on zone->*_priority.
1170 if ((total_reclaimed >= SWAP_CLUSTER_MAX) && (!nr_pages))
1174 for (i = 0; i < pgdat->nr_zones; i++) {
1175 struct zone *zone = pgdat->node_zones + i;
1177 zone->prev_priority = zone->temp_priority;
1179 if (!all_zones_ok) {
1184 return total_reclaimed;
1188 * The background pageout daemon, started as a kernel thread
1189 * from the init process.
1191 * This basically trickles out pages so that we have _some_
1192 * free memory available even if there is no other activity
1193 * that frees anything up. This is needed for things like routing
1194 * etc, where we otherwise might have all activity going on in
1195 * asynchronous contexts that cannot page things out.
1197 * If there are applications that are active memory-allocators
1198 * (most normal use), this basically shouldn't matter.
1200 static int kswapd(void *p)
1202 unsigned long order;
1203 pg_data_t *pgdat = (pg_data_t*)p;
1204 struct task_struct *tsk = current;
1206 struct reclaim_state reclaim_state = {
1207 .reclaimed_slab = 0,
1211 daemonize("kswapd%d", pgdat->node_id);
1212 cpumask = node_to_cpumask(pgdat->node_id);
1213 if (!cpus_empty(cpumask))
1214 set_cpus_allowed(tsk, cpumask);
1215 current->reclaim_state = &reclaim_state;
1218 * Tell the memory management that we're a "memory allocator",
1219 * and that if we need more memory we should get access to it
1220 * regardless (see "__alloc_pages()"). "kswapd" should
1221 * never get caught in the normal page freeing logic.
1223 * (Kswapd normally doesn't need memory anyway, but sometimes
1224 * you need a small amount of memory in order to be able to
1225 * page out something else, and this flag essentially protects
1226 * us from recursively trying to free more memory as we're
1227 * trying to free the first piece of memory in the first place).
1229 tsk->flags |= PF_MEMALLOC|PF_KSWAPD;
1233 unsigned long new_order;
1237 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1238 new_order = pgdat->kswapd_max_order;
1239 pgdat->kswapd_max_order = 0;
1240 if (order < new_order) {
1242 * Don't sleep if someone wants a larger 'order'
1248 order = pgdat->kswapd_max_order;
1250 finish_wait(&pgdat->kswapd_wait, &wait);
1252 balance_pgdat(pgdat, 0, order);
1258 * A zone is low on free memory, so wake its kswapd task to service it.
1260 void wakeup_kswapd(struct zone *zone, int order)
1264 if (!populated_zone(zone))
1267 pgdat = zone->zone_pgdat;
1268 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
1270 if (pgdat->kswapd_max_order < order)
1271 pgdat->kswapd_max_order = order;
1272 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1274 if (!waitqueue_active(&pgdat->kswapd_wait))
1276 wake_up_interruptible(&pgdat->kswapd_wait);
1281 * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
1284 int shrink_all_memory(int nr_pages)
1287 int nr_to_free = nr_pages;
1289 struct reclaim_state reclaim_state = {
1290 .reclaimed_slab = 0,
1293 current->reclaim_state = &reclaim_state;
1294 for_each_pgdat(pgdat) {
1296 freed = balance_pgdat(pgdat, nr_to_free, 0);
1298 nr_to_free -= freed;
1299 if (nr_to_free <= 0)
1302 current->reclaim_state = NULL;
1307 #ifdef CONFIG_HOTPLUG_CPU
1308 /* It's optimal to keep kswapds on the same CPUs as their memory, but
1309 not required for correctness. So if the last cpu in a node goes
1310 away, we get changed to run anywhere: as the first one comes back,
1311 restore their cpu bindings. */
1312 static int __devinit cpu_callback(struct notifier_block *nfb,
1313 unsigned long action,
1319 if (action == CPU_ONLINE) {
1320 for_each_pgdat(pgdat) {
1321 mask = node_to_cpumask(pgdat->node_id);
1322 if (any_online_cpu(mask) != NR_CPUS)
1323 /* One of our CPUs online: restore mask */
1324 set_cpus_allowed(pgdat->kswapd, mask);
1329 #endif /* CONFIG_HOTPLUG_CPU */
1331 static int __init kswapd_init(void)
1335 for_each_pgdat(pgdat)
1337 = find_task_by_pid(kernel_thread(kswapd, pgdat, CLONE_KERNEL));
1338 total_memory = nr_free_pagecache_pages();
1339 hotcpu_notifier(cpu_callback, 0);
1343 module_init(kswapd_init)