blob: 7408a8a7d882d4b333d6b6d370d9d48e1c8076d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/vmscan.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 *
6 * Swap reorganised 29.12.95, Stephen Tweedie.
7 * kswapd added: 7.1.96 sct
8 * Removed kswapd_ctl limits, and swap out as many pages as needed
9 * to bring the system back to freepages.high: 2.4.97, Rik van Riel.
10 * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
11 * Multiqueue VM started 5.8.00, Rik van Riel.
12 */
13
14#include <linux/mm.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/kernel_stat.h>
18#include <linux/swap.h>
19#include <linux/pagemap.h>
20#include <linux/init.h>
21#include <linux/highmem.h>
Andrew Mortone129b5c2006-09-27 01:50:00 -070022#include <linux/vmstat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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>
Rafael J. Wysocki248a0302006-03-22 00:09:04 -080037#include <linux/delay.h>
Yasunori Goto3218ae12006-06-27 02:53:33 -070038#include <linux/kthread.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080039#include <linux/freezer.h>
Balbir Singh66e17072008-02-07 00:13:56 -080040#include <linux/memcontrol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/tlbflush.h>
43#include <asm/div64.h>
44
45#include <linux/swapops.h>
46
Nick Piggin0f8053a2006-03-22 00:08:33 -080047#include "internal.h"
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049struct scan_control {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 /* Incremented by the number of inactive pages that were scanned */
51 unsigned long nr_scanned;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040054 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 int may_writepage;
57
Christoph Lameterf1fd1062006-01-18 17:42:30 -080058 /* Can pages be swapped as part of reclaim? */
59 int may_swap;
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
62 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
63 * In this context, it doesn't matter that we scan the
64 * whole list at once. */
65 int swap_cluster_max;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -070066
67 int swappiness;
Nick Piggin408d8542006-09-25 23:31:27 -070068
69 int all_unreclaimable;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -070070
71 int order;
Balbir Singh66e17072008-02-07 00:13:56 -080072
73 /* Which cgroup do we reclaim from */
74 struct mem_cgroup *mem_cgroup;
75
76 /* Pluggable isolate pages callback */
77 unsigned long (*isolate_pages)(unsigned long nr, struct list_head *dst,
78 unsigned long *scanned, int order, int mode,
79 struct zone *z, struct mem_cgroup *mem_cont,
80 int active);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
84
85#ifdef ARCH_HAS_PREFETCH
86#define prefetch_prev_lru_page(_page, _base, _field) \
87 do { \
88 if ((_page)->lru.prev != _base) { \
89 struct page *prev; \
90 \
91 prev = lru_to_page(&(_page->lru)); \
92 prefetch(&prev->_field); \
93 } \
94 } while (0)
95#else
96#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
97#endif
98
99#ifdef ARCH_HAS_PREFETCHW
100#define prefetchw_prev_lru_page(_page, _base, _field) \
101 do { \
102 if ((_page)->lru.prev != _base) { \
103 struct page *prev; \
104 \
105 prev = lru_to_page(&(_page->lru)); \
106 prefetchw(&prev->_field); \
107 } \
108 } while (0)
109#else
110#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
111#endif
112
113/*
114 * From 0 .. 100. Higher means more swappy.
115 */
116int vm_swappiness = 60;
Andrew Mortonbd1e22b2006-06-23 02:03:47 -0700117long vm_total_pages; /* The total number of pages which the VM controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119static LIST_HEAD(shrinker_list);
120static DECLARE_RWSEM(shrinker_rwsem);
121
122/*
123 * Add a shrinker callback to be called from the vm
124 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700125void register_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700127 shrinker->nr = 0;
128 down_write(&shrinker_rwsem);
129 list_add_tail(&shrinker->list, &shrinker_list);
130 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700132EXPORT_SYMBOL(register_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/*
135 * Remove one
136 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700137void unregister_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 down_write(&shrinker_rwsem);
140 list_del(&shrinker->list);
141 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700143EXPORT_SYMBOL(unregister_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145#define SHRINK_BATCH 128
146/*
147 * Call the shrink functions to age shrinkable caches
148 *
149 * Here we assume it costs one seek to replace a lru page and that it also
150 * takes a seek to recreate a cache object. With this in mind we age equal
151 * percentages of the lru and ageable caches. This should balance the seeks
152 * generated by these structures.
153 *
Simon Arlott183ff222007-10-20 01:27:18 +0200154 * If the vm encountered mapped pages on the LRU it increase the pressure on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * slab to avoid swapping.
156 *
157 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
158 *
159 * `lru_pages' represents the number of on-LRU pages in all the zones which
160 * are eligible for the caller's allocation attempt. It is used for balancing
161 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700162 *
163 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
Andrew Morton69e05942006-03-22 00:08:19 -0800165unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
166 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800169 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if (scanned == 0)
172 scanned = SWAP_CLUSTER_MAX;
173
174 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700175 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 list_for_each_entry(shrinker, &shrinker_list, list) {
178 unsigned long long delta;
179 unsigned long total_scan;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700180 unsigned long max_pass = (*shrinker->shrink)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800183 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 do_div(delta, lru_pages + 1);
185 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800186 if (shrinker->nr < 0) {
187 printk(KERN_ERR "%s: nr=%ld\n",
188 __FUNCTION__, shrinker->nr);
189 shrinker->nr = max_pass;
190 }
191
192 /*
193 * Avoid risking looping forever due to too large nr value:
194 * never try to free more than twice the estimate number of
195 * freeable entries.
196 */
197 if (shrinker->nr > max_pass * 2)
198 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 total_scan = shrinker->nr;
201 shrinker->nr = 0;
202
203 while (total_scan >= SHRINK_BATCH) {
204 long this_scan = SHRINK_BATCH;
205 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700206 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Rusty Russell8e1f9362007-07-17 04:03:17 -0700208 nr_before = (*shrinker->shrink)(0, gfp_mask);
209 shrink_ret = (*shrinker->shrink)(this_scan, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (shrink_ret == -1)
211 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700212 if (shrink_ret < nr_before)
213 ret += nr_before - shrink_ret;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700214 count_vm_events(SLABS_SCANNED, this_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 total_scan -= this_scan;
216
217 cond_resched();
218 }
219
220 shrinker->nr += total_scan;
221 }
222 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226/* Called without lock on whether page is mapped, so answer is unstable */
227static inline int page_mapping_inuse(struct page *page)
228{
229 struct address_space *mapping;
230
231 /* Page is in somebody's page tables. */
232 if (page_mapped(page))
233 return 1;
234
235 /* Be more reluctant to reclaim swapcache than pagecache */
236 if (PageSwapCache(page))
237 return 1;
238
239 mapping = page_mapping(page);
240 if (!mapping)
241 return 0;
242
243 /* File is mmap'd by somebody? */
244 return mapping_mapped(mapping);
245}
246
247static inline int is_page_cache_freeable(struct page *page)
248{
249 return page_count(page) - !!PagePrivate(page) == 2;
250}
251
252static int may_write_to_queue(struct backing_dev_info *bdi)
253{
Christoph Lameter930d9152006-01-08 01:00:47 -0800254 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return 1;
256 if (!bdi_write_congested(bdi))
257 return 1;
258 if (bdi == current->backing_dev_info)
259 return 1;
260 return 0;
261}
262
263/*
264 * We detected a synchronous write error writing a page out. Probably
265 * -ENOSPC. We need to propagate that into the address_space for a subsequent
266 * fsync(), msync() or close().
267 *
268 * The tricky part is that after writepage we cannot touch the mapping: nothing
269 * prevents it from being freed up. But we have a ref on the page and once
270 * that page is locked, the mapping is pinned.
271 *
272 * We're allowed to run sleeping lock_page() here because we know the caller has
273 * __GFP_FS.
274 */
275static void handle_write_error(struct address_space *mapping,
276 struct page *page, int error)
277{
278 lock_page(page);
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -0700279 if (page_mapping(page) == mapping)
280 mapping_set_error(mapping, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 unlock_page(page);
282}
283
Andy Whitcroftc661b072007-08-22 14:01:26 -0700284/* Request for sync pageout. */
285enum pageout_io {
286 PAGEOUT_IO_ASYNC,
287 PAGEOUT_IO_SYNC,
288};
289
Christoph Lameter04e62a22006-06-23 02:03:38 -0700290/* possible outcome of pageout() */
291typedef enum {
292 /* failed to write page out, page is locked */
293 PAGE_KEEP,
294 /* move page to the active list, page is locked */
295 PAGE_ACTIVATE,
296 /* page has been sent to the disk successfully, page is unlocked */
297 PAGE_SUCCESS,
298 /* page is clean and locked */
299 PAGE_CLEAN,
300} pageout_t;
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*
Andrew Morton1742f192006-03-22 00:08:21 -0800303 * pageout is called by shrink_page_list() for each dirty page.
304 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700306static pageout_t pageout(struct page *page, struct address_space *mapping,
307 enum pageout_io sync_writeback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 /*
310 * If the page is dirty, only perform writeback if that write
311 * will be non-blocking. To prevent this allocation from being
312 * stalled by pagecache activity. But note that there may be
313 * stalls if we need to run get_block(). We could test
314 * PagePrivate for that.
315 *
316 * If this process is currently in generic_file_write() against
317 * this page's queue, we can perform writeback even if that
318 * will block.
319 *
320 * If the page is swapcache, write it back even if that would
321 * block, for some throttling. This happens by accident, because
322 * swap_backing_dev_info is bust: it doesn't reflect the
323 * congestion state of the swapdevs. Easy to fix, if needed.
324 * See swapfile.c:page_queue_congested().
325 */
326 if (!is_page_cache_freeable(page))
327 return PAGE_KEEP;
328 if (!mapping) {
329 /*
330 * Some data journaling orphaned pages can have
331 * page->mapping == NULL while being dirty with clean buffers.
332 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700333 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (try_to_free_buffers(page)) {
335 ClearPageDirty(page);
336 printk("%s: orphaned page\n", __FUNCTION__);
337 return PAGE_CLEAN;
338 }
339 }
340 return PAGE_KEEP;
341 }
342 if (mapping->a_ops->writepage == NULL)
343 return PAGE_ACTIVATE;
344 if (!may_write_to_queue(mapping->backing_dev_info))
345 return PAGE_KEEP;
346
347 if (clear_page_dirty_for_io(page)) {
348 int res;
349 struct writeback_control wbc = {
350 .sync_mode = WB_SYNC_NONE,
351 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700352 .range_start = 0,
353 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 .nonblocking = 1,
355 .for_reclaim = 1,
356 };
357
358 SetPageReclaim(page);
359 res = mapping->a_ops->writepage(page, &wbc);
360 if (res < 0)
361 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800362 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 ClearPageReclaim(page);
364 return PAGE_ACTIVATE;
365 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700366
367 /*
368 * Wait on writeback if requested to. This happens when
369 * direct reclaiming a large contiguous area and the
370 * first attempt to free a range of pages fails.
371 */
372 if (PageWriteback(page) && sync_writeback == PAGEOUT_IO_SYNC)
373 wait_on_page_writeback(page);
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (!PageWriteback(page)) {
376 /* synchronous write or broken a_ops? */
377 ClearPageReclaim(page);
378 }
Andrew Mortone129b5c2006-09-27 01:50:00 -0700379 inc_zone_page_state(page, NR_VMSCAN_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return PAGE_SUCCESS;
381 }
382
383 return PAGE_CLEAN;
384}
385
Andrew Mortona649fd92006-10-17 00:09:36 -0700386/*
387 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
388 * someone else has a ref on the page, abort and return 0. If it was
389 * successfully detached, return 1. Assumes the caller has a single ref on
390 * this page.
391 */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800392int remove_mapping(struct address_space *mapping, struct page *page)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800393{
Nick Piggin28e4d962006-09-25 23:31:23 -0700394 BUG_ON(!PageLocked(page));
395 BUG_ON(mapping != page_mapping(page));
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800396
397 write_lock_irq(&mapping->tree_lock);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800398 /*
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700399 * The non racy check for a busy page.
400 *
401 * Must be careful with the order of the tests. When someone has
402 * a ref to the page, it may be possible that they dirty it then
403 * drop the reference. So if PageDirty is tested before page_count
404 * here, then the following race may occur:
405 *
406 * get_user_pages(&page);
407 * [user mapping goes away]
408 * write_to(page);
409 * !PageDirty(page) [good]
410 * SetPageDirty(page);
411 * put_page(page);
412 * !page_count(page) [good, discard it]
413 *
414 * [oops, our write_to data is lost]
415 *
416 * Reversing the order of the tests ensures such a situation cannot
417 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
418 * load is not satisfied before that of page->_count.
419 *
420 * Note that if SetPageDirty is always performed via set_page_dirty,
421 * and thus under tree_lock, then this ordering is not required.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800422 */
423 if (unlikely(page_count(page) != 2))
424 goto cannot_free;
425 smp_rmb();
426 if (unlikely(PageDirty(page)))
427 goto cannot_free;
428
429 if (PageSwapCache(page)) {
430 swp_entry_t swap = { .val = page_private(page) };
431 __delete_from_swap_cache(page);
432 write_unlock_irq(&mapping->tree_lock);
433 swap_free(swap);
434 __put_page(page); /* The pagecache ref */
435 return 1;
436 }
437
438 __remove_from_page_cache(page);
439 write_unlock_irq(&mapping->tree_lock);
440 __put_page(page);
441 return 1;
442
443cannot_free:
444 write_unlock_irq(&mapping->tree_lock);
445 return 0;
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/*
Andrew Morton1742f192006-03-22 00:08:21 -0800449 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
Andrew Morton1742f192006-03-22 00:08:21 -0800451static unsigned long shrink_page_list(struct list_head *page_list,
Andy Whitcroftc661b072007-08-22 14:01:26 -0700452 struct scan_control *sc,
453 enum pageout_io sync_writeback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 LIST_HEAD(ret_pages);
456 struct pagevec freed_pvec;
457 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800458 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 cond_resched();
461
462 pagevec_init(&freed_pvec, 1);
463 while (!list_empty(page_list)) {
464 struct address_space *mapping;
465 struct page *page;
466 int may_enter_fs;
467 int referenced;
468
469 cond_resched();
470
471 page = lru_to_page(page_list);
472 list_del(&page->lru);
473
474 if (TestSetPageLocked(page))
475 goto keep;
476
Nick Piggin725d7042006-09-25 23:30:55 -0700477 VM_BUG_ON(PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800480
481 if (!sc->may_swap && page_mapped(page))
482 goto keep_locked;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /* Double the slab pressure for mapped and swapcache pages */
485 if (page_mapped(page) || PageSwapCache(page))
486 sc->nr_scanned++;
487
Andy Whitcroftc661b072007-08-22 14:01:26 -0700488 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
489 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
490
491 if (PageWriteback(page)) {
492 /*
493 * Synchronous reclaim is performed in two passes,
494 * first an asynchronous pass over the list to
495 * start parallel writeback, and a second synchronous
496 * pass to wait for the IO to complete. Wait here
497 * for any page for which writeback has already
498 * started.
499 */
500 if (sync_writeback == PAGEOUT_IO_SYNC && may_enter_fs)
501 wait_on_page_writeback(page);
502 else
503 goto keep_locked;
504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800506 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* In active use or really unfreeable? Activate it. */
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700508 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER &&
509 referenced && page_mapping_inuse(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto activate_locked;
511
512#ifdef CONFIG_SWAP
513 /*
514 * Anonymous process memory has backing store?
515 * Try to allocate it some swap space here.
516 */
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800517 if (PageAnon(page) && !PageSwapCache(page))
Christoph Lameter1480a542006-01-08 01:00:53 -0800518 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520#endif /* CONFIG_SWAP */
521
522 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 /*
525 * The page is mapped into the page tables of one or more
526 * processes. Try to unmap it here.
527 */
528 if (page_mapped(page) && mapping) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800529 switch (try_to_unmap(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 case SWAP_FAIL:
531 goto activate_locked;
532 case SWAP_AGAIN:
533 goto keep_locked;
534 case SWAP_SUCCESS:
535 ; /* try to free the page below */
536 }
537 }
538
539 if (PageDirty(page)) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700540 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && referenced)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 goto keep_locked;
542 if (!may_enter_fs)
543 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800544 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto keep_locked;
546
547 /* Page is dirty, try to write it out here */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700548 switch (pageout(page, mapping, sync_writeback)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 case PAGE_KEEP:
550 goto keep_locked;
551 case PAGE_ACTIVATE:
552 goto activate_locked;
553 case PAGE_SUCCESS:
554 if (PageWriteback(page) || PageDirty(page))
555 goto keep;
556 /*
557 * A synchronous write - probably a ramdisk. Go
558 * ahead and try to reclaim the page.
559 */
560 if (TestSetPageLocked(page))
561 goto keep;
562 if (PageDirty(page) || PageWriteback(page))
563 goto keep_locked;
564 mapping = page_mapping(page);
565 case PAGE_CLEAN:
566 ; /* try to free the page below */
567 }
568 }
569
570 /*
571 * If the page has buffers, try to free the buffer mappings
572 * associated with this page. If we succeed we try to free
573 * the page as well.
574 *
575 * We do this even if the page is PageDirty().
576 * try_to_release_page() does not perform I/O, but it is
577 * possible for a page to have PageDirty set, but it is actually
578 * clean (all its buffers are clean). This happens if the
579 * buffers were written out directly, with submit_bh(). ext3
580 * will do this, as well as the blockdev mapping.
581 * try_to_release_page() will discover that cleanness and will
582 * drop the buffers and mark the page clean - it can be freed.
583 *
584 * Rarely, pages can have buffers and no ->mapping. These are
585 * the pages which were not successfully invalidated in
586 * truncate_complete_page(). We try to drop those buffers here
587 * and if that worked, and the page is no longer mapped into
588 * process address space (page_count == 1) it can be freed.
589 * Otherwise, leave the page on the LRU so it is swappable.
590 */
591 if (PagePrivate(page)) {
592 if (!try_to_release_page(page, sc->gfp_mask))
593 goto activate_locked;
594 if (!mapping && page_count(page) == 1)
595 goto free_it;
596 }
597
Nick Piggin28e4d962006-09-25 23:31:23 -0700598 if (!mapping || !remove_mapping(mapping, page))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800599 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601free_it:
602 unlock_page(page);
Andrew Morton05ff5132006-03-22 00:08:20 -0800603 nr_reclaimed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!pagevec_add(&freed_pvec, page))
605 __pagevec_release_nonlru(&freed_pvec);
606 continue;
607
608activate_locked:
609 SetPageActive(page);
610 pgactivate++;
611keep_locked:
612 unlock_page(page);
613keep:
614 list_add(&page->lru, &ret_pages);
Nick Piggin725d7042006-09-25 23:30:55 -0700615 VM_BUG_ON(PageLRU(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 list_splice(&ret_pages, page_list);
618 if (pagevec_count(&freed_pvec))
619 __pagevec_release_nonlru(&freed_pvec);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700620 count_vm_events(PGACTIVATE, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800621 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700624/* LRU Isolation modes. */
625#define ISOLATE_INACTIVE 0 /* Isolate inactive pages. */
626#define ISOLATE_ACTIVE 1 /* Isolate active pages. */
627#define ISOLATE_BOTH 2 /* Isolate both active and inactive pages. */
628
629/*
630 * Attempt to remove the specified page from its LRU. Only take this page
631 * if it is of the appropriate PageActive status. Pages which are being
632 * freed elsewhere are also ignored.
633 *
634 * page: page to consider
635 * mode: one of the LRU isolation modes defined above
636 *
637 * returns 0 on success, -ve errno on failure.
638 */
Balbir Singh66e17072008-02-07 00:13:56 -0800639int __isolate_lru_page(struct page *page, int mode)
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700640{
641 int ret = -EINVAL;
642
643 /* Only take pages on the LRU. */
644 if (!PageLRU(page))
645 return ret;
646
647 /*
648 * When checking the active state, we need to be sure we are
649 * dealing with comparible boolean values. Take the logical not
650 * of each.
651 */
652 if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode))
653 return ret;
654
655 ret = -EBUSY;
656 if (likely(get_page_unless_zero(page))) {
657 /*
658 * Be careful not to clear PageLRU until after we're
659 * sure the page is not being freed elsewhere -- the
660 * page release code relies on it.
661 */
662 ClearPageLRU(page);
663 ret = 0;
664 }
665
666 return ret;
667}
668
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800669/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 * zone->lru_lock is heavily contended. Some of the functions that
671 * shrink the lists perform better by taking out a batch of pages
672 * and working on them outside the LRU lock.
673 *
674 * For pagecache intensive workloads, this function is the hottest
675 * spot in the kernel (apart from copy_*_user functions).
676 *
677 * Appropriate locks must be held before calling this function.
678 *
679 * @nr_to_scan: The number of pages to look through on the list.
680 * @src: The LRU list to pull pages off.
681 * @dst: The temp list to put pages on to.
682 * @scanned: The number of pages that were scanned.
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700683 * @order: The caller's attempted allocation order
684 * @mode: One of the LRU isolation modes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 *
686 * returns how many pages were moved onto *@dst.
687 */
Andrew Morton69e05942006-03-22 00:08:19 -0800688static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
689 struct list_head *src, struct list_head *dst,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700690 unsigned long *scanned, int order, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Andrew Morton69e05942006-03-22 00:08:19 -0800692 unsigned long nr_taken = 0;
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800693 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800695 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700696 struct page *page;
697 unsigned long pfn;
698 unsigned long end_pfn;
699 unsigned long page_pfn;
700 int zone_id;
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 page = lru_to_page(src);
703 prefetchw_prev_lru_page(page, src, flags);
704
Nick Piggin725d7042006-09-25 23:30:55 -0700705 VM_BUG_ON(!PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800706
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700707 switch (__isolate_lru_page(page, mode)) {
708 case 0:
709 list_move(&page->lru, dst);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800710 nr_taken++;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700711 break;
Nick Piggin46453a62006-03-22 00:07:58 -0800712
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700713 case -EBUSY:
714 /* else it is being freed elsewhere */
715 list_move(&page->lru, src);
716 continue;
717
718 default:
719 BUG();
720 }
721
722 if (!order)
723 continue;
724
725 /*
726 * Attempt to take all pages in the order aligned region
727 * surrounding the tag page. Only take those pages of
728 * the same active state as that tag page. We may safely
729 * round the target page pfn down to the requested order
730 * as the mem_map is guarenteed valid out to MAX_ORDER,
731 * where that page is in a different zone we will detect
732 * it from its zone id and abort this block scan.
733 */
734 zone_id = page_zone_id(page);
735 page_pfn = page_to_pfn(page);
736 pfn = page_pfn & ~((1 << order) - 1);
737 end_pfn = pfn + (1 << order);
738 for (; pfn < end_pfn; pfn++) {
739 struct page *cursor_page;
740
741 /* The target page is in the block, ignore it. */
742 if (unlikely(pfn == page_pfn))
743 continue;
744
745 /* Avoid holes within the zone. */
746 if (unlikely(!pfn_valid_within(pfn)))
747 break;
748
749 cursor_page = pfn_to_page(pfn);
750 /* Check that we have not crossed a zone boundary. */
751 if (unlikely(page_zone_id(cursor_page) != zone_id))
752 continue;
753 switch (__isolate_lru_page(cursor_page, mode)) {
754 case 0:
755 list_move(&cursor_page->lru, dst);
756 nr_taken++;
757 scan++;
758 break;
759
760 case -EBUSY:
761 /* else it is being freed elsewhere */
762 list_move(&cursor_page->lru, src);
763 default:
764 break;
765 }
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
769 *scanned = scan;
770 return nr_taken;
771}
772
Balbir Singh66e17072008-02-07 00:13:56 -0800773static unsigned long isolate_pages_global(unsigned long nr,
774 struct list_head *dst,
775 unsigned long *scanned, int order,
776 int mode, struct zone *z,
777 struct mem_cgroup *mem_cont,
778 int active)
779{
780 if (active)
781 return isolate_lru_pages(nr, &z->active_list, dst,
782 scanned, order, mode);
783 else
784 return isolate_lru_pages(nr, &z->inactive_list, dst,
785 scanned, order, mode);
786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788/*
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700789 * clear_active_flags() is a helper for shrink_active_list(), clearing
790 * any active bits from the pages in the list.
791 */
792static unsigned long clear_active_flags(struct list_head *page_list)
793{
794 int nr_active = 0;
795 struct page *page;
796
797 list_for_each_entry(page, page_list, lru)
798 if (PageActive(page)) {
799 ClearPageActive(page);
800 nr_active++;
801 }
802
803 return nr_active;
804}
805
806/*
Andrew Morton1742f192006-03-22 00:08:21 -0800807 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
808 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
Andrew Morton1742f192006-03-22 00:08:21 -0800810static unsigned long shrink_inactive_list(unsigned long max_scan,
811 struct zone *zone, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 LIST_HEAD(page_list);
814 struct pagevec pvec;
Andrew Morton69e05942006-03-22 00:08:19 -0800815 unsigned long nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800816 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 pagevec_init(&pvec, 1);
819
820 lru_add_drain();
821 spin_lock_irq(&zone->lru_lock);
Andrew Morton69e05942006-03-22 00:08:19 -0800822 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -0800824 unsigned long nr_taken;
825 unsigned long nr_scan;
826 unsigned long nr_freed;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700827 unsigned long nr_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Balbir Singh66e17072008-02-07 00:13:56 -0800829 nr_taken = sc->isolate_pages(sc->swap_cluster_max,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700830 &page_list, &nr_scan, sc->order,
831 (sc->order > PAGE_ALLOC_COSTLY_ORDER)?
Balbir Singh66e17072008-02-07 00:13:56 -0800832 ISOLATE_BOTH : ISOLATE_INACTIVE,
833 zone, sc->mem_cgroup, 0);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700834 nr_active = clear_active_flags(&page_list);
Andy Whitcrofte9187bd2007-08-22 14:01:25 -0700835 __count_vm_events(PGDEACTIVATE, nr_active);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700836
837 __mod_zone_page_state(zone, NR_ACTIVE, -nr_active);
838 __mod_zone_page_state(zone, NR_INACTIVE,
839 -(nr_taken - nr_active));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 zone->pages_scanned += nr_scan;
841 spin_unlock_irq(&zone->lru_lock);
842
Andrew Morton69e05942006-03-22 00:08:19 -0800843 nr_scanned += nr_scan;
Andy Whitcroftc661b072007-08-22 14:01:26 -0700844 nr_freed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC);
845
846 /*
847 * If we are direct reclaiming for contiguous pages and we do
848 * not reclaim everything in the list, try again and wait
849 * for IO to complete. This will stall high-order allocations
850 * but that should be acceptable to the caller
851 */
852 if (nr_freed < nr_taken && !current_is_kswapd() &&
853 sc->order > PAGE_ALLOC_COSTLY_ORDER) {
854 congestion_wait(WRITE, HZ/10);
855
856 /*
857 * The attempt at page out may have made some
858 * of the pages active, mark them inactive again.
859 */
860 nr_active = clear_active_flags(&page_list);
861 count_vm_events(PGDEACTIVATE, nr_active);
862
863 nr_freed += shrink_page_list(&page_list, sc,
864 PAGEOUT_IO_SYNC);
865 }
866
Andrew Morton05ff5132006-03-22 00:08:20 -0800867 nr_reclaimed += nr_freed;
Nick Piggina74609f2006-01-06 00:11:20 -0800868 local_irq_disable();
869 if (current_is_kswapd()) {
Christoph Lameterf8891e52006-06-30 01:55:45 -0700870 __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
871 __count_vm_events(KSWAPD_STEAL, nr_freed);
Nick Piggina74609f2006-01-06 00:11:20 -0800872 } else
Christoph Lameterf8891e52006-06-30 01:55:45 -0700873 __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
Shantanu Goel918d3f92006-12-29 16:48:59 -0800874 __count_zone_vm_events(PGSTEAL, zone, nr_freed);
Nick Piggina74609f2006-01-06 00:11:20 -0800875
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800876 if (nr_taken == 0)
877 goto done;
878
Nick Piggina74609f2006-01-06 00:11:20 -0800879 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /*
881 * Put back any unfreeable pages.
882 */
883 while (!list_empty(&page_list)) {
884 page = lru_to_page(&page_list);
Nick Piggin725d7042006-09-25 23:30:55 -0700885 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800886 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 list_del(&page->lru);
888 if (PageActive(page))
889 add_page_to_active_list(zone, page);
890 else
891 add_page_to_inactive_list(zone, page);
892 if (!pagevec_add(&pvec, page)) {
893 spin_unlock_irq(&zone->lru_lock);
894 __pagevec_release(&pvec);
895 spin_lock_irq(&zone->lru_lock);
896 }
897 }
Andrew Morton69e05942006-03-22 00:08:19 -0800898 } while (nr_scanned < max_scan);
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800899 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900done:
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800901 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 pagevec_release(&pvec);
Andrew Morton05ff5132006-03-22 00:08:20 -0800903 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Martin Bligh3bb1a8522006-10-28 10:38:24 -0700906/*
907 * We are about to scan this zone at a certain priority level. If that priority
908 * level is smaller (ie: more urgent) than the previous priority, then note
909 * that priority level within the zone. This is done so that when the next
910 * process comes in to scan this zone, it will immediately start out at this
911 * priority level rather than having to build up its own scanning priority.
912 * Here, this priority affects only the reclaim-mapped threshold.
913 */
914static inline void note_zone_scanning_priority(struct zone *zone, int priority)
915{
916 if (priority < zone->prev_priority)
917 zone->prev_priority = priority;
918}
919
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700920static inline int zone_is_near_oom(struct zone *zone)
921{
Christoph Lameterc8785382007-02-10 01:43:01 -0800922 return zone->pages_scanned >= (zone_page_state(zone, NR_ACTIVE)
923 + zone_page_state(zone, NR_INACTIVE))*3;
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926/*
927 * This moves pages from the active list to the inactive list.
928 *
929 * We move them the other way if the page is referenced by one or more
930 * processes, from rmap.
931 *
932 * If the pages are mostly unmapped, the processing is fast and it is
933 * appropriate to hold zone->lru_lock across the whole operation. But if
934 * the pages are mapped, the processing is slow (page_referenced()) so we
935 * should drop zone->lru_lock around each page. It's impossible to balance
936 * this, so instead we remove the pages from the LRU while processing them.
937 * It is safe to rely on PG_active against the non-LRU pages in here because
938 * nobody will play with that bit on a non-LRU page.
939 *
940 * The downside is that we have to touch page->_count against each page.
941 * But we had to alter page->flags anyway.
942 */
Andrew Morton1742f192006-03-22 00:08:21 -0800943static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
Martin Blighbbdb3962006-10-28 10:38:25 -0700944 struct scan_control *sc, int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Andrew Morton69e05942006-03-22 00:08:19 -0800946 unsigned long pgmoved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 int pgdeactivate = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800948 unsigned long pgscanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 LIST_HEAD(l_hold); /* The pages which were snipped off */
950 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
951 LIST_HEAD(l_active); /* Pages to go onto the active_list */
952 struct page *page;
953 struct pagevec pvec;
954 int reclaim_mapped = 0;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800955
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800956 if (sc->may_swap) {
Christoph Lameter2903fb12006-02-11 17:55:55 -0800957 long mapped_ratio;
958 long distress;
959 long swap_tendency;
Andrea Arcangeli4106f832007-10-16 01:25:42 -0700960 long imbalance;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800961
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700962 if (zone_is_near_oom(zone))
963 goto force_reclaim_mapped;
964
Christoph Lameter2903fb12006-02-11 17:55:55 -0800965 /*
966 * `distress' is a measure of how much trouble we're having
967 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
968 */
Martin Blighbbdb3962006-10-28 10:38:25 -0700969 distress = 100 >> min(zone->prev_priority, priority);
Christoph Lameter2903fb12006-02-11 17:55:55 -0800970
971 /*
972 * The point of this algorithm is to decide when to start
973 * reclaiming mapped memory instead of just pagecache. Work out
974 * how much memory
975 * is mapped.
976 */
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700977 mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
978 global_page_state(NR_ANON_PAGES)) * 100) /
Christoph Lameterbf02cf42006-06-30 01:55:36 -0700979 vm_total_pages;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800980
981 /*
982 * Now decide how much we really want to unmap some pages. The
983 * mapped ratio is downgraded - just because there's a lot of
984 * mapped memory doesn't necessarily mean that page reclaim
985 * isn't succeeding.
986 *
987 * The distress ratio is important - we don't want to start
988 * going oom.
989 *
990 * A 100% value of vm_swappiness overrides this algorithm
991 * altogether.
992 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700993 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800994
995 /*
Andrea Arcangeli4106f832007-10-16 01:25:42 -0700996 * If there's huge imbalance between active and inactive
997 * (think active 100 times larger than inactive) we should
998 * become more permissive, or the system will take too much
999 * cpu before it start swapping during memory pressure.
1000 * Distress is about avoiding early-oom, this is about
1001 * making swappiness graceful despite setting it to low
1002 * values.
1003 *
1004 * Avoid div by zero with nr_inactive+1, and max resulting
1005 * value is vm_total_pages.
1006 */
1007 imbalance = zone_page_state(zone, NR_ACTIVE);
1008 imbalance /= zone_page_state(zone, NR_INACTIVE) + 1;
1009
1010 /*
1011 * Reduce the effect of imbalance if swappiness is low,
1012 * this means for a swappiness very low, the imbalance
1013 * must be much higher than 100 for this logic to make
1014 * the difference.
1015 *
1016 * Max temporary value is vm_total_pages*100.
1017 */
1018 imbalance *= (vm_swappiness + 1);
1019 imbalance /= 100;
1020
1021 /*
1022 * If not much of the ram is mapped, makes the imbalance
1023 * less relevant, it's high priority we refill the inactive
1024 * list with mapped pages only in presence of high ratio of
1025 * mapped pages.
1026 *
1027 * Max temporary value is vm_total_pages*100.
1028 */
1029 imbalance *= mapped_ratio;
1030 imbalance /= 100;
1031
1032 /* apply imbalance feedback to swap_tendency */
1033 swap_tendency += imbalance;
1034
1035 /*
Christoph Lameter2903fb12006-02-11 17:55:55 -08001036 * Now use this metric to decide whether to start moving mapped
1037 * memory onto the inactive list.
1038 */
1039 if (swap_tendency >= 100)
Nick Piggin4ff1ffb2006-09-25 23:31:28 -07001040force_reclaim_mapped:
Christoph Lameter2903fb12006-02-11 17:55:55 -08001041 reclaim_mapped = 1;
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 lru_add_drain();
1045 spin_lock_irq(&zone->lru_lock);
Balbir Singh66e17072008-02-07 00:13:56 -08001046 pgmoved = sc->isolate_pages(nr_pages, &l_hold, &pgscanned, sc->order,
1047 ISOLATE_ACTIVE, zone,
1048 sc->mem_cgroup, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 zone->pages_scanned += pgscanned;
Christoph Lameterc8785382007-02-10 01:43:01 -08001050 __mod_zone_page_state(zone, NR_ACTIVE, -pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 spin_unlock_irq(&zone->lru_lock);
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 while (!list_empty(&l_hold)) {
1054 cond_resched();
1055 page = lru_to_page(&l_hold);
1056 list_del(&page->lru);
1057 if (page_mapped(page)) {
1058 if (!reclaim_mapped ||
1059 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001060 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 list_add(&page->lru, &l_active);
1062 continue;
1063 }
1064 }
1065 list_add(&page->lru, &l_inactive);
1066 }
1067
1068 pagevec_init(&pvec, 1);
1069 pgmoved = 0;
1070 spin_lock_irq(&zone->lru_lock);
1071 while (!list_empty(&l_inactive)) {
1072 page = lru_to_page(&l_inactive);
1073 prefetchw_prev_lru_page(page, &l_inactive, flags);
Nick Piggin725d7042006-09-25 23:30:55 -07001074 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -08001075 SetPageLRU(page);
Nick Piggin725d7042006-09-25 23:30:55 -07001076 VM_BUG_ON(!PageActive(page));
Nick Piggin4c84cac2006-03-22 00:08:00 -08001077 ClearPageActive(page);
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 list_move(&page->lru, &zone->inactive_list);
Balbir Singh66e17072008-02-07 00:13:56 -08001080 mem_cgroup_move_lists(page_get_page_cgroup(page), false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 pgmoved++;
1082 if (!pagevec_add(&pvec, page)) {
Christoph Lameterc8785382007-02-10 01:43:01 -08001083 __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 spin_unlock_irq(&zone->lru_lock);
1085 pgdeactivate += pgmoved;
1086 pgmoved = 0;
1087 if (buffer_heads_over_limit)
1088 pagevec_strip(&pvec);
1089 __pagevec_release(&pvec);
1090 spin_lock_irq(&zone->lru_lock);
1091 }
1092 }
Christoph Lameterc8785382007-02-10 01:43:01 -08001093 __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 pgdeactivate += pgmoved;
1095 if (buffer_heads_over_limit) {
1096 spin_unlock_irq(&zone->lru_lock);
1097 pagevec_strip(&pvec);
1098 spin_lock_irq(&zone->lru_lock);
1099 }
1100
1101 pgmoved = 0;
1102 while (!list_empty(&l_active)) {
1103 page = lru_to_page(&l_active);
1104 prefetchw_prev_lru_page(page, &l_active, flags);
Nick Piggin725d7042006-09-25 23:30:55 -07001105 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -08001106 SetPageLRU(page);
Nick Piggin725d7042006-09-25 23:30:55 -07001107 VM_BUG_ON(!PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 list_move(&page->lru, &zone->active_list);
Balbir Singh66e17072008-02-07 00:13:56 -08001109 mem_cgroup_move_lists(page_get_page_cgroup(page), true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 pgmoved++;
1111 if (!pagevec_add(&pvec, page)) {
Christoph Lameterc8785382007-02-10 01:43:01 -08001112 __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 pgmoved = 0;
1114 spin_unlock_irq(&zone->lru_lock);
1115 __pagevec_release(&pvec);
1116 spin_lock_irq(&zone->lru_lock);
1117 }
1118 }
Christoph Lameterc8785382007-02-10 01:43:01 -08001119 __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Christoph Lameterf8891e52006-06-30 01:55:45 -07001121 __count_zone_vm_events(PGREFILL, zone, pgscanned);
1122 __count_vm_events(PGDEACTIVATE, pgdeactivate);
1123 spin_unlock_irq(&zone->lru_lock);
Nick Piggina74609f2006-01-06 00:11:20 -08001124
1125 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128/*
1129 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1130 */
Andrew Morton05ff5132006-03-22 00:08:20 -08001131static unsigned long shrink_zone(int priority, struct zone *zone,
1132 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 unsigned long nr_active;
1135 unsigned long nr_inactive;
Christoph Lameter86959492006-03-22 00:08:18 -08001136 unsigned long nr_to_scan;
Andrew Morton05ff5132006-03-22 00:08:20 -08001137 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /*
1140 * Add one to `nr_to_scan' just to make sure that the kernel will
1141 * slowly sift through the active list.
1142 */
Christoph Lameterc8785382007-02-10 01:43:01 -08001143 zone->nr_scan_active +=
1144 (zone_page_state(zone, NR_ACTIVE) >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 nr_active = zone->nr_scan_active;
1146 if (nr_active >= sc->swap_cluster_max)
1147 zone->nr_scan_active = 0;
1148 else
1149 nr_active = 0;
1150
Christoph Lameterc8785382007-02-10 01:43:01 -08001151 zone->nr_scan_inactive +=
1152 (zone_page_state(zone, NR_INACTIVE) >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 nr_inactive = zone->nr_scan_inactive;
1154 if (nr_inactive >= sc->swap_cluster_max)
1155 zone->nr_scan_inactive = 0;
1156 else
1157 nr_inactive = 0;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 while (nr_active || nr_inactive) {
1160 if (nr_active) {
Christoph Lameter86959492006-03-22 00:08:18 -08001161 nr_to_scan = min(nr_active,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -08001163 nr_active -= nr_to_scan;
Martin Blighbbdb3962006-10-28 10:38:25 -07001164 shrink_active_list(nr_to_scan, zone, sc, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166
1167 if (nr_inactive) {
Christoph Lameter86959492006-03-22 00:08:18 -08001168 nr_to_scan = min(nr_inactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -08001170 nr_inactive -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -08001171 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
1172 sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174 }
1175
Andrew Morton232ea4d2007-02-28 20:13:21 -08001176 throttle_vm_writeout(sc->gfp_mask);
Andrew Morton05ff5132006-03-22 00:08:20 -08001177 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
1180/*
1181 * This is the direct reclaim path, for page-allocating processes. We only
1182 * try to reclaim pages from zones which will satisfy the caller's allocation
1183 * request.
1184 *
1185 * We reclaim from a zone even if that zone is over pages_high. Because:
1186 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1187 * allocation or
1188 * b) The zones may be over pages_high but they must go *over* pages_high to
1189 * satisfy the `incremental min' zone defense algorithm.
1190 *
1191 * Returns the number of reclaimed pages.
1192 *
1193 * If a zone is deemed to be full of pinned pages then just give it a light
1194 * scan then give up on it.
1195 */
Andrew Morton1742f192006-03-22 00:08:21 -08001196static unsigned long shrink_zones(int priority, struct zone **zones,
Andrew Morton05ff5132006-03-22 00:08:20 -08001197 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Andrew Morton05ff5132006-03-22 00:08:20 -08001199 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int i;
1201
Nick Piggin408d8542006-09-25 23:31:27 -07001202 sc->all_unreclaimable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 for (i = 0; zones[i] != NULL; i++) {
1204 struct zone *zone = zones[i];
1205
Con Kolivasf3fe6512006-01-06 00:11:15 -08001206 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 continue;
1208
Paul Jackson02a0e532006-12-13 00:34:25 -08001209 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 continue;
1211
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001212 note_zone_scanning_priority(zone, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
David Rientjese815af92007-10-16 23:25:54 -07001214 if (zone_is_all_unreclaimable(zone) && priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 continue; /* Let kswapd poll it */
1216
Nick Piggin408d8542006-09-25 23:31:27 -07001217 sc->all_unreclaimable = 0;
1218
Andrew Morton05ff5132006-03-22 00:08:20 -08001219 nr_reclaimed += shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Andrew Morton05ff5132006-03-22 00:08:20 -08001221 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
1224/*
1225 * This is the main entry point to direct page reclaim.
1226 *
1227 * If a full scan of the inactive list fails to free enough memory then we
1228 * are "out of memory" and something needs to be killed.
1229 *
1230 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1231 * high - the zone may be full of dirty or under-writeback pages, which this
1232 * caller can't do much about. We kick pdflush and take explicit naps in the
1233 * hope that some of these pages can be written. But if the allocating task
1234 * holds filesystem locks which prevent writeout this might not work, and the
1235 * allocation attempt will fail.
1236 */
Balbir Singh66e17072008-02-07 00:13:56 -08001237static unsigned long do_try_to_free_pages(struct zone **zones, gfp_t gfp_mask,
1238 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 int priority;
1241 int ret = 0;
Andrew Morton69e05942006-03-22 00:08:19 -08001242 unsigned long total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001243 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 struct reclaim_state *reclaim_state = current->reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 unsigned long lru_pages = 0;
1246 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Christoph Lameterf8891e52006-06-30 01:55:45 -07001248 count_vm_event(ALLOCSTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 for (i = 0; zones[i] != NULL; i++) {
1251 struct zone *zone = zones[i];
1252
Paul Jackson02a0e532006-12-13 00:34:25 -08001253 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 continue;
1255
Christoph Lameterc8785382007-02-10 01:43:01 -08001256 lru_pages += zone_page_state(zone, NR_ACTIVE)
1257 + zone_page_state(zone, NR_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
1260 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
Balbir Singh66e17072008-02-07 00:13:56 -08001261 sc->nr_scanned = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001262 if (!priority)
1263 disable_swap_token();
Balbir Singh66e17072008-02-07 00:13:56 -08001264 nr_reclaimed += shrink_zones(priority, zones, sc);
1265 /*
1266 * Don't shrink slabs when reclaiming memory from
1267 * over limit cgroups
1268 */
1269 if (sc->mem_cgroup == NULL)
1270 shrink_slab(sc->nr_scanned, gfp_mask, lru_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (reclaim_state) {
Andrew Morton05ff5132006-03-22 00:08:20 -08001272 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 reclaim_state->reclaimed_slab = 0;
1274 }
Balbir Singh66e17072008-02-07 00:13:56 -08001275 total_scanned += sc->nr_scanned;
1276 if (nr_reclaimed >= sc->swap_cluster_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 ret = 1;
1278 goto out;
1279 }
1280
1281 /*
1282 * Try to write back as many pages as we just scanned. This
1283 * tends to cause slow streaming writers to write data to the
1284 * disk smoothly, at the dirtying rate, which is nice. But
1285 * that's undesirable in laptop mode, where we *want* lumpy
1286 * writeout. So in laptop mode, write out the whole world.
1287 */
Balbir Singh66e17072008-02-07 00:13:56 -08001288 if (total_scanned > sc->swap_cluster_max +
1289 sc->swap_cluster_max / 2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001290 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Balbir Singh66e17072008-02-07 00:13:56 -08001291 sc->may_writepage = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293
1294 /* Take a nap, wait for some writeback to complete */
Balbir Singh66e17072008-02-07 00:13:56 -08001295 if (sc->nr_scanned && priority < DEF_PRIORITY - 2)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001296 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Nick Piggin408d8542006-09-25 23:31:27 -07001298 /* top priority shrink_caches still had more to do? don't OOM, then */
Balbir Singh66e17072008-02-07 00:13:56 -08001299 if (!sc->all_unreclaimable && sc->mem_cgroup == NULL)
Nick Piggin408d8542006-09-25 23:31:27 -07001300 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301out:
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001302 /*
1303 * Now that we've scanned all the zones at this priority level, note
1304 * that level within the zone so that the next thread which performs
1305 * scanning of this zone will immediately start out at this priority
1306 * level. This affects only the decision whether or not to bring
1307 * mapped pages onto the inactive list.
1308 */
1309 if (priority < 0)
1310 priority = 0;
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001311 for (i = 0; zones[i] != NULL; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 struct zone *zone = zones[i];
1313
Paul Jackson02a0e532006-12-13 00:34:25 -08001314 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 continue;
1316
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001317 zone->prev_priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319 return ret;
1320}
1321
Balbir Singh66e17072008-02-07 00:13:56 -08001322unsigned long try_to_free_pages(struct zone **zones, int order, gfp_t gfp_mask)
1323{
1324 struct scan_control sc = {
1325 .gfp_mask = gfp_mask,
1326 .may_writepage = !laptop_mode,
1327 .swap_cluster_max = SWAP_CLUSTER_MAX,
1328 .may_swap = 1,
1329 .swappiness = vm_swappiness,
1330 .order = order,
1331 .mem_cgroup = NULL,
1332 .isolate_pages = isolate_pages_global,
1333 };
1334
1335 return do_try_to_free_pages(zones, gfp_mask, &sc);
1336}
1337
1338#ifdef CONFIG_CGROUP_MEM_CONT
1339
1340#ifdef CONFIG_HIGHMEM
1341#define ZONE_USERPAGES ZONE_HIGHMEM
1342#else
1343#define ZONE_USERPAGES ZONE_NORMAL
1344#endif
1345
1346unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont)
1347{
1348 struct scan_control sc = {
1349 .gfp_mask = GFP_KERNEL,
1350 .may_writepage = !laptop_mode,
1351 .may_swap = 1,
1352 .swap_cluster_max = SWAP_CLUSTER_MAX,
1353 .swappiness = vm_swappiness,
1354 .order = 0,
1355 .mem_cgroup = mem_cont,
1356 .isolate_pages = mem_cgroup_isolate_pages,
1357 };
1358 int node;
1359 struct zone **zones;
1360
1361 for_each_online_node(node) {
1362 zones = NODE_DATA(node)->node_zonelists[ZONE_USERPAGES].zones;
1363 if (do_try_to_free_pages(zones, sc.gfp_mask, &sc))
1364 return 1;
1365 }
1366 return 0;
1367}
1368#endif
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370/*
1371 * For kswapd, balance_pgdat() will work across all this node's zones until
1372 * they are all at pages_high.
1373 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 * Returns the number of pages which were actually freed.
1375 *
1376 * There is special handling here for zones which are full of pinned pages.
1377 * This can happen if the pages are all mlocked, or if they are all used by
1378 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1379 * What we do is to detect the case where all pages in the zone have been
1380 * scanned twice and there has been zero successful reclaim. Mark the zone as
1381 * dead and from now on, only perform a short scan. Basically we're polling
1382 * the zone for when the problem goes away.
1383 *
1384 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1385 * zones which have free_pages > pages_high, but once a zone is found to have
1386 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1387 * of the number of free pages in the lower zones. This interoperates with
1388 * the page allocator fallback scheme to ensure that aging of pages is balanced
1389 * across the zones.
1390 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001391static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 int all_zones_ok;
1394 int priority;
1395 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08001396 unsigned long total_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001397 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08001399 struct scan_control sc = {
1400 .gfp_mask = GFP_KERNEL,
1401 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001402 .swap_cluster_max = SWAP_CLUSTER_MAX,
1403 .swappiness = vm_swappiness,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001404 .order = order,
Balbir Singh66e17072008-02-07 00:13:56 -08001405 .mem_cgroup = NULL,
1406 .isolate_pages = isolate_pages_global,
Andrew Morton179e9632006-03-22 00:08:18 -08001407 };
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001408 /*
1409 * temp_priority is used to remember the scanning priority at which
1410 * this zone was successfully refilled to free_pages == pages_high.
1411 */
1412 int temp_priority[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414loop_again:
1415 total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001416 nr_reclaimed = 0;
Christoph Lameterc0bbbc72006-06-11 15:22:26 -07001417 sc.may_writepage = !laptop_mode;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001418 count_vm_event(PAGEOUTRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001420 for (i = 0; i < pgdat->nr_zones; i++)
1421 temp_priority[i] = DEF_PRIORITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1424 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1425 unsigned long lru_pages = 0;
1426
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001427 /* The swap token gets in the way of swapout... */
1428 if (!priority)
1429 disable_swap_token();
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 all_zones_ok = 1;
1432
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001433 /*
1434 * Scan in the highmem->dma direction for the highest
1435 * zone which needs scanning
1436 */
1437 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1438 struct zone *zone = pgdat->node_zones + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001440 if (!populated_zone(zone))
1441 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
David Rientjese815af92007-10-16 23:25:54 -07001443 if (zone_is_all_unreclaimable(zone) &&
1444 priority != DEF_PRIORITY)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001445 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001447 if (!zone_watermark_ok(zone, order, zone->pages_high,
1448 0, 0)) {
1449 end_zone = i;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08001450 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
Andrew Mortone1dbeda2006-12-06 20:32:01 -08001453 if (i < 0)
1454 goto out;
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 for (i = 0; i <= end_zone; i++) {
1457 struct zone *zone = pgdat->node_zones + i;
1458
Christoph Lameterc8785382007-02-10 01:43:01 -08001459 lru_pages += zone_page_state(zone, NR_ACTIVE)
1460 + zone_page_state(zone, NR_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462
1463 /*
1464 * Now scan the zone in the dma->highmem direction, stopping
1465 * at the last zone which needs scanning.
1466 *
1467 * We do this because the page allocator works in the opposite
1468 * direction. This prevents the page allocator from allocating
1469 * pages behind kswapd's direction of progress, which would
1470 * cause too much scanning of the lower zones.
1471 */
1472 for (i = 0; i <= end_zone; i++) {
1473 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001474 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Con Kolivasf3fe6512006-01-06 00:11:15 -08001476 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 continue;
1478
David Rientjese815af92007-10-16 23:25:54 -07001479 if (zone_is_all_unreclaimable(zone) &&
1480 priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 continue;
1482
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001483 if (!zone_watermark_ok(zone, order, zone->pages_high,
1484 end_zone, 0))
1485 all_zones_ok = 0;
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001486 temp_priority[i] = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 sc.nr_scanned = 0;
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001488 note_zone_scanning_priority(zone, priority);
Rik van Riel32a43302007-10-16 01:24:50 -07001489 /*
1490 * We put equal pressure on every zone, unless one
1491 * zone has way too many pages free already.
1492 */
1493 if (!zone_watermark_ok(zone, order, 8*zone->pages_high,
1494 end_zone, 0))
1495 nr_reclaimed += shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001497 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1498 lru_pages);
Andrew Morton05ff5132006-03-22 00:08:20 -08001499 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 total_scanned += sc.nr_scanned;
David Rientjese815af92007-10-16 23:25:54 -07001501 if (zone_is_all_unreclaimable(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001503 if (nr_slab == 0 && zone->pages_scanned >=
Christoph Lameterc8785382007-02-10 01:43:01 -08001504 (zone_page_state(zone, NR_ACTIVE)
1505 + zone_page_state(zone, NR_INACTIVE)) * 6)
David Rientjese815af92007-10-16 23:25:54 -07001506 zone_set_flag(zone,
1507 ZONE_ALL_UNRECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /*
1509 * If we've done a decent amount of scanning and
1510 * the reclaim ratio is low, start doing writepage
1511 * even in laptop mode
1512 */
1513 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Andrew Morton05ff5132006-03-22 00:08:20 -08001514 total_scanned > nr_reclaimed + nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 sc.may_writepage = 1;
1516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (all_zones_ok)
1518 break; /* kswapd: all done */
1519 /*
1520 * OK, kswapd is getting into trouble. Take a nap, then take
1521 * another pass across the zones.
1522 */
1523 if (total_scanned && priority < DEF_PRIORITY - 2)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001524 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 /*
1527 * We do this so kswapd doesn't build up large priorities for
1528 * example when it is freeing in parallel with allocators. It
1529 * matches the direct reclaim path behaviour in terms of impact
1530 * on zone->*_priority.
1531 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001532 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 break;
1534 }
1535out:
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001536 /*
1537 * Note within each zone the priority level at which this zone was
1538 * brought into a happy state. So that the next thread which scans this
1539 * zone will start out at that priority level.
1540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 for (i = 0; i < pgdat->nr_zones; i++) {
1542 struct zone *zone = pgdat->node_zones + i;
1543
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001544 zone->prev_priority = temp_priority[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546 if (!all_zones_ok) {
1547 cond_resched();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001548
1549 try_to_freeze();
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 goto loop_again;
1552 }
1553
Andrew Morton05ff5132006-03-22 00:08:20 -08001554 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
1557/*
1558 * The background pageout daemon, started as a kernel thread
1559 * from the init process.
1560 *
1561 * This basically trickles out pages so that we have _some_
1562 * free memory available even if there is no other activity
1563 * that frees anything up. This is needed for things like routing
1564 * etc, where we otherwise might have all activity going on in
1565 * asynchronous contexts that cannot page things out.
1566 *
1567 * If there are applications that are active memory-allocators
1568 * (most normal use), this basically shouldn't matter.
1569 */
1570static int kswapd(void *p)
1571{
1572 unsigned long order;
1573 pg_data_t *pgdat = (pg_data_t*)p;
1574 struct task_struct *tsk = current;
1575 DEFINE_WAIT(wait);
1576 struct reclaim_state reclaim_state = {
1577 .reclaimed_slab = 0,
1578 };
1579 cpumask_t cpumask;
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 cpumask = node_to_cpumask(pgdat->node_id);
1582 if (!cpus_empty(cpumask))
1583 set_cpus_allowed(tsk, cpumask);
1584 current->reclaim_state = &reclaim_state;
1585
1586 /*
1587 * Tell the memory management that we're a "memory allocator",
1588 * and that if we need more memory we should get access to it
1589 * regardless (see "__alloc_pages()"). "kswapd" should
1590 * never get caught in the normal page freeing logic.
1591 *
1592 * (Kswapd normally doesn't need memory anyway, but sometimes
1593 * you need a small amount of memory in order to be able to
1594 * page out something else, and this flag essentially protects
1595 * us from recursively trying to free more memory as we're
1596 * trying to free the first piece of memory in the first place).
1597 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001598 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001599 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 order = 0;
1602 for ( ; ; ) {
1603 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1606 new_order = pgdat->kswapd_max_order;
1607 pgdat->kswapd_max_order = 0;
1608 if (order < new_order) {
1609 /*
1610 * Don't sleep if someone wants a larger 'order'
1611 * allocation
1612 */
1613 order = new_order;
1614 } else {
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07001615 if (!freezing(current))
1616 schedule();
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 order = pgdat->kswapd_max_order;
1619 }
1620 finish_wait(&pgdat->kswapd_wait, &wait);
1621
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07001622 if (!try_to_freeze()) {
1623 /* We can speed up thawing tasks if we don't call
1624 * balance_pgdat after returning from the refrigerator
1625 */
1626 balance_pgdat(pgdat, order);
1627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629 return 0;
1630}
1631
1632/*
1633 * A zone is low on free memory, so wake its kswapd task to service it.
1634 */
1635void wakeup_kswapd(struct zone *zone, int order)
1636{
1637 pg_data_t *pgdat;
1638
Con Kolivasf3fe6512006-01-06 00:11:15 -08001639 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return;
1641
1642 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001643 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return;
1645 if (pgdat->kswapd_max_order < order)
1646 pgdat->kswapd_max_order = order;
Paul Jackson02a0e532006-12-13 00:34:25 -08001647 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001649 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001651 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654#ifdef CONFIG_PM
1655/*
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001656 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1657 * from LRU lists system-wide, for given pass and priority, and returns the
1658 * number of reclaimed pages
1659 *
1660 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1661 */
Nigel Cunninghame07aa052006-12-22 01:07:21 -08001662static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
1663 int pass, struct scan_control *sc)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001664{
1665 struct zone *zone;
1666 unsigned long nr_to_scan, ret = 0;
1667
1668 for_each_zone(zone) {
1669
1670 if (!populated_zone(zone))
1671 continue;
1672
David Rientjese815af92007-10-16 23:25:54 -07001673 if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001674 continue;
1675
1676 /* For pass = 0 we don't shrink the active list */
1677 if (pass > 0) {
Christoph Lameterc8785382007-02-10 01:43:01 -08001678 zone->nr_scan_active +=
1679 (zone_page_state(zone, NR_ACTIVE) >> prio) + 1;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001680 if (zone->nr_scan_active >= nr_pages || pass > 3) {
1681 zone->nr_scan_active = 0;
Christoph Lameterc8785382007-02-10 01:43:01 -08001682 nr_to_scan = min(nr_pages,
1683 zone_page_state(zone, NR_ACTIVE));
Martin Blighbbdb3962006-10-28 10:38:25 -07001684 shrink_active_list(nr_to_scan, zone, sc, prio);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001685 }
1686 }
1687
Christoph Lameterc8785382007-02-10 01:43:01 -08001688 zone->nr_scan_inactive +=
1689 (zone_page_state(zone, NR_INACTIVE) >> prio) + 1;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001690 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1691 zone->nr_scan_inactive = 0;
Christoph Lameterc8785382007-02-10 01:43:01 -08001692 nr_to_scan = min(nr_pages,
1693 zone_page_state(zone, NR_INACTIVE));
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001694 ret += shrink_inactive_list(nr_to_scan, zone, sc);
1695 if (ret >= nr_pages)
1696 return ret;
1697 }
1698 }
1699
1700 return ret;
1701}
1702
Andrew Morton76395d32007-01-05 16:37:05 -08001703static unsigned long count_lru_pages(void)
1704{
Christoph Lameterc8785382007-02-10 01:43:01 -08001705 return global_page_state(NR_ACTIVE) + global_page_state(NR_INACTIVE);
Andrew Morton76395d32007-01-05 16:37:05 -08001706}
1707
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001708/*
1709 * Try to free `nr_pages' of memory, system-wide, and return the number of
1710 * freed pages.
1711 *
1712 * Rather than trying to age LRUs the aim is to preserve the overall
1713 * LRU order by reclaiming preferentially
1714 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 */
Andrew Morton69e05942006-03-22 00:08:19 -08001716unsigned long shrink_all_memory(unsigned long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001718 unsigned long lru_pages, nr_slab;
Andrew Morton69e05942006-03-22 00:08:19 -08001719 unsigned long ret = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001720 int pass;
1721 struct reclaim_state reclaim_state;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001722 struct scan_control sc = {
1723 .gfp_mask = GFP_KERNEL,
1724 .may_swap = 0,
1725 .swap_cluster_max = nr_pages,
1726 .may_writepage = 1,
1727 .swappiness = vm_swappiness,
Balbir Singh66e17072008-02-07 00:13:56 -08001728 .isolate_pages = isolate_pages_global,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 };
1730
1731 current->reclaim_state = &reclaim_state;
Andrew Morton69e05942006-03-22 00:08:19 -08001732
Andrew Morton76395d32007-01-05 16:37:05 -08001733 lru_pages = count_lru_pages();
Christoph Lameter972d1a72006-09-25 23:31:51 -07001734 nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001735 /* If slab caches are huge, it's better to hit them first */
1736 while (nr_slab >= lru_pages) {
1737 reclaim_state.reclaimed_slab = 0;
1738 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1739 if (!reclaim_state.reclaimed_slab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 break;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001741
1742 ret += reclaim_state.reclaimed_slab;
1743 if (ret >= nr_pages)
1744 goto out;
1745
1746 nr_slab -= reclaim_state.reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001748
1749 /*
1750 * We try to shrink LRUs in 5 passes:
1751 * 0 = Reclaim from inactive_list only
1752 * 1 = Reclaim from active list but don't reclaim mapped
1753 * 2 = 2nd pass of type 1
1754 * 3 = Reclaim mapped (normal reclaim)
1755 * 4 = 2nd pass of type 3
1756 */
1757 for (pass = 0; pass < 5; pass++) {
1758 int prio;
1759
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001760 /* Force reclaiming mapped pages in the passes #3 and #4 */
1761 if (pass > 2) {
1762 sc.may_swap = 1;
1763 sc.swappiness = 100;
1764 }
1765
1766 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1767 unsigned long nr_to_scan = nr_pages - ret;
1768
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001769 sc.nr_scanned = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001770 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1771 if (ret >= nr_pages)
1772 goto out;
1773
1774 reclaim_state.reclaimed_slab = 0;
Andrew Morton76395d32007-01-05 16:37:05 -08001775 shrink_slab(sc.nr_scanned, sc.gfp_mask,
1776 count_lru_pages());
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001777 ret += reclaim_state.reclaimed_slab;
1778 if (ret >= nr_pages)
1779 goto out;
1780
1781 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001782 congestion_wait(WRITE, HZ / 10);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001783 }
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001784 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001785
1786 /*
1787 * If ret = 0, we could not shrink LRUs, but there may be something
1788 * in slab caches
1789 */
Andrew Morton76395d32007-01-05 16:37:05 -08001790 if (!ret) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001791 do {
1792 reclaim_state.reclaimed_slab = 0;
Andrew Morton76395d32007-01-05 16:37:05 -08001793 shrink_slab(nr_pages, sc.gfp_mask, count_lru_pages());
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001794 ret += reclaim_state.reclaimed_slab;
1795 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
Andrew Morton76395d32007-01-05 16:37:05 -08001796 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001797
1798out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 current->reclaim_state = NULL;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return ret;
1802}
1803#endif
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805/* It's optimal to keep kswapds on the same CPUs as their memory, but
1806 not required for correctness. So if the last cpu in a node goes
1807 away, we get changed to run anywhere: as the first one comes back,
1808 restore their cpu bindings. */
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07001809static int __devinit cpu_callback(struct notifier_block *nfb,
Andrew Morton69e05942006-03-22 00:08:19 -08001810 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 pg_data_t *pgdat;
1813 cpumask_t mask;
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07001814 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001816 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07001817 for_each_node_state(nid, N_HIGH_MEMORY) {
1818 pgdat = NODE_DATA(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 mask = node_to_cpumask(pgdat->node_id);
1820 if (any_online_cpu(mask) != NR_CPUS)
1821 /* One of our CPUs online: restore mask */
1822 set_cpus_allowed(pgdat->kswapd, mask);
1823 }
1824 }
1825 return NOTIFY_OK;
1826}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Yasunori Goto3218ae12006-06-27 02:53:33 -07001828/*
1829 * This kswapd start function will be called by init and node-hot-add.
1830 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
1831 */
1832int kswapd_run(int nid)
1833{
1834 pg_data_t *pgdat = NODE_DATA(nid);
1835 int ret = 0;
1836
1837 if (pgdat->kswapd)
1838 return 0;
1839
1840 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
1841 if (IS_ERR(pgdat->kswapd)) {
1842 /* failure at boot is fatal */
1843 BUG_ON(system_state == SYSTEM_BOOTING);
1844 printk("Failed to start kswapd on node %d\n",nid);
1845 ret = -1;
1846 }
1847 return ret;
1848}
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850static int __init kswapd_init(void)
1851{
Yasunori Goto3218ae12006-06-27 02:53:33 -07001852 int nid;
Andrew Morton69e05942006-03-22 00:08:19 -08001853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 swap_setup();
Christoph Lameter9422ffb2007-10-16 01:25:31 -07001855 for_each_node_state(nid, N_HIGH_MEMORY)
Yasunori Goto3218ae12006-06-27 02:53:33 -07001856 kswapd_run(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 hotcpu_notifier(cpu_callback, 0);
1858 return 0;
1859}
1860
1861module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001862
1863#ifdef CONFIG_NUMA
1864/*
1865 * Zone reclaim mode
1866 *
1867 * If non-zero call zone_reclaim when the number of free pages falls below
1868 * the watermarks.
Christoph Lameter9eeff232006-01-18 17:42:31 -08001869 */
1870int zone_reclaim_mode __read_mostly;
1871
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001872#define RECLAIM_OFF 0
1873#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1874#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1875#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1876
Christoph Lameter9eeff232006-01-18 17:42:31 -08001877/*
Christoph Lametera92f7122006-02-01 03:05:32 -08001878 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1879 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1880 * a zone.
1881 */
1882#define ZONE_RECLAIM_PRIORITY 4
1883
Christoph Lameter9eeff232006-01-18 17:42:31 -08001884/*
Christoph Lameter96146342006-07-03 00:24:13 -07001885 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
1886 * occur.
1887 */
1888int sysctl_min_unmapped_ratio = 1;
1889
1890/*
Christoph Lameter0ff38492006-09-25 23:31:52 -07001891 * If the number of slab pages in a zone grows beyond this percentage then
1892 * slab reclaim needs to occur.
1893 */
1894int sysctl_min_slab_ratio = 5;
1895
1896/*
Christoph Lameter9eeff232006-01-18 17:42:31 -08001897 * Try to free up some pages from this zone through reclaim.
1898 */
Andrew Morton179e9632006-03-22 00:08:18 -08001899static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001900{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001901 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08001902 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001903 struct task_struct *p = current;
1904 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08001905 int priority;
Andrew Morton05ff5132006-03-22 00:08:20 -08001906 unsigned long nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001907 struct scan_control sc = {
1908 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1909 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
Andrew Morton69e05942006-03-22 00:08:19 -08001910 .swap_cluster_max = max_t(unsigned long, nr_pages,
1911 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08001912 .gfp_mask = gfp_mask,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001913 .swappiness = vm_swappiness,
Balbir Singh66e17072008-02-07 00:13:56 -08001914 .isolate_pages = isolate_pages_global,
Andrew Morton179e9632006-03-22 00:08:18 -08001915 };
Christoph Lameter83e33a42006-09-25 23:31:53 -07001916 unsigned long slab_reclaimable;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001917
1918 disable_swap_token();
Christoph Lameter9eeff232006-01-18 17:42:31 -08001919 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08001920 /*
1921 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1922 * and we also need to be able to write out pages for RECLAIM_WRITE
1923 * and RECLAIM_SWAP.
1924 */
1925 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001926 reclaim_state.reclaimed_slab = 0;
1927 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001928
Christoph Lameter0ff38492006-09-25 23:31:52 -07001929 if (zone_page_state(zone, NR_FILE_PAGES) -
1930 zone_page_state(zone, NR_FILE_MAPPED) >
1931 zone->min_unmapped_pages) {
1932 /*
1933 * Free memory by calling shrink zone with increasing
1934 * priorities until we have enough memory freed.
1935 */
1936 priority = ZONE_RECLAIM_PRIORITY;
1937 do {
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001938 note_zone_scanning_priority(zone, priority);
Christoph Lameter0ff38492006-09-25 23:31:52 -07001939 nr_reclaimed += shrink_zone(priority, zone, &sc);
1940 priority--;
1941 } while (priority >= 0 && nr_reclaimed < nr_pages);
1942 }
Christoph Lameterc84db232006-02-01 03:05:29 -08001943
Christoph Lameter83e33a42006-09-25 23:31:53 -07001944 slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1945 if (slab_reclaimable > zone->min_slab_pages) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001946 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001947 * shrink_slab() does not currently allow us to determine how
Christoph Lameter0ff38492006-09-25 23:31:52 -07001948 * many pages were freed in this zone. So we take the current
1949 * number of slab pages and shake the slab until it is reduced
1950 * by the same nr_pages that we used for reclaiming unmapped
1951 * pages.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001952 *
Christoph Lameter0ff38492006-09-25 23:31:52 -07001953 * Note that shrink_slab will free memory on all zones and may
1954 * take a long time.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001955 */
Christoph Lameter0ff38492006-09-25 23:31:52 -07001956 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
Christoph Lameter83e33a42006-09-25 23:31:53 -07001957 zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
1958 slab_reclaimable - nr_pages)
Christoph Lameter0ff38492006-09-25 23:31:52 -07001959 ;
Christoph Lameter83e33a42006-09-25 23:31:53 -07001960
1961 /*
1962 * Update nr_reclaimed by the number of slab pages we
1963 * reclaimed from this zone.
1964 */
1965 nr_reclaimed += slab_reclaimable -
1966 zone_page_state(zone, NR_SLAB_RECLAIMABLE);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001967 }
1968
Christoph Lameter9eeff232006-01-18 17:42:31 -08001969 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08001970 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Andrew Morton05ff5132006-03-22 00:08:20 -08001971 return nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001972}
Andrew Morton179e9632006-03-22 00:08:18 -08001973
1974int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1975{
Andrew Morton179e9632006-03-22 00:08:18 -08001976 int node_id;
David Rientjesd773ed62007-10-16 23:26:01 -07001977 int ret;
Andrew Morton179e9632006-03-22 00:08:18 -08001978
1979 /*
Christoph Lameter0ff38492006-09-25 23:31:52 -07001980 * Zone reclaim reclaims unmapped file backed pages and
1981 * slab pages if we are over the defined limits.
Christoph Lameter34aa1332006-06-30 01:55:37 -07001982 *
Christoph Lameter96146342006-07-03 00:24:13 -07001983 * A small portion of unmapped file backed pages is needed for
1984 * file I/O otherwise pages read by file I/O will be immediately
1985 * thrown out if the zone is overallocated. So we do not reclaim
1986 * if less than a specified percentage of the zone is used by
1987 * unmapped file backed pages.
Andrew Morton179e9632006-03-22 00:08:18 -08001988 */
Christoph Lameter34aa1332006-06-30 01:55:37 -07001989 if (zone_page_state(zone, NR_FILE_PAGES) -
Christoph Lameter0ff38492006-09-25 23:31:52 -07001990 zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
1991 && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
1992 <= zone->min_slab_pages)
Christoph Lameter96146342006-07-03 00:24:13 -07001993 return 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001994
David Rientjesd773ed62007-10-16 23:26:01 -07001995 if (zone_is_all_unreclaimable(zone))
1996 return 0;
1997
Andrew Morton179e9632006-03-22 00:08:18 -08001998 /*
David Rientjesd773ed62007-10-16 23:26:01 -07001999 * Do not scan if the allocation should not be delayed.
Andrew Morton179e9632006-03-22 00:08:18 -08002000 */
David Rientjesd773ed62007-10-16 23:26:01 -07002001 if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
Andrew Morton179e9632006-03-22 00:08:18 -08002002 return 0;
2003
2004 /*
2005 * Only run zone reclaim on the local zone or on zones that do not
2006 * have associated processors. This will favor the local processor
2007 * over remote processors and spread off node memory allocations
2008 * as wide as possible.
2009 */
Christoph Lameter89fa3022006-09-25 23:31:55 -07002010 node_id = zone_to_nid(zone);
Christoph Lameter37c07082007-10-16 01:25:36 -07002011 if (node_state(node_id, N_CPU) && node_id != numa_node_id())
Andrew Morton179e9632006-03-22 00:08:18 -08002012 return 0;
David Rientjesd773ed62007-10-16 23:26:01 -07002013
2014 if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
2015 return 0;
2016 ret = __zone_reclaim(zone, gfp_mask, order);
2017 zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
2018
2019 return ret;
Andrew Morton179e9632006-03-22 00:08:18 -08002020}
Christoph Lameter9eeff232006-01-18 17:42:31 -08002021#endif