blob: fd572bbdc9f5a67064b0c37e8790671657721875 [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;
64};
65
66/*
67 * The list of shrinker callbacks used by to apply pressure to
68 * ageable caches.
69 */
70struct shrinker {
71 shrinker_t shrinker;
72 struct list_head list;
73 int seeks; /* seeks to recreate an obj */
74 long nr; /* objs pending delete */
75};
76
77#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
78
79#ifdef ARCH_HAS_PREFETCH
80#define prefetch_prev_lru_page(_page, _base, _field) \
81 do { \
82 if ((_page)->lru.prev != _base) { \
83 struct page *prev; \
84 \
85 prev = lru_to_page(&(_page->lru)); \
86 prefetch(&prev->_field); \
87 } \
88 } while (0)
89#else
90#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
91#endif
92
93#ifdef ARCH_HAS_PREFETCHW
94#define prefetchw_prev_lru_page(_page, _base, _field) \
95 do { \
96 if ((_page)->lru.prev != _base) { \
97 struct page *prev; \
98 \
99 prev = lru_to_page(&(_page->lru)); \
100 prefetchw(&prev->_field); \
101 } \
102 } while (0)
103#else
104#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
105#endif
106
107/*
108 * From 0 .. 100. Higher means more swappy.
109 */
110int vm_swappiness = 60;
111static long total_memory;
112
113static LIST_HEAD(shrinker_list);
114static DECLARE_RWSEM(shrinker_rwsem);
115
116/*
117 * Add a shrinker callback to be called from the vm
118 */
119struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
120{
121 struct shrinker *shrinker;
122
123 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
124 if (shrinker) {
125 shrinker->shrinker = theshrinker;
126 shrinker->seeks = seeks;
127 shrinker->nr = 0;
128 down_write(&shrinker_rwsem);
129 list_add_tail(&shrinker->list, &shrinker_list);
130 up_write(&shrinker_rwsem);
131 }
132 return shrinker;
133}
134EXPORT_SYMBOL(set_shrinker);
135
136/*
137 * Remove one
138 */
139void remove_shrinker(struct shrinker *shrinker)
140{
141 down_write(&shrinker_rwsem);
142 list_del(&shrinker->list);
143 up_write(&shrinker_rwsem);
144 kfree(shrinker);
145}
146EXPORT_SYMBOL(remove_shrinker);
147
148#define SHRINK_BATCH 128
149/*
150 * Call the shrink functions to age shrinkable caches
151 *
152 * Here we assume it costs one seek to replace a lru page and that it also
153 * takes a seek to recreate a cache object. With this in mind we age equal
154 * percentages of the lru and ageable caches. This should balance the seeks
155 * generated by these structures.
156 *
157 * If the vm encounted mapped pages on the LRU it increase the pressure on
158 * slab to avoid swapping.
159 *
160 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
161 *
162 * `lru_pages' represents the number of on-LRU pages in all the zones which
163 * are eligible for the caller's allocation attempt. It is used for balancing
164 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700165 *
166 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
Andrew Morton69e05942006-03-22 00:08:19 -0800168unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
169 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800172 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (scanned == 0)
175 scanned = SWAP_CLUSTER_MAX;
176
177 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700178 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 list_for_each_entry(shrinker, &shrinker_list, list) {
181 unsigned long long delta;
182 unsigned long total_scan;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800183 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800186 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 do_div(delta, lru_pages + 1);
188 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800189 if (shrinker->nr < 0) {
190 printk(KERN_ERR "%s: nr=%ld\n",
191 __FUNCTION__, shrinker->nr);
192 shrinker->nr = max_pass;
193 }
194
195 /*
196 * Avoid risking looping forever due to too large nr value:
197 * never try to free more than twice the estimate number of
198 * freeable entries.
199 */
200 if (shrinker->nr > max_pass * 2)
201 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 total_scan = shrinker->nr;
204 shrinker->nr = 0;
205
206 while (total_scan >= SHRINK_BATCH) {
207 long this_scan = SHRINK_BATCH;
208 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700209 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700211 nr_before = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
213 if (shrink_ret == -1)
214 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700215 if (shrink_ret < nr_before)
216 ret += nr_before - shrink_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 mod_page_state(slabs_scanned, this_scan);
218 total_scan -= this_scan;
219
220 cond_resched();
221 }
222
223 shrinker->nr += total_scan;
224 }
225 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700226 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229/* Called without lock on whether page is mapped, so answer is unstable */
230static inline int page_mapping_inuse(struct page *page)
231{
232 struct address_space *mapping;
233
234 /* Page is in somebody's page tables. */
235 if (page_mapped(page))
236 return 1;
237
238 /* Be more reluctant to reclaim swapcache than pagecache */
239 if (PageSwapCache(page))
240 return 1;
241
242 mapping = page_mapping(page);
243 if (!mapping)
244 return 0;
245
246 /* File is mmap'd by somebody? */
247 return mapping_mapped(mapping);
248}
249
250static inline int is_page_cache_freeable(struct page *page)
251{
252 return page_count(page) - !!PagePrivate(page) == 2;
253}
254
255static int may_write_to_queue(struct backing_dev_info *bdi)
256{
Christoph Lameter930d9152006-01-08 01:00:47 -0800257 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 1;
259 if (!bdi_write_congested(bdi))
260 return 1;
261 if (bdi == current->backing_dev_info)
262 return 1;
263 return 0;
264}
265
266/*
267 * We detected a synchronous write error writing a page out. Probably
268 * -ENOSPC. We need to propagate that into the address_space for a subsequent
269 * fsync(), msync() or close().
270 *
271 * The tricky part is that after writepage we cannot touch the mapping: nothing
272 * prevents it from being freed up. But we have a ref on the page and once
273 * that page is locked, the mapping is pinned.
274 *
275 * We're allowed to run sleeping lock_page() here because we know the caller has
276 * __GFP_FS.
277 */
278static void handle_write_error(struct address_space *mapping,
279 struct page *page, int error)
280{
281 lock_page(page);
282 if (page_mapping(page) == mapping) {
283 if (error == -ENOSPC)
284 set_bit(AS_ENOSPC, &mapping->flags);
285 else
286 set_bit(AS_EIO, &mapping->flags);
287 }
288 unlock_page(page);
289}
290
291/*
Andrew Morton1742f192006-03-22 00:08:21 -0800292 * pageout is called by shrink_page_list() for each dirty page.
293 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800295pageout_t pageout(struct page *page, struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 /*
298 * If the page is dirty, only perform writeback if that write
299 * will be non-blocking. To prevent this allocation from being
300 * stalled by pagecache activity. But note that there may be
301 * stalls if we need to run get_block(). We could test
302 * PagePrivate for that.
303 *
304 * If this process is currently in generic_file_write() against
305 * this page's queue, we can perform writeback even if that
306 * will block.
307 *
308 * If the page is swapcache, write it back even if that would
309 * block, for some throttling. This happens by accident, because
310 * swap_backing_dev_info is bust: it doesn't reflect the
311 * congestion state of the swapdevs. Easy to fix, if needed.
312 * See swapfile.c:page_queue_congested().
313 */
314 if (!is_page_cache_freeable(page))
315 return PAGE_KEEP;
316 if (!mapping) {
317 /*
318 * Some data journaling orphaned pages can have
319 * page->mapping == NULL while being dirty with clean buffers.
320 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700321 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (try_to_free_buffers(page)) {
323 ClearPageDirty(page);
324 printk("%s: orphaned page\n", __FUNCTION__);
325 return PAGE_CLEAN;
326 }
327 }
328 return PAGE_KEEP;
329 }
330 if (mapping->a_ops->writepage == NULL)
331 return PAGE_ACTIVATE;
332 if (!may_write_to_queue(mapping->backing_dev_info))
333 return PAGE_KEEP;
334
335 if (clear_page_dirty_for_io(page)) {
336 int res;
337 struct writeback_control wbc = {
338 .sync_mode = WB_SYNC_NONE,
339 .nr_to_write = SWAP_CLUSTER_MAX,
340 .nonblocking = 1,
341 .for_reclaim = 1,
342 };
343
344 SetPageReclaim(page);
345 res = mapping->a_ops->writepage(page, &wbc);
346 if (res < 0)
347 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800348 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 ClearPageReclaim(page);
350 return PAGE_ACTIVATE;
351 }
352 if (!PageWriteback(page)) {
353 /* synchronous write or broken a_ops? */
354 ClearPageReclaim(page);
355 }
356
357 return PAGE_SUCCESS;
358 }
359
360 return PAGE_CLEAN;
361}
362
Christoph Lameterb20a3502006-03-22 00:09:12 -0800363int remove_mapping(struct address_space *mapping, struct page *page)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800364{
365 if (!mapping)
366 return 0; /* truncate got there first */
367
368 write_lock_irq(&mapping->tree_lock);
369
370 /*
371 * The non-racy check for busy page. It is critical to check
372 * PageDirty _after_ making sure that the page is freeable and
373 * not in use by anybody. (pagecache + us == 2)
374 */
375 if (unlikely(page_count(page) != 2))
376 goto cannot_free;
377 smp_rmb();
378 if (unlikely(PageDirty(page)))
379 goto cannot_free;
380
381 if (PageSwapCache(page)) {
382 swp_entry_t swap = { .val = page_private(page) };
383 __delete_from_swap_cache(page);
384 write_unlock_irq(&mapping->tree_lock);
385 swap_free(swap);
386 __put_page(page); /* The pagecache ref */
387 return 1;
388 }
389
390 __remove_from_page_cache(page);
391 write_unlock_irq(&mapping->tree_lock);
392 __put_page(page);
393 return 1;
394
395cannot_free:
396 write_unlock_irq(&mapping->tree_lock);
397 return 0;
398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/*
Andrew Morton1742f192006-03-22 00:08:21 -0800401 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 */
Andrew Morton1742f192006-03-22 00:08:21 -0800403static unsigned long shrink_page_list(struct list_head *page_list,
404 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 LIST_HEAD(ret_pages);
407 struct pagevec freed_pvec;
408 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800409 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 cond_resched();
412
413 pagevec_init(&freed_pvec, 1);
414 while (!list_empty(page_list)) {
415 struct address_space *mapping;
416 struct page *page;
417 int may_enter_fs;
418 int referenced;
419
420 cond_resched();
421
422 page = lru_to_page(page_list);
423 list_del(&page->lru);
424
425 if (TestSetPageLocked(page))
426 goto keep;
427
428 BUG_ON(PageActive(page));
429
430 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800431
432 if (!sc->may_swap && page_mapped(page))
433 goto keep_locked;
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /* Double the slab pressure for mapped and swapcache pages */
436 if (page_mapped(page) || PageSwapCache(page))
437 sc->nr_scanned++;
438
439 if (PageWriteback(page))
440 goto keep_locked;
441
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800442 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* In active use or really unfreeable? Activate it. */
444 if (referenced && page_mapping_inuse(page))
445 goto activate_locked;
446
447#ifdef CONFIG_SWAP
448 /*
449 * Anonymous process memory has backing store?
450 * Try to allocate it some swap space here.
451 */
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800452 if (PageAnon(page) && !PageSwapCache(page))
Christoph Lameter1480a542006-01-08 01:00:53 -0800453 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455#endif /* CONFIG_SWAP */
456
457 mapping = page_mapping(page);
458 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
459 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
460
461 /*
462 * The page is mapped into the page tables of one or more
463 * processes. Try to unmap it here.
464 */
465 if (page_mapped(page) && mapping) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800466 switch (try_to_unmap(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 case SWAP_FAIL:
468 goto activate_locked;
469 case SWAP_AGAIN:
470 goto keep_locked;
471 case SWAP_SUCCESS:
472 ; /* try to free the page below */
473 }
474 }
475
476 if (PageDirty(page)) {
477 if (referenced)
478 goto keep_locked;
479 if (!may_enter_fs)
480 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800481 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto keep_locked;
483
484 /* Page is dirty, try to write it out here */
485 switch(pageout(page, mapping)) {
486 case PAGE_KEEP:
487 goto keep_locked;
488 case PAGE_ACTIVATE:
489 goto activate_locked;
490 case PAGE_SUCCESS:
491 if (PageWriteback(page) || PageDirty(page))
492 goto keep;
493 /*
494 * A synchronous write - probably a ramdisk. Go
495 * ahead and try to reclaim the page.
496 */
497 if (TestSetPageLocked(page))
498 goto keep;
499 if (PageDirty(page) || PageWriteback(page))
500 goto keep_locked;
501 mapping = page_mapping(page);
502 case PAGE_CLEAN:
503 ; /* try to free the page below */
504 }
505 }
506
507 /*
508 * If the page has buffers, try to free the buffer mappings
509 * associated with this page. If we succeed we try to free
510 * the page as well.
511 *
512 * We do this even if the page is PageDirty().
513 * try_to_release_page() does not perform I/O, but it is
514 * possible for a page to have PageDirty set, but it is actually
515 * clean (all its buffers are clean). This happens if the
516 * buffers were written out directly, with submit_bh(). ext3
517 * will do this, as well as the blockdev mapping.
518 * try_to_release_page() will discover that cleanness and will
519 * drop the buffers and mark the page clean - it can be freed.
520 *
521 * Rarely, pages can have buffers and no ->mapping. These are
522 * the pages which were not successfully invalidated in
523 * truncate_complete_page(). We try to drop those buffers here
524 * and if that worked, and the page is no longer mapped into
525 * process address space (page_count == 1) it can be freed.
526 * Otherwise, leave the page on the LRU so it is swappable.
527 */
528 if (PagePrivate(page)) {
529 if (!try_to_release_page(page, sc->gfp_mask))
530 goto activate_locked;
531 if (!mapping && page_count(page) == 1)
532 goto free_it;
533 }
534
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800535 if (!remove_mapping(mapping, page))
536 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538free_it:
539 unlock_page(page);
Andrew Morton05ff5132006-03-22 00:08:20 -0800540 nr_reclaimed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (!pagevec_add(&freed_pvec, page))
542 __pagevec_release_nonlru(&freed_pvec);
543 continue;
544
545activate_locked:
546 SetPageActive(page);
547 pgactivate++;
548keep_locked:
549 unlock_page(page);
550keep:
551 list_add(&page->lru, &ret_pages);
552 BUG_ON(PageLRU(page));
553 }
554 list_splice(&ret_pages, page_list);
555 if (pagevec_count(&freed_pvec))
556 __pagevec_release_nonlru(&freed_pvec);
557 mod_page_state(pgactivate, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800558 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800561/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 * zone->lru_lock is heavily contended. Some of the functions that
563 * shrink the lists perform better by taking out a batch of pages
564 * and working on them outside the LRU lock.
565 *
566 * For pagecache intensive workloads, this function is the hottest
567 * spot in the kernel (apart from copy_*_user functions).
568 *
569 * Appropriate locks must be held before calling this function.
570 *
571 * @nr_to_scan: The number of pages to look through on the list.
572 * @src: The LRU list to pull pages off.
573 * @dst: The temp list to put pages on to.
574 * @scanned: The number of pages that were scanned.
575 *
576 * returns how many pages were moved onto *@dst.
577 */
Andrew Morton69e05942006-03-22 00:08:19 -0800578static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
579 struct list_head *src, struct list_head *dst,
580 unsigned long *scanned)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Andrew Morton69e05942006-03-22 00:08:19 -0800582 unsigned long nr_taken = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct page *page;
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800584 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800586 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800587 struct list_head *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 page = lru_to_page(src);
589 prefetchw_prev_lru_page(page, src, flags);
590
Nick Piggin8d438f92006-03-22 00:07:59 -0800591 BUG_ON(!PageLRU(page));
592
Nick Piggin053837f2006-01-18 17:42:27 -0800593 list_del(&page->lru);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800594 target = src;
595 if (likely(get_page_unless_zero(page))) {
Nick Piggin053837f2006-01-18 17:42:27 -0800596 /*
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800597 * Be careful not to clear PageLRU until after we're
598 * sure the page is not being freed elsewhere -- the
599 * page release code relies on it.
Nick Piggin053837f2006-01-18 17:42:27 -0800600 */
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800601 ClearPageLRU(page);
602 target = dst;
603 nr_taken++;
604 } /* else it is being freed elsewhere */
Nick Piggin46453a62006-03-22 00:07:58 -0800605
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800606 list_add(&page->lru, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 *scanned = scan;
610 return nr_taken;
611}
612
613/*
Andrew Morton1742f192006-03-22 00:08:21 -0800614 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
615 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Andrew Morton1742f192006-03-22 00:08:21 -0800617static unsigned long shrink_inactive_list(unsigned long max_scan,
618 struct zone *zone, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 LIST_HEAD(page_list);
621 struct pagevec pvec;
Andrew Morton69e05942006-03-22 00:08:19 -0800622 unsigned long nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800623 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 pagevec_init(&pvec, 1);
626
627 lru_add_drain();
628 spin_lock_irq(&zone->lru_lock);
Andrew Morton69e05942006-03-22 00:08:19 -0800629 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -0800631 unsigned long nr_taken;
632 unsigned long nr_scan;
633 unsigned long nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
636 &zone->inactive_list,
637 &page_list, &nr_scan);
638 zone->nr_inactive -= nr_taken;
639 zone->pages_scanned += nr_scan;
640 spin_unlock_irq(&zone->lru_lock);
641
Andrew Morton69e05942006-03-22 00:08:19 -0800642 nr_scanned += nr_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800643 nr_freed = shrink_page_list(&page_list, sc);
Andrew Morton05ff5132006-03-22 00:08:20 -0800644 nr_reclaimed += nr_freed;
Nick Piggina74609f2006-01-06 00:11:20 -0800645 local_irq_disable();
646 if (current_is_kswapd()) {
647 __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
648 __mod_page_state(kswapd_steal, nr_freed);
649 } else
650 __mod_page_state_zone(zone, pgscan_direct, nr_scan);
651 __mod_page_state_zone(zone, pgsteal, nr_freed);
652
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800653 if (nr_taken == 0)
654 goto done;
655
Nick Piggina74609f2006-01-06 00:11:20 -0800656 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
658 * Put back any unfreeable pages.
659 */
660 while (!list_empty(&page_list)) {
661 page = lru_to_page(&page_list);
Nick Piggin8d438f92006-03-22 00:07:59 -0800662 BUG_ON(PageLRU(page));
663 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 list_del(&page->lru);
665 if (PageActive(page))
666 add_page_to_active_list(zone, page);
667 else
668 add_page_to_inactive_list(zone, page);
669 if (!pagevec_add(&pvec, page)) {
670 spin_unlock_irq(&zone->lru_lock);
671 __pagevec_release(&pvec);
672 spin_lock_irq(&zone->lru_lock);
673 }
674 }
Andrew Morton69e05942006-03-22 00:08:19 -0800675 } while (nr_scanned < max_scan);
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800676 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677done:
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800678 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 pagevec_release(&pvec);
Andrew Morton05ff5132006-03-22 00:08:20 -0800680 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683/*
684 * This moves pages from the active list to the inactive list.
685 *
686 * We move them the other way if the page is referenced by one or more
687 * processes, from rmap.
688 *
689 * If the pages are mostly unmapped, the processing is fast and it is
690 * appropriate to hold zone->lru_lock across the whole operation. But if
691 * the pages are mapped, the processing is slow (page_referenced()) so we
692 * should drop zone->lru_lock around each page. It's impossible to balance
693 * this, so instead we remove the pages from the LRU while processing them.
694 * It is safe to rely on PG_active against the non-LRU pages in here because
695 * nobody will play with that bit on a non-LRU page.
696 *
697 * The downside is that we have to touch page->_count against each page.
698 * But we had to alter page->flags anyway.
699 */
Andrew Morton1742f192006-03-22 00:08:21 -0800700static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
701 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Andrew Morton69e05942006-03-22 00:08:19 -0800703 unsigned long pgmoved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int pgdeactivate = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800705 unsigned long pgscanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 LIST_HEAD(l_hold); /* The pages which were snipped off */
707 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
708 LIST_HEAD(l_active); /* Pages to go onto the active_list */
709 struct page *page;
710 struct pagevec pvec;
711 int reclaim_mapped = 0;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800712
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800713 if (sc->may_swap) {
Christoph Lameter2903fb12006-02-11 17:55:55 -0800714 long mapped_ratio;
715 long distress;
716 long swap_tendency;
717
718 /*
719 * `distress' is a measure of how much trouble we're having
720 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
721 */
722 distress = 100 >> zone->prev_priority;
723
724 /*
725 * The point of this algorithm is to decide when to start
726 * reclaiming mapped memory instead of just pagecache. Work out
727 * how much memory
728 * is mapped.
729 */
730 mapped_ratio = (sc->nr_mapped * 100) / total_memory;
731
732 /*
733 * Now decide how much we really want to unmap some pages. The
734 * mapped ratio is downgraded - just because there's a lot of
735 * mapped memory doesn't necessarily mean that page reclaim
736 * isn't succeeding.
737 *
738 * The distress ratio is important - we don't want to start
739 * going oom.
740 *
741 * A 100% value of vm_swappiness overrides this algorithm
742 * altogether.
743 */
744 swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
745
746 /*
747 * Now use this metric to decide whether to start moving mapped
748 * memory onto the inactive list.
749 */
750 if (swap_tendency >= 100)
751 reclaim_mapped = 1;
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 lru_add_drain();
755 spin_lock_irq(&zone->lru_lock);
756 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
757 &l_hold, &pgscanned);
758 zone->pages_scanned += pgscanned;
759 zone->nr_active -= pgmoved;
760 spin_unlock_irq(&zone->lru_lock);
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 while (!list_empty(&l_hold)) {
763 cond_resched();
764 page = lru_to_page(&l_hold);
765 list_del(&page->lru);
766 if (page_mapped(page)) {
767 if (!reclaim_mapped ||
768 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800769 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 list_add(&page->lru, &l_active);
771 continue;
772 }
773 }
774 list_add(&page->lru, &l_inactive);
775 }
776
777 pagevec_init(&pvec, 1);
778 pgmoved = 0;
779 spin_lock_irq(&zone->lru_lock);
780 while (!list_empty(&l_inactive)) {
781 page = lru_to_page(&l_inactive);
782 prefetchw_prev_lru_page(page, &l_inactive, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -0800783 BUG_ON(PageLRU(page));
784 SetPageLRU(page);
Nick Piggin4c84cac2006-03-22 00:08:00 -0800785 BUG_ON(!PageActive(page));
786 ClearPageActive(page);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 list_move(&page->lru, &zone->inactive_list);
789 pgmoved++;
790 if (!pagevec_add(&pvec, page)) {
791 zone->nr_inactive += pgmoved;
792 spin_unlock_irq(&zone->lru_lock);
793 pgdeactivate += pgmoved;
794 pgmoved = 0;
795 if (buffer_heads_over_limit)
796 pagevec_strip(&pvec);
797 __pagevec_release(&pvec);
798 spin_lock_irq(&zone->lru_lock);
799 }
800 }
801 zone->nr_inactive += pgmoved;
802 pgdeactivate += pgmoved;
803 if (buffer_heads_over_limit) {
804 spin_unlock_irq(&zone->lru_lock);
805 pagevec_strip(&pvec);
806 spin_lock_irq(&zone->lru_lock);
807 }
808
809 pgmoved = 0;
810 while (!list_empty(&l_active)) {
811 page = lru_to_page(&l_active);
812 prefetchw_prev_lru_page(page, &l_active, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -0800813 BUG_ON(PageLRU(page));
814 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 BUG_ON(!PageActive(page));
816 list_move(&page->lru, &zone->active_list);
817 pgmoved++;
818 if (!pagevec_add(&pvec, page)) {
819 zone->nr_active += pgmoved;
820 pgmoved = 0;
821 spin_unlock_irq(&zone->lru_lock);
822 __pagevec_release(&pvec);
823 spin_lock_irq(&zone->lru_lock);
824 }
825 }
826 zone->nr_active += pgmoved;
Nick Piggina74609f2006-01-06 00:11:20 -0800827 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Nick Piggina74609f2006-01-06 00:11:20 -0800829 __mod_page_state_zone(zone, pgrefill, pgscanned);
830 __mod_page_state(pgdeactivate, pgdeactivate);
831 local_irq_enable();
832
833 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
836/*
837 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
838 */
Andrew Morton05ff5132006-03-22 00:08:20 -0800839static unsigned long shrink_zone(int priority, struct zone *zone,
840 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 unsigned long nr_active;
843 unsigned long nr_inactive;
Christoph Lameter86959492006-03-22 00:08:18 -0800844 unsigned long nr_to_scan;
Andrew Morton05ff5132006-03-22 00:08:20 -0800845 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Martin Hicks53e9a612005-09-03 15:54:51 -0700847 atomic_inc(&zone->reclaim_in_progress);
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /*
850 * Add one to `nr_to_scan' just to make sure that the kernel will
851 * slowly sift through the active list.
852 */
Christoph Lameter86959492006-03-22 00:08:18 -0800853 zone->nr_scan_active += (zone->nr_active >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 nr_active = zone->nr_scan_active;
855 if (nr_active >= sc->swap_cluster_max)
856 zone->nr_scan_active = 0;
857 else
858 nr_active = 0;
859
Christoph Lameter86959492006-03-22 00:08:18 -0800860 zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 nr_inactive = zone->nr_scan_inactive;
862 if (nr_inactive >= sc->swap_cluster_max)
863 zone->nr_scan_inactive = 0;
864 else
865 nr_inactive = 0;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 while (nr_active || nr_inactive) {
868 if (nr_active) {
Christoph Lameter86959492006-03-22 00:08:18 -0800869 nr_to_scan = min(nr_active,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -0800871 nr_active -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800872 shrink_active_list(nr_to_scan, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
875 if (nr_inactive) {
Christoph Lameter86959492006-03-22 00:08:18 -0800876 nr_to_scan = min(nr_inactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -0800878 nr_inactive -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800879 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
880 sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882 }
883
884 throttle_vm_writeout();
Martin Hicks53e9a612005-09-03 15:54:51 -0700885
886 atomic_dec(&zone->reclaim_in_progress);
Andrew Morton05ff5132006-03-22 00:08:20 -0800887 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890/*
891 * This is the direct reclaim path, for page-allocating processes. We only
892 * try to reclaim pages from zones which will satisfy the caller's allocation
893 * request.
894 *
895 * We reclaim from a zone even if that zone is over pages_high. Because:
896 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
897 * allocation or
898 * b) The zones may be over pages_high but they must go *over* pages_high to
899 * satisfy the `incremental min' zone defense algorithm.
900 *
901 * Returns the number of reclaimed pages.
902 *
903 * If a zone is deemed to be full of pinned pages then just give it a light
904 * scan then give up on it.
905 */
Andrew Morton1742f192006-03-22 00:08:21 -0800906static unsigned long shrink_zones(int priority, struct zone **zones,
Andrew Morton05ff5132006-03-22 00:08:20 -0800907 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Andrew Morton05ff5132006-03-22 00:08:20 -0800909 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int i;
911
912 for (i = 0; zones[i] != NULL; i++) {
913 struct zone *zone = zones[i];
914
Con Kolivasf3fe6512006-01-06 00:11:15 -0800915 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 continue;
917
Paul Jackson9bf22292005-09-06 15:18:12 -0700918 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 continue;
920
Christoph Lameter86959492006-03-22 00:08:18 -0800921 zone->temp_priority = priority;
922 if (zone->prev_priority > priority)
923 zone->prev_priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Christoph Lameter86959492006-03-22 00:08:18 -0800925 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 continue; /* Let kswapd poll it */
927
Andrew Morton05ff5132006-03-22 00:08:20 -0800928 nr_reclaimed += shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
Andrew Morton05ff5132006-03-22 00:08:20 -0800930 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933/*
934 * This is the main entry point to direct page reclaim.
935 *
936 * If a full scan of the inactive list fails to free enough memory then we
937 * are "out of memory" and something needs to be killed.
938 *
939 * If the caller is !__GFP_FS then the probability of a failure is reasonably
940 * high - the zone may be full of dirty or under-writeback pages, which this
941 * caller can't do much about. We kick pdflush and take explicit naps in the
942 * hope that some of these pages can be written. But if the allocating task
943 * holds filesystem locks which prevent writeout this might not work, and the
944 * allocation attempt will fail.
945 */
Andrew Morton69e05942006-03-22 00:08:19 -0800946unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 int priority;
949 int ret = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800950 unsigned long total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800951 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct reclaim_state *reclaim_state = current->reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 unsigned long lru_pages = 0;
954 int i;
Andrew Morton179e9632006-03-22 00:08:18 -0800955 struct scan_control sc = {
956 .gfp_mask = gfp_mask,
957 .may_writepage = !laptop_mode,
958 .swap_cluster_max = SWAP_CLUSTER_MAX,
959 .may_swap = 1,
960 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 inc_page_state(allocstall);
963
964 for (i = 0; zones[i] != NULL; i++) {
965 struct zone *zone = zones[i];
966
Paul Jackson9bf22292005-09-06 15:18:12 -0700967 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 continue;
969
970 zone->temp_priority = DEF_PRIORITY;
971 lru_pages += zone->nr_active + zone->nr_inactive;
972 }
973
974 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
975 sc.nr_mapped = read_page_state(nr_mapped);
976 sc.nr_scanned = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800977 if (!priority)
978 disable_swap_token();
Andrew Morton1742f192006-03-22 00:08:21 -0800979 nr_reclaimed += shrink_zones(priority, zones, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
981 if (reclaim_state) {
Andrew Morton05ff5132006-03-22 00:08:20 -0800982 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 reclaim_state->reclaimed_slab = 0;
984 }
985 total_scanned += sc.nr_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -0800986 if (nr_reclaimed >= sc.swap_cluster_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 ret = 1;
988 goto out;
989 }
990
991 /*
992 * Try to write back as many pages as we just scanned. This
993 * tends to cause slow streaming writers to write data to the
994 * disk smoothly, at the dirtying rate, which is nice. But
995 * that's undesirable in laptop mode, where we *want* lumpy
996 * writeout. So in laptop mode, write out the whole world.
997 */
Andrew Morton179e9632006-03-22 00:08:18 -0800998 if (total_scanned > sc.swap_cluster_max +
999 sc.swap_cluster_max / 2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001000 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 sc.may_writepage = 1;
1002 }
1003
1004 /* Take a nap, wait for some writeback to complete */
1005 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1006 blk_congestion_wait(WRITE, HZ/10);
1007 }
1008out:
1009 for (i = 0; zones[i] != 0; i++) {
1010 struct zone *zone = zones[i];
1011
Paul Jackson9bf22292005-09-06 15:18:12 -07001012 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 continue;
1014
1015 zone->prev_priority = zone->temp_priority;
1016 }
1017 return ret;
1018}
1019
1020/*
1021 * For kswapd, balance_pgdat() will work across all this node's zones until
1022 * they are all at pages_high.
1023 *
1024 * If `nr_pages' is non-zero then it is the number of pages which are to be
1025 * reclaimed, regardless of the zone occupancies. This is a software suspend
1026 * special.
1027 *
1028 * Returns the number of pages which were actually freed.
1029 *
1030 * There is special handling here for zones which are full of pinned pages.
1031 * This can happen if the pages are all mlocked, or if they are all used by
1032 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1033 * What we do is to detect the case where all pages in the zone have been
1034 * scanned twice and there has been zero successful reclaim. Mark the zone as
1035 * dead and from now on, only perform a short scan. Basically we're polling
1036 * the zone for when the problem goes away.
1037 *
1038 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1039 * zones which have free_pages > pages_high, but once a zone is found to have
1040 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1041 * of the number of free pages in the lower zones. This interoperates with
1042 * the page allocator fallback scheme to ensure that aging of pages is balanced
1043 * across the zones.
1044 */
Andrew Morton69e05942006-03-22 00:08:19 -08001045static unsigned long balance_pgdat(pg_data_t *pgdat, unsigned long nr_pages,
1046 int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Andrew Morton69e05942006-03-22 00:08:19 -08001048 unsigned long to_free = nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 int all_zones_ok;
1050 int priority;
1051 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08001052 unsigned long total_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001053 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08001055 struct scan_control sc = {
1056 .gfp_mask = GFP_KERNEL,
1057 .may_swap = 1,
1058 .swap_cluster_max = nr_pages ? nr_pages : SWAP_CLUSTER_MAX,
1059 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061loop_again:
1062 total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001063 nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001064 sc.may_writepage = !laptop_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 sc.nr_mapped = read_page_state(nr_mapped);
1066
1067 inc_page_state(pageoutrun);
1068
1069 for (i = 0; i < pgdat->nr_zones; i++) {
1070 struct zone *zone = pgdat->node_zones + i;
1071
1072 zone->temp_priority = DEF_PRIORITY;
1073 }
1074
1075 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1076 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1077 unsigned long lru_pages = 0;
1078
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001079 /* The swap token gets in the way of swapout... */
1080 if (!priority)
1081 disable_swap_token();
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 all_zones_ok = 1;
1084
1085 if (nr_pages == 0) {
1086 /*
1087 * Scan in the highmem->dma direction for the highest
1088 * zone which needs scanning
1089 */
1090 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1091 struct zone *zone = pgdat->node_zones + i;
1092
Con Kolivasf3fe6512006-01-06 00:11:15 -08001093 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 continue;
1095
1096 if (zone->all_unreclaimable &&
1097 priority != DEF_PRIORITY)
1098 continue;
1099
1100 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001101 zone->pages_high, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 end_zone = i;
1103 goto scan;
1104 }
1105 }
1106 goto out;
1107 } else {
1108 end_zone = pgdat->nr_zones - 1;
1109 }
1110scan:
1111 for (i = 0; i <= end_zone; i++) {
1112 struct zone *zone = pgdat->node_zones + i;
1113
1114 lru_pages += zone->nr_active + zone->nr_inactive;
1115 }
1116
1117 /*
1118 * Now scan the zone in the dma->highmem direction, stopping
1119 * at the last zone which needs scanning.
1120 *
1121 * We do this because the page allocator works in the opposite
1122 * direction. This prevents the page allocator from allocating
1123 * pages behind kswapd's direction of progress, which would
1124 * cause too much scanning of the lower zones.
1125 */
1126 for (i = 0; i <= end_zone; i++) {
1127 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001128 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Con Kolivasf3fe6512006-01-06 00:11:15 -08001130 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 continue;
1132
1133 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1134 continue;
1135
1136 if (nr_pages == 0) { /* Not software suspend */
1137 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001138 zone->pages_high, end_zone, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 all_zones_ok = 0;
1140 }
1141 zone->temp_priority = priority;
1142 if (zone->prev_priority > priority)
1143 zone->prev_priority = priority;
1144 sc.nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001145 nr_reclaimed += shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001147 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1148 lru_pages);
Andrew Morton05ff5132006-03-22 00:08:20 -08001149 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 total_scanned += sc.nr_scanned;
1151 if (zone->all_unreclaimable)
1152 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001153 if (nr_slab == 0 && zone->pages_scanned >=
1154 (zone->nr_active + zone->nr_inactive) * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 zone->all_unreclaimable = 1;
1156 /*
1157 * If we've done a decent amount of scanning and
1158 * the reclaim ratio is low, start doing writepage
1159 * even in laptop mode
1160 */
1161 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Andrew Morton05ff5132006-03-22 00:08:20 -08001162 total_scanned > nr_reclaimed + nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 sc.may_writepage = 1;
1164 }
Andrew Morton05ff5132006-03-22 00:08:20 -08001165 if (nr_pages && to_free > nr_reclaimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 continue; /* swsusp: need to do more work */
1167 if (all_zones_ok)
1168 break; /* kswapd: all done */
1169 /*
1170 * OK, kswapd is getting into trouble. Take a nap, then take
1171 * another pass across the zones.
1172 */
1173 if (total_scanned && priority < DEF_PRIORITY - 2)
1174 blk_congestion_wait(WRITE, HZ/10);
1175
1176 /*
1177 * We do this so kswapd doesn't build up large priorities for
1178 * example when it is freeing in parallel with allocators. It
1179 * matches the direct reclaim path behaviour in terms of impact
1180 * on zone->*_priority.
1181 */
Andrew Morton05ff5132006-03-22 00:08:20 -08001182 if ((nr_reclaimed >= SWAP_CLUSTER_MAX) && !nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 break;
1184 }
1185out:
1186 for (i = 0; i < pgdat->nr_zones; i++) {
1187 struct zone *zone = pgdat->node_zones + i;
1188
1189 zone->prev_priority = zone->temp_priority;
1190 }
1191 if (!all_zones_ok) {
1192 cond_resched();
1193 goto loop_again;
1194 }
1195
Andrew Morton05ff5132006-03-22 00:08:20 -08001196 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199/*
1200 * The background pageout daemon, started as a kernel thread
1201 * from the init process.
1202 *
1203 * This basically trickles out pages so that we have _some_
1204 * free memory available even if there is no other activity
1205 * that frees anything up. This is needed for things like routing
1206 * etc, where we otherwise might have all activity going on in
1207 * asynchronous contexts that cannot page things out.
1208 *
1209 * If there are applications that are active memory-allocators
1210 * (most normal use), this basically shouldn't matter.
1211 */
1212static int kswapd(void *p)
1213{
1214 unsigned long order;
1215 pg_data_t *pgdat = (pg_data_t*)p;
1216 struct task_struct *tsk = current;
1217 DEFINE_WAIT(wait);
1218 struct reclaim_state reclaim_state = {
1219 .reclaimed_slab = 0,
1220 };
1221 cpumask_t cpumask;
1222
1223 daemonize("kswapd%d", pgdat->node_id);
1224 cpumask = node_to_cpumask(pgdat->node_id);
1225 if (!cpus_empty(cpumask))
1226 set_cpus_allowed(tsk, cpumask);
1227 current->reclaim_state = &reclaim_state;
1228
1229 /*
1230 * Tell the memory management that we're a "memory allocator",
1231 * and that if we need more memory we should get access to it
1232 * regardless (see "__alloc_pages()"). "kswapd" should
1233 * never get caught in the normal page freeing logic.
1234 *
1235 * (Kswapd normally doesn't need memory anyway, but sometimes
1236 * you need a small amount of memory in order to be able to
1237 * page out something else, and this flag essentially protects
1238 * us from recursively trying to free more memory as we're
1239 * trying to free the first piece of memory in the first place).
1240 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001241 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 order = 0;
1244 for ( ; ; ) {
1245 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001246
1247 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1250 new_order = pgdat->kswapd_max_order;
1251 pgdat->kswapd_max_order = 0;
1252 if (order < new_order) {
1253 /*
1254 * Don't sleep if someone wants a larger 'order'
1255 * allocation
1256 */
1257 order = new_order;
1258 } else {
1259 schedule();
1260 order = pgdat->kswapd_max_order;
1261 }
1262 finish_wait(&pgdat->kswapd_wait, &wait);
1263
1264 balance_pgdat(pgdat, 0, order);
1265 }
1266 return 0;
1267}
1268
1269/*
1270 * A zone is low on free memory, so wake its kswapd task to service it.
1271 */
1272void wakeup_kswapd(struct zone *zone, int order)
1273{
1274 pg_data_t *pgdat;
1275
Con Kolivasf3fe6512006-01-06 00:11:15 -08001276 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return;
1278
1279 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001280 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return;
1282 if (pgdat->kswapd_max_order < order)
1283 pgdat->kswapd_max_order = order;
Paul Jackson9bf22292005-09-06 15:18:12 -07001284 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001286 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001288 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
1291#ifdef CONFIG_PM
1292/*
1293 * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
1294 * pages.
1295 */
Andrew Morton69e05942006-03-22 00:08:19 -08001296unsigned long shrink_all_memory(unsigned long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 pg_data_t *pgdat;
Andrew Morton69e05942006-03-22 00:08:19 -08001299 unsigned long nr_to_free = nr_pages;
1300 unsigned long ret = 0;
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001301 unsigned retry = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 struct reclaim_state reclaim_state = {
1303 .reclaimed_slab = 0,
1304 };
1305
1306 current->reclaim_state = &reclaim_state;
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001307repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 for_each_pgdat(pgdat) {
Andrew Morton69e05942006-03-22 00:08:19 -08001309 unsigned long freed;
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 freed = balance_pgdat(pgdat, nr_to_free, 0);
1312 ret += freed;
1313 nr_to_free -= freed;
Andrew Morton69e05942006-03-22 00:08:19 -08001314 if ((long)nr_to_free <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 break;
1316 }
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001317 if (retry-- && ret < nr_pages) {
1318 blk_congestion_wait(WRITE, HZ/5);
1319 goto repeat;
1320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 current->reclaim_state = NULL;
1322 return ret;
1323}
1324#endif
1325
1326#ifdef CONFIG_HOTPLUG_CPU
1327/* It's optimal to keep kswapds on the same CPUs as their memory, but
1328 not required for correctness. So if the last cpu in a node goes
1329 away, we get changed to run anywhere: as the first one comes back,
1330 restore their cpu bindings. */
1331static int __devinit cpu_callback(struct notifier_block *nfb,
Andrew Morton69e05942006-03-22 00:08:19 -08001332 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 pg_data_t *pgdat;
1335 cpumask_t mask;
1336
1337 if (action == CPU_ONLINE) {
1338 for_each_pgdat(pgdat) {
1339 mask = node_to_cpumask(pgdat->node_id);
1340 if (any_online_cpu(mask) != NR_CPUS)
1341 /* One of our CPUs online: restore mask */
1342 set_cpus_allowed(pgdat->kswapd, mask);
1343 }
1344 }
1345 return NOTIFY_OK;
1346}
1347#endif /* CONFIG_HOTPLUG_CPU */
1348
1349static int __init kswapd_init(void)
1350{
1351 pg_data_t *pgdat;
Andrew Morton69e05942006-03-22 00:08:19 -08001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 swap_setup();
Andrew Morton69e05942006-03-22 00:08:19 -08001354 for_each_pgdat(pgdat) {
1355 pid_t pid;
1356
1357 pid = kernel_thread(kswapd, pgdat, CLONE_KERNEL);
1358 BUG_ON(pid < 0);
1359 pgdat->kswapd = find_task_by_pid(pid);
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 total_memory = nr_free_pagecache_pages();
1362 hotcpu_notifier(cpu_callback, 0);
1363 return 0;
1364}
1365
1366module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001367
1368#ifdef CONFIG_NUMA
1369/*
1370 * Zone reclaim mode
1371 *
1372 * If non-zero call zone_reclaim when the number of free pages falls below
1373 * the watermarks.
1374 *
1375 * In the future we may add flags to the mode. However, the page allocator
1376 * should only have to check that zone_reclaim_mode != 0 before calling
1377 * zone_reclaim().
1378 */
1379int zone_reclaim_mode __read_mostly;
1380
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001381#define RECLAIM_OFF 0
1382#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1383#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1384#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001385#define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001386
Christoph Lameter9eeff232006-01-18 17:42:31 -08001387/*
1388 * Mininum time between zone reclaim scans
1389 */
Christoph Lameter2a11ff02006-02-01 03:05:33 -08001390int zone_reclaim_interval __read_mostly = 30*HZ;
Christoph Lametera92f7122006-02-01 03:05:32 -08001391
1392/*
1393 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1394 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1395 * a zone.
1396 */
1397#define ZONE_RECLAIM_PRIORITY 4
1398
Christoph Lameter9eeff232006-01-18 17:42:31 -08001399/*
1400 * Try to free up some pages from this zone through reclaim.
1401 */
Andrew Morton179e9632006-03-22 00:08:18 -08001402static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001403{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001404 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08001405 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001406 struct task_struct *p = current;
1407 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08001408 int priority;
Andrew Morton05ff5132006-03-22 00:08:20 -08001409 unsigned long nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001410 struct scan_control sc = {
1411 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1412 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
1413 .nr_mapped = read_page_state(nr_mapped),
Andrew Morton69e05942006-03-22 00:08:19 -08001414 .swap_cluster_max = max_t(unsigned long, nr_pages,
1415 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08001416 .gfp_mask = gfp_mask,
1417 };
Christoph Lameter9eeff232006-01-18 17:42:31 -08001418
1419 disable_swap_token();
Christoph Lameter9eeff232006-01-18 17:42:31 -08001420 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08001421 /*
1422 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1423 * and we also need to be able to write out pages for RECLAIM_WRITE
1424 * and RECLAIM_SWAP.
1425 */
1426 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001427 reclaim_state.reclaimed_slab = 0;
1428 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001429
Christoph Lametera92f7122006-02-01 03:05:32 -08001430 /*
1431 * Free memory by calling shrink zone with increasing priorities
1432 * until we have enough memory freed.
1433 */
Christoph Lameter86959492006-03-22 00:08:18 -08001434 priority = ZONE_RECLAIM_PRIORITY;
Christoph Lametera92f7122006-02-01 03:05:32 -08001435 do {
Andrew Morton05ff5132006-03-22 00:08:20 -08001436 nr_reclaimed += shrink_zone(priority, zone, &sc);
Christoph Lameter86959492006-03-22 00:08:18 -08001437 priority--;
Andrew Morton05ff5132006-03-22 00:08:20 -08001438 } while (priority >= 0 && nr_reclaimed < nr_pages);
Christoph Lameterc84db232006-02-01 03:05:29 -08001439
Andrew Morton05ff5132006-03-22 00:08:20 -08001440 if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001441 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001442 * shrink_slab() does not currently allow us to determine how
1443 * many pages were freed in this zone. So we just shake the slab
1444 * a bit and then go off node for this particular allocation
1445 * despite possibly having freed enough memory to allocate in
1446 * this zone. If we freed local memory then the next
1447 * allocations will be local again.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001448 *
1449 * shrink_slab will free memory on all zones and may take
1450 * a long time.
1451 */
1452 shrink_slab(sc.nr_scanned, gfp_mask, order);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001453 }
1454
Christoph Lameter9eeff232006-01-18 17:42:31 -08001455 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08001456 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Christoph Lameter9eeff232006-01-18 17:42:31 -08001457
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001458 if (nr_reclaimed == 0) {
1459 /*
1460 * We were unable to reclaim enough pages to stay on node. We
1461 * now allow off node accesses for a certain time period before
1462 * trying again to reclaim pages from the local zone.
1463 */
Christoph Lameter9eeff232006-01-18 17:42:31 -08001464 zone->last_unsuccessful_zone_reclaim = jiffies;
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001465 }
Christoph Lameter9eeff232006-01-18 17:42:31 -08001466
Andrew Morton05ff5132006-03-22 00:08:20 -08001467 return nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001468}
Andrew Morton179e9632006-03-22 00:08:18 -08001469
1470int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1471{
1472 cpumask_t mask;
1473 int node_id;
1474
1475 /*
1476 * Do not reclaim if there was a recent unsuccessful attempt at zone
1477 * reclaim. In that case we let allocations go off node for the
1478 * zone_reclaim_interval. Otherwise we would scan for each off-node
1479 * page allocation.
1480 */
1481 if (time_before(jiffies,
1482 zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval))
1483 return 0;
1484
1485 /*
1486 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1487 * not have reclaimable pages and if we should not delay the allocation
1488 * then do not scan.
1489 */
1490 if (!(gfp_mask & __GFP_WAIT) ||
1491 zone->all_unreclaimable ||
1492 atomic_read(&zone->reclaim_in_progress) > 0 ||
1493 (current->flags & PF_MEMALLOC))
1494 return 0;
1495
1496 /*
1497 * Only run zone reclaim on the local zone or on zones that do not
1498 * have associated processors. This will favor the local processor
1499 * over remote processors and spread off node memory allocations
1500 * as wide as possible.
1501 */
1502 node_id = zone->zone_pgdat->node_id;
1503 mask = node_to_cpumask(node_id);
1504 if (!cpus_empty(mask) && node_id != numa_node_id())
1505 return 0;
1506 return __zone_reclaim(zone, gfp_mask, order);
1507}
Christoph Lameter9eeff232006-01-18 17:42:31 -08001508#endif