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
));
446 /* Double the slab pressure for mapped and swapcache pages */
447 if (page_mapped(page
) || PageSwapCache(page
))
450 if (PageWriteback(page
))
453 referenced
= page_referenced(page
, 1);
454 /* In active use or really unfreeable? Activate it. */
455 if (referenced
&& page_mapping_inuse(page
))
456 goto activate_locked
;
460 * Anonymous process memory has backing store?
461 * Try to allocate it some swap space here.
463 if (PageAnon(page
) && !PageSwapCache(page
)) {
466 if (!add_to_swap(page
, GFP_ATOMIC
))
467 goto activate_locked
;
469 #endif /* CONFIG_SWAP */
471 mapping
= page_mapping(page
);
472 may_enter_fs
= (sc
->gfp_mask
& __GFP_FS
) ||
473 (PageSwapCache(page
) && (sc
->gfp_mask
& __GFP_IO
));
476 * The page is mapped into the page tables of one or more
477 * processes. Try to unmap it here.
479 if (page_mapped(page
) && mapping
) {
481 * No unmapping if we do not swap
486 switch (try_to_unmap(page
, 0)) {
488 goto activate_locked
;
492 ; /* try to free the page below */
496 if (PageDirty(page
)) {
501 if (!sc
->may_writepage
)
504 /* Page is dirty, try to write it out here */
505 switch(pageout(page
, mapping
)) {
509 goto activate_locked
;
511 if (PageWriteback(page
) || PageDirty(page
))
514 * A synchronous write - probably a ramdisk. Go
515 * ahead and try to reclaim the page.
517 if (TestSetPageLocked(page
))
519 if (PageDirty(page
) || PageWriteback(page
))
521 mapping
= page_mapping(page
);
523 ; /* try to free the page below */
528 * If the page has buffers, try to free the buffer mappings
529 * associated with this page. If we succeed we try to free
532 * We do this even if the page is PageDirty().
533 * try_to_release_page() does not perform I/O, but it is
534 * possible for a page to have PageDirty set, but it is actually
535 * clean (all its buffers are clean). This happens if the
536 * buffers were written out directly, with submit_bh(). ext3
537 * will do this, as well as the blockdev mapping.
538 * try_to_release_page() will discover that cleanness and will
539 * drop the buffers and mark the page clean - it can be freed.
541 * Rarely, pages can have buffers and no ->mapping. These are
542 * the pages which were not successfully invalidated in
543 * truncate_complete_page(). We try to drop those buffers here
544 * and if that worked, and the page is no longer mapped into
545 * process address space (page_count == 1) it can be freed.
546 * Otherwise, leave the page on the LRU so it is swappable.
548 if (PagePrivate(page
)) {
549 if (!try_to_release_page(page
, sc
->gfp_mask
))
550 goto activate_locked
;
551 if (!mapping
&& page_count(page
) == 1)
555 if (!remove_mapping(mapping
, page
))
561 if (!pagevec_add(&freed_pvec
, page
))
562 __pagevec_release_nonlru(&freed_pvec
);
571 list_add(&page
->lru
, &ret_pages
);
572 BUG_ON(PageLRU(page
));
574 list_splice(&ret_pages
, page_list
);
575 if (pagevec_count(&freed_pvec
))
576 __pagevec_release_nonlru(&freed_pvec
);
577 mod_page_state(pgactivate
, pgactivate
);
578 sc
->nr_reclaimed
+= reclaimed
;
582 #ifdef CONFIG_MIGRATION
583 static inline void move_to_lru(struct page
*page
)
585 list_del(&page
->lru
);
586 if (PageActive(page
)) {
588 * lru_cache_add_active checks that
589 * the PG_active bit is off.
591 ClearPageActive(page
);
592 lru_cache_add_active(page
);
600 * Add isolated pages on the list back to the LRU.
602 * returns the number of pages put back.
604 int putback_lru_pages(struct list_head
*l
)
610 list_for_each_entry_safe(page
, page2
, l
, lru
) {
618 * swapout a single page
619 * page is locked upon entry, unlocked on exit
621 static int swap_page(struct page
*page
)
623 struct address_space
*mapping
= page_mapping(page
);
625 if (page_mapped(page
) && mapping
)
626 if (try_to_unmap(page
, 0) != SWAP_SUCCESS
)
629 if (PageDirty(page
)) {
630 /* Page is dirty, try to write it out here */
631 switch(pageout(page
, mapping
)) {
640 ; /* try to free the page below */
644 if (PagePrivate(page
)) {
645 if (!try_to_release_page(page
, GFP_KERNEL
) ||
646 (!mapping
&& page_count(page
) == 1))
650 if (remove_mapping(mapping
, page
)) {
664 * Page migration was first developed in the context of the memory hotplug
665 * project. The main authors of the migration code are:
667 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
668 * Hirokazu Takahashi <taka@valinux.co.jp>
669 * Dave Hansen <haveblue@us.ibm.com>
670 * Christoph Lameter <clameter@sgi.com>
674 * Remove references for a page and establish the new page with the correct
675 * basic settings to be able to stop accesses to the page.
677 static int migrate_page_remove_references(struct page
*newpage
,
678 struct page
*page
, int nr_refs
)
680 struct address_space
*mapping
= page_mapping(page
);
681 struct page
**radix_pointer
;
684 * Avoid doing any of the following work if the page count
685 * indicates that the page is in use or truncate has removed
688 if (!mapping
|| page_mapcount(page
) + nr_refs
!= page_count(page
))
692 * Establish swap ptes for anonymous pages or destroy pte
695 * In order to reestablish file backed mappings the fault handlers
696 * will take the radix tree_lock which may then be used to stop
697 * processses from accessing this page until the new page is ready.
699 * A process accessing via a swap pte (an anonymous page) will take a
700 * page_lock on the old page which will block the process until the
701 * migration attempt is complete. At that time the PageSwapCache bit
702 * will be examined. If the page was migrated then the PageSwapCache
703 * bit will be clear and the operation to retrieve the page will be
704 * retried which will find the new page in the radix tree. Then a new
705 * direct mapping may be generated based on the radix tree contents.
707 * If the page was not migrated then the PageSwapCache bit
708 * is still set and the operation may continue.
710 try_to_unmap(page
, 1);
713 * Give up if we were unable to remove all mappings.
715 if (page_mapcount(page
))
718 write_lock_irq(&mapping
->tree_lock
);
720 radix_pointer
= (struct page
**)radix_tree_lookup_slot(
724 if (!page_mapping(page
) || page_count(page
) != nr_refs
||
725 *radix_pointer
!= page
) {
726 write_unlock_irq(&mapping
->tree_lock
);
731 * Now we know that no one else is looking at the page.
733 * Certain minimal information about a page must be available
734 * in order for other subsystems to properly handle the page if they
735 * find it through the radix tree update before we are finished
739 newpage
->index
= page
->index
;
740 newpage
->mapping
= page
->mapping
;
741 if (PageSwapCache(page
)) {
742 SetPageSwapCache(newpage
);
743 set_page_private(newpage
, page_private(page
));
746 *radix_pointer
= newpage
;
748 write_unlock_irq(&mapping
->tree_lock
);
754 * Copy the page to its new location
756 void migrate_page_copy(struct page
*newpage
, struct page
*page
)
758 copy_highpage(newpage
, page
);
761 SetPageError(newpage
);
762 if (PageReferenced(page
))
763 SetPageReferenced(newpage
);
764 if (PageUptodate(page
))
765 SetPageUptodate(newpage
);
766 if (PageActive(page
))
767 SetPageActive(newpage
);
768 if (PageChecked(page
))
769 SetPageChecked(newpage
);
770 if (PageMappedToDisk(page
))
771 SetPageMappedToDisk(newpage
);
773 if (PageDirty(page
)) {
774 clear_page_dirty_for_io(page
);
775 set_page_dirty(newpage
);
778 ClearPageSwapCache(page
);
779 ClearPageActive(page
);
780 ClearPagePrivate(page
);
781 set_page_private(page
, 0);
782 page
->mapping
= NULL
;
785 * If any waiters have accumulated on the new page then
788 if (PageWriteback(newpage
))
789 end_page_writeback(newpage
);
793 * Common logic to directly migrate a single page suitable for
794 * pages that do not use PagePrivate.
796 * Pages are locked upon entry and exit.
798 int migrate_page(struct page
*newpage
, struct page
*page
)
800 BUG_ON(PageWriteback(page
)); /* Writeback must be complete */
802 if (migrate_page_remove_references(newpage
, page
, 2))
805 migrate_page_copy(newpage
, page
);
808 * Remove auxiliary swap entries and replace
809 * them with real ptes.
811 * Note that a real pte entry will allow processes that are not
812 * waiting on the page lock to use the new page via the page tables
813 * before the new page is unlocked.
815 remove_from_swap(newpage
);
822 * Two lists are passed to this function. The first list
823 * contains the pages isolated from the LRU to be migrated.
824 * The second list contains new pages that the pages isolated
825 * can be moved to. If the second list is NULL then all
826 * pages are swapped out.
828 * The function returns after 10 attempts or if no pages
829 * are movable anymore because t has become empty
830 * or no retryable pages exist anymore.
832 * Return: Number of pages not migrated when "to" ran empty.
834 int migrate_pages(struct list_head
*from
, struct list_head
*to
,
835 struct list_head
*moved
, struct list_head
*failed
)
842 int swapwrite
= current
->flags
& PF_SWAPWRITE
;
846 current
->flags
|= PF_SWAPWRITE
;
851 list_for_each_entry_safe(page
, page2
, from
, lru
) {
852 struct page
*newpage
= NULL
;
853 struct address_space
*mapping
;
858 if (page_count(page
) == 1)
859 /* page was freed from under us. So we are done. */
862 if (to
&& list_empty(to
))
866 * Skip locked pages during the first two passes to give the
867 * functions holding the lock time to release the page. Later we
868 * use lock_page() to have a higher chance of acquiring the
875 if (TestSetPageLocked(page
))
879 * Only wait on writeback if we have already done a pass where
880 * we we may have triggered writeouts for lots of pages.
883 wait_on_page_writeback(page
);
885 if (PageWriteback(page
))
890 * Anonymous pages must have swap cache references otherwise
891 * the information contained in the page maps cannot be
894 if (PageAnon(page
) && !PageSwapCache(page
)) {
895 if (!add_to_swap(page
, GFP_KERNEL
)) {
902 rc
= swap_page(page
);
906 newpage
= lru_to_page(to
);
910 * Pages are properly locked and writeback is complete.
911 * Try to migrate the page.
913 mapping
= page_mapping(page
);
918 * Trigger writeout if page is dirty
920 if (PageDirty(page
)) {
921 switch (pageout(page
, mapping
)) {
927 unlock_page(newpage
);
931 ; /* try to migrate the page below */
935 * If we have no buffer or can release the buffer
936 * then do a simple migration.
938 if (!page_has_buffers(page
) ||
939 try_to_release_page(page
, GFP_KERNEL
)) {
940 rc
= migrate_page(newpage
, page
);
945 * On early passes with mapped pages simply
946 * retry. There may be a lock held for some
947 * buffers that may go away. Later
951 unlock_page(newpage
);
953 rc
= swap_page(page
);
958 unlock_page(newpage
);
967 /* Permanent failure */
968 list_move(&page
->lru
, failed
);
972 /* Successful migration. Return page to LRU */
973 move_to_lru(newpage
);
975 list_move(&page
->lru
, moved
);
978 if (retry
&& pass
++ < 10)
982 current
->flags
&= ~PF_SWAPWRITE
;
984 return nr_failed
+ retry
;
988 * Isolate one page from the LRU lists and put it on the
989 * indicated list with elevated refcount.
992 * 0 = page not on LRU list
993 * 1 = page removed from LRU list and added to the specified list.
995 int isolate_lru_page(struct page
*page
)
1000 struct zone
*zone
= page_zone(page
);
1001 spin_lock_irq(&zone
->lru_lock
);
1002 if (TestClearPageLRU(page
)) {
1005 if (PageActive(page
))
1006 del_page_from_active_list(zone
, page
);
1008 del_page_from_inactive_list(zone
, page
);
1010 spin_unlock_irq(&zone
->lru_lock
);
1018 * zone->lru_lock is heavily contended. Some of the functions that
1019 * shrink the lists perform better by taking out a batch of pages
1020 * and working on them outside the LRU lock.
1022 * For pagecache intensive workloads, this function is the hottest
1023 * spot in the kernel (apart from copy_*_user functions).
1025 * Appropriate locks must be held before calling this function.
1027 * @nr_to_scan: The number of pages to look through on the list.
1028 * @src: The LRU list to pull pages off.
1029 * @dst: The temp list to put pages on to.
1030 * @scanned: The number of pages that were scanned.
1032 * returns how many pages were moved onto *@dst.
1034 static int isolate_lru_pages(int nr_to_scan
, struct list_head
*src
,
1035 struct list_head
*dst
, int *scanned
)
1041 while (scan
++ < nr_to_scan
&& !list_empty(src
)) {
1042 page
= lru_to_page(src
);
1043 prefetchw_prev_lru_page(page
, src
, flags
);
1045 if (!TestClearPageLRU(page
))
1047 list_del(&page
->lru
);
1048 if (get_page_testone(page
)) {
1050 * It is being freed elsewhere
1054 list_add(&page
->lru
, src
);
1057 list_add(&page
->lru
, dst
);
1067 * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed
1069 static void shrink_cache(struct zone
*zone
, struct scan_control
*sc
)
1071 LIST_HEAD(page_list
);
1072 struct pagevec pvec
;
1073 int max_scan
= sc
->nr_to_scan
;
1075 pagevec_init(&pvec
, 1);
1078 spin_lock_irq(&zone
->lru_lock
);
1079 while (max_scan
> 0) {
1085 nr_taken
= isolate_lru_pages(sc
->swap_cluster_max
,
1086 &zone
->inactive_list
,
1087 &page_list
, &nr_scan
);
1088 zone
->nr_inactive
-= nr_taken
;
1089 zone
->pages_scanned
+= nr_scan
;
1090 spin_unlock_irq(&zone
->lru_lock
);
1095 max_scan
-= nr_scan
;
1096 nr_freed
= shrink_list(&page_list
, sc
);
1098 local_irq_disable();
1099 if (current_is_kswapd()) {
1100 __mod_page_state_zone(zone
, pgscan_kswapd
, nr_scan
);
1101 __mod_page_state(kswapd_steal
, nr_freed
);
1103 __mod_page_state_zone(zone
, pgscan_direct
, nr_scan
);
1104 __mod_page_state_zone(zone
, pgsteal
, nr_freed
);
1106 spin_lock(&zone
->lru_lock
);
1108 * Put back any unfreeable pages.
1110 while (!list_empty(&page_list
)) {
1111 page
= lru_to_page(&page_list
);
1112 if (TestSetPageLRU(page
))
1114 list_del(&page
->lru
);
1115 if (PageActive(page
))
1116 add_page_to_active_list(zone
, page
);
1118 add_page_to_inactive_list(zone
, page
);
1119 if (!pagevec_add(&pvec
, page
)) {
1120 spin_unlock_irq(&zone
->lru_lock
);
1121 __pagevec_release(&pvec
);
1122 spin_lock_irq(&zone
->lru_lock
);
1126 spin_unlock_irq(&zone
->lru_lock
);
1128 pagevec_release(&pvec
);
1132 * This moves pages from the active list to the inactive list.
1134 * We move them the other way if the page is referenced by one or more
1135 * processes, from rmap.
1137 * If the pages are mostly unmapped, the processing is fast and it is
1138 * appropriate to hold zone->lru_lock across the whole operation. But if
1139 * the pages are mapped, the processing is slow (page_referenced()) so we
1140 * should drop zone->lru_lock around each page. It's impossible to balance
1141 * this, so instead we remove the pages from the LRU while processing them.
1142 * It is safe to rely on PG_active against the non-LRU pages in here because
1143 * nobody will play with that bit on a non-LRU page.
1145 * The downside is that we have to touch page->_count against each page.
1146 * But we had to alter page->flags anyway.
1149 refill_inactive_zone(struct zone
*zone
, struct scan_control
*sc
)
1152 int pgdeactivate
= 0;
1154 int nr_pages
= sc
->nr_to_scan
;
1155 LIST_HEAD(l_hold
); /* The pages which were snipped off */
1156 LIST_HEAD(l_inactive
); /* Pages to go onto the inactive_list */
1157 LIST_HEAD(l_active
); /* Pages to go onto the active_list */
1159 struct pagevec pvec
;
1160 int reclaim_mapped
= 0;
1166 spin_lock_irq(&zone
->lru_lock
);
1167 pgmoved
= isolate_lru_pages(nr_pages
, &zone
->active_list
,
1168 &l_hold
, &pgscanned
);
1169 zone
->pages_scanned
+= pgscanned
;
1170 zone
->nr_active
-= pgmoved
;
1171 spin_unlock_irq(&zone
->lru_lock
);
1174 * `distress' is a measure of how much trouble we're having reclaiming
1175 * pages. 0 -> no problems. 100 -> great trouble.
1177 distress
= 100 >> zone
->prev_priority
;
1180 * The point of this algorithm is to decide when to start reclaiming
1181 * mapped memory instead of just pagecache. Work out how much memory
1184 mapped_ratio
= (sc
->nr_mapped
* 100) / total_memory
;
1187 * Now decide how much we really want to unmap some pages. The mapped
1188 * ratio is downgraded - just because there's a lot of mapped memory
1189 * doesn't necessarily mean that page reclaim isn't succeeding.
1191 * The distress ratio is important - we don't want to start going oom.
1193 * A 100% value of vm_swappiness overrides this algorithm altogether.
1195 swap_tendency
= mapped_ratio
/ 2 + distress
+ vm_swappiness
;
1198 * Now use this metric to decide whether to start moving mapped memory
1199 * onto the inactive list.
1201 if (swap_tendency
>= 100)
1204 while (!list_empty(&l_hold
)) {
1206 page
= lru_to_page(&l_hold
);
1207 list_del(&page
->lru
);
1208 if (page_mapped(page
)) {
1209 if (!reclaim_mapped
||
1210 (total_swap_pages
== 0 && PageAnon(page
)) ||
1211 page_referenced(page
, 0)) {
1212 list_add(&page
->lru
, &l_active
);
1216 list_add(&page
->lru
, &l_inactive
);
1219 pagevec_init(&pvec
, 1);
1221 spin_lock_irq(&zone
->lru_lock
);
1222 while (!list_empty(&l_inactive
)) {
1223 page
= lru_to_page(&l_inactive
);
1224 prefetchw_prev_lru_page(page
, &l_inactive
, flags
);
1225 if (TestSetPageLRU(page
))
1227 if (!TestClearPageActive(page
))
1229 list_move(&page
->lru
, &zone
->inactive_list
);
1231 if (!pagevec_add(&pvec
, page
)) {
1232 zone
->nr_inactive
+= pgmoved
;
1233 spin_unlock_irq(&zone
->lru_lock
);
1234 pgdeactivate
+= pgmoved
;
1236 if (buffer_heads_over_limit
)
1237 pagevec_strip(&pvec
);
1238 __pagevec_release(&pvec
);
1239 spin_lock_irq(&zone
->lru_lock
);
1242 zone
->nr_inactive
+= pgmoved
;
1243 pgdeactivate
+= pgmoved
;
1244 if (buffer_heads_over_limit
) {
1245 spin_unlock_irq(&zone
->lru_lock
);
1246 pagevec_strip(&pvec
);
1247 spin_lock_irq(&zone
->lru_lock
);
1251 while (!list_empty(&l_active
)) {
1252 page
= lru_to_page(&l_active
);
1253 prefetchw_prev_lru_page(page
, &l_active
, flags
);
1254 if (TestSetPageLRU(page
))
1256 BUG_ON(!PageActive(page
));
1257 list_move(&page
->lru
, &zone
->active_list
);
1259 if (!pagevec_add(&pvec
, page
)) {
1260 zone
->nr_active
+= pgmoved
;
1262 spin_unlock_irq(&zone
->lru_lock
);
1263 __pagevec_release(&pvec
);
1264 spin_lock_irq(&zone
->lru_lock
);
1267 zone
->nr_active
+= pgmoved
;
1268 spin_unlock(&zone
->lru_lock
);
1270 __mod_page_state_zone(zone
, pgrefill
, pgscanned
);
1271 __mod_page_state(pgdeactivate
, pgdeactivate
);
1274 pagevec_release(&pvec
);
1278 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1281 shrink_zone(struct zone
*zone
, struct scan_control
*sc
)
1283 unsigned long nr_active
;
1284 unsigned long nr_inactive
;
1286 atomic_inc(&zone
->reclaim_in_progress
);
1289 * Add one to `nr_to_scan' just to make sure that the kernel will
1290 * slowly sift through the active list.
1292 zone
->nr_scan_active
+= (zone
->nr_active
>> sc
->priority
) + 1;
1293 nr_active
= zone
->nr_scan_active
;
1294 if (nr_active
>= sc
->swap_cluster_max
)
1295 zone
->nr_scan_active
= 0;
1299 zone
->nr_scan_inactive
+= (zone
->nr_inactive
>> sc
->priority
) + 1;
1300 nr_inactive
= zone
->nr_scan_inactive
;
1301 if (nr_inactive
>= sc
->swap_cluster_max
)
1302 zone
->nr_scan_inactive
= 0;
1306 while (nr_active
|| nr_inactive
) {
1308 sc
->nr_to_scan
= min(nr_active
,
1309 (unsigned long)sc
->swap_cluster_max
);
1310 nr_active
-= sc
->nr_to_scan
;
1311 refill_inactive_zone(zone
, sc
);
1315 sc
->nr_to_scan
= min(nr_inactive
,
1316 (unsigned long)sc
->swap_cluster_max
);
1317 nr_inactive
-= sc
->nr_to_scan
;
1318 shrink_cache(zone
, sc
);
1322 throttle_vm_writeout();
1324 atomic_dec(&zone
->reclaim_in_progress
);
1328 * This is the direct reclaim path, for page-allocating processes. We only
1329 * try to reclaim pages from zones which will satisfy the caller's allocation
1332 * We reclaim from a zone even if that zone is over pages_high. Because:
1333 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1335 * b) The zones may be over pages_high but they must go *over* pages_high to
1336 * satisfy the `incremental min' zone defense algorithm.
1338 * Returns the number of reclaimed pages.
1340 * If a zone is deemed to be full of pinned pages then just give it a light
1341 * scan then give up on it.
1344 shrink_caches(struct zone
**zones
, struct scan_control
*sc
)
1348 for (i
= 0; zones
[i
] != NULL
; i
++) {
1349 struct zone
*zone
= zones
[i
];
1351 if (!populated_zone(zone
))
1354 if (!cpuset_zone_allowed(zone
, __GFP_HARDWALL
))
1357 zone
->temp_priority
= sc
->priority
;
1358 if (zone
->prev_priority
> sc
->priority
)
1359 zone
->prev_priority
= sc
->priority
;
1361 if (zone
->all_unreclaimable
&& sc
->priority
!= DEF_PRIORITY
)
1362 continue; /* Let kswapd poll it */
1364 shrink_zone(zone
, sc
);
1369 * This is the main entry point to direct page reclaim.
1371 * If a full scan of the inactive list fails to free enough memory then we
1372 * are "out of memory" and something needs to be killed.
1374 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1375 * high - the zone may be full of dirty or under-writeback pages, which this
1376 * caller can't do much about. We kick pdflush and take explicit naps in the
1377 * hope that some of these pages can be written. But if the allocating task
1378 * holds filesystem locks which prevent writeout this might not work, and the
1379 * allocation attempt will fail.
1381 int try_to_free_pages(struct zone
**zones
, gfp_t gfp_mask
)
1385 int total_scanned
= 0, total_reclaimed
= 0;
1386 struct reclaim_state
*reclaim_state
= current
->reclaim_state
;
1387 struct scan_control sc
;
1388 unsigned long lru_pages
= 0;
1391 sc
.gfp_mask
= gfp_mask
;
1392 sc
.may_writepage
= !laptop_mode
;
1395 inc_page_state(allocstall
);
1397 for (i
= 0; zones
[i
] != NULL
; i
++) {
1398 struct zone
*zone
= zones
[i
];
1400 if (!cpuset_zone_allowed(zone
, __GFP_HARDWALL
))
1403 zone
->temp_priority
= DEF_PRIORITY
;
1404 lru_pages
+= zone
->nr_active
+ zone
->nr_inactive
;
1407 for (priority
= DEF_PRIORITY
; priority
>= 0; priority
--) {
1408 sc
.nr_mapped
= read_page_state(nr_mapped
);
1410 sc
.nr_reclaimed
= 0;
1411 sc
.priority
= priority
;
1412 sc
.swap_cluster_max
= SWAP_CLUSTER_MAX
;
1414 disable_swap_token();
1415 shrink_caches(zones
, &sc
);
1416 shrink_slab(sc
.nr_scanned
, gfp_mask
, lru_pages
);
1417 if (reclaim_state
) {
1418 sc
.nr_reclaimed
+= reclaim_state
->reclaimed_slab
;
1419 reclaim_state
->reclaimed_slab
= 0;
1421 total_scanned
+= sc
.nr_scanned
;
1422 total_reclaimed
+= sc
.nr_reclaimed
;
1423 if (total_reclaimed
>= sc
.swap_cluster_max
) {
1429 * Try to write back as many pages as we just scanned. This
1430 * tends to cause slow streaming writers to write data to the
1431 * disk smoothly, at the dirtying rate, which is nice. But
1432 * that's undesirable in laptop mode, where we *want* lumpy
1433 * writeout. So in laptop mode, write out the whole world.
1435 if (total_scanned
> sc
.swap_cluster_max
+ sc
.swap_cluster_max
/2) {
1436 wakeup_pdflush(laptop_mode
? 0 : total_scanned
);
1437 sc
.may_writepage
= 1;
1440 /* Take a nap, wait for some writeback to complete */
1441 if (sc
.nr_scanned
&& priority
< DEF_PRIORITY
- 2)
1442 blk_congestion_wait(WRITE
, HZ
/10);
1445 for (i
= 0; zones
[i
] != 0; i
++) {
1446 struct zone
*zone
= zones
[i
];
1448 if (!cpuset_zone_allowed(zone
, __GFP_HARDWALL
))
1451 zone
->prev_priority
= zone
->temp_priority
;
1457 * For kswapd, balance_pgdat() will work across all this node's zones until
1458 * they are all at pages_high.
1460 * If `nr_pages' is non-zero then it is the number of pages which are to be
1461 * reclaimed, regardless of the zone occupancies. This is a software suspend
1464 * Returns the number of pages which were actually freed.
1466 * There is special handling here for zones which are full of pinned pages.
1467 * This can happen if the pages are all mlocked, or if they are all used by
1468 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1469 * What we do is to detect the case where all pages in the zone have been
1470 * scanned twice and there has been zero successful reclaim. Mark the zone as
1471 * dead and from now on, only perform a short scan. Basically we're polling
1472 * the zone for when the problem goes away.
1474 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1475 * zones which have free_pages > pages_high, but once a zone is found to have
1476 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1477 * of the number of free pages in the lower zones. This interoperates with
1478 * the page allocator fallback scheme to ensure that aging of pages is balanced
1481 static int balance_pgdat(pg_data_t
*pgdat
, int nr_pages
, int order
)
1483 int to_free
= nr_pages
;
1487 int total_scanned
, total_reclaimed
;
1488 struct reclaim_state
*reclaim_state
= current
->reclaim_state
;
1489 struct scan_control sc
;
1493 total_reclaimed
= 0;
1494 sc
.gfp_mask
= GFP_KERNEL
;
1495 sc
.may_writepage
= !laptop_mode
;
1497 sc
.nr_mapped
= read_page_state(nr_mapped
);
1499 inc_page_state(pageoutrun
);
1501 for (i
= 0; i
< pgdat
->nr_zones
; i
++) {
1502 struct zone
*zone
= pgdat
->node_zones
+ i
;
1504 zone
->temp_priority
= DEF_PRIORITY
;
1507 for (priority
= DEF_PRIORITY
; priority
>= 0; priority
--) {
1508 int end_zone
= 0; /* Inclusive. 0 = ZONE_DMA */
1509 unsigned long lru_pages
= 0;
1511 /* The swap token gets in the way of swapout... */
1513 disable_swap_token();
1517 if (nr_pages
== 0) {
1519 * Scan in the highmem->dma direction for the highest
1520 * zone which needs scanning
1522 for (i
= pgdat
->nr_zones
- 1; i
>= 0; i
--) {
1523 struct zone
*zone
= pgdat
->node_zones
+ i
;
1525 if (!populated_zone(zone
))
1528 if (zone
->all_unreclaimable
&&
1529 priority
!= DEF_PRIORITY
)
1532 if (!zone_watermark_ok(zone
, order
,
1533 zone
->pages_high
, 0, 0)) {
1540 end_zone
= pgdat
->nr_zones
- 1;
1543 for (i
= 0; i
<= end_zone
; i
++) {
1544 struct zone
*zone
= pgdat
->node_zones
+ i
;
1546 lru_pages
+= zone
->nr_active
+ zone
->nr_inactive
;
1550 * Now scan the zone in the dma->highmem direction, stopping
1551 * at the last zone which needs scanning.
1553 * We do this because the page allocator works in the opposite
1554 * direction. This prevents the page allocator from allocating
1555 * pages behind kswapd's direction of progress, which would
1556 * cause too much scanning of the lower zones.
1558 for (i
= 0; i
<= end_zone
; i
++) {
1559 struct zone
*zone
= pgdat
->node_zones
+ i
;
1562 if (!populated_zone(zone
))
1565 if (zone
->all_unreclaimable
&& priority
!= DEF_PRIORITY
)
1568 if (nr_pages
== 0) { /* Not software suspend */
1569 if (!zone_watermark_ok(zone
, order
,
1570 zone
->pages_high
, end_zone
, 0))
1573 zone
->temp_priority
= priority
;
1574 if (zone
->prev_priority
> priority
)
1575 zone
->prev_priority
= priority
;
1577 sc
.nr_reclaimed
= 0;
1578 sc
.priority
= priority
;
1579 sc
.swap_cluster_max
= nr_pages
? nr_pages
: SWAP_CLUSTER_MAX
;
1580 atomic_inc(&zone
->reclaim_in_progress
);
1581 shrink_zone(zone
, &sc
);
1582 atomic_dec(&zone
->reclaim_in_progress
);
1583 reclaim_state
->reclaimed_slab
= 0;
1584 nr_slab
= shrink_slab(sc
.nr_scanned
, GFP_KERNEL
,
1586 sc
.nr_reclaimed
+= reclaim_state
->reclaimed_slab
;
1587 total_reclaimed
+= sc
.nr_reclaimed
;
1588 total_scanned
+= sc
.nr_scanned
;
1589 if (zone
->all_unreclaimable
)
1591 if (nr_slab
== 0 && zone
->pages_scanned
>=
1592 (zone
->nr_active
+ zone
->nr_inactive
) * 4)
1593 zone
->all_unreclaimable
= 1;
1595 * If we've done a decent amount of scanning and
1596 * the reclaim ratio is low, start doing writepage
1597 * even in laptop mode
1599 if (total_scanned
> SWAP_CLUSTER_MAX
* 2 &&
1600 total_scanned
> total_reclaimed
+total_reclaimed
/2)
1601 sc
.may_writepage
= 1;
1603 if (nr_pages
&& to_free
> total_reclaimed
)
1604 continue; /* swsusp: need to do more work */
1606 break; /* kswapd: all done */
1608 * OK, kswapd is getting into trouble. Take a nap, then take
1609 * another pass across the zones.
1611 if (total_scanned
&& priority
< DEF_PRIORITY
- 2)
1612 blk_congestion_wait(WRITE
, HZ
/10);
1615 * We do this so kswapd doesn't build up large priorities for
1616 * example when it is freeing in parallel with allocators. It
1617 * matches the direct reclaim path behaviour in terms of impact
1618 * on zone->*_priority.
1620 if ((total_reclaimed
>= SWAP_CLUSTER_MAX
) && (!nr_pages
))
1624 for (i
= 0; i
< pgdat
->nr_zones
; i
++) {
1625 struct zone
*zone
= pgdat
->node_zones
+ i
;
1627 zone
->prev_priority
= zone
->temp_priority
;
1629 if (!all_zones_ok
) {
1634 return total_reclaimed
;
1638 * The background pageout daemon, started as a kernel thread
1639 * from the init process.
1641 * This basically trickles out pages so that we have _some_
1642 * free memory available even if there is no other activity
1643 * that frees anything up. This is needed for things like routing
1644 * etc, where we otherwise might have all activity going on in
1645 * asynchronous contexts that cannot page things out.
1647 * If there are applications that are active memory-allocators
1648 * (most normal use), this basically shouldn't matter.
1650 static int kswapd(void *p
)
1652 unsigned long order
;
1653 pg_data_t
*pgdat
= (pg_data_t
*)p
;
1654 struct task_struct
*tsk
= current
;
1656 struct reclaim_state reclaim_state
= {
1657 .reclaimed_slab
= 0,
1661 daemonize("kswapd%d", pgdat
->node_id
);
1662 cpumask
= node_to_cpumask(pgdat
->node_id
);
1663 if (!cpus_empty(cpumask
))
1664 set_cpus_allowed(tsk
, cpumask
);
1665 current
->reclaim_state
= &reclaim_state
;
1668 * Tell the memory management that we're a "memory allocator",
1669 * and that if we need more memory we should get access to it
1670 * regardless (see "__alloc_pages()"). "kswapd" should
1671 * never get caught in the normal page freeing logic.
1673 * (Kswapd normally doesn't need memory anyway, but sometimes
1674 * you need a small amount of memory in order to be able to
1675 * page out something else, and this flag essentially protects
1676 * us from recursively trying to free more memory as we're
1677 * trying to free the first piece of memory in the first place).
1679 tsk
->flags
|= PF_MEMALLOC
| PF_SWAPWRITE
| PF_KSWAPD
;
1683 unsigned long new_order
;
1687 prepare_to_wait(&pgdat
->kswapd_wait
, &wait
, TASK_INTERRUPTIBLE
);
1688 new_order
= pgdat
->kswapd_max_order
;
1689 pgdat
->kswapd_max_order
= 0;
1690 if (order
< new_order
) {
1692 * Don't sleep if someone wants a larger 'order'
1698 order
= pgdat
->kswapd_max_order
;
1700 finish_wait(&pgdat
->kswapd_wait
, &wait
);
1702 balance_pgdat(pgdat
, 0, order
);
1708 * A zone is low on free memory, so wake its kswapd task to service it.
1710 void wakeup_kswapd(struct zone
*zone
, int order
)
1714 if (!populated_zone(zone
))
1717 pgdat
= zone
->zone_pgdat
;
1718 if (zone_watermark_ok(zone
, order
, zone
->pages_low
, 0, 0))
1720 if (pgdat
->kswapd_max_order
< order
)
1721 pgdat
->kswapd_max_order
= order
;
1722 if (!cpuset_zone_allowed(zone
, __GFP_HARDWALL
))
1724 if (!waitqueue_active(&pgdat
->kswapd_wait
))
1726 wake_up_interruptible(&pgdat
->kswapd_wait
);
1731 * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
1734 int shrink_all_memory(int nr_pages
)
1737 int nr_to_free
= nr_pages
;
1739 struct reclaim_state reclaim_state
= {
1740 .reclaimed_slab
= 0,
1743 current
->reclaim_state
= &reclaim_state
;
1744 for_each_pgdat(pgdat
) {
1746 freed
= balance_pgdat(pgdat
, nr_to_free
, 0);
1748 nr_to_free
-= freed
;
1749 if (nr_to_free
<= 0)
1752 current
->reclaim_state
= NULL
;
1757 #ifdef CONFIG_HOTPLUG_CPU
1758 /* It's optimal to keep kswapds on the same CPUs as their memory, but
1759 not required for correctness. So if the last cpu in a node goes
1760 away, we get changed to run anywhere: as the first one comes back,
1761 restore their cpu bindings. */
1762 static int __devinit
cpu_callback(struct notifier_block
*nfb
,
1763 unsigned long action
,
1769 if (action
== CPU_ONLINE
) {
1770 for_each_pgdat(pgdat
) {
1771 mask
= node_to_cpumask(pgdat
->node_id
);
1772 if (any_online_cpu(mask
) != NR_CPUS
)
1773 /* One of our CPUs online: restore mask */
1774 set_cpus_allowed(pgdat
->kswapd
, mask
);
1779 #endif /* CONFIG_HOTPLUG_CPU */
1781 static int __init
kswapd_init(void)
1785 for_each_pgdat(pgdat
)
1787 = find_task_by_pid(kernel_thread(kswapd
, pgdat
, CLONE_KERNEL
));
1788 total_memory
= nr_free_pagecache_pages();
1789 hotcpu_notifier(cpu_callback
, 0);
1793 module_init(kswapd_init
)
1799 * If non-zero call zone_reclaim when the number of free pages falls below
1802 * In the future we may add flags to the mode. However, the page allocator
1803 * should only have to check that zone_reclaim_mode != 0 before calling
1806 int zone_reclaim_mode __read_mostly
;
1808 #define RECLAIM_OFF 0
1809 #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1810 #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1811 #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1812 #define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
1815 * Mininum time between zone reclaim scans
1817 int zone_reclaim_interval __read_mostly
= 30*HZ
;
1820 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1821 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1824 #define ZONE_RECLAIM_PRIORITY 4
1827 * Try to free up some pages from this zone through reclaim.
1829 int zone_reclaim(struct zone
*zone
, gfp_t gfp_mask
, unsigned int order
)
1832 struct task_struct
*p
= current
;
1833 struct reclaim_state reclaim_state
;
1834 struct scan_control sc
;
1838 if (time_before(jiffies
,
1839 zone
->last_unsuccessful_zone_reclaim
+ zone_reclaim_interval
))
1842 if (!(gfp_mask
& __GFP_WAIT
) ||
1843 zone
->all_unreclaimable
||
1844 atomic_read(&zone
->reclaim_in_progress
) > 0)
1847 node_id
= zone
->zone_pgdat
->node_id
;
1848 mask
= node_to_cpumask(node_id
);
1849 if (!cpus_empty(mask
) && node_id
!= numa_node_id())
1852 sc
.may_writepage
= !!(zone_reclaim_mode
& RECLAIM_WRITE
);
1853 sc
.may_swap
= !!(zone_reclaim_mode
& RECLAIM_SWAP
);
1855 sc
.nr_reclaimed
= 0;
1856 sc
.priority
= ZONE_RECLAIM_PRIORITY
+ 1;
1857 sc
.nr_mapped
= read_page_state(nr_mapped
);
1858 sc
.gfp_mask
= gfp_mask
;
1860 disable_swap_token();
1862 nr_pages
= 1 << order
;
1863 if (nr_pages
> SWAP_CLUSTER_MAX
)
1864 sc
.swap_cluster_max
= nr_pages
;
1866 sc
.swap_cluster_max
= SWAP_CLUSTER_MAX
;
1869 p
->flags
|= PF_MEMALLOC
;
1870 reclaim_state
.reclaimed_slab
= 0;
1871 p
->reclaim_state
= &reclaim_state
;
1874 * Free memory by calling shrink zone with increasing priorities
1875 * until we have enough memory freed.
1879 shrink_zone(zone
, &sc
);
1881 } while (sc
.nr_reclaimed
< nr_pages
&& sc
.priority
> 0);
1883 if (sc
.nr_reclaimed
< nr_pages
&& (zone_reclaim_mode
& RECLAIM_SLAB
)) {
1885 * shrink_slab does not currently allow us to determine
1886 * how many pages were freed in the zone. So we just
1887 * shake the slab and then go offnode for a single allocation.
1889 * shrink_slab will free memory on all zones and may take
1892 shrink_slab(sc
.nr_scanned
, gfp_mask
, order
);
1893 sc
.nr_reclaimed
= 1; /* Avoid getting the off node timeout */
1896 p
->reclaim_state
= NULL
;
1897 current
->flags
&= ~PF_MEMALLOC
;
1899 if (sc
.nr_reclaimed
== 0)
1900 zone
->last_unsuccessful_zone_reclaim
= jiffies
;
1902 return sc
.nr_reclaimed
>= nr_pages
;