blob: be4dfe87be03eef25206710409d9ae4e44143975 [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
Rik van Rielf1a9ee72008-02-07 00:14:08 -080073 /*
74 * Pages that have (or should have) IO pending. If we run into
75 * a lot of these, we're better off waiting a little for IO to
76 * finish rather than scanning more pages in the VM.
77 */
78 int nr_io_pages;
79
Balbir Singh66e17072008-02-07 00:13:56 -080080 /* Which cgroup do we reclaim from */
81 struct mem_cgroup *mem_cgroup;
82
83 /* Pluggable isolate pages callback */
84 unsigned long (*isolate_pages)(unsigned long nr, struct list_head *dst,
85 unsigned long *scanned, int order, int mode,
86 struct zone *z, struct mem_cgroup *mem_cont,
87 int active);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
91
92#ifdef ARCH_HAS_PREFETCH
93#define prefetch_prev_lru_page(_page, _base, _field) \
94 do { \
95 if ((_page)->lru.prev != _base) { \
96 struct page *prev; \
97 \
98 prev = lru_to_page(&(_page->lru)); \
99 prefetch(&prev->_field); \
100 } \
101 } while (0)
102#else
103#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
104#endif
105
106#ifdef ARCH_HAS_PREFETCHW
107#define prefetchw_prev_lru_page(_page, _base, _field) \
108 do { \
109 if ((_page)->lru.prev != _base) { \
110 struct page *prev; \
111 \
112 prev = lru_to_page(&(_page->lru)); \
113 prefetchw(&prev->_field); \
114 } \
115 } while (0)
116#else
117#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
118#endif
119
120/*
121 * From 0 .. 100. Higher means more swappy.
122 */
123int vm_swappiness = 60;
Andrew Mortonbd1e22b2006-06-23 02:03:47 -0700124long vm_total_pages; /* The total number of pages which the VM controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126static LIST_HEAD(shrinker_list);
127static DECLARE_RWSEM(shrinker_rwsem);
128
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -0800129#ifdef CONFIG_CGROUP_MEM_CONT
130#define scan_global_lru(sc) (!(sc)->mem_cgroup)
131#else
132#define scan_global_lru(sc) (1)
133#endif
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
136 * Add a shrinker callback to be called from the vm
137 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700138void register_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700140 shrinker->nr = 0;
141 down_write(&shrinker_rwsem);
142 list_add_tail(&shrinker->list, &shrinker_list);
143 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700145EXPORT_SYMBOL(register_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/*
148 * Remove one
149 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700150void unregister_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 down_write(&shrinker_rwsem);
153 list_del(&shrinker->list);
154 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700156EXPORT_SYMBOL(unregister_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158#define SHRINK_BATCH 128
159/*
160 * Call the shrink functions to age shrinkable caches
161 *
162 * Here we assume it costs one seek to replace a lru page and that it also
163 * takes a seek to recreate a cache object. With this in mind we age equal
164 * percentages of the lru and ageable caches. This should balance the seeks
165 * generated by these structures.
166 *
Simon Arlott183ff222007-10-20 01:27:18 +0200167 * If the vm encountered mapped pages on the LRU it increase the pressure on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * slab to avoid swapping.
169 *
170 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
171 *
172 * `lru_pages' represents the number of on-LRU pages in all the zones which
173 * are eligible for the caller's allocation attempt. It is used for balancing
174 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700175 *
176 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
Andrew Morton69e05942006-03-22 00:08:19 -0800178unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
179 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800182 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 if (scanned == 0)
185 scanned = SWAP_CLUSTER_MAX;
186
187 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700188 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 list_for_each_entry(shrinker, &shrinker_list, list) {
191 unsigned long long delta;
192 unsigned long total_scan;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700193 unsigned long max_pass = (*shrinker->shrink)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800196 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 do_div(delta, lru_pages + 1);
198 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800199 if (shrinker->nr < 0) {
200 printk(KERN_ERR "%s: nr=%ld\n",
201 __FUNCTION__, shrinker->nr);
202 shrinker->nr = max_pass;
203 }
204
205 /*
206 * Avoid risking looping forever due to too large nr value:
207 * never try to free more than twice the estimate number of
208 * freeable entries.
209 */
210 if (shrinker->nr > max_pass * 2)
211 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 total_scan = shrinker->nr;
214 shrinker->nr = 0;
215
216 while (total_scan >= SHRINK_BATCH) {
217 long this_scan = SHRINK_BATCH;
218 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700219 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Rusty Russell8e1f9362007-07-17 04:03:17 -0700221 nr_before = (*shrinker->shrink)(0, gfp_mask);
222 shrink_ret = (*shrinker->shrink)(this_scan, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (shrink_ret == -1)
224 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700225 if (shrink_ret < nr_before)
226 ret += nr_before - shrink_ret;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700227 count_vm_events(SLABS_SCANNED, this_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 total_scan -= this_scan;
229
230 cond_resched();
231 }
232
233 shrinker->nr += total_scan;
234 }
235 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700236 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239/* Called without lock on whether page is mapped, so answer is unstable */
240static inline int page_mapping_inuse(struct page *page)
241{
242 struct address_space *mapping;
243
244 /* Page is in somebody's page tables. */
245 if (page_mapped(page))
246 return 1;
247
248 /* Be more reluctant to reclaim swapcache than pagecache */
249 if (PageSwapCache(page))
250 return 1;
251
252 mapping = page_mapping(page);
253 if (!mapping)
254 return 0;
255
256 /* File is mmap'd by somebody? */
257 return mapping_mapped(mapping);
258}
259
260static inline int is_page_cache_freeable(struct page *page)
261{
262 return page_count(page) - !!PagePrivate(page) == 2;
263}
264
265static int may_write_to_queue(struct backing_dev_info *bdi)
266{
Christoph Lameter930d9152006-01-08 01:00:47 -0800267 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 1;
269 if (!bdi_write_congested(bdi))
270 return 1;
271 if (bdi == current->backing_dev_info)
272 return 1;
273 return 0;
274}
275
276/*
277 * We detected a synchronous write error writing a page out. Probably
278 * -ENOSPC. We need to propagate that into the address_space for a subsequent
279 * fsync(), msync() or close().
280 *
281 * The tricky part is that after writepage we cannot touch the mapping: nothing
282 * prevents it from being freed up. But we have a ref on the page and once
283 * that page is locked, the mapping is pinned.
284 *
285 * We're allowed to run sleeping lock_page() here because we know the caller has
286 * __GFP_FS.
287 */
288static void handle_write_error(struct address_space *mapping,
289 struct page *page, int error)
290{
291 lock_page(page);
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -0700292 if (page_mapping(page) == mapping)
293 mapping_set_error(mapping, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 unlock_page(page);
295}
296
Andy Whitcroftc661b072007-08-22 14:01:26 -0700297/* Request for sync pageout. */
298enum pageout_io {
299 PAGEOUT_IO_ASYNC,
300 PAGEOUT_IO_SYNC,
301};
302
Christoph Lameter04e62a22006-06-23 02:03:38 -0700303/* possible outcome of pageout() */
304typedef enum {
305 /* failed to write page out, page is locked */
306 PAGE_KEEP,
307 /* move page to the active list, page is locked */
308 PAGE_ACTIVATE,
309 /* page has been sent to the disk successfully, page is unlocked */
310 PAGE_SUCCESS,
311 /* page is clean and locked */
312 PAGE_CLEAN,
313} pageout_t;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/*
Andrew Morton1742f192006-03-22 00:08:21 -0800316 * pageout is called by shrink_page_list() for each dirty page.
317 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700319static pageout_t pageout(struct page *page, struct address_space *mapping,
320 enum pageout_io sync_writeback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 /*
323 * If the page is dirty, only perform writeback if that write
324 * will be non-blocking. To prevent this allocation from being
325 * stalled by pagecache activity. But note that there may be
326 * stalls if we need to run get_block(). We could test
327 * PagePrivate for that.
328 *
329 * If this process is currently in generic_file_write() against
330 * this page's queue, we can perform writeback even if that
331 * will block.
332 *
333 * If the page is swapcache, write it back even if that would
334 * block, for some throttling. This happens by accident, because
335 * swap_backing_dev_info is bust: it doesn't reflect the
336 * congestion state of the swapdevs. Easy to fix, if needed.
337 * See swapfile.c:page_queue_congested().
338 */
339 if (!is_page_cache_freeable(page))
340 return PAGE_KEEP;
341 if (!mapping) {
342 /*
343 * Some data journaling orphaned pages can have
344 * page->mapping == NULL while being dirty with clean buffers.
345 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700346 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (try_to_free_buffers(page)) {
348 ClearPageDirty(page);
349 printk("%s: orphaned page\n", __FUNCTION__);
350 return PAGE_CLEAN;
351 }
352 }
353 return PAGE_KEEP;
354 }
355 if (mapping->a_ops->writepage == NULL)
356 return PAGE_ACTIVATE;
357 if (!may_write_to_queue(mapping->backing_dev_info))
358 return PAGE_KEEP;
359
360 if (clear_page_dirty_for_io(page)) {
361 int res;
362 struct writeback_control wbc = {
363 .sync_mode = WB_SYNC_NONE,
364 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700365 .range_start = 0,
366 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 .nonblocking = 1,
368 .for_reclaim = 1,
369 };
370
371 SetPageReclaim(page);
372 res = mapping->a_ops->writepage(page, &wbc);
373 if (res < 0)
374 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800375 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 ClearPageReclaim(page);
377 return PAGE_ACTIVATE;
378 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700379
380 /*
381 * Wait on writeback if requested to. This happens when
382 * direct reclaiming a large contiguous area and the
383 * first attempt to free a range of pages fails.
384 */
385 if (PageWriteback(page) && sync_writeback == PAGEOUT_IO_SYNC)
386 wait_on_page_writeback(page);
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (!PageWriteback(page)) {
389 /* synchronous write or broken a_ops? */
390 ClearPageReclaim(page);
391 }
Andrew Mortone129b5c2006-09-27 01:50:00 -0700392 inc_zone_page_state(page, NR_VMSCAN_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return PAGE_SUCCESS;
394 }
395
396 return PAGE_CLEAN;
397}
398
Andrew Mortona649fd92006-10-17 00:09:36 -0700399/*
400 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
401 * someone else has a ref on the page, abort and return 0. If it was
402 * successfully detached, return 1. Assumes the caller has a single ref on
403 * this page.
404 */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800405int remove_mapping(struct address_space *mapping, struct page *page)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800406{
Nick Piggin28e4d962006-09-25 23:31:23 -0700407 BUG_ON(!PageLocked(page));
408 BUG_ON(mapping != page_mapping(page));
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800409
410 write_lock_irq(&mapping->tree_lock);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800411 /*
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700412 * The non racy check for a busy page.
413 *
414 * Must be careful with the order of the tests. When someone has
415 * a ref to the page, it may be possible that they dirty it then
416 * drop the reference. So if PageDirty is tested before page_count
417 * here, then the following race may occur:
418 *
419 * get_user_pages(&page);
420 * [user mapping goes away]
421 * write_to(page);
422 * !PageDirty(page) [good]
423 * SetPageDirty(page);
424 * put_page(page);
425 * !page_count(page) [good, discard it]
426 *
427 * [oops, our write_to data is lost]
428 *
429 * Reversing the order of the tests ensures such a situation cannot
430 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
431 * load is not satisfied before that of page->_count.
432 *
433 * Note that if SetPageDirty is always performed via set_page_dirty,
434 * and thus under tree_lock, then this ordering is not required.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800435 */
436 if (unlikely(page_count(page) != 2))
437 goto cannot_free;
438 smp_rmb();
439 if (unlikely(PageDirty(page)))
440 goto cannot_free;
441
442 if (PageSwapCache(page)) {
443 swp_entry_t swap = { .val = page_private(page) };
444 __delete_from_swap_cache(page);
445 write_unlock_irq(&mapping->tree_lock);
446 swap_free(swap);
447 __put_page(page); /* The pagecache ref */
448 return 1;
449 }
450
451 __remove_from_page_cache(page);
452 write_unlock_irq(&mapping->tree_lock);
453 __put_page(page);
454 return 1;
455
456cannot_free:
457 write_unlock_irq(&mapping->tree_lock);
458 return 0;
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/*
Andrew Morton1742f192006-03-22 00:08:21 -0800462 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 */
Andrew Morton1742f192006-03-22 00:08:21 -0800464static unsigned long shrink_page_list(struct list_head *page_list,
Andy Whitcroftc661b072007-08-22 14:01:26 -0700465 struct scan_control *sc,
466 enum pageout_io sync_writeback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 LIST_HEAD(ret_pages);
469 struct pagevec freed_pvec;
470 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800471 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 cond_resched();
474
475 pagevec_init(&freed_pvec, 1);
476 while (!list_empty(page_list)) {
477 struct address_space *mapping;
478 struct page *page;
479 int may_enter_fs;
480 int referenced;
481
482 cond_resched();
483
484 page = lru_to_page(page_list);
485 list_del(&page->lru);
486
487 if (TestSetPageLocked(page))
488 goto keep;
489
Nick Piggin725d7042006-09-25 23:30:55 -0700490 VM_BUG_ON(PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800493
494 if (!sc->may_swap && page_mapped(page))
495 goto keep_locked;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /* Double the slab pressure for mapped and swapcache pages */
498 if (page_mapped(page) || PageSwapCache(page))
499 sc->nr_scanned++;
500
Andy Whitcroftc661b072007-08-22 14:01:26 -0700501 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
502 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
503
504 if (PageWriteback(page)) {
505 /*
506 * Synchronous reclaim is performed in two passes,
507 * first an asynchronous pass over the list to
508 * start parallel writeback, and a second synchronous
509 * pass to wait for the IO to complete. Wait here
510 * for any page for which writeback has already
511 * started.
512 */
513 if (sync_writeback == PAGEOUT_IO_SYNC && may_enter_fs)
514 wait_on_page_writeback(page);
Rik van Rielf1a9ee72008-02-07 00:14:08 -0800515 else {
516 sc->nr_io_pages++;
Andy Whitcroftc661b072007-08-22 14:01:26 -0700517 goto keep_locked;
Rik van Rielf1a9ee72008-02-07 00:14:08 -0800518 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Balbir Singhbed71612008-02-07 00:14:01 -0800521 referenced = page_referenced(page, 1, sc->mem_cgroup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* In active use or really unfreeable? Activate it. */
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700523 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER &&
524 referenced && page_mapping_inuse(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto activate_locked;
526
527#ifdef CONFIG_SWAP
528 /*
529 * Anonymous process memory has backing store?
530 * Try to allocate it some swap space here.
531 */
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800532 if (PageAnon(page) && !PageSwapCache(page))
Christoph Lameter1480a542006-01-08 01:00:53 -0800533 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#endif /* CONFIG_SWAP */
536
537 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 /*
540 * The page is mapped into the page tables of one or more
541 * processes. Try to unmap it here.
542 */
543 if (page_mapped(page) && mapping) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800544 switch (try_to_unmap(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 case SWAP_FAIL:
546 goto activate_locked;
547 case SWAP_AGAIN:
548 goto keep_locked;
549 case SWAP_SUCCESS:
550 ; /* try to free the page below */
551 }
552 }
553
554 if (PageDirty(page)) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700555 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && referenced)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto keep_locked;
Rik van Rielf1a9ee72008-02-07 00:14:08 -0800557 if (!may_enter_fs) {
558 sc->nr_io_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto keep_locked;
Rik van Rielf1a9ee72008-02-07 00:14:08 -0800560 }
Christoph Lameter52a83632006-02-01 03:05:28 -0800561 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto keep_locked;
563
564 /* Page is dirty, try to write it out here */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700565 switch (pageout(page, mapping, sync_writeback)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 case PAGE_KEEP:
567 goto keep_locked;
568 case PAGE_ACTIVATE:
569 goto activate_locked;
570 case PAGE_SUCCESS:
Rik van Rielf1a9ee72008-02-07 00:14:08 -0800571 if (PageWriteback(page) || PageDirty(page)) {
572 sc->nr_io_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 goto keep;
Rik van Rielf1a9ee72008-02-07 00:14:08 -0800574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /*
576 * A synchronous write - probably a ramdisk. Go
577 * ahead and try to reclaim the page.
578 */
579 if (TestSetPageLocked(page))
580 goto keep;
581 if (PageDirty(page) || PageWriteback(page))
582 goto keep_locked;
583 mapping = page_mapping(page);
584 case PAGE_CLEAN:
585 ; /* try to free the page below */
586 }
587 }
588
589 /*
590 * If the page has buffers, try to free the buffer mappings
591 * associated with this page. If we succeed we try to free
592 * the page as well.
593 *
594 * We do this even if the page is PageDirty().
595 * try_to_release_page() does not perform I/O, but it is
596 * possible for a page to have PageDirty set, but it is actually
597 * clean (all its buffers are clean). This happens if the
598 * buffers were written out directly, with submit_bh(). ext3
599 * will do this, as well as the blockdev mapping.
600 * try_to_release_page() will discover that cleanness and will
601 * drop the buffers and mark the page clean - it can be freed.
602 *
603 * Rarely, pages can have buffers and no ->mapping. These are
604 * the pages which were not successfully invalidated in
605 * truncate_complete_page(). We try to drop those buffers here
606 * and if that worked, and the page is no longer mapped into
607 * process address space (page_count == 1) it can be freed.
608 * Otherwise, leave the page on the LRU so it is swappable.
609 */
610 if (PagePrivate(page)) {
611 if (!try_to_release_page(page, sc->gfp_mask))
612 goto activate_locked;
613 if (!mapping && page_count(page) == 1)
614 goto free_it;
615 }
616
Nick Piggin28e4d962006-09-25 23:31:23 -0700617 if (!mapping || !remove_mapping(mapping, page))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800618 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620free_it:
621 unlock_page(page);
Andrew Morton05ff5132006-03-22 00:08:20 -0800622 nr_reclaimed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!pagevec_add(&freed_pvec, page))
624 __pagevec_release_nonlru(&freed_pvec);
625 continue;
626
627activate_locked:
628 SetPageActive(page);
629 pgactivate++;
630keep_locked:
631 unlock_page(page);
632keep:
633 list_add(&page->lru, &ret_pages);
Nick Piggin725d7042006-09-25 23:30:55 -0700634 VM_BUG_ON(PageLRU(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 list_splice(&ret_pages, page_list);
637 if (pagevec_count(&freed_pvec))
638 __pagevec_release_nonlru(&freed_pvec);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700639 count_vm_events(PGACTIVATE, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800640 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700643/* LRU Isolation modes. */
644#define ISOLATE_INACTIVE 0 /* Isolate inactive pages. */
645#define ISOLATE_ACTIVE 1 /* Isolate active pages. */
646#define ISOLATE_BOTH 2 /* Isolate both active and inactive pages. */
647
648/*
649 * Attempt to remove the specified page from its LRU. Only take this page
650 * if it is of the appropriate PageActive status. Pages which are being
651 * freed elsewhere are also ignored.
652 *
653 * page: page to consider
654 * mode: one of the LRU isolation modes defined above
655 *
656 * returns 0 on success, -ve errno on failure.
657 */
Balbir Singh66e17072008-02-07 00:13:56 -0800658int __isolate_lru_page(struct page *page, int mode)
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700659{
660 int ret = -EINVAL;
661
662 /* Only take pages on the LRU. */
663 if (!PageLRU(page))
664 return ret;
665
666 /*
667 * When checking the active state, we need to be sure we are
668 * dealing with comparible boolean values. Take the logical not
669 * of each.
670 */
671 if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode))
672 return ret;
673
674 ret = -EBUSY;
675 if (likely(get_page_unless_zero(page))) {
676 /*
677 * Be careful not to clear PageLRU until after we're
678 * sure the page is not being freed elsewhere -- the
679 * page release code relies on it.
680 */
681 ClearPageLRU(page);
682 ret = 0;
683 }
684
685 return ret;
686}
687
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800688/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * zone->lru_lock is heavily contended. Some of the functions that
690 * shrink the lists perform better by taking out a batch of pages
691 * and working on them outside the LRU lock.
692 *
693 * For pagecache intensive workloads, this function is the hottest
694 * spot in the kernel (apart from copy_*_user functions).
695 *
696 * Appropriate locks must be held before calling this function.
697 *
698 * @nr_to_scan: The number of pages to look through on the list.
699 * @src: The LRU list to pull pages off.
700 * @dst: The temp list to put pages on to.
701 * @scanned: The number of pages that were scanned.
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700702 * @order: The caller's attempted allocation order
703 * @mode: One of the LRU isolation modes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 *
705 * returns how many pages were moved onto *@dst.
706 */
Andrew Morton69e05942006-03-22 00:08:19 -0800707static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
708 struct list_head *src, struct list_head *dst,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700709 unsigned long *scanned, int order, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Andrew Morton69e05942006-03-22 00:08:19 -0800711 unsigned long nr_taken = 0;
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800712 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800714 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700715 struct page *page;
716 unsigned long pfn;
717 unsigned long end_pfn;
718 unsigned long page_pfn;
719 int zone_id;
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 page = lru_to_page(src);
722 prefetchw_prev_lru_page(page, src, flags);
723
Nick Piggin725d7042006-09-25 23:30:55 -0700724 VM_BUG_ON(!PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800725
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700726 switch (__isolate_lru_page(page, mode)) {
727 case 0:
728 list_move(&page->lru, dst);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800729 nr_taken++;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700730 break;
Nick Piggin46453a62006-03-22 00:07:58 -0800731
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700732 case -EBUSY:
733 /* else it is being freed elsewhere */
734 list_move(&page->lru, src);
735 continue;
736
737 default:
738 BUG();
739 }
740
741 if (!order)
742 continue;
743
744 /*
745 * Attempt to take all pages in the order aligned region
746 * surrounding the tag page. Only take those pages of
747 * the same active state as that tag page. We may safely
748 * round the target page pfn down to the requested order
749 * as the mem_map is guarenteed valid out to MAX_ORDER,
750 * where that page is in a different zone we will detect
751 * it from its zone id and abort this block scan.
752 */
753 zone_id = page_zone_id(page);
754 page_pfn = page_to_pfn(page);
755 pfn = page_pfn & ~((1 << order) - 1);
756 end_pfn = pfn + (1 << order);
757 for (; pfn < end_pfn; pfn++) {
758 struct page *cursor_page;
759
760 /* The target page is in the block, ignore it. */
761 if (unlikely(pfn == page_pfn))
762 continue;
763
764 /* Avoid holes within the zone. */
765 if (unlikely(!pfn_valid_within(pfn)))
766 break;
767
768 cursor_page = pfn_to_page(pfn);
769 /* Check that we have not crossed a zone boundary. */
770 if (unlikely(page_zone_id(cursor_page) != zone_id))
771 continue;
772 switch (__isolate_lru_page(cursor_page, mode)) {
773 case 0:
774 list_move(&cursor_page->lru, dst);
775 nr_taken++;
776 scan++;
777 break;
778
779 case -EBUSY:
780 /* else it is being freed elsewhere */
781 list_move(&cursor_page->lru, src);
782 default:
783 break;
784 }
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
788 *scanned = scan;
789 return nr_taken;
790}
791
Balbir Singh66e17072008-02-07 00:13:56 -0800792static unsigned long isolate_pages_global(unsigned long nr,
793 struct list_head *dst,
794 unsigned long *scanned, int order,
795 int mode, struct zone *z,
796 struct mem_cgroup *mem_cont,
797 int active)
798{
799 if (active)
800 return isolate_lru_pages(nr, &z->active_list, dst,
801 scanned, order, mode);
802 else
803 return isolate_lru_pages(nr, &z->inactive_list, dst,
804 scanned, order, mode);
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807/*
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700808 * clear_active_flags() is a helper for shrink_active_list(), clearing
809 * any active bits from the pages in the list.
810 */
811static unsigned long clear_active_flags(struct list_head *page_list)
812{
813 int nr_active = 0;
814 struct page *page;
815
816 list_for_each_entry(page, page_list, lru)
817 if (PageActive(page)) {
818 ClearPageActive(page);
819 nr_active++;
820 }
821
822 return nr_active;
823}
824
825/*
Andrew Morton1742f192006-03-22 00:08:21 -0800826 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
827 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 */
Andrew Morton1742f192006-03-22 00:08:21 -0800829static unsigned long shrink_inactive_list(unsigned long max_scan,
830 struct zone *zone, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 LIST_HEAD(page_list);
833 struct pagevec pvec;
Andrew Morton69e05942006-03-22 00:08:19 -0800834 unsigned long nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800835 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 pagevec_init(&pvec, 1);
838
839 lru_add_drain();
840 spin_lock_irq(&zone->lru_lock);
Andrew Morton69e05942006-03-22 00:08:19 -0800841 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -0800843 unsigned long nr_taken;
844 unsigned long nr_scan;
845 unsigned long nr_freed;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700846 unsigned long nr_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Balbir Singh66e17072008-02-07 00:13:56 -0800848 nr_taken = sc->isolate_pages(sc->swap_cluster_max,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700849 &page_list, &nr_scan, sc->order,
850 (sc->order > PAGE_ALLOC_COSTLY_ORDER)?
Balbir Singh66e17072008-02-07 00:13:56 -0800851 ISOLATE_BOTH : ISOLATE_INACTIVE,
852 zone, sc->mem_cgroup, 0);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700853 nr_active = clear_active_flags(&page_list);
Andy Whitcrofte9187bd2007-08-22 14:01:25 -0700854 __count_vm_events(PGDEACTIVATE, nr_active);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700855
856 __mod_zone_page_state(zone, NR_ACTIVE, -nr_active);
857 __mod_zone_page_state(zone, NR_INACTIVE,
858 -(nr_taken - nr_active));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 zone->pages_scanned += nr_scan;
860 spin_unlock_irq(&zone->lru_lock);
861
Andrew Morton69e05942006-03-22 00:08:19 -0800862 nr_scanned += nr_scan;
Andy Whitcroftc661b072007-08-22 14:01:26 -0700863 nr_freed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC);
864
865 /*
866 * If we are direct reclaiming for contiguous pages and we do
867 * not reclaim everything in the list, try again and wait
868 * for IO to complete. This will stall high-order allocations
869 * but that should be acceptable to the caller
870 */
871 if (nr_freed < nr_taken && !current_is_kswapd() &&
872 sc->order > PAGE_ALLOC_COSTLY_ORDER) {
873 congestion_wait(WRITE, HZ/10);
874
875 /*
876 * The attempt at page out may have made some
877 * of the pages active, mark them inactive again.
878 */
879 nr_active = clear_active_flags(&page_list);
880 count_vm_events(PGDEACTIVATE, nr_active);
881
882 nr_freed += shrink_page_list(&page_list, sc,
883 PAGEOUT_IO_SYNC);
884 }
885
Andrew Morton05ff5132006-03-22 00:08:20 -0800886 nr_reclaimed += nr_freed;
Nick Piggina74609f2006-01-06 00:11:20 -0800887 local_irq_disable();
888 if (current_is_kswapd()) {
Christoph Lameterf8891e52006-06-30 01:55:45 -0700889 __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
890 __count_vm_events(KSWAPD_STEAL, nr_freed);
Nick Piggina74609f2006-01-06 00:11:20 -0800891 } else
Christoph Lameterf8891e52006-06-30 01:55:45 -0700892 __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
Shantanu Goel918d3f92006-12-29 16:48:59 -0800893 __count_zone_vm_events(PGSTEAL, zone, nr_freed);
Nick Piggina74609f2006-01-06 00:11:20 -0800894
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800895 if (nr_taken == 0)
896 goto done;
897
Nick Piggina74609f2006-01-06 00:11:20 -0800898 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /*
900 * Put back any unfreeable pages.
901 */
902 while (!list_empty(&page_list)) {
903 page = lru_to_page(&page_list);
Nick Piggin725d7042006-09-25 23:30:55 -0700904 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800905 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 list_del(&page->lru);
907 if (PageActive(page))
908 add_page_to_active_list(zone, page);
909 else
910 add_page_to_inactive_list(zone, page);
911 if (!pagevec_add(&pvec, page)) {
912 spin_unlock_irq(&zone->lru_lock);
913 __pagevec_release(&pvec);
914 spin_lock_irq(&zone->lru_lock);
915 }
916 }
Andrew Morton69e05942006-03-22 00:08:19 -0800917 } while (nr_scanned < max_scan);
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800918 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919done:
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800920 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 pagevec_release(&pvec);
Andrew Morton05ff5132006-03-22 00:08:20 -0800922 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Martin Bligh3bb1a8522006-10-28 10:38:24 -0700925/*
926 * We are about to scan this zone at a certain priority level. If that priority
927 * level is smaller (ie: more urgent) than the previous priority, then note
928 * that priority level within the zone. This is done so that when the next
929 * process comes in to scan this zone, it will immediately start out at this
930 * priority level rather than having to build up its own scanning priority.
931 * Here, this priority affects only the reclaim-mapped threshold.
932 */
933static inline void note_zone_scanning_priority(struct zone *zone, int priority)
934{
935 if (priority < zone->prev_priority)
936 zone->prev_priority = priority;
937}
938
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700939static inline int zone_is_near_oom(struct zone *zone)
940{
Christoph Lameterc8785382007-02-10 01:43:01 -0800941 return zone->pages_scanned >= (zone_page_state(zone, NR_ACTIVE)
942 + zone_page_state(zone, NR_INACTIVE))*3;
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945/*
946 * This moves pages from the active list to the inactive list.
947 *
948 * We move them the other way if the page is referenced by one or more
949 * processes, from rmap.
950 *
951 * If the pages are mostly unmapped, the processing is fast and it is
952 * appropriate to hold zone->lru_lock across the whole operation. But if
953 * the pages are mapped, the processing is slow (page_referenced()) so we
954 * should drop zone->lru_lock around each page. It's impossible to balance
955 * this, so instead we remove the pages from the LRU while processing them.
956 * It is safe to rely on PG_active against the non-LRU pages in here because
957 * nobody will play with that bit on a non-LRU page.
958 *
959 * The downside is that we have to touch page->_count against each page.
960 * But we had to alter page->flags anyway.
961 */
Andrew Morton1742f192006-03-22 00:08:21 -0800962static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
Martin Blighbbdb3962006-10-28 10:38:25 -0700963 struct scan_control *sc, int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Andrew Morton69e05942006-03-22 00:08:19 -0800965 unsigned long pgmoved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int pgdeactivate = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800967 unsigned long pgscanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 LIST_HEAD(l_hold); /* The pages which were snipped off */
969 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
970 LIST_HEAD(l_active); /* Pages to go onto the active_list */
971 struct page *page;
972 struct pagevec pvec;
973 int reclaim_mapped = 0;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800974
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800975 if (sc->may_swap) {
Christoph Lameter2903fb12006-02-11 17:55:55 -0800976 long mapped_ratio;
977 long distress;
978 long swap_tendency;
Andrea Arcangeli4106f832007-10-16 01:25:42 -0700979 long imbalance;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800980
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700981 if (zone_is_near_oom(zone))
982 goto force_reclaim_mapped;
983
Christoph Lameter2903fb12006-02-11 17:55:55 -0800984 /*
985 * `distress' is a measure of how much trouble we're having
986 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
987 */
Martin Blighbbdb3962006-10-28 10:38:25 -0700988 distress = 100 >> min(zone->prev_priority, priority);
Christoph Lameter2903fb12006-02-11 17:55:55 -0800989
990 /*
991 * The point of this algorithm is to decide when to start
992 * reclaiming mapped memory instead of just pagecache. Work out
993 * how much memory
994 * is mapped.
995 */
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700996 mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
997 global_page_state(NR_ANON_PAGES)) * 100) /
Christoph Lameterbf02cf42006-06-30 01:55:36 -0700998 vm_total_pages;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800999
1000 /*
1001 * Now decide how much we really want to unmap some pages. The
1002 * mapped ratio is downgraded - just because there's a lot of
1003 * mapped memory doesn't necessarily mean that page reclaim
1004 * isn't succeeding.
1005 *
1006 * The distress ratio is important - we don't want to start
1007 * going oom.
1008 *
1009 * A 100% value of vm_swappiness overrides this algorithm
1010 * altogether.
1011 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001012 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
Christoph Lameter2903fb12006-02-11 17:55:55 -08001013
1014 /*
Andrea Arcangeli4106f832007-10-16 01:25:42 -07001015 * If there's huge imbalance between active and inactive
1016 * (think active 100 times larger than inactive) we should
1017 * become more permissive, or the system will take too much
1018 * cpu before it start swapping during memory pressure.
1019 * Distress is about avoiding early-oom, this is about
1020 * making swappiness graceful despite setting it to low
1021 * values.
1022 *
1023 * Avoid div by zero with nr_inactive+1, and max resulting
1024 * value is vm_total_pages.
1025 */
1026 imbalance = zone_page_state(zone, NR_ACTIVE);
1027 imbalance /= zone_page_state(zone, NR_INACTIVE) + 1;
1028
1029 /*
1030 * Reduce the effect of imbalance if swappiness is low,
1031 * this means for a swappiness very low, the imbalance
1032 * must be much higher than 100 for this logic to make
1033 * the difference.
1034 *
1035 * Max temporary value is vm_total_pages*100.
1036 */
1037 imbalance *= (vm_swappiness + 1);
1038 imbalance /= 100;
1039
1040 /*
1041 * If not much of the ram is mapped, makes the imbalance
1042 * less relevant, it's high priority we refill the inactive
1043 * list with mapped pages only in presence of high ratio of
1044 * mapped pages.
1045 *
1046 * Max temporary value is vm_total_pages*100.
1047 */
1048 imbalance *= mapped_ratio;
1049 imbalance /= 100;
1050
1051 /* apply imbalance feedback to swap_tendency */
1052 swap_tendency += imbalance;
1053
1054 /*
Christoph Lameter2903fb12006-02-11 17:55:55 -08001055 * Now use this metric to decide whether to start moving mapped
1056 * memory onto the inactive list.
1057 */
1058 if (swap_tendency >= 100)
Nick Piggin4ff1ffb2006-09-25 23:31:28 -07001059force_reclaim_mapped:
Christoph Lameter2903fb12006-02-11 17:55:55 -08001060 reclaim_mapped = 1;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 lru_add_drain();
1064 spin_lock_irq(&zone->lru_lock);
Balbir Singh66e17072008-02-07 00:13:56 -08001065 pgmoved = sc->isolate_pages(nr_pages, &l_hold, &pgscanned, sc->order,
1066 ISOLATE_ACTIVE, zone,
1067 sc->mem_cgroup, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 zone->pages_scanned += pgscanned;
Christoph Lameterc8785382007-02-10 01:43:01 -08001069 __mod_zone_page_state(zone, NR_ACTIVE, -pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 spin_unlock_irq(&zone->lru_lock);
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 while (!list_empty(&l_hold)) {
1073 cond_resched();
1074 page = lru_to_page(&l_hold);
1075 list_del(&page->lru);
1076 if (page_mapped(page)) {
1077 if (!reclaim_mapped ||
1078 (total_swap_pages == 0 && PageAnon(page)) ||
Balbir Singhbed71612008-02-07 00:14:01 -08001079 page_referenced(page, 0, sc->mem_cgroup)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 list_add(&page->lru, &l_active);
1081 continue;
1082 }
1083 }
1084 list_add(&page->lru, &l_inactive);
1085 }
1086
1087 pagevec_init(&pvec, 1);
1088 pgmoved = 0;
1089 spin_lock_irq(&zone->lru_lock);
1090 while (!list_empty(&l_inactive)) {
1091 page = lru_to_page(&l_inactive);
1092 prefetchw_prev_lru_page(page, &l_inactive, flags);
Nick Piggin725d7042006-09-25 23:30:55 -07001093 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -08001094 SetPageLRU(page);
Nick Piggin725d7042006-09-25 23:30:55 -07001095 VM_BUG_ON(!PageActive(page));
Nick Piggin4c84cac2006-03-22 00:08:00 -08001096 ClearPageActive(page);
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 list_move(&page->lru, &zone->inactive_list);
Balbir Singh66e17072008-02-07 00:13:56 -08001099 mem_cgroup_move_lists(page_get_page_cgroup(page), false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 pgmoved++;
1101 if (!pagevec_add(&pvec, page)) {
Christoph Lameterc8785382007-02-10 01:43:01 -08001102 __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 spin_unlock_irq(&zone->lru_lock);
1104 pgdeactivate += pgmoved;
1105 pgmoved = 0;
1106 if (buffer_heads_over_limit)
1107 pagevec_strip(&pvec);
1108 __pagevec_release(&pvec);
1109 spin_lock_irq(&zone->lru_lock);
1110 }
1111 }
Christoph Lameterc8785382007-02-10 01:43:01 -08001112 __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 pgdeactivate += pgmoved;
1114 if (buffer_heads_over_limit) {
1115 spin_unlock_irq(&zone->lru_lock);
1116 pagevec_strip(&pvec);
1117 spin_lock_irq(&zone->lru_lock);
1118 }
1119
1120 pgmoved = 0;
1121 while (!list_empty(&l_active)) {
1122 page = lru_to_page(&l_active);
1123 prefetchw_prev_lru_page(page, &l_active, flags);
Nick Piggin725d7042006-09-25 23:30:55 -07001124 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -08001125 SetPageLRU(page);
Nick Piggin725d7042006-09-25 23:30:55 -07001126 VM_BUG_ON(!PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 list_move(&page->lru, &zone->active_list);
Balbir Singh66e17072008-02-07 00:13:56 -08001128 mem_cgroup_move_lists(page_get_page_cgroup(page), true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 pgmoved++;
1130 if (!pagevec_add(&pvec, page)) {
Christoph Lameterc8785382007-02-10 01:43:01 -08001131 __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 pgmoved = 0;
1133 spin_unlock_irq(&zone->lru_lock);
1134 __pagevec_release(&pvec);
1135 spin_lock_irq(&zone->lru_lock);
1136 }
1137 }
Christoph Lameterc8785382007-02-10 01:43:01 -08001138 __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Christoph Lameterf8891e52006-06-30 01:55:45 -07001140 __count_zone_vm_events(PGREFILL, zone, pgscanned);
1141 __count_vm_events(PGDEACTIVATE, pgdeactivate);
1142 spin_unlock_irq(&zone->lru_lock);
Nick Piggina74609f2006-01-06 00:11:20 -08001143
1144 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
1147/*
1148 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1149 */
Andrew Morton05ff5132006-03-22 00:08:20 -08001150static unsigned long shrink_zone(int priority, struct zone *zone,
1151 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 unsigned long nr_active;
1154 unsigned long nr_inactive;
Christoph Lameter86959492006-03-22 00:08:18 -08001155 unsigned long nr_to_scan;
Andrew Morton05ff5132006-03-22 00:08:20 -08001156 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 /*
1159 * Add one to `nr_to_scan' just to make sure that the kernel will
1160 * slowly sift through the active list.
1161 */
Christoph Lameterc8785382007-02-10 01:43:01 -08001162 zone->nr_scan_active +=
1163 (zone_page_state(zone, NR_ACTIVE) >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 nr_active = zone->nr_scan_active;
1165 if (nr_active >= sc->swap_cluster_max)
1166 zone->nr_scan_active = 0;
1167 else
1168 nr_active = 0;
1169
Christoph Lameterc8785382007-02-10 01:43:01 -08001170 zone->nr_scan_inactive +=
1171 (zone_page_state(zone, NR_INACTIVE) >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 nr_inactive = zone->nr_scan_inactive;
1173 if (nr_inactive >= sc->swap_cluster_max)
1174 zone->nr_scan_inactive = 0;
1175 else
1176 nr_inactive = 0;
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 while (nr_active || nr_inactive) {
1179 if (nr_active) {
Christoph Lameter86959492006-03-22 00:08:18 -08001180 nr_to_scan = min(nr_active,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -08001182 nr_active -= nr_to_scan;
Martin Blighbbdb3962006-10-28 10:38:25 -07001183 shrink_active_list(nr_to_scan, zone, sc, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185
1186 if (nr_inactive) {
Christoph Lameter86959492006-03-22 00:08:18 -08001187 nr_to_scan = min(nr_inactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -08001189 nr_inactive -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -08001190 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
1191 sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193 }
1194
Andrew Morton232ea4d2007-02-28 20:13:21 -08001195 throttle_vm_writeout(sc->gfp_mask);
Andrew Morton05ff5132006-03-22 00:08:20 -08001196 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199/*
1200 * This is the direct reclaim path, for page-allocating processes. We only
1201 * try to reclaim pages from zones which will satisfy the caller's allocation
1202 * request.
1203 *
1204 * We reclaim from a zone even if that zone is over pages_high. Because:
1205 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1206 * allocation or
1207 * b) The zones may be over pages_high but they must go *over* pages_high to
1208 * satisfy the `incremental min' zone defense algorithm.
1209 *
1210 * Returns the number of reclaimed pages.
1211 *
1212 * If a zone is deemed to be full of pinned pages then just give it a light
1213 * scan then give up on it.
1214 */
Andrew Morton1742f192006-03-22 00:08:21 -08001215static unsigned long shrink_zones(int priority, struct zone **zones,
Andrew Morton05ff5132006-03-22 00:08:20 -08001216 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Andrew Morton05ff5132006-03-22 00:08:20 -08001218 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 int i;
1220
Nick Piggin408d8542006-09-25 23:31:27 -07001221 sc->all_unreclaimable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 for (i = 0; zones[i] != NULL; i++) {
1223 struct zone *zone = zones[i];
1224
Con Kolivasf3fe6512006-01-06 00:11:15 -08001225 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 continue;
1227
Paul Jackson02a0e532006-12-13 00:34:25 -08001228 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 continue;
1230
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001231 note_zone_scanning_priority(zone, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
David Rientjese815af92007-10-16 23:25:54 -07001233 if (zone_is_all_unreclaimable(zone) && priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 continue; /* Let kswapd poll it */
1235
Nick Piggin408d8542006-09-25 23:31:27 -07001236 sc->all_unreclaimable = 0;
1237
Andrew Morton05ff5132006-03-22 00:08:20 -08001238 nr_reclaimed += shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Andrew Morton05ff5132006-03-22 00:08:20 -08001240 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243/*
1244 * This is the main entry point to direct page reclaim.
1245 *
1246 * If a full scan of the inactive list fails to free enough memory then we
1247 * are "out of memory" and something needs to be killed.
1248 *
1249 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1250 * high - the zone may be full of dirty or under-writeback pages, which this
1251 * caller can't do much about. We kick pdflush and take explicit naps in the
1252 * hope that some of these pages can be written. But if the allocating task
1253 * holds filesystem locks which prevent writeout this might not work, and the
1254 * allocation attempt will fail.
1255 */
Balbir Singh66e17072008-02-07 00:13:56 -08001256static unsigned long do_try_to_free_pages(struct zone **zones, gfp_t gfp_mask,
1257 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 int priority;
1260 int ret = 0;
Andrew Morton69e05942006-03-22 00:08:19 -08001261 unsigned long total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001262 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 struct reclaim_state *reclaim_state = current->reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 unsigned long lru_pages = 0;
1265 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Christoph Lameterf8891e52006-06-30 01:55:45 -07001267 count_vm_event(ALLOCSTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 for (i = 0; zones[i] != NULL; i++) {
1270 struct zone *zone = zones[i];
1271
Paul Jackson02a0e532006-12-13 00:34:25 -08001272 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 continue;
1274
Christoph Lameterc8785382007-02-10 01:43:01 -08001275 lru_pages += zone_page_state(zone, NR_ACTIVE)
1276 + zone_page_state(zone, NR_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
1279 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
Balbir Singh66e17072008-02-07 00:13:56 -08001280 sc->nr_scanned = 0;
Rik van Rielf1a9ee72008-02-07 00:14:08 -08001281 sc->nr_io_pages = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001282 if (!priority)
1283 disable_swap_token();
Balbir Singh66e17072008-02-07 00:13:56 -08001284 nr_reclaimed += shrink_zones(priority, zones, sc);
1285 /*
1286 * Don't shrink slabs when reclaiming memory from
1287 * over limit cgroups
1288 */
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -08001289 if (scan_global_lru(sc)) {
Balbir Singh66e17072008-02-07 00:13:56 -08001290 shrink_slab(sc->nr_scanned, gfp_mask, lru_pages);
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -08001291 if (reclaim_state) {
1292 nr_reclaimed += reclaim_state->reclaimed_slab;
1293 reclaim_state->reclaimed_slab = 0;
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Balbir Singh66e17072008-02-07 00:13:56 -08001296 total_scanned += sc->nr_scanned;
1297 if (nr_reclaimed >= sc->swap_cluster_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 ret = 1;
1299 goto out;
1300 }
1301
1302 /*
1303 * Try to write back as many pages as we just scanned. This
1304 * tends to cause slow streaming writers to write data to the
1305 * disk smoothly, at the dirtying rate, which is nice. But
1306 * that's undesirable in laptop mode, where we *want* lumpy
1307 * writeout. So in laptop mode, write out the whole world.
1308 */
Balbir Singh66e17072008-02-07 00:13:56 -08001309 if (total_scanned > sc->swap_cluster_max +
1310 sc->swap_cluster_max / 2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001311 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Balbir Singh66e17072008-02-07 00:13:56 -08001312 sc->may_writepage = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
1315 /* Take a nap, wait for some writeback to complete */
Rik van Rielf1a9ee72008-02-07 00:14:08 -08001316 if (sc->nr_scanned && priority < DEF_PRIORITY - 2 &&
1317 sc->nr_io_pages > sc->swap_cluster_max)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001318 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
Nick Piggin408d8542006-09-25 23:31:27 -07001320 /* top priority shrink_caches still had more to do? don't OOM, then */
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -08001321 if (!sc->all_unreclaimable && scan_global_lru(sc))
Nick Piggin408d8542006-09-25 23:31:27 -07001322 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323out:
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001324 /*
1325 * Now that we've scanned all the zones at this priority level, note
1326 * that level within the zone so that the next thread which performs
1327 * scanning of this zone will immediately start out at this priority
1328 * level. This affects only the decision whether or not to bring
1329 * mapped pages onto the inactive list.
1330 */
1331 if (priority < 0)
1332 priority = 0;
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001333 for (i = 0; zones[i] != NULL; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 struct zone *zone = zones[i];
1335
Paul Jackson02a0e532006-12-13 00:34:25 -08001336 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 continue;
1338
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001339 zone->prev_priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 return ret;
1342}
1343
Balbir Singh66e17072008-02-07 00:13:56 -08001344unsigned long try_to_free_pages(struct zone **zones, int order, gfp_t gfp_mask)
1345{
1346 struct scan_control sc = {
1347 .gfp_mask = gfp_mask,
1348 .may_writepage = !laptop_mode,
1349 .swap_cluster_max = SWAP_CLUSTER_MAX,
1350 .may_swap = 1,
1351 .swappiness = vm_swappiness,
1352 .order = order,
1353 .mem_cgroup = NULL,
1354 .isolate_pages = isolate_pages_global,
1355 };
1356
1357 return do_try_to_free_pages(zones, gfp_mask, &sc);
1358}
1359
1360#ifdef CONFIG_CGROUP_MEM_CONT
1361
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001362unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
1363 gfp_t gfp_mask)
Balbir Singh66e17072008-02-07 00:13:56 -08001364{
1365 struct scan_control sc = {
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001366 .gfp_mask = gfp_mask,
Balbir Singh66e17072008-02-07 00:13:56 -08001367 .may_writepage = !laptop_mode,
1368 .may_swap = 1,
1369 .swap_cluster_max = SWAP_CLUSTER_MAX,
1370 .swappiness = vm_swappiness,
1371 .order = 0,
1372 .mem_cgroup = mem_cont,
1373 .isolate_pages = mem_cgroup_isolate_pages,
1374 };
Balbir Singh66e17072008-02-07 00:13:56 -08001375 struct zone **zones;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001376 int target_zone = gfp_zone(GFP_HIGHUSER_MOVABLE);
Balbir Singh66e17072008-02-07 00:13:56 -08001377
KAMEZAWA Hiroyuki417eead2008-02-07 00:14:14 -08001378 zones = NODE_DATA(numa_node_id())->node_zonelists[target_zone].zones;
1379 if (do_try_to_free_pages(zones, sc.gfp_mask, &sc))
1380 return 1;
Balbir Singh66e17072008-02-07 00:13:56 -08001381 return 0;
1382}
1383#endif
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385/*
1386 * For kswapd, balance_pgdat() will work across all this node's zones until
1387 * they are all at pages_high.
1388 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 * Returns the number of pages which were actually freed.
1390 *
1391 * There is special handling here for zones which are full of pinned pages.
1392 * This can happen if the pages are all mlocked, or if they are all used by
1393 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1394 * What we do is to detect the case where all pages in the zone have been
1395 * scanned twice and there has been zero successful reclaim. Mark the zone as
1396 * dead and from now on, only perform a short scan. Basically we're polling
1397 * the zone for when the problem goes away.
1398 *
1399 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1400 * zones which have free_pages > pages_high, but once a zone is found to have
1401 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1402 * of the number of free pages in the lower zones. This interoperates with
1403 * the page allocator fallback scheme to ensure that aging of pages is balanced
1404 * across the zones.
1405 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001406static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int all_zones_ok;
1409 int priority;
1410 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08001411 unsigned long total_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001412 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08001414 struct scan_control sc = {
1415 .gfp_mask = GFP_KERNEL,
1416 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001417 .swap_cluster_max = SWAP_CLUSTER_MAX,
1418 .swappiness = vm_swappiness,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001419 .order = order,
Balbir Singh66e17072008-02-07 00:13:56 -08001420 .mem_cgroup = NULL,
1421 .isolate_pages = isolate_pages_global,
Andrew Morton179e9632006-03-22 00:08:18 -08001422 };
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001423 /*
1424 * temp_priority is used to remember the scanning priority at which
1425 * this zone was successfully refilled to free_pages == pages_high.
1426 */
1427 int temp_priority[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429loop_again:
1430 total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001431 nr_reclaimed = 0;
Christoph Lameterc0bbbc72006-06-11 15:22:26 -07001432 sc.may_writepage = !laptop_mode;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001433 count_vm_event(PAGEOUTRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001435 for (i = 0; i < pgdat->nr_zones; i++)
1436 temp_priority[i] = DEF_PRIORITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1439 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1440 unsigned long lru_pages = 0;
1441
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001442 /* The swap token gets in the way of swapout... */
1443 if (!priority)
1444 disable_swap_token();
1445
Rik van Rielf1a9ee72008-02-07 00:14:08 -08001446 sc.nr_io_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 all_zones_ok = 1;
1448
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001449 /*
1450 * Scan in the highmem->dma direction for the highest
1451 * zone which needs scanning
1452 */
1453 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1454 struct zone *zone = pgdat->node_zones + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001456 if (!populated_zone(zone))
1457 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
David Rientjese815af92007-10-16 23:25:54 -07001459 if (zone_is_all_unreclaimable(zone) &&
1460 priority != DEF_PRIORITY)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001461 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001463 if (!zone_watermark_ok(zone, order, zone->pages_high,
1464 0, 0)) {
1465 end_zone = i;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08001466 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
Andrew Mortone1dbeda2006-12-06 20:32:01 -08001469 if (i < 0)
1470 goto out;
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 for (i = 0; i <= end_zone; i++) {
1473 struct zone *zone = pgdat->node_zones + i;
1474
Christoph Lameterc8785382007-02-10 01:43:01 -08001475 lru_pages += zone_page_state(zone, NR_ACTIVE)
1476 + zone_page_state(zone, NR_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478
1479 /*
1480 * Now scan the zone in the dma->highmem direction, stopping
1481 * at the last zone which needs scanning.
1482 *
1483 * We do this because the page allocator works in the opposite
1484 * direction. This prevents the page allocator from allocating
1485 * pages behind kswapd's direction of progress, which would
1486 * cause too much scanning of the lower zones.
1487 */
1488 for (i = 0; i <= end_zone; i++) {
1489 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001490 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Con Kolivasf3fe6512006-01-06 00:11:15 -08001492 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 continue;
1494
David Rientjese815af92007-10-16 23:25:54 -07001495 if (zone_is_all_unreclaimable(zone) &&
1496 priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 continue;
1498
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001499 if (!zone_watermark_ok(zone, order, zone->pages_high,
1500 end_zone, 0))
1501 all_zones_ok = 0;
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001502 temp_priority[i] = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 sc.nr_scanned = 0;
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001504 note_zone_scanning_priority(zone, priority);
Rik van Riel32a43302007-10-16 01:24:50 -07001505 /*
1506 * We put equal pressure on every zone, unless one
1507 * zone has way too many pages free already.
1508 */
1509 if (!zone_watermark_ok(zone, order, 8*zone->pages_high,
1510 end_zone, 0))
1511 nr_reclaimed += shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001513 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1514 lru_pages);
Andrew Morton05ff5132006-03-22 00:08:20 -08001515 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 total_scanned += sc.nr_scanned;
David Rientjese815af92007-10-16 23:25:54 -07001517 if (zone_is_all_unreclaimable(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001519 if (nr_slab == 0 && zone->pages_scanned >=
Christoph Lameterc8785382007-02-10 01:43:01 -08001520 (zone_page_state(zone, NR_ACTIVE)
1521 + zone_page_state(zone, NR_INACTIVE)) * 6)
David Rientjese815af92007-10-16 23:25:54 -07001522 zone_set_flag(zone,
1523 ZONE_ALL_UNRECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 /*
1525 * If we've done a decent amount of scanning and
1526 * the reclaim ratio is low, start doing writepage
1527 * even in laptop mode
1528 */
1529 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Andrew Morton05ff5132006-03-22 00:08:20 -08001530 total_scanned > nr_reclaimed + nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 sc.may_writepage = 1;
1532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (all_zones_ok)
1534 break; /* kswapd: all done */
1535 /*
1536 * OK, kswapd is getting into trouble. Take a nap, then take
1537 * another pass across the zones.
1538 */
Rik van Rielf1a9ee72008-02-07 00:14:08 -08001539 if (total_scanned && priority < DEF_PRIORITY - 2 &&
1540 sc.nr_io_pages > sc.swap_cluster_max)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001541 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 /*
1544 * We do this so kswapd doesn't build up large priorities for
1545 * example when it is freeing in parallel with allocators. It
1546 * matches the direct reclaim path behaviour in terms of impact
1547 * on zone->*_priority.
1548 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001549 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 break;
1551 }
1552out:
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001553 /*
1554 * Note within each zone the priority level at which this zone was
1555 * brought into a happy state. So that the next thread which scans this
1556 * zone will start out at that priority level.
1557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 for (i = 0; i < pgdat->nr_zones; i++) {
1559 struct zone *zone = pgdat->node_zones + i;
1560
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001561 zone->prev_priority = temp_priority[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
1563 if (!all_zones_ok) {
1564 cond_resched();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001565
1566 try_to_freeze();
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 goto loop_again;
1569 }
1570
Andrew Morton05ff5132006-03-22 00:08:20 -08001571 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
1574/*
1575 * The background pageout daemon, started as a kernel thread
1576 * from the init process.
1577 *
1578 * This basically trickles out pages so that we have _some_
1579 * free memory available even if there is no other activity
1580 * that frees anything up. This is needed for things like routing
1581 * etc, where we otherwise might have all activity going on in
1582 * asynchronous contexts that cannot page things out.
1583 *
1584 * If there are applications that are active memory-allocators
1585 * (most normal use), this basically shouldn't matter.
1586 */
1587static int kswapd(void *p)
1588{
1589 unsigned long order;
1590 pg_data_t *pgdat = (pg_data_t*)p;
1591 struct task_struct *tsk = current;
1592 DEFINE_WAIT(wait);
1593 struct reclaim_state reclaim_state = {
1594 .reclaimed_slab = 0,
1595 };
1596 cpumask_t cpumask;
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 cpumask = node_to_cpumask(pgdat->node_id);
1599 if (!cpus_empty(cpumask))
1600 set_cpus_allowed(tsk, cpumask);
1601 current->reclaim_state = &reclaim_state;
1602
1603 /*
1604 * Tell the memory management that we're a "memory allocator",
1605 * and that if we need more memory we should get access to it
1606 * regardless (see "__alloc_pages()"). "kswapd" should
1607 * never get caught in the normal page freeing logic.
1608 *
1609 * (Kswapd normally doesn't need memory anyway, but sometimes
1610 * you need a small amount of memory in order to be able to
1611 * page out something else, and this flag essentially protects
1612 * us from recursively trying to free more memory as we're
1613 * trying to free the first piece of memory in the first place).
1614 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001615 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001616 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 order = 0;
1619 for ( ; ; ) {
1620 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1623 new_order = pgdat->kswapd_max_order;
1624 pgdat->kswapd_max_order = 0;
1625 if (order < new_order) {
1626 /*
1627 * Don't sleep if someone wants a larger 'order'
1628 * allocation
1629 */
1630 order = new_order;
1631 } else {
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07001632 if (!freezing(current))
1633 schedule();
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 order = pgdat->kswapd_max_order;
1636 }
1637 finish_wait(&pgdat->kswapd_wait, &wait);
1638
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07001639 if (!try_to_freeze()) {
1640 /* We can speed up thawing tasks if we don't call
1641 * balance_pgdat after returning from the refrigerator
1642 */
1643 balance_pgdat(pgdat, order);
1644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
1646 return 0;
1647}
1648
1649/*
1650 * A zone is low on free memory, so wake its kswapd task to service it.
1651 */
1652void wakeup_kswapd(struct zone *zone, int order)
1653{
1654 pg_data_t *pgdat;
1655
Con Kolivasf3fe6512006-01-06 00:11:15 -08001656 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return;
1658
1659 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001660 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return;
1662 if (pgdat->kswapd_max_order < order)
1663 pgdat->kswapd_max_order = order;
Paul Jackson02a0e532006-12-13 00:34:25 -08001664 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001666 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001668 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
1671#ifdef CONFIG_PM
1672/*
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001673 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1674 * from LRU lists system-wide, for given pass and priority, and returns the
1675 * number of reclaimed pages
1676 *
1677 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1678 */
Nigel Cunninghame07aa052006-12-22 01:07:21 -08001679static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
1680 int pass, struct scan_control *sc)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001681{
1682 struct zone *zone;
1683 unsigned long nr_to_scan, ret = 0;
1684
1685 for_each_zone(zone) {
1686
1687 if (!populated_zone(zone))
1688 continue;
1689
David Rientjese815af92007-10-16 23:25:54 -07001690 if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001691 continue;
1692
1693 /* For pass = 0 we don't shrink the active list */
1694 if (pass > 0) {
Christoph Lameterc8785382007-02-10 01:43:01 -08001695 zone->nr_scan_active +=
1696 (zone_page_state(zone, NR_ACTIVE) >> prio) + 1;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001697 if (zone->nr_scan_active >= nr_pages || pass > 3) {
1698 zone->nr_scan_active = 0;
Christoph Lameterc8785382007-02-10 01:43:01 -08001699 nr_to_scan = min(nr_pages,
1700 zone_page_state(zone, NR_ACTIVE));
Martin Blighbbdb3962006-10-28 10:38:25 -07001701 shrink_active_list(nr_to_scan, zone, sc, prio);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001702 }
1703 }
1704
Christoph Lameterc8785382007-02-10 01:43:01 -08001705 zone->nr_scan_inactive +=
1706 (zone_page_state(zone, NR_INACTIVE) >> prio) + 1;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001707 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1708 zone->nr_scan_inactive = 0;
Christoph Lameterc8785382007-02-10 01:43:01 -08001709 nr_to_scan = min(nr_pages,
1710 zone_page_state(zone, NR_INACTIVE));
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001711 ret += shrink_inactive_list(nr_to_scan, zone, sc);
1712 if (ret >= nr_pages)
1713 return ret;
1714 }
1715 }
1716
1717 return ret;
1718}
1719
Andrew Morton76395d32007-01-05 16:37:05 -08001720static unsigned long count_lru_pages(void)
1721{
Christoph Lameterc8785382007-02-10 01:43:01 -08001722 return global_page_state(NR_ACTIVE) + global_page_state(NR_INACTIVE);
Andrew Morton76395d32007-01-05 16:37:05 -08001723}
1724
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001725/*
1726 * Try to free `nr_pages' of memory, system-wide, and return the number of
1727 * freed pages.
1728 *
1729 * Rather than trying to age LRUs the aim is to preserve the overall
1730 * LRU order by reclaiming preferentially
1731 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 */
Andrew Morton69e05942006-03-22 00:08:19 -08001733unsigned long shrink_all_memory(unsigned long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001735 unsigned long lru_pages, nr_slab;
Andrew Morton69e05942006-03-22 00:08:19 -08001736 unsigned long ret = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001737 int pass;
1738 struct reclaim_state reclaim_state;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001739 struct scan_control sc = {
1740 .gfp_mask = GFP_KERNEL,
1741 .may_swap = 0,
1742 .swap_cluster_max = nr_pages,
1743 .may_writepage = 1,
1744 .swappiness = vm_swappiness,
Balbir Singh66e17072008-02-07 00:13:56 -08001745 .isolate_pages = isolate_pages_global,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 };
1747
1748 current->reclaim_state = &reclaim_state;
Andrew Morton69e05942006-03-22 00:08:19 -08001749
Andrew Morton76395d32007-01-05 16:37:05 -08001750 lru_pages = count_lru_pages();
Christoph Lameter972d1a72006-09-25 23:31:51 -07001751 nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001752 /* If slab caches are huge, it's better to hit them first */
1753 while (nr_slab >= lru_pages) {
1754 reclaim_state.reclaimed_slab = 0;
1755 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1756 if (!reclaim_state.reclaimed_slab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 break;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001758
1759 ret += reclaim_state.reclaimed_slab;
1760 if (ret >= nr_pages)
1761 goto out;
1762
1763 nr_slab -= reclaim_state.reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001765
1766 /*
1767 * We try to shrink LRUs in 5 passes:
1768 * 0 = Reclaim from inactive_list only
1769 * 1 = Reclaim from active list but don't reclaim mapped
1770 * 2 = 2nd pass of type 1
1771 * 3 = Reclaim mapped (normal reclaim)
1772 * 4 = 2nd pass of type 3
1773 */
1774 for (pass = 0; pass < 5; pass++) {
1775 int prio;
1776
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001777 /* Force reclaiming mapped pages in the passes #3 and #4 */
1778 if (pass > 2) {
1779 sc.may_swap = 1;
1780 sc.swappiness = 100;
1781 }
1782
1783 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1784 unsigned long nr_to_scan = nr_pages - ret;
1785
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001786 sc.nr_scanned = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001787 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1788 if (ret >= nr_pages)
1789 goto out;
1790
1791 reclaim_state.reclaimed_slab = 0;
Andrew Morton76395d32007-01-05 16:37:05 -08001792 shrink_slab(sc.nr_scanned, sc.gfp_mask,
1793 count_lru_pages());
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001794 ret += reclaim_state.reclaimed_slab;
1795 if (ret >= nr_pages)
1796 goto out;
1797
1798 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001799 congestion_wait(WRITE, HZ / 10);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001800 }
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001801 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001802
1803 /*
1804 * If ret = 0, we could not shrink LRUs, but there may be something
1805 * in slab caches
1806 */
Andrew Morton76395d32007-01-05 16:37:05 -08001807 if (!ret) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001808 do {
1809 reclaim_state.reclaimed_slab = 0;
Andrew Morton76395d32007-01-05 16:37:05 -08001810 shrink_slab(nr_pages, sc.gfp_mask, count_lru_pages());
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001811 ret += reclaim_state.reclaimed_slab;
1812 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
Andrew Morton76395d32007-01-05 16:37:05 -08001813 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001814
1815out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 current->reclaim_state = NULL;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return ret;
1819}
1820#endif
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822/* It's optimal to keep kswapds on the same CPUs as their memory, but
1823 not required for correctness. So if the last cpu in a node goes
1824 away, we get changed to run anywhere: as the first one comes back,
1825 restore their cpu bindings. */
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07001826static int __devinit cpu_callback(struct notifier_block *nfb,
Andrew Morton69e05942006-03-22 00:08:19 -08001827 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 pg_data_t *pgdat;
1830 cpumask_t mask;
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07001831 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001833 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07001834 for_each_node_state(nid, N_HIGH_MEMORY) {
1835 pgdat = NODE_DATA(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 mask = node_to_cpumask(pgdat->node_id);
1837 if (any_online_cpu(mask) != NR_CPUS)
1838 /* One of our CPUs online: restore mask */
1839 set_cpus_allowed(pgdat->kswapd, mask);
1840 }
1841 }
1842 return NOTIFY_OK;
1843}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Yasunori Goto3218ae12006-06-27 02:53:33 -07001845/*
1846 * This kswapd start function will be called by init and node-hot-add.
1847 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
1848 */
1849int kswapd_run(int nid)
1850{
1851 pg_data_t *pgdat = NODE_DATA(nid);
1852 int ret = 0;
1853
1854 if (pgdat->kswapd)
1855 return 0;
1856
1857 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
1858 if (IS_ERR(pgdat->kswapd)) {
1859 /* failure at boot is fatal */
1860 BUG_ON(system_state == SYSTEM_BOOTING);
1861 printk("Failed to start kswapd on node %d\n",nid);
1862 ret = -1;
1863 }
1864 return ret;
1865}
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867static int __init kswapd_init(void)
1868{
Yasunori Goto3218ae12006-06-27 02:53:33 -07001869 int nid;
Andrew Morton69e05942006-03-22 00:08:19 -08001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 swap_setup();
Christoph Lameter9422ffb2007-10-16 01:25:31 -07001872 for_each_node_state(nid, N_HIGH_MEMORY)
Yasunori Goto3218ae12006-06-27 02:53:33 -07001873 kswapd_run(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 hotcpu_notifier(cpu_callback, 0);
1875 return 0;
1876}
1877
1878module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001879
1880#ifdef CONFIG_NUMA
1881/*
1882 * Zone reclaim mode
1883 *
1884 * If non-zero call zone_reclaim when the number of free pages falls below
1885 * the watermarks.
Christoph Lameter9eeff232006-01-18 17:42:31 -08001886 */
1887int zone_reclaim_mode __read_mostly;
1888
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001889#define RECLAIM_OFF 0
1890#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1891#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1892#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1893
Christoph Lameter9eeff232006-01-18 17:42:31 -08001894/*
Christoph Lametera92f7122006-02-01 03:05:32 -08001895 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1896 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1897 * a zone.
1898 */
1899#define ZONE_RECLAIM_PRIORITY 4
1900
Christoph Lameter9eeff232006-01-18 17:42:31 -08001901/*
Christoph Lameter96146342006-07-03 00:24:13 -07001902 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
1903 * occur.
1904 */
1905int sysctl_min_unmapped_ratio = 1;
1906
1907/*
Christoph Lameter0ff38492006-09-25 23:31:52 -07001908 * If the number of slab pages in a zone grows beyond this percentage then
1909 * slab reclaim needs to occur.
1910 */
1911int sysctl_min_slab_ratio = 5;
1912
1913/*
Christoph Lameter9eeff232006-01-18 17:42:31 -08001914 * Try to free up some pages from this zone through reclaim.
1915 */
Andrew Morton179e9632006-03-22 00:08:18 -08001916static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001917{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001918 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08001919 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001920 struct task_struct *p = current;
1921 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08001922 int priority;
Andrew Morton05ff5132006-03-22 00:08:20 -08001923 unsigned long nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001924 struct scan_control sc = {
1925 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1926 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
Andrew Morton69e05942006-03-22 00:08:19 -08001927 .swap_cluster_max = max_t(unsigned long, nr_pages,
1928 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08001929 .gfp_mask = gfp_mask,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001930 .swappiness = vm_swappiness,
Balbir Singh66e17072008-02-07 00:13:56 -08001931 .isolate_pages = isolate_pages_global,
Andrew Morton179e9632006-03-22 00:08:18 -08001932 };
Christoph Lameter83e33a42006-09-25 23:31:53 -07001933 unsigned long slab_reclaimable;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001934
1935 disable_swap_token();
Christoph Lameter9eeff232006-01-18 17:42:31 -08001936 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08001937 /*
1938 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1939 * and we also need to be able to write out pages for RECLAIM_WRITE
1940 * and RECLAIM_SWAP.
1941 */
1942 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001943 reclaim_state.reclaimed_slab = 0;
1944 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001945
Christoph Lameter0ff38492006-09-25 23:31:52 -07001946 if (zone_page_state(zone, NR_FILE_PAGES) -
1947 zone_page_state(zone, NR_FILE_MAPPED) >
1948 zone->min_unmapped_pages) {
1949 /*
1950 * Free memory by calling shrink zone with increasing
1951 * priorities until we have enough memory freed.
1952 */
1953 priority = ZONE_RECLAIM_PRIORITY;
1954 do {
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001955 note_zone_scanning_priority(zone, priority);
Christoph Lameter0ff38492006-09-25 23:31:52 -07001956 nr_reclaimed += shrink_zone(priority, zone, &sc);
1957 priority--;
1958 } while (priority >= 0 && nr_reclaimed < nr_pages);
1959 }
Christoph Lameterc84db232006-02-01 03:05:29 -08001960
Christoph Lameter83e33a42006-09-25 23:31:53 -07001961 slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1962 if (slab_reclaimable > zone->min_slab_pages) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001963 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001964 * shrink_slab() does not currently allow us to determine how
Christoph Lameter0ff38492006-09-25 23:31:52 -07001965 * many pages were freed in this zone. So we take the current
1966 * number of slab pages and shake the slab until it is reduced
1967 * by the same nr_pages that we used for reclaiming unmapped
1968 * pages.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001969 *
Christoph Lameter0ff38492006-09-25 23:31:52 -07001970 * Note that shrink_slab will free memory on all zones and may
1971 * take a long time.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001972 */
Christoph Lameter0ff38492006-09-25 23:31:52 -07001973 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
Christoph Lameter83e33a42006-09-25 23:31:53 -07001974 zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
1975 slab_reclaimable - nr_pages)
Christoph Lameter0ff38492006-09-25 23:31:52 -07001976 ;
Christoph Lameter83e33a42006-09-25 23:31:53 -07001977
1978 /*
1979 * Update nr_reclaimed by the number of slab pages we
1980 * reclaimed from this zone.
1981 */
1982 nr_reclaimed += slab_reclaimable -
1983 zone_page_state(zone, NR_SLAB_RECLAIMABLE);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001984 }
1985
Christoph Lameter9eeff232006-01-18 17:42:31 -08001986 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08001987 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Andrew Morton05ff5132006-03-22 00:08:20 -08001988 return nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001989}
Andrew Morton179e9632006-03-22 00:08:18 -08001990
1991int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1992{
Andrew Morton179e9632006-03-22 00:08:18 -08001993 int node_id;
David Rientjesd773ed62007-10-16 23:26:01 -07001994 int ret;
Andrew Morton179e9632006-03-22 00:08:18 -08001995
1996 /*
Christoph Lameter0ff38492006-09-25 23:31:52 -07001997 * Zone reclaim reclaims unmapped file backed pages and
1998 * slab pages if we are over the defined limits.
Christoph Lameter34aa1332006-06-30 01:55:37 -07001999 *
Christoph Lameter96146342006-07-03 00:24:13 -07002000 * A small portion of unmapped file backed pages is needed for
2001 * file I/O otherwise pages read by file I/O will be immediately
2002 * thrown out if the zone is overallocated. So we do not reclaim
2003 * if less than a specified percentage of the zone is used by
2004 * unmapped file backed pages.
Andrew Morton179e9632006-03-22 00:08:18 -08002005 */
Christoph Lameter34aa1332006-06-30 01:55:37 -07002006 if (zone_page_state(zone, NR_FILE_PAGES) -
Christoph Lameter0ff38492006-09-25 23:31:52 -07002007 zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
2008 && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
2009 <= zone->min_slab_pages)
Christoph Lameter96146342006-07-03 00:24:13 -07002010 return 0;
Andrew Morton179e9632006-03-22 00:08:18 -08002011
David Rientjesd773ed62007-10-16 23:26:01 -07002012 if (zone_is_all_unreclaimable(zone))
2013 return 0;
2014
Andrew Morton179e9632006-03-22 00:08:18 -08002015 /*
David Rientjesd773ed62007-10-16 23:26:01 -07002016 * Do not scan if the allocation should not be delayed.
Andrew Morton179e9632006-03-22 00:08:18 -08002017 */
David Rientjesd773ed62007-10-16 23:26:01 -07002018 if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
Andrew Morton179e9632006-03-22 00:08:18 -08002019 return 0;
2020
2021 /*
2022 * Only run zone reclaim on the local zone or on zones that do not
2023 * have associated processors. This will favor the local processor
2024 * over remote processors and spread off node memory allocations
2025 * as wide as possible.
2026 */
Christoph Lameter89fa3022006-09-25 23:31:55 -07002027 node_id = zone_to_nid(zone);
Christoph Lameter37c07082007-10-16 01:25:36 -07002028 if (node_state(node_id, N_CPU) && node_id != numa_node_id())
Andrew Morton179e9632006-03-22 00:08:18 -08002029 return 0;
David Rientjesd773ed62007-10-16 23:26:01 -07002030
2031 if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
2032 return 0;
2033 ret = __zone_reclaim(zone, gfp_mask, order);
2034 zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
2035
2036 return ret;
Andrew Morton179e9632006-03-22 00:08:18 -08002037}
Christoph Lameter9eeff232006-01-18 17:42:31 -08002038#endif