blob: 1be5a6376ef0719b5e46937924a8a66cf59a35f1 [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>
Andrew Mortone129b5c2006-09-27 01:50:00 -070022#include <linux/vmstat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/file.h>
24#include <linux/writeback.h>
25#include <linux/blkdev.h>
26#include <linux/buffer_head.h> /* for try_to_release_page(),
27 buffer_heads_over_limit */
28#include <linux/mm_inline.h>
29#include <linux/pagevec.h>
30#include <linux/backing-dev.h>
31#include <linux/rmap.h>
32#include <linux/topology.h>
33#include <linux/cpu.h>
34#include <linux/cpuset.h>
35#include <linux/notifier.h>
36#include <linux/rwsem.h>
Rafael J. Wysocki248a0302006-03-22 00:09:04 -080037#include <linux/delay.h>
Yasunori Goto3218ae12006-06-27 02:53:33 -070038#include <linux/kthread.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080039#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/tlbflush.h>
42#include <asm/div64.h>
43
44#include <linux/swapops.h>
45
Nick Piggin0f8053a2006-03-22 00:08:33 -080046#include "internal.h"
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048struct scan_control {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 /* Incremented by the number of inactive pages that were scanned */
50 unsigned long nr_scanned;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040053 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 int may_writepage;
56
Christoph Lameterf1fd1062006-01-18 17:42:30 -080057 /* Can pages be swapped as part of reclaim? */
58 int may_swap;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
61 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
62 * In this context, it doesn't matter that we scan the
63 * whole list at once. */
64 int swap_cluster_max;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -070065
66 int swappiness;
Nick Piggin408d8542006-09-25 23:31:27 -070067
68 int all_unreclaimable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71/*
72 * The list of shrinker callbacks used by to apply pressure to
73 * ageable caches.
74 */
75struct shrinker {
76 shrinker_t shrinker;
77 struct list_head list;
78 int seeks; /* seeks to recreate an obj */
79 long nr; /* objs pending delete */
80};
81
82#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
83
84#ifdef ARCH_HAS_PREFETCH
85#define prefetch_prev_lru_page(_page, _base, _field) \
86 do { \
87 if ((_page)->lru.prev != _base) { \
88 struct page *prev; \
89 \
90 prev = lru_to_page(&(_page->lru)); \
91 prefetch(&prev->_field); \
92 } \
93 } while (0)
94#else
95#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
96#endif
97
98#ifdef ARCH_HAS_PREFETCHW
99#define prefetchw_prev_lru_page(_page, _base, _field) \
100 do { \
101 if ((_page)->lru.prev != _base) { \
102 struct page *prev; \
103 \
104 prev = lru_to_page(&(_page->lru)); \
105 prefetchw(&prev->_field); \
106 } \
107 } while (0)
108#else
109#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
110#endif
111
112/*
113 * From 0 .. 100. Higher means more swappy.
114 */
115int vm_swappiness = 60;
Andrew Mortonbd1e22b2006-06-23 02:03:47 -0700116long vm_total_pages; /* The total number of pages which the VM controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118static LIST_HEAD(shrinker_list);
119static DECLARE_RWSEM(shrinker_rwsem);
120
121/*
122 * Add a shrinker callback to be called from the vm
123 */
124struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
125{
126 struct shrinker *shrinker;
127
128 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
129 if (shrinker) {
130 shrinker->shrinker = theshrinker;
131 shrinker->seeks = seeks;
132 shrinker->nr = 0;
133 down_write(&shrinker_rwsem);
134 list_add_tail(&shrinker->list, &shrinker_list);
135 up_write(&shrinker_rwsem);
136 }
137 return shrinker;
138}
139EXPORT_SYMBOL(set_shrinker);
140
141/*
142 * Remove one
143 */
144void remove_shrinker(struct shrinker *shrinker)
145{
146 down_write(&shrinker_rwsem);
147 list_del(&shrinker->list);
148 up_write(&shrinker_rwsem);
149 kfree(shrinker);
150}
151EXPORT_SYMBOL(remove_shrinker);
152
153#define SHRINK_BATCH 128
154/*
155 * Call the shrink functions to age shrinkable caches
156 *
157 * Here we assume it costs one seek to replace a lru page and that it also
158 * takes a seek to recreate a cache object. With this in mind we age equal
159 * percentages of the lru and ageable caches. This should balance the seeks
160 * generated by these structures.
161 *
162 * If the vm encounted mapped pages on the LRU it increase the pressure on
163 * slab to avoid swapping.
164 *
165 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
166 *
167 * `lru_pages' represents the number of on-LRU pages in all the zones which
168 * are eligible for the caller's allocation attempt. It is used for balancing
169 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700170 *
171 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
Andrew Morton69e05942006-03-22 00:08:19 -0800173unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
174 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800177 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if (scanned == 0)
180 scanned = SWAP_CLUSTER_MAX;
181
182 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700183 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 list_for_each_entry(shrinker, &shrinker_list, list) {
186 unsigned long long delta;
187 unsigned long total_scan;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800188 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800191 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 do_div(delta, lru_pages + 1);
193 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800194 if (shrinker->nr < 0) {
195 printk(KERN_ERR "%s: nr=%ld\n",
196 __FUNCTION__, shrinker->nr);
197 shrinker->nr = max_pass;
198 }
199
200 /*
201 * Avoid risking looping forever due to too large nr value:
202 * never try to free more than twice the estimate number of
203 * freeable entries.
204 */
205 if (shrinker->nr > max_pass * 2)
206 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 total_scan = shrinker->nr;
209 shrinker->nr = 0;
210
211 while (total_scan >= SHRINK_BATCH) {
212 long this_scan = SHRINK_BATCH;
213 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700214 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700216 nr_before = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
218 if (shrink_ret == -1)
219 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700220 if (shrink_ret < nr_before)
221 ret += nr_before - shrink_ret;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700222 count_vm_events(SLABS_SCANNED, this_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 total_scan -= this_scan;
224
225 cond_resched();
226 }
227
228 shrinker->nr += total_scan;
229 }
230 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700231 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234/* Called without lock on whether page is mapped, so answer is unstable */
235static inline int page_mapping_inuse(struct page *page)
236{
237 struct address_space *mapping;
238
239 /* Page is in somebody's page tables. */
240 if (page_mapped(page))
241 return 1;
242
243 /* Be more reluctant to reclaim swapcache than pagecache */
244 if (PageSwapCache(page))
245 return 1;
246
247 mapping = page_mapping(page);
248 if (!mapping)
249 return 0;
250
251 /* File is mmap'd by somebody? */
252 return mapping_mapped(mapping);
253}
254
255static inline int is_page_cache_freeable(struct page *page)
256{
257 return page_count(page) - !!PagePrivate(page) == 2;
258}
259
260static int may_write_to_queue(struct backing_dev_info *bdi)
261{
Christoph Lameter930d9152006-01-08 01:00:47 -0800262 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 1;
264 if (!bdi_write_congested(bdi))
265 return 1;
266 if (bdi == current->backing_dev_info)
267 return 1;
268 return 0;
269}
270
271/*
272 * We detected a synchronous write error writing a page out. Probably
273 * -ENOSPC. We need to propagate that into the address_space for a subsequent
274 * fsync(), msync() or close().
275 *
276 * The tricky part is that after writepage we cannot touch the mapping: nothing
277 * prevents it from being freed up. But we have a ref on the page and once
278 * that page is locked, the mapping is pinned.
279 *
280 * We're allowed to run sleeping lock_page() here because we know the caller has
281 * __GFP_FS.
282 */
283static void handle_write_error(struct address_space *mapping,
284 struct page *page, int error)
285{
286 lock_page(page);
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -0700287 if (page_mapping(page) == mapping)
288 mapping_set_error(mapping, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 unlock_page(page);
290}
291
Christoph Lameter04e62a22006-06-23 02:03:38 -0700292/* possible outcome of pageout() */
293typedef enum {
294 /* failed to write page out, page is locked */
295 PAGE_KEEP,
296 /* move page to the active list, page is locked */
297 PAGE_ACTIVATE,
298 /* page has been sent to the disk successfully, page is unlocked */
299 PAGE_SUCCESS,
300 /* page is clean and locked */
301 PAGE_CLEAN,
302} pageout_t;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/*
Andrew Morton1742f192006-03-22 00:08:21 -0800305 * pageout is called by shrink_page_list() for each dirty page.
306 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
Christoph Lameter04e62a22006-06-23 02:03:38 -0700308static pageout_t pageout(struct page *page, struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 /*
311 * If the page is dirty, only perform writeback if that write
312 * will be non-blocking. To prevent this allocation from being
313 * stalled by pagecache activity. But note that there may be
314 * stalls if we need to run get_block(). We could test
315 * PagePrivate for that.
316 *
317 * If this process is currently in generic_file_write() against
318 * this page's queue, we can perform writeback even if that
319 * will block.
320 *
321 * If the page is swapcache, write it back even if that would
322 * block, for some throttling. This happens by accident, because
323 * swap_backing_dev_info is bust: it doesn't reflect the
324 * congestion state of the swapdevs. Easy to fix, if needed.
325 * See swapfile.c:page_queue_congested().
326 */
327 if (!is_page_cache_freeable(page))
328 return PAGE_KEEP;
329 if (!mapping) {
330 /*
331 * Some data journaling orphaned pages can have
332 * page->mapping == NULL while being dirty with clean buffers.
333 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700334 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (try_to_free_buffers(page)) {
336 ClearPageDirty(page);
337 printk("%s: orphaned page\n", __FUNCTION__);
338 return PAGE_CLEAN;
339 }
340 }
341 return PAGE_KEEP;
342 }
343 if (mapping->a_ops->writepage == NULL)
344 return PAGE_ACTIVATE;
345 if (!may_write_to_queue(mapping->backing_dev_info))
346 return PAGE_KEEP;
347
348 if (clear_page_dirty_for_io(page)) {
349 int res;
350 struct writeback_control wbc = {
351 .sync_mode = WB_SYNC_NONE,
352 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700353 .range_start = 0,
354 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 .nonblocking = 1,
356 .for_reclaim = 1,
357 };
358
359 SetPageReclaim(page);
360 res = mapping->a_ops->writepage(page, &wbc);
361 if (res < 0)
362 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800363 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 ClearPageReclaim(page);
365 return PAGE_ACTIVATE;
366 }
367 if (!PageWriteback(page)) {
368 /* synchronous write or broken a_ops? */
369 ClearPageReclaim(page);
370 }
Andrew Mortone129b5c2006-09-27 01:50:00 -0700371 inc_zone_page_state(page, NR_VMSCAN_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return PAGE_SUCCESS;
373 }
374
375 return PAGE_CLEAN;
376}
377
Andrew Mortona649fd92006-10-17 00:09:36 -0700378/*
379 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
380 * someone else has a ref on the page, abort and return 0. If it was
381 * successfully detached, return 1. Assumes the caller has a single ref on
382 * this page.
383 */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800384int remove_mapping(struct address_space *mapping, struct page *page)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800385{
Nick Piggin28e4d962006-09-25 23:31:23 -0700386 BUG_ON(!PageLocked(page));
387 BUG_ON(mapping != page_mapping(page));
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800388
389 write_lock_irq(&mapping->tree_lock);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800390 /*
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700391 * The non racy check for a busy page.
392 *
393 * Must be careful with the order of the tests. When someone has
394 * a ref to the page, it may be possible that they dirty it then
395 * drop the reference. So if PageDirty is tested before page_count
396 * here, then the following race may occur:
397 *
398 * get_user_pages(&page);
399 * [user mapping goes away]
400 * write_to(page);
401 * !PageDirty(page) [good]
402 * SetPageDirty(page);
403 * put_page(page);
404 * !page_count(page) [good, discard it]
405 *
406 * [oops, our write_to data is lost]
407 *
408 * Reversing the order of the tests ensures such a situation cannot
409 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
410 * load is not satisfied before that of page->_count.
411 *
412 * Note that if SetPageDirty is always performed via set_page_dirty,
413 * and thus under tree_lock, then this ordering is not required.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800414 */
415 if (unlikely(page_count(page) != 2))
416 goto cannot_free;
417 smp_rmb();
418 if (unlikely(PageDirty(page)))
419 goto cannot_free;
420
421 if (PageSwapCache(page)) {
422 swp_entry_t swap = { .val = page_private(page) };
423 __delete_from_swap_cache(page);
424 write_unlock_irq(&mapping->tree_lock);
425 swap_free(swap);
426 __put_page(page); /* The pagecache ref */
427 return 1;
428 }
429
430 __remove_from_page_cache(page);
431 write_unlock_irq(&mapping->tree_lock);
432 __put_page(page);
433 return 1;
434
435cannot_free:
436 write_unlock_irq(&mapping->tree_lock);
437 return 0;
438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/*
Andrew Morton1742f192006-03-22 00:08:21 -0800441 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
Andrew Morton1742f192006-03-22 00:08:21 -0800443static unsigned long shrink_page_list(struct list_head *page_list,
444 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 LIST_HEAD(ret_pages);
447 struct pagevec freed_pvec;
448 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800449 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 cond_resched();
452
453 pagevec_init(&freed_pvec, 1);
454 while (!list_empty(page_list)) {
455 struct address_space *mapping;
456 struct page *page;
457 int may_enter_fs;
458 int referenced;
459
460 cond_resched();
461
462 page = lru_to_page(page_list);
463 list_del(&page->lru);
464
465 if (TestSetPageLocked(page))
466 goto keep;
467
Nick Piggin725d7042006-09-25 23:30:55 -0700468 VM_BUG_ON(PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800471
472 if (!sc->may_swap && page_mapped(page))
473 goto keep_locked;
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /* Double the slab pressure for mapped and swapcache pages */
476 if (page_mapped(page) || PageSwapCache(page))
477 sc->nr_scanned++;
478
479 if (PageWriteback(page))
480 goto keep_locked;
481
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800482 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* In active use or really unfreeable? Activate it. */
484 if (referenced && page_mapping_inuse(page))
485 goto activate_locked;
486
487#ifdef CONFIG_SWAP
488 /*
489 * Anonymous process memory has backing store?
490 * Try to allocate it some swap space here.
491 */
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800492 if (PageAnon(page) && !PageSwapCache(page))
Christoph Lameter1480a542006-01-08 01:00:53 -0800493 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495#endif /* CONFIG_SWAP */
496
497 mapping = page_mapping(page);
498 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
499 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
500
501 /*
502 * The page is mapped into the page tables of one or more
503 * processes. Try to unmap it here.
504 */
505 if (page_mapped(page) && mapping) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800506 switch (try_to_unmap(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 case SWAP_FAIL:
508 goto activate_locked;
509 case SWAP_AGAIN:
510 goto keep_locked;
511 case SWAP_SUCCESS:
512 ; /* try to free the page below */
513 }
514 }
515
516 if (PageDirty(page)) {
517 if (referenced)
518 goto keep_locked;
519 if (!may_enter_fs)
520 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800521 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto keep_locked;
523
524 /* Page is dirty, try to write it out here */
525 switch(pageout(page, mapping)) {
526 case PAGE_KEEP:
527 goto keep_locked;
528 case PAGE_ACTIVATE:
529 goto activate_locked;
530 case PAGE_SUCCESS:
531 if (PageWriteback(page) || PageDirty(page))
532 goto keep;
533 /*
534 * A synchronous write - probably a ramdisk. Go
535 * ahead and try to reclaim the page.
536 */
537 if (TestSetPageLocked(page))
538 goto keep;
539 if (PageDirty(page) || PageWriteback(page))
540 goto keep_locked;
541 mapping = page_mapping(page);
542 case PAGE_CLEAN:
543 ; /* try to free the page below */
544 }
545 }
546
547 /*
548 * If the page has buffers, try to free the buffer mappings
549 * associated with this page. If we succeed we try to free
550 * the page as well.
551 *
552 * We do this even if the page is PageDirty().
553 * try_to_release_page() does not perform I/O, but it is
554 * possible for a page to have PageDirty set, but it is actually
555 * clean (all its buffers are clean). This happens if the
556 * buffers were written out directly, with submit_bh(). ext3
557 * will do this, as well as the blockdev mapping.
558 * try_to_release_page() will discover that cleanness and will
559 * drop the buffers and mark the page clean - it can be freed.
560 *
561 * Rarely, pages can have buffers and no ->mapping. These are
562 * the pages which were not successfully invalidated in
563 * truncate_complete_page(). We try to drop those buffers here
564 * and if that worked, and the page is no longer mapped into
565 * process address space (page_count == 1) it can be freed.
566 * Otherwise, leave the page on the LRU so it is swappable.
567 */
568 if (PagePrivate(page)) {
569 if (!try_to_release_page(page, sc->gfp_mask))
570 goto activate_locked;
571 if (!mapping && page_count(page) == 1)
572 goto free_it;
573 }
574
Nick Piggin28e4d962006-09-25 23:31:23 -0700575 if (!mapping || !remove_mapping(mapping, page))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800576 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578free_it:
579 unlock_page(page);
Andrew Morton05ff5132006-03-22 00:08:20 -0800580 nr_reclaimed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (!pagevec_add(&freed_pvec, page))
582 __pagevec_release_nonlru(&freed_pvec);
583 continue;
584
585activate_locked:
586 SetPageActive(page);
587 pgactivate++;
588keep_locked:
589 unlock_page(page);
590keep:
591 list_add(&page->lru, &ret_pages);
Nick Piggin725d7042006-09-25 23:30:55 -0700592 VM_BUG_ON(PageLRU(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 list_splice(&ret_pages, page_list);
595 if (pagevec_count(&freed_pvec))
596 __pagevec_release_nonlru(&freed_pvec);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700597 count_vm_events(PGACTIVATE, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800598 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800601/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * zone->lru_lock is heavily contended. Some of the functions that
603 * shrink the lists perform better by taking out a batch of pages
604 * and working on them outside the LRU lock.
605 *
606 * For pagecache intensive workloads, this function is the hottest
607 * spot in the kernel (apart from copy_*_user functions).
608 *
609 * Appropriate locks must be held before calling this function.
610 *
611 * @nr_to_scan: The number of pages to look through on the list.
612 * @src: The LRU list to pull pages off.
613 * @dst: The temp list to put pages on to.
614 * @scanned: The number of pages that were scanned.
615 *
616 * returns how many pages were moved onto *@dst.
617 */
Andrew Morton69e05942006-03-22 00:08:19 -0800618static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
619 struct list_head *src, struct list_head *dst,
620 unsigned long *scanned)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Andrew Morton69e05942006-03-22 00:08:19 -0800622 unsigned long nr_taken = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct page *page;
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800624 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800626 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800627 struct list_head *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 page = lru_to_page(src);
629 prefetchw_prev_lru_page(page, src, flags);
630
Nick Piggin725d7042006-09-25 23:30:55 -0700631 VM_BUG_ON(!PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800632
Nick Piggin053837f2006-01-18 17:42:27 -0800633 list_del(&page->lru);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800634 target = src;
635 if (likely(get_page_unless_zero(page))) {
Nick Piggin053837f2006-01-18 17:42:27 -0800636 /*
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800637 * Be careful not to clear PageLRU until after we're
638 * sure the page is not being freed elsewhere -- the
639 * page release code relies on it.
Nick Piggin053837f2006-01-18 17:42:27 -0800640 */
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800641 ClearPageLRU(page);
642 target = dst;
643 nr_taken++;
644 } /* else it is being freed elsewhere */
Nick Piggin46453a62006-03-22 00:07:58 -0800645
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800646 list_add(&page->lru, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
649 *scanned = scan;
650 return nr_taken;
651}
652
653/*
Andrew Morton1742f192006-03-22 00:08:21 -0800654 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
655 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
Andrew Morton1742f192006-03-22 00:08:21 -0800657static unsigned long shrink_inactive_list(unsigned long max_scan,
658 struct zone *zone, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 LIST_HEAD(page_list);
661 struct pagevec pvec;
Andrew Morton69e05942006-03-22 00:08:19 -0800662 unsigned long nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800663 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 pagevec_init(&pvec, 1);
666
667 lru_add_drain();
668 spin_lock_irq(&zone->lru_lock);
Andrew Morton69e05942006-03-22 00:08:19 -0800669 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -0800671 unsigned long nr_taken;
672 unsigned long nr_scan;
673 unsigned long nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
676 &zone->inactive_list,
677 &page_list, &nr_scan);
Christoph Lameterc8785382007-02-10 01:43:01 -0800678 __mod_zone_page_state(zone, NR_INACTIVE, -nr_taken);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 zone->pages_scanned += nr_scan;
680 spin_unlock_irq(&zone->lru_lock);
681
Andrew Morton69e05942006-03-22 00:08:19 -0800682 nr_scanned += nr_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800683 nr_freed = shrink_page_list(&page_list, sc);
Andrew Morton05ff5132006-03-22 00:08:20 -0800684 nr_reclaimed += nr_freed;
Nick Piggina74609f2006-01-06 00:11:20 -0800685 local_irq_disable();
686 if (current_is_kswapd()) {
Christoph Lameterf8891e52006-06-30 01:55:45 -0700687 __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
688 __count_vm_events(KSWAPD_STEAL, nr_freed);
Nick Piggina74609f2006-01-06 00:11:20 -0800689 } else
Christoph Lameterf8891e52006-06-30 01:55:45 -0700690 __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
Shantanu Goel918d3f92006-12-29 16:48:59 -0800691 __count_zone_vm_events(PGSTEAL, zone, nr_freed);
Nick Piggina74609f2006-01-06 00:11:20 -0800692
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800693 if (nr_taken == 0)
694 goto done;
695
Nick Piggina74609f2006-01-06 00:11:20 -0800696 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /*
698 * Put back any unfreeable pages.
699 */
700 while (!list_empty(&page_list)) {
701 page = lru_to_page(&page_list);
Nick Piggin725d7042006-09-25 23:30:55 -0700702 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800703 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 list_del(&page->lru);
705 if (PageActive(page))
706 add_page_to_active_list(zone, page);
707 else
708 add_page_to_inactive_list(zone, page);
709 if (!pagevec_add(&pvec, page)) {
710 spin_unlock_irq(&zone->lru_lock);
711 __pagevec_release(&pvec);
712 spin_lock_irq(&zone->lru_lock);
713 }
714 }
Andrew Morton69e05942006-03-22 00:08:19 -0800715 } while (nr_scanned < max_scan);
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800716 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717done:
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800718 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 pagevec_release(&pvec);
Andrew Morton05ff5132006-03-22 00:08:20 -0800720 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
Martin Bligh3bb1a852006-10-28 10:38:24 -0700723/*
724 * We are about to scan this zone at a certain priority level. If that priority
725 * level is smaller (ie: more urgent) than the previous priority, then note
726 * that priority level within the zone. This is done so that when the next
727 * process comes in to scan this zone, it will immediately start out at this
728 * priority level rather than having to build up its own scanning priority.
729 * Here, this priority affects only the reclaim-mapped threshold.
730 */
731static inline void note_zone_scanning_priority(struct zone *zone, int priority)
732{
733 if (priority < zone->prev_priority)
734 zone->prev_priority = priority;
735}
736
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700737static inline int zone_is_near_oom(struct zone *zone)
738{
Christoph Lameterc8785382007-02-10 01:43:01 -0800739 return zone->pages_scanned >= (zone_page_state(zone, NR_ACTIVE)
740 + zone_page_state(zone, NR_INACTIVE))*3;
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743/*
744 * This moves pages from the active list to the inactive list.
745 *
746 * We move them the other way if the page is referenced by one or more
747 * processes, from rmap.
748 *
749 * If the pages are mostly unmapped, the processing is fast and it is
750 * appropriate to hold zone->lru_lock across the whole operation. But if
751 * the pages are mapped, the processing is slow (page_referenced()) so we
752 * should drop zone->lru_lock around each page. It's impossible to balance
753 * this, so instead we remove the pages from the LRU while processing them.
754 * It is safe to rely on PG_active against the non-LRU pages in here because
755 * nobody will play with that bit on a non-LRU page.
756 *
757 * The downside is that we have to touch page->_count against each page.
758 * But we had to alter page->flags anyway.
759 */
Andrew Morton1742f192006-03-22 00:08:21 -0800760static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
Martin Blighbbdb3962006-10-28 10:38:25 -0700761 struct scan_control *sc, int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Andrew Morton69e05942006-03-22 00:08:19 -0800763 unsigned long pgmoved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 int pgdeactivate = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800765 unsigned long pgscanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 LIST_HEAD(l_hold); /* The pages which were snipped off */
767 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
768 LIST_HEAD(l_active); /* Pages to go onto the active_list */
769 struct page *page;
770 struct pagevec pvec;
771 int reclaim_mapped = 0;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800772
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800773 if (sc->may_swap) {
Christoph Lameter2903fb12006-02-11 17:55:55 -0800774 long mapped_ratio;
775 long distress;
776 long swap_tendency;
777
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700778 if (zone_is_near_oom(zone))
779 goto force_reclaim_mapped;
780
Christoph Lameter2903fb12006-02-11 17:55:55 -0800781 /*
782 * `distress' is a measure of how much trouble we're having
783 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
784 */
Martin Blighbbdb3962006-10-28 10:38:25 -0700785 distress = 100 >> min(zone->prev_priority, priority);
Christoph Lameter2903fb12006-02-11 17:55:55 -0800786
787 /*
788 * The point of this algorithm is to decide when to start
789 * reclaiming mapped memory instead of just pagecache. Work out
790 * how much memory
791 * is mapped.
792 */
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700793 mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
794 global_page_state(NR_ANON_PAGES)) * 100) /
Christoph Lameterbf02cf42006-06-30 01:55:36 -0700795 vm_total_pages;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800796
797 /*
798 * Now decide how much we really want to unmap some pages. The
799 * mapped ratio is downgraded - just because there's a lot of
800 * mapped memory doesn't necessarily mean that page reclaim
801 * isn't succeeding.
802 *
803 * The distress ratio is important - we don't want to start
804 * going oom.
805 *
806 * A 100% value of vm_swappiness overrides this algorithm
807 * altogether.
808 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700809 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800810
811 /*
812 * Now use this metric to decide whether to start moving mapped
813 * memory onto the inactive list.
814 */
815 if (swap_tendency >= 100)
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700816force_reclaim_mapped:
Christoph Lameter2903fb12006-02-11 17:55:55 -0800817 reclaim_mapped = 1;
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 lru_add_drain();
821 spin_lock_irq(&zone->lru_lock);
822 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
823 &l_hold, &pgscanned);
824 zone->pages_scanned += pgscanned;
Christoph Lameterc8785382007-02-10 01:43:01 -0800825 __mod_zone_page_state(zone, NR_ACTIVE, -pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 spin_unlock_irq(&zone->lru_lock);
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 while (!list_empty(&l_hold)) {
829 cond_resched();
830 page = lru_to_page(&l_hold);
831 list_del(&page->lru);
832 if (page_mapped(page)) {
833 if (!reclaim_mapped ||
834 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800835 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 list_add(&page->lru, &l_active);
837 continue;
838 }
839 }
840 list_add(&page->lru, &l_inactive);
841 }
842
843 pagevec_init(&pvec, 1);
844 pgmoved = 0;
845 spin_lock_irq(&zone->lru_lock);
846 while (!list_empty(&l_inactive)) {
847 page = lru_to_page(&l_inactive);
848 prefetchw_prev_lru_page(page, &l_inactive, flags);
Nick Piggin725d7042006-09-25 23:30:55 -0700849 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800850 SetPageLRU(page);
Nick Piggin725d7042006-09-25 23:30:55 -0700851 VM_BUG_ON(!PageActive(page));
Nick Piggin4c84cac2006-03-22 00:08:00 -0800852 ClearPageActive(page);
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 list_move(&page->lru, &zone->inactive_list);
855 pgmoved++;
856 if (!pagevec_add(&pvec, page)) {
Christoph Lameterc8785382007-02-10 01:43:01 -0800857 __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 spin_unlock_irq(&zone->lru_lock);
859 pgdeactivate += pgmoved;
860 pgmoved = 0;
861 if (buffer_heads_over_limit)
862 pagevec_strip(&pvec);
863 __pagevec_release(&pvec);
864 spin_lock_irq(&zone->lru_lock);
865 }
866 }
Christoph Lameterc8785382007-02-10 01:43:01 -0800867 __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 pgdeactivate += pgmoved;
869 if (buffer_heads_over_limit) {
870 spin_unlock_irq(&zone->lru_lock);
871 pagevec_strip(&pvec);
872 spin_lock_irq(&zone->lru_lock);
873 }
874
875 pgmoved = 0;
876 while (!list_empty(&l_active)) {
877 page = lru_to_page(&l_active);
878 prefetchw_prev_lru_page(page, &l_active, flags);
Nick Piggin725d7042006-09-25 23:30:55 -0700879 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800880 SetPageLRU(page);
Nick Piggin725d7042006-09-25 23:30:55 -0700881 VM_BUG_ON(!PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 list_move(&page->lru, &zone->active_list);
883 pgmoved++;
884 if (!pagevec_add(&pvec, page)) {
Christoph Lameterc8785382007-02-10 01:43:01 -0800885 __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 pgmoved = 0;
887 spin_unlock_irq(&zone->lru_lock);
888 __pagevec_release(&pvec);
889 spin_lock_irq(&zone->lru_lock);
890 }
891 }
Christoph Lameterc8785382007-02-10 01:43:01 -0800892 __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Christoph Lameterf8891e52006-06-30 01:55:45 -0700894 __count_zone_vm_events(PGREFILL, zone, pgscanned);
895 __count_vm_events(PGDEACTIVATE, pgdeactivate);
896 spin_unlock_irq(&zone->lru_lock);
Nick Piggina74609f2006-01-06 00:11:20 -0800897
898 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901/*
902 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
903 */
Andrew Morton05ff5132006-03-22 00:08:20 -0800904static unsigned long shrink_zone(int priority, struct zone *zone,
905 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 unsigned long nr_active;
908 unsigned long nr_inactive;
Christoph Lameter86959492006-03-22 00:08:18 -0800909 unsigned long nr_to_scan;
Andrew Morton05ff5132006-03-22 00:08:20 -0800910 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Martin Hicks53e9a612005-09-03 15:54:51 -0700912 atomic_inc(&zone->reclaim_in_progress);
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /*
915 * Add one to `nr_to_scan' just to make sure that the kernel will
916 * slowly sift through the active list.
917 */
Christoph Lameterc8785382007-02-10 01:43:01 -0800918 zone->nr_scan_active +=
919 (zone_page_state(zone, NR_ACTIVE) >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 nr_active = zone->nr_scan_active;
921 if (nr_active >= sc->swap_cluster_max)
922 zone->nr_scan_active = 0;
923 else
924 nr_active = 0;
925
Christoph Lameterc8785382007-02-10 01:43:01 -0800926 zone->nr_scan_inactive +=
927 (zone_page_state(zone, NR_INACTIVE) >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 nr_inactive = zone->nr_scan_inactive;
929 if (nr_inactive >= sc->swap_cluster_max)
930 zone->nr_scan_inactive = 0;
931 else
932 nr_inactive = 0;
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 while (nr_active || nr_inactive) {
935 if (nr_active) {
Christoph Lameter86959492006-03-22 00:08:18 -0800936 nr_to_scan = min(nr_active,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -0800938 nr_active -= nr_to_scan;
Martin Blighbbdb3962006-10-28 10:38:25 -0700939 shrink_active_list(nr_to_scan, zone, sc, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
942 if (nr_inactive) {
Christoph Lameter86959492006-03-22 00:08:18 -0800943 nr_to_scan = min(nr_inactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -0800945 nr_inactive -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800946 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
947 sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949 }
950
Andrew Morton232ea4d2007-02-28 20:13:21 -0800951 throttle_vm_writeout(sc->gfp_mask);
Martin Hicks53e9a612005-09-03 15:54:51 -0700952
953 atomic_dec(&zone->reclaim_in_progress);
Andrew Morton05ff5132006-03-22 00:08:20 -0800954 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957/*
958 * This is the direct reclaim path, for page-allocating processes. We only
959 * try to reclaim pages from zones which will satisfy the caller's allocation
960 * request.
961 *
962 * We reclaim from a zone even if that zone is over pages_high. Because:
963 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
964 * allocation or
965 * b) The zones may be over pages_high but they must go *over* pages_high to
966 * satisfy the `incremental min' zone defense algorithm.
967 *
968 * Returns the number of reclaimed pages.
969 *
970 * If a zone is deemed to be full of pinned pages then just give it a light
971 * scan then give up on it.
972 */
Andrew Morton1742f192006-03-22 00:08:21 -0800973static unsigned long shrink_zones(int priority, struct zone **zones,
Andrew Morton05ff5132006-03-22 00:08:20 -0800974 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Andrew Morton05ff5132006-03-22 00:08:20 -0800976 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 int i;
978
Nick Piggin408d8542006-09-25 23:31:27 -0700979 sc->all_unreclaimable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 for (i = 0; zones[i] != NULL; i++) {
981 struct zone *zone = zones[i];
982
Con Kolivasf3fe6512006-01-06 00:11:15 -0800983 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 continue;
985
Paul Jackson02a0e532006-12-13 00:34:25 -0800986 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 continue;
988
Martin Bligh3bb1a852006-10-28 10:38:24 -0700989 note_zone_scanning_priority(zone, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Christoph Lameter86959492006-03-22 00:08:18 -0800991 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 continue; /* Let kswapd poll it */
993
Nick Piggin408d8542006-09-25 23:31:27 -0700994 sc->all_unreclaimable = 0;
995
Andrew Morton05ff5132006-03-22 00:08:20 -0800996 nr_reclaimed += shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Andrew Morton05ff5132006-03-22 00:08:20 -0800998 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001/*
1002 * This is the main entry point to direct page reclaim.
1003 *
1004 * If a full scan of the inactive list fails to free enough memory then we
1005 * are "out of memory" and something needs to be killed.
1006 *
1007 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1008 * high - the zone may be full of dirty or under-writeback pages, which this
1009 * caller can't do much about. We kick pdflush and take explicit naps in the
1010 * hope that some of these pages can be written. But if the allocating task
1011 * holds filesystem locks which prevent writeout this might not work, and the
1012 * allocation attempt will fail.
1013 */
Andrew Morton69e05942006-03-22 00:08:19 -08001014unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 int priority;
1017 int ret = 0;
Andrew Morton69e05942006-03-22 00:08:19 -08001018 unsigned long total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001019 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct reclaim_state *reclaim_state = current->reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 unsigned long lru_pages = 0;
1022 int i;
Andrew Morton179e9632006-03-22 00:08:18 -08001023 struct scan_control sc = {
1024 .gfp_mask = gfp_mask,
1025 .may_writepage = !laptop_mode,
1026 .swap_cluster_max = SWAP_CLUSTER_MAX,
1027 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001028 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -08001029 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Christoph Lameterf8891e52006-06-30 01:55:45 -07001031 count_vm_event(ALLOCSTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 for (i = 0; zones[i] != NULL; i++) {
1034 struct zone *zone = zones[i];
1035
Paul Jackson02a0e532006-12-13 00:34:25 -08001036 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 continue;
1038
Christoph Lameterc8785382007-02-10 01:43:01 -08001039 lru_pages += zone_page_state(zone, NR_ACTIVE)
1040 + zone_page_state(zone, NR_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
1043 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 sc.nr_scanned = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001045 if (!priority)
1046 disable_swap_token();
Andrew Morton1742f192006-03-22 00:08:21 -08001047 nr_reclaimed += shrink_zones(priority, zones, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
1049 if (reclaim_state) {
Andrew Morton05ff5132006-03-22 00:08:20 -08001050 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 reclaim_state->reclaimed_slab = 0;
1052 }
1053 total_scanned += sc.nr_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001054 if (nr_reclaimed >= sc.swap_cluster_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 ret = 1;
1056 goto out;
1057 }
1058
1059 /*
1060 * Try to write back as many pages as we just scanned. This
1061 * tends to cause slow streaming writers to write data to the
1062 * disk smoothly, at the dirtying rate, which is nice. But
1063 * that's undesirable in laptop mode, where we *want* lumpy
1064 * writeout. So in laptop mode, write out the whole world.
1065 */
Andrew Morton179e9632006-03-22 00:08:18 -08001066 if (total_scanned > sc.swap_cluster_max +
1067 sc.swap_cluster_max / 2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001068 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 sc.may_writepage = 1;
1070 }
1071
1072 /* Take a nap, wait for some writeback to complete */
1073 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001074 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Nick Piggin408d8542006-09-25 23:31:27 -07001076 /* top priority shrink_caches still had more to do? don't OOM, then */
1077 if (!sc.all_unreclaimable)
1078 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079out:
Martin Bligh3bb1a852006-10-28 10:38:24 -07001080 /*
1081 * Now that we've scanned all the zones at this priority level, note
1082 * that level within the zone so that the next thread which performs
1083 * scanning of this zone will immediately start out at this priority
1084 * level. This affects only the decision whether or not to bring
1085 * mapped pages onto the inactive list.
1086 */
1087 if (priority < 0)
1088 priority = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 for (i = 0; zones[i] != 0; i++) {
1090 struct zone *zone = zones[i];
1091
Paul Jackson02a0e532006-12-13 00:34:25 -08001092 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 continue;
1094
Martin Bligh3bb1a852006-10-28 10:38:24 -07001095 zone->prev_priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097 return ret;
1098}
1099
1100/*
1101 * For kswapd, balance_pgdat() will work across all this node's zones until
1102 * they are all at pages_high.
1103 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 * Returns the number of pages which were actually freed.
1105 *
1106 * There is special handling here for zones which are full of pinned pages.
1107 * This can happen if the pages are all mlocked, or if they are all used by
1108 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1109 * What we do is to detect the case where all pages in the zone have been
1110 * scanned twice and there has been zero successful reclaim. Mark the zone as
1111 * dead and from now on, only perform a short scan. Basically we're polling
1112 * the zone for when the problem goes away.
1113 *
1114 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1115 * zones which have free_pages > pages_high, but once a zone is found to have
1116 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1117 * of the number of free pages in the lower zones. This interoperates with
1118 * the page allocator fallback scheme to ensure that aging of pages is balanced
1119 * across the zones.
1120 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001121static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 int all_zones_ok;
1124 int priority;
1125 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08001126 unsigned long total_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001127 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08001129 struct scan_control sc = {
1130 .gfp_mask = GFP_KERNEL,
1131 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001132 .swap_cluster_max = SWAP_CLUSTER_MAX,
1133 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -08001134 };
Martin Bligh3bb1a852006-10-28 10:38:24 -07001135 /*
1136 * temp_priority is used to remember the scanning priority at which
1137 * this zone was successfully refilled to free_pages == pages_high.
1138 */
1139 int temp_priority[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141loop_again:
1142 total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001143 nr_reclaimed = 0;
Christoph Lameterc0bbbc72006-06-11 15:22:26 -07001144 sc.may_writepage = !laptop_mode;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001145 count_vm_event(PAGEOUTRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Martin Bligh3bb1a852006-10-28 10:38:24 -07001147 for (i = 0; i < pgdat->nr_zones; i++)
1148 temp_priority[i] = DEF_PRIORITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1151 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1152 unsigned long lru_pages = 0;
1153
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001154 /* The swap token gets in the way of swapout... */
1155 if (!priority)
1156 disable_swap_token();
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 all_zones_ok = 1;
1159
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001160 /*
1161 * Scan in the highmem->dma direction for the highest
1162 * zone which needs scanning
1163 */
1164 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1165 struct zone *zone = pgdat->node_zones + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001167 if (!populated_zone(zone))
1168 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001170 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1171 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001173 if (!zone_watermark_ok(zone, order, zone->pages_high,
1174 0, 0)) {
1175 end_zone = i;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08001176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
Andrew Mortone1dbeda2006-12-06 20:32:01 -08001179 if (i < 0)
1180 goto out;
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 for (i = 0; i <= end_zone; i++) {
1183 struct zone *zone = pgdat->node_zones + i;
1184
Christoph Lameterc8785382007-02-10 01:43:01 -08001185 lru_pages += zone_page_state(zone, NR_ACTIVE)
1186 + zone_page_state(zone, NR_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
1189 /*
1190 * Now scan the zone in the dma->highmem direction, stopping
1191 * at the last zone which needs scanning.
1192 *
1193 * We do this because the page allocator works in the opposite
1194 * direction. This prevents the page allocator from allocating
1195 * pages behind kswapd's direction of progress, which would
1196 * cause too much scanning of the lower zones.
1197 */
1198 for (i = 0; i <= end_zone; i++) {
1199 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001200 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Con Kolivasf3fe6512006-01-06 00:11:15 -08001202 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 continue;
1204
1205 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1206 continue;
1207
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001208 if (!zone_watermark_ok(zone, order, zone->pages_high,
1209 end_zone, 0))
1210 all_zones_ok = 0;
Martin Bligh3bb1a852006-10-28 10:38:24 -07001211 temp_priority[i] = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 sc.nr_scanned = 0;
Martin Bligh3bb1a852006-10-28 10:38:24 -07001213 note_zone_scanning_priority(zone, priority);
Andrew Morton05ff5132006-03-22 00:08:20 -08001214 nr_reclaimed += shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001216 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1217 lru_pages);
Andrew Morton05ff5132006-03-22 00:08:20 -08001218 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 total_scanned += sc.nr_scanned;
1220 if (zone->all_unreclaimable)
1221 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001222 if (nr_slab == 0 && zone->pages_scanned >=
Christoph Lameterc8785382007-02-10 01:43:01 -08001223 (zone_page_state(zone, NR_ACTIVE)
1224 + zone_page_state(zone, NR_INACTIVE)) * 6)
1225 zone->all_unreclaimable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /*
1227 * If we've done a decent amount of scanning and
1228 * the reclaim ratio is low, start doing writepage
1229 * even in laptop mode
1230 */
1231 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Andrew Morton05ff5132006-03-22 00:08:20 -08001232 total_scanned > nr_reclaimed + nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 sc.may_writepage = 1;
1234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (all_zones_ok)
1236 break; /* kswapd: all done */
1237 /*
1238 * OK, kswapd is getting into trouble. Take a nap, then take
1239 * another pass across the zones.
1240 */
1241 if (total_scanned && priority < DEF_PRIORITY - 2)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001242 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 /*
1245 * We do this so kswapd doesn't build up large priorities for
1246 * example when it is freeing in parallel with allocators. It
1247 * matches the direct reclaim path behaviour in terms of impact
1248 * on zone->*_priority.
1249 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001250 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 break;
1252 }
1253out:
Martin Bligh3bb1a852006-10-28 10:38:24 -07001254 /*
1255 * Note within each zone the priority level at which this zone was
1256 * brought into a happy state. So that the next thread which scans this
1257 * zone will start out at that priority level.
1258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 for (i = 0; i < pgdat->nr_zones; i++) {
1260 struct zone *zone = pgdat->node_zones + i;
1261
Martin Bligh3bb1a852006-10-28 10:38:24 -07001262 zone->prev_priority = temp_priority[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264 if (!all_zones_ok) {
1265 cond_resched();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001266
1267 try_to_freeze();
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 goto loop_again;
1270 }
1271
Andrew Morton05ff5132006-03-22 00:08:20 -08001272 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
1275/*
1276 * The background pageout daemon, started as a kernel thread
1277 * from the init process.
1278 *
1279 * This basically trickles out pages so that we have _some_
1280 * free memory available even if there is no other activity
1281 * that frees anything up. This is needed for things like routing
1282 * etc, where we otherwise might have all activity going on in
1283 * asynchronous contexts that cannot page things out.
1284 *
1285 * If there are applications that are active memory-allocators
1286 * (most normal use), this basically shouldn't matter.
1287 */
1288static int kswapd(void *p)
1289{
1290 unsigned long order;
1291 pg_data_t *pgdat = (pg_data_t*)p;
1292 struct task_struct *tsk = current;
1293 DEFINE_WAIT(wait);
1294 struct reclaim_state reclaim_state = {
1295 .reclaimed_slab = 0,
1296 };
1297 cpumask_t cpumask;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 cpumask = node_to_cpumask(pgdat->node_id);
1300 if (!cpus_empty(cpumask))
1301 set_cpus_allowed(tsk, cpumask);
1302 current->reclaim_state = &reclaim_state;
1303
1304 /*
1305 * Tell the memory management that we're a "memory allocator",
1306 * and that if we need more memory we should get access to it
1307 * regardless (see "__alloc_pages()"). "kswapd" should
1308 * never get caught in the normal page freeing logic.
1309 *
1310 * (Kswapd normally doesn't need memory anyway, but sometimes
1311 * you need a small amount of memory in order to be able to
1312 * page out something else, and this flag essentially protects
1313 * us from recursively trying to free more memory as we're
1314 * trying to free the first piece of memory in the first place).
1315 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001316 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 order = 0;
1319 for ( ; ; ) {
1320 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1323 new_order = pgdat->kswapd_max_order;
1324 pgdat->kswapd_max_order = 0;
1325 if (order < new_order) {
1326 /*
1327 * Don't sleep if someone wants a larger 'order'
1328 * allocation
1329 */
1330 order = new_order;
1331 } else {
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07001332 if (!freezing(current))
1333 schedule();
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 order = pgdat->kswapd_max_order;
1336 }
1337 finish_wait(&pgdat->kswapd_wait, &wait);
1338
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07001339 if (!try_to_freeze()) {
1340 /* We can speed up thawing tasks if we don't call
1341 * balance_pgdat after returning from the refrigerator
1342 */
1343 balance_pgdat(pgdat, order);
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 return 0;
1347}
1348
1349/*
1350 * A zone is low on free memory, so wake its kswapd task to service it.
1351 */
1352void wakeup_kswapd(struct zone *zone, int order)
1353{
1354 pg_data_t *pgdat;
1355
Con Kolivasf3fe6512006-01-06 00:11:15 -08001356 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return;
1358
1359 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001360 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return;
1362 if (pgdat->kswapd_max_order < order)
1363 pgdat->kswapd_max_order = order;
Paul Jackson02a0e532006-12-13 00:34:25 -08001364 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001366 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001368 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
1371#ifdef CONFIG_PM
1372/*
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001373 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1374 * from LRU lists system-wide, for given pass and priority, and returns the
1375 * number of reclaimed pages
1376 *
1377 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1378 */
Nigel Cunninghame07aa052006-12-22 01:07:21 -08001379static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
1380 int pass, struct scan_control *sc)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001381{
1382 struct zone *zone;
1383 unsigned long nr_to_scan, ret = 0;
1384
1385 for_each_zone(zone) {
1386
1387 if (!populated_zone(zone))
1388 continue;
1389
1390 if (zone->all_unreclaimable && prio != DEF_PRIORITY)
1391 continue;
1392
1393 /* For pass = 0 we don't shrink the active list */
1394 if (pass > 0) {
Christoph Lameterc8785382007-02-10 01:43:01 -08001395 zone->nr_scan_active +=
1396 (zone_page_state(zone, NR_ACTIVE) >> prio) + 1;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001397 if (zone->nr_scan_active >= nr_pages || pass > 3) {
1398 zone->nr_scan_active = 0;
Christoph Lameterc8785382007-02-10 01:43:01 -08001399 nr_to_scan = min(nr_pages,
1400 zone_page_state(zone, NR_ACTIVE));
Martin Blighbbdb3962006-10-28 10:38:25 -07001401 shrink_active_list(nr_to_scan, zone, sc, prio);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001402 }
1403 }
1404
Christoph Lameterc8785382007-02-10 01:43:01 -08001405 zone->nr_scan_inactive +=
1406 (zone_page_state(zone, NR_INACTIVE) >> prio) + 1;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001407 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1408 zone->nr_scan_inactive = 0;
Christoph Lameterc8785382007-02-10 01:43:01 -08001409 nr_to_scan = min(nr_pages,
1410 zone_page_state(zone, NR_INACTIVE));
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001411 ret += shrink_inactive_list(nr_to_scan, zone, sc);
1412 if (ret >= nr_pages)
1413 return ret;
1414 }
1415 }
1416
1417 return ret;
1418}
1419
Andrew Morton76395d32007-01-05 16:37:05 -08001420static unsigned long count_lru_pages(void)
1421{
Christoph Lameterc8785382007-02-10 01:43:01 -08001422 return global_page_state(NR_ACTIVE) + global_page_state(NR_INACTIVE);
Andrew Morton76395d32007-01-05 16:37:05 -08001423}
1424
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001425/*
1426 * Try to free `nr_pages' of memory, system-wide, and return the number of
1427 * freed pages.
1428 *
1429 * Rather than trying to age LRUs the aim is to preserve the overall
1430 * LRU order by reclaiming preferentially
1431 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
Andrew Morton69e05942006-03-22 00:08:19 -08001433unsigned long shrink_all_memory(unsigned long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001435 unsigned long lru_pages, nr_slab;
Andrew Morton69e05942006-03-22 00:08:19 -08001436 unsigned long ret = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001437 int pass;
1438 struct reclaim_state reclaim_state;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001439 struct scan_control sc = {
1440 .gfp_mask = GFP_KERNEL,
1441 .may_swap = 0,
1442 .swap_cluster_max = nr_pages,
1443 .may_writepage = 1,
1444 .swappiness = vm_swappiness,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 };
1446
1447 current->reclaim_state = &reclaim_state;
Andrew Morton69e05942006-03-22 00:08:19 -08001448
Andrew Morton76395d32007-01-05 16:37:05 -08001449 lru_pages = count_lru_pages();
Christoph Lameter972d1a72006-09-25 23:31:51 -07001450 nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001451 /* If slab caches are huge, it's better to hit them first */
1452 while (nr_slab >= lru_pages) {
1453 reclaim_state.reclaimed_slab = 0;
1454 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1455 if (!reclaim_state.reclaimed_slab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 break;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001457
1458 ret += reclaim_state.reclaimed_slab;
1459 if (ret >= nr_pages)
1460 goto out;
1461
1462 nr_slab -= reclaim_state.reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001464
1465 /*
1466 * We try to shrink LRUs in 5 passes:
1467 * 0 = Reclaim from inactive_list only
1468 * 1 = Reclaim from active list but don't reclaim mapped
1469 * 2 = 2nd pass of type 1
1470 * 3 = Reclaim mapped (normal reclaim)
1471 * 4 = 2nd pass of type 3
1472 */
1473 for (pass = 0; pass < 5; pass++) {
1474 int prio;
1475
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001476 /* Force reclaiming mapped pages in the passes #3 and #4 */
1477 if (pass > 2) {
1478 sc.may_swap = 1;
1479 sc.swappiness = 100;
1480 }
1481
1482 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1483 unsigned long nr_to_scan = nr_pages - ret;
1484
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001485 sc.nr_scanned = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001486 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1487 if (ret >= nr_pages)
1488 goto out;
1489
1490 reclaim_state.reclaimed_slab = 0;
Andrew Morton76395d32007-01-05 16:37:05 -08001491 shrink_slab(sc.nr_scanned, sc.gfp_mask,
1492 count_lru_pages());
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001493 ret += reclaim_state.reclaimed_slab;
1494 if (ret >= nr_pages)
1495 goto out;
1496
1497 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001498 congestion_wait(WRITE, HZ / 10);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001499 }
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001500 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001501
1502 /*
1503 * If ret = 0, we could not shrink LRUs, but there may be something
1504 * in slab caches
1505 */
Andrew Morton76395d32007-01-05 16:37:05 -08001506 if (!ret) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001507 do {
1508 reclaim_state.reclaimed_slab = 0;
Andrew Morton76395d32007-01-05 16:37:05 -08001509 shrink_slab(nr_pages, sc.gfp_mask, count_lru_pages());
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001510 ret += reclaim_state.reclaimed_slab;
1511 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
Andrew Morton76395d32007-01-05 16:37:05 -08001512 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001513
1514out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 current->reclaim_state = NULL;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return ret;
1518}
1519#endif
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521/* It's optimal to keep kswapds on the same CPUs as their memory, but
1522 not required for correctness. So if the last cpu in a node goes
1523 away, we get changed to run anywhere: as the first one comes back,
1524 restore their cpu bindings. */
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07001525static int __devinit cpu_callback(struct notifier_block *nfb,
Andrew Morton69e05942006-03-22 00:08:19 -08001526 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 pg_data_t *pgdat;
1529 cpumask_t mask;
1530
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001531 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08001532 for_each_online_pgdat(pgdat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 mask = node_to_cpumask(pgdat->node_id);
1534 if (any_online_cpu(mask) != NR_CPUS)
1535 /* One of our CPUs online: restore mask */
1536 set_cpus_allowed(pgdat->kswapd, mask);
1537 }
1538 }
1539 return NOTIFY_OK;
1540}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Yasunori Goto3218ae12006-06-27 02:53:33 -07001542/*
1543 * This kswapd start function will be called by init and node-hot-add.
1544 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
1545 */
1546int kswapd_run(int nid)
1547{
1548 pg_data_t *pgdat = NODE_DATA(nid);
1549 int ret = 0;
1550
1551 if (pgdat->kswapd)
1552 return 0;
1553
1554 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
1555 if (IS_ERR(pgdat->kswapd)) {
1556 /* failure at boot is fatal */
1557 BUG_ON(system_state == SYSTEM_BOOTING);
1558 printk("Failed to start kswapd on node %d\n",nid);
1559 ret = -1;
1560 }
1561 return ret;
1562}
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564static int __init kswapd_init(void)
1565{
Yasunori Goto3218ae12006-06-27 02:53:33 -07001566 int nid;
Andrew Morton69e05942006-03-22 00:08:19 -08001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 swap_setup();
Yasunori Goto3218ae12006-06-27 02:53:33 -07001569 for_each_online_node(nid)
1570 kswapd_run(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 hotcpu_notifier(cpu_callback, 0);
1572 return 0;
1573}
1574
1575module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001576
1577#ifdef CONFIG_NUMA
1578/*
1579 * Zone reclaim mode
1580 *
1581 * If non-zero call zone_reclaim when the number of free pages falls below
1582 * the watermarks.
Christoph Lameter9eeff232006-01-18 17:42:31 -08001583 */
1584int zone_reclaim_mode __read_mostly;
1585
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001586#define RECLAIM_OFF 0
1587#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1588#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1589#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1590
Christoph Lameter9eeff232006-01-18 17:42:31 -08001591/*
Christoph Lametera92f7122006-02-01 03:05:32 -08001592 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1593 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1594 * a zone.
1595 */
1596#define ZONE_RECLAIM_PRIORITY 4
1597
Christoph Lameter9eeff232006-01-18 17:42:31 -08001598/*
Christoph Lameter96146342006-07-03 00:24:13 -07001599 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
1600 * occur.
1601 */
1602int sysctl_min_unmapped_ratio = 1;
1603
1604/*
Christoph Lameter0ff38492006-09-25 23:31:52 -07001605 * If the number of slab pages in a zone grows beyond this percentage then
1606 * slab reclaim needs to occur.
1607 */
1608int sysctl_min_slab_ratio = 5;
1609
1610/*
Christoph Lameter9eeff232006-01-18 17:42:31 -08001611 * Try to free up some pages from this zone through reclaim.
1612 */
Andrew Morton179e9632006-03-22 00:08:18 -08001613static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001614{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001615 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08001616 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001617 struct task_struct *p = current;
1618 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08001619 int priority;
Andrew Morton05ff5132006-03-22 00:08:20 -08001620 unsigned long nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001621 struct scan_control sc = {
1622 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1623 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
Andrew Morton69e05942006-03-22 00:08:19 -08001624 .swap_cluster_max = max_t(unsigned long, nr_pages,
1625 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08001626 .gfp_mask = gfp_mask,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001627 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -08001628 };
Christoph Lameter83e33a42006-09-25 23:31:53 -07001629 unsigned long slab_reclaimable;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001630
1631 disable_swap_token();
Christoph Lameter9eeff232006-01-18 17:42:31 -08001632 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08001633 /*
1634 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1635 * and we also need to be able to write out pages for RECLAIM_WRITE
1636 * and RECLAIM_SWAP.
1637 */
1638 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001639 reclaim_state.reclaimed_slab = 0;
1640 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001641
Christoph Lameter0ff38492006-09-25 23:31:52 -07001642 if (zone_page_state(zone, NR_FILE_PAGES) -
1643 zone_page_state(zone, NR_FILE_MAPPED) >
1644 zone->min_unmapped_pages) {
1645 /*
1646 * Free memory by calling shrink zone with increasing
1647 * priorities until we have enough memory freed.
1648 */
1649 priority = ZONE_RECLAIM_PRIORITY;
1650 do {
Martin Bligh3bb1a852006-10-28 10:38:24 -07001651 note_zone_scanning_priority(zone, priority);
Christoph Lameter0ff38492006-09-25 23:31:52 -07001652 nr_reclaimed += shrink_zone(priority, zone, &sc);
1653 priority--;
1654 } while (priority >= 0 && nr_reclaimed < nr_pages);
1655 }
Christoph Lameterc84db232006-02-01 03:05:29 -08001656
Christoph Lameter83e33a42006-09-25 23:31:53 -07001657 slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1658 if (slab_reclaimable > zone->min_slab_pages) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001659 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001660 * shrink_slab() does not currently allow us to determine how
Christoph Lameter0ff38492006-09-25 23:31:52 -07001661 * many pages were freed in this zone. So we take the current
1662 * number of slab pages and shake the slab until it is reduced
1663 * by the same nr_pages that we used for reclaiming unmapped
1664 * pages.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001665 *
Christoph Lameter0ff38492006-09-25 23:31:52 -07001666 * Note that shrink_slab will free memory on all zones and may
1667 * take a long time.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001668 */
Christoph Lameter0ff38492006-09-25 23:31:52 -07001669 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
Christoph Lameter83e33a42006-09-25 23:31:53 -07001670 zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
1671 slab_reclaimable - nr_pages)
Christoph Lameter0ff38492006-09-25 23:31:52 -07001672 ;
Christoph Lameter83e33a42006-09-25 23:31:53 -07001673
1674 /*
1675 * Update nr_reclaimed by the number of slab pages we
1676 * reclaimed from this zone.
1677 */
1678 nr_reclaimed += slab_reclaimable -
1679 zone_page_state(zone, NR_SLAB_RECLAIMABLE);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001680 }
1681
Christoph Lameter9eeff232006-01-18 17:42:31 -08001682 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08001683 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Andrew Morton05ff5132006-03-22 00:08:20 -08001684 return nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001685}
Andrew Morton179e9632006-03-22 00:08:18 -08001686
1687int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1688{
1689 cpumask_t mask;
1690 int node_id;
1691
1692 /*
Christoph Lameter0ff38492006-09-25 23:31:52 -07001693 * Zone reclaim reclaims unmapped file backed pages and
1694 * slab pages if we are over the defined limits.
Christoph Lameter34aa1332006-06-30 01:55:37 -07001695 *
Christoph Lameter96146342006-07-03 00:24:13 -07001696 * A small portion of unmapped file backed pages is needed for
1697 * file I/O otherwise pages read by file I/O will be immediately
1698 * thrown out if the zone is overallocated. So we do not reclaim
1699 * if less than a specified percentage of the zone is used by
1700 * unmapped file backed pages.
Andrew Morton179e9632006-03-22 00:08:18 -08001701 */
Christoph Lameter34aa1332006-06-30 01:55:37 -07001702 if (zone_page_state(zone, NR_FILE_PAGES) -
Christoph Lameter0ff38492006-09-25 23:31:52 -07001703 zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
1704 && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
1705 <= zone->min_slab_pages)
Christoph Lameter96146342006-07-03 00:24:13 -07001706 return 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001707
1708 /*
1709 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1710 * not have reclaimable pages and if we should not delay the allocation
1711 * then do not scan.
1712 */
1713 if (!(gfp_mask & __GFP_WAIT) ||
1714 zone->all_unreclaimable ||
1715 atomic_read(&zone->reclaim_in_progress) > 0 ||
1716 (current->flags & PF_MEMALLOC))
1717 return 0;
1718
1719 /*
1720 * Only run zone reclaim on the local zone or on zones that do not
1721 * have associated processors. This will favor the local processor
1722 * over remote processors and spread off node memory allocations
1723 * as wide as possible.
1724 */
Christoph Lameter89fa3022006-09-25 23:31:55 -07001725 node_id = zone_to_nid(zone);
Andrew Morton179e9632006-03-22 00:08:18 -08001726 mask = node_to_cpumask(node_id);
1727 if (!cpus_empty(mask) && node_id != numa_node_id())
1728 return 0;
1729 return __zone_reclaim(zone, gfp_mask, order);
1730}
Christoph Lameter9eeff232006-01-18 17:42:31 -08001731#endif