blob: bbd194630c5b0c16dd550d21b81fb2b0865c3538 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/tlbflush.h>
42#include <asm/div64.h>
43
44#include <linux/swapops.h>
45
Nick Piggin0f8053a2006-03-22 00:08:33 -080046#include "internal.h"
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048struct scan_control {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 /* Incremented by the number of inactive pages that were scanned */
50 unsigned long nr_scanned;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040053 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 int may_writepage;
56
Christoph Lameterf1fd1062006-01-18 17:42:30 -080057 /* Can pages be swapped as part of reclaim? */
58 int may_swap;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
61 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
62 * In this context, it doesn't matter that we scan the
63 * whole list at once. */
64 int swap_cluster_max;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -070065
66 int swappiness;
Nick Piggin408d8542006-09-25 23:31:27 -070067
68 int all_unreclaimable;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -070069
70 int order;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
74
75#ifdef ARCH_HAS_PREFETCH
76#define prefetch_prev_lru_page(_page, _base, _field) \
77 do { \
78 if ((_page)->lru.prev != _base) { \
79 struct page *prev; \
80 \
81 prev = lru_to_page(&(_page->lru)); \
82 prefetch(&prev->_field); \
83 } \
84 } while (0)
85#else
86#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
87#endif
88
89#ifdef ARCH_HAS_PREFETCHW
90#define prefetchw_prev_lru_page(_page, _base, _field) \
91 do { \
92 if ((_page)->lru.prev != _base) { \
93 struct page *prev; \
94 \
95 prev = lru_to_page(&(_page->lru)); \
96 prefetchw(&prev->_field); \
97 } \
98 } while (0)
99#else
100#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
101#endif
102
103/*
104 * From 0 .. 100. Higher means more swappy.
105 */
106int vm_swappiness = 60;
Andrew Mortonbd1e22b2006-06-23 02:03:47 -0700107long vm_total_pages; /* The total number of pages which the VM controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109static LIST_HEAD(shrinker_list);
110static DECLARE_RWSEM(shrinker_rwsem);
111
112/*
113 * Add a shrinker callback to be called from the vm
114 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700115void register_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700117 shrinker->nr = 0;
118 down_write(&shrinker_rwsem);
119 list_add_tail(&shrinker->list, &shrinker_list);
120 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700122EXPORT_SYMBOL(register_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124/*
125 * Remove one
126 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700127void unregister_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 down_write(&shrinker_rwsem);
130 list_del(&shrinker->list);
131 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700133EXPORT_SYMBOL(unregister_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135#define SHRINK_BATCH 128
136/*
137 * Call the shrink functions to age shrinkable caches
138 *
139 * Here we assume it costs one seek to replace a lru page and that it also
140 * takes a seek to recreate a cache object. With this in mind we age equal
141 * percentages of the lru and ageable caches. This should balance the seeks
142 * generated by these structures.
143 *
144 * If the vm encounted mapped pages on the LRU it increase the pressure on
145 * slab to avoid swapping.
146 *
147 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
148 *
149 * `lru_pages' represents the number of on-LRU pages in all the zones which
150 * are eligible for the caller's allocation attempt. It is used for balancing
151 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700152 *
153 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
Andrew Morton69e05942006-03-22 00:08:19 -0800155unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
156 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800159 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 if (scanned == 0)
162 scanned = SWAP_CLUSTER_MAX;
163
164 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700165 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 list_for_each_entry(shrinker, &shrinker_list, list) {
168 unsigned long long delta;
169 unsigned long total_scan;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700170 unsigned long max_pass = (*shrinker->shrink)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800173 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 do_div(delta, lru_pages + 1);
175 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800176 if (shrinker->nr < 0) {
177 printk(KERN_ERR "%s: nr=%ld\n",
178 __FUNCTION__, shrinker->nr);
179 shrinker->nr = max_pass;
180 }
181
182 /*
183 * Avoid risking looping forever due to too large nr value:
184 * never try to free more than twice the estimate number of
185 * freeable entries.
186 */
187 if (shrinker->nr > max_pass * 2)
188 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 total_scan = shrinker->nr;
191 shrinker->nr = 0;
192
193 while (total_scan >= SHRINK_BATCH) {
194 long this_scan = SHRINK_BATCH;
195 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700196 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Rusty Russell8e1f9362007-07-17 04:03:17 -0700198 nr_before = (*shrinker->shrink)(0, gfp_mask);
199 shrink_ret = (*shrinker->shrink)(this_scan, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (shrink_ret == -1)
201 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700202 if (shrink_ret < nr_before)
203 ret += nr_before - shrink_ret;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700204 count_vm_events(SLABS_SCANNED, this_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 total_scan -= this_scan;
206
207 cond_resched();
208 }
209
210 shrinker->nr += total_scan;
211 }
212 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700213 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216/* Called without lock on whether page is mapped, so answer is unstable */
217static inline int page_mapping_inuse(struct page *page)
218{
219 struct address_space *mapping;
220
221 /* Page is in somebody's page tables. */
222 if (page_mapped(page))
223 return 1;
224
225 /* Be more reluctant to reclaim swapcache than pagecache */
226 if (PageSwapCache(page))
227 return 1;
228
229 mapping = page_mapping(page);
230 if (!mapping)
231 return 0;
232
233 /* File is mmap'd by somebody? */
234 return mapping_mapped(mapping);
235}
236
237static inline int is_page_cache_freeable(struct page *page)
238{
239 return page_count(page) - !!PagePrivate(page) == 2;
240}
241
242static int may_write_to_queue(struct backing_dev_info *bdi)
243{
Christoph Lameter930d9152006-01-08 01:00:47 -0800244 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 1;
246 if (!bdi_write_congested(bdi))
247 return 1;
248 if (bdi == current->backing_dev_info)
249 return 1;
250 return 0;
251}
252
253/*
254 * We detected a synchronous write error writing a page out. Probably
255 * -ENOSPC. We need to propagate that into the address_space for a subsequent
256 * fsync(), msync() or close().
257 *
258 * The tricky part is that after writepage we cannot touch the mapping: nothing
259 * prevents it from being freed up. But we have a ref on the page and once
260 * that page is locked, the mapping is pinned.
261 *
262 * We're allowed to run sleeping lock_page() here because we know the caller has
263 * __GFP_FS.
264 */
265static void handle_write_error(struct address_space *mapping,
266 struct page *page, int error)
267{
268 lock_page(page);
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -0700269 if (page_mapping(page) == mapping)
270 mapping_set_error(mapping, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 unlock_page(page);
272}
273
Andy Whitcroftc661b072007-08-22 14:01:26 -0700274/* Request for sync pageout. */
275enum pageout_io {
276 PAGEOUT_IO_ASYNC,
277 PAGEOUT_IO_SYNC,
278};
279
Christoph Lameter04e62a22006-06-23 02:03:38 -0700280/* possible outcome of pageout() */
281typedef enum {
282 /* failed to write page out, page is locked */
283 PAGE_KEEP,
284 /* move page to the active list, page is locked */
285 PAGE_ACTIVATE,
286 /* page has been sent to the disk successfully, page is unlocked */
287 PAGE_SUCCESS,
288 /* page is clean and locked */
289 PAGE_CLEAN,
290} pageout_t;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
Andrew Morton1742f192006-03-22 00:08:21 -0800293 * pageout is called by shrink_page_list() for each dirty page.
294 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700296static pageout_t pageout(struct page *page, struct address_space *mapping,
297 enum pageout_io sync_writeback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 /*
300 * If the page is dirty, only perform writeback if that write
301 * will be non-blocking. To prevent this allocation from being
302 * stalled by pagecache activity. But note that there may be
303 * stalls if we need to run get_block(). We could test
304 * PagePrivate for that.
305 *
306 * If this process is currently in generic_file_write() against
307 * this page's queue, we can perform writeback even if that
308 * will block.
309 *
310 * If the page is swapcache, write it back even if that would
311 * block, for some throttling. This happens by accident, because
312 * swap_backing_dev_info is bust: it doesn't reflect the
313 * congestion state of the swapdevs. Easy to fix, if needed.
314 * See swapfile.c:page_queue_congested().
315 */
316 if (!is_page_cache_freeable(page))
317 return PAGE_KEEP;
318 if (!mapping) {
319 /*
320 * Some data journaling orphaned pages can have
321 * page->mapping == NULL while being dirty with clean buffers.
322 */
akpm@osdl.org323aca62005-04-16 15:24:06 -0700323 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (try_to_free_buffers(page)) {
325 ClearPageDirty(page);
326 printk("%s: orphaned page\n", __FUNCTION__);
327 return PAGE_CLEAN;
328 }
329 }
330 return PAGE_KEEP;
331 }
332 if (mapping->a_ops->writepage == NULL)
333 return PAGE_ACTIVATE;
334 if (!may_write_to_queue(mapping->backing_dev_info))
335 return PAGE_KEEP;
336
337 if (clear_page_dirty_for_io(page)) {
338 int res;
339 struct writeback_control wbc = {
340 .sync_mode = WB_SYNC_NONE,
341 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700342 .range_start = 0,
343 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 .nonblocking = 1,
345 .for_reclaim = 1,
346 };
347
348 SetPageReclaim(page);
349 res = mapping->a_ops->writepage(page, &wbc);
350 if (res < 0)
351 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800352 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 ClearPageReclaim(page);
354 return PAGE_ACTIVATE;
355 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700356
357 /*
358 * Wait on writeback if requested to. This happens when
359 * direct reclaiming a large contiguous area and the
360 * first attempt to free a range of pages fails.
361 */
362 if (PageWriteback(page) && sync_writeback == PAGEOUT_IO_SYNC)
363 wait_on_page_writeback(page);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (!PageWriteback(page)) {
366 /* synchronous write or broken a_ops? */
367 ClearPageReclaim(page);
368 }
Andrew Mortone129b5c2006-09-27 01:50:00 -0700369 inc_zone_page_state(page, NR_VMSCAN_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return PAGE_SUCCESS;
371 }
372
373 return PAGE_CLEAN;
374}
375
Andrew Mortona649fd92006-10-17 00:09:36 -0700376/*
377 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
378 * someone else has a ref on the page, abort and return 0. If it was
379 * successfully detached, return 1. Assumes the caller has a single ref on
380 * this page.
381 */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800382int remove_mapping(struct address_space *mapping, struct page *page)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800383{
Nick Piggin28e4d962006-09-25 23:31:23 -0700384 BUG_ON(!PageLocked(page));
385 BUG_ON(mapping != page_mapping(page));
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800386
387 write_lock_irq(&mapping->tree_lock);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800388 /*
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700389 * The non racy check for a busy page.
390 *
391 * Must be careful with the order of the tests. When someone has
392 * a ref to the page, it may be possible that they dirty it then
393 * drop the reference. So if PageDirty is tested before page_count
394 * here, then the following race may occur:
395 *
396 * get_user_pages(&page);
397 * [user mapping goes away]
398 * write_to(page);
399 * !PageDirty(page) [good]
400 * SetPageDirty(page);
401 * put_page(page);
402 * !page_count(page) [good, discard it]
403 *
404 * [oops, our write_to data is lost]
405 *
406 * Reversing the order of the tests ensures such a situation cannot
407 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
408 * load is not satisfied before that of page->_count.
409 *
410 * Note that if SetPageDirty is always performed via set_page_dirty,
411 * and thus under tree_lock, then this ordering is not required.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800412 */
413 if (unlikely(page_count(page) != 2))
414 goto cannot_free;
415 smp_rmb();
416 if (unlikely(PageDirty(page)))
417 goto cannot_free;
418
419 if (PageSwapCache(page)) {
420 swp_entry_t swap = { .val = page_private(page) };
421 __delete_from_swap_cache(page);
422 write_unlock_irq(&mapping->tree_lock);
423 swap_free(swap);
424 __put_page(page); /* The pagecache ref */
425 return 1;
426 }
427
428 __remove_from_page_cache(page);
429 write_unlock_irq(&mapping->tree_lock);
430 __put_page(page);
431 return 1;
432
433cannot_free:
434 write_unlock_irq(&mapping->tree_lock);
435 return 0;
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/*
Andrew Morton1742f192006-03-22 00:08:21 -0800439 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
Andrew Morton1742f192006-03-22 00:08:21 -0800441static unsigned long shrink_page_list(struct list_head *page_list,
Andy Whitcroftc661b072007-08-22 14:01:26 -0700442 struct scan_control *sc,
443 enum pageout_io sync_writeback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 LIST_HEAD(ret_pages);
446 struct pagevec freed_pvec;
447 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800448 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 cond_resched();
451
452 pagevec_init(&freed_pvec, 1);
453 while (!list_empty(page_list)) {
454 struct address_space *mapping;
455 struct page *page;
456 int may_enter_fs;
457 int referenced;
458
459 cond_resched();
460
461 page = lru_to_page(page_list);
462 list_del(&page->lru);
463
464 if (TestSetPageLocked(page))
465 goto keep;
466
Nick Piggin725d7042006-09-25 23:30:55 -0700467 VM_BUG_ON(PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800470
471 if (!sc->may_swap && page_mapped(page))
472 goto keep_locked;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* Double the slab pressure for mapped and swapcache pages */
475 if (page_mapped(page) || PageSwapCache(page))
476 sc->nr_scanned++;
477
Andy Whitcroftc661b072007-08-22 14:01:26 -0700478 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
479 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
480
481 if (PageWriteback(page)) {
482 /*
483 * Synchronous reclaim is performed in two passes,
484 * first an asynchronous pass over the list to
485 * start parallel writeback, and a second synchronous
486 * pass to wait for the IO to complete. Wait here
487 * for any page for which writeback has already
488 * started.
489 */
490 if (sync_writeback == PAGEOUT_IO_SYNC && may_enter_fs)
491 wait_on_page_writeback(page);
492 else
493 goto keep_locked;
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800496 referenced = page_referenced(page, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /* In active use or really unfreeable? Activate it. */
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700498 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER &&
499 referenced && page_mapping_inuse(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 goto activate_locked;
501
502#ifdef CONFIG_SWAP
503 /*
504 * Anonymous process memory has backing store?
505 * Try to allocate it some swap space here.
506 */
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800507 if (PageAnon(page) && !PageSwapCache(page))
Christoph Lameter1480a542006-01-08 01:00:53 -0800508 if (!add_to_swap(page, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 goto activate_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510#endif /* CONFIG_SWAP */
511
512 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 /*
515 * The page is mapped into the page tables of one or more
516 * processes. Try to unmap it here.
517 */
518 if (page_mapped(page) && mapping) {
Christoph Lametera48d07a2006-02-01 03:05:38 -0800519 switch (try_to_unmap(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 case SWAP_FAIL:
521 goto activate_locked;
522 case SWAP_AGAIN:
523 goto keep_locked;
524 case SWAP_SUCCESS:
525 ; /* try to free the page below */
526 }
527 }
528
529 if (PageDirty(page)) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700530 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && referenced)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto keep_locked;
532 if (!may_enter_fs)
533 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800534 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto keep_locked;
536
537 /* Page is dirty, try to write it out here */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700538 switch (pageout(page, mapping, sync_writeback)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 case PAGE_KEEP:
540 goto keep_locked;
541 case PAGE_ACTIVATE:
542 goto activate_locked;
543 case PAGE_SUCCESS:
544 if (PageWriteback(page) || PageDirty(page))
545 goto keep;
546 /*
547 * A synchronous write - probably a ramdisk. Go
548 * ahead and try to reclaim the page.
549 */
550 if (TestSetPageLocked(page))
551 goto keep;
552 if (PageDirty(page) || PageWriteback(page))
553 goto keep_locked;
554 mapping = page_mapping(page);
555 case PAGE_CLEAN:
556 ; /* try to free the page below */
557 }
558 }
559
560 /*
561 * If the page has buffers, try to free the buffer mappings
562 * associated with this page. If we succeed we try to free
563 * the page as well.
564 *
565 * We do this even if the page is PageDirty().
566 * try_to_release_page() does not perform I/O, but it is
567 * possible for a page to have PageDirty set, but it is actually
568 * clean (all its buffers are clean). This happens if the
569 * buffers were written out directly, with submit_bh(). ext3
570 * will do this, as well as the blockdev mapping.
571 * try_to_release_page() will discover that cleanness and will
572 * drop the buffers and mark the page clean - it can be freed.
573 *
574 * Rarely, pages can have buffers and no ->mapping. These are
575 * the pages which were not successfully invalidated in
576 * truncate_complete_page(). We try to drop those buffers here
577 * and if that worked, and the page is no longer mapped into
578 * process address space (page_count == 1) it can be freed.
579 * Otherwise, leave the page on the LRU so it is swappable.
580 */
581 if (PagePrivate(page)) {
582 if (!try_to_release_page(page, sc->gfp_mask))
583 goto activate_locked;
584 if (!mapping && page_count(page) == 1)
585 goto free_it;
586 }
587
Nick Piggin28e4d962006-09-25 23:31:23 -0700588 if (!mapping || !remove_mapping(mapping, page))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800589 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591free_it:
592 unlock_page(page);
Andrew Morton05ff5132006-03-22 00:08:20 -0800593 nr_reclaimed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (!pagevec_add(&freed_pvec, page))
595 __pagevec_release_nonlru(&freed_pvec);
596 continue;
597
598activate_locked:
599 SetPageActive(page);
600 pgactivate++;
601keep_locked:
602 unlock_page(page);
603keep:
604 list_add(&page->lru, &ret_pages);
Nick Piggin725d7042006-09-25 23:30:55 -0700605 VM_BUG_ON(PageLRU(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 list_splice(&ret_pages, page_list);
608 if (pagevec_count(&freed_pvec))
609 __pagevec_release_nonlru(&freed_pvec);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700610 count_vm_events(PGACTIVATE, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800611 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700614/* LRU Isolation modes. */
615#define ISOLATE_INACTIVE 0 /* Isolate inactive pages. */
616#define ISOLATE_ACTIVE 1 /* Isolate active pages. */
617#define ISOLATE_BOTH 2 /* Isolate both active and inactive pages. */
618
619/*
620 * Attempt to remove the specified page from its LRU. Only take this page
621 * if it is of the appropriate PageActive status. Pages which are being
622 * freed elsewhere are also ignored.
623 *
624 * page: page to consider
625 * mode: one of the LRU isolation modes defined above
626 *
627 * returns 0 on success, -ve errno on failure.
628 */
629static int __isolate_lru_page(struct page *page, int mode)
630{
631 int ret = -EINVAL;
632
633 /* Only take pages on the LRU. */
634 if (!PageLRU(page))
635 return ret;
636
637 /*
638 * When checking the active state, we need to be sure we are
639 * dealing with comparible boolean values. Take the logical not
640 * of each.
641 */
642 if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode))
643 return ret;
644
645 ret = -EBUSY;
646 if (likely(get_page_unless_zero(page))) {
647 /*
648 * Be careful not to clear PageLRU until after we're
649 * sure the page is not being freed elsewhere -- the
650 * page release code relies on it.
651 */
652 ClearPageLRU(page);
653 ret = 0;
654 }
655
656 return ret;
657}
658
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800659/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 * zone->lru_lock is heavily contended. Some of the functions that
661 * shrink the lists perform better by taking out a batch of pages
662 * and working on them outside the LRU lock.
663 *
664 * For pagecache intensive workloads, this function is the hottest
665 * spot in the kernel (apart from copy_*_user functions).
666 *
667 * Appropriate locks must be held before calling this function.
668 *
669 * @nr_to_scan: The number of pages to look through on the list.
670 * @src: The LRU list to pull pages off.
671 * @dst: The temp list to put pages on to.
672 * @scanned: The number of pages that were scanned.
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700673 * @order: The caller's attempted allocation order
674 * @mode: One of the LRU isolation modes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 *
676 * returns how many pages were moved onto *@dst.
677 */
Andrew Morton69e05942006-03-22 00:08:19 -0800678static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
679 struct list_head *src, struct list_head *dst,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700680 unsigned long *scanned, int order, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Andrew Morton69e05942006-03-22 00:08:19 -0800682 unsigned long nr_taken = 0;
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800683 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800685 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700686 struct page *page;
687 unsigned long pfn;
688 unsigned long end_pfn;
689 unsigned long page_pfn;
690 int zone_id;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 page = lru_to_page(src);
693 prefetchw_prev_lru_page(page, src, flags);
694
Nick Piggin725d7042006-09-25 23:30:55 -0700695 VM_BUG_ON(!PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800696
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700697 switch (__isolate_lru_page(page, mode)) {
698 case 0:
699 list_move(&page->lru, dst);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800700 nr_taken++;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700701 break;
Nick Piggin46453a62006-03-22 00:07:58 -0800702
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700703 case -EBUSY:
704 /* else it is being freed elsewhere */
705 list_move(&page->lru, src);
706 continue;
707
708 default:
709 BUG();
710 }
711
712 if (!order)
713 continue;
714
715 /*
716 * Attempt to take all pages in the order aligned region
717 * surrounding the tag page. Only take those pages of
718 * the same active state as that tag page. We may safely
719 * round the target page pfn down to the requested order
720 * as the mem_map is guarenteed valid out to MAX_ORDER,
721 * where that page is in a different zone we will detect
722 * it from its zone id and abort this block scan.
723 */
724 zone_id = page_zone_id(page);
725 page_pfn = page_to_pfn(page);
726 pfn = page_pfn & ~((1 << order) - 1);
727 end_pfn = pfn + (1 << order);
728 for (; pfn < end_pfn; pfn++) {
729 struct page *cursor_page;
730
731 /* The target page is in the block, ignore it. */
732 if (unlikely(pfn == page_pfn))
733 continue;
734
735 /* Avoid holes within the zone. */
736 if (unlikely(!pfn_valid_within(pfn)))
737 break;
738
739 cursor_page = pfn_to_page(pfn);
740 /* Check that we have not crossed a zone boundary. */
741 if (unlikely(page_zone_id(cursor_page) != zone_id))
742 continue;
743 switch (__isolate_lru_page(cursor_page, mode)) {
744 case 0:
745 list_move(&cursor_page->lru, dst);
746 nr_taken++;
747 scan++;
748 break;
749
750 case -EBUSY:
751 /* else it is being freed elsewhere */
752 list_move(&cursor_page->lru, src);
753 default:
754 break;
755 }
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
759 *scanned = scan;
760 return nr_taken;
761}
762
763/*
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700764 * clear_active_flags() is a helper for shrink_active_list(), clearing
765 * any active bits from the pages in the list.
766 */
767static unsigned long clear_active_flags(struct list_head *page_list)
768{
769 int nr_active = 0;
770 struct page *page;
771
772 list_for_each_entry(page, page_list, lru)
773 if (PageActive(page)) {
774 ClearPageActive(page);
775 nr_active++;
776 }
777
778 return nr_active;
779}
780
781/*
Andrew Morton1742f192006-03-22 00:08:21 -0800782 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
783 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
Andrew Morton1742f192006-03-22 00:08:21 -0800785static unsigned long shrink_inactive_list(unsigned long max_scan,
786 struct zone *zone, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 LIST_HEAD(page_list);
789 struct pagevec pvec;
Andrew Morton69e05942006-03-22 00:08:19 -0800790 unsigned long nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800791 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 pagevec_init(&pvec, 1);
794
795 lru_add_drain();
796 spin_lock_irq(&zone->lru_lock);
Andrew Morton69e05942006-03-22 00:08:19 -0800797 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -0800799 unsigned long nr_taken;
800 unsigned long nr_scan;
801 unsigned long nr_freed;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700802 unsigned long nr_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 nr_taken = isolate_lru_pages(sc->swap_cluster_max,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700805 &zone->inactive_list,
806 &page_list, &nr_scan, sc->order,
807 (sc->order > PAGE_ALLOC_COSTLY_ORDER)?
808 ISOLATE_BOTH : ISOLATE_INACTIVE);
809 nr_active = clear_active_flags(&page_list);
Andy Whitcrofte9187bd2007-08-22 14:01:25 -0700810 __count_vm_events(PGDEACTIVATE, nr_active);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700811
812 __mod_zone_page_state(zone, NR_ACTIVE, -nr_active);
813 __mod_zone_page_state(zone, NR_INACTIVE,
814 -(nr_taken - nr_active));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 zone->pages_scanned += nr_scan;
816 spin_unlock_irq(&zone->lru_lock);
817
Andrew Morton69e05942006-03-22 00:08:19 -0800818 nr_scanned += nr_scan;
Andy Whitcroftc661b072007-08-22 14:01:26 -0700819 nr_freed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC);
820
821 /*
822 * If we are direct reclaiming for contiguous pages and we do
823 * not reclaim everything in the list, try again and wait
824 * for IO to complete. This will stall high-order allocations
825 * but that should be acceptable to the caller
826 */
827 if (nr_freed < nr_taken && !current_is_kswapd() &&
828 sc->order > PAGE_ALLOC_COSTLY_ORDER) {
829 congestion_wait(WRITE, HZ/10);
830
831 /*
832 * The attempt at page out may have made some
833 * of the pages active, mark them inactive again.
834 */
835 nr_active = clear_active_flags(&page_list);
836 count_vm_events(PGDEACTIVATE, nr_active);
837
838 nr_freed += shrink_page_list(&page_list, sc,
839 PAGEOUT_IO_SYNC);
840 }
841
Andrew Morton05ff5132006-03-22 00:08:20 -0800842 nr_reclaimed += nr_freed;
Nick Piggina74609f2006-01-06 00:11:20 -0800843 local_irq_disable();
844 if (current_is_kswapd()) {
Christoph Lameterf8891e52006-06-30 01:55:45 -0700845 __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
846 __count_vm_events(KSWAPD_STEAL, nr_freed);
Nick Piggina74609f2006-01-06 00:11:20 -0800847 } else
Christoph Lameterf8891e52006-06-30 01:55:45 -0700848 __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
Shantanu Goel918d3f92006-12-29 16:48:59 -0800849 __count_zone_vm_events(PGSTEAL, zone, nr_freed);
Nick Piggina74609f2006-01-06 00:11:20 -0800850
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800851 if (nr_taken == 0)
852 goto done;
853
Nick Piggina74609f2006-01-06 00:11:20 -0800854 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
856 * Put back any unfreeable pages.
857 */
858 while (!list_empty(&page_list)) {
859 page = lru_to_page(&page_list);
Nick Piggin725d7042006-09-25 23:30:55 -0700860 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800861 SetPageLRU(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 list_del(&page->lru);
863 if (PageActive(page))
864 add_page_to_active_list(zone, page);
865 else
866 add_page_to_inactive_list(zone, page);
867 if (!pagevec_add(&pvec, page)) {
868 spin_unlock_irq(&zone->lru_lock);
869 __pagevec_release(&pvec);
870 spin_lock_irq(&zone->lru_lock);
871 }
872 }
Andrew Morton69e05942006-03-22 00:08:19 -0800873 } while (nr_scanned < max_scan);
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800874 spin_unlock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875done:
Wu Fengguangfb8d14e2006-03-22 00:08:28 -0800876 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 pagevec_release(&pvec);
Andrew Morton05ff5132006-03-22 00:08:20 -0800878 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Martin Bligh3bb1a8522006-10-28 10:38:24 -0700881/*
882 * We are about to scan this zone at a certain priority level. If that priority
883 * level is smaller (ie: more urgent) than the previous priority, then note
884 * that priority level within the zone. This is done so that when the next
885 * process comes in to scan this zone, it will immediately start out at this
886 * priority level rather than having to build up its own scanning priority.
887 * Here, this priority affects only the reclaim-mapped threshold.
888 */
889static inline void note_zone_scanning_priority(struct zone *zone, int priority)
890{
891 if (priority < zone->prev_priority)
892 zone->prev_priority = priority;
893}
894
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700895static inline int zone_is_near_oom(struct zone *zone)
896{
Christoph Lameterc8785382007-02-10 01:43:01 -0800897 return zone->pages_scanned >= (zone_page_state(zone, NR_ACTIVE)
898 + zone_page_state(zone, NR_INACTIVE))*3;
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901/*
902 * This moves pages from the active list to the inactive list.
903 *
904 * We move them the other way if the page is referenced by one or more
905 * processes, from rmap.
906 *
907 * If the pages are mostly unmapped, the processing is fast and it is
908 * appropriate to hold zone->lru_lock across the whole operation. But if
909 * the pages are mapped, the processing is slow (page_referenced()) so we
910 * should drop zone->lru_lock around each page. It's impossible to balance
911 * this, so instead we remove the pages from the LRU while processing them.
912 * It is safe to rely on PG_active against the non-LRU pages in here because
913 * nobody will play with that bit on a non-LRU page.
914 *
915 * The downside is that we have to touch page->_count against each page.
916 * But we had to alter page->flags anyway.
917 */
Andrew Morton1742f192006-03-22 00:08:21 -0800918static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
Martin Blighbbdb3962006-10-28 10:38:25 -0700919 struct scan_control *sc, int priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Andrew Morton69e05942006-03-22 00:08:19 -0800921 unsigned long pgmoved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 int pgdeactivate = 0;
Andrew Morton69e05942006-03-22 00:08:19 -0800923 unsigned long pgscanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 LIST_HEAD(l_hold); /* The pages which were snipped off */
925 LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
926 LIST_HEAD(l_active); /* Pages to go onto the active_list */
927 struct page *page;
928 struct pagevec pvec;
929 int reclaim_mapped = 0;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800930
Christoph Lameter6e5ef1a2006-03-22 00:08:45 -0800931 if (sc->may_swap) {
Christoph Lameter2903fb12006-02-11 17:55:55 -0800932 long mapped_ratio;
933 long distress;
934 long swap_tendency;
Andrea Arcangeli4106f832007-10-16 01:25:42 -0700935 long imbalance;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800936
Nick Piggin4ff1ffb2006-09-25 23:31:28 -0700937 if (zone_is_near_oom(zone))
938 goto force_reclaim_mapped;
939
Christoph Lameter2903fb12006-02-11 17:55:55 -0800940 /*
941 * `distress' is a measure of how much trouble we're having
942 * reclaiming pages. 0 -> no problems. 100 -> great trouble.
943 */
Martin Blighbbdb3962006-10-28 10:38:25 -0700944 distress = 100 >> min(zone->prev_priority, priority);
Christoph Lameter2903fb12006-02-11 17:55:55 -0800945
946 /*
947 * The point of this algorithm is to decide when to start
948 * reclaiming mapped memory instead of just pagecache. Work out
949 * how much memory
950 * is mapped.
951 */
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700952 mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
953 global_page_state(NR_ANON_PAGES)) * 100) /
Christoph Lameterbf02cf42006-06-30 01:55:36 -0700954 vm_total_pages;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800955
956 /*
957 * Now decide how much we really want to unmap some pages. The
958 * mapped ratio is downgraded - just because there's a lot of
959 * mapped memory doesn't necessarily mean that page reclaim
960 * isn't succeeding.
961 *
962 * The distress ratio is important - we don't want to start
963 * going oom.
964 *
965 * A 100% value of vm_swappiness overrides this algorithm
966 * altogether.
967 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -0700968 swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
Christoph Lameter2903fb12006-02-11 17:55:55 -0800969
970 /*
Andrea Arcangeli4106f832007-10-16 01:25:42 -0700971 * If there's huge imbalance between active and inactive
972 * (think active 100 times larger than inactive) we should
973 * become more permissive, or the system will take too much
974 * cpu before it start swapping during memory pressure.
975 * Distress is about avoiding early-oom, this is about
976 * making swappiness graceful despite setting it to low
977 * values.
978 *
979 * Avoid div by zero with nr_inactive+1, and max resulting
980 * value is vm_total_pages.
981 */
982 imbalance = zone_page_state(zone, NR_ACTIVE);
983 imbalance /= zone_page_state(zone, NR_INACTIVE) + 1;
984
985 /*
986 * Reduce the effect of imbalance if swappiness is low,
987 * this means for a swappiness very low, the imbalance
988 * must be much higher than 100 for this logic to make
989 * the difference.
990 *
991 * Max temporary value is vm_total_pages*100.
992 */
993 imbalance *= (vm_swappiness + 1);
994 imbalance /= 100;
995
996 /*
997 * If not much of the ram is mapped, makes the imbalance
998 * less relevant, it's high priority we refill the inactive
999 * list with mapped pages only in presence of high ratio of
1000 * mapped pages.
1001 *
1002 * Max temporary value is vm_total_pages*100.
1003 */
1004 imbalance *= mapped_ratio;
1005 imbalance /= 100;
1006
1007 /* apply imbalance feedback to swap_tendency */
1008 swap_tendency += imbalance;
1009
1010 /*
Christoph Lameter2903fb12006-02-11 17:55:55 -08001011 * Now use this metric to decide whether to start moving mapped
1012 * memory onto the inactive list.
1013 */
1014 if (swap_tendency >= 100)
Nick Piggin4ff1ffb2006-09-25 23:31:28 -07001015force_reclaim_mapped:
Christoph Lameter2903fb12006-02-11 17:55:55 -08001016 reclaim_mapped = 1;
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 lru_add_drain();
1020 spin_lock_irq(&zone->lru_lock);
1021 pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001022 &l_hold, &pgscanned, sc->order, ISOLATE_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 zone->pages_scanned += pgscanned;
Christoph Lameterc8785382007-02-10 01:43:01 -08001024 __mod_zone_page_state(zone, NR_ACTIVE, -pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 spin_unlock_irq(&zone->lru_lock);
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 while (!list_empty(&l_hold)) {
1028 cond_resched();
1029 page = lru_to_page(&l_hold);
1030 list_del(&page->lru);
1031 if (page_mapped(page)) {
1032 if (!reclaim_mapped ||
1033 (total_swap_pages == 0 && PageAnon(page)) ||
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001034 page_referenced(page, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 list_add(&page->lru, &l_active);
1036 continue;
1037 }
1038 }
1039 list_add(&page->lru, &l_inactive);
1040 }
1041
1042 pagevec_init(&pvec, 1);
1043 pgmoved = 0;
1044 spin_lock_irq(&zone->lru_lock);
1045 while (!list_empty(&l_inactive)) {
1046 page = lru_to_page(&l_inactive);
1047 prefetchw_prev_lru_page(page, &l_inactive, flags);
Nick Piggin725d7042006-09-25 23:30:55 -07001048 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -08001049 SetPageLRU(page);
Nick Piggin725d7042006-09-25 23:30:55 -07001050 VM_BUG_ON(!PageActive(page));
Nick Piggin4c84cac2006-03-22 00:08:00 -08001051 ClearPageActive(page);
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 list_move(&page->lru, &zone->inactive_list);
1054 pgmoved++;
1055 if (!pagevec_add(&pvec, page)) {
Christoph Lameterc8785382007-02-10 01:43:01 -08001056 __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 spin_unlock_irq(&zone->lru_lock);
1058 pgdeactivate += pgmoved;
1059 pgmoved = 0;
1060 if (buffer_heads_over_limit)
1061 pagevec_strip(&pvec);
1062 __pagevec_release(&pvec);
1063 spin_lock_irq(&zone->lru_lock);
1064 }
1065 }
Christoph Lameterc8785382007-02-10 01:43:01 -08001066 __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 pgdeactivate += pgmoved;
1068 if (buffer_heads_over_limit) {
1069 spin_unlock_irq(&zone->lru_lock);
1070 pagevec_strip(&pvec);
1071 spin_lock_irq(&zone->lru_lock);
1072 }
1073
1074 pgmoved = 0;
1075 while (!list_empty(&l_active)) {
1076 page = lru_to_page(&l_active);
1077 prefetchw_prev_lru_page(page, &l_active, flags);
Nick Piggin725d7042006-09-25 23:30:55 -07001078 VM_BUG_ON(PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -08001079 SetPageLRU(page);
Nick Piggin725d7042006-09-25 23:30:55 -07001080 VM_BUG_ON(!PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 list_move(&page->lru, &zone->active_list);
1082 pgmoved++;
1083 if (!pagevec_add(&pvec, page)) {
Christoph Lameterc8785382007-02-10 01:43:01 -08001084 __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 pgmoved = 0;
1086 spin_unlock_irq(&zone->lru_lock);
1087 __pagevec_release(&pvec);
1088 spin_lock_irq(&zone->lru_lock);
1089 }
1090 }
Christoph Lameterc8785382007-02-10 01:43:01 -08001091 __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Christoph Lameterf8891e52006-06-30 01:55:45 -07001093 __count_zone_vm_events(PGREFILL, zone, pgscanned);
1094 __count_vm_events(PGDEACTIVATE, pgdeactivate);
1095 spin_unlock_irq(&zone->lru_lock);
Nick Piggina74609f2006-01-06 00:11:20 -08001096
1097 pagevec_release(&pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
1100/*
1101 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1102 */
Andrew Morton05ff5132006-03-22 00:08:20 -08001103static unsigned long shrink_zone(int priority, struct zone *zone,
1104 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 unsigned long nr_active;
1107 unsigned long nr_inactive;
Christoph Lameter86959492006-03-22 00:08:18 -08001108 unsigned long nr_to_scan;
Andrew Morton05ff5132006-03-22 00:08:20 -08001109 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Martin Hicks53e9a612005-09-03 15:54:51 -07001111 atomic_inc(&zone->reclaim_in_progress);
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 /*
1114 * Add one to `nr_to_scan' just to make sure that the kernel will
1115 * slowly sift through the active list.
1116 */
Christoph Lameterc8785382007-02-10 01:43:01 -08001117 zone->nr_scan_active +=
1118 (zone_page_state(zone, NR_ACTIVE) >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 nr_active = zone->nr_scan_active;
1120 if (nr_active >= sc->swap_cluster_max)
1121 zone->nr_scan_active = 0;
1122 else
1123 nr_active = 0;
1124
Christoph Lameterc8785382007-02-10 01:43:01 -08001125 zone->nr_scan_inactive +=
1126 (zone_page_state(zone, NR_INACTIVE) >> priority) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 nr_inactive = zone->nr_scan_inactive;
1128 if (nr_inactive >= sc->swap_cluster_max)
1129 zone->nr_scan_inactive = 0;
1130 else
1131 nr_inactive = 0;
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 while (nr_active || nr_inactive) {
1134 if (nr_active) {
Christoph Lameter86959492006-03-22 00:08:18 -08001135 nr_to_scan = min(nr_active,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -08001137 nr_active -= nr_to_scan;
Martin Blighbbdb3962006-10-28 10:38:25 -07001138 shrink_active_list(nr_to_scan, zone, sc, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140
1141 if (nr_inactive) {
Christoph Lameter86959492006-03-22 00:08:18 -08001142 nr_to_scan = min(nr_inactive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 (unsigned long)sc->swap_cluster_max);
Christoph Lameter86959492006-03-22 00:08:18 -08001144 nr_inactive -= nr_to_scan;
Andrew Morton1742f192006-03-22 00:08:21 -08001145 nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
1146 sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148 }
1149
Andrew Morton232ea4d2007-02-28 20:13:21 -08001150 throttle_vm_writeout(sc->gfp_mask);
Martin Hicks53e9a612005-09-03 15:54:51 -07001151
1152 atomic_dec(&zone->reclaim_in_progress);
Andrew Morton05ff5132006-03-22 00:08:20 -08001153 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156/*
1157 * This is the direct reclaim path, for page-allocating processes. We only
1158 * try to reclaim pages from zones which will satisfy the caller's allocation
1159 * request.
1160 *
1161 * We reclaim from a zone even if that zone is over pages_high. Because:
1162 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1163 * allocation or
1164 * b) The zones may be over pages_high but they must go *over* pages_high to
1165 * satisfy the `incremental min' zone defense algorithm.
1166 *
1167 * Returns the number of reclaimed pages.
1168 *
1169 * If a zone is deemed to be full of pinned pages then just give it a light
1170 * scan then give up on it.
1171 */
Andrew Morton1742f192006-03-22 00:08:21 -08001172static unsigned long shrink_zones(int priority, struct zone **zones,
Andrew Morton05ff5132006-03-22 00:08:20 -08001173 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Andrew Morton05ff5132006-03-22 00:08:20 -08001175 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int i;
1177
Nick Piggin408d8542006-09-25 23:31:27 -07001178 sc->all_unreclaimable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 for (i = 0; zones[i] != NULL; i++) {
1180 struct zone *zone = zones[i];
1181
Con Kolivasf3fe6512006-01-06 00:11:15 -08001182 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 continue;
1184
Paul Jackson02a0e532006-12-13 00:34:25 -08001185 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 continue;
1187
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001188 note_zone_scanning_priority(zone, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Christoph Lameter86959492006-03-22 00:08:18 -08001190 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 continue; /* Let kswapd poll it */
1192
Nick Piggin408d8542006-09-25 23:31:27 -07001193 sc->all_unreclaimable = 0;
1194
Andrew Morton05ff5132006-03-22 00:08:20 -08001195 nr_reclaimed += shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Andrew Morton05ff5132006-03-22 00:08:20 -08001197 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
1200/*
1201 * This is the main entry point to direct page reclaim.
1202 *
1203 * If a full scan of the inactive list fails to free enough memory then we
1204 * are "out of memory" and something needs to be killed.
1205 *
1206 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1207 * high - the zone may be full of dirty or under-writeback pages, which this
1208 * caller can't do much about. We kick pdflush and take explicit naps in the
1209 * hope that some of these pages can be written. But if the allocating task
1210 * holds filesystem locks which prevent writeout this might not work, and the
1211 * allocation attempt will fail.
1212 */
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001213unsigned long try_to_free_pages(struct zone **zones, int order, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
1215 int priority;
1216 int ret = 0;
Andrew Morton69e05942006-03-22 00:08:19 -08001217 unsigned long total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001218 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 struct reclaim_state *reclaim_state = current->reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 unsigned long lru_pages = 0;
1221 int i;
Andrew Morton179e9632006-03-22 00:08:18 -08001222 struct scan_control sc = {
1223 .gfp_mask = gfp_mask,
1224 .may_writepage = !laptop_mode,
1225 .swap_cluster_max = SWAP_CLUSTER_MAX,
1226 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001227 .swappiness = vm_swappiness,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001228 .order = order,
Andrew Morton179e9632006-03-22 00:08:18 -08001229 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Christoph Lameterf8891e52006-06-30 01:55:45 -07001231 count_vm_event(ALLOCSTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 for (i = 0; zones[i] != NULL; i++) {
1234 struct zone *zone = zones[i];
1235
Paul Jackson02a0e532006-12-13 00:34:25 -08001236 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 continue;
1238
Christoph Lameterc8785382007-02-10 01:43:01 -08001239 lru_pages += zone_page_state(zone, NR_ACTIVE)
1240 + zone_page_state(zone, NR_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242
1243 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 sc.nr_scanned = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001245 if (!priority)
1246 disable_swap_token();
Andrew Morton1742f192006-03-22 00:08:21 -08001247 nr_reclaimed += shrink_zones(priority, zones, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
1249 if (reclaim_state) {
Andrew Morton05ff5132006-03-22 00:08:20 -08001250 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 reclaim_state->reclaimed_slab = 0;
1252 }
1253 total_scanned += sc.nr_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001254 if (nr_reclaimed >= sc.swap_cluster_max) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 ret = 1;
1256 goto out;
1257 }
1258
1259 /*
1260 * Try to write back as many pages as we just scanned. This
1261 * tends to cause slow streaming writers to write data to the
1262 * disk smoothly, at the dirtying rate, which is nice. But
1263 * that's undesirable in laptop mode, where we *want* lumpy
1264 * writeout. So in laptop mode, write out the whole world.
1265 */
Andrew Morton179e9632006-03-22 00:08:18 -08001266 if (total_scanned > sc.swap_cluster_max +
1267 sc.swap_cluster_max / 2) {
Pekka J Enberg687a21c2005-06-28 20:44:55 -07001268 wakeup_pdflush(laptop_mode ? 0 : total_scanned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 sc.may_writepage = 1;
1270 }
1271
1272 /* Take a nap, wait for some writeback to complete */
1273 if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001274 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Nick Piggin408d8542006-09-25 23:31:27 -07001276 /* top priority shrink_caches still had more to do? don't OOM, then */
1277 if (!sc.all_unreclaimable)
1278 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279out:
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001280 /*
1281 * Now that we've scanned all the zones at this priority level, note
1282 * that level within the zone so that the next thread which performs
1283 * scanning of this zone will immediately start out at this priority
1284 * level. This affects only the decision whether or not to bring
1285 * mapped pages onto the inactive list.
1286 */
1287 if (priority < 0)
1288 priority = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 for (i = 0; zones[i] != 0; i++) {
1290 struct zone *zone = zones[i];
1291
Paul Jackson02a0e532006-12-13 00:34:25 -08001292 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 continue;
1294
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001295 zone->prev_priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297 return ret;
1298}
1299
1300/*
1301 * For kswapd, balance_pgdat() will work across all this node's zones until
1302 * they are all at pages_high.
1303 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 * Returns the number of pages which were actually freed.
1305 *
1306 * There is special handling here for zones which are full of pinned pages.
1307 * This can happen if the pages are all mlocked, or if they are all used by
1308 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1309 * What we do is to detect the case where all pages in the zone have been
1310 * scanned twice and there has been zero successful reclaim. Mark the zone as
1311 * dead and from now on, only perform a short scan. Basically we're polling
1312 * the zone for when the problem goes away.
1313 *
1314 * kswapd scans the zones in the highmem->normal->dma direction. It skips
1315 * zones which have free_pages > pages_high, but once a zone is found to have
1316 * free_pages <= pages_high, we scan that zone and the lower zones regardless
1317 * of the number of free pages in the lower zones. This interoperates with
1318 * the page allocator fallback scheme to ensure that aging of pages is balanced
1319 * across the zones.
1320 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001321static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 int all_zones_ok;
1324 int priority;
1325 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08001326 unsigned long total_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001327 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08001329 struct scan_control sc = {
1330 .gfp_mask = GFP_KERNEL,
1331 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001332 .swap_cluster_max = SWAP_CLUSTER_MAX,
1333 .swappiness = vm_swappiness,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001334 .order = order,
Andrew Morton179e9632006-03-22 00:08:18 -08001335 };
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001336 /*
1337 * temp_priority is used to remember the scanning priority at which
1338 * this zone was successfully refilled to free_pages == pages_high.
1339 */
1340 int temp_priority[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342loop_again:
1343 total_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001344 nr_reclaimed = 0;
Christoph Lameterc0bbbc72006-06-11 15:22:26 -07001345 sc.may_writepage = !laptop_mode;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001346 count_vm_event(PAGEOUTRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001348 for (i = 0; i < pgdat->nr_zones; i++)
1349 temp_priority[i] = DEF_PRIORITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1352 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1353 unsigned long lru_pages = 0;
1354
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001355 /* The swap token gets in the way of swapout... */
1356 if (!priority)
1357 disable_swap_token();
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 all_zones_ok = 1;
1360
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001361 /*
1362 * Scan in the highmem->dma direction for the highest
1363 * zone which needs scanning
1364 */
1365 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
1366 struct zone *zone = pgdat->node_zones + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001368 if (!populated_zone(zone))
1369 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001371 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1372 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001374 if (!zone_watermark_ok(zone, order, zone->pages_high,
1375 0, 0)) {
1376 end_zone = i;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08001377 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Andrew Mortone1dbeda2006-12-06 20:32:01 -08001380 if (i < 0)
1381 goto out;
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 for (i = 0; i <= end_zone; i++) {
1384 struct zone *zone = pgdat->node_zones + i;
1385
Christoph Lameterc8785382007-02-10 01:43:01 -08001386 lru_pages += zone_page_state(zone, NR_ACTIVE)
1387 + zone_page_state(zone, NR_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389
1390 /*
1391 * Now scan the zone in the dma->highmem direction, stopping
1392 * at the last zone which needs scanning.
1393 *
1394 * We do this because the page allocator works in the opposite
1395 * direction. This prevents the page allocator from allocating
1396 * pages behind kswapd's direction of progress, which would
1397 * cause too much scanning of the lower zones.
1398 */
1399 for (i = 0; i <= end_zone; i++) {
1400 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001401 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Con Kolivasf3fe6512006-01-06 00:11:15 -08001403 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 continue;
1405
1406 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
1407 continue;
1408
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001409 if (!zone_watermark_ok(zone, order, zone->pages_high,
1410 end_zone, 0))
1411 all_zones_ok = 0;
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001412 temp_priority[i] = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 sc.nr_scanned = 0;
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001414 note_zone_scanning_priority(zone, priority);
Rik van Riel32a43302007-10-16 01:24:50 -07001415 /*
1416 * We put equal pressure on every zone, unless one
1417 * zone has way too many pages free already.
1418 */
1419 if (!zone_watermark_ok(zone, order, 8*zone->pages_high,
1420 end_zone, 0))
1421 nr_reclaimed += shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001423 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1424 lru_pages);
Andrew Morton05ff5132006-03-22 00:08:20 -08001425 nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 total_scanned += sc.nr_scanned;
1427 if (zone->all_unreclaimable)
1428 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07001429 if (nr_slab == 0 && zone->pages_scanned >=
Christoph Lameterc8785382007-02-10 01:43:01 -08001430 (zone_page_state(zone, NR_ACTIVE)
1431 + zone_page_state(zone, NR_INACTIVE)) * 6)
1432 zone->all_unreclaimable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /*
1434 * If we've done a decent amount of scanning and
1435 * the reclaim ratio is low, start doing writepage
1436 * even in laptop mode
1437 */
1438 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Andrew Morton05ff5132006-03-22 00:08:20 -08001439 total_scanned > nr_reclaimed + nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 sc.may_writepage = 1;
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (all_zones_ok)
1443 break; /* kswapd: all done */
1444 /*
1445 * OK, kswapd is getting into trouble. Take a nap, then take
1446 * another pass across the zones.
1447 */
1448 if (total_scanned && priority < DEF_PRIORITY - 2)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001449 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 /*
1452 * We do this so kswapd doesn't build up large priorities for
1453 * example when it is freeing in parallel with allocators. It
1454 * matches the direct reclaim path behaviour in terms of impact
1455 * on zone->*_priority.
1456 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001457 if (nr_reclaimed >= SWAP_CLUSTER_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 break;
1459 }
1460out:
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001461 /*
1462 * Note within each zone the priority level at which this zone was
1463 * brought into a happy state. So that the next thread which scans this
1464 * zone will start out at that priority level.
1465 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 for (i = 0; i < pgdat->nr_zones; i++) {
1467 struct zone *zone = pgdat->node_zones + i;
1468
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001469 zone->prev_priority = temp_priority[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471 if (!all_zones_ok) {
1472 cond_resched();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001473
1474 try_to_freeze();
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 goto loop_again;
1477 }
1478
Andrew Morton05ff5132006-03-22 00:08:20 -08001479 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482/*
1483 * The background pageout daemon, started as a kernel thread
1484 * from the init process.
1485 *
1486 * This basically trickles out pages so that we have _some_
1487 * free memory available even if there is no other activity
1488 * that frees anything up. This is needed for things like routing
1489 * etc, where we otherwise might have all activity going on in
1490 * asynchronous contexts that cannot page things out.
1491 *
1492 * If there are applications that are active memory-allocators
1493 * (most normal use), this basically shouldn't matter.
1494 */
1495static int kswapd(void *p)
1496{
1497 unsigned long order;
1498 pg_data_t *pgdat = (pg_data_t*)p;
1499 struct task_struct *tsk = current;
1500 DEFINE_WAIT(wait);
1501 struct reclaim_state reclaim_state = {
1502 .reclaimed_slab = 0,
1503 };
1504 cpumask_t cpumask;
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 cpumask = node_to_cpumask(pgdat->node_id);
1507 if (!cpus_empty(cpumask))
1508 set_cpus_allowed(tsk, cpumask);
1509 current->reclaim_state = &reclaim_state;
1510
1511 /*
1512 * Tell the memory management that we're a "memory allocator",
1513 * and that if we need more memory we should get access to it
1514 * regardless (see "__alloc_pages()"). "kswapd" should
1515 * never get caught in the normal page freeing logic.
1516 *
1517 * (Kswapd normally doesn't need memory anyway, but sometimes
1518 * you need a small amount of memory in order to be able to
1519 * page out something else, and this flag essentially protects
1520 * us from recursively trying to free more memory as we're
1521 * trying to free the first piece of memory in the first place).
1522 */
Christoph Lameter930d9152006-01-08 01:00:47 -08001523 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001524 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 order = 0;
1527 for ( ; ; ) {
1528 unsigned long new_order;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
1531 new_order = pgdat->kswapd_max_order;
1532 pgdat->kswapd_max_order = 0;
1533 if (order < new_order) {
1534 /*
1535 * Don't sleep if someone wants a larger 'order'
1536 * allocation
1537 */
1538 order = new_order;
1539 } else {
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07001540 if (!freezing(current))
1541 schedule();
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 order = pgdat->kswapd_max_order;
1544 }
1545 finish_wait(&pgdat->kswapd_wait, &wait);
1546
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07001547 if (!try_to_freeze()) {
1548 /* We can speed up thawing tasks if we don't call
1549 * balance_pgdat after returning from the refrigerator
1550 */
1551 balance_pgdat(pgdat, order);
1552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
1554 return 0;
1555}
1556
1557/*
1558 * A zone is low on free memory, so wake its kswapd task to service it.
1559 */
1560void wakeup_kswapd(struct zone *zone, int order)
1561{
1562 pg_data_t *pgdat;
1563
Con Kolivasf3fe6512006-01-06 00:11:15 -08001564 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return;
1566
1567 pgdat = zone->zone_pgdat;
Rohit Seth7fb1d9f2005-11-13 16:06:43 -08001568 if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return;
1570 if (pgdat->kswapd_max_order < order)
1571 pgdat->kswapd_max_order = order;
Paul Jackson02a0e532006-12-13 00:34:25 -08001572 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001574 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07001576 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
1579#ifdef CONFIG_PM
1580/*
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001581 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
1582 * from LRU lists system-wide, for given pass and priority, and returns the
1583 * number of reclaimed pages
1584 *
1585 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
1586 */
Nigel Cunninghame07aa052006-12-22 01:07:21 -08001587static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
1588 int pass, struct scan_control *sc)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001589{
1590 struct zone *zone;
1591 unsigned long nr_to_scan, ret = 0;
1592
1593 for_each_zone(zone) {
1594
1595 if (!populated_zone(zone))
1596 continue;
1597
1598 if (zone->all_unreclaimable && prio != DEF_PRIORITY)
1599 continue;
1600
1601 /* For pass = 0 we don't shrink the active list */
1602 if (pass > 0) {
Christoph Lameterc8785382007-02-10 01:43:01 -08001603 zone->nr_scan_active +=
1604 (zone_page_state(zone, NR_ACTIVE) >> prio) + 1;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001605 if (zone->nr_scan_active >= nr_pages || pass > 3) {
1606 zone->nr_scan_active = 0;
Christoph Lameterc8785382007-02-10 01:43:01 -08001607 nr_to_scan = min(nr_pages,
1608 zone_page_state(zone, NR_ACTIVE));
Martin Blighbbdb3962006-10-28 10:38:25 -07001609 shrink_active_list(nr_to_scan, zone, sc, prio);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001610 }
1611 }
1612
Christoph Lameterc8785382007-02-10 01:43:01 -08001613 zone->nr_scan_inactive +=
1614 (zone_page_state(zone, NR_INACTIVE) >> prio) + 1;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001615 if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
1616 zone->nr_scan_inactive = 0;
Christoph Lameterc8785382007-02-10 01:43:01 -08001617 nr_to_scan = min(nr_pages,
1618 zone_page_state(zone, NR_INACTIVE));
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001619 ret += shrink_inactive_list(nr_to_scan, zone, sc);
1620 if (ret >= nr_pages)
1621 return ret;
1622 }
1623 }
1624
1625 return ret;
1626}
1627
Andrew Morton76395d32007-01-05 16:37:05 -08001628static unsigned long count_lru_pages(void)
1629{
Christoph Lameterc8785382007-02-10 01:43:01 -08001630 return global_page_state(NR_ACTIVE) + global_page_state(NR_INACTIVE);
Andrew Morton76395d32007-01-05 16:37:05 -08001631}
1632
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001633/*
1634 * Try to free `nr_pages' of memory, system-wide, and return the number of
1635 * freed pages.
1636 *
1637 * Rather than trying to age LRUs the aim is to preserve the overall
1638 * LRU order by reclaiming preferentially
1639 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 */
Andrew Morton69e05942006-03-22 00:08:19 -08001641unsigned long shrink_all_memory(unsigned long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001643 unsigned long lru_pages, nr_slab;
Andrew Morton69e05942006-03-22 00:08:19 -08001644 unsigned long ret = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001645 int pass;
1646 struct reclaim_state reclaim_state;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001647 struct scan_control sc = {
1648 .gfp_mask = GFP_KERNEL,
1649 .may_swap = 0,
1650 .swap_cluster_max = nr_pages,
1651 .may_writepage = 1,
1652 .swappiness = vm_swappiness,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 };
1654
1655 current->reclaim_state = &reclaim_state;
Andrew Morton69e05942006-03-22 00:08:19 -08001656
Andrew Morton76395d32007-01-05 16:37:05 -08001657 lru_pages = count_lru_pages();
Christoph Lameter972d1a72006-09-25 23:31:51 -07001658 nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001659 /* If slab caches are huge, it's better to hit them first */
1660 while (nr_slab >= lru_pages) {
1661 reclaim_state.reclaimed_slab = 0;
1662 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
1663 if (!reclaim_state.reclaimed_slab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 break;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001665
1666 ret += reclaim_state.reclaimed_slab;
1667 if (ret >= nr_pages)
1668 goto out;
1669
1670 nr_slab -= reclaim_state.reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001672
1673 /*
1674 * We try to shrink LRUs in 5 passes:
1675 * 0 = Reclaim from inactive_list only
1676 * 1 = Reclaim from active list but don't reclaim mapped
1677 * 2 = 2nd pass of type 1
1678 * 3 = Reclaim mapped (normal reclaim)
1679 * 4 = 2nd pass of type 3
1680 */
1681 for (pass = 0; pass < 5; pass++) {
1682 int prio;
1683
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001684 /* Force reclaiming mapped pages in the passes #3 and #4 */
1685 if (pass > 2) {
1686 sc.may_swap = 1;
1687 sc.swappiness = 100;
1688 }
1689
1690 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
1691 unsigned long nr_to_scan = nr_pages - ret;
1692
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001693 sc.nr_scanned = 0;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001694 ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
1695 if (ret >= nr_pages)
1696 goto out;
1697
1698 reclaim_state.reclaimed_slab = 0;
Andrew Morton76395d32007-01-05 16:37:05 -08001699 shrink_slab(sc.nr_scanned, sc.gfp_mask,
1700 count_lru_pages());
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001701 ret += reclaim_state.reclaimed_slab;
1702 if (ret >= nr_pages)
1703 goto out;
1704
1705 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001706 congestion_wait(WRITE, HZ / 10);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001707 }
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08001708 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001709
1710 /*
1711 * If ret = 0, we could not shrink LRUs, but there may be something
1712 * in slab caches
1713 */
Andrew Morton76395d32007-01-05 16:37:05 -08001714 if (!ret) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001715 do {
1716 reclaim_state.reclaimed_slab = 0;
Andrew Morton76395d32007-01-05 16:37:05 -08001717 shrink_slab(nr_pages, sc.gfp_mask, count_lru_pages());
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001718 ret += reclaim_state.reclaimed_slab;
1719 } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
Andrew Morton76395d32007-01-05 16:37:05 -08001720 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001721
1722out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 current->reclaim_state = NULL;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return ret;
1726}
1727#endif
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729/* It's optimal to keep kswapds on the same CPUs as their memory, but
1730 not required for correctness. So if the last cpu in a node goes
1731 away, we get changed to run anywhere: as the first one comes back,
1732 restore their cpu bindings. */
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07001733static int __devinit cpu_callback(struct notifier_block *nfb,
Andrew Morton69e05942006-03-22 00:08:19 -08001734 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 pg_data_t *pgdat;
1737 cpumask_t mask;
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07001738 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001740 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07001741 for_each_node_state(nid, N_HIGH_MEMORY) {
1742 pgdat = NODE_DATA(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 mask = node_to_cpumask(pgdat->node_id);
1744 if (any_online_cpu(mask) != NR_CPUS)
1745 /* One of our CPUs online: restore mask */
1746 set_cpus_allowed(pgdat->kswapd, mask);
1747 }
1748 }
1749 return NOTIFY_OK;
1750}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Yasunori Goto3218ae12006-06-27 02:53:33 -07001752/*
1753 * This kswapd start function will be called by init and node-hot-add.
1754 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
1755 */
1756int kswapd_run(int nid)
1757{
1758 pg_data_t *pgdat = NODE_DATA(nid);
1759 int ret = 0;
1760
1761 if (pgdat->kswapd)
1762 return 0;
1763
1764 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
1765 if (IS_ERR(pgdat->kswapd)) {
1766 /* failure at boot is fatal */
1767 BUG_ON(system_state == SYSTEM_BOOTING);
1768 printk("Failed to start kswapd on node %d\n",nid);
1769 ret = -1;
1770 }
1771 return ret;
1772}
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774static int __init kswapd_init(void)
1775{
Yasunori Goto3218ae12006-06-27 02:53:33 -07001776 int nid;
Andrew Morton69e05942006-03-22 00:08:19 -08001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 swap_setup();
Christoph Lameter9422ffb2007-10-16 01:25:31 -07001779 for_each_node_state(nid, N_HIGH_MEMORY)
Yasunori Goto3218ae12006-06-27 02:53:33 -07001780 kswapd_run(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 hotcpu_notifier(cpu_callback, 0);
1782 return 0;
1783}
1784
1785module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001786
1787#ifdef CONFIG_NUMA
1788/*
1789 * Zone reclaim mode
1790 *
1791 * If non-zero call zone_reclaim when the number of free pages falls below
1792 * the watermarks.
Christoph Lameter9eeff232006-01-18 17:42:31 -08001793 */
1794int zone_reclaim_mode __read_mostly;
1795
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08001796#define RECLAIM_OFF 0
1797#define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
1798#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
1799#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
1800
Christoph Lameter9eeff232006-01-18 17:42:31 -08001801/*
Christoph Lametera92f7122006-02-01 03:05:32 -08001802 * Priority for ZONE_RECLAIM. This determines the fraction of pages
1803 * of a node considered for each zone_reclaim. 4 scans 1/16th of
1804 * a zone.
1805 */
1806#define ZONE_RECLAIM_PRIORITY 4
1807
Christoph Lameter9eeff232006-01-18 17:42:31 -08001808/*
Christoph Lameter96146342006-07-03 00:24:13 -07001809 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
1810 * occur.
1811 */
1812int sysctl_min_unmapped_ratio = 1;
1813
1814/*
Christoph Lameter0ff38492006-09-25 23:31:52 -07001815 * If the number of slab pages in a zone grows beyond this percentage then
1816 * slab reclaim needs to occur.
1817 */
1818int sysctl_min_slab_ratio = 5;
1819
1820/*
Christoph Lameter9eeff232006-01-18 17:42:31 -08001821 * Try to free up some pages from this zone through reclaim.
1822 */
Andrew Morton179e9632006-03-22 00:08:18 -08001823static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08001824{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001825 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08001826 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001827 struct task_struct *p = current;
1828 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08001829 int priority;
Andrew Morton05ff5132006-03-22 00:08:20 -08001830 unsigned long nr_reclaimed = 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001831 struct scan_control sc = {
1832 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
1833 .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
Andrew Morton69e05942006-03-22 00:08:19 -08001834 .swap_cluster_max = max_t(unsigned long, nr_pages,
1835 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08001836 .gfp_mask = gfp_mask,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001837 .swappiness = vm_swappiness,
Andrew Morton179e9632006-03-22 00:08:18 -08001838 };
Christoph Lameter83e33a42006-09-25 23:31:53 -07001839 unsigned long slab_reclaimable;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001840
1841 disable_swap_token();
Christoph Lameter9eeff232006-01-18 17:42:31 -08001842 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08001843 /*
1844 * We need to be able to allocate from the reserves for RECLAIM_SWAP
1845 * and we also need to be able to write out pages for RECLAIM_WRITE
1846 * and RECLAIM_SWAP.
1847 */
1848 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001849 reclaim_state.reclaimed_slab = 0;
1850 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08001851
Christoph Lameter0ff38492006-09-25 23:31:52 -07001852 if (zone_page_state(zone, NR_FILE_PAGES) -
1853 zone_page_state(zone, NR_FILE_MAPPED) >
1854 zone->min_unmapped_pages) {
1855 /*
1856 * Free memory by calling shrink zone with increasing
1857 * priorities until we have enough memory freed.
1858 */
1859 priority = ZONE_RECLAIM_PRIORITY;
1860 do {
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001861 note_zone_scanning_priority(zone, priority);
Christoph Lameter0ff38492006-09-25 23:31:52 -07001862 nr_reclaimed += shrink_zone(priority, zone, &sc);
1863 priority--;
1864 } while (priority >= 0 && nr_reclaimed < nr_pages);
1865 }
Christoph Lameterc84db232006-02-01 03:05:29 -08001866
Christoph Lameter83e33a42006-09-25 23:31:53 -07001867 slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
1868 if (slab_reclaimable > zone->min_slab_pages) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001869 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08001870 * shrink_slab() does not currently allow us to determine how
Christoph Lameter0ff38492006-09-25 23:31:52 -07001871 * many pages were freed in this zone. So we take the current
1872 * number of slab pages and shake the slab until it is reduced
1873 * by the same nr_pages that we used for reclaiming unmapped
1874 * pages.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001875 *
Christoph Lameter0ff38492006-09-25 23:31:52 -07001876 * Note that shrink_slab will free memory on all zones and may
1877 * take a long time.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001878 */
Christoph Lameter0ff38492006-09-25 23:31:52 -07001879 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
Christoph Lameter83e33a42006-09-25 23:31:53 -07001880 zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
1881 slab_reclaimable - nr_pages)
Christoph Lameter0ff38492006-09-25 23:31:52 -07001882 ;
Christoph Lameter83e33a42006-09-25 23:31:53 -07001883
1884 /*
1885 * Update nr_reclaimed by the number of slab pages we
1886 * reclaimed from this zone.
1887 */
1888 nr_reclaimed += slab_reclaimable -
1889 zone_page_state(zone, NR_SLAB_RECLAIMABLE);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08001890 }
1891
Christoph Lameter9eeff232006-01-18 17:42:31 -08001892 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08001893 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Andrew Morton05ff5132006-03-22 00:08:20 -08001894 return nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08001895}
Andrew Morton179e9632006-03-22 00:08:18 -08001896
1897int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
1898{
Andrew Morton179e9632006-03-22 00:08:18 -08001899 int node_id;
1900
1901 /*
Christoph Lameter0ff38492006-09-25 23:31:52 -07001902 * Zone reclaim reclaims unmapped file backed pages and
1903 * slab pages if we are over the defined limits.
Christoph Lameter34aa1332006-06-30 01:55:37 -07001904 *
Christoph Lameter96146342006-07-03 00:24:13 -07001905 * A small portion of unmapped file backed pages is needed for
1906 * file I/O otherwise pages read by file I/O will be immediately
1907 * thrown out if the zone is overallocated. So we do not reclaim
1908 * if less than a specified percentage of the zone is used by
1909 * unmapped file backed pages.
Andrew Morton179e9632006-03-22 00:08:18 -08001910 */
Christoph Lameter34aa1332006-06-30 01:55:37 -07001911 if (zone_page_state(zone, NR_FILE_PAGES) -
Christoph Lameter0ff38492006-09-25 23:31:52 -07001912 zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
1913 && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
1914 <= zone->min_slab_pages)
Christoph Lameter96146342006-07-03 00:24:13 -07001915 return 0;
Andrew Morton179e9632006-03-22 00:08:18 -08001916
1917 /*
1918 * Avoid concurrent zone reclaims, do not reclaim in a zone that does
1919 * not have reclaimable pages and if we should not delay the allocation
1920 * then do not scan.
1921 */
1922 if (!(gfp_mask & __GFP_WAIT) ||
1923 zone->all_unreclaimable ||
1924 atomic_read(&zone->reclaim_in_progress) > 0 ||
1925 (current->flags & PF_MEMALLOC))
1926 return 0;
1927
1928 /*
1929 * Only run zone reclaim on the local zone or on zones that do not
1930 * have associated processors. This will favor the local processor
1931 * over remote processors and spread off node memory allocations
1932 * as wide as possible.
1933 */
Christoph Lameter89fa3022006-09-25 23:31:55 -07001934 node_id = zone_to_nid(zone);
Christoph Lameter37c07082007-10-16 01:25:36 -07001935 if (node_state(node_id, N_CPU) && node_id != numa_node_id())
Andrew Morton179e9632006-03-22 00:08:18 -08001936 return 0;
1937 return __zone_reclaim(zone, gfp_mask, order);
1938}
Christoph Lameter9eeff232006-01-18 17:42:31 -08001939#endif