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/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>
42 #include <asm/tlbflush.h>
43 #include <asm/div64.h>
45 #include <linux/swapops.h>
50 /* Incremented by the number of inactive pages that were scanned */
51 unsigned long nr_scanned
;
53 /* This context's GFP mask */
58 /* Can pages be swapped as part of reclaim? */
61 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
62 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
63 * In this context, it doesn't matter that we scan the
64 * whole list at once. */
69 int all_unreclaimable
;
74 * Pages that have (or should have) IO pending. If we run into
75 * a lot of these, we're better off waiting a little for IO to
76 * finish rather than scanning more pages in the VM.
80 /* Which cgroup do we reclaim from */
81 struct mem_cgroup
*mem_cgroup
;
83 /* Pluggable isolate pages callback */
84 unsigned long (*isolate_pages
)(unsigned long nr
, struct list_head
*dst
,
85 unsigned long *scanned
, int order
, int mode
,
86 struct zone
*z
, struct mem_cgroup
*mem_cont
,
90 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
92 #ifdef ARCH_HAS_PREFETCH
93 #define prefetch_prev_lru_page(_page, _base, _field) \
95 if ((_page)->lru.prev != _base) { \
98 prev = lru_to_page(&(_page->lru)); \
99 prefetch(&prev->_field); \
103 #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
106 #ifdef ARCH_HAS_PREFETCHW
107 #define prefetchw_prev_lru_page(_page, _base, _field) \
109 if ((_page)->lru.prev != _base) { \
112 prev = lru_to_page(&(_page->lru)); \
113 prefetchw(&prev->_field); \
117 #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
121 * From 0 .. 100. Higher means more swappy.
123 int vm_swappiness
= 60;
124 long vm_total_pages
; /* The total number of pages which the VM controls */
126 static LIST_HEAD(shrinker_list
);
127 static DECLARE_RWSEM(shrinker_rwsem
);
130 * Add a shrinker callback to be called from the vm
132 void register_shrinker(struct shrinker
*shrinker
)
135 down_write(&shrinker_rwsem
);
136 list_add_tail(&shrinker
->list
, &shrinker_list
);
137 up_write(&shrinker_rwsem
);
139 EXPORT_SYMBOL(register_shrinker
);
144 void unregister_shrinker(struct shrinker
*shrinker
)
146 down_write(&shrinker_rwsem
);
147 list_del(&shrinker
->list
);
148 up_write(&shrinker_rwsem
);
150 EXPORT_SYMBOL(unregister_shrinker
);
152 #define SHRINK_BATCH 128
154 * Call the shrink functions to age shrinkable caches
156 * Here we assume it costs one seek to replace a lru page and that it also
157 * takes a seek to recreate a cache object. With this in mind we age equal
158 * percentages of the lru and ageable caches. This should balance the seeks
159 * generated by these structures.
161 * If the vm encountered mapped pages on the LRU it increase the pressure on
162 * slab to avoid swapping.
164 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
166 * `lru_pages' represents the number of on-LRU pages in all the zones which
167 * are eligible for the caller's allocation attempt. It is used for balancing
168 * slab reclaim versus page reclaim.
170 * Returns the number of slab objects which we shrunk.
172 unsigned long shrink_slab(unsigned long scanned
, gfp_t gfp_mask
,
173 unsigned long lru_pages
)
175 struct shrinker
*shrinker
;
176 unsigned long ret
= 0;
179 scanned
= SWAP_CLUSTER_MAX
;
181 if (!down_read_trylock(&shrinker_rwsem
))
182 return 1; /* Assume we'll be able to shrink next time */
184 list_for_each_entry(shrinker
, &shrinker_list
, list
) {
185 unsigned long long delta
;
186 unsigned long total_scan
;
187 unsigned long max_pass
= (*shrinker
->shrink
)(0, gfp_mask
);
189 delta
= (4 * scanned
) / shrinker
->seeks
;
191 do_div(delta
, lru_pages
+ 1);
192 shrinker
->nr
+= delta
;
193 if (shrinker
->nr
< 0) {
194 printk(KERN_ERR
"%s: nr=%ld\n",
195 __FUNCTION__
, shrinker
->nr
);
196 shrinker
->nr
= max_pass
;
200 * Avoid risking looping forever due to too large nr value:
201 * never try to free more than twice the estimate number of
204 if (shrinker
->nr
> max_pass
* 2)
205 shrinker
->nr
= max_pass
* 2;
207 total_scan
= shrinker
->nr
;
210 while (total_scan
>= SHRINK_BATCH
) {
211 long this_scan
= SHRINK_BATCH
;
215 nr_before
= (*shrinker
->shrink
)(0, gfp_mask
);
216 shrink_ret
= (*shrinker
->shrink
)(this_scan
, gfp_mask
);
217 if (shrink_ret
== -1)
219 if (shrink_ret
< nr_before
)
220 ret
+= nr_before
- shrink_ret
;
221 count_vm_events(SLABS_SCANNED
, this_scan
);
222 total_scan
-= this_scan
;
227 shrinker
->nr
+= total_scan
;
229 up_read(&shrinker_rwsem
);
233 /* Called without lock on whether page is mapped, so answer is unstable */
234 static inline int page_mapping_inuse(struct page
*page
)
236 struct address_space
*mapping
;
238 /* Page is in somebody's page tables. */
239 if (page_mapped(page
))
242 /* Be more reluctant to reclaim swapcache than pagecache */
243 if (PageSwapCache(page
))
246 mapping
= page_mapping(page
);
250 /* File is mmap'd by somebody? */
251 return mapping_mapped(mapping
);
254 static inline int is_page_cache_freeable(struct page
*page
)
256 return page_count(page
) - !!PagePrivate(page
) == 2;
259 static int may_write_to_queue(struct backing_dev_info
*bdi
)
261 if (current
->flags
& PF_SWAPWRITE
)
263 if (!bdi_write_congested(bdi
))
265 if (bdi
== current
->backing_dev_info
)
271 * We detected a synchronous write error writing a page out. Probably
272 * -ENOSPC. We need to propagate that into the address_space for a subsequent
273 * fsync(), msync() or close().
275 * The tricky part is that after writepage we cannot touch the mapping: nothing
276 * prevents it from being freed up. But we have a ref on the page and once
277 * that page is locked, the mapping is pinned.
279 * We're allowed to run sleeping lock_page() here because we know the caller has
282 static void handle_write_error(struct address_space
*mapping
,
283 struct page
*page
, int error
)
286 if (page_mapping(page
) == mapping
)
287 mapping_set_error(mapping
, error
);
291 /* Request for sync pageout. */
297 /* possible outcome of pageout() */
299 /* failed to write page out, page is locked */
301 /* move page to the active list, page is locked */
303 /* page has been sent to the disk successfully, page is unlocked */
305 /* page is clean and locked */
310 * pageout is called by shrink_page_list() for each dirty page.
311 * Calls ->writepage().
313 static pageout_t
pageout(struct page
*page
, struct address_space
*mapping
,
314 enum pageout_io sync_writeback
)
317 * If the page is dirty, only perform writeback if that write
318 * will be non-blocking. To prevent this allocation from being
319 * stalled by pagecache activity. But note that there may be
320 * stalls if we need to run get_block(). We could test
321 * PagePrivate for that.
323 * If this process is currently in generic_file_write() against
324 * this page's queue, we can perform writeback even if that
327 * If the page is swapcache, write it back even if that would
328 * block, for some throttling. This happens by accident, because
329 * swap_backing_dev_info is bust: it doesn't reflect the
330 * congestion state of the swapdevs. Easy to fix, if needed.
331 * See swapfile.c:page_queue_congested().
333 if (!is_page_cache_freeable(page
))
337 * Some data journaling orphaned pages can have
338 * page->mapping == NULL while being dirty with clean buffers.
340 if (PagePrivate(page
)) {
341 if (try_to_free_buffers(page
)) {
342 ClearPageDirty(page
);
343 printk("%s: orphaned page\n", __FUNCTION__
);
349 if (mapping
->a_ops
->writepage
== NULL
)
350 return PAGE_ACTIVATE
;
351 if (!may_write_to_queue(mapping
->backing_dev_info
))
354 if (clear_page_dirty_for_io(page
)) {
356 struct writeback_control wbc
= {
357 .sync_mode
= WB_SYNC_NONE
,
358 .nr_to_write
= SWAP_CLUSTER_MAX
,
360 .range_end
= LLONG_MAX
,
365 SetPageReclaim(page
);
366 res
= mapping
->a_ops
->writepage(page
, &wbc
);
368 handle_write_error(mapping
, page
, res
);
369 if (res
== AOP_WRITEPAGE_ACTIVATE
) {
370 ClearPageReclaim(page
);
371 return PAGE_ACTIVATE
;
375 * Wait on writeback if requested to. This happens when
376 * direct reclaiming a large contiguous area and the
377 * first attempt to free a range of pages fails.
379 if (PageWriteback(page
) && sync_writeback
== PAGEOUT_IO_SYNC
)
380 wait_on_page_writeback(page
);
382 if (!PageWriteback(page
)) {
383 /* synchronous write or broken a_ops? */
384 ClearPageReclaim(page
);
386 inc_zone_page_state(page
, NR_VMSCAN_WRITE
);
394 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
395 * someone else has a ref on the page, abort and return 0. If it was
396 * successfully detached, return 1. Assumes the caller has a single ref on
399 int remove_mapping(struct address_space
*mapping
, struct page
*page
)
401 BUG_ON(!PageLocked(page
));
402 BUG_ON(mapping
!= page_mapping(page
));
404 write_lock_irq(&mapping
->tree_lock
);
406 * The non racy check for a busy page.
408 * Must be careful with the order of the tests. When someone has
409 * a ref to the page, it may be possible that they dirty it then
410 * drop the reference. So if PageDirty is tested before page_count
411 * here, then the following race may occur:
413 * get_user_pages(&page);
414 * [user mapping goes away]
416 * !PageDirty(page) [good]
417 * SetPageDirty(page);
419 * !page_count(page) [good, discard it]
421 * [oops, our write_to data is lost]
423 * Reversing the order of the tests ensures such a situation cannot
424 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
425 * load is not satisfied before that of page->_count.
427 * Note that if SetPageDirty is always performed via set_page_dirty,
428 * and thus under tree_lock, then this ordering is not required.
430 if (unlikely(page_count(page
) != 2))
433 if (unlikely(PageDirty(page
)))
436 if (PageSwapCache(page
)) {
437 swp_entry_t swap
= { .val
= page_private(page
) };
438 __delete_from_swap_cache(page
);
439 write_unlock_irq(&mapping
->tree_lock
);
441 __put_page(page
); /* The pagecache ref */
445 __remove_from_page_cache(page
);
446 write_unlock_irq(&mapping
->tree_lock
);
451 write_unlock_irq(&mapping
->tree_lock
);
456 * shrink_page_list() returns the number of reclaimed pages
458 static unsigned long shrink_page_list(struct list_head
*page_list
,
459 struct scan_control
*sc
,
460 enum pageout_io sync_writeback
)
462 LIST_HEAD(ret_pages
);
463 struct pagevec freed_pvec
;
465 unsigned long nr_reclaimed
= 0;
469 pagevec_init(&freed_pvec
, 1);
470 while (!list_empty(page_list
)) {
471 struct address_space
*mapping
;
478 page
= lru_to_page(page_list
);
479 list_del(&page
->lru
);
481 if (TestSetPageLocked(page
))
484 VM_BUG_ON(PageActive(page
));
488 if (!sc
->may_swap
&& page_mapped(page
))
491 /* Double the slab pressure for mapped and swapcache pages */
492 if (page_mapped(page
) || PageSwapCache(page
))
495 may_enter_fs
= (sc
->gfp_mask
& __GFP_FS
) ||
496 (PageSwapCache(page
) && (sc
->gfp_mask
& __GFP_IO
));
498 if (PageWriteback(page
)) {
500 * Synchronous reclaim is performed in two passes,
501 * first an asynchronous pass over the list to
502 * start parallel writeback, and a second synchronous
503 * pass to wait for the IO to complete. Wait here
504 * for any page for which writeback has already
507 if (sync_writeback
== PAGEOUT_IO_SYNC
&& may_enter_fs
)
508 wait_on_page_writeback(page
);
515 referenced
= page_referenced(page
, 1, sc
->mem_cgroup
);
516 /* In active use or really unfreeable? Activate it. */
517 if (sc
->order
<= PAGE_ALLOC_COSTLY_ORDER
&&
518 referenced
&& page_mapping_inuse(page
))
519 goto activate_locked
;
523 * Anonymous process memory has backing store?
524 * Try to allocate it some swap space here.
526 if (PageAnon(page
) && !PageSwapCache(page
))
527 if (!add_to_swap(page
, GFP_ATOMIC
))
528 goto activate_locked
;
529 #endif /* CONFIG_SWAP */
531 mapping
= page_mapping(page
);
534 * The page is mapped into the page tables of one or more
535 * processes. Try to unmap it here.
537 if (page_mapped(page
) && mapping
) {
538 switch (try_to_unmap(page
, 0)) {
540 goto activate_locked
;
544 ; /* try to free the page below */
548 if (PageDirty(page
)) {
549 if (sc
->order
<= PAGE_ALLOC_COSTLY_ORDER
&& referenced
)
555 if (!sc
->may_writepage
)
558 /* Page is dirty, try to write it out here */
559 switch (pageout(page
, mapping
, sync_writeback
)) {
563 goto activate_locked
;
565 if (PageWriteback(page
) || PageDirty(page
)) {
570 * A synchronous write - probably a ramdisk. Go
571 * ahead and try to reclaim the page.
573 if (TestSetPageLocked(page
))
575 if (PageDirty(page
) || PageWriteback(page
))
577 mapping
= page_mapping(page
);
579 ; /* try to free the page below */
584 * If the page has buffers, try to free the buffer mappings
585 * associated with this page. If we succeed we try to free
588 * We do this even if the page is PageDirty().
589 * try_to_release_page() does not perform I/O, but it is
590 * possible for a page to have PageDirty set, but it is actually
591 * clean (all its buffers are clean). This happens if the
592 * buffers were written out directly, with submit_bh(). ext3
593 * will do this, as well as the blockdev mapping.
594 * try_to_release_page() will discover that cleanness and will
595 * drop the buffers and mark the page clean - it can be freed.
597 * Rarely, pages can have buffers and no ->mapping. These are
598 * the pages which were not successfully invalidated in
599 * truncate_complete_page(). We try to drop those buffers here
600 * and if that worked, and the page is no longer mapped into
601 * process address space (page_count == 1) it can be freed.
602 * Otherwise, leave the page on the LRU so it is swappable.
604 if (PagePrivate(page
)) {
605 if (!try_to_release_page(page
, sc
->gfp_mask
))
606 goto activate_locked
;
607 if (!mapping
&& page_count(page
) == 1)
611 if (!mapping
|| !remove_mapping(mapping
, page
))
617 if (!pagevec_add(&freed_pvec
, page
))
618 __pagevec_release_nonlru(&freed_pvec
);
627 list_add(&page
->lru
, &ret_pages
);
628 VM_BUG_ON(PageLRU(page
));
630 list_splice(&ret_pages
, page_list
);
631 if (pagevec_count(&freed_pvec
))
632 __pagevec_release_nonlru(&freed_pvec
);
633 count_vm_events(PGACTIVATE
, pgactivate
);
637 /* LRU Isolation modes. */
638 #define ISOLATE_INACTIVE 0 /* Isolate inactive pages. */
639 #define ISOLATE_ACTIVE 1 /* Isolate active pages. */
640 #define ISOLATE_BOTH 2 /* Isolate both active and inactive pages. */
643 * Attempt to remove the specified page from its LRU. Only take this page
644 * if it is of the appropriate PageActive status. Pages which are being
645 * freed elsewhere are also ignored.
647 * page: page to consider
648 * mode: one of the LRU isolation modes defined above
650 * returns 0 on success, -ve errno on failure.
652 int __isolate_lru_page(struct page
*page
, int mode
)
656 /* Only take pages on the LRU. */
661 * When checking the active state, we need to be sure we are
662 * dealing with comparible boolean values. Take the logical not
665 if (mode
!= ISOLATE_BOTH
&& (!PageActive(page
) != !mode
))
669 if (likely(get_page_unless_zero(page
))) {
671 * Be careful not to clear PageLRU until after we're
672 * sure the page is not being freed elsewhere -- the
673 * page release code relies on it.
683 * zone->lru_lock is heavily contended. Some of the functions that
684 * shrink the lists perform better by taking out a batch of pages
685 * and working on them outside the LRU lock.
687 * For pagecache intensive workloads, this function is the hottest
688 * spot in the kernel (apart from copy_*_user functions).
690 * Appropriate locks must be held before calling this function.
692 * @nr_to_scan: The number of pages to look through on the list.
693 * @src: The LRU list to pull pages off.
694 * @dst: The temp list to put pages on to.
695 * @scanned: The number of pages that were scanned.
696 * @order: The caller's attempted allocation order
697 * @mode: One of the LRU isolation modes
699 * returns how many pages were moved onto *@dst.
701 static unsigned long isolate_lru_pages(unsigned long nr_to_scan
,
702 struct list_head
*src
, struct list_head
*dst
,
703 unsigned long *scanned
, int order
, int mode
)
705 unsigned long nr_taken
= 0;
708 for (scan
= 0; scan
< nr_to_scan
&& !list_empty(src
); scan
++) {
711 unsigned long end_pfn
;
712 unsigned long page_pfn
;
715 page
= lru_to_page(src
);
716 prefetchw_prev_lru_page(page
, src
, flags
);
718 VM_BUG_ON(!PageLRU(page
));
720 switch (__isolate_lru_page(page
, mode
)) {
722 list_move(&page
->lru
, dst
);
727 /* else it is being freed elsewhere */
728 list_move(&page
->lru
, src
);
739 * Attempt to take all pages in the order aligned region
740 * surrounding the tag page. Only take those pages of
741 * the same active state as that tag page. We may safely
742 * round the target page pfn down to the requested order
743 * as the mem_map is guarenteed valid out to MAX_ORDER,
744 * where that page is in a different zone we will detect
745 * it from its zone id and abort this block scan.
747 zone_id
= page_zone_id(page
);
748 page_pfn
= page_to_pfn(page
);
749 pfn
= page_pfn
& ~((1 << order
) - 1);
750 end_pfn
= pfn
+ (1 << order
);
751 for (; pfn
< end_pfn
; pfn
++) {
752 struct page
*cursor_page
;
754 /* The target page is in the block, ignore it. */
755 if (unlikely(pfn
== page_pfn
))
758 /* Avoid holes within the zone. */
759 if (unlikely(!pfn_valid_within(pfn
)))
762 cursor_page
= pfn_to_page(pfn
);
763 /* Check that we have not crossed a zone boundary. */
764 if (unlikely(page_zone_id(cursor_page
) != zone_id
))
766 switch (__isolate_lru_page(cursor_page
, mode
)) {
768 list_move(&cursor_page
->lru
, dst
);
774 /* else it is being freed elsewhere */
775 list_move(&cursor_page
->lru
, src
);
786 static unsigned long isolate_pages_global(unsigned long nr
,
787 struct list_head
*dst
,
788 unsigned long *scanned
, int order
,
789 int mode
, struct zone
*z
,
790 struct mem_cgroup
*mem_cont
,
794 return isolate_lru_pages(nr
, &z
->active_list
, dst
,
795 scanned
, order
, mode
);
797 return isolate_lru_pages(nr
, &z
->inactive_list
, dst
,
798 scanned
, order
, mode
);
802 * clear_active_flags() is a helper for shrink_active_list(), clearing
803 * any active bits from the pages in the list.
805 static unsigned long clear_active_flags(struct list_head
*page_list
)
810 list_for_each_entry(page
, page_list
, lru
)
811 if (PageActive(page
)) {
812 ClearPageActive(page
);
820 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
823 static unsigned long shrink_inactive_list(unsigned long max_scan
,
824 struct zone
*zone
, struct scan_control
*sc
)
826 LIST_HEAD(page_list
);
828 unsigned long nr_scanned
= 0;
829 unsigned long nr_reclaimed
= 0;
831 pagevec_init(&pvec
, 1);
834 spin_lock_irq(&zone
->lru_lock
);
837 unsigned long nr_taken
;
838 unsigned long nr_scan
;
839 unsigned long nr_freed
;
840 unsigned long nr_active
;
842 nr_taken
= sc
->isolate_pages(sc
->swap_cluster_max
,
843 &page_list
, &nr_scan
, sc
->order
,
844 (sc
->order
> PAGE_ALLOC_COSTLY_ORDER
)?
845 ISOLATE_BOTH
: ISOLATE_INACTIVE
,
846 zone
, sc
->mem_cgroup
, 0);
847 nr_active
= clear_active_flags(&page_list
);
848 __count_vm_events(PGDEACTIVATE
, nr_active
);
850 __mod_zone_page_state(zone
, NR_ACTIVE
, -nr_active
);
851 __mod_zone_page_state(zone
, NR_INACTIVE
,
852 -(nr_taken
- nr_active
));
853 zone
->pages_scanned
+= nr_scan
;
854 spin_unlock_irq(&zone
->lru_lock
);
856 nr_scanned
+= nr_scan
;
857 nr_freed
= shrink_page_list(&page_list
, sc
, PAGEOUT_IO_ASYNC
);
860 * If we are direct reclaiming for contiguous pages and we do
861 * not reclaim everything in the list, try again and wait
862 * for IO to complete. This will stall high-order allocations
863 * but that should be acceptable to the caller
865 if (nr_freed
< nr_taken
&& !current_is_kswapd() &&
866 sc
->order
> PAGE_ALLOC_COSTLY_ORDER
) {
867 congestion_wait(WRITE
, HZ
/10);
870 * The attempt at page out may have made some
871 * of the pages active, mark them inactive again.
873 nr_active
= clear_active_flags(&page_list
);
874 count_vm_events(PGDEACTIVATE
, nr_active
);
876 nr_freed
+= shrink_page_list(&page_list
, sc
,
880 nr_reclaimed
+= nr_freed
;
882 if (current_is_kswapd()) {
883 __count_zone_vm_events(PGSCAN_KSWAPD
, zone
, nr_scan
);
884 __count_vm_events(KSWAPD_STEAL
, nr_freed
);
886 __count_zone_vm_events(PGSCAN_DIRECT
, zone
, nr_scan
);
887 __count_zone_vm_events(PGSTEAL
, zone
, nr_freed
);
892 spin_lock(&zone
->lru_lock
);
894 * Put back any unfreeable pages.
896 while (!list_empty(&page_list
)) {
897 page
= lru_to_page(&page_list
);
898 VM_BUG_ON(PageLRU(page
));
900 list_del(&page
->lru
);
901 if (PageActive(page
))
902 add_page_to_active_list(zone
, page
);
904 add_page_to_inactive_list(zone
, page
);
905 if (!pagevec_add(&pvec
, page
)) {
906 spin_unlock_irq(&zone
->lru_lock
);
907 __pagevec_release(&pvec
);
908 spin_lock_irq(&zone
->lru_lock
);
911 } while (nr_scanned
< max_scan
);
912 spin_unlock(&zone
->lru_lock
);
915 pagevec_release(&pvec
);
920 * We are about to scan this zone at a certain priority level. If that priority
921 * level is smaller (ie: more urgent) than the previous priority, then note
922 * that priority level within the zone. This is done so that when the next
923 * process comes in to scan this zone, it will immediately start out at this
924 * priority level rather than having to build up its own scanning priority.
925 * Here, this priority affects only the reclaim-mapped threshold.
927 static inline void note_zone_scanning_priority(struct zone
*zone
, int priority
)
929 if (priority
< zone
->prev_priority
)
930 zone
->prev_priority
= priority
;
933 static inline int zone_is_near_oom(struct zone
*zone
)
935 return zone
->pages_scanned
>= (zone_page_state(zone
, NR_ACTIVE
)
936 + zone_page_state(zone
, NR_INACTIVE
))*3;
940 * This moves pages from the active list to the inactive list.
942 * We move them the other way if the page is referenced by one or more
943 * processes, from rmap.
945 * If the pages are mostly unmapped, the processing is fast and it is
946 * appropriate to hold zone->lru_lock across the whole operation. But if
947 * the pages are mapped, the processing is slow (page_referenced()) so we
948 * should drop zone->lru_lock around each page. It's impossible to balance
949 * this, so instead we remove the pages from the LRU while processing them.
950 * It is safe to rely on PG_active against the non-LRU pages in here because
951 * nobody will play with that bit on a non-LRU page.
953 * The downside is that we have to touch page->_count against each page.
954 * But we had to alter page->flags anyway.
956 static void shrink_active_list(unsigned long nr_pages
, struct zone
*zone
,
957 struct scan_control
*sc
, int priority
)
959 unsigned long pgmoved
;
960 int pgdeactivate
= 0;
961 unsigned long pgscanned
;
962 LIST_HEAD(l_hold
); /* The pages which were snipped off */
963 LIST_HEAD(l_inactive
); /* Pages to go onto the inactive_list */
964 LIST_HEAD(l_active
); /* Pages to go onto the active_list */
967 int reclaim_mapped
= 0;
975 if (zone_is_near_oom(zone
))
976 goto force_reclaim_mapped
;
979 * `distress' is a measure of how much trouble we're having
980 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
982 distress
= 100 >> min(zone
->prev_priority
, priority
);
985 * The point of this algorithm is to decide when to start
986 * reclaiming mapped memory instead of just pagecache. Work out
990 mapped_ratio
= ((global_page_state(NR_FILE_MAPPED
) +
991 global_page_state(NR_ANON_PAGES
)) * 100) /
995 * Now decide how much we really want to unmap some pages. The
996 * mapped ratio is downgraded - just because there's a lot of
997 * mapped memory doesn't necessarily mean that page reclaim
1000 * The distress ratio is important - we don't want to start
1003 * A 100% value of vm_swappiness overrides this algorithm
1006 swap_tendency
= mapped_ratio
/ 2 + distress
+ sc
->swappiness
;
1009 * If there's huge imbalance between active and inactive
1010 * (think active 100 times larger than inactive) we should
1011 * become more permissive, or the system will take too much
1012 * cpu before it start swapping during memory pressure.
1013 * Distress is about avoiding early-oom, this is about
1014 * making swappiness graceful despite setting it to low
1017 * Avoid div by zero with nr_inactive+1, and max resulting
1018 * value is vm_total_pages.
1020 imbalance
= zone_page_state(zone
, NR_ACTIVE
);
1021 imbalance
/= zone_page_state(zone
, NR_INACTIVE
) + 1;
1024 * Reduce the effect of imbalance if swappiness is low,
1025 * this means for a swappiness very low, the imbalance
1026 * must be much higher than 100 for this logic to make
1029 * Max temporary value is vm_total_pages*100.
1031 imbalance
*= (vm_swappiness
+ 1);
1035 * If not much of the ram is mapped, makes the imbalance
1036 * less relevant, it's high priority we refill the inactive
1037 * list with mapped pages only in presence of high ratio of
1040 * Max temporary value is vm_total_pages*100.
1042 imbalance
*= mapped_ratio
;
1045 /* apply imbalance feedback to swap_tendency */
1046 swap_tendency
+= imbalance
;
1049 * Now use this metric to decide whether to start moving mapped
1050 * memory onto the inactive list.
1052 if (swap_tendency
>= 100)
1053 force_reclaim_mapped
:
1058 spin_lock_irq(&zone
->lru_lock
);
1059 pgmoved
= sc
->isolate_pages(nr_pages
, &l_hold
, &pgscanned
, sc
->order
,
1060 ISOLATE_ACTIVE
, zone
,
1062 zone
->pages_scanned
+= pgscanned
;
1063 __mod_zone_page_state(zone
, NR_ACTIVE
, -pgmoved
);
1064 spin_unlock_irq(&zone
->lru_lock
);
1066 while (!list_empty(&l_hold
)) {
1068 page
= lru_to_page(&l_hold
);
1069 list_del(&page
->lru
);
1070 if (page_mapped(page
)) {
1071 if (!reclaim_mapped
||
1072 (total_swap_pages
== 0 && PageAnon(page
)) ||
1073 page_referenced(page
, 0, sc
->mem_cgroup
)) {
1074 list_add(&page
->lru
, &l_active
);
1078 list_add(&page
->lru
, &l_inactive
);
1081 pagevec_init(&pvec
, 1);
1083 spin_lock_irq(&zone
->lru_lock
);
1084 while (!list_empty(&l_inactive
)) {
1085 page
= lru_to_page(&l_inactive
);
1086 prefetchw_prev_lru_page(page
, &l_inactive
, flags
);
1087 VM_BUG_ON(PageLRU(page
));
1089 VM_BUG_ON(!PageActive(page
));
1090 ClearPageActive(page
);
1092 list_move(&page
->lru
, &zone
->inactive_list
);
1093 mem_cgroup_move_lists(page_get_page_cgroup(page
), false);
1095 if (!pagevec_add(&pvec
, page
)) {
1096 __mod_zone_page_state(zone
, NR_INACTIVE
, pgmoved
);
1097 spin_unlock_irq(&zone
->lru_lock
);
1098 pgdeactivate
+= pgmoved
;
1100 if (buffer_heads_over_limit
)
1101 pagevec_strip(&pvec
);
1102 __pagevec_release(&pvec
);
1103 spin_lock_irq(&zone
->lru_lock
);
1106 __mod_zone_page_state(zone
, NR_INACTIVE
, pgmoved
);
1107 pgdeactivate
+= pgmoved
;
1108 if (buffer_heads_over_limit
) {
1109 spin_unlock_irq(&zone
->lru_lock
);
1110 pagevec_strip(&pvec
);
1111 spin_lock_irq(&zone
->lru_lock
);
1115 while (!list_empty(&l_active
)) {
1116 page
= lru_to_page(&l_active
);
1117 prefetchw_prev_lru_page(page
, &l_active
, flags
);
1118 VM_BUG_ON(PageLRU(page
));
1120 VM_BUG_ON(!PageActive(page
));
1121 list_move(&page
->lru
, &zone
->active_list
);
1122 mem_cgroup_move_lists(page_get_page_cgroup(page
), true);
1124 if (!pagevec_add(&pvec
, page
)) {
1125 __mod_zone_page_state(zone
, NR_ACTIVE
, pgmoved
);
1127 spin_unlock_irq(&zone
->lru_lock
);
1128 __pagevec_release(&pvec
);
1129 spin_lock_irq(&zone
->lru_lock
);
1132 __mod_zone_page_state(zone
, NR_ACTIVE
, pgmoved
);
1134 __count_zone_vm_events(PGREFILL
, zone
, pgscanned
);
1135 __count_vm_events(PGDEACTIVATE
, pgdeactivate
);
1136 spin_unlock_irq(&zone
->lru_lock
);
1138 pagevec_release(&pvec
);
1142 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1144 static unsigned long shrink_zone(int priority
, struct zone
*zone
,
1145 struct scan_control
*sc
)
1147 unsigned long nr_active
;
1148 unsigned long nr_inactive
;
1149 unsigned long nr_to_scan
;
1150 unsigned long nr_reclaimed
= 0;
1153 * Add one to `nr_to_scan' just to make sure that the kernel will
1154 * slowly sift through the active list.
1156 zone
->nr_scan_active
+=
1157 (zone_page_state(zone
, NR_ACTIVE
) >> priority
) + 1;
1158 nr_active
= zone
->nr_scan_active
;
1159 if (nr_active
>= sc
->swap_cluster_max
)
1160 zone
->nr_scan_active
= 0;
1164 zone
->nr_scan_inactive
+=
1165 (zone_page_state(zone
, NR_INACTIVE
) >> priority
) + 1;
1166 nr_inactive
= zone
->nr_scan_inactive
;
1167 if (nr_inactive
>= sc
->swap_cluster_max
)
1168 zone
->nr_scan_inactive
= 0;
1172 while (nr_active
|| nr_inactive
) {
1174 nr_to_scan
= min(nr_active
,
1175 (unsigned long)sc
->swap_cluster_max
);
1176 nr_active
-= nr_to_scan
;
1177 shrink_active_list(nr_to_scan
, zone
, sc
, priority
);
1181 nr_to_scan
= min(nr_inactive
,
1182 (unsigned long)sc
->swap_cluster_max
);
1183 nr_inactive
-= nr_to_scan
;
1184 nr_reclaimed
+= shrink_inactive_list(nr_to_scan
, zone
,
1189 throttle_vm_writeout(sc
->gfp_mask
);
1190 return nr_reclaimed
;
1194 * This is the direct reclaim path, for page-allocating processes. We only
1195 * try to reclaim pages from zones which will satisfy the caller's allocation
1198 * We reclaim from a zone even if that zone is over pages_high. Because:
1199 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1201 * b) The zones may be over pages_high but they must go *over* pages_high to
1202 * satisfy the `incremental min' zone defense algorithm.
1204 * Returns the number of reclaimed pages.
1206 * If a zone is deemed to be full of pinned pages then just give it a light
1207 * scan then give up on it.
1209 static unsigned long shrink_zones(int priority
, struct zone
**zones
,
1210 struct scan_control
*sc
)
1212 unsigned long nr_reclaimed
= 0;
1215 sc
->all_unreclaimable
= 1;
1216 for (i
= 0; zones
[i
] != NULL
; i
++) {
1217 struct zone
*zone
= zones
[i
];
1219 if (!populated_zone(zone
))
1222 if (!cpuset_zone_allowed_hardwall(zone
, GFP_KERNEL
))
1225 note_zone_scanning_priority(zone
, priority
);
1227 if (zone_is_all_unreclaimable(zone
) && priority
!= DEF_PRIORITY
)
1228 continue; /* Let kswapd poll it */
1230 sc
->all_unreclaimable
= 0;
1232 nr_reclaimed
+= shrink_zone(priority
, zone
, sc
);
1234 return nr_reclaimed
;
1238 * This is the main entry point to direct page reclaim.
1240 * If a full scan of the inactive list fails to free enough memory then we
1241 * are "out of memory" and something needs to be killed.
1243 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1244 * high - the zone may be full of dirty or under-writeback pages, which this
1245 * caller can't do much about. We kick pdflush and take explicit naps in the
1246 * hope that some of these pages can be written. But if the allocating task
1247 * holds filesystem locks which prevent writeout this might not work, and the
1248 * allocation attempt will fail.
1250 static unsigned long do_try_to_free_pages(struct zone
**zones
, gfp_t gfp_mask
,
1251 struct scan_control
*sc
)
1255 unsigned long total_scanned
= 0;
1256 unsigned long nr_reclaimed
= 0;
1257 struct reclaim_state
*reclaim_state
= current
->reclaim_state
;
1258 unsigned long lru_pages
= 0;
1261 count_vm_event(ALLOCSTALL
);
1263 for (i
= 0; zones
[i
] != NULL
; i
++) {
1264 struct zone
*zone
= zones
[i
];
1266 if (!cpuset_zone_allowed_hardwall(zone
, GFP_KERNEL
))
1269 lru_pages
+= zone_page_state(zone
, NR_ACTIVE
)
1270 + zone_page_state(zone
, NR_INACTIVE
);
1273 for (priority
= DEF_PRIORITY
; priority
>= 0; priority
--) {
1275 sc
->nr_io_pages
= 0;
1277 disable_swap_token();
1278 nr_reclaimed
+= shrink_zones(priority
, zones
, sc
);
1280 * Don't shrink slabs when reclaiming memory from
1281 * over limit cgroups
1283 if (sc
->mem_cgroup
== NULL
)
1284 shrink_slab(sc
->nr_scanned
, gfp_mask
, lru_pages
);
1285 if (reclaim_state
) {
1286 nr_reclaimed
+= reclaim_state
->reclaimed_slab
;
1287 reclaim_state
->reclaimed_slab
= 0;
1289 total_scanned
+= sc
->nr_scanned
;
1290 if (nr_reclaimed
>= sc
->swap_cluster_max
) {
1296 * Try to write back as many pages as we just scanned. This
1297 * tends to cause slow streaming writers to write data to the
1298 * disk smoothly, at the dirtying rate, which is nice. But
1299 * that's undesirable in laptop mode, where we *want* lumpy
1300 * writeout. So in laptop mode, write out the whole world.
1302 if (total_scanned
> sc
->swap_cluster_max
+
1303 sc
->swap_cluster_max
/ 2) {
1304 wakeup_pdflush(laptop_mode
? 0 : total_scanned
);
1305 sc
->may_writepage
= 1;
1308 /* Take a nap, wait for some writeback to complete */
1309 if (sc
->nr_scanned
&& priority
< DEF_PRIORITY
- 2 &&
1310 sc
->nr_io_pages
> sc
->swap_cluster_max
)
1311 congestion_wait(WRITE
, HZ
/10);
1313 /* top priority shrink_caches still had more to do? don't OOM, then */
1314 if (!sc
->all_unreclaimable
&& sc
->mem_cgroup
== NULL
)
1318 * Now that we've scanned all the zones at this priority level, note
1319 * that level within the zone so that the next thread which performs
1320 * scanning of this zone will immediately start out at this priority
1321 * level. This affects only the decision whether or not to bring
1322 * mapped pages onto the inactive list.
1326 for (i
= 0; zones
[i
] != NULL
; i
++) {
1327 struct zone
*zone
= zones
[i
];
1329 if (!cpuset_zone_allowed_hardwall(zone
, GFP_KERNEL
))
1332 zone
->prev_priority
= priority
;
1337 unsigned long try_to_free_pages(struct zone
**zones
, int order
, gfp_t gfp_mask
)
1339 struct scan_control sc
= {
1340 .gfp_mask
= gfp_mask
,
1341 .may_writepage
= !laptop_mode
,
1342 .swap_cluster_max
= SWAP_CLUSTER_MAX
,
1344 .swappiness
= vm_swappiness
,
1347 .isolate_pages
= isolate_pages_global
,
1350 return do_try_to_free_pages(zones
, gfp_mask
, &sc
);
1353 #ifdef CONFIG_CGROUP_MEM_CONT
1355 unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup
*mem_cont
,
1358 struct scan_control sc
= {
1359 .gfp_mask
= gfp_mask
,
1360 .may_writepage
= !laptop_mode
,
1362 .swap_cluster_max
= SWAP_CLUSTER_MAX
,
1363 .swappiness
= vm_swappiness
,
1365 .mem_cgroup
= mem_cont
,
1366 .isolate_pages
= mem_cgroup_isolate_pages
,
1369 struct zone
**zones
;
1370 int target_zone
= gfp_zone(GFP_HIGHUSER_MOVABLE
);
1372 for_each_online_node(node
) {
1373 zones
= NODE_DATA(node
)->node_zonelists
[target_zone
].zones
;
1374 if (do_try_to_free_pages(zones
, sc
.gfp_mask
, &sc
))
1382 * For kswapd, balance_pgdat() will work across all this node's zones until
1383 * they are all at pages_high.
1385 * Returns the number of pages which were actually freed.
1387 * There is special handling here for zones which are full of pinned pages.
1388 * This can happen if the pages are all mlocked, or if they are all used by
1389 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1390 * What we do is to detect the case where all pages in the zone have been
1391 * scanned twice and there has been zero successful reclaim. Mark the zone as
1392 * dead and from now on, only perform a short scan. Basically we're polling
1393 * the zone for when the problem goes away.
1395 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1396 * zones which have free_pages > pages_high, but once a zone is found to have
1397 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1398 * of the number of free pages in the lower zones. This interoperates with
1399 * the page allocator fallback scheme to ensure that aging of pages is balanced
1402 static unsigned long balance_pgdat(pg_data_t
*pgdat
, int order
)
1407 unsigned long total_scanned
;
1408 unsigned long nr_reclaimed
;
1409 struct reclaim_state
*reclaim_state
= current
->reclaim_state
;
1410 struct scan_control sc
= {
1411 .gfp_mask
= GFP_KERNEL
,
1413 .swap_cluster_max
= SWAP_CLUSTER_MAX
,
1414 .swappiness
= vm_swappiness
,
1417 .isolate_pages
= isolate_pages_global
,
1420 * temp_priority is used to remember the scanning priority at which
1421 * this zone was successfully refilled to free_pages == pages_high.
1423 int temp_priority
[MAX_NR_ZONES
];
1428 sc
.may_writepage
= !laptop_mode
;
1429 count_vm_event(PAGEOUTRUN
);
1431 for (i
= 0; i
< pgdat
->nr_zones
; i
++)
1432 temp_priority
[i
] = DEF_PRIORITY
;
1434 for (priority
= DEF_PRIORITY
; priority
>= 0; priority
--) {
1435 int end_zone
= 0; /* Inclusive. 0 = ZONE_DMA */
1436 unsigned long lru_pages
= 0;
1438 /* The swap token gets in the way of swapout... */
1440 disable_swap_token();
1446 * Scan in the highmem->dma direction for the highest
1447 * zone which needs scanning
1449 for (i
= pgdat
->nr_zones
- 1; i
>= 0; i
--) {
1450 struct zone
*zone
= pgdat
->node_zones
+ i
;
1452 if (!populated_zone(zone
))
1455 if (zone_is_all_unreclaimable(zone
) &&
1456 priority
!= DEF_PRIORITY
)
1459 if (!zone_watermark_ok(zone
, order
, zone
->pages_high
,
1468 for (i
= 0; i
<= end_zone
; i
++) {
1469 struct zone
*zone
= pgdat
->node_zones
+ i
;
1471 lru_pages
+= zone_page_state(zone
, NR_ACTIVE
)
1472 + zone_page_state(zone
, NR_INACTIVE
);
1476 * Now scan the zone in the dma->highmem direction, stopping
1477 * at the last zone which needs scanning.
1479 * We do this because the page allocator works in the opposite
1480 * direction. This prevents the page allocator from allocating
1481 * pages behind kswapd's direction of progress, which would
1482 * cause too much scanning of the lower zones.
1484 for (i
= 0; i
<= end_zone
; i
++) {
1485 struct zone
*zone
= pgdat
->node_zones
+ i
;
1488 if (!populated_zone(zone
))
1491 if (zone_is_all_unreclaimable(zone
) &&
1492 priority
!= DEF_PRIORITY
)
1495 if (!zone_watermark_ok(zone
, order
, zone
->pages_high
,
1498 temp_priority
[i
] = priority
;
1500 note_zone_scanning_priority(zone
, priority
);
1502 * We put equal pressure on every zone, unless one
1503 * zone has way too many pages free already.
1505 if (!zone_watermark_ok(zone
, order
, 8*zone
->pages_high
,
1507 nr_reclaimed
+= shrink_zone(priority
, zone
, &sc
);
1508 reclaim_state
->reclaimed_slab
= 0;
1509 nr_slab
= shrink_slab(sc
.nr_scanned
, GFP_KERNEL
,
1511 nr_reclaimed
+= reclaim_state
->reclaimed_slab
;
1512 total_scanned
+= sc
.nr_scanned
;
1513 if (zone_is_all_unreclaimable(zone
))
1515 if (nr_slab
== 0 && zone
->pages_scanned
>=
1516 (zone_page_state(zone
, NR_ACTIVE
)
1517 + zone_page_state(zone
, NR_INACTIVE
)) * 6)
1519 ZONE_ALL_UNRECLAIMABLE
);
1521 * If we've done a decent amount of scanning and
1522 * the reclaim ratio is low, start doing writepage
1523 * even in laptop mode
1525 if (total_scanned
> SWAP_CLUSTER_MAX
* 2 &&
1526 total_scanned
> nr_reclaimed
+ nr_reclaimed
/ 2)
1527 sc
.may_writepage
= 1;
1530 break; /* kswapd: all done */
1532 * OK, kswapd is getting into trouble. Take a nap, then take
1533 * another pass across the zones.
1535 if (total_scanned
&& priority
< DEF_PRIORITY
- 2 &&
1536 sc
.nr_io_pages
> sc
.swap_cluster_max
)
1537 congestion_wait(WRITE
, HZ
/10);
1540 * We do this so kswapd doesn't build up large priorities for
1541 * example when it is freeing in parallel with allocators. It
1542 * matches the direct reclaim path behaviour in terms of impact
1543 * on zone->*_priority.
1545 if (nr_reclaimed
>= SWAP_CLUSTER_MAX
)
1550 * Note within each zone the priority level at which this zone was
1551 * brought into a happy state. So that the next thread which scans this
1552 * zone will start out at that priority level.
1554 for (i
= 0; i
< pgdat
->nr_zones
; i
++) {
1555 struct zone
*zone
= pgdat
->node_zones
+ i
;
1557 zone
->prev_priority
= temp_priority
[i
];
1559 if (!all_zones_ok
) {
1567 return nr_reclaimed
;
1571 * The background pageout daemon, started as a kernel thread
1572 * from the init process.
1574 * This basically trickles out pages so that we have _some_
1575 * free memory available even if there is no other activity
1576 * that frees anything up. This is needed for things like routing
1577 * etc, where we otherwise might have all activity going on in
1578 * asynchronous contexts that cannot page things out.
1580 * If there are applications that are active memory-allocators
1581 * (most normal use), this basically shouldn't matter.
1583 static int kswapd(void *p
)
1585 unsigned long order
;
1586 pg_data_t
*pgdat
= (pg_data_t
*)p
;
1587 struct task_struct
*tsk
= current
;
1589 struct reclaim_state reclaim_state
= {
1590 .reclaimed_slab
= 0,
1594 cpumask
= node_to_cpumask(pgdat
->node_id
);
1595 if (!cpus_empty(cpumask
))
1596 set_cpus_allowed(tsk
, cpumask
);
1597 current
->reclaim_state
= &reclaim_state
;
1600 * Tell the memory management that we're a "memory allocator",
1601 * and that if we need more memory we should get access to it
1602 * regardless (see "__alloc_pages()"). "kswapd" should
1603 * never get caught in the normal page freeing logic.
1605 * (Kswapd normally doesn't need memory anyway, but sometimes
1606 * you need a small amount of memory in order to be able to
1607 * page out something else, and this flag essentially protects
1608 * us from recursively trying to free more memory as we're
1609 * trying to free the first piece of memory in the first place).
1611 tsk
->flags
|= PF_MEMALLOC
| PF_SWAPWRITE
| PF_KSWAPD
;
1616 unsigned long new_order
;
1618 prepare_to_wait(&pgdat
->kswapd_wait
, &wait
, TASK_INTERRUPTIBLE
);
1619 new_order
= pgdat
->kswapd_max_order
;
1620 pgdat
->kswapd_max_order
= 0;
1621 if (order
< new_order
) {
1623 * Don't sleep if someone wants a larger 'order'
1628 if (!freezing(current
))
1631 order
= pgdat
->kswapd_max_order
;
1633 finish_wait(&pgdat
->kswapd_wait
, &wait
);
1635 if (!try_to_freeze()) {
1636 /* We can speed up thawing tasks if we don't call
1637 * balance_pgdat after returning from the refrigerator
1639 balance_pgdat(pgdat
, order
);
1646 * A zone is low on free memory, so wake its kswapd task to service it.
1648 void wakeup_kswapd(struct zone
*zone
, int order
)
1652 if (!populated_zone(zone
))
1655 pgdat
= zone
->zone_pgdat
;
1656 if (zone_watermark_ok(zone
, order
, zone
->pages_low
, 0, 0))
1658 if (pgdat
->kswapd_max_order
< order
)
1659 pgdat
->kswapd_max_order
= order
;
1660 if (!cpuset_zone_allowed_hardwall(zone
, GFP_KERNEL
))
1662 if (!waitqueue_active(&pgdat
->kswapd_wait
))
1664 wake_up_interruptible(&pgdat
->kswapd_wait
);
1669 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1670 * from LRU lists system-wide, for given pass and priority, and returns the
1671 * number of reclaimed pages
1673 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1675 static unsigned long shrink_all_zones(unsigned long nr_pages
, int prio
,
1676 int pass
, struct scan_control
*sc
)
1679 unsigned long nr_to_scan
, ret
= 0;
1681 for_each_zone(zone
) {
1683 if (!populated_zone(zone
))
1686 if (zone_is_all_unreclaimable(zone
) && prio
!= DEF_PRIORITY
)
1689 /* For pass = 0 we don't shrink the active list */
1691 zone
->nr_scan_active
+=
1692 (zone_page_state(zone
, NR_ACTIVE
) >> prio
) + 1;
1693 if (zone
->nr_scan_active
>= nr_pages
|| pass
> 3) {
1694 zone
->nr_scan_active
= 0;
1695 nr_to_scan
= min(nr_pages
,
1696 zone_page_state(zone
, NR_ACTIVE
));
1697 shrink_active_list(nr_to_scan
, zone
, sc
, prio
);
1701 zone
->nr_scan_inactive
+=
1702 (zone_page_state(zone
, NR_INACTIVE
) >> prio
) + 1;
1703 if (zone
->nr_scan_inactive
>= nr_pages
|| pass
> 3) {
1704 zone
->nr_scan_inactive
= 0;
1705 nr_to_scan
= min(nr_pages
,
1706 zone_page_state(zone
, NR_INACTIVE
));
1707 ret
+= shrink_inactive_list(nr_to_scan
, zone
, sc
);
1708 if (ret
>= nr_pages
)
1716 static unsigned long count_lru_pages(void)
1718 return global_page_state(NR_ACTIVE
) + global_page_state(NR_INACTIVE
);
1722 * Try to free `nr_pages' of memory, system-wide, and return the number of
1725 * Rather than trying to age LRUs the aim is to preserve the overall
1726 * LRU order by reclaiming preferentially
1727 * inactive > active > active referenced > active mapped
1729 unsigned long shrink_all_memory(unsigned long nr_pages
)
1731 unsigned long lru_pages
, nr_slab
;
1732 unsigned long ret
= 0;
1734 struct reclaim_state reclaim_state
;
1735 struct scan_control sc
= {
1736 .gfp_mask
= GFP_KERNEL
,
1738 .swap_cluster_max
= nr_pages
,
1740 .swappiness
= vm_swappiness
,
1741 .isolate_pages
= isolate_pages_global
,
1744 current
->reclaim_state
= &reclaim_state
;
1746 lru_pages
= count_lru_pages();
1747 nr_slab
= global_page_state(NR_SLAB_RECLAIMABLE
);
1748 /* If slab caches are huge, it's better to hit them first */
1749 while (nr_slab
>= lru_pages
) {
1750 reclaim_state
.reclaimed_slab
= 0;
1751 shrink_slab(nr_pages
, sc
.gfp_mask
, lru_pages
);
1752 if (!reclaim_state
.reclaimed_slab
)
1755 ret
+= reclaim_state
.reclaimed_slab
;
1756 if (ret
>= nr_pages
)
1759 nr_slab
-= reclaim_state
.reclaimed_slab
;
1763 * We try to shrink LRUs in 5 passes:
1764 * 0 = Reclaim from inactive_list only
1765 * 1 = Reclaim from active list but don't reclaim mapped
1766 * 2 = 2nd pass of type 1
1767 * 3 = Reclaim mapped (normal reclaim)
1768 * 4 = 2nd pass of type 3
1770 for (pass
= 0; pass
< 5; pass
++) {
1773 /* Force reclaiming mapped pages in the passes #3 and #4 */
1776 sc
.swappiness
= 100;
1779 for (prio
= DEF_PRIORITY
; prio
>= 0; prio
--) {
1780 unsigned long nr_to_scan
= nr_pages
- ret
;
1783 ret
+= shrink_all_zones(nr_to_scan
, prio
, pass
, &sc
);
1784 if (ret
>= nr_pages
)
1787 reclaim_state
.reclaimed_slab
= 0;
1788 shrink_slab(sc
.nr_scanned
, sc
.gfp_mask
,
1790 ret
+= reclaim_state
.reclaimed_slab
;
1791 if (ret
>= nr_pages
)
1794 if (sc
.nr_scanned
&& prio
< DEF_PRIORITY
- 2)
1795 congestion_wait(WRITE
, HZ
/ 10);
1800 * If ret = 0, we could not shrink LRUs, but there may be something
1805 reclaim_state
.reclaimed_slab
= 0;
1806 shrink_slab(nr_pages
, sc
.gfp_mask
, count_lru_pages());
1807 ret
+= reclaim_state
.reclaimed_slab
;
1808 } while (ret
< nr_pages
&& reclaim_state
.reclaimed_slab
> 0);
1812 current
->reclaim_state
= NULL
;
1818 /* It's optimal to keep kswapds on the same CPUs as their memory, but
1819 not required for correctness. So if the last cpu in a node goes
1820 away, we get changed to run anywhere: as the first one comes back,
1821 restore their cpu bindings. */
1822 static int __devinit
cpu_callback(struct notifier_block
*nfb
,
1823 unsigned long action
, void *hcpu
)
1829 if (action
== CPU_ONLINE
|| action
== CPU_ONLINE_FROZEN
) {
1830 for_each_node_state(nid
, N_HIGH_MEMORY
) {
1831 pgdat
= NODE_DATA(nid
);
1832 mask
= node_to_cpumask(pgdat
->node_id
);
1833 if (any_online_cpu(mask
) != NR_CPUS
)
1834 /* One of our CPUs online: restore mask */
1835 set_cpus_allowed(pgdat
->kswapd
, mask
);
1842 * This kswapd start function will be called by init and node-hot-add.
1843 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
1845 int kswapd_run(int nid
)
1847 pg_data_t
*pgdat
= NODE_DATA(nid
);
1853 pgdat
->kswapd
= kthread_run(kswapd
, pgdat
, "kswapd%d", nid
);
1854 if (IS_ERR(pgdat
->kswapd
)) {
1855 /* failure at boot is fatal */
1856 BUG_ON(system_state
== SYSTEM_BOOTING
);
1857 printk("Failed to start kswapd on node %d\n",nid
);
1863 static int __init
kswapd_init(void)
1868 for_each_node_state(nid
, N_HIGH_MEMORY
)
1870 hotcpu_notifier(cpu_callback
, 0);
1874 module_init(kswapd_init
)
1880 * If non-zero call zone_reclaim when the number of free pages falls below
1883 int zone_reclaim_mode __read_mostly
;
1885 #define RECLAIM_OFF 0
1886 #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1887 #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1888 #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1891 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1892 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1895 #define ZONE_RECLAIM_PRIORITY 4
1898 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
1901 int sysctl_min_unmapped_ratio
= 1;
1904 * If the number of slab pages in a zone grows beyond this percentage then
1905 * slab reclaim needs to occur.
1907 int sysctl_min_slab_ratio
= 5;
1910 * Try to free up some pages from this zone through reclaim.
1912 static int __zone_reclaim(struct zone
*zone
, gfp_t gfp_mask
, unsigned int order
)
1914 /* Minimum pages needed in order to stay on node */
1915 const unsigned long nr_pages
= 1 << order
;
1916 struct task_struct
*p
= current
;
1917 struct reclaim_state reclaim_state
;
1919 unsigned long nr_reclaimed
= 0;
1920 struct scan_control sc
= {
1921 .may_writepage
= !!(zone_reclaim_mode
& RECLAIM_WRITE
),
1922 .may_swap
= !!(zone_reclaim_mode
& RECLAIM_SWAP
),
1923 .swap_cluster_max
= max_t(unsigned long, nr_pages
,
1925 .gfp_mask
= gfp_mask
,
1926 .swappiness
= vm_swappiness
,
1927 .isolate_pages
= isolate_pages_global
,
1929 unsigned long slab_reclaimable
;
1931 disable_swap_token();
1934 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1935 * and we also need to be able to write out pages for RECLAIM_WRITE
1938 p
->flags
|= PF_MEMALLOC
| PF_SWAPWRITE
;
1939 reclaim_state
.reclaimed_slab
= 0;
1940 p
->reclaim_state
= &reclaim_state
;
1942 if (zone_page_state(zone
, NR_FILE_PAGES
) -
1943 zone_page_state(zone
, NR_FILE_MAPPED
) >
1944 zone
->min_unmapped_pages
) {
1946 * Free memory by calling shrink zone with increasing
1947 * priorities until we have enough memory freed.
1949 priority
= ZONE_RECLAIM_PRIORITY
;
1951 note_zone_scanning_priority(zone
, priority
);
1952 nr_reclaimed
+= shrink_zone(priority
, zone
, &sc
);
1954 } while (priority
>= 0 && nr_reclaimed
< nr_pages
);
1957 slab_reclaimable
= zone_page_state(zone
, NR_SLAB_RECLAIMABLE
);
1958 if (slab_reclaimable
> zone
->min_slab_pages
) {
1960 * shrink_slab() does not currently allow us to determine how
1961 * many pages were freed in this zone. So we take the current
1962 * number of slab pages and shake the slab until it is reduced
1963 * by the same nr_pages that we used for reclaiming unmapped
1966 * Note that shrink_slab will free memory on all zones and may
1969 while (shrink_slab(sc
.nr_scanned
, gfp_mask
, order
) &&
1970 zone_page_state(zone
, NR_SLAB_RECLAIMABLE
) >
1971 slab_reclaimable
- nr_pages
)
1975 * Update nr_reclaimed by the number of slab pages we
1976 * reclaimed from this zone.
1978 nr_reclaimed
+= slab_reclaimable
-
1979 zone_page_state(zone
, NR_SLAB_RECLAIMABLE
);
1982 p
->reclaim_state
= NULL
;
1983 current
->flags
&= ~(PF_MEMALLOC
| PF_SWAPWRITE
);
1984 return nr_reclaimed
>= nr_pages
;
1987 int zone_reclaim(struct zone
*zone
, gfp_t gfp_mask
, unsigned int order
)
1993 * Zone reclaim reclaims unmapped file backed pages and
1994 * slab pages if we are over the defined limits.
1996 * A small portion of unmapped file backed pages is needed for
1997 * file I/O otherwise pages read by file I/O will be immediately
1998 * thrown out if the zone is overallocated. So we do not reclaim
1999 * if less than a specified percentage of the zone is used by
2000 * unmapped file backed pages.
2002 if (zone_page_state(zone
, NR_FILE_PAGES
) -
2003 zone_page_state(zone
, NR_FILE_MAPPED
) <= zone
->min_unmapped_pages
2004 && zone_page_state(zone
, NR_SLAB_RECLAIMABLE
)
2005 <= zone
->min_slab_pages
)
2008 if (zone_is_all_unreclaimable(zone
))
2012 * Do not scan if the allocation should not be delayed.
2014 if (!(gfp_mask
& __GFP_WAIT
) || (current
->flags
& PF_MEMALLOC
))
2018 * Only run zone reclaim on the local zone or on zones that do not
2019 * have associated processors. This will favor the local processor
2020 * over remote processors and spread off node memory allocations
2021 * as wide as possible.
2023 node_id
= zone_to_nid(zone
);
2024 if (node_state(node_id
, N_CPU
) && node_id
!= numa_node_id())
2027 if (zone_test_and_set_flag(zone
, ZONE_RECLAIM_LOCKED
))
2029 ret
= __zone_reclaim(zone
, gfp_mask
, order
);
2030 zone_clear_flag(zone
, ZONE_RECLAIM_LOCKED
);