blob: 87d340999ce8a5d8ada08487627769bb0574381f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/vmscan.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 *
6 * Swap reorganised 29.12.95, Stephen Tweedie.
7 * kswapd added: 7.1.96 sct
8 * Removed kswapd_ctl limits, and swap out as many pages as needed
9 * to bring the system back to freepages.high: 2.4.97, Rik van Riel.
10 * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
11 * Multiqueue VM started 5.8.00, Rik van Riel.
12 */
13
14#include <linux/mm.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/kernel_stat.h>
18#include <linux/swap.h>
19#include <linux/pagemap.h>
20#include <linux/init.h>
21#include <linux/highmem.h>
Andrew Mortone129b5c2006-09-27 01:50:00 -070022#include <linux/vmstat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/file.h>
24#include <linux/writeback.h>
25#include <linux/blkdev.h>
26#include <linux/buffer_head.h> /* for try_to_release_page(),
27 buffer_heads_over_limit */
28#include <linux/mm_inline.h>
29#include <linux/pagevec.h>
30#include <linux/backing-dev.h>
31#include <linux/rmap.h>
32#include <linux/topology.h>
33#include <linux/cpu.h>
34#include <linux/cpuset.h>
35#include <linux/notifier.h>
36#include <linux/rwsem.h>
Rafael J. Wysocki248a0302006-03-22 00:09:04 -080037#include <linux/delay.h>
Yasunori Goto3218ae12006-06-27 02:53:33 -070038#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/tlbflush.h>
41#include <asm/div64.h>
42
43#include <linux/swapops.h>
44
Nick Piggin0f8053a2006-03-22 00:08:33 -080045#include "internal.h"
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct scan_control {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 /* Incremented by the number of inactive pages that were scanned */
49 unsigned long nr_scanned;
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;
Nick Piggin408d8542006-09-25 23:31:27 -070066
67 int all_unreclaimable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
70/*
71 * The list of shrinker callbacks used by to apply pressure to
72 * ageable caches.
73 */
74struct shrinker {
75 shrinker_t shrinker;
76 struct list_head list;
77 int seeks; /* seeks to recreate an obj */
78 long nr; /* objs pending delete */
79};
80
81#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
82
83#ifdef ARCH_HAS_PREFETCH
84#define prefetch_prev_lru_page(_page, _base, _field) \
85 do { \
86 if ((_page)->lru.prev != _base) { \
87 struct page *prev; \
88 \
89 prev = lru_to_page(&(_page->lru)); \
90 prefetch(&prev->_field); \
91 } \
92 } while (0)
93#else
94#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
95#endif
96
97#ifdef ARCH_HAS_PREFETCHW
98#define prefetchw_prev_lru_page(_page, _base, _field) \
99 do { \
100 if ((_page)->lru.prev != _base) { \
101 struct page *prev; \
102 \
103 prev = lru_to_page(&(_page->lru)); \
104 prefetchw(&prev->_field); \
105 } \
106 } while (0)
107#else
108#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
109#endif
110
111/*
112 * From 0 .. 100. Higher means more swappy.
113 */
114int vm_swappiness = 60;
Andrew Mortonbd1e22b2006-06-23 02:03:47 -0700115long vm_total_pages; /* The total number of pages which the VM controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117static LIST_HEAD(shrinker_list);
118static DECLARE_RWSEM(shrinker_rwsem);
119
120/*
121 * Add a shrinker callback to be called from the vm
122 */
123struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
124{
125 struct shrinker *shrinker;
126
127 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
128 if (shrinker) {
129 shrinker->shrinker = theshrinker;
130 shrinker->seeks = seeks;
131 shrinker->nr = 0;
132 down_write(&shrinker_rwsem);
133 list_add_tail(&shrinker->list, &shrinker_list);
134 up_write(&shrinker_rwsem);
135 }
136 return shrinker;
137}
138EXPORT_SYMBOL(set_shrinker);
139
140/*
141 * Remove one
142 */
143void remove_shrinker(struct shrinker *shrinker)
144{
145 down_write(&shrinker_rwsem);
146 list_del(&shrinker->list);
147 up_write(&shrinker_rwsem);
148 kfree(shrinker);
149}
150EXPORT_SYMBOL(remove_shrinker);
151
152#define SHRINK_BATCH 128
153/*
154 * Call the shrink functions to age shrinkable caches
155 *
156 * Here we assume it costs one seek to replace a lru page and that it also
157 * takes a seek to recreate a cache object. With this in mind we age equal
158 * percentages of the lru and ageable caches. This should balance the seeks
159 * generated by these structures.
160 *
161 * If the vm encounted mapped pages on the LRU it increase the pressure on
162 * slab to avoid swapping.
163 *
164 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
165 *
166 * `lru_pages' represents the number of on-LRU pages in all the zones which
167 * are eligible for the caller's allocation attempt. It is used for balancing
168 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700169 *
170 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
Andrew Morton69e05942006-03-22 00:08:19 -0800172unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
173 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800176 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 if (scanned == 0)
179 scanned = SWAP_CLUSTER_MAX;
180
181 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700182 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 list_for_each_entry(shrinker, &shrinker_list, list) {
185 unsigned long long delta;
186 unsigned long total_scan;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800187 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800190 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 do_div(delta, lru_pages + 1);
192 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800193 if (shrinker->nr < 0) {
194 printk(KERN_ERR "%s: nr=%ld\n",
195 __FUNCTION__, shrinker->nr);
196 shrinker->nr = max_pass;
197 }
198
199 /*
200 * Avoid risking looping forever due to too large nr value:
201 * never try to free more than twice the estimate number of
202 * freeable entries.
203 */
204 if (shrinker->nr > max_pass * 2)
205 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 total_scan = shrinker->nr;
208 shrinker->nr = 0;
209
210 while (total_scan >= SHRINK_BATCH) {
211 long this_scan = SHRINK_BATCH;
212 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700213 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700215 nr_before = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
217 if (shrink_ret == -1)
218 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700219 if (shrink_ret < nr_before)
220 ret += nr_before - shrink_ret;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700221 count_vm_events(SLABS_SCANNED, this_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 total_scan -= this_scan;
223
224 cond_resched();
225 }
226
227 shrinker->nr += total_scan;
228 }
229 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700230 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233/* Called without lock on whether page is mapped, so answer is unstable */
234static inline int page_mapping_inuse(struct page *page)
235{
236 struct address_space *mapping;
237
238 /* Page is in somebody's page tables. */
239 if (page_mapped(page))
240 return 1;
241
242 /* Be more reluctant to reclaim swapcache than pagecache */
243 if (PageSwapCache(page))
244 return 1;
245
246 mapping = page_mapping(page);
247 if (!mapping)
248 return 0;
249
250 /* File is mmap'd by somebody? */
251 return mapping_mapped(mapping);
252}
253
254static inline int is_page_cache_freeable(struct page *page)
255{
256 return page_count(page) - !!PagePrivate(page) == 2;
257}
258
259static int may_write_to_queue(struct backing_dev_info *bdi)
260{
Christoph Lameter930d9152006-01-08 01:00:47 -0800261 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 1;
263 if (!bdi_write_congested(bdi))
264 return 1;
265 if (bdi == current->backing_dev_info)
266 return 1;
267 return 0;
268}
269
270/*
271 * We detected a synchronous write error writing a page out. Probably
272 * -ENOSPC. We need to propagate that into the address_space for a subsequent
273 * fsync(), msync() or close().
274 *
275 * The tricky part is that after writepage we cannot touch the mapping: nothing
276 * prevents it from being freed up. But we have a ref on the page and once
277 * that page is locked, the mapping is pinned.
278 *
279 * We're allowed to run sleeping lock_page() here because we know the caller has
280 * __GFP_FS.
281 */
282static void handle_write_error(struct address_space *mapping,
283 struct page *page, int error)
284{
285 lock_page(page);
286 if (page_mapping(page) == mapping) {
287 if (error == -ENOSPC)
288 set_bit(AS_ENOSPC, &mapping->flags);
289 else
290 set_bit(AS_EIO, &mapping->flags);
291 }
292 unlock_page(page);
293}
294
Christoph Lameter04e62a22006-06-23 02:03:38 -0700295/* possible outcome of pageout() */
296typedef enum {
297 /* failed to write page out, page is locked */
298 PAGE_KEEP,
299 /* move page to the active list, page is locked */
300 PAGE_ACTIVATE,
301 /* page has been sent to the disk successfully, page is unlocked */
302 PAGE_SUCCESS,
303 /* page is clean and locked */
304 PAGE_CLEAN,
305} pageout_t;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/*
Andrew Morton1742f192006-03-22 00:08:21 -0800308 * pageout is called by shrink_page_list() for each dirty page.
309 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Christoph Lameter04e62a22006-06-23 02:03:38 -0700311static pageout_t pageout(struct page *page, struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 /*
314 * If the page is dirty, only perform writeback if that write
315 * will be non-blocking. To prevent this allocation from being
316 * stalled by pagecache activity. But note that there may be
317 * stalls if we need to run get_block(). We could test
318 * PagePrivate for that.
319 *
320 * If this process is currently in generic_file_write() against
321 * this page's queue, we can perform writeback even if that
322 * will block.
323 *
324 * If the page is swapcache, write it back even if that would
325 * block, for some throttling. This happens by accident, because
326 * swap_backing_dev_info is bust: it doesn't reflect the
327 * congestion state of the swapdevs. Easy to fix, if needed.
328 * See swapfile.c:page_queue_congested().
329 */
330 if (!is_page_cache_freeable(page))
331 return PAGE_KEEP;
332 if (!mapping) {
333 /*
334 * Some data journaling orphaned pages can have
335 * page->mapping == NULL while being dirty with clean buffers.
336 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700337 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (try_to_free_buffers(page)) {
339 ClearPageDirty(page);
340 printk("%s: orphaned page\n", __FUNCTION__);
341 return PAGE_CLEAN;
342 }
343 }
344 return PAGE_KEEP;
345 }
346 if (mapping->a_ops->writepage == NULL)
347 return PAGE_ACTIVATE;
348 if (!may_write_to_queue(mapping->backing_dev_info))
349 return PAGE_KEEP;
350
351 if (clear_page_dirty_for_io(page)) {
352 int res;
353 struct writeback_control wbc = {
354 .sync_mode = WB_SYNC_NONE,
355 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700356 .range_start = 0,
357 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 .nonblocking = 1,
359 .for_reclaim = 1,
360 };
361
362 SetPageReclaim(page);
363 res = mapping->a_ops->writepage(page, &wbc);
364 if (res < 0)
365 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800366 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 ClearPageReclaim(page);
368 return PAGE_ACTIVATE;
369 }
370 if (!PageWriteback(page)) {
371 /* synchronous write or broken a_ops? */
372 ClearPageReclaim(page);
373 }
Andrew Mortone129b5c2006-09-27 01:50:00 -0700374 inc_zone_page_state(page, NR_VMSCAN_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return PAGE_SUCCESS;
376 }
377
378 return PAGE_CLEAN;
379}
380
Christoph Lameterb20a3502006-03-22 00:09:12 -0800381int remove_mapping(struct address_space *mapping, struct page *page)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800382{
Nick Piggin28e4d962006-09-25 23:31:23 -0700383 BUG_ON(!PageLocked(page));
384 BUG_ON(mapping != page_mapping(page));
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800385
386 write_lock_irq(&mapping->tree_lock);
387
388 /*
389 * The non-racy check for busy page. It is critical to check
390 * PageDirty _after_ making sure that the page is freeable and
391 * not in use by anybody. (pagecache + us == 2)
392 */
393 if (unlikely(page_count(page) != 2))
394 goto cannot_free;
395 smp_rmb();
396 if (unlikely(PageDirty(page)))
397 goto cannot_free;
398
399 if (PageSwapCache(page)) {
400 swp_entry_t swap = { .val = page_private(page) };
401 __delete_from_swap_cache(page);
402 write_unlock_irq(&mapping->tree_lock);
403 swap_free(swap);
404 __put_page(page); /* The pagecache ref */
405 return 1;
406 }
407
408 __remove_from_page_cache(page);
409 write_unlock_irq(&mapping->tree_lock);
410 __put_page(page);
411 return 1;
412
413cannot_free:
414 write_unlock_irq(&mapping->tree_lock);
415 return 0;
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/*
Andrew Morton1742f192006-03-22 00:08:21 -0800419 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Andrew Morton1742f192006-03-22 00:08:21 -0800421static unsigned long shrink_page_list(struct list_head *page_list,
422 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 LIST_HEAD(ret_pages);
425 struct pagevec freed_pvec;
426 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800427 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 cond_resched();
430
431 pagevec_init(&freed_pvec, 1);
432 while (!list_empty(page_list)) {
433 struct address_space *mapping;
434 struct page *page;
435 int may_enter_fs;
436 int referenced;
437
438 cond_resched();
439
440 page = lru_to_page(page_list);
441 list_del(&page->lru);
442
443 if (TestSetPageLocked(page))
444 goto keep;
445
Nick Piggin725d7042006-09-25 23:30:55 -0700446 VM_BUG_ON(PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800449
450 if (!sc->may_swap && page_mapped(page))
451 goto keep_locked;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* Double the slab pressure for mapped and swapcache pages */
454 if (page_mapped(page) || PageSwapCache(page))
455 sc->nr_scanned++;
456
457 if (PageWriteback(page))
458 goto keep_locked;
459
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800460 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* In active use or really unfreeable? Activate it. */
462 if (referenced && page_mapping_inuse(page))
463 goto activate_locked;
464
465#ifdef CONFIG_SWAP
466 /*
467 * Anonymous process memory has backing store?
468 * Try to allocate it some swap space here.
469 */
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800470 if (PageAnon(page) && !PageSwapCache(page))
Christoph Lameter1480a542006-01-08 01:00:53 -0800471 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473#endif /* CONFIG_SWAP */
474
475 mapping = page_mapping(page);
476 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
477 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
478
479 /*
480 * The page is mapped into the page tables of one or more
481 * processes. Try to unmap it here.
482 */
483 if (page_mapped(page) && mapping) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800484 switch (try_to_unmap(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 case SWAP_FAIL:
486 goto activate_locked;
487 case SWAP_AGAIN:
488 goto keep_locked;
489 case SWAP_SUCCESS:
490 ; /* try to free the page below */
491 }
492 }
493
494 if (PageDirty(page)) {
495 if (referenced)
496 goto keep_locked;
497 if (!may_enter_fs)
498 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800499 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 goto keep_locked;
501
502 /* Page is dirty, try to write it out here */
503 switch(pageout(page, mapping)) {
504 case PAGE_KEEP:
505 goto keep_locked;
506 case PAGE_ACTIVATE:
507 goto activate_locked;
508 case PAGE_SUCCESS:
509 if (PageWriteback(page) || PageDirty(page))
510 goto keep;
511 /*
512 * A synchronous write - probably a ramdisk. Go
513 * ahead and try to reclaim the page.
514 */
515 if (TestSetPageLocked(page))
516 goto keep;
517 if (PageDirty(page) || PageWriteback(page))
518 goto keep_locked;
519 mapping = page_mapping(page);
520 case PAGE_CLEAN:
521 ; /* try to free the page below */
522 }
523 }
524
525 /*
526 * If the page has buffers, try to free the buffer mappings
527 * associated with this page. If we succeed we try to free
528 * the page as well.
529 *
530 * We do this even if the page is PageDirty().
531 * try_to_release_page() does not perform I/O, but it is
532 * possible for a page to have PageDirty set, but it is actually
533 * clean (all its buffers are clean). This happens if the
534 * buffers were written out directly, with submit_bh(). ext3
535 * will do this, as well as the blockdev mapping.
536 * try_to_release_page() will discover that cleanness and will
537 * drop the buffers and mark the page clean - it can be freed.
538 *
539 * Rarely, pages can have buffers and no ->mapping. These are
540 * the pages which were not successfully invalidated in
541 * truncate_complete_page(). We try to drop those buffers here
542 * and if that worked, and the page is no longer mapped into
543 * process address space (page_count == 1) it can be freed.
544 * Otherwise, leave the page on the LRU so it is swappable.
545 */
546 if (PagePrivate(page)) {
547 if (!try_to_release_page(page, sc->gfp_mask))
548 goto activate_locked;
549 if (!mapping && page_count(page) == 1)
550 goto free_it;
551 }
552
Nick Piggin28e4d962006-09-25 23:31:23 -0700553 if (!mapping || !remove_mapping(mapping, page))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800554 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556free_it:
557 unlock_page(page);
Andrew Morton05ff5132006-03-22 00:08:20 -0800558 nr_reclaimed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (!pagevec_add(&freed_pvec, page))
560 __pagevec_release_nonlru(&freed_pvec);
561 continue;
562
563activate_locked:
564 SetPageActive(page);
565 pgactivate++;
566keep_locked:
567 unlock_page(page);
568keep:
569 list_add(&page->lru, &ret_pages);
Nick Piggin725d7042006-09-25 23:30:55 -0700570 VM_BUG_ON(PageLRU(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 list_splice(&ret_pages, page_list);
573 if (pagevec_count(&freed_pvec))
574 __pagevec_release_nonlru(&freed_pvec);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700575 count_vm_events(PGACTIVATE, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800576 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800579/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * zone->lru_lock is heavily contended. Some of the functions that
581 * shrink the lists perform better by taking out a batch of pages
582 * and working on them outside the LRU lock.
583 *
584 * For pagecache intensive workloads, this function is the hottest
585 * spot in the kernel (apart from copy_*_user functions).
586 *
587 * Appropriate locks must be held before calling this function.
588 *
589 * @nr_to_scan: The number of pages to look through on the list.
590 * @src: The LRU list to pull pages off.
591 * @dst: The temp list to put pages on to.
592 * @scanned: The number of pages that were scanned.
593 *
594 * returns how many pages were moved onto *@dst.
595 */
Andrew Morton69e05942006-03-22 00:08:19 -0800596static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
597 struct list_head *src, struct list_head *dst,
598 unsigned long *scanned)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Andrew Morton69e05942006-03-22 00:08:19 -0800600 unsigned long nr_taken = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct page *page;
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800602 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800604 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800605 struct list_head *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 page = lru_to_page(src);
607 prefetchw_prev_lru_page(page, src, flags);
608
Nick Piggin725d7042006-09-25 23:30:55 -0700609 VM_BUG_ON(!PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800610
Nick Piggin053837f2006-01-18 17:42:27 -0800611 list_del(&page->lru);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800612 target = src;
613 if (likely(get_page_unless_zero(page))) {
Nick Piggin053837f2006-01-18 17:42:27 -0800614 /*
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800615 * Be careful not to clear PageLRU until after we're
616 * sure the page is not being freed elsewhere -- the
617 * page release code relies on it.
Nick Piggin053837f2006-01-18 17:42:27 -0800618 */
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800619 ClearPageLRU(page);
620 target = dst;
621 nr_taken++;
622 } /* else it is being freed elsewhere */
Nick Piggin46453a62006-03-22 00:07:58 -0800623
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800624 list_add(&page->lru, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
627 *scanned = scan;
628 return nr_taken;
629}
630
631/*
Andrew Morton1742f192006-03-22 00:08:21 -0800632 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
633 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
Andrew Morton1742f192006-03-22 00:08:21 -0800635static unsigned long shrink_inactive_list(unsigned long max_scan,
636 struct zone *zone, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 LIST_HEAD(page_list);
639 struct pagevec pvec;
Andrew Morton69e05942006-03-22 00:08:19 -0800640 unsigned long nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800641 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 pagevec_init(&pvec, 1);
644
645 lru_add_drain();
646 spin_lock_irq(&zone->lru_lock);
Andrew Morton69e05942006-03-22 00:08:19 -0800647 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -0800649 unsigned long nr_taken;
650 unsigned long nr_scan;
651 unsigned long nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
654 &zone->inactive_list,
655 &page_list, &nr_scan);
656 zone->nr_inactive -= nr_taken;
657 zone->pages_scanned += nr_scan;
658 spin_unlock_irq(&zone->lru_lock);
659
Andrew Morton69e05942006-03-22 00:08:19 -0800660 nr_scanned += nr_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800661 nr_freed = shrink_page_list(&page_list, sc);
Andrew Morton05ff5132006-03-22 00:08:20 -0800662 nr_reclaimed += nr_freed;
Nick Piggina74609f2006-01-06 00:11:20 -0800663 local_irq_disable();
664 if (current_is_kswapd()) {
Christoph Lameterf8891e52006-06-30 01:55:45 -0700665 __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
666 __count_vm_events(KSWAPD_STEAL, nr_freed);
Nick Piggina74609f2006-01-06 00:11:20 -0800667 } else
Christoph Lameterf8891e52006-06-30 01:55:45 -0700668 __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
669 __count_vm_events(PGACTIVATE, nr_freed);
Nick Piggina74609f2006-01-06 00:11:20 -0800670
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800671 if (nr_taken == 0)
672 goto done;
673
Nick Piggina74609f2006-01-06 00:11:20 -0800674 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /*
676 * Put back any unfreeable pages.
677 */
678 while (!list_empty(&page_list)) {
679 page = lru_to_page(&page_list);
Nick Piggin725d7042006-09-25 23:30:55 -0700680 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800681 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 list_del(&page->lru);
683 if (PageActive(page))
684 add_page_to_active_list(zone, page);
685 else
686 add_page_to_inactive_list(zone, page);
687 if (!pagevec_add(&pvec, page)) {
688 spin_unlock_irq(&zone->lru_lock);
689 __pagevec_release(&pvec);
690 spin_lock_irq(&zone->lru_lock);
691 }
692 }
Andrew Morton69e05942006-03-22 00:08:19 -0800693 } while (nr_scanned < max_scan);
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800694 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695done:
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800696 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 pagevec_release(&pvec);
Andrew Morton05ff5132006-03-22 00:08:20 -0800698 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700701static inline int zone_is_near_oom(struct zone *zone)
702{
703 return zone->pages_scanned >= (zone->nr_active + zone->nr_inactive)*3;
704}
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706/*
707 * This moves pages from the active list to the inactive list.
708 *
709 * We move them the other way if the page is referenced by one or more
710 * processes, from rmap.
711 *
712 * If the pages are mostly unmapped, the processing is fast and it is
713 * appropriate to hold zone->lru_lock across the whole operation. But if
714 * the pages are mapped, the processing is slow (page_referenced()) so we
715 * should drop zone->lru_lock around each page. It's impossible to balance
716 * this, so instead we remove the pages from the LRU while processing them.
717 * It is safe to rely on PG_active against the non-LRU pages in here because
718 * nobody will play with that bit on a non-LRU page.
719 *
720 * The downside is that we have to touch page->_count against each page.
721 * But we had to alter page->flags anyway.
722 */
Andrew Morton1742f192006-03-22 00:08:21 -0800723static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
724 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Andrew Morton69e05942006-03-22 00:08:19 -0800726 unsigned long pgmoved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int pgdeactivate = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800728 unsigned long pgscanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 LIST_HEAD(l_hold); /* The pages which were snipped off */
730 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
731 LIST_HEAD(l_active); /* Pages to go onto the active_list */
732 struct page *page;
733 struct pagevec pvec;
734 int reclaim_mapped = 0;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800735
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800736 if (sc->may_swap) {
Christoph Lameter2903fb12006-02-11 17:55:55 -0800737 long mapped_ratio;
738 long distress;
739 long swap_tendency;
740
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700741 if (zone_is_near_oom(zone))
742 goto force_reclaim_mapped;
743
Christoph Lameter2903fb12006-02-11 17:55:55 -0800744 /*
745 * `distress' is a measure of how much trouble we're having
746 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
747 */
748 distress = 100 >> zone->prev_priority;
749
750 /*
751 * The point of this algorithm is to decide when to start
752 * reclaiming mapped memory instead of just pagecache. Work out
753 * how much memory
754 * is mapped.
755 */
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700756 mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
757 global_page_state(NR_ANON_PAGES)) * 100) /
Christoph Lameterbf02cf42006-06-30 01:55:36 -0700758 vm_total_pages;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800759
760 /*
761 * Now decide how much we really want to unmap some pages. The
762 * mapped ratio is downgraded - just because there's a lot of
763 * mapped memory doesn't necessarily mean that page reclaim
764 * isn't succeeding.
765 *
766 * The distress ratio is important - we don't want to start
767 * going oom.
768 *
769 * A 100% value of vm_swappiness overrides this algorithm
770 * altogether.
771 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700772 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800773
774 /*
775 * Now use this metric to decide whether to start moving mapped
776 * memory onto the inactive list.
777 */
778 if (swap_tendency >= 100)
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700779force_reclaim_mapped:
Christoph Lameter2903fb12006-02-11 17:55:55 -0800780 reclaim_mapped = 1;
781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 lru_add_drain();
784 spin_lock_irq(&zone->lru_lock);
785 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
786 &l_hold, &pgscanned);
787 zone->pages_scanned += pgscanned;
788 zone->nr_active -= pgmoved;
789 spin_unlock_irq(&zone->lru_lock);
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 while (!list_empty(&l_hold)) {
792 cond_resched();
793 page = lru_to_page(&l_hold);
794 list_del(&page->lru);
795 if (page_mapped(page)) {
796 if (!reclaim_mapped ||
797 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800798 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 list_add(&page->lru, &l_active);
800 continue;
801 }
802 }
803 list_add(&page->lru, &l_inactive);
804 }
805
806 pagevec_init(&pvec, 1);
807 pgmoved = 0;
808 spin_lock_irq(&zone->lru_lock);
809 while (!list_empty(&l_inactive)) {
810 page = lru_to_page(&l_inactive);
811 prefetchw_prev_lru_page(page, &l_inactive, flags);
Nick Piggin725d7042006-09-25 23:30:55 -0700812 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800813 SetPageLRU(page);
Nick Piggin725d7042006-09-25 23:30:55 -0700814 VM_BUG_ON(!PageActive(page));
Nick Piggin4c84cac2006-03-22 00:08:00 -0800815 ClearPageActive(page);
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 list_move(&page->lru, &zone->inactive_list);
818 pgmoved++;
819 if (!pagevec_add(&pvec, page)) {
820 zone->nr_inactive += pgmoved;
821 spin_unlock_irq(&zone->lru_lock);
822 pgdeactivate += pgmoved;
823 pgmoved = 0;
824 if (buffer_heads_over_limit)
825 pagevec_strip(&pvec);
826 __pagevec_release(&pvec);
827 spin_lock_irq(&zone->lru_lock);
828 }
829 }
830 zone->nr_inactive += pgmoved;
831 pgdeactivate += pgmoved;
832 if (buffer_heads_over_limit) {
833 spin_unlock_irq(&zone->lru_lock);
834 pagevec_strip(&pvec);
835 spin_lock_irq(&zone->lru_lock);
836 }
837
838 pgmoved = 0;
839 while (!list_empty(&l_active)) {
840 page = lru_to_page(&l_active);
841 prefetchw_prev_lru_page(page, &l_active, flags);
Nick Piggin725d7042006-09-25 23:30:55 -0700842 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800843 SetPageLRU(page);
Nick Piggin725d7042006-09-25 23:30:55 -0700844 VM_BUG_ON(!PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 list_move(&page->lru, &zone->active_list);
846 pgmoved++;
847 if (!pagevec_add(&pvec, page)) {
848 zone->nr_active += pgmoved;
849 pgmoved = 0;
850 spin_unlock_irq(&zone->lru_lock);
851 __pagevec_release(&pvec);
852 spin_lock_irq(&zone->lru_lock);
853 }
854 }
855 zone->nr_active += pgmoved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Christoph Lameterf8891e52006-06-30 01:55:45 -0700857 __count_zone_vm_events(PGREFILL, zone, pgscanned);
858 __count_vm_events(PGDEACTIVATE, pgdeactivate);
859 spin_unlock_irq(&zone->lru_lock);
Nick Piggina74609f2006-01-06 00:11:20 -0800860
861 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
864/*
865 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
866 */
Andrew Morton05ff5132006-03-22 00:08:20 -0800867static unsigned long shrink_zone(int priority, struct zone *zone,
868 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 unsigned long nr_active;
871 unsigned long nr_inactive;
Christoph Lameter86959492006-03-22 00:08:18 -0800872 unsigned long nr_to_scan;
Andrew Morton05ff5132006-03-22 00:08:20 -0800873 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Martin Hicks53e9a612005-09-03 15:54:51 -0700875 atomic_inc(&zone->reclaim_in_progress);
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /*
878 * Add one to `nr_to_scan' just to make sure that the kernel will
879 * slowly sift through the active list.
880 */
Christoph Lameter86959492006-03-22 00:08:18 -0800881 zone->nr_scan_active += (zone->nr_active >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 nr_active = zone->nr_scan_active;
883 if (nr_active >= sc->swap_cluster_max)
884 zone->nr_scan_active = 0;
885 else
886 nr_active = 0;
887
Christoph Lameter86959492006-03-22 00:08:18 -0800888 zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 nr_inactive = zone->nr_scan_inactive;
890 if (nr_inactive >= sc->swap_cluster_max)
891 zone->nr_scan_inactive = 0;
892 else
893 nr_inactive = 0;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 while (nr_active || nr_inactive) {
896 if (nr_active) {
Christoph Lameter86959492006-03-22 00:08:18 -0800897 nr_to_scan = min(nr_active,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -0800899 nr_active -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800900 shrink_active_list(nr_to_scan, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902
903 if (nr_inactive) {
Christoph Lameter86959492006-03-22 00:08:18 -0800904 nr_to_scan = min(nr_inactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -0800906 nr_inactive -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800907 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
908 sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910 }
911
912 throttle_vm_writeout();
Martin Hicks53e9a612005-09-03 15:54:51 -0700913
914 atomic_dec(&zone->reclaim_in_progress);
Andrew Morton05ff5132006-03-22 00:08:20 -0800915 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918/*
919 * This is the direct reclaim path, for page-allocating processes. We only
920 * try to reclaim pages from zones which will satisfy the caller's allocation
921 * request.
922 *
923 * We reclaim from a zone even if that zone is over pages_high. Because:
924 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
925 * allocation or
926 * b) The zones may be over pages_high but they must go *over* pages_high to
927 * satisfy the `incremental min' zone defense algorithm.
928 *
929 * Returns the number of reclaimed pages.
930 *
931 * If a zone is deemed to be full of pinned pages then just give it a light
932 * scan then give up on it.
933 */
Andrew Morton1742f192006-03-22 00:08:21 -0800934static unsigned long shrink_zones(int priority, struct zone **zones,
Andrew Morton05ff5132006-03-22 00:08:20 -0800935 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Andrew Morton05ff5132006-03-22 00:08:20 -0800937 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 int i;
939
Nick Piggin408d8542006-09-25 23:31:27 -0700940 sc->all_unreclaimable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 for (i = 0; zones[i] != NULL; i++) {
942 struct zone *zone = zones[i];
943
Con Kolivasf3fe6512006-01-06 00:11:15 -0800944 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 continue;
946
Paul Jackson9bf22292005-09-06 15:18:12 -0700947 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 continue;
949
Christoph Lameter86959492006-03-22 00:08:18 -0800950 zone->temp_priority = priority;
951 if (zone->prev_priority > priority)
952 zone->prev_priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Christoph Lameter86959492006-03-22 00:08:18 -0800954 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 continue; /* Let kswapd poll it */
956
Nick Piggin408d8542006-09-25 23:31:27 -0700957 sc->all_unreclaimable = 0;
958
Andrew Morton05ff5132006-03-22 00:08:20 -0800959 nr_reclaimed += shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
Andrew Morton05ff5132006-03-22 00:08:20 -0800961 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964/*
965 * This is the main entry point to direct page reclaim.
966 *
967 * If a full scan of the inactive list fails to free enough memory then we
968 * are "out of memory" and something needs to be killed.
969 *
970 * If the caller is !__GFP_FS then the probability of a failure is reasonably
971 * high - the zone may be full of dirty or under-writeback pages, which this
972 * caller can't do much about. We kick pdflush and take explicit naps in the
973 * hope that some of these pages can be written. But if the allocating task
974 * holds filesystem locks which prevent writeout this might not work, and the
975 * allocation attempt will fail.
976 */
Andrew Morton69e05942006-03-22 00:08:19 -0800977unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 int priority;
980 int ret = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800981 unsigned long total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800982 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 struct reclaim_state *reclaim_state = current->reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 unsigned long lru_pages = 0;
985 int i;
Andrew Morton179e9632006-03-22 00:08:18 -0800986 struct scan_control sc = {
987 .gfp_mask = gfp_mask,
988 .may_writepage = !laptop_mode,
989 .swap_cluster_max = SWAP_CLUSTER_MAX,
990 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700991 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -0800992 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Christoph Lameterf8891e52006-06-30 01:55:45 -0700994 count_vm_event(ALLOCSTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 for (i = 0; zones[i] != NULL; i++) {
997 struct zone *zone = zones[i];
998
Paul Jackson9bf22292005-09-06 15:18:12 -0700999 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 continue;
1001
1002 zone->temp_priority = DEF_PRIORITY;
1003 lru_pages += zone->nr_active + zone->nr_inactive;
1004 }
1005
1006 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 sc.nr_scanned = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001008 if (!priority)
1009 disable_swap_token();
Andrew Morton1742f192006-03-22 00:08:21 -08001010 nr_reclaimed += shrink_zones(priority, zones, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
1012 if (reclaim_state) {
Andrew Morton05ff5132006-03-22 00:08:20 -08001013 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 reclaim_state->reclaimed_slab = 0;
1015 }
1016 total_scanned += sc.nr_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001017 if (nr_reclaimed >= sc.swap_cluster_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 ret = 1;
1019 goto out;
1020 }
1021
1022 /*
1023 * Try to write back as many pages as we just scanned. This
1024 * tends to cause slow streaming writers to write data to the
1025 * disk smoothly, at the dirtying rate, which is nice. But
1026 * that's undesirable in laptop mode, where we *want* lumpy
1027 * writeout. So in laptop mode, write out the whole world.
1028 */
Andrew Morton179e9632006-03-22 00:08:18 -08001029 if (total_scanned > sc.swap_cluster_max +
1030 sc.swap_cluster_max / 2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001031 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 sc.may_writepage = 1;
1033 }
1034
1035 /* Take a nap, wait for some writeback to complete */
1036 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1037 blk_congestion_wait(WRITE, HZ/10);
1038 }
Nick Piggin408d8542006-09-25 23:31:27 -07001039 /* top priority shrink_caches still had more to do? don't OOM, then */
1040 if (!sc.all_unreclaimable)
1041 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042out:
1043 for (i = 0; zones[i] != 0; i++) {
1044 struct zone *zone = zones[i];
1045
Paul Jackson9bf22292005-09-06 15:18:12 -07001046 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 continue;
1048
1049 zone->prev_priority = zone->temp_priority;
1050 }
1051 return ret;
1052}
1053
1054/*
1055 * For kswapd, balance_pgdat() will work across all this node's zones until
1056 * they are all at pages_high.
1057 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 * Returns the number of pages which were actually freed.
1059 *
1060 * There is special handling here for zones which are full of pinned pages.
1061 * This can happen if the pages are all mlocked, or if they are all used by
1062 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1063 * What we do is to detect the case where all pages in the zone have been
1064 * scanned twice and there has been zero successful reclaim. Mark the zone as
1065 * dead and from now on, only perform a short scan. Basically we're polling
1066 * the zone for when the problem goes away.
1067 *
1068 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1069 * zones which have free_pages > pages_high, but once a zone is found to have
1070 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1071 * of the number of free pages in the lower zones. This interoperates with
1072 * the page allocator fallback scheme to ensure that aging of pages is balanced
1073 * across the zones.
1074 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001075static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 int all_zones_ok;
1078 int priority;
1079 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08001080 unsigned long total_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001081 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08001083 struct scan_control sc = {
1084 .gfp_mask = GFP_KERNEL,
1085 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001086 .swap_cluster_max = SWAP_CLUSTER_MAX,
1087 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -08001088 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090loop_again:
1091 total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001092 nr_reclaimed = 0;
Christoph Lameterc0bbbc72006-06-11 15:22:26 -07001093 sc.may_writepage = !laptop_mode;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001094 count_vm_event(PAGEOUTRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 for (i = 0; i < pgdat->nr_zones; i++) {
1097 struct zone *zone = pgdat->node_zones + i;
1098
1099 zone->temp_priority = DEF_PRIORITY;
1100 }
1101
1102 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1103 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1104 unsigned long lru_pages = 0;
1105
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001106 /* The swap token gets in the way of swapout... */
1107 if (!priority)
1108 disable_swap_token();
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 all_zones_ok = 1;
1111
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001112 /*
1113 * Scan in the highmem->dma direction for the highest
1114 * zone which needs scanning
1115 */
1116 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1117 struct zone *zone = pgdat->node_zones + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001119 if (!populated_zone(zone))
1120 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001122 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1123 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001125 if (!zone_watermark_ok(zone, order, zone->pages_high,
1126 0, 0)) {
1127 end_zone = i;
1128 goto scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001131 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132scan:
1133 for (i = 0; i <= end_zone; i++) {
1134 struct zone *zone = pgdat->node_zones + i;
1135
1136 lru_pages += zone->nr_active + zone->nr_inactive;
1137 }
1138
1139 /*
1140 * Now scan the zone in the dma->highmem direction, stopping
1141 * at the last zone which needs scanning.
1142 *
1143 * We do this because the page allocator works in the opposite
1144 * direction. This prevents the page allocator from allocating
1145 * pages behind kswapd's direction of progress, which would
1146 * cause too much scanning of the lower zones.
1147 */
1148 for (i = 0; i <= end_zone; i++) {
1149 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001150 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Con Kolivasf3fe6512006-01-06 00:11:15 -08001152 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 continue;
1154
1155 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1156 continue;
1157
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001158 if (!zone_watermark_ok(zone, order, zone->pages_high,
1159 end_zone, 0))
1160 all_zones_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 zone->temp_priority = priority;
1162 if (zone->prev_priority > priority)
1163 zone->prev_priority = priority;
1164 sc.nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001165 nr_reclaimed += shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001167 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1168 lru_pages);
Andrew Morton05ff5132006-03-22 00:08:20 -08001169 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 total_scanned += sc.nr_scanned;
1171 if (zone->all_unreclaimable)
1172 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001173 if (nr_slab == 0 && zone->pages_scanned >=
Nick Piggin4ff1ffb2006-09-25 23:31:28 -07001174 (zone->nr_active + zone->nr_inactive) * 6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 zone->all_unreclaimable = 1;
1176 /*
1177 * If we've done a decent amount of scanning and
1178 * the reclaim ratio is low, start doing writepage
1179 * even in laptop mode
1180 */
1181 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Andrew Morton05ff5132006-03-22 00:08:20 -08001182 total_scanned > nr_reclaimed + nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 sc.may_writepage = 1;
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (all_zones_ok)
1186 break; /* kswapd: all done */
1187 /*
1188 * OK, kswapd is getting into trouble. Take a nap, then take
1189 * another pass across the zones.
1190 */
1191 if (total_scanned && priority < DEF_PRIORITY - 2)
1192 blk_congestion_wait(WRITE, HZ/10);
1193
1194 /*
1195 * We do this so kswapd doesn't build up large priorities for
1196 * example when it is freeing in parallel with allocators. It
1197 * matches the direct reclaim path behaviour in terms of impact
1198 * on zone->*_priority.
1199 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001200 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 break;
1202 }
1203out:
1204 for (i = 0; i < pgdat->nr_zones; i++) {
1205 struct zone *zone = pgdat->node_zones + i;
1206
1207 zone->prev_priority = zone->temp_priority;
1208 }
1209 if (!all_zones_ok) {
1210 cond_resched();
1211 goto loop_again;
1212 }
1213
Andrew Morton05ff5132006-03-22 00:08:20 -08001214 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217/*
1218 * The background pageout daemon, started as a kernel thread
1219 * from the init process.
1220 *
1221 * This basically trickles out pages so that we have _some_
1222 * free memory available even if there is no other activity
1223 * that frees anything up. This is needed for things like routing
1224 * etc, where we otherwise might have all activity going on in
1225 * asynchronous contexts that cannot page things out.
1226 *
1227 * If there are applications that are active memory-allocators
1228 * (most normal use), this basically shouldn't matter.
1229 */
1230static int kswapd(void *p)
1231{
1232 unsigned long order;
1233 pg_data_t *pgdat = (pg_data_t*)p;
1234 struct task_struct *tsk = current;
1235 DEFINE_WAIT(wait);
1236 struct reclaim_state reclaim_state = {
1237 .reclaimed_slab = 0,
1238 };
1239 cpumask_t cpumask;
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 cpumask = node_to_cpumask(pgdat->node_id);
1242 if (!cpus_empty(cpumask))
1243 set_cpus_allowed(tsk, cpumask);
1244 current->reclaim_state = &reclaim_state;
1245
1246 /*
1247 * Tell the memory management that we're a "memory allocator",
1248 * and that if we need more memory we should get access to it
1249 * regardless (see "__alloc_pages()"). "kswapd" should
1250 * never get caught in the normal page freeing logic.
1251 *
1252 * (Kswapd normally doesn't need memory anyway, but sometimes
1253 * you need a small amount of memory in order to be able to
1254 * page out something else, and this flag essentially protects
1255 * us from recursively trying to free more memory as we're
1256 * trying to free the first piece of memory in the first place).
1257 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001258 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 order = 0;
1261 for ( ; ; ) {
1262 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001263
1264 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1267 new_order = pgdat->kswapd_max_order;
1268 pgdat->kswapd_max_order = 0;
1269 if (order < new_order) {
1270 /*
1271 * Don't sleep if someone wants a larger 'order'
1272 * allocation
1273 */
1274 order = new_order;
1275 } else {
1276 schedule();
1277 order = pgdat->kswapd_max_order;
1278 }
1279 finish_wait(&pgdat->kswapd_wait, &wait);
1280
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001281 balance_pgdat(pgdat, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 return 0;
1284}
1285
1286/*
1287 * A zone is low on free memory, so wake its kswapd task to service it.
1288 */
1289void wakeup_kswapd(struct zone *zone, int order)
1290{
1291 pg_data_t *pgdat;
1292
Con Kolivasf3fe6512006-01-06 00:11:15 -08001293 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return;
1295
1296 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001297 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return;
1299 if (pgdat->kswapd_max_order < order)
1300 pgdat->kswapd_max_order = order;
Paul Jackson9bf22292005-09-06 15:18:12 -07001301 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001303 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001305 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308#ifdef CONFIG_PM
1309/*
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001310 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1311 * from LRU lists system-wide, for given pass and priority, and returns the
1312 * number of reclaimed pages
1313 *
1314 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1315 */
1316static unsigned long shrink_all_zones(unsigned long nr_pages, int pass,
1317 int prio, struct scan_control *sc)
1318{
1319 struct zone *zone;
1320 unsigned long nr_to_scan, ret = 0;
1321
1322 for_each_zone(zone) {
1323
1324 if (!populated_zone(zone))
1325 continue;
1326
1327 if (zone->all_unreclaimable && prio != DEF_PRIORITY)
1328 continue;
1329
1330 /* For pass = 0 we don't shrink the active list */
1331 if (pass > 0) {
1332 zone->nr_scan_active += (zone->nr_active >> prio) + 1;
1333 if (zone->nr_scan_active >= nr_pages || pass > 3) {
1334 zone->nr_scan_active = 0;
1335 nr_to_scan = min(nr_pages, zone->nr_active);
1336 shrink_active_list(nr_to_scan, zone, sc);
1337 }
1338 }
1339
1340 zone->nr_scan_inactive += (zone->nr_inactive >> prio) + 1;
1341 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1342 zone->nr_scan_inactive = 0;
1343 nr_to_scan = min(nr_pages, zone->nr_inactive);
1344 ret += shrink_inactive_list(nr_to_scan, zone, sc);
1345 if (ret >= nr_pages)
1346 return ret;
1347 }
1348 }
1349
1350 return ret;
1351}
1352
1353/*
1354 * Try to free `nr_pages' of memory, system-wide, and return the number of
1355 * freed pages.
1356 *
1357 * Rather than trying to age LRUs the aim is to preserve the overall
1358 * LRU order by reclaiming preferentially
1359 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
Andrew Morton69e05942006-03-22 00:08:19 -08001361unsigned long shrink_all_memory(unsigned long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001363 unsigned long lru_pages, nr_slab;
Andrew Morton69e05942006-03-22 00:08:19 -08001364 unsigned long ret = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001365 int pass;
1366 struct reclaim_state reclaim_state;
1367 struct zone *zone;
1368 struct scan_control sc = {
1369 .gfp_mask = GFP_KERNEL,
1370 .may_swap = 0,
1371 .swap_cluster_max = nr_pages,
1372 .may_writepage = 1,
1373 .swappiness = vm_swappiness,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 };
1375
1376 current->reclaim_state = &reclaim_state;
Andrew Morton69e05942006-03-22 00:08:19 -08001377
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001378 lru_pages = 0;
1379 for_each_zone(zone)
1380 lru_pages += zone->nr_active + zone->nr_inactive;
1381
Christoph Lameter972d1a72006-09-25 23:31:51 -07001382 nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001383 /* If slab caches are huge, it's better to hit them first */
1384 while (nr_slab >= lru_pages) {
1385 reclaim_state.reclaimed_slab = 0;
1386 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1387 if (!reclaim_state.reclaimed_slab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 break;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001389
1390 ret += reclaim_state.reclaimed_slab;
1391 if (ret >= nr_pages)
1392 goto out;
1393
1394 nr_slab -= reclaim_state.reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001396
1397 /*
1398 * We try to shrink LRUs in 5 passes:
1399 * 0 = Reclaim from inactive_list only
1400 * 1 = Reclaim from active list but don't reclaim mapped
1401 * 2 = 2nd pass of type 1
1402 * 3 = Reclaim mapped (normal reclaim)
1403 * 4 = 2nd pass of type 3
1404 */
1405 for (pass = 0; pass < 5; pass++) {
1406 int prio;
1407
1408 /* Needed for shrinking slab caches later on */
1409 if (!lru_pages)
1410 for_each_zone(zone) {
1411 lru_pages += zone->nr_active;
1412 lru_pages += zone->nr_inactive;
1413 }
1414
1415 /* Force reclaiming mapped pages in the passes #3 and #4 */
1416 if (pass > 2) {
1417 sc.may_swap = 1;
1418 sc.swappiness = 100;
1419 }
1420
1421 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1422 unsigned long nr_to_scan = nr_pages - ret;
1423
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001424 sc.nr_scanned = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001425 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1426 if (ret >= nr_pages)
1427 goto out;
1428
1429 reclaim_state.reclaimed_slab = 0;
1430 shrink_slab(sc.nr_scanned, sc.gfp_mask, lru_pages);
1431 ret += reclaim_state.reclaimed_slab;
1432 if (ret >= nr_pages)
1433 goto out;
1434
1435 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
1436 blk_congestion_wait(WRITE, HZ / 10);
1437 }
1438
1439 lru_pages = 0;
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001440 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001441
1442 /*
1443 * If ret = 0, we could not shrink LRUs, but there may be something
1444 * in slab caches
1445 */
1446 if (!ret)
1447 do {
1448 reclaim_state.reclaimed_slab = 0;
1449 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1450 ret += reclaim_state.reclaimed_slab;
1451 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
1452
1453out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 current->reclaim_state = NULL;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return ret;
1457}
1458#endif
1459
1460#ifdef CONFIG_HOTPLUG_CPU
1461/* It's optimal to keep kswapds on the same CPUs as their memory, but
1462 not required for correctness. So if the last cpu in a node goes
1463 away, we get changed to run anywhere: as the first one comes back,
1464 restore their cpu bindings. */
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07001465static int __devinit cpu_callback(struct notifier_block *nfb,
Andrew Morton69e05942006-03-22 00:08:19 -08001466 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 pg_data_t *pgdat;
1469 cpumask_t mask;
1470
1471 if (action == CPU_ONLINE) {
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08001472 for_each_online_pgdat(pgdat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 mask = node_to_cpumask(pgdat->node_id);
1474 if (any_online_cpu(mask) != NR_CPUS)
1475 /* One of our CPUs online: restore mask */
1476 set_cpus_allowed(pgdat->kswapd, mask);
1477 }
1478 }
1479 return NOTIFY_OK;
1480}
1481#endif /* CONFIG_HOTPLUG_CPU */
1482
Yasunori Goto3218ae12006-06-27 02:53:33 -07001483/*
1484 * This kswapd start function will be called by init and node-hot-add.
1485 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
1486 */
1487int kswapd_run(int nid)
1488{
1489 pg_data_t *pgdat = NODE_DATA(nid);
1490 int ret = 0;
1491
1492 if (pgdat->kswapd)
1493 return 0;
1494
1495 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
1496 if (IS_ERR(pgdat->kswapd)) {
1497 /* failure at boot is fatal */
1498 BUG_ON(system_state == SYSTEM_BOOTING);
1499 printk("Failed to start kswapd on node %d\n",nid);
1500 ret = -1;
1501 }
1502 return ret;
1503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505static int __init kswapd_init(void)
1506{
Yasunori Goto3218ae12006-06-27 02:53:33 -07001507 int nid;
Andrew Morton69e05942006-03-22 00:08:19 -08001508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 swap_setup();
Yasunori Goto3218ae12006-06-27 02:53:33 -07001510 for_each_online_node(nid)
1511 kswapd_run(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 hotcpu_notifier(cpu_callback, 0);
1513 return 0;
1514}
1515
1516module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001517
1518#ifdef CONFIG_NUMA
1519/*
1520 * Zone reclaim mode
1521 *
1522 * If non-zero call zone_reclaim when the number of free pages falls below
1523 * the watermarks.
Christoph Lameter9eeff232006-01-18 17:42:31 -08001524 */
1525int zone_reclaim_mode __read_mostly;
1526
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001527#define RECLAIM_OFF 0
1528#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1529#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1530#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1531
Christoph Lameter9eeff232006-01-18 17:42:31 -08001532/*
Christoph Lametera92f7122006-02-01 03:05:32 -08001533 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1534 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1535 * a zone.
1536 */
1537#define ZONE_RECLAIM_PRIORITY 4
1538
Christoph Lameter9eeff232006-01-18 17:42:31 -08001539/*
Christoph Lameter96146342006-07-03 00:24:13 -07001540 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
1541 * occur.
1542 */
1543int sysctl_min_unmapped_ratio = 1;
1544
1545/*
Christoph Lameter0ff38492006-09-25 23:31:52 -07001546 * If the number of slab pages in a zone grows beyond this percentage then
1547 * slab reclaim needs to occur.
1548 */
1549int sysctl_min_slab_ratio = 5;
1550
1551/*
Christoph Lameter9eeff232006-01-18 17:42:31 -08001552 * Try to free up some pages from this zone through reclaim.
1553 */
Andrew Morton179e9632006-03-22 00:08:18 -08001554static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001555{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001556 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08001557 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001558 struct task_struct *p = current;
1559 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08001560 int priority;
Andrew Morton05ff5132006-03-22 00:08:20 -08001561 unsigned long nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001562 struct scan_control sc = {
1563 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1564 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
Andrew Morton69e05942006-03-22 00:08:19 -08001565 .swap_cluster_max = max_t(unsigned long, nr_pages,
1566 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08001567 .gfp_mask = gfp_mask,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001568 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -08001569 };
Christoph Lameter83e33a42006-09-25 23:31:53 -07001570 unsigned long slab_reclaimable;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001571
1572 disable_swap_token();
Christoph Lameter9eeff232006-01-18 17:42:31 -08001573 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08001574 /*
1575 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1576 * and we also need to be able to write out pages for RECLAIM_WRITE
1577 * and RECLAIM_SWAP.
1578 */
1579 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001580 reclaim_state.reclaimed_slab = 0;
1581 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001582
Christoph Lameter0ff38492006-09-25 23:31:52 -07001583 if (zone_page_state(zone, NR_FILE_PAGES) -
1584 zone_page_state(zone, NR_FILE_MAPPED) >
1585 zone->min_unmapped_pages) {
1586 /*
1587 * Free memory by calling shrink zone with increasing
1588 * priorities until we have enough memory freed.
1589 */
1590 priority = ZONE_RECLAIM_PRIORITY;
1591 do {
1592 nr_reclaimed += shrink_zone(priority, zone, &sc);
1593 priority--;
1594 } while (priority >= 0 && nr_reclaimed < nr_pages);
1595 }
Christoph Lameterc84db232006-02-01 03:05:29 -08001596
Christoph Lameter83e33a42006-09-25 23:31:53 -07001597 slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1598 if (slab_reclaimable > zone->min_slab_pages) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001599 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001600 * shrink_slab() does not currently allow us to determine how
Christoph Lameter0ff38492006-09-25 23:31:52 -07001601 * many pages were freed in this zone. So we take the current
1602 * number of slab pages and shake the slab until it is reduced
1603 * by the same nr_pages that we used for reclaiming unmapped
1604 * pages.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001605 *
Christoph Lameter0ff38492006-09-25 23:31:52 -07001606 * Note that shrink_slab will free memory on all zones and may
1607 * take a long time.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001608 */
Christoph Lameter0ff38492006-09-25 23:31:52 -07001609 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
Christoph Lameter83e33a42006-09-25 23:31:53 -07001610 zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
1611 slab_reclaimable - nr_pages)
Christoph Lameter0ff38492006-09-25 23:31:52 -07001612 ;
Christoph Lameter83e33a42006-09-25 23:31:53 -07001613
1614 /*
1615 * Update nr_reclaimed by the number of slab pages we
1616 * reclaimed from this zone.
1617 */
1618 nr_reclaimed += slab_reclaimable -
1619 zone_page_state(zone, NR_SLAB_RECLAIMABLE);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001620 }
1621
Christoph Lameter9eeff232006-01-18 17:42:31 -08001622 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08001623 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Andrew Morton05ff5132006-03-22 00:08:20 -08001624 return nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001625}
Andrew Morton179e9632006-03-22 00:08:18 -08001626
1627int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1628{
1629 cpumask_t mask;
1630 int node_id;
1631
1632 /*
Christoph Lameter0ff38492006-09-25 23:31:52 -07001633 * Zone reclaim reclaims unmapped file backed pages and
1634 * slab pages if we are over the defined limits.
Christoph Lameter34aa1332006-06-30 01:55:37 -07001635 *
Christoph Lameter96146342006-07-03 00:24:13 -07001636 * A small portion of unmapped file backed pages is needed for
1637 * file I/O otherwise pages read by file I/O will be immediately
1638 * thrown out if the zone is overallocated. So we do not reclaim
1639 * if less than a specified percentage of the zone is used by
1640 * unmapped file backed pages.
Andrew Morton179e9632006-03-22 00:08:18 -08001641 */
Christoph Lameter34aa1332006-06-30 01:55:37 -07001642 if (zone_page_state(zone, NR_FILE_PAGES) -
Christoph Lameter0ff38492006-09-25 23:31:52 -07001643 zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
1644 && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
1645 <= zone->min_slab_pages)
Christoph Lameter96146342006-07-03 00:24:13 -07001646 return 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001647
1648 /*
1649 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1650 * not have reclaimable pages and if we should not delay the allocation
1651 * then do not scan.
1652 */
1653 if (!(gfp_mask & __GFP_WAIT) ||
1654 zone->all_unreclaimable ||
1655 atomic_read(&zone->reclaim_in_progress) > 0 ||
1656 (current->flags & PF_MEMALLOC))
1657 return 0;
1658
1659 /*
1660 * Only run zone reclaim on the local zone or on zones that do not
1661 * have associated processors. This will favor the local processor
1662 * over remote processors and spread off node memory allocations
1663 * as wide as possible.
1664 */
Christoph Lameter89fa3022006-09-25 23:31:55 -07001665 node_id = zone_to_nid(zone);
Andrew Morton179e9632006-03-22 00:08:18 -08001666 mask = node_to_cpumask(node_id);
1667 if (!cpus_empty(mask) && node_id != numa_node_id())
1668 return 0;
1669 return __zone_reclaim(zone, gfp_mask, order);
1670}
Christoph Lameter9eeff232006-01-18 17:42:31 -08001671#endif