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 /* Incremented by the number of inactive pages that were scanned */
56 unsigned long nr_scanned;
58 unsigned long nr_mapped; /* From page_state */
60 /* This context's GFP mask */
65 /* Can pages be swapped as part of reclaim? */
68 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
69 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
70 * In this context, it doesn't matter that we scan the
71 * whole list at once. */
76 * The list of shrinker callbacks used by to apply pressure to
81 struct list_head list;
82 int seeks; /* seeks to recreate an obj */
83 long nr; /* objs pending delete */
86 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
88 #ifdef ARCH_HAS_PREFETCH
89 #define prefetch_prev_lru_page(_page, _base, _field) \
91 if ((_page)->lru.prev != _base) { \
94 prev = lru_to_page(&(_page->lru)); \
95 prefetch(&prev->_field); \
99 #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
102 #ifdef ARCH_HAS_PREFETCHW
103 #define prefetchw_prev_lru_page(_page, _base, _field) \
105 if ((_page)->lru.prev != _base) { \
108 prev = lru_to_page(&(_page->lru)); \
109 prefetchw(&prev->_field); \
113 #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
117 * From 0 .. 100. Higher means more swappy.
119 int vm_swappiness = 60;
120 static long total_memory;
122 static LIST_HEAD(shrinker_list);
123 static DECLARE_RWSEM(shrinker_rwsem);
126 * Add a shrinker callback to be called from the vm
128 struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
130 struct shrinker *shrinker;
132 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
134 shrinker->shrinker = theshrinker;
135 shrinker->seeks = seeks;
137 down_write(&shrinker_rwsem);
138 list_add_tail(&shrinker->list, &shrinker_list);
139 up_write(&shrinker_rwsem);
143 EXPORT_SYMBOL(set_shrinker);
148 void remove_shrinker(struct shrinker *shrinker)
150 down_write(&shrinker_rwsem);
151 list_del(&shrinker->list);
152 up_write(&shrinker_rwsem);
155 EXPORT_SYMBOL(remove_shrinker);
157 #define SHRINK_BATCH 128
159 * Call the shrink functions to age shrinkable caches
161 * Here we assume it costs one seek to replace a lru page and that it also
162 * takes a seek to recreate a cache object. With this in mind we age equal
163 * percentages of the lru and ageable caches. This should balance the seeks
164 * generated by these structures.
166 * If the vm encounted mapped pages on the LRU it increase the pressure on
167 * slab to avoid swapping.
169 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
171 * `lru_pages' represents the number of on-LRU pages in all the zones which
172 * are eligible for the caller's allocation attempt. It is used for balancing
173 * slab reclaim versus page reclaim.
175 * Returns the number of slab objects which we shrunk.
177 unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
178 unsigned long lru_pages)
180 struct shrinker *shrinker;
181 unsigned long ret = 0;
184 scanned = SWAP_CLUSTER_MAX;
186 if (!down_read_trylock(&shrinker_rwsem))
187 return 1; /* Assume we'll be able to shrink next time */
189 list_for_each_entry(shrinker, &shrinker_list, list) {
190 unsigned long long delta;
191 unsigned long total_scan;
192 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
194 delta = (4 * scanned) / shrinker->seeks;
196 do_div(delta, lru_pages + 1);
197 shrinker->nr += delta;
198 if (shrinker->nr < 0) {
199 printk(KERN_ERR "%s: nr=%ld\n",
200 __FUNCTION__, shrinker->nr);
201 shrinker->nr = max_pass;
205 * Avoid risking looping forever due to too large nr value:
206 * never try to free more than twice the estimate number of
209 if (shrinker->nr > max_pass * 2)
210 shrinker->nr = max_pass * 2;
212 total_scan = shrinker->nr;
215 while (total_scan >= SHRINK_BATCH) {
216 long this_scan = SHRINK_BATCH;
220 nr_before = (*shrinker->shrinker)(0, gfp_mask);
221 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
222 if (shrink_ret == -1)
224 if (shrink_ret < nr_before)
225 ret += nr_before - shrink_ret;
226 mod_page_state(slabs_scanned, this_scan);
227 total_scan -= this_scan;
232 shrinker->nr += total_scan;
234 up_read(&shrinker_rwsem);
238 /* Called without lock on whether page is mapped, so answer is unstable */
239 static inline int page_mapping_inuse(struct page *page)
241 struct address_space *mapping;
243 /* Page is in somebody's page tables. */
244 if (page_mapped(page))
247 /* Be more reluctant to reclaim swapcache than pagecache */
248 if (PageSwapCache(page))
251 mapping = page_mapping(page);
255 /* File is mmap'd by somebody? */
256 return mapping_mapped(mapping);
259 static inline int is_page_cache_freeable(struct page *page)
261 return page_count(page) - !!PagePrivate(page) == 2;
264 static int may_write_to_queue(struct backing_dev_info *bdi)
266 if (current->flags & PF_SWAPWRITE)
268 if (!bdi_write_congested(bdi))
270 if (bdi == current->backing_dev_info)
276 * We detected a synchronous write error writing a page out. Probably
277 * -ENOSPC. We need to propagate that into the address_space for a subsequent
278 * fsync(), msync() or close().
280 * The tricky part is that after writepage we cannot touch the mapping: nothing
281 * prevents it from being freed up. But we have a ref on the page and once
282 * that page is locked, the mapping is pinned.
284 * We're allowed to run sleeping lock_page() here because we know the caller has
287 static void handle_write_error(struct address_space *mapping,
288 struct page *page, int error)
291 if (page_mapping(page) == mapping) {
292 if (error == -ENOSPC)
293 set_bit(AS_ENOSPC, &mapping->flags);
295 set_bit(AS_EIO, &mapping->flags);
301 * pageout is called by shrink_page_list() for each dirty page.
302 * Calls ->writepage().
304 static pageout_t pageout(struct page *page, struct address_space *mapping)
307 * If the page is dirty, only perform writeback if that write
308 * will be non-blocking. To prevent this allocation from being
309 * stalled by pagecache activity. But note that there may be
310 * stalls if we need to run get_block(). We could test
311 * PagePrivate for that.
313 * If this process is currently in generic_file_write() against
314 * this page's queue, we can perform writeback even if that
317 * If the page is swapcache, write it back even if that would
318 * block, for some throttling. This happens by accident, because
319 * swap_backing_dev_info is bust: it doesn't reflect the
320 * congestion state of the swapdevs. Easy to fix, if needed.
321 * See swapfile.c:page_queue_congested().
323 if (!is_page_cache_freeable(page))
327 * Some data journaling orphaned pages can have
328 * page->mapping == NULL while being dirty with clean buffers.
330 if (PagePrivate(page)) {
331 if (try_to_free_buffers(page)) {
332 ClearPageDirty(page);
333 printk("%s: orphaned page\n", __FUNCTION__);
339 if (mapping->a_ops->writepage == NULL)
340 return PAGE_ACTIVATE;
341 if (!may_write_to_queue(mapping->backing_dev_info))
344 if (clear_page_dirty_for_io(page)) {
346 struct writeback_control wbc = {
347 .sync_mode = WB_SYNC_NONE,
348 .nr_to_write = SWAP_CLUSTER_MAX,
353 SetPageReclaim(page);
354 res = mapping->a_ops->writepage(page, &wbc);
356 handle_write_error(mapping, page, res);
357 if (res == AOP_WRITEPAGE_ACTIVATE) {
358 ClearPageReclaim(page);
359 return PAGE_ACTIVATE;
361 if (!PageWriteback(page)) {
362 /* synchronous write or broken a_ops? */
363 ClearPageReclaim(page);
372 static int remove_mapping(struct address_space *mapping, struct page *page)
375 return 0; /* truncate got there first */
377 write_lock_irq(&mapping->tree_lock);
380 * The non-racy check for busy page. It is critical to check
381 * PageDirty _after_ making sure that the page is freeable and
382 * not in use by anybody. (pagecache + us == 2)
384 if (unlikely(page_count(page) != 2))
387 if (unlikely(PageDirty(page)))
390 if (PageSwapCache(page)) {
391 swp_entry_t swap = { .val = page_private(page) };
392 __delete_from_swap_cache(page);
393 write_unlock_irq(&mapping->tree_lock);
395 __put_page(page); /* The pagecache ref */
399 __remove_from_page_cache(page);
400 write_unlock_irq(&mapping->tree_lock);
405 write_unlock_irq(&mapping->tree_lock);
410 * shrink_page_list() returns the number of reclaimed pages
412 static unsigned long shrink_page_list(struct list_head *page_list,
413 struct scan_control *sc)
415 LIST_HEAD(ret_pages);
416 struct pagevec freed_pvec;
418 unsigned long nr_reclaimed = 0;
422 pagevec_init(&freed_pvec, 1);
423 while (!list_empty(page_list)) {
424 struct address_space *mapping;
431 page = lru_to_page(page_list);
432 list_del(&page->lru);
434 if (TestSetPageLocked(page))
437 BUG_ON(PageActive(page));
441 if (!sc->may_swap && page_mapped(page))
444 /* Double the slab pressure for mapped and swapcache pages */
445 if (page_mapped(page) || PageSwapCache(page))
448 if (PageWriteback(page))
451 referenced = page_referenced(page, 1);
452 /* In active use or really unfreeable? Activate it. */
453 if (referenced && page_mapping_inuse(page))
454 goto activate_locked;
458 * Anonymous process memory has backing store?
459 * Try to allocate it some swap space here.
461 if (PageAnon(page) && !PageSwapCache(page)) {
464 if (!add_to_swap(page, GFP_ATOMIC))
465 goto activate_locked;
467 #endif /* CONFIG_SWAP */
469 mapping = page_mapping(page);
470 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
471 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
474 * The page is mapped into the page tables of one or more
475 * processes. Try to unmap it here.
477 if (page_mapped(page) && mapping) {
479 * No unmapping if we do not swap
484 switch (try_to_unmap(page, 0)) {
486 goto activate_locked;
490 ; /* try to free the page below */
494 if (PageDirty(page)) {
499 if (!sc->may_writepage)
502 /* Page is dirty, try to write it out here */
503 switch(pageout(page, mapping)) {
507 goto activate_locked;
509 if (PageWriteback(page) || PageDirty(page))
512 * A synchronous write - probably a ramdisk. Go
513 * ahead and try to reclaim the page.
515 if (TestSetPageLocked(page))
517 if (PageDirty(page) || PageWriteback(page))
519 mapping = page_mapping(page);
521 ; /* try to free the page below */
526 * If the page has buffers, try to free the buffer mappings
527 * associated with this page. If we succeed we try to free
530 * We do this even if the page is PageDirty().
531 * try_to_release_page() does not perform I/O, but it is
532 * possible for a page to have PageDirty set, but it is actually
533 * clean (all its buffers are clean). This happens if the
534 * buffers were written out directly, with submit_bh(). ext3
535 * will do this, as well as the blockdev mapping.
536 * try_to_release_page() will discover that cleanness and will
537 * drop the buffers and mark the page clean - it can be freed.
539 * Rarely, pages can have buffers and no ->mapping. These are
540 * the pages which were not successfully invalidated in
541 * truncate_complete_page(). We try to drop those buffers here
542 * and if that worked, and the page is no longer mapped into
543 * process address space (page_count == 1) it can be freed.
544 * Otherwise, leave the page on the LRU so it is swappable.
546 if (PagePrivate(page)) {
547 if (!try_to_release_page(page, sc->gfp_mask))
548 goto activate_locked;
549 if (!mapping && page_count(page) == 1)
553 if (!remove_mapping(mapping, page))
559 if (!pagevec_add(&freed_pvec, page))
560 __pagevec_release_nonlru(&freed_pvec);
569 list_add(&page->lru, &ret_pages);
570 BUG_ON(PageLRU(page));
572 list_splice(&ret_pages, page_list);
573 if (pagevec_count(&freed_pvec))
574 __pagevec_release_nonlru(&freed_pvec);
575 mod_page_state(pgactivate, pgactivate);
579 #ifdef CONFIG_MIGRATION
580 static inline void move_to_lru(struct page *page)
582 list_del(&page->lru);
583 if (PageActive(page)) {
585 * lru_cache_add_active checks that
586 * the PG_active bit is off.
588 ClearPageActive(page);
589 lru_cache_add_active(page);
597 * Add isolated pages on the list back to the LRU.
599 * returns the number of pages put back.
601 unsigned long putback_lru_pages(struct list_head *l)
605 unsigned long count = 0;
607 list_for_each_entry_safe(page, page2, l, lru) {
615 * Non migratable page
617 int fail_migrate_page(struct page *newpage, struct page *page)
621 EXPORT_SYMBOL(fail_migrate_page);
624 * swapout a single page
625 * page is locked upon entry, unlocked on exit
627 static int swap_page(struct page *page)
629 struct address_space *mapping = page_mapping(page);
631 if (page_mapped(page) && mapping)
632 if (try_to_unmap(page, 1) != SWAP_SUCCESS)
635 if (PageDirty(page)) {
636 /* Page is dirty, try to write it out here */
637 switch(pageout(page, mapping)) {
646 ; /* try to free the page below */
650 if (PagePrivate(page)) {
651 if (!try_to_release_page(page, GFP_KERNEL) ||
652 (!mapping && page_count(page) == 1))
656 if (remove_mapping(mapping, page)) {
668 EXPORT_SYMBOL(swap_page);
671 * Page migration was first developed in the context of the memory hotplug
672 * project. The main authors of the migration code are:
674 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
675 * Hirokazu Takahashi <taka@valinux.co.jp>
676 * Dave Hansen <haveblue@us.ibm.com>
677 * Christoph Lameter <clameter@sgi.com>
681 * Remove references for a page and establish the new page with the correct
682 * basic settings to be able to stop accesses to the page.
684 int migrate_page_remove_references(struct page *newpage,
685 struct page *page, int nr_refs)
687 struct address_space *mapping = page_mapping(page);
688 struct page **radix_pointer;
691 * Avoid doing any of the following work if the page count
692 * indicates that the page is in use or truncate has removed
695 if (!mapping || page_mapcount(page) + nr_refs != page_count(page))
699 * Establish swap ptes for anonymous pages or destroy pte
702 * In order to reestablish file backed mappings the fault handlers
703 * will take the radix tree_lock which may then be used to stop
704 * processses from accessing this page until the new page is ready.
706 * A process accessing via a swap pte (an anonymous page) will take a
707 * page_lock on the old page which will block the process until the
708 * migration attempt is complete. At that time the PageSwapCache bit
709 * will be examined. If the page was migrated then the PageSwapCache
710 * bit will be clear and the operation to retrieve the page will be
711 * retried which will find the new page in the radix tree. Then a new
712 * direct mapping may be generated based on the radix tree contents.
714 * If the page was not migrated then the PageSwapCache bit
715 * is still set and the operation may continue.
717 if (try_to_unmap(page, 1) == SWAP_FAIL)
718 /* A vma has VM_LOCKED set -> Permanent failure */
722 * Give up if we were unable to remove all mappings.
724 if (page_mapcount(page))
727 write_lock_irq(&mapping->tree_lock);
729 radix_pointer = (struct page **)radix_tree_lookup_slot(
733 if (!page_mapping(page) || page_count(page) != nr_refs ||
734 *radix_pointer != page) {
735 write_unlock_irq(&mapping->tree_lock);
740 * Now we know that no one else is looking at the page.
742 * Certain minimal information about a page must be available
743 * in order for other subsystems to properly handle the page if they
744 * find it through the radix tree update before we are finished
748 newpage->index = page->index;
749 newpage->mapping = page->mapping;
750 if (PageSwapCache(page)) {
751 SetPageSwapCache(newpage);
752 set_page_private(newpage, page_private(page));
755 *radix_pointer = newpage;
757 write_unlock_irq(&mapping->tree_lock);
761 EXPORT_SYMBOL(migrate_page_remove_references);
764 * Copy the page to its new location
766 void migrate_page_copy(struct page *newpage, struct page *page)
768 copy_highpage(newpage, page);
771 SetPageError(newpage);
772 if (PageReferenced(page))
773 SetPageReferenced(newpage);
774 if (PageUptodate(page))
775 SetPageUptodate(newpage);
776 if (PageActive(page))
777 SetPageActive(newpage);
778 if (PageChecked(page))
779 SetPageChecked(newpage);
780 if (PageMappedToDisk(page))
781 SetPageMappedToDisk(newpage);
783 if (PageDirty(page)) {
784 clear_page_dirty_for_io(page);
785 set_page_dirty(newpage);
788 ClearPageSwapCache(page);
789 ClearPageActive(page);
790 ClearPagePrivate(page);
791 set_page_private(page, 0);
792 page->mapping = NULL;
795 * If any waiters have accumulated on the new page then
798 if (PageWriteback(newpage))
799 end_page_writeback(newpage);
801 EXPORT_SYMBOL(migrate_page_copy);
804 * Common logic to directly migrate a single page suitable for
805 * pages that do not use PagePrivate.
807 * Pages are locked upon entry and exit.
809 int migrate_page(struct page *newpage, struct page *page)
813 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
815 rc = migrate_page_remove_references(newpage, page, 2);
820 migrate_page_copy(newpage, page);
823 * Remove auxiliary swap entries and replace
824 * them with real ptes.
826 * Note that a real pte entry will allow processes that are not
827 * waiting on the page lock to use the new page via the page tables
828 * before the new page is unlocked.
830 remove_from_swap(newpage);
833 EXPORT_SYMBOL(migrate_page);
838 * Two lists are passed to this function. The first list
839 * contains the pages isolated from the LRU to be migrated.
840 * The second list contains new pages that the pages isolated
841 * can be moved to. If the second list is NULL then all
842 * pages are swapped out.
844 * The function returns after 10 attempts or if no pages
845 * are movable anymore because to has become empty
846 * or no retryable pages exist anymore.
848 * Return: Number of pages not migrated when "to" ran empty.
850 unsigned long migrate_pages(struct list_head *from, struct list_head *to,
851 struct list_head *moved, struct list_head *failed)
854 unsigned long nr_failed = 0;
858 int swapwrite = current->flags & PF_SWAPWRITE;
862 current->flags |= PF_SWAPWRITE;
867 list_for_each_entry_safe(page, page2, from, lru) {
868 struct page *newpage = NULL;
869 struct address_space *mapping;
874 if (page_count(page) == 1)
875 /* page was freed from under us. So we are done. */
878 if (to && list_empty(to))
882 * Skip locked pages during the first two passes to give the
883 * functions holding the lock time to release the page. Later we
884 * use lock_page() to have a higher chance of acquiring the
891 if (TestSetPageLocked(page))
895 * Only wait on writeback if we have already done a pass where
896 * we we may have triggered writeouts for lots of pages.
899 wait_on_page_writeback(page);
901 if (PageWriteback(page))
906 * Anonymous pages must have swap cache references otherwise
907 * the information contained in the page maps cannot be
910 if (PageAnon(page) && !PageSwapCache(page)) {
911 if (!add_to_swap(page, GFP_KERNEL)) {
918 rc = swap_page(page);
922 newpage = lru_to_page(to);
926 * Pages are properly locked and writeback is complete.
927 * Try to migrate the page.
929 mapping = page_mapping(page);
933 if (mapping->a_ops->migratepage) {
935 * Most pages have a mapping and most filesystems
936 * should provide a migration function. Anonymous
937 * pages are part of swap space which also has its
938 * own migration function. This is the most common
939 * path for page migration.
941 rc = mapping->a_ops->migratepage(newpage, page);
946 * Default handling if a filesystem does not provide
947 * a migration function. We can only migrate clean
948 * pages so try to write out any dirty pages first.
950 if (PageDirty(page)) {
951 switch (pageout(page, mapping)) {
957 unlock_page(newpage);
961 ; /* try to migrate the page below */
966 * Buffers are managed in a filesystem specific way.
967 * We must have no buffers or drop them.
969 if (!page_has_buffers(page) ||
970 try_to_release_page(page, GFP_KERNEL)) {
971 rc = migrate_page(newpage, page);
976 * On early passes with mapped pages simply
977 * retry. There may be a lock held for some
978 * buffers that may go away. Later
983 * Persistently unable to drop buffers..... As a
984 * measure of last resort we fall back to
987 unlock_page(newpage);
989 rc = swap_page(page);
994 unlock_page(newpage);
1000 if (rc == -EAGAIN) {
1003 /* Permanent failure */
1004 list_move(&page->lru, failed);
1008 /* Successful migration. Return page to LRU */
1009 move_to_lru(newpage);
1011 list_move(&page->lru, moved);
1014 if (retry && pass++ < 10)
1018 current->flags &= ~PF_SWAPWRITE;
1020 return nr_failed + retry;
1024 * Isolate one page from the LRU lists and put it on the
1025 * indicated list with elevated refcount.
1028 * 0 = page not on LRU list
1029 * 1 = page removed from LRU list and added to the specified list.
1031 int isolate_lru_page(struct page *page)
1035 if (PageLRU(page)) {
1036 struct zone *zone = page_zone(page);
1037 spin_lock_irq(&zone->lru_lock);
1038 if (PageLRU(page)) {
1042 if (PageActive(page))
1043 del_page_from_active_list(zone, page);
1045 del_page_from_inactive_list(zone, page);
1047 spin_unlock_irq(&zone->lru_lock);
1055 * zone->lru_lock is heavily contended. Some of the functions that
1056 * shrink the lists perform better by taking out a batch of pages
1057 * and working on them outside the LRU lock.
1059 * For pagecache intensive workloads, this function is the hottest
1060 * spot in the kernel (apart from copy_*_user functions).
1062 * Appropriate locks must be held before calling this function.
1064 * @nr_to_scan: The number of pages to look through on the list.
1065 * @src: The LRU list to pull pages off.
1066 * @dst: The temp list to put pages on to.
1067 * @scanned: The number of pages that were scanned.
1069 * returns how many pages were moved onto *@dst.
1071 static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
1072 struct list_head *src, struct list_head *dst,
1073 unsigned long *scanned)
1075 unsigned long nr_taken = 0;
1077 unsigned long scan = 0;
1079 while (scan++ < nr_to_scan && !list_empty(src)) {
1080 struct list_head *target;
1081 page = lru_to_page(src);
1082 prefetchw_prev_lru_page(page, src, flags);
1084 BUG_ON(!PageLRU(page));
1086 list_del(&page->lru);
1088 if (likely(get_page_unless_zero(page))) {
1090 * Be careful not to clear PageLRU until after we're
1091 * sure the page is not being freed elsewhere -- the
1092 * page release code relies on it.
1097 } /* else it is being freed elsewhere */
1099 list_add(&page->lru, target);
1107 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
1108 * of reclaimed pages
1110 static unsigned long shrink_inactive_list(unsigned long max_scan,
1111 struct zone *zone, struct scan_control *sc)
1113 LIST_HEAD(page_list);
1114 struct pagevec pvec;
1115 unsigned long nr_scanned = 0;
1116 unsigned long nr_reclaimed = 0;
1118 pagevec_init(&pvec, 1);
1121 spin_lock_irq(&zone->lru_lock);
1124 unsigned long nr_taken;
1125 unsigned long nr_scan;
1126 unsigned long nr_freed;
1128 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
1129 &zone->inactive_list,
1130 &page_list, &nr_scan);
1131 zone->nr_inactive -= nr_taken;
1132 zone->pages_scanned += nr_scan;
1133 spin_unlock_irq(&zone->lru_lock);
1138 nr_scanned += nr_scan;
1139 nr_freed = shrink_page_list(&page_list, sc);
1140 nr_reclaimed += nr_freed;
1141 local_irq_disable();
1142 if (current_is_kswapd()) {
1143 __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
1144 __mod_page_state(kswapd_steal, nr_freed);
1146 __mod_page_state_zone(zone, pgscan_direct, nr_scan);
1147 __mod_page_state_zone(zone, pgsteal, nr_freed);
1149 spin_lock(&zone->lru_lock);
1151 * Put back any unfreeable pages.
1153 while (!list_empty(&page_list)) {
1154 page = lru_to_page(&page_list);
1155 BUG_ON(PageLRU(page));
1157 list_del(&page->lru);
1158 if (PageActive(page))
1159 add_page_to_active_list(zone, page);
1161 add_page_to_inactive_list(zone, page);
1162 if (!pagevec_add(&pvec, page)) {
1163 spin_unlock_irq(&zone->lru_lock);
1164 __pagevec_release(&pvec);
1165 spin_lock_irq(&zone->lru_lock);
1168 } while (nr_scanned < max_scan);
1169 spin_unlock_irq(&zone->lru_lock);
1171 pagevec_release(&pvec);
1172 return nr_reclaimed;
1176 * This moves pages from the active list to the inactive list.
1178 * We move them the other way if the page is referenced by one or more
1179 * processes, from rmap.
1181 * If the pages are mostly unmapped, the processing is fast and it is
1182 * appropriate to hold zone->lru_lock across the whole operation. But if
1183 * the pages are mapped, the processing is slow (page_referenced()) so we
1184 * should drop zone->lru_lock around each page. It's impossible to balance
1185 * this, so instead we remove the pages from the LRU while processing them.
1186 * It is safe to rely on PG_active against the non-LRU pages in here because
1187 * nobody will play with that bit on a non-LRU page.
1189 * The downside is that we have to touch page->_count against each page.
1190 * But we had to alter page->flags anyway.
1192 static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
1193 struct scan_control *sc)
1195 unsigned long pgmoved;
1196 int pgdeactivate = 0;
1197 unsigned long pgscanned;
1198 LIST_HEAD(l_hold); /* The pages which were snipped off */
1199 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
1200 LIST_HEAD(l_active); /* Pages to go onto the active_list */
1202 struct pagevec pvec;
1203 int reclaim_mapped = 0;
1205 if (unlikely(sc->may_swap)) {
1211 * `distress' is a measure of how much trouble we're having
1212 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
1214 distress = 100 >> zone->prev_priority;
1217 * The point of this algorithm is to decide when to start
1218 * reclaiming mapped memory instead of just pagecache. Work out
1222 mapped_ratio = (sc->nr_mapped * 100) / total_memory;
1225 * Now decide how much we really want to unmap some pages. The
1226 * mapped ratio is downgraded - just because there's a lot of
1227 * mapped memory doesn't necessarily mean that page reclaim
1230 * The distress ratio is important - we don't want to start
1233 * A 100% value of vm_swappiness overrides this algorithm
1236 swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
1239 * Now use this metric to decide whether to start moving mapped
1240 * memory onto the inactive list.
1242 if (swap_tendency >= 100)
1247 spin_lock_irq(&zone->lru_lock);
1248 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
1249 &l_hold, &pgscanned);
1250 zone->pages_scanned += pgscanned;
1251 zone->nr_active -= pgmoved;
1252 spin_unlock_irq(&zone->lru_lock);
1254 while (!list_empty(&l_hold)) {
1256 page = lru_to_page(&l_hold);
1257 list_del(&page->lru);
1258 if (page_mapped(page)) {
1259 if (!reclaim_mapped ||
1260 (total_swap_pages == 0 && PageAnon(page)) ||
1261 page_referenced(page, 0)) {
1262 list_add(&page->lru, &l_active);
1266 list_add(&page->lru, &l_inactive);
1269 pagevec_init(&pvec, 1);
1271 spin_lock_irq(&zone->lru_lock);
1272 while (!list_empty(&l_inactive)) {
1273 page = lru_to_page(&l_inactive);
1274 prefetchw_prev_lru_page(page, &l_inactive, flags);
1275 BUG_ON(PageLRU(page));
1277 BUG_ON(!PageActive(page));
1278 ClearPageActive(page);
1280 list_move(&page->lru, &zone->inactive_list);
1282 if (!pagevec_add(&pvec, page)) {
1283 zone->nr_inactive += pgmoved;
1284 spin_unlock_irq(&zone->lru_lock);
1285 pgdeactivate += pgmoved;
1287 if (buffer_heads_over_limit)
1288 pagevec_strip(&pvec);
1289 __pagevec_release(&pvec);
1290 spin_lock_irq(&zone->lru_lock);
1293 zone->nr_inactive += pgmoved;
1294 pgdeactivate += pgmoved;
1295 if (buffer_heads_over_limit) {
1296 spin_unlock_irq(&zone->lru_lock);
1297 pagevec_strip(&pvec);
1298 spin_lock_irq(&zone->lru_lock);
1302 while (!list_empty(&l_active)) {
1303 page = lru_to_page(&l_active);
1304 prefetchw_prev_lru_page(page, &l_active, flags);
1305 BUG_ON(PageLRU(page));
1307 BUG_ON(!PageActive(page));
1308 list_move(&page->lru, &zone->active_list);
1310 if (!pagevec_add(&pvec, page)) {
1311 zone->nr_active += pgmoved;
1313 spin_unlock_irq(&zone->lru_lock);
1314 __pagevec_release(&pvec);
1315 spin_lock_irq(&zone->lru_lock);
1318 zone->nr_active += pgmoved;
1319 spin_unlock(&zone->lru_lock);
1321 __mod_page_state_zone(zone, pgrefill, pgscanned);
1322 __mod_page_state(pgdeactivate, pgdeactivate);
1325 pagevec_release(&pvec);
1329 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1331 static unsigned long shrink_zone(int priority, struct zone *zone,
1332 struct scan_control *sc)
1334 unsigned long nr_active;
1335 unsigned long nr_inactive;
1336 unsigned long nr_to_scan;
1337 unsigned long nr_reclaimed = 0;
1339 atomic_inc(&zone->reclaim_in_progress);
1342 * Add one to `nr_to_scan' just to make sure that the kernel will
1343 * slowly sift through the active list.
1345 zone->nr_scan_active += (zone->nr_active >> priority) + 1;
1346 nr_active = zone->nr_scan_active;
1347 if (nr_active >= sc->swap_cluster_max)
1348 zone->nr_scan_active = 0;
1352 zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
1353 nr_inactive = zone->nr_scan_inactive;
1354 if (nr_inactive >= sc->swap_cluster_max)
1355 zone->nr_scan_inactive = 0;
1359 while (nr_active || nr_inactive) {
1361 nr_to_scan = min(nr_active,
1362 (unsigned long)sc->swap_cluster_max);
1363 nr_active -= nr_to_scan;
1364 shrink_active_list(nr_to_scan, zone, sc);
1368 nr_to_scan = min(nr_inactive,
1369 (unsigned long)sc->swap_cluster_max);
1370 nr_inactive -= nr_to_scan;
1371 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
1376 throttle_vm_writeout();
1378 atomic_dec(&zone->reclaim_in_progress);
1379 return nr_reclaimed;
1383 * This is the direct reclaim path, for page-allocating processes. We only
1384 * try to reclaim pages from zones which will satisfy the caller's allocation
1387 * We reclaim from a zone even if that zone is over pages_high. Because:
1388 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1390 * b) The zones may be over pages_high but they must go *over* pages_high to
1391 * satisfy the `incremental min' zone defense algorithm.
1393 * Returns the number of reclaimed pages.
1395 * If a zone is deemed to be full of pinned pages then just give it a light
1396 * scan then give up on it.
1398 static unsigned long shrink_zones(int priority, struct zone **zones,
1399 struct scan_control *sc)
1401 unsigned long nr_reclaimed = 0;
1404 for (i = 0; zones[i] != NULL; i++) {
1405 struct zone *zone = zones[i];
1407 if (!populated_zone(zone))
1410 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1413 zone->temp_priority = priority;
1414 if (zone->prev_priority > priority)
1415 zone->prev_priority = priority;
1417 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1418 continue; /* Let kswapd poll it */
1420 nr_reclaimed += shrink_zone(priority, zone, sc);
1422 return nr_reclaimed;
1426 * This is the main entry point to direct page reclaim.
1428 * If a full scan of the inactive list fails to free enough memory then we
1429 * are "out of memory" and something needs to be killed.
1431 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1432 * high - the zone may be full of dirty or under-writeback pages, which this
1433 * caller can't do much about. We kick pdflush and take explicit naps in the
1434 * hope that some of these pages can be written. But if the allocating task
1435 * holds filesystem locks which prevent writeout this might not work, and the
1436 * allocation attempt will fail.
1438 unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
1442 unsigned long total_scanned = 0;
1443 unsigned long nr_reclaimed = 0;
1444 struct reclaim_state *reclaim_state = current->reclaim_state;
1445 unsigned long lru_pages = 0;
1447 struct scan_control sc = {
1448 .gfp_mask = gfp_mask,
1449 .may_writepage = !laptop_mode,
1450 .swap_cluster_max = SWAP_CLUSTER_MAX,
1454 inc_page_state(allocstall);
1456 for (i = 0; zones[i] != NULL; i++) {
1457 struct zone *zone = zones[i];
1459 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1462 zone->temp_priority = DEF_PRIORITY;
1463 lru_pages += zone->nr_active + zone->nr_inactive;
1466 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1467 sc.nr_mapped = read_page_state(nr_mapped);
1470 disable_swap_token();
1471 nr_reclaimed += shrink_zones(priority, zones, &sc);
1472 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
1473 if (reclaim_state) {
1474 nr_reclaimed += reclaim_state->reclaimed_slab;
1475 reclaim_state->reclaimed_slab = 0;
1477 total_scanned += sc.nr_scanned;
1478 if (nr_reclaimed >= sc.swap_cluster_max) {
1484 * Try to write back as many pages as we just scanned. This
1485 * tends to cause slow streaming writers to write data to the
1486 * disk smoothly, at the dirtying rate, which is nice. But
1487 * that's undesirable in laptop mode, where we *want* lumpy
1488 * writeout. So in laptop mode, write out the whole world.
1490 if (total_scanned > sc.swap_cluster_max +
1491 sc.swap_cluster_max / 2) {
1492 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
1493 sc.may_writepage = 1;
1496 /* Take a nap, wait for some writeback to complete */
1497 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1498 blk_congestion_wait(WRITE, HZ/10);
1501 for (i = 0; zones[i] != 0; i++) {
1502 struct zone *zone = zones[i];
1504 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1507 zone->prev_priority = zone->temp_priority;
1513 * For kswapd, balance_pgdat() will work across all this node's zones until
1514 * they are all at pages_high.
1516 * If `nr_pages' is non-zero then it is the number of pages which are to be
1517 * reclaimed, regardless of the zone occupancies. This is a software suspend
1520 * Returns the number of pages which were actually freed.
1522 * There is special handling here for zones which are full of pinned pages.
1523 * This can happen if the pages are all mlocked, or if they are all used by
1524 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1525 * What we do is to detect the case where all pages in the zone have been
1526 * scanned twice and there has been zero successful reclaim. Mark the zone as
1527 * dead and from now on, only perform a short scan. Basically we're polling
1528 * the zone for when the problem goes away.
1530 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1531 * zones which have free_pages > pages_high, but once a zone is found to have
1532 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1533 * of the number of free pages in the lower zones. This interoperates with
1534 * the page allocator fallback scheme to ensure that aging of pages is balanced
1537 static unsigned long balance_pgdat(pg_data_t *pgdat, unsigned long nr_pages,
1540 unsigned long to_free = nr_pages;
1544 unsigned long total_scanned;
1545 unsigned long nr_reclaimed;
1546 struct reclaim_state *reclaim_state = current->reclaim_state;
1547 struct scan_control sc = {
1548 .gfp_mask = GFP_KERNEL,
1550 .swap_cluster_max = nr_pages ? nr_pages : SWAP_CLUSTER_MAX,
1556 sc.may_writepage = !laptop_mode,
1557 sc.nr_mapped = read_page_state(nr_mapped);
1559 inc_page_state(pageoutrun);
1561 for (i = 0; i < pgdat->nr_zones; i++) {
1562 struct zone *zone = pgdat->node_zones + i;
1564 zone->temp_priority = DEF_PRIORITY;
1567 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1568 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1569 unsigned long lru_pages = 0;
1571 /* The swap token gets in the way of swapout... */
1573 disable_swap_token();
1577 if (nr_pages == 0) {
1579 * Scan in the highmem->dma direction for the highest
1580 * zone which needs scanning
1582 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1583 struct zone *zone = pgdat->node_zones + i;
1585 if (!populated_zone(zone))
1588 if (zone->all_unreclaimable &&
1589 priority != DEF_PRIORITY)
1592 if (!zone_watermark_ok(zone, order,
1593 zone->pages_high, 0, 0)) {
1600 end_zone = pgdat->nr_zones - 1;
1603 for (i = 0; i <= end_zone; i++) {
1604 struct zone *zone = pgdat->node_zones + i;
1606 lru_pages += zone->nr_active + zone->nr_inactive;
1610 * Now scan the zone in the dma->highmem direction, stopping
1611 * at the last zone which needs scanning.
1613 * We do this because the page allocator works in the opposite
1614 * direction. This prevents the page allocator from allocating
1615 * pages behind kswapd's direction of progress, which would
1616 * cause too much scanning of the lower zones.
1618 for (i = 0; i <= end_zone; i++) {
1619 struct zone *zone = pgdat->node_zones + i;
1622 if (!populated_zone(zone))
1625 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1628 if (nr_pages == 0) { /* Not software suspend */
1629 if (!zone_watermark_ok(zone, order,
1630 zone->pages_high, end_zone, 0))
1633 zone->temp_priority = priority;
1634 if (zone->prev_priority > priority)
1635 zone->prev_priority = priority;
1637 nr_reclaimed += shrink_zone(priority, zone, &sc);
1638 reclaim_state->reclaimed_slab = 0;
1639 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1641 nr_reclaimed += reclaim_state->reclaimed_slab;
1642 total_scanned += sc.nr_scanned;
1643 if (zone->all_unreclaimable)
1645 if (nr_slab == 0 && zone->pages_scanned >=
1646 (zone->nr_active + zone->nr_inactive) * 4)
1647 zone->all_unreclaimable = 1;
1649 * If we've done a decent amount of scanning and
1650 * the reclaim ratio is low, start doing writepage
1651 * even in laptop mode
1653 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
1654 total_scanned > nr_reclaimed + nr_reclaimed / 2)
1655 sc.may_writepage = 1;
1657 if (nr_pages && to_free > nr_reclaimed)
1658 continue; /* swsusp: need to do more work */
1660 break; /* kswapd: all done */
1662 * OK, kswapd is getting into trouble. Take a nap, then take
1663 * another pass across the zones.
1665 if (total_scanned && priority < DEF_PRIORITY - 2)
1666 blk_congestion_wait(WRITE, HZ/10);
1669 * We do this so kswapd doesn't build up large priorities for
1670 * example when it is freeing in parallel with allocators. It
1671 * matches the direct reclaim path behaviour in terms of impact
1672 * on zone->*_priority.
1674 if ((nr_reclaimed >= SWAP_CLUSTER_MAX) && !nr_pages)
1678 for (i = 0; i < pgdat->nr_zones; i++) {
1679 struct zone *zone = pgdat->node_zones + i;
1681 zone->prev_priority = zone->temp_priority;
1683 if (!all_zones_ok) {
1688 return nr_reclaimed;
1692 * The background pageout daemon, started as a kernel thread
1693 * from the init process.
1695 * This basically trickles out pages so that we have _some_
1696 * free memory available even if there is no other activity
1697 * that frees anything up. This is needed for things like routing
1698 * etc, where we otherwise might have all activity going on in
1699 * asynchronous contexts that cannot page things out.
1701 * If there are applications that are active memory-allocators
1702 * (most normal use), this basically shouldn't matter.
1704 static int kswapd(void *p)
1706 unsigned long order;
1707 pg_data_t *pgdat = (pg_data_t*)p;
1708 struct task_struct *tsk = current;
1710 struct reclaim_state reclaim_state = {
1711 .reclaimed_slab = 0,
1715 daemonize("kswapd%d", pgdat->node_id);
1716 cpumask = node_to_cpumask(pgdat->node_id);
1717 if (!cpus_empty(cpumask))
1718 set_cpus_allowed(tsk, cpumask);
1719 current->reclaim_state = &reclaim_state;
1722 * Tell the memory management that we're a "memory allocator",
1723 * and that if we need more memory we should get access to it
1724 * regardless (see "__alloc_pages()"). "kswapd" should
1725 * never get caught in the normal page freeing logic.
1727 * (Kswapd normally doesn't need memory anyway, but sometimes
1728 * you need a small amount of memory in order to be able to
1729 * page out something else, and this flag essentially protects
1730 * us from recursively trying to free more memory as we're
1731 * trying to free the first piece of memory in the first place).
1733 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
1737 unsigned long new_order;
1741 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1742 new_order = pgdat->kswapd_max_order;
1743 pgdat->kswapd_max_order = 0;
1744 if (order < new_order) {
1746 * Don't sleep if someone wants a larger 'order'
1752 order = pgdat->kswapd_max_order;
1754 finish_wait(&pgdat->kswapd_wait, &wait);
1756 balance_pgdat(pgdat, 0, order);
1762 * A zone is low on free memory, so wake its kswapd task to service it.
1764 void wakeup_kswapd(struct zone *zone, int order)
1768 if (!populated_zone(zone))
1771 pgdat = zone->zone_pgdat;
1772 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
1774 if (pgdat->kswapd_max_order < order)
1775 pgdat->kswapd_max_order = order;
1776 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1778 if (!waitqueue_active(&pgdat->kswapd_wait))
1780 wake_up_interruptible(&pgdat->kswapd_wait);
1785 * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
1788 unsigned long shrink_all_memory(unsigned long nr_pages)
1791 unsigned long nr_to_free = nr_pages;
1792 unsigned long ret = 0;
1793 struct reclaim_state reclaim_state = {
1794 .reclaimed_slab = 0,
1797 current->reclaim_state = &reclaim_state;
1798 for_each_pgdat(pgdat) {
1799 unsigned long freed;
1801 freed = balance_pgdat(pgdat, nr_to_free, 0);
1803 nr_to_free -= freed;
1804 if ((long)nr_to_free <= 0)
1807 current->reclaim_state = NULL;
1812 #ifdef CONFIG_HOTPLUG_CPU
1813 /* It's optimal to keep kswapds on the same CPUs as their memory, but
1814 not required for correctness. So if the last cpu in a node goes
1815 away, we get changed to run anywhere: as the first one comes back,
1816 restore their cpu bindings. */
1817 static int __devinit cpu_callback(struct notifier_block *nfb,
1818 unsigned long action, void *hcpu)
1823 if (action == CPU_ONLINE) {
1824 for_each_pgdat(pgdat) {
1825 mask = node_to_cpumask(pgdat->node_id);
1826 if (any_online_cpu(mask) != NR_CPUS)
1827 /* One of our CPUs online: restore mask */
1828 set_cpus_allowed(pgdat->kswapd, mask);
1833 #endif /* CONFIG_HOTPLUG_CPU */
1835 static int __init kswapd_init(void)
1840 for_each_pgdat(pgdat) {
1843 pid = kernel_thread(kswapd, pgdat, CLONE_KERNEL);
1845 pgdat->kswapd = find_task_by_pid(pid);
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 static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1890 const unsigned long nr_pages = 1 << order;
1891 struct task_struct *p = current;
1892 struct reclaim_state reclaim_state;
1894 unsigned long nr_reclaimed = 0;
1895 struct scan_control sc = {
1896 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1897 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
1898 .nr_mapped = read_page_state(nr_mapped),
1899 .swap_cluster_max = max_t(unsigned long, nr_pages,
1901 .gfp_mask = gfp_mask,
1904 disable_swap_token();
1907 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1908 * and we also need to be able to write out pages for RECLAIM_WRITE
1911 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
1912 reclaim_state.reclaimed_slab = 0;
1913 p->reclaim_state = &reclaim_state;
1916 * Free memory by calling shrink zone with increasing priorities
1917 * until we have enough memory freed.
1919 priority = ZONE_RECLAIM_PRIORITY;
1921 nr_reclaimed += shrink_zone(priority, zone, &sc);
1923 } while (priority >= 0 && nr_reclaimed < nr_pages);
1925 if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
1927 * shrink_slab does not currently allow us to determine
1928 * how many pages were freed in the zone. So we just
1929 * shake the slab and then go offnode for a single allocation.
1931 * shrink_slab will free memory on all zones and may take
1934 shrink_slab(sc.nr_scanned, gfp_mask, order);
1937 p->reclaim_state = NULL;
1938 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
1940 if (nr_reclaimed == 0)
1941 zone->last_unsuccessful_zone_reclaim = jiffies;
1943 return nr_reclaimed >= nr_pages;
1946 int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1952 * Do not reclaim if there was a recent unsuccessful attempt at zone
1953 * reclaim. In that case we let allocations go off node for the
1954 * zone_reclaim_interval. Otherwise we would scan for each off-node
1957 if (time_before(jiffies,
1958 zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval))
1962 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1963 * not have reclaimable pages and if we should not delay the allocation
1966 if (!(gfp_mask & __GFP_WAIT) ||
1967 zone->all_unreclaimable ||
1968 atomic_read(&zone->reclaim_in_progress) > 0 ||
1969 (current->flags & PF_MEMALLOC))
1973 * Only run zone reclaim on the local zone or on zones that do not
1974 * have associated processors. This will favor the local processor
1975 * over remote processors and spread off node memory allocations
1976 * as wide as possible.
1978 node_id = zone->zone_pgdat->node_id;
1979 mask = node_to_cpumask(node_id);
1980 if (!cpus_empty(mask) && node_id != numa_node_id())
1982 return __zone_reclaim(zone, gfp_mask, order);