blob: eeacb0d695c35233e57688e4d20314a149d1d22c [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>
Yasunori Goto3218ae12006-06-27 02:53:33 -070037#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/tlbflush.h>
40#include <asm/div64.h>
41
42#include <linux/swapops.h>
43
Nick Piggin0f8053a2006-03-22 00:08:33 -080044#include "internal.h"
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046struct scan_control {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 /* Incremented by the number of inactive pages that were scanned */
48 unsigned long nr_scanned;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 unsigned long nr_mapped; /* From page_state */
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040053 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 int may_writepage;
56
Christoph Lameterf1fd1062006-01-18 17:42:30 -080057 /* Can pages be swapped as part of reclaim? */
58 int may_swap;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
61 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
62 * In this context, it doesn't matter that we scan the
63 * whole list at once. */
64 int swap_cluster_max;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -070065
66 int swappiness;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
69/*
70 * The list of shrinker callbacks used by to apply pressure to
71 * ageable caches.
72 */
73struct shrinker {
74 shrinker_t shrinker;
75 struct list_head list;
76 int seeks; /* seeks to recreate an obj */
77 long nr; /* objs pending delete */
78};
79
80#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
81
82#ifdef ARCH_HAS_PREFETCH
83#define prefetch_prev_lru_page(_page, _base, _field) \
84 do { \
85 if ((_page)->lru.prev != _base) { \
86 struct page *prev; \
87 \
88 prev = lru_to_page(&(_page->lru)); \
89 prefetch(&prev->_field); \
90 } \
91 } while (0)
92#else
93#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
94#endif
95
96#ifdef ARCH_HAS_PREFETCHW
97#define prefetchw_prev_lru_page(_page, _base, _field) \
98 do { \
99 if ((_page)->lru.prev != _base) { \
100 struct page *prev; \
101 \
102 prev = lru_to_page(&(_page->lru)); \
103 prefetchw(&prev->_field); \
104 } \
105 } while (0)
106#else
107#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
108#endif
109
110/*
111 * From 0 .. 100. Higher means more swappy.
112 */
113int vm_swappiness = 60;
Andrew Mortonbd1e22b2006-06-23 02:03:47 -0700114long vm_total_pages; /* The total number of pages which the VM controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116static LIST_HEAD(shrinker_list);
117static DECLARE_RWSEM(shrinker_rwsem);
118
119/*
120 * Add a shrinker callback to be called from the vm
121 */
122struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
123{
124 struct shrinker *shrinker;
125
126 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
127 if (shrinker) {
128 shrinker->shrinker = theshrinker;
129 shrinker->seeks = seeks;
130 shrinker->nr = 0;
131 down_write(&shrinker_rwsem);
132 list_add_tail(&shrinker->list, &shrinker_list);
133 up_write(&shrinker_rwsem);
134 }
135 return shrinker;
136}
137EXPORT_SYMBOL(set_shrinker);
138
139/*
140 * Remove one
141 */
142void remove_shrinker(struct shrinker *shrinker)
143{
144 down_write(&shrinker_rwsem);
145 list_del(&shrinker->list);
146 up_write(&shrinker_rwsem);
147 kfree(shrinker);
148}
149EXPORT_SYMBOL(remove_shrinker);
150
151#define SHRINK_BATCH 128
152/*
153 * Call the shrink functions to age shrinkable caches
154 *
155 * Here we assume it costs one seek to replace a lru page and that it also
156 * takes a seek to recreate a cache object. With this in mind we age equal
157 * percentages of the lru and ageable caches. This should balance the seeks
158 * generated by these structures.
159 *
160 * If the vm encounted mapped pages on the LRU it increase the pressure on
161 * slab to avoid swapping.
162 *
163 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
164 *
165 * `lru_pages' represents the number of on-LRU pages in all the zones which
166 * are eligible for the caller's allocation attempt. It is used for balancing
167 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700168 *
169 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
Andrew Morton69e05942006-03-22 00:08:19 -0800171unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
172 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800175 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 if (scanned == 0)
178 scanned = SWAP_CLUSTER_MAX;
179
180 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700181 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 list_for_each_entry(shrinker, &shrinker_list, list) {
184 unsigned long long delta;
185 unsigned long total_scan;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800186 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800189 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 do_div(delta, lru_pages + 1);
191 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800192 if (shrinker->nr < 0) {
193 printk(KERN_ERR "%s: nr=%ld\n",
194 __FUNCTION__, shrinker->nr);
195 shrinker->nr = max_pass;
196 }
197
198 /*
199 * Avoid risking looping forever due to too large nr value:
200 * never try to free more than twice the estimate number of
201 * freeable entries.
202 */
203 if (shrinker->nr > max_pass * 2)
204 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 total_scan = shrinker->nr;
207 shrinker->nr = 0;
208
209 while (total_scan >= SHRINK_BATCH) {
210 long this_scan = SHRINK_BATCH;
211 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700212 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700214 nr_before = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
216 if (shrink_ret == -1)
217 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700218 if (shrink_ret < nr_before)
219 ret += nr_before - shrink_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 mod_page_state(slabs_scanned, this_scan);
221 total_scan -= this_scan;
222
223 cond_resched();
224 }
225
226 shrinker->nr += total_scan;
227 }
228 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700229 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232/* Called without lock on whether page is mapped, so answer is unstable */
233static inline int page_mapping_inuse(struct page *page)
234{
235 struct address_space *mapping;
236
237 /* Page is in somebody's page tables. */
238 if (page_mapped(page))
239 return 1;
240
241 /* Be more reluctant to reclaim swapcache than pagecache */
242 if (PageSwapCache(page))
243 return 1;
244
245 mapping = page_mapping(page);
246 if (!mapping)
247 return 0;
248
249 /* File is mmap'd by somebody? */
250 return mapping_mapped(mapping);
251}
252
253static inline int is_page_cache_freeable(struct page *page)
254{
255 return page_count(page) - !!PagePrivate(page) == 2;
256}
257
258static int may_write_to_queue(struct backing_dev_info *bdi)
259{
Christoph Lameter930d9152006-01-08 01:00:47 -0800260 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 1;
262 if (!bdi_write_congested(bdi))
263 return 1;
264 if (bdi == current->backing_dev_info)
265 return 1;
266 return 0;
267}
268
269/*
270 * We detected a synchronous write error writing a page out. Probably
271 * -ENOSPC. We need to propagate that into the address_space for a subsequent
272 * fsync(), msync() or close().
273 *
274 * The tricky part is that after writepage we cannot touch the mapping: nothing
275 * prevents it from being freed up. But we have a ref on the page and once
276 * that page is locked, the mapping is pinned.
277 *
278 * We're allowed to run sleeping lock_page() here because we know the caller has
279 * __GFP_FS.
280 */
281static void handle_write_error(struct address_space *mapping,
282 struct page *page, int error)
283{
284 lock_page(page);
285 if (page_mapping(page) == mapping) {
286 if (error == -ENOSPC)
287 set_bit(AS_ENOSPC, &mapping->flags);
288 else
289 set_bit(AS_EIO, &mapping->flags);
290 }
291 unlock_page(page);
292}
293
Christoph Lameter04e62a22006-06-23 02:03:38 -0700294/* possible outcome of pageout() */
295typedef enum {
296 /* failed to write page out, page is locked */
297 PAGE_KEEP,
298 /* move page to the active list, page is locked */
299 PAGE_ACTIVATE,
300 /* page has been sent to the disk successfully, page is unlocked */
301 PAGE_SUCCESS,
302 /* page is clean and locked */
303 PAGE_CLEAN,
304} pageout_t;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
Andrew Morton1742f192006-03-22 00:08:21 -0800307 * pageout is called by shrink_page_list() for each dirty page.
308 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
Christoph Lameter04e62a22006-06-23 02:03:38 -0700310static pageout_t pageout(struct page *page, struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 /*
313 * If the page is dirty, only perform writeback if that write
314 * will be non-blocking. To prevent this allocation from being
315 * stalled by pagecache activity. But note that there may be
316 * stalls if we need to run get_block(). We could test
317 * PagePrivate for that.
318 *
319 * If this process is currently in generic_file_write() against
320 * this page's queue, we can perform writeback even if that
321 * will block.
322 *
323 * If the page is swapcache, write it back even if that would
324 * block, for some throttling. This happens by accident, because
325 * swap_backing_dev_info is bust: it doesn't reflect the
326 * congestion state of the swapdevs. Easy to fix, if needed.
327 * See swapfile.c:page_queue_congested().
328 */
329 if (!is_page_cache_freeable(page))
330 return PAGE_KEEP;
331 if (!mapping) {
332 /*
333 * Some data journaling orphaned pages can have
334 * page->mapping == NULL while being dirty with clean buffers.
335 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700336 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (try_to_free_buffers(page)) {
338 ClearPageDirty(page);
339 printk("%s: orphaned page\n", __FUNCTION__);
340 return PAGE_CLEAN;
341 }
342 }
343 return PAGE_KEEP;
344 }
345 if (mapping->a_ops->writepage == NULL)
346 return PAGE_ACTIVATE;
347 if (!may_write_to_queue(mapping->backing_dev_info))
348 return PAGE_KEEP;
349
350 if (clear_page_dirty_for_io(page)) {
351 int res;
352 struct writeback_control wbc = {
353 .sync_mode = WB_SYNC_NONE,
354 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700355 .range_start = 0,
356 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 .nonblocking = 1,
358 .for_reclaim = 1,
359 };
360
361 SetPageReclaim(page);
362 res = mapping->a_ops->writepage(page, &wbc);
363 if (res < 0)
364 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800365 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 ClearPageReclaim(page);
367 return PAGE_ACTIVATE;
368 }
369 if (!PageWriteback(page)) {
370 /* synchronous write or broken a_ops? */
371 ClearPageReclaim(page);
372 }
373
374 return PAGE_SUCCESS;
375 }
376
377 return PAGE_CLEAN;
378}
379
Christoph Lameterb20a3502006-03-22 00:09:12 -0800380int remove_mapping(struct address_space *mapping, struct page *page)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800381{
382 if (!mapping)
383 return 0; /* truncate got there first */
384
385 write_lock_irq(&mapping->tree_lock);
386
387 /*
388 * The non-racy check for busy page. It is critical to check
389 * PageDirty _after_ making sure that the page is freeable and
390 * not in use by anybody. (pagecache + us == 2)
391 */
392 if (unlikely(page_count(page) != 2))
393 goto cannot_free;
394 smp_rmb();
395 if (unlikely(PageDirty(page)))
396 goto cannot_free;
397
398 if (PageSwapCache(page)) {
399 swp_entry_t swap = { .val = page_private(page) };
400 __delete_from_swap_cache(page);
401 write_unlock_irq(&mapping->tree_lock);
402 swap_free(swap);
403 __put_page(page); /* The pagecache ref */
404 return 1;
405 }
406
407 __remove_from_page_cache(page);
408 write_unlock_irq(&mapping->tree_lock);
409 __put_page(page);
410 return 1;
411
412cannot_free:
413 write_unlock_irq(&mapping->tree_lock);
414 return 0;
415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/*
Andrew Morton1742f192006-03-22 00:08:21 -0800418 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
Andrew Morton1742f192006-03-22 00:08:21 -0800420static unsigned long shrink_page_list(struct list_head *page_list,
421 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 LIST_HEAD(ret_pages);
424 struct pagevec freed_pvec;
425 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800426 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 cond_resched();
429
430 pagevec_init(&freed_pvec, 1);
431 while (!list_empty(page_list)) {
432 struct address_space *mapping;
433 struct page *page;
434 int may_enter_fs;
435 int referenced;
436
437 cond_resched();
438
439 page = lru_to_page(page_list);
440 list_del(&page->lru);
441
442 if (TestSetPageLocked(page))
443 goto keep;
444
445 BUG_ON(PageActive(page));
446
447 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800448
449 if (!sc->may_swap && page_mapped(page))
450 goto keep_locked;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* Double the slab pressure for mapped and swapcache pages */
453 if (page_mapped(page) || PageSwapCache(page))
454 sc->nr_scanned++;
455
456 if (PageWriteback(page))
457 goto keep_locked;
458
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800459 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* In active use or really unfreeable? Activate it. */
461 if (referenced && page_mapping_inuse(page))
462 goto activate_locked;
463
464#ifdef CONFIG_SWAP
465 /*
466 * Anonymous process memory has backing store?
467 * Try to allocate it some swap space here.
468 */
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800469 if (PageAnon(page) && !PageSwapCache(page))
Christoph Lameter1480a542006-01-08 01:00:53 -0800470 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#endif /* CONFIG_SWAP */
473
474 mapping = page_mapping(page);
475 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
476 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
477
478 /*
479 * The page is mapped into the page tables of one or more
480 * processes. Try to unmap it here.
481 */
482 if (page_mapped(page) && mapping) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800483 switch (try_to_unmap(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 case SWAP_FAIL:
485 goto activate_locked;
486 case SWAP_AGAIN:
487 goto keep_locked;
488 case SWAP_SUCCESS:
489 ; /* try to free the page below */
490 }
491 }
492
493 if (PageDirty(page)) {
494 if (referenced)
495 goto keep_locked;
496 if (!may_enter_fs)
497 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800498 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 goto keep_locked;
500
501 /* Page is dirty, try to write it out here */
502 switch(pageout(page, mapping)) {
503 case PAGE_KEEP:
504 goto keep_locked;
505 case PAGE_ACTIVATE:
506 goto activate_locked;
507 case PAGE_SUCCESS:
508 if (PageWriteback(page) || PageDirty(page))
509 goto keep;
510 /*
511 * A synchronous write - probably a ramdisk. Go
512 * ahead and try to reclaim the page.
513 */
514 if (TestSetPageLocked(page))
515 goto keep;
516 if (PageDirty(page) || PageWriteback(page))
517 goto keep_locked;
518 mapping = page_mapping(page);
519 case PAGE_CLEAN:
520 ; /* try to free the page below */
521 }
522 }
523
524 /*
525 * If the page has buffers, try to free the buffer mappings
526 * associated with this page. If we succeed we try to free
527 * the page as well.
528 *
529 * We do this even if the page is PageDirty().
530 * try_to_release_page() does not perform I/O, but it is
531 * possible for a page to have PageDirty set, but it is actually
532 * clean (all its buffers are clean). This happens if the
533 * buffers were written out directly, with submit_bh(). ext3
534 * will do this, as well as the blockdev mapping.
535 * try_to_release_page() will discover that cleanness and will
536 * drop the buffers and mark the page clean - it can be freed.
537 *
538 * Rarely, pages can have buffers and no ->mapping. These are
539 * the pages which were not successfully invalidated in
540 * truncate_complete_page(). We try to drop those buffers here
541 * and if that worked, and the page is no longer mapped into
542 * process address space (page_count == 1) it can be freed.
543 * Otherwise, leave the page on the LRU so it is swappable.
544 */
545 if (PagePrivate(page)) {
546 if (!try_to_release_page(page, sc->gfp_mask))
547 goto activate_locked;
548 if (!mapping && page_count(page) == 1)
549 goto free_it;
550 }
551
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800552 if (!remove_mapping(mapping, page))
553 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555free_it:
556 unlock_page(page);
Andrew Morton05ff5132006-03-22 00:08:20 -0800557 nr_reclaimed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (!pagevec_add(&freed_pvec, page))
559 __pagevec_release_nonlru(&freed_pvec);
560 continue;
561
562activate_locked:
563 SetPageActive(page);
564 pgactivate++;
565keep_locked:
566 unlock_page(page);
567keep:
568 list_add(&page->lru, &ret_pages);
569 BUG_ON(PageLRU(page));
570 }
571 list_splice(&ret_pages, page_list);
572 if (pagevec_count(&freed_pvec))
573 __pagevec_release_nonlru(&freed_pvec);
574 mod_page_state(pgactivate, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800575 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800578/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * zone->lru_lock is heavily contended. Some of the functions that
580 * shrink the lists perform better by taking out a batch of pages
581 * and working on them outside the LRU lock.
582 *
583 * For pagecache intensive workloads, this function is the hottest
584 * spot in the kernel (apart from copy_*_user functions).
585 *
586 * Appropriate locks must be held before calling this function.
587 *
588 * @nr_to_scan: The number of pages to look through on the list.
589 * @src: The LRU list to pull pages off.
590 * @dst: The temp list to put pages on to.
591 * @scanned: The number of pages that were scanned.
592 *
593 * returns how many pages were moved onto *@dst.
594 */
Andrew Morton69e05942006-03-22 00:08:19 -0800595static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
596 struct list_head *src, struct list_head *dst,
597 unsigned long *scanned)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Andrew Morton69e05942006-03-22 00:08:19 -0800599 unsigned long nr_taken = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 struct page *page;
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800601 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800603 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800604 struct list_head *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 page = lru_to_page(src);
606 prefetchw_prev_lru_page(page, src, flags);
607
Nick Piggin8d438f92006-03-22 00:07:59 -0800608 BUG_ON(!PageLRU(page));
609
Nick Piggin053837f2006-01-18 17:42:27 -0800610 list_del(&page->lru);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800611 target = src;
612 if (likely(get_page_unless_zero(page))) {
Nick Piggin053837f2006-01-18 17:42:27 -0800613 /*
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800614 * Be careful not to clear PageLRU until after we're
615 * sure the page is not being freed elsewhere -- the
616 * page release code relies on it.
Nick Piggin053837f2006-01-18 17:42:27 -0800617 */
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800618 ClearPageLRU(page);
619 target = dst;
620 nr_taken++;
621 } /* else it is being freed elsewhere */
Nick Piggin46453a62006-03-22 00:07:58 -0800622
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800623 list_add(&page->lru, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625
626 *scanned = scan;
627 return nr_taken;
628}
629
630/*
Andrew Morton1742f192006-03-22 00:08:21 -0800631 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
632 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Andrew Morton1742f192006-03-22 00:08:21 -0800634static unsigned long shrink_inactive_list(unsigned long max_scan,
635 struct zone *zone, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 LIST_HEAD(page_list);
638 struct pagevec pvec;
Andrew Morton69e05942006-03-22 00:08:19 -0800639 unsigned long nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800640 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 pagevec_init(&pvec, 1);
643
644 lru_add_drain();
645 spin_lock_irq(&zone->lru_lock);
Andrew Morton69e05942006-03-22 00:08:19 -0800646 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -0800648 unsigned long nr_taken;
649 unsigned long nr_scan;
650 unsigned long nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
653 &zone->inactive_list,
654 &page_list, &nr_scan);
655 zone->nr_inactive -= nr_taken;
656 zone->pages_scanned += nr_scan;
657 spin_unlock_irq(&zone->lru_lock);
658
Andrew Morton69e05942006-03-22 00:08:19 -0800659 nr_scanned += nr_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800660 nr_freed = shrink_page_list(&page_list, sc);
Andrew Morton05ff5132006-03-22 00:08:20 -0800661 nr_reclaimed += nr_freed;
Nick Piggina74609f2006-01-06 00:11:20 -0800662 local_irq_disable();
663 if (current_is_kswapd()) {
664 __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
665 __mod_page_state(kswapd_steal, nr_freed);
666 } else
667 __mod_page_state_zone(zone, pgscan_direct, nr_scan);
668 __mod_page_state_zone(zone, pgsteal, nr_freed);
669
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800670 if (nr_taken == 0)
671 goto done;
672
Nick Piggina74609f2006-01-06 00:11:20 -0800673 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /*
675 * Put back any unfreeable pages.
676 */
677 while (!list_empty(&page_list)) {
678 page = lru_to_page(&page_list);
Nick Piggin8d438f92006-03-22 00:07:59 -0800679 BUG_ON(PageLRU(page));
680 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 list_del(&page->lru);
682 if (PageActive(page))
683 add_page_to_active_list(zone, page);
684 else
685 add_page_to_inactive_list(zone, page);
686 if (!pagevec_add(&pvec, page)) {
687 spin_unlock_irq(&zone->lru_lock);
688 __pagevec_release(&pvec);
689 spin_lock_irq(&zone->lru_lock);
690 }
691 }
Andrew Morton69e05942006-03-22 00:08:19 -0800692 } while (nr_scanned < max_scan);
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800693 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694done:
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800695 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 pagevec_release(&pvec);
Andrew Morton05ff5132006-03-22 00:08:20 -0800697 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700/*
701 * This moves pages from the active list to the inactive list.
702 *
703 * We move them the other way if the page is referenced by one or more
704 * processes, from rmap.
705 *
706 * If the pages are mostly unmapped, the processing is fast and it is
707 * appropriate to hold zone->lru_lock across the whole operation. But if
708 * the pages are mapped, the processing is slow (page_referenced()) so we
709 * should drop zone->lru_lock around each page. It's impossible to balance
710 * this, so instead we remove the pages from the LRU while processing them.
711 * It is safe to rely on PG_active against the non-LRU pages in here because
712 * nobody will play with that bit on a non-LRU page.
713 *
714 * The downside is that we have to touch page->_count against each page.
715 * But we had to alter page->flags anyway.
716 */
Andrew Morton1742f192006-03-22 00:08:21 -0800717static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
718 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Andrew Morton69e05942006-03-22 00:08:19 -0800720 unsigned long pgmoved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 int pgdeactivate = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800722 unsigned long pgscanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 LIST_HEAD(l_hold); /* The pages which were snipped off */
724 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
725 LIST_HEAD(l_active); /* Pages to go onto the active_list */
726 struct page *page;
727 struct pagevec pvec;
728 int reclaim_mapped = 0;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800729
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800730 if (sc->may_swap) {
Christoph Lameter2903fb12006-02-11 17:55:55 -0800731 long mapped_ratio;
732 long distress;
733 long swap_tendency;
734
735 /*
736 * `distress' is a measure of how much trouble we're having
737 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
738 */
739 distress = 100 >> zone->prev_priority;
740
741 /*
742 * The point of this algorithm is to decide when to start
743 * reclaiming mapped memory instead of just pagecache. Work out
744 * how much memory
745 * is mapped.
746 */
Andrew Mortonbd1e22b2006-06-23 02:03:47 -0700747 mapped_ratio = (sc->nr_mapped * 100) / vm_total_pages;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800748
749 /*
750 * Now decide how much we really want to unmap some pages. The
751 * mapped ratio is downgraded - just because there's a lot of
752 * mapped memory doesn't necessarily mean that page reclaim
753 * isn't succeeding.
754 *
755 * The distress ratio is important - we don't want to start
756 * going oom.
757 *
758 * A 100% value of vm_swappiness overrides this algorithm
759 * altogether.
760 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700761 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800762
763 /*
764 * Now use this metric to decide whether to start moving mapped
765 * memory onto the inactive list.
766 */
767 if (swap_tendency >= 100)
768 reclaim_mapped = 1;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 lru_add_drain();
772 spin_lock_irq(&zone->lru_lock);
773 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
774 &l_hold, &pgscanned);
775 zone->pages_scanned += pgscanned;
776 zone->nr_active -= pgmoved;
777 spin_unlock_irq(&zone->lru_lock);
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 while (!list_empty(&l_hold)) {
780 cond_resched();
781 page = lru_to_page(&l_hold);
782 list_del(&page->lru);
783 if (page_mapped(page)) {
784 if (!reclaim_mapped ||
785 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800786 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 list_add(&page->lru, &l_active);
788 continue;
789 }
790 }
791 list_add(&page->lru, &l_inactive);
792 }
793
794 pagevec_init(&pvec, 1);
795 pgmoved = 0;
796 spin_lock_irq(&zone->lru_lock);
797 while (!list_empty(&l_inactive)) {
798 page = lru_to_page(&l_inactive);
799 prefetchw_prev_lru_page(page, &l_inactive, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -0800800 BUG_ON(PageLRU(page));
801 SetPageLRU(page);
Nick Piggin4c84cac2006-03-22 00:08:00 -0800802 BUG_ON(!PageActive(page));
803 ClearPageActive(page);
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 list_move(&page->lru, &zone->inactive_list);
806 pgmoved++;
807 if (!pagevec_add(&pvec, page)) {
808 zone->nr_inactive += pgmoved;
809 spin_unlock_irq(&zone->lru_lock);
810 pgdeactivate += pgmoved;
811 pgmoved = 0;
812 if (buffer_heads_over_limit)
813 pagevec_strip(&pvec);
814 __pagevec_release(&pvec);
815 spin_lock_irq(&zone->lru_lock);
816 }
817 }
818 zone->nr_inactive += pgmoved;
819 pgdeactivate += pgmoved;
820 if (buffer_heads_over_limit) {
821 spin_unlock_irq(&zone->lru_lock);
822 pagevec_strip(&pvec);
823 spin_lock_irq(&zone->lru_lock);
824 }
825
826 pgmoved = 0;
827 while (!list_empty(&l_active)) {
828 page = lru_to_page(&l_active);
829 prefetchw_prev_lru_page(page, &l_active, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -0800830 BUG_ON(PageLRU(page));
831 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 BUG_ON(!PageActive(page));
833 list_move(&page->lru, &zone->active_list);
834 pgmoved++;
835 if (!pagevec_add(&pvec, page)) {
836 zone->nr_active += pgmoved;
837 pgmoved = 0;
838 spin_unlock_irq(&zone->lru_lock);
839 __pagevec_release(&pvec);
840 spin_lock_irq(&zone->lru_lock);
841 }
842 }
843 zone->nr_active += pgmoved;
Nick Piggina74609f2006-01-06 00:11:20 -0800844 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Nick Piggina74609f2006-01-06 00:11:20 -0800846 __mod_page_state_zone(zone, pgrefill, pgscanned);
847 __mod_page_state(pgdeactivate, pgdeactivate);
848 local_irq_enable();
849
850 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853/*
854 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
855 */
Andrew Morton05ff5132006-03-22 00:08:20 -0800856static unsigned long shrink_zone(int priority, struct zone *zone,
857 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 unsigned long nr_active;
860 unsigned long nr_inactive;
Christoph Lameter86959492006-03-22 00:08:18 -0800861 unsigned long nr_to_scan;
Andrew Morton05ff5132006-03-22 00:08:20 -0800862 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Martin Hicks53e9a612005-09-03 15:54:51 -0700864 atomic_inc(&zone->reclaim_in_progress);
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 /*
867 * Add one to `nr_to_scan' just to make sure that the kernel will
868 * slowly sift through the active list.
869 */
Christoph Lameter86959492006-03-22 00:08:18 -0800870 zone->nr_scan_active += (zone->nr_active >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 nr_active = zone->nr_scan_active;
872 if (nr_active >= sc->swap_cluster_max)
873 zone->nr_scan_active = 0;
874 else
875 nr_active = 0;
876
Christoph Lameter86959492006-03-22 00:08:18 -0800877 zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 nr_inactive = zone->nr_scan_inactive;
879 if (nr_inactive >= sc->swap_cluster_max)
880 zone->nr_scan_inactive = 0;
881 else
882 nr_inactive = 0;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 while (nr_active || nr_inactive) {
885 if (nr_active) {
Christoph Lameter86959492006-03-22 00:08:18 -0800886 nr_to_scan = min(nr_active,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -0800888 nr_active -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800889 shrink_active_list(nr_to_scan, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
892 if (nr_inactive) {
Christoph Lameter86959492006-03-22 00:08:18 -0800893 nr_to_scan = min(nr_inactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -0800895 nr_inactive -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800896 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
897 sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899 }
900
901 throttle_vm_writeout();
Martin Hicks53e9a612005-09-03 15:54:51 -0700902
903 atomic_dec(&zone->reclaim_in_progress);
Andrew Morton05ff5132006-03-22 00:08:20 -0800904 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
907/*
908 * This is the direct reclaim path, for page-allocating processes. We only
909 * try to reclaim pages from zones which will satisfy the caller's allocation
910 * request.
911 *
912 * We reclaim from a zone even if that zone is over pages_high. Because:
913 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
914 * allocation or
915 * b) The zones may be over pages_high but they must go *over* pages_high to
916 * satisfy the `incremental min' zone defense algorithm.
917 *
918 * Returns the number of reclaimed pages.
919 *
920 * If a zone is deemed to be full of pinned pages then just give it a light
921 * scan then give up on it.
922 */
Andrew Morton1742f192006-03-22 00:08:21 -0800923static unsigned long shrink_zones(int priority, struct zone **zones,
Andrew Morton05ff5132006-03-22 00:08:20 -0800924 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Andrew Morton05ff5132006-03-22 00:08:20 -0800926 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int i;
928
929 for (i = 0; zones[i] != NULL; i++) {
930 struct zone *zone = zones[i];
931
Con Kolivasf3fe6512006-01-06 00:11:15 -0800932 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 continue;
934
Paul Jackson9bf22292005-09-06 15:18:12 -0700935 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 continue;
937
Christoph Lameter86959492006-03-22 00:08:18 -0800938 zone->temp_priority = priority;
939 if (zone->prev_priority > priority)
940 zone->prev_priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Christoph Lameter86959492006-03-22 00:08:18 -0800942 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 continue; /* Let kswapd poll it */
944
Andrew Morton05ff5132006-03-22 00:08:20 -0800945 nr_reclaimed += shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Andrew Morton05ff5132006-03-22 00:08:20 -0800947 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
950/*
951 * This is the main entry point to direct page reclaim.
952 *
953 * If a full scan of the inactive list fails to free enough memory then we
954 * are "out of memory" and something needs to be killed.
955 *
956 * If the caller is !__GFP_FS then the probability of a failure is reasonably
957 * high - the zone may be full of dirty or under-writeback pages, which this
958 * caller can't do much about. We kick pdflush and take explicit naps in the
959 * hope that some of these pages can be written. But if the allocating task
960 * holds filesystem locks which prevent writeout this might not work, and the
961 * allocation attempt will fail.
962 */
Andrew Morton69e05942006-03-22 00:08:19 -0800963unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 int priority;
966 int ret = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800967 unsigned long total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800968 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 struct reclaim_state *reclaim_state = current->reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 unsigned long lru_pages = 0;
971 int i;
Andrew Morton179e9632006-03-22 00:08:18 -0800972 struct scan_control sc = {
973 .gfp_mask = gfp_mask,
974 .may_writepage = !laptop_mode,
975 .swap_cluster_max = SWAP_CLUSTER_MAX,
976 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700977 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -0800978 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 inc_page_state(allocstall);
981
982 for (i = 0; zones[i] != NULL; i++) {
983 struct zone *zone = zones[i];
984
Paul Jackson9bf22292005-09-06 15:18:12 -0700985 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 continue;
987
988 zone->temp_priority = DEF_PRIORITY;
989 lru_pages += zone->nr_active + zone->nr_inactive;
990 }
991
992 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
993 sc.nr_mapped = read_page_state(nr_mapped);
994 sc.nr_scanned = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800995 if (!priority)
996 disable_swap_token();
Andrew Morton1742f192006-03-22 00:08:21 -0800997 nr_reclaimed += shrink_zones(priority, zones, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
999 if (reclaim_state) {
Andrew Morton05ff5132006-03-22 00:08:20 -08001000 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 reclaim_state->reclaimed_slab = 0;
1002 }
1003 total_scanned += sc.nr_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001004 if (nr_reclaimed >= sc.swap_cluster_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 ret = 1;
1006 goto out;
1007 }
1008
1009 /*
1010 * Try to write back as many pages as we just scanned. This
1011 * tends to cause slow streaming writers to write data to the
1012 * disk smoothly, at the dirtying rate, which is nice. But
1013 * that's undesirable in laptop mode, where we *want* lumpy
1014 * writeout. So in laptop mode, write out the whole world.
1015 */
Andrew Morton179e9632006-03-22 00:08:18 -08001016 if (total_scanned > sc.swap_cluster_max +
1017 sc.swap_cluster_max / 2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001018 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 sc.may_writepage = 1;
1020 }
1021
1022 /* Take a nap, wait for some writeback to complete */
1023 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1024 blk_congestion_wait(WRITE, HZ/10);
1025 }
1026out:
1027 for (i = 0; zones[i] != 0; i++) {
1028 struct zone *zone = zones[i];
1029
Paul Jackson9bf22292005-09-06 15:18:12 -07001030 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 continue;
1032
1033 zone->prev_priority = zone->temp_priority;
1034 }
1035 return ret;
1036}
1037
1038/*
1039 * For kswapd, balance_pgdat() will work across all this node's zones until
1040 * they are all at pages_high.
1041 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 * Returns the number of pages which were actually freed.
1043 *
1044 * There is special handling here for zones which are full of pinned pages.
1045 * This can happen if the pages are all mlocked, or if they are all used by
1046 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1047 * What we do is to detect the case where all pages in the zone have been
1048 * scanned twice and there has been zero successful reclaim. Mark the zone as
1049 * dead and from now on, only perform a short scan. Basically we're polling
1050 * the zone for when the problem goes away.
1051 *
1052 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1053 * zones which have free_pages > pages_high, but once a zone is found to have
1054 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1055 * of the number of free pages in the lower zones. This interoperates with
1056 * the page allocator fallback scheme to ensure that aging of pages is balanced
1057 * across the zones.
1058 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001059static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 int all_zones_ok;
1062 int priority;
1063 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08001064 unsigned long total_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001065 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08001067 struct scan_control sc = {
1068 .gfp_mask = GFP_KERNEL,
1069 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001070 .swap_cluster_max = SWAP_CLUSTER_MAX,
1071 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -08001072 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074loop_again:
1075 total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001076 nr_reclaimed = 0;
Christoph Lameterc0bbbc72006-06-11 15:22:26 -07001077 sc.may_writepage = !laptop_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 sc.nr_mapped = read_page_state(nr_mapped);
1079
1080 inc_page_state(pageoutrun);
1081
1082 for (i = 0; i < pgdat->nr_zones; i++) {
1083 struct zone *zone = pgdat->node_zones + i;
1084
1085 zone->temp_priority = DEF_PRIORITY;
1086 }
1087
1088 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1089 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1090 unsigned long lru_pages = 0;
1091
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001092 /* The swap token gets in the way of swapout... */
1093 if (!priority)
1094 disable_swap_token();
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 all_zones_ok = 1;
1097
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001098 /*
1099 * Scan in the highmem->dma direction for the highest
1100 * zone which needs scanning
1101 */
1102 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1103 struct zone *zone = pgdat->node_zones + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001105 if (!populated_zone(zone))
1106 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001108 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1109 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001111 if (!zone_watermark_ok(zone, order, zone->pages_high,
1112 0, 0)) {
1113 end_zone = i;
1114 goto scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001117 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118scan:
1119 for (i = 0; i <= end_zone; i++) {
1120 struct zone *zone = pgdat->node_zones + i;
1121
1122 lru_pages += zone->nr_active + zone->nr_inactive;
1123 }
1124
1125 /*
1126 * Now scan the zone in the dma->highmem direction, stopping
1127 * at the last zone which needs scanning.
1128 *
1129 * We do this because the page allocator works in the opposite
1130 * direction. This prevents the page allocator from allocating
1131 * pages behind kswapd's direction of progress, which would
1132 * cause too much scanning of the lower zones.
1133 */
1134 for (i = 0; i <= end_zone; i++) {
1135 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001136 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Con Kolivasf3fe6512006-01-06 00:11:15 -08001138 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 continue;
1140
1141 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1142 continue;
1143
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001144 if (!zone_watermark_ok(zone, order, zone->pages_high,
1145 end_zone, 0))
1146 all_zones_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 zone->temp_priority = priority;
1148 if (zone->prev_priority > priority)
1149 zone->prev_priority = priority;
1150 sc.nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001151 nr_reclaimed += shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001153 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1154 lru_pages);
Andrew Morton05ff5132006-03-22 00:08:20 -08001155 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 total_scanned += sc.nr_scanned;
1157 if (zone->all_unreclaimable)
1158 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001159 if (nr_slab == 0 && zone->pages_scanned >=
1160 (zone->nr_active + zone->nr_inactive) * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 zone->all_unreclaimable = 1;
1162 /*
1163 * If we've done a decent amount of scanning and
1164 * the reclaim ratio is low, start doing writepage
1165 * even in laptop mode
1166 */
1167 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Andrew Morton05ff5132006-03-22 00:08:20 -08001168 total_scanned > nr_reclaimed + nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 sc.may_writepage = 1;
1170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (all_zones_ok)
1172 break; /* kswapd: all done */
1173 /*
1174 * OK, kswapd is getting into trouble. Take a nap, then take
1175 * another pass across the zones.
1176 */
1177 if (total_scanned && priority < DEF_PRIORITY - 2)
1178 blk_congestion_wait(WRITE, HZ/10);
1179
1180 /*
1181 * We do this so kswapd doesn't build up large priorities for
1182 * example when it is freeing in parallel with allocators. It
1183 * matches the direct reclaim path behaviour in terms of impact
1184 * on zone->*_priority.
1185 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001186 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 break;
1188 }
1189out:
1190 for (i = 0; i < pgdat->nr_zones; i++) {
1191 struct zone *zone = pgdat->node_zones + i;
1192
1193 zone->prev_priority = zone->temp_priority;
1194 }
1195 if (!all_zones_ok) {
1196 cond_resched();
1197 goto loop_again;
1198 }
1199
Andrew Morton05ff5132006-03-22 00:08:20 -08001200 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203/*
1204 * The background pageout daemon, started as a kernel thread
1205 * from the init process.
1206 *
1207 * This basically trickles out pages so that we have _some_
1208 * free memory available even if there is no other activity
1209 * that frees anything up. This is needed for things like routing
1210 * etc, where we otherwise might have all activity going on in
1211 * asynchronous contexts that cannot page things out.
1212 *
1213 * If there are applications that are active memory-allocators
1214 * (most normal use), this basically shouldn't matter.
1215 */
1216static int kswapd(void *p)
1217{
1218 unsigned long order;
1219 pg_data_t *pgdat = (pg_data_t*)p;
1220 struct task_struct *tsk = current;
1221 DEFINE_WAIT(wait);
1222 struct reclaim_state reclaim_state = {
1223 .reclaimed_slab = 0,
1224 };
1225 cpumask_t cpumask;
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 cpumask = node_to_cpumask(pgdat->node_id);
1228 if (!cpus_empty(cpumask))
1229 set_cpus_allowed(tsk, cpumask);
1230 current->reclaim_state = &reclaim_state;
1231
1232 /*
1233 * Tell the memory management that we're a "memory allocator",
1234 * and that if we need more memory we should get access to it
1235 * regardless (see "__alloc_pages()"). "kswapd" should
1236 * never get caught in the normal page freeing logic.
1237 *
1238 * (Kswapd normally doesn't need memory anyway, but sometimes
1239 * you need a small amount of memory in order to be able to
1240 * page out something else, and this flag essentially protects
1241 * us from recursively trying to free more memory as we're
1242 * trying to free the first piece of memory in the first place).
1243 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001244 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 order = 0;
1247 for ( ; ; ) {
1248 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001249
1250 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1253 new_order = pgdat->kswapd_max_order;
1254 pgdat->kswapd_max_order = 0;
1255 if (order < new_order) {
1256 /*
1257 * Don't sleep if someone wants a larger 'order'
1258 * allocation
1259 */
1260 order = new_order;
1261 } else {
1262 schedule();
1263 order = pgdat->kswapd_max_order;
1264 }
1265 finish_wait(&pgdat->kswapd_wait, &wait);
1266
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001267 balance_pgdat(pgdat, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 return 0;
1270}
1271
1272/*
1273 * A zone is low on free memory, so wake its kswapd task to service it.
1274 */
1275void wakeup_kswapd(struct zone *zone, int order)
1276{
1277 pg_data_t *pgdat;
1278
Con Kolivasf3fe6512006-01-06 00:11:15 -08001279 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return;
1281
1282 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001283 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return;
1285 if (pgdat->kswapd_max_order < order)
1286 pgdat->kswapd_max_order = order;
Paul Jackson9bf22292005-09-06 15:18:12 -07001287 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001289 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001291 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294#ifdef CONFIG_PM
1295/*
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001296 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1297 * from LRU lists system-wide, for given pass and priority, and returns the
1298 * number of reclaimed pages
1299 *
1300 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1301 */
1302static unsigned long shrink_all_zones(unsigned long nr_pages, int pass,
1303 int prio, struct scan_control *sc)
1304{
1305 struct zone *zone;
1306 unsigned long nr_to_scan, ret = 0;
1307
1308 for_each_zone(zone) {
1309
1310 if (!populated_zone(zone))
1311 continue;
1312
1313 if (zone->all_unreclaimable && prio != DEF_PRIORITY)
1314 continue;
1315
1316 /* For pass = 0 we don't shrink the active list */
1317 if (pass > 0) {
1318 zone->nr_scan_active += (zone->nr_active >> prio) + 1;
1319 if (zone->nr_scan_active >= nr_pages || pass > 3) {
1320 zone->nr_scan_active = 0;
1321 nr_to_scan = min(nr_pages, zone->nr_active);
1322 shrink_active_list(nr_to_scan, zone, sc);
1323 }
1324 }
1325
1326 zone->nr_scan_inactive += (zone->nr_inactive >> prio) + 1;
1327 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1328 zone->nr_scan_inactive = 0;
1329 nr_to_scan = min(nr_pages, zone->nr_inactive);
1330 ret += shrink_inactive_list(nr_to_scan, zone, sc);
1331 if (ret >= nr_pages)
1332 return ret;
1333 }
1334 }
1335
1336 return ret;
1337}
1338
1339/*
1340 * Try to free `nr_pages' of memory, system-wide, and return the number of
1341 * freed pages.
1342 *
1343 * Rather than trying to age LRUs the aim is to preserve the overall
1344 * LRU order by reclaiming preferentially
1345 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 */
Andrew Morton69e05942006-03-22 00:08:19 -08001347unsigned long shrink_all_memory(unsigned long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001349 unsigned long lru_pages, nr_slab;
Andrew Morton69e05942006-03-22 00:08:19 -08001350 unsigned long ret = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001351 int pass;
1352 struct reclaim_state reclaim_state;
1353 struct zone *zone;
1354 struct scan_control sc = {
1355 .gfp_mask = GFP_KERNEL,
1356 .may_swap = 0,
1357 .swap_cluster_max = nr_pages,
1358 .may_writepage = 1,
1359 .swappiness = vm_swappiness,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 };
1361
1362 current->reclaim_state = &reclaim_state;
Andrew Morton69e05942006-03-22 00:08:19 -08001363
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001364 lru_pages = 0;
1365 for_each_zone(zone)
1366 lru_pages += zone->nr_active + zone->nr_inactive;
1367
1368 nr_slab = read_page_state(nr_slab);
1369 /* If slab caches are huge, it's better to hit them first */
1370 while (nr_slab >= lru_pages) {
1371 reclaim_state.reclaimed_slab = 0;
1372 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1373 if (!reclaim_state.reclaimed_slab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 break;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001375
1376 ret += reclaim_state.reclaimed_slab;
1377 if (ret >= nr_pages)
1378 goto out;
1379
1380 nr_slab -= reclaim_state.reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001382
1383 /*
1384 * We try to shrink LRUs in 5 passes:
1385 * 0 = Reclaim from inactive_list only
1386 * 1 = Reclaim from active list but don't reclaim mapped
1387 * 2 = 2nd pass of type 1
1388 * 3 = Reclaim mapped (normal reclaim)
1389 * 4 = 2nd pass of type 3
1390 */
1391 for (pass = 0; pass < 5; pass++) {
1392 int prio;
1393
1394 /* Needed for shrinking slab caches later on */
1395 if (!lru_pages)
1396 for_each_zone(zone) {
1397 lru_pages += zone->nr_active;
1398 lru_pages += zone->nr_inactive;
1399 }
1400
1401 /* Force reclaiming mapped pages in the passes #3 and #4 */
1402 if (pass > 2) {
1403 sc.may_swap = 1;
1404 sc.swappiness = 100;
1405 }
1406
1407 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1408 unsigned long nr_to_scan = nr_pages - ret;
1409
1410 sc.nr_mapped = read_page_state(nr_mapped);
1411 sc.nr_scanned = 0;
1412
1413 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1414 if (ret >= nr_pages)
1415 goto out;
1416
1417 reclaim_state.reclaimed_slab = 0;
1418 shrink_slab(sc.nr_scanned, sc.gfp_mask, lru_pages);
1419 ret += reclaim_state.reclaimed_slab;
1420 if (ret >= nr_pages)
1421 goto out;
1422
1423 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
1424 blk_congestion_wait(WRITE, HZ / 10);
1425 }
1426
1427 lru_pages = 0;
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001428 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001429
1430 /*
1431 * If ret = 0, we could not shrink LRUs, but there may be something
1432 * in slab caches
1433 */
1434 if (!ret)
1435 do {
1436 reclaim_state.reclaimed_slab = 0;
1437 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1438 ret += reclaim_state.reclaimed_slab;
1439 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
1440
1441out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 current->reclaim_state = NULL;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return ret;
1445}
1446#endif
1447
1448#ifdef CONFIG_HOTPLUG_CPU
1449/* It's optimal to keep kswapds on the same CPUs as their memory, but
1450 not required for correctness. So if the last cpu in a node goes
1451 away, we get changed to run anywhere: as the first one comes back,
1452 restore their cpu bindings. */
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07001453static int __devinit cpu_callback(struct notifier_block *nfb,
Andrew Morton69e05942006-03-22 00:08:19 -08001454 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 pg_data_t *pgdat;
1457 cpumask_t mask;
1458
1459 if (action == CPU_ONLINE) {
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08001460 for_each_online_pgdat(pgdat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 mask = node_to_cpumask(pgdat->node_id);
1462 if (any_online_cpu(mask) != NR_CPUS)
1463 /* One of our CPUs online: restore mask */
1464 set_cpus_allowed(pgdat->kswapd, mask);
1465 }
1466 }
1467 return NOTIFY_OK;
1468}
1469#endif /* CONFIG_HOTPLUG_CPU */
1470
Yasunori Goto3218ae12006-06-27 02:53:33 -07001471/*
1472 * This kswapd start function will be called by init and node-hot-add.
1473 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
1474 */
1475int kswapd_run(int nid)
1476{
1477 pg_data_t *pgdat = NODE_DATA(nid);
1478 int ret = 0;
1479
1480 if (pgdat->kswapd)
1481 return 0;
1482
1483 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
1484 if (IS_ERR(pgdat->kswapd)) {
1485 /* failure at boot is fatal */
1486 BUG_ON(system_state == SYSTEM_BOOTING);
1487 printk("Failed to start kswapd on node %d\n",nid);
1488 ret = -1;
1489 }
1490 return ret;
1491}
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493static int __init kswapd_init(void)
1494{
Yasunori Goto3218ae12006-06-27 02:53:33 -07001495 int nid;
Andrew Morton69e05942006-03-22 00:08:19 -08001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 swap_setup();
Yasunori Goto3218ae12006-06-27 02:53:33 -07001498 for_each_online_node(nid)
1499 kswapd_run(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 hotcpu_notifier(cpu_callback, 0);
1501 return 0;
1502}
1503
1504module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001505
1506#ifdef CONFIG_NUMA
1507/*
1508 * Zone reclaim mode
1509 *
1510 * If non-zero call zone_reclaim when the number of free pages falls below
1511 * the watermarks.
1512 *
1513 * In the future we may add flags to the mode. However, the page allocator
1514 * should only have to check that zone_reclaim_mode != 0 before calling
1515 * zone_reclaim().
1516 */
1517int zone_reclaim_mode __read_mostly;
1518
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001519#define RECLAIM_OFF 0
1520#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1521#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1522#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001523#define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001524
Christoph Lameter9eeff232006-01-18 17:42:31 -08001525/*
1526 * Mininum time between zone reclaim scans
1527 */
Christoph Lameter2a11ff02006-02-01 03:05:33 -08001528int zone_reclaim_interval __read_mostly = 30*HZ;
Christoph Lametera92f7122006-02-01 03:05:32 -08001529
1530/*
1531 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1532 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1533 * a zone.
1534 */
1535#define ZONE_RECLAIM_PRIORITY 4
1536
Christoph Lameter9eeff232006-01-18 17:42:31 -08001537/*
1538 * Try to free up some pages from this zone through reclaim.
1539 */
Andrew Morton179e9632006-03-22 00:08:18 -08001540static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001541{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001542 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08001543 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001544 struct task_struct *p = current;
1545 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08001546 int priority;
Andrew Morton05ff5132006-03-22 00:08:20 -08001547 unsigned long nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001548 struct scan_control sc = {
1549 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1550 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
1551 .nr_mapped = read_page_state(nr_mapped),
Andrew Morton69e05942006-03-22 00:08:19 -08001552 .swap_cluster_max = max_t(unsigned long, nr_pages,
1553 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08001554 .gfp_mask = gfp_mask,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001555 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -08001556 };
Christoph Lameter9eeff232006-01-18 17:42:31 -08001557
1558 disable_swap_token();
Christoph Lameter9eeff232006-01-18 17:42:31 -08001559 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08001560 /*
1561 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1562 * and we also need to be able to write out pages for RECLAIM_WRITE
1563 * and RECLAIM_SWAP.
1564 */
1565 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001566 reclaim_state.reclaimed_slab = 0;
1567 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001568
Christoph Lametera92f7122006-02-01 03:05:32 -08001569 /*
1570 * Free memory by calling shrink zone with increasing priorities
1571 * until we have enough memory freed.
1572 */
Christoph Lameter86959492006-03-22 00:08:18 -08001573 priority = ZONE_RECLAIM_PRIORITY;
Christoph Lametera92f7122006-02-01 03:05:32 -08001574 do {
Andrew Morton05ff5132006-03-22 00:08:20 -08001575 nr_reclaimed += shrink_zone(priority, zone, &sc);
Christoph Lameter86959492006-03-22 00:08:18 -08001576 priority--;
Andrew Morton05ff5132006-03-22 00:08:20 -08001577 } while (priority >= 0 && nr_reclaimed < nr_pages);
Christoph Lameterc84db232006-02-01 03:05:29 -08001578
Andrew Morton05ff5132006-03-22 00:08:20 -08001579 if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001580 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001581 * shrink_slab() does not currently allow us to determine how
1582 * many pages were freed in this zone. So we just shake the slab
1583 * a bit and then go off node for this particular allocation
1584 * despite possibly having freed enough memory to allocate in
1585 * this zone. If we freed local memory then the next
1586 * allocations will be local again.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001587 *
1588 * shrink_slab will free memory on all zones and may take
1589 * a long time.
1590 */
1591 shrink_slab(sc.nr_scanned, gfp_mask, order);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001592 }
1593
Christoph Lameter9eeff232006-01-18 17:42:31 -08001594 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08001595 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Christoph Lameter9eeff232006-01-18 17:42:31 -08001596
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001597 if (nr_reclaimed == 0) {
1598 /*
1599 * We were unable to reclaim enough pages to stay on node. We
1600 * now allow off node accesses for a certain time period before
1601 * trying again to reclaim pages from the local zone.
1602 */
Christoph Lameter9eeff232006-01-18 17:42:31 -08001603 zone->last_unsuccessful_zone_reclaim = jiffies;
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001604 }
Christoph Lameter9eeff232006-01-18 17:42:31 -08001605
Andrew Morton05ff5132006-03-22 00:08:20 -08001606 return nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001607}
Andrew Morton179e9632006-03-22 00:08:18 -08001608
1609int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1610{
1611 cpumask_t mask;
1612 int node_id;
1613
1614 /*
1615 * Do not reclaim if there was a recent unsuccessful attempt at zone
1616 * reclaim. In that case we let allocations go off node for the
1617 * zone_reclaim_interval. Otherwise we would scan for each off-node
1618 * page allocation.
1619 */
1620 if (time_before(jiffies,
1621 zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval))
1622 return 0;
1623
1624 /*
1625 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1626 * not have reclaimable pages and if we should not delay the allocation
1627 * then do not scan.
1628 */
1629 if (!(gfp_mask & __GFP_WAIT) ||
1630 zone->all_unreclaimable ||
1631 atomic_read(&zone->reclaim_in_progress) > 0 ||
1632 (current->flags & PF_MEMALLOC))
1633 return 0;
1634
1635 /*
1636 * Only run zone reclaim on the local zone or on zones that do not
1637 * have associated processors. This will favor the local processor
1638 * over remote processors and spread off node memory allocations
1639 * as wide as possible.
1640 */
1641 node_id = zone->zone_pgdat->node_id;
1642 mask = node_to_cpumask(node_id);
1643 if (!cpus_empty(mask) && node_id != numa_node_id())
1644 return 0;
1645 return __zone_reclaim(zone, gfp_mask, order);
1646}
Christoph Lameter9eeff232006-01-18 17:42:31 -08001647#endif