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 /* Can pages be swapped as part of reclaim? */
77 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
78 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
79 * In this context, it doesn't matter that we scan the
80 * whole list at once. */
85 * The list of shrinker callbacks used by to apply pressure to
90 struct list_head list
;
91 int seeks
; /* seeks to recreate an obj */
92 long nr
; /* objs pending delete */
95 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
97 #ifdef ARCH_HAS_PREFETCH
98 #define prefetch_prev_lru_page(_page, _base, _field) \
100 if ((_page)->lru.prev != _base) { \
103 prev = lru_to_page(&(_page->lru)); \
104 prefetch(&prev->_field); \
108 #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
111 #ifdef ARCH_HAS_PREFETCHW
112 #define prefetchw_prev_lru_page(_page, _base, _field) \
114 if ((_page)->lru.prev != _base) { \
117 prev = lru_to_page(&(_page->lru)); \
118 prefetchw(&prev->_field); \
122 #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
126 * From 0 .. 100. Higher means more swappy.
128 int vm_swappiness
= 60;
129 static long total_memory
;
131 static LIST_HEAD(shrinker_list
);
132 static DECLARE_RWSEM(shrinker_rwsem
);
135 * Add a shrinker callback to be called from the vm
137 struct shrinker
*set_shrinker(int seeks
, shrinker_t theshrinker
)
139 struct shrinker
*shrinker
;
141 shrinker
= kmalloc(sizeof(*shrinker
), GFP_KERNEL
);
143 shrinker
->shrinker
= theshrinker
;
144 shrinker
->seeks
= seeks
;
146 down_write(&shrinker_rwsem
);
147 list_add_tail(&shrinker
->list
, &shrinker_list
);
148 up_write(&shrinker_rwsem
);
152 EXPORT_SYMBOL(set_shrinker
);
157 void remove_shrinker(struct shrinker
*shrinker
)
159 down_write(&shrinker_rwsem
);
160 list_del(&shrinker
->list
);
161 up_write(&shrinker_rwsem
);
164 EXPORT_SYMBOL(remove_shrinker
);
166 #define SHRINK_BATCH 128
168 * Call the shrink functions to age shrinkable caches
170 * Here we assume it costs one seek to replace a lru page and that it also
171 * takes a seek to recreate a cache object. With this in mind we age equal
172 * percentages of the lru and ageable caches. This should balance the seeks
173 * generated by these structures.
175 * If the vm encounted mapped pages on the LRU it increase the pressure on
176 * slab to avoid swapping.
178 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
180 * `lru_pages' represents the number of on-LRU pages in all the zones which
181 * are eligible for the caller's allocation attempt. It is used for balancing
182 * slab reclaim versus page reclaim.
184 * Returns the number of slab objects which we shrunk.
186 int shrink_slab(unsigned long scanned
, gfp_t gfp_mask
, unsigned long lru_pages
)
188 struct shrinker
*shrinker
;
192 scanned
= SWAP_CLUSTER_MAX
;
194 if (!down_read_trylock(&shrinker_rwsem
))
195 return 1; /* Assume we'll be able to shrink next time */
197 list_for_each_entry(shrinker
, &shrinker_list
, list
) {
198 unsigned long long delta
;
199 unsigned long total_scan
;
200 unsigned long max_pass
= (*shrinker
->shrinker
)(0, gfp_mask
);
202 delta
= (4 * scanned
) / shrinker
->seeks
;
204 do_div(delta
, lru_pages
+ 1);
205 shrinker
->nr
+= delta
;
206 if (shrinker
->nr
< 0) {
207 printk(KERN_ERR
"%s: nr=%ld\n",
208 __FUNCTION__
, shrinker
->nr
);
209 shrinker
->nr
= max_pass
;
213 * Avoid risking looping forever due to too large nr value:
214 * never try to free more than twice the estimate number of
217 if (shrinker
->nr
> max_pass
* 2)
218 shrinker
->nr
= max_pass
* 2;
220 total_scan
= shrinker
->nr
;
223 while (total_scan
>= SHRINK_BATCH
) {
224 long this_scan
= SHRINK_BATCH
;
228 nr_before
= (*shrinker
->shrinker
)(0, gfp_mask
);
229 shrink_ret
= (*shrinker
->shrinker
)(this_scan
, gfp_mask
);
230 if (shrink_ret
== -1)
232 if (shrink_ret
< nr_before
)
233 ret
+= nr_before
- shrink_ret
;
234 mod_page_state(slabs_scanned
, this_scan
);
235 total_scan
-= this_scan
;
240 shrinker
->nr
+= total_scan
;
242 up_read(&shrinker_rwsem
);
246 /* Called without lock on whether page is mapped, so answer is unstable */
247 static inline int page_mapping_inuse(struct page
*page
)
249 struct address_space
*mapping
;
251 /* Page is in somebody's page tables. */
252 if (page_mapped(page
))
255 /* Be more reluctant to reclaim swapcache than pagecache */
256 if (PageSwapCache(page
))
259 mapping
= page_mapping(page
);
263 /* File is mmap'd by somebody? */
264 return mapping_mapped(mapping
);
267 static inline int is_page_cache_freeable(struct page
*page
)
269 return page_count(page
) - !!PagePrivate(page
) == 2;
272 static int may_write_to_queue(struct backing_dev_info
*bdi
)
274 if (current
->flags
& PF_SWAPWRITE
)
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
);
379 static 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_list adds the number of reclaimed pages to sc->nr_reclaimed
419 static int shrink_list(struct list_head
*page_list
, struct scan_control
*sc
)
421 LIST_HEAD(ret_pages
);
422 struct pagevec freed_pvec
;
428 pagevec_init(&freed_pvec
, 1);
429 while (!list_empty(page_list
)) {
430 struct address_space
*mapping
;
437 page
= lru_to_page(page_list
);
438 list_del(&page
->lru
);
440 if (TestSetPageLocked(page
))
443 BUG_ON(PageActive(page
));
447 if (!sc
->may_swap
&& page_mapped(page
))
450 /* Double the slab pressure for mapped and swapcache pages */
451 if (page_mapped(page
) || PageSwapCache(page
))
454 if (PageWriteback(page
))
457 referenced
= page_referenced(page
, 1);
458 /* In active use or really unfreeable? Activate it. */
459 if (referenced
&& page_mapping_inuse(page
))
460 goto activate_locked
;
464 * Anonymous process memory has backing store?
465 * Try to allocate it some swap space here.
467 if (PageAnon(page
) && !PageSwapCache(page
)) {
470 if (!add_to_swap(page
, GFP_ATOMIC
))
471 goto activate_locked
;
473 #endif /* CONFIG_SWAP */
475 mapping
= page_mapping(page
);
476 may_enter_fs
= (sc
->gfp_mask
& __GFP_FS
) ||
477 (PageSwapCache(page
) && (sc
->gfp_mask
& __GFP_IO
));
480 * The page is mapped into the page tables of one or more
481 * processes. Try to unmap it here.
483 if (page_mapped(page
) && mapping
) {
485 * No unmapping if we do not swap
490 switch (try_to_unmap(page
, 0)) {
492 goto activate_locked
;
496 ; /* try to free the page below */
500 if (PageDirty(page
)) {
505 if (!sc
->may_writepage
)
508 /* Page is dirty, try to write it out here */
509 switch(pageout(page
, mapping
)) {
513 goto activate_locked
;
515 if (PageWriteback(page
) || PageDirty(page
))
518 * A synchronous write - probably a ramdisk. Go
519 * ahead and try to reclaim the page.
521 if (TestSetPageLocked(page
))
523 if (PageDirty(page
) || PageWriteback(page
))
525 mapping
= page_mapping(page
);
527 ; /* try to free the page below */
532 * If the page has buffers, try to free the buffer mappings
533 * associated with this page. If we succeed we try to free
536 * We do this even if the page is PageDirty().
537 * try_to_release_page() does not perform I/O, but it is
538 * possible for a page to have PageDirty set, but it is actually
539 * clean (all its buffers are clean). This happens if the
540 * buffers were written out directly, with submit_bh(). ext3
541 * will do this, as well as the blockdev mapping.
542 * try_to_release_page() will discover that cleanness and will
543 * drop the buffers and mark the page clean - it can be freed.
545 * Rarely, pages can have buffers and no ->mapping. These are
546 * the pages which were not successfully invalidated in
547 * truncate_complete_page(). We try to drop those buffers here
548 * and if that worked, and the page is no longer mapped into
549 * process address space (page_count == 1) it can be freed.
550 * Otherwise, leave the page on the LRU so it is swappable.
552 if (PagePrivate(page
)) {
553 if (!try_to_release_page(page
, sc
->gfp_mask
))
554 goto activate_locked
;
555 if (!mapping
&& page_count(page
) == 1)
559 if (!remove_mapping(mapping
, page
))
565 if (!pagevec_add(&freed_pvec
, page
))
566 __pagevec_release_nonlru(&freed_pvec
);
575 list_add(&page
->lru
, &ret_pages
);
576 BUG_ON(PageLRU(page
));
578 list_splice(&ret_pages
, page_list
);
579 if (pagevec_count(&freed_pvec
))
580 __pagevec_release_nonlru(&freed_pvec
);
581 mod_page_state(pgactivate
, pgactivate
);
582 sc
->nr_reclaimed
+= reclaimed
;
586 #ifdef CONFIG_MIGRATION
587 static inline void move_to_lru(struct page
*page
)
589 list_del(&page
->lru
);
590 if (PageActive(page
)) {
592 * lru_cache_add_active checks that
593 * the PG_active bit is off.
595 ClearPageActive(page
);
596 lru_cache_add_active(page
);
604 * Add isolated pages on the list back to the LRU.
606 * returns the number of pages put back.
608 int putback_lru_pages(struct list_head
*l
)
614 list_for_each_entry_safe(page
, page2
, l
, lru
) {
622 * Non migratable page
624 int fail_migrate_page(struct page
*newpage
, struct page
*page
)
628 EXPORT_SYMBOL(fail_migrate_page
);
631 * swapout a single page
632 * page is locked upon entry, unlocked on exit
634 static int swap_page(struct page
*page
)
636 struct address_space
*mapping
= page_mapping(page
);
638 if (page_mapped(page
) && mapping
)
639 if (try_to_unmap(page
, 1) != SWAP_SUCCESS
)
642 if (PageDirty(page
)) {
643 /* Page is dirty, try to write it out here */
644 switch(pageout(page
, mapping
)) {
653 ; /* try to free the page below */
657 if (PagePrivate(page
)) {
658 if (!try_to_release_page(page
, GFP_KERNEL
) ||
659 (!mapping
&& page_count(page
) == 1))
663 if (remove_mapping(mapping
, page
)) {
675 EXPORT_SYMBOL(swap_page
);
678 * Page migration was first developed in the context of the memory hotplug
679 * project. The main authors of the migration code are:
681 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
682 * Hirokazu Takahashi <taka@valinux.co.jp>
683 * Dave Hansen <haveblue@us.ibm.com>
684 * Christoph Lameter <clameter@sgi.com>
688 * Remove references for a page and establish the new page with the correct
689 * basic settings to be able to stop accesses to the page.
691 int migrate_page_remove_references(struct page
*newpage
,
692 struct page
*page
, int nr_refs
)
694 struct address_space
*mapping
= page_mapping(page
);
695 struct page
**radix_pointer
;
698 * Avoid doing any of the following work if the page count
699 * indicates that the page is in use or truncate has removed
702 if (!mapping
|| page_mapcount(page
) + nr_refs
!= page_count(page
))
706 * Establish swap ptes for anonymous pages or destroy pte
709 * In order to reestablish file backed mappings the fault handlers
710 * will take the radix tree_lock which may then be used to stop
711 * processses from accessing this page until the new page is ready.
713 * A process accessing via a swap pte (an anonymous page) will take a
714 * page_lock on the old page which will block the process until the
715 * migration attempt is complete. At that time the PageSwapCache bit
716 * will be examined. If the page was migrated then the PageSwapCache
717 * bit will be clear and the operation to retrieve the page will be
718 * retried which will find the new page in the radix tree. Then a new
719 * direct mapping may be generated based on the radix tree contents.
721 * If the page was not migrated then the PageSwapCache bit
722 * is still set and the operation may continue.
724 if (try_to_unmap(page
, 1) == SWAP_FAIL
)
725 /* A vma has VM_LOCKED set -> Permanent failure */
729 * Give up if we were unable to remove all mappings.
731 if (page_mapcount(page
))
734 write_lock_irq(&mapping
->tree_lock
);
736 radix_pointer
= (struct page
**)radix_tree_lookup_slot(
740 if (!page_mapping(page
) || page_count(page
) != nr_refs
||
741 *radix_pointer
!= page
) {
742 write_unlock_irq(&mapping
->tree_lock
);
747 * Now we know that no one else is looking at the page.
749 * Certain minimal information about a page must be available
750 * in order for other subsystems to properly handle the page if they
751 * find it through the radix tree update before we are finished
755 newpage
->index
= page
->index
;
756 newpage
->mapping
= page
->mapping
;
757 if (PageSwapCache(page
)) {
758 SetPageSwapCache(newpage
);
759 set_page_private(newpage
, page_private(page
));
762 *radix_pointer
= newpage
;
764 write_unlock_irq(&mapping
->tree_lock
);
768 EXPORT_SYMBOL(migrate_page_remove_references
);
771 * Copy the page to its new location
773 void migrate_page_copy(struct page
*newpage
, struct page
*page
)
775 copy_highpage(newpage
, page
);
778 SetPageError(newpage
);
779 if (PageReferenced(page
))
780 SetPageReferenced(newpage
);
781 if (PageUptodate(page
))
782 SetPageUptodate(newpage
);
783 if (PageActive(page
))
784 SetPageActive(newpage
);
785 if (PageChecked(page
))
786 SetPageChecked(newpage
);
787 if (PageMappedToDisk(page
))
788 SetPageMappedToDisk(newpage
);
790 if (PageDirty(page
)) {
791 clear_page_dirty_for_io(page
);
792 set_page_dirty(newpage
);
795 ClearPageSwapCache(page
);
796 ClearPageActive(page
);
797 ClearPagePrivate(page
);
798 set_page_private(page
, 0);
799 page
->mapping
= NULL
;
802 * If any waiters have accumulated on the new page then
805 if (PageWriteback(newpage
))
806 end_page_writeback(newpage
);
808 EXPORT_SYMBOL(migrate_page_copy
);
811 * Common logic to directly migrate a single page suitable for
812 * pages that do not use PagePrivate.
814 * Pages are locked upon entry and exit.
816 int migrate_page(struct page
*newpage
, struct page
*page
)
820 BUG_ON(PageWriteback(page
)); /* Writeback must be complete */
822 rc
= migrate_page_remove_references(newpage
, page
, 2);
827 migrate_page_copy(newpage
, page
);
830 * Remove auxiliary swap entries and replace
831 * them with real ptes.
833 * Note that a real pte entry will allow processes that are not
834 * waiting on the page lock to use the new page via the page tables
835 * before the new page is unlocked.
837 remove_from_swap(newpage
);
840 EXPORT_SYMBOL(migrate_page
);
845 * Two lists are passed to this function. The first list
846 * contains the pages isolated from the LRU to be migrated.
847 * The second list contains new pages that the pages isolated
848 * can be moved to. If the second list is NULL then all
849 * pages are swapped out.
851 * The function returns after 10 attempts or if no pages
852 * are movable anymore because to has become empty
853 * or no retryable pages exist anymore.
855 * Return: Number of pages not migrated when "to" ran empty.
857 int migrate_pages(struct list_head
*from
, struct list_head
*to
,
858 struct list_head
*moved
, struct list_head
*failed
)
865 int swapwrite
= current
->flags
& PF_SWAPWRITE
;
869 current
->flags
|= PF_SWAPWRITE
;
874 list_for_each_entry_safe(page
, page2
, from
, lru
) {
875 struct page
*newpage
= NULL
;
876 struct address_space
*mapping
;
881 if (page_count(page
) == 1)
882 /* page was freed from under us. So we are done. */
885 if (to
&& list_empty(to
))
889 * Skip locked pages during the first two passes to give the
890 * functions holding the lock time to release the page. Later we
891 * use lock_page() to have a higher chance of acquiring the
898 if (TestSetPageLocked(page
))
902 * Only wait on writeback if we have already done a pass where
903 * we we may have triggered writeouts for lots of pages.
906 wait_on_page_writeback(page
);
908 if (PageWriteback(page
))
913 * Anonymous pages must have swap cache references otherwise
914 * the information contained in the page maps cannot be
917 if (PageAnon(page
) && !PageSwapCache(page
)) {
918 if (!add_to_swap(page
, GFP_KERNEL
)) {
925 rc
= swap_page(page
);
929 newpage
= lru_to_page(to
);
933 * Pages are properly locked and writeback is complete.
934 * Try to migrate the page.
936 mapping
= page_mapping(page
);
940 if (mapping
->a_ops
->migratepage
) {
942 * Most pages have a mapping and most filesystems
943 * should provide a migration function. Anonymous
944 * pages are part of swap space which also has its
945 * own migration function. This is the most common
946 * path for page migration.
948 rc
= mapping
->a_ops
->migratepage(newpage
, page
);
952 /* Make sure the dirty bit is up to date */
953 if (try_to_unmap(page
, 1) == SWAP_FAIL
) {
958 if (page_mapcount(page
)) {
964 * Default handling if a filesystem does not provide
965 * a migration function. We can only migrate clean
966 * pages so try to write out any dirty pages first.
968 if (PageDirty(page
)) {
969 switch (pageout(page
, mapping
)) {
975 unlock_page(newpage
);
979 ; /* try to migrate the page below */
984 * Buffers are managed in a filesystem specific way.
985 * We must have no buffers or drop them.
987 if (!page_has_buffers(page
) ||
988 try_to_release_page(page
, GFP_KERNEL
)) {
989 rc
= migrate_page(newpage
, page
);
994 * On early passes with mapped pages simply
995 * retry. There may be a lock held for some
996 * buffers that may go away. Later
1001 * Persistently unable to drop buffers..... As a
1002 * measure of last resort we fall back to
1005 unlock_page(newpage
);
1007 rc
= swap_page(page
);
1012 unlock_page(newpage
);
1018 if (rc
== -EAGAIN
) {
1021 /* Permanent failure */
1022 list_move(&page
->lru
, failed
);
1026 /* Successful migration. Return page to LRU */
1027 move_to_lru(newpage
);
1029 list_move(&page
->lru
, moved
);
1032 if (retry
&& pass
++ < 10)
1036 current
->flags
&= ~PF_SWAPWRITE
;
1038 return nr_failed
+ retry
;
1042 * Isolate one page from the LRU lists and put it on the
1043 * indicated list with elevated refcount.
1046 * 0 = page not on LRU list
1047 * 1 = page removed from LRU list and added to the specified list.
1049 int isolate_lru_page(struct page
*page
)
1053 if (PageLRU(page
)) {
1054 struct zone
*zone
= page_zone(page
);
1055 spin_lock_irq(&zone
->lru_lock
);
1056 if (TestClearPageLRU(page
)) {
1059 if (PageActive(page
))
1060 del_page_from_active_list(zone
, page
);
1062 del_page_from_inactive_list(zone
, page
);
1064 spin_unlock_irq(&zone
->lru_lock
);
1072 * zone->lru_lock is heavily contended. Some of the functions that
1073 * shrink the lists perform better by taking out a batch of pages
1074 * and working on them outside the LRU lock.
1076 * For pagecache intensive workloads, this function is the hottest
1077 * spot in the kernel (apart from copy_*_user functions).
1079 * Appropriate locks must be held before calling this function.
1081 * @nr_to_scan: The number of pages to look through on the list.
1082 * @src: The LRU list to pull pages off.
1083 * @dst: The temp list to put pages on to.
1084 * @scanned: The number of pages that were scanned.
1086 * returns how many pages were moved onto *@dst.
1088 static int isolate_lru_pages(int nr_to_scan
, struct list_head
*src
,
1089 struct list_head
*dst
, int *scanned
)
1095 while (scan
++ < nr_to_scan
&& !list_empty(src
)) {
1096 page
= lru_to_page(src
);
1097 prefetchw_prev_lru_page(page
, src
, flags
);
1099 if (!TestClearPageLRU(page
))
1101 list_del(&page
->lru
);
1102 if (get_page_testone(page
)) {
1104 * It is being freed elsewhere
1108 list_add(&page
->lru
, src
);
1111 list_add(&page
->lru
, dst
);
1121 * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed
1123 static void shrink_cache(struct zone
*zone
, struct scan_control
*sc
)
1125 LIST_HEAD(page_list
);
1126 struct pagevec pvec
;
1127 int max_scan
= sc
->nr_to_scan
;
1129 pagevec_init(&pvec
, 1);
1132 spin_lock_irq(&zone
->lru_lock
);
1133 while (max_scan
> 0) {
1139 nr_taken
= isolate_lru_pages(sc
->swap_cluster_max
,
1140 &zone
->inactive_list
,
1141 &page_list
, &nr_scan
);
1142 zone
->nr_inactive
-= nr_taken
;
1143 zone
->pages_scanned
+= nr_scan
;
1144 spin_unlock_irq(&zone
->lru_lock
);
1149 max_scan
-= nr_scan
;
1150 nr_freed
= shrink_list(&page_list
, sc
);
1152 local_irq_disable();
1153 if (current_is_kswapd()) {
1154 __mod_page_state_zone(zone
, pgscan_kswapd
, nr_scan
);
1155 __mod_page_state(kswapd_steal
, nr_freed
);
1157 __mod_page_state_zone(zone
, pgscan_direct
, nr_scan
);
1158 __mod_page_state_zone(zone
, pgsteal
, nr_freed
);
1160 spin_lock(&zone
->lru_lock
);
1162 * Put back any unfreeable pages.
1164 while (!list_empty(&page_list
)) {
1165 page
= lru_to_page(&page_list
);
1166 if (TestSetPageLRU(page
))
1168 list_del(&page
->lru
);
1169 if (PageActive(page
))
1170 add_page_to_active_list(zone
, page
);
1172 add_page_to_inactive_list(zone
, page
);
1173 if (!pagevec_add(&pvec
, page
)) {
1174 spin_unlock_irq(&zone
->lru_lock
);
1175 __pagevec_release(&pvec
);
1176 spin_lock_irq(&zone
->lru_lock
);
1180 spin_unlock_irq(&zone
->lru_lock
);
1182 pagevec_release(&pvec
);
1186 * This moves pages from the active list to the inactive list.
1188 * We move them the other way if the page is referenced by one or more
1189 * processes, from rmap.
1191 * If the pages are mostly unmapped, the processing is fast and it is
1192 * appropriate to hold zone->lru_lock across the whole operation. But if
1193 * the pages are mapped, the processing is slow (page_referenced()) so we
1194 * should drop zone->lru_lock around each page. It's impossible to balance
1195 * this, so instead we remove the pages from the LRU while processing them.
1196 * It is safe to rely on PG_active against the non-LRU pages in here because
1197 * nobody will play with that bit on a non-LRU page.
1199 * The downside is that we have to touch page->_count against each page.
1200 * But we had to alter page->flags anyway.
1203 refill_inactive_zone(struct zone
*zone
, struct scan_control
*sc
)
1206 int pgdeactivate
= 0;
1208 int nr_pages
= sc
->nr_to_scan
;
1209 LIST_HEAD(l_hold
); /* The pages which were snipped off */
1210 LIST_HEAD(l_inactive
); /* Pages to go onto the inactive_list */
1211 LIST_HEAD(l_active
); /* Pages to go onto the active_list */
1213 struct pagevec pvec
;
1214 int reclaim_mapped
= 0;
1216 if (unlikely(sc
->may_swap
)) {
1222 * `distress' is a measure of how much trouble we're having
1223 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
1225 distress
= 100 >> zone
->prev_priority
;
1228 * The point of this algorithm is to decide when to start
1229 * reclaiming mapped memory instead of just pagecache. Work out
1233 mapped_ratio
= (sc
->nr_mapped
* 100) / total_memory
;
1236 * Now decide how much we really want to unmap some pages. The
1237 * mapped ratio is downgraded - just because there's a lot of
1238 * mapped memory doesn't necessarily mean that page reclaim
1241 * The distress ratio is important - we don't want to start
1244 * A 100% value of vm_swappiness overrides this algorithm
1247 swap_tendency
= mapped_ratio
/ 2 + distress
+ vm_swappiness
;
1250 * Now use this metric to decide whether to start moving mapped
1251 * memory onto the inactive list.
1253 if (swap_tendency
>= 100)
1258 spin_lock_irq(&zone
->lru_lock
);
1259 pgmoved
= isolate_lru_pages(nr_pages
, &zone
->active_list
,
1260 &l_hold
, &pgscanned
);
1261 zone
->pages_scanned
+= pgscanned
;
1262 zone
->nr_active
-= pgmoved
;
1263 spin_unlock_irq(&zone
->lru_lock
);
1265 while (!list_empty(&l_hold
)) {
1267 page
= lru_to_page(&l_hold
);
1268 list_del(&page
->lru
);
1269 if (page_mapped(page
)) {
1270 if (!reclaim_mapped
||
1271 (total_swap_pages
== 0 && PageAnon(page
)) ||
1272 page_referenced(page
, 0)) {
1273 list_add(&page
->lru
, &l_active
);
1277 list_add(&page
->lru
, &l_inactive
);
1280 pagevec_init(&pvec
, 1);
1282 spin_lock_irq(&zone
->lru_lock
);
1283 while (!list_empty(&l_inactive
)) {
1284 page
= lru_to_page(&l_inactive
);
1285 prefetchw_prev_lru_page(page
, &l_inactive
, flags
);
1286 if (TestSetPageLRU(page
))
1288 if (!TestClearPageActive(page
))
1290 list_move(&page
->lru
, &zone
->inactive_list
);
1292 if (!pagevec_add(&pvec
, page
)) {
1293 zone
->nr_inactive
+= pgmoved
;
1294 spin_unlock_irq(&zone
->lru_lock
);
1295 pgdeactivate
+= pgmoved
;
1297 if (buffer_heads_over_limit
)
1298 pagevec_strip(&pvec
);
1299 __pagevec_release(&pvec
);
1300 spin_lock_irq(&zone
->lru_lock
);
1303 zone
->nr_inactive
+= pgmoved
;
1304 pgdeactivate
+= pgmoved
;
1305 if (buffer_heads_over_limit
) {
1306 spin_unlock_irq(&zone
->lru_lock
);
1307 pagevec_strip(&pvec
);
1308 spin_lock_irq(&zone
->lru_lock
);
1312 while (!list_empty(&l_active
)) {
1313 page
= lru_to_page(&l_active
);
1314 prefetchw_prev_lru_page(page
, &l_active
, flags
);
1315 if (TestSetPageLRU(page
))
1317 BUG_ON(!PageActive(page
));
1318 list_move(&page
->lru
, &zone
->active_list
);
1320 if (!pagevec_add(&pvec
, page
)) {
1321 zone
->nr_active
+= pgmoved
;
1323 spin_unlock_irq(&zone
->lru_lock
);
1324 __pagevec_release(&pvec
);
1325 spin_lock_irq(&zone
->lru_lock
);
1328 zone
->nr_active
+= pgmoved
;
1329 spin_unlock(&zone
->lru_lock
);
1331 __mod_page_state_zone(zone
, pgrefill
, pgscanned
);
1332 __mod_page_state(pgdeactivate
, pgdeactivate
);
1335 pagevec_release(&pvec
);
1339 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1342 shrink_zone(struct zone
*zone
, struct scan_control
*sc
)
1344 unsigned long nr_active
;
1345 unsigned long nr_inactive
;
1347 atomic_inc(&zone
->reclaim_in_progress
);
1350 * Add one to `nr_to_scan' just to make sure that the kernel will
1351 * slowly sift through the active list.
1353 zone
->nr_scan_active
+= (zone
->nr_active
>> sc
->priority
) + 1;
1354 nr_active
= zone
->nr_scan_active
;
1355 if (nr_active
>= sc
->swap_cluster_max
)
1356 zone
->nr_scan_active
= 0;
1360 zone
->nr_scan_inactive
+= (zone
->nr_inactive
>> sc
->priority
) + 1;
1361 nr_inactive
= zone
->nr_scan_inactive
;
1362 if (nr_inactive
>= sc
->swap_cluster_max
)
1363 zone
->nr_scan_inactive
= 0;
1367 while (nr_active
|| nr_inactive
) {
1369 sc
->nr_to_scan
= min(nr_active
,
1370 (unsigned long)sc
->swap_cluster_max
);
1371 nr_active
-= sc
->nr_to_scan
;
1372 refill_inactive_zone(zone
, sc
);
1376 sc
->nr_to_scan
= min(nr_inactive
,
1377 (unsigned long)sc
->swap_cluster_max
);
1378 nr_inactive
-= sc
->nr_to_scan
;
1379 shrink_cache(zone
, sc
);
1383 throttle_vm_writeout();
1385 atomic_dec(&zone
->reclaim_in_progress
);
1389 * This is the direct reclaim path, for page-allocating processes. We only
1390 * try to reclaim pages from zones which will satisfy the caller's allocation
1393 * We reclaim from a zone even if that zone is over pages_high. Because:
1394 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1396 * b) The zones may be over pages_high but they must go *over* pages_high to
1397 * satisfy the `incremental min' zone defense algorithm.
1399 * Returns the number of reclaimed pages.
1401 * If a zone is deemed to be full of pinned pages then just give it a light
1402 * scan then give up on it.
1405 shrink_caches(struct zone
**zones
, struct scan_control
*sc
)
1409 for (i
= 0; zones
[i
] != NULL
; i
++) {
1410 struct zone
*zone
= zones
[i
];
1412 if (!populated_zone(zone
))
1415 if (!cpuset_zone_allowed(zone
, __GFP_HARDWALL
))
1418 zone
->temp_priority
= sc
->priority
;
1419 if (zone
->prev_priority
> sc
->priority
)
1420 zone
->prev_priority
= sc
->priority
;
1422 if (zone
->all_unreclaimable
&& sc
->priority
!= DEF_PRIORITY
)
1423 continue; /* Let kswapd poll it */
1425 shrink_zone(zone
, sc
);
1430 * This is the main entry point to direct page reclaim.
1432 * If a full scan of the inactive list fails to free enough memory then we
1433 * are "out of memory" and something needs to be killed.
1435 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1436 * high - the zone may be full of dirty or under-writeback pages, which this
1437 * caller can't do much about. We kick pdflush and take explicit naps in the
1438 * hope that some of these pages can be written. But if the allocating task
1439 * holds filesystem locks which prevent writeout this might not work, and the
1440 * allocation attempt will fail.
1442 int try_to_free_pages(struct zone
**zones
, gfp_t gfp_mask
)
1446 int total_scanned
= 0, total_reclaimed
= 0;
1447 struct reclaim_state
*reclaim_state
= current
->reclaim_state
;
1448 struct scan_control sc
;
1449 unsigned long lru_pages
= 0;
1452 sc
.gfp_mask
= gfp_mask
;
1453 sc
.may_writepage
= !laptop_mode
;
1456 inc_page_state(allocstall
);
1458 for (i
= 0; zones
[i
] != NULL
; i
++) {
1459 struct zone
*zone
= zones
[i
];
1461 if (!cpuset_zone_allowed(zone
, __GFP_HARDWALL
))
1464 zone
->temp_priority
= DEF_PRIORITY
;
1465 lru_pages
+= zone
->nr_active
+ zone
->nr_inactive
;
1468 for (priority
= DEF_PRIORITY
; priority
>= 0; priority
--) {
1469 sc
.nr_mapped
= read_page_state(nr_mapped
);
1471 sc
.nr_reclaimed
= 0;
1472 sc
.priority
= priority
;
1473 sc
.swap_cluster_max
= SWAP_CLUSTER_MAX
;
1475 disable_swap_token();
1476 shrink_caches(zones
, &sc
);
1477 shrink_slab(sc
.nr_scanned
, gfp_mask
, lru_pages
);
1478 if (reclaim_state
) {
1479 sc
.nr_reclaimed
+= reclaim_state
->reclaimed_slab
;
1480 reclaim_state
->reclaimed_slab
= 0;
1482 total_scanned
+= sc
.nr_scanned
;
1483 total_reclaimed
+= sc
.nr_reclaimed
;
1484 if (total_reclaimed
>= sc
.swap_cluster_max
) {
1490 * Try to write back as many pages as we just scanned. This
1491 * tends to cause slow streaming writers to write data to the
1492 * disk smoothly, at the dirtying rate, which is nice. But
1493 * that's undesirable in laptop mode, where we *want* lumpy
1494 * writeout. So in laptop mode, write out the whole world.
1496 if (total_scanned
> sc
.swap_cluster_max
+ sc
.swap_cluster_max
/2) {
1497 wakeup_pdflush(laptop_mode
? 0 : total_scanned
);
1498 sc
.may_writepage
= 1;
1501 /* Take a nap, wait for some writeback to complete */
1502 if (sc
.nr_scanned
&& priority
< DEF_PRIORITY
- 2)
1503 blk_congestion_wait(WRITE
, HZ
/10);
1506 for (i
= 0; zones
[i
] != 0; i
++) {
1507 struct zone
*zone
= zones
[i
];
1509 if (!cpuset_zone_allowed(zone
, __GFP_HARDWALL
))
1512 zone
->prev_priority
= zone
->temp_priority
;
1518 * For kswapd, balance_pgdat() will work across all this node's zones until
1519 * they are all at pages_high.
1521 * If `nr_pages' is non-zero then it is the number of pages which are to be
1522 * reclaimed, regardless of the zone occupancies. This is a software suspend
1525 * Returns the number of pages which were actually freed.
1527 * There is special handling here for zones which are full of pinned pages.
1528 * This can happen if the pages are all mlocked, or if they are all used by
1529 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1530 * What we do is to detect the case where all pages in the zone have been
1531 * scanned twice and there has been zero successful reclaim. Mark the zone as
1532 * dead and from now on, only perform a short scan. Basically we're polling
1533 * the zone for when the problem goes away.
1535 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1536 * zones which have free_pages > pages_high, but once a zone is found to have
1537 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1538 * of the number of free pages in the lower zones. This interoperates with
1539 * the page allocator fallback scheme to ensure that aging of pages is balanced
1542 static int balance_pgdat(pg_data_t
*pgdat
, int nr_pages
, int order
)
1544 int to_free
= nr_pages
;
1548 int total_scanned
, total_reclaimed
;
1549 struct reclaim_state
*reclaim_state
= current
->reclaim_state
;
1550 struct scan_control sc
;
1554 total_reclaimed
= 0;
1555 sc
.gfp_mask
= GFP_KERNEL
;
1556 sc
.may_writepage
= !laptop_mode
;
1558 sc
.nr_mapped
= read_page_state(nr_mapped
);
1560 inc_page_state(pageoutrun
);
1562 for (i
= 0; i
< pgdat
->nr_zones
; i
++) {
1563 struct zone
*zone
= pgdat
->node_zones
+ i
;
1565 zone
->temp_priority
= DEF_PRIORITY
;
1568 for (priority
= DEF_PRIORITY
; priority
>= 0; priority
--) {
1569 int end_zone
= 0; /* Inclusive. 0 = ZONE_DMA */
1570 unsigned long lru_pages
= 0;
1572 /* The swap token gets in the way of swapout... */
1574 disable_swap_token();
1578 if (nr_pages
== 0) {
1580 * Scan in the highmem->dma direction for the highest
1581 * zone which needs scanning
1583 for (i
= pgdat
->nr_zones
- 1; i
>= 0; i
--) {
1584 struct zone
*zone
= pgdat
->node_zones
+ i
;
1586 if (!populated_zone(zone
))
1589 if (zone
->all_unreclaimable
&&
1590 priority
!= DEF_PRIORITY
)
1593 if (!zone_watermark_ok(zone
, order
,
1594 zone
->pages_high
, 0, 0)) {
1601 end_zone
= pgdat
->nr_zones
- 1;
1604 for (i
= 0; i
<= end_zone
; i
++) {
1605 struct zone
*zone
= pgdat
->node_zones
+ i
;
1607 lru_pages
+= zone
->nr_active
+ zone
->nr_inactive
;
1611 * Now scan the zone in the dma->highmem direction, stopping
1612 * at the last zone which needs scanning.
1614 * We do this because the page allocator works in the opposite
1615 * direction. This prevents the page allocator from allocating
1616 * pages behind kswapd's direction of progress, which would
1617 * cause too much scanning of the lower zones.
1619 for (i
= 0; i
<= end_zone
; i
++) {
1620 struct zone
*zone
= pgdat
->node_zones
+ i
;
1623 if (!populated_zone(zone
))
1626 if (zone
->all_unreclaimable
&& priority
!= DEF_PRIORITY
)
1629 if (nr_pages
== 0) { /* Not software suspend */
1630 if (!zone_watermark_ok(zone
, order
,
1631 zone
->pages_high
, end_zone
, 0))
1634 zone
->temp_priority
= priority
;
1635 if (zone
->prev_priority
> priority
)
1636 zone
->prev_priority
= priority
;
1638 sc
.nr_reclaimed
= 0;
1639 sc
.priority
= priority
;
1640 sc
.swap_cluster_max
= nr_pages
? nr_pages
: SWAP_CLUSTER_MAX
;
1641 shrink_zone(zone
, &sc
);
1642 reclaim_state
->reclaimed_slab
= 0;
1643 nr_slab
= shrink_slab(sc
.nr_scanned
, GFP_KERNEL
,
1645 sc
.nr_reclaimed
+= reclaim_state
->reclaimed_slab
;
1646 total_reclaimed
+= sc
.nr_reclaimed
;
1647 total_scanned
+= sc
.nr_scanned
;
1648 if (zone
->all_unreclaimable
)
1650 if (nr_slab
== 0 && zone
->pages_scanned
>=
1651 (zone
->nr_active
+ zone
->nr_inactive
) * 4)
1652 zone
->all_unreclaimable
= 1;
1654 * If we've done a decent amount of scanning and
1655 * the reclaim ratio is low, start doing writepage
1656 * even in laptop mode
1658 if (total_scanned
> SWAP_CLUSTER_MAX
* 2 &&
1659 total_scanned
> total_reclaimed
+total_reclaimed
/2)
1660 sc
.may_writepage
= 1;
1662 if (nr_pages
&& to_free
> total_reclaimed
)
1663 continue; /* swsusp: need to do more work */
1665 break; /* kswapd: all done */
1667 * OK, kswapd is getting into trouble. Take a nap, then take
1668 * another pass across the zones.
1670 if (total_scanned
&& priority
< DEF_PRIORITY
- 2)
1671 blk_congestion_wait(WRITE
, HZ
/10);
1674 * We do this so kswapd doesn't build up large priorities for
1675 * example when it is freeing in parallel with allocators. It
1676 * matches the direct reclaim path behaviour in terms of impact
1677 * on zone->*_priority.
1679 if ((total_reclaimed
>= SWAP_CLUSTER_MAX
) && (!nr_pages
))
1683 for (i
= 0; i
< pgdat
->nr_zones
; i
++) {
1684 struct zone
*zone
= pgdat
->node_zones
+ i
;
1686 zone
->prev_priority
= zone
->temp_priority
;
1688 if (!all_zones_ok
) {
1693 return total_reclaimed
;
1697 * The background pageout daemon, started as a kernel thread
1698 * from the init process.
1700 * This basically trickles out pages so that we have _some_
1701 * free memory available even if there is no other activity
1702 * that frees anything up. This is needed for things like routing
1703 * etc, where we otherwise might have all activity going on in
1704 * asynchronous contexts that cannot page things out.
1706 * If there are applications that are active memory-allocators
1707 * (most normal use), this basically shouldn't matter.
1709 static int kswapd(void *p
)
1711 unsigned long order
;
1712 pg_data_t
*pgdat
= (pg_data_t
*)p
;
1713 struct task_struct
*tsk
= current
;
1715 struct reclaim_state reclaim_state
= {
1716 .reclaimed_slab
= 0,
1720 daemonize("kswapd%d", pgdat
->node_id
);
1721 cpumask
= node_to_cpumask(pgdat
->node_id
);
1722 if (!cpus_empty(cpumask
))
1723 set_cpus_allowed(tsk
, cpumask
);
1724 current
->reclaim_state
= &reclaim_state
;
1727 * Tell the memory management that we're a "memory allocator",
1728 * and that if we need more memory we should get access to it
1729 * regardless (see "__alloc_pages()"). "kswapd" should
1730 * never get caught in the normal page freeing logic.
1732 * (Kswapd normally doesn't need memory anyway, but sometimes
1733 * you need a small amount of memory in order to be able to
1734 * page out something else, and this flag essentially protects
1735 * us from recursively trying to free more memory as we're
1736 * trying to free the first piece of memory in the first place).
1738 tsk
->flags
|= PF_MEMALLOC
| PF_SWAPWRITE
| PF_KSWAPD
;
1742 unsigned long new_order
;
1746 prepare_to_wait(&pgdat
->kswapd_wait
, &wait
, TASK_INTERRUPTIBLE
);
1747 new_order
= pgdat
->kswapd_max_order
;
1748 pgdat
->kswapd_max_order
= 0;
1749 if (order
< new_order
) {
1751 * Don't sleep if someone wants a larger 'order'
1757 order
= pgdat
->kswapd_max_order
;
1759 finish_wait(&pgdat
->kswapd_wait
, &wait
);
1761 balance_pgdat(pgdat
, 0, order
);
1767 * A zone is low on free memory, so wake its kswapd task to service it.
1769 void wakeup_kswapd(struct zone
*zone
, int order
)
1773 if (!populated_zone(zone
))
1776 pgdat
= zone
->zone_pgdat
;
1777 if (zone_watermark_ok(zone
, order
, zone
->pages_low
, 0, 0))
1779 if (pgdat
->kswapd_max_order
< order
)
1780 pgdat
->kswapd_max_order
= order
;
1781 if (!cpuset_zone_allowed(zone
, __GFP_HARDWALL
))
1783 if (!waitqueue_active(&pgdat
->kswapd_wait
))
1785 wake_up_interruptible(&pgdat
->kswapd_wait
);
1790 * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
1793 int shrink_all_memory(int nr_pages
)
1796 int nr_to_free
= nr_pages
;
1798 struct reclaim_state reclaim_state
= {
1799 .reclaimed_slab
= 0,
1802 current
->reclaim_state
= &reclaim_state
;
1803 for_each_pgdat(pgdat
) {
1805 freed
= balance_pgdat(pgdat
, nr_to_free
, 0);
1807 nr_to_free
-= freed
;
1808 if (nr_to_free
<= 0)
1811 current
->reclaim_state
= NULL
;
1816 #ifdef CONFIG_HOTPLUG_CPU
1817 /* It's optimal to keep kswapds on the same CPUs as their memory, but
1818 not required for correctness. So if the last cpu in a node goes
1819 away, we get changed to run anywhere: as the first one comes back,
1820 restore their cpu bindings. */
1821 static int __devinit
cpu_callback(struct notifier_block
*nfb
,
1822 unsigned long action
,
1828 if (action
== CPU_ONLINE
) {
1829 for_each_pgdat(pgdat
) {
1830 mask
= node_to_cpumask(pgdat
->node_id
);
1831 if (any_online_cpu(mask
) != NR_CPUS
)
1832 /* One of our CPUs online: restore mask */
1833 set_cpus_allowed(pgdat
->kswapd
, mask
);
1838 #endif /* CONFIG_HOTPLUG_CPU */
1840 static int __init
kswapd_init(void)
1844 for_each_pgdat(pgdat
)
1846 = find_task_by_pid(kernel_thread(kswapd
, pgdat
, CLONE_KERNEL
));
1847 total_memory
= nr_free_pagecache_pages();
1848 hotcpu_notifier(cpu_callback
, 0);
1852 module_init(kswapd_init
)
1858 * If non-zero call zone_reclaim when the number of free pages falls below
1861 * In the future we may add flags to the mode. However, the page allocator
1862 * should only have to check that zone_reclaim_mode != 0 before calling
1865 int zone_reclaim_mode __read_mostly
;
1867 #define RECLAIM_OFF 0
1868 #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1869 #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1870 #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1871 #define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
1874 * Mininum time between zone reclaim scans
1876 int zone_reclaim_interval __read_mostly
= 30*HZ
;
1879 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1880 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1883 #define ZONE_RECLAIM_PRIORITY 4
1886 * Try to free up some pages from this zone through reclaim.
1888 int zone_reclaim(struct zone
*zone
, gfp_t gfp_mask
, unsigned int order
)
1891 struct task_struct
*p
= current
;
1892 struct reclaim_state reclaim_state
;
1893 struct scan_control sc
;
1897 if (time_before(jiffies
,
1898 zone
->last_unsuccessful_zone_reclaim
+ zone_reclaim_interval
))
1901 if (!(gfp_mask
& __GFP_WAIT
) ||
1902 zone
->all_unreclaimable
||
1903 atomic_read(&zone
->reclaim_in_progress
) > 0 ||
1904 (p
->flags
& PF_MEMALLOC
))
1907 node_id
= zone
->zone_pgdat
->node_id
;
1908 mask
= node_to_cpumask(node_id
);
1909 if (!cpus_empty(mask
) && node_id
!= numa_node_id())
1912 sc
.may_writepage
= !!(zone_reclaim_mode
& RECLAIM_WRITE
);
1913 sc
.may_swap
= !!(zone_reclaim_mode
& RECLAIM_SWAP
);
1915 sc
.nr_reclaimed
= 0;
1916 sc
.priority
= ZONE_RECLAIM_PRIORITY
+ 1;
1917 sc
.nr_mapped
= read_page_state(nr_mapped
);
1918 sc
.gfp_mask
= gfp_mask
;
1920 disable_swap_token();
1922 nr_pages
= 1 << order
;
1923 if (nr_pages
> SWAP_CLUSTER_MAX
)
1924 sc
.swap_cluster_max
= nr_pages
;
1926 sc
.swap_cluster_max
= SWAP_CLUSTER_MAX
;
1930 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1931 * and we also need to be able to write out pages for RECLAIM_WRITE
1934 p
->flags
|= PF_MEMALLOC
| PF_SWAPWRITE
;
1935 reclaim_state
.reclaimed_slab
= 0;
1936 p
->reclaim_state
= &reclaim_state
;
1939 * Free memory by calling shrink zone with increasing priorities
1940 * until we have enough memory freed.
1944 shrink_zone(zone
, &sc
);
1946 } while (sc
.nr_reclaimed
< nr_pages
&& sc
.priority
> 0);
1948 if (sc
.nr_reclaimed
< nr_pages
&& (zone_reclaim_mode
& RECLAIM_SLAB
)) {
1950 * shrink_slab does not currently allow us to determine
1951 * how many pages were freed in the zone. So we just
1952 * shake the slab and then go offnode for a single allocation.
1954 * shrink_slab will free memory on all zones and may take
1957 shrink_slab(sc
.nr_scanned
, gfp_mask
, order
);
1960 p
->reclaim_state
= NULL
;
1961 current
->flags
&= ~(PF_MEMALLOC
| PF_SWAPWRITE
);
1963 if (sc
.nr_reclaimed
== 0)
1964 zone
->last_unsuccessful_zone_reclaim
= jiffies
;
1966 return sc
.nr_reclaimed
>= nr_pages
;