i2c-sibyte: Kip Walker is gone
[linux-2.6/linux-mips.git] / mm / vmscan.c
blob87779dda4ec6d7af4afe8427c6cf0476138d3245
1 /*
2 * linux/mm/vmscan.c
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.
14 #include <linux/mm.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/kernel_stat.h>
18 #include <linux/swap.h>
19 #include <linux/pagemap.h>
20 #include <linux/init.h>
21 #include <linux/highmem.h>
22 #include <linux/file.h>
23 #include <linux/writeback.h>
24 #include <linux/blkdev.h>
25 #include <linux/buffer_head.h> /* for try_to_release_page(),
26 buffer_heads_over_limit */
27 #include <linux/mm_inline.h>
28 #include <linux/pagevec.h>
29 #include <linux/backing-dev.h>
30 #include <linux/rmap.h>
31 #include <linux/topology.h>
32 #include <linux/cpu.h>
33 #include <linux/cpuset.h>
34 #include <linux/notifier.h>
35 #include <linux/rwsem.h>
36 #include <linux/delay.h>
37 #include <linux/kthread.h>
39 #include <asm/tlbflush.h>
40 #include <asm/div64.h>
42 #include <linux/swapops.h>
44 #include "internal.h"
46 struct scan_control {
47 /* Incremented by the number of inactive pages that were scanned */
48 unsigned long nr_scanned;
50 /* This context's GFP mask */
51 gfp_t gfp_mask;
53 int may_writepage;
55 /* Can pages be swapped as part of reclaim? */
56 int may_swap;
58 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
59 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
60 * In this context, it doesn't matter that we scan the
61 * whole list at once. */
62 int swap_cluster_max;
64 int swappiness;
66 int all_unreclaimable;
70 * The list of shrinker callbacks used by to apply pressure to
71 * ageable caches.
73 struct shrinker {
74 shrinker_t shrinker;
75 struct list_head list;
76 int seeks; /* seeks to recreate an obj */
77 long nr; /* objs pending delete */
80 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
82 #ifdef ARCH_HAS_PREFETCH
83 #define prefetch_prev_lru_page(_page, _base, _field) \
84 do { \
85 if ((_page)->lru.prev != _base) { \
86 struct page *prev; \
88 prev = lru_to_page(&(_page->lru)); \
89 prefetch(&prev->_field); \
90 } \
91 } while (0)
92 #else
93 #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
94 #endif
96 #ifdef ARCH_HAS_PREFETCHW
97 #define prefetchw_prev_lru_page(_page, _base, _field) \
98 do { \
99 if ((_page)->lru.prev != _base) { \
100 struct page *prev; \
102 prev = lru_to_page(&(_page->lru)); \
103 prefetchw(&prev->_field); \
105 } while (0)
106 #else
107 #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
108 #endif
111 * From 0 .. 100. Higher means more swappy.
113 int vm_swappiness = 60;
114 long vm_total_pages; /* The total number of pages which the VM controls */
116 static LIST_HEAD(shrinker_list);
117 static DECLARE_RWSEM(shrinker_rwsem);
120 * Add a shrinker callback to be called from the vm
122 struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
124 struct shrinker *shrinker;
126 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
127 if (shrinker) {
128 shrinker->shrinker = theshrinker;
129 shrinker->seeks = seeks;
130 shrinker->nr = 0;
131 down_write(&shrinker_rwsem);
132 list_add_tail(&shrinker->list, &shrinker_list);
133 up_write(&shrinker_rwsem);
135 return shrinker;
137 EXPORT_SYMBOL(set_shrinker);
140 * Remove one
142 void remove_shrinker(struct shrinker *shrinker)
144 down_write(&shrinker_rwsem);
145 list_del(&shrinker->list);
146 up_write(&shrinker_rwsem);
147 kfree(shrinker);
149 EXPORT_SYMBOL(remove_shrinker);
151 #define SHRINK_BATCH 128
153 * Call the shrink functions to age shrinkable caches
155 * Here we assume it costs one seek to replace a lru page and that it also
156 * takes a seek to recreate a cache object. With this in mind we age equal
157 * percentages of the lru and ageable caches. This should balance the seeks
158 * generated by these structures.
160 * If the vm encounted mapped pages on the LRU it increase the pressure on
161 * slab to avoid swapping.
163 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
165 * `lru_pages' represents the number of on-LRU pages in all the zones which
166 * are eligible for the caller's allocation attempt. It is used for balancing
167 * slab reclaim versus page reclaim.
169 * Returns the number of slab objects which we shrunk.
171 unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
172 unsigned long lru_pages)
174 struct shrinker *shrinker;
175 unsigned long ret = 0;
177 if (scanned == 0)
178 scanned = SWAP_CLUSTER_MAX;
180 if (!down_read_trylock(&shrinker_rwsem))
181 return 1; /* Assume we'll be able to shrink next time */
183 list_for_each_entry(shrinker, &shrinker_list, list) {
184 unsigned long long delta;
185 unsigned long total_scan;
186 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
188 delta = (4 * scanned) / shrinker->seeks;
189 delta *= max_pass;
190 do_div(delta, lru_pages + 1);
191 shrinker->nr += delta;
192 if (shrinker->nr < 0) {
193 printk(KERN_ERR "%s: nr=%ld\n",
194 __FUNCTION__, shrinker->nr);
195 shrinker->nr = max_pass;
199 * Avoid risking looping forever due to too large nr value:
200 * never try to free more than twice the estimate number of
201 * freeable entries.
203 if (shrinker->nr > max_pass * 2)
204 shrinker->nr = max_pass * 2;
206 total_scan = shrinker->nr;
207 shrinker->nr = 0;
209 while (total_scan >= SHRINK_BATCH) {
210 long this_scan = SHRINK_BATCH;
211 int shrink_ret;
212 int nr_before;
214 nr_before = (*shrinker->shrinker)(0, gfp_mask);
215 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
216 if (shrink_ret == -1)
217 break;
218 if (shrink_ret < nr_before)
219 ret += nr_before - shrink_ret;
220 count_vm_events(SLABS_SCANNED, this_scan);
221 total_scan -= this_scan;
223 cond_resched();
226 shrinker->nr += total_scan;
228 up_read(&shrinker_rwsem);
229 return ret;
232 /* Called without lock on whether page is mapped, so answer is unstable */
233 static inline int page_mapping_inuse(struct page *page)
235 struct address_space *mapping;
237 /* Page is in somebody's page tables. */
238 if (page_mapped(page))
239 return 1;
241 /* Be more reluctant to reclaim swapcache than pagecache */
242 if (PageSwapCache(page))
243 return 1;
245 mapping = page_mapping(page);
246 if (!mapping)
247 return 0;
249 /* File is mmap'd by somebody? */
250 return mapping_mapped(mapping);
253 static inline int is_page_cache_freeable(struct page *page)
255 return page_count(page) - !!PagePrivate(page) == 2;
258 static int may_write_to_queue(struct backing_dev_info *bdi)
260 if (current->flags & PF_SWAPWRITE)
261 return 1;
262 if (!bdi_write_congested(bdi))
263 return 1;
264 if (bdi == current->backing_dev_info)
265 return 1;
266 return 0;
270 * We detected a synchronous write error writing a page out. Probably
271 * -ENOSPC. We need to propagate that into the address_space for a subsequent
272 * fsync(), msync() or close().
274 * The tricky part is that after writepage we cannot touch the mapping: nothing
275 * prevents it from being freed up. But we have a ref on the page and once
276 * that page is locked, the mapping is pinned.
278 * We're allowed to run sleeping lock_page() here because we know the caller has
279 * __GFP_FS.
281 static void handle_write_error(struct address_space *mapping,
282 struct page *page, int error)
284 lock_page(page);
285 if (page_mapping(page) == mapping) {
286 if (error == -ENOSPC)
287 set_bit(AS_ENOSPC, &mapping->flags);
288 else
289 set_bit(AS_EIO, &mapping->flags);
291 unlock_page(page);
294 /* possible outcome of pageout() */
295 typedef enum {
296 /* failed to write page out, page is locked */
297 PAGE_KEEP,
298 /* move page to the active list, page is locked */
299 PAGE_ACTIVATE,
300 /* page has been sent to the disk successfully, page is unlocked */
301 PAGE_SUCCESS,
302 /* page is clean and locked */
303 PAGE_CLEAN,
304 } pageout_t;
307 * pageout is called by shrink_page_list() for each dirty page.
308 * Calls ->writepage().
310 static pageout_t pageout(struct page *page, struct address_space *mapping)
313 * If the page is dirty, only perform writeback if that write
314 * will be non-blocking. To prevent this allocation from being
315 * stalled by pagecache activity. But note that there may be
316 * stalls if we need to run get_block(). We could test
317 * PagePrivate for that.
319 * If this process is currently in generic_file_write() against
320 * this page's queue, we can perform writeback even if that
321 * will block.
323 * If the page is swapcache, write it back even if that would
324 * block, for some throttling. This happens by accident, because
325 * swap_backing_dev_info is bust: it doesn't reflect the
326 * congestion state of the swapdevs. Easy to fix, if needed.
327 * See swapfile.c:page_queue_congested().
329 if (!is_page_cache_freeable(page))
330 return PAGE_KEEP;
331 if (!mapping) {
333 * Some data journaling orphaned pages can have
334 * page->mapping == NULL while being dirty with clean buffers.
336 if (PagePrivate(page)) {
337 if (try_to_free_buffers(page)) {
338 ClearPageDirty(page);
339 printk("%s: orphaned page\n", __FUNCTION__);
340 return PAGE_CLEAN;
343 return PAGE_KEEP;
345 if (mapping->a_ops->writepage == NULL)
346 return PAGE_ACTIVATE;
347 if (!may_write_to_queue(mapping->backing_dev_info))
348 return PAGE_KEEP;
350 if (clear_page_dirty_for_io(page)) {
351 int res;
352 struct writeback_control wbc = {
353 .sync_mode = WB_SYNC_NONE,
354 .nr_to_write = SWAP_CLUSTER_MAX,
355 .range_start = 0,
356 .range_end = LLONG_MAX,
357 .nonblocking = 1,
358 .for_reclaim = 1,
361 SetPageReclaim(page);
362 res = mapping->a_ops->writepage(page, &wbc);
363 if (res < 0)
364 handle_write_error(mapping, page, res);
365 if (res == AOP_WRITEPAGE_ACTIVATE) {
366 ClearPageReclaim(page);
367 return PAGE_ACTIVATE;
369 if (!PageWriteback(page)) {
370 /* synchronous write or broken a_ops? */
371 ClearPageReclaim(page);
374 return PAGE_SUCCESS;
377 return PAGE_CLEAN;
380 int remove_mapping(struct address_space *mapping, struct page *page)
382 BUG_ON(!PageLocked(page));
383 BUG_ON(mapping != page_mapping(page));
385 write_lock_irq(&mapping->tree_lock);
388 * The non-racy check for busy page. It is critical to check
389 * PageDirty _after_ making sure that the page is freeable and
390 * not in use by anybody. (pagecache + us == 2)
392 if (unlikely(page_count(page) != 2))
393 goto cannot_free;
394 smp_rmb();
395 if (unlikely(PageDirty(page)))
396 goto cannot_free;
398 if (PageSwapCache(page)) {
399 swp_entry_t swap = { .val = page_private(page) };
400 __delete_from_swap_cache(page);
401 write_unlock_irq(&mapping->tree_lock);
402 swap_free(swap);
403 __put_page(page); /* The pagecache ref */
404 return 1;
407 __remove_from_page_cache(page);
408 write_unlock_irq(&mapping->tree_lock);
409 __put_page(page);
410 return 1;
412 cannot_free:
413 write_unlock_irq(&mapping->tree_lock);
414 return 0;
418 * shrink_page_list() returns the number of reclaimed pages
420 static unsigned long shrink_page_list(struct list_head *page_list,
421 struct scan_control *sc)
423 LIST_HEAD(ret_pages);
424 struct pagevec freed_pvec;
425 int pgactivate = 0;
426 unsigned long nr_reclaimed = 0;
428 cond_resched();
430 pagevec_init(&freed_pvec, 1);
431 while (!list_empty(page_list)) {
432 struct address_space *mapping;
433 struct page *page;
434 int may_enter_fs;
435 int referenced;
437 cond_resched();
439 page = lru_to_page(page_list);
440 list_del(&page->lru);
442 if (TestSetPageLocked(page))
443 goto keep;
445 VM_BUG_ON(PageActive(page));
447 sc->nr_scanned++;
449 if (!sc->may_swap && page_mapped(page))
450 goto keep_locked;
452 /* Double the slab pressure for mapped and swapcache pages */
453 if (page_mapped(page) || PageSwapCache(page))
454 sc->nr_scanned++;
456 if (PageWriteback(page))
457 goto keep_locked;
459 referenced = page_referenced(page, 1);
460 /* In active use or really unfreeable? Activate it. */
461 if (referenced && page_mapping_inuse(page))
462 goto activate_locked;
464 #ifdef CONFIG_SWAP
466 * Anonymous process memory has backing store?
467 * Try to allocate it some swap space here.
469 if (PageAnon(page) && !PageSwapCache(page))
470 if (!add_to_swap(page, GFP_ATOMIC))
471 goto activate_locked;
472 #endif /* CONFIG_SWAP */
474 mapping = page_mapping(page);
475 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
476 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
479 * The page is mapped into the page tables of one or more
480 * processes. Try to unmap it here.
482 if (page_mapped(page) && mapping) {
483 switch (try_to_unmap(page, 0)) {
484 case SWAP_FAIL:
485 goto activate_locked;
486 case SWAP_AGAIN:
487 goto keep_locked;
488 case SWAP_SUCCESS:
489 ; /* try to free the page below */
493 if (PageDirty(page)) {
494 if (referenced)
495 goto keep_locked;
496 if (!may_enter_fs)
497 goto keep_locked;
498 if (!sc->may_writepage)
499 goto keep_locked;
501 /* Page is dirty, try to write it out here */
502 switch(pageout(page, mapping)) {
503 case PAGE_KEEP:
504 goto keep_locked;
505 case PAGE_ACTIVATE:
506 goto activate_locked;
507 case PAGE_SUCCESS:
508 if (PageWriteback(page) || PageDirty(page))
509 goto keep;
511 * A synchronous write - probably a ramdisk. Go
512 * ahead and try to reclaim the page.
514 if (TestSetPageLocked(page))
515 goto keep;
516 if (PageDirty(page) || PageWriteback(page))
517 goto keep_locked;
518 mapping = page_mapping(page);
519 case PAGE_CLEAN:
520 ; /* try to free the page below */
525 * If the page has buffers, try to free the buffer mappings
526 * associated with this page. If we succeed we try to free
527 * the page as well.
529 * We do this even if the page is PageDirty().
530 * try_to_release_page() does not perform I/O, but it is
531 * possible for a page to have PageDirty set, but it is actually
532 * clean (all its buffers are clean). This happens if the
533 * buffers were written out directly, with submit_bh(). ext3
534 * will do this, as well as the blockdev mapping.
535 * try_to_release_page() will discover that cleanness and will
536 * drop the buffers and mark the page clean - it can be freed.
538 * Rarely, pages can have buffers and no ->mapping. These are
539 * the pages which were not successfully invalidated in
540 * truncate_complete_page(). We try to drop those buffers here
541 * and if that worked, and the page is no longer mapped into
542 * process address space (page_count == 1) it can be freed.
543 * Otherwise, leave the page on the LRU so it is swappable.
545 if (PagePrivate(page)) {
546 if (!try_to_release_page(page, sc->gfp_mask))
547 goto activate_locked;
548 if (!mapping && page_count(page) == 1)
549 goto free_it;
552 if (!mapping || !remove_mapping(mapping, page))
553 goto keep_locked;
555 free_it:
556 unlock_page(page);
557 nr_reclaimed++;
558 if (!pagevec_add(&freed_pvec, page))
559 __pagevec_release_nonlru(&freed_pvec);
560 continue;
562 activate_locked:
563 SetPageActive(page);
564 pgactivate++;
565 keep_locked:
566 unlock_page(page);
567 keep:
568 list_add(&page->lru, &ret_pages);
569 VM_BUG_ON(PageLRU(page));
571 list_splice(&ret_pages, page_list);
572 if (pagevec_count(&freed_pvec))
573 __pagevec_release_nonlru(&freed_pvec);
574 count_vm_events(PGACTIVATE, pgactivate);
575 return nr_reclaimed;
579 * zone->lru_lock is heavily contended. Some of the functions that
580 * shrink the lists perform better by taking out a batch of pages
581 * and working on them outside the LRU lock.
583 * For pagecache intensive workloads, this function is the hottest
584 * spot in the kernel (apart from copy_*_user functions).
586 * Appropriate locks must be held before calling this function.
588 * @nr_to_scan: The number of pages to look through on the list.
589 * @src: The LRU list to pull pages off.
590 * @dst: The temp list to put pages on to.
591 * @scanned: The number of pages that were scanned.
593 * returns how many pages were moved onto *@dst.
595 static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
596 struct list_head *src, struct list_head *dst,
597 unsigned long *scanned)
599 unsigned long nr_taken = 0;
600 struct page *page;
601 unsigned long scan;
603 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
604 struct list_head *target;
605 page = lru_to_page(src);
606 prefetchw_prev_lru_page(page, src, flags);
608 VM_BUG_ON(!PageLRU(page));
610 list_del(&page->lru);
611 target = src;
612 if (likely(get_page_unless_zero(page))) {
614 * Be careful not to clear PageLRU until after we're
615 * sure the page is not being freed elsewhere -- the
616 * page release code relies on it.
618 ClearPageLRU(page);
619 target = dst;
620 nr_taken++;
621 } /* else it is being freed elsewhere */
623 list_add(&page->lru, target);
626 *scanned = scan;
627 return nr_taken;
631 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
632 * of reclaimed pages
634 static unsigned long shrink_inactive_list(unsigned long max_scan,
635 struct zone *zone, struct scan_control *sc)
637 LIST_HEAD(page_list);
638 struct pagevec pvec;
639 unsigned long nr_scanned = 0;
640 unsigned long nr_reclaimed = 0;
642 pagevec_init(&pvec, 1);
644 lru_add_drain();
645 spin_lock_irq(&zone->lru_lock);
646 do {
647 struct page *page;
648 unsigned long nr_taken;
649 unsigned long nr_scan;
650 unsigned long nr_freed;
652 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
653 &zone->inactive_list,
654 &page_list, &nr_scan);
655 zone->nr_inactive -= nr_taken;
656 zone->pages_scanned += nr_scan;
657 spin_unlock_irq(&zone->lru_lock);
659 nr_scanned += nr_scan;
660 nr_freed = shrink_page_list(&page_list, sc);
661 nr_reclaimed += nr_freed;
662 local_irq_disable();
663 if (current_is_kswapd()) {
664 __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
665 __count_vm_events(KSWAPD_STEAL, nr_freed);
666 } else
667 __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
668 __count_vm_events(PGACTIVATE, nr_freed);
670 if (nr_taken == 0)
671 goto done;
673 spin_lock(&zone->lru_lock);
675 * Put back any unfreeable pages.
677 while (!list_empty(&page_list)) {
678 page = lru_to_page(&page_list);
679 VM_BUG_ON(PageLRU(page));
680 SetPageLRU(page);
681 list_del(&page->lru);
682 if (PageActive(page))
683 add_page_to_active_list(zone, page);
684 else
685 add_page_to_inactive_list(zone, page);
686 if (!pagevec_add(&pvec, page)) {
687 spin_unlock_irq(&zone->lru_lock);
688 __pagevec_release(&pvec);
689 spin_lock_irq(&zone->lru_lock);
692 } while (nr_scanned < max_scan);
693 spin_unlock(&zone->lru_lock);
694 done:
695 local_irq_enable();
696 pagevec_release(&pvec);
697 return nr_reclaimed;
700 static inline int zone_is_near_oom(struct zone *zone)
702 return zone->pages_scanned >= (zone->nr_active + zone->nr_inactive)*3;
706 * This moves pages from the active list to the inactive list.
708 * We move them the other way if the page is referenced by one or more
709 * processes, from rmap.
711 * If the pages are mostly unmapped, the processing is fast and it is
712 * appropriate to hold zone->lru_lock across the whole operation. But if
713 * the pages are mapped, the processing is slow (page_referenced()) so we
714 * should drop zone->lru_lock around each page. It's impossible to balance
715 * this, so instead we remove the pages from the LRU while processing them.
716 * It is safe to rely on PG_active against the non-LRU pages in here because
717 * nobody will play with that bit on a non-LRU page.
719 * The downside is that we have to touch page->_count against each page.
720 * But we had to alter page->flags anyway.
722 static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
723 struct scan_control *sc)
725 unsigned long pgmoved;
726 int pgdeactivate = 0;
727 unsigned long pgscanned;
728 LIST_HEAD(l_hold); /* The pages which were snipped off */
729 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
730 LIST_HEAD(l_active); /* Pages to go onto the active_list */
731 struct page *page;
732 struct pagevec pvec;
733 int reclaim_mapped = 0;
735 if (sc->may_swap) {
736 long mapped_ratio;
737 long distress;
738 long swap_tendency;
740 if (zone_is_near_oom(zone))
741 goto force_reclaim_mapped;
744 * `distress' is a measure of how much trouble we're having
745 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
747 distress = 100 >> zone->prev_priority;
750 * The point of this algorithm is to decide when to start
751 * reclaiming mapped memory instead of just pagecache. Work out
752 * how much memory
753 * is mapped.
755 mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
756 global_page_state(NR_ANON_PAGES)) * 100) /
757 vm_total_pages;
760 * Now decide how much we really want to unmap some pages. The
761 * mapped ratio is downgraded - just because there's a lot of
762 * mapped memory doesn't necessarily mean that page reclaim
763 * isn't succeeding.
765 * The distress ratio is important - we don't want to start
766 * going oom.
768 * A 100% value of vm_swappiness overrides this algorithm
769 * altogether.
771 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
774 * Now use this metric to decide whether to start moving mapped
775 * memory onto the inactive list.
777 if (swap_tendency >= 100)
778 force_reclaim_mapped:
779 reclaim_mapped = 1;
782 lru_add_drain();
783 spin_lock_irq(&zone->lru_lock);
784 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
785 &l_hold, &pgscanned);
786 zone->pages_scanned += pgscanned;
787 zone->nr_active -= pgmoved;
788 spin_unlock_irq(&zone->lru_lock);
790 while (!list_empty(&l_hold)) {
791 cond_resched();
792 page = lru_to_page(&l_hold);
793 list_del(&page->lru);
794 if (page_mapped(page)) {
795 if (!reclaim_mapped ||
796 (total_swap_pages == 0 && PageAnon(page)) ||
797 page_referenced(page, 0)) {
798 list_add(&page->lru, &l_active);
799 continue;
802 list_add(&page->lru, &l_inactive);
805 pagevec_init(&pvec, 1);
806 pgmoved = 0;
807 spin_lock_irq(&zone->lru_lock);
808 while (!list_empty(&l_inactive)) {
809 page = lru_to_page(&l_inactive);
810 prefetchw_prev_lru_page(page, &l_inactive, flags);
811 VM_BUG_ON(PageLRU(page));
812 SetPageLRU(page);
813 VM_BUG_ON(!PageActive(page));
814 ClearPageActive(page);
816 list_move(&page->lru, &zone->inactive_list);
817 pgmoved++;
818 if (!pagevec_add(&pvec, page)) {
819 zone->nr_inactive += pgmoved;
820 spin_unlock_irq(&zone->lru_lock);
821 pgdeactivate += pgmoved;
822 pgmoved = 0;
823 if (buffer_heads_over_limit)
824 pagevec_strip(&pvec);
825 __pagevec_release(&pvec);
826 spin_lock_irq(&zone->lru_lock);
829 zone->nr_inactive += pgmoved;
830 pgdeactivate += pgmoved;
831 if (buffer_heads_over_limit) {
832 spin_unlock_irq(&zone->lru_lock);
833 pagevec_strip(&pvec);
834 spin_lock_irq(&zone->lru_lock);
837 pgmoved = 0;
838 while (!list_empty(&l_active)) {
839 page = lru_to_page(&l_active);
840 prefetchw_prev_lru_page(page, &l_active, flags);
841 VM_BUG_ON(PageLRU(page));
842 SetPageLRU(page);
843 VM_BUG_ON(!PageActive(page));
844 list_move(&page->lru, &zone->active_list);
845 pgmoved++;
846 if (!pagevec_add(&pvec, page)) {
847 zone->nr_active += pgmoved;
848 pgmoved = 0;
849 spin_unlock_irq(&zone->lru_lock);
850 __pagevec_release(&pvec);
851 spin_lock_irq(&zone->lru_lock);
854 zone->nr_active += pgmoved;
856 __count_zone_vm_events(PGREFILL, zone, pgscanned);
857 __count_vm_events(PGDEACTIVATE, pgdeactivate);
858 spin_unlock_irq(&zone->lru_lock);
860 pagevec_release(&pvec);
864 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
866 static unsigned long shrink_zone(int priority, struct zone *zone,
867 struct scan_control *sc)
869 unsigned long nr_active;
870 unsigned long nr_inactive;
871 unsigned long nr_to_scan;
872 unsigned long nr_reclaimed = 0;
874 atomic_inc(&zone->reclaim_in_progress);
877 * Add one to `nr_to_scan' just to make sure that the kernel will
878 * slowly sift through the active list.
880 zone->nr_scan_active += (zone->nr_active >> priority) + 1;
881 nr_active = zone->nr_scan_active;
882 if (nr_active >= sc->swap_cluster_max)
883 zone->nr_scan_active = 0;
884 else
885 nr_active = 0;
887 zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
888 nr_inactive = zone->nr_scan_inactive;
889 if (nr_inactive >= sc->swap_cluster_max)
890 zone->nr_scan_inactive = 0;
891 else
892 nr_inactive = 0;
894 while (nr_active || nr_inactive) {
895 if (nr_active) {
896 nr_to_scan = min(nr_active,
897 (unsigned long)sc->swap_cluster_max);
898 nr_active -= nr_to_scan;
899 shrink_active_list(nr_to_scan, zone, sc);
902 if (nr_inactive) {
903 nr_to_scan = min(nr_inactive,
904 (unsigned long)sc->swap_cluster_max);
905 nr_inactive -= nr_to_scan;
906 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
907 sc);
911 throttle_vm_writeout();
913 atomic_dec(&zone->reclaim_in_progress);
914 return nr_reclaimed;
918 * This is the direct reclaim path, for page-allocating processes. We only
919 * try to reclaim pages from zones which will satisfy the caller's allocation
920 * request.
922 * We reclaim from a zone even if that zone is over pages_high. Because:
923 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
924 * allocation or
925 * b) The zones may be over pages_high but they must go *over* pages_high to
926 * satisfy the `incremental min' zone defense algorithm.
928 * Returns the number of reclaimed pages.
930 * If a zone is deemed to be full of pinned pages then just give it a light
931 * scan then give up on it.
933 static unsigned long shrink_zones(int priority, struct zone **zones,
934 struct scan_control *sc)
936 unsigned long nr_reclaimed = 0;
937 int i;
939 sc->all_unreclaimable = 1;
940 for (i = 0; zones[i] != NULL; i++) {
941 struct zone *zone = zones[i];
943 if (!populated_zone(zone))
944 continue;
946 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
947 continue;
949 zone->temp_priority = priority;
950 if (zone->prev_priority > priority)
951 zone->prev_priority = priority;
953 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
954 continue; /* Let kswapd poll it */
956 sc->all_unreclaimable = 0;
958 nr_reclaimed += shrink_zone(priority, zone, sc);
960 return nr_reclaimed;
964 * This is the main entry point to direct page reclaim.
966 * If a full scan of the inactive list fails to free enough memory then we
967 * are "out of memory" and something needs to be killed.
969 * If the caller is !__GFP_FS then the probability of a failure is reasonably
970 * high - the zone may be full of dirty or under-writeback pages, which this
971 * caller can't do much about. We kick pdflush and take explicit naps in the
972 * hope that some of these pages can be written. But if the allocating task
973 * holds filesystem locks which prevent writeout this might not work, and the
974 * allocation attempt will fail.
976 unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
978 int priority;
979 int ret = 0;
980 unsigned long total_scanned = 0;
981 unsigned long nr_reclaimed = 0;
982 struct reclaim_state *reclaim_state = current->reclaim_state;
983 unsigned long lru_pages = 0;
984 int i;
985 struct scan_control sc = {
986 .gfp_mask = gfp_mask,
987 .may_writepage = !laptop_mode,
988 .swap_cluster_max = SWAP_CLUSTER_MAX,
989 .may_swap = 1,
990 .swappiness = vm_swappiness,
993 count_vm_event(ALLOCSTALL);
995 for (i = 0; zones[i] != NULL; i++) {
996 struct zone *zone = zones[i];
998 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
999 continue;
1001 zone->temp_priority = DEF_PRIORITY;
1002 lru_pages += zone->nr_active + zone->nr_inactive;
1005 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1006 sc.nr_scanned = 0;
1007 if (!priority)
1008 disable_swap_token();
1009 nr_reclaimed += shrink_zones(priority, zones, &sc);
1010 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
1011 if (reclaim_state) {
1012 nr_reclaimed += reclaim_state->reclaimed_slab;
1013 reclaim_state->reclaimed_slab = 0;
1015 total_scanned += sc.nr_scanned;
1016 if (nr_reclaimed >= sc.swap_cluster_max) {
1017 ret = 1;
1018 goto out;
1022 * Try to write back as many pages as we just scanned. This
1023 * tends to cause slow streaming writers to write data to the
1024 * disk smoothly, at the dirtying rate, which is nice. But
1025 * that's undesirable in laptop mode, where we *want* lumpy
1026 * writeout. So in laptop mode, write out the whole world.
1028 if (total_scanned > sc.swap_cluster_max +
1029 sc.swap_cluster_max / 2) {
1030 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
1031 sc.may_writepage = 1;
1034 /* Take a nap, wait for some writeback to complete */
1035 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1036 blk_congestion_wait(WRITE, HZ/10);
1038 /* top priority shrink_caches still had more to do? don't OOM, then */
1039 if (!sc.all_unreclaimable)
1040 ret = 1;
1041 out:
1042 for (i = 0; zones[i] != 0; i++) {
1043 struct zone *zone = zones[i];
1045 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1046 continue;
1048 zone->prev_priority = zone->temp_priority;
1050 return ret;
1054 * For kswapd, balance_pgdat() will work across all this node's zones until
1055 * they are all at pages_high.
1057 * Returns the number of pages which were actually freed.
1059 * There is special handling here for zones which are full of pinned pages.
1060 * This can happen if the pages are all mlocked, or if they are all used by
1061 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1062 * What we do is to detect the case where all pages in the zone have been
1063 * scanned twice and there has been zero successful reclaim. Mark the zone as
1064 * dead and from now on, only perform a short scan. Basically we're polling
1065 * the zone for when the problem goes away.
1067 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1068 * zones which have free_pages > pages_high, but once a zone is found to have
1069 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1070 * of the number of free pages in the lower zones. This interoperates with
1071 * the page allocator fallback scheme to ensure that aging of pages is balanced
1072 * across the zones.
1074 static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
1076 int all_zones_ok;
1077 int priority;
1078 int i;
1079 unsigned long total_scanned;
1080 unsigned long nr_reclaimed;
1081 struct reclaim_state *reclaim_state = current->reclaim_state;
1082 struct scan_control sc = {
1083 .gfp_mask = GFP_KERNEL,
1084 .may_swap = 1,
1085 .swap_cluster_max = SWAP_CLUSTER_MAX,
1086 .swappiness = vm_swappiness,
1089 loop_again:
1090 total_scanned = 0;
1091 nr_reclaimed = 0;
1092 sc.may_writepage = !laptop_mode;
1093 count_vm_event(PAGEOUTRUN);
1095 for (i = 0; i < pgdat->nr_zones; i++) {
1096 struct zone *zone = pgdat->node_zones + i;
1098 zone->temp_priority = DEF_PRIORITY;
1101 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1102 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1103 unsigned long lru_pages = 0;
1105 /* The swap token gets in the way of swapout... */
1106 if (!priority)
1107 disable_swap_token();
1109 all_zones_ok = 1;
1112 * Scan in the highmem->dma direction for the highest
1113 * zone which needs scanning
1115 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1116 struct zone *zone = pgdat->node_zones + i;
1118 if (!populated_zone(zone))
1119 continue;
1121 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1122 continue;
1124 if (!zone_watermark_ok(zone, order, zone->pages_high,
1125 0, 0)) {
1126 end_zone = i;
1127 goto scan;
1130 goto out;
1131 scan:
1132 for (i = 0; i <= end_zone; i++) {
1133 struct zone *zone = pgdat->node_zones + i;
1135 lru_pages += zone->nr_active + zone->nr_inactive;
1139 * Now scan the zone in the dma->highmem direction, stopping
1140 * at the last zone which needs scanning.
1142 * We do this because the page allocator works in the opposite
1143 * direction. This prevents the page allocator from allocating
1144 * pages behind kswapd's direction of progress, which would
1145 * cause too much scanning of the lower zones.
1147 for (i = 0; i <= end_zone; i++) {
1148 struct zone *zone = pgdat->node_zones + i;
1149 int nr_slab;
1151 if (!populated_zone(zone))
1152 continue;
1154 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1155 continue;
1157 if (!zone_watermark_ok(zone, order, zone->pages_high,
1158 end_zone, 0))
1159 all_zones_ok = 0;
1160 zone->temp_priority = priority;
1161 if (zone->prev_priority > priority)
1162 zone->prev_priority = priority;
1163 sc.nr_scanned = 0;
1164 nr_reclaimed += shrink_zone(priority, zone, &sc);
1165 reclaim_state->reclaimed_slab = 0;
1166 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1167 lru_pages);
1168 nr_reclaimed += reclaim_state->reclaimed_slab;
1169 total_scanned += sc.nr_scanned;
1170 if (zone->all_unreclaimable)
1171 continue;
1172 if (nr_slab == 0 && zone->pages_scanned >=
1173 (zone->nr_active + zone->nr_inactive) * 6)
1174 zone->all_unreclaimable = 1;
1176 * If we've done a decent amount of scanning and
1177 * the reclaim ratio is low, start doing writepage
1178 * even in laptop mode
1180 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
1181 total_scanned > nr_reclaimed + nr_reclaimed / 2)
1182 sc.may_writepage = 1;
1184 if (all_zones_ok)
1185 break; /* kswapd: all done */
1187 * OK, kswapd is getting into trouble. Take a nap, then take
1188 * another pass across the zones.
1190 if (total_scanned && priority < DEF_PRIORITY - 2)
1191 blk_congestion_wait(WRITE, HZ/10);
1194 * We do this so kswapd doesn't build up large priorities for
1195 * example when it is freeing in parallel with allocators. It
1196 * matches the direct reclaim path behaviour in terms of impact
1197 * on zone->*_priority.
1199 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
1200 break;
1202 out:
1203 for (i = 0; i < pgdat->nr_zones; i++) {
1204 struct zone *zone = pgdat->node_zones + i;
1206 zone->prev_priority = zone->temp_priority;
1208 if (!all_zones_ok) {
1209 cond_resched();
1210 goto loop_again;
1213 return nr_reclaimed;
1217 * The background pageout daemon, started as a kernel thread
1218 * from the init process.
1220 * This basically trickles out pages so that we have _some_
1221 * free memory available even if there is no other activity
1222 * that frees anything up. This is needed for things like routing
1223 * etc, where we otherwise might have all activity going on in
1224 * asynchronous contexts that cannot page things out.
1226 * If there are applications that are active memory-allocators
1227 * (most normal use), this basically shouldn't matter.
1229 static int kswapd(void *p)
1231 unsigned long order;
1232 pg_data_t *pgdat = (pg_data_t*)p;
1233 struct task_struct *tsk = current;
1234 DEFINE_WAIT(wait);
1235 struct reclaim_state reclaim_state = {
1236 .reclaimed_slab = 0,
1238 cpumask_t cpumask;
1240 cpumask = node_to_cpumask(pgdat->node_id);
1241 if (!cpus_empty(cpumask))
1242 set_cpus_allowed(tsk, cpumask);
1243 current->reclaim_state = &reclaim_state;
1246 * Tell the memory management that we're a "memory allocator",
1247 * and that if we need more memory we should get access to it
1248 * regardless (see "__alloc_pages()"). "kswapd" should
1249 * never get caught in the normal page freeing logic.
1251 * (Kswapd normally doesn't need memory anyway, but sometimes
1252 * you need a small amount of memory in order to be able to
1253 * page out something else, and this flag essentially protects
1254 * us from recursively trying to free more memory as we're
1255 * trying to free the first piece of memory in the first place).
1257 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
1259 order = 0;
1260 for ( ; ; ) {
1261 unsigned long new_order;
1263 try_to_freeze();
1265 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1266 new_order = pgdat->kswapd_max_order;
1267 pgdat->kswapd_max_order = 0;
1268 if (order < new_order) {
1270 * Don't sleep if someone wants a larger 'order'
1271 * allocation
1273 order = new_order;
1274 } else {
1275 schedule();
1276 order = pgdat->kswapd_max_order;
1278 finish_wait(&pgdat->kswapd_wait, &wait);
1280 balance_pgdat(pgdat, order);
1282 return 0;
1286 * A zone is low on free memory, so wake its kswapd task to service it.
1288 void wakeup_kswapd(struct zone *zone, int order)
1290 pg_data_t *pgdat;
1292 if (!populated_zone(zone))
1293 return;
1295 pgdat = zone->zone_pgdat;
1296 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
1297 return;
1298 if (pgdat->kswapd_max_order < order)
1299 pgdat->kswapd_max_order = order;
1300 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1301 return;
1302 if (!waitqueue_active(&pgdat->kswapd_wait))
1303 return;
1304 wake_up_interruptible(&pgdat->kswapd_wait);
1307 #ifdef CONFIG_PM
1309 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1310 * from LRU lists system-wide, for given pass and priority, and returns the
1311 * number of reclaimed pages
1313 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1315 static unsigned long shrink_all_zones(unsigned long nr_pages, int pass,
1316 int prio, struct scan_control *sc)
1318 struct zone *zone;
1319 unsigned long nr_to_scan, ret = 0;
1321 for_each_zone(zone) {
1323 if (!populated_zone(zone))
1324 continue;
1326 if (zone->all_unreclaimable && prio != DEF_PRIORITY)
1327 continue;
1329 /* For pass = 0 we don't shrink the active list */
1330 if (pass > 0) {
1331 zone->nr_scan_active += (zone->nr_active >> prio) + 1;
1332 if (zone->nr_scan_active >= nr_pages || pass > 3) {
1333 zone->nr_scan_active = 0;
1334 nr_to_scan = min(nr_pages, zone->nr_active);
1335 shrink_active_list(nr_to_scan, zone, sc);
1339 zone->nr_scan_inactive += (zone->nr_inactive >> prio) + 1;
1340 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1341 zone->nr_scan_inactive = 0;
1342 nr_to_scan = min(nr_pages, zone->nr_inactive);
1343 ret += shrink_inactive_list(nr_to_scan, zone, sc);
1344 if (ret >= nr_pages)
1345 return ret;
1349 return ret;
1353 * Try to free `nr_pages' of memory, system-wide, and return the number of
1354 * freed pages.
1356 * Rather than trying to age LRUs the aim is to preserve the overall
1357 * LRU order by reclaiming preferentially
1358 * inactive > active > active referenced > active mapped
1360 unsigned long shrink_all_memory(unsigned long nr_pages)
1362 unsigned long lru_pages, nr_slab;
1363 unsigned long ret = 0;
1364 int pass;
1365 struct reclaim_state reclaim_state;
1366 struct zone *zone;
1367 struct scan_control sc = {
1368 .gfp_mask = GFP_KERNEL,
1369 .may_swap = 0,
1370 .swap_cluster_max = nr_pages,
1371 .may_writepage = 1,
1372 .swappiness = vm_swappiness,
1375 current->reclaim_state = &reclaim_state;
1377 lru_pages = 0;
1378 for_each_zone(zone)
1379 lru_pages += zone->nr_active + zone->nr_inactive;
1381 nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
1382 /* If slab caches are huge, it's better to hit them first */
1383 while (nr_slab >= lru_pages) {
1384 reclaim_state.reclaimed_slab = 0;
1385 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1386 if (!reclaim_state.reclaimed_slab)
1387 break;
1389 ret += reclaim_state.reclaimed_slab;
1390 if (ret >= nr_pages)
1391 goto out;
1393 nr_slab -= reclaim_state.reclaimed_slab;
1397 * We try to shrink LRUs in 5 passes:
1398 * 0 = Reclaim from inactive_list only
1399 * 1 = Reclaim from active list but don't reclaim mapped
1400 * 2 = 2nd pass of type 1
1401 * 3 = Reclaim mapped (normal reclaim)
1402 * 4 = 2nd pass of type 3
1404 for (pass = 0; pass < 5; pass++) {
1405 int prio;
1407 /* Needed for shrinking slab caches later on */
1408 if (!lru_pages)
1409 for_each_zone(zone) {
1410 lru_pages += zone->nr_active;
1411 lru_pages += zone->nr_inactive;
1414 /* Force reclaiming mapped pages in the passes #3 and #4 */
1415 if (pass > 2) {
1416 sc.may_swap = 1;
1417 sc.swappiness = 100;
1420 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1421 unsigned long nr_to_scan = nr_pages - ret;
1423 sc.nr_scanned = 0;
1424 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1425 if (ret >= nr_pages)
1426 goto out;
1428 reclaim_state.reclaimed_slab = 0;
1429 shrink_slab(sc.nr_scanned, sc.gfp_mask, lru_pages);
1430 ret += reclaim_state.reclaimed_slab;
1431 if (ret >= nr_pages)
1432 goto out;
1434 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
1435 blk_congestion_wait(WRITE, HZ / 10);
1438 lru_pages = 0;
1442 * If ret = 0, we could not shrink LRUs, but there may be something
1443 * in slab caches
1445 if (!ret)
1446 do {
1447 reclaim_state.reclaimed_slab = 0;
1448 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1449 ret += reclaim_state.reclaimed_slab;
1450 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
1452 out:
1453 current->reclaim_state = NULL;
1455 return ret;
1457 #endif
1459 #ifdef CONFIG_HOTPLUG_CPU
1460 /* It's optimal to keep kswapds on the same CPUs as their memory, but
1461 not required for correctness. So if the last cpu in a node goes
1462 away, we get changed to run anywhere: as the first one comes back,
1463 restore their cpu bindings. */
1464 static int __devinit cpu_callback(struct notifier_block *nfb,
1465 unsigned long action, void *hcpu)
1467 pg_data_t *pgdat;
1468 cpumask_t mask;
1470 if (action == CPU_ONLINE) {
1471 for_each_online_pgdat(pgdat) {
1472 mask = node_to_cpumask(pgdat->node_id);
1473 if (any_online_cpu(mask) != NR_CPUS)
1474 /* One of our CPUs online: restore mask */
1475 set_cpus_allowed(pgdat->kswapd, mask);
1478 return NOTIFY_OK;
1480 #endif /* CONFIG_HOTPLUG_CPU */
1483 * This kswapd start function will be called by init and node-hot-add.
1484 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
1486 int kswapd_run(int nid)
1488 pg_data_t *pgdat = NODE_DATA(nid);
1489 int ret = 0;
1491 if (pgdat->kswapd)
1492 return 0;
1494 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
1495 if (IS_ERR(pgdat->kswapd)) {
1496 /* failure at boot is fatal */
1497 BUG_ON(system_state == SYSTEM_BOOTING);
1498 printk("Failed to start kswapd on node %d\n",nid);
1499 ret = -1;
1501 return ret;
1504 static int __init kswapd_init(void)
1506 int nid;
1508 swap_setup();
1509 for_each_online_node(nid)
1510 kswapd_run(nid);
1511 hotcpu_notifier(cpu_callback, 0);
1512 return 0;
1515 module_init(kswapd_init)
1517 #ifdef CONFIG_NUMA
1519 * Zone reclaim mode
1521 * If non-zero call zone_reclaim when the number of free pages falls below
1522 * the watermarks.
1524 int zone_reclaim_mode __read_mostly;
1526 #define RECLAIM_OFF 0
1527 #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1528 #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1529 #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1532 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1533 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1534 * a zone.
1536 #define ZONE_RECLAIM_PRIORITY 4
1539 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
1540 * occur.
1542 int sysctl_min_unmapped_ratio = 1;
1545 * If the number of slab pages in a zone grows beyond this percentage then
1546 * slab reclaim needs to occur.
1548 int sysctl_min_slab_ratio = 5;
1551 * Try to free up some pages from this zone through reclaim.
1553 static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1555 /* Minimum pages needed in order to stay on node */
1556 const unsigned long nr_pages = 1 << order;
1557 struct task_struct *p = current;
1558 struct reclaim_state reclaim_state;
1559 int priority;
1560 unsigned long nr_reclaimed = 0;
1561 struct scan_control sc = {
1562 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1563 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
1564 .swap_cluster_max = max_t(unsigned long, nr_pages,
1565 SWAP_CLUSTER_MAX),
1566 .gfp_mask = gfp_mask,
1567 .swappiness = vm_swappiness,
1569 unsigned long slab_reclaimable;
1571 disable_swap_token();
1572 cond_resched();
1574 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1575 * and we also need to be able to write out pages for RECLAIM_WRITE
1576 * and RECLAIM_SWAP.
1578 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
1579 reclaim_state.reclaimed_slab = 0;
1580 p->reclaim_state = &reclaim_state;
1582 if (zone_page_state(zone, NR_FILE_PAGES) -
1583 zone_page_state(zone, NR_FILE_MAPPED) >
1584 zone->min_unmapped_pages) {
1586 * Free memory by calling shrink zone with increasing
1587 * priorities until we have enough memory freed.
1589 priority = ZONE_RECLAIM_PRIORITY;
1590 do {
1591 nr_reclaimed += shrink_zone(priority, zone, &sc);
1592 priority--;
1593 } while (priority >= 0 && nr_reclaimed < nr_pages);
1596 slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1597 if (slab_reclaimable > zone->min_slab_pages) {
1599 * shrink_slab() does not currently allow us to determine how
1600 * many pages were freed in this zone. So we take the current
1601 * number of slab pages and shake the slab until it is reduced
1602 * by the same nr_pages that we used for reclaiming unmapped
1603 * pages.
1605 * Note that shrink_slab will free memory on all zones and may
1606 * take a long time.
1608 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
1609 zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
1610 slab_reclaimable - nr_pages)
1614 * Update nr_reclaimed by the number of slab pages we
1615 * reclaimed from this zone.
1617 nr_reclaimed += slab_reclaimable -
1618 zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1621 p->reclaim_state = NULL;
1622 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
1623 return nr_reclaimed >= nr_pages;
1626 int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1628 cpumask_t mask;
1629 int node_id;
1632 * Zone reclaim reclaims unmapped file backed pages and
1633 * slab pages if we are over the defined limits.
1635 * A small portion of unmapped file backed pages is needed for
1636 * file I/O otherwise pages read by file I/O will be immediately
1637 * thrown out if the zone is overallocated. So we do not reclaim
1638 * if less than a specified percentage of the zone is used by
1639 * unmapped file backed pages.
1641 if (zone_page_state(zone, NR_FILE_PAGES) -
1642 zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
1643 && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
1644 <= zone->min_slab_pages)
1645 return 0;
1648 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1649 * not have reclaimable pages and if we should not delay the allocation
1650 * then do not scan.
1652 if (!(gfp_mask & __GFP_WAIT) ||
1653 zone->all_unreclaimable ||
1654 atomic_read(&zone->reclaim_in_progress) > 0 ||
1655 (current->flags & PF_MEMALLOC))
1656 return 0;
1659 * Only run zone reclaim on the local zone or on zones that do not
1660 * have associated processors. This will favor the local processor
1661 * over remote processors and spread off node memory allocations
1662 * as wide as possible.
1664 node_id = zone_to_nid(zone);
1665 mask = node_to_cpumask(node_id);
1666 if (!cpus_empty(mask) && node_id != numa_node_id())
1667 return 0;
1668 return __zone_reclaim(zone, gfp_mask, order);
1670 #endif