blob: aa4b80dbe3ad6023d3d705cbad21ea73f69023ec [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>
36
37#include <asm/tlbflush.h>
38#include <asm/div64.h>
39
40#include <linux/swapops.h>
41
42/* possible outcome of pageout() */
43typedef enum {
44 /* failed to write page out, page is locked */
45 PAGE_KEEP,
46 /* move page to the active list, page is locked */
47 PAGE_ACTIVATE,
48 /* page has been sent to the disk successfully, page is unlocked */
49 PAGE_SUCCESS,
50 /* page is clean and locked */
51 PAGE_CLEAN,
52} pageout_t;
53
54struct scan_control {
55 /* Ask refill_inactive_zone, or shrink_cache to scan this many pages */
56 unsigned long nr_to_scan;
57
58 /* Incremented by the number of inactive pages that were scanned */
59 unsigned long nr_scanned;
60
61 /* Incremented by the number of pages reclaimed */
62 unsigned long nr_reclaimed;
63
64 unsigned long nr_mapped; /* From page_state */
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 /* Ask shrink_caches, or shrink_zone to scan at this priority */
67 unsigned int priority;
68
69 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040070 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 int may_writepage;
73
Christoph Lameterf1fd1062006-01-18 17:42:30 -080074 /* Can pages be swapped as part of reclaim? */
75 int may_swap;
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
78 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
79 * In this context, it doesn't matter that we scan the
80 * whole list at once. */
81 int swap_cluster_max;
82};
83
84/*
85 * The list of shrinker callbacks used by to apply pressure to
86 * ageable caches.
87 */
88struct shrinker {
89 shrinker_t shrinker;
90 struct list_head list;
91 int seeks; /* seeks to recreate an obj */
92 long nr; /* objs pending delete */
93};
94
95#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
96
97#ifdef ARCH_HAS_PREFETCH
98#define prefetch_prev_lru_page(_page, _base, _field) \
99 do { \
100 if ((_page)->lru.prev != _base) { \
101 struct page *prev; \
102 \
103 prev = lru_to_page(&(_page->lru)); \
104 prefetch(&prev->_field); \
105 } \
106 } while (0)
107#else
108#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
109#endif
110
111#ifdef ARCH_HAS_PREFETCHW
112#define prefetchw_prev_lru_page(_page, _base, _field) \
113 do { \
114 if ((_page)->lru.prev != _base) { \
115 struct page *prev; \
116 \
117 prev = lru_to_page(&(_page->lru)); \
118 prefetchw(&prev->_field); \
119 } \
120 } while (0)
121#else
122#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
123#endif
124
125/*
126 * From 0 .. 100. Higher means more swappy.
127 */
128int vm_swappiness = 60;
129static long total_memory;
130
131static LIST_HEAD(shrinker_list);
132static DECLARE_RWSEM(shrinker_rwsem);
133
134/*
135 * Add a shrinker callback to be called from the vm
136 */
137struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
138{
139 struct shrinker *shrinker;
140
141 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
142 if (shrinker) {
143 shrinker->shrinker = theshrinker;
144 shrinker->seeks = seeks;
145 shrinker->nr = 0;
146 down_write(&shrinker_rwsem);
147 list_add_tail(&shrinker->list, &shrinker_list);
148 up_write(&shrinker_rwsem);
149 }
150 return shrinker;
151}
152EXPORT_SYMBOL(set_shrinker);
153
154/*
155 * Remove one
156 */
157void remove_shrinker(struct shrinker *shrinker)
158{
159 down_write(&shrinker_rwsem);
160 list_del(&shrinker->list);
161 up_write(&shrinker_rwsem);
162 kfree(shrinker);
163}
164EXPORT_SYMBOL(remove_shrinker);
165
166#define SHRINK_BATCH 128
167/*
168 * Call the shrink functions to age shrinkable caches
169 *
170 * Here we assume it costs one seek to replace a lru page and that it also
171 * takes a seek to recreate a cache object. With this in mind we age equal
172 * percentages of the lru and ageable caches. This should balance the seeks
173 * generated by these structures.
174 *
175 * If the vm encounted mapped pages on the LRU it increase the pressure on
176 * slab to avoid swapping.
177 *
178 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
179 *
180 * `lru_pages' represents the number of on-LRU pages in all the zones which
181 * are eligible for the caller's allocation attempt. It is used for balancing
182 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700183 *
184 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Andrew Morton9d0243b2006-01-08 01:00:39 -0800186int shrink_slab(unsigned long scanned, gfp_t gfp_mask, unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct shrinker *shrinker;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700189 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 if (scanned == 0)
192 scanned = SWAP_CLUSTER_MAX;
193
194 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700195 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 list_for_each_entry(shrinker, &shrinker_list, list) {
198 unsigned long long delta;
199 unsigned long total_scan;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800200 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800203 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 do_div(delta, lru_pages + 1);
205 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800206 if (shrinker->nr < 0) {
207 printk(KERN_ERR "%s: nr=%ld\n",
208 __FUNCTION__, shrinker->nr);
209 shrinker->nr = max_pass;
210 }
211
212 /*
213 * Avoid risking looping forever due to too large nr value:
214 * never try to free more than twice the estimate number of
215 * freeable entries.
216 */
217 if (shrinker->nr > max_pass * 2)
218 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 total_scan = shrinker->nr;
221 shrinker->nr = 0;
222
223 while (total_scan >= SHRINK_BATCH) {
224 long this_scan = SHRINK_BATCH;
225 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700226 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700228 nr_before = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
230 if (shrink_ret == -1)
231 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700232 if (shrink_ret < nr_before)
233 ret += nr_before - shrink_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 mod_page_state(slabs_scanned, this_scan);
235 total_scan -= this_scan;
236
237 cond_resched();
238 }
239
240 shrinker->nr += total_scan;
241 }
242 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700243 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246/* Called without lock on whether page is mapped, so answer is unstable */
247static inline int page_mapping_inuse(struct page *page)
248{
249 struct address_space *mapping;
250
251 /* Page is in somebody's page tables. */
252 if (page_mapped(page))
253 return 1;
254
255 /* Be more reluctant to reclaim swapcache than pagecache */
256 if (PageSwapCache(page))
257 return 1;
258
259 mapping = page_mapping(page);
260 if (!mapping)
261 return 0;
262
263 /* File is mmap'd by somebody? */
264 return mapping_mapped(mapping);
265}
266
267static inline int is_page_cache_freeable(struct page *page)
268{
269 return page_count(page) - !!PagePrivate(page) == 2;
270}
271
272static int may_write_to_queue(struct backing_dev_info *bdi)
273{
Christoph Lameter930d9152006-01-08 01:00:47 -0800274 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 1;
276 if (!bdi_write_congested(bdi))
277 return 1;
278 if (bdi == current->backing_dev_info)
279 return 1;
280 return 0;
281}
282
283/*
284 * We detected a synchronous write error writing a page out. Probably
285 * -ENOSPC. We need to propagate that into the address_space for a subsequent
286 * fsync(), msync() or close().
287 *
288 * The tricky part is that after writepage we cannot touch the mapping: nothing
289 * prevents it from being freed up. But we have a ref on the page and once
290 * that page is locked, the mapping is pinned.
291 *
292 * We're allowed to run sleeping lock_page() here because we know the caller has
293 * __GFP_FS.
294 */
295static void handle_write_error(struct address_space *mapping,
296 struct page *page, int error)
297{
298 lock_page(page);
299 if (page_mapping(page) == mapping) {
300 if (error == -ENOSPC)
301 set_bit(AS_ENOSPC, &mapping->flags);
302 else
303 set_bit(AS_EIO, &mapping->flags);
304 }
305 unlock_page(page);
306}
307
308/*
309 * pageout is called by shrink_list() for each dirty page. Calls ->writepage().
310 */
311static pageout_t pageout(struct page *page, struct address_space *mapping)
312{
313 /*
314 * If the page is dirty, only perform writeback if that write
315 * will be non-blocking. To prevent this allocation from being
316 * stalled by pagecache activity. But note that there may be
317 * stalls if we need to run get_block(). We could test
318 * PagePrivate for that.
319 *
320 * If this process is currently in generic_file_write() against
321 * this page's queue, we can perform writeback even if that
322 * will block.
323 *
324 * If the page is swapcache, write it back even if that would
325 * block, for some throttling. This happens by accident, because
326 * swap_backing_dev_info is bust: it doesn't reflect the
327 * congestion state of the swapdevs. Easy to fix, if needed.
328 * See swapfile.c:page_queue_congested().
329 */
330 if (!is_page_cache_freeable(page))
331 return PAGE_KEEP;
332 if (!mapping) {
333 /*
334 * Some data journaling orphaned pages can have
335 * page->mapping == NULL while being dirty with clean buffers.
336 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700337 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (try_to_free_buffers(page)) {
339 ClearPageDirty(page);
340 printk("%s: orphaned page\n", __FUNCTION__);
341 return PAGE_CLEAN;
342 }
343 }
344 return PAGE_KEEP;
345 }
346 if (mapping->a_ops->writepage == NULL)
347 return PAGE_ACTIVATE;
348 if (!may_write_to_queue(mapping->backing_dev_info))
349 return PAGE_KEEP;
350
351 if (clear_page_dirty_for_io(page)) {
352 int res;
353 struct writeback_control wbc = {
354 .sync_mode = WB_SYNC_NONE,
355 .nr_to_write = SWAP_CLUSTER_MAX,
356 .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 Lameter49d2e9c2006-01-08 01:00:48 -0800379static int remove_mapping(struct address_space *mapping, struct page *page)
380{
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/*
417 * shrink_list adds the number of reclaimed pages to sc->nr_reclaimed
418 */
419static int shrink_list(struct list_head *page_list, struct scan_control *sc)
420{
421 LIST_HEAD(ret_pages);
422 struct pagevec freed_pvec;
423 int pgactivate = 0;
424 int reclaimed = 0;
425
426 cond_resched();
427
428 pagevec_init(&freed_pvec, 1);
429 while (!list_empty(page_list)) {
430 struct address_space *mapping;
431 struct page *page;
432 int may_enter_fs;
433 int referenced;
434
435 cond_resched();
436
437 page = lru_to_page(page_list);
438 list_del(&page->lru);
439
440 if (TestSetPageLocked(page))
441 goto keep;
442
443 BUG_ON(PageActive(page));
444
445 sc->nr_scanned++;
446 /* Double the slab pressure for mapped and swapcache pages */
447 if (page_mapped(page) || PageSwapCache(page))
448 sc->nr_scanned++;
449
450 if (PageWriteback(page))
451 goto keep_locked;
452
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800453 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* In active use or really unfreeable? Activate it. */
455 if (referenced && page_mapping_inuse(page))
456 goto activate_locked;
457
458#ifdef CONFIG_SWAP
459 /*
460 * Anonymous process memory has backing store?
461 * Try to allocate it some swap space here.
462 */
Lee Schermerhornc3400102005-10-29 18:15:51 -0700463 if (PageAnon(page) && !PageSwapCache(page)) {
Christoph Lameterf1fd1062006-01-18 17:42:30 -0800464 if (!sc->may_swap)
465 goto keep_locked;
Christoph Lameter1480a542006-01-08 01:00:53 -0800466 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 goto activate_locked;
468 }
469#endif /* CONFIG_SWAP */
470
471 mapping = page_mapping(page);
472 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
473 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
474
475 /*
476 * The page is mapped into the page tables of one or more
477 * processes. Try to unmap it here.
478 */
479 if (page_mapped(page) && mapping) {
Christoph Lameteraa3f18b32006-02-01 03:05:32 -0800480 /*
481 * No unmapping if we do not swap
482 */
483 if (!sc->may_swap)
484 goto keep_locked;
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 switch (try_to_unmap(page)) {
487 case SWAP_FAIL:
488 goto activate_locked;
489 case SWAP_AGAIN:
490 goto keep_locked;
491 case SWAP_SUCCESS:
492 ; /* try to free the page below */
493 }
494 }
495
496 if (PageDirty(page)) {
497 if (referenced)
498 goto keep_locked;
499 if (!may_enter_fs)
500 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800501 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto keep_locked;
503
504 /* Page is dirty, try to write it out here */
505 switch(pageout(page, mapping)) {
506 case PAGE_KEEP:
507 goto keep_locked;
508 case PAGE_ACTIVATE:
509 goto activate_locked;
510 case PAGE_SUCCESS:
511 if (PageWriteback(page) || PageDirty(page))
512 goto keep;
513 /*
514 * A synchronous write - probably a ramdisk. Go
515 * ahead and try to reclaim the page.
516 */
517 if (TestSetPageLocked(page))
518 goto keep;
519 if (PageDirty(page) || PageWriteback(page))
520 goto keep_locked;
521 mapping = page_mapping(page);
522 case PAGE_CLEAN:
523 ; /* try to free the page below */
524 }
525 }
526
527 /*
528 * If the page has buffers, try to free the buffer mappings
529 * associated with this page. If we succeed we try to free
530 * the page as well.
531 *
532 * We do this even if the page is PageDirty().
533 * try_to_release_page() does not perform I/O, but it is
534 * possible for a page to have PageDirty set, but it is actually
535 * clean (all its buffers are clean). This happens if the
536 * buffers were written out directly, with submit_bh(). ext3
537 * will do this, as well as the blockdev mapping.
538 * try_to_release_page() will discover that cleanness and will
539 * drop the buffers and mark the page clean - it can be freed.
540 *
541 * Rarely, pages can have buffers and no ->mapping. These are
542 * the pages which were not successfully invalidated in
543 * truncate_complete_page(). We try to drop those buffers here
544 * and if that worked, and the page is no longer mapped into
545 * process address space (page_count == 1) it can be freed.
546 * Otherwise, leave the page on the LRU so it is swappable.
547 */
548 if (PagePrivate(page)) {
549 if (!try_to_release_page(page, sc->gfp_mask))
550 goto activate_locked;
551 if (!mapping && page_count(page) == 1)
552 goto free_it;
553 }
554
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800555 if (!remove_mapping(mapping, page))
556 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558free_it:
559 unlock_page(page);
560 reclaimed++;
561 if (!pagevec_add(&freed_pvec, page))
562 __pagevec_release_nonlru(&freed_pvec);
563 continue;
564
565activate_locked:
566 SetPageActive(page);
567 pgactivate++;
568keep_locked:
569 unlock_page(page);
570keep:
571 list_add(&page->lru, &ret_pages);
572 BUG_ON(PageLRU(page));
573 }
574 list_splice(&ret_pages, page_list);
575 if (pagevec_count(&freed_pvec))
576 __pagevec_release_nonlru(&freed_pvec);
577 mod_page_state(pgactivate, pgactivate);
578 sc->nr_reclaimed += reclaimed;
579 return reclaimed;
580}
581
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800582#ifdef CONFIG_MIGRATION
Christoph Lameter8419c312006-01-08 01:00:52 -0800583static inline void move_to_lru(struct page *page)
584{
585 list_del(&page->lru);
586 if (PageActive(page)) {
587 /*
588 * lru_cache_add_active checks that
589 * the PG_active bit is off.
590 */
591 ClearPageActive(page);
592 lru_cache_add_active(page);
593 } else {
594 lru_cache_add(page);
595 }
596 put_page(page);
597}
598
599/*
Nick Piggin053837f2006-01-18 17:42:27 -0800600 * Add isolated pages on the list back to the LRU.
Christoph Lameter8419c312006-01-08 01:00:52 -0800601 *
602 * returns the number of pages put back.
603 */
604int putback_lru_pages(struct list_head *l)
605{
606 struct page *page;
607 struct page *page2;
608 int count = 0;
609
610 list_for_each_entry_safe(page, page2, l, lru) {
611 move_to_lru(page);
612 count++;
613 }
614 return count;
615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617/*
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800618 * swapout a single page
619 * page is locked upon entry, unlocked on exit
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800620 */
621static int swap_page(struct page *page)
622{
623 struct address_space *mapping = page_mapping(page);
624
625 if (page_mapped(page) && mapping)
626 if (try_to_unmap(page) != SWAP_SUCCESS)
627 goto unlock_retry;
628
629 if (PageDirty(page)) {
630 /* Page is dirty, try to write it out here */
631 switch(pageout(page, mapping)) {
632 case PAGE_KEEP:
633 case PAGE_ACTIVATE:
634 goto unlock_retry;
635
636 case PAGE_SUCCESS:
637 goto retry;
638
639 case PAGE_CLEAN:
640 ; /* try to free the page below */
641 }
642 }
643
644 if (PagePrivate(page)) {
645 if (!try_to_release_page(page, GFP_KERNEL) ||
646 (!mapping && page_count(page) == 1))
647 goto unlock_retry;
648 }
649
650 if (remove_mapping(mapping, page)) {
651 /* Success */
652 unlock_page(page);
653 return 0;
654 }
655
656unlock_retry:
657 unlock_page(page);
658
659retry:
Christoph Lameterd0d96322006-01-08 01:00:55 -0800660 return -EAGAIN;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800661}
662/*
663 * migrate_pages
664 *
665 * Two lists are passed to this function. The first list
666 * contains the pages isolated from the LRU to be migrated.
667 * The second list contains new pages that the pages isolated
668 * can be moved to. If the second list is NULL then all
669 * pages are swapped out.
670 *
671 * The function returns after 10 attempts or if no pages
672 * are movable anymore because t has become empty
673 * or no retryable pages exist anymore.
674 *
675 * SIMPLIFIED VERSION: This implementation of migrate_pages
676 * is only swapping out pages and never touches the second
677 * list. The direct migration patchset
678 * extends this function to avoid the use of swap.
Christoph Lameterd0d96322006-01-08 01:00:55 -0800679 *
680 * Return: Number of pages not migrated when "to" ran empty.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800681 */
Christoph Lameterd4984712006-01-08 01:00:55 -0800682int migrate_pages(struct list_head *from, struct list_head *to,
683 struct list_head *moved, struct list_head *failed)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800684{
685 int retry;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800686 int nr_failed = 0;
687 int pass = 0;
688 struct page *page;
689 struct page *page2;
690 int swapwrite = current->flags & PF_SWAPWRITE;
Christoph Lameterd0d96322006-01-08 01:00:55 -0800691 int rc;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800692
693 if (!swapwrite)
694 current->flags |= PF_SWAPWRITE;
695
696redo:
697 retry = 0;
698
Christoph Lameterd4984712006-01-08 01:00:55 -0800699 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800700 cond_resched();
701
Christoph Lameterd0d96322006-01-08 01:00:55 -0800702 rc = 0;
703 if (page_count(page) == 1)
Christoph Lameteree274972006-01-08 01:00:54 -0800704 /* page was freed from under us. So we are done. */
Christoph Lameterd0d96322006-01-08 01:00:55 -0800705 goto next;
706
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800707 /*
708 * Skip locked pages during the first two passes to give the
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800709 * functions holding the lock time to release the page. Later we
710 * use lock_page() to have a higher chance of acquiring the
711 * lock.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800712 */
Christoph Lameterd0d96322006-01-08 01:00:55 -0800713 rc = -EAGAIN;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800714 if (pass > 2)
715 lock_page(page);
716 else
717 if (TestSetPageLocked(page))
Christoph Lameterd0d96322006-01-08 01:00:55 -0800718 goto next;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800719
720 /*
721 * Only wait on writeback if we have already done a pass where
722 * we we may have triggered writeouts for lots of pages.
723 */
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800724 if (pass > 0) {
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800725 wait_on_page_writeback(page);
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800726 } else {
Christoph Lameterd0d96322006-01-08 01:00:55 -0800727 if (PageWriteback(page))
728 goto unlock_page;
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800729 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800730
Christoph Lameterd0d96322006-01-08 01:00:55 -0800731 /*
732 * Anonymous pages must have swap cache references otherwise
733 * the information contained in the page maps cannot be
734 * preserved.
735 */
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800736 if (PageAnon(page) && !PageSwapCache(page)) {
Christoph Lameter1480a542006-01-08 01:00:53 -0800737 if (!add_to_swap(page, GFP_KERNEL)) {
Christoph Lameterd0d96322006-01-08 01:00:55 -0800738 rc = -ENOMEM;
739 goto unlock_page;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800740 }
741 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800742
743 /*
744 * Page is properly locked and writeback is complete.
745 * Try to migrate the page.
746 */
Christoph Lameterd0d96322006-01-08 01:00:55 -0800747 rc = swap_page(page);
748 goto next;
749
750unlock_page:
751 unlock_page(page);
752
753next:
754 if (rc == -EAGAIN) {
755 retry++;
756 } else if (rc) {
757 /* Permanent failure */
758 list_move(&page->lru, failed);
759 nr_failed++;
760 } else {
761 /* Success */
Christoph Lameterd4984712006-01-08 01:00:55 -0800762 list_move(&page->lru, moved);
Christoph Lameterd4984712006-01-08 01:00:55 -0800763 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800764 }
765 if (retry && pass++ < 10)
766 goto redo;
767
768 if (!swapwrite)
769 current->flags &= ~PF_SWAPWRITE;
770
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800771 return nr_failed + retry;
772}
Christoph Lameter8419c312006-01-08 01:00:52 -0800773
Christoph Lameter8419c312006-01-08 01:00:52 -0800774/*
775 * Isolate one page from the LRU lists and put it on the
Nick Piggin053837f2006-01-18 17:42:27 -0800776 * indicated list with elevated refcount.
Christoph Lameter8419c312006-01-08 01:00:52 -0800777 *
778 * Result:
779 * 0 = page not on LRU list
780 * 1 = page removed from LRU list and added to the specified list.
Christoph Lameter8419c312006-01-08 01:00:52 -0800781 */
782int isolate_lru_page(struct page *page)
783{
Nick Piggin053837f2006-01-18 17:42:27 -0800784 int ret = 0;
Christoph Lameter8419c312006-01-08 01:00:52 -0800785
Nick Piggin053837f2006-01-18 17:42:27 -0800786 if (PageLRU(page)) {
787 struct zone *zone = page_zone(page);
788 spin_lock_irq(&zone->lru_lock);
789 if (TestClearPageLRU(page)) {
790 ret = 1;
791 get_page(page);
792 if (PageActive(page))
793 del_page_from_active_list(zone, page);
794 else
795 del_page_from_inactive_list(zone, page);
796 }
797 spin_unlock_irq(&zone->lru_lock);
Christoph Lameter8419c312006-01-08 01:00:52 -0800798 }
Nick Piggin053837f2006-01-18 17:42:27 -0800799
800 return ret;
Christoph Lameter8419c312006-01-08 01:00:52 -0800801}
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800802#endif
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800803
804/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * zone->lru_lock is heavily contended. Some of the functions that
806 * shrink the lists perform better by taking out a batch of pages
807 * and working on them outside the LRU lock.
808 *
809 * For pagecache intensive workloads, this function is the hottest
810 * spot in the kernel (apart from copy_*_user functions).
811 *
812 * Appropriate locks must be held before calling this function.
813 *
814 * @nr_to_scan: The number of pages to look through on the list.
815 * @src: The LRU list to pull pages off.
816 * @dst: The temp list to put pages on to.
817 * @scanned: The number of pages that were scanned.
818 *
819 * returns how many pages were moved onto *@dst.
820 */
821static int isolate_lru_pages(int nr_to_scan, struct list_head *src,
822 struct list_head *dst, int *scanned)
823{
824 int nr_taken = 0;
825 struct page *page;
826 int scan = 0;
827
828 while (scan++ < nr_to_scan && !list_empty(src)) {
829 page = lru_to_page(src);
830 prefetchw_prev_lru_page(page, src, flags);
831
Nick Piggin053837f2006-01-18 17:42:27 -0800832 if (!TestClearPageLRU(page))
Christoph Lameter21eac812006-01-08 01:00:45 -0800833 BUG();
Nick Piggin053837f2006-01-18 17:42:27 -0800834 list_del(&page->lru);
835 if (get_page_testone(page)) {
836 /*
837 * It is being freed elsewhere
838 */
839 __put_page(page);
840 SetPageLRU(page);
841 list_add(&page->lru, src);
842 continue;
843 } else {
844 list_add(&page->lru, dst);
845 nr_taken++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847 }
848
849 *scanned = scan;
850 return nr_taken;
851}
852
853/*
854 * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed
855 */
856static void shrink_cache(struct zone *zone, struct scan_control *sc)
857{
858 LIST_HEAD(page_list);
859 struct pagevec pvec;
860 int max_scan = sc->nr_to_scan;
861
862 pagevec_init(&pvec, 1);
863
864 lru_add_drain();
865 spin_lock_irq(&zone->lru_lock);
866 while (max_scan > 0) {
867 struct page *page;
868 int nr_taken;
869 int nr_scan;
870 int nr_freed;
871
872 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
873 &zone->inactive_list,
874 &page_list, &nr_scan);
875 zone->nr_inactive -= nr_taken;
876 zone->pages_scanned += nr_scan;
877 spin_unlock_irq(&zone->lru_lock);
878
879 if (nr_taken == 0)
880 goto done;
881
882 max_scan -= nr_scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 nr_freed = shrink_list(&page_list, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Nick Piggina74609f2006-01-06 00:11:20 -0800885 local_irq_disable();
886 if (current_is_kswapd()) {
887 __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
888 __mod_page_state(kswapd_steal, nr_freed);
889 } else
890 __mod_page_state_zone(zone, pgscan_direct, nr_scan);
891 __mod_page_state_zone(zone, pgsteal, nr_freed);
892
893 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
895 * Put back any unfreeable pages.
896 */
897 while (!list_empty(&page_list)) {
898 page = lru_to_page(&page_list);
899 if (TestSetPageLRU(page))
900 BUG();
901 list_del(&page->lru);
902 if (PageActive(page))
903 add_page_to_active_list(zone, page);
904 else
905 add_page_to_inactive_list(zone, page);
906 if (!pagevec_add(&pvec, page)) {
907 spin_unlock_irq(&zone->lru_lock);
908 __pagevec_release(&pvec);
909 spin_lock_irq(&zone->lru_lock);
910 }
911 }
912 }
913 spin_unlock_irq(&zone->lru_lock);
914done:
915 pagevec_release(&pvec);
916}
917
918/*
919 * This moves pages from the active list to the inactive list.
920 *
921 * We move them the other way if the page is referenced by one or more
922 * processes, from rmap.
923 *
924 * If the pages are mostly unmapped, the processing is fast and it is
925 * appropriate to hold zone->lru_lock across the whole operation. But if
926 * the pages are mapped, the processing is slow (page_referenced()) so we
927 * should drop zone->lru_lock around each page. It's impossible to balance
928 * this, so instead we remove the pages from the LRU while processing them.
929 * It is safe to rely on PG_active against the non-LRU pages in here because
930 * nobody will play with that bit on a non-LRU page.
931 *
932 * The downside is that we have to touch page->_count against each page.
933 * But we had to alter page->flags anyway.
934 */
935static void
936refill_inactive_zone(struct zone *zone, struct scan_control *sc)
937{
938 int pgmoved;
939 int pgdeactivate = 0;
940 int pgscanned;
941 int nr_pages = sc->nr_to_scan;
942 LIST_HEAD(l_hold); /* The pages which were snipped off */
943 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
944 LIST_HEAD(l_active); /* Pages to go onto the active_list */
945 struct page *page;
946 struct pagevec pvec;
947 int reclaim_mapped = 0;
948 long mapped_ratio;
949 long distress;
950 long swap_tendency;
951
952 lru_add_drain();
953 spin_lock_irq(&zone->lru_lock);
954 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
955 &l_hold, &pgscanned);
956 zone->pages_scanned += pgscanned;
957 zone->nr_active -= pgmoved;
958 spin_unlock_irq(&zone->lru_lock);
959
960 /*
961 * `distress' is a measure of how much trouble we're having reclaiming
962 * pages. 0 -> no problems. 100 -> great trouble.
963 */
964 distress = 100 >> zone->prev_priority;
965
966 /*
967 * The point of this algorithm is to decide when to start reclaiming
968 * mapped memory instead of just pagecache. Work out how much memory
969 * is mapped.
970 */
971 mapped_ratio = (sc->nr_mapped * 100) / total_memory;
972
973 /*
974 * Now decide how much we really want to unmap some pages. The mapped
975 * ratio is downgraded - just because there's a lot of mapped memory
976 * doesn't necessarily mean that page reclaim isn't succeeding.
977 *
978 * The distress ratio is important - we don't want to start going oom.
979 *
980 * A 100% value of vm_swappiness overrides this algorithm altogether.
981 */
982 swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
983
984 /*
985 * Now use this metric to decide whether to start moving mapped memory
986 * onto the inactive list.
987 */
988 if (swap_tendency >= 100)
989 reclaim_mapped = 1;
990
991 while (!list_empty(&l_hold)) {
992 cond_resched();
993 page = lru_to_page(&l_hold);
994 list_del(&page->lru);
995 if (page_mapped(page)) {
996 if (!reclaim_mapped ||
997 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800998 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 list_add(&page->lru, &l_active);
1000 continue;
1001 }
1002 }
1003 list_add(&page->lru, &l_inactive);
1004 }
1005
1006 pagevec_init(&pvec, 1);
1007 pgmoved = 0;
1008 spin_lock_irq(&zone->lru_lock);
1009 while (!list_empty(&l_inactive)) {
1010 page = lru_to_page(&l_inactive);
1011 prefetchw_prev_lru_page(page, &l_inactive, flags);
1012 if (TestSetPageLRU(page))
1013 BUG();
1014 if (!TestClearPageActive(page))
1015 BUG();
1016 list_move(&page->lru, &zone->inactive_list);
1017 pgmoved++;
1018 if (!pagevec_add(&pvec, page)) {
1019 zone->nr_inactive += pgmoved;
1020 spin_unlock_irq(&zone->lru_lock);
1021 pgdeactivate += pgmoved;
1022 pgmoved = 0;
1023 if (buffer_heads_over_limit)
1024 pagevec_strip(&pvec);
1025 __pagevec_release(&pvec);
1026 spin_lock_irq(&zone->lru_lock);
1027 }
1028 }
1029 zone->nr_inactive += pgmoved;
1030 pgdeactivate += pgmoved;
1031 if (buffer_heads_over_limit) {
1032 spin_unlock_irq(&zone->lru_lock);
1033 pagevec_strip(&pvec);
1034 spin_lock_irq(&zone->lru_lock);
1035 }
1036
1037 pgmoved = 0;
1038 while (!list_empty(&l_active)) {
1039 page = lru_to_page(&l_active);
1040 prefetchw_prev_lru_page(page, &l_active, flags);
1041 if (TestSetPageLRU(page))
1042 BUG();
1043 BUG_ON(!PageActive(page));
1044 list_move(&page->lru, &zone->active_list);
1045 pgmoved++;
1046 if (!pagevec_add(&pvec, page)) {
1047 zone->nr_active += pgmoved;
1048 pgmoved = 0;
1049 spin_unlock_irq(&zone->lru_lock);
1050 __pagevec_release(&pvec);
1051 spin_lock_irq(&zone->lru_lock);
1052 }
1053 }
1054 zone->nr_active += pgmoved;
Nick Piggina74609f2006-01-06 00:11:20 -08001055 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Nick Piggina74609f2006-01-06 00:11:20 -08001057 __mod_page_state_zone(zone, pgrefill, pgscanned);
1058 __mod_page_state(pgdeactivate, pgdeactivate);
1059 local_irq_enable();
1060
1061 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064/*
1065 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1066 */
1067static void
1068shrink_zone(struct zone *zone, struct scan_control *sc)
1069{
1070 unsigned long nr_active;
1071 unsigned long nr_inactive;
1072
Martin Hicks53e9a612005-09-03 15:54:51 -07001073 atomic_inc(&zone->reclaim_in_progress);
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /*
1076 * Add one to `nr_to_scan' just to make sure that the kernel will
1077 * slowly sift through the active list.
1078 */
1079 zone->nr_scan_active += (zone->nr_active >> sc->priority) + 1;
1080 nr_active = zone->nr_scan_active;
1081 if (nr_active >= sc->swap_cluster_max)
1082 zone->nr_scan_active = 0;
1083 else
1084 nr_active = 0;
1085
1086 zone->nr_scan_inactive += (zone->nr_inactive >> sc->priority) + 1;
1087 nr_inactive = zone->nr_scan_inactive;
1088 if (nr_inactive >= sc->swap_cluster_max)
1089 zone->nr_scan_inactive = 0;
1090 else
1091 nr_inactive = 0;
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 while (nr_active || nr_inactive) {
1094 if (nr_active) {
1095 sc->nr_to_scan = min(nr_active,
1096 (unsigned long)sc->swap_cluster_max);
1097 nr_active -= sc->nr_to_scan;
1098 refill_inactive_zone(zone, sc);
1099 }
1100
1101 if (nr_inactive) {
1102 sc->nr_to_scan = min(nr_inactive,
1103 (unsigned long)sc->swap_cluster_max);
1104 nr_inactive -= sc->nr_to_scan;
1105 shrink_cache(zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107 }
1108
1109 throttle_vm_writeout();
Martin Hicks53e9a612005-09-03 15:54:51 -07001110
1111 atomic_dec(&zone->reclaim_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
1114/*
1115 * This is the direct reclaim path, for page-allocating processes. We only
1116 * try to reclaim pages from zones which will satisfy the caller's allocation
1117 * request.
1118 *
1119 * We reclaim from a zone even if that zone is over pages_high. Because:
1120 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1121 * allocation or
1122 * b) The zones may be over pages_high but they must go *over* pages_high to
1123 * satisfy the `incremental min' zone defense algorithm.
1124 *
1125 * Returns the number of reclaimed pages.
1126 *
1127 * If a zone is deemed to be full of pinned pages then just give it a light
1128 * scan then give up on it.
1129 */
1130static void
1131shrink_caches(struct zone **zones, struct scan_control *sc)
1132{
1133 int i;
1134
1135 for (i = 0; zones[i] != NULL; i++) {
1136 struct zone *zone = zones[i];
1137
Con Kolivasf3fe6512006-01-06 00:11:15 -08001138 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 continue;
1140
Paul Jackson9bf22292005-09-06 15:18:12 -07001141 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 continue;
1143
1144 zone->temp_priority = sc->priority;
1145 if (zone->prev_priority > sc->priority)
1146 zone->prev_priority = sc->priority;
1147
1148 if (zone->all_unreclaimable && sc->priority != DEF_PRIORITY)
1149 continue; /* Let kswapd poll it */
1150
1151 shrink_zone(zone, sc);
1152 }
1153}
1154
1155/*
1156 * This is the main entry point to direct page reclaim.
1157 *
1158 * If a full scan of the inactive list fails to free enough memory then we
1159 * are "out of memory" and something needs to be killed.
1160 *
1161 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1162 * high - the zone may be full of dirty or under-writeback pages, which this
1163 * caller can't do much about. We kick pdflush and take explicit naps in the
1164 * hope that some of these pages can be written. But if the allocating task
1165 * holds filesystem locks which prevent writeout this might not work, and the
1166 * allocation attempt will fail.
1167 */
Al Viro6daa0e22005-10-21 03:18:50 -04001168int try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 int priority;
1171 int ret = 0;
1172 int total_scanned = 0, total_reclaimed = 0;
1173 struct reclaim_state *reclaim_state = current->reclaim_state;
1174 struct scan_control sc;
1175 unsigned long lru_pages = 0;
1176 int i;
1177
1178 sc.gfp_mask = gfp_mask;
Christoph Lameter52a83632006-02-01 03:05:28 -08001179 sc.may_writepage = !laptop_mode;
Christoph Lameterf1fd1062006-01-18 17:42:30 -08001180 sc.may_swap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 inc_page_state(allocstall);
1183
1184 for (i = 0; zones[i] != NULL; i++) {
1185 struct zone *zone = zones[i];
1186
Paul Jackson9bf22292005-09-06 15:18:12 -07001187 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 continue;
1189
1190 zone->temp_priority = DEF_PRIORITY;
1191 lru_pages += zone->nr_active + zone->nr_inactive;
1192 }
1193
1194 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1195 sc.nr_mapped = read_page_state(nr_mapped);
1196 sc.nr_scanned = 0;
1197 sc.nr_reclaimed = 0;
1198 sc.priority = priority;
1199 sc.swap_cluster_max = SWAP_CLUSTER_MAX;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001200 if (!priority)
1201 disable_swap_token();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 shrink_caches(zones, &sc);
1203 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
1204 if (reclaim_state) {
1205 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
1206 reclaim_state->reclaimed_slab = 0;
1207 }
1208 total_scanned += sc.nr_scanned;
1209 total_reclaimed += sc.nr_reclaimed;
1210 if (total_reclaimed >= sc.swap_cluster_max) {
1211 ret = 1;
1212 goto out;
1213 }
1214
1215 /*
1216 * Try to write back as many pages as we just scanned. This
1217 * tends to cause slow streaming writers to write data to the
1218 * disk smoothly, at the dirtying rate, which is nice. But
1219 * that's undesirable in laptop mode, where we *want* lumpy
1220 * writeout. So in laptop mode, write out the whole world.
1221 */
1222 if (total_scanned > sc.swap_cluster_max + sc.swap_cluster_max/2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001223 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 sc.may_writepage = 1;
1225 }
1226
1227 /* Take a nap, wait for some writeback to complete */
1228 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1229 blk_congestion_wait(WRITE, HZ/10);
1230 }
1231out:
1232 for (i = 0; zones[i] != 0; i++) {
1233 struct zone *zone = zones[i];
1234
Paul Jackson9bf22292005-09-06 15:18:12 -07001235 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 continue;
1237
1238 zone->prev_priority = zone->temp_priority;
1239 }
1240 return ret;
1241}
1242
1243/*
1244 * For kswapd, balance_pgdat() will work across all this node's zones until
1245 * they are all at pages_high.
1246 *
1247 * If `nr_pages' is non-zero then it is the number of pages which are to be
1248 * reclaimed, regardless of the zone occupancies. This is a software suspend
1249 * special.
1250 *
1251 * Returns the number of pages which were actually freed.
1252 *
1253 * There is special handling here for zones which are full of pinned pages.
1254 * This can happen if the pages are all mlocked, or if they are all used by
1255 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1256 * What we do is to detect the case where all pages in the zone have been
1257 * scanned twice and there has been zero successful reclaim. Mark the zone as
1258 * dead and from now on, only perform a short scan. Basically we're polling
1259 * the zone for when the problem goes away.
1260 *
1261 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1262 * zones which have free_pages > pages_high, but once a zone is found to have
1263 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1264 * of the number of free pages in the lower zones. This interoperates with
1265 * the page allocator fallback scheme to ensure that aging of pages is balanced
1266 * across the zones.
1267 */
1268static int balance_pgdat(pg_data_t *pgdat, int nr_pages, int order)
1269{
1270 int to_free = nr_pages;
1271 int all_zones_ok;
1272 int priority;
1273 int i;
1274 int total_scanned, total_reclaimed;
1275 struct reclaim_state *reclaim_state = current->reclaim_state;
1276 struct scan_control sc;
1277
1278loop_again:
1279 total_scanned = 0;
1280 total_reclaimed = 0;
1281 sc.gfp_mask = GFP_KERNEL;
Christoph Lameter52a83632006-02-01 03:05:28 -08001282 sc.may_writepage = !laptop_mode;
Christoph Lameterf1fd1062006-01-18 17:42:30 -08001283 sc.may_swap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 sc.nr_mapped = read_page_state(nr_mapped);
1285
1286 inc_page_state(pageoutrun);
1287
1288 for (i = 0; i < pgdat->nr_zones; i++) {
1289 struct zone *zone = pgdat->node_zones + i;
1290
1291 zone->temp_priority = DEF_PRIORITY;
1292 }
1293
1294 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1295 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1296 unsigned long lru_pages = 0;
1297
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001298 /* The swap token gets in the way of swapout... */
1299 if (!priority)
1300 disable_swap_token();
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 all_zones_ok = 1;
1303
1304 if (nr_pages == 0) {
1305 /*
1306 * Scan in the highmem->dma direction for the highest
1307 * zone which needs scanning
1308 */
1309 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1310 struct zone *zone = pgdat->node_zones + i;
1311
Con Kolivasf3fe6512006-01-06 00:11:15 -08001312 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 continue;
1314
1315 if (zone->all_unreclaimable &&
1316 priority != DEF_PRIORITY)
1317 continue;
1318
1319 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001320 zone->pages_high, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 end_zone = i;
1322 goto scan;
1323 }
1324 }
1325 goto out;
1326 } else {
1327 end_zone = pgdat->nr_zones - 1;
1328 }
1329scan:
1330 for (i = 0; i <= end_zone; i++) {
1331 struct zone *zone = pgdat->node_zones + i;
1332
1333 lru_pages += zone->nr_active + zone->nr_inactive;
1334 }
1335
1336 /*
1337 * Now scan the zone in the dma->highmem direction, stopping
1338 * at the last zone which needs scanning.
1339 *
1340 * We do this because the page allocator works in the opposite
1341 * direction. This prevents the page allocator from allocating
1342 * pages behind kswapd's direction of progress, which would
1343 * cause too much scanning of the lower zones.
1344 */
1345 for (i = 0; i <= end_zone; i++) {
1346 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001347 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Con Kolivasf3fe6512006-01-06 00:11:15 -08001349 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 continue;
1351
1352 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1353 continue;
1354
1355 if (nr_pages == 0) { /* Not software suspend */
1356 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001357 zone->pages_high, end_zone, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 all_zones_ok = 0;
1359 }
1360 zone->temp_priority = priority;
1361 if (zone->prev_priority > priority)
1362 zone->prev_priority = priority;
1363 sc.nr_scanned = 0;
1364 sc.nr_reclaimed = 0;
1365 sc.priority = priority;
1366 sc.swap_cluster_max = nr_pages? nr_pages : SWAP_CLUSTER_MAX;
Martin Hicks1e7e5a92005-06-21 17:14:43 -07001367 atomic_inc(&zone->reclaim_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 shrink_zone(zone, &sc);
Martin Hicks1e7e5a92005-06-21 17:14:43 -07001369 atomic_dec(&zone->reclaim_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001371 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1372 lru_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
1374 total_reclaimed += sc.nr_reclaimed;
1375 total_scanned += sc.nr_scanned;
1376 if (zone->all_unreclaimable)
1377 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001378 if (nr_slab == 0 && zone->pages_scanned >=
1379 (zone->nr_active + zone->nr_inactive) * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 zone->all_unreclaimable = 1;
1381 /*
1382 * If we've done a decent amount of scanning and
1383 * the reclaim ratio is low, start doing writepage
1384 * even in laptop mode
1385 */
1386 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
1387 total_scanned > total_reclaimed+total_reclaimed/2)
1388 sc.may_writepage = 1;
1389 }
1390 if (nr_pages && to_free > total_reclaimed)
1391 continue; /* swsusp: need to do more work */
1392 if (all_zones_ok)
1393 break; /* kswapd: all done */
1394 /*
1395 * OK, kswapd is getting into trouble. Take a nap, then take
1396 * another pass across the zones.
1397 */
1398 if (total_scanned && priority < DEF_PRIORITY - 2)
1399 blk_congestion_wait(WRITE, HZ/10);
1400
1401 /*
1402 * We do this so kswapd doesn't build up large priorities for
1403 * example when it is freeing in parallel with allocators. It
1404 * matches the direct reclaim path behaviour in terms of impact
1405 * on zone->*_priority.
1406 */
1407 if ((total_reclaimed >= SWAP_CLUSTER_MAX) && (!nr_pages))
1408 break;
1409 }
1410out:
1411 for (i = 0; i < pgdat->nr_zones; i++) {
1412 struct zone *zone = pgdat->node_zones + i;
1413
1414 zone->prev_priority = zone->temp_priority;
1415 }
1416 if (!all_zones_ok) {
1417 cond_resched();
1418 goto loop_again;
1419 }
1420
1421 return total_reclaimed;
1422}
1423
1424/*
1425 * The background pageout daemon, started as a kernel thread
1426 * from the init process.
1427 *
1428 * This basically trickles out pages so that we have _some_
1429 * free memory available even if there is no other activity
1430 * that frees anything up. This is needed for things like routing
1431 * etc, where we otherwise might have all activity going on in
1432 * asynchronous contexts that cannot page things out.
1433 *
1434 * If there are applications that are active memory-allocators
1435 * (most normal use), this basically shouldn't matter.
1436 */
1437static int kswapd(void *p)
1438{
1439 unsigned long order;
1440 pg_data_t *pgdat = (pg_data_t*)p;
1441 struct task_struct *tsk = current;
1442 DEFINE_WAIT(wait);
1443 struct reclaim_state reclaim_state = {
1444 .reclaimed_slab = 0,
1445 };
1446 cpumask_t cpumask;
1447
1448 daemonize("kswapd%d", pgdat->node_id);
1449 cpumask = node_to_cpumask(pgdat->node_id);
1450 if (!cpus_empty(cpumask))
1451 set_cpus_allowed(tsk, cpumask);
1452 current->reclaim_state = &reclaim_state;
1453
1454 /*
1455 * Tell the memory management that we're a "memory allocator",
1456 * and that if we need more memory we should get access to it
1457 * regardless (see "__alloc_pages()"). "kswapd" should
1458 * never get caught in the normal page freeing logic.
1459 *
1460 * (Kswapd normally doesn't need memory anyway, but sometimes
1461 * you need a small amount of memory in order to be able to
1462 * page out something else, and this flag essentially protects
1463 * us from recursively trying to free more memory as we're
1464 * trying to free the first piece of memory in the first place).
1465 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001466 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 order = 0;
1469 for ( ; ; ) {
1470 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001471
1472 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1475 new_order = pgdat->kswapd_max_order;
1476 pgdat->kswapd_max_order = 0;
1477 if (order < new_order) {
1478 /*
1479 * Don't sleep if someone wants a larger 'order'
1480 * allocation
1481 */
1482 order = new_order;
1483 } else {
1484 schedule();
1485 order = pgdat->kswapd_max_order;
1486 }
1487 finish_wait(&pgdat->kswapd_wait, &wait);
1488
1489 balance_pgdat(pgdat, 0, order);
1490 }
1491 return 0;
1492}
1493
1494/*
1495 * A zone is low on free memory, so wake its kswapd task to service it.
1496 */
1497void wakeup_kswapd(struct zone *zone, int order)
1498{
1499 pg_data_t *pgdat;
1500
Con Kolivasf3fe6512006-01-06 00:11:15 -08001501 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return;
1503
1504 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001505 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return;
1507 if (pgdat->kswapd_max_order < order)
1508 pgdat->kswapd_max_order = order;
Paul Jackson9bf22292005-09-06 15:18:12 -07001509 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001511 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001513 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
1516#ifdef CONFIG_PM
1517/*
1518 * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
1519 * pages.
1520 */
1521int shrink_all_memory(int nr_pages)
1522{
1523 pg_data_t *pgdat;
1524 int nr_to_free = nr_pages;
1525 int ret = 0;
1526 struct reclaim_state reclaim_state = {
1527 .reclaimed_slab = 0,
1528 };
1529
1530 current->reclaim_state = &reclaim_state;
1531 for_each_pgdat(pgdat) {
1532 int freed;
1533 freed = balance_pgdat(pgdat, nr_to_free, 0);
1534 ret += freed;
1535 nr_to_free -= freed;
1536 if (nr_to_free <= 0)
1537 break;
1538 }
1539 current->reclaim_state = NULL;
1540 return ret;
1541}
1542#endif
1543
1544#ifdef CONFIG_HOTPLUG_CPU
1545/* It's optimal to keep kswapds on the same CPUs as their memory, but
1546 not required for correctness. So if the last cpu in a node goes
1547 away, we get changed to run anywhere: as the first one comes back,
1548 restore their cpu bindings. */
1549static int __devinit cpu_callback(struct notifier_block *nfb,
1550 unsigned long action,
1551 void *hcpu)
1552{
1553 pg_data_t *pgdat;
1554 cpumask_t mask;
1555
1556 if (action == CPU_ONLINE) {
1557 for_each_pgdat(pgdat) {
1558 mask = node_to_cpumask(pgdat->node_id);
1559 if (any_online_cpu(mask) != NR_CPUS)
1560 /* One of our CPUs online: restore mask */
1561 set_cpus_allowed(pgdat->kswapd, mask);
1562 }
1563 }
1564 return NOTIFY_OK;
1565}
1566#endif /* CONFIG_HOTPLUG_CPU */
1567
1568static int __init kswapd_init(void)
1569{
1570 pg_data_t *pgdat;
1571 swap_setup();
1572 for_each_pgdat(pgdat)
1573 pgdat->kswapd
1574 = find_task_by_pid(kernel_thread(kswapd, pgdat, CLONE_KERNEL));
1575 total_memory = nr_free_pagecache_pages();
1576 hotcpu_notifier(cpu_callback, 0);
1577 return 0;
1578}
1579
1580module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001581
1582#ifdef CONFIG_NUMA
1583/*
1584 * Zone reclaim mode
1585 *
1586 * If non-zero call zone_reclaim when the number of free pages falls below
1587 * the watermarks.
1588 *
1589 * In the future we may add flags to the mode. However, the page allocator
1590 * should only have to check that zone_reclaim_mode != 0 before calling
1591 * zone_reclaim().
1592 */
1593int zone_reclaim_mode __read_mostly;
1594
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001595#define RECLAIM_OFF 0
1596#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1597#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1598#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001599#define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001600
Christoph Lameter9eeff232006-01-18 17:42:31 -08001601/*
1602 * Mininum time between zone reclaim scans
1603 */
Christoph Lameter2a11ff02006-02-01 03:05:33 -08001604int zone_reclaim_interval __read_mostly = 30*HZ;
Christoph Lametera92f7122006-02-01 03:05:32 -08001605
1606/*
1607 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1608 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1609 * a zone.
1610 */
1611#define ZONE_RECLAIM_PRIORITY 4
1612
Christoph Lameter9eeff232006-01-18 17:42:31 -08001613/*
1614 * Try to free up some pages from this zone through reclaim.
1615 */
1616int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1617{
Christoph Lameter89288622006-02-01 03:05:25 -08001618 int nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001619 struct task_struct *p = current;
1620 struct reclaim_state reclaim_state;
Christoph Lameter89288622006-02-01 03:05:25 -08001621 struct scan_control sc;
Christoph Lameter42c722d2006-02-01 03:05:26 -08001622 cpumask_t mask;
1623 int node_id;
Christoph Lameter89288622006-02-01 03:05:25 -08001624
1625 if (time_before(jiffies,
Christoph Lameter2a11ff02006-02-01 03:05:33 -08001626 zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval))
Christoph Lameter89288622006-02-01 03:05:25 -08001627 return 0;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001628
1629 if (!(gfp_mask & __GFP_WAIT) ||
Christoph Lameter9eeff232006-01-18 17:42:31 -08001630 zone->all_unreclaimable ||
1631 atomic_read(&zone->reclaim_in_progress) > 0)
1632 return 0;
1633
Christoph Lameter42c722d2006-02-01 03:05:26 -08001634 node_id = zone->zone_pgdat->node_id;
1635 mask = node_to_cpumask(node_id);
1636 if (!cpus_empty(mask) && node_id != numa_node_id())
1637 return 0;
1638
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001639 sc.may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE);
1640 sc.may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP);
Christoph Lameter89288622006-02-01 03:05:25 -08001641 sc.nr_scanned = 0;
1642 sc.nr_reclaimed = 0;
Christoph Lametera92f7122006-02-01 03:05:32 -08001643 sc.priority = ZONE_RECLAIM_PRIORITY + 1;
Christoph Lameter89288622006-02-01 03:05:25 -08001644 sc.nr_mapped = read_page_state(nr_mapped);
1645 sc.gfp_mask = gfp_mask;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001646
1647 disable_swap_token();
1648
Christoph Lameter89288622006-02-01 03:05:25 -08001649 nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001650 if (nr_pages > SWAP_CLUSTER_MAX)
1651 sc.swap_cluster_max = nr_pages;
1652 else
1653 sc.swap_cluster_max = SWAP_CLUSTER_MAX;
1654
1655 cond_resched();
1656 p->flags |= PF_MEMALLOC;
1657 reclaim_state.reclaimed_slab = 0;
1658 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001659
Christoph Lametera92f7122006-02-01 03:05:32 -08001660 /*
1661 * Free memory by calling shrink zone with increasing priorities
1662 * until we have enough memory freed.
1663 */
1664 do {
1665 sc.priority--;
1666 shrink_zone(zone, &sc);
1667
1668 } while (sc.nr_reclaimed < nr_pages && sc.priority > 0);
Christoph Lameterc84db232006-02-01 03:05:29 -08001669
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001670 if (sc.nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
1671 /*
1672 * shrink_slab does not currently allow us to determine
1673 * how many pages were freed in the zone. So we just
1674 * shake the slab and then go offnode for a single allocation.
1675 *
1676 * shrink_slab will free memory on all zones and may take
1677 * a long time.
1678 */
1679 shrink_slab(sc.nr_scanned, gfp_mask, order);
1680 sc.nr_reclaimed = 1; /* Avoid getting the off node timeout */
1681 }
1682
Christoph Lameter9eeff232006-01-18 17:42:31 -08001683 p->reclaim_state = NULL;
1684 current->flags &= ~PF_MEMALLOC;
1685
1686 if (sc.nr_reclaimed == 0)
1687 zone->last_unsuccessful_zone_reclaim = jiffies;
1688
Christoph Lameterc84db232006-02-01 03:05:29 -08001689 return sc.nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001690}
1691#endif
1692