[PATCH] media/video i2c updates
[linux-2.6/history.git] / mm / vmscan.c
bloba8b9d2c450c43acb1ba610b9e360ba2d1b4fadd5
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/slab.h>
16 #include <linux/kernel_stat.h>
17 #include <linux/swap.h>
18 #include <linux/pagemap.h>
19 #include <linux/init.h>
20 #include <linux/highmem.h>
21 #include <linux/file.h>
22 #include <linux/writeback.h>
23 #include <linux/suspend.h>
24 #include <linux/blkdev.h>
25 #include <linux/buffer_head.h> /* for try_to_release_page() */
26 #include <linux/mm_inline.h>
27 #include <linux/pagevec.h>
28 #include <linux/backing-dev.h>
29 #include <linux/rmap-locking.h>
31 #include <asm/pgalloc.h>
32 #include <asm/tlbflush.h>
33 #include <asm/topology.h>
34 #include <asm/div64.h>
36 #include <linux/swapops.h>
39 * The "priority" of VM scanning is how much of the queues we will scan in one
40 * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the
41 * queues ("queue_length >> 12") during an aging round.
43 #define DEF_PRIORITY 12
46 * From 0 .. 100. Higher means more swappy.
48 int vm_swappiness = 60;
49 static long total_memory;
51 #ifdef ARCH_HAS_PREFETCH
52 #define prefetch_prev_lru_page(_page, _base, _field) \
53 do { \
54 if ((_page)->lru.prev != _base) { \
55 struct page *prev; \
57 prev = list_entry(_page->lru.prev, \
58 struct page, lru); \
59 prefetch(&prev->_field); \
60 } \
61 } while (0)
62 #else
63 #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
64 #endif
66 #ifdef ARCH_HAS_PREFETCHW
67 #define prefetchw_prev_lru_page(_page, _base, _field) \
68 do { \
69 if ((_page)->lru.prev != _base) { \
70 struct page *prev; \
72 prev = list_entry(_page->lru.prev, \
73 struct page, lru); \
74 prefetchw(&prev->_field); \
75 } \
76 } while (0)
77 #else
78 #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
79 #endif
82 * The list of shrinker callbacks used by to apply pressure to
83 * ageable caches.
85 struct shrinker {
86 shrinker_t shrinker;
87 struct list_head list;
88 int seeks; /* seeks to recreate an obj */
89 long nr; /* objs pending delete */
92 static LIST_HEAD(shrinker_list);
93 static DECLARE_MUTEX(shrinker_sem);
96 * Add a shrinker callback to be called from the vm
98 struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
100 struct shrinker *shrinker;
102 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
103 if (shrinker) {
104 shrinker->shrinker = theshrinker;
105 shrinker->seeks = seeks;
106 shrinker->nr = 0;
107 down(&shrinker_sem);
108 list_add(&shrinker->list, &shrinker_list);
109 up(&shrinker_sem);
111 return shrinker;
115 * Remove one
117 void remove_shrinker(struct shrinker *shrinker)
119 down(&shrinker_sem);
120 list_del(&shrinker->list);
121 up(&shrinker_sem);
122 kfree(shrinker);
125 #define SHRINK_BATCH 128
127 * Call the shrink functions to age shrinkable caches
129 * Here we assume it costs one seek to replace a lru page and that it also
130 * takes a seek to recreate a cache object. With this in mind we age equal
131 * percentages of the lru and ageable caches. This should balance the seeks
132 * generated by these structures.
134 * If the vm encounted mapped pages on the LRU it increase the pressure on
135 * slab to avoid swapping.
137 * FIXME: do not do for zone highmem
139 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
141 static int shrink_slab(long scanned, unsigned int gfp_mask)
143 struct shrinker *shrinker;
144 long pages;
146 if (down_trylock(&shrinker_sem))
147 return 0;
149 pages = nr_used_zone_pages();
150 list_for_each_entry(shrinker, &shrinker_list, list) {
151 long long delta;
153 delta = scanned * shrinker->seeks;
154 delta *= (*shrinker->shrinker)(0, gfp_mask);
155 do_div(delta, pages + 1);
156 shrinker->nr += delta;
157 if (shrinker->nr > SHRINK_BATCH) {
158 long nr_to_scan = shrinker->nr;
160 shrinker->nr = 0;
161 while (nr_to_scan) {
162 long this_scan = nr_to_scan;
164 if (this_scan > 128)
165 this_scan = 128;
166 (*shrinker->shrinker)(this_scan, gfp_mask);
167 nr_to_scan -= this_scan;
168 cond_resched();
172 up(&shrinker_sem);
173 return 0;
176 /* Must be called with page's pte_chain_lock held. */
177 static inline int page_mapping_inuse(struct page *page)
179 struct address_space *mapping = page->mapping;
181 /* Page is in somebody's page tables. */
182 if (page_mapped(page))
183 return 1;
185 /* XXX: does this happen ? */
186 if (!mapping)
187 return 0;
189 /* Be more reluctant to reclaim swapcache than pagecache */
190 if (PageSwapCache(page))
191 return 1;
193 /* File is mmap'd by somebody. */
194 if (!list_empty(&mapping->i_mmap))
195 return 1;
196 if (!list_empty(&mapping->i_mmap_shared))
197 return 1;
199 return 0;
202 static inline int is_page_cache_freeable(struct page *page)
204 return page_count(page) - !!PagePrivate(page) == 2;
207 static int may_write_to_queue(struct backing_dev_info *bdi)
209 if (current_is_kswapd())
210 return 1;
211 if (current_is_pdflush()) /* This is unlikely, but why not... */
212 return 1;
213 if (!bdi_write_congested(bdi))
214 return 1;
215 if (bdi == current->backing_dev_info)
216 return 1;
217 return 0;
221 * shrink_list returns the number of reclaimed pages
223 static int
224 shrink_list(struct list_head *page_list, unsigned int gfp_mask,
225 int *max_scan, int *nr_mapped)
227 struct address_space *mapping;
228 LIST_HEAD(ret_pages);
229 struct pagevec freed_pvec;
230 int pgactivate = 0;
231 int ret = 0;
233 cond_resched();
235 pagevec_init(&freed_pvec, 1);
236 while (!list_empty(page_list)) {
237 struct page *page;
238 int may_enter_fs;
240 page = list_entry(page_list->prev, struct page, lru);
241 list_del(&page->lru);
243 if (TestSetPageLocked(page))
244 goto keep;
246 /* Double the slab pressure for mapped and swapcache pages */
247 if (page_mapped(page) || PageSwapCache(page))
248 (*nr_mapped)++;
250 BUG_ON(PageActive(page));
251 may_enter_fs = (gfp_mask & __GFP_FS) ||
252 (PageSwapCache(page) && (gfp_mask & __GFP_IO));
254 if (PageWriteback(page))
255 goto keep_locked;
257 pte_chain_lock(page);
258 if (page_referenced(page) && page_mapping_inuse(page)) {
259 /* In active use or really unfreeable. Activate it. */
260 pte_chain_unlock(page);
261 goto activate_locked;
264 mapping = page->mapping;
266 #ifdef CONFIG_SWAP
268 * Anonymous process memory without backing store. Try to
269 * allocate it some swap space here.
271 * XXX: implement swap clustering ?
273 if (page_mapped(page) && !mapping && !PagePrivate(page)) {
274 pte_chain_unlock(page);
275 if (!add_to_swap(page))
276 goto activate_locked;
277 pte_chain_lock(page);
278 mapping = page->mapping;
282 * The page is mapped into the page tables of one or more
283 * processes. Try to unmap it here.
285 if (page_mapped(page) && mapping) {
286 switch (try_to_unmap(page)) {
287 case SWAP_ERROR:
288 case SWAP_FAIL:
289 pte_chain_unlock(page);
290 goto activate_locked;
291 case SWAP_AGAIN:
292 pte_chain_unlock(page);
293 goto keep_locked;
294 case SWAP_SUCCESS:
295 ; /* try to free the page below */
298 #endif /* CONFIG_SWAP */
299 pte_chain_unlock(page);
302 * If the page is dirty, only perform writeback if that write
303 * will be non-blocking. To prevent this allocation from being
304 * stalled by pagecache activity. But note that there may be
305 * stalls if we need to run get_block(). We could test
306 * PagePrivate for that.
308 * If this process is currently in generic_file_write() against
309 * this page's queue, we can perform writeback even if that
310 * will block.
312 * If the page is swapcache, write it back even if that would
313 * block, for some throttling. This happens by accident, because
314 * swap_backing_dev_info is bust: it doesn't reflect the
315 * congestion state of the swapdevs. Easy to fix, if needed.
316 * See swapfile.c:page_queue_congested().
318 if (PageDirty(page)) {
319 if (!is_page_cache_freeable(page))
320 goto keep_locked;
321 if (!mapping)
322 goto keep_locked;
323 if (mapping->a_ops->writepage == NULL)
324 goto activate_locked;
325 if (!may_enter_fs)
326 goto keep_locked;
327 if (!may_write_to_queue(mapping->backing_dev_info))
328 goto keep_locked;
329 write_lock(&mapping->page_lock);
330 if (test_clear_page_dirty(page)) {
331 int res;
332 struct writeback_control wbc = {
333 .sync_mode = WB_SYNC_NONE,
334 .nr_to_write = SWAP_CLUSTER_MAX,
335 .nonblocking = 1,
336 .for_reclaim = 1,
339 list_move(&page->list, &mapping->locked_pages);
340 write_unlock(&mapping->page_lock);
342 SetPageReclaim(page);
343 res = mapping->a_ops->writepage(page, &wbc);
345 if (res == WRITEPAGE_ACTIVATE) {
346 ClearPageReclaim(page);
347 goto activate_locked;
349 if (!PageWriteback(page)) {
350 /* synchronous write or broken a_ops? */
351 ClearPageReclaim(page);
353 goto keep;
355 write_unlock(&mapping->page_lock);
359 * If the page has buffers, try to free the buffer mappings
360 * associated with this page. If we succeed we try to free
361 * the page as well.
363 * We do this even if the page is PageDirty().
364 * try_to_release_page() does not perform I/O, but it is
365 * possible for a page to have PageDirty set, but it is actually
366 * clean (all its buffers are clean). This happens if the
367 * buffers were written out directly, with submit_bh(). ext3
368 * will do this, as well as the blockdev mapping.
369 * try_to_release_page() will discover that cleanness and will
370 * drop the buffers and mark the page clean - it can be freed.
372 * Rarely, pages can have buffers and no ->mapping. These are
373 * the pages which were not successfully invalidated in
374 * truncate_complete_page(). We try to drop those buffers here
375 * and if that worked, and the page is no longer mapped into
376 * process address space (page_count == 0) it can be freed.
377 * Otherwise, leave the page on the LRU so it is swappable.
379 if (PagePrivate(page)) {
380 if (!try_to_release_page(page, gfp_mask))
381 goto activate_locked;
382 if (!mapping && page_count(page) == 1)
383 goto free_it;
386 if (!mapping)
387 goto keep_locked; /* truncate got there first */
389 write_lock(&mapping->page_lock);
392 * The non-racy check for busy page. It is critical to check
393 * PageDirty _after_ making sure that the page is freeable and
394 * not in use by anybody. (pagecache + us == 2)
396 if (page_count(page) != 2 || PageDirty(page)) {
397 write_unlock(&mapping->page_lock);
398 goto keep_locked;
401 #ifdef CONFIG_SWAP
402 if (PageSwapCache(page)) {
403 swp_entry_t swap = { .val = page->index };
404 __delete_from_swap_cache(page);
405 write_unlock(&mapping->page_lock);
406 swap_free(swap);
407 __put_page(page); /* The pagecache ref */
408 goto free_it;
410 #endif /* CONFIG_SWAP */
412 __remove_from_page_cache(page);
413 write_unlock(&mapping->page_lock);
414 __put_page(page);
416 free_it:
417 unlock_page(page);
418 ret++;
419 if (!pagevec_add(&freed_pvec, page))
420 __pagevec_release_nonlru(&freed_pvec);
421 continue;
423 activate_locked:
424 SetPageActive(page);
425 pgactivate++;
426 keep_locked:
427 unlock_page(page);
428 keep:
429 list_add(&page->lru, &ret_pages);
430 BUG_ON(PageLRU(page));
432 list_splice(&ret_pages, page_list);
433 if (pagevec_count(&freed_pvec))
434 __pagevec_release_nonlru(&freed_pvec);
435 mod_page_state(pgsteal, ret);
436 if (current_is_kswapd())
437 mod_page_state(kswapd_steal, ret);
438 mod_page_state(pgactivate, pgactivate);
439 return ret;
443 * zone->lru_lock is heavily contented. We relieve it by quickly privatising
444 * a batch of pages and working on them outside the lock. Any pages which were
445 * not freed will be added back to the LRU.
447 * shrink_cache() is passed the number of pages to try to free, and returns
448 * the number of pages which were reclaimed.
450 * For pagecache intensive workloads, the first loop here is the hottest spot
451 * in the kernel (apart from the copy_*_user functions).
453 static int
454 shrink_cache(const int nr_pages, struct zone *zone,
455 unsigned int gfp_mask, int max_scan, int *nr_mapped)
457 LIST_HEAD(page_list);
458 struct pagevec pvec;
459 int nr_to_process;
460 int ret = 0;
463 * Try to ensure that we free `nr_pages' pages in one pass of the loop.
465 nr_to_process = nr_pages;
466 if (nr_to_process < SWAP_CLUSTER_MAX)
467 nr_to_process = SWAP_CLUSTER_MAX;
469 pagevec_init(&pvec, 1);
471 lru_add_drain();
472 spin_lock_irq(&zone->lru_lock);
473 while (max_scan > 0 && ret < nr_pages) {
474 struct page *page;
475 int nr_taken = 0;
476 int nr_scan = 0;
477 int nr_freed;
479 while (nr_scan++ < nr_to_process &&
480 !list_empty(&zone->inactive_list)) {
481 page = list_entry(zone->inactive_list.prev,
482 struct page, lru);
484 prefetchw_prev_lru_page(page,
485 &zone->inactive_list, flags);
487 if (!TestClearPageLRU(page))
488 BUG();
489 list_del(&page->lru);
490 if (page_count(page) == 0) {
491 /* It is currently in pagevec_release() */
492 SetPageLRU(page);
493 list_add(&page->lru, &zone->inactive_list);
494 continue;
496 list_add(&page->lru, &page_list);
497 page_cache_get(page);
498 nr_taken++;
500 zone->nr_inactive -= nr_taken;
501 zone->pages_scanned += nr_taken;
502 spin_unlock_irq(&zone->lru_lock);
504 if (nr_taken == 0)
505 goto done;
507 max_scan -= nr_scan;
508 mod_page_state(pgscan, nr_scan);
509 nr_freed = shrink_list(&page_list, gfp_mask,
510 &max_scan, nr_mapped);
511 ret += nr_freed;
512 if (nr_freed <= 0 && list_empty(&page_list))
513 goto done;
515 spin_lock_irq(&zone->lru_lock);
517 * Put back any unfreeable pages.
519 while (!list_empty(&page_list)) {
520 page = list_entry(page_list.prev, struct page, lru);
521 if (TestSetPageLRU(page))
522 BUG();
523 list_del(&page->lru);
524 if (PageActive(page))
525 add_page_to_active_list(zone, page);
526 else
527 add_page_to_inactive_list(zone, page);
528 if (!pagevec_add(&pvec, page)) {
529 spin_unlock_irq(&zone->lru_lock);
530 __pagevec_release(&pvec);
531 spin_lock_irq(&zone->lru_lock);
535 spin_unlock_irq(&zone->lru_lock);
536 done:
537 pagevec_release(&pvec);
538 return ret;
542 * This moves pages from the active list to the inactive list.
544 * We move them the other way if the page is referenced by one or more
545 * processes, from rmap.
547 * If the pages are mostly unmapped, the processing is fast and it is
548 * appropriate to hold zone->lru_lock across the whole operation. But if
549 * the pages are mapped, the processing is slow (page_referenced()) so we
550 * should drop zone->lru_lock around each page. It's impossible to balance
551 * this, so instead we remove the pages from the LRU while processing them.
552 * It is safe to rely on PG_active against the non-LRU pages in here because
553 * nobody will play with that bit on a non-LRU page.
555 * The downside is that we have to touch page->count against each page.
556 * But we had to alter page->flags anyway.
558 static void
559 refill_inactive_zone(struct zone *zone, const int nr_pages_in,
560 struct page_state *ps, int priority)
562 int pgdeactivate = 0;
563 int nr_pages = nr_pages_in;
564 LIST_HEAD(l_hold); /* The pages which were snipped off */
565 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
566 LIST_HEAD(l_active); /* Pages to go onto the active_list */
567 struct page *page;
568 struct pagevec pvec;
569 int reclaim_mapped = 0;
570 long mapped_ratio;
571 long distress;
572 long swap_tendency;
574 lru_add_drain();
575 spin_lock_irq(&zone->lru_lock);
576 while (nr_pages && !list_empty(&zone->active_list)) {
577 page = list_entry(zone->active_list.prev, struct page, lru);
578 prefetchw_prev_lru_page(page, &zone->active_list, flags);
579 if (!TestClearPageLRU(page))
580 BUG();
581 list_del(&page->lru);
582 if (page_count(page) == 0) {
583 /* It is currently in pagevec_release() */
584 SetPageLRU(page);
585 list_add(&page->lru, &zone->active_list);
586 } else {
587 page_cache_get(page);
588 list_add(&page->lru, &l_hold);
590 nr_pages--;
592 spin_unlock_irq(&zone->lru_lock);
595 * `distress' is a measure of how much trouble we're having reclaiming
596 * pages. 0 -> no problems. 100 -> great trouble.
598 distress = 100 >> priority;
601 * The point of this algorithm is to decide when to start reclaiming
602 * mapped memory instead of just pagecache. Work out how much memory
603 * is mapped.
605 mapped_ratio = (ps->nr_mapped * 100) / total_memory;
608 * Now decide how much we really want to unmap some pages. The mapped
609 * ratio is downgraded - just because there's a lot of mapped memory
610 * doesn't necessarily mean that page reclaim isn't succeeding.
612 * The distress ratio is important - we don't want to start going oom.
614 * A 100% value of vm_swappiness overrides this algorithm altogether.
616 swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
619 * Now use this metric to decide whether to start moving mapped memory
620 * onto the inactive list.
622 if (swap_tendency >= 100)
623 reclaim_mapped = 1;
625 while (!list_empty(&l_hold)) {
626 page = list_entry(l_hold.prev, struct page, lru);
627 list_del(&page->lru);
628 if (page_mapped(page)) {
629 pte_chain_lock(page);
630 if (page_mapped(page) && page_referenced(page)) {
631 pte_chain_unlock(page);
632 list_add(&page->lru, &l_active);
633 continue;
635 pte_chain_unlock(page);
636 if (!reclaim_mapped) {
637 list_add(&page->lru, &l_active);
638 continue;
642 * FIXME: need to consider page_count(page) here if/when we
643 * reap orphaned pages via the LRU (Daniel's locking stuff)
645 if (total_swap_pages == 0 && !page->mapping &&
646 !PagePrivate(page)) {
647 list_add(&page->lru, &l_active);
648 continue;
650 list_add(&page->lru, &l_inactive);
651 pgdeactivate++;
654 pagevec_init(&pvec, 1);
655 spin_lock_irq(&zone->lru_lock);
656 while (!list_empty(&l_inactive)) {
657 page = list_entry(l_inactive.prev, struct page, lru);
658 prefetchw_prev_lru_page(page, &l_inactive, flags);
659 if (TestSetPageLRU(page))
660 BUG();
661 if (!TestClearPageActive(page))
662 BUG();
663 list_move(&page->lru, &zone->inactive_list);
664 if (!pagevec_add(&pvec, page)) {
665 spin_unlock_irq(&zone->lru_lock);
666 if (buffer_heads_over_limit)
667 pagevec_strip(&pvec);
668 __pagevec_release(&pvec);
669 spin_lock_irq(&zone->lru_lock);
672 if (buffer_heads_over_limit) {
673 spin_unlock_irq(&zone->lru_lock);
674 pagevec_strip(&pvec);
675 spin_lock_irq(&zone->lru_lock);
677 while (!list_empty(&l_active)) {
678 page = list_entry(l_active.prev, struct page, lru);
679 prefetchw_prev_lru_page(page, &l_active, flags);
680 if (TestSetPageLRU(page))
681 BUG();
682 BUG_ON(!PageActive(page));
683 list_move(&page->lru, &zone->active_list);
684 if (!pagevec_add(&pvec, page)) {
685 spin_unlock_irq(&zone->lru_lock);
686 __pagevec_release(&pvec);
687 spin_lock_irq(&zone->lru_lock);
690 zone->nr_active -= pgdeactivate;
691 zone->nr_inactive += pgdeactivate;
692 spin_unlock_irq(&zone->lru_lock);
693 pagevec_release(&pvec);
695 mod_page_state(pgrefill, nr_pages_in - nr_pages);
696 mod_page_state(pgdeactivate, pgdeactivate);
700 * Try to reclaim `nr_pages' from this zone. Returns the number of reclaimed
701 * pages. This is a basic per-zone page freer. Used by both kswapd and
702 * direct reclaim.
704 static int
705 shrink_zone(struct zone *zone, int max_scan, unsigned int gfp_mask,
706 const int nr_pages, int *nr_mapped, struct page_state *ps, int priority)
708 unsigned long ratio;
711 * Try to keep the active list 2/3 of the size of the cache. And
712 * make sure that refill_inactive is given a decent number of pages.
714 * The "ratio+1" here is important. With pagecache-intensive workloads
715 * the inactive list is huge, and `ratio' evaluates to zero all the
716 * time. Which pins the active list memory. So we add one to `ratio'
717 * just to make sure that the kernel will slowly sift through the
718 * active list.
720 ratio = (unsigned long)nr_pages * zone->nr_active /
721 ((zone->nr_inactive | 1) * 2);
722 atomic_add(ratio+1, &zone->refill_counter);
723 if (atomic_read(&zone->refill_counter) > SWAP_CLUSTER_MAX) {
724 int count;
727 * Don't try to bring down too many pages in one attempt.
728 * If this fails, the caller will increase `priority' and
729 * we'll try again, with an increased chance of reclaiming
730 * mapped memory.
732 count = atomic_read(&zone->refill_counter);
733 if (count > SWAP_CLUSTER_MAX * 4)
734 count = SWAP_CLUSTER_MAX * 4;
735 atomic_sub(count, &zone->refill_counter);
736 refill_inactive_zone(zone, count, ps, priority);
738 return shrink_cache(nr_pages, zone, gfp_mask,
739 max_scan, nr_mapped);
743 * This is the direct reclaim path, for page-allocating processes. We only
744 * try to reclaim pages from zones which will satisfy the caller's allocation
745 * request.
747 * We reclaim from a zone even if that zone is over pages_high. Because:
748 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
749 * allocation or
750 * b) The zones may be over pages_high but they must go *over* pages_high to
751 * satisfy the `incremental min' zone defense algorithm.
753 * Returns the number of reclaimed pages.
755 * If a zone is deemed to be full of pinned pages then just give it a light
756 * scan then give up on it.
758 static int
759 shrink_caches(struct zone *classzone, int priority, int *total_scanned,
760 int gfp_mask, const int nr_pages, struct page_state *ps)
762 struct zone *first_classzone;
763 struct zone *zone;
764 int ret = 0;
766 first_classzone = classzone->zone_pgdat->node_zones;
767 for (zone = classzone; zone >= first_classzone; zone--) {
768 int to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX);
769 int nr_mapped = 0;
770 int max_scan;
772 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
773 continue; /* Let kswapd poll it */
776 * If we cannot reclaim `nr_pages' pages by scanning twice
777 * that many pages then fall back to the next zone.
779 max_scan = zone->nr_inactive >> priority;
780 if (max_scan < to_reclaim * 2)
781 max_scan = to_reclaim * 2;
782 ret += shrink_zone(zone, max_scan, gfp_mask,
783 to_reclaim, &nr_mapped, ps, priority);
784 *total_scanned += max_scan + nr_mapped;
785 if (ret >= nr_pages)
786 break;
788 return ret;
792 * This is the main entry point to direct page reclaim.
794 * If a full scan of the inactive list fails to free enough memory then we
795 * are "out of memory" and something needs to be killed.
797 * If the caller is !__GFP_FS then the probability of a failure is reasonably
798 * high - the zone may be full of dirty or under-writeback pages, which this
799 * caller can't do much about. So for !__GFP_FS callers, we just perform a
800 * small LRU walk and if that didn't work out, fail the allocation back to the
801 * caller. GFP_NOFS allocators need to know how to deal with it. Kicking
802 * bdflush, waiting and retrying will work.
804 * This is a fairly lame algorithm - it can result in excessive CPU burning and
805 * excessive rotation of the inactive list, which is _supposed_ to be an LRU,
806 * yes?
809 try_to_free_pages(struct zone *classzone,
810 unsigned int gfp_mask, unsigned int order)
812 int priority;
813 const int nr_pages = SWAP_CLUSTER_MAX;
814 int nr_reclaimed = 0;
816 inc_page_state(allocstall);
818 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
819 int total_scanned = 0;
820 struct page_state ps;
822 get_page_state(&ps);
823 nr_reclaimed += shrink_caches(classzone, priority,
824 &total_scanned, gfp_mask,
825 nr_pages, &ps);
826 if (nr_reclaimed >= nr_pages)
827 return 1;
828 if (!(gfp_mask & __GFP_FS))
829 break; /* Let the caller handle it */
831 * Try to write back as many pages as we just scanned. Not
832 * sure if that makes sense, but it's an attempt to avoid
833 * creating IO storms unnecessarily
835 wakeup_bdflush(total_scanned);
837 /* Take a nap, wait for some writeback to complete */
838 blk_congestion_wait(WRITE, HZ/4);
839 shrink_slab(total_scanned, gfp_mask);
841 if (gfp_mask & __GFP_FS)
842 out_of_memory();
843 return 0;
847 * For kswapd, balance_pgdat() will work across all this node's zones until
848 * they are all at pages_high.
850 * If `nr_pages' is non-zero then it is the number of pages which are to be
851 * reclaimed, regardless of the zone occupancies. This is a software suspend
852 * special.
854 * Returns the number of pages which were actually freed.
856 * There is special handling here for zones which are full of pinned pages.
857 * This can happen if the pages are all mlocked, or if they are all used by
858 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
859 * What we do is to detect the case where all pages in the zone have been
860 * scanned twice and there has been zero successful reclaim. Mark the zone as
861 * dead and from now on, only perform a short scan. Basically we're polling
862 * the zone for when the problem goes away.
864 static int balance_pgdat(pg_data_t *pgdat, int nr_pages, struct page_state *ps)
866 int to_free = nr_pages;
867 int priority;
868 int i;
870 inc_page_state(pageoutrun);
872 for (priority = DEF_PRIORITY; priority; priority--) {
873 int all_zones_ok = 1;
875 for (i = 0; i < pgdat->nr_zones; i++) {
876 struct zone *zone = pgdat->node_zones + i;
877 int nr_mapped = 0;
878 int max_scan;
879 int to_reclaim;
881 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
882 continue;
884 if (nr_pages && to_free > 0) { /* Software suspend */
885 to_reclaim = min(to_free, SWAP_CLUSTER_MAX*8);
886 } else { /* Zone balancing */
887 to_reclaim = zone->pages_high-zone->free_pages;
888 if (to_reclaim <= 0)
889 continue;
891 all_zones_ok = 0;
892 max_scan = zone->nr_inactive >> priority;
893 if (max_scan < to_reclaim * 2)
894 max_scan = to_reclaim * 2;
895 if (max_scan < SWAP_CLUSTER_MAX)
896 max_scan = SWAP_CLUSTER_MAX;
897 to_free -= shrink_zone(zone, max_scan, GFP_KSWAPD,
898 to_reclaim, &nr_mapped, ps, priority);
899 shrink_slab(max_scan + nr_mapped, GFP_KSWAPD);
900 if (zone->all_unreclaimable)
901 continue;
902 if (zone->pages_scanned > zone->present_pages * 2)
903 zone->all_unreclaimable = 1;
905 if (all_zones_ok)
906 break;
907 blk_congestion_wait(WRITE, HZ/4);
909 return nr_pages - to_free;
913 * The background pageout daemon, started as a kernel thread
914 * from the init process.
916 * This basically trickles out pages so that we have _some_
917 * free memory available even if there is no other activity
918 * that frees anything up. This is needed for things like routing
919 * etc, where we otherwise might have all activity going on in
920 * asynchronous contexts that cannot page things out.
922 * If there are applications that are active memory-allocators
923 * (most normal use), this basically shouldn't matter.
925 int kswapd(void *p)
927 pg_data_t *pgdat = (pg_data_t*)p;
928 struct task_struct *tsk = current;
929 DEFINE_WAIT(wait);
931 daemonize();
932 set_cpus_allowed(tsk, __node_to_cpu_mask(pgdat->node_id));
933 sprintf(tsk->comm, "kswapd%d", pgdat->node_id);
934 sigfillset(&tsk->blocked);
937 * Tell the memory management that we're a "memory allocator",
938 * and that if we need more memory we should get access to it
939 * regardless (see "__alloc_pages()"). "kswapd" should
940 * never get caught in the normal page freeing logic.
942 * (Kswapd normally doesn't need memory anyway, but sometimes
943 * you need a small amount of memory in order to be able to
944 * page out something else, and this flag essentially protects
945 * us from recursively trying to free more memory as we're
946 * trying to free the first piece of memory in the first place).
948 tsk->flags |= PF_MEMALLOC|PF_KSWAPD;
950 for ( ; ; ) {
951 struct page_state ps;
953 if (current->flags & PF_FREEZE)
954 refrigerator(PF_IOTHREAD);
955 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
956 schedule();
957 finish_wait(&pgdat->kswapd_wait, &wait);
958 get_page_state(&ps);
959 balance_pgdat(pgdat, 0, &ps);
960 blk_run_queues();
965 * A zone is low on free memory, so wake its kswapd task to service it.
967 void wakeup_kswapd(struct zone *zone)
969 if (zone->free_pages > zone->pages_low)
970 return;
971 if (!waitqueue_active(&zone->zone_pgdat->kswapd_wait))
972 return;
973 wake_up_interruptible(&zone->zone_pgdat->kswapd_wait);
976 #ifdef CONFIG_SOFTWARE_SUSPEND
978 * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
979 * pages.
981 int shrink_all_memory(int nr_pages)
983 pg_data_t *pgdat;
984 int nr_to_free = nr_pages;
985 int ret = 0;
987 for_each_pgdat(pgdat) {
988 int freed;
989 struct page_state ps;
991 get_page_state(&ps);
992 freed = balance_pgdat(pgdat, nr_to_free, &ps);
993 ret += freed;
994 nr_to_free -= freed;
995 if (nr_to_free <= 0)
996 break;
998 return ret;
1000 #endif
1002 static int __init kswapd_init(void)
1004 pg_data_t *pgdat;
1005 swap_setup();
1006 for_each_pgdat(pgdat)
1007 kernel_thread(kswapd, pgdat, CLONE_KERNEL);
1008 total_memory = nr_free_pagecache_pages();
1009 return 0;
1012 module_init(kswapd_init)