blob: 46be8a02280e70f1526e518a820340977a3afc69 [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>
Rafael J. Wysocki248a0302006-03-22 00:09:04 -080036#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/tlbflush.h>
39#include <asm/div64.h>
40
41#include <linux/swapops.h>
42
Nick Piggin0f8053a2006-03-22 00:08:33 -080043#include "internal.h"
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045struct scan_control {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 /* Incremented by the number of inactive pages that were scanned */
47 unsigned long nr_scanned;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 unsigned long nr_mapped; /* From page_state */
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040052 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 int may_writepage;
55
Christoph Lameterf1fd1062006-01-18 17:42:30 -080056 /* Can pages be swapped as part of reclaim? */
57 int may_swap;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
60 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
61 * In this context, it doesn't matter that we scan the
62 * whole list at once. */
63 int swap_cluster_max;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -070064
65 int swappiness;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
68/*
69 * The list of shrinker callbacks used by to apply pressure to
70 * ageable caches.
71 */
72struct shrinker {
73 shrinker_t shrinker;
74 struct list_head list;
75 int seeks; /* seeks to recreate an obj */
76 long nr; /* objs pending delete */
77};
78
79#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
80
81#ifdef ARCH_HAS_PREFETCH
82#define prefetch_prev_lru_page(_page, _base, _field) \
83 do { \
84 if ((_page)->lru.prev != _base) { \
85 struct page *prev; \
86 \
87 prev = lru_to_page(&(_page->lru)); \
88 prefetch(&prev->_field); \
89 } \
90 } while (0)
91#else
92#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
93#endif
94
95#ifdef ARCH_HAS_PREFETCHW
96#define prefetchw_prev_lru_page(_page, _base, _field) \
97 do { \
98 if ((_page)->lru.prev != _base) { \
99 struct page *prev; \
100 \
101 prev = lru_to_page(&(_page->lru)); \
102 prefetchw(&prev->_field); \
103 } \
104 } while (0)
105#else
106#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
107#endif
108
109/*
110 * From 0 .. 100. Higher means more swappy.
111 */
112int vm_swappiness = 60;
113static long total_memory;
114
115static LIST_HEAD(shrinker_list);
116static DECLARE_RWSEM(shrinker_rwsem);
117
118/*
119 * Add a shrinker callback to be called from the vm
120 */
121struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
122{
123 struct shrinker *shrinker;
124
125 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
126 if (shrinker) {
127 shrinker->shrinker = theshrinker;
128 shrinker->seeks = seeks;
129 shrinker->nr = 0;
130 down_write(&shrinker_rwsem);
131 list_add_tail(&shrinker->list, &shrinker_list);
132 up_write(&shrinker_rwsem);
133 }
134 return shrinker;
135}
136EXPORT_SYMBOL(set_shrinker);
137
138/*
139 * Remove one
140 */
141void remove_shrinker(struct shrinker *shrinker)
142{
143 down_write(&shrinker_rwsem);
144 list_del(&shrinker->list);
145 up_write(&shrinker_rwsem);
146 kfree(shrinker);
147}
148EXPORT_SYMBOL(remove_shrinker);
149
150#define SHRINK_BATCH 128
151/*
152 * Call the shrink functions to age shrinkable caches
153 *
154 * Here we assume it costs one seek to replace a lru page and that it also
155 * takes a seek to recreate a cache object. With this in mind we age equal
156 * percentages of the lru and ageable caches. This should balance the seeks
157 * generated by these structures.
158 *
159 * If the vm encounted mapped pages on the LRU it increase the pressure on
160 * slab to avoid swapping.
161 *
162 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
163 *
164 * `lru_pages' represents the number of on-LRU pages in all the zones which
165 * are eligible for the caller's allocation attempt. It is used for balancing
166 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700167 *
168 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Andrew Morton69e05942006-03-22 00:08:19 -0800170unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
171 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800174 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (scanned == 0)
177 scanned = SWAP_CLUSTER_MAX;
178
179 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700180 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 list_for_each_entry(shrinker, &shrinker_list, list) {
183 unsigned long long delta;
184 unsigned long total_scan;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800185 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800188 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 do_div(delta, lru_pages + 1);
190 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800191 if (shrinker->nr < 0) {
192 printk(KERN_ERR "%s: nr=%ld\n",
193 __FUNCTION__, shrinker->nr);
194 shrinker->nr = max_pass;
195 }
196
197 /*
198 * Avoid risking looping forever due to too large nr value:
199 * never try to free more than twice the estimate number of
200 * freeable entries.
201 */
202 if (shrinker->nr > max_pass * 2)
203 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 total_scan = shrinker->nr;
206 shrinker->nr = 0;
207
208 while (total_scan >= SHRINK_BATCH) {
209 long this_scan = SHRINK_BATCH;
210 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700211 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700213 nr_before = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
215 if (shrink_ret == -1)
216 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700217 if (shrink_ret < nr_before)
218 ret += nr_before - shrink_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 mod_page_state(slabs_scanned, this_scan);
220 total_scan -= this_scan;
221
222 cond_resched();
223 }
224
225 shrinker->nr += total_scan;
226 }
227 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700228 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231/* Called without lock on whether page is mapped, so answer is unstable */
232static inline int page_mapping_inuse(struct page *page)
233{
234 struct address_space *mapping;
235
236 /* Page is in somebody's page tables. */
237 if (page_mapped(page))
238 return 1;
239
240 /* Be more reluctant to reclaim swapcache than pagecache */
241 if (PageSwapCache(page))
242 return 1;
243
244 mapping = page_mapping(page);
245 if (!mapping)
246 return 0;
247
248 /* File is mmap'd by somebody? */
249 return mapping_mapped(mapping);
250}
251
252static inline int is_page_cache_freeable(struct page *page)
253{
254 return page_count(page) - !!PagePrivate(page) == 2;
255}
256
257static int may_write_to_queue(struct backing_dev_info *bdi)
258{
Christoph Lameter930d9152006-01-08 01:00:47 -0800259 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 1;
261 if (!bdi_write_congested(bdi))
262 return 1;
263 if (bdi == current->backing_dev_info)
264 return 1;
265 return 0;
266}
267
268/*
269 * We detected a synchronous write error writing a page out. Probably
270 * -ENOSPC. We need to propagate that into the address_space for a subsequent
271 * fsync(), msync() or close().
272 *
273 * The tricky part is that after writepage we cannot touch the mapping: nothing
274 * prevents it from being freed up. But we have a ref on the page and once
275 * that page is locked, the mapping is pinned.
276 *
277 * We're allowed to run sleeping lock_page() here because we know the caller has
278 * __GFP_FS.
279 */
280static void handle_write_error(struct address_space *mapping,
281 struct page *page, int error)
282{
283 lock_page(page);
284 if (page_mapping(page) == mapping) {
285 if (error == -ENOSPC)
286 set_bit(AS_ENOSPC, &mapping->flags);
287 else
288 set_bit(AS_EIO, &mapping->flags);
289 }
290 unlock_page(page);
291}
292
293/*
Andrew Morton1742f192006-03-22 00:08:21 -0800294 * pageout is called by shrink_page_list() for each dirty page.
295 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800297pageout_t pageout(struct page *page, struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 /*
300 * If the page is dirty, only perform writeback if that write
301 * will be non-blocking. To prevent this allocation from being
302 * stalled by pagecache activity. But note that there may be
303 * stalls if we need to run get_block(). We could test
304 * PagePrivate for that.
305 *
306 * If this process is currently in generic_file_write() against
307 * this page's queue, we can perform writeback even if that
308 * will block.
309 *
310 * If the page is swapcache, write it back even if that would
311 * block, for some throttling. This happens by accident, because
312 * swap_backing_dev_info is bust: it doesn't reflect the
313 * congestion state of the swapdevs. Easy to fix, if needed.
314 * See swapfile.c:page_queue_congested().
315 */
316 if (!is_page_cache_freeable(page))
317 return PAGE_KEEP;
318 if (!mapping) {
319 /*
320 * Some data journaling orphaned pages can have
321 * page->mapping == NULL while being dirty with clean buffers.
322 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700323 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (try_to_free_buffers(page)) {
325 ClearPageDirty(page);
326 printk("%s: orphaned page\n", __FUNCTION__);
327 return PAGE_CLEAN;
328 }
329 }
330 return PAGE_KEEP;
331 }
332 if (mapping->a_ops->writepage == NULL)
333 return PAGE_ACTIVATE;
334 if (!may_write_to_queue(mapping->backing_dev_info))
335 return PAGE_KEEP;
336
337 if (clear_page_dirty_for_io(page)) {
338 int res;
339 struct writeback_control wbc = {
340 .sync_mode = WB_SYNC_NONE,
341 .nr_to_write = SWAP_CLUSTER_MAX,
342 .nonblocking = 1,
343 .for_reclaim = 1,
344 };
345
346 SetPageReclaim(page);
347 res = mapping->a_ops->writepage(page, &wbc);
348 if (res < 0)
349 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800350 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ClearPageReclaim(page);
352 return PAGE_ACTIVATE;
353 }
354 if (!PageWriteback(page)) {
355 /* synchronous write or broken a_ops? */
356 ClearPageReclaim(page);
357 }
358
359 return PAGE_SUCCESS;
360 }
361
362 return PAGE_CLEAN;
363}
364
Christoph Lameterb20a3502006-03-22 00:09:12 -0800365int remove_mapping(struct address_space *mapping, struct page *page)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800366{
367 if (!mapping)
368 return 0; /* truncate got there first */
369
370 write_lock_irq(&mapping->tree_lock);
371
372 /*
373 * The non-racy check for busy page. It is critical to check
374 * PageDirty _after_ making sure that the page is freeable and
375 * not in use by anybody. (pagecache + us == 2)
376 */
377 if (unlikely(page_count(page) != 2))
378 goto cannot_free;
379 smp_rmb();
380 if (unlikely(PageDirty(page)))
381 goto cannot_free;
382
383 if (PageSwapCache(page)) {
384 swp_entry_t swap = { .val = page_private(page) };
385 __delete_from_swap_cache(page);
386 write_unlock_irq(&mapping->tree_lock);
387 swap_free(swap);
388 __put_page(page); /* The pagecache ref */
389 return 1;
390 }
391
392 __remove_from_page_cache(page);
393 write_unlock_irq(&mapping->tree_lock);
394 __put_page(page);
395 return 1;
396
397cannot_free:
398 write_unlock_irq(&mapping->tree_lock);
399 return 0;
400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402/*
Andrew Morton1742f192006-03-22 00:08:21 -0800403 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
Andrew Morton1742f192006-03-22 00:08:21 -0800405static unsigned long shrink_page_list(struct list_head *page_list,
406 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 LIST_HEAD(ret_pages);
409 struct pagevec freed_pvec;
410 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800411 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 cond_resched();
414
415 pagevec_init(&freed_pvec, 1);
416 while (!list_empty(page_list)) {
417 struct address_space *mapping;
418 struct page *page;
419 int may_enter_fs;
420 int referenced;
421
422 cond_resched();
423
424 page = lru_to_page(page_list);
425 list_del(&page->lru);
426
427 if (TestSetPageLocked(page))
428 goto keep;
429
430 BUG_ON(PageActive(page));
431
432 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800433
434 if (!sc->may_swap && page_mapped(page))
435 goto keep_locked;
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* Double the slab pressure for mapped and swapcache pages */
438 if (page_mapped(page) || PageSwapCache(page))
439 sc->nr_scanned++;
440
441 if (PageWriteback(page))
442 goto keep_locked;
443
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800444 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /* In active use or really unfreeable? Activate it. */
446 if (referenced && page_mapping_inuse(page))
447 goto activate_locked;
448
449#ifdef CONFIG_SWAP
450 /*
451 * Anonymous process memory has backing store?
452 * Try to allocate it some swap space here.
453 */
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800454 if (PageAnon(page) && !PageSwapCache(page))
Christoph Lameter1480a542006-01-08 01:00:53 -0800455 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#endif /* CONFIG_SWAP */
458
459 mapping = page_mapping(page);
460 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
461 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
462
463 /*
464 * The page is mapped into the page tables of one or more
465 * processes. Try to unmap it here.
466 */
467 if (page_mapped(page) && mapping) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800468 switch (try_to_unmap(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 case SWAP_FAIL:
470 goto activate_locked;
471 case SWAP_AGAIN:
472 goto keep_locked;
473 case SWAP_SUCCESS:
474 ; /* try to free the page below */
475 }
476 }
477
478 if (PageDirty(page)) {
479 if (referenced)
480 goto keep_locked;
481 if (!may_enter_fs)
482 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800483 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 goto keep_locked;
485
486 /* Page is dirty, try to write it out here */
487 switch(pageout(page, mapping)) {
488 case PAGE_KEEP:
489 goto keep_locked;
490 case PAGE_ACTIVATE:
491 goto activate_locked;
492 case PAGE_SUCCESS:
493 if (PageWriteback(page) || PageDirty(page))
494 goto keep;
495 /*
496 * A synchronous write - probably a ramdisk. Go
497 * ahead and try to reclaim the page.
498 */
499 if (TestSetPageLocked(page))
500 goto keep;
501 if (PageDirty(page) || PageWriteback(page))
502 goto keep_locked;
503 mapping = page_mapping(page);
504 case PAGE_CLEAN:
505 ; /* try to free the page below */
506 }
507 }
508
509 /*
510 * If the page has buffers, try to free the buffer mappings
511 * associated with this page. If we succeed we try to free
512 * the page as well.
513 *
514 * We do this even if the page is PageDirty().
515 * try_to_release_page() does not perform I/O, but it is
516 * possible for a page to have PageDirty set, but it is actually
517 * clean (all its buffers are clean). This happens if the
518 * buffers were written out directly, with submit_bh(). ext3
519 * will do this, as well as the blockdev mapping.
520 * try_to_release_page() will discover that cleanness and will
521 * drop the buffers and mark the page clean - it can be freed.
522 *
523 * Rarely, pages can have buffers and no ->mapping. These are
524 * the pages which were not successfully invalidated in
525 * truncate_complete_page(). We try to drop those buffers here
526 * and if that worked, and the page is no longer mapped into
527 * process address space (page_count == 1) it can be freed.
528 * Otherwise, leave the page on the LRU so it is swappable.
529 */
530 if (PagePrivate(page)) {
531 if (!try_to_release_page(page, sc->gfp_mask))
532 goto activate_locked;
533 if (!mapping && page_count(page) == 1)
534 goto free_it;
535 }
536
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800537 if (!remove_mapping(mapping, page))
538 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540free_it:
541 unlock_page(page);
Andrew Morton05ff5132006-03-22 00:08:20 -0800542 nr_reclaimed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (!pagevec_add(&freed_pvec, page))
544 __pagevec_release_nonlru(&freed_pvec);
545 continue;
546
547activate_locked:
548 SetPageActive(page);
549 pgactivate++;
550keep_locked:
551 unlock_page(page);
552keep:
553 list_add(&page->lru, &ret_pages);
554 BUG_ON(PageLRU(page));
555 }
556 list_splice(&ret_pages, page_list);
557 if (pagevec_count(&freed_pvec))
558 __pagevec_release_nonlru(&freed_pvec);
559 mod_page_state(pgactivate, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800560 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800563/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 * zone->lru_lock is heavily contended. Some of the functions that
565 * shrink the lists perform better by taking out a batch of pages
566 * and working on them outside the LRU lock.
567 *
568 * For pagecache intensive workloads, this function is the hottest
569 * spot in the kernel (apart from copy_*_user functions).
570 *
571 * Appropriate locks must be held before calling this function.
572 *
573 * @nr_to_scan: The number of pages to look through on the list.
574 * @src: The LRU list to pull pages off.
575 * @dst: The temp list to put pages on to.
576 * @scanned: The number of pages that were scanned.
577 *
578 * returns how many pages were moved onto *@dst.
579 */
Andrew Morton69e05942006-03-22 00:08:19 -0800580static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
581 struct list_head *src, struct list_head *dst,
582 unsigned long *scanned)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Andrew Morton69e05942006-03-22 00:08:19 -0800584 unsigned long nr_taken = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct page *page;
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800586 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800588 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800589 struct list_head *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 page = lru_to_page(src);
591 prefetchw_prev_lru_page(page, src, flags);
592
Nick Piggin8d438f92006-03-22 00:07:59 -0800593 BUG_ON(!PageLRU(page));
594
Nick Piggin053837f2006-01-18 17:42:27 -0800595 list_del(&page->lru);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800596 target = src;
597 if (likely(get_page_unless_zero(page))) {
Nick Piggin053837f2006-01-18 17:42:27 -0800598 /*
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800599 * Be careful not to clear PageLRU until after we're
600 * sure the page is not being freed elsewhere -- the
601 * page release code relies on it.
Nick Piggin053837f2006-01-18 17:42:27 -0800602 */
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800603 ClearPageLRU(page);
604 target = dst;
605 nr_taken++;
606 } /* else it is being freed elsewhere */
Nick Piggin46453a62006-03-22 00:07:58 -0800607
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800608 list_add(&page->lru, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
611 *scanned = scan;
612 return nr_taken;
613}
614
615/*
Andrew Morton1742f192006-03-22 00:08:21 -0800616 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
617 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Andrew Morton1742f192006-03-22 00:08:21 -0800619static unsigned long shrink_inactive_list(unsigned long max_scan,
620 struct zone *zone, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 LIST_HEAD(page_list);
623 struct pagevec pvec;
Andrew Morton69e05942006-03-22 00:08:19 -0800624 unsigned long nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800625 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 pagevec_init(&pvec, 1);
628
629 lru_add_drain();
630 spin_lock_irq(&zone->lru_lock);
Andrew Morton69e05942006-03-22 00:08:19 -0800631 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -0800633 unsigned long nr_taken;
634 unsigned long nr_scan;
635 unsigned long nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
638 &zone->inactive_list,
639 &page_list, &nr_scan);
640 zone->nr_inactive -= nr_taken;
641 zone->pages_scanned += nr_scan;
642 spin_unlock_irq(&zone->lru_lock);
643
Andrew Morton69e05942006-03-22 00:08:19 -0800644 nr_scanned += nr_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800645 nr_freed = shrink_page_list(&page_list, sc);
Andrew Morton05ff5132006-03-22 00:08:20 -0800646 nr_reclaimed += nr_freed;
Nick Piggina74609f2006-01-06 00:11:20 -0800647 local_irq_disable();
648 if (current_is_kswapd()) {
649 __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
650 __mod_page_state(kswapd_steal, nr_freed);
651 } else
652 __mod_page_state_zone(zone, pgscan_direct, nr_scan);
653 __mod_page_state_zone(zone, pgsteal, nr_freed);
654
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800655 if (nr_taken == 0)
656 goto done;
657
Nick Piggina74609f2006-01-06 00:11:20 -0800658 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /*
660 * Put back any unfreeable pages.
661 */
662 while (!list_empty(&page_list)) {
663 page = lru_to_page(&page_list);
Nick Piggin8d438f92006-03-22 00:07:59 -0800664 BUG_ON(PageLRU(page));
665 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 list_del(&page->lru);
667 if (PageActive(page))
668 add_page_to_active_list(zone, page);
669 else
670 add_page_to_inactive_list(zone, page);
671 if (!pagevec_add(&pvec, page)) {
672 spin_unlock_irq(&zone->lru_lock);
673 __pagevec_release(&pvec);
674 spin_lock_irq(&zone->lru_lock);
675 }
676 }
Andrew Morton69e05942006-03-22 00:08:19 -0800677 } while (nr_scanned < max_scan);
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800678 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679done:
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800680 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 pagevec_release(&pvec);
Andrew Morton05ff5132006-03-22 00:08:20 -0800682 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685/*
686 * This moves pages from the active list to the inactive list.
687 *
688 * We move them the other way if the page is referenced by one or more
689 * processes, from rmap.
690 *
691 * If the pages are mostly unmapped, the processing is fast and it is
692 * appropriate to hold zone->lru_lock across the whole operation. But if
693 * the pages are mapped, the processing is slow (page_referenced()) so we
694 * should drop zone->lru_lock around each page. It's impossible to balance
695 * this, so instead we remove the pages from the LRU while processing them.
696 * It is safe to rely on PG_active against the non-LRU pages in here because
697 * nobody will play with that bit on a non-LRU page.
698 *
699 * The downside is that we have to touch page->_count against each page.
700 * But we had to alter page->flags anyway.
701 */
Andrew Morton1742f192006-03-22 00:08:21 -0800702static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
703 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Andrew Morton69e05942006-03-22 00:08:19 -0800705 unsigned long pgmoved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 int pgdeactivate = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800707 unsigned long pgscanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 LIST_HEAD(l_hold); /* The pages which were snipped off */
709 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
710 LIST_HEAD(l_active); /* Pages to go onto the active_list */
711 struct page *page;
712 struct pagevec pvec;
713 int reclaim_mapped = 0;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800714
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800715 if (sc->may_swap) {
Christoph Lameter2903fb12006-02-11 17:55:55 -0800716 long mapped_ratio;
717 long distress;
718 long swap_tendency;
719
720 /*
721 * `distress' is a measure of how much trouble we're having
722 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
723 */
724 distress = 100 >> zone->prev_priority;
725
726 /*
727 * The point of this algorithm is to decide when to start
728 * reclaiming mapped memory instead of just pagecache. Work out
729 * 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
736 * mapped ratio is downgraded - just because there's a lot of
737 * mapped memory doesn't necessarily mean that page reclaim
738 * isn't succeeding.
739 *
740 * The distress ratio is important - we don't want to start
741 * going oom.
742 *
743 * A 100% value of vm_swappiness overrides this algorithm
744 * altogether.
745 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700746 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800747
748 /*
749 * Now use this metric to decide whether to start moving mapped
750 * memory onto the inactive list.
751 */
752 if (swap_tendency >= 100)
753 reclaim_mapped = 1;
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 lru_add_drain();
757 spin_lock_irq(&zone->lru_lock);
758 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
759 &l_hold, &pgscanned);
760 zone->pages_scanned += pgscanned;
761 zone->nr_active -= pgmoved;
762 spin_unlock_irq(&zone->lru_lock);
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 while (!list_empty(&l_hold)) {
765 cond_resched();
766 page = lru_to_page(&l_hold);
767 list_del(&page->lru);
768 if (page_mapped(page)) {
769 if (!reclaim_mapped ||
770 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800771 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 list_add(&page->lru, &l_active);
773 continue;
774 }
775 }
776 list_add(&page->lru, &l_inactive);
777 }
778
779 pagevec_init(&pvec, 1);
780 pgmoved = 0;
781 spin_lock_irq(&zone->lru_lock);
782 while (!list_empty(&l_inactive)) {
783 page = lru_to_page(&l_inactive);
784 prefetchw_prev_lru_page(page, &l_inactive, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -0800785 BUG_ON(PageLRU(page));
786 SetPageLRU(page);
Nick Piggin4c84cac2006-03-22 00:08:00 -0800787 BUG_ON(!PageActive(page));
788 ClearPageActive(page);
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 list_move(&page->lru, &zone->inactive_list);
791 pgmoved++;
792 if (!pagevec_add(&pvec, page)) {
793 zone->nr_inactive += pgmoved;
794 spin_unlock_irq(&zone->lru_lock);
795 pgdeactivate += pgmoved;
796 pgmoved = 0;
797 if (buffer_heads_over_limit)
798 pagevec_strip(&pvec);
799 __pagevec_release(&pvec);
800 spin_lock_irq(&zone->lru_lock);
801 }
802 }
803 zone->nr_inactive += pgmoved;
804 pgdeactivate += pgmoved;
805 if (buffer_heads_over_limit) {
806 spin_unlock_irq(&zone->lru_lock);
807 pagevec_strip(&pvec);
808 spin_lock_irq(&zone->lru_lock);
809 }
810
811 pgmoved = 0;
812 while (!list_empty(&l_active)) {
813 page = lru_to_page(&l_active);
814 prefetchw_prev_lru_page(page, &l_active, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -0800815 BUG_ON(PageLRU(page));
816 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 BUG_ON(!PageActive(page));
818 list_move(&page->lru, &zone->active_list);
819 pgmoved++;
820 if (!pagevec_add(&pvec, page)) {
821 zone->nr_active += pgmoved;
822 pgmoved = 0;
823 spin_unlock_irq(&zone->lru_lock);
824 __pagevec_release(&pvec);
825 spin_lock_irq(&zone->lru_lock);
826 }
827 }
828 zone->nr_active += pgmoved;
Nick Piggina74609f2006-01-06 00:11:20 -0800829 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Nick Piggina74609f2006-01-06 00:11:20 -0800831 __mod_page_state_zone(zone, pgrefill, pgscanned);
832 __mod_page_state(pgdeactivate, pgdeactivate);
833 local_irq_enable();
834
835 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838/*
839 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
840 */
Andrew Morton05ff5132006-03-22 00:08:20 -0800841static unsigned long shrink_zone(int priority, struct zone *zone,
842 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 unsigned long nr_active;
845 unsigned long nr_inactive;
Christoph Lameter86959492006-03-22 00:08:18 -0800846 unsigned long nr_to_scan;
Andrew Morton05ff5132006-03-22 00:08:20 -0800847 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Martin Hicks53e9a612005-09-03 15:54:51 -0700849 atomic_inc(&zone->reclaim_in_progress);
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /*
852 * Add one to `nr_to_scan' just to make sure that the kernel will
853 * slowly sift through the active list.
854 */
Christoph Lameter86959492006-03-22 00:08:18 -0800855 zone->nr_scan_active += (zone->nr_active >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 nr_active = zone->nr_scan_active;
857 if (nr_active >= sc->swap_cluster_max)
858 zone->nr_scan_active = 0;
859 else
860 nr_active = 0;
861
Christoph Lameter86959492006-03-22 00:08:18 -0800862 zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 nr_inactive = zone->nr_scan_inactive;
864 if (nr_inactive >= sc->swap_cluster_max)
865 zone->nr_scan_inactive = 0;
866 else
867 nr_inactive = 0;
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 while (nr_active || nr_inactive) {
870 if (nr_active) {
Christoph Lameter86959492006-03-22 00:08:18 -0800871 nr_to_scan = min(nr_active,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -0800873 nr_active -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800874 shrink_active_list(nr_to_scan, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876
877 if (nr_inactive) {
Christoph Lameter86959492006-03-22 00:08:18 -0800878 nr_to_scan = min(nr_inactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -0800880 nr_inactive -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800881 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
882 sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884 }
885
886 throttle_vm_writeout();
Martin Hicks53e9a612005-09-03 15:54:51 -0700887
888 atomic_dec(&zone->reclaim_in_progress);
Andrew Morton05ff5132006-03-22 00:08:20 -0800889 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892/*
893 * This is the direct reclaim path, for page-allocating processes. We only
894 * try to reclaim pages from zones which will satisfy the caller's allocation
895 * request.
896 *
897 * We reclaim from a zone even if that zone is over pages_high. Because:
898 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
899 * allocation or
900 * b) The zones may be over pages_high but they must go *over* pages_high to
901 * satisfy the `incremental min' zone defense algorithm.
902 *
903 * Returns the number of reclaimed pages.
904 *
905 * If a zone is deemed to be full of pinned pages then just give it a light
906 * scan then give up on it.
907 */
Andrew Morton1742f192006-03-22 00:08:21 -0800908static unsigned long shrink_zones(int priority, struct zone **zones,
Andrew Morton05ff5132006-03-22 00:08:20 -0800909 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Andrew Morton05ff5132006-03-22 00:08:20 -0800911 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 int i;
913
914 for (i = 0; zones[i] != NULL; i++) {
915 struct zone *zone = zones[i];
916
Con Kolivasf3fe6512006-01-06 00:11:15 -0800917 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 continue;
919
Paul Jackson9bf22292005-09-06 15:18:12 -0700920 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 continue;
922
Christoph Lameter86959492006-03-22 00:08:18 -0800923 zone->temp_priority = priority;
924 if (zone->prev_priority > priority)
925 zone->prev_priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Christoph Lameter86959492006-03-22 00:08:18 -0800927 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 continue; /* Let kswapd poll it */
929
Andrew Morton05ff5132006-03-22 00:08:20 -0800930 nr_reclaimed += shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Andrew Morton05ff5132006-03-22 00:08:20 -0800932 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
935/*
936 * This is the main entry point to direct page reclaim.
937 *
938 * If a full scan of the inactive list fails to free enough memory then we
939 * are "out of memory" and something needs to be killed.
940 *
941 * If the caller is !__GFP_FS then the probability of a failure is reasonably
942 * high - the zone may be full of dirty or under-writeback pages, which this
943 * caller can't do much about. We kick pdflush and take explicit naps in the
944 * hope that some of these pages can be written. But if the allocating task
945 * holds filesystem locks which prevent writeout this might not work, and the
946 * allocation attempt will fail.
947 */
Andrew Morton69e05942006-03-22 00:08:19 -0800948unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 int priority;
951 int ret = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800952 unsigned long total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800953 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 struct reclaim_state *reclaim_state = current->reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 unsigned long lru_pages = 0;
956 int i;
Andrew Morton179e9632006-03-22 00:08:18 -0800957 struct scan_control sc = {
958 .gfp_mask = gfp_mask,
959 .may_writepage = !laptop_mode,
960 .swap_cluster_max = SWAP_CLUSTER_MAX,
961 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700962 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -0800963 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 inc_page_state(allocstall);
966
967 for (i = 0; zones[i] != NULL; i++) {
968 struct zone *zone = zones[i];
969
Paul Jackson9bf22292005-09-06 15:18:12 -0700970 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 continue;
972
973 zone->temp_priority = DEF_PRIORITY;
974 lru_pages += zone->nr_active + zone->nr_inactive;
975 }
976
977 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
978 sc.nr_mapped = read_page_state(nr_mapped);
979 sc.nr_scanned = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800980 if (!priority)
981 disable_swap_token();
Andrew Morton1742f192006-03-22 00:08:21 -0800982 nr_reclaimed += shrink_zones(priority, zones, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
984 if (reclaim_state) {
Andrew Morton05ff5132006-03-22 00:08:20 -0800985 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 reclaim_state->reclaimed_slab = 0;
987 }
988 total_scanned += sc.nr_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -0800989 if (nr_reclaimed >= sc.swap_cluster_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 ret = 1;
991 goto out;
992 }
993
994 /*
995 * Try to write back as many pages as we just scanned. This
996 * tends to cause slow streaming writers to write data to the
997 * disk smoothly, at the dirtying rate, which is nice. But
998 * that's undesirable in laptop mode, where we *want* lumpy
999 * writeout. So in laptop mode, write out the whole world.
1000 */
Andrew Morton179e9632006-03-22 00:08:18 -08001001 if (total_scanned > sc.swap_cluster_max +
1002 sc.swap_cluster_max / 2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001003 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 sc.may_writepage = 1;
1005 }
1006
1007 /* Take a nap, wait for some writeback to complete */
1008 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1009 blk_congestion_wait(WRITE, HZ/10);
1010 }
1011out:
1012 for (i = 0; zones[i] != 0; i++) {
1013 struct zone *zone = zones[i];
1014
Paul Jackson9bf22292005-09-06 15:18:12 -07001015 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 continue;
1017
1018 zone->prev_priority = zone->temp_priority;
1019 }
1020 return ret;
1021}
1022
1023/*
1024 * For kswapd, balance_pgdat() will work across all this node's zones until
1025 * they are all at pages_high.
1026 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * Returns the number of pages which were actually freed.
1028 *
1029 * There is special handling here for zones which are full of pinned pages.
1030 * This can happen if the pages are all mlocked, or if they are all used by
1031 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1032 * What we do is to detect the case where all pages in the zone have been
1033 * scanned twice and there has been zero successful reclaim. Mark the zone as
1034 * dead and from now on, only perform a short scan. Basically we're polling
1035 * the zone for when the problem goes away.
1036 *
1037 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1038 * zones which have free_pages > pages_high, but once a zone is found to have
1039 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1040 * of the number of free pages in the lower zones. This interoperates with
1041 * the page allocator fallback scheme to ensure that aging of pages is balanced
1042 * across the zones.
1043 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001044static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 int all_zones_ok;
1047 int priority;
1048 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08001049 unsigned long total_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001050 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08001052 struct scan_control sc = {
1053 .gfp_mask = GFP_KERNEL,
1054 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001055 .swap_cluster_max = SWAP_CLUSTER_MAX,
1056 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -08001057 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059loop_again:
1060 total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001061 nr_reclaimed = 0;
Christoph Lameterc0bbbc72006-06-11 15:22:26 -07001062 sc.may_writepage = !laptop_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 sc.nr_mapped = read_page_state(nr_mapped);
1064
1065 inc_page_state(pageoutrun);
1066
1067 for (i = 0; i < pgdat->nr_zones; i++) {
1068 struct zone *zone = pgdat->node_zones + i;
1069
1070 zone->temp_priority = DEF_PRIORITY;
1071 }
1072
1073 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1074 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1075 unsigned long lru_pages = 0;
1076
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001077 /* The swap token gets in the way of swapout... */
1078 if (!priority)
1079 disable_swap_token();
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 all_zones_ok = 1;
1082
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001083 /*
1084 * Scan in the highmem->dma direction for the highest
1085 * zone which needs scanning
1086 */
1087 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1088 struct zone *zone = pgdat->node_zones + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001090 if (!populated_zone(zone))
1091 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001093 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1094 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001096 if (!zone_watermark_ok(zone, order, zone->pages_high,
1097 0, 0)) {
1098 end_zone = i;
1099 goto scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103scan:
1104 for (i = 0; i <= end_zone; i++) {
1105 struct zone *zone = pgdat->node_zones + i;
1106
1107 lru_pages += zone->nr_active + zone->nr_inactive;
1108 }
1109
1110 /*
1111 * Now scan the zone in the dma->highmem direction, stopping
1112 * at the last zone which needs scanning.
1113 *
1114 * We do this because the page allocator works in the opposite
1115 * direction. This prevents the page allocator from allocating
1116 * pages behind kswapd's direction of progress, which would
1117 * cause too much scanning of the lower zones.
1118 */
1119 for (i = 0; i <= end_zone; i++) {
1120 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001121 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Con Kolivasf3fe6512006-01-06 00:11:15 -08001123 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 continue;
1125
1126 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1127 continue;
1128
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001129 if (!zone_watermark_ok(zone, order, zone->pages_high,
1130 end_zone, 0))
1131 all_zones_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 zone->temp_priority = priority;
1133 if (zone->prev_priority > priority)
1134 zone->prev_priority = priority;
1135 sc.nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001136 nr_reclaimed += shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001138 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1139 lru_pages);
Andrew Morton05ff5132006-03-22 00:08:20 -08001140 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 total_scanned += sc.nr_scanned;
1142 if (zone->all_unreclaimable)
1143 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001144 if (nr_slab == 0 && zone->pages_scanned >=
1145 (zone->nr_active + zone->nr_inactive) * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 zone->all_unreclaimable = 1;
1147 /*
1148 * If we've done a decent amount of scanning and
1149 * the reclaim ratio is low, start doing writepage
1150 * even in laptop mode
1151 */
1152 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Andrew Morton05ff5132006-03-22 00:08:20 -08001153 total_scanned > nr_reclaimed + nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 sc.may_writepage = 1;
1155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (all_zones_ok)
1157 break; /* kswapd: all done */
1158 /*
1159 * OK, kswapd is getting into trouble. Take a nap, then take
1160 * another pass across the zones.
1161 */
1162 if (total_scanned && priority < DEF_PRIORITY - 2)
1163 blk_congestion_wait(WRITE, HZ/10);
1164
1165 /*
1166 * We do this so kswapd doesn't build up large priorities for
1167 * example when it is freeing in parallel with allocators. It
1168 * matches the direct reclaim path behaviour in terms of impact
1169 * on zone->*_priority.
1170 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001171 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 break;
1173 }
1174out:
1175 for (i = 0; i < pgdat->nr_zones; i++) {
1176 struct zone *zone = pgdat->node_zones + i;
1177
1178 zone->prev_priority = zone->temp_priority;
1179 }
1180 if (!all_zones_ok) {
1181 cond_resched();
1182 goto loop_again;
1183 }
1184
Andrew Morton05ff5132006-03-22 00:08:20 -08001185 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
1188/*
1189 * The background pageout daemon, started as a kernel thread
1190 * from the init process.
1191 *
1192 * This basically trickles out pages so that we have _some_
1193 * free memory available even if there is no other activity
1194 * that frees anything up. This is needed for things like routing
1195 * etc, where we otherwise might have all activity going on in
1196 * asynchronous contexts that cannot page things out.
1197 *
1198 * If there are applications that are active memory-allocators
1199 * (most normal use), this basically shouldn't matter.
1200 */
1201static int kswapd(void *p)
1202{
1203 unsigned long order;
1204 pg_data_t *pgdat = (pg_data_t*)p;
1205 struct task_struct *tsk = current;
1206 DEFINE_WAIT(wait);
1207 struct reclaim_state reclaim_state = {
1208 .reclaimed_slab = 0,
1209 };
1210 cpumask_t cpumask;
1211
1212 daemonize("kswapd%d", pgdat->node_id);
1213 cpumask = node_to_cpumask(pgdat->node_id);
1214 if (!cpus_empty(cpumask))
1215 set_cpus_allowed(tsk, cpumask);
1216 current->reclaim_state = &reclaim_state;
1217
1218 /*
1219 * Tell the memory management that we're a "memory allocator",
1220 * and that if we need more memory we should get access to it
1221 * regardless (see "__alloc_pages()"). "kswapd" should
1222 * never get caught in the normal page freeing logic.
1223 *
1224 * (Kswapd normally doesn't need memory anyway, but sometimes
1225 * you need a small amount of memory in order to be able to
1226 * page out something else, and this flag essentially protects
1227 * us from recursively trying to free more memory as we're
1228 * trying to free the first piece of memory in the first place).
1229 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001230 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 order = 0;
1233 for ( ; ; ) {
1234 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001235
1236 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1239 new_order = pgdat->kswapd_max_order;
1240 pgdat->kswapd_max_order = 0;
1241 if (order < new_order) {
1242 /*
1243 * Don't sleep if someone wants a larger 'order'
1244 * allocation
1245 */
1246 order = new_order;
1247 } else {
1248 schedule();
1249 order = pgdat->kswapd_max_order;
1250 }
1251 finish_wait(&pgdat->kswapd_wait, &wait);
1252
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001253 balance_pgdat(pgdat, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255 return 0;
1256}
1257
1258/*
1259 * A zone is low on free memory, so wake its kswapd task to service it.
1260 */
1261void wakeup_kswapd(struct zone *zone, int order)
1262{
1263 pg_data_t *pgdat;
1264
Con Kolivasf3fe6512006-01-06 00:11:15 -08001265 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return;
1267
1268 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001269 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return;
1271 if (pgdat->kswapd_max_order < order)
1272 pgdat->kswapd_max_order = order;
Paul Jackson9bf22292005-09-06 15:18:12 -07001273 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001275 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001277 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280#ifdef CONFIG_PM
1281/*
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001282 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1283 * from LRU lists system-wide, for given pass and priority, and returns the
1284 * number of reclaimed pages
1285 *
1286 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1287 */
1288static unsigned long shrink_all_zones(unsigned long nr_pages, int pass,
1289 int prio, struct scan_control *sc)
1290{
1291 struct zone *zone;
1292 unsigned long nr_to_scan, ret = 0;
1293
1294 for_each_zone(zone) {
1295
1296 if (!populated_zone(zone))
1297 continue;
1298
1299 if (zone->all_unreclaimable && prio != DEF_PRIORITY)
1300 continue;
1301
1302 /* For pass = 0 we don't shrink the active list */
1303 if (pass > 0) {
1304 zone->nr_scan_active += (zone->nr_active >> prio) + 1;
1305 if (zone->nr_scan_active >= nr_pages || pass > 3) {
1306 zone->nr_scan_active = 0;
1307 nr_to_scan = min(nr_pages, zone->nr_active);
1308 shrink_active_list(nr_to_scan, zone, sc);
1309 }
1310 }
1311
1312 zone->nr_scan_inactive += (zone->nr_inactive >> prio) + 1;
1313 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1314 zone->nr_scan_inactive = 0;
1315 nr_to_scan = min(nr_pages, zone->nr_inactive);
1316 ret += shrink_inactive_list(nr_to_scan, zone, sc);
1317 if (ret >= nr_pages)
1318 return ret;
1319 }
1320 }
1321
1322 return ret;
1323}
1324
1325/*
1326 * Try to free `nr_pages' of memory, system-wide, and return the number of
1327 * freed pages.
1328 *
1329 * Rather than trying to age LRUs the aim is to preserve the overall
1330 * LRU order by reclaiming preferentially
1331 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 */
Andrew Morton69e05942006-03-22 00:08:19 -08001333unsigned long shrink_all_memory(unsigned long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001335 unsigned long lru_pages, nr_slab;
Andrew Morton69e05942006-03-22 00:08:19 -08001336 unsigned long ret = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001337 int pass;
1338 struct reclaim_state reclaim_state;
1339 struct zone *zone;
1340 struct scan_control sc = {
1341 .gfp_mask = GFP_KERNEL,
1342 .may_swap = 0,
1343 .swap_cluster_max = nr_pages,
1344 .may_writepage = 1,
1345 .swappiness = vm_swappiness,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 };
1347
1348 current->reclaim_state = &reclaim_state;
Andrew Morton69e05942006-03-22 00:08:19 -08001349
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001350 lru_pages = 0;
1351 for_each_zone(zone)
1352 lru_pages += zone->nr_active + zone->nr_inactive;
1353
1354 nr_slab = read_page_state(nr_slab);
1355 /* If slab caches are huge, it's better to hit them first */
1356 while (nr_slab >= lru_pages) {
1357 reclaim_state.reclaimed_slab = 0;
1358 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1359 if (!reclaim_state.reclaimed_slab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 break;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001361
1362 ret += reclaim_state.reclaimed_slab;
1363 if (ret >= nr_pages)
1364 goto out;
1365
1366 nr_slab -= reclaim_state.reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001368
1369 /*
1370 * We try to shrink LRUs in 5 passes:
1371 * 0 = Reclaim from inactive_list only
1372 * 1 = Reclaim from active list but don't reclaim mapped
1373 * 2 = 2nd pass of type 1
1374 * 3 = Reclaim mapped (normal reclaim)
1375 * 4 = 2nd pass of type 3
1376 */
1377 for (pass = 0; pass < 5; pass++) {
1378 int prio;
1379
1380 /* Needed for shrinking slab caches later on */
1381 if (!lru_pages)
1382 for_each_zone(zone) {
1383 lru_pages += zone->nr_active;
1384 lru_pages += zone->nr_inactive;
1385 }
1386
1387 /* Force reclaiming mapped pages in the passes #3 and #4 */
1388 if (pass > 2) {
1389 sc.may_swap = 1;
1390 sc.swappiness = 100;
1391 }
1392
1393 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1394 unsigned long nr_to_scan = nr_pages - ret;
1395
1396 sc.nr_mapped = read_page_state(nr_mapped);
1397 sc.nr_scanned = 0;
1398
1399 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1400 if (ret >= nr_pages)
1401 goto out;
1402
1403 reclaim_state.reclaimed_slab = 0;
1404 shrink_slab(sc.nr_scanned, sc.gfp_mask, lru_pages);
1405 ret += reclaim_state.reclaimed_slab;
1406 if (ret >= nr_pages)
1407 goto out;
1408
1409 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
1410 blk_congestion_wait(WRITE, HZ / 10);
1411 }
1412
1413 lru_pages = 0;
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001414 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001415
1416 /*
1417 * If ret = 0, we could not shrink LRUs, but there may be something
1418 * in slab caches
1419 */
1420 if (!ret)
1421 do {
1422 reclaim_state.reclaimed_slab = 0;
1423 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1424 ret += reclaim_state.reclaimed_slab;
1425 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
1426
1427out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 current->reclaim_state = NULL;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return ret;
1431}
1432#endif
1433
1434#ifdef CONFIG_HOTPLUG_CPU
1435/* It's optimal to keep kswapds on the same CPUs as their memory, but
1436 not required for correctness. So if the last cpu in a node goes
1437 away, we get changed to run anywhere: as the first one comes back,
1438 restore their cpu bindings. */
Chandra Seetharaman83d722f2006-04-24 19:35:21 -07001439static int cpu_callback(struct notifier_block *nfb,
Andrew Morton69e05942006-03-22 00:08:19 -08001440 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 pg_data_t *pgdat;
1443 cpumask_t mask;
1444
1445 if (action == CPU_ONLINE) {
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08001446 for_each_online_pgdat(pgdat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 mask = node_to_cpumask(pgdat->node_id);
1448 if (any_online_cpu(mask) != NR_CPUS)
1449 /* One of our CPUs online: restore mask */
1450 set_cpus_allowed(pgdat->kswapd, mask);
1451 }
1452 }
1453 return NOTIFY_OK;
1454}
1455#endif /* CONFIG_HOTPLUG_CPU */
1456
1457static int __init kswapd_init(void)
1458{
1459 pg_data_t *pgdat;
Andrew Morton69e05942006-03-22 00:08:19 -08001460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 swap_setup();
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08001462 for_each_online_pgdat(pgdat) {
Andrew Morton69e05942006-03-22 00:08:19 -08001463 pid_t pid;
1464
1465 pid = kernel_thread(kswapd, pgdat, CLONE_KERNEL);
1466 BUG_ON(pid < 0);
Andrew Morton05eeae22006-03-25 03:07:48 -08001467 read_lock(&tasklist_lock);
Andrew Morton69e05942006-03-22 00:08:19 -08001468 pgdat->kswapd = find_task_by_pid(pid);
Andrew Morton05eeae22006-03-25 03:07:48 -08001469 read_unlock(&tasklist_lock);
Andrew Morton69e05942006-03-22 00:08:19 -08001470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 total_memory = nr_free_pagecache_pages();
1472 hotcpu_notifier(cpu_callback, 0);
1473 return 0;
1474}
1475
1476module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001477
1478#ifdef CONFIG_NUMA
1479/*
1480 * Zone reclaim mode
1481 *
1482 * If non-zero call zone_reclaim when the number of free pages falls below
1483 * the watermarks.
1484 *
1485 * In the future we may add flags to the mode. However, the page allocator
1486 * should only have to check that zone_reclaim_mode != 0 before calling
1487 * zone_reclaim().
1488 */
1489int zone_reclaim_mode __read_mostly;
1490
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001491#define RECLAIM_OFF 0
1492#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1493#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1494#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001495#define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001496
Christoph Lameter9eeff232006-01-18 17:42:31 -08001497/*
1498 * Mininum time between zone reclaim scans
1499 */
Christoph Lameter2a11ff02006-02-01 03:05:33 -08001500int zone_reclaim_interval __read_mostly = 30*HZ;
Christoph Lametera92f7122006-02-01 03:05:32 -08001501
1502/*
1503 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1504 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1505 * a zone.
1506 */
1507#define ZONE_RECLAIM_PRIORITY 4
1508
Christoph Lameter9eeff232006-01-18 17:42:31 -08001509/*
1510 * Try to free up some pages from this zone through reclaim.
1511 */
Andrew Morton179e9632006-03-22 00:08:18 -08001512static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001513{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001514 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08001515 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001516 struct task_struct *p = current;
1517 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08001518 int priority;
Andrew Morton05ff5132006-03-22 00:08:20 -08001519 unsigned long nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001520 struct scan_control sc = {
1521 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1522 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
1523 .nr_mapped = read_page_state(nr_mapped),
Andrew Morton69e05942006-03-22 00:08:19 -08001524 .swap_cluster_max = max_t(unsigned long, nr_pages,
1525 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08001526 .gfp_mask = gfp_mask,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001527 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -08001528 };
Christoph Lameter9eeff232006-01-18 17:42:31 -08001529
1530 disable_swap_token();
Christoph Lameter9eeff232006-01-18 17:42:31 -08001531 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08001532 /*
1533 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1534 * and we also need to be able to write out pages for RECLAIM_WRITE
1535 * and RECLAIM_SWAP.
1536 */
1537 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001538 reclaim_state.reclaimed_slab = 0;
1539 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001540
Christoph Lametera92f7122006-02-01 03:05:32 -08001541 /*
1542 * Free memory by calling shrink zone with increasing priorities
1543 * until we have enough memory freed.
1544 */
Christoph Lameter86959492006-03-22 00:08:18 -08001545 priority = ZONE_RECLAIM_PRIORITY;
Christoph Lametera92f7122006-02-01 03:05:32 -08001546 do {
Andrew Morton05ff5132006-03-22 00:08:20 -08001547 nr_reclaimed += shrink_zone(priority, zone, &sc);
Christoph Lameter86959492006-03-22 00:08:18 -08001548 priority--;
Andrew Morton05ff5132006-03-22 00:08:20 -08001549 } while (priority >= 0 && nr_reclaimed < nr_pages);
Christoph Lameterc84db232006-02-01 03:05:29 -08001550
Andrew Morton05ff5132006-03-22 00:08:20 -08001551 if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001552 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001553 * shrink_slab() does not currently allow us to determine how
1554 * many pages were freed in this zone. So we just shake the slab
1555 * a bit and then go off node for this particular allocation
1556 * despite possibly having freed enough memory to allocate in
1557 * this zone. If we freed local memory then the next
1558 * allocations will be local again.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001559 *
1560 * shrink_slab will free memory on all zones and may take
1561 * a long time.
1562 */
1563 shrink_slab(sc.nr_scanned, gfp_mask, order);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001564 }
1565
Christoph Lameter9eeff232006-01-18 17:42:31 -08001566 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08001567 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Christoph Lameter9eeff232006-01-18 17:42:31 -08001568
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001569 if (nr_reclaimed == 0) {
1570 /*
1571 * We were unable to reclaim enough pages to stay on node. We
1572 * now allow off node accesses for a certain time period before
1573 * trying again to reclaim pages from the local zone.
1574 */
Christoph Lameter9eeff232006-01-18 17:42:31 -08001575 zone->last_unsuccessful_zone_reclaim = jiffies;
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001576 }
Christoph Lameter9eeff232006-01-18 17:42:31 -08001577
Andrew Morton05ff5132006-03-22 00:08:20 -08001578 return nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001579}
Andrew Morton179e9632006-03-22 00:08:18 -08001580
1581int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1582{
1583 cpumask_t mask;
1584 int node_id;
1585
1586 /*
1587 * Do not reclaim if there was a recent unsuccessful attempt at zone
1588 * reclaim. In that case we let allocations go off node for the
1589 * zone_reclaim_interval. Otherwise we would scan for each off-node
1590 * page allocation.
1591 */
1592 if (time_before(jiffies,
1593 zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval))
1594 return 0;
1595
1596 /*
1597 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1598 * not have reclaimable pages and if we should not delay the allocation
1599 * then do not scan.
1600 */
1601 if (!(gfp_mask & __GFP_WAIT) ||
1602 zone->all_unreclaimable ||
1603 atomic_read(&zone->reclaim_in_progress) > 0 ||
1604 (current->flags & PF_MEMALLOC))
1605 return 0;
1606
1607 /*
1608 * Only run zone reclaim on the local zone or on zones that do not
1609 * have associated processors. This will favor the local processor
1610 * over remote processors and spread off node memory allocations
1611 * as wide as possible.
1612 */
1613 node_id = zone->zone_pgdat->node_id;
1614 mask = node_to_cpumask(node_id);
1615 if (!cpus_empty(mask) && node_id != numa_node_id())
1616 return 0;
1617 return __zone_reclaim(zone, gfp_mask, order);
1618}
Christoph Lameter9eeff232006-01-18 17:42:31 -08001619#endif