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>
36 #include <linux/delay.h>
38 #include <asm/tlbflush.h>
39 #include <asm/div64.h>
41 #include <linux/swapops.h>
46 /* Incremented by the number of inactive pages that were scanned */
47 unsigned long nr_scanned
;
49 unsigned long nr_mapped
; /* From page_state */
51 /* This context's GFP mask */
56 /* Can pages be swapped as part of reclaim? */
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. */
69 * The list of shrinker callbacks used by to apply pressure to
74 struct list_head list
;
75 int seeks
; /* seeks to recreate an obj */
76 long nr
; /* objs pending delete */
79 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
81 #ifdef ARCH_HAS_PREFETCH
82 #define prefetch_prev_lru_page(_page, _base, _field) \
84 if ((_page)->lru.prev != _base) { \
87 prev = lru_to_page(&(_page->lru)); \
88 prefetch(&prev->_field); \
92 #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
95 #ifdef ARCH_HAS_PREFETCHW
96 #define prefetchw_prev_lru_page(_page, _base, _field) \
98 if ((_page)->lru.prev != _base) { \
101 prev = lru_to_page(&(_page->lru)); \
102 prefetchw(&prev->_field); \
106 #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
110 * From 0 .. 100. Higher means more swappy.
112 int vm_swappiness
= 60;
113 static long total_memory
;
115 static LIST_HEAD(shrinker_list
);
116 static DECLARE_RWSEM(shrinker_rwsem
);
119 * Add a shrinker callback to be called from the vm
121 struct shrinker
*set_shrinker(int seeks
, shrinker_t theshrinker
)
123 struct shrinker
*shrinker
;
125 shrinker
= kmalloc(sizeof(*shrinker
), GFP_KERNEL
);
127 shrinker
->shrinker
= theshrinker
;
128 shrinker
->seeks
= seeks
;
130 down_write(&shrinker_rwsem
);
131 list_add_tail(&shrinker
->list
, &shrinker_list
);
132 up_write(&shrinker_rwsem
);
136 EXPORT_SYMBOL(set_shrinker
);
141 void remove_shrinker(struct shrinker
*shrinker
)
143 down_write(&shrinker_rwsem
);
144 list_del(&shrinker
->list
);
145 up_write(&shrinker_rwsem
);
148 EXPORT_SYMBOL(remove_shrinker
);
150 #define SHRINK_BATCH 128
152 * Call the shrink functions to age shrinkable caches
154 * Here we assume it costs one seek to replace a lru page and that it also
155 * takes a seek to recreate a cache object. With this in mind we age equal
156 * percentages of the lru and ageable caches. This should balance the seeks
157 * generated by these structures.
159 * If the vm encounted mapped pages on the LRU it increase the pressure on
160 * slab to avoid swapping.
162 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
164 * `lru_pages' represents the number of on-LRU pages in all the zones which
165 * are eligible for the caller's allocation attempt. It is used for balancing
166 * slab reclaim versus page reclaim.
168 * Returns the number of slab objects which we shrunk.
170 unsigned long shrink_slab(unsigned long scanned
, gfp_t gfp_mask
,
171 unsigned long lru_pages
)
173 struct shrinker
*shrinker
;
174 unsigned long ret
= 0;
177 scanned
= SWAP_CLUSTER_MAX
;
179 if (!down_read_trylock(&shrinker_rwsem
))
180 return 1; /* Assume we'll be able to shrink next time */
182 list_for_each_entry(shrinker
, &shrinker_list
, list
) {
183 unsigned long long delta
;
184 unsigned long total_scan
;
185 unsigned long max_pass
= (*shrinker
->shrinker
)(0, gfp_mask
);
187 delta
= (4 * scanned
) / shrinker
->seeks
;
189 do_div(delta
, lru_pages
+ 1);
190 shrinker
->nr
+= delta
;
191 if (shrinker
->nr
< 0) {
192 printk(KERN_ERR
"%s: nr=%ld\n",
193 __FUNCTION__
, shrinker
->nr
);
194 shrinker
->nr
= max_pass
;
198 * Avoid risking looping forever due to too large nr value:
199 * never try to free more than twice the estimate number of
202 if (shrinker
->nr
> max_pass
* 2)
203 shrinker
->nr
= max_pass
* 2;
205 total_scan
= shrinker
->nr
;
208 while (total_scan
>= SHRINK_BATCH
) {
209 long this_scan
= SHRINK_BATCH
;
213 nr_before
= (*shrinker
->shrinker
)(0, gfp_mask
);
214 shrink_ret
= (*shrinker
->shrinker
)(this_scan
, gfp_mask
);
215 if (shrink_ret
== -1)
217 if (shrink_ret
< nr_before
)
218 ret
+= nr_before
- shrink_ret
;
219 mod_page_state(slabs_scanned
, this_scan
);
220 total_scan
-= this_scan
;
225 shrinker
->nr
+= total_scan
;
227 up_read(&shrinker_rwsem
);
231 /* Called without lock on whether page is mapped, so answer is unstable */
232 static inline int page_mapping_inuse(struct page
*page
)
234 struct address_space
*mapping
;
236 /* Page is in somebody's page tables. */
237 if (page_mapped(page
))
240 /* Be more reluctant to reclaim swapcache than pagecache */
241 if (PageSwapCache(page
))
244 mapping
= page_mapping(page
);
248 /* File is mmap'd by somebody? */
249 return mapping_mapped(mapping
);
252 static inline int is_page_cache_freeable(struct page
*page
)
254 return page_count(page
) - !!PagePrivate(page
) == 2;
257 static int may_write_to_queue(struct backing_dev_info
*bdi
)
259 if (current
->flags
& PF_SWAPWRITE
)
261 if (!bdi_write_congested(bdi
))
263 if (bdi
== current
->backing_dev_info
)
269 * We detected a synchronous write error writing a page out. Probably
270 * -ENOSPC. We need to propagate that into the address_space for a subsequent
271 * fsync(), msync() or close().
273 * The tricky part is that after writepage we cannot touch the mapping: nothing
274 * prevents it from being freed up. But we have a ref on the page and once
275 * that page is locked, the mapping is pinned.
277 * We're allowed to run sleeping lock_page() here because we know the caller has
280 static void handle_write_error(struct address_space
*mapping
,
281 struct page
*page
, int error
)
284 if (page_mapping(page
) == mapping
) {
285 if (error
== -ENOSPC
)
286 set_bit(AS_ENOSPC
, &mapping
->flags
);
288 set_bit(AS_EIO
, &mapping
->flags
);
293 /* possible outcome of pageout() */
295 /* failed to write page out, page is locked */
297 /* move page to the active list, page is locked */
299 /* page has been sent to the disk successfully, page is unlocked */
301 /* page is clean and locked */
306 * pageout is called by shrink_page_list() for each dirty page.
307 * Calls ->writepage().
309 static pageout_t
pageout(struct page
*page
, struct address_space
*mapping
)
312 * If the page is dirty, only perform writeback if that write
313 * will be non-blocking. To prevent this allocation from being
314 * stalled by pagecache activity. But note that there may be
315 * stalls if we need to run get_block(). We could test
316 * PagePrivate for that.
318 * If this process is currently in generic_file_write() against
319 * this page's queue, we can perform writeback even if that
322 * If the page is swapcache, write it back even if that would
323 * block, for some throttling. This happens by accident, because
324 * swap_backing_dev_info is bust: it doesn't reflect the
325 * congestion state of the swapdevs. Easy to fix, if needed.
326 * See swapfile.c:page_queue_congested().
328 if (!is_page_cache_freeable(page
))
332 * Some data journaling orphaned pages can have
333 * page->mapping == NULL while being dirty with clean buffers.
335 if (PagePrivate(page
)) {
336 if (try_to_free_buffers(page
)) {
337 ClearPageDirty(page
);
338 printk("%s: orphaned page\n", __FUNCTION__
);
344 if (mapping
->a_ops
->writepage
== NULL
)
345 return PAGE_ACTIVATE
;
346 if (!may_write_to_queue(mapping
->backing_dev_info
))
349 if (clear_page_dirty_for_io(page
)) {
351 struct writeback_control wbc
= {
352 .sync_mode
= WB_SYNC_NONE
,
353 .nr_to_write
= SWAP_CLUSTER_MAX
,
355 .range_end
= LLONG_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
);
379 int remove_mapping(struct address_space
*mapping
, struct page
*page
)
382 return 0; /* truncate got there first */
384 write_lock_irq(&mapping
->tree_lock
);
387 * The non-racy check for busy page. It is critical to check
388 * PageDirty _after_ making sure that the page is freeable and
389 * not in use by anybody. (pagecache + us == 2)
391 if (unlikely(page_count(page
) != 2))
394 if (unlikely(PageDirty(page
)))
397 if (PageSwapCache(page
)) {
398 swp_entry_t swap
= { .val
= page_private(page
) };
399 __delete_from_swap_cache(page
);
400 write_unlock_irq(&mapping
->tree_lock
);
402 __put_page(page
); /* The pagecache ref */
406 __remove_from_page_cache(page
);
407 write_unlock_irq(&mapping
->tree_lock
);
412 write_unlock_irq(&mapping
->tree_lock
);
417 * shrink_page_list() returns the number of reclaimed pages
419 static unsigned long shrink_page_list(struct list_head
*page_list
,
420 struct scan_control
*sc
)
422 LIST_HEAD(ret_pages
);
423 struct pagevec freed_pvec
;
425 unsigned long nr_reclaimed
= 0;
429 pagevec_init(&freed_pvec
, 1);
430 while (!list_empty(page_list
)) {
431 struct address_space
*mapping
;
438 page
= lru_to_page(page_list
);
439 list_del(&page
->lru
);
441 if (TestSetPageLocked(page
))
444 BUG_ON(PageActive(page
));
448 if (!sc
->may_swap
&& page_mapped(page
))
451 /* Double the slab pressure for mapped and swapcache pages */
452 if (page_mapped(page
) || PageSwapCache(page
))
455 if (PageWriteback(page
))
458 referenced
= page_referenced(page
, 1);
459 /* In active use or really unfreeable? Activate it. */
460 if (referenced
&& page_mapping_inuse(page
))
461 goto activate_locked
;
465 * Anonymous process memory has backing store?
466 * Try to allocate it some swap space here.
468 if (PageAnon(page
) && !PageSwapCache(page
))
469 if (!add_to_swap(page
, GFP_ATOMIC
))
470 goto activate_locked
;
471 #endif /* CONFIG_SWAP */
473 mapping
= page_mapping(page
);
474 may_enter_fs
= (sc
->gfp_mask
& __GFP_FS
) ||
475 (PageSwapCache(page
) && (sc
->gfp_mask
& __GFP_IO
));
478 * The page is mapped into the page tables of one or more
479 * processes. Try to unmap it here.
481 if (page_mapped(page
) && mapping
) {
482 switch (try_to_unmap(page
, 0)) {
484 goto activate_locked
;
488 ; /* try to free the page below */
492 if (PageDirty(page
)) {
497 if (!sc
->may_writepage
)
500 /* Page is dirty, try to write it out here */
501 switch(pageout(page
, mapping
)) {
505 goto activate_locked
;
507 if (PageWriteback(page
) || PageDirty(page
))
510 * A synchronous write - probably a ramdisk. Go
511 * ahead and try to reclaim the page.
513 if (TestSetPageLocked(page
))
515 if (PageDirty(page
) || PageWriteback(page
))
517 mapping
= page_mapping(page
);
519 ; /* try to free the page below */
524 * If the page has buffers, try to free the buffer mappings
525 * associated with this page. If we succeed we try to free
528 * We do this even if the page is PageDirty().
529 * try_to_release_page() does not perform I/O, but it is
530 * possible for a page to have PageDirty set, but it is actually
531 * clean (all its buffers are clean). This happens if the
532 * buffers were written out directly, with submit_bh(). ext3
533 * will do this, as well as the blockdev mapping.
534 * try_to_release_page() will discover that cleanness and will
535 * drop the buffers and mark the page clean - it can be freed.
537 * Rarely, pages can have buffers and no ->mapping. These are
538 * the pages which were not successfully invalidated in
539 * truncate_complete_page(). We try to drop those buffers here
540 * and if that worked, and the page is no longer mapped into
541 * process address space (page_count == 1) it can be freed.
542 * Otherwise, leave the page on the LRU so it is swappable.
544 if (PagePrivate(page
)) {
545 if (!try_to_release_page(page
, sc
->gfp_mask
))
546 goto activate_locked
;
547 if (!mapping
&& page_count(page
) == 1)
551 if (!remove_mapping(mapping
, page
))
557 if (!pagevec_add(&freed_pvec
, page
))
558 __pagevec_release_nonlru(&freed_pvec
);
567 list_add(&page
->lru
, &ret_pages
);
568 BUG_ON(PageLRU(page
));
570 list_splice(&ret_pages
, page_list
);
571 if (pagevec_count(&freed_pvec
))
572 __pagevec_release_nonlru(&freed_pvec
);
573 mod_page_state(pgactivate
, pgactivate
);
578 * zone->lru_lock is heavily contended. Some of the functions that
579 * shrink the lists perform better by taking out a batch of pages
580 * and working on them outside the LRU lock.
582 * For pagecache intensive workloads, this function is the hottest
583 * spot in the kernel (apart from copy_*_user functions).
585 * Appropriate locks must be held before calling this function.
587 * @nr_to_scan: The number of pages to look through on the list.
588 * @src: The LRU list to pull pages off.
589 * @dst: The temp list to put pages on to.
590 * @scanned: The number of pages that were scanned.
592 * returns how many pages were moved onto *@dst.
594 static unsigned long isolate_lru_pages(unsigned long nr_to_scan
,
595 struct list_head
*src
, struct list_head
*dst
,
596 unsigned long *scanned
)
598 unsigned long nr_taken
= 0;
602 for (scan
= 0; scan
< nr_to_scan
&& !list_empty(src
); scan
++) {
603 struct list_head
*target
;
604 page
= lru_to_page(src
);
605 prefetchw_prev_lru_page(page
, src
, flags
);
607 BUG_ON(!PageLRU(page
));
609 list_del(&page
->lru
);
611 if (likely(get_page_unless_zero(page
))) {
613 * Be careful not to clear PageLRU until after we're
614 * sure the page is not being freed elsewhere -- the
615 * page release code relies on it.
620 } /* else it is being freed elsewhere */
622 list_add(&page
->lru
, target
);
630 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
633 static unsigned long shrink_inactive_list(unsigned long max_scan
,
634 struct zone
*zone
, struct scan_control
*sc
)
636 LIST_HEAD(page_list
);
638 unsigned long nr_scanned
= 0;
639 unsigned long nr_reclaimed
= 0;
641 pagevec_init(&pvec
, 1);
644 spin_lock_irq(&zone
->lru_lock
);
647 unsigned long nr_taken
;
648 unsigned long nr_scan
;
649 unsigned long nr_freed
;
651 nr_taken
= isolate_lru_pages(sc
->swap_cluster_max
,
652 &zone
->inactive_list
,
653 &page_list
, &nr_scan
);
654 zone
->nr_inactive
-= nr_taken
;
655 zone
->pages_scanned
+= nr_scan
;
656 spin_unlock_irq(&zone
->lru_lock
);
658 nr_scanned
+= nr_scan
;
659 nr_freed
= shrink_page_list(&page_list
, sc
);
660 nr_reclaimed
+= nr_freed
;
662 if (current_is_kswapd()) {
663 __mod_page_state_zone(zone
, pgscan_kswapd
, nr_scan
);
664 __mod_page_state(kswapd_steal
, nr_freed
);
666 __mod_page_state_zone(zone
, pgscan_direct
, nr_scan
);
667 __mod_page_state_zone(zone
, pgsteal
, nr_freed
);
672 spin_lock(&zone
->lru_lock
);
674 * Put back any unfreeable pages.
676 while (!list_empty(&page_list
)) {
677 page
= lru_to_page(&page_list
);
678 BUG_ON(PageLRU(page
));
680 list_del(&page
->lru
);
681 if (PageActive(page
))
682 add_page_to_active_list(zone
, page
);
684 add_page_to_inactive_list(zone
, page
);
685 if (!pagevec_add(&pvec
, page
)) {
686 spin_unlock_irq(&zone
->lru_lock
);
687 __pagevec_release(&pvec
);
688 spin_lock_irq(&zone
->lru_lock
);
691 } while (nr_scanned
< max_scan
);
692 spin_unlock(&zone
->lru_lock
);
695 pagevec_release(&pvec
);
700 * This moves pages from the active list to the inactive list.
702 * We move them the other way if the page is referenced by one or more
703 * processes, from rmap.
705 * If the pages are mostly unmapped, the processing is fast and it is
706 * appropriate to hold zone->lru_lock across the whole operation. But if
707 * the pages are mapped, the processing is slow (page_referenced()) so we
708 * should drop zone->lru_lock around each page. It's impossible to balance
709 * this, so instead we remove the pages from the LRU while processing them.
710 * It is safe to rely on PG_active against the non-LRU pages in here because
711 * nobody will play with that bit on a non-LRU page.
713 * The downside is that we have to touch page->_count against each page.
714 * But we had to alter page->flags anyway.
716 static void shrink_active_list(unsigned long nr_pages
, struct zone
*zone
,
717 struct scan_control
*sc
)
719 unsigned long pgmoved
;
720 int pgdeactivate
= 0;
721 unsigned long pgscanned
;
722 LIST_HEAD(l_hold
); /* The pages which were snipped off */
723 LIST_HEAD(l_inactive
); /* Pages to go onto the inactive_list */
724 LIST_HEAD(l_active
); /* Pages to go onto the active_list */
727 int reclaim_mapped
= 0;
735 * `distress' is a measure of how much trouble we're having
736 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
738 distress
= 100 >> zone
->prev_priority
;
741 * The point of this algorithm is to decide when to start
742 * reclaiming mapped memory instead of just pagecache. Work out
746 mapped_ratio
= (sc
->nr_mapped
* 100) / total_memory
;
749 * Now decide how much we really want to unmap some pages. The
750 * mapped ratio is downgraded - just because there's a lot of
751 * mapped memory doesn't necessarily mean that page reclaim
754 * The distress ratio is important - we don't want to start
757 * A 100% value of vm_swappiness overrides this algorithm
760 swap_tendency
= mapped_ratio
/ 2 + distress
+ sc
->swappiness
;
763 * Now use this metric to decide whether to start moving mapped
764 * memory onto the inactive list.
766 if (swap_tendency
>= 100)
771 spin_lock_irq(&zone
->lru_lock
);
772 pgmoved
= isolate_lru_pages(nr_pages
, &zone
->active_list
,
773 &l_hold
, &pgscanned
);
774 zone
->pages_scanned
+= pgscanned
;
775 zone
->nr_active
-= pgmoved
;
776 spin_unlock_irq(&zone
->lru_lock
);
778 while (!list_empty(&l_hold
)) {
780 page
= lru_to_page(&l_hold
);
781 list_del(&page
->lru
);
782 if (page_mapped(page
)) {
783 if (!reclaim_mapped
||
784 (total_swap_pages
== 0 && PageAnon(page
)) ||
785 page_referenced(page
, 0)) {
786 list_add(&page
->lru
, &l_active
);
790 list_add(&page
->lru
, &l_inactive
);
793 pagevec_init(&pvec
, 1);
795 spin_lock_irq(&zone
->lru_lock
);
796 while (!list_empty(&l_inactive
)) {
797 page
= lru_to_page(&l_inactive
);
798 prefetchw_prev_lru_page(page
, &l_inactive
, flags
);
799 BUG_ON(PageLRU(page
));
801 BUG_ON(!PageActive(page
));
802 ClearPageActive(page
);
804 list_move(&page
->lru
, &zone
->inactive_list
);
806 if (!pagevec_add(&pvec
, page
)) {
807 zone
->nr_inactive
+= pgmoved
;
808 spin_unlock_irq(&zone
->lru_lock
);
809 pgdeactivate
+= pgmoved
;
811 if (buffer_heads_over_limit
)
812 pagevec_strip(&pvec
);
813 __pagevec_release(&pvec
);
814 spin_lock_irq(&zone
->lru_lock
);
817 zone
->nr_inactive
+= pgmoved
;
818 pgdeactivate
+= pgmoved
;
819 if (buffer_heads_over_limit
) {
820 spin_unlock_irq(&zone
->lru_lock
);
821 pagevec_strip(&pvec
);
822 spin_lock_irq(&zone
->lru_lock
);
826 while (!list_empty(&l_active
)) {
827 page
= lru_to_page(&l_active
);
828 prefetchw_prev_lru_page(page
, &l_active
, flags
);
829 BUG_ON(PageLRU(page
));
831 BUG_ON(!PageActive(page
));
832 list_move(&page
->lru
, &zone
->active_list
);
834 if (!pagevec_add(&pvec
, page
)) {
835 zone
->nr_active
+= pgmoved
;
837 spin_unlock_irq(&zone
->lru_lock
);
838 __pagevec_release(&pvec
);
839 spin_lock_irq(&zone
->lru_lock
);
842 zone
->nr_active
+= pgmoved
;
843 spin_unlock(&zone
->lru_lock
);
845 __mod_page_state_zone(zone
, pgrefill
, pgscanned
);
846 __mod_page_state(pgdeactivate
, pgdeactivate
);
849 pagevec_release(&pvec
);
853 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
855 static unsigned long shrink_zone(int priority
, struct zone
*zone
,
856 struct scan_control
*sc
)
858 unsigned long nr_active
;
859 unsigned long nr_inactive
;
860 unsigned long nr_to_scan
;
861 unsigned long nr_reclaimed
= 0;
863 atomic_inc(&zone
->reclaim_in_progress
);
866 * Add one to `nr_to_scan' just to make sure that the kernel will
867 * slowly sift through the active list.
869 zone
->nr_scan_active
+= (zone
->nr_active
>> priority
) + 1;
870 nr_active
= zone
->nr_scan_active
;
871 if (nr_active
>= sc
->swap_cluster_max
)
872 zone
->nr_scan_active
= 0;
876 zone
->nr_scan_inactive
+= (zone
->nr_inactive
>> priority
) + 1;
877 nr_inactive
= zone
->nr_scan_inactive
;
878 if (nr_inactive
>= sc
->swap_cluster_max
)
879 zone
->nr_scan_inactive
= 0;
883 while (nr_active
|| nr_inactive
) {
885 nr_to_scan
= min(nr_active
,
886 (unsigned long)sc
->swap_cluster_max
);
887 nr_active
-= nr_to_scan
;
888 shrink_active_list(nr_to_scan
, zone
, sc
);
892 nr_to_scan
= min(nr_inactive
,
893 (unsigned long)sc
->swap_cluster_max
);
894 nr_inactive
-= nr_to_scan
;
895 nr_reclaimed
+= shrink_inactive_list(nr_to_scan
, zone
,
900 throttle_vm_writeout();
902 atomic_dec(&zone
->reclaim_in_progress
);
907 * This is the direct reclaim path, for page-allocating processes. We only
908 * try to reclaim pages from zones which will satisfy the caller's allocation
911 * We reclaim from a zone even if that zone is over pages_high. Because:
912 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
914 * b) The zones may be over pages_high but they must go *over* pages_high to
915 * satisfy the `incremental min' zone defense algorithm.
917 * Returns the number of reclaimed pages.
919 * If a zone is deemed to be full of pinned pages then just give it a light
920 * scan then give up on it.
922 static unsigned long shrink_zones(int priority
, struct zone
**zones
,
923 struct scan_control
*sc
)
925 unsigned long nr_reclaimed
= 0;
928 for (i
= 0; zones
[i
] != NULL
; i
++) {
929 struct zone
*zone
= zones
[i
];
931 if (!populated_zone(zone
))
934 if (!cpuset_zone_allowed(zone
, __GFP_HARDWALL
))
937 zone
->temp_priority
= priority
;
938 if (zone
->prev_priority
> priority
)
939 zone
->prev_priority
= priority
;
941 if (zone
->all_unreclaimable
&& priority
!= DEF_PRIORITY
)
942 continue; /* Let kswapd poll it */
944 nr_reclaimed
+= shrink_zone(priority
, zone
, sc
);
950 * This is the main entry point to direct page reclaim.
952 * If a full scan of the inactive list fails to free enough memory then we
953 * are "out of memory" and something needs to be killed.
955 * If the caller is !__GFP_FS then the probability of a failure is reasonably
956 * high - the zone may be full of dirty or under-writeback pages, which this
957 * caller can't do much about. We kick pdflush and take explicit naps in the
958 * hope that some of these pages can be written. But if the allocating task
959 * holds filesystem locks which prevent writeout this might not work, and the
960 * allocation attempt will fail.
962 unsigned long try_to_free_pages(struct zone
**zones
, gfp_t gfp_mask
)
966 unsigned long total_scanned
= 0;
967 unsigned long nr_reclaimed
= 0;
968 struct reclaim_state
*reclaim_state
= current
->reclaim_state
;
969 unsigned long lru_pages
= 0;
971 struct scan_control sc
= {
972 .gfp_mask
= gfp_mask
,
973 .may_writepage
= !laptop_mode
,
974 .swap_cluster_max
= SWAP_CLUSTER_MAX
,
976 .swappiness
= vm_swappiness
,
979 inc_page_state(allocstall
);
981 for (i
= 0; zones
[i
] != NULL
; i
++) {
982 struct zone
*zone
= zones
[i
];
984 if (!cpuset_zone_allowed(zone
, __GFP_HARDWALL
))
987 zone
->temp_priority
= DEF_PRIORITY
;
988 lru_pages
+= zone
->nr_active
+ zone
->nr_inactive
;
991 for (priority
= DEF_PRIORITY
; priority
>= 0; priority
--) {
992 sc
.nr_mapped
= read_page_state(nr_mapped
);
995 disable_swap_token();
996 nr_reclaimed
+= shrink_zones(priority
, zones
, &sc
);
997 shrink_slab(sc
.nr_scanned
, gfp_mask
, lru_pages
);
999 nr_reclaimed
+= reclaim_state
->reclaimed_slab
;
1000 reclaim_state
->reclaimed_slab
= 0;
1002 total_scanned
+= sc
.nr_scanned
;
1003 if (nr_reclaimed
>= sc
.swap_cluster_max
) {
1009 * Try to write back as many pages as we just scanned. This
1010 * tends to cause slow streaming writers to write data to the
1011 * disk smoothly, at the dirtying rate, which is nice. But
1012 * that's undesirable in laptop mode, where we *want* lumpy
1013 * writeout. So in laptop mode, write out the whole world.
1015 if (total_scanned
> sc
.swap_cluster_max
+
1016 sc
.swap_cluster_max
/ 2) {
1017 wakeup_pdflush(laptop_mode
? 0 : total_scanned
);
1018 sc
.may_writepage
= 1;
1021 /* Take a nap, wait for some writeback to complete */
1022 if (sc
.nr_scanned
&& priority
< DEF_PRIORITY
- 2)
1023 blk_congestion_wait(WRITE
, HZ
/10);
1026 for (i
= 0; zones
[i
] != 0; i
++) {
1027 struct zone
*zone
= zones
[i
];
1029 if (!cpuset_zone_allowed(zone
, __GFP_HARDWALL
))
1032 zone
->prev_priority
= zone
->temp_priority
;
1038 * For kswapd, balance_pgdat() will work across all this node's zones until
1039 * they are all at pages_high.
1041 * Returns the number of pages which were actually freed.
1043 * There is special handling here for zones which are full of pinned pages.
1044 * This can happen if the pages are all mlocked, or if they are all used by
1045 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1046 * What we do is to detect the case where all pages in the zone have been
1047 * scanned twice and there has been zero successful reclaim. Mark the zone as
1048 * dead and from now on, only perform a short scan. Basically we're polling
1049 * the zone for when the problem goes away.
1051 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1052 * zones which have free_pages > pages_high, but once a zone is found to have
1053 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1054 * of the number of free pages in the lower zones. This interoperates with
1055 * the page allocator fallback scheme to ensure that aging of pages is balanced
1058 static unsigned long balance_pgdat(pg_data_t
*pgdat
, int order
)
1063 unsigned long total_scanned
;
1064 unsigned long nr_reclaimed
;
1065 struct reclaim_state
*reclaim_state
= current
->reclaim_state
;
1066 struct scan_control sc
= {
1067 .gfp_mask
= GFP_KERNEL
,
1069 .swap_cluster_max
= SWAP_CLUSTER_MAX
,
1070 .swappiness
= vm_swappiness
,
1076 sc
.may_writepage
= !laptop_mode
;
1077 sc
.nr_mapped
= read_page_state(nr_mapped
);
1079 inc_page_state(pageoutrun
);
1081 for (i
= 0; i
< pgdat
->nr_zones
; i
++) {
1082 struct zone
*zone
= pgdat
->node_zones
+ i
;
1084 zone
->temp_priority
= DEF_PRIORITY
;
1087 for (priority
= DEF_PRIORITY
; priority
>= 0; priority
--) {
1088 int end_zone
= 0; /* Inclusive. 0 = ZONE_DMA */
1089 unsigned long lru_pages
= 0;
1091 /* The swap token gets in the way of swapout... */
1093 disable_swap_token();
1098 * Scan in the highmem->dma direction for the highest
1099 * zone which needs scanning
1101 for (i
= pgdat
->nr_zones
- 1; i
>= 0; i
--) {
1102 struct zone
*zone
= pgdat
->node_zones
+ i
;
1104 if (!populated_zone(zone
))
1107 if (zone
->all_unreclaimable
&& priority
!= DEF_PRIORITY
)
1110 if (!zone_watermark_ok(zone
, order
, zone
->pages_high
,
1118 for (i
= 0; i
<= end_zone
; i
++) {
1119 struct zone
*zone
= pgdat
->node_zones
+ i
;
1121 lru_pages
+= zone
->nr_active
+ zone
->nr_inactive
;
1125 * Now scan the zone in the dma->highmem direction, stopping
1126 * at the last zone which needs scanning.
1128 * We do this because the page allocator works in the opposite
1129 * direction. This prevents the page allocator from allocating
1130 * pages behind kswapd's direction of progress, which would
1131 * cause too much scanning of the lower zones.
1133 for (i
= 0; i
<= end_zone
; i
++) {
1134 struct zone
*zone
= pgdat
->node_zones
+ i
;
1137 if (!populated_zone(zone
))
1140 if (zone
->all_unreclaimable
&& priority
!= DEF_PRIORITY
)
1143 if (!zone_watermark_ok(zone
, order
, zone
->pages_high
,
1146 zone
->temp_priority
= priority
;
1147 if (zone
->prev_priority
> priority
)
1148 zone
->prev_priority
= priority
;
1150 nr_reclaimed
+= shrink_zone(priority
, zone
, &sc
);
1151 reclaim_state
->reclaimed_slab
= 0;
1152 nr_slab
= shrink_slab(sc
.nr_scanned
, GFP_KERNEL
,
1154 nr_reclaimed
+= reclaim_state
->reclaimed_slab
;
1155 total_scanned
+= sc
.nr_scanned
;
1156 if (zone
->all_unreclaimable
)
1158 if (nr_slab
== 0 && zone
->pages_scanned
>=
1159 (zone
->nr_active
+ zone
->nr_inactive
) * 4)
1160 zone
->all_unreclaimable
= 1;
1162 * If we've done a decent amount of scanning and
1163 * the reclaim ratio is low, start doing writepage
1164 * even in laptop mode
1166 if (total_scanned
> SWAP_CLUSTER_MAX
* 2 &&
1167 total_scanned
> nr_reclaimed
+ nr_reclaimed
/ 2)
1168 sc
.may_writepage
= 1;
1171 break; /* kswapd: all done */
1173 * OK, kswapd is getting into trouble. Take a nap, then take
1174 * another pass across the zones.
1176 if (total_scanned
&& priority
< DEF_PRIORITY
- 2)
1177 blk_congestion_wait(WRITE
, HZ
/10);
1180 * We do this so kswapd doesn't build up large priorities for
1181 * example when it is freeing in parallel with allocators. It
1182 * matches the direct reclaim path behaviour in terms of impact
1183 * on zone->*_priority.
1185 if (nr_reclaimed
>= SWAP_CLUSTER_MAX
)
1189 for (i
= 0; i
< pgdat
->nr_zones
; i
++) {
1190 struct zone
*zone
= pgdat
->node_zones
+ i
;
1192 zone
->prev_priority
= zone
->temp_priority
;
1194 if (!all_zones_ok
) {
1199 return nr_reclaimed
;
1203 * The background pageout daemon, started as a kernel thread
1204 * from the init process.
1206 * This basically trickles out pages so that we have _some_
1207 * free memory available even if there is no other activity
1208 * that frees anything up. This is needed for things like routing
1209 * etc, where we otherwise might have all activity going on in
1210 * asynchronous contexts that cannot page things out.
1212 * If there are applications that are active memory-allocators
1213 * (most normal use), this basically shouldn't matter.
1215 static int kswapd(void *p
)
1217 unsigned long order
;
1218 pg_data_t
*pgdat
= (pg_data_t
*)p
;
1219 struct task_struct
*tsk
= current
;
1221 struct reclaim_state reclaim_state
= {
1222 .reclaimed_slab
= 0,
1226 daemonize("kswapd%d", pgdat
->node_id
);
1227 cpumask
= node_to_cpumask(pgdat
->node_id
);
1228 if (!cpus_empty(cpumask
))
1229 set_cpus_allowed(tsk
, cpumask
);
1230 current
->reclaim_state
= &reclaim_state
;
1233 * Tell the memory management that we're a "memory allocator",
1234 * and that if we need more memory we should get access to it
1235 * regardless (see "__alloc_pages()"). "kswapd" should
1236 * never get caught in the normal page freeing logic.
1238 * (Kswapd normally doesn't need memory anyway, but sometimes
1239 * you need a small amount of memory in order to be able to
1240 * page out something else, and this flag essentially protects
1241 * us from recursively trying to free more memory as we're
1242 * trying to free the first piece of memory in the first place).
1244 tsk
->flags
|= PF_MEMALLOC
| PF_SWAPWRITE
| PF_KSWAPD
;
1248 unsigned long new_order
;
1252 prepare_to_wait(&pgdat
->kswapd_wait
, &wait
, TASK_INTERRUPTIBLE
);
1253 new_order
= pgdat
->kswapd_max_order
;
1254 pgdat
->kswapd_max_order
= 0;
1255 if (order
< new_order
) {
1257 * Don't sleep if someone wants a larger 'order'
1263 order
= pgdat
->kswapd_max_order
;
1265 finish_wait(&pgdat
->kswapd_wait
, &wait
);
1267 balance_pgdat(pgdat
, order
);
1273 * A zone is low on free memory, so wake its kswapd task to service it.
1275 void wakeup_kswapd(struct zone
*zone
, int order
)
1279 if (!populated_zone(zone
))
1282 pgdat
= zone
->zone_pgdat
;
1283 if (zone_watermark_ok(zone
, order
, zone
->pages_low
, 0, 0))
1285 if (pgdat
->kswapd_max_order
< order
)
1286 pgdat
->kswapd_max_order
= order
;
1287 if (!cpuset_zone_allowed(zone
, __GFP_HARDWALL
))
1289 if (!waitqueue_active(&pgdat
->kswapd_wait
))
1291 wake_up_interruptible(&pgdat
->kswapd_wait
);
1296 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1297 * from LRU lists system-wide, for given pass and priority, and returns the
1298 * number of reclaimed pages
1300 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1302 static unsigned long shrink_all_zones(unsigned long nr_pages
, int pass
,
1303 int prio
, struct scan_control
*sc
)
1306 unsigned long nr_to_scan
, ret
= 0;
1308 for_each_zone(zone
) {
1310 if (!populated_zone(zone
))
1313 if (zone
->all_unreclaimable
&& prio
!= DEF_PRIORITY
)
1316 /* For pass = 0 we don't shrink the active list */
1318 zone
->nr_scan_active
+= (zone
->nr_active
>> prio
) + 1;
1319 if (zone
->nr_scan_active
>= nr_pages
|| pass
> 3) {
1320 zone
->nr_scan_active
= 0;
1321 nr_to_scan
= min(nr_pages
, zone
->nr_active
);
1322 shrink_active_list(nr_to_scan
, zone
, sc
);
1326 zone
->nr_scan_inactive
+= (zone
->nr_inactive
>> prio
) + 1;
1327 if (zone
->nr_scan_inactive
>= nr_pages
|| pass
> 3) {
1328 zone
->nr_scan_inactive
= 0;
1329 nr_to_scan
= min(nr_pages
, zone
->nr_inactive
);
1330 ret
+= shrink_inactive_list(nr_to_scan
, zone
, sc
);
1331 if (ret
>= nr_pages
)
1340 * Try to free `nr_pages' of memory, system-wide, and return the number of
1343 * Rather than trying to age LRUs the aim is to preserve the overall
1344 * LRU order by reclaiming preferentially
1345 * inactive > active > active referenced > active mapped
1347 unsigned long shrink_all_memory(unsigned long nr_pages
)
1349 unsigned long lru_pages
, nr_slab
;
1350 unsigned long ret
= 0;
1352 struct reclaim_state reclaim_state
;
1354 struct scan_control sc
= {
1355 .gfp_mask
= GFP_KERNEL
,
1357 .swap_cluster_max
= nr_pages
,
1359 .swappiness
= vm_swappiness
,
1362 current
->reclaim_state
= &reclaim_state
;
1366 lru_pages
+= zone
->nr_active
+ zone
->nr_inactive
;
1368 nr_slab
= read_page_state(nr_slab
);
1369 /* If slab caches are huge, it's better to hit them first */
1370 while (nr_slab
>= lru_pages
) {
1371 reclaim_state
.reclaimed_slab
= 0;
1372 shrink_slab(nr_pages
, sc
.gfp_mask
, lru_pages
);
1373 if (!reclaim_state
.reclaimed_slab
)
1376 ret
+= reclaim_state
.reclaimed_slab
;
1377 if (ret
>= nr_pages
)
1380 nr_slab
-= reclaim_state
.reclaimed_slab
;
1384 * We try to shrink LRUs in 5 passes:
1385 * 0 = Reclaim from inactive_list only
1386 * 1 = Reclaim from active list but don't reclaim mapped
1387 * 2 = 2nd pass of type 1
1388 * 3 = Reclaim mapped (normal reclaim)
1389 * 4 = 2nd pass of type 3
1391 for (pass
= 0; pass
< 5; pass
++) {
1394 /* Needed for shrinking slab caches later on */
1396 for_each_zone(zone
) {
1397 lru_pages
+= zone
->nr_active
;
1398 lru_pages
+= zone
->nr_inactive
;
1401 /* Force reclaiming mapped pages in the passes #3 and #4 */
1404 sc
.swappiness
= 100;
1407 for (prio
= DEF_PRIORITY
; prio
>= 0; prio
--) {
1408 unsigned long nr_to_scan
= nr_pages
- ret
;
1410 sc
.nr_mapped
= read_page_state(nr_mapped
);
1413 ret
+= shrink_all_zones(nr_to_scan
, prio
, pass
, &sc
);
1414 if (ret
>= nr_pages
)
1417 reclaim_state
.reclaimed_slab
= 0;
1418 shrink_slab(sc
.nr_scanned
, sc
.gfp_mask
, lru_pages
);
1419 ret
+= reclaim_state
.reclaimed_slab
;
1420 if (ret
>= nr_pages
)
1423 if (sc
.nr_scanned
&& prio
< DEF_PRIORITY
- 2)
1424 blk_congestion_wait(WRITE
, HZ
/ 10);
1431 * If ret = 0, we could not shrink LRUs, but there may be something
1436 reclaim_state
.reclaimed_slab
= 0;
1437 shrink_slab(nr_pages
, sc
.gfp_mask
, lru_pages
);
1438 ret
+= reclaim_state
.reclaimed_slab
;
1439 } while (ret
< nr_pages
&& reclaim_state
.reclaimed_slab
> 0);
1442 current
->reclaim_state
= NULL
;
1448 #ifdef CONFIG_HOTPLUG_CPU
1449 /* It's optimal to keep kswapds on the same CPUs as their memory, but
1450 not required for correctness. So if the last cpu in a node goes
1451 away, we get changed to run anywhere: as the first one comes back,
1452 restore their cpu bindings. */
1453 static int cpu_callback(struct notifier_block
*nfb
,
1454 unsigned long action
, void *hcpu
)
1459 if (action
== CPU_ONLINE
) {
1460 for_each_online_pgdat(pgdat
) {
1461 mask
= node_to_cpumask(pgdat
->node_id
);
1462 if (any_online_cpu(mask
) != NR_CPUS
)
1463 /* One of our CPUs online: restore mask */
1464 set_cpus_allowed(pgdat
->kswapd
, mask
);
1469 #endif /* CONFIG_HOTPLUG_CPU */
1471 static int __init
kswapd_init(void)
1476 for_each_online_pgdat(pgdat
) {
1479 pid
= kernel_thread(kswapd
, pgdat
, CLONE_KERNEL
);
1481 read_lock(&tasklist_lock
);
1482 pgdat
->kswapd
= find_task_by_pid(pid
);
1483 read_unlock(&tasklist_lock
);
1485 total_memory
= nr_free_pagecache_pages();
1486 hotcpu_notifier(cpu_callback
, 0);
1490 module_init(kswapd_init
)
1496 * If non-zero call zone_reclaim when the number of free pages falls below
1499 * In the future we may add flags to the mode. However, the page allocator
1500 * should only have to check that zone_reclaim_mode != 0 before calling
1503 int zone_reclaim_mode __read_mostly
;
1505 #define RECLAIM_OFF 0
1506 #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1507 #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1508 #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1509 #define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
1512 * Mininum time between zone reclaim scans
1514 int zone_reclaim_interval __read_mostly
= 30*HZ
;
1517 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1518 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1521 #define ZONE_RECLAIM_PRIORITY 4
1524 * Try to free up some pages from this zone through reclaim.
1526 static int __zone_reclaim(struct zone
*zone
, gfp_t gfp_mask
, unsigned int order
)
1528 /* Minimum pages needed in order to stay on node */
1529 const unsigned long nr_pages
= 1 << order
;
1530 struct task_struct
*p
= current
;
1531 struct reclaim_state reclaim_state
;
1533 unsigned long nr_reclaimed
= 0;
1534 struct scan_control sc
= {
1535 .may_writepage
= !!(zone_reclaim_mode
& RECLAIM_WRITE
),
1536 .may_swap
= !!(zone_reclaim_mode
& RECLAIM_SWAP
),
1537 .nr_mapped
= read_page_state(nr_mapped
),
1538 .swap_cluster_max
= max_t(unsigned long, nr_pages
,
1540 .gfp_mask
= gfp_mask
,
1541 .swappiness
= vm_swappiness
,
1544 disable_swap_token();
1547 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1548 * and we also need to be able to write out pages for RECLAIM_WRITE
1551 p
->flags
|= PF_MEMALLOC
| PF_SWAPWRITE
;
1552 reclaim_state
.reclaimed_slab
= 0;
1553 p
->reclaim_state
= &reclaim_state
;
1556 * Free memory by calling shrink zone with increasing priorities
1557 * until we have enough memory freed.
1559 priority
= ZONE_RECLAIM_PRIORITY
;
1561 nr_reclaimed
+= shrink_zone(priority
, zone
, &sc
);
1563 } while (priority
>= 0 && nr_reclaimed
< nr_pages
);
1565 if (nr_reclaimed
< nr_pages
&& (zone_reclaim_mode
& RECLAIM_SLAB
)) {
1567 * shrink_slab() does not currently allow us to determine how
1568 * many pages were freed in this zone. So we just shake the slab
1569 * a bit and then go off node for this particular allocation
1570 * despite possibly having freed enough memory to allocate in
1571 * this zone. If we freed local memory then the next
1572 * allocations will be local again.
1574 * shrink_slab will free memory on all zones and may take
1577 shrink_slab(sc
.nr_scanned
, gfp_mask
, order
);
1580 p
->reclaim_state
= NULL
;
1581 current
->flags
&= ~(PF_MEMALLOC
| PF_SWAPWRITE
);
1583 if (nr_reclaimed
== 0) {
1585 * We were unable to reclaim enough pages to stay on node. We
1586 * now allow off node accesses for a certain time period before
1587 * trying again to reclaim pages from the local zone.
1589 zone
->last_unsuccessful_zone_reclaim
= jiffies
;
1592 return nr_reclaimed
>= nr_pages
;
1595 int zone_reclaim(struct zone
*zone
, gfp_t gfp_mask
, unsigned int order
)
1601 * Do not reclaim if there was a recent unsuccessful attempt at zone
1602 * reclaim. In that case we let allocations go off node for the
1603 * zone_reclaim_interval. Otherwise we would scan for each off-node
1606 if (time_before(jiffies
,
1607 zone
->last_unsuccessful_zone_reclaim
+ zone_reclaim_interval
))
1611 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1612 * not have reclaimable pages and if we should not delay the allocation
1615 if (!(gfp_mask
& __GFP_WAIT
) ||
1616 zone
->all_unreclaimable
||
1617 atomic_read(&zone
->reclaim_in_progress
) > 0 ||
1618 (current
->flags
& PF_MEMALLOC
))
1622 * Only run zone reclaim on the local zone or on zones that do not
1623 * have associated processors. This will favor the local processor
1624 * over remote processors and spread off node memory allocations
1625 * as wide as possible.
1627 node_id
= zone
->zone_pgdat
->node_id
;
1628 mask
= node_to_cpumask(node_id
);
1629 if (!cpus_empty(mask
) && node_id
!= numa_node_id())
1631 return __zone_reclaim(zone
, gfp_mask
, order
);