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/gfp.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/vmstat.h>
23 #include <linux/file.h>
24 #include <linux/writeback.h>
25 #include <linux/blkdev.h>
26 #include <linux/buffer_head.h> /* for try_to_release_page(),
27 buffer_heads_over_limit */
28 #include <linux/mm_inline.h>
29 #include <linux/pagevec.h>
30 #include <linux/backing-dev.h>
31 #include <linux/rmap.h>
32 #include <linux/topology.h>
33 #include <linux/cpu.h>
34 #include <linux/cpuset.h>
35 #include <linux/notifier.h>
36 #include <linux/rwsem.h>
37 #include <linux/delay.h>
38 #include <linux/kthread.h>
39 #include <linux/freezer.h>
40 #include <linux/memcontrol.h>
41 #include <linux/delayacct.h>
42 #include <linux/sysctl.h>
44 #include <asm/tlbflush.h>
45 #include <asm/div64.h>
47 #include <linux/swapops.h>
52 /* Incremented by the number of inactive pages that were scanned */
53 unsigned long nr_scanned
;
55 /* Number of pages freed so far during a call to shrink_zones() */
56 unsigned long nr_reclaimed
;
58 /* How many pages shrink_list() should reclaim */
59 unsigned long nr_to_reclaim
;
61 unsigned long hibernation_mode
;
63 /* This context's GFP mask */
68 /* Can mapped pages be reclaimed? */
71 /* Can pages be swapped as part of reclaim? */
76 int all_unreclaimable
;
81 * Intend to reclaim enough contenious memory rather than to reclaim
82 * enough amount memory. I.e, it's the mode for high order allocation.
84 bool lumpy_reclaim_mode
;
86 /* Which cgroup do we reclaim from */
87 struct mem_cgroup
*mem_cgroup
;
90 * Nodemask of nodes allowed by the caller. If NULL, all nodes
95 /* Pluggable isolate pages callback */
96 unsigned long (*isolate_pages
)(unsigned long nr
, struct list_head
*dst
,
97 unsigned long *scanned
, int order
, int mode
,
98 struct zone
*z
, struct mem_cgroup
*mem_cont
,
99 int active
, int file
);
102 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
104 #ifdef ARCH_HAS_PREFETCH
105 #define prefetch_prev_lru_page(_page, _base, _field) \
107 if ((_page)->lru.prev != _base) { \
110 prev = lru_to_page(&(_page->lru)); \
111 prefetch(&prev->_field); \
115 #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
118 #ifdef ARCH_HAS_PREFETCHW
119 #define prefetchw_prev_lru_page(_page, _base, _field) \
121 if ((_page)->lru.prev != _base) { \
124 prev = lru_to_page(&(_page->lru)); \
125 prefetchw(&prev->_field); \
129 #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
133 * From 0 .. 100. Higher means more swappy.
135 int vm_swappiness
= 60;
136 long vm_total_pages
; /* The total number of pages which the VM controls */
138 static LIST_HEAD(shrinker_list
);
139 static DECLARE_RWSEM(shrinker_rwsem
);
141 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
142 #define scanning_global_lru(sc) (!(sc)->mem_cgroup)
144 #define scanning_global_lru(sc) (1)
147 static struct zone_reclaim_stat
*get_reclaim_stat(struct zone
*zone
,
148 struct scan_control
*sc
)
150 if (!scanning_global_lru(sc
))
151 return mem_cgroup_get_reclaim_stat(sc
->mem_cgroup
, zone
);
153 return &zone
->reclaim_stat
;
156 static unsigned long zone_nr_lru_pages(struct zone
*zone
,
157 struct scan_control
*sc
, enum lru_list lru
)
159 if (!scanning_global_lru(sc
))
160 return mem_cgroup_zone_nr_pages(sc
->mem_cgroup
, zone
, lru
);
162 return zone_page_state(zone
, NR_LRU_BASE
+ lru
);
167 * Add a shrinker callback to be called from the vm
169 void register_shrinker(struct shrinker
*shrinker
)
172 down_write(&shrinker_rwsem
);
173 list_add_tail(&shrinker
->list
, &shrinker_list
);
174 up_write(&shrinker_rwsem
);
176 EXPORT_SYMBOL(register_shrinker
);
181 void unregister_shrinker(struct shrinker
*shrinker
)
183 down_write(&shrinker_rwsem
);
184 list_del(&shrinker
->list
);
185 up_write(&shrinker_rwsem
);
187 EXPORT_SYMBOL(unregister_shrinker
);
189 #define SHRINK_BATCH 128
191 * Call the shrink functions to age shrinkable caches
193 * Here we assume it costs one seek to replace a lru page and that it also
194 * takes a seek to recreate a cache object. With this in mind we age equal
195 * percentages of the lru and ageable caches. This should balance the seeks
196 * generated by these structures.
198 * If the vm encountered mapped pages on the LRU it increase the pressure on
199 * slab to avoid swapping.
201 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
203 * `lru_pages' represents the number of on-LRU pages in all the zones which
204 * are eligible for the caller's allocation attempt. It is used for balancing
205 * slab reclaim versus page reclaim.
207 * Returns the number of slab objects which we shrunk.
209 unsigned long shrink_slab(unsigned long scanned
, gfp_t gfp_mask
,
210 unsigned long lru_pages
)
212 struct shrinker
*shrinker
;
213 unsigned long ret
= 0;
216 scanned
= SWAP_CLUSTER_MAX
;
218 if (!down_read_trylock(&shrinker_rwsem
))
219 return 1; /* Assume we'll be able to shrink next time */
221 list_for_each_entry(shrinker
, &shrinker_list
, list
) {
222 unsigned long long delta
;
223 unsigned long total_scan
;
224 unsigned long max_pass
= (*shrinker
->shrink
)(0, gfp_mask
);
226 delta
= (4 * scanned
) / shrinker
->seeks
;
228 do_div(delta
, lru_pages
+ 1);
229 shrinker
->nr
+= delta
;
230 if (shrinker
->nr
< 0) {
231 printk(KERN_ERR
"shrink_slab: %pF negative objects to "
233 shrinker
->shrink
, shrinker
->nr
);
234 shrinker
->nr
= max_pass
;
238 * Avoid risking looping forever due to too large nr value:
239 * never try to free more than twice the estimate number of
242 if (shrinker
->nr
> max_pass
* 2)
243 shrinker
->nr
= max_pass
* 2;
245 total_scan
= shrinker
->nr
;
248 while (total_scan
>= SHRINK_BATCH
) {
249 long this_scan
= SHRINK_BATCH
;
253 nr_before
= (*shrinker
->shrink
)(0, gfp_mask
);
254 shrink_ret
= (*shrinker
->shrink
)(this_scan
, gfp_mask
);
255 if (shrink_ret
== -1)
257 if (shrink_ret
< nr_before
)
258 ret
+= nr_before
- shrink_ret
;
259 count_vm_events(SLABS_SCANNED
, this_scan
);
260 total_scan
-= this_scan
;
265 shrinker
->nr
+= total_scan
;
267 up_read(&shrinker_rwsem
);
271 static inline int is_page_cache_freeable(struct page
*page
)
274 * A freeable page cache page is referenced only by the caller
275 * that isolated the page, the page cache radix tree and
276 * optional buffer heads at page->private.
278 return page_count(page
) - page_has_private(page
) == 2;
281 static int may_write_to_queue(struct backing_dev_info
*bdi
)
283 if (current
->flags
& PF_SWAPWRITE
)
285 if (!bdi_write_congested(bdi
))
287 if (bdi
== current
->backing_dev_info
)
293 * We detected a synchronous write error writing a page out. Probably
294 * -ENOSPC. We need to propagate that into the address_space for a subsequent
295 * fsync(), msync() or close().
297 * The tricky part is that after writepage we cannot touch the mapping: nothing
298 * prevents it from being freed up. But we have a ref on the page and once
299 * that page is locked, the mapping is pinned.
301 * We're allowed to run sleeping lock_page() here because we know the caller has
304 static void handle_write_error(struct address_space
*mapping
,
305 struct page
*page
, int error
)
308 if (page_mapping(page
) == mapping
)
309 mapping_set_error(mapping
, error
);
313 /* Request for sync pageout. */
319 /* possible outcome of pageout() */
321 /* failed to write page out, page is locked */
323 /* move page to the active list, page is locked */
325 /* page has been sent to the disk successfully, page is unlocked */
327 /* page is clean and locked */
332 * pageout is called by shrink_page_list() for each dirty page.
333 * Calls ->writepage().
335 static pageout_t
pageout(struct page
*page
, struct address_space
*mapping
,
336 enum pageout_io sync_writeback
)
339 * If the page is dirty, only perform writeback if that write
340 * will be non-blocking. To prevent this allocation from being
341 * stalled by pagecache activity. But note that there may be
342 * stalls if we need to run get_block(). We could test
343 * PagePrivate for that.
345 * If this process is currently in __generic_file_aio_write() against
346 * this page's queue, we can perform writeback even if that
349 * If the page is swapcache, write it back even if that would
350 * block, for some throttling. This happens by accident, because
351 * swap_backing_dev_info is bust: it doesn't reflect the
352 * congestion state of the swapdevs. Easy to fix, if needed.
354 if (!is_page_cache_freeable(page
))
358 * Some data journaling orphaned pages can have
359 * page->mapping == NULL while being dirty with clean buffers.
361 if (page_has_private(page
)) {
362 if (try_to_free_buffers(page
)) {
363 ClearPageDirty(page
);
364 printk("%s: orphaned page\n", __func__
);
370 if (mapping
->a_ops
->writepage
== NULL
)
371 return PAGE_ACTIVATE
;
372 if (!may_write_to_queue(mapping
->backing_dev_info
))
375 if (clear_page_dirty_for_io(page
)) {
377 struct writeback_control wbc
= {
378 .sync_mode
= WB_SYNC_NONE
,
379 .nr_to_write
= SWAP_CLUSTER_MAX
,
381 .range_end
= LLONG_MAX
,
386 SetPageReclaim(page
);
387 res
= mapping
->a_ops
->writepage(page
, &wbc
);
389 handle_write_error(mapping
, page
, res
);
390 if (res
== AOP_WRITEPAGE_ACTIVATE
) {
391 ClearPageReclaim(page
);
392 return PAGE_ACTIVATE
;
396 * Wait on writeback if requested to. This happens when
397 * direct reclaiming a large contiguous area and the
398 * first attempt to free a range of pages fails.
400 if (PageWriteback(page
) && sync_writeback
== PAGEOUT_IO_SYNC
)
401 wait_on_page_writeback(page
);
403 if (!PageWriteback(page
)) {
404 /* synchronous write or broken a_ops? */
405 ClearPageReclaim(page
);
407 inc_zone_page_state(page
, NR_VMSCAN_WRITE
);
415 * Same as remove_mapping, but if the page is removed from the mapping, it
416 * gets returned with a refcount of 0.
418 static int __remove_mapping(struct address_space
*mapping
, struct page
*page
)
420 BUG_ON(!PageLocked(page
));
421 BUG_ON(mapping
!= page_mapping(page
));
423 spin_lock_irq(&mapping
->tree_lock
);
425 * The non racy check for a busy page.
427 * Must be careful with the order of the tests. When someone has
428 * a ref to the page, it may be possible that they dirty it then
429 * drop the reference. So if PageDirty is tested before page_count
430 * here, then the following race may occur:
432 * get_user_pages(&page);
433 * [user mapping goes away]
435 * !PageDirty(page) [good]
436 * SetPageDirty(page);
438 * !page_count(page) [good, discard it]
440 * [oops, our write_to data is lost]
442 * Reversing the order of the tests ensures such a situation cannot
443 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
444 * load is not satisfied before that of page->_count.
446 * Note that if SetPageDirty is always performed via set_page_dirty,
447 * and thus under tree_lock, then this ordering is not required.
449 if (!page_freeze_refs(page
, 2))
451 /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
452 if (unlikely(PageDirty(page
))) {
453 page_unfreeze_refs(page
, 2);
457 if (PageSwapCache(page
)) {
458 swp_entry_t swap
= { .val
= page_private(page
) };
459 __delete_from_swap_cache(page
);
460 spin_unlock_irq(&mapping
->tree_lock
);
461 swapcache_free(swap
, page
);
463 __remove_from_page_cache(page
);
464 spin_unlock_irq(&mapping
->tree_lock
);
465 mem_cgroup_uncharge_cache_page(page
);
471 spin_unlock_irq(&mapping
->tree_lock
);
476 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
477 * someone else has a ref on the page, abort and return 0. If it was
478 * successfully detached, return 1. Assumes the caller has a single ref on
481 int remove_mapping(struct address_space
*mapping
, struct page
*page
)
483 if (__remove_mapping(mapping
, page
)) {
485 * Unfreezing the refcount with 1 rather than 2 effectively
486 * drops the pagecache ref for us without requiring another
489 page_unfreeze_refs(page
, 1);
496 * putback_lru_page - put previously isolated page onto appropriate LRU list
497 * @page: page to be put back to appropriate lru list
499 * Add previously isolated @page to appropriate LRU list.
500 * Page may still be unevictable for other reasons.
502 * lru_lock must not be held, interrupts must be enabled.
504 void putback_lru_page(struct page
*page
)
507 int active
= !!TestClearPageActive(page
);
508 int was_unevictable
= PageUnevictable(page
);
510 VM_BUG_ON(PageLRU(page
));
513 ClearPageUnevictable(page
);
515 if (page_evictable(page
, NULL
)) {
517 * For evictable pages, we can use the cache.
518 * In event of a race, worst case is we end up with an
519 * unevictable page on [in]active list.
520 * We know how to handle that.
522 lru
= active
+ page_lru_base_type(page
);
523 lru_cache_add_lru(page
, lru
);
526 * Put unevictable pages directly on zone's unevictable
529 lru
= LRU_UNEVICTABLE
;
530 add_page_to_unevictable_list(page
);
532 * When racing with an mlock clearing (page is
533 * unlocked), make sure that if the other thread does
534 * not observe our setting of PG_lru and fails
535 * isolation, we see PG_mlocked cleared below and move
536 * the page back to the evictable list.
538 * The other side is TestClearPageMlocked().
544 * page's status can change while we move it among lru. If an evictable
545 * page is on unevictable list, it never be freed. To avoid that,
546 * check after we added it to the list, again.
548 if (lru
== LRU_UNEVICTABLE
&& page_evictable(page
, NULL
)) {
549 if (!isolate_lru_page(page
)) {
553 /* This means someone else dropped this page from LRU
554 * So, it will be freed or putback to LRU again. There is
555 * nothing to do here.
559 if (was_unevictable
&& lru
!= LRU_UNEVICTABLE
)
560 count_vm_event(UNEVICTABLE_PGRESCUED
);
561 else if (!was_unevictable
&& lru
== LRU_UNEVICTABLE
)
562 count_vm_event(UNEVICTABLE_PGCULLED
);
564 put_page(page
); /* drop ref from isolate */
567 enum page_references
{
569 PAGEREF_RECLAIM_CLEAN
,
574 static enum page_references
page_check_references(struct page
*page
,
575 struct scan_control
*sc
)
577 int referenced_ptes
, referenced_page
;
578 unsigned long vm_flags
;
580 referenced_ptes
= page_referenced(page
, 1, sc
->mem_cgroup
, &vm_flags
);
581 referenced_page
= TestClearPageReferenced(page
);
583 /* Lumpy reclaim - ignore references */
584 if (sc
->lumpy_reclaim_mode
)
585 return PAGEREF_RECLAIM
;
588 * Mlock lost the isolation race with us. Let try_to_unmap()
589 * move the page to the unevictable list.
591 if (vm_flags
& VM_LOCKED
)
592 return PAGEREF_RECLAIM
;
594 if (referenced_ptes
) {
596 return PAGEREF_ACTIVATE
;
598 * All mapped pages start out with page table
599 * references from the instantiating fault, so we need
600 * to look twice if a mapped file page is used more
603 * Mark it and spare it for another trip around the
604 * inactive list. Another page table reference will
605 * lead to its activation.
607 * Note: the mark is set for activated pages as well
608 * so that recently deactivated but used pages are
611 SetPageReferenced(page
);
614 return PAGEREF_ACTIVATE
;
619 /* Reclaim if clean, defer dirty pages to writeback */
621 return PAGEREF_RECLAIM_CLEAN
;
623 return PAGEREF_RECLAIM
;
627 * shrink_page_list() returns the number of reclaimed pages
629 static unsigned long shrink_page_list(struct list_head
*page_list
,
630 struct scan_control
*sc
,
631 enum pageout_io sync_writeback
)
633 LIST_HEAD(ret_pages
);
634 struct pagevec freed_pvec
;
636 unsigned long nr_reclaimed
= 0;
640 pagevec_init(&freed_pvec
, 1);
641 while (!list_empty(page_list
)) {
642 enum page_references references
;
643 struct address_space
*mapping
;
649 page
= lru_to_page(page_list
);
650 list_del(&page
->lru
);
652 if (!trylock_page(page
))
655 VM_BUG_ON(PageActive(page
));
659 if (unlikely(!page_evictable(page
, NULL
)))
662 if (!sc
->may_unmap
&& page_mapped(page
))
665 /* Double the slab pressure for mapped and swapcache pages */
666 if (page_mapped(page
) || PageSwapCache(page
))
669 may_enter_fs
= (sc
->gfp_mask
& __GFP_FS
) ||
670 (PageSwapCache(page
) && (sc
->gfp_mask
& __GFP_IO
));
672 if (PageWriteback(page
)) {
674 * Synchronous reclaim is performed in two passes,
675 * first an asynchronous pass over the list to
676 * start parallel writeback, and a second synchronous
677 * pass to wait for the IO to complete. Wait here
678 * for any page for which writeback has already
681 if (sync_writeback
== PAGEOUT_IO_SYNC
&& may_enter_fs
)
682 wait_on_page_writeback(page
);
687 references
= page_check_references(page
, sc
);
688 switch (references
) {
689 case PAGEREF_ACTIVATE
:
690 goto activate_locked
;
693 case PAGEREF_RECLAIM
:
694 case PAGEREF_RECLAIM_CLEAN
:
695 ; /* try to reclaim the page below */
699 * Anonymous process memory has backing store?
700 * Try to allocate it some swap space here.
702 if (PageAnon(page
) && !PageSwapCache(page
)) {
703 if (!(sc
->gfp_mask
& __GFP_IO
))
705 if (!add_to_swap(page
))
706 goto activate_locked
;
710 mapping
= page_mapping(page
);
713 * The page is mapped into the page tables of one or more
714 * processes. Try to unmap it here.
716 if (page_mapped(page
) && mapping
) {
717 switch (try_to_unmap(page
, TTU_UNMAP
)) {
719 goto activate_locked
;
725 ; /* try to free the page below */
729 if (PageDirty(page
)) {
730 if (references
== PAGEREF_RECLAIM_CLEAN
)
734 if (!sc
->may_writepage
)
737 /* Page is dirty, try to write it out here */
738 switch (pageout(page
, mapping
, sync_writeback
)) {
742 goto activate_locked
;
744 if (PageWriteback(page
) || PageDirty(page
))
747 * A synchronous write - probably a ramdisk. Go
748 * ahead and try to reclaim the page.
750 if (!trylock_page(page
))
752 if (PageDirty(page
) || PageWriteback(page
))
754 mapping
= page_mapping(page
);
756 ; /* try to free the page below */
761 * If the page has buffers, try to free the buffer mappings
762 * associated with this page. If we succeed we try to free
765 * We do this even if the page is PageDirty().
766 * try_to_release_page() does not perform I/O, but it is
767 * possible for a page to have PageDirty set, but it is actually
768 * clean (all its buffers are clean). This happens if the
769 * buffers were written out directly, with submit_bh(). ext3
770 * will do this, as well as the blockdev mapping.
771 * try_to_release_page() will discover that cleanness and will
772 * drop the buffers and mark the page clean - it can be freed.
774 * Rarely, pages can have buffers and no ->mapping. These are
775 * the pages which were not successfully invalidated in
776 * truncate_complete_page(). We try to drop those buffers here
777 * and if that worked, and the page is no longer mapped into
778 * process address space (page_count == 1) it can be freed.
779 * Otherwise, leave the page on the LRU so it is swappable.
781 if (page_has_private(page
)) {
782 if (!try_to_release_page(page
, sc
->gfp_mask
))
783 goto activate_locked
;
784 if (!mapping
&& page_count(page
) == 1) {
786 if (put_page_testzero(page
))
790 * rare race with speculative reference.
791 * the speculative reference will free
792 * this page shortly, so we may
793 * increment nr_reclaimed here (and
794 * leave it off the LRU).
802 if (!mapping
|| !__remove_mapping(mapping
, page
))
806 * At this point, we have no other references and there is
807 * no way to pick any more up (removed from LRU, removed
808 * from pagecache). Can use non-atomic bitops now (and
809 * we obviously don't have to worry about waking up a process
810 * waiting on the page lock, because there are no references.
812 __clear_page_locked(page
);
815 if (!pagevec_add(&freed_pvec
, page
)) {
816 __pagevec_free(&freed_pvec
);
817 pagevec_reinit(&freed_pvec
);
822 if (PageSwapCache(page
))
823 try_to_free_swap(page
);
825 putback_lru_page(page
);
829 /* Not a candidate for swapping, so reclaim swap space. */
830 if (PageSwapCache(page
) && vm_swap_full())
831 try_to_free_swap(page
);
832 VM_BUG_ON(PageActive(page
));
838 list_add(&page
->lru
, &ret_pages
);
839 VM_BUG_ON(PageLRU(page
) || PageUnevictable(page
));
841 list_splice(&ret_pages
, page_list
);
842 if (pagevec_count(&freed_pvec
))
843 __pagevec_free(&freed_pvec
);
844 count_vm_events(PGACTIVATE
, pgactivate
);
849 * Attempt to remove the specified page from its LRU. Only take this page
850 * if it is of the appropriate PageActive status. Pages which are being
851 * freed elsewhere are also ignored.
853 * page: page to consider
854 * mode: one of the LRU isolation modes defined above
856 * returns 0 on success, -ve errno on failure.
858 int __isolate_lru_page(struct page
*page
, int mode
, int file
)
862 /* Only take pages on the LRU. */
867 * When checking the active state, we need to be sure we are
868 * dealing with comparible boolean values. Take the logical not
871 if (mode
!= ISOLATE_BOTH
&& (!PageActive(page
) != !mode
))
874 if (mode
!= ISOLATE_BOTH
&& page_is_file_cache(page
) != file
)
878 * When this function is being called for lumpy reclaim, we
879 * initially look into all LRU pages, active, inactive and
880 * unevictable; only give shrink_page_list evictable pages.
882 if (PageUnevictable(page
))
887 if (likely(get_page_unless_zero(page
))) {
889 * Be careful not to clear PageLRU until after we're
890 * sure the page is not being freed elsewhere -- the
891 * page release code relies on it.
901 * zone->lru_lock is heavily contended. Some of the functions that
902 * shrink the lists perform better by taking out a batch of pages
903 * and working on them outside the LRU lock.
905 * For pagecache intensive workloads, this function is the hottest
906 * spot in the kernel (apart from copy_*_user functions).
908 * Appropriate locks must be held before calling this function.
910 * @nr_to_scan: The number of pages to look through on the list.
911 * @src: The LRU list to pull pages off.
912 * @dst: The temp list to put pages on to.
913 * @scanned: The number of pages that were scanned.
914 * @order: The caller's attempted allocation order
915 * @mode: One of the LRU isolation modes
916 * @file: True [1] if isolating file [!anon] pages
918 * returns how many pages were moved onto *@dst.
920 static unsigned long isolate_lru_pages(unsigned long nr_to_scan
,
921 struct list_head
*src
, struct list_head
*dst
,
922 unsigned long *scanned
, int order
, int mode
, int file
)
924 unsigned long nr_taken
= 0;
927 for (scan
= 0; scan
< nr_to_scan
&& !list_empty(src
); scan
++) {
930 unsigned long end_pfn
;
931 unsigned long page_pfn
;
934 page
= lru_to_page(src
);
935 prefetchw_prev_lru_page(page
, src
, flags
);
937 VM_BUG_ON(!PageLRU(page
));
939 switch (__isolate_lru_page(page
, mode
, file
)) {
941 list_move(&page
->lru
, dst
);
942 mem_cgroup_del_lru(page
);
947 /* else it is being freed elsewhere */
948 list_move(&page
->lru
, src
);
949 mem_cgroup_rotate_lru_list(page
, page_lru(page
));
960 * Attempt to take all pages in the order aligned region
961 * surrounding the tag page. Only take those pages of
962 * the same active state as that tag page. We may safely
963 * round the target page pfn down to the requested order
964 * as the mem_map is guarenteed valid out to MAX_ORDER,
965 * where that page is in a different zone we will detect
966 * it from its zone id and abort this block scan.
968 zone_id
= page_zone_id(page
);
969 page_pfn
= page_to_pfn(page
);
970 pfn
= page_pfn
& ~((1 << order
) - 1);
971 end_pfn
= pfn
+ (1 << order
);
972 for (; pfn
< end_pfn
; pfn
++) {
973 struct page
*cursor_page
;
975 /* The target page is in the block, ignore it. */
976 if (unlikely(pfn
== page_pfn
))
979 /* Avoid holes within the zone. */
980 if (unlikely(!pfn_valid_within(pfn
)))
983 cursor_page
= pfn_to_page(pfn
);
985 /* Check that we have not crossed a zone boundary. */
986 if (unlikely(page_zone_id(cursor_page
) != zone_id
))
990 * If we don't have enough swap space, reclaiming of
991 * anon page which don't already have a swap slot is
994 if (nr_swap_pages
<= 0 && PageAnon(cursor_page
) &&
995 !PageSwapCache(cursor_page
))
998 if (__isolate_lru_page(cursor_page
, mode
, file
) == 0) {
999 list_move(&cursor_page
->lru
, dst
);
1000 mem_cgroup_del_lru(cursor_page
);
1011 static unsigned long isolate_pages_global(unsigned long nr
,
1012 struct list_head
*dst
,
1013 unsigned long *scanned
, int order
,
1014 int mode
, struct zone
*z
,
1015 struct mem_cgroup
*mem_cont
,
1016 int active
, int file
)
1023 return isolate_lru_pages(nr
, &z
->lru
[lru
].list
, dst
, scanned
, order
,
1028 * clear_active_flags() is a helper for shrink_active_list(), clearing
1029 * any active bits from the pages in the list.
1031 static unsigned long clear_active_flags(struct list_head
*page_list
,
1032 unsigned int *count
)
1038 list_for_each_entry(page
, page_list
, lru
) {
1039 lru
= page_lru_base_type(page
);
1040 if (PageActive(page
)) {
1042 ClearPageActive(page
);
1052 * isolate_lru_page - tries to isolate a page from its LRU list
1053 * @page: page to isolate from its LRU list
1055 * Isolates a @page from an LRU list, clears PageLRU and adjusts the
1056 * vmstat statistic corresponding to whatever LRU list the page was on.
1058 * Returns 0 if the page was removed from an LRU list.
1059 * Returns -EBUSY if the page was not on an LRU list.
1061 * The returned page will have PageLRU() cleared. If it was found on
1062 * the active list, it will have PageActive set. If it was found on
1063 * the unevictable list, it will have the PageUnevictable bit set. That flag
1064 * may need to be cleared by the caller before letting the page go.
1066 * The vmstat statistic corresponding to the list on which the page was
1067 * found will be decremented.
1070 * (1) Must be called with an elevated refcount on the page. This is a
1071 * fundamentnal difference from isolate_lru_pages (which is called
1072 * without a stable reference).
1073 * (2) the lru_lock must not be held.
1074 * (3) interrupts must be enabled.
1076 int isolate_lru_page(struct page
*page
)
1080 if (PageLRU(page
)) {
1081 struct zone
*zone
= page_zone(page
);
1083 spin_lock_irq(&zone
->lru_lock
);
1084 if (PageLRU(page
) && get_page_unless_zero(page
)) {
1085 int lru
= page_lru(page
);
1089 del_page_from_lru_list(zone
, page
, lru
);
1091 spin_unlock_irq(&zone
->lru_lock
);
1097 * Are there way too many processes in the direct reclaim path already?
1099 static int too_many_isolated(struct zone
*zone
, int file
,
1100 struct scan_control
*sc
)
1102 unsigned long inactive
, isolated
;
1104 if (current_is_kswapd())
1107 if (!scanning_global_lru(sc
))
1111 inactive
= zone_page_state(zone
, NR_INACTIVE_FILE
);
1112 isolated
= zone_page_state(zone
, NR_ISOLATED_FILE
);
1114 inactive
= zone_page_state(zone
, NR_INACTIVE_ANON
);
1115 isolated
= zone_page_state(zone
, NR_ISOLATED_ANON
);
1118 return isolated
> inactive
;
1122 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
1123 * of reclaimed pages
1125 static unsigned long shrink_inactive_list(unsigned long max_scan
,
1126 struct zone
*zone
, struct scan_control
*sc
,
1127 int priority
, int file
)
1129 LIST_HEAD(page_list
);
1130 struct pagevec pvec
;
1131 unsigned long nr_scanned
= 0;
1132 unsigned long nr_reclaimed
= 0;
1133 struct zone_reclaim_stat
*reclaim_stat
= get_reclaim_stat(zone
, sc
);
1135 while (unlikely(too_many_isolated(zone
, file
, sc
))) {
1136 congestion_wait(BLK_RW_ASYNC
, HZ
/10);
1138 /* We are about to die and free our memory. Return now. */
1139 if (fatal_signal_pending(current
))
1140 return SWAP_CLUSTER_MAX
;
1144 pagevec_init(&pvec
, 1);
1147 spin_lock_irq(&zone
->lru_lock
);
1150 unsigned long nr_taken
;
1151 unsigned long nr_scan
;
1152 unsigned long nr_freed
;
1153 unsigned long nr_active
;
1154 unsigned int count
[NR_LRU_LISTS
] = { 0, };
1155 int mode
= sc
->lumpy_reclaim_mode
? ISOLATE_BOTH
: ISOLATE_INACTIVE
;
1156 unsigned long nr_anon
;
1157 unsigned long nr_file
;
1159 nr_taken
= sc
->isolate_pages(SWAP_CLUSTER_MAX
,
1160 &page_list
, &nr_scan
, sc
->order
, mode
,
1161 zone
, sc
->mem_cgroup
, 0, file
);
1163 if (scanning_global_lru(sc
)) {
1164 zone
->pages_scanned
+= nr_scan
;
1165 if (current_is_kswapd())
1166 __count_zone_vm_events(PGSCAN_KSWAPD
, zone
,
1169 __count_zone_vm_events(PGSCAN_DIRECT
, zone
,
1176 nr_active
= clear_active_flags(&page_list
, count
);
1177 __count_vm_events(PGDEACTIVATE
, nr_active
);
1179 __mod_zone_page_state(zone
, NR_ACTIVE_FILE
,
1180 -count
[LRU_ACTIVE_FILE
]);
1181 __mod_zone_page_state(zone
, NR_INACTIVE_FILE
,
1182 -count
[LRU_INACTIVE_FILE
]);
1183 __mod_zone_page_state(zone
, NR_ACTIVE_ANON
,
1184 -count
[LRU_ACTIVE_ANON
]);
1185 __mod_zone_page_state(zone
, NR_INACTIVE_ANON
,
1186 -count
[LRU_INACTIVE_ANON
]);
1188 nr_anon
= count
[LRU_ACTIVE_ANON
] + count
[LRU_INACTIVE_ANON
];
1189 nr_file
= count
[LRU_ACTIVE_FILE
] + count
[LRU_INACTIVE_FILE
];
1190 __mod_zone_page_state(zone
, NR_ISOLATED_ANON
, nr_anon
);
1191 __mod_zone_page_state(zone
, NR_ISOLATED_FILE
, nr_file
);
1193 reclaim_stat
->recent_scanned
[0] += nr_anon
;
1194 reclaim_stat
->recent_scanned
[1] += nr_file
;
1196 spin_unlock_irq(&zone
->lru_lock
);
1198 nr_scanned
+= nr_scan
;
1199 nr_freed
= shrink_page_list(&page_list
, sc
, PAGEOUT_IO_ASYNC
);
1202 * If we are direct reclaiming for contiguous pages and we do
1203 * not reclaim everything in the list, try again and wait
1204 * for IO to complete. This will stall high-order allocations
1205 * but that should be acceptable to the caller
1207 if (nr_freed
< nr_taken
&& !current_is_kswapd() &&
1208 sc
->lumpy_reclaim_mode
) {
1209 congestion_wait(BLK_RW_ASYNC
, HZ
/10);
1212 * The attempt at page out may have made some
1213 * of the pages active, mark them inactive again.
1215 nr_active
= clear_active_flags(&page_list
, count
);
1216 count_vm_events(PGDEACTIVATE
, nr_active
);
1218 nr_freed
+= shrink_page_list(&page_list
, sc
,
1222 nr_reclaimed
+= nr_freed
;
1224 local_irq_disable();
1225 if (current_is_kswapd())
1226 __count_vm_events(KSWAPD_STEAL
, nr_freed
);
1227 __count_zone_vm_events(PGSTEAL
, zone
, nr_freed
);
1229 spin_lock(&zone
->lru_lock
);
1231 * Put back any unfreeable pages.
1233 while (!list_empty(&page_list
)) {
1235 page
= lru_to_page(&page_list
);
1236 VM_BUG_ON(PageLRU(page
));
1237 list_del(&page
->lru
);
1238 if (unlikely(!page_evictable(page
, NULL
))) {
1239 spin_unlock_irq(&zone
->lru_lock
);
1240 putback_lru_page(page
);
1241 spin_lock_irq(&zone
->lru_lock
);
1245 lru
= page_lru(page
);
1246 add_page_to_lru_list(zone
, page
, lru
);
1247 if (is_active_lru(lru
)) {
1248 int file
= is_file_lru(lru
);
1249 reclaim_stat
->recent_rotated
[file
]++;
1251 if (!pagevec_add(&pvec
, page
)) {
1252 spin_unlock_irq(&zone
->lru_lock
);
1253 __pagevec_release(&pvec
);
1254 spin_lock_irq(&zone
->lru_lock
);
1257 __mod_zone_page_state(zone
, NR_ISOLATED_ANON
, -nr_anon
);
1258 __mod_zone_page_state(zone
, NR_ISOLATED_FILE
, -nr_file
);
1260 } while (nr_scanned
< max_scan
);
1263 spin_unlock_irq(&zone
->lru_lock
);
1264 pagevec_release(&pvec
);
1265 return nr_reclaimed
;
1269 * We are about to scan this zone at a certain priority level. If that priority
1270 * level is smaller (ie: more urgent) than the previous priority, then note
1271 * that priority level within the zone. This is done so that when the next
1272 * process comes in to scan this zone, it will immediately start out at this
1273 * priority level rather than having to build up its own scanning priority.
1274 * Here, this priority affects only the reclaim-mapped threshold.
1276 static inline void note_zone_scanning_priority(struct zone
*zone
, int priority
)
1278 if (priority
< zone
->prev_priority
)
1279 zone
->prev_priority
= priority
;
1283 * This moves pages from the active list to the inactive list.
1285 * We move them the other way if the page is referenced by one or more
1286 * processes, from rmap.
1288 * If the pages are mostly unmapped, the processing is fast and it is
1289 * appropriate to hold zone->lru_lock across the whole operation. But if
1290 * the pages are mapped, the processing is slow (page_referenced()) so we
1291 * should drop zone->lru_lock around each page. It's impossible to balance
1292 * this, so instead we remove the pages from the LRU while processing them.
1293 * It is safe to rely on PG_active against the non-LRU pages in here because
1294 * nobody will play with that bit on a non-LRU page.
1296 * The downside is that we have to touch page->_count against each page.
1297 * But we had to alter page->flags anyway.
1300 static void move_active_pages_to_lru(struct zone
*zone
,
1301 struct list_head
*list
,
1304 unsigned long pgmoved
= 0;
1305 struct pagevec pvec
;
1308 pagevec_init(&pvec
, 1);
1310 while (!list_empty(list
)) {
1311 page
= lru_to_page(list
);
1313 VM_BUG_ON(PageLRU(page
));
1316 list_move(&page
->lru
, &zone
->lru
[lru
].list
);
1317 mem_cgroup_add_lru_list(page
, lru
);
1320 if (!pagevec_add(&pvec
, page
) || list_empty(list
)) {
1321 spin_unlock_irq(&zone
->lru_lock
);
1322 if (buffer_heads_over_limit
)
1323 pagevec_strip(&pvec
);
1324 __pagevec_release(&pvec
);
1325 spin_lock_irq(&zone
->lru_lock
);
1328 __mod_zone_page_state(zone
, NR_LRU_BASE
+ lru
, pgmoved
);
1329 if (!is_active_lru(lru
))
1330 __count_vm_events(PGDEACTIVATE
, pgmoved
);
1333 static void shrink_active_list(unsigned long nr_pages
, struct zone
*zone
,
1334 struct scan_control
*sc
, int priority
, int file
)
1336 unsigned long nr_taken
;
1337 unsigned long pgscanned
;
1338 unsigned long vm_flags
;
1339 LIST_HEAD(l_hold
); /* The pages which were snipped off */
1340 LIST_HEAD(l_active
);
1341 LIST_HEAD(l_inactive
);
1343 struct zone_reclaim_stat
*reclaim_stat
= get_reclaim_stat(zone
, sc
);
1344 unsigned long nr_rotated
= 0;
1347 spin_lock_irq(&zone
->lru_lock
);
1348 nr_taken
= sc
->isolate_pages(nr_pages
, &l_hold
, &pgscanned
, sc
->order
,
1349 ISOLATE_ACTIVE
, zone
,
1350 sc
->mem_cgroup
, 1, file
);
1352 * zone->pages_scanned is used for detect zone's oom
1353 * mem_cgroup remembers nr_scan by itself.
1355 if (scanning_global_lru(sc
)) {
1356 zone
->pages_scanned
+= pgscanned
;
1358 reclaim_stat
->recent_scanned
[file
] += nr_taken
;
1360 __count_zone_vm_events(PGREFILL
, zone
, pgscanned
);
1362 __mod_zone_page_state(zone
, NR_ACTIVE_FILE
, -nr_taken
);
1364 __mod_zone_page_state(zone
, NR_ACTIVE_ANON
, -nr_taken
);
1365 __mod_zone_page_state(zone
, NR_ISOLATED_ANON
+ file
, nr_taken
);
1366 spin_unlock_irq(&zone
->lru_lock
);
1368 while (!list_empty(&l_hold
)) {
1370 page
= lru_to_page(&l_hold
);
1371 list_del(&page
->lru
);
1373 if (unlikely(!page_evictable(page
, NULL
))) {
1374 putback_lru_page(page
);
1378 if (page_referenced(page
, 0, sc
->mem_cgroup
, &vm_flags
)) {
1381 * Identify referenced, file-backed active pages and
1382 * give them one more trip around the active list. So
1383 * that executable code get better chances to stay in
1384 * memory under moderate memory pressure. Anon pages
1385 * are not likely to be evicted by use-once streaming
1386 * IO, plus JVM can create lots of anon VM_EXEC pages,
1387 * so we ignore them here.
1389 if ((vm_flags
& VM_EXEC
) && page_is_file_cache(page
)) {
1390 list_add(&page
->lru
, &l_active
);
1395 ClearPageActive(page
); /* we are de-activating */
1396 list_add(&page
->lru
, &l_inactive
);
1400 * Move pages back to the lru list.
1402 spin_lock_irq(&zone
->lru_lock
);
1404 * Count referenced pages from currently used mappings as rotated,
1405 * even though only some of them are actually re-activated. This
1406 * helps balance scan pressure between file and anonymous pages in
1409 reclaim_stat
->recent_rotated
[file
] += nr_rotated
;
1411 move_active_pages_to_lru(zone
, &l_active
,
1412 LRU_ACTIVE
+ file
* LRU_FILE
);
1413 move_active_pages_to_lru(zone
, &l_inactive
,
1414 LRU_BASE
+ file
* LRU_FILE
);
1415 __mod_zone_page_state(zone
, NR_ISOLATED_ANON
+ file
, -nr_taken
);
1416 spin_unlock_irq(&zone
->lru_lock
);
1419 static int inactive_anon_is_low_global(struct zone
*zone
)
1421 unsigned long active
, inactive
;
1423 active
= zone_page_state(zone
, NR_ACTIVE_ANON
);
1424 inactive
= zone_page_state(zone
, NR_INACTIVE_ANON
);
1426 if (inactive
* zone
->inactive_ratio
< active
)
1433 * inactive_anon_is_low - check if anonymous pages need to be deactivated
1434 * @zone: zone to check
1435 * @sc: scan control of this context
1437 * Returns true if the zone does not have enough inactive anon pages,
1438 * meaning some active anon pages need to be deactivated.
1440 static int inactive_anon_is_low(struct zone
*zone
, struct scan_control
*sc
)
1444 if (scanning_global_lru(sc
))
1445 low
= inactive_anon_is_low_global(zone
);
1447 low
= mem_cgroup_inactive_anon_is_low(sc
->mem_cgroup
);
1451 static int inactive_file_is_low_global(struct zone
*zone
)
1453 unsigned long active
, inactive
;
1455 active
= zone_page_state(zone
, NR_ACTIVE_FILE
);
1456 inactive
= zone_page_state(zone
, NR_INACTIVE_FILE
);
1458 return (active
> inactive
);
1462 * inactive_file_is_low - check if file pages need to be deactivated
1463 * @zone: zone to check
1464 * @sc: scan control of this context
1466 * When the system is doing streaming IO, memory pressure here
1467 * ensures that active file pages get deactivated, until more
1468 * than half of the file pages are on the inactive list.
1470 * Once we get to that situation, protect the system's working
1471 * set from being evicted by disabling active file page aging.
1473 * This uses a different ratio than the anonymous pages, because
1474 * the page cache uses a use-once replacement algorithm.
1476 static int inactive_file_is_low(struct zone
*zone
, struct scan_control
*sc
)
1480 if (scanning_global_lru(sc
))
1481 low
= inactive_file_is_low_global(zone
);
1483 low
= mem_cgroup_inactive_file_is_low(sc
->mem_cgroup
);
1487 static int inactive_list_is_low(struct zone
*zone
, struct scan_control
*sc
,
1491 return inactive_file_is_low(zone
, sc
);
1493 return inactive_anon_is_low(zone
, sc
);
1496 static unsigned long shrink_list(enum lru_list lru
, unsigned long nr_to_scan
,
1497 struct zone
*zone
, struct scan_control
*sc
, int priority
)
1499 int file
= is_file_lru(lru
);
1501 if (is_active_lru(lru
)) {
1502 if (inactive_list_is_low(zone
, sc
, file
))
1503 shrink_active_list(nr_to_scan
, zone
, sc
, priority
, file
);
1507 return shrink_inactive_list(nr_to_scan
, zone
, sc
, priority
, file
);
1511 * Smallish @nr_to_scan's are deposited in @nr_saved_scan,
1512 * until we collected @swap_cluster_max pages to scan.
1514 static unsigned long nr_scan_try_batch(unsigned long nr_to_scan
,
1515 unsigned long *nr_saved_scan
)
1519 *nr_saved_scan
+= nr_to_scan
;
1520 nr
= *nr_saved_scan
;
1522 if (nr
>= SWAP_CLUSTER_MAX
)
1531 * Determine how aggressively the anon and file LRU lists should be
1532 * scanned. The relative value of each set of LRU lists is determined
1533 * by looking at the fraction of the pages scanned we did rotate back
1534 * onto the active list instead of evict.
1536 * nr[0] = anon pages to scan; nr[1] = file pages to scan
1538 static void get_scan_count(struct zone
*zone
, struct scan_control
*sc
,
1539 unsigned long *nr
, int priority
)
1541 unsigned long anon
, file
, free
;
1542 unsigned long anon_prio
, file_prio
;
1543 unsigned long ap
, fp
;
1544 struct zone_reclaim_stat
*reclaim_stat
= get_reclaim_stat(zone
, sc
);
1545 u64 fraction
[2], denominator
;
1549 /* If we have no swap space, do not bother scanning anon pages. */
1550 if (!sc
->may_swap
|| (nr_swap_pages
<= 0)) {
1558 anon
= zone_nr_lru_pages(zone
, sc
, LRU_ACTIVE_ANON
) +
1559 zone_nr_lru_pages(zone
, sc
, LRU_INACTIVE_ANON
);
1560 file
= zone_nr_lru_pages(zone
, sc
, LRU_ACTIVE_FILE
) +
1561 zone_nr_lru_pages(zone
, sc
, LRU_INACTIVE_FILE
);
1563 if (scanning_global_lru(sc
)) {
1564 free
= zone_page_state(zone
, NR_FREE_PAGES
);
1565 /* If we have very few page cache pages,
1566 force-scan anon pages. */
1567 if (unlikely(file
+ free
<= high_wmark_pages(zone
))) {
1576 * OK, so we have swap space and a fair amount of page cache
1577 * pages. We use the recently rotated / recently scanned
1578 * ratios to determine how valuable each cache is.
1580 * Because workloads change over time (and to avoid overflow)
1581 * we keep these statistics as a floating average, which ends
1582 * up weighing recent references more than old ones.
1584 * anon in [0], file in [1]
1586 if (unlikely(reclaim_stat
->recent_scanned
[0] > anon
/ 4)) {
1587 spin_lock_irq(&zone
->lru_lock
);
1588 reclaim_stat
->recent_scanned
[0] /= 2;
1589 reclaim_stat
->recent_rotated
[0] /= 2;
1590 spin_unlock_irq(&zone
->lru_lock
);
1593 if (unlikely(reclaim_stat
->recent_scanned
[1] > file
/ 4)) {
1594 spin_lock_irq(&zone
->lru_lock
);
1595 reclaim_stat
->recent_scanned
[1] /= 2;
1596 reclaim_stat
->recent_rotated
[1] /= 2;
1597 spin_unlock_irq(&zone
->lru_lock
);
1601 * With swappiness at 100, anonymous and file have the same priority.
1602 * This scanning priority is essentially the inverse of IO cost.
1604 anon_prio
= sc
->swappiness
;
1605 file_prio
= 200 - sc
->swappiness
;
1608 * The amount of pressure on anon vs file pages is inversely
1609 * proportional to the fraction of recently scanned pages on
1610 * each list that were recently referenced and in active use.
1612 ap
= (anon_prio
+ 1) * (reclaim_stat
->recent_scanned
[0] + 1);
1613 ap
/= reclaim_stat
->recent_rotated
[0] + 1;
1615 fp
= (file_prio
+ 1) * (reclaim_stat
->recent_scanned
[1] + 1);
1616 fp
/= reclaim_stat
->recent_rotated
[1] + 1;
1620 denominator
= ap
+ fp
+ 1;
1622 for_each_evictable_lru(l
) {
1623 int file
= is_file_lru(l
);
1626 scan
= zone_nr_lru_pages(zone
, sc
, l
);
1627 if (priority
|| noswap
) {
1629 scan
= div64_u64(scan
* fraction
[file
], denominator
);
1631 nr
[l
] = nr_scan_try_batch(scan
,
1632 &reclaim_stat
->nr_saved_scan
[l
]);
1636 static void set_lumpy_reclaim_mode(int priority
, struct scan_control
*sc
)
1639 * If we need a large contiguous chunk of memory, or have
1640 * trouble getting a small set of contiguous pages, we
1641 * will reclaim both active and inactive pages.
1643 if (sc
->order
> PAGE_ALLOC_COSTLY_ORDER
)
1644 sc
->lumpy_reclaim_mode
= 1;
1645 else if (sc
->order
&& priority
< DEF_PRIORITY
- 2)
1646 sc
->lumpy_reclaim_mode
= 1;
1648 sc
->lumpy_reclaim_mode
= 0;
1652 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1654 static void shrink_zone(int priority
, struct zone
*zone
,
1655 struct scan_control
*sc
)
1657 unsigned long nr
[NR_LRU_LISTS
];
1658 unsigned long nr_to_scan
;
1660 unsigned long nr_reclaimed
= sc
->nr_reclaimed
;
1661 unsigned long nr_to_reclaim
= sc
->nr_to_reclaim
;
1663 get_scan_count(zone
, sc
, nr
, priority
);
1665 set_lumpy_reclaim_mode(priority
, sc
);
1667 while (nr
[LRU_INACTIVE_ANON
] || nr
[LRU_ACTIVE_FILE
] ||
1668 nr
[LRU_INACTIVE_FILE
]) {
1669 for_each_evictable_lru(l
) {
1671 nr_to_scan
= min_t(unsigned long,
1672 nr
[l
], SWAP_CLUSTER_MAX
);
1673 nr
[l
] -= nr_to_scan
;
1675 nr_reclaimed
+= shrink_list(l
, nr_to_scan
,
1676 zone
, sc
, priority
);
1680 * On large memory systems, scan >> priority can become
1681 * really large. This is fine for the starting priority;
1682 * we want to put equal scanning pressure on each zone.
1683 * However, if the VM has a harder time of freeing pages,
1684 * with multiple processes reclaiming pages, the total
1685 * freeing target can get unreasonably large.
1687 if (nr_reclaimed
>= nr_to_reclaim
&& priority
< DEF_PRIORITY
)
1691 sc
->nr_reclaimed
= nr_reclaimed
;
1694 * Even if we did not try to evict anon pages at all, we want to
1695 * rebalance the anon lru active/inactive ratio.
1697 if (inactive_anon_is_low(zone
, sc
) && nr_swap_pages
> 0)
1698 shrink_active_list(SWAP_CLUSTER_MAX
, zone
, sc
, priority
, 0);
1700 throttle_vm_writeout(sc
->gfp_mask
);
1704 * This is the direct reclaim path, for page-allocating processes. We only
1705 * try to reclaim pages from zones which will satisfy the caller's allocation
1708 * We reclaim from a zone even if that zone is over high_wmark_pages(zone).
1710 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1712 * b) The target zone may be at high_wmark_pages(zone) but the lower zones
1713 * must go *over* high_wmark_pages(zone) to satisfy the `incremental min'
1714 * zone defense algorithm.
1716 * If a zone is deemed to be full of pinned pages then just give it a light
1717 * scan then give up on it.
1719 static void shrink_zones(int priority
, struct zonelist
*zonelist
,
1720 struct scan_control
*sc
)
1722 enum zone_type high_zoneidx
= gfp_zone(sc
->gfp_mask
);
1726 sc
->all_unreclaimable
= 1;
1727 for_each_zone_zonelist_nodemask(zone
, z
, zonelist
, high_zoneidx
,
1729 if (!populated_zone(zone
))
1732 * Take care memory controller reclaiming has small influence
1735 if (scanning_global_lru(sc
)) {
1736 if (!cpuset_zone_allowed_hardwall(zone
, GFP_KERNEL
))
1738 note_zone_scanning_priority(zone
, priority
);
1740 if (zone
->all_unreclaimable
&& priority
!= DEF_PRIORITY
)
1741 continue; /* Let kswapd poll it */
1742 sc
->all_unreclaimable
= 0;
1745 * Ignore cpuset limitation here. We just want to reduce
1746 * # of used pages by us regardless of memory shortage.
1748 sc
->all_unreclaimable
= 0;
1749 mem_cgroup_note_reclaim_priority(sc
->mem_cgroup
,
1753 shrink_zone(priority
, zone
, sc
);
1758 * This is the main entry point to direct page reclaim.
1760 * If a full scan of the inactive list fails to free enough memory then we
1761 * are "out of memory" and something needs to be killed.
1763 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1764 * high - the zone may be full of dirty or under-writeback pages, which this
1765 * caller can't do much about. We kick the writeback threads and take explicit
1766 * naps in the hope that some of these pages can be written. But if the
1767 * allocating task holds filesystem locks which prevent writeout this might not
1768 * work, and the allocation attempt will fail.
1770 * returns: 0, if no pages reclaimed
1771 * else, the number of pages reclaimed
1773 static unsigned long do_try_to_free_pages(struct zonelist
*zonelist
,
1774 struct scan_control
*sc
)
1777 unsigned long ret
= 0;
1778 unsigned long total_scanned
= 0;
1779 struct reclaim_state
*reclaim_state
= current
->reclaim_state
;
1780 unsigned long lru_pages
= 0;
1783 enum zone_type high_zoneidx
= gfp_zone(sc
->gfp_mask
);
1784 unsigned long writeback_threshold
;
1787 delayacct_freepages_start();
1789 if (scanning_global_lru(sc
))
1790 count_vm_event(ALLOCSTALL
);
1792 * mem_cgroup will not do shrink_slab.
1794 if (scanning_global_lru(sc
)) {
1795 for_each_zone_zonelist(zone
, z
, zonelist
, high_zoneidx
) {
1797 if (!cpuset_zone_allowed_hardwall(zone
, GFP_KERNEL
))
1800 lru_pages
+= zone_reclaimable_pages(zone
);
1804 for (priority
= DEF_PRIORITY
; priority
>= 0; priority
--) {
1807 disable_swap_token();
1808 shrink_zones(priority
, zonelist
, sc
);
1810 * Don't shrink slabs when reclaiming memory from
1811 * over limit cgroups
1813 if (scanning_global_lru(sc
)) {
1814 shrink_slab(sc
->nr_scanned
, sc
->gfp_mask
, lru_pages
);
1815 if (reclaim_state
) {
1816 sc
->nr_reclaimed
+= reclaim_state
->reclaimed_slab
;
1817 reclaim_state
->reclaimed_slab
= 0;
1820 total_scanned
+= sc
->nr_scanned
;
1821 if (sc
->nr_reclaimed
>= sc
->nr_to_reclaim
) {
1822 ret
= sc
->nr_reclaimed
;
1827 * Try to write back as many pages as we just scanned. This
1828 * tends to cause slow streaming writers to write data to the
1829 * disk smoothly, at the dirtying rate, which is nice. But
1830 * that's undesirable in laptop mode, where we *want* lumpy
1831 * writeout. So in laptop mode, write out the whole world.
1833 writeback_threshold
= sc
->nr_to_reclaim
+ sc
->nr_to_reclaim
/ 2;
1834 if (total_scanned
> writeback_threshold
) {
1835 wakeup_flusher_threads(laptop_mode
? 0 : total_scanned
);
1836 sc
->may_writepage
= 1;
1839 /* Take a nap, wait for some writeback to complete */
1840 if (!sc
->hibernation_mode
&& sc
->nr_scanned
&&
1841 priority
< DEF_PRIORITY
- 2)
1842 congestion_wait(BLK_RW_ASYNC
, HZ
/10);
1844 /* top priority shrink_zones still had more to do? don't OOM, then */
1845 if (!sc
->all_unreclaimable
&& scanning_global_lru(sc
))
1846 ret
= sc
->nr_reclaimed
;
1849 * Now that we've scanned all the zones at this priority level, note
1850 * that level within the zone so that the next thread which performs
1851 * scanning of this zone will immediately start out at this priority
1852 * level. This affects only the decision whether or not to bring
1853 * mapped pages onto the inactive list.
1858 if (scanning_global_lru(sc
)) {
1859 for_each_zone_zonelist(zone
, z
, zonelist
, high_zoneidx
) {
1861 if (!cpuset_zone_allowed_hardwall(zone
, GFP_KERNEL
))
1864 zone
->prev_priority
= priority
;
1867 mem_cgroup_record_reclaim_priority(sc
->mem_cgroup
, priority
);
1869 delayacct_freepages_end();
1875 unsigned long try_to_free_pages(struct zonelist
*zonelist
, int order
,
1876 gfp_t gfp_mask
, nodemask_t
*nodemask
)
1878 struct scan_control sc
= {
1879 .gfp_mask
= gfp_mask
,
1880 .may_writepage
= !laptop_mode
,
1881 .nr_to_reclaim
= SWAP_CLUSTER_MAX
,
1884 .swappiness
= vm_swappiness
,
1887 .isolate_pages
= isolate_pages_global
,
1888 .nodemask
= nodemask
,
1891 return do_try_to_free_pages(zonelist
, &sc
);
1894 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
1896 unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup
*mem
,
1897 gfp_t gfp_mask
, bool noswap
,
1898 unsigned int swappiness
,
1899 struct zone
*zone
, int nid
)
1901 struct scan_control sc
= {
1902 .may_writepage
= !laptop_mode
,
1904 .may_swap
= !noswap
,
1905 .swappiness
= swappiness
,
1908 .isolate_pages
= mem_cgroup_isolate_pages
,
1910 nodemask_t nm
= nodemask_of_node(nid
);
1912 sc
.gfp_mask
= (gfp_mask
& GFP_RECLAIM_MASK
) |
1913 (GFP_HIGHUSER_MOVABLE
& ~GFP_RECLAIM_MASK
);
1915 sc
.nr_reclaimed
= 0;
1918 * NOTE: Although we can get the priority field, using it
1919 * here is not a good idea, since it limits the pages we can scan.
1920 * if we don't reclaim here, the shrink_zone from balance_pgdat
1921 * will pick up pages from other mem cgroup's as well. We hack
1922 * the priority and make it zero.
1924 shrink_zone(0, zone
, &sc
);
1925 return sc
.nr_reclaimed
;
1928 unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup
*mem_cont
,
1931 unsigned int swappiness
)
1933 struct zonelist
*zonelist
;
1934 struct scan_control sc
= {
1935 .may_writepage
= !laptop_mode
,
1937 .may_swap
= !noswap
,
1938 .nr_to_reclaim
= SWAP_CLUSTER_MAX
,
1939 .swappiness
= swappiness
,
1941 .mem_cgroup
= mem_cont
,
1942 .isolate_pages
= mem_cgroup_isolate_pages
,
1943 .nodemask
= NULL
, /* we don't care the placement */
1946 sc
.gfp_mask
= (gfp_mask
& GFP_RECLAIM_MASK
) |
1947 (GFP_HIGHUSER_MOVABLE
& ~GFP_RECLAIM_MASK
);
1948 zonelist
= NODE_DATA(numa_node_id())->node_zonelists
;
1949 return do_try_to_free_pages(zonelist
, &sc
);
1953 /* is kswapd sleeping prematurely? */
1954 static int sleeping_prematurely(pg_data_t
*pgdat
, int order
, long remaining
)
1958 /* If a direct reclaimer woke kswapd within HZ/10, it's premature */
1962 /* If after HZ/10, a zone is below the high mark, it's premature */
1963 for (i
= 0; i
< pgdat
->nr_zones
; i
++) {
1964 struct zone
*zone
= pgdat
->node_zones
+ i
;
1966 if (!populated_zone(zone
))
1969 if (zone
->all_unreclaimable
)
1972 if (!zone_watermark_ok(zone
, order
, high_wmark_pages(zone
),
1981 * For kswapd, balance_pgdat() will work across all this node's zones until
1982 * they are all at high_wmark_pages(zone).
1984 * Returns the number of pages which were actually freed.
1986 * There is special handling here for zones which are full of pinned pages.
1987 * This can happen if the pages are all mlocked, or if they are all used by
1988 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1989 * What we do is to detect the case where all pages in the zone have been
1990 * scanned twice and there has been zero successful reclaim. Mark the zone as
1991 * dead and from now on, only perform a short scan. Basically we're polling
1992 * the zone for when the problem goes away.
1994 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1995 * zones which have free_pages > high_wmark_pages(zone), but once a zone is
1996 * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the
1997 * lower zones regardless of the number of free pages in the lower zones. This
1998 * interoperates with the page allocator fallback scheme to ensure that aging
1999 * of pages is balanced across the zones.
2001 static unsigned long balance_pgdat(pg_data_t
*pgdat
, int order
)
2006 unsigned long total_scanned
;
2007 struct reclaim_state
*reclaim_state
= current
->reclaim_state
;
2008 struct scan_control sc
= {
2009 .gfp_mask
= GFP_KERNEL
,
2013 * kswapd doesn't want to be bailed out while reclaim. because
2014 * we want to put equal scanning pressure on each zone.
2016 .nr_to_reclaim
= ULONG_MAX
,
2017 .swappiness
= vm_swappiness
,
2020 .isolate_pages
= isolate_pages_global
,
2023 * temp_priority is used to remember the scanning priority at which
2024 * this zone was successfully refilled to
2025 * free_pages == high_wmark_pages(zone).
2027 int temp_priority
[MAX_NR_ZONES
];
2031 sc
.nr_reclaimed
= 0;
2032 sc
.may_writepage
= !laptop_mode
;
2033 count_vm_event(PAGEOUTRUN
);
2035 for (i
= 0; i
< pgdat
->nr_zones
; i
++)
2036 temp_priority
[i
] = DEF_PRIORITY
;
2038 for (priority
= DEF_PRIORITY
; priority
>= 0; priority
--) {
2039 int end_zone
= 0; /* Inclusive. 0 = ZONE_DMA */
2040 unsigned long lru_pages
= 0;
2041 int has_under_min_watermark_zone
= 0;
2043 /* The swap token gets in the way of swapout... */
2045 disable_swap_token();
2050 * Scan in the highmem->dma direction for the highest
2051 * zone which needs scanning
2053 for (i
= pgdat
->nr_zones
- 1; i
>= 0; i
--) {
2054 struct zone
*zone
= pgdat
->node_zones
+ i
;
2056 if (!populated_zone(zone
))
2059 if (zone
->all_unreclaimable
&& priority
!= DEF_PRIORITY
)
2063 * Do some background aging of the anon list, to give
2064 * pages a chance to be referenced before reclaiming.
2066 if (inactive_anon_is_low(zone
, &sc
))
2067 shrink_active_list(SWAP_CLUSTER_MAX
, zone
,
2070 if (!zone_watermark_ok(zone
, order
,
2071 high_wmark_pages(zone
), 0, 0)) {
2079 for (i
= 0; i
<= end_zone
; i
++) {
2080 struct zone
*zone
= pgdat
->node_zones
+ i
;
2082 lru_pages
+= zone_reclaimable_pages(zone
);
2086 * Now scan the zone in the dma->highmem direction, stopping
2087 * at the last zone which needs scanning.
2089 * We do this because the page allocator works in the opposite
2090 * direction. This prevents the page allocator from allocating
2091 * pages behind kswapd's direction of progress, which would
2092 * cause too much scanning of the lower zones.
2094 for (i
= 0; i
<= end_zone
; i
++) {
2095 struct zone
*zone
= pgdat
->node_zones
+ i
;
2099 if (!populated_zone(zone
))
2102 if (zone
->all_unreclaimable
&& priority
!= DEF_PRIORITY
)
2105 temp_priority
[i
] = priority
;
2107 note_zone_scanning_priority(zone
, priority
);
2109 nid
= pgdat
->node_id
;
2110 zid
= zone_idx(zone
);
2112 * Call soft limit reclaim before calling shrink_zone.
2113 * For now we ignore the return value
2115 mem_cgroup_soft_limit_reclaim(zone
, order
, sc
.gfp_mask
,
2118 * We put equal pressure on every zone, unless one
2119 * zone has way too many pages free already.
2121 if (!zone_watermark_ok(zone
, order
,
2122 8*high_wmark_pages(zone
), end_zone
, 0))
2123 shrink_zone(priority
, zone
, &sc
);
2124 reclaim_state
->reclaimed_slab
= 0;
2125 nr_slab
= shrink_slab(sc
.nr_scanned
, GFP_KERNEL
,
2127 sc
.nr_reclaimed
+= reclaim_state
->reclaimed_slab
;
2128 total_scanned
+= sc
.nr_scanned
;
2129 if (zone
->all_unreclaimable
)
2132 zone
->pages_scanned
>= (zone_reclaimable_pages(zone
) * 6))
2133 zone
->all_unreclaimable
= 1;
2135 * If we've done a decent amount of scanning and
2136 * the reclaim ratio is low, start doing writepage
2137 * even in laptop mode
2139 if (total_scanned
> SWAP_CLUSTER_MAX
* 2 &&
2140 total_scanned
> sc
.nr_reclaimed
+ sc
.nr_reclaimed
/ 2)
2141 sc
.may_writepage
= 1;
2143 if (!zone_watermark_ok(zone
, order
,
2144 high_wmark_pages(zone
), end_zone
, 0)) {
2147 * We are still under min water mark. This
2148 * means that we have a GFP_ATOMIC allocation
2149 * failure risk. Hurry up!
2151 if (!zone_watermark_ok(zone
, order
,
2152 min_wmark_pages(zone
), end_zone
, 0))
2153 has_under_min_watermark_zone
= 1;
2158 break; /* kswapd: all done */
2160 * OK, kswapd is getting into trouble. Take a nap, then take
2161 * another pass across the zones.
2163 if (total_scanned
&& (priority
< DEF_PRIORITY
- 2)) {
2164 if (has_under_min_watermark_zone
)
2165 count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT
);
2167 congestion_wait(BLK_RW_ASYNC
, HZ
/10);
2171 * We do this so kswapd doesn't build up large priorities for
2172 * example when it is freeing in parallel with allocators. It
2173 * matches the direct reclaim path behaviour in terms of impact
2174 * on zone->*_priority.
2176 if (sc
.nr_reclaimed
>= SWAP_CLUSTER_MAX
)
2181 * Note within each zone the priority level at which this zone was
2182 * brought into a happy state. So that the next thread which scans this
2183 * zone will start out at that priority level.
2185 for (i
= 0; i
< pgdat
->nr_zones
; i
++) {
2186 struct zone
*zone
= pgdat
->node_zones
+ i
;
2188 zone
->prev_priority
= temp_priority
[i
];
2190 if (!all_zones_ok
) {
2196 * Fragmentation may mean that the system cannot be
2197 * rebalanced for high-order allocations in all zones.
2198 * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
2199 * it means the zones have been fully scanned and are still
2200 * not balanced. For high-order allocations, there is
2201 * little point trying all over again as kswapd may
2204 * Instead, recheck all watermarks at order-0 as they
2205 * are the most important. If watermarks are ok, kswapd will go
2206 * back to sleep. High-order users can still perform direct
2207 * reclaim if they wish.
2209 if (sc
.nr_reclaimed
< SWAP_CLUSTER_MAX
)
2210 order
= sc
.order
= 0;
2215 return sc
.nr_reclaimed
;
2219 * The background pageout daemon, started as a kernel thread
2220 * from the init process.
2222 * This basically trickles out pages so that we have _some_
2223 * free memory available even if there is no other activity
2224 * that frees anything up. This is needed for things like routing
2225 * etc, where we otherwise might have all activity going on in
2226 * asynchronous contexts that cannot page things out.
2228 * If there are applications that are active memory-allocators
2229 * (most normal use), this basically shouldn't matter.
2231 static int kswapd(void *p
)
2233 unsigned long order
;
2234 pg_data_t
*pgdat
= (pg_data_t
*)p
;
2235 struct task_struct
*tsk
= current
;
2237 struct reclaim_state reclaim_state
= {
2238 .reclaimed_slab
= 0,
2240 const struct cpumask
*cpumask
= cpumask_of_node(pgdat
->node_id
);
2242 lockdep_set_current_reclaim_state(GFP_KERNEL
);
2244 if (!cpumask_empty(cpumask
))
2245 set_cpus_allowed_ptr(tsk
, cpumask
);
2246 current
->reclaim_state
= &reclaim_state
;
2249 * Tell the memory management that we're a "memory allocator",
2250 * and that if we need more memory we should get access to it
2251 * regardless (see "__alloc_pages()"). "kswapd" should
2252 * never get caught in the normal page freeing logic.
2254 * (Kswapd normally doesn't need memory anyway, but sometimes
2255 * you need a small amount of memory in order to be able to
2256 * page out something else, and this flag essentially protects
2257 * us from recursively trying to free more memory as we're
2258 * trying to free the first piece of memory in the first place).
2260 tsk
->flags
|= PF_MEMALLOC
| PF_SWAPWRITE
| PF_KSWAPD
;
2265 unsigned long new_order
;
2268 prepare_to_wait(&pgdat
->kswapd_wait
, &wait
, TASK_INTERRUPTIBLE
);
2269 new_order
= pgdat
->kswapd_max_order
;
2270 pgdat
->kswapd_max_order
= 0;
2271 if (order
< new_order
) {
2273 * Don't sleep if someone wants a larger 'order'
2278 if (!freezing(current
) && !kthread_should_stop()) {
2281 /* Try to sleep for a short interval */
2282 if (!sleeping_prematurely(pgdat
, order
, remaining
)) {
2283 remaining
= schedule_timeout(HZ
/10);
2284 finish_wait(&pgdat
->kswapd_wait
, &wait
);
2285 prepare_to_wait(&pgdat
->kswapd_wait
, &wait
, TASK_INTERRUPTIBLE
);
2289 * After a short sleep, check if it was a
2290 * premature sleep. If not, then go fully
2291 * to sleep until explicitly woken up
2293 if (!sleeping_prematurely(pgdat
, order
, remaining
))
2297 count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY
);
2299 count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY
);
2303 order
= pgdat
->kswapd_max_order
;
2305 finish_wait(&pgdat
->kswapd_wait
, &wait
);
2307 ret
= try_to_freeze();
2308 if (kthread_should_stop())
2312 * We can speed up thawing tasks if we don't call balance_pgdat
2313 * after returning from the refrigerator
2316 balance_pgdat(pgdat
, order
);
2322 * A zone is low on free memory, so wake its kswapd task to service it.
2324 void wakeup_kswapd(struct zone
*zone
, int order
)
2328 if (!populated_zone(zone
))
2331 pgdat
= zone
->zone_pgdat
;
2332 if (zone_watermark_ok(zone
, order
, low_wmark_pages(zone
), 0, 0))
2334 if (pgdat
->kswapd_max_order
< order
)
2335 pgdat
->kswapd_max_order
= order
;
2336 if (!cpuset_zone_allowed_hardwall(zone
, GFP_KERNEL
))
2338 if (!waitqueue_active(&pgdat
->kswapd_wait
))
2340 wake_up_interruptible(&pgdat
->kswapd_wait
);
2344 * The reclaimable count would be mostly accurate.
2345 * The less reclaimable pages may be
2346 * - mlocked pages, which will be moved to unevictable list when encountered
2347 * - mapped pages, which may require several travels to be reclaimed
2348 * - dirty pages, which is not "instantly" reclaimable
2350 unsigned long global_reclaimable_pages(void)
2354 nr
= global_page_state(NR_ACTIVE_FILE
) +
2355 global_page_state(NR_INACTIVE_FILE
);
2357 if (nr_swap_pages
> 0)
2358 nr
+= global_page_state(NR_ACTIVE_ANON
) +
2359 global_page_state(NR_INACTIVE_ANON
);
2364 unsigned long zone_reclaimable_pages(struct zone
*zone
)
2368 nr
= zone_page_state(zone
, NR_ACTIVE_FILE
) +
2369 zone_page_state(zone
, NR_INACTIVE_FILE
);
2371 if (nr_swap_pages
> 0)
2372 nr
+= zone_page_state(zone
, NR_ACTIVE_ANON
) +
2373 zone_page_state(zone
, NR_INACTIVE_ANON
);
2378 #ifdef CONFIG_HIBERNATION
2380 * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
2383 * Rather than trying to age LRUs the aim is to preserve the overall
2384 * LRU order by reclaiming preferentially
2385 * inactive > active > active referenced > active mapped
2387 unsigned long shrink_all_memory(unsigned long nr_to_reclaim
)
2389 struct reclaim_state reclaim_state
;
2390 struct scan_control sc
= {
2391 .gfp_mask
= GFP_HIGHUSER_MOVABLE
,
2395 .nr_to_reclaim
= nr_to_reclaim
,
2396 .hibernation_mode
= 1,
2397 .swappiness
= vm_swappiness
,
2399 .isolate_pages
= isolate_pages_global
,
2401 struct zonelist
* zonelist
= node_zonelist(numa_node_id(), sc
.gfp_mask
);
2402 struct task_struct
*p
= current
;
2403 unsigned long nr_reclaimed
;
2405 p
->flags
|= PF_MEMALLOC
;
2406 lockdep_set_current_reclaim_state(sc
.gfp_mask
);
2407 reclaim_state
.reclaimed_slab
= 0;
2408 p
->reclaim_state
= &reclaim_state
;
2410 nr_reclaimed
= do_try_to_free_pages(zonelist
, &sc
);
2412 p
->reclaim_state
= NULL
;
2413 lockdep_clear_current_reclaim_state();
2414 p
->flags
&= ~PF_MEMALLOC
;
2416 return nr_reclaimed
;
2418 #endif /* CONFIG_HIBERNATION */
2420 /* It's optimal to keep kswapds on the same CPUs as their memory, but
2421 not required for correctness. So if the last cpu in a node goes
2422 away, we get changed to run anywhere: as the first one comes back,
2423 restore their cpu bindings. */
2424 static int __devinit
cpu_callback(struct notifier_block
*nfb
,
2425 unsigned long action
, void *hcpu
)
2429 if (action
== CPU_ONLINE
|| action
== CPU_ONLINE_FROZEN
) {
2430 for_each_node_state(nid
, N_HIGH_MEMORY
) {
2431 pg_data_t
*pgdat
= NODE_DATA(nid
);
2432 const struct cpumask
*mask
;
2434 mask
= cpumask_of_node(pgdat
->node_id
);
2436 if (cpumask_any_and(cpu_online_mask
, mask
) < nr_cpu_ids
)
2437 /* One of our CPUs online: restore mask */
2438 set_cpus_allowed_ptr(pgdat
->kswapd
, mask
);
2445 * This kswapd start function will be called by init and node-hot-add.
2446 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
2448 int kswapd_run(int nid
)
2450 pg_data_t
*pgdat
= NODE_DATA(nid
);
2456 pgdat
->kswapd
= kthread_run(kswapd
, pgdat
, "kswapd%d", nid
);
2457 if (IS_ERR(pgdat
->kswapd
)) {
2458 /* failure at boot is fatal */
2459 BUG_ON(system_state
== SYSTEM_BOOTING
);
2460 printk("Failed to start kswapd on node %d\n",nid
);
2467 * Called by memory hotplug when all memory in a node is offlined.
2469 void kswapd_stop(int nid
)
2471 struct task_struct
*kswapd
= NODE_DATA(nid
)->kswapd
;
2474 kthread_stop(kswapd
);
2477 static int __init
kswapd_init(void)
2482 for_each_node_state(nid
, N_HIGH_MEMORY
)
2484 hotcpu_notifier(cpu_callback
, 0);
2488 module_init(kswapd_init
)
2494 * If non-zero call zone_reclaim when the number of free pages falls below
2497 int zone_reclaim_mode __read_mostly
;
2499 #define RECLAIM_OFF 0
2500 #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
2501 #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
2502 #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
2505 * Priority for ZONE_RECLAIM. This determines the fraction of pages
2506 * of a node considered for each zone_reclaim. 4 scans 1/16th of
2509 #define ZONE_RECLAIM_PRIORITY 4
2512 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
2515 int sysctl_min_unmapped_ratio
= 1;
2518 * If the number of slab pages in a zone grows beyond this percentage then
2519 * slab reclaim needs to occur.
2521 int sysctl_min_slab_ratio
= 5;
2523 static inline unsigned long zone_unmapped_file_pages(struct zone
*zone
)
2525 unsigned long file_mapped
= zone_page_state(zone
, NR_FILE_MAPPED
);
2526 unsigned long file_lru
= zone_page_state(zone
, NR_INACTIVE_FILE
) +
2527 zone_page_state(zone
, NR_ACTIVE_FILE
);
2530 * It's possible for there to be more file mapped pages than
2531 * accounted for by the pages on the file LRU lists because
2532 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
2534 return (file_lru
> file_mapped
) ? (file_lru
- file_mapped
) : 0;
2537 /* Work out how many page cache pages we can reclaim in this reclaim_mode */
2538 static long zone_pagecache_reclaimable(struct zone
*zone
)
2540 long nr_pagecache_reclaimable
;
2544 * If RECLAIM_SWAP is set, then all file pages are considered
2545 * potentially reclaimable. Otherwise, we have to worry about
2546 * pages like swapcache and zone_unmapped_file_pages() provides
2549 if (zone_reclaim_mode
& RECLAIM_SWAP
)
2550 nr_pagecache_reclaimable
= zone_page_state(zone
, NR_FILE_PAGES
);
2552 nr_pagecache_reclaimable
= zone_unmapped_file_pages(zone
);
2554 /* If we can't clean pages, remove dirty pages from consideration */
2555 if (!(zone_reclaim_mode
& RECLAIM_WRITE
))
2556 delta
+= zone_page_state(zone
, NR_FILE_DIRTY
);
2558 /* Watch for any possible underflows due to delta */
2559 if (unlikely(delta
> nr_pagecache_reclaimable
))
2560 delta
= nr_pagecache_reclaimable
;
2562 return nr_pagecache_reclaimable
- delta
;
2566 * Try to free up some pages from this zone through reclaim.
2568 static int __zone_reclaim(struct zone
*zone
, gfp_t gfp_mask
, unsigned int order
)
2570 /* Minimum pages needed in order to stay on node */
2571 const unsigned long nr_pages
= 1 << order
;
2572 struct task_struct
*p
= current
;
2573 struct reclaim_state reclaim_state
;
2575 struct scan_control sc
= {
2576 .may_writepage
= !!(zone_reclaim_mode
& RECLAIM_WRITE
),
2577 .may_unmap
= !!(zone_reclaim_mode
& RECLAIM_SWAP
),
2579 .nr_to_reclaim
= max_t(unsigned long, nr_pages
,
2581 .gfp_mask
= gfp_mask
,
2582 .swappiness
= vm_swappiness
,
2584 .isolate_pages
= isolate_pages_global
,
2586 unsigned long slab_reclaimable
;
2588 disable_swap_token();
2591 * We need to be able to allocate from the reserves for RECLAIM_SWAP
2592 * and we also need to be able to write out pages for RECLAIM_WRITE
2595 p
->flags
|= PF_MEMALLOC
| PF_SWAPWRITE
;
2596 lockdep_set_current_reclaim_state(gfp_mask
);
2597 reclaim_state
.reclaimed_slab
= 0;
2598 p
->reclaim_state
= &reclaim_state
;
2600 if (zone_pagecache_reclaimable(zone
) > zone
->min_unmapped_pages
) {
2602 * Free memory by calling shrink zone with increasing
2603 * priorities until we have enough memory freed.
2605 priority
= ZONE_RECLAIM_PRIORITY
;
2607 note_zone_scanning_priority(zone
, priority
);
2608 shrink_zone(priority
, zone
, &sc
);
2610 } while (priority
>= 0 && sc
.nr_reclaimed
< nr_pages
);
2613 slab_reclaimable
= zone_page_state(zone
, NR_SLAB_RECLAIMABLE
);
2614 if (slab_reclaimable
> zone
->min_slab_pages
) {
2616 * shrink_slab() does not currently allow us to determine how
2617 * many pages were freed in this zone. So we take the current
2618 * number of slab pages and shake the slab until it is reduced
2619 * by the same nr_pages that we used for reclaiming unmapped
2622 * Note that shrink_slab will free memory on all zones and may
2625 while (shrink_slab(sc
.nr_scanned
, gfp_mask
, order
) &&
2626 zone_page_state(zone
, NR_SLAB_RECLAIMABLE
) >
2627 slab_reclaimable
- nr_pages
)
2631 * Update nr_reclaimed by the number of slab pages we
2632 * reclaimed from this zone.
2634 sc
.nr_reclaimed
+= slab_reclaimable
-
2635 zone_page_state(zone
, NR_SLAB_RECLAIMABLE
);
2638 p
->reclaim_state
= NULL
;
2639 current
->flags
&= ~(PF_MEMALLOC
| PF_SWAPWRITE
);
2640 lockdep_clear_current_reclaim_state();
2641 return sc
.nr_reclaimed
>= nr_pages
;
2644 int zone_reclaim(struct zone
*zone
, gfp_t gfp_mask
, unsigned int order
)
2650 * Zone reclaim reclaims unmapped file backed pages and
2651 * slab pages if we are over the defined limits.
2653 * A small portion of unmapped file backed pages is needed for
2654 * file I/O otherwise pages read by file I/O will be immediately
2655 * thrown out if the zone is overallocated. So we do not reclaim
2656 * if less than a specified percentage of the zone is used by
2657 * unmapped file backed pages.
2659 if (zone_pagecache_reclaimable(zone
) <= zone
->min_unmapped_pages
&&
2660 zone_page_state(zone
, NR_SLAB_RECLAIMABLE
) <= zone
->min_slab_pages
)
2661 return ZONE_RECLAIM_FULL
;
2663 if (zone
->all_unreclaimable
)
2664 return ZONE_RECLAIM_FULL
;
2667 * Do not scan if the allocation should not be delayed.
2669 if (!(gfp_mask
& __GFP_WAIT
) || (current
->flags
& PF_MEMALLOC
))
2670 return ZONE_RECLAIM_NOSCAN
;
2673 * Only run zone reclaim on the local zone or on zones that do not
2674 * have associated processors. This will favor the local processor
2675 * over remote processors and spread off node memory allocations
2676 * as wide as possible.
2678 node_id
= zone_to_nid(zone
);
2679 if (node_state(node_id
, N_CPU
) && node_id
!= numa_node_id())
2680 return ZONE_RECLAIM_NOSCAN
;
2682 if (zone_test_and_set_flag(zone
, ZONE_RECLAIM_LOCKED
))
2683 return ZONE_RECLAIM_NOSCAN
;
2685 ret
= __zone_reclaim(zone
, gfp_mask
, order
);
2686 zone_clear_flag(zone
, ZONE_RECLAIM_LOCKED
);
2689 count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED
);
2696 * page_evictable - test whether a page is evictable
2697 * @page: the page to test
2698 * @vma: the VMA in which the page is or will be mapped, may be NULL
2700 * Test whether page is evictable--i.e., should be placed on active/inactive
2701 * lists vs unevictable list. The vma argument is !NULL when called from the
2702 * fault path to determine how to instantate a new page.
2704 * Reasons page might not be evictable:
2705 * (1) page's mapping marked unevictable
2706 * (2) page is part of an mlocked VMA
2709 int page_evictable(struct page
*page
, struct vm_area_struct
*vma
)
2712 if (mapping_unevictable(page_mapping(page
)))
2715 if (PageMlocked(page
) || (vma
&& is_mlocked_vma(vma
, page
)))
2722 * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list
2723 * @page: page to check evictability and move to appropriate lru list
2724 * @zone: zone page is in
2726 * Checks a page for evictability and moves the page to the appropriate
2729 * Restrictions: zone->lru_lock must be held, page must be on LRU and must
2730 * have PageUnevictable set.
2732 static void check_move_unevictable_page(struct page
*page
, struct zone
*zone
)
2734 VM_BUG_ON(PageActive(page
));
2737 ClearPageUnevictable(page
);
2738 if (page_evictable(page
, NULL
)) {
2739 enum lru_list l
= page_lru_base_type(page
);
2741 __dec_zone_state(zone
, NR_UNEVICTABLE
);
2742 list_move(&page
->lru
, &zone
->lru
[l
].list
);
2743 mem_cgroup_move_lists(page
, LRU_UNEVICTABLE
, l
);
2744 __inc_zone_state(zone
, NR_INACTIVE_ANON
+ l
);
2745 __count_vm_event(UNEVICTABLE_PGRESCUED
);
2748 * rotate unevictable list
2750 SetPageUnevictable(page
);
2751 list_move(&page
->lru
, &zone
->lru
[LRU_UNEVICTABLE
].list
);
2752 mem_cgroup_rotate_lru_list(page
, LRU_UNEVICTABLE
);
2753 if (page_evictable(page
, NULL
))
2759 * scan_mapping_unevictable_pages - scan an address space for evictable pages
2760 * @mapping: struct address_space to scan for evictable pages
2762 * Scan all pages in mapping. Check unevictable pages for
2763 * evictability and move them to the appropriate zone lru list.
2765 void scan_mapping_unevictable_pages(struct address_space
*mapping
)
2768 pgoff_t end
= (i_size_read(mapping
->host
) + PAGE_CACHE_SIZE
- 1) >>
2771 struct pagevec pvec
;
2773 if (mapping
->nrpages
== 0)
2776 pagevec_init(&pvec
, 0);
2777 while (next
< end
&&
2778 pagevec_lookup(&pvec
, mapping
, next
, PAGEVEC_SIZE
)) {
2784 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
2785 struct page
*page
= pvec
.pages
[i
];
2786 pgoff_t page_index
= page
->index
;
2787 struct zone
*pagezone
= page_zone(page
);
2790 if (page_index
> next
)
2794 if (pagezone
!= zone
) {
2796 spin_unlock_irq(&zone
->lru_lock
);
2798 spin_lock_irq(&zone
->lru_lock
);
2801 if (PageLRU(page
) && PageUnevictable(page
))
2802 check_move_unevictable_page(page
, zone
);
2805 spin_unlock_irq(&zone
->lru_lock
);
2806 pagevec_release(&pvec
);
2808 count_vm_events(UNEVICTABLE_PGSCANNED
, pg_scanned
);
2814 * scan_zone_unevictable_pages - check unevictable list for evictable pages
2815 * @zone - zone of which to scan the unevictable list
2817 * Scan @zone's unevictable LRU lists to check for pages that have become
2818 * evictable. Move those that have to @zone's inactive list where they
2819 * become candidates for reclaim, unless shrink_inactive_zone() decides
2820 * to reactivate them. Pages that are still unevictable are rotated
2821 * back onto @zone's unevictable list.
2823 #define SCAN_UNEVICTABLE_BATCH_SIZE 16UL /* arbitrary lock hold batch size */
2824 static void scan_zone_unevictable_pages(struct zone
*zone
)
2826 struct list_head
*l_unevictable
= &zone
->lru
[LRU_UNEVICTABLE
].list
;
2828 unsigned long nr_to_scan
= zone_page_state(zone
, NR_UNEVICTABLE
);
2830 while (nr_to_scan
> 0) {
2831 unsigned long batch_size
= min(nr_to_scan
,
2832 SCAN_UNEVICTABLE_BATCH_SIZE
);
2834 spin_lock_irq(&zone
->lru_lock
);
2835 for (scan
= 0; scan
< batch_size
; scan
++) {
2836 struct page
*page
= lru_to_page(l_unevictable
);
2838 if (!trylock_page(page
))
2841 prefetchw_prev_lru_page(page
, l_unevictable
, flags
);
2843 if (likely(PageLRU(page
) && PageUnevictable(page
)))
2844 check_move_unevictable_page(page
, zone
);
2848 spin_unlock_irq(&zone
->lru_lock
);
2850 nr_to_scan
-= batch_size
;
2856 * scan_all_zones_unevictable_pages - scan all unevictable lists for evictable pages
2858 * A really big hammer: scan all zones' unevictable LRU lists to check for
2859 * pages that have become evictable. Move those back to the zones'
2860 * inactive list where they become candidates for reclaim.
2861 * This occurs when, e.g., we have unswappable pages on the unevictable lists,
2862 * and we add swap to the system. As such, it runs in the context of a task
2863 * that has possibly/probably made some previously unevictable pages
2866 static void scan_all_zones_unevictable_pages(void)
2870 for_each_zone(zone
) {
2871 scan_zone_unevictable_pages(zone
);
2876 * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of
2877 * all nodes' unevictable lists for evictable pages
2879 unsigned long scan_unevictable_pages
;
2881 int scan_unevictable_handler(struct ctl_table
*table
, int write
,
2882 void __user
*buffer
,
2883 size_t *length
, loff_t
*ppos
)
2885 proc_doulongvec_minmax(table
, write
, buffer
, length
, ppos
);
2887 if (write
&& *(unsigned long *)table
->data
)
2888 scan_all_zones_unevictable_pages();
2890 scan_unevictable_pages
= 0;
2895 * per node 'scan_unevictable_pages' attribute. On demand re-scan of
2896 * a specified node's per zone unevictable lists for evictable pages.
2899 static ssize_t
read_scan_unevictable_node(struct sys_device
*dev
,
2900 struct sysdev_attribute
*attr
,
2903 return sprintf(buf
, "0\n"); /* always zero; should fit... */
2906 static ssize_t
write_scan_unevictable_node(struct sys_device
*dev
,
2907 struct sysdev_attribute
*attr
,
2908 const char *buf
, size_t count
)
2910 struct zone
*node_zones
= NODE_DATA(dev
->id
)->node_zones
;
2913 unsigned long req
= strict_strtoul(buf
, 10, &res
);
2916 return 1; /* zero is no-op */
2918 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; ++zone
) {
2919 if (!populated_zone(zone
))
2921 scan_zone_unevictable_pages(zone
);
2927 static SYSDEV_ATTR(scan_unevictable_pages
, S_IRUGO
| S_IWUSR
,
2928 read_scan_unevictable_node
,
2929 write_scan_unevictable_node
);
2931 int scan_unevictable_register_node(struct node
*node
)
2933 return sysdev_create_file(&node
->sysdev
, &attr_scan_unevictable_pages
);
2936 void scan_unevictable_unregister_node(struct node
*node
)
2938 sysdev_remove_file(&node
->sysdev
, &attr_scan_unevictable_pages
);