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