blob: 078cf920208af05e1e5d250dc7941ee134d02ada [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/vmscan.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 *
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.
12 */
13
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
37#include <asm/tlbflush.h>
38#include <asm/div64.h>
39
40#include <linux/swapops.h>
41
42/* possible outcome of pageout() */
43typedef enum {
44 /* failed to write page out, page is locked */
45 PAGE_KEEP,
46 /* move page to the active list, page is locked */
47 PAGE_ACTIVATE,
48 /* page has been sent to the disk successfully, page is unlocked */
49 PAGE_SUCCESS,
50 /* page is clean and locked */
51 PAGE_CLEAN,
52} pageout_t;
53
54struct scan_control {
55 /* Ask refill_inactive_zone, or shrink_cache to scan this many pages */
56 unsigned long nr_to_scan;
57
58 /* Incremented by the number of inactive pages that were scanned */
59 unsigned long nr_scanned;
60
61 /* Incremented by the number of pages reclaimed */
62 unsigned long nr_reclaimed;
63
64 unsigned long nr_mapped; /* From page_state */
65
66 /* How many pages shrink_cache() should reclaim */
67 int nr_to_reclaim;
68
69 /* Ask shrink_caches, or shrink_zone to scan at this priority */
70 unsigned int priority;
71
72 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040073 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 int may_writepage;
76
Martin Hicksbfbb38f2005-06-21 17:14:40 -070077 /* Can pages be swapped as part of reclaim? */
78 int may_swap;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
81 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
82 * In this context, it doesn't matter that we scan the
83 * whole list at once. */
84 int swap_cluster_max;
85};
86
87/*
88 * The list of shrinker callbacks used by to apply pressure to
89 * ageable caches.
90 */
91struct shrinker {
92 shrinker_t shrinker;
93 struct list_head list;
94 int seeks; /* seeks to recreate an obj */
95 long nr; /* objs pending delete */
96};
97
98#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
99
100#ifdef ARCH_HAS_PREFETCH
101#define prefetch_prev_lru_page(_page, _base, _field) \
102 do { \
103 if ((_page)->lru.prev != _base) { \
104 struct page *prev; \
105 \
106 prev = lru_to_page(&(_page->lru)); \
107 prefetch(&prev->_field); \
108 } \
109 } while (0)
110#else
111#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
112#endif
113
114#ifdef ARCH_HAS_PREFETCHW
115#define prefetchw_prev_lru_page(_page, _base, _field) \
116 do { \
117 if ((_page)->lru.prev != _base) { \
118 struct page *prev; \
119 \
120 prev = lru_to_page(&(_page->lru)); \
121 prefetchw(&prev->_field); \
122 } \
123 } while (0)
124#else
125#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
126#endif
127
128/*
129 * From 0 .. 100. Higher means more swappy.
130 */
131int vm_swappiness = 60;
132static long total_memory;
133
134static LIST_HEAD(shrinker_list);
135static DECLARE_RWSEM(shrinker_rwsem);
136
137/*
138 * Add a shrinker callback to be called from the vm
139 */
140struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
141{
142 struct shrinker *shrinker;
143
144 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
145 if (shrinker) {
146 shrinker->shrinker = theshrinker;
147 shrinker->seeks = seeks;
148 shrinker->nr = 0;
149 down_write(&shrinker_rwsem);
150 list_add_tail(&shrinker->list, &shrinker_list);
151 up_write(&shrinker_rwsem);
152 }
153 return shrinker;
154}
155EXPORT_SYMBOL(set_shrinker);
156
157/*
158 * Remove one
159 */
160void remove_shrinker(struct shrinker *shrinker)
161{
162 down_write(&shrinker_rwsem);
163 list_del(&shrinker->list);
164 up_write(&shrinker_rwsem);
165 kfree(shrinker);
166}
167EXPORT_SYMBOL(remove_shrinker);
168
169#define SHRINK_BATCH 128
170/*
171 * Call the shrink functions to age shrinkable caches
172 *
173 * Here we assume it costs one seek to replace a lru page and that it also
174 * takes a seek to recreate a cache object. With this in mind we age equal
175 * percentages of the lru and ageable caches. This should balance the seeks
176 * generated by these structures.
177 *
178 * If the vm encounted mapped pages on the LRU it increase the pressure on
179 * slab to avoid swapping.
180 *
181 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
182 *
183 * `lru_pages' represents the number of on-LRU pages in all the zones which
184 * are eligible for the caller's allocation attempt. It is used for balancing
185 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700186 *
187 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
Al Viro6daa0e22005-10-21 03:18:50 -0400189static int shrink_slab(unsigned long scanned, gfp_t gfp_mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned long lru_pages)
191{
192 struct shrinker *shrinker;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700193 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (scanned == 0)
196 scanned = SWAP_CLUSTER_MAX;
197
198 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700199 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 list_for_each_entry(shrinker, &shrinker_list, list) {
202 unsigned long long delta;
203 unsigned long total_scan;
204
205 delta = (4 * scanned) / shrinker->seeks;
206 delta *= (*shrinker->shrinker)(0, gfp_mask);
207 do_div(delta, lru_pages + 1);
208 shrinker->nr += delta;
209 if (shrinker->nr < 0)
210 shrinker->nr = LONG_MAX; /* It wrapped! */
211
212 total_scan = shrinker->nr;
213 shrinker->nr = 0;
214
215 while (total_scan >= SHRINK_BATCH) {
216 long this_scan = SHRINK_BATCH;
217 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700218 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700220 nr_before = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
222 if (shrink_ret == -1)
223 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700224 if (shrink_ret < nr_before)
225 ret += nr_before - shrink_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 mod_page_state(slabs_scanned, this_scan);
227 total_scan -= this_scan;
228
229 cond_resched();
230 }
231
232 shrinker->nr += total_scan;
233 }
234 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/* Called without lock on whether page is mapped, so answer is unstable */
239static inline int page_mapping_inuse(struct page *page)
240{
241 struct address_space *mapping;
242
243 /* Page is in somebody's page tables. */
244 if (page_mapped(page))
245 return 1;
246
247 /* Be more reluctant to reclaim swapcache than pagecache */
248 if (PageSwapCache(page))
249 return 1;
250
251 mapping = page_mapping(page);
252 if (!mapping)
253 return 0;
254
255 /* File is mmap'd by somebody? */
256 return mapping_mapped(mapping);
257}
258
259static inline int is_page_cache_freeable(struct page *page)
260{
261 return page_count(page) - !!PagePrivate(page) == 2;
262}
263
264static int may_write_to_queue(struct backing_dev_info *bdi)
265{
266 if (current_is_kswapd())
267 return 1;
268 if (current_is_pdflush()) /* This is unlikely, but why not... */
269 return 1;
270 if (!bdi_write_congested(bdi))
271 return 1;
272 if (bdi == current->backing_dev_info)
273 return 1;
274 return 0;
275}
276
277/*
278 * We detected a synchronous write error writing a page out. Probably
279 * -ENOSPC. We need to propagate that into the address_space for a subsequent
280 * fsync(), msync() or close().
281 *
282 * The tricky part is that after writepage we cannot touch the mapping: nothing
283 * prevents it from being freed up. But we have a ref on the page and once
284 * that page is locked, the mapping is pinned.
285 *
286 * We're allowed to run sleeping lock_page() here because we know the caller has
287 * __GFP_FS.
288 */
289static void handle_write_error(struct address_space *mapping,
290 struct page *page, int error)
291{
292 lock_page(page);
293 if (page_mapping(page) == mapping) {
294 if (error == -ENOSPC)
295 set_bit(AS_ENOSPC, &mapping->flags);
296 else
297 set_bit(AS_EIO, &mapping->flags);
298 }
299 unlock_page(page);
300}
301
302/*
303 * pageout is called by shrink_list() for each dirty page. Calls ->writepage().
304 */
305static pageout_t pageout(struct page *page, struct address_space *mapping)
306{
307 /*
308 * If the page is dirty, only perform writeback if that write
309 * will be non-blocking. To prevent this allocation from being
310 * stalled by pagecache activity. But note that there may be
311 * stalls if we need to run get_block(). We could test
312 * PagePrivate for that.
313 *
314 * If this process is currently in generic_file_write() against
315 * this page's queue, we can perform writeback even if that
316 * will block.
317 *
318 * If the page is swapcache, write it back even if that would
319 * block, for some throttling. This happens by accident, because
320 * swap_backing_dev_info is bust: it doesn't reflect the
321 * congestion state of the swapdevs. Easy to fix, if needed.
322 * See swapfile.c:page_queue_congested().
323 */
324 if (!is_page_cache_freeable(page))
325 return PAGE_KEEP;
326 if (!mapping) {
327 /*
328 * Some data journaling orphaned pages can have
329 * page->mapping == NULL while being dirty with clean buffers.
330 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700331 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (try_to_free_buffers(page)) {
333 ClearPageDirty(page);
334 printk("%s: orphaned page\n", __FUNCTION__);
335 return PAGE_CLEAN;
336 }
337 }
338 return PAGE_KEEP;
339 }
340 if (mapping->a_ops->writepage == NULL)
341 return PAGE_ACTIVATE;
342 if (!may_write_to_queue(mapping->backing_dev_info))
343 return PAGE_KEEP;
344
345 if (clear_page_dirty_for_io(page)) {
346 int res;
347 struct writeback_control wbc = {
348 .sync_mode = WB_SYNC_NONE,
349 .nr_to_write = SWAP_CLUSTER_MAX,
350 .nonblocking = 1,
351 .for_reclaim = 1,
352 };
353
354 SetPageReclaim(page);
355 res = mapping->a_ops->writepage(page, &wbc);
356 if (res < 0)
357 handle_write_error(mapping, page, res);
358 if (res == WRITEPAGE_ACTIVATE) {
359 ClearPageReclaim(page);
360 return PAGE_ACTIVATE;
361 }
362 if (!PageWriteback(page)) {
363 /* synchronous write or broken a_ops? */
364 ClearPageReclaim(page);
365 }
366
367 return PAGE_SUCCESS;
368 }
369
370 return PAGE_CLEAN;
371}
372
373/*
374 * shrink_list adds the number of reclaimed pages to sc->nr_reclaimed
375 */
376static int shrink_list(struct list_head *page_list, struct scan_control *sc)
377{
378 LIST_HEAD(ret_pages);
379 struct pagevec freed_pvec;
380 int pgactivate = 0;
381 int reclaimed = 0;
382
383 cond_resched();
384
385 pagevec_init(&freed_pvec, 1);
386 while (!list_empty(page_list)) {
387 struct address_space *mapping;
388 struct page *page;
389 int may_enter_fs;
390 int referenced;
391
392 cond_resched();
393
394 page = lru_to_page(page_list);
395 list_del(&page->lru);
396
397 if (TestSetPageLocked(page))
398 goto keep;
399
400 BUG_ON(PageActive(page));
401
402 sc->nr_scanned++;
403 /* Double the slab pressure for mapped and swapcache pages */
404 if (page_mapped(page) || PageSwapCache(page))
405 sc->nr_scanned++;
406
407 if (PageWriteback(page))
408 goto keep_locked;
409
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800410 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* In active use or really unfreeable? Activate it. */
412 if (referenced && page_mapping_inuse(page))
413 goto activate_locked;
414
415#ifdef CONFIG_SWAP
416 /*
417 * Anonymous process memory has backing store?
418 * Try to allocate it some swap space here.
419 */
Lee Schermerhornc3400102005-10-29 18:15:51 -0700420 if (PageAnon(page) && !PageSwapCache(page)) {
421 if (!sc->may_swap)
422 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (!add_to_swap(page))
424 goto activate_locked;
425 }
426#endif /* CONFIG_SWAP */
427
428 mapping = page_mapping(page);
429 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
430 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
431
432 /*
433 * The page is mapped into the page tables of one or more
434 * processes. Try to unmap it here.
435 */
436 if (page_mapped(page) && mapping) {
437 switch (try_to_unmap(page)) {
438 case SWAP_FAIL:
439 goto activate_locked;
440 case SWAP_AGAIN:
441 goto keep_locked;
442 case SWAP_SUCCESS:
443 ; /* try to free the page below */
444 }
445 }
446
447 if (PageDirty(page)) {
448 if (referenced)
449 goto keep_locked;
450 if (!may_enter_fs)
451 goto keep_locked;
452 if (laptop_mode && !sc->may_writepage)
453 goto keep_locked;
454
455 /* Page is dirty, try to write it out here */
456 switch(pageout(page, mapping)) {
457 case PAGE_KEEP:
458 goto keep_locked;
459 case PAGE_ACTIVATE:
460 goto activate_locked;
461 case PAGE_SUCCESS:
462 if (PageWriteback(page) || PageDirty(page))
463 goto keep;
464 /*
465 * A synchronous write - probably a ramdisk. Go
466 * ahead and try to reclaim the page.
467 */
468 if (TestSetPageLocked(page))
469 goto keep;
470 if (PageDirty(page) || PageWriteback(page))
471 goto keep_locked;
472 mapping = page_mapping(page);
473 case PAGE_CLEAN:
474 ; /* try to free the page below */
475 }
476 }
477
478 /*
479 * If the page has buffers, try to free the buffer mappings
480 * associated with this page. If we succeed we try to free
481 * the page as well.
482 *
483 * We do this even if the page is PageDirty().
484 * try_to_release_page() does not perform I/O, but it is
485 * possible for a page to have PageDirty set, but it is actually
486 * clean (all its buffers are clean). This happens if the
487 * buffers were written out directly, with submit_bh(). ext3
488 * will do this, as well as the blockdev mapping.
489 * try_to_release_page() will discover that cleanness and will
490 * drop the buffers and mark the page clean - it can be freed.
491 *
492 * Rarely, pages can have buffers and no ->mapping. These are
493 * the pages which were not successfully invalidated in
494 * truncate_complete_page(). We try to drop those buffers here
495 * and if that worked, and the page is no longer mapped into
496 * process address space (page_count == 1) it can be freed.
497 * Otherwise, leave the page on the LRU so it is swappable.
498 */
499 if (PagePrivate(page)) {
500 if (!try_to_release_page(page, sc->gfp_mask))
501 goto activate_locked;
502 if (!mapping && page_count(page) == 1)
503 goto free_it;
504 }
505
506 if (!mapping)
507 goto keep_locked; /* truncate got there first */
508
509 write_lock_irq(&mapping->tree_lock);
510
511 /*
512 * The non-racy check for busy page. It is critical to check
513 * PageDirty _after_ making sure that the page is freeable and
514 * not in use by anybody. (pagecache + us == 2)
515 */
Linus Torvalds3d806362005-10-16 17:36:06 -0700516 if (unlikely(page_count(page) != 2))
517 goto cannot_free;
518 smp_rmb();
519 if (unlikely(PageDirty(page)))
520 goto cannot_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522#ifdef CONFIG_SWAP
523 if (PageSwapCache(page)) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700524 swp_entry_t swap = { .val = page_private(page) };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 __delete_from_swap_cache(page);
526 write_unlock_irq(&mapping->tree_lock);
527 swap_free(swap);
528 __put_page(page); /* The pagecache ref */
529 goto free_it;
530 }
531#endif /* CONFIG_SWAP */
532
533 __remove_from_page_cache(page);
534 write_unlock_irq(&mapping->tree_lock);
535 __put_page(page);
536
537free_it:
538 unlock_page(page);
539 reclaimed++;
540 if (!pagevec_add(&freed_pvec, page))
541 __pagevec_release_nonlru(&freed_pvec);
542 continue;
543
Linus Torvalds3d806362005-10-16 17:36:06 -0700544cannot_free:
545 write_unlock_irq(&mapping->tree_lock);
546 goto keep_locked;
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548activate_locked:
549 SetPageActive(page);
550 pgactivate++;
551keep_locked:
552 unlock_page(page);
553keep:
554 list_add(&page->lru, &ret_pages);
555 BUG_ON(PageLRU(page));
556 }
557 list_splice(&ret_pages, page_list);
558 if (pagevec_count(&freed_pvec))
559 __pagevec_release_nonlru(&freed_pvec);
560 mod_page_state(pgactivate, pgactivate);
561 sc->nr_reclaimed += reclaimed;
562 return reclaimed;
563}
564
565/*
566 * zone->lru_lock is heavily contended. Some of the functions that
567 * shrink the lists perform better by taking out a batch of pages
568 * and working on them outside the LRU lock.
569 *
570 * For pagecache intensive workloads, this function is the hottest
571 * spot in the kernel (apart from copy_*_user functions).
572 *
573 * Appropriate locks must be held before calling this function.
574 *
575 * @nr_to_scan: The number of pages to look through on the list.
576 * @src: The LRU list to pull pages off.
577 * @dst: The temp list to put pages on to.
578 * @scanned: The number of pages that were scanned.
579 *
580 * returns how many pages were moved onto *@dst.
581 */
582static int isolate_lru_pages(int nr_to_scan, struct list_head *src,
583 struct list_head *dst, int *scanned)
584{
585 int nr_taken = 0;
586 struct page *page;
587 int scan = 0;
588
589 while (scan++ < nr_to_scan && !list_empty(src)) {
590 page = lru_to_page(src);
591 prefetchw_prev_lru_page(page, src, flags);
592
593 if (!TestClearPageLRU(page))
594 BUG();
595 list_del(&page->lru);
596 if (get_page_testone(page)) {
597 /*
598 * It is being freed elsewhere
599 */
600 __put_page(page);
601 SetPageLRU(page);
602 list_add(&page->lru, src);
603 continue;
604 } else {
605 list_add(&page->lru, dst);
606 nr_taken++;
607 }
608 }
609
610 *scanned = scan;
611 return nr_taken;
612}
613
614/*
615 * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed
616 */
617static void shrink_cache(struct zone *zone, struct scan_control *sc)
618{
619 LIST_HEAD(page_list);
620 struct pagevec pvec;
621 int max_scan = sc->nr_to_scan;
622
623 pagevec_init(&pvec, 1);
624
625 lru_add_drain();
626 spin_lock_irq(&zone->lru_lock);
627 while (max_scan > 0) {
628 struct page *page;
629 int nr_taken;
630 int nr_scan;
631 int nr_freed;
632
633 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
634 &zone->inactive_list,
635 &page_list, &nr_scan);
636 zone->nr_inactive -= nr_taken;
637 zone->pages_scanned += nr_scan;
638 spin_unlock_irq(&zone->lru_lock);
639
640 if (nr_taken == 0)
641 goto done;
642
643 max_scan -= nr_scan;
644 if (current_is_kswapd())
645 mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
646 else
647 mod_page_state_zone(zone, pgscan_direct, nr_scan);
648 nr_freed = shrink_list(&page_list, sc);
649 if (current_is_kswapd())
650 mod_page_state(kswapd_steal, nr_freed);
651 mod_page_state_zone(zone, pgsteal, nr_freed);
652 sc->nr_to_reclaim -= nr_freed;
653
654 spin_lock_irq(&zone->lru_lock);
655 /*
656 * Put back any unfreeable pages.
657 */
658 while (!list_empty(&page_list)) {
659 page = lru_to_page(&page_list);
660 if (TestSetPageLRU(page))
661 BUG();
662 list_del(&page->lru);
663 if (PageActive(page))
664 add_page_to_active_list(zone, page);
665 else
666 add_page_to_inactive_list(zone, page);
667 if (!pagevec_add(&pvec, page)) {
668 spin_unlock_irq(&zone->lru_lock);
669 __pagevec_release(&pvec);
670 spin_lock_irq(&zone->lru_lock);
671 }
672 }
673 }
674 spin_unlock_irq(&zone->lru_lock);
675done:
676 pagevec_release(&pvec);
677}
678
679/*
680 * This moves pages from the active list to the inactive list.
681 *
682 * We move them the other way if the page is referenced by one or more
683 * processes, from rmap.
684 *
685 * If the pages are mostly unmapped, the processing is fast and it is
686 * appropriate to hold zone->lru_lock across the whole operation. But if
687 * the pages are mapped, the processing is slow (page_referenced()) so we
688 * should drop zone->lru_lock around each page. It's impossible to balance
689 * this, so instead we remove the pages from the LRU while processing them.
690 * It is safe to rely on PG_active against the non-LRU pages in here because
691 * nobody will play with that bit on a non-LRU page.
692 *
693 * The downside is that we have to touch page->_count against each page.
694 * But we had to alter page->flags anyway.
695 */
696static void
697refill_inactive_zone(struct zone *zone, struct scan_control *sc)
698{
699 int pgmoved;
700 int pgdeactivate = 0;
701 int pgscanned;
702 int nr_pages = sc->nr_to_scan;
703 LIST_HEAD(l_hold); /* The pages which were snipped off */
704 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
705 LIST_HEAD(l_active); /* Pages to go onto the active_list */
706 struct page *page;
707 struct pagevec pvec;
708 int reclaim_mapped = 0;
709 long mapped_ratio;
710 long distress;
711 long swap_tendency;
712
713 lru_add_drain();
714 spin_lock_irq(&zone->lru_lock);
715 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
716 &l_hold, &pgscanned);
717 zone->pages_scanned += pgscanned;
718 zone->nr_active -= pgmoved;
719 spin_unlock_irq(&zone->lru_lock);
720
721 /*
722 * `distress' is a measure of how much trouble we're having reclaiming
723 * pages. 0 -> no problems. 100 -> great trouble.
724 */
725 distress = 100 >> zone->prev_priority;
726
727 /*
728 * The point of this algorithm is to decide when to start reclaiming
729 * mapped memory instead of just pagecache. Work out how much memory
730 * is mapped.
731 */
732 mapped_ratio = (sc->nr_mapped * 100) / total_memory;
733
734 /*
735 * Now decide how much we really want to unmap some pages. The mapped
736 * ratio is downgraded - just because there's a lot of mapped memory
737 * doesn't necessarily mean that page reclaim isn't succeeding.
738 *
739 * The distress ratio is important - we don't want to start going oom.
740 *
741 * A 100% value of vm_swappiness overrides this algorithm altogether.
742 */
743 swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
744
745 /*
746 * Now use this metric to decide whether to start moving mapped memory
747 * onto the inactive list.
748 */
749 if (swap_tendency >= 100)
750 reclaim_mapped = 1;
751
752 while (!list_empty(&l_hold)) {
753 cond_resched();
754 page = lru_to_page(&l_hold);
755 list_del(&page->lru);
756 if (page_mapped(page)) {
757 if (!reclaim_mapped ||
758 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800759 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 list_add(&page->lru, &l_active);
761 continue;
762 }
763 }
764 list_add(&page->lru, &l_inactive);
765 }
766
767 pagevec_init(&pvec, 1);
768 pgmoved = 0;
769 spin_lock_irq(&zone->lru_lock);
770 while (!list_empty(&l_inactive)) {
771 page = lru_to_page(&l_inactive);
772 prefetchw_prev_lru_page(page, &l_inactive, flags);
773 if (TestSetPageLRU(page))
774 BUG();
775 if (!TestClearPageActive(page))
776 BUG();
777 list_move(&page->lru, &zone->inactive_list);
778 pgmoved++;
779 if (!pagevec_add(&pvec, page)) {
780 zone->nr_inactive += pgmoved;
781 spin_unlock_irq(&zone->lru_lock);
782 pgdeactivate += pgmoved;
783 pgmoved = 0;
784 if (buffer_heads_over_limit)
785 pagevec_strip(&pvec);
786 __pagevec_release(&pvec);
787 spin_lock_irq(&zone->lru_lock);
788 }
789 }
790 zone->nr_inactive += pgmoved;
791 pgdeactivate += pgmoved;
792 if (buffer_heads_over_limit) {
793 spin_unlock_irq(&zone->lru_lock);
794 pagevec_strip(&pvec);
795 spin_lock_irq(&zone->lru_lock);
796 }
797
798 pgmoved = 0;
799 while (!list_empty(&l_active)) {
800 page = lru_to_page(&l_active);
801 prefetchw_prev_lru_page(page, &l_active, flags);
802 if (TestSetPageLRU(page))
803 BUG();
804 BUG_ON(!PageActive(page));
805 list_move(&page->lru, &zone->active_list);
806 pgmoved++;
807 if (!pagevec_add(&pvec, page)) {
808 zone->nr_active += pgmoved;
809 pgmoved = 0;
810 spin_unlock_irq(&zone->lru_lock);
811 __pagevec_release(&pvec);
812 spin_lock_irq(&zone->lru_lock);
813 }
814 }
815 zone->nr_active += pgmoved;
816 spin_unlock_irq(&zone->lru_lock);
817 pagevec_release(&pvec);
818
819 mod_page_state_zone(zone, pgrefill, pgscanned);
820 mod_page_state(pgdeactivate, pgdeactivate);
821}
822
823/*
824 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
825 */
826static void
827shrink_zone(struct zone *zone, struct scan_control *sc)
828{
829 unsigned long nr_active;
830 unsigned long nr_inactive;
831
Martin Hicks53e9a612005-09-03 15:54:51 -0700832 atomic_inc(&zone->reclaim_in_progress);
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /*
835 * Add one to `nr_to_scan' just to make sure that the kernel will
836 * slowly sift through the active list.
837 */
838 zone->nr_scan_active += (zone->nr_active >> sc->priority) + 1;
839 nr_active = zone->nr_scan_active;
840 if (nr_active >= sc->swap_cluster_max)
841 zone->nr_scan_active = 0;
842 else
843 nr_active = 0;
844
845 zone->nr_scan_inactive += (zone->nr_inactive >> sc->priority) + 1;
846 nr_inactive = zone->nr_scan_inactive;
847 if (nr_inactive >= sc->swap_cluster_max)
848 zone->nr_scan_inactive = 0;
849 else
850 nr_inactive = 0;
851
852 sc->nr_to_reclaim = sc->swap_cluster_max;
853
854 while (nr_active || nr_inactive) {
855 if (nr_active) {
856 sc->nr_to_scan = min(nr_active,
857 (unsigned long)sc->swap_cluster_max);
858 nr_active -= sc->nr_to_scan;
859 refill_inactive_zone(zone, sc);
860 }
861
862 if (nr_inactive) {
863 sc->nr_to_scan = min(nr_inactive,
864 (unsigned long)sc->swap_cluster_max);
865 nr_inactive -= sc->nr_to_scan;
866 shrink_cache(zone, sc);
867 if (sc->nr_to_reclaim <= 0)
868 break;
869 }
870 }
871
872 throttle_vm_writeout();
Martin Hicks53e9a612005-09-03 15:54:51 -0700873
874 atomic_dec(&zone->reclaim_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
877/*
878 * This is the direct reclaim path, for page-allocating processes. We only
879 * try to reclaim pages from zones which will satisfy the caller's allocation
880 * request.
881 *
882 * We reclaim from a zone even if that zone is over pages_high. Because:
883 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
884 * allocation or
885 * b) The zones may be over pages_high but they must go *over* pages_high to
886 * satisfy the `incremental min' zone defense algorithm.
887 *
888 * Returns the number of reclaimed pages.
889 *
890 * If a zone is deemed to be full of pinned pages then just give it a light
891 * scan then give up on it.
892 */
893static void
894shrink_caches(struct zone **zones, struct scan_control *sc)
895{
896 int i;
897
898 for (i = 0; zones[i] != NULL; i++) {
899 struct zone *zone = zones[i];
900
901 if (zone->present_pages == 0)
902 continue;
903
Paul Jackson9bf22292005-09-06 15:18:12 -0700904 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 continue;
906
907 zone->temp_priority = sc->priority;
908 if (zone->prev_priority > sc->priority)
909 zone->prev_priority = sc->priority;
910
911 if (zone->all_unreclaimable && sc->priority != DEF_PRIORITY)
912 continue; /* Let kswapd poll it */
913
914 shrink_zone(zone, sc);
915 }
916}
917
918/*
919 * This is the main entry point to direct page reclaim.
920 *
921 * If a full scan of the inactive list fails to free enough memory then we
922 * are "out of memory" and something needs to be killed.
923 *
924 * If the caller is !__GFP_FS then the probability of a failure is reasonably
925 * high - the zone may be full of dirty or under-writeback pages, which this
926 * caller can't do much about. We kick pdflush and take explicit naps in the
927 * hope that some of these pages can be written. But if the allocating task
928 * holds filesystem locks which prevent writeout this might not work, and the
929 * allocation attempt will fail.
930 */
Al Viro6daa0e22005-10-21 03:18:50 -0400931int try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 int priority;
934 int ret = 0;
935 int total_scanned = 0, total_reclaimed = 0;
936 struct reclaim_state *reclaim_state = current->reclaim_state;
937 struct scan_control sc;
938 unsigned long lru_pages = 0;
939 int i;
940
941 sc.gfp_mask = gfp_mask;
942 sc.may_writepage = 0;
Martin Hicksbfbb38f2005-06-21 17:14:40 -0700943 sc.may_swap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 inc_page_state(allocstall);
946
947 for (i = 0; zones[i] != NULL; i++) {
948 struct zone *zone = zones[i];
949
Paul Jackson9bf22292005-09-06 15:18:12 -0700950 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 continue;
952
953 zone->temp_priority = DEF_PRIORITY;
954 lru_pages += zone->nr_active + zone->nr_inactive;
955 }
956
957 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
958 sc.nr_mapped = read_page_state(nr_mapped);
959 sc.nr_scanned = 0;
960 sc.nr_reclaimed = 0;
961 sc.priority = priority;
962 sc.swap_cluster_max = SWAP_CLUSTER_MAX;
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800963 if (!priority)
964 disable_swap_token();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 shrink_caches(zones, &sc);
966 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
967 if (reclaim_state) {
968 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
969 reclaim_state->reclaimed_slab = 0;
970 }
971 total_scanned += sc.nr_scanned;
972 total_reclaimed += sc.nr_reclaimed;
973 if (total_reclaimed >= sc.swap_cluster_max) {
974 ret = 1;
975 goto out;
976 }
977
978 /*
979 * Try to write back as many pages as we just scanned. This
980 * tends to cause slow streaming writers to write data to the
981 * disk smoothly, at the dirtying rate, which is nice. But
982 * that's undesirable in laptop mode, where we *want* lumpy
983 * writeout. So in laptop mode, write out the whole world.
984 */
985 if (total_scanned > sc.swap_cluster_max + sc.swap_cluster_max/2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -0700986 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 sc.may_writepage = 1;
988 }
989
990 /* Take a nap, wait for some writeback to complete */
991 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
992 blk_congestion_wait(WRITE, HZ/10);
993 }
994out:
995 for (i = 0; zones[i] != 0; i++) {
996 struct zone *zone = zones[i];
997
Paul Jackson9bf22292005-09-06 15:18:12 -0700998 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 continue;
1000
1001 zone->prev_priority = zone->temp_priority;
1002 }
1003 return ret;
1004}
1005
1006/*
1007 * For kswapd, balance_pgdat() will work across all this node's zones until
1008 * they are all at pages_high.
1009 *
1010 * If `nr_pages' is non-zero then it is the number of pages which are to be
1011 * reclaimed, regardless of the zone occupancies. This is a software suspend
1012 * special.
1013 *
1014 * Returns the number of pages which were actually freed.
1015 *
1016 * There is special handling here for zones which are full of pinned pages.
1017 * This can happen if the pages are all mlocked, or if they are all used by
1018 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1019 * What we do is to detect the case where all pages in the zone have been
1020 * scanned twice and there has been zero successful reclaim. Mark the zone as
1021 * dead and from now on, only perform a short scan. Basically we're polling
1022 * the zone for when the problem goes away.
1023 *
1024 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1025 * zones which have free_pages > pages_high, but once a zone is found to have
1026 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1027 * of the number of free pages in the lower zones. This interoperates with
1028 * the page allocator fallback scheme to ensure that aging of pages is balanced
1029 * across the zones.
1030 */
1031static int balance_pgdat(pg_data_t *pgdat, int nr_pages, int order)
1032{
1033 int to_free = nr_pages;
1034 int all_zones_ok;
1035 int priority;
1036 int i;
1037 int total_scanned, total_reclaimed;
1038 struct reclaim_state *reclaim_state = current->reclaim_state;
1039 struct scan_control sc;
1040
1041loop_again:
1042 total_scanned = 0;
1043 total_reclaimed = 0;
1044 sc.gfp_mask = GFP_KERNEL;
1045 sc.may_writepage = 0;
Martin Hicksbfbb38f2005-06-21 17:14:40 -07001046 sc.may_swap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 sc.nr_mapped = read_page_state(nr_mapped);
1048
1049 inc_page_state(pageoutrun);
1050
1051 for (i = 0; i < pgdat->nr_zones; i++) {
1052 struct zone *zone = pgdat->node_zones + i;
1053
1054 zone->temp_priority = DEF_PRIORITY;
1055 }
1056
1057 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1058 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1059 unsigned long lru_pages = 0;
1060
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001061 /* The swap token gets in the way of swapout... */
1062 if (!priority)
1063 disable_swap_token();
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 all_zones_ok = 1;
1066
1067 if (nr_pages == 0) {
1068 /*
1069 * Scan in the highmem->dma direction for the highest
1070 * zone which needs scanning
1071 */
1072 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1073 struct zone *zone = pgdat->node_zones + i;
1074
1075 if (zone->present_pages == 0)
1076 continue;
1077
1078 if (zone->all_unreclaimable &&
1079 priority != DEF_PRIORITY)
1080 continue;
1081
1082 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001083 zone->pages_high, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 end_zone = i;
1085 goto scan;
1086 }
1087 }
1088 goto out;
1089 } else {
1090 end_zone = pgdat->nr_zones - 1;
1091 }
1092scan:
1093 for (i = 0; i <= end_zone; i++) {
1094 struct zone *zone = pgdat->node_zones + i;
1095
1096 lru_pages += zone->nr_active + zone->nr_inactive;
1097 }
1098
1099 /*
1100 * Now scan the zone in the dma->highmem direction, stopping
1101 * at the last zone which needs scanning.
1102 *
1103 * We do this because the page allocator works in the opposite
1104 * direction. This prevents the page allocator from allocating
1105 * pages behind kswapd's direction of progress, which would
1106 * cause too much scanning of the lower zones.
1107 */
1108 for (i = 0; i <= end_zone; i++) {
1109 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001110 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (zone->present_pages == 0)
1113 continue;
1114
1115 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1116 continue;
1117
1118 if (nr_pages == 0) { /* Not software suspend */
1119 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001120 zone->pages_high, end_zone, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 all_zones_ok = 0;
1122 }
1123 zone->temp_priority = priority;
1124 if (zone->prev_priority > priority)
1125 zone->prev_priority = priority;
1126 sc.nr_scanned = 0;
1127 sc.nr_reclaimed = 0;
1128 sc.priority = priority;
1129 sc.swap_cluster_max = nr_pages? nr_pages : SWAP_CLUSTER_MAX;
Martin Hicks1e7e5a92005-06-21 17:14:43 -07001130 atomic_inc(&zone->reclaim_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 shrink_zone(zone, &sc);
Martin Hicks1e7e5a92005-06-21 17:14:43 -07001132 atomic_dec(&zone->reclaim_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001134 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1135 lru_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
1137 total_reclaimed += sc.nr_reclaimed;
1138 total_scanned += sc.nr_scanned;
1139 if (zone->all_unreclaimable)
1140 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001141 if (nr_slab == 0 && zone->pages_scanned >=
1142 (zone->nr_active + zone->nr_inactive) * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 zone->all_unreclaimable = 1;
1144 /*
1145 * If we've done a decent amount of scanning and
1146 * the reclaim ratio is low, start doing writepage
1147 * even in laptop mode
1148 */
1149 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
1150 total_scanned > total_reclaimed+total_reclaimed/2)
1151 sc.may_writepage = 1;
1152 }
1153 if (nr_pages && to_free > total_reclaimed)
1154 continue; /* swsusp: need to do more work */
1155 if (all_zones_ok)
1156 break; /* kswapd: all done */
1157 /*
1158 * OK, kswapd is getting into trouble. Take a nap, then take
1159 * another pass across the zones.
1160 */
1161 if (total_scanned && priority < DEF_PRIORITY - 2)
1162 blk_congestion_wait(WRITE, HZ/10);
1163
1164 /*
1165 * We do this so kswapd doesn't build up large priorities for
1166 * example when it is freeing in parallel with allocators. It
1167 * matches the direct reclaim path behaviour in terms of impact
1168 * on zone->*_priority.
1169 */
1170 if ((total_reclaimed >= SWAP_CLUSTER_MAX) && (!nr_pages))
1171 break;
1172 }
1173out:
1174 for (i = 0; i < pgdat->nr_zones; i++) {
1175 struct zone *zone = pgdat->node_zones + i;
1176
1177 zone->prev_priority = zone->temp_priority;
1178 }
1179 if (!all_zones_ok) {
1180 cond_resched();
1181 goto loop_again;
1182 }
1183
1184 return total_reclaimed;
1185}
1186
1187/*
1188 * The background pageout daemon, started as a kernel thread
1189 * from the init process.
1190 *
1191 * This basically trickles out pages so that we have _some_
1192 * free memory available even if there is no other activity
1193 * that frees anything up. This is needed for things like routing
1194 * etc, where we otherwise might have all activity going on in
1195 * asynchronous contexts that cannot page things out.
1196 *
1197 * If there are applications that are active memory-allocators
1198 * (most normal use), this basically shouldn't matter.
1199 */
1200static int kswapd(void *p)
1201{
1202 unsigned long order;
1203 pg_data_t *pgdat = (pg_data_t*)p;
1204 struct task_struct *tsk = current;
1205 DEFINE_WAIT(wait);
1206 struct reclaim_state reclaim_state = {
1207 .reclaimed_slab = 0,
1208 };
1209 cpumask_t cpumask;
1210
1211 daemonize("kswapd%d", pgdat->node_id);
1212 cpumask = node_to_cpumask(pgdat->node_id);
1213 if (!cpus_empty(cpumask))
1214 set_cpus_allowed(tsk, cpumask);
1215 current->reclaim_state = &reclaim_state;
1216
1217 /*
1218 * Tell the memory management that we're a "memory allocator",
1219 * and that if we need more memory we should get access to it
1220 * regardless (see "__alloc_pages()"). "kswapd" should
1221 * never get caught in the normal page freeing logic.
1222 *
1223 * (Kswapd normally doesn't need memory anyway, but sometimes
1224 * you need a small amount of memory in order to be able to
1225 * page out something else, and this flag essentially protects
1226 * us from recursively trying to free more memory as we're
1227 * trying to free the first piece of memory in the first place).
1228 */
1229 tsk->flags |= PF_MEMALLOC|PF_KSWAPD;
1230
1231 order = 0;
1232 for ( ; ; ) {
1233 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001234
1235 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1238 new_order = pgdat->kswapd_max_order;
1239 pgdat->kswapd_max_order = 0;
1240 if (order < new_order) {
1241 /*
1242 * Don't sleep if someone wants a larger 'order'
1243 * allocation
1244 */
1245 order = new_order;
1246 } else {
1247 schedule();
1248 order = pgdat->kswapd_max_order;
1249 }
1250 finish_wait(&pgdat->kswapd_wait, &wait);
1251
1252 balance_pgdat(pgdat, 0, order);
1253 }
1254 return 0;
1255}
1256
1257/*
1258 * A zone is low on free memory, so wake its kswapd task to service it.
1259 */
1260void wakeup_kswapd(struct zone *zone, int order)
1261{
1262 pg_data_t *pgdat;
1263
1264 if (zone->present_pages == 0)
1265 return;
1266
1267 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001268 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return;
1270 if (pgdat->kswapd_max_order < order)
1271 pgdat->kswapd_max_order = order;
Paul Jackson9bf22292005-09-06 15:18:12 -07001272 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001274 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001276 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
1279#ifdef CONFIG_PM
1280/*
1281 * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
1282 * pages.
1283 */
1284int shrink_all_memory(int nr_pages)
1285{
1286 pg_data_t *pgdat;
1287 int nr_to_free = nr_pages;
1288 int ret = 0;
1289 struct reclaim_state reclaim_state = {
1290 .reclaimed_slab = 0,
1291 };
1292
1293 current->reclaim_state = &reclaim_state;
1294 for_each_pgdat(pgdat) {
1295 int freed;
1296 freed = balance_pgdat(pgdat, nr_to_free, 0);
1297 ret += freed;
1298 nr_to_free -= freed;
1299 if (nr_to_free <= 0)
1300 break;
1301 }
1302 current->reclaim_state = NULL;
1303 return ret;
1304}
1305#endif
1306
1307#ifdef CONFIG_HOTPLUG_CPU
1308/* It's optimal to keep kswapds on the same CPUs as their memory, but
1309 not required for correctness. So if the last cpu in a node goes
1310 away, we get changed to run anywhere: as the first one comes back,
1311 restore their cpu bindings. */
1312static int __devinit cpu_callback(struct notifier_block *nfb,
1313 unsigned long action,
1314 void *hcpu)
1315{
1316 pg_data_t *pgdat;
1317 cpumask_t mask;
1318
1319 if (action == CPU_ONLINE) {
1320 for_each_pgdat(pgdat) {
1321 mask = node_to_cpumask(pgdat->node_id);
1322 if (any_online_cpu(mask) != NR_CPUS)
1323 /* One of our CPUs online: restore mask */
1324 set_cpus_allowed(pgdat->kswapd, mask);
1325 }
1326 }
1327 return NOTIFY_OK;
1328}
1329#endif /* CONFIG_HOTPLUG_CPU */
1330
1331static int __init kswapd_init(void)
1332{
1333 pg_data_t *pgdat;
1334 swap_setup();
1335 for_each_pgdat(pgdat)
1336 pgdat->kswapd
1337 = find_task_by_pid(kernel_thread(kswapd, pgdat, CLONE_KERNEL));
1338 total_memory = nr_free_pagecache_pages();
1339 hotcpu_notifier(cpu_callback, 0);
1340 return 0;
1341}
1342
1343module_init(kswapd_init)
Martin Hicks753ee722005-06-21 17:14:41 -07001344
1345
1346/*
1347 * Try to free up some pages from this zone through reclaim.
1348 */
Al Viro6daa0e22005-10-21 03:18:50 -04001349int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Martin Hicks753ee722005-06-21 17:14:41 -07001350{
1351 struct scan_control sc;
1352 int nr_pages = 1 << order;
1353 int total_reclaimed = 0;
1354
1355 /* The reclaim may sleep, so don't do it if sleep isn't allowed */
1356 if (!(gfp_mask & __GFP_WAIT))
1357 return 0;
1358 if (zone->all_unreclaimable)
1359 return 0;
1360
1361 sc.gfp_mask = gfp_mask;
1362 sc.may_writepage = 0;
1363 sc.may_swap = 0;
1364 sc.nr_mapped = read_page_state(nr_mapped);
1365 sc.nr_scanned = 0;
1366 sc.nr_reclaimed = 0;
1367 /* scan at the highest priority */
1368 sc.priority = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001369 disable_swap_token();
Martin Hicks753ee722005-06-21 17:14:41 -07001370
1371 if (nr_pages > SWAP_CLUSTER_MAX)
1372 sc.swap_cluster_max = nr_pages;
1373 else
1374 sc.swap_cluster_max = SWAP_CLUSTER_MAX;
1375
Martin Hicks1e7e5a92005-06-21 17:14:43 -07001376 /* Don't reclaim the zone if there are other reclaimers active */
Martin Hicks53e9a612005-09-03 15:54:51 -07001377 if (atomic_read(&zone->reclaim_in_progress) > 0)
Martin Hicks1e7e5a92005-06-21 17:14:43 -07001378 goto out;
1379
Martin Hicks753ee722005-06-21 17:14:41 -07001380 shrink_zone(zone, &sc);
1381 total_reclaimed = sc.nr_reclaimed;
1382
Martin Hicks1e7e5a92005-06-21 17:14:43 -07001383 out:
Martin Hicks753ee722005-06-21 17:14:41 -07001384 return total_reclaimed;
1385}
1386
1387asmlinkage long sys_set_zone_reclaim(unsigned int node, unsigned int zone,
1388 unsigned int state)
1389{
1390 struct zone *z;
1391 int i;
1392
Martin Hicksbce5f6ba2005-09-03 15:54:50 -07001393 if (!capable(CAP_SYS_ADMIN))
1394 return -EACCES;
1395
Martin Hicks753ee722005-06-21 17:14:41 -07001396 if (node >= MAX_NUMNODES || !node_online(node))
1397 return -EINVAL;
1398
1399 /* This will break if we ever add more zones */
1400 if (!(zone & (1<<ZONE_DMA|1<<ZONE_NORMAL|1<<ZONE_HIGHMEM)))
1401 return -EINVAL;
1402
1403 for (i = 0; i < MAX_NR_ZONES; i++) {
1404 if (!(zone & 1<<i))
1405 continue;
1406
1407 z = &NODE_DATA(node)->node_zones[i];
1408
1409 if (state)
1410 z->reclaim_pages = 1;
1411 else
1412 z->reclaim_pages = 0;
1413 }
1414
1415 return 0;
1416}