blob: 71a02e2950379626001ea4d7d87f46a875ee7c45 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/vmscan.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 *
6 * Swap reorganised 29.12.95, Stephen Tweedie.
7 * kswapd added: 7.1.96 sct
8 * Removed kswapd_ctl limits, and swap out as many pages as needed
9 * to bring the system back to freepages.high: 2.4.97, Rik van Riel.
10 * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
11 * Multiqueue VM started 5.8.00, Rik van Riel.
12 */
13
14#include <linux/mm.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/kernel_stat.h>
18#include <linux/swap.h>
19#include <linux/pagemap.h>
20#include <linux/init.h>
21#include <linux/highmem.h>
22#include <linux/file.h>
23#include <linux/writeback.h>
24#include <linux/blkdev.h>
25#include <linux/buffer_head.h> /* for try_to_release_page(),
26 buffer_heads_over_limit */
27#include <linux/mm_inline.h>
28#include <linux/pagevec.h>
29#include <linux/backing-dev.h>
30#include <linux/rmap.h>
31#include <linux/topology.h>
32#include <linux/cpu.h>
33#include <linux/cpuset.h>
34#include <linux/notifier.h>
35#include <linux/rwsem.h>
Rafael J. Wysocki248a0302006-03-22 00:09:04 -080036#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/tlbflush.h>
39#include <asm/div64.h>
40
41#include <linux/swapops.h>
42
Nick Piggin0f8053a2006-03-22 00:08:33 -080043#include "internal.h"
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045struct scan_control {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 /* Incremented by the number of inactive pages that were scanned */
47 unsigned long nr_scanned;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 unsigned long nr_mapped; /* From page_state */
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040052 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 int may_writepage;
55
Christoph Lameterf1fd1062006-01-18 17:42:30 -080056 /* Can pages be swapped as part of reclaim? */
57 int may_swap;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
60 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
61 * In this context, it doesn't matter that we scan the
62 * whole list at once. */
63 int swap_cluster_max;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -070064
65 int swappiness;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
68/*
69 * The list of shrinker callbacks used by to apply pressure to
70 * ageable caches.
71 */
72struct shrinker {
73 shrinker_t shrinker;
74 struct list_head list;
75 int seeks; /* seeks to recreate an obj */
76 long nr; /* objs pending delete */
77};
78
79#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
80
81#ifdef ARCH_HAS_PREFETCH
82#define prefetch_prev_lru_page(_page, _base, _field) \
83 do { \
84 if ((_page)->lru.prev != _base) { \
85 struct page *prev; \
86 \
87 prev = lru_to_page(&(_page->lru)); \
88 prefetch(&prev->_field); \
89 } \
90 } while (0)
91#else
92#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
93#endif
94
95#ifdef ARCH_HAS_PREFETCHW
96#define prefetchw_prev_lru_page(_page, _base, _field) \
97 do { \
98 if ((_page)->lru.prev != _base) { \
99 struct page *prev; \
100 \
101 prev = lru_to_page(&(_page->lru)); \
102 prefetchw(&prev->_field); \
103 } \
104 } while (0)
105#else
106#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
107#endif
108
109/*
110 * From 0 .. 100. Higher means more swappy.
111 */
112int vm_swappiness = 60;
113static long total_memory;
114
115static LIST_HEAD(shrinker_list);
116static DECLARE_RWSEM(shrinker_rwsem);
117
118/*
119 * Add a shrinker callback to be called from the vm
120 */
121struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
122{
123 struct shrinker *shrinker;
124
125 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
126 if (shrinker) {
127 shrinker->shrinker = theshrinker;
128 shrinker->seeks = seeks;
129 shrinker->nr = 0;
130 down_write(&shrinker_rwsem);
131 list_add_tail(&shrinker->list, &shrinker_list);
132 up_write(&shrinker_rwsem);
133 }
134 return shrinker;
135}
136EXPORT_SYMBOL(set_shrinker);
137
138/*
139 * Remove one
140 */
141void remove_shrinker(struct shrinker *shrinker)
142{
143 down_write(&shrinker_rwsem);
144 list_del(&shrinker->list);
145 up_write(&shrinker_rwsem);
146 kfree(shrinker);
147}
148EXPORT_SYMBOL(remove_shrinker);
149
150#define SHRINK_BATCH 128
151/*
152 * Call the shrink functions to age shrinkable caches
153 *
154 * Here we assume it costs one seek to replace a lru page and that it also
155 * takes a seek to recreate a cache object. With this in mind we age equal
156 * percentages of the lru and ageable caches. This should balance the seeks
157 * generated by these structures.
158 *
159 * If the vm encounted mapped pages on the LRU it increase the pressure on
160 * slab to avoid swapping.
161 *
162 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
163 *
164 * `lru_pages' represents the number of on-LRU pages in all the zones which
165 * are eligible for the caller's allocation attempt. It is used for balancing
166 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700167 *
168 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Andrew Morton69e05942006-03-22 00:08:19 -0800170unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
171 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800174 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (scanned == 0)
177 scanned = SWAP_CLUSTER_MAX;
178
179 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700180 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 list_for_each_entry(shrinker, &shrinker_list, list) {
183 unsigned long long delta;
184 unsigned long total_scan;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800185 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800188 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 do_div(delta, lru_pages + 1);
190 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800191 if (shrinker->nr < 0) {
192 printk(KERN_ERR "%s: nr=%ld\n",
193 __FUNCTION__, shrinker->nr);
194 shrinker->nr = max_pass;
195 }
196
197 /*
198 * Avoid risking looping forever due to too large nr value:
199 * never try to free more than twice the estimate number of
200 * freeable entries.
201 */
202 if (shrinker->nr > max_pass * 2)
203 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 total_scan = shrinker->nr;
206 shrinker->nr = 0;
207
208 while (total_scan >= SHRINK_BATCH) {
209 long this_scan = SHRINK_BATCH;
210 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700211 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700213 nr_before = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
215 if (shrink_ret == -1)
216 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700217 if (shrink_ret < nr_before)
218 ret += nr_before - shrink_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 mod_page_state(slabs_scanned, this_scan);
220 total_scan -= this_scan;
221
222 cond_resched();
223 }
224
225 shrinker->nr += total_scan;
226 }
227 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700228 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231/* Called without lock on whether page is mapped, so answer is unstable */
232static inline int page_mapping_inuse(struct page *page)
233{
234 struct address_space *mapping;
235
236 /* Page is in somebody's page tables. */
237 if (page_mapped(page))
238 return 1;
239
240 /* Be more reluctant to reclaim swapcache than pagecache */
241 if (PageSwapCache(page))
242 return 1;
243
244 mapping = page_mapping(page);
245 if (!mapping)
246 return 0;
247
248 /* File is mmap'd by somebody? */
249 return mapping_mapped(mapping);
250}
251
252static inline int is_page_cache_freeable(struct page *page)
253{
254 return page_count(page) - !!PagePrivate(page) == 2;
255}
256
257static int may_write_to_queue(struct backing_dev_info *bdi)
258{
Christoph Lameter930d9152006-01-08 01:00:47 -0800259 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 1;
261 if (!bdi_write_congested(bdi))
262 return 1;
263 if (bdi == current->backing_dev_info)
264 return 1;
265 return 0;
266}
267
268/*
269 * We detected a synchronous write error writing a page out. Probably
270 * -ENOSPC. We need to propagate that into the address_space for a subsequent
271 * fsync(), msync() or close().
272 *
273 * The tricky part is that after writepage we cannot touch the mapping: nothing
274 * prevents it from being freed up. But we have a ref on the page and once
275 * that page is locked, the mapping is pinned.
276 *
277 * We're allowed to run sleeping lock_page() here because we know the caller has
278 * __GFP_FS.
279 */
280static void handle_write_error(struct address_space *mapping,
281 struct page *page, int error)
282{
283 lock_page(page);
284 if (page_mapping(page) == mapping) {
285 if (error == -ENOSPC)
286 set_bit(AS_ENOSPC, &mapping->flags);
287 else
288 set_bit(AS_EIO, &mapping->flags);
289 }
290 unlock_page(page);
291}
292
Christoph Lameter04e62a22006-06-23 02:03:38 -0700293/* possible outcome of pageout() */
294typedef enum {
295 /* failed to write page out, page is locked */
296 PAGE_KEEP,
297 /* move page to the active list, page is locked */
298 PAGE_ACTIVATE,
299 /* page has been sent to the disk successfully, page is unlocked */
300 PAGE_SUCCESS,
301 /* page is clean and locked */
302 PAGE_CLEAN,
303} pageout_t;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/*
Andrew Morton1742f192006-03-22 00:08:21 -0800306 * pageout is called by shrink_page_list() for each dirty page.
307 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
Christoph Lameter04e62a22006-06-23 02:03:38 -0700309static pageout_t pageout(struct page *page, struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 /*
312 * If the page is dirty, only perform writeback if that write
313 * will be non-blocking. To prevent this allocation from being
314 * stalled by pagecache activity. But note that there may be
315 * stalls if we need to run get_block(). We could test
316 * PagePrivate for that.
317 *
318 * If this process is currently in generic_file_write() against
319 * this page's queue, we can perform writeback even if that
320 * will block.
321 *
322 * If the page is swapcache, write it back even if that would
323 * block, for some throttling. This happens by accident, because
324 * swap_backing_dev_info is bust: it doesn't reflect the
325 * congestion state of the swapdevs. Easy to fix, if needed.
326 * See swapfile.c:page_queue_congested().
327 */
328 if (!is_page_cache_freeable(page))
329 return PAGE_KEEP;
330 if (!mapping) {
331 /*
332 * Some data journaling orphaned pages can have
333 * page->mapping == NULL while being dirty with clean buffers.
334 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700335 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (try_to_free_buffers(page)) {
337 ClearPageDirty(page);
338 printk("%s: orphaned page\n", __FUNCTION__);
339 return PAGE_CLEAN;
340 }
341 }
342 return PAGE_KEEP;
343 }
344 if (mapping->a_ops->writepage == NULL)
345 return PAGE_ACTIVATE;
346 if (!may_write_to_queue(mapping->backing_dev_info))
347 return PAGE_KEEP;
348
349 if (clear_page_dirty_for_io(page)) {
350 int res;
351 struct writeback_control wbc = {
352 .sync_mode = WB_SYNC_NONE,
353 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700354 .range_start = 0,
355 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 .nonblocking = 1,
357 .for_reclaim = 1,
358 };
359
360 SetPageReclaim(page);
361 res = mapping->a_ops->writepage(page, &wbc);
362 if (res < 0)
363 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800364 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 ClearPageReclaim(page);
366 return PAGE_ACTIVATE;
367 }
368 if (!PageWriteback(page)) {
369 /* synchronous write or broken a_ops? */
370 ClearPageReclaim(page);
371 }
372
373 return PAGE_SUCCESS;
374 }
375
376 return PAGE_CLEAN;
377}
378
Christoph Lameterb20a3502006-03-22 00:09:12 -0800379int remove_mapping(struct address_space *mapping, struct page *page)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800380{
381 if (!mapping)
382 return 0; /* truncate got there first */
383
384 write_lock_irq(&mapping->tree_lock);
385
386 /*
387 * The non-racy check for busy page. It is critical to check
388 * PageDirty _after_ making sure that the page is freeable and
389 * not in use by anybody. (pagecache + us == 2)
390 */
391 if (unlikely(page_count(page) != 2))
392 goto cannot_free;
393 smp_rmb();
394 if (unlikely(PageDirty(page)))
395 goto cannot_free;
396
397 if (PageSwapCache(page)) {
398 swp_entry_t swap = { .val = page_private(page) };
399 __delete_from_swap_cache(page);
400 write_unlock_irq(&mapping->tree_lock);
401 swap_free(swap);
402 __put_page(page); /* The pagecache ref */
403 return 1;
404 }
405
406 __remove_from_page_cache(page);
407 write_unlock_irq(&mapping->tree_lock);
408 __put_page(page);
409 return 1;
410
411cannot_free:
412 write_unlock_irq(&mapping->tree_lock);
413 return 0;
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/*
Andrew Morton1742f192006-03-22 00:08:21 -0800417 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
Andrew Morton1742f192006-03-22 00:08:21 -0800419static unsigned long shrink_page_list(struct list_head *page_list,
420 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 LIST_HEAD(ret_pages);
423 struct pagevec freed_pvec;
424 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800425 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 cond_resched();
428
429 pagevec_init(&freed_pvec, 1);
430 while (!list_empty(page_list)) {
431 struct address_space *mapping;
432 struct page *page;
433 int may_enter_fs;
434 int referenced;
435
436 cond_resched();
437
438 page = lru_to_page(page_list);
439 list_del(&page->lru);
440
441 if (TestSetPageLocked(page))
442 goto keep;
443
444 BUG_ON(PageActive(page));
445
446 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800447
448 if (!sc->may_swap && page_mapped(page))
449 goto keep_locked;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* Double the slab pressure for mapped and swapcache pages */
452 if (page_mapped(page) || PageSwapCache(page))
453 sc->nr_scanned++;
454
455 if (PageWriteback(page))
456 goto keep_locked;
457
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800458 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /* In active use or really unfreeable? Activate it. */
460 if (referenced && page_mapping_inuse(page))
461 goto activate_locked;
462
463#ifdef CONFIG_SWAP
464 /*
465 * Anonymous process memory has backing store?
466 * Try to allocate it some swap space here.
467 */
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800468 if (PageAnon(page) && !PageSwapCache(page))
Christoph Lameter1480a542006-01-08 01:00:53 -0800469 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471#endif /* CONFIG_SWAP */
472
473 mapping = page_mapping(page);
474 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
475 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
476
477 /*
478 * The page is mapped into the page tables of one or more
479 * processes. Try to unmap it here.
480 */
481 if (page_mapped(page) && mapping) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800482 switch (try_to_unmap(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 case SWAP_FAIL:
484 goto activate_locked;
485 case SWAP_AGAIN:
486 goto keep_locked;
487 case SWAP_SUCCESS:
488 ; /* try to free the page below */
489 }
490 }
491
492 if (PageDirty(page)) {
493 if (referenced)
494 goto keep_locked;
495 if (!may_enter_fs)
496 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800497 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 goto keep_locked;
499
500 /* Page is dirty, try to write it out here */
501 switch(pageout(page, mapping)) {
502 case PAGE_KEEP:
503 goto keep_locked;
504 case PAGE_ACTIVATE:
505 goto activate_locked;
506 case PAGE_SUCCESS:
507 if (PageWriteback(page) || PageDirty(page))
508 goto keep;
509 /*
510 * A synchronous write - probably a ramdisk. Go
511 * ahead and try to reclaim the page.
512 */
513 if (TestSetPageLocked(page))
514 goto keep;
515 if (PageDirty(page) || PageWriteback(page))
516 goto keep_locked;
517 mapping = page_mapping(page);
518 case PAGE_CLEAN:
519 ; /* try to free the page below */
520 }
521 }
522
523 /*
524 * If the page has buffers, try to free the buffer mappings
525 * associated with this page. If we succeed we try to free
526 * the page as well.
527 *
528 * We do this even if the page is PageDirty().
529 * try_to_release_page() does not perform I/O, but it is
530 * possible for a page to have PageDirty set, but it is actually
531 * clean (all its buffers are clean). This happens if the
532 * buffers were written out directly, with submit_bh(). ext3
533 * will do this, as well as the blockdev mapping.
534 * try_to_release_page() will discover that cleanness and will
535 * drop the buffers and mark the page clean - it can be freed.
536 *
537 * Rarely, pages can have buffers and no ->mapping. These are
538 * the pages which were not successfully invalidated in
539 * truncate_complete_page(). We try to drop those buffers here
540 * and if that worked, and the page is no longer mapped into
541 * process address space (page_count == 1) it can be freed.
542 * Otherwise, leave the page on the LRU so it is swappable.
543 */
544 if (PagePrivate(page)) {
545 if (!try_to_release_page(page, sc->gfp_mask))
546 goto activate_locked;
547 if (!mapping && page_count(page) == 1)
548 goto free_it;
549 }
550
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800551 if (!remove_mapping(mapping, page))
552 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554free_it:
555 unlock_page(page);
Andrew Morton05ff5132006-03-22 00:08:20 -0800556 nr_reclaimed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (!pagevec_add(&freed_pvec, page))
558 __pagevec_release_nonlru(&freed_pvec);
559 continue;
560
561activate_locked:
562 SetPageActive(page);
563 pgactivate++;
564keep_locked:
565 unlock_page(page);
566keep:
567 list_add(&page->lru, &ret_pages);
568 BUG_ON(PageLRU(page));
569 }
570 list_splice(&ret_pages, page_list);
571 if (pagevec_count(&freed_pvec))
572 __pagevec_release_nonlru(&freed_pvec);
573 mod_page_state(pgactivate, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800574 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800577/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * zone->lru_lock is heavily contended. Some of the functions that
579 * shrink the lists perform better by taking out a batch of pages
580 * and working on them outside the LRU lock.
581 *
582 * For pagecache intensive workloads, this function is the hottest
583 * spot in the kernel (apart from copy_*_user functions).
584 *
585 * Appropriate locks must be held before calling this function.
586 *
587 * @nr_to_scan: The number of pages to look through on the list.
588 * @src: The LRU list to pull pages off.
589 * @dst: The temp list to put pages on to.
590 * @scanned: The number of pages that were scanned.
591 *
592 * returns how many pages were moved onto *@dst.
593 */
Andrew Morton69e05942006-03-22 00:08:19 -0800594static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
595 struct list_head *src, struct list_head *dst,
596 unsigned long *scanned)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Andrew Morton69e05942006-03-22 00:08:19 -0800598 unsigned long nr_taken = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct page *page;
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800600 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800602 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800603 struct list_head *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 page = lru_to_page(src);
605 prefetchw_prev_lru_page(page, src, flags);
606
Nick Piggin8d438f92006-03-22 00:07:59 -0800607 BUG_ON(!PageLRU(page));
608
Nick Piggin053837f2006-01-18 17:42:27 -0800609 list_del(&page->lru);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800610 target = src;
611 if (likely(get_page_unless_zero(page))) {
Nick Piggin053837f2006-01-18 17:42:27 -0800612 /*
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800613 * Be careful not to clear PageLRU until after we're
614 * sure the page is not being freed elsewhere -- the
615 * page release code relies on it.
Nick Piggin053837f2006-01-18 17:42:27 -0800616 */
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800617 ClearPageLRU(page);
618 target = dst;
619 nr_taken++;
620 } /* else it is being freed elsewhere */
Nick Piggin46453a62006-03-22 00:07:58 -0800621
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800622 list_add(&page->lru, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
625 *scanned = scan;
626 return nr_taken;
627}
628
629/*
Andrew Morton1742f192006-03-22 00:08:21 -0800630 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
631 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
Andrew Morton1742f192006-03-22 00:08:21 -0800633static unsigned long shrink_inactive_list(unsigned long max_scan,
634 struct zone *zone, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 LIST_HEAD(page_list);
637 struct pagevec pvec;
Andrew Morton69e05942006-03-22 00:08:19 -0800638 unsigned long nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800639 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 pagevec_init(&pvec, 1);
642
643 lru_add_drain();
644 spin_lock_irq(&zone->lru_lock);
Andrew Morton69e05942006-03-22 00:08:19 -0800645 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -0800647 unsigned long nr_taken;
648 unsigned long nr_scan;
649 unsigned long nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
652 &zone->inactive_list,
653 &page_list, &nr_scan);
654 zone->nr_inactive -= nr_taken;
655 zone->pages_scanned += nr_scan;
656 spin_unlock_irq(&zone->lru_lock);
657
Andrew Morton69e05942006-03-22 00:08:19 -0800658 nr_scanned += nr_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800659 nr_freed = shrink_page_list(&page_list, sc);
Andrew Morton05ff5132006-03-22 00:08:20 -0800660 nr_reclaimed += nr_freed;
Nick Piggina74609f2006-01-06 00:11:20 -0800661 local_irq_disable();
662 if (current_is_kswapd()) {
663 __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
664 __mod_page_state(kswapd_steal, nr_freed);
665 } else
666 __mod_page_state_zone(zone, pgscan_direct, nr_scan);
667 __mod_page_state_zone(zone, pgsteal, nr_freed);
668
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800669 if (nr_taken == 0)
670 goto done;
671
Nick Piggina74609f2006-01-06 00:11:20 -0800672 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /*
674 * Put back any unfreeable pages.
675 */
676 while (!list_empty(&page_list)) {
677 page = lru_to_page(&page_list);
Nick Piggin8d438f92006-03-22 00:07:59 -0800678 BUG_ON(PageLRU(page));
679 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 list_del(&page->lru);
681 if (PageActive(page))
682 add_page_to_active_list(zone, page);
683 else
684 add_page_to_inactive_list(zone, page);
685 if (!pagevec_add(&pvec, page)) {
686 spin_unlock_irq(&zone->lru_lock);
687 __pagevec_release(&pvec);
688 spin_lock_irq(&zone->lru_lock);
689 }
690 }
Andrew Morton69e05942006-03-22 00:08:19 -0800691 } while (nr_scanned < max_scan);
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800692 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693done:
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800694 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 pagevec_release(&pvec);
Andrew Morton05ff5132006-03-22 00:08:20 -0800696 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699/*
700 * This moves pages from the active list to the inactive list.
701 *
702 * We move them the other way if the page is referenced by one or more
703 * processes, from rmap.
704 *
705 * If the pages are mostly unmapped, the processing is fast and it is
706 * appropriate to hold zone->lru_lock across the whole operation. But if
707 * the pages are mapped, the processing is slow (page_referenced()) so we
708 * should drop zone->lru_lock around each page. It's impossible to balance
709 * this, so instead we remove the pages from the LRU while processing them.
710 * It is safe to rely on PG_active against the non-LRU pages in here because
711 * nobody will play with that bit on a non-LRU page.
712 *
713 * The downside is that we have to touch page->_count against each page.
714 * But we had to alter page->flags anyway.
715 */
Andrew Morton1742f192006-03-22 00:08:21 -0800716static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
717 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Andrew Morton69e05942006-03-22 00:08:19 -0800719 unsigned long pgmoved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 int pgdeactivate = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800721 unsigned long pgscanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 LIST_HEAD(l_hold); /* The pages which were snipped off */
723 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
724 LIST_HEAD(l_active); /* Pages to go onto the active_list */
725 struct page *page;
726 struct pagevec pvec;
727 int reclaim_mapped = 0;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800728
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800729 if (sc->may_swap) {
Christoph Lameter2903fb12006-02-11 17:55:55 -0800730 long mapped_ratio;
731 long distress;
732 long swap_tendency;
733
734 /*
735 * `distress' is a measure of how much trouble we're having
736 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
737 */
738 distress = 100 >> zone->prev_priority;
739
740 /*
741 * The point of this algorithm is to decide when to start
742 * reclaiming mapped memory instead of just pagecache. Work out
743 * how much memory
744 * is mapped.
745 */
746 mapped_ratio = (sc->nr_mapped * 100) / total_memory;
747
748 /*
749 * Now decide how much we really want to unmap some pages. The
750 * mapped ratio is downgraded - just because there's a lot of
751 * mapped memory doesn't necessarily mean that page reclaim
752 * isn't succeeding.
753 *
754 * The distress ratio is important - we don't want to start
755 * going oom.
756 *
757 * A 100% value of vm_swappiness overrides this algorithm
758 * altogether.
759 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700760 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800761
762 /*
763 * Now use this metric to decide whether to start moving mapped
764 * memory onto the inactive list.
765 */
766 if (swap_tendency >= 100)
767 reclaim_mapped = 1;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 lru_add_drain();
771 spin_lock_irq(&zone->lru_lock);
772 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
773 &l_hold, &pgscanned);
774 zone->pages_scanned += pgscanned;
775 zone->nr_active -= pgmoved;
776 spin_unlock_irq(&zone->lru_lock);
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 while (!list_empty(&l_hold)) {
779 cond_resched();
780 page = lru_to_page(&l_hold);
781 list_del(&page->lru);
782 if (page_mapped(page)) {
783 if (!reclaim_mapped ||
784 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800785 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 list_add(&page->lru, &l_active);
787 continue;
788 }
789 }
790 list_add(&page->lru, &l_inactive);
791 }
792
793 pagevec_init(&pvec, 1);
794 pgmoved = 0;
795 spin_lock_irq(&zone->lru_lock);
796 while (!list_empty(&l_inactive)) {
797 page = lru_to_page(&l_inactive);
798 prefetchw_prev_lru_page(page, &l_inactive, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -0800799 BUG_ON(PageLRU(page));
800 SetPageLRU(page);
Nick Piggin4c84cac2006-03-22 00:08:00 -0800801 BUG_ON(!PageActive(page));
802 ClearPageActive(page);
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 list_move(&page->lru, &zone->inactive_list);
805 pgmoved++;
806 if (!pagevec_add(&pvec, page)) {
807 zone->nr_inactive += pgmoved;
808 spin_unlock_irq(&zone->lru_lock);
809 pgdeactivate += pgmoved;
810 pgmoved = 0;
811 if (buffer_heads_over_limit)
812 pagevec_strip(&pvec);
813 __pagevec_release(&pvec);
814 spin_lock_irq(&zone->lru_lock);
815 }
816 }
817 zone->nr_inactive += pgmoved;
818 pgdeactivate += pgmoved;
819 if (buffer_heads_over_limit) {
820 spin_unlock_irq(&zone->lru_lock);
821 pagevec_strip(&pvec);
822 spin_lock_irq(&zone->lru_lock);
823 }
824
825 pgmoved = 0;
826 while (!list_empty(&l_active)) {
827 page = lru_to_page(&l_active);
828 prefetchw_prev_lru_page(page, &l_active, flags);
Nick Piggin8d438f92006-03-22 00:07:59 -0800829 BUG_ON(PageLRU(page));
830 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 BUG_ON(!PageActive(page));
832 list_move(&page->lru, &zone->active_list);
833 pgmoved++;
834 if (!pagevec_add(&pvec, page)) {
835 zone->nr_active += pgmoved;
836 pgmoved = 0;
837 spin_unlock_irq(&zone->lru_lock);
838 __pagevec_release(&pvec);
839 spin_lock_irq(&zone->lru_lock);
840 }
841 }
842 zone->nr_active += pgmoved;
Nick Piggina74609f2006-01-06 00:11:20 -0800843 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Nick Piggina74609f2006-01-06 00:11:20 -0800845 __mod_page_state_zone(zone, pgrefill, pgscanned);
846 __mod_page_state(pgdeactivate, pgdeactivate);
847 local_irq_enable();
848
849 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852/*
853 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
854 */
Andrew Morton05ff5132006-03-22 00:08:20 -0800855static unsigned long shrink_zone(int priority, struct zone *zone,
856 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 unsigned long nr_active;
859 unsigned long nr_inactive;
Christoph Lameter86959492006-03-22 00:08:18 -0800860 unsigned long nr_to_scan;
Andrew Morton05ff5132006-03-22 00:08:20 -0800861 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Martin Hicks53e9a612005-09-03 15:54:51 -0700863 atomic_inc(&zone->reclaim_in_progress);
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 /*
866 * Add one to `nr_to_scan' just to make sure that the kernel will
867 * slowly sift through the active list.
868 */
Christoph Lameter86959492006-03-22 00:08:18 -0800869 zone->nr_scan_active += (zone->nr_active >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 nr_active = zone->nr_scan_active;
871 if (nr_active >= sc->swap_cluster_max)
872 zone->nr_scan_active = 0;
873 else
874 nr_active = 0;
875
Christoph Lameter86959492006-03-22 00:08:18 -0800876 zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 nr_inactive = zone->nr_scan_inactive;
878 if (nr_inactive >= sc->swap_cluster_max)
879 zone->nr_scan_inactive = 0;
880 else
881 nr_inactive = 0;
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 while (nr_active || nr_inactive) {
884 if (nr_active) {
Christoph Lameter86959492006-03-22 00:08:18 -0800885 nr_to_scan = min(nr_active,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -0800887 nr_active -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800888 shrink_active_list(nr_to_scan, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
891 if (nr_inactive) {
Christoph Lameter86959492006-03-22 00:08:18 -0800892 nr_to_scan = min(nr_inactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -0800894 nr_inactive -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -0800895 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
896 sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898 }
899
900 throttle_vm_writeout();
Martin Hicks53e9a612005-09-03 15:54:51 -0700901
902 atomic_dec(&zone->reclaim_in_progress);
Andrew Morton05ff5132006-03-22 00:08:20 -0800903 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906/*
907 * This is the direct reclaim path, for page-allocating processes. We only
908 * try to reclaim pages from zones which will satisfy the caller's allocation
909 * request.
910 *
911 * We reclaim from a zone even if that zone is over pages_high. Because:
912 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
913 * allocation or
914 * b) The zones may be over pages_high but they must go *over* pages_high to
915 * satisfy the `incremental min' zone defense algorithm.
916 *
917 * Returns the number of reclaimed pages.
918 *
919 * If a zone is deemed to be full of pinned pages then just give it a light
920 * scan then give up on it.
921 */
Andrew Morton1742f192006-03-22 00:08:21 -0800922static unsigned long shrink_zones(int priority, struct zone **zones,
Andrew Morton05ff5132006-03-22 00:08:20 -0800923 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Andrew Morton05ff5132006-03-22 00:08:20 -0800925 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int i;
927
928 for (i = 0; zones[i] != NULL; i++) {
929 struct zone *zone = zones[i];
930
Con Kolivasf3fe6512006-01-06 00:11:15 -0800931 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 continue;
933
Paul Jackson9bf22292005-09-06 15:18:12 -0700934 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 continue;
936
Christoph Lameter86959492006-03-22 00:08:18 -0800937 zone->temp_priority = priority;
938 if (zone->prev_priority > priority)
939 zone->prev_priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Christoph Lameter86959492006-03-22 00:08:18 -0800941 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 continue; /* Let kswapd poll it */
943
Andrew Morton05ff5132006-03-22 00:08:20 -0800944 nr_reclaimed += shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
Andrew Morton05ff5132006-03-22 00:08:20 -0800946 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949/*
950 * This is the main entry point to direct page reclaim.
951 *
952 * If a full scan of the inactive list fails to free enough memory then we
953 * are "out of memory" and something needs to be killed.
954 *
955 * If the caller is !__GFP_FS then the probability of a failure is reasonably
956 * high - the zone may be full of dirty or under-writeback pages, which this
957 * caller can't do much about. We kick pdflush and take explicit naps in the
958 * hope that some of these pages can be written. But if the allocating task
959 * holds filesystem locks which prevent writeout this might not work, and the
960 * allocation attempt will fail.
961 */
Andrew Morton69e05942006-03-22 00:08:19 -0800962unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 int priority;
965 int ret = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800966 unsigned long total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800967 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 struct reclaim_state *reclaim_state = current->reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 unsigned long lru_pages = 0;
970 int i;
Andrew Morton179e9632006-03-22 00:08:18 -0800971 struct scan_control sc = {
972 .gfp_mask = gfp_mask,
973 .may_writepage = !laptop_mode,
974 .swap_cluster_max = SWAP_CLUSTER_MAX,
975 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700976 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -0800977 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 inc_page_state(allocstall);
980
981 for (i = 0; zones[i] != NULL; i++) {
982 struct zone *zone = zones[i];
983
Paul Jackson9bf22292005-09-06 15:18:12 -0700984 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 continue;
986
987 zone->temp_priority = DEF_PRIORITY;
988 lru_pages += zone->nr_active + zone->nr_inactive;
989 }
990
991 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
992 sc.nr_mapped = read_page_state(nr_mapped);
993 sc.nr_scanned = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800994 if (!priority)
995 disable_swap_token();
Andrew Morton1742f192006-03-22 00:08:21 -0800996 nr_reclaimed += shrink_zones(priority, zones, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
998 if (reclaim_state) {
Andrew Morton05ff5132006-03-22 00:08:20 -0800999 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 reclaim_state->reclaimed_slab = 0;
1001 }
1002 total_scanned += sc.nr_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001003 if (nr_reclaimed >= sc.swap_cluster_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 ret = 1;
1005 goto out;
1006 }
1007
1008 /*
1009 * Try to write back as many pages as we just scanned. This
1010 * tends to cause slow streaming writers to write data to the
1011 * disk smoothly, at the dirtying rate, which is nice. But
1012 * that's undesirable in laptop mode, where we *want* lumpy
1013 * writeout. So in laptop mode, write out the whole world.
1014 */
Andrew Morton179e9632006-03-22 00:08:18 -08001015 if (total_scanned > sc.swap_cluster_max +
1016 sc.swap_cluster_max / 2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001017 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 sc.may_writepage = 1;
1019 }
1020
1021 /* Take a nap, wait for some writeback to complete */
1022 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1023 blk_congestion_wait(WRITE, HZ/10);
1024 }
1025out:
1026 for (i = 0; zones[i] != 0; i++) {
1027 struct zone *zone = zones[i];
1028
Paul Jackson9bf22292005-09-06 15:18:12 -07001029 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 continue;
1031
1032 zone->prev_priority = zone->temp_priority;
1033 }
1034 return ret;
1035}
1036
1037/*
1038 * For kswapd, balance_pgdat() will work across all this node's zones until
1039 * they are all at pages_high.
1040 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 * Returns the number of pages which were actually freed.
1042 *
1043 * There is special handling here for zones which are full of pinned pages.
1044 * This can happen if the pages are all mlocked, or if they are all used by
1045 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1046 * What we do is to detect the case where all pages in the zone have been
1047 * scanned twice and there has been zero successful reclaim. Mark the zone as
1048 * dead and from now on, only perform a short scan. Basically we're polling
1049 * the zone for when the problem goes away.
1050 *
1051 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1052 * zones which have free_pages > pages_high, but once a zone is found to have
1053 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1054 * of the number of free pages in the lower zones. This interoperates with
1055 * the page allocator fallback scheme to ensure that aging of pages is balanced
1056 * across the zones.
1057 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001058static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 int all_zones_ok;
1061 int priority;
1062 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08001063 unsigned long total_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001064 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08001066 struct scan_control sc = {
1067 .gfp_mask = GFP_KERNEL,
1068 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001069 .swap_cluster_max = SWAP_CLUSTER_MAX,
1070 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -08001071 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073loop_again:
1074 total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001075 nr_reclaimed = 0;
Christoph Lameterc0bbbc72006-06-11 15:22:26 -07001076 sc.may_writepage = !laptop_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 sc.nr_mapped = read_page_state(nr_mapped);
1078
1079 inc_page_state(pageoutrun);
1080
1081 for (i = 0; i < pgdat->nr_zones; i++) {
1082 struct zone *zone = pgdat->node_zones + i;
1083
1084 zone->temp_priority = DEF_PRIORITY;
1085 }
1086
1087 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1088 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1089 unsigned long lru_pages = 0;
1090
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001091 /* The swap token gets in the way of swapout... */
1092 if (!priority)
1093 disable_swap_token();
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 all_zones_ok = 1;
1096
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001097 /*
1098 * Scan in the highmem->dma direction for the highest
1099 * zone which needs scanning
1100 */
1101 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1102 struct zone *zone = pgdat->node_zones + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001104 if (!populated_zone(zone))
1105 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001107 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1108 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001110 if (!zone_watermark_ok(zone, order, zone->pages_high,
1111 0, 0)) {
1112 end_zone = i;
1113 goto scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001116 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117scan:
1118 for (i = 0; i <= end_zone; i++) {
1119 struct zone *zone = pgdat->node_zones + i;
1120
1121 lru_pages += zone->nr_active + zone->nr_inactive;
1122 }
1123
1124 /*
1125 * Now scan the zone in the dma->highmem direction, stopping
1126 * at the last zone which needs scanning.
1127 *
1128 * We do this because the page allocator works in the opposite
1129 * direction. This prevents the page allocator from allocating
1130 * pages behind kswapd's direction of progress, which would
1131 * cause too much scanning of the lower zones.
1132 */
1133 for (i = 0; i <= end_zone; i++) {
1134 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001135 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Con Kolivasf3fe6512006-01-06 00:11:15 -08001137 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 continue;
1139
1140 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1141 continue;
1142
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001143 if (!zone_watermark_ok(zone, order, zone->pages_high,
1144 end_zone, 0))
1145 all_zones_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 zone->temp_priority = priority;
1147 if (zone->prev_priority > priority)
1148 zone->prev_priority = priority;
1149 sc.nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001150 nr_reclaimed += shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001152 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1153 lru_pages);
Andrew Morton05ff5132006-03-22 00:08:20 -08001154 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 total_scanned += sc.nr_scanned;
1156 if (zone->all_unreclaimable)
1157 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001158 if (nr_slab == 0 && zone->pages_scanned >=
1159 (zone->nr_active + zone->nr_inactive) * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 zone->all_unreclaimable = 1;
1161 /*
1162 * If we've done a decent amount of scanning and
1163 * the reclaim ratio is low, start doing writepage
1164 * even in laptop mode
1165 */
1166 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Andrew Morton05ff5132006-03-22 00:08:20 -08001167 total_scanned > nr_reclaimed + nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 sc.may_writepage = 1;
1169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (all_zones_ok)
1171 break; /* kswapd: all done */
1172 /*
1173 * OK, kswapd is getting into trouble. Take a nap, then take
1174 * another pass across the zones.
1175 */
1176 if (total_scanned && priority < DEF_PRIORITY - 2)
1177 blk_congestion_wait(WRITE, HZ/10);
1178
1179 /*
1180 * We do this so kswapd doesn't build up large priorities for
1181 * example when it is freeing in parallel with allocators. It
1182 * matches the direct reclaim path behaviour in terms of impact
1183 * on zone->*_priority.
1184 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001185 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 break;
1187 }
1188out:
1189 for (i = 0; i < pgdat->nr_zones; i++) {
1190 struct zone *zone = pgdat->node_zones + i;
1191
1192 zone->prev_priority = zone->temp_priority;
1193 }
1194 if (!all_zones_ok) {
1195 cond_resched();
1196 goto loop_again;
1197 }
1198
Andrew Morton05ff5132006-03-22 00:08:20 -08001199 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
1202/*
1203 * The background pageout daemon, started as a kernel thread
1204 * from the init process.
1205 *
1206 * This basically trickles out pages so that we have _some_
1207 * free memory available even if there is no other activity
1208 * that frees anything up. This is needed for things like routing
1209 * etc, where we otherwise might have all activity going on in
1210 * asynchronous contexts that cannot page things out.
1211 *
1212 * If there are applications that are active memory-allocators
1213 * (most normal use), this basically shouldn't matter.
1214 */
1215static int kswapd(void *p)
1216{
1217 unsigned long order;
1218 pg_data_t *pgdat = (pg_data_t*)p;
1219 struct task_struct *tsk = current;
1220 DEFINE_WAIT(wait);
1221 struct reclaim_state reclaim_state = {
1222 .reclaimed_slab = 0,
1223 };
1224 cpumask_t cpumask;
1225
1226 daemonize("kswapd%d", pgdat->node_id);
1227 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 Seetharaman83d722f2006-04-24 19:35:21 -07001453static int 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
1471static int __init kswapd_init(void)
1472{
1473 pg_data_t *pgdat;
Andrew Morton69e05942006-03-22 00:08:19 -08001474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 swap_setup();
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -08001476 for_each_online_pgdat(pgdat) {
Andrew Morton69e05942006-03-22 00:08:19 -08001477 pid_t pid;
1478
1479 pid = kernel_thread(kswapd, pgdat, CLONE_KERNEL);
1480 BUG_ON(pid < 0);
Andrew Morton05eeae22006-03-25 03:07:48 -08001481 read_lock(&tasklist_lock);
Andrew Morton69e05942006-03-22 00:08:19 -08001482 pgdat->kswapd = find_task_by_pid(pid);
Andrew Morton05eeae22006-03-25 03:07:48 -08001483 read_unlock(&tasklist_lock);
Andrew Morton69e05942006-03-22 00:08:19 -08001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 total_memory = nr_free_pagecache_pages();
1486 hotcpu_notifier(cpu_callback, 0);
1487 return 0;
1488}
1489
1490module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001491
1492#ifdef CONFIG_NUMA
1493/*
1494 * Zone reclaim mode
1495 *
1496 * If non-zero call zone_reclaim when the number of free pages falls below
1497 * the watermarks.
1498 *
1499 * In the future we may add flags to the mode. However, the page allocator
1500 * should only have to check that zone_reclaim_mode != 0 before calling
1501 * zone_reclaim().
1502 */
1503int zone_reclaim_mode __read_mostly;
1504
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001505#define RECLAIM_OFF 0
1506#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1507#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1508#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001509#define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001510
Christoph Lameter9eeff232006-01-18 17:42:31 -08001511/*
1512 * Mininum time between zone reclaim scans
1513 */
Christoph Lameter2a11ff02006-02-01 03:05:33 -08001514int zone_reclaim_interval __read_mostly = 30*HZ;
Christoph Lametera92f7122006-02-01 03:05:32 -08001515
1516/*
1517 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1518 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1519 * a zone.
1520 */
1521#define ZONE_RECLAIM_PRIORITY 4
1522
Christoph Lameter9eeff232006-01-18 17:42:31 -08001523/*
1524 * Try to free up some pages from this zone through reclaim.
1525 */
Andrew Morton179e9632006-03-22 00:08:18 -08001526static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001527{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001528 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08001529 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001530 struct task_struct *p = current;
1531 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08001532 int priority;
Andrew Morton05ff5132006-03-22 00:08:20 -08001533 unsigned long nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001534 struct scan_control sc = {
1535 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1536 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
1537 .nr_mapped = read_page_state(nr_mapped),
Andrew Morton69e05942006-03-22 00:08:19 -08001538 .swap_cluster_max = max_t(unsigned long, nr_pages,
1539 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08001540 .gfp_mask = gfp_mask,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001541 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -08001542 };
Christoph Lameter9eeff232006-01-18 17:42:31 -08001543
1544 disable_swap_token();
Christoph Lameter9eeff232006-01-18 17:42:31 -08001545 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08001546 /*
1547 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1548 * and we also need to be able to write out pages for RECLAIM_WRITE
1549 * and RECLAIM_SWAP.
1550 */
1551 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001552 reclaim_state.reclaimed_slab = 0;
1553 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001554
Christoph Lametera92f7122006-02-01 03:05:32 -08001555 /*
1556 * Free memory by calling shrink zone with increasing priorities
1557 * until we have enough memory freed.
1558 */
Christoph Lameter86959492006-03-22 00:08:18 -08001559 priority = ZONE_RECLAIM_PRIORITY;
Christoph Lametera92f7122006-02-01 03:05:32 -08001560 do {
Andrew Morton05ff5132006-03-22 00:08:20 -08001561 nr_reclaimed += shrink_zone(priority, zone, &sc);
Christoph Lameter86959492006-03-22 00:08:18 -08001562 priority--;
Andrew Morton05ff5132006-03-22 00:08:20 -08001563 } while (priority >= 0 && nr_reclaimed < nr_pages);
Christoph Lameterc84db232006-02-01 03:05:29 -08001564
Andrew Morton05ff5132006-03-22 00:08:20 -08001565 if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001566 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001567 * shrink_slab() does not currently allow us to determine how
1568 * many pages were freed in this zone. So we just shake the slab
1569 * a bit and then go off node for this particular allocation
1570 * despite possibly having freed enough memory to allocate in
1571 * this zone. If we freed local memory then the next
1572 * allocations will be local again.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001573 *
1574 * shrink_slab will free memory on all zones and may take
1575 * a long time.
1576 */
1577 shrink_slab(sc.nr_scanned, gfp_mask, order);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001578 }
1579
Christoph Lameter9eeff232006-01-18 17:42:31 -08001580 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08001581 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Christoph Lameter9eeff232006-01-18 17:42:31 -08001582
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001583 if (nr_reclaimed == 0) {
1584 /*
1585 * We were unable to reclaim enough pages to stay on node. We
1586 * now allow off node accesses for a certain time period before
1587 * trying again to reclaim pages from the local zone.
1588 */
Christoph Lameter9eeff232006-01-18 17:42:31 -08001589 zone->last_unsuccessful_zone_reclaim = jiffies;
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001590 }
Christoph Lameter9eeff232006-01-18 17:42:31 -08001591
Andrew Morton05ff5132006-03-22 00:08:20 -08001592 return nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001593}
Andrew Morton179e9632006-03-22 00:08:18 -08001594
1595int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1596{
1597 cpumask_t mask;
1598 int node_id;
1599
1600 /*
1601 * Do not reclaim if there was a recent unsuccessful attempt at zone
1602 * reclaim. In that case we let allocations go off node for the
1603 * zone_reclaim_interval. Otherwise we would scan for each off-node
1604 * page allocation.
1605 */
1606 if (time_before(jiffies,
1607 zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval))
1608 return 0;
1609
1610 /*
1611 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1612 * not have reclaimable pages and if we should not delay the allocation
1613 * then do not scan.
1614 */
1615 if (!(gfp_mask & __GFP_WAIT) ||
1616 zone->all_unreclaimable ||
1617 atomic_read(&zone->reclaim_in_progress) > 0 ||
1618 (current->flags & PF_MEMALLOC))
1619 return 0;
1620
1621 /*
1622 * Only run zone reclaim on the local zone or on zones that do not
1623 * have associated processors. This will favor the local processor
1624 * over remote processors and spread off node memory allocations
1625 * as wide as possible.
1626 */
1627 node_id = zone->zone_pgdat->node_id;
1628 mask = node_to_cpumask(node_id);
1629 if (!cpus_empty(mask) && node_id != numa_node_id())
1630 return 0;
1631 return __zone_reclaim(zone, gfp_mask, order);
1632}
Christoph Lameter9eeff232006-01-18 17:42:31 -08001633#endif