blob: 1b85217b528c38538487d46356069a8c9ffc0b5f [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
129/*
130 * Add a shrinker callback to be called from the vm
131 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700132void register_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700134 shrinker->nr = 0;
135 down_write(&shrinker_rwsem);
136 list_add_tail(&shrinker->list, &shrinker_list);
137 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700139EXPORT_SYMBOL(register_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141/*
142 * Remove one
143 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700144void unregister_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 down_write(&shrinker_rwsem);
147 list_del(&shrinker->list);
148 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700150EXPORT_SYMBOL(unregister_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152#define SHRINK_BATCH 128
153/*
154 * Call the shrink functions to age shrinkable caches
155 *
156 * Here we assume it costs one seek to replace a lru page and that it also
157 * takes a seek to recreate a cache object. With this in mind we age equal
158 * percentages of the lru and ageable caches. This should balance the seeks
159 * generated by these structures.
160 *
Simon Arlott183ff222007-10-20 01:27:18 +0200161 * If the vm encountered mapped pages on the LRU it increase the pressure on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * slab to avoid swapping.
163 *
164 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
165 *
166 * `lru_pages' represents the number of on-LRU pages in all the zones which
167 * are eligible for the caller's allocation attempt. It is used for balancing
168 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700169 *
170 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
Andrew Morton69e05942006-03-22 00:08:19 -0800172unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
173 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800176 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 if (scanned == 0)
179 scanned = SWAP_CLUSTER_MAX;
180
181 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700182 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 list_for_each_entry(shrinker, &shrinker_list, list) {
185 unsigned long long delta;
186 unsigned long total_scan;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700187 unsigned long max_pass = (*shrinker->shrink)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800190 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 do_div(delta, lru_pages + 1);
192 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800193 if (shrinker->nr < 0) {
194 printk(KERN_ERR "%s: nr=%ld\n",
195 __FUNCTION__, shrinker->nr);
196 shrinker->nr = max_pass;
197 }
198
199 /*
200 * Avoid risking looping forever due to too large nr value:
201 * never try to free more than twice the estimate number of
202 * freeable entries.
203 */
204 if (shrinker->nr > max_pass * 2)
205 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 total_scan = shrinker->nr;
208 shrinker->nr = 0;
209
210 while (total_scan >= SHRINK_BATCH) {
211 long this_scan = SHRINK_BATCH;
212 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700213 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Rusty Russell8e1f9362007-07-17 04:03:17 -0700215 nr_before = (*shrinker->shrink)(0, gfp_mask);
216 shrink_ret = (*shrinker->shrink)(this_scan, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (shrink_ret == -1)
218 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700219 if (shrink_ret < nr_before)
220 ret += nr_before - shrink_ret;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700221 count_vm_events(SLABS_SCANNED, this_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 total_scan -= this_scan;
223
224 cond_resched();
225 }
226
227 shrinker->nr += total_scan;
228 }
229 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700230 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233/* Called without lock on whether page is mapped, so answer is unstable */
234static inline int page_mapping_inuse(struct page *page)
235{
236 struct address_space *mapping;
237
238 /* Page is in somebody's page tables. */
239 if (page_mapped(page))
240 return 1;
241
242 /* Be more reluctant to reclaim swapcache than pagecache */
243 if (PageSwapCache(page))
244 return 1;
245
246 mapping = page_mapping(page);
247 if (!mapping)
248 return 0;
249
250 /* File is mmap'd by somebody? */
251 return mapping_mapped(mapping);
252}
253
254static inline int is_page_cache_freeable(struct page *page)
255{
256 return page_count(page) - !!PagePrivate(page) == 2;
257}
258
259static int may_write_to_queue(struct backing_dev_info *bdi)
260{
Christoph Lameter930d9152006-01-08 01:00:47 -0800261 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 1;
263 if (!bdi_write_congested(bdi))
264 return 1;
265 if (bdi == current->backing_dev_info)
266 return 1;
267 return 0;
268}
269
270/*
271 * We detected a synchronous write error writing a page out. Probably
272 * -ENOSPC. We need to propagate that into the address_space for a subsequent
273 * fsync(), msync() or close().
274 *
275 * The tricky part is that after writepage we cannot touch the mapping: nothing
276 * prevents it from being freed up. But we have a ref on the page and once
277 * that page is locked, the mapping is pinned.
278 *
279 * We're allowed to run sleeping lock_page() here because we know the caller has
280 * __GFP_FS.
281 */
282static void handle_write_error(struct address_space *mapping,
283 struct page *page, int error)
284{
285 lock_page(page);
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -0700286 if (page_mapping(page) == mapping)
287 mapping_set_error(mapping, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 unlock_page(page);
289}
290
Andy Whitcroftc661b072007-08-22 14:01:26 -0700291/* Request for sync pageout. */
292enum pageout_io {
293 PAGEOUT_IO_ASYNC,
294 PAGEOUT_IO_SYNC,
295};
296
Christoph Lameter04e62a22006-06-23 02:03:38 -0700297/* possible outcome of pageout() */
298typedef enum {
299 /* failed to write page out, page is locked */
300 PAGE_KEEP,
301 /* move page to the active list, page is locked */
302 PAGE_ACTIVATE,
303 /* page has been sent to the disk successfully, page is unlocked */
304 PAGE_SUCCESS,
305 /* page is clean and locked */
306 PAGE_CLEAN,
307} pageout_t;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
Andrew Morton1742f192006-03-22 00:08:21 -0800310 * pageout is called by shrink_page_list() for each dirty page.
311 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700313static pageout_t pageout(struct page *page, struct address_space *mapping,
314 enum pageout_io sync_writeback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 /*
317 * If the page is dirty, only perform writeback if that write
318 * will be non-blocking. To prevent this allocation from being
319 * stalled by pagecache activity. But note that there may be
320 * stalls if we need to run get_block(). We could test
321 * PagePrivate for that.
322 *
323 * If this process is currently in generic_file_write() against
324 * this page's queue, we can perform writeback even if that
325 * will block.
326 *
327 * If the page is swapcache, write it back even if that would
328 * block, for some throttling. This happens by accident, because
329 * swap_backing_dev_info is bust: it doesn't reflect the
330 * congestion state of the swapdevs. Easy to fix, if needed.
331 * See swapfile.c:page_queue_congested().
332 */
333 if (!is_page_cache_freeable(page))
334 return PAGE_KEEP;
335 if (!mapping) {
336 /*
337 * Some data journaling orphaned pages can have
338 * page->mapping == NULL while being dirty with clean buffers.
339 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700340 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (try_to_free_buffers(page)) {
342 ClearPageDirty(page);
343 printk("%s: orphaned page\n", __FUNCTION__);
344 return PAGE_CLEAN;
345 }
346 }
347 return PAGE_KEEP;
348 }
349 if (mapping->a_ops->writepage == NULL)
350 return PAGE_ACTIVATE;
351 if (!may_write_to_queue(mapping->backing_dev_info))
352 return PAGE_KEEP;
353
354 if (clear_page_dirty_for_io(page)) {
355 int res;
356 struct writeback_control wbc = {
357 .sync_mode = WB_SYNC_NONE,
358 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700359 .range_start = 0,
360 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 .nonblocking = 1,
362 .for_reclaim = 1,
363 };
364
365 SetPageReclaim(page);
366 res = mapping->a_ops->writepage(page, &wbc);
367 if (res < 0)
368 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800369 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ClearPageReclaim(page);
371 return PAGE_ACTIVATE;
372 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700373
374 /*
375 * Wait on writeback if requested to. This happens when
376 * direct reclaiming a large contiguous area and the
377 * first attempt to free a range of pages fails.
378 */
379 if (PageWriteback(page) && sync_writeback == PAGEOUT_IO_SYNC)
380 wait_on_page_writeback(page);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!PageWriteback(page)) {
383 /* synchronous write or broken a_ops? */
384 ClearPageReclaim(page);
385 }
Andrew Mortone129b5c2006-09-27 01:50:00 -0700386 inc_zone_page_state(page, NR_VMSCAN_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return PAGE_SUCCESS;
388 }
389
390 return PAGE_CLEAN;
391}
392
Andrew Mortona649fd92006-10-17 00:09:36 -0700393/*
394 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
395 * someone else has a ref on the page, abort and return 0. If it was
396 * successfully detached, return 1. Assumes the caller has a single ref on
397 * this page.
398 */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800399int remove_mapping(struct address_space *mapping, struct page *page)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800400{
Nick Piggin28e4d962006-09-25 23:31:23 -0700401 BUG_ON(!PageLocked(page));
402 BUG_ON(mapping != page_mapping(page));
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800403
404 write_lock_irq(&mapping->tree_lock);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800405 /*
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700406 * The non racy check for a busy page.
407 *
408 * Must be careful with the order of the tests. When someone has
409 * a ref to the page, it may be possible that they dirty it then
410 * drop the reference. So if PageDirty is tested before page_count
411 * here, then the following race may occur:
412 *
413 * get_user_pages(&page);
414 * [user mapping goes away]
415 * write_to(page);
416 * !PageDirty(page) [good]
417 * SetPageDirty(page);
418 * put_page(page);
419 * !page_count(page) [good, discard it]
420 *
421 * [oops, our write_to data is lost]
422 *
423 * Reversing the order of the tests ensures such a situation cannot
424 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
425 * load is not satisfied before that of page->_count.
426 *
427 * Note that if SetPageDirty is always performed via set_page_dirty,
428 * and thus under tree_lock, then this ordering is not required.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800429 */
430 if (unlikely(page_count(page) != 2))
431 goto cannot_free;
432 smp_rmb();
433 if (unlikely(PageDirty(page)))
434 goto cannot_free;
435
436 if (PageSwapCache(page)) {
437 swp_entry_t swap = { .val = page_private(page) };
438 __delete_from_swap_cache(page);
439 write_unlock_irq(&mapping->tree_lock);
440 swap_free(swap);
441 __put_page(page); /* The pagecache ref */
442 return 1;
443 }
444
445 __remove_from_page_cache(page);
446 write_unlock_irq(&mapping->tree_lock);
447 __put_page(page);
448 return 1;
449
450cannot_free:
451 write_unlock_irq(&mapping->tree_lock);
452 return 0;
453}
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/*
Andrew Morton1742f192006-03-22 00:08:21 -0800456 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 */
Andrew Morton1742f192006-03-22 00:08:21 -0800458static unsigned long shrink_page_list(struct list_head *page_list,
Andy Whitcroftc661b072007-08-22 14:01:26 -0700459 struct scan_control *sc,
460 enum pageout_io sync_writeback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 LIST_HEAD(ret_pages);
463 struct pagevec freed_pvec;
464 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800465 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 cond_resched();
468
469 pagevec_init(&freed_pvec, 1);
470 while (!list_empty(page_list)) {
471 struct address_space *mapping;
472 struct page *page;
473 int may_enter_fs;
474 int referenced;
475
476 cond_resched();
477
478 page = lru_to_page(page_list);
479 list_del(&page->lru);
480
481 if (TestSetPageLocked(page))
482 goto keep;
483
Nick Piggin725d7042006-09-25 23:30:55 -0700484 VM_BUG_ON(PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800487
488 if (!sc->may_swap && page_mapped(page))
489 goto keep_locked;
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* Double the slab pressure for mapped and swapcache pages */
492 if (page_mapped(page) || PageSwapCache(page))
493 sc->nr_scanned++;
494
Andy Whitcroftc661b072007-08-22 14:01:26 -0700495 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
496 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
497
498 if (PageWriteback(page)) {
499 /*
500 * Synchronous reclaim is performed in two passes,
501 * first an asynchronous pass over the list to
502 * start parallel writeback, and a second synchronous
503 * pass to wait for the IO to complete. Wait here
504 * for any page for which writeback has already
505 * started.
506 */
507 if (sync_writeback == PAGEOUT_IO_SYNC && may_enter_fs)
508 wait_on_page_writeback(page);
Rik van Rielf1a9ee72008-02-07 00:14:08 -0800509 else {
510 sc->nr_io_pages++;
Andy Whitcroftc661b072007-08-22 14:01:26 -0700511 goto keep_locked;
Rik van Rielf1a9ee72008-02-07 00:14:08 -0800512 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Balbir Singhbed71612008-02-07 00:14:01 -0800515 referenced = page_referenced(page, 1, sc->mem_cgroup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /* In active use or really unfreeable? Activate it. */
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700517 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER &&
518 referenced && page_mapping_inuse(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto activate_locked;
520
521#ifdef CONFIG_SWAP
522 /*
523 * Anonymous process memory has backing store?
524 * Try to allocate it some swap space here.
525 */
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800526 if (PageAnon(page) && !PageSwapCache(page))
Christoph Lameter1480a542006-01-08 01:00:53 -0800527 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529#endif /* CONFIG_SWAP */
530
531 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /*
534 * The page is mapped into the page tables of one or more
535 * processes. Try to unmap it here.
536 */
537 if (page_mapped(page) && mapping) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800538 switch (try_to_unmap(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 case SWAP_FAIL:
540 goto activate_locked;
541 case SWAP_AGAIN:
542 goto keep_locked;
543 case SWAP_SUCCESS:
544 ; /* try to free the page below */
545 }
546 }
547
548 if (PageDirty(page)) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700549 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && referenced)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto keep_locked;
Rik van Rielf1a9ee72008-02-07 00:14:08 -0800551 if (!may_enter_fs) {
552 sc->nr_io_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto keep_locked;
Rik van Rielf1a9ee72008-02-07 00:14:08 -0800554 }
Christoph Lameter52a83632006-02-01 03:05:28 -0800555 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto keep_locked;
557
558 /* Page is dirty, try to write it out here */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700559 switch (pageout(page, mapping, sync_writeback)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 case PAGE_KEEP:
561 goto keep_locked;
562 case PAGE_ACTIVATE:
563 goto activate_locked;
564 case PAGE_SUCCESS:
Rik van Rielf1a9ee72008-02-07 00:14:08 -0800565 if (PageWriteback(page) || PageDirty(page)) {
566 sc->nr_io_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto keep;
Rik van Rielf1a9ee72008-02-07 00:14:08 -0800568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /*
570 * A synchronous write - probably a ramdisk. Go
571 * ahead and try to reclaim the page.
572 */
573 if (TestSetPageLocked(page))
574 goto keep;
575 if (PageDirty(page) || PageWriteback(page))
576 goto keep_locked;
577 mapping = page_mapping(page);
578 case PAGE_CLEAN:
579 ; /* try to free the page below */
580 }
581 }
582
583 /*
584 * If the page has buffers, try to free the buffer mappings
585 * associated with this page. If we succeed we try to free
586 * the page as well.
587 *
588 * We do this even if the page is PageDirty().
589 * try_to_release_page() does not perform I/O, but it is
590 * possible for a page to have PageDirty set, but it is actually
591 * clean (all its buffers are clean). This happens if the
592 * buffers were written out directly, with submit_bh(). ext3
593 * will do this, as well as the blockdev mapping.
594 * try_to_release_page() will discover that cleanness and will
595 * drop the buffers and mark the page clean - it can be freed.
596 *
597 * Rarely, pages can have buffers and no ->mapping. These are
598 * the pages which were not successfully invalidated in
599 * truncate_complete_page(). We try to drop those buffers here
600 * and if that worked, and the page is no longer mapped into
601 * process address space (page_count == 1) it can be freed.
602 * Otherwise, leave the page on the LRU so it is swappable.
603 */
604 if (PagePrivate(page)) {
605 if (!try_to_release_page(page, sc->gfp_mask))
606 goto activate_locked;
607 if (!mapping && page_count(page) == 1)
608 goto free_it;
609 }
610
Nick Piggin28e4d962006-09-25 23:31:23 -0700611 if (!mapping || !remove_mapping(mapping, page))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800612 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614free_it:
615 unlock_page(page);
Andrew Morton05ff5132006-03-22 00:08:20 -0800616 nr_reclaimed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (!pagevec_add(&freed_pvec, page))
618 __pagevec_release_nonlru(&freed_pvec);
619 continue;
620
621activate_locked:
622 SetPageActive(page);
623 pgactivate++;
624keep_locked:
625 unlock_page(page);
626keep:
627 list_add(&page->lru, &ret_pages);
Nick Piggin725d7042006-09-25 23:30:55 -0700628 VM_BUG_ON(PageLRU(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630 list_splice(&ret_pages, page_list);
631 if (pagevec_count(&freed_pvec))
632 __pagevec_release_nonlru(&freed_pvec);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700633 count_vm_events(PGACTIVATE, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800634 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700637/* LRU Isolation modes. */
638#define ISOLATE_INACTIVE 0 /* Isolate inactive pages. */
639#define ISOLATE_ACTIVE 1 /* Isolate active pages. */
640#define ISOLATE_BOTH 2 /* Isolate both active and inactive pages. */
641
642/*
643 * Attempt to remove the specified page from its LRU. Only take this page
644 * if it is of the appropriate PageActive status. Pages which are being
645 * freed elsewhere are also ignored.
646 *
647 * page: page to consider
648 * mode: one of the LRU isolation modes defined above
649 *
650 * returns 0 on success, -ve errno on failure.
651 */
Balbir Singh66e17072008-02-07 00:13:56 -0800652int __isolate_lru_page(struct page *page, int mode)
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700653{
654 int ret = -EINVAL;
655
656 /* Only take pages on the LRU. */
657 if (!PageLRU(page))
658 return ret;
659
660 /*
661 * When checking the active state, we need to be sure we are
662 * dealing with comparible boolean values. Take the logical not
663 * of each.
664 */
665 if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode))
666 return ret;
667
668 ret = -EBUSY;
669 if (likely(get_page_unless_zero(page))) {
670 /*
671 * Be careful not to clear PageLRU until after we're
672 * sure the page is not being freed elsewhere -- the
673 * page release code relies on it.
674 */
675 ClearPageLRU(page);
676 ret = 0;
677 }
678
679 return ret;
680}
681
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800682/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 * zone->lru_lock is heavily contended. Some of the functions that
684 * shrink the lists perform better by taking out a batch of pages
685 * and working on them outside the LRU lock.
686 *
687 * For pagecache intensive workloads, this function is the hottest
688 * spot in the kernel (apart from copy_*_user functions).
689 *
690 * Appropriate locks must be held before calling this function.
691 *
692 * @nr_to_scan: The number of pages to look through on the list.
693 * @src: The LRU list to pull pages off.
694 * @dst: The temp list to put pages on to.
695 * @scanned: The number of pages that were scanned.
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700696 * @order: The caller's attempted allocation order
697 * @mode: One of the LRU isolation modes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 *
699 * returns how many pages were moved onto *@dst.
700 */
Andrew Morton69e05942006-03-22 00:08:19 -0800701static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
702 struct list_head *src, struct list_head *dst,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700703 unsigned long *scanned, int order, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Andrew Morton69e05942006-03-22 00:08:19 -0800705 unsigned long nr_taken = 0;
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800706 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800708 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700709 struct page *page;
710 unsigned long pfn;
711 unsigned long end_pfn;
712 unsigned long page_pfn;
713 int zone_id;
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 page = lru_to_page(src);
716 prefetchw_prev_lru_page(page, src, flags);
717
Nick Piggin725d7042006-09-25 23:30:55 -0700718 VM_BUG_ON(!PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800719
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700720 switch (__isolate_lru_page(page, mode)) {
721 case 0:
722 list_move(&page->lru, dst);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800723 nr_taken++;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700724 break;
Nick Piggin46453a62006-03-22 00:07:58 -0800725
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700726 case -EBUSY:
727 /* else it is being freed elsewhere */
728 list_move(&page->lru, src);
729 continue;
730
731 default:
732 BUG();
733 }
734
735 if (!order)
736 continue;
737
738 /*
739 * Attempt to take all pages in the order aligned region
740 * surrounding the tag page. Only take those pages of
741 * the same active state as that tag page. We may safely
742 * round the target page pfn down to the requested order
743 * as the mem_map is guarenteed valid out to MAX_ORDER,
744 * where that page is in a different zone we will detect
745 * it from its zone id and abort this block scan.
746 */
747 zone_id = page_zone_id(page);
748 page_pfn = page_to_pfn(page);
749 pfn = page_pfn & ~((1 << order) - 1);
750 end_pfn = pfn + (1 << order);
751 for (; pfn < end_pfn; pfn++) {
752 struct page *cursor_page;
753
754 /* The target page is in the block, ignore it. */
755 if (unlikely(pfn == page_pfn))
756 continue;
757
758 /* Avoid holes within the zone. */
759 if (unlikely(!pfn_valid_within(pfn)))
760 break;
761
762 cursor_page = pfn_to_page(pfn);
763 /* Check that we have not crossed a zone boundary. */
764 if (unlikely(page_zone_id(cursor_page) != zone_id))
765 continue;
766 switch (__isolate_lru_page(cursor_page, mode)) {
767 case 0:
768 list_move(&cursor_page->lru, dst);
769 nr_taken++;
770 scan++;
771 break;
772
773 case -EBUSY:
774 /* else it is being freed elsewhere */
775 list_move(&cursor_page->lru, src);
776 default:
777 break;
778 }
779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781
782 *scanned = scan;
783 return nr_taken;
784}
785
Balbir Singh66e17072008-02-07 00:13:56 -0800786static unsigned long isolate_pages_global(unsigned long nr,
787 struct list_head *dst,
788 unsigned long *scanned, int order,
789 int mode, struct zone *z,
790 struct mem_cgroup *mem_cont,
791 int active)
792{
793 if (active)
794 return isolate_lru_pages(nr, &z->active_list, dst,
795 scanned, order, mode);
796 else
797 return isolate_lru_pages(nr, &z->inactive_list, dst,
798 scanned, order, mode);
799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/*
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700802 * clear_active_flags() is a helper for shrink_active_list(), clearing
803 * any active bits from the pages in the list.
804 */
805static unsigned long clear_active_flags(struct list_head *page_list)
806{
807 int nr_active = 0;
808 struct page *page;
809
810 list_for_each_entry(page, page_list, lru)
811 if (PageActive(page)) {
812 ClearPageActive(page);
813 nr_active++;
814 }
815
816 return nr_active;
817}
818
819/*
Andrew Morton1742f192006-03-22 00:08:21 -0800820 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
821 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
Andrew Morton1742f192006-03-22 00:08:21 -0800823static unsigned long shrink_inactive_list(unsigned long max_scan,
824 struct zone *zone, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 LIST_HEAD(page_list);
827 struct pagevec pvec;
Andrew Morton69e05942006-03-22 00:08:19 -0800828 unsigned long nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800829 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 pagevec_init(&pvec, 1);
832
833 lru_add_drain();
834 spin_lock_irq(&zone->lru_lock);
Andrew Morton69e05942006-03-22 00:08:19 -0800835 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -0800837 unsigned long nr_taken;
838 unsigned long nr_scan;
839 unsigned long nr_freed;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700840 unsigned long nr_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Balbir Singh66e17072008-02-07 00:13:56 -0800842 nr_taken = sc->isolate_pages(sc->swap_cluster_max,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700843 &page_list, &nr_scan, sc->order,
844 (sc->order > PAGE_ALLOC_COSTLY_ORDER)?
Balbir Singh66e17072008-02-07 00:13:56 -0800845 ISOLATE_BOTH : ISOLATE_INACTIVE,
846 zone, sc->mem_cgroup, 0);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700847 nr_active = clear_active_flags(&page_list);
Andy Whitcrofte9187bd2007-08-22 14:01:25 -0700848 __count_vm_events(PGDEACTIVATE, nr_active);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700849
850 __mod_zone_page_state(zone, NR_ACTIVE, -nr_active);
851 __mod_zone_page_state(zone, NR_INACTIVE,
852 -(nr_taken - nr_active));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 zone->pages_scanned += nr_scan;
854 spin_unlock_irq(&zone->lru_lock);
855
Andrew Morton69e05942006-03-22 00:08:19 -0800856 nr_scanned += nr_scan;
Andy Whitcroftc661b072007-08-22 14:01:26 -0700857 nr_freed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC);
858
859 /*
860 * If we are direct reclaiming for contiguous pages and we do
861 * not reclaim everything in the list, try again and wait
862 * for IO to complete. This will stall high-order allocations
863 * but that should be acceptable to the caller
864 */
865 if (nr_freed < nr_taken && !current_is_kswapd() &&
866 sc->order > PAGE_ALLOC_COSTLY_ORDER) {
867 congestion_wait(WRITE, HZ/10);
868
869 /*
870 * The attempt at page out may have made some
871 * of the pages active, mark them inactive again.
872 */
873 nr_active = clear_active_flags(&page_list);
874 count_vm_events(PGDEACTIVATE, nr_active);
875
876 nr_freed += shrink_page_list(&page_list, sc,
877 PAGEOUT_IO_SYNC);
878 }
879
Andrew Morton05ff5132006-03-22 00:08:20 -0800880 nr_reclaimed += nr_freed;
Nick Piggina74609f2006-01-06 00:11:20 -0800881 local_irq_disable();
882 if (current_is_kswapd()) {
Christoph Lameterf8891e52006-06-30 01:55:45 -0700883 __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
884 __count_vm_events(KSWAPD_STEAL, nr_freed);
Nick Piggina74609f2006-01-06 00:11:20 -0800885 } else
Christoph Lameterf8891e52006-06-30 01:55:45 -0700886 __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
Shantanu Goel918d3f92006-12-29 16:48:59 -0800887 __count_zone_vm_events(PGSTEAL, zone, nr_freed);
Nick Piggina74609f2006-01-06 00:11:20 -0800888
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800889 if (nr_taken == 0)
890 goto done;
891
Nick Piggina74609f2006-01-06 00:11:20 -0800892 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /*
894 * Put back any unfreeable pages.
895 */
896 while (!list_empty(&page_list)) {
897 page = lru_to_page(&page_list);
Nick Piggin725d7042006-09-25 23:30:55 -0700898 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800899 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 list_del(&page->lru);
901 if (PageActive(page))
902 add_page_to_active_list(zone, page);
903 else
904 add_page_to_inactive_list(zone, page);
905 if (!pagevec_add(&pvec, page)) {
906 spin_unlock_irq(&zone->lru_lock);
907 __pagevec_release(&pvec);
908 spin_lock_irq(&zone->lru_lock);
909 }
910 }
Andrew Morton69e05942006-03-22 00:08:19 -0800911 } while (nr_scanned < max_scan);
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800912 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913done:
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800914 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 pagevec_release(&pvec);
Andrew Morton05ff5132006-03-22 00:08:20 -0800916 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Martin Bligh3bb1a8522006-10-28 10:38:24 -0700919/*
920 * We are about to scan this zone at a certain priority level. If that priority
921 * level is smaller (ie: more urgent) than the previous priority, then note
922 * that priority level within the zone. This is done so that when the next
923 * process comes in to scan this zone, it will immediately start out at this
924 * priority level rather than having to build up its own scanning priority.
925 * Here, this priority affects only the reclaim-mapped threshold.
926 */
927static inline void note_zone_scanning_priority(struct zone *zone, int priority)
928{
929 if (priority < zone->prev_priority)
930 zone->prev_priority = priority;
931}
932
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700933static inline int zone_is_near_oom(struct zone *zone)
934{
Christoph Lameterc8785382007-02-10 01:43:01 -0800935 return zone->pages_scanned >= (zone_page_state(zone, NR_ACTIVE)
936 + zone_page_state(zone, NR_INACTIVE))*3;
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939/*
940 * This moves pages from the active list to the inactive list.
941 *
942 * We move them the other way if the page is referenced by one or more
943 * processes, from rmap.
944 *
945 * If the pages are mostly unmapped, the processing is fast and it is
946 * appropriate to hold zone->lru_lock across the whole operation. But if
947 * the pages are mapped, the processing is slow (page_referenced()) so we
948 * should drop zone->lru_lock around each page. It's impossible to balance
949 * this, so instead we remove the pages from the LRU while processing them.
950 * It is safe to rely on PG_active against the non-LRU pages in here because
951 * nobody will play with that bit on a non-LRU page.
952 *
953 * The downside is that we have to touch page->_count against each page.
954 * But we had to alter page->flags anyway.
955 */
Andrew Morton1742f192006-03-22 00:08:21 -0800956static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
Martin Blighbbdb3962006-10-28 10:38:25 -0700957 struct scan_control *sc, int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Andrew Morton69e05942006-03-22 00:08:19 -0800959 unsigned long pgmoved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int pgdeactivate = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800961 unsigned long pgscanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 LIST_HEAD(l_hold); /* The pages which were snipped off */
963 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
964 LIST_HEAD(l_active); /* Pages to go onto the active_list */
965 struct page *page;
966 struct pagevec pvec;
967 int reclaim_mapped = 0;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800968
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800969 if (sc->may_swap) {
Christoph Lameter2903fb12006-02-11 17:55:55 -0800970 long mapped_ratio;
971 long distress;
972 long swap_tendency;
Andrea Arcangeli4106f832007-10-16 01:25:42 -0700973 long imbalance;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800974
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700975 if (zone_is_near_oom(zone))
976 goto force_reclaim_mapped;
977
Christoph Lameter2903fb12006-02-11 17:55:55 -0800978 /*
979 * `distress' is a measure of how much trouble we're having
980 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
981 */
Martin Blighbbdb3962006-10-28 10:38:25 -0700982 distress = 100 >> min(zone->prev_priority, priority);
Christoph Lameter2903fb12006-02-11 17:55:55 -0800983
984 /*
985 * The point of this algorithm is to decide when to start
986 * reclaiming mapped memory instead of just pagecache. Work out
987 * how much memory
988 * is mapped.
989 */
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700990 mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
991 global_page_state(NR_ANON_PAGES)) * 100) /
Christoph Lameterbf02cf42006-06-30 01:55:36 -0700992 vm_total_pages;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800993
994 /*
995 * Now decide how much we really want to unmap some pages. The
996 * mapped ratio is downgraded - just because there's a lot of
997 * mapped memory doesn't necessarily mean that page reclaim
998 * isn't succeeding.
999 *
1000 * The distress ratio is important - we don't want to start
1001 * going oom.
1002 *
1003 * A 100% value of vm_swappiness overrides this algorithm
1004 * altogether.
1005 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001006 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
Christoph Lameter2903fb12006-02-11 17:55:55 -08001007
1008 /*
Andrea Arcangeli4106f832007-10-16 01:25:42 -07001009 * If there's huge imbalance between active and inactive
1010 * (think active 100 times larger than inactive) we should
1011 * become more permissive, or the system will take too much
1012 * cpu before it start swapping during memory pressure.
1013 * Distress is about avoiding early-oom, this is about
1014 * making swappiness graceful despite setting it to low
1015 * values.
1016 *
1017 * Avoid div by zero with nr_inactive+1, and max resulting
1018 * value is vm_total_pages.
1019 */
1020 imbalance = zone_page_state(zone, NR_ACTIVE);
1021 imbalance /= zone_page_state(zone, NR_INACTIVE) + 1;
1022
1023 /*
1024 * Reduce the effect of imbalance if swappiness is low,
1025 * this means for a swappiness very low, the imbalance
1026 * must be much higher than 100 for this logic to make
1027 * the difference.
1028 *
1029 * Max temporary value is vm_total_pages*100.
1030 */
1031 imbalance *= (vm_swappiness + 1);
1032 imbalance /= 100;
1033
1034 /*
1035 * If not much of the ram is mapped, makes the imbalance
1036 * less relevant, it's high priority we refill the inactive
1037 * list with mapped pages only in presence of high ratio of
1038 * mapped pages.
1039 *
1040 * Max temporary value is vm_total_pages*100.
1041 */
1042 imbalance *= mapped_ratio;
1043 imbalance /= 100;
1044
1045 /* apply imbalance feedback to swap_tendency */
1046 swap_tendency += imbalance;
1047
1048 /*
Christoph Lameter2903fb12006-02-11 17:55:55 -08001049 * Now use this metric to decide whether to start moving mapped
1050 * memory onto the inactive list.
1051 */
1052 if (swap_tendency >= 100)
Nick Piggin4ff1ffb2006-09-25 23:31:28 -07001053force_reclaim_mapped:
Christoph Lameter2903fb12006-02-11 17:55:55 -08001054 reclaim_mapped = 1;
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 lru_add_drain();
1058 spin_lock_irq(&zone->lru_lock);
Balbir Singh66e17072008-02-07 00:13:56 -08001059 pgmoved = sc->isolate_pages(nr_pages, &l_hold, &pgscanned, sc->order,
1060 ISOLATE_ACTIVE, zone,
1061 sc->mem_cgroup, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 zone->pages_scanned += pgscanned;
Christoph Lameterc8785382007-02-10 01:43:01 -08001063 __mod_zone_page_state(zone, NR_ACTIVE, -pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 spin_unlock_irq(&zone->lru_lock);
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 while (!list_empty(&l_hold)) {
1067 cond_resched();
1068 page = lru_to_page(&l_hold);
1069 list_del(&page->lru);
1070 if (page_mapped(page)) {
1071 if (!reclaim_mapped ||
1072 (total_swap_pages == 0 && PageAnon(page)) ||
Balbir Singhbed71612008-02-07 00:14:01 -08001073 page_referenced(page, 0, sc->mem_cgroup)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 list_add(&page->lru, &l_active);
1075 continue;
1076 }
1077 }
1078 list_add(&page->lru, &l_inactive);
1079 }
1080
1081 pagevec_init(&pvec, 1);
1082 pgmoved = 0;
1083 spin_lock_irq(&zone->lru_lock);
1084 while (!list_empty(&l_inactive)) {
1085 page = lru_to_page(&l_inactive);
1086 prefetchw_prev_lru_page(page, &l_inactive, flags);
Nick Piggin725d7042006-09-25 23:30:55 -07001087 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -08001088 SetPageLRU(page);
Nick Piggin725d7042006-09-25 23:30:55 -07001089 VM_BUG_ON(!PageActive(page));
Nick Piggin4c84cac2006-03-22 00:08:00 -08001090 ClearPageActive(page);
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 list_move(&page->lru, &zone->inactive_list);
Balbir Singh66e17072008-02-07 00:13:56 -08001093 mem_cgroup_move_lists(page_get_page_cgroup(page), false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 pgmoved++;
1095 if (!pagevec_add(&pvec, page)) {
Christoph Lameterc8785382007-02-10 01:43:01 -08001096 __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_unlock_irq(&zone->lru_lock);
1098 pgdeactivate += pgmoved;
1099 pgmoved = 0;
1100 if (buffer_heads_over_limit)
1101 pagevec_strip(&pvec);
1102 __pagevec_release(&pvec);
1103 spin_lock_irq(&zone->lru_lock);
1104 }
1105 }
Christoph Lameterc8785382007-02-10 01:43:01 -08001106 __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 pgdeactivate += pgmoved;
1108 if (buffer_heads_over_limit) {
1109 spin_unlock_irq(&zone->lru_lock);
1110 pagevec_strip(&pvec);
1111 spin_lock_irq(&zone->lru_lock);
1112 }
1113
1114 pgmoved = 0;
1115 while (!list_empty(&l_active)) {
1116 page = lru_to_page(&l_active);
1117 prefetchw_prev_lru_page(page, &l_active, flags);
Nick Piggin725d7042006-09-25 23:30:55 -07001118 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -08001119 SetPageLRU(page);
Nick Piggin725d7042006-09-25 23:30:55 -07001120 VM_BUG_ON(!PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 list_move(&page->lru, &zone->active_list);
Balbir Singh66e17072008-02-07 00:13:56 -08001122 mem_cgroup_move_lists(page_get_page_cgroup(page), true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 pgmoved++;
1124 if (!pagevec_add(&pvec, page)) {
Christoph Lameterc8785382007-02-10 01:43:01 -08001125 __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 pgmoved = 0;
1127 spin_unlock_irq(&zone->lru_lock);
1128 __pagevec_release(&pvec);
1129 spin_lock_irq(&zone->lru_lock);
1130 }
1131 }
Christoph Lameterc8785382007-02-10 01:43:01 -08001132 __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Christoph Lameterf8891e52006-06-30 01:55:45 -07001134 __count_zone_vm_events(PGREFILL, zone, pgscanned);
1135 __count_vm_events(PGDEACTIVATE, pgdeactivate);
1136 spin_unlock_irq(&zone->lru_lock);
Nick Piggina74609f2006-01-06 00:11:20 -08001137
1138 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141/*
1142 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1143 */
Andrew Morton05ff5132006-03-22 00:08:20 -08001144static unsigned long shrink_zone(int priority, struct zone *zone,
1145 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 unsigned long nr_active;
1148 unsigned long nr_inactive;
Christoph Lameter86959492006-03-22 00:08:18 -08001149 unsigned long nr_to_scan;
Andrew Morton05ff5132006-03-22 00:08:20 -08001150 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /*
1153 * Add one to `nr_to_scan' just to make sure that the kernel will
1154 * slowly sift through the active list.
1155 */
Christoph Lameterc8785382007-02-10 01:43:01 -08001156 zone->nr_scan_active +=
1157 (zone_page_state(zone, NR_ACTIVE) >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 nr_active = zone->nr_scan_active;
1159 if (nr_active >= sc->swap_cluster_max)
1160 zone->nr_scan_active = 0;
1161 else
1162 nr_active = 0;
1163
Christoph Lameterc8785382007-02-10 01:43:01 -08001164 zone->nr_scan_inactive +=
1165 (zone_page_state(zone, NR_INACTIVE) >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 nr_inactive = zone->nr_scan_inactive;
1167 if (nr_inactive >= sc->swap_cluster_max)
1168 zone->nr_scan_inactive = 0;
1169 else
1170 nr_inactive = 0;
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 while (nr_active || nr_inactive) {
1173 if (nr_active) {
Christoph Lameter86959492006-03-22 00:08:18 -08001174 nr_to_scan = min(nr_active,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -08001176 nr_active -= nr_to_scan;
Martin Blighbbdb3962006-10-28 10:38:25 -07001177 shrink_active_list(nr_to_scan, zone, sc, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179
1180 if (nr_inactive) {
Christoph Lameter86959492006-03-22 00:08:18 -08001181 nr_to_scan = min(nr_inactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -08001183 nr_inactive -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -08001184 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
1185 sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187 }
1188
Andrew Morton232ea4d2007-02-28 20:13:21 -08001189 throttle_vm_writeout(sc->gfp_mask);
Andrew Morton05ff5132006-03-22 00:08:20 -08001190 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
1193/*
1194 * This is the direct reclaim path, for page-allocating processes. We only
1195 * try to reclaim pages from zones which will satisfy the caller's allocation
1196 * request.
1197 *
1198 * We reclaim from a zone even if that zone is over pages_high. Because:
1199 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1200 * allocation or
1201 * b) The zones may be over pages_high but they must go *over* pages_high to
1202 * satisfy the `incremental min' zone defense algorithm.
1203 *
1204 * Returns the number of reclaimed pages.
1205 *
1206 * If a zone is deemed to be full of pinned pages then just give it a light
1207 * scan then give up on it.
1208 */
Andrew Morton1742f192006-03-22 00:08:21 -08001209static unsigned long shrink_zones(int priority, struct zone **zones,
Andrew Morton05ff5132006-03-22 00:08:20 -08001210 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Andrew Morton05ff5132006-03-22 00:08:20 -08001212 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 int i;
1214
Nick Piggin408d8542006-09-25 23:31:27 -07001215 sc->all_unreclaimable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 for (i = 0; zones[i] != NULL; i++) {
1217 struct zone *zone = zones[i];
1218
Con Kolivasf3fe6512006-01-06 00:11:15 -08001219 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 continue;
1221
Paul Jackson02a0e532006-12-13 00:34:25 -08001222 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 continue;
1224
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001225 note_zone_scanning_priority(zone, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
David Rientjese815af92007-10-16 23:25:54 -07001227 if (zone_is_all_unreclaimable(zone) && priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 continue; /* Let kswapd poll it */
1229
Nick Piggin408d8542006-09-25 23:31:27 -07001230 sc->all_unreclaimable = 0;
1231
Andrew Morton05ff5132006-03-22 00:08:20 -08001232 nr_reclaimed += shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
Andrew Morton05ff5132006-03-22 00:08:20 -08001234 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237/*
1238 * This is the main entry point to direct page reclaim.
1239 *
1240 * If a full scan of the inactive list fails to free enough memory then we
1241 * are "out of memory" and something needs to be killed.
1242 *
1243 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1244 * high - the zone may be full of dirty or under-writeback pages, which this
1245 * caller can't do much about. We kick pdflush and take explicit naps in the
1246 * hope that some of these pages can be written. But if the allocating task
1247 * holds filesystem locks which prevent writeout this might not work, and the
1248 * allocation attempt will fail.
1249 */
Balbir Singh66e17072008-02-07 00:13:56 -08001250static unsigned long do_try_to_free_pages(struct zone **zones, gfp_t gfp_mask,
1251 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 int priority;
1254 int ret = 0;
Andrew Morton69e05942006-03-22 00:08:19 -08001255 unsigned long total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001256 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 struct reclaim_state *reclaim_state = current->reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 unsigned long lru_pages = 0;
1259 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Christoph Lameterf8891e52006-06-30 01:55:45 -07001261 count_vm_event(ALLOCSTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 for (i = 0; zones[i] != NULL; i++) {
1264 struct zone *zone = zones[i];
1265
Paul Jackson02a0e532006-12-13 00:34:25 -08001266 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 continue;
1268
Christoph Lameterc8785382007-02-10 01:43:01 -08001269 lru_pages += zone_page_state(zone, NR_ACTIVE)
1270 + zone_page_state(zone, NR_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272
1273 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
Balbir Singh66e17072008-02-07 00:13:56 -08001274 sc->nr_scanned = 0;
Rik van Rielf1a9ee72008-02-07 00:14:08 -08001275 sc->nr_io_pages = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001276 if (!priority)
1277 disable_swap_token();
Balbir Singh66e17072008-02-07 00:13:56 -08001278 nr_reclaimed += shrink_zones(priority, zones, sc);
1279 /*
1280 * Don't shrink slabs when reclaiming memory from
1281 * over limit cgroups
1282 */
1283 if (sc->mem_cgroup == NULL)
1284 shrink_slab(sc->nr_scanned, gfp_mask, lru_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (reclaim_state) {
Andrew Morton05ff5132006-03-22 00:08:20 -08001286 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 reclaim_state->reclaimed_slab = 0;
1288 }
Balbir Singh66e17072008-02-07 00:13:56 -08001289 total_scanned += sc->nr_scanned;
1290 if (nr_reclaimed >= sc->swap_cluster_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 ret = 1;
1292 goto out;
1293 }
1294
1295 /*
1296 * Try to write back as many pages as we just scanned. This
1297 * tends to cause slow streaming writers to write data to the
1298 * disk smoothly, at the dirtying rate, which is nice. But
1299 * that's undesirable in laptop mode, where we *want* lumpy
1300 * writeout. So in laptop mode, write out the whole world.
1301 */
Balbir Singh66e17072008-02-07 00:13:56 -08001302 if (total_scanned > sc->swap_cluster_max +
1303 sc->swap_cluster_max / 2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001304 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Balbir Singh66e17072008-02-07 00:13:56 -08001305 sc->may_writepage = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
1308 /* Take a nap, wait for some writeback to complete */
Rik van Rielf1a9ee72008-02-07 00:14:08 -08001309 if (sc->nr_scanned && priority < DEF_PRIORITY - 2 &&
1310 sc->nr_io_pages > sc->swap_cluster_max)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001311 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
Nick Piggin408d8542006-09-25 23:31:27 -07001313 /* top priority shrink_caches still had more to do? don't OOM, then */
Balbir Singh66e17072008-02-07 00:13:56 -08001314 if (!sc->all_unreclaimable && sc->mem_cgroup == NULL)
Nick Piggin408d8542006-09-25 23:31:27 -07001315 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316out:
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001317 /*
1318 * Now that we've scanned all the zones at this priority level, note
1319 * that level within the zone so that the next thread which performs
1320 * scanning of this zone will immediately start out at this priority
1321 * level. This affects only the decision whether or not to bring
1322 * mapped pages onto the inactive list.
1323 */
1324 if (priority < 0)
1325 priority = 0;
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001326 for (i = 0; zones[i] != NULL; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 struct zone *zone = zones[i];
1328
Paul Jackson02a0e532006-12-13 00:34:25 -08001329 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 continue;
1331
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001332 zone->prev_priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334 return ret;
1335}
1336
Balbir Singh66e17072008-02-07 00:13:56 -08001337unsigned long try_to_free_pages(struct zone **zones, int order, gfp_t gfp_mask)
1338{
1339 struct scan_control sc = {
1340 .gfp_mask = gfp_mask,
1341 .may_writepage = !laptop_mode,
1342 .swap_cluster_max = SWAP_CLUSTER_MAX,
1343 .may_swap = 1,
1344 .swappiness = vm_swappiness,
1345 .order = order,
1346 .mem_cgroup = NULL,
1347 .isolate_pages = isolate_pages_global,
1348 };
1349
1350 return do_try_to_free_pages(zones, gfp_mask, &sc);
1351}
1352
1353#ifdef CONFIG_CGROUP_MEM_CONT
1354
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001355unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
1356 gfp_t gfp_mask)
Balbir Singh66e17072008-02-07 00:13:56 -08001357{
1358 struct scan_control sc = {
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001359 .gfp_mask = gfp_mask,
Balbir Singh66e17072008-02-07 00:13:56 -08001360 .may_writepage = !laptop_mode,
1361 .may_swap = 1,
1362 .swap_cluster_max = SWAP_CLUSTER_MAX,
1363 .swappiness = vm_swappiness,
1364 .order = 0,
1365 .mem_cgroup = mem_cont,
1366 .isolate_pages = mem_cgroup_isolate_pages,
1367 };
1368 int node;
1369 struct zone **zones;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001370 int target_zone = gfp_zone(GFP_HIGHUSER_MOVABLE);
Balbir Singh66e17072008-02-07 00:13:56 -08001371
1372 for_each_online_node(node) {
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001373 zones = NODE_DATA(node)->node_zonelists[target_zone].zones;
Balbir Singh66e17072008-02-07 00:13:56 -08001374 if (do_try_to_free_pages(zones, sc.gfp_mask, &sc))
1375 return 1;
1376 }
1377 return 0;
1378}
1379#endif
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381/*
1382 * For kswapd, balance_pgdat() will work across all this node's zones until
1383 * they are all at pages_high.
1384 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 * Returns the number of pages which were actually freed.
1386 *
1387 * There is special handling here for zones which are full of pinned pages.
1388 * This can happen if the pages are all mlocked, or if they are all used by
1389 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1390 * What we do is to detect the case where all pages in the zone have been
1391 * scanned twice and there has been zero successful reclaim. Mark the zone as
1392 * dead and from now on, only perform a short scan. Basically we're polling
1393 * the zone for when the problem goes away.
1394 *
1395 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1396 * zones which have free_pages > pages_high, but once a zone is found to have
1397 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1398 * of the number of free pages in the lower zones. This interoperates with
1399 * the page allocator fallback scheme to ensure that aging of pages is balanced
1400 * across the zones.
1401 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001402static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 int all_zones_ok;
1405 int priority;
1406 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08001407 unsigned long total_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001408 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08001410 struct scan_control sc = {
1411 .gfp_mask = GFP_KERNEL,
1412 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001413 .swap_cluster_max = SWAP_CLUSTER_MAX,
1414 .swappiness = vm_swappiness,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001415 .order = order,
Balbir Singh66e17072008-02-07 00:13:56 -08001416 .mem_cgroup = NULL,
1417 .isolate_pages = isolate_pages_global,
Andrew Morton179e9632006-03-22 00:08:18 -08001418 };
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001419 /*
1420 * temp_priority is used to remember the scanning priority at which
1421 * this zone was successfully refilled to free_pages == pages_high.
1422 */
1423 int temp_priority[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425loop_again:
1426 total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001427 nr_reclaimed = 0;
Christoph Lameterc0bbbc72006-06-11 15:22:26 -07001428 sc.may_writepage = !laptop_mode;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001429 count_vm_event(PAGEOUTRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001431 for (i = 0; i < pgdat->nr_zones; i++)
1432 temp_priority[i] = DEF_PRIORITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1435 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1436 unsigned long lru_pages = 0;
1437
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001438 /* The swap token gets in the way of swapout... */
1439 if (!priority)
1440 disable_swap_token();
1441
Rik van Rielf1a9ee72008-02-07 00:14:08 -08001442 sc.nr_io_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 all_zones_ok = 1;
1444
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001445 /*
1446 * Scan in the highmem->dma direction for the highest
1447 * zone which needs scanning
1448 */
1449 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1450 struct zone *zone = pgdat->node_zones + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001452 if (!populated_zone(zone))
1453 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
David Rientjese815af92007-10-16 23:25:54 -07001455 if (zone_is_all_unreclaimable(zone) &&
1456 priority != DEF_PRIORITY)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001457 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001459 if (!zone_watermark_ok(zone, order, zone->pages_high,
1460 0, 0)) {
1461 end_zone = i;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08001462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Andrew Mortone1dbeda2006-12-06 20:32:01 -08001465 if (i < 0)
1466 goto out;
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 for (i = 0; i <= end_zone; i++) {
1469 struct zone *zone = pgdat->node_zones + i;
1470
Christoph Lameterc8785382007-02-10 01:43:01 -08001471 lru_pages += zone_page_state(zone, NR_ACTIVE)
1472 + zone_page_state(zone, NR_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474
1475 /*
1476 * Now scan the zone in the dma->highmem direction, stopping
1477 * at the last zone which needs scanning.
1478 *
1479 * We do this because the page allocator works in the opposite
1480 * direction. This prevents the page allocator from allocating
1481 * pages behind kswapd's direction of progress, which would
1482 * cause too much scanning of the lower zones.
1483 */
1484 for (i = 0; i <= end_zone; i++) {
1485 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001486 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Con Kolivasf3fe6512006-01-06 00:11:15 -08001488 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 continue;
1490
David Rientjese815af92007-10-16 23:25:54 -07001491 if (zone_is_all_unreclaimable(zone) &&
1492 priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 continue;
1494
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001495 if (!zone_watermark_ok(zone, order, zone->pages_high,
1496 end_zone, 0))
1497 all_zones_ok = 0;
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001498 temp_priority[i] = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 sc.nr_scanned = 0;
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001500 note_zone_scanning_priority(zone, priority);
Rik van Riel32a43302007-10-16 01:24:50 -07001501 /*
1502 * We put equal pressure on every zone, unless one
1503 * zone has way too many pages free already.
1504 */
1505 if (!zone_watermark_ok(zone, order, 8*zone->pages_high,
1506 end_zone, 0))
1507 nr_reclaimed += shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001509 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1510 lru_pages);
Andrew Morton05ff5132006-03-22 00:08:20 -08001511 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 total_scanned += sc.nr_scanned;
David Rientjese815af92007-10-16 23:25:54 -07001513 if (zone_is_all_unreclaimable(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001515 if (nr_slab == 0 && zone->pages_scanned >=
Christoph Lameterc8785382007-02-10 01:43:01 -08001516 (zone_page_state(zone, NR_ACTIVE)
1517 + zone_page_state(zone, NR_INACTIVE)) * 6)
David Rientjese815af92007-10-16 23:25:54 -07001518 zone_set_flag(zone,
1519 ZONE_ALL_UNRECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /*
1521 * If we've done a decent amount of scanning and
1522 * the reclaim ratio is low, start doing writepage
1523 * even in laptop mode
1524 */
1525 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Andrew Morton05ff5132006-03-22 00:08:20 -08001526 total_scanned > nr_reclaimed + nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 sc.may_writepage = 1;
1528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (all_zones_ok)
1530 break; /* kswapd: all done */
1531 /*
1532 * OK, kswapd is getting into trouble. Take a nap, then take
1533 * another pass across the zones.
1534 */
Rik van Rielf1a9ee72008-02-07 00:14:08 -08001535 if (total_scanned && priority < DEF_PRIORITY - 2 &&
1536 sc.nr_io_pages > sc.swap_cluster_max)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001537 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 /*
1540 * We do this so kswapd doesn't build up large priorities for
1541 * example when it is freeing in parallel with allocators. It
1542 * matches the direct reclaim path behaviour in terms of impact
1543 * on zone->*_priority.
1544 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001545 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 break;
1547 }
1548out:
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001549 /*
1550 * Note within each zone the priority level at which this zone was
1551 * brought into a happy state. So that the next thread which scans this
1552 * zone will start out at that priority level.
1553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 for (i = 0; i < pgdat->nr_zones; i++) {
1555 struct zone *zone = pgdat->node_zones + i;
1556
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001557 zone->prev_priority = temp_priority[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559 if (!all_zones_ok) {
1560 cond_resched();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001561
1562 try_to_freeze();
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 goto loop_again;
1565 }
1566
Andrew Morton05ff5132006-03-22 00:08:20 -08001567 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
1570/*
1571 * The background pageout daemon, started as a kernel thread
1572 * from the init process.
1573 *
1574 * This basically trickles out pages so that we have _some_
1575 * free memory available even if there is no other activity
1576 * that frees anything up. This is needed for things like routing
1577 * etc, where we otherwise might have all activity going on in
1578 * asynchronous contexts that cannot page things out.
1579 *
1580 * If there are applications that are active memory-allocators
1581 * (most normal use), this basically shouldn't matter.
1582 */
1583static int kswapd(void *p)
1584{
1585 unsigned long order;
1586 pg_data_t *pgdat = (pg_data_t*)p;
1587 struct task_struct *tsk = current;
1588 DEFINE_WAIT(wait);
1589 struct reclaim_state reclaim_state = {
1590 .reclaimed_slab = 0,
1591 };
1592 cpumask_t cpumask;
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 cpumask = node_to_cpumask(pgdat->node_id);
1595 if (!cpus_empty(cpumask))
1596 set_cpus_allowed(tsk, cpumask);
1597 current->reclaim_state = &reclaim_state;
1598
1599 /*
1600 * Tell the memory management that we're a "memory allocator",
1601 * and that if we need more memory we should get access to it
1602 * regardless (see "__alloc_pages()"). "kswapd" should
1603 * never get caught in the normal page freeing logic.
1604 *
1605 * (Kswapd normally doesn't need memory anyway, but sometimes
1606 * you need a small amount of memory in order to be able to
1607 * page out something else, and this flag essentially protects
1608 * us from recursively trying to free more memory as we're
1609 * trying to free the first piece of memory in the first place).
1610 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001611 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001612 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 order = 0;
1615 for ( ; ; ) {
1616 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1619 new_order = pgdat->kswapd_max_order;
1620 pgdat->kswapd_max_order = 0;
1621 if (order < new_order) {
1622 /*
1623 * Don't sleep if someone wants a larger 'order'
1624 * allocation
1625 */
1626 order = new_order;
1627 } else {
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07001628 if (!freezing(current))
1629 schedule();
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 order = pgdat->kswapd_max_order;
1632 }
1633 finish_wait(&pgdat->kswapd_wait, &wait);
1634
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07001635 if (!try_to_freeze()) {
1636 /* We can speed up thawing tasks if we don't call
1637 * balance_pgdat after returning from the refrigerator
1638 */
1639 balance_pgdat(pgdat, order);
1640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642 return 0;
1643}
1644
1645/*
1646 * A zone is low on free memory, so wake its kswapd task to service it.
1647 */
1648void wakeup_kswapd(struct zone *zone, int order)
1649{
1650 pg_data_t *pgdat;
1651
Con Kolivasf3fe6512006-01-06 00:11:15 -08001652 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return;
1654
1655 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001656 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return;
1658 if (pgdat->kswapd_max_order < order)
1659 pgdat->kswapd_max_order = order;
Paul Jackson02a0e532006-12-13 00:34:25 -08001660 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001662 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001664 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
1667#ifdef CONFIG_PM
1668/*
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001669 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1670 * from LRU lists system-wide, for given pass and priority, and returns the
1671 * number of reclaimed pages
1672 *
1673 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1674 */
Nigel Cunninghame07aa052006-12-22 01:07:21 -08001675static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
1676 int pass, struct scan_control *sc)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001677{
1678 struct zone *zone;
1679 unsigned long nr_to_scan, ret = 0;
1680
1681 for_each_zone(zone) {
1682
1683 if (!populated_zone(zone))
1684 continue;
1685
David Rientjese815af92007-10-16 23:25:54 -07001686 if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001687 continue;
1688
1689 /* For pass = 0 we don't shrink the active list */
1690 if (pass > 0) {
Christoph Lameterc8785382007-02-10 01:43:01 -08001691 zone->nr_scan_active +=
1692 (zone_page_state(zone, NR_ACTIVE) >> prio) + 1;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001693 if (zone->nr_scan_active >= nr_pages || pass > 3) {
1694 zone->nr_scan_active = 0;
Christoph Lameterc8785382007-02-10 01:43:01 -08001695 nr_to_scan = min(nr_pages,
1696 zone_page_state(zone, NR_ACTIVE));
Martin Blighbbdb3962006-10-28 10:38:25 -07001697 shrink_active_list(nr_to_scan, zone, sc, prio);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001698 }
1699 }
1700
Christoph Lameterc8785382007-02-10 01:43:01 -08001701 zone->nr_scan_inactive +=
1702 (zone_page_state(zone, NR_INACTIVE) >> prio) + 1;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001703 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1704 zone->nr_scan_inactive = 0;
Christoph Lameterc8785382007-02-10 01:43:01 -08001705 nr_to_scan = min(nr_pages,
1706 zone_page_state(zone, NR_INACTIVE));
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001707 ret += shrink_inactive_list(nr_to_scan, zone, sc);
1708 if (ret >= nr_pages)
1709 return ret;
1710 }
1711 }
1712
1713 return ret;
1714}
1715
Andrew Morton76395d32007-01-05 16:37:05 -08001716static unsigned long count_lru_pages(void)
1717{
Christoph Lameterc8785382007-02-10 01:43:01 -08001718 return global_page_state(NR_ACTIVE) + global_page_state(NR_INACTIVE);
Andrew Morton76395d32007-01-05 16:37:05 -08001719}
1720
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001721/*
1722 * Try to free `nr_pages' of memory, system-wide, and return the number of
1723 * freed pages.
1724 *
1725 * Rather than trying to age LRUs the aim is to preserve the overall
1726 * LRU order by reclaiming preferentially
1727 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 */
Andrew Morton69e05942006-03-22 00:08:19 -08001729unsigned long shrink_all_memory(unsigned long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001731 unsigned long lru_pages, nr_slab;
Andrew Morton69e05942006-03-22 00:08:19 -08001732 unsigned long ret = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001733 int pass;
1734 struct reclaim_state reclaim_state;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001735 struct scan_control sc = {
1736 .gfp_mask = GFP_KERNEL,
1737 .may_swap = 0,
1738 .swap_cluster_max = nr_pages,
1739 .may_writepage = 1,
1740 .swappiness = vm_swappiness,
Balbir Singh66e17072008-02-07 00:13:56 -08001741 .isolate_pages = isolate_pages_global,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 };
1743
1744 current->reclaim_state = &reclaim_state;
Andrew Morton69e05942006-03-22 00:08:19 -08001745
Andrew Morton76395d32007-01-05 16:37:05 -08001746 lru_pages = count_lru_pages();
Christoph Lameter972d1a72006-09-25 23:31:51 -07001747 nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001748 /* If slab caches are huge, it's better to hit them first */
1749 while (nr_slab >= lru_pages) {
1750 reclaim_state.reclaimed_slab = 0;
1751 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1752 if (!reclaim_state.reclaimed_slab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 break;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001754
1755 ret += reclaim_state.reclaimed_slab;
1756 if (ret >= nr_pages)
1757 goto out;
1758
1759 nr_slab -= reclaim_state.reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001761
1762 /*
1763 * We try to shrink LRUs in 5 passes:
1764 * 0 = Reclaim from inactive_list only
1765 * 1 = Reclaim from active list but don't reclaim mapped
1766 * 2 = 2nd pass of type 1
1767 * 3 = Reclaim mapped (normal reclaim)
1768 * 4 = 2nd pass of type 3
1769 */
1770 for (pass = 0; pass < 5; pass++) {
1771 int prio;
1772
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001773 /* Force reclaiming mapped pages in the passes #3 and #4 */
1774 if (pass > 2) {
1775 sc.may_swap = 1;
1776 sc.swappiness = 100;
1777 }
1778
1779 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1780 unsigned long nr_to_scan = nr_pages - ret;
1781
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001782 sc.nr_scanned = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001783 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1784 if (ret >= nr_pages)
1785 goto out;
1786
1787 reclaim_state.reclaimed_slab = 0;
Andrew Morton76395d32007-01-05 16:37:05 -08001788 shrink_slab(sc.nr_scanned, sc.gfp_mask,
1789 count_lru_pages());
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001790 ret += reclaim_state.reclaimed_slab;
1791 if (ret >= nr_pages)
1792 goto out;
1793
1794 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001795 congestion_wait(WRITE, HZ / 10);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001796 }
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001797 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001798
1799 /*
1800 * If ret = 0, we could not shrink LRUs, but there may be something
1801 * in slab caches
1802 */
Andrew Morton76395d32007-01-05 16:37:05 -08001803 if (!ret) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001804 do {
1805 reclaim_state.reclaimed_slab = 0;
Andrew Morton76395d32007-01-05 16:37:05 -08001806 shrink_slab(nr_pages, sc.gfp_mask, count_lru_pages());
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001807 ret += reclaim_state.reclaimed_slab;
1808 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
Andrew Morton76395d32007-01-05 16:37:05 -08001809 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001810
1811out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 current->reclaim_state = NULL;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return ret;
1815}
1816#endif
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818/* It's optimal to keep kswapds on the same CPUs as their memory, but
1819 not required for correctness. So if the last cpu in a node goes
1820 away, we get changed to run anywhere: as the first one comes back,
1821 restore their cpu bindings. */
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07001822static int __devinit cpu_callback(struct notifier_block *nfb,
Andrew Morton69e05942006-03-22 00:08:19 -08001823 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
1825 pg_data_t *pgdat;
1826 cpumask_t mask;
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07001827 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001829 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07001830 for_each_node_state(nid, N_HIGH_MEMORY) {
1831 pgdat = NODE_DATA(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 mask = node_to_cpumask(pgdat->node_id);
1833 if (any_online_cpu(mask) != NR_CPUS)
1834 /* One of our CPUs online: restore mask */
1835 set_cpus_allowed(pgdat->kswapd, mask);
1836 }
1837 }
1838 return NOTIFY_OK;
1839}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Yasunori Goto3218ae12006-06-27 02:53:33 -07001841/*
1842 * This kswapd start function will be called by init and node-hot-add.
1843 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
1844 */
1845int kswapd_run(int nid)
1846{
1847 pg_data_t *pgdat = NODE_DATA(nid);
1848 int ret = 0;
1849
1850 if (pgdat->kswapd)
1851 return 0;
1852
1853 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
1854 if (IS_ERR(pgdat->kswapd)) {
1855 /* failure at boot is fatal */
1856 BUG_ON(system_state == SYSTEM_BOOTING);
1857 printk("Failed to start kswapd on node %d\n",nid);
1858 ret = -1;
1859 }
1860 return ret;
1861}
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863static int __init kswapd_init(void)
1864{
Yasunori Goto3218ae12006-06-27 02:53:33 -07001865 int nid;
Andrew Morton69e05942006-03-22 00:08:19 -08001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 swap_setup();
Christoph Lameter9422ffb2007-10-16 01:25:31 -07001868 for_each_node_state(nid, N_HIGH_MEMORY)
Yasunori Goto3218ae12006-06-27 02:53:33 -07001869 kswapd_run(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 hotcpu_notifier(cpu_callback, 0);
1871 return 0;
1872}
1873
1874module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001875
1876#ifdef CONFIG_NUMA
1877/*
1878 * Zone reclaim mode
1879 *
1880 * If non-zero call zone_reclaim when the number of free pages falls below
1881 * the watermarks.
Christoph Lameter9eeff232006-01-18 17:42:31 -08001882 */
1883int zone_reclaim_mode __read_mostly;
1884
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001885#define RECLAIM_OFF 0
1886#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1887#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1888#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1889
Christoph Lameter9eeff232006-01-18 17:42:31 -08001890/*
Christoph Lametera92f7122006-02-01 03:05:32 -08001891 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1892 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1893 * a zone.
1894 */
1895#define ZONE_RECLAIM_PRIORITY 4
1896
Christoph Lameter9eeff232006-01-18 17:42:31 -08001897/*
Christoph Lameter96146342006-07-03 00:24:13 -07001898 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
1899 * occur.
1900 */
1901int sysctl_min_unmapped_ratio = 1;
1902
1903/*
Christoph Lameter0ff38492006-09-25 23:31:52 -07001904 * If the number of slab pages in a zone grows beyond this percentage then
1905 * slab reclaim needs to occur.
1906 */
1907int sysctl_min_slab_ratio = 5;
1908
1909/*
Christoph Lameter9eeff232006-01-18 17:42:31 -08001910 * Try to free up some pages from this zone through reclaim.
1911 */
Andrew Morton179e9632006-03-22 00:08:18 -08001912static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001913{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001914 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08001915 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001916 struct task_struct *p = current;
1917 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08001918 int priority;
Andrew Morton05ff5132006-03-22 00:08:20 -08001919 unsigned long nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001920 struct scan_control sc = {
1921 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1922 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
Andrew Morton69e05942006-03-22 00:08:19 -08001923 .swap_cluster_max = max_t(unsigned long, nr_pages,
1924 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08001925 .gfp_mask = gfp_mask,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001926 .swappiness = vm_swappiness,
Balbir Singh66e17072008-02-07 00:13:56 -08001927 .isolate_pages = isolate_pages_global,
Andrew Morton179e9632006-03-22 00:08:18 -08001928 };
Christoph Lameter83e33a42006-09-25 23:31:53 -07001929 unsigned long slab_reclaimable;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001930
1931 disable_swap_token();
Christoph Lameter9eeff232006-01-18 17:42:31 -08001932 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08001933 /*
1934 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1935 * and we also need to be able to write out pages for RECLAIM_WRITE
1936 * and RECLAIM_SWAP.
1937 */
1938 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001939 reclaim_state.reclaimed_slab = 0;
1940 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001941
Christoph Lameter0ff38492006-09-25 23:31:52 -07001942 if (zone_page_state(zone, NR_FILE_PAGES) -
1943 zone_page_state(zone, NR_FILE_MAPPED) >
1944 zone->min_unmapped_pages) {
1945 /*
1946 * Free memory by calling shrink zone with increasing
1947 * priorities until we have enough memory freed.
1948 */
1949 priority = ZONE_RECLAIM_PRIORITY;
1950 do {
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001951 note_zone_scanning_priority(zone, priority);
Christoph Lameter0ff38492006-09-25 23:31:52 -07001952 nr_reclaimed += shrink_zone(priority, zone, &sc);
1953 priority--;
1954 } while (priority >= 0 && nr_reclaimed < nr_pages);
1955 }
Christoph Lameterc84db232006-02-01 03:05:29 -08001956
Christoph Lameter83e33a42006-09-25 23:31:53 -07001957 slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1958 if (slab_reclaimable > zone->min_slab_pages) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001959 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001960 * shrink_slab() does not currently allow us to determine how
Christoph Lameter0ff38492006-09-25 23:31:52 -07001961 * many pages were freed in this zone. So we take the current
1962 * number of slab pages and shake the slab until it is reduced
1963 * by the same nr_pages that we used for reclaiming unmapped
1964 * pages.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001965 *
Christoph Lameter0ff38492006-09-25 23:31:52 -07001966 * Note that shrink_slab will free memory on all zones and may
1967 * take a long time.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001968 */
Christoph Lameter0ff38492006-09-25 23:31:52 -07001969 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
Christoph Lameter83e33a42006-09-25 23:31:53 -07001970 zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
1971 slab_reclaimable - nr_pages)
Christoph Lameter0ff38492006-09-25 23:31:52 -07001972 ;
Christoph Lameter83e33a42006-09-25 23:31:53 -07001973
1974 /*
1975 * Update nr_reclaimed by the number of slab pages we
1976 * reclaimed from this zone.
1977 */
1978 nr_reclaimed += slab_reclaimable -
1979 zone_page_state(zone, NR_SLAB_RECLAIMABLE);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001980 }
1981
Christoph Lameter9eeff232006-01-18 17:42:31 -08001982 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08001983 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Andrew Morton05ff5132006-03-22 00:08:20 -08001984 return nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001985}
Andrew Morton179e9632006-03-22 00:08:18 -08001986
1987int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1988{
Andrew Morton179e9632006-03-22 00:08:18 -08001989 int node_id;
David Rientjesd773ed62007-10-16 23:26:01 -07001990 int ret;
Andrew Morton179e9632006-03-22 00:08:18 -08001991
1992 /*
Christoph Lameter0ff38492006-09-25 23:31:52 -07001993 * Zone reclaim reclaims unmapped file backed pages and
1994 * slab pages if we are over the defined limits.
Christoph Lameter34aa1332006-06-30 01:55:37 -07001995 *
Christoph Lameter96146342006-07-03 00:24:13 -07001996 * A small portion of unmapped file backed pages is needed for
1997 * file I/O otherwise pages read by file I/O will be immediately
1998 * thrown out if the zone is overallocated. So we do not reclaim
1999 * if less than a specified percentage of the zone is used by
2000 * unmapped file backed pages.
Andrew Morton179e9632006-03-22 00:08:18 -08002001 */
Christoph Lameter34aa1332006-06-30 01:55:37 -07002002 if (zone_page_state(zone, NR_FILE_PAGES) -
Christoph Lameter0ff38492006-09-25 23:31:52 -07002003 zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
2004 && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
2005 <= zone->min_slab_pages)
Christoph Lameter96146342006-07-03 00:24:13 -07002006 return 0;
Andrew Morton179e9632006-03-22 00:08:18 -08002007
David Rientjesd773ed62007-10-16 23:26:01 -07002008 if (zone_is_all_unreclaimable(zone))
2009 return 0;
2010
Andrew Morton179e9632006-03-22 00:08:18 -08002011 /*
David Rientjesd773ed62007-10-16 23:26:01 -07002012 * Do not scan if the allocation should not be delayed.
Andrew Morton179e9632006-03-22 00:08:18 -08002013 */
David Rientjesd773ed62007-10-16 23:26:01 -07002014 if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
Andrew Morton179e9632006-03-22 00:08:18 -08002015 return 0;
2016
2017 /*
2018 * Only run zone reclaim on the local zone or on zones that do not
2019 * have associated processors. This will favor the local processor
2020 * over remote processors and spread off node memory allocations
2021 * as wide as possible.
2022 */
Christoph Lameter89fa3022006-09-25 23:31:55 -07002023 node_id = zone_to_nid(zone);
Christoph Lameter37c07082007-10-16 01:25:36 -07002024 if (node_state(node_id, N_CPU) && node_id != numa_node_id())
Andrew Morton179e9632006-03-22 00:08:18 -08002025 return 0;
David Rientjesd773ed62007-10-16 23:26:01 -07002026
2027 if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
2028 return 0;
2029 ret = __zone_reclaim(zone, gfp_mask, order);
2030 zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
2031
2032 return ret;
Andrew Morton179e9632006-03-22 00:08:18 -08002033}
Christoph Lameter9eeff232006-01-18 17:42:31 -08002034#endif