blob: 827bf674577a8d41649da2f1fbfb8d07a355c3d1 [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
74 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
75 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
76 * In this context, it doesn't matter that we scan the
77 * whole list at once. */
78 int swap_cluster_max;
79};
80
81/*
82 * The list of shrinker callbacks used by to apply pressure to
83 * ageable caches.
84 */
85struct shrinker {
86 shrinker_t shrinker;
87 struct list_head list;
88 int seeks; /* seeks to recreate an obj */
89 long nr; /* objs pending delete */
90};
91
92#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
93
94#ifdef ARCH_HAS_PREFETCH
95#define prefetch_prev_lru_page(_page, _base, _field) \
96 do { \
97 if ((_page)->lru.prev != _base) { \
98 struct page *prev; \
99 \
100 prev = lru_to_page(&(_page->lru)); \
101 prefetch(&prev->_field); \
102 } \
103 } while (0)
104#else
105#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
106#endif
107
108#ifdef ARCH_HAS_PREFETCHW
109#define prefetchw_prev_lru_page(_page, _base, _field) \
110 do { \
111 if ((_page)->lru.prev != _base) { \
112 struct page *prev; \
113 \
114 prev = lru_to_page(&(_page->lru)); \
115 prefetchw(&prev->_field); \
116 } \
117 } while (0)
118#else
119#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
120#endif
121
122/*
123 * From 0 .. 100. Higher means more swappy.
124 */
125int vm_swappiness = 60;
126static long total_memory;
127
128static LIST_HEAD(shrinker_list);
129static DECLARE_RWSEM(shrinker_rwsem);
130
131/*
132 * Add a shrinker callback to be called from the vm
133 */
134struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
135{
136 struct shrinker *shrinker;
137
138 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
139 if (shrinker) {
140 shrinker->shrinker = theshrinker;
141 shrinker->seeks = seeks;
142 shrinker->nr = 0;
143 down_write(&shrinker_rwsem);
144 list_add_tail(&shrinker->list, &shrinker_list);
145 up_write(&shrinker_rwsem);
146 }
147 return shrinker;
148}
149EXPORT_SYMBOL(set_shrinker);
150
151/*
152 * Remove one
153 */
154void remove_shrinker(struct shrinker *shrinker)
155{
156 down_write(&shrinker_rwsem);
157 list_del(&shrinker->list);
158 up_write(&shrinker_rwsem);
159 kfree(shrinker);
160}
161EXPORT_SYMBOL(remove_shrinker);
162
163#define SHRINK_BATCH 128
164/*
165 * Call the shrink functions to age shrinkable caches
166 *
167 * Here we assume it costs one seek to replace a lru page and that it also
168 * takes a seek to recreate a cache object. With this in mind we age equal
169 * percentages of the lru and ageable caches. This should balance the seeks
170 * generated by these structures.
171 *
172 * If the vm encounted mapped pages on the LRU it increase the pressure on
173 * slab to avoid swapping.
174 *
175 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
176 *
177 * `lru_pages' represents the number of on-LRU pages in all the zones which
178 * are eligible for the caller's allocation attempt. It is used for balancing
179 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700180 *
181 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Andrew Morton9d0243b2006-01-08 01:00:39 -0800183int shrink_slab(unsigned long scanned, gfp_t gfp_mask, unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct shrinker *shrinker;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700186 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if (scanned == 0)
189 scanned = SWAP_CLUSTER_MAX;
190
191 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700192 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 list_for_each_entry(shrinker, &shrinker_list, list) {
195 unsigned long long delta;
196 unsigned long total_scan;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800197 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800200 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 do_div(delta, lru_pages + 1);
202 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800203 if (shrinker->nr < 0) {
204 printk(KERN_ERR "%s: nr=%ld\n",
205 __FUNCTION__, shrinker->nr);
206 shrinker->nr = max_pass;
207 }
208
209 /*
210 * Avoid risking looping forever due to too large nr value:
211 * never try to free more than twice the estimate number of
212 * freeable entries.
213 */
214 if (shrinker->nr > max_pass * 2)
215 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 total_scan = shrinker->nr;
218 shrinker->nr = 0;
219
220 while (total_scan >= SHRINK_BATCH) {
221 long this_scan = SHRINK_BATCH;
222 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700223 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700225 nr_before = (*shrinker->shrinker)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
227 if (shrink_ret == -1)
228 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700229 if (shrink_ret < nr_before)
230 ret += nr_before - shrink_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 mod_page_state(slabs_scanned, this_scan);
232 total_scan -= this_scan;
233
234 cond_resched();
235 }
236
237 shrinker->nr += total_scan;
238 }
239 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700240 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243/* Called without lock on whether page is mapped, so answer is unstable */
244static inline int page_mapping_inuse(struct page *page)
245{
246 struct address_space *mapping;
247
248 /* Page is in somebody's page tables. */
249 if (page_mapped(page))
250 return 1;
251
252 /* Be more reluctant to reclaim swapcache than pagecache */
253 if (PageSwapCache(page))
254 return 1;
255
256 mapping = page_mapping(page);
257 if (!mapping)
258 return 0;
259
260 /* File is mmap'd by somebody? */
261 return mapping_mapped(mapping);
262}
263
264static inline int is_page_cache_freeable(struct page *page)
265{
266 return page_count(page) - !!PagePrivate(page) == 2;
267}
268
269static int may_write_to_queue(struct backing_dev_info *bdi)
270{
Christoph Lameter930d9152006-01-08 01:00:47 -0800271 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 1;
273 if (!bdi_write_congested(bdi))
274 return 1;
275 if (bdi == current->backing_dev_info)
276 return 1;
277 return 0;
278}
279
280/*
281 * We detected a synchronous write error writing a page out. Probably
282 * -ENOSPC. We need to propagate that into the address_space for a subsequent
283 * fsync(), msync() or close().
284 *
285 * The tricky part is that after writepage we cannot touch the mapping: nothing
286 * prevents it from being freed up. But we have a ref on the page and once
287 * that page is locked, the mapping is pinned.
288 *
289 * We're allowed to run sleeping lock_page() here because we know the caller has
290 * __GFP_FS.
291 */
292static void handle_write_error(struct address_space *mapping,
293 struct page *page, int error)
294{
295 lock_page(page);
296 if (page_mapping(page) == mapping) {
297 if (error == -ENOSPC)
298 set_bit(AS_ENOSPC, &mapping->flags);
299 else
300 set_bit(AS_EIO, &mapping->flags);
301 }
302 unlock_page(page);
303}
304
305/*
306 * pageout is called by shrink_list() for each dirty page. Calls ->writepage().
307 */
308static pageout_t pageout(struct page *page, struct address_space *mapping)
309{
310 /*
311 * If the page is dirty, only perform writeback if that write
312 * will be non-blocking. To prevent this allocation from being
313 * stalled by pagecache activity. But note that there may be
314 * stalls if we need to run get_block(). We could test
315 * PagePrivate for that.
316 *
317 * If this process is currently in generic_file_write() against
318 * this page's queue, we can perform writeback even if that
319 * will block.
320 *
321 * If the page is swapcache, write it back even if that would
322 * block, for some throttling. This happens by accident, because
323 * swap_backing_dev_info is bust: it doesn't reflect the
324 * congestion state of the swapdevs. Easy to fix, if needed.
325 * See swapfile.c:page_queue_congested().
326 */
327 if (!is_page_cache_freeable(page))
328 return PAGE_KEEP;
329 if (!mapping) {
330 /*
331 * Some data journaling orphaned pages can have
332 * page->mapping == NULL while being dirty with clean buffers.
333 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700334 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (try_to_free_buffers(page)) {
336 ClearPageDirty(page);
337 printk("%s: orphaned page\n", __FUNCTION__);
338 return PAGE_CLEAN;
339 }
340 }
341 return PAGE_KEEP;
342 }
343 if (mapping->a_ops->writepage == NULL)
344 return PAGE_ACTIVATE;
345 if (!may_write_to_queue(mapping->backing_dev_info))
346 return PAGE_KEEP;
347
348 if (clear_page_dirty_for_io(page)) {
349 int res;
350 struct writeback_control wbc = {
351 .sync_mode = WB_SYNC_NONE,
352 .nr_to_write = SWAP_CLUSTER_MAX,
353 .nonblocking = 1,
354 .for_reclaim = 1,
355 };
356
357 SetPageReclaim(page);
358 res = mapping->a_ops->writepage(page, &wbc);
359 if (res < 0)
360 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800361 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 ClearPageReclaim(page);
363 return PAGE_ACTIVATE;
364 }
365 if (!PageWriteback(page)) {
366 /* synchronous write or broken a_ops? */
367 ClearPageReclaim(page);
368 }
369
370 return PAGE_SUCCESS;
371 }
372
373 return PAGE_CLEAN;
374}
375
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800376static int remove_mapping(struct address_space *mapping, struct page *page)
377{
378 if (!mapping)
379 return 0; /* truncate got there first */
380
381 write_lock_irq(&mapping->tree_lock);
382
383 /*
384 * The non-racy check for busy page. It is critical to check
385 * PageDirty _after_ making sure that the page is freeable and
386 * not in use by anybody. (pagecache + us == 2)
387 */
388 if (unlikely(page_count(page) != 2))
389 goto cannot_free;
390 smp_rmb();
391 if (unlikely(PageDirty(page)))
392 goto cannot_free;
393
394 if (PageSwapCache(page)) {
395 swp_entry_t swap = { .val = page_private(page) };
396 __delete_from_swap_cache(page);
397 write_unlock_irq(&mapping->tree_lock);
398 swap_free(swap);
399 __put_page(page); /* The pagecache ref */
400 return 1;
401 }
402
403 __remove_from_page_cache(page);
404 write_unlock_irq(&mapping->tree_lock);
405 __put_page(page);
406 return 1;
407
408cannot_free:
409 write_unlock_irq(&mapping->tree_lock);
410 return 0;
411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/*
414 * shrink_list adds the number of reclaimed pages to sc->nr_reclaimed
415 */
416static int shrink_list(struct list_head *page_list, struct scan_control *sc)
417{
418 LIST_HEAD(ret_pages);
419 struct pagevec freed_pvec;
420 int pgactivate = 0;
421 int reclaimed = 0;
422
423 cond_resched();
424
425 pagevec_init(&freed_pvec, 1);
426 while (!list_empty(page_list)) {
427 struct address_space *mapping;
428 struct page *page;
429 int may_enter_fs;
430 int referenced;
431
432 cond_resched();
433
434 page = lru_to_page(page_list);
435 list_del(&page->lru);
436
437 if (TestSetPageLocked(page))
438 goto keep;
439
440 BUG_ON(PageActive(page));
441
442 sc->nr_scanned++;
443 /* Double the slab pressure for mapped and swapcache pages */
444 if (page_mapped(page) || PageSwapCache(page))
445 sc->nr_scanned++;
446
447 if (PageWriteback(page))
448 goto keep_locked;
449
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800450 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* In active use or really unfreeable? Activate it. */
452 if (referenced && page_mapping_inuse(page))
453 goto activate_locked;
454
455#ifdef CONFIG_SWAP
456 /*
457 * Anonymous process memory has backing store?
458 * Try to allocate it some swap space here.
459 */
Lee Schermerhornc3400102005-10-29 18:15:51 -0700460 if (PageAnon(page) && !PageSwapCache(page)) {
Christoph Lameter1480a542006-01-08 01:00:53 -0800461 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto activate_locked;
463 }
464#endif /* CONFIG_SWAP */
465
466 mapping = page_mapping(page);
467 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
468 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
469
470 /*
471 * The page is mapped into the page tables of one or more
472 * processes. Try to unmap it here.
473 */
474 if (page_mapped(page) && mapping) {
475 switch (try_to_unmap(page)) {
476 case SWAP_FAIL:
477 goto activate_locked;
478 case SWAP_AGAIN:
479 goto keep_locked;
480 case SWAP_SUCCESS:
481 ; /* try to free the page below */
482 }
483 }
484
485 if (PageDirty(page)) {
486 if (referenced)
487 goto keep_locked;
488 if (!may_enter_fs)
489 goto keep_locked;
490 if (laptop_mode && !sc->may_writepage)
491 goto keep_locked;
492
493 /* Page is dirty, try to write it out here */
494 switch(pageout(page, mapping)) {
495 case PAGE_KEEP:
496 goto keep_locked;
497 case PAGE_ACTIVATE:
498 goto activate_locked;
499 case PAGE_SUCCESS:
500 if (PageWriteback(page) || PageDirty(page))
501 goto keep;
502 /*
503 * A synchronous write - probably a ramdisk. Go
504 * ahead and try to reclaim the page.
505 */
506 if (TestSetPageLocked(page))
507 goto keep;
508 if (PageDirty(page) || PageWriteback(page))
509 goto keep_locked;
510 mapping = page_mapping(page);
511 case PAGE_CLEAN:
512 ; /* try to free the page below */
513 }
514 }
515
516 /*
517 * If the page has buffers, try to free the buffer mappings
518 * associated with this page. If we succeed we try to free
519 * the page as well.
520 *
521 * We do this even if the page is PageDirty().
522 * try_to_release_page() does not perform I/O, but it is
523 * possible for a page to have PageDirty set, but it is actually
524 * clean (all its buffers are clean). This happens if the
525 * buffers were written out directly, with submit_bh(). ext3
526 * will do this, as well as the blockdev mapping.
527 * try_to_release_page() will discover that cleanness and will
528 * drop the buffers and mark the page clean - it can be freed.
529 *
530 * Rarely, pages can have buffers and no ->mapping. These are
531 * the pages which were not successfully invalidated in
532 * truncate_complete_page(). We try to drop those buffers here
533 * and if that worked, and the page is no longer mapped into
534 * process address space (page_count == 1) it can be freed.
535 * Otherwise, leave the page on the LRU so it is swappable.
536 */
537 if (PagePrivate(page)) {
538 if (!try_to_release_page(page, sc->gfp_mask))
539 goto activate_locked;
540 if (!mapping && page_count(page) == 1)
541 goto free_it;
542 }
543
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800544 if (!remove_mapping(mapping, page))
545 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547free_it:
548 unlock_page(page);
549 reclaimed++;
550 if (!pagevec_add(&freed_pvec, page))
551 __pagevec_release_nonlru(&freed_pvec);
552 continue;
553
554activate_locked:
555 SetPageActive(page);
556 pgactivate++;
557keep_locked:
558 unlock_page(page);
559keep:
560 list_add(&page->lru, &ret_pages);
561 BUG_ON(PageLRU(page));
562 }
563 list_splice(&ret_pages, page_list);
564 if (pagevec_count(&freed_pvec))
565 __pagevec_release_nonlru(&freed_pvec);
566 mod_page_state(pgactivate, pgactivate);
567 sc->nr_reclaimed += reclaimed;
568 return reclaimed;
569}
570
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800571#ifdef CONFIG_MIGRATION
Christoph Lameter8419c312006-01-08 01:00:52 -0800572static inline void move_to_lru(struct page *page)
573{
574 list_del(&page->lru);
575 if (PageActive(page)) {
576 /*
577 * lru_cache_add_active checks that
578 * the PG_active bit is off.
579 */
580 ClearPageActive(page);
581 lru_cache_add_active(page);
582 } else {
583 lru_cache_add(page);
584 }
585 put_page(page);
586}
587
588/*
Nick Piggin053837f2006-01-18 17:42:27 -0800589 * Add isolated pages on the list back to the LRU.
Christoph Lameter8419c312006-01-08 01:00:52 -0800590 *
591 * returns the number of pages put back.
592 */
593int putback_lru_pages(struct list_head *l)
594{
595 struct page *page;
596 struct page *page2;
597 int count = 0;
598
599 list_for_each_entry_safe(page, page2, l, lru) {
600 move_to_lru(page);
601 count++;
602 }
603 return count;
604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/*
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800607 * swapout a single page
608 * page is locked upon entry, unlocked on exit
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800609 */
610static int swap_page(struct page *page)
611{
612 struct address_space *mapping = page_mapping(page);
613
614 if (page_mapped(page) && mapping)
615 if (try_to_unmap(page) != SWAP_SUCCESS)
616 goto unlock_retry;
617
618 if (PageDirty(page)) {
619 /* Page is dirty, try to write it out here */
620 switch(pageout(page, mapping)) {
621 case PAGE_KEEP:
622 case PAGE_ACTIVATE:
623 goto unlock_retry;
624
625 case PAGE_SUCCESS:
626 goto retry;
627
628 case PAGE_CLEAN:
629 ; /* try to free the page below */
630 }
631 }
632
633 if (PagePrivate(page)) {
634 if (!try_to_release_page(page, GFP_KERNEL) ||
635 (!mapping && page_count(page) == 1))
636 goto unlock_retry;
637 }
638
639 if (remove_mapping(mapping, page)) {
640 /* Success */
641 unlock_page(page);
642 return 0;
643 }
644
645unlock_retry:
646 unlock_page(page);
647
648retry:
Christoph Lameterd0d96322006-01-08 01:00:55 -0800649 return -EAGAIN;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800650}
651/*
652 * migrate_pages
653 *
654 * Two lists are passed to this function. The first list
655 * contains the pages isolated from the LRU to be migrated.
656 * The second list contains new pages that the pages isolated
657 * can be moved to. If the second list is NULL then all
658 * pages are swapped out.
659 *
660 * The function returns after 10 attempts or if no pages
661 * are movable anymore because t has become empty
662 * or no retryable pages exist anymore.
663 *
664 * SIMPLIFIED VERSION: This implementation of migrate_pages
665 * is only swapping out pages and never touches the second
666 * list. The direct migration patchset
667 * extends this function to avoid the use of swap.
Christoph Lameterd0d96322006-01-08 01:00:55 -0800668 *
669 * Return: Number of pages not migrated when "to" ran empty.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800670 */
Christoph Lameterd4984712006-01-08 01:00:55 -0800671int migrate_pages(struct list_head *from, struct list_head *to,
672 struct list_head *moved, struct list_head *failed)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800673{
674 int retry;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800675 int nr_failed = 0;
676 int pass = 0;
677 struct page *page;
678 struct page *page2;
679 int swapwrite = current->flags & PF_SWAPWRITE;
Christoph Lameterd0d96322006-01-08 01:00:55 -0800680 int rc;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800681
682 if (!swapwrite)
683 current->flags |= PF_SWAPWRITE;
684
685redo:
686 retry = 0;
687
Christoph Lameterd4984712006-01-08 01:00:55 -0800688 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800689 cond_resched();
690
Christoph Lameterd0d96322006-01-08 01:00:55 -0800691 rc = 0;
692 if (page_count(page) == 1)
Christoph Lameteree274972006-01-08 01:00:54 -0800693 /* page was freed from under us. So we are done. */
Christoph Lameterd0d96322006-01-08 01:00:55 -0800694 goto next;
695
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800696 /*
697 * Skip locked pages during the first two passes to give the
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800698 * functions holding the lock time to release the page. Later we
699 * use lock_page() to have a higher chance of acquiring the
700 * lock.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800701 */
Christoph Lameterd0d96322006-01-08 01:00:55 -0800702 rc = -EAGAIN;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800703 if (pass > 2)
704 lock_page(page);
705 else
706 if (TestSetPageLocked(page))
Christoph Lameterd0d96322006-01-08 01:00:55 -0800707 goto next;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800708
709 /*
710 * Only wait on writeback if we have already done a pass where
711 * we we may have triggered writeouts for lots of pages.
712 */
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800713 if (pass > 0) {
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800714 wait_on_page_writeback(page);
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800715 } else {
Christoph Lameterd0d96322006-01-08 01:00:55 -0800716 if (PageWriteback(page))
717 goto unlock_page;
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800718 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800719
Christoph Lameterd0d96322006-01-08 01:00:55 -0800720 /*
721 * Anonymous pages must have swap cache references otherwise
722 * the information contained in the page maps cannot be
723 * preserved.
724 */
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800725 if (PageAnon(page) && !PageSwapCache(page)) {
Christoph Lameter1480a542006-01-08 01:00:53 -0800726 if (!add_to_swap(page, GFP_KERNEL)) {
Christoph Lameterd0d96322006-01-08 01:00:55 -0800727 rc = -ENOMEM;
728 goto unlock_page;
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800729 }
730 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800731
732 /*
733 * Page is properly locked and writeback is complete.
734 * Try to migrate the page.
735 */
Christoph Lameterd0d96322006-01-08 01:00:55 -0800736 rc = swap_page(page);
737 goto next;
738
739unlock_page:
740 unlock_page(page);
741
742next:
743 if (rc == -EAGAIN) {
744 retry++;
745 } else if (rc) {
746 /* Permanent failure */
747 list_move(&page->lru, failed);
748 nr_failed++;
749 } else {
750 /* Success */
Christoph Lameterd4984712006-01-08 01:00:55 -0800751 list_move(&page->lru, moved);
Christoph Lameterd4984712006-01-08 01:00:55 -0800752 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800753 }
754 if (retry && pass++ < 10)
755 goto redo;
756
757 if (!swapwrite)
758 current->flags &= ~PF_SWAPWRITE;
759
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800760 return nr_failed + retry;
761}
Christoph Lameter8419c312006-01-08 01:00:52 -0800762
Christoph Lameter8419c312006-01-08 01:00:52 -0800763/*
764 * Isolate one page from the LRU lists and put it on the
Nick Piggin053837f2006-01-18 17:42:27 -0800765 * indicated list with elevated refcount.
Christoph Lameter8419c312006-01-08 01:00:52 -0800766 *
767 * Result:
768 * 0 = page not on LRU list
769 * 1 = page removed from LRU list and added to the specified list.
Christoph Lameter8419c312006-01-08 01:00:52 -0800770 */
771int isolate_lru_page(struct page *page)
772{
Nick Piggin053837f2006-01-18 17:42:27 -0800773 int ret = 0;
Christoph Lameter8419c312006-01-08 01:00:52 -0800774
Nick Piggin053837f2006-01-18 17:42:27 -0800775 if (PageLRU(page)) {
776 struct zone *zone = page_zone(page);
777 spin_lock_irq(&zone->lru_lock);
778 if (TestClearPageLRU(page)) {
779 ret = 1;
780 get_page(page);
781 if (PageActive(page))
782 del_page_from_active_list(zone, page);
783 else
784 del_page_from_inactive_list(zone, page);
785 }
786 spin_unlock_irq(&zone->lru_lock);
Christoph Lameter8419c312006-01-08 01:00:52 -0800787 }
Nick Piggin053837f2006-01-18 17:42:27 -0800788
789 return ret;
Christoph Lameter8419c312006-01-08 01:00:52 -0800790}
Christoph Lameter7cbe34c2006-01-08 01:00:49 -0800791#endif
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800792
793/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 * zone->lru_lock is heavily contended. Some of the functions that
795 * shrink the lists perform better by taking out a batch of pages
796 * and working on them outside the LRU lock.
797 *
798 * For pagecache intensive workloads, this function is the hottest
799 * spot in the kernel (apart from copy_*_user functions).
800 *
801 * Appropriate locks must be held before calling this function.
802 *
803 * @nr_to_scan: The number of pages to look through on the list.
804 * @src: The LRU list to pull pages off.
805 * @dst: The temp list to put pages on to.
806 * @scanned: The number of pages that were scanned.
807 *
808 * returns how many pages were moved onto *@dst.
809 */
810static int isolate_lru_pages(int nr_to_scan, struct list_head *src,
811 struct list_head *dst, int *scanned)
812{
813 int nr_taken = 0;
814 struct page *page;
815 int scan = 0;
816
817 while (scan++ < nr_to_scan && !list_empty(src)) {
818 page = lru_to_page(src);
819 prefetchw_prev_lru_page(page, src, flags);
820
Nick Piggin053837f2006-01-18 17:42:27 -0800821 if (!TestClearPageLRU(page))
Christoph Lameter21eac812006-01-08 01:00:45 -0800822 BUG();
Nick Piggin053837f2006-01-18 17:42:27 -0800823 list_del(&page->lru);
824 if (get_page_testone(page)) {
825 /*
826 * It is being freed elsewhere
827 */
828 __put_page(page);
829 SetPageLRU(page);
830 list_add(&page->lru, src);
831 continue;
832 } else {
833 list_add(&page->lru, dst);
834 nr_taken++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 }
837
838 *scanned = scan;
839 return nr_taken;
840}
841
842/*
843 * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed
844 */
845static void shrink_cache(struct zone *zone, struct scan_control *sc)
846{
847 LIST_HEAD(page_list);
848 struct pagevec pvec;
849 int max_scan = sc->nr_to_scan;
850
851 pagevec_init(&pvec, 1);
852
853 lru_add_drain();
854 spin_lock_irq(&zone->lru_lock);
855 while (max_scan > 0) {
856 struct page *page;
857 int nr_taken;
858 int nr_scan;
859 int nr_freed;
860
861 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
862 &zone->inactive_list,
863 &page_list, &nr_scan);
864 zone->nr_inactive -= nr_taken;
865 zone->pages_scanned += nr_scan;
866 spin_unlock_irq(&zone->lru_lock);
867
868 if (nr_taken == 0)
869 goto done;
870
871 max_scan -= nr_scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 nr_freed = shrink_list(&page_list, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Nick Piggina74609f2006-01-06 00:11:20 -0800874 local_irq_disable();
875 if (current_is_kswapd()) {
876 __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
877 __mod_page_state(kswapd_steal, nr_freed);
878 } else
879 __mod_page_state_zone(zone, pgscan_direct, nr_scan);
880 __mod_page_state_zone(zone, pgsteal, nr_freed);
881
882 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /*
884 * Put back any unfreeable pages.
885 */
886 while (!list_empty(&page_list)) {
887 page = lru_to_page(&page_list);
888 if (TestSetPageLRU(page))
889 BUG();
890 list_del(&page->lru);
891 if (PageActive(page))
892 add_page_to_active_list(zone, page);
893 else
894 add_page_to_inactive_list(zone, page);
895 if (!pagevec_add(&pvec, page)) {
896 spin_unlock_irq(&zone->lru_lock);
897 __pagevec_release(&pvec);
898 spin_lock_irq(&zone->lru_lock);
899 }
900 }
901 }
902 spin_unlock_irq(&zone->lru_lock);
903done:
904 pagevec_release(&pvec);
905}
906
907/*
908 * This moves pages from the active list to the inactive list.
909 *
910 * We move them the other way if the page is referenced by one or more
911 * processes, from rmap.
912 *
913 * If the pages are mostly unmapped, the processing is fast and it is
914 * appropriate to hold zone->lru_lock across the whole operation. But if
915 * the pages are mapped, the processing is slow (page_referenced()) so we
916 * should drop zone->lru_lock around each page. It's impossible to balance
917 * this, so instead we remove the pages from the LRU while processing them.
918 * It is safe to rely on PG_active against the non-LRU pages in here because
919 * nobody will play with that bit on a non-LRU page.
920 *
921 * The downside is that we have to touch page->_count against each page.
922 * But we had to alter page->flags anyway.
923 */
924static void
925refill_inactive_zone(struct zone *zone, struct scan_control *sc)
926{
927 int pgmoved;
928 int pgdeactivate = 0;
929 int pgscanned;
930 int nr_pages = sc->nr_to_scan;
931 LIST_HEAD(l_hold); /* The pages which were snipped off */
932 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
933 LIST_HEAD(l_active); /* Pages to go onto the active_list */
934 struct page *page;
935 struct pagevec pvec;
936 int reclaim_mapped = 0;
937 long mapped_ratio;
938 long distress;
939 long swap_tendency;
940
941 lru_add_drain();
942 spin_lock_irq(&zone->lru_lock);
943 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
944 &l_hold, &pgscanned);
945 zone->pages_scanned += pgscanned;
946 zone->nr_active -= pgmoved;
947 spin_unlock_irq(&zone->lru_lock);
948
949 /*
950 * `distress' is a measure of how much trouble we're having reclaiming
951 * pages. 0 -> no problems. 100 -> great trouble.
952 */
953 distress = 100 >> zone->prev_priority;
954
955 /*
956 * The point of this algorithm is to decide when to start reclaiming
957 * mapped memory instead of just pagecache. Work out how much memory
958 * is mapped.
959 */
960 mapped_ratio = (sc->nr_mapped * 100) / total_memory;
961
962 /*
963 * Now decide how much we really want to unmap some pages. The mapped
964 * ratio is downgraded - just because there's a lot of mapped memory
965 * doesn't necessarily mean that page reclaim isn't succeeding.
966 *
967 * The distress ratio is important - we don't want to start going oom.
968 *
969 * A 100% value of vm_swappiness overrides this algorithm altogether.
970 */
971 swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
972
973 /*
974 * Now use this metric to decide whether to start moving mapped memory
975 * onto the inactive list.
976 */
977 if (swap_tendency >= 100)
978 reclaim_mapped = 1;
979
980 while (!list_empty(&l_hold)) {
981 cond_resched();
982 page = lru_to_page(&l_hold);
983 list_del(&page->lru);
984 if (page_mapped(page)) {
985 if (!reclaim_mapped ||
986 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800987 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 list_add(&page->lru, &l_active);
989 continue;
990 }
991 }
992 list_add(&page->lru, &l_inactive);
993 }
994
995 pagevec_init(&pvec, 1);
996 pgmoved = 0;
997 spin_lock_irq(&zone->lru_lock);
998 while (!list_empty(&l_inactive)) {
999 page = lru_to_page(&l_inactive);
1000 prefetchw_prev_lru_page(page, &l_inactive, flags);
1001 if (TestSetPageLRU(page))
1002 BUG();
1003 if (!TestClearPageActive(page))
1004 BUG();
1005 list_move(&page->lru, &zone->inactive_list);
1006 pgmoved++;
1007 if (!pagevec_add(&pvec, page)) {
1008 zone->nr_inactive += pgmoved;
1009 spin_unlock_irq(&zone->lru_lock);
1010 pgdeactivate += pgmoved;
1011 pgmoved = 0;
1012 if (buffer_heads_over_limit)
1013 pagevec_strip(&pvec);
1014 __pagevec_release(&pvec);
1015 spin_lock_irq(&zone->lru_lock);
1016 }
1017 }
1018 zone->nr_inactive += pgmoved;
1019 pgdeactivate += pgmoved;
1020 if (buffer_heads_over_limit) {
1021 spin_unlock_irq(&zone->lru_lock);
1022 pagevec_strip(&pvec);
1023 spin_lock_irq(&zone->lru_lock);
1024 }
1025
1026 pgmoved = 0;
1027 while (!list_empty(&l_active)) {
1028 page = lru_to_page(&l_active);
1029 prefetchw_prev_lru_page(page, &l_active, flags);
1030 if (TestSetPageLRU(page))
1031 BUG();
1032 BUG_ON(!PageActive(page));
1033 list_move(&page->lru, &zone->active_list);
1034 pgmoved++;
1035 if (!pagevec_add(&pvec, page)) {
1036 zone->nr_active += pgmoved;
1037 pgmoved = 0;
1038 spin_unlock_irq(&zone->lru_lock);
1039 __pagevec_release(&pvec);
1040 spin_lock_irq(&zone->lru_lock);
1041 }
1042 }
1043 zone->nr_active += pgmoved;
Nick Piggina74609f2006-01-06 00:11:20 -08001044 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Nick Piggina74609f2006-01-06 00:11:20 -08001046 __mod_page_state_zone(zone, pgrefill, pgscanned);
1047 __mod_page_state(pgdeactivate, pgdeactivate);
1048 local_irq_enable();
1049
1050 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053/*
1054 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1055 */
1056static void
1057shrink_zone(struct zone *zone, struct scan_control *sc)
1058{
1059 unsigned long nr_active;
1060 unsigned long nr_inactive;
1061
Martin Hicks53e9a612005-09-03 15:54:51 -07001062 atomic_inc(&zone->reclaim_in_progress);
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /*
1065 * Add one to `nr_to_scan' just to make sure that the kernel will
1066 * slowly sift through the active list.
1067 */
1068 zone->nr_scan_active += (zone->nr_active >> sc->priority) + 1;
1069 nr_active = zone->nr_scan_active;
1070 if (nr_active >= sc->swap_cluster_max)
1071 zone->nr_scan_active = 0;
1072 else
1073 nr_active = 0;
1074
1075 zone->nr_scan_inactive += (zone->nr_inactive >> sc->priority) + 1;
1076 nr_inactive = zone->nr_scan_inactive;
1077 if (nr_inactive >= sc->swap_cluster_max)
1078 zone->nr_scan_inactive = 0;
1079 else
1080 nr_inactive = 0;
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 while (nr_active || nr_inactive) {
1083 if (nr_active) {
1084 sc->nr_to_scan = min(nr_active,
1085 (unsigned long)sc->swap_cluster_max);
1086 nr_active -= sc->nr_to_scan;
1087 refill_inactive_zone(zone, sc);
1088 }
1089
1090 if (nr_inactive) {
1091 sc->nr_to_scan = min(nr_inactive,
1092 (unsigned long)sc->swap_cluster_max);
1093 nr_inactive -= sc->nr_to_scan;
1094 shrink_cache(zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096 }
1097
1098 throttle_vm_writeout();
Martin Hicks53e9a612005-09-03 15:54:51 -07001099
1100 atomic_dec(&zone->reclaim_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
1103/*
1104 * This is the direct reclaim path, for page-allocating processes. We only
1105 * try to reclaim pages from zones which will satisfy the caller's allocation
1106 * request.
1107 *
1108 * We reclaim from a zone even if that zone is over pages_high. Because:
1109 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1110 * allocation or
1111 * b) The zones may be over pages_high but they must go *over* pages_high to
1112 * satisfy the `incremental min' zone defense algorithm.
1113 *
1114 * Returns the number of reclaimed pages.
1115 *
1116 * If a zone is deemed to be full of pinned pages then just give it a light
1117 * scan then give up on it.
1118 */
1119static void
1120shrink_caches(struct zone **zones, struct scan_control *sc)
1121{
1122 int i;
1123
1124 for (i = 0; zones[i] != NULL; i++) {
1125 struct zone *zone = zones[i];
1126
Con Kolivasf3fe6512006-01-06 00:11:15 -08001127 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 continue;
1129
Paul Jackson9bf22292005-09-06 15:18:12 -07001130 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 continue;
1132
1133 zone->temp_priority = sc->priority;
1134 if (zone->prev_priority > sc->priority)
1135 zone->prev_priority = sc->priority;
1136
1137 if (zone->all_unreclaimable && sc->priority != DEF_PRIORITY)
1138 continue; /* Let kswapd poll it */
1139
1140 shrink_zone(zone, sc);
1141 }
1142}
1143
1144/*
1145 * This is the main entry point to direct page reclaim.
1146 *
1147 * If a full scan of the inactive list fails to free enough memory then we
1148 * are "out of memory" and something needs to be killed.
1149 *
1150 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1151 * high - the zone may be full of dirty or under-writeback pages, which this
1152 * caller can't do much about. We kick pdflush and take explicit naps in the
1153 * hope that some of these pages can be written. But if the allocating task
1154 * holds filesystem locks which prevent writeout this might not work, and the
1155 * allocation attempt will fail.
1156 */
Al Viro6daa0e22005-10-21 03:18:50 -04001157int try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 int priority;
1160 int ret = 0;
1161 int total_scanned = 0, total_reclaimed = 0;
1162 struct reclaim_state *reclaim_state = current->reclaim_state;
1163 struct scan_control sc;
1164 unsigned long lru_pages = 0;
1165 int i;
1166
1167 sc.gfp_mask = gfp_mask;
1168 sc.may_writepage = 0;
1169
1170 inc_page_state(allocstall);
1171
1172 for (i = 0; zones[i] != NULL; i++) {
1173 struct zone *zone = zones[i];
1174
Paul Jackson9bf22292005-09-06 15:18:12 -07001175 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 continue;
1177
1178 zone->temp_priority = DEF_PRIORITY;
1179 lru_pages += zone->nr_active + zone->nr_inactive;
1180 }
1181
1182 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1183 sc.nr_mapped = read_page_state(nr_mapped);
1184 sc.nr_scanned = 0;
1185 sc.nr_reclaimed = 0;
1186 sc.priority = priority;
1187 sc.swap_cluster_max = SWAP_CLUSTER_MAX;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001188 if (!priority)
1189 disable_swap_token();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 shrink_caches(zones, &sc);
1191 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
1192 if (reclaim_state) {
1193 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
1194 reclaim_state->reclaimed_slab = 0;
1195 }
1196 total_scanned += sc.nr_scanned;
1197 total_reclaimed += sc.nr_reclaimed;
1198 if (total_reclaimed >= sc.swap_cluster_max) {
1199 ret = 1;
1200 goto out;
1201 }
1202
1203 /*
1204 * Try to write back as many pages as we just scanned. This
1205 * tends to cause slow streaming writers to write data to the
1206 * disk smoothly, at the dirtying rate, which is nice. But
1207 * that's undesirable in laptop mode, where we *want* lumpy
1208 * writeout. So in laptop mode, write out the whole world.
1209 */
1210 if (total_scanned > sc.swap_cluster_max + sc.swap_cluster_max/2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001211 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 sc.may_writepage = 1;
1213 }
1214
1215 /* Take a nap, wait for some writeback to complete */
1216 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
1217 blk_congestion_wait(WRITE, HZ/10);
1218 }
1219out:
1220 for (i = 0; zones[i] != 0; i++) {
1221 struct zone *zone = zones[i];
1222
Paul Jackson9bf22292005-09-06 15:18:12 -07001223 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 continue;
1225
1226 zone->prev_priority = zone->temp_priority;
1227 }
1228 return ret;
1229}
1230
1231/*
1232 * For kswapd, balance_pgdat() will work across all this node's zones until
1233 * they are all at pages_high.
1234 *
1235 * If `nr_pages' is non-zero then it is the number of pages which are to be
1236 * reclaimed, regardless of the zone occupancies. This is a software suspend
1237 * special.
1238 *
1239 * Returns the number of pages which were actually freed.
1240 *
1241 * There is special handling here for zones which are full of pinned pages.
1242 * This can happen if the pages are all mlocked, or if they are all used by
1243 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1244 * What we do is to detect the case where all pages in the zone have been
1245 * scanned twice and there has been zero successful reclaim. Mark the zone as
1246 * dead and from now on, only perform a short scan. Basically we're polling
1247 * the zone for when the problem goes away.
1248 *
1249 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1250 * zones which have free_pages > pages_high, but once a zone is found to have
1251 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1252 * of the number of free pages in the lower zones. This interoperates with
1253 * the page allocator fallback scheme to ensure that aging of pages is balanced
1254 * across the zones.
1255 */
1256static int balance_pgdat(pg_data_t *pgdat, int nr_pages, int order)
1257{
1258 int to_free = nr_pages;
1259 int all_zones_ok;
1260 int priority;
1261 int i;
1262 int total_scanned, total_reclaimed;
1263 struct reclaim_state *reclaim_state = current->reclaim_state;
1264 struct scan_control sc;
1265
1266loop_again:
1267 total_scanned = 0;
1268 total_reclaimed = 0;
1269 sc.gfp_mask = GFP_KERNEL;
1270 sc.may_writepage = 0;
1271 sc.nr_mapped = read_page_state(nr_mapped);
1272
1273 inc_page_state(pageoutrun);
1274
1275 for (i = 0; i < pgdat->nr_zones; i++) {
1276 struct zone *zone = pgdat->node_zones + i;
1277
1278 zone->temp_priority = DEF_PRIORITY;
1279 }
1280
1281 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1282 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1283 unsigned long lru_pages = 0;
1284
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001285 /* The swap token gets in the way of swapout... */
1286 if (!priority)
1287 disable_swap_token();
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 all_zones_ok = 1;
1290
1291 if (nr_pages == 0) {
1292 /*
1293 * Scan in the highmem->dma direction for the highest
1294 * zone which needs scanning
1295 */
1296 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1297 struct zone *zone = pgdat->node_zones + i;
1298
Con Kolivasf3fe6512006-01-06 00:11:15 -08001299 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 continue;
1301
1302 if (zone->all_unreclaimable &&
1303 priority != DEF_PRIORITY)
1304 continue;
1305
1306 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001307 zone->pages_high, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 end_zone = i;
1309 goto scan;
1310 }
1311 }
1312 goto out;
1313 } else {
1314 end_zone = pgdat->nr_zones - 1;
1315 }
1316scan:
1317 for (i = 0; i <= end_zone; i++) {
1318 struct zone *zone = pgdat->node_zones + i;
1319
1320 lru_pages += zone->nr_active + zone->nr_inactive;
1321 }
1322
1323 /*
1324 * Now scan the zone in the dma->highmem direction, stopping
1325 * at the last zone which needs scanning.
1326 *
1327 * We do this because the page allocator works in the opposite
1328 * direction. This prevents the page allocator from allocating
1329 * pages behind kswapd's direction of progress, which would
1330 * cause too much scanning of the lower zones.
1331 */
1332 for (i = 0; i <= end_zone; i++) {
1333 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001334 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Con Kolivasf3fe6512006-01-06 00:11:15 -08001336 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 continue;
1338
1339 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1340 continue;
1341
1342 if (nr_pages == 0) { /* Not software suspend */
1343 if (!zone_watermark_ok(zone, order,
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001344 zone->pages_high, end_zone, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 all_zones_ok = 0;
1346 }
1347 zone->temp_priority = priority;
1348 if (zone->prev_priority > priority)
1349 zone->prev_priority = priority;
1350 sc.nr_scanned = 0;
1351 sc.nr_reclaimed = 0;
1352 sc.priority = priority;
1353 sc.swap_cluster_max = nr_pages? nr_pages : SWAP_CLUSTER_MAX;
Martin Hicks1e7e5a92005-06-21 17:14:43 -07001354 atomic_inc(&zone->reclaim_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 shrink_zone(zone, &sc);
Martin Hicks1e7e5a92005-06-21 17:14:43 -07001356 atomic_dec(&zone->reclaim_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001358 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1359 lru_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
1361 total_reclaimed += sc.nr_reclaimed;
1362 total_scanned += sc.nr_scanned;
1363 if (zone->all_unreclaimable)
1364 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001365 if (nr_slab == 0 && zone->pages_scanned >=
1366 (zone->nr_active + zone->nr_inactive) * 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 zone->all_unreclaimable = 1;
1368 /*
1369 * If we've done a decent amount of scanning and
1370 * the reclaim ratio is low, start doing writepage
1371 * even in laptop mode
1372 */
1373 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
1374 total_scanned > total_reclaimed+total_reclaimed/2)
1375 sc.may_writepage = 1;
1376 }
1377 if (nr_pages && to_free > total_reclaimed)
1378 continue; /* swsusp: need to do more work */
1379 if (all_zones_ok)
1380 break; /* kswapd: all done */
1381 /*
1382 * OK, kswapd is getting into trouble. Take a nap, then take
1383 * another pass across the zones.
1384 */
1385 if (total_scanned && priority < DEF_PRIORITY - 2)
1386 blk_congestion_wait(WRITE, HZ/10);
1387
1388 /*
1389 * We do this so kswapd doesn't build up large priorities for
1390 * example when it is freeing in parallel with allocators. It
1391 * matches the direct reclaim path behaviour in terms of impact
1392 * on zone->*_priority.
1393 */
1394 if ((total_reclaimed >= SWAP_CLUSTER_MAX) && (!nr_pages))
1395 break;
1396 }
1397out:
1398 for (i = 0; i < pgdat->nr_zones; i++) {
1399 struct zone *zone = pgdat->node_zones + i;
1400
1401 zone->prev_priority = zone->temp_priority;
1402 }
1403 if (!all_zones_ok) {
1404 cond_resched();
1405 goto loop_again;
1406 }
1407
1408 return total_reclaimed;
1409}
1410
1411/*
1412 * The background pageout daemon, started as a kernel thread
1413 * from the init process.
1414 *
1415 * This basically trickles out pages so that we have _some_
1416 * free memory available even if there is no other activity
1417 * that frees anything up. This is needed for things like routing
1418 * etc, where we otherwise might have all activity going on in
1419 * asynchronous contexts that cannot page things out.
1420 *
1421 * If there are applications that are active memory-allocators
1422 * (most normal use), this basically shouldn't matter.
1423 */
1424static int kswapd(void *p)
1425{
1426 unsigned long order;
1427 pg_data_t *pgdat = (pg_data_t*)p;
1428 struct task_struct *tsk = current;
1429 DEFINE_WAIT(wait);
1430 struct reclaim_state reclaim_state = {
1431 .reclaimed_slab = 0,
1432 };
1433 cpumask_t cpumask;
1434
1435 daemonize("kswapd%d", pgdat->node_id);
1436 cpumask = node_to_cpumask(pgdat->node_id);
1437 if (!cpus_empty(cpumask))
1438 set_cpus_allowed(tsk, cpumask);
1439 current->reclaim_state = &reclaim_state;
1440
1441 /*
1442 * Tell the memory management that we're a "memory allocator",
1443 * and that if we need more memory we should get access to it
1444 * regardless (see "__alloc_pages()"). "kswapd" should
1445 * never get caught in the normal page freeing logic.
1446 *
1447 * (Kswapd normally doesn't need memory anyway, but sometimes
1448 * you need a small amount of memory in order to be able to
1449 * page out something else, and this flag essentially protects
1450 * us from recursively trying to free more memory as we're
1451 * trying to free the first piece of memory in the first place).
1452 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001453 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 order = 0;
1456 for ( ; ; ) {
1457 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001458
1459 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1462 new_order = pgdat->kswapd_max_order;
1463 pgdat->kswapd_max_order = 0;
1464 if (order < new_order) {
1465 /*
1466 * Don't sleep if someone wants a larger 'order'
1467 * allocation
1468 */
1469 order = new_order;
1470 } else {
1471 schedule();
1472 order = pgdat->kswapd_max_order;
1473 }
1474 finish_wait(&pgdat->kswapd_wait, &wait);
1475
1476 balance_pgdat(pgdat, 0, order);
1477 }
1478 return 0;
1479}
1480
1481/*
1482 * A zone is low on free memory, so wake its kswapd task to service it.
1483 */
1484void wakeup_kswapd(struct zone *zone, int order)
1485{
1486 pg_data_t *pgdat;
1487
Con Kolivasf3fe6512006-01-06 00:11:15 -08001488 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return;
1490
1491 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001492 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return;
1494 if (pgdat->kswapd_max_order < order)
1495 pgdat->kswapd_max_order = order;
Paul Jackson9bf22292005-09-06 15:18:12 -07001496 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001498 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001500 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
1503#ifdef CONFIG_PM
1504/*
1505 * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
1506 * pages.
1507 */
1508int shrink_all_memory(int nr_pages)
1509{
1510 pg_data_t *pgdat;
1511 int nr_to_free = nr_pages;
1512 int ret = 0;
1513 struct reclaim_state reclaim_state = {
1514 .reclaimed_slab = 0,
1515 };
1516
1517 current->reclaim_state = &reclaim_state;
1518 for_each_pgdat(pgdat) {
1519 int freed;
1520 freed = balance_pgdat(pgdat, nr_to_free, 0);
1521 ret += freed;
1522 nr_to_free -= freed;
1523 if (nr_to_free <= 0)
1524 break;
1525 }
1526 current->reclaim_state = NULL;
1527 return ret;
1528}
1529#endif
1530
1531#ifdef CONFIG_HOTPLUG_CPU
1532/* It's optimal to keep kswapds on the same CPUs as their memory, but
1533 not required for correctness. So if the last cpu in a node goes
1534 away, we get changed to run anywhere: as the first one comes back,
1535 restore their cpu bindings. */
1536static int __devinit cpu_callback(struct notifier_block *nfb,
1537 unsigned long action,
1538 void *hcpu)
1539{
1540 pg_data_t *pgdat;
1541 cpumask_t mask;
1542
1543 if (action == CPU_ONLINE) {
1544 for_each_pgdat(pgdat) {
1545 mask = node_to_cpumask(pgdat->node_id);
1546 if (any_online_cpu(mask) != NR_CPUS)
1547 /* One of our CPUs online: restore mask */
1548 set_cpus_allowed(pgdat->kswapd, mask);
1549 }
1550 }
1551 return NOTIFY_OK;
1552}
1553#endif /* CONFIG_HOTPLUG_CPU */
1554
1555static int __init kswapd_init(void)
1556{
1557 pg_data_t *pgdat;
1558 swap_setup();
1559 for_each_pgdat(pgdat)
1560 pgdat->kswapd
1561 = find_task_by_pid(kernel_thread(kswapd, pgdat, CLONE_KERNEL));
1562 total_memory = nr_free_pagecache_pages();
1563 hotcpu_notifier(cpu_callback, 0);
1564 return 0;
1565}
1566
1567module_init(kswapd_init)