[PATCH] mm: clean up pagecache allocation
[deliverable/linux.git] / mm / vmscan.c
CommitLineData
1da177e4
LT
1/*
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>
e129b5c2 22#include <linux/vmstat.h>
1da177e4
LT
23#include <linux/file.h>
24#include <linux/writeback.h>
25#include <linux/blkdev.h>
26#include <linux/buffer_head.h> /* for try_to_release_page(),
27 buffer_heads_over_limit */
28#include <linux/mm_inline.h>
29#include <linux/pagevec.h>
30#include <linux/backing-dev.h>
31#include <linux/rmap.h>
32#include <linux/topology.h>
33#include <linux/cpu.h>
34#include <linux/cpuset.h>
35#include <linux/notifier.h>
36#include <linux/rwsem.h>
248a0301 37#include <linux/delay.h>
3218ae14 38#include <linux/kthread.h>
1da177e4
LT
39
40#include <asm/tlbflush.h>
41#include <asm/div64.h>
42
43#include <linux/swapops.h>
44
0f8053a5
NP
45#include "internal.h"
46
1da177e4 47struct scan_control {
1da177e4
LT
48 /* Incremented by the number of inactive pages that were scanned */
49 unsigned long nr_scanned;
50
1da177e4 51 /* This context's GFP mask */
6daa0e28 52 gfp_t gfp_mask;
1da177e4
LT
53
54 int may_writepage;
55
f1fd1067
CL
56 /* Can pages be swapped as part of reclaim? */
57 int may_swap;
58
1da177e4
LT
59 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
60 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
61 * In this context, it doesn't matter that we scan the
62 * whole list at once. */
63 int swap_cluster_max;
d6277db4
RW
64
65 int swappiness;
408d8544
NP
66
67 int all_unreclaimable;
1da177e4
LT
68};
69
70/*
71 * The list of shrinker callbacks used by to apply pressure to
72 * ageable caches.
73 */
74struct shrinker {
75 shrinker_t shrinker;
76 struct list_head list;
77 int seeks; /* seeks to recreate an obj */
78 long nr; /* objs pending delete */
79};
80
81#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
82
83#ifdef ARCH_HAS_PREFETCH
84#define prefetch_prev_lru_page(_page, _base, _field) \
85 do { \
86 if ((_page)->lru.prev != _base) { \
87 struct page *prev; \
88 \
89 prev = lru_to_page(&(_page->lru)); \
90 prefetch(&prev->_field); \
91 } \
92 } while (0)
93#else
94#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
95#endif
96
97#ifdef ARCH_HAS_PREFETCHW
98#define prefetchw_prev_lru_page(_page, _base, _field) \
99 do { \
100 if ((_page)->lru.prev != _base) { \
101 struct page *prev; \
102 \
103 prev = lru_to_page(&(_page->lru)); \
104 prefetchw(&prev->_field); \
105 } \
106 } while (0)
107#else
108#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
109#endif
110
111/*
112 * From 0 .. 100. Higher means more swappy.
113 */
114int vm_swappiness = 60;
bd1e22b8 115long vm_total_pages; /* The total number of pages which the VM controls */
1da177e4
LT
116
117static LIST_HEAD(shrinker_list);
118static DECLARE_RWSEM(shrinker_rwsem);
119
120/*
121 * Add a shrinker callback to be called from the vm
122 */
123struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
124{
125 struct shrinker *shrinker;
126
127 shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
128 if (shrinker) {
129 shrinker->shrinker = theshrinker;
130 shrinker->seeks = seeks;
131 shrinker->nr = 0;
132 down_write(&shrinker_rwsem);
133 list_add_tail(&shrinker->list, &shrinker_list);
134 up_write(&shrinker_rwsem);
135 }
136 return shrinker;
137}
138EXPORT_SYMBOL(set_shrinker);
139
140/*
141 * Remove one
142 */
143void remove_shrinker(struct shrinker *shrinker)
144{
145 down_write(&shrinker_rwsem);
146 list_del(&shrinker->list);
147 up_write(&shrinker_rwsem);
148 kfree(shrinker);
149}
150EXPORT_SYMBOL(remove_shrinker);
151
152#define SHRINK_BATCH 128
153/*
154 * Call the shrink functions to age shrinkable caches
155 *
156 * Here we assume it costs one seek to replace a lru page and that it also
157 * takes a seek to recreate a cache object. With this in mind we age equal
158 * percentages of the lru and ageable caches. This should balance the seeks
159 * generated by these structures.
160 *
161 * If the vm encounted mapped pages on the LRU it increase the pressure on
162 * slab to avoid swapping.
163 *
164 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
165 *
166 * `lru_pages' represents the number of on-LRU pages in all the zones which
167 * are eligible for the caller's allocation attempt. It is used for balancing
168 * slab reclaim versus page reclaim.
b15e0905 169 *
170 * Returns the number of slab objects which we shrunk.
1da177e4 171 */
69e05944
AM
172unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
173 unsigned long lru_pages)
1da177e4
LT
174{
175 struct shrinker *shrinker;
69e05944 176 unsigned long ret = 0;
1da177e4
LT
177
178 if (scanned == 0)
179 scanned = SWAP_CLUSTER_MAX;
180
181 if (!down_read_trylock(&shrinker_rwsem))
b15e0905 182 return 1; /* Assume we'll be able to shrink next time */
1da177e4
LT
183
184 list_for_each_entry(shrinker, &shrinker_list, list) {
185 unsigned long long delta;
186 unsigned long total_scan;
ea164d73 187 unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
1da177e4
LT
188
189 delta = (4 * scanned) / shrinker->seeks;
ea164d73 190 delta *= max_pass;
1da177e4
LT
191 do_div(delta, lru_pages + 1);
192 shrinker->nr += delta;
ea164d73
AA
193 if (shrinker->nr < 0) {
194 printk(KERN_ERR "%s: nr=%ld\n",
195 __FUNCTION__, shrinker->nr);
196 shrinker->nr = max_pass;
197 }
198
199 /*
200 * Avoid risking looping forever due to too large nr value:
201 * never try to free more than twice the estimate number of
202 * freeable entries.
203 */
204 if (shrinker->nr > max_pass * 2)
205 shrinker->nr = max_pass * 2;
1da177e4
LT
206
207 total_scan = shrinker->nr;
208 shrinker->nr = 0;
209
210 while (total_scan >= SHRINK_BATCH) {
211 long this_scan = SHRINK_BATCH;
212 int shrink_ret;
b15e0905 213 int nr_before;
1da177e4 214
b15e0905 215 nr_before = (*shrinker->shrinker)(0, gfp_mask);
1da177e4
LT
216 shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
217 if (shrink_ret == -1)
218 break;
b15e0905 219 if (shrink_ret < nr_before)
220 ret += nr_before - shrink_ret;
f8891e5e 221 count_vm_events(SLABS_SCANNED, this_scan);
1da177e4
LT
222 total_scan -= this_scan;
223
224 cond_resched();
225 }
226
227 shrinker->nr += total_scan;
228 }
229 up_read(&shrinker_rwsem);
b15e0905 230 return ret;
1da177e4
LT
231}
232
233/* Called without lock on whether page is mapped, so answer is unstable */
234static inline int page_mapping_inuse(struct page *page)
235{
236 struct address_space *mapping;
237
238 /* Page is in somebody's page tables. */
239 if (page_mapped(page))
240 return 1;
241
242 /* Be more reluctant to reclaim swapcache than pagecache */
243 if (PageSwapCache(page))
244 return 1;
245
246 mapping = page_mapping(page);
247 if (!mapping)
248 return 0;
249
250 /* File is mmap'd by somebody? */
251 return mapping_mapped(mapping);
252}
253
254static inline int is_page_cache_freeable(struct page *page)
255{
256 return page_count(page) - !!PagePrivate(page) == 2;
257}
258
259static int may_write_to_queue(struct backing_dev_info *bdi)
260{
930d9152 261 if (current->flags & PF_SWAPWRITE)
1da177e4
LT
262 return 1;
263 if (!bdi_write_congested(bdi))
264 return 1;
265 if (bdi == current->backing_dev_info)
266 return 1;
267 return 0;
268}
269
270/*
271 * We detected a synchronous write error writing a page out. Probably
272 * -ENOSPC. We need to propagate that into the address_space for a subsequent
273 * fsync(), msync() or close().
274 *
275 * The tricky part is that after writepage we cannot touch the mapping: nothing
276 * prevents it from being freed up. But we have a ref on the page and once
277 * that page is locked, the mapping is pinned.
278 *
279 * We're allowed to run sleeping lock_page() here because we know the caller has
280 * __GFP_FS.
281 */
282static void handle_write_error(struct address_space *mapping,
283 struct page *page, int error)
284{
285 lock_page(page);
286 if (page_mapping(page) == mapping) {
287 if (error == -ENOSPC)
288 set_bit(AS_ENOSPC, &mapping->flags);
289 else
290 set_bit(AS_EIO, &mapping->flags);
291 }
292 unlock_page(page);
293}
294
04e62a29
CL
295/* possible outcome of pageout() */
296typedef enum {
297 /* failed to write page out, page is locked */
298 PAGE_KEEP,
299 /* move page to the active list, page is locked */
300 PAGE_ACTIVATE,
301 /* page has been sent to the disk successfully, page is unlocked */
302 PAGE_SUCCESS,
303 /* page is clean and locked */
304 PAGE_CLEAN,
305} pageout_t;
306
1da177e4 307/*
1742f19f
AM
308 * pageout is called by shrink_page_list() for each dirty page.
309 * Calls ->writepage().
1da177e4 310 */
04e62a29 311static pageout_t pageout(struct page *page, struct address_space *mapping)
1da177e4
LT
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 */
323aca6c 337 if (PagePrivate(page)) {
1da177e4
LT
338 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,
111ebb6e
OH
356 .range_start = 0,
357 .range_end = LLONG_MAX,
1da177e4
LT
358 .nonblocking = 1,
359 .for_reclaim = 1,
360 };
361
362 SetPageReclaim(page);
363 res = mapping->a_ops->writepage(page, &wbc);
364 if (res < 0)
365 handle_write_error(mapping, page, res);
994fc28c 366 if (res == AOP_WRITEPAGE_ACTIVATE) {
1da177e4
LT
367 ClearPageReclaim(page);
368 return PAGE_ACTIVATE;
369 }
370 if (!PageWriteback(page)) {
371 /* synchronous write or broken a_ops? */
372 ClearPageReclaim(page);
373 }
e129b5c2 374 inc_zone_page_state(page, NR_VMSCAN_WRITE);
1da177e4
LT
375 return PAGE_SUCCESS;
376 }
377
378 return PAGE_CLEAN;
379}
380
a649fd92
AM
381/*
382 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
383 * someone else has a ref on the page, abort and return 0. If it was
384 * successfully detached, return 1. Assumes the caller has a single ref on
385 * this page.
386 */
b20a3503 387int remove_mapping(struct address_space *mapping, struct page *page)
49d2e9cc 388{
28e4d965
NP
389 BUG_ON(!PageLocked(page));
390 BUG_ON(mapping != page_mapping(page));
49d2e9cc
CL
391
392 write_lock_irq(&mapping->tree_lock);
49d2e9cc 393 /*
0fd0e6b0
NP
394 * The non racy check for a busy page.
395 *
396 * Must be careful with the order of the tests. When someone has
397 * a ref to the page, it may be possible that they dirty it then
398 * drop the reference. So if PageDirty is tested before page_count
399 * here, then the following race may occur:
400 *
401 * get_user_pages(&page);
402 * [user mapping goes away]
403 * write_to(page);
404 * !PageDirty(page) [good]
405 * SetPageDirty(page);
406 * put_page(page);
407 * !page_count(page) [good, discard it]
408 *
409 * [oops, our write_to data is lost]
410 *
411 * Reversing the order of the tests ensures such a situation cannot
412 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
413 * load is not satisfied before that of page->_count.
414 *
415 * Note that if SetPageDirty is always performed via set_page_dirty,
416 * and thus under tree_lock, then this ordering is not required.
49d2e9cc
CL
417 */
418 if (unlikely(page_count(page) != 2))
419 goto cannot_free;
420 smp_rmb();
421 if (unlikely(PageDirty(page)))
422 goto cannot_free;
423
424 if (PageSwapCache(page)) {
425 swp_entry_t swap = { .val = page_private(page) };
426 __delete_from_swap_cache(page);
427 write_unlock_irq(&mapping->tree_lock);
428 swap_free(swap);
429 __put_page(page); /* The pagecache ref */
430 return 1;
431 }
432
433 __remove_from_page_cache(page);
434 write_unlock_irq(&mapping->tree_lock);
435 __put_page(page);
436 return 1;
437
438cannot_free:
439 write_unlock_irq(&mapping->tree_lock);
440 return 0;
441}
442
1da177e4 443/*
1742f19f 444 * shrink_page_list() returns the number of reclaimed pages
1da177e4 445 */
1742f19f
AM
446static unsigned long shrink_page_list(struct list_head *page_list,
447 struct scan_control *sc)
1da177e4
LT
448{
449 LIST_HEAD(ret_pages);
450 struct pagevec freed_pvec;
451 int pgactivate = 0;
05ff5137 452 unsigned long nr_reclaimed = 0;
1da177e4
LT
453
454 cond_resched();
455
456 pagevec_init(&freed_pvec, 1);
457 while (!list_empty(page_list)) {
458 struct address_space *mapping;
459 struct page *page;
460 int may_enter_fs;
461 int referenced;
462
463 cond_resched();
464
465 page = lru_to_page(page_list);
466 list_del(&page->lru);
467
468 if (TestSetPageLocked(page))
469 goto keep;
470
725d704e 471 VM_BUG_ON(PageActive(page));
1da177e4
LT
472
473 sc->nr_scanned++;
80e43426
CL
474
475 if (!sc->may_swap && page_mapped(page))
476 goto keep_locked;
477
1da177e4
LT
478 /* Double the slab pressure for mapped and swapcache pages */
479 if (page_mapped(page) || PageSwapCache(page))
480 sc->nr_scanned++;
481
482 if (PageWriteback(page))
483 goto keep_locked;
484
f7b7fd8f 485 referenced = page_referenced(page, 1);
1da177e4
LT
486 /* In active use or really unfreeable? Activate it. */
487 if (referenced && page_mapping_inuse(page))
488 goto activate_locked;
489
490#ifdef CONFIG_SWAP
491 /*
492 * Anonymous process memory has backing store?
493 * Try to allocate it some swap space here.
494 */
6e5ef1a9 495 if (PageAnon(page) && !PageSwapCache(page))
1480a540 496 if (!add_to_swap(page, GFP_ATOMIC))
1da177e4 497 goto activate_locked;
1da177e4
LT
498#endif /* CONFIG_SWAP */
499
500 mapping = page_mapping(page);
501 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
502 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
503
504 /*
505 * The page is mapped into the page tables of one or more
506 * processes. Try to unmap it here.
507 */
508 if (page_mapped(page) && mapping) {
a48d07af 509 switch (try_to_unmap(page, 0)) {
1da177e4
LT
510 case SWAP_FAIL:
511 goto activate_locked;
512 case SWAP_AGAIN:
513 goto keep_locked;
514 case SWAP_SUCCESS:
515 ; /* try to free the page below */
516 }
517 }
518
519 if (PageDirty(page)) {
520 if (referenced)
521 goto keep_locked;
522 if (!may_enter_fs)
523 goto keep_locked;
52a8363e 524 if (!sc->may_writepage)
1da177e4
LT
525 goto keep_locked;
526
527 /* Page is dirty, try to write it out here */
528 switch(pageout(page, mapping)) {
529 case PAGE_KEEP:
530 goto keep_locked;
531 case PAGE_ACTIVATE:
532 goto activate_locked;
533 case PAGE_SUCCESS:
534 if (PageWriteback(page) || PageDirty(page))
535 goto keep;
536 /*
537 * A synchronous write - probably a ramdisk. Go
538 * ahead and try to reclaim the page.
539 */
540 if (TestSetPageLocked(page))
541 goto keep;
542 if (PageDirty(page) || PageWriteback(page))
543 goto keep_locked;
544 mapping = page_mapping(page);
545 case PAGE_CLEAN:
546 ; /* try to free the page below */
547 }
548 }
549
550 /*
551 * If the page has buffers, try to free the buffer mappings
552 * associated with this page. If we succeed we try to free
553 * the page as well.
554 *
555 * We do this even if the page is PageDirty().
556 * try_to_release_page() does not perform I/O, but it is
557 * possible for a page to have PageDirty set, but it is actually
558 * clean (all its buffers are clean). This happens if the
559 * buffers were written out directly, with submit_bh(). ext3
560 * will do this, as well as the blockdev mapping.
561 * try_to_release_page() will discover that cleanness and will
562 * drop the buffers and mark the page clean - it can be freed.
563 *
564 * Rarely, pages can have buffers and no ->mapping. These are
565 * the pages which were not successfully invalidated in
566 * truncate_complete_page(). We try to drop those buffers here
567 * and if that worked, and the page is no longer mapped into
568 * process address space (page_count == 1) it can be freed.
569 * Otherwise, leave the page on the LRU so it is swappable.
570 */
571 if (PagePrivate(page)) {
572 if (!try_to_release_page(page, sc->gfp_mask))
573 goto activate_locked;
574 if (!mapping && page_count(page) == 1)
575 goto free_it;
576 }
577
28e4d965 578 if (!mapping || !remove_mapping(mapping, page))
49d2e9cc 579 goto keep_locked;
1da177e4
LT
580
581free_it:
582 unlock_page(page);
05ff5137 583 nr_reclaimed++;
1da177e4
LT
584 if (!pagevec_add(&freed_pvec, page))
585 __pagevec_release_nonlru(&freed_pvec);
586 continue;
587
588activate_locked:
589 SetPageActive(page);
590 pgactivate++;
591keep_locked:
592 unlock_page(page);
593keep:
594 list_add(&page->lru, &ret_pages);
725d704e 595 VM_BUG_ON(PageLRU(page));
1da177e4
LT
596 }
597 list_splice(&ret_pages, page_list);
598 if (pagevec_count(&freed_pvec))
599 __pagevec_release_nonlru(&freed_pvec);
f8891e5e 600 count_vm_events(PGACTIVATE, pgactivate);
05ff5137 601 return nr_reclaimed;
1da177e4
LT
602}
603
604/*
605 * zone->lru_lock is heavily contended. Some of the functions that
606 * shrink the lists perform better by taking out a batch of pages
607 * and working on them outside the LRU lock.
608 *
609 * For pagecache intensive workloads, this function is the hottest
610 * spot in the kernel (apart from copy_*_user functions).
611 *
612 * Appropriate locks must be held before calling this function.
613 *
614 * @nr_to_scan: The number of pages to look through on the list.
615 * @src: The LRU list to pull pages off.
616 * @dst: The temp list to put pages on to.
617 * @scanned: The number of pages that were scanned.
618 *
619 * returns how many pages were moved onto *@dst.
620 */
69e05944
AM
621static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
622 struct list_head *src, struct list_head *dst,
623 unsigned long *scanned)
1da177e4 624{
69e05944 625 unsigned long nr_taken = 0;
1da177e4 626 struct page *page;
c9b02d97 627 unsigned long scan;
1da177e4 628
c9b02d97 629 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
7c8ee9a8 630 struct list_head *target;
1da177e4
LT
631 page = lru_to_page(src);
632 prefetchw_prev_lru_page(page, src, flags);
633
725d704e 634 VM_BUG_ON(!PageLRU(page));
8d438f96 635
053837fc 636 list_del(&page->lru);
7c8ee9a8
NP
637 target = src;
638 if (likely(get_page_unless_zero(page))) {
053837fc 639 /*
7c8ee9a8
NP
640 * Be careful not to clear PageLRU until after we're
641 * sure the page is not being freed elsewhere -- the
642 * page release code relies on it.
053837fc 643 */
7c8ee9a8
NP
644 ClearPageLRU(page);
645 target = dst;
646 nr_taken++;
647 } /* else it is being freed elsewhere */
46453a6e 648
7c8ee9a8 649 list_add(&page->lru, target);
1da177e4
LT
650 }
651
652 *scanned = scan;
653 return nr_taken;
654}
655
656/*
1742f19f
AM
657 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
658 * of reclaimed pages
1da177e4 659 */
1742f19f
AM
660static unsigned long shrink_inactive_list(unsigned long max_scan,
661 struct zone *zone, struct scan_control *sc)
1da177e4
LT
662{
663 LIST_HEAD(page_list);
664 struct pagevec pvec;
69e05944 665 unsigned long nr_scanned = 0;
05ff5137 666 unsigned long nr_reclaimed = 0;
1da177e4
LT
667
668 pagevec_init(&pvec, 1);
669
670 lru_add_drain();
671 spin_lock_irq(&zone->lru_lock);
69e05944 672 do {
1da177e4 673 struct page *page;
69e05944
AM
674 unsigned long nr_taken;
675 unsigned long nr_scan;
676 unsigned long nr_freed;
1da177e4
LT
677
678 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
679 &zone->inactive_list,
680 &page_list, &nr_scan);
681 zone->nr_inactive -= nr_taken;
682 zone->pages_scanned += nr_scan;
683 spin_unlock_irq(&zone->lru_lock);
684
69e05944 685 nr_scanned += nr_scan;
1742f19f 686 nr_freed = shrink_page_list(&page_list, sc);
05ff5137 687 nr_reclaimed += nr_freed;
a74609fa
NP
688 local_irq_disable();
689 if (current_is_kswapd()) {
f8891e5e
CL
690 __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
691 __count_vm_events(KSWAPD_STEAL, nr_freed);
a74609fa 692 } else
f8891e5e
CL
693 __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
694 __count_vm_events(PGACTIVATE, nr_freed);
a74609fa 695
fb8d14e1
WF
696 if (nr_taken == 0)
697 goto done;
698
a74609fa 699 spin_lock(&zone->lru_lock);
1da177e4
LT
700 /*
701 * Put back any unfreeable pages.
702 */
703 while (!list_empty(&page_list)) {
704 page = lru_to_page(&page_list);
725d704e 705 VM_BUG_ON(PageLRU(page));
8d438f96 706 SetPageLRU(page);
1da177e4
LT
707 list_del(&page->lru);
708 if (PageActive(page))
709 add_page_to_active_list(zone, page);
710 else
711 add_page_to_inactive_list(zone, page);
712 if (!pagevec_add(&pvec, page)) {
713 spin_unlock_irq(&zone->lru_lock);
714 __pagevec_release(&pvec);
715 spin_lock_irq(&zone->lru_lock);
716 }
717 }
69e05944 718 } while (nr_scanned < max_scan);
fb8d14e1 719 spin_unlock(&zone->lru_lock);
1da177e4 720done:
fb8d14e1 721 local_irq_enable();
1da177e4 722 pagevec_release(&pvec);
05ff5137 723 return nr_reclaimed;
1da177e4
LT
724}
725
4ff1ffb4
NP
726static inline int zone_is_near_oom(struct zone *zone)
727{
728 return zone->pages_scanned >= (zone->nr_active + zone->nr_inactive)*3;
729}
730
1da177e4
LT
731/*
732 * This moves pages from the active list to the inactive list.
733 *
734 * We move them the other way if the page is referenced by one or more
735 * processes, from rmap.
736 *
737 * If the pages are mostly unmapped, the processing is fast and it is
738 * appropriate to hold zone->lru_lock across the whole operation. But if
739 * the pages are mapped, the processing is slow (page_referenced()) so we
740 * should drop zone->lru_lock around each page. It's impossible to balance
741 * this, so instead we remove the pages from the LRU while processing them.
742 * It is safe to rely on PG_active against the non-LRU pages in here because
743 * nobody will play with that bit on a non-LRU page.
744 *
745 * The downside is that we have to touch page->_count against each page.
746 * But we had to alter page->flags anyway.
747 */
1742f19f
AM
748static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
749 struct scan_control *sc)
1da177e4 750{
69e05944 751 unsigned long pgmoved;
1da177e4 752 int pgdeactivate = 0;
69e05944 753 unsigned long pgscanned;
1da177e4
LT
754 LIST_HEAD(l_hold); /* The pages which were snipped off */
755 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
756 LIST_HEAD(l_active); /* Pages to go onto the active_list */
757 struct page *page;
758 struct pagevec pvec;
759 int reclaim_mapped = 0;
2903fb16 760
6e5ef1a9 761 if (sc->may_swap) {
2903fb16
CL
762 long mapped_ratio;
763 long distress;
764 long swap_tendency;
765
4ff1ffb4
NP
766 if (zone_is_near_oom(zone))
767 goto force_reclaim_mapped;
768
2903fb16
CL
769 /*
770 * `distress' is a measure of how much trouble we're having
771 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
772 */
773 distress = 100 >> zone->prev_priority;
774
775 /*
776 * The point of this algorithm is to decide when to start
777 * reclaiming mapped memory instead of just pagecache. Work out
778 * how much memory
779 * is mapped.
780 */
f3dbd344
CL
781 mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
782 global_page_state(NR_ANON_PAGES)) * 100) /
bf02cf4b 783 vm_total_pages;
2903fb16
CL
784
785 /*
786 * Now decide how much we really want to unmap some pages. The
787 * mapped ratio is downgraded - just because there's a lot of
788 * mapped memory doesn't necessarily mean that page reclaim
789 * isn't succeeding.
790 *
791 * The distress ratio is important - we don't want to start
792 * going oom.
793 *
794 * A 100% value of vm_swappiness overrides this algorithm
795 * altogether.
796 */
d6277db4 797 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
2903fb16
CL
798
799 /*
800 * Now use this metric to decide whether to start moving mapped
801 * memory onto the inactive list.
802 */
803 if (swap_tendency >= 100)
4ff1ffb4 804force_reclaim_mapped:
2903fb16
CL
805 reclaim_mapped = 1;
806 }
1da177e4
LT
807
808 lru_add_drain();
809 spin_lock_irq(&zone->lru_lock);
810 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
811 &l_hold, &pgscanned);
812 zone->pages_scanned += pgscanned;
813 zone->nr_active -= pgmoved;
814 spin_unlock_irq(&zone->lru_lock);
815
1da177e4
LT
816 while (!list_empty(&l_hold)) {
817 cond_resched();
818 page = lru_to_page(&l_hold);
819 list_del(&page->lru);
820 if (page_mapped(page)) {
821 if (!reclaim_mapped ||
822 (total_swap_pages == 0 && PageAnon(page)) ||
f7b7fd8f 823 page_referenced(page, 0)) {
1da177e4
LT
824 list_add(&page->lru, &l_active);
825 continue;
826 }
827 }
828 list_add(&page->lru, &l_inactive);
829 }
830
831 pagevec_init(&pvec, 1);
832 pgmoved = 0;
833 spin_lock_irq(&zone->lru_lock);
834 while (!list_empty(&l_inactive)) {
835 page = lru_to_page(&l_inactive);
836 prefetchw_prev_lru_page(page, &l_inactive, flags);
725d704e 837 VM_BUG_ON(PageLRU(page));
8d438f96 838 SetPageLRU(page);
725d704e 839 VM_BUG_ON(!PageActive(page));
4c84cacf
NP
840 ClearPageActive(page);
841
1da177e4
LT
842 list_move(&page->lru, &zone->inactive_list);
843 pgmoved++;
844 if (!pagevec_add(&pvec, page)) {
845 zone->nr_inactive += pgmoved;
846 spin_unlock_irq(&zone->lru_lock);
847 pgdeactivate += pgmoved;
848 pgmoved = 0;
849 if (buffer_heads_over_limit)
850 pagevec_strip(&pvec);
851 __pagevec_release(&pvec);
852 spin_lock_irq(&zone->lru_lock);
853 }
854 }
855 zone->nr_inactive += pgmoved;
856 pgdeactivate += pgmoved;
857 if (buffer_heads_over_limit) {
858 spin_unlock_irq(&zone->lru_lock);
859 pagevec_strip(&pvec);
860 spin_lock_irq(&zone->lru_lock);
861 }
862
863 pgmoved = 0;
864 while (!list_empty(&l_active)) {
865 page = lru_to_page(&l_active);
866 prefetchw_prev_lru_page(page, &l_active, flags);
725d704e 867 VM_BUG_ON(PageLRU(page));
8d438f96 868 SetPageLRU(page);
725d704e 869 VM_BUG_ON(!PageActive(page));
1da177e4
LT
870 list_move(&page->lru, &zone->active_list);
871 pgmoved++;
872 if (!pagevec_add(&pvec, page)) {
873 zone->nr_active += pgmoved;
874 pgmoved = 0;
875 spin_unlock_irq(&zone->lru_lock);
876 __pagevec_release(&pvec);
877 spin_lock_irq(&zone->lru_lock);
878 }
879 }
880 zone->nr_active += pgmoved;
a74609fa 881
f8891e5e
CL
882 __count_zone_vm_events(PGREFILL, zone, pgscanned);
883 __count_vm_events(PGDEACTIVATE, pgdeactivate);
884 spin_unlock_irq(&zone->lru_lock);
1da177e4 885
a74609fa 886 pagevec_release(&pvec);
1da177e4
LT
887}
888
889/*
890 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
891 */
05ff5137
AM
892static unsigned long shrink_zone(int priority, struct zone *zone,
893 struct scan_control *sc)
1da177e4
LT
894{
895 unsigned long nr_active;
896 unsigned long nr_inactive;
8695949a 897 unsigned long nr_to_scan;
05ff5137 898 unsigned long nr_reclaimed = 0;
1da177e4 899
53e9a615
MH
900 atomic_inc(&zone->reclaim_in_progress);
901
1da177e4
LT
902 /*
903 * Add one to `nr_to_scan' just to make sure that the kernel will
904 * slowly sift through the active list.
905 */
8695949a 906 zone->nr_scan_active += (zone->nr_active >> priority) + 1;
1da177e4
LT
907 nr_active = zone->nr_scan_active;
908 if (nr_active >= sc->swap_cluster_max)
909 zone->nr_scan_active = 0;
910 else
911 nr_active = 0;
912
8695949a 913 zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
1da177e4
LT
914 nr_inactive = zone->nr_scan_inactive;
915 if (nr_inactive >= sc->swap_cluster_max)
916 zone->nr_scan_inactive = 0;
917 else
918 nr_inactive = 0;
919
1da177e4
LT
920 while (nr_active || nr_inactive) {
921 if (nr_active) {
8695949a 922 nr_to_scan = min(nr_active,
1da177e4 923 (unsigned long)sc->swap_cluster_max);
8695949a 924 nr_active -= nr_to_scan;
1742f19f 925 shrink_active_list(nr_to_scan, zone, sc);
1da177e4
LT
926 }
927
928 if (nr_inactive) {
8695949a 929 nr_to_scan = min(nr_inactive,
1da177e4 930 (unsigned long)sc->swap_cluster_max);
8695949a 931 nr_inactive -= nr_to_scan;
1742f19f
AM
932 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
933 sc);
1da177e4
LT
934 }
935 }
936
937 throttle_vm_writeout();
53e9a615
MH
938
939 atomic_dec(&zone->reclaim_in_progress);
05ff5137 940 return nr_reclaimed;
1da177e4
LT
941}
942
943/*
944 * This is the direct reclaim path, for page-allocating processes. We only
945 * try to reclaim pages from zones which will satisfy the caller's allocation
946 * request.
947 *
948 * We reclaim from a zone even if that zone is over pages_high. Because:
949 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
950 * allocation or
951 * b) The zones may be over pages_high but they must go *over* pages_high to
952 * satisfy the `incremental min' zone defense algorithm.
953 *
954 * Returns the number of reclaimed pages.
955 *
956 * If a zone is deemed to be full of pinned pages then just give it a light
957 * scan then give up on it.
958 */
1742f19f 959static unsigned long shrink_zones(int priority, struct zone **zones,
05ff5137 960 struct scan_control *sc)
1da177e4 961{
05ff5137 962 unsigned long nr_reclaimed = 0;
1da177e4
LT
963 int i;
964
408d8544 965 sc->all_unreclaimable = 1;
1da177e4
LT
966 for (i = 0; zones[i] != NULL; i++) {
967 struct zone *zone = zones[i];
968
f3fe6512 969 if (!populated_zone(zone))
1da177e4
LT
970 continue;
971
9bf2229f 972 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1da177e4
LT
973 continue;
974
8695949a
CL
975 zone->temp_priority = priority;
976 if (zone->prev_priority > priority)
977 zone->prev_priority = priority;
1da177e4 978
8695949a 979 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1da177e4
LT
980 continue; /* Let kswapd poll it */
981
408d8544
NP
982 sc->all_unreclaimable = 0;
983
05ff5137 984 nr_reclaimed += shrink_zone(priority, zone, sc);
1da177e4 985 }
05ff5137 986 return nr_reclaimed;
1da177e4
LT
987}
988
989/*
990 * This is the main entry point to direct page reclaim.
991 *
992 * If a full scan of the inactive list fails to free enough memory then we
993 * are "out of memory" and something needs to be killed.
994 *
995 * If the caller is !__GFP_FS then the probability of a failure is reasonably
996 * high - the zone may be full of dirty or under-writeback pages, which this
997 * caller can't do much about. We kick pdflush and take explicit naps in the
998 * hope that some of these pages can be written. But if the allocating task
999 * holds filesystem locks which prevent writeout this might not work, and the
1000 * allocation attempt will fail.
1001 */
69e05944 1002unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
1da177e4
LT
1003{
1004 int priority;
1005 int ret = 0;
69e05944 1006 unsigned long total_scanned = 0;
05ff5137 1007 unsigned long nr_reclaimed = 0;
1da177e4 1008 struct reclaim_state *reclaim_state = current->reclaim_state;
1da177e4
LT
1009 unsigned long lru_pages = 0;
1010 int i;
179e9639
AM
1011 struct scan_control sc = {
1012 .gfp_mask = gfp_mask,
1013 .may_writepage = !laptop_mode,
1014 .swap_cluster_max = SWAP_CLUSTER_MAX,
1015 .may_swap = 1,
d6277db4 1016 .swappiness = vm_swappiness,
179e9639 1017 };
1da177e4 1018
f8891e5e 1019 count_vm_event(ALLOCSTALL);
1da177e4
LT
1020
1021 for (i = 0; zones[i] != NULL; i++) {
1022 struct zone *zone = zones[i];
1023
9bf2229f 1024 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1da177e4
LT
1025 continue;
1026
1027 zone->temp_priority = DEF_PRIORITY;
1028 lru_pages += zone->nr_active + zone->nr_inactive;
1029 }
1030
1031 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1da177e4 1032 sc.nr_scanned = 0;
f7b7fd8f
RR
1033 if (!priority)
1034 disable_swap_token();
1742f19f 1035 nr_reclaimed += shrink_zones(priority, zones, &sc);
1da177e4
LT
1036 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
1037 if (reclaim_state) {
05ff5137 1038 nr_reclaimed += reclaim_state->reclaimed_slab;
1da177e4
LT
1039 reclaim_state->reclaimed_slab = 0;
1040 }
1041 total_scanned += sc.nr_scanned;
05ff5137 1042 if (nr_reclaimed >= sc.swap_cluster_max) {
1da177e4
LT
1043 ret = 1;
1044 goto out;
1045 }
1046
1047 /*
1048 * Try to write back as many pages as we just scanned. This
1049 * tends to cause slow streaming writers to write data to the
1050 * disk smoothly, at the dirtying rate, which is nice. But
1051 * that's undesirable in laptop mode, where we *want* lumpy
1052 * writeout. So in laptop mode, write out the whole world.
1053 */
179e9639
AM
1054 if (total_scanned > sc.swap_cluster_max +
1055 sc.swap_cluster_max / 2) {
687a21ce 1056 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
1da177e4
LT
1057 sc.may_writepage = 1;
1058 }
1059
1060 /* Take a nap, wait for some writeback to complete */
1061 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
3fcfab16 1062 congestion_wait(WRITE, HZ/10);
1da177e4 1063 }
408d8544
NP
1064 /* top priority shrink_caches still had more to do? don't OOM, then */
1065 if (!sc.all_unreclaimable)
1066 ret = 1;
1da177e4
LT
1067out:
1068 for (i = 0; zones[i] != 0; i++) {
1069 struct zone *zone = zones[i];
1070
9bf2229f 1071 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1da177e4
LT
1072 continue;
1073
1074 zone->prev_priority = zone->temp_priority;
1075 }
1076 return ret;
1077}
1078
1079/*
1080 * For kswapd, balance_pgdat() will work across all this node's zones until
1081 * they are all at pages_high.
1082 *
1da177e4
LT
1083 * Returns the number of pages which were actually freed.
1084 *
1085 * There is special handling here for zones which are full of pinned pages.
1086 * This can happen if the pages are all mlocked, or if they are all used by
1087 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1088 * What we do is to detect the case where all pages in the zone have been
1089 * scanned twice and there has been zero successful reclaim. Mark the zone as
1090 * dead and from now on, only perform a short scan. Basically we're polling
1091 * the zone for when the problem goes away.
1092 *
1093 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1094 * zones which have free_pages > pages_high, but once a zone is found to have
1095 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1096 * of the number of free pages in the lower zones. This interoperates with
1097 * the page allocator fallback scheme to ensure that aging of pages is balanced
1098 * across the zones.
1099 */
d6277db4 1100static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
1da177e4 1101{
1da177e4
LT
1102 int all_zones_ok;
1103 int priority;
1104 int i;
69e05944 1105 unsigned long total_scanned;
05ff5137 1106 unsigned long nr_reclaimed;
1da177e4 1107 struct reclaim_state *reclaim_state = current->reclaim_state;
179e9639
AM
1108 struct scan_control sc = {
1109 .gfp_mask = GFP_KERNEL,
1110 .may_swap = 1,
d6277db4
RW
1111 .swap_cluster_max = SWAP_CLUSTER_MAX,
1112 .swappiness = vm_swappiness,
179e9639 1113 };
1da177e4
LT
1114
1115loop_again:
1116 total_scanned = 0;
05ff5137 1117 nr_reclaimed = 0;
c0bbbc73 1118 sc.may_writepage = !laptop_mode;
f8891e5e 1119 count_vm_event(PAGEOUTRUN);
1da177e4
LT
1120
1121 for (i = 0; i < pgdat->nr_zones; i++) {
1122 struct zone *zone = pgdat->node_zones + i;
1123
1124 zone->temp_priority = DEF_PRIORITY;
1125 }
1126
1127 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1128 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1129 unsigned long lru_pages = 0;
1130
f7b7fd8f
RR
1131 /* The swap token gets in the way of swapout... */
1132 if (!priority)
1133 disable_swap_token();
1134
1da177e4
LT
1135 all_zones_ok = 1;
1136
d6277db4
RW
1137 /*
1138 * Scan in the highmem->dma direction for the highest
1139 * zone which needs scanning
1140 */
1141 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1142 struct zone *zone = pgdat->node_zones + i;
1da177e4 1143
d6277db4
RW
1144 if (!populated_zone(zone))
1145 continue;
1da177e4 1146
d6277db4
RW
1147 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1148 continue;
1da177e4 1149
d6277db4
RW
1150 if (!zone_watermark_ok(zone, order, zone->pages_high,
1151 0, 0)) {
1152 end_zone = i;
1153 goto scan;
1da177e4 1154 }
1da177e4 1155 }
d6277db4 1156 goto out;
1da177e4
LT
1157scan:
1158 for (i = 0; i <= end_zone; i++) {
1159 struct zone *zone = pgdat->node_zones + i;
1160
1161 lru_pages += zone->nr_active + zone->nr_inactive;
1162 }
1163
1164 /*
1165 * Now scan the zone in the dma->highmem direction, stopping
1166 * at the last zone which needs scanning.
1167 *
1168 * We do this because the page allocator works in the opposite
1169 * direction. This prevents the page allocator from allocating
1170 * pages behind kswapd's direction of progress, which would
1171 * cause too much scanning of the lower zones.
1172 */
1173 for (i = 0; i <= end_zone; i++) {
1174 struct zone *zone = pgdat->node_zones + i;
b15e0905 1175 int nr_slab;
1da177e4 1176
f3fe6512 1177 if (!populated_zone(zone))
1da177e4
LT
1178 continue;
1179
1180 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1181 continue;
1182
d6277db4
RW
1183 if (!zone_watermark_ok(zone, order, zone->pages_high,
1184 end_zone, 0))
1185 all_zones_ok = 0;
1da177e4
LT
1186 zone->temp_priority = priority;
1187 if (zone->prev_priority > priority)
1188 zone->prev_priority = priority;
1189 sc.nr_scanned = 0;
05ff5137 1190 nr_reclaimed += shrink_zone(priority, zone, &sc);
1da177e4 1191 reclaim_state->reclaimed_slab = 0;
b15e0905 1192 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1193 lru_pages);
05ff5137 1194 nr_reclaimed += reclaim_state->reclaimed_slab;
1da177e4
LT
1195 total_scanned += sc.nr_scanned;
1196 if (zone->all_unreclaimable)
1197 continue;
b15e0905 1198 if (nr_slab == 0 && zone->pages_scanned >=
4ff1ffb4 1199 (zone->nr_active + zone->nr_inactive) * 6)
1da177e4
LT
1200 zone->all_unreclaimable = 1;
1201 /*
1202 * If we've done a decent amount of scanning and
1203 * the reclaim ratio is low, start doing writepage
1204 * even in laptop mode
1205 */
1206 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
05ff5137 1207 total_scanned > nr_reclaimed + nr_reclaimed / 2)
1da177e4
LT
1208 sc.may_writepage = 1;
1209 }
1da177e4
LT
1210 if (all_zones_ok)
1211 break; /* kswapd: all done */
1212 /*
1213 * OK, kswapd is getting into trouble. Take a nap, then take
1214 * another pass across the zones.
1215 */
1216 if (total_scanned && priority < DEF_PRIORITY - 2)
3fcfab16 1217 congestion_wait(WRITE, HZ/10);
1da177e4
LT
1218
1219 /*
1220 * We do this so kswapd doesn't build up large priorities for
1221 * example when it is freeing in parallel with allocators. It
1222 * matches the direct reclaim path behaviour in terms of impact
1223 * on zone->*_priority.
1224 */
d6277db4 1225 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
1da177e4
LT
1226 break;
1227 }
1228out:
1229 for (i = 0; i < pgdat->nr_zones; i++) {
1230 struct zone *zone = pgdat->node_zones + i;
1231
1232 zone->prev_priority = zone->temp_priority;
1233 }
1234 if (!all_zones_ok) {
1235 cond_resched();
1236 goto loop_again;
1237 }
1238
05ff5137 1239 return nr_reclaimed;
1da177e4
LT
1240}
1241
1242/*
1243 * The background pageout daemon, started as a kernel thread
1244 * from the init process.
1245 *
1246 * This basically trickles out pages so that we have _some_
1247 * free memory available even if there is no other activity
1248 * that frees anything up. This is needed for things like routing
1249 * etc, where we otherwise might have all activity going on in
1250 * asynchronous contexts that cannot page things out.
1251 *
1252 * If there are applications that are active memory-allocators
1253 * (most normal use), this basically shouldn't matter.
1254 */
1255static int kswapd(void *p)
1256{
1257 unsigned long order;
1258 pg_data_t *pgdat = (pg_data_t*)p;
1259 struct task_struct *tsk = current;
1260 DEFINE_WAIT(wait);
1261 struct reclaim_state reclaim_state = {
1262 .reclaimed_slab = 0,
1263 };
1264 cpumask_t cpumask;
1265
1da177e4
LT
1266 cpumask = node_to_cpumask(pgdat->node_id);
1267 if (!cpus_empty(cpumask))
1268 set_cpus_allowed(tsk, cpumask);
1269 current->reclaim_state = &reclaim_state;
1270
1271 /*
1272 * Tell the memory management that we're a "memory allocator",
1273 * and that if we need more memory we should get access to it
1274 * regardless (see "__alloc_pages()"). "kswapd" should
1275 * never get caught in the normal page freeing logic.
1276 *
1277 * (Kswapd normally doesn't need memory anyway, but sometimes
1278 * you need a small amount of memory in order to be able to
1279 * page out something else, and this flag essentially protects
1280 * us from recursively trying to free more memory as we're
1281 * trying to free the first piece of memory in the first place).
1282 */
930d9152 1283 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
1da177e4
LT
1284
1285 order = 0;
1286 for ( ; ; ) {
1287 unsigned long new_order;
3e1d1d28
CL
1288
1289 try_to_freeze();
1da177e4
LT
1290
1291 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1292 new_order = pgdat->kswapd_max_order;
1293 pgdat->kswapd_max_order = 0;
1294 if (order < new_order) {
1295 /*
1296 * Don't sleep if someone wants a larger 'order'
1297 * allocation
1298 */
1299 order = new_order;
1300 } else {
1301 schedule();
1302 order = pgdat->kswapd_max_order;
1303 }
1304 finish_wait(&pgdat->kswapd_wait, &wait);
1305
d6277db4 1306 balance_pgdat(pgdat, order);
1da177e4
LT
1307 }
1308 return 0;
1309}
1310
1311/*
1312 * A zone is low on free memory, so wake its kswapd task to service it.
1313 */
1314void wakeup_kswapd(struct zone *zone, int order)
1315{
1316 pg_data_t *pgdat;
1317
f3fe6512 1318 if (!populated_zone(zone))
1da177e4
LT
1319 return;
1320
1321 pgdat = zone->zone_pgdat;
7fb1d9fc 1322 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
1da177e4
LT
1323 return;
1324 if (pgdat->kswapd_max_order < order)
1325 pgdat->kswapd_max_order = order;
9bf2229f 1326 if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
1da177e4 1327 return;
8d0986e2 1328 if (!waitqueue_active(&pgdat->kswapd_wait))
1da177e4 1329 return;
8d0986e2 1330 wake_up_interruptible(&pgdat->kswapd_wait);
1da177e4
LT
1331}
1332
1333#ifdef CONFIG_PM
1334/*
d6277db4
RW
1335 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1336 * from LRU lists system-wide, for given pass and priority, and returns the
1337 * number of reclaimed pages
1338 *
1339 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1340 */
1341static unsigned long shrink_all_zones(unsigned long nr_pages, int pass,
1342 int prio, struct scan_control *sc)
1343{
1344 struct zone *zone;
1345 unsigned long nr_to_scan, ret = 0;
1346
1347 for_each_zone(zone) {
1348
1349 if (!populated_zone(zone))
1350 continue;
1351
1352 if (zone->all_unreclaimable && prio != DEF_PRIORITY)
1353 continue;
1354
1355 /* For pass = 0 we don't shrink the active list */
1356 if (pass > 0) {
1357 zone->nr_scan_active += (zone->nr_active >> prio) + 1;
1358 if (zone->nr_scan_active >= nr_pages || pass > 3) {
1359 zone->nr_scan_active = 0;
1360 nr_to_scan = min(nr_pages, zone->nr_active);
1361 shrink_active_list(nr_to_scan, zone, sc);
1362 }
1363 }
1364
1365 zone->nr_scan_inactive += (zone->nr_inactive >> prio) + 1;
1366 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1367 zone->nr_scan_inactive = 0;
1368 nr_to_scan = min(nr_pages, zone->nr_inactive);
1369 ret += shrink_inactive_list(nr_to_scan, zone, sc);
1370 if (ret >= nr_pages)
1371 return ret;
1372 }
1373 }
1374
1375 return ret;
1376}
1377
1378/*
1379 * Try to free `nr_pages' of memory, system-wide, and return the number of
1380 * freed pages.
1381 *
1382 * Rather than trying to age LRUs the aim is to preserve the overall
1383 * LRU order by reclaiming preferentially
1384 * inactive > active > active referenced > active mapped
1da177e4 1385 */
69e05944 1386unsigned long shrink_all_memory(unsigned long nr_pages)
1da177e4 1387{
d6277db4 1388 unsigned long lru_pages, nr_slab;
69e05944 1389 unsigned long ret = 0;
d6277db4
RW
1390 int pass;
1391 struct reclaim_state reclaim_state;
1392 struct zone *zone;
1393 struct scan_control sc = {
1394 .gfp_mask = GFP_KERNEL,
1395 .may_swap = 0,
1396 .swap_cluster_max = nr_pages,
1397 .may_writepage = 1,
1398 .swappiness = vm_swappiness,
1da177e4
LT
1399 };
1400
1401 current->reclaim_state = &reclaim_state;
69e05944 1402
d6277db4
RW
1403 lru_pages = 0;
1404 for_each_zone(zone)
1405 lru_pages += zone->nr_active + zone->nr_inactive;
1406
972d1a7b 1407 nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
d6277db4
RW
1408 /* If slab caches are huge, it's better to hit them first */
1409 while (nr_slab >= lru_pages) {
1410 reclaim_state.reclaimed_slab = 0;
1411 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1412 if (!reclaim_state.reclaimed_slab)
1da177e4 1413 break;
d6277db4
RW
1414
1415 ret += reclaim_state.reclaimed_slab;
1416 if (ret >= nr_pages)
1417 goto out;
1418
1419 nr_slab -= reclaim_state.reclaimed_slab;
1da177e4 1420 }
d6277db4
RW
1421
1422 /*
1423 * We try to shrink LRUs in 5 passes:
1424 * 0 = Reclaim from inactive_list only
1425 * 1 = Reclaim from active list but don't reclaim mapped
1426 * 2 = 2nd pass of type 1
1427 * 3 = Reclaim mapped (normal reclaim)
1428 * 4 = 2nd pass of type 3
1429 */
1430 for (pass = 0; pass < 5; pass++) {
1431 int prio;
1432
1433 /* Needed for shrinking slab caches later on */
1434 if (!lru_pages)
1435 for_each_zone(zone) {
1436 lru_pages += zone->nr_active;
1437 lru_pages += zone->nr_inactive;
1438 }
1439
1440 /* Force reclaiming mapped pages in the passes #3 and #4 */
1441 if (pass > 2) {
1442 sc.may_swap = 1;
1443 sc.swappiness = 100;
1444 }
1445
1446 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1447 unsigned long nr_to_scan = nr_pages - ret;
1448
d6277db4 1449 sc.nr_scanned = 0;
d6277db4
RW
1450 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1451 if (ret >= nr_pages)
1452 goto out;
1453
1454 reclaim_state.reclaimed_slab = 0;
1455 shrink_slab(sc.nr_scanned, sc.gfp_mask, lru_pages);
1456 ret += reclaim_state.reclaimed_slab;
1457 if (ret >= nr_pages)
1458 goto out;
1459
1460 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
3fcfab16 1461 congestion_wait(WRITE, HZ / 10);
d6277db4
RW
1462 }
1463
1464 lru_pages = 0;
248a0301 1465 }
d6277db4
RW
1466
1467 /*
1468 * If ret = 0, we could not shrink LRUs, but there may be something
1469 * in slab caches
1470 */
1471 if (!ret)
1472 do {
1473 reclaim_state.reclaimed_slab = 0;
1474 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1475 ret += reclaim_state.reclaimed_slab;
1476 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
1477
1478out:
1da177e4 1479 current->reclaim_state = NULL;
d6277db4 1480
1da177e4
LT
1481 return ret;
1482}
1483#endif
1484
1485#ifdef CONFIG_HOTPLUG_CPU
1486/* It's optimal to keep kswapds on the same CPUs as their memory, but
1487 not required for correctness. So if the last cpu in a node goes
1488 away, we get changed to run anywhere: as the first one comes back,
1489 restore their cpu bindings. */
9c7b216d 1490static int __devinit cpu_callback(struct notifier_block *nfb,
69e05944 1491 unsigned long action, void *hcpu)
1da177e4
LT
1492{
1493 pg_data_t *pgdat;
1494 cpumask_t mask;
1495
1496 if (action == CPU_ONLINE) {
ec936fc5 1497 for_each_online_pgdat(pgdat) {
1da177e4
LT
1498 mask = node_to_cpumask(pgdat->node_id);
1499 if (any_online_cpu(mask) != NR_CPUS)
1500 /* One of our CPUs online: restore mask */
1501 set_cpus_allowed(pgdat->kswapd, mask);
1502 }
1503 }
1504 return NOTIFY_OK;
1505}
1506#endif /* CONFIG_HOTPLUG_CPU */
1507
3218ae14
YG
1508/*
1509 * This kswapd start function will be called by init and node-hot-add.
1510 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
1511 */
1512int kswapd_run(int nid)
1513{
1514 pg_data_t *pgdat = NODE_DATA(nid);
1515 int ret = 0;
1516
1517 if (pgdat->kswapd)
1518 return 0;
1519
1520 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
1521 if (IS_ERR(pgdat->kswapd)) {
1522 /* failure at boot is fatal */
1523 BUG_ON(system_state == SYSTEM_BOOTING);
1524 printk("Failed to start kswapd on node %d\n",nid);
1525 ret = -1;
1526 }
1527 return ret;
1528}
1529
1da177e4
LT
1530static int __init kswapd_init(void)
1531{
3218ae14 1532 int nid;
69e05944 1533
1da177e4 1534 swap_setup();
3218ae14
YG
1535 for_each_online_node(nid)
1536 kswapd_run(nid);
1da177e4
LT
1537 hotcpu_notifier(cpu_callback, 0);
1538 return 0;
1539}
1540
1541module_init(kswapd_init)
9eeff239
CL
1542
1543#ifdef CONFIG_NUMA
1544/*
1545 * Zone reclaim mode
1546 *
1547 * If non-zero call zone_reclaim when the number of free pages falls below
1548 * the watermarks.
9eeff239
CL
1549 */
1550int zone_reclaim_mode __read_mostly;
1551
1b2ffb78
CL
1552#define RECLAIM_OFF 0
1553#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1554#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1555#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1556
a92f7126
CL
1557/*
1558 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1559 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1560 * a zone.
1561 */
1562#define ZONE_RECLAIM_PRIORITY 4
1563
9614634f
CL
1564/*
1565 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
1566 * occur.
1567 */
1568int sysctl_min_unmapped_ratio = 1;
1569
0ff38490
CL
1570/*
1571 * If the number of slab pages in a zone grows beyond this percentage then
1572 * slab reclaim needs to occur.
1573 */
1574int sysctl_min_slab_ratio = 5;
1575
9eeff239
CL
1576/*
1577 * Try to free up some pages from this zone through reclaim.
1578 */
179e9639 1579static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
9eeff239 1580{
7fb2d46d 1581 /* Minimum pages needed in order to stay on node */
69e05944 1582 const unsigned long nr_pages = 1 << order;
9eeff239
CL
1583 struct task_struct *p = current;
1584 struct reclaim_state reclaim_state;
8695949a 1585 int priority;
05ff5137 1586 unsigned long nr_reclaimed = 0;
179e9639
AM
1587 struct scan_control sc = {
1588 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1589 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
69e05944
AM
1590 .swap_cluster_max = max_t(unsigned long, nr_pages,
1591 SWAP_CLUSTER_MAX),
179e9639 1592 .gfp_mask = gfp_mask,
d6277db4 1593 .swappiness = vm_swappiness,
179e9639 1594 };
83e33a47 1595 unsigned long slab_reclaimable;
9eeff239
CL
1596
1597 disable_swap_token();
9eeff239 1598 cond_resched();
d4f7796e
CL
1599 /*
1600 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1601 * and we also need to be able to write out pages for RECLAIM_WRITE
1602 * and RECLAIM_SWAP.
1603 */
1604 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
9eeff239
CL
1605 reclaim_state.reclaimed_slab = 0;
1606 p->reclaim_state = &reclaim_state;
c84db23c 1607
0ff38490
CL
1608 if (zone_page_state(zone, NR_FILE_PAGES) -
1609 zone_page_state(zone, NR_FILE_MAPPED) >
1610 zone->min_unmapped_pages) {
1611 /*
1612 * Free memory by calling shrink zone with increasing
1613 * priorities until we have enough memory freed.
1614 */
1615 priority = ZONE_RECLAIM_PRIORITY;
1616 do {
1617 nr_reclaimed += shrink_zone(priority, zone, &sc);
1618 priority--;
1619 } while (priority >= 0 && nr_reclaimed < nr_pages);
1620 }
c84db23c 1621
83e33a47
CL
1622 slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1623 if (slab_reclaimable > zone->min_slab_pages) {
2a16e3f4 1624 /*
7fb2d46d 1625 * shrink_slab() does not currently allow us to determine how
0ff38490
CL
1626 * many pages were freed in this zone. So we take the current
1627 * number of slab pages and shake the slab until it is reduced
1628 * by the same nr_pages that we used for reclaiming unmapped
1629 * pages.
2a16e3f4 1630 *
0ff38490
CL
1631 * Note that shrink_slab will free memory on all zones and may
1632 * take a long time.
2a16e3f4 1633 */
0ff38490 1634 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
83e33a47
CL
1635 zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
1636 slab_reclaimable - nr_pages)
0ff38490 1637 ;
83e33a47
CL
1638
1639 /*
1640 * Update nr_reclaimed by the number of slab pages we
1641 * reclaimed from this zone.
1642 */
1643 nr_reclaimed += slab_reclaimable -
1644 zone_page_state(zone, NR_SLAB_RECLAIMABLE);
2a16e3f4
CL
1645 }
1646
9eeff239 1647 p->reclaim_state = NULL;
d4f7796e 1648 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
05ff5137 1649 return nr_reclaimed >= nr_pages;
9eeff239 1650}
179e9639
AM
1651
1652int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1653{
1654 cpumask_t mask;
1655 int node_id;
1656
1657 /*
0ff38490
CL
1658 * Zone reclaim reclaims unmapped file backed pages and
1659 * slab pages if we are over the defined limits.
34aa1330 1660 *
9614634f
CL
1661 * A small portion of unmapped file backed pages is needed for
1662 * file I/O otherwise pages read by file I/O will be immediately
1663 * thrown out if the zone is overallocated. So we do not reclaim
1664 * if less than a specified percentage of the zone is used by
1665 * unmapped file backed pages.
179e9639 1666 */
34aa1330 1667 if (zone_page_state(zone, NR_FILE_PAGES) -
0ff38490
CL
1668 zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
1669 && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
1670 <= zone->min_slab_pages)
9614634f 1671 return 0;
179e9639
AM
1672
1673 /*
1674 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1675 * not have reclaimable pages and if we should not delay the allocation
1676 * then do not scan.
1677 */
1678 if (!(gfp_mask & __GFP_WAIT) ||
1679 zone->all_unreclaimable ||
1680 atomic_read(&zone->reclaim_in_progress) > 0 ||
1681 (current->flags & PF_MEMALLOC))
1682 return 0;
1683
1684 /*
1685 * Only run zone reclaim on the local zone or on zones that do not
1686 * have associated processors. This will favor the local processor
1687 * over remote processors and spread off node memory allocations
1688 * as wide as possible.
1689 */
89fa3024 1690 node_id = zone_to_nid(zone);
179e9639
AM
1691 mask = node_to_cpumask(node_id);
1692 if (!cpus_empty(mask) && node_id != numa_node_id())
1693 return 0;
1694 return __zone_reclaim(zone, gfp_mask, order);
1695}
9eeff239 1696#endif
This page took 0.340139 seconds and 5 git commands to generate.