blob: 7b0d5c784c7e481720173239977860217d7d2f67 [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>
Keika Kobayashi873b4772008-07-25 01:48:52 -070041#include <linux/delayacct.h>
Lee Schermerhornaf936a12008-10-18 20:26:53 -070042#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <asm/tlbflush.h>
45#include <asm/div64.h>
46
47#include <linux/swapops.h>
48
Nick Piggin0f8053a2006-03-22 00:08:33 -080049#include "internal.h"
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051struct scan_control {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 /* Incremented by the number of inactive pages that were scanned */
53 unsigned long nr_scanned;
54
Rik van Riela79311c2009-01-06 14:40:01 -080055 /* Number of pages freed so far during a call to shrink_zones() */
56 unsigned long nr_reclaimed;
57
KOSAKI Motohiro22fba332009-12-14 17:59:10 -080058 /* How many pages shrink_list() should reclaim */
59 unsigned long nr_to_reclaim;
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040062 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 int may_writepage;
65
Johannes Weinera6dc60f2009-03-31 15:19:30 -070066 /* Can mapped pages be reclaimed? */
67 int may_unmap;
Christoph Lameterf1fd1062006-01-18 17:42:30 -080068
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -070069 /* Can pages be swapped as part of reclaim? */
70 int may_swap;
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 /* This context's SWAP_CLUSTER_MAX. If freeing memory for
73 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
74 * In this context, it doesn't matter that we scan the
75 * whole list at once. */
76 int swap_cluster_max;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -070077
78 int swappiness;
Nick Piggin408d8542006-09-25 23:31:27 -070079
80 int all_unreclaimable;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -070081
82 int order;
Balbir Singh66e17072008-02-07 00:13:56 -080083
84 /* Which cgroup do we reclaim from */
85 struct mem_cgroup *mem_cgroup;
86
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -070087 /*
88 * Nodemask of nodes allowed by the caller. If NULL, all nodes
89 * are scanned.
90 */
91 nodemask_t *nodemask;
92
Balbir Singh66e17072008-02-07 00:13:56 -080093 /* Pluggable isolate pages callback */
94 unsigned long (*isolate_pages)(unsigned long nr, struct list_head *dst,
95 unsigned long *scanned, int order, int mode,
96 struct zone *z, struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -070097 int active, int file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
101
102#ifdef ARCH_HAS_PREFETCH
103#define prefetch_prev_lru_page(_page, _base, _field) \
104 do { \
105 if ((_page)->lru.prev != _base) { \
106 struct page *prev; \
107 \
108 prev = lru_to_page(&(_page->lru)); \
109 prefetch(&prev->_field); \
110 } \
111 } while (0)
112#else
113#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
114#endif
115
116#ifdef ARCH_HAS_PREFETCHW
117#define prefetchw_prev_lru_page(_page, _base, _field) \
118 do { \
119 if ((_page)->lru.prev != _base) { \
120 struct page *prev; \
121 \
122 prev = lru_to_page(&(_page->lru)); \
123 prefetchw(&prev->_field); \
124 } \
125 } while (0)
126#else
127#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
128#endif
129
130/*
131 * From 0 .. 100. Higher means more swappy.
132 */
133int vm_swappiness = 60;
Andrew Mortonbd1e22b2006-06-23 02:03:47 -0700134long vm_total_pages; /* The total number of pages which the VM controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136static LIST_HEAD(shrinker_list);
137static DECLARE_RWSEM(shrinker_rwsem);
138
Balbir Singh00f0b822008-03-04 14:28:39 -0800139#ifdef CONFIG_CGROUP_MEM_RES_CTLR
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -0800140#define scanning_global_lru(sc) (!(sc)->mem_cgroup)
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -0800141#else
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -0800142#define scanning_global_lru(sc) (1)
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -0800143#endif
144
KOSAKI Motohiro6e901572009-01-07 18:08:15 -0800145static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone,
146 struct scan_control *sc)
147{
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -0800148 if (!scanning_global_lru(sc))
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800149 return mem_cgroup_get_reclaim_stat(sc->mem_cgroup, zone);
150
KOSAKI Motohiro6e901572009-01-07 18:08:15 -0800151 return &zone->reclaim_stat;
152}
153
Vincent Li0b217672009-09-21 17:03:09 -0700154static unsigned long zone_nr_lru_pages(struct zone *zone,
155 struct scan_control *sc, enum lru_list lru)
KOSAKI Motohiroc9f299d2009-01-07 18:08:16 -0800156{
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -0800157 if (!scanning_global_lru(sc))
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800158 return mem_cgroup_zone_nr_pages(sc->mem_cgroup, zone, lru);
159
KOSAKI Motohiroc9f299d2009-01-07 18:08:16 -0800160 return zone_page_state(zone, NR_LRU_BASE + lru);
161}
162
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
165 * Add a shrinker callback to be called from the vm
166 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700167void register_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700169 shrinker->nr = 0;
170 down_write(&shrinker_rwsem);
171 list_add_tail(&shrinker->list, &shrinker_list);
172 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700174EXPORT_SYMBOL(register_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/*
177 * Remove one
178 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700179void unregister_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 down_write(&shrinker_rwsem);
182 list_del(&shrinker->list);
183 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700185EXPORT_SYMBOL(unregister_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187#define SHRINK_BATCH 128
188/*
189 * Call the shrink functions to age shrinkable caches
190 *
191 * Here we assume it costs one seek to replace a lru page and that it also
192 * takes a seek to recreate a cache object. With this in mind we age equal
193 * percentages of the lru and ageable caches. This should balance the seeks
194 * generated by these structures.
195 *
Simon Arlott183ff222007-10-20 01:27:18 +0200196 * If the vm encountered mapped pages on the LRU it increase the pressure on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * slab to avoid swapping.
198 *
199 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
200 *
201 * `lru_pages' represents the number of on-LRU pages in all the zones which
202 * are eligible for the caller's allocation attempt. It is used for balancing
203 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700204 *
205 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
Andrew Morton69e05942006-03-22 00:08:19 -0800207unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
208 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800211 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 if (scanned == 0)
214 scanned = SWAP_CLUSTER_MAX;
215
216 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700217 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 list_for_each_entry(shrinker, &shrinker_list, list) {
220 unsigned long long delta;
221 unsigned long total_scan;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700222 unsigned long max_pass = (*shrinker->shrink)(0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800225 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 do_div(delta, lru_pages + 1);
227 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800228 if (shrinker->nr < 0) {
David Rientjes88c3bd72009-03-31 15:23:29 -0700229 printk(KERN_ERR "shrink_slab: %pF negative objects to "
230 "delete nr=%ld\n",
231 shrinker->shrink, shrinker->nr);
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800232 shrinker->nr = max_pass;
233 }
234
235 /*
236 * Avoid risking looping forever due to too large nr value:
237 * never try to free more than twice the estimate number of
238 * freeable entries.
239 */
240 if (shrinker->nr > max_pass * 2)
241 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 total_scan = shrinker->nr;
244 shrinker->nr = 0;
245
246 while (total_scan >= SHRINK_BATCH) {
247 long this_scan = SHRINK_BATCH;
248 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700249 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Rusty Russell8e1f9362007-07-17 04:03:17 -0700251 nr_before = (*shrinker->shrink)(0, gfp_mask);
252 shrink_ret = (*shrinker->shrink)(this_scan, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (shrink_ret == -1)
254 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700255 if (shrink_ret < nr_before)
256 ret += nr_before - shrink_ret;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700257 count_vm_events(SLABS_SCANNED, this_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 total_scan -= this_scan;
259
260 cond_resched();
261 }
262
263 shrinker->nr += total_scan;
264 }
265 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700266 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269/* Called without lock on whether page is mapped, so answer is unstable */
270static inline int page_mapping_inuse(struct page *page)
271{
272 struct address_space *mapping;
273
274 /* Page is in somebody's page tables. */
275 if (page_mapped(page))
276 return 1;
277
278 /* Be more reluctant to reclaim swapcache than pagecache */
279 if (PageSwapCache(page))
280 return 1;
281
282 mapping = page_mapping(page);
283 if (!mapping)
284 return 0;
285
286 /* File is mmap'd by somebody? */
287 return mapping_mapped(mapping);
288}
289
290static inline int is_page_cache_freeable(struct page *page)
291{
Johannes Weinerceddc3a2009-09-21 17:03:00 -0700292 /*
293 * A freeable page cache page is referenced only by the caller
294 * that isolated the page, the page cache radix tree and
295 * optional buffer heads at page->private.
296 */
Johannes Weineredcf4742009-09-21 17:02:59 -0700297 return page_count(page) - page_has_private(page) == 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300static int may_write_to_queue(struct backing_dev_info *bdi)
301{
Christoph Lameter930d9152006-01-08 01:00:47 -0800302 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return 1;
304 if (!bdi_write_congested(bdi))
305 return 1;
306 if (bdi == current->backing_dev_info)
307 return 1;
308 return 0;
309}
310
311/*
312 * We detected a synchronous write error writing a page out. Probably
313 * -ENOSPC. We need to propagate that into the address_space for a subsequent
314 * fsync(), msync() or close().
315 *
316 * The tricky part is that after writepage we cannot touch the mapping: nothing
317 * prevents it from being freed up. But we have a ref on the page and once
318 * that page is locked, the mapping is pinned.
319 *
320 * We're allowed to run sleeping lock_page() here because we know the caller has
321 * __GFP_FS.
322 */
323static void handle_write_error(struct address_space *mapping,
324 struct page *page, int error)
325{
326 lock_page(page);
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -0700327 if (page_mapping(page) == mapping)
328 mapping_set_error(mapping, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 unlock_page(page);
330}
331
Andy Whitcroftc661b072007-08-22 14:01:26 -0700332/* Request for sync pageout. */
333enum pageout_io {
334 PAGEOUT_IO_ASYNC,
335 PAGEOUT_IO_SYNC,
336};
337
Christoph Lameter04e62a22006-06-23 02:03:38 -0700338/* possible outcome of pageout() */
339typedef enum {
340 /* failed to write page out, page is locked */
341 PAGE_KEEP,
342 /* move page to the active list, page is locked */
343 PAGE_ACTIVATE,
344 /* page has been sent to the disk successfully, page is unlocked */
345 PAGE_SUCCESS,
346 /* page is clean and locked */
347 PAGE_CLEAN,
348} pageout_t;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/*
Andrew Morton1742f192006-03-22 00:08:21 -0800351 * pageout is called by shrink_page_list() for each dirty page.
352 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700354static pageout_t pageout(struct page *page, struct address_space *mapping,
355 enum pageout_io sync_writeback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 /*
358 * If the page is dirty, only perform writeback if that write
359 * will be non-blocking. To prevent this allocation from being
360 * stalled by pagecache activity. But note that there may be
361 * stalls if we need to run get_block(). We could test
362 * PagePrivate for that.
363 *
Vincent Li6aceb532009-12-14 17:58:49 -0800364 * If this process is currently in __generic_file_aio_write() against
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * this page's queue, we can perform writeback even if that
366 * will block.
367 *
368 * If the page is swapcache, write it back even if that would
369 * block, for some throttling. This happens by accident, because
370 * swap_backing_dev_info is bust: it doesn't reflect the
371 * congestion state of the swapdevs. Easy to fix, if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
373 if (!is_page_cache_freeable(page))
374 return PAGE_KEEP;
375 if (!mapping) {
376 /*
377 * Some data journaling orphaned pages can have
378 * page->mapping == NULL while being dirty with clean buffers.
379 */
David Howells266cf652009-04-03 16:42:36 +0100380 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (try_to_free_buffers(page)) {
382 ClearPageDirty(page);
Harvey Harrisond40cee22008-04-30 00:55:07 -0700383 printk("%s: orphaned page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return PAGE_CLEAN;
385 }
386 }
387 return PAGE_KEEP;
388 }
389 if (mapping->a_ops->writepage == NULL)
390 return PAGE_ACTIVATE;
391 if (!may_write_to_queue(mapping->backing_dev_info))
392 return PAGE_KEEP;
393
394 if (clear_page_dirty_for_io(page)) {
395 int res;
396 struct writeback_control wbc = {
397 .sync_mode = WB_SYNC_NONE,
398 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700399 .range_start = 0,
400 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 .nonblocking = 1,
402 .for_reclaim = 1,
403 };
404
405 SetPageReclaim(page);
406 res = mapping->a_ops->writepage(page, &wbc);
407 if (res < 0)
408 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800409 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 ClearPageReclaim(page);
411 return PAGE_ACTIVATE;
412 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700413
414 /*
415 * Wait on writeback if requested to. This happens when
416 * direct reclaiming a large contiguous area and the
417 * first attempt to free a range of pages fails.
418 */
419 if (PageWriteback(page) && sync_writeback == PAGEOUT_IO_SYNC)
420 wait_on_page_writeback(page);
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (!PageWriteback(page)) {
423 /* synchronous write or broken a_ops? */
424 ClearPageReclaim(page);
425 }
Andrew Mortone129b5c2006-09-27 01:50:00 -0700426 inc_zone_page_state(page, NR_VMSCAN_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return PAGE_SUCCESS;
428 }
429
430 return PAGE_CLEAN;
431}
432
Andrew Mortona649fd92006-10-17 00:09:36 -0700433/*
Nick Piggine2867812008-07-25 19:45:30 -0700434 * Same as remove_mapping, but if the page is removed from the mapping, it
435 * gets returned with a refcount of 0.
Andrew Mortona649fd92006-10-17 00:09:36 -0700436 */
Nick Piggine2867812008-07-25 19:45:30 -0700437static int __remove_mapping(struct address_space *mapping, struct page *page)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800438{
Nick Piggin28e4d962006-09-25 23:31:23 -0700439 BUG_ON(!PageLocked(page));
440 BUG_ON(mapping != page_mapping(page));
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800441
Nick Piggin19fd6232008-07-25 19:45:32 -0700442 spin_lock_irq(&mapping->tree_lock);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800443 /*
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700444 * The non racy check for a busy page.
445 *
446 * Must be careful with the order of the tests. When someone has
447 * a ref to the page, it may be possible that they dirty it then
448 * drop the reference. So if PageDirty is tested before page_count
449 * here, then the following race may occur:
450 *
451 * get_user_pages(&page);
452 * [user mapping goes away]
453 * write_to(page);
454 * !PageDirty(page) [good]
455 * SetPageDirty(page);
456 * put_page(page);
457 * !page_count(page) [good, discard it]
458 *
459 * [oops, our write_to data is lost]
460 *
461 * Reversing the order of the tests ensures such a situation cannot
462 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
463 * load is not satisfied before that of page->_count.
464 *
465 * Note that if SetPageDirty is always performed via set_page_dirty,
466 * and thus under tree_lock, then this ordering is not required.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800467 */
Nick Piggine2867812008-07-25 19:45:30 -0700468 if (!page_freeze_refs(page, 2))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800469 goto cannot_free;
Nick Piggine2867812008-07-25 19:45:30 -0700470 /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
471 if (unlikely(PageDirty(page))) {
472 page_unfreeze_refs(page, 2);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800473 goto cannot_free;
Nick Piggine2867812008-07-25 19:45:30 -0700474 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800475
476 if (PageSwapCache(page)) {
477 swp_entry_t swap = { .val = page_private(page) };
478 __delete_from_swap_cache(page);
Nick Piggin19fd6232008-07-25 19:45:32 -0700479 spin_unlock_irq(&mapping->tree_lock);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700480 swapcache_free(swap, page);
Nick Piggine2867812008-07-25 19:45:30 -0700481 } else {
482 __remove_from_page_cache(page);
Nick Piggin19fd6232008-07-25 19:45:32 -0700483 spin_unlock_irq(&mapping->tree_lock);
Daisuke Nishimurae767e052009-05-28 14:34:28 -0700484 mem_cgroup_uncharge_cache_page(page);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800485 }
486
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800487 return 1;
488
489cannot_free:
Nick Piggin19fd6232008-07-25 19:45:32 -0700490 spin_unlock_irq(&mapping->tree_lock);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800491 return 0;
492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
Nick Piggine2867812008-07-25 19:45:30 -0700495 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
496 * someone else has a ref on the page, abort and return 0. If it was
497 * successfully detached, return 1. Assumes the caller has a single ref on
498 * this page.
499 */
500int remove_mapping(struct address_space *mapping, struct page *page)
501{
502 if (__remove_mapping(mapping, page)) {
503 /*
504 * Unfreezing the refcount with 1 rather than 2 effectively
505 * drops the pagecache ref for us without requiring another
506 * atomic operation.
507 */
508 page_unfreeze_refs(page, 1);
509 return 1;
510 }
511 return 0;
512}
513
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700514/**
515 * putback_lru_page - put previously isolated page onto appropriate LRU list
516 * @page: page to be put back to appropriate lru list
517 *
518 * Add previously isolated @page to appropriate LRU list.
519 * Page may still be unevictable for other reasons.
520 *
521 * lru_lock must not be held, interrupts must be enabled.
522 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700523void putback_lru_page(struct page *page)
524{
525 int lru;
526 int active = !!TestClearPageActive(page);
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700527 int was_unevictable = PageUnevictable(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700528
529 VM_BUG_ON(PageLRU(page));
530
531redo:
532 ClearPageUnevictable(page);
533
534 if (page_evictable(page, NULL)) {
535 /*
536 * For evictable pages, we can use the cache.
537 * In event of a race, worst case is we end up with an
538 * unevictable page on [in]active list.
539 * We know how to handle that.
540 */
Johannes Weiner401a8e12009-09-21 17:02:58 -0700541 lru = active + page_lru_base_type(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700542 lru_cache_add_lru(page, lru);
543 } else {
544 /*
545 * Put unevictable pages directly on zone's unevictable
546 * list.
547 */
548 lru = LRU_UNEVICTABLE;
549 add_page_to_unevictable_list(page);
Johannes Weiner6a7b9542009-10-26 16:50:00 -0700550 /*
551 * When racing with an mlock clearing (page is
552 * unlocked), make sure that if the other thread does
553 * not observe our setting of PG_lru and fails
554 * isolation, we see PG_mlocked cleared below and move
555 * the page back to the evictable list.
556 *
557 * The other side is TestClearPageMlocked().
558 */
559 smp_mb();
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700560 }
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700561
562 /*
563 * page's status can change while we move it among lru. If an evictable
564 * page is on unevictable list, it never be freed. To avoid that,
565 * check after we added it to the list, again.
566 */
567 if (lru == LRU_UNEVICTABLE && page_evictable(page, NULL)) {
568 if (!isolate_lru_page(page)) {
569 put_page(page);
570 goto redo;
571 }
572 /* This means someone else dropped this page from LRU
573 * So, it will be freed or putback to LRU again. There is
574 * nothing to do here.
575 */
576 }
577
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700578 if (was_unevictable && lru != LRU_UNEVICTABLE)
579 count_vm_event(UNEVICTABLE_PGRESCUED);
580 else if (!was_unevictable && lru == LRU_UNEVICTABLE)
581 count_vm_event(UNEVICTABLE_PGCULLED);
582
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700583 put_page(page); /* drop ref from isolate */
584}
585
Nick Piggine2867812008-07-25 19:45:30 -0700586/*
Andrew Morton1742f192006-03-22 00:08:21 -0800587 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 */
Andrew Morton1742f192006-03-22 00:08:21 -0800589static unsigned long shrink_page_list(struct list_head *page_list,
Andy Whitcroftc661b072007-08-22 14:01:26 -0700590 struct scan_control *sc,
591 enum pageout_io sync_writeback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 LIST_HEAD(ret_pages);
594 struct pagevec freed_pvec;
595 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800596 unsigned long nr_reclaimed = 0;
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700597 unsigned long vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 cond_resched();
600
601 pagevec_init(&freed_pvec, 1);
602 while (!list_empty(page_list)) {
603 struct address_space *mapping;
604 struct page *page;
605 int may_enter_fs;
606 int referenced;
607
608 cond_resched();
609
610 page = lru_to_page(page_list);
611 list_del(&page->lru);
612
Nick Piggin529ae9a2008-08-02 12:01:03 +0200613 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 goto keep;
615
Nick Piggin725d7042006-09-25 23:30:55 -0700616 VM_BUG_ON(PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800619
Nick Pigginb291f002008-10-18 20:26:44 -0700620 if (unlikely(!page_evictable(page, NULL)))
621 goto cull_mlocked;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700622
Johannes Weinera6dc60f2009-03-31 15:19:30 -0700623 if (!sc->may_unmap && page_mapped(page))
Christoph Lameter80e43422006-02-11 17:55:53 -0800624 goto keep_locked;
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /* Double the slab pressure for mapped and swapcache pages */
627 if (page_mapped(page) || PageSwapCache(page))
628 sc->nr_scanned++;
629
Andy Whitcroftc661b072007-08-22 14:01:26 -0700630 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
631 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
632
633 if (PageWriteback(page)) {
634 /*
635 * Synchronous reclaim is performed in two passes,
636 * first an asynchronous pass over the list to
637 * start parallel writeback, and a second synchronous
638 * pass to wait for the IO to complete. Wait here
639 * for any page for which writeback has already
640 * started.
641 */
642 if (sync_writeback == PAGEOUT_IO_SYNC && may_enter_fs)
643 wait_on_page_writeback(page);
Andrew Morton4dd4b922008-03-24 12:29:52 -0700644 else
Andy Whitcroftc661b072007-08-22 14:01:26 -0700645 goto keep_locked;
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -0700648 referenced = page_referenced(page, 1,
649 sc->mem_cgroup, &vm_flags);
Minchan Kim03ef83a2009-08-26 14:29:23 -0700650 /*
651 * In active use or really unfreeable? Activate it.
652 * If page which have PG_mlocked lost isoltation race,
653 * try_to_unmap moves it to unevictable list
654 */
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700655 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER &&
Minchan Kim03ef83a2009-08-26 14:29:23 -0700656 referenced && page_mapping_inuse(page)
657 && !(vm_flags & VM_LOCKED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto activate_locked;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /*
661 * Anonymous process memory has backing store?
662 * Try to allocate it some swap space here.
663 */
Nick Pigginb291f002008-10-18 20:26:44 -0700664 if (PageAnon(page) && !PageSwapCache(page)) {
Hugh Dickins63eb6b92008-11-19 15:36:37 -0800665 if (!(sc->gfp_mask & __GFP_IO))
666 goto keep_locked;
Hugh Dickinsac47b002009-01-06 14:39:39 -0800667 if (!add_to_swap(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 goto activate_locked;
Hugh Dickins63eb6b92008-11-19 15:36:37 -0800669 may_enter_fs = 1;
Nick Pigginb291f002008-10-18 20:26:44 -0700670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /*
675 * The page is mapped into the page tables of one or more
676 * processes. Try to unmap it here.
677 */
678 if (page_mapped(page) && mapping) {
Andi Kleen14fa31b2009-09-16 11:50:10 +0200679 switch (try_to_unmap(page, TTU_UNMAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 case SWAP_FAIL:
681 goto activate_locked;
682 case SWAP_AGAIN:
683 goto keep_locked;
Nick Pigginb291f002008-10-18 20:26:44 -0700684 case SWAP_MLOCK:
685 goto cull_mlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 case SWAP_SUCCESS:
687 ; /* try to free the page below */
688 }
689 }
690
691 if (PageDirty(page)) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700692 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && referenced)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 goto keep_locked;
Andrew Morton4dd4b922008-03-24 12:29:52 -0700694 if (!may_enter_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800696 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 goto keep_locked;
698
699 /* Page is dirty, try to write it out here */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700700 switch (pageout(page, mapping, sync_writeback)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 case PAGE_KEEP:
702 goto keep_locked;
703 case PAGE_ACTIVATE:
704 goto activate_locked;
705 case PAGE_SUCCESS:
Andrew Morton4dd4b922008-03-24 12:29:52 -0700706 if (PageWriteback(page) || PageDirty(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 goto keep;
708 /*
709 * A synchronous write - probably a ramdisk. Go
710 * ahead and try to reclaim the page.
711 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200712 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto keep;
714 if (PageDirty(page) || PageWriteback(page))
715 goto keep_locked;
716 mapping = page_mapping(page);
717 case PAGE_CLEAN:
718 ; /* try to free the page below */
719 }
720 }
721
722 /*
723 * If the page has buffers, try to free the buffer mappings
724 * associated with this page. If we succeed we try to free
725 * the page as well.
726 *
727 * We do this even if the page is PageDirty().
728 * try_to_release_page() does not perform I/O, but it is
729 * possible for a page to have PageDirty set, but it is actually
730 * clean (all its buffers are clean). This happens if the
731 * buffers were written out directly, with submit_bh(). ext3
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700732 * will do this, as well as the blockdev mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * try_to_release_page() will discover that cleanness and will
734 * drop the buffers and mark the page clean - it can be freed.
735 *
736 * Rarely, pages can have buffers and no ->mapping. These are
737 * the pages which were not successfully invalidated in
738 * truncate_complete_page(). We try to drop those buffers here
739 * and if that worked, and the page is no longer mapped into
740 * process address space (page_count == 1) it can be freed.
741 * Otherwise, leave the page on the LRU so it is swappable.
742 */
David Howells266cf652009-04-03 16:42:36 +0100743 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!try_to_release_page(page, sc->gfp_mask))
745 goto activate_locked;
Nick Piggine2867812008-07-25 19:45:30 -0700746 if (!mapping && page_count(page) == 1) {
747 unlock_page(page);
748 if (put_page_testzero(page))
749 goto free_it;
750 else {
751 /*
752 * rare race with speculative reference.
753 * the speculative reference will free
754 * this page shortly, so we may
755 * increment nr_reclaimed here (and
756 * leave it off the LRU).
757 */
758 nr_reclaimed++;
759 continue;
760 }
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763
Nick Piggine2867812008-07-25 19:45:30 -0700764 if (!mapping || !__remove_mapping(mapping, page))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800765 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Nick Piggina978d6f2008-10-18 20:26:58 -0700767 /*
768 * At this point, we have no other references and there is
769 * no way to pick any more up (removed from LRU, removed
770 * from pagecache). Can use non-atomic bitops now (and
771 * we obviously don't have to worry about waking up a process
772 * waiting on the page lock, because there are no references.
773 */
774 __clear_page_locked(page);
Nick Piggine2867812008-07-25 19:45:30 -0700775free_it:
Andrew Morton05ff5132006-03-22 00:08:20 -0800776 nr_reclaimed++;
Nick Piggine2867812008-07-25 19:45:30 -0700777 if (!pagevec_add(&freed_pvec, page)) {
778 __pagevec_free(&freed_pvec);
779 pagevec_reinit(&freed_pvec);
780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 continue;
782
Nick Pigginb291f002008-10-18 20:26:44 -0700783cull_mlocked:
Hugh Dickins63d6c5a2009-01-06 14:39:38 -0800784 if (PageSwapCache(page))
785 try_to_free_swap(page);
Nick Pigginb291f002008-10-18 20:26:44 -0700786 unlock_page(page);
787 putback_lru_page(page);
788 continue;
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790activate_locked:
Rik van Riel68a223942008-10-18 20:26:23 -0700791 /* Not a candidate for swapping, so reclaim swap space. */
792 if (PageSwapCache(page) && vm_swap_full())
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800793 try_to_free_swap(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700794 VM_BUG_ON(PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 SetPageActive(page);
796 pgactivate++;
797keep_locked:
798 unlock_page(page);
799keep:
800 list_add(&page->lru, &ret_pages);
Nick Pigginb291f002008-10-18 20:26:44 -0700801 VM_BUG_ON(PageLRU(page) || PageUnevictable(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803 list_splice(&ret_pages, page_list);
804 if (pagevec_count(&freed_pvec))
Nick Piggine2867812008-07-25 19:45:30 -0700805 __pagevec_free(&freed_pvec);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700806 count_vm_events(PGACTIVATE, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800807 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700810/* LRU Isolation modes. */
811#define ISOLATE_INACTIVE 0 /* Isolate inactive pages. */
812#define ISOLATE_ACTIVE 1 /* Isolate active pages. */
813#define ISOLATE_BOTH 2 /* Isolate both active and inactive pages. */
814
815/*
816 * Attempt to remove the specified page from its LRU. Only take this page
817 * if it is of the appropriate PageActive status. Pages which are being
818 * freed elsewhere are also ignored.
819 *
820 * page: page to consider
821 * mode: one of the LRU isolation modes defined above
822 *
823 * returns 0 on success, -ve errno on failure.
824 */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700825int __isolate_lru_page(struct page *page, int mode, int file)
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700826{
827 int ret = -EINVAL;
828
829 /* Only take pages on the LRU. */
830 if (!PageLRU(page))
831 return ret;
832
833 /*
834 * When checking the active state, we need to be sure we are
835 * dealing with comparible boolean values. Take the logical not
836 * of each.
837 */
838 if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode))
839 return ret;
840
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700841 if (mode != ISOLATE_BOTH && page_is_file_cache(page) != file)
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700842 return ret;
843
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700844 /*
845 * When this function is being called for lumpy reclaim, we
846 * initially look into all LRU pages, active, inactive and
847 * unevictable; only give shrink_page_list evictable pages.
848 */
849 if (PageUnevictable(page))
850 return ret;
851
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700852 ret = -EBUSY;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800853
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700854 if (likely(get_page_unless_zero(page))) {
855 /*
856 * Be careful not to clear PageLRU until after we're
857 * sure the page is not being freed elsewhere -- the
858 * page release code relies on it.
859 */
860 ClearPageLRU(page);
861 ret = 0;
862 }
863
864 return ret;
865}
866
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800867/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 * zone->lru_lock is heavily contended. Some of the functions that
869 * shrink the lists perform better by taking out a batch of pages
870 * and working on them outside the LRU lock.
871 *
872 * For pagecache intensive workloads, this function is the hottest
873 * spot in the kernel (apart from copy_*_user functions).
874 *
875 * Appropriate locks must be held before calling this function.
876 *
877 * @nr_to_scan: The number of pages to look through on the list.
878 * @src: The LRU list to pull pages off.
879 * @dst: The temp list to put pages on to.
880 * @scanned: The number of pages that were scanned.
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700881 * @order: The caller's attempted allocation order
882 * @mode: One of the LRU isolation modes
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700883 * @file: True [1] if isolating file [!anon] pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 *
885 * returns how many pages were moved onto *@dst.
886 */
Andrew Morton69e05942006-03-22 00:08:19 -0800887static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
888 struct list_head *src, struct list_head *dst,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700889 unsigned long *scanned, int order, int mode, int file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Andrew Morton69e05942006-03-22 00:08:19 -0800891 unsigned long nr_taken = 0;
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800892 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800894 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700895 struct page *page;
896 unsigned long pfn;
897 unsigned long end_pfn;
898 unsigned long page_pfn;
899 int zone_id;
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 page = lru_to_page(src);
902 prefetchw_prev_lru_page(page, src, flags);
903
Nick Piggin725d7042006-09-25 23:30:55 -0700904 VM_BUG_ON(!PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -0800905
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700906 switch (__isolate_lru_page(page, mode, file)) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700907 case 0:
908 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700909 mem_cgroup_del_lru(page);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -0800910 nr_taken++;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700911 break;
Nick Piggin46453a62006-03-22 00:07:58 -0800912
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700913 case -EBUSY:
914 /* else it is being freed elsewhere */
915 list_move(&page->lru, src);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700916 mem_cgroup_rotate_lru_list(page, page_lru(page));
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700917 continue;
918
919 default:
920 BUG();
921 }
922
923 if (!order)
924 continue;
925
926 /*
927 * Attempt to take all pages in the order aligned region
928 * surrounding the tag page. Only take those pages of
929 * the same active state as that tag page. We may safely
930 * round the target page pfn down to the requested order
931 * as the mem_map is guarenteed valid out to MAX_ORDER,
932 * where that page is in a different zone we will detect
933 * it from its zone id and abort this block scan.
934 */
935 zone_id = page_zone_id(page);
936 page_pfn = page_to_pfn(page);
937 pfn = page_pfn & ~((1 << order) - 1);
938 end_pfn = pfn + (1 << order);
939 for (; pfn < end_pfn; pfn++) {
940 struct page *cursor_page;
941
942 /* The target page is in the block, ignore it. */
943 if (unlikely(pfn == page_pfn))
944 continue;
945
946 /* Avoid holes within the zone. */
947 if (unlikely(!pfn_valid_within(pfn)))
948 break;
949
950 cursor_page = pfn_to_page(pfn);
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700951
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700952 /* Check that we have not crossed a zone boundary. */
953 if (unlikely(page_zone_id(cursor_page) != zone_id))
954 continue;
Minchan Kimde2e7562009-09-21 17:01:43 -0700955
956 /*
957 * If we don't have enough swap space, reclaiming of
958 * anon page which don't already have a swap slot is
959 * pointless.
960 */
961 if (nr_swap_pages <= 0 && PageAnon(cursor_page) &&
962 !PageSwapCache(cursor_page))
963 continue;
964
KAMEZAWA Hiroyukiee993b12009-06-16 15:33:24 -0700965 if (__isolate_lru_page(cursor_page, mode, file) == 0) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700966 list_move(&cursor_page->lru, dst);
KAMEZAWA Hiroyukicb4cbcf2009-06-23 08:57:55 +0900967 mem_cgroup_del_lru(cursor_page);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700968 nr_taken++;
969 scan++;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700970 }
971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973
974 *scanned = scan;
975 return nr_taken;
976}
977
Balbir Singh66e17072008-02-07 00:13:56 -0800978static unsigned long isolate_pages_global(unsigned long nr,
979 struct list_head *dst,
980 unsigned long *scanned, int order,
981 int mode, struct zone *z,
982 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700983 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800984{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700985 int lru = LRU_BASE;
Balbir Singh66e17072008-02-07 00:13:56 -0800986 if (active)
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700987 lru += LRU_ACTIVE;
988 if (file)
989 lru += LRU_FILE;
990 return isolate_lru_pages(nr, &z->lru[lru].list, dst, scanned, order,
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700991 mode, file);
Balbir Singh66e17072008-02-07 00:13:56 -0800992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994/*
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700995 * clear_active_flags() is a helper for shrink_active_list(), clearing
996 * any active bits from the pages in the list.
997 */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700998static unsigned long clear_active_flags(struct list_head *page_list,
999 unsigned int *count)
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001000{
1001 int nr_active = 0;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001002 int lru;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001003 struct page *page;
1004
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001005 list_for_each_entry(page, page_list, lru) {
Johannes Weiner401a8e12009-09-21 17:02:58 -07001006 lru = page_lru_base_type(page);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001007 if (PageActive(page)) {
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001008 lru += LRU_ACTIVE;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001009 ClearPageActive(page);
1010 nr_active++;
1011 }
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001012 count[lru]++;
1013 }
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001014
1015 return nr_active;
1016}
1017
Nick Piggin62695a82008-10-18 20:26:09 -07001018/**
1019 * isolate_lru_page - tries to isolate a page from its LRU list
1020 * @page: page to isolate from its LRU list
1021 *
1022 * Isolates a @page from an LRU list, clears PageLRU and adjusts the
1023 * vmstat statistic corresponding to whatever LRU list the page was on.
1024 *
1025 * Returns 0 if the page was removed from an LRU list.
1026 * Returns -EBUSY if the page was not on an LRU list.
1027 *
1028 * The returned page will have PageLRU() cleared. If it was found on
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001029 * the active list, it will have PageActive set. If it was found on
1030 * the unevictable list, it will have the PageUnevictable bit set. That flag
1031 * may need to be cleared by the caller before letting the page go.
Nick Piggin62695a82008-10-18 20:26:09 -07001032 *
1033 * The vmstat statistic corresponding to the list on which the page was
1034 * found will be decremented.
1035 *
1036 * Restrictions:
1037 * (1) Must be called with an elevated refcount on the page. This is a
1038 * fundamentnal difference from isolate_lru_pages (which is called
1039 * without a stable reference).
1040 * (2) the lru_lock must not be held.
1041 * (3) interrupts must be enabled.
1042 */
1043int isolate_lru_page(struct page *page)
1044{
1045 int ret = -EBUSY;
1046
1047 if (PageLRU(page)) {
1048 struct zone *zone = page_zone(page);
1049
1050 spin_lock_irq(&zone->lru_lock);
1051 if (PageLRU(page) && get_page_unless_zero(page)) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001052 int lru = page_lru(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001053 ret = 0;
1054 ClearPageLRU(page);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001055
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001056 del_page_from_lru_list(zone, page, lru);
Nick Piggin62695a82008-10-18 20:26:09 -07001057 }
1058 spin_unlock_irq(&zone->lru_lock);
1059 }
1060 return ret;
1061}
1062
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001063/*
Rik van Riel35cd7812009-09-21 17:01:38 -07001064 * Are there way too many processes in the direct reclaim path already?
1065 */
1066static int too_many_isolated(struct zone *zone, int file,
1067 struct scan_control *sc)
1068{
1069 unsigned long inactive, isolated;
1070
1071 if (current_is_kswapd())
1072 return 0;
1073
1074 if (!scanning_global_lru(sc))
1075 return 0;
1076
1077 if (file) {
1078 inactive = zone_page_state(zone, NR_INACTIVE_FILE);
1079 isolated = zone_page_state(zone, NR_ISOLATED_FILE);
1080 } else {
1081 inactive = zone_page_state(zone, NR_INACTIVE_ANON);
1082 isolated = zone_page_state(zone, NR_ISOLATED_ANON);
1083 }
1084
1085 return isolated > inactive;
1086}
1087
1088/*
Andrew Morton1742f192006-03-22 00:08:21 -08001089 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
1090 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
Andrew Morton1742f192006-03-22 00:08:21 -08001092static unsigned long shrink_inactive_list(unsigned long max_scan,
Rik van Riel33c120e2008-10-18 20:26:36 -07001093 struct zone *zone, struct scan_control *sc,
1094 int priority, int file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 LIST_HEAD(page_list);
1097 struct pagevec pvec;
Andrew Morton69e05942006-03-22 00:08:19 -08001098 unsigned long nr_scanned = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -08001099 unsigned long nr_reclaimed = 0;
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001100 struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
KOSAKI Motohiro78dc5832009-06-16 15:31:40 -07001101 int lumpy_reclaim = 0;
1102
Rik van Riel35cd7812009-09-21 17:01:38 -07001103 while (unlikely(too_many_isolated(zone, file, sc))) {
KOSAKI Motohiro58355c72009-10-26 16:49:35 -07001104 congestion_wait(BLK_RW_ASYNC, HZ/10);
Rik van Riel35cd7812009-09-21 17:01:38 -07001105
1106 /* We are about to die and free our memory. Return now. */
1107 if (fatal_signal_pending(current))
1108 return SWAP_CLUSTER_MAX;
1109 }
1110
KOSAKI Motohiro78dc5832009-06-16 15:31:40 -07001111 /*
1112 * If we need a large contiguous chunk of memory, or have
1113 * trouble getting a small set of contiguous pages, we
1114 * will reclaim both active and inactive pages.
1115 *
1116 * We use the same threshold as pageout congestion_wait below.
1117 */
1118 if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
1119 lumpy_reclaim = 1;
1120 else if (sc->order && priority < DEF_PRIORITY - 2)
1121 lumpy_reclaim = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 pagevec_init(&pvec, 1);
1124
1125 lru_add_drain();
1126 spin_lock_irq(&zone->lru_lock);
Andrew Morton69e05942006-03-22 00:08:19 -08001127 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct page *page;
Andrew Morton69e05942006-03-22 00:08:19 -08001129 unsigned long nr_taken;
1130 unsigned long nr_scan;
1131 unsigned long nr_freed;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001132 unsigned long nr_active;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001133 unsigned int count[NR_LRU_LISTS] = { 0, };
KOSAKI Motohiro78dc5832009-06-16 15:31:40 -07001134 int mode = lumpy_reclaim ? ISOLATE_BOTH : ISOLATE_INACTIVE;
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07001135 unsigned long nr_anon;
1136 unsigned long nr_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Balbir Singh66e17072008-02-07 00:13:56 -08001138 nr_taken = sc->isolate_pages(sc->swap_cluster_max,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001139 &page_list, &nr_scan, sc->order, mode,
1140 zone, sc->mem_cgroup, 0, file);
KOSAKI Motohirob35ea172009-09-21 17:01:36 -07001141
1142 if (scanning_global_lru(sc)) {
1143 zone->pages_scanned += nr_scan;
1144 if (current_is_kswapd())
1145 __count_zone_vm_events(PGSCAN_KSWAPD, zone,
1146 nr_scan);
1147 else
1148 __count_zone_vm_events(PGSCAN_DIRECT, zone,
1149 nr_scan);
1150 }
1151
1152 if (nr_taken == 0)
1153 goto done;
1154
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001155 nr_active = clear_active_flags(&page_list, count);
Andy Whitcrofte9187bd2007-08-22 14:01:25 -07001156 __count_vm_events(PGDEACTIVATE, nr_active);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001157
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001158 __mod_zone_page_state(zone, NR_ACTIVE_FILE,
1159 -count[LRU_ACTIVE_FILE]);
1160 __mod_zone_page_state(zone, NR_INACTIVE_FILE,
1161 -count[LRU_INACTIVE_FILE]);
1162 __mod_zone_page_state(zone, NR_ACTIVE_ANON,
1163 -count[LRU_ACTIVE_ANON]);
1164 __mod_zone_page_state(zone, NR_INACTIVE_ANON,
1165 -count[LRU_INACTIVE_ANON]);
1166
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07001167 nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON];
1168 nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE];
1169 __mod_zone_page_state(zone, NR_ISOLATED_ANON, nr_anon);
1170 __mod_zone_page_state(zone, NR_ISOLATED_FILE, nr_file);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001171
1172 reclaim_stat->recent_scanned[0] += count[LRU_INACTIVE_ANON];
1173 reclaim_stat->recent_scanned[0] += count[LRU_ACTIVE_ANON];
1174 reclaim_stat->recent_scanned[1] += count[LRU_INACTIVE_FILE];
1175 reclaim_stat->recent_scanned[1] += count[LRU_ACTIVE_FILE];
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 spin_unlock_irq(&zone->lru_lock);
1178
Andrew Morton69e05942006-03-22 00:08:19 -08001179 nr_scanned += nr_scan;
Andy Whitcroftc661b072007-08-22 14:01:26 -07001180 nr_freed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC);
1181
1182 /*
1183 * If we are direct reclaiming for contiguous pages and we do
1184 * not reclaim everything in the list, try again and wait
1185 * for IO to complete. This will stall high-order allocations
1186 * but that should be acceptable to the caller
1187 */
1188 if (nr_freed < nr_taken && !current_is_kswapd() &&
KOSAKI Motohiro78dc5832009-06-16 15:31:40 -07001189 lumpy_reclaim) {
Jens Axboe8aa7e842009-07-09 14:52:32 +02001190 congestion_wait(BLK_RW_ASYNC, HZ/10);
Andy Whitcroftc661b072007-08-22 14:01:26 -07001191
1192 /*
1193 * The attempt at page out may have made some
1194 * of the pages active, mark them inactive again.
1195 */
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001196 nr_active = clear_active_flags(&page_list, count);
Andy Whitcroftc661b072007-08-22 14:01:26 -07001197 count_vm_events(PGDEACTIVATE, nr_active);
1198
1199 nr_freed += shrink_page_list(&page_list, sc,
1200 PAGEOUT_IO_SYNC);
1201 }
1202
Andrew Morton05ff5132006-03-22 00:08:20 -08001203 nr_reclaimed += nr_freed;
KOSAKI Motohirob35ea172009-09-21 17:01:36 -07001204
Nick Piggina74609f2006-01-06 00:11:20 -08001205 local_irq_disable();
KOSAKI Motohirob35ea172009-09-21 17:01:36 -07001206 if (current_is_kswapd())
Christoph Lameterf8891e52006-06-30 01:55:45 -07001207 __count_vm_events(KSWAPD_STEAL, nr_freed);
Shantanu Goel918d3f92006-12-29 16:48:59 -08001208 __count_zone_vm_events(PGSTEAL, zone, nr_freed);
Nick Piggina74609f2006-01-06 00:11:20 -08001209
1210 spin_lock(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /*
1212 * Put back any unfreeable pages.
1213 */
1214 while (!list_empty(&page_list)) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001215 int lru;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 page = lru_to_page(&page_list);
Nick Piggin725d7042006-09-25 23:30:55 -07001217 VM_BUG_ON(PageLRU(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 list_del(&page->lru);
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001219 if (unlikely(!page_evictable(page, NULL))) {
1220 spin_unlock_irq(&zone->lru_lock);
1221 putback_lru_page(page);
1222 spin_lock_irq(&zone->lru_lock);
1223 continue;
1224 }
1225 SetPageLRU(page);
1226 lru = page_lru(page);
1227 add_page_to_lru_list(zone, page, lru);
KOSAKI Motohiro74a1c482009-09-21 17:01:45 -07001228 if (is_active_lru(lru)) {
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001229 int file = is_file_lru(lru);
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001230 reclaim_stat->recent_rotated[file]++;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (!pagevec_add(&pvec, page)) {
1233 spin_unlock_irq(&zone->lru_lock);
1234 __pagevec_release(&pvec);
1235 spin_lock_irq(&zone->lru_lock);
1236 }
1237 }
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07001238 __mod_zone_page_state(zone, NR_ISOLATED_ANON, -nr_anon);
1239 __mod_zone_page_state(zone, NR_ISOLATED_FILE, -nr_file);
1240
Andrew Morton69e05942006-03-22 00:08:19 -08001241 } while (nr_scanned < max_scan);
KOSAKI Motohirob35ea172009-09-21 17:01:36 -07001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243done:
KOSAKI Motohirob35ea172009-09-21 17:01:36 -07001244 spin_unlock_irq(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 pagevec_release(&pvec);
Andrew Morton05ff5132006-03-22 00:08:20 -08001246 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Martin Bligh3bb1a852006-10-28 10:38:24 -07001249/*
1250 * We are about to scan this zone at a certain priority level. If that priority
1251 * level is smaller (ie: more urgent) than the previous priority, then note
1252 * that priority level within the zone. This is done so that when the next
1253 * process comes in to scan this zone, it will immediately start out at this
1254 * priority level rather than having to build up its own scanning priority.
1255 * Here, this priority affects only the reclaim-mapped threshold.
1256 */
1257static inline void note_zone_scanning_priority(struct zone *zone, int priority)
1258{
1259 if (priority < zone->prev_priority)
1260 zone->prev_priority = priority;
1261}
1262
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001263/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 * This moves pages from the active list to the inactive list.
1265 *
1266 * We move them the other way if the page is referenced by one or more
1267 * processes, from rmap.
1268 *
1269 * If the pages are mostly unmapped, the processing is fast and it is
1270 * appropriate to hold zone->lru_lock across the whole operation. But if
1271 * the pages are mapped, the processing is slow (page_referenced()) so we
1272 * should drop zone->lru_lock around each page. It's impossible to balance
1273 * this, so instead we remove the pages from the LRU while processing them.
1274 * It is safe to rely on PG_active against the non-LRU pages in here because
1275 * nobody will play with that bit on a non-LRU page.
1276 *
1277 * The downside is that we have to touch page->_count against each page.
1278 * But we had to alter page->flags anyway.
1279 */
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001280
Wu Fengguang3eb41402009-06-16 15:33:13 -07001281static void move_active_pages_to_lru(struct zone *zone,
1282 struct list_head *list,
1283 enum lru_list lru)
1284{
1285 unsigned long pgmoved = 0;
1286 struct pagevec pvec;
1287 struct page *page;
1288
1289 pagevec_init(&pvec, 1);
1290
1291 while (!list_empty(list)) {
1292 page = lru_to_page(list);
Wu Fengguang3eb41402009-06-16 15:33:13 -07001293
1294 VM_BUG_ON(PageLRU(page));
1295 SetPageLRU(page);
1296
Wu Fengguang3eb41402009-06-16 15:33:13 -07001297 list_move(&page->lru, &zone->lru[lru].list);
1298 mem_cgroup_add_lru_list(page, lru);
1299 pgmoved++;
1300
1301 if (!pagevec_add(&pvec, page) || list_empty(list)) {
1302 spin_unlock_irq(&zone->lru_lock);
1303 if (buffer_heads_over_limit)
1304 pagevec_strip(&pvec);
1305 __pagevec_release(&pvec);
1306 spin_lock_irq(&zone->lru_lock);
1307 }
1308 }
1309 __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
1310 if (!is_active_lru(lru))
1311 __count_vm_events(PGDEACTIVATE, pgmoved);
1312}
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001313
Andrew Morton1742f192006-03-22 00:08:21 -08001314static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001315 struct scan_control *sc, int priority, int file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001317 unsigned long nr_taken;
Andrew Morton69e05942006-03-22 00:08:19 -08001318 unsigned long pgscanned;
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -07001319 unsigned long vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 LIST_HEAD(l_hold); /* The pages which were snipped off */
Wu Fengguang8cab4752009-06-16 15:33:12 -07001321 LIST_HEAD(l_active);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001322 LIST_HEAD(l_inactive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 struct page *page;
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001324 struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001325 unsigned long nr_rotated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 lru_add_drain();
1328 spin_lock_irq(&zone->lru_lock);
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001329 nr_taken = sc->isolate_pages(nr_pages, &l_hold, &pgscanned, sc->order,
Balbir Singh66e17072008-02-07 00:13:56 -08001330 ISOLATE_ACTIVE, zone,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001331 sc->mem_cgroup, 1, file);
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001332 /*
1333 * zone->pages_scanned is used for detect zone's oom
1334 * mem_cgroup remembers nr_scan by itself.
1335 */
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001336 if (scanning_global_lru(sc)) {
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001337 zone->pages_scanned += pgscanned;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001338 }
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001339 reclaim_stat->recent_scanned[file] += nr_taken;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001340
Wu Fengguang3eb41402009-06-16 15:33:13 -07001341 __count_zone_vm_events(PGREFILL, zone, pgscanned);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001342 if (file)
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001343 __mod_zone_page_state(zone, NR_ACTIVE_FILE, -nr_taken);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001344 else
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001345 __mod_zone_page_state(zone, NR_ACTIVE_ANON, -nr_taken);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07001346 __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 spin_unlock_irq(&zone->lru_lock);
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 while (!list_empty(&l_hold)) {
1350 cond_resched();
1351 page = lru_to_page(&l_hold);
1352 list_del(&page->lru);
Rik van Riel7e9cd482008-10-18 20:26:35 -07001353
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001354 if (unlikely(!page_evictable(page, NULL))) {
1355 putback_lru_page(page);
1356 continue;
1357 }
1358
Rik van Riel7e9cd482008-10-18 20:26:35 -07001359 /* page_referenced clears PageReferenced */
1360 if (page_mapping_inuse(page) &&
Wu Fengguang8cab4752009-06-16 15:33:12 -07001361 page_referenced(page, 0, sc->mem_cgroup, &vm_flags)) {
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001362 nr_rotated++;
Wu Fengguang8cab4752009-06-16 15:33:12 -07001363 /*
1364 * Identify referenced, file-backed active pages and
1365 * give them one more trip around the active list. So
1366 * that executable code get better chances to stay in
1367 * memory under moderate memory pressure. Anon pages
1368 * are not likely to be evicted by use-once streaming
1369 * IO, plus JVM can create lots of anon VM_EXEC pages,
1370 * so we ignore them here.
1371 */
Wu Fengguang41e20982009-10-26 16:49:53 -07001372 if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
Wu Fengguang8cab4752009-06-16 15:33:12 -07001373 list_add(&page->lru, &l_active);
1374 continue;
1375 }
1376 }
Rik van Riel7e9cd482008-10-18 20:26:35 -07001377
KOSAKI Motohiro5205e562009-09-21 17:01:44 -07001378 ClearPageActive(page); /* we are de-activating */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 list_add(&page->lru, &l_inactive);
1380 }
1381
Andrew Mortonb5557492009-01-06 14:40:13 -08001382 /*
Wu Fengguang8cab4752009-06-16 15:33:12 -07001383 * Move pages back to the lru list.
Andrew Mortonb5557492009-01-06 14:40:13 -08001384 */
Johannes Weiner2a1dc502008-12-01 03:00:35 +01001385 spin_lock_irq(&zone->lru_lock);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001386 /*
Wu Fengguang8cab4752009-06-16 15:33:12 -07001387 * Count referenced pages from currently used mappings as rotated,
1388 * even though only some of them are actually re-activated. This
1389 * helps balance scan pressure between file and anonymous pages in
1390 * get_scan_ratio.
Rik van Riel7e9cd482008-10-18 20:26:35 -07001391 */
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001392 reclaim_stat->recent_rotated[file] += nr_rotated;
Rik van Riel556adec2008-10-18 20:26:34 -07001393
Wu Fengguang3eb41402009-06-16 15:33:13 -07001394 move_active_pages_to_lru(zone, &l_active,
1395 LRU_ACTIVE + file * LRU_FILE);
1396 move_active_pages_to_lru(zone, &l_inactive,
1397 LRU_BASE + file * LRU_FILE);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07001398 __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001399 spin_unlock_irq(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001402static int inactive_anon_is_low_global(struct zone *zone)
KOSAKI Motohirof89eb902009-01-07 18:08:14 -08001403{
1404 unsigned long active, inactive;
1405
1406 active = zone_page_state(zone, NR_ACTIVE_ANON);
1407 inactive = zone_page_state(zone, NR_INACTIVE_ANON);
1408
1409 if (inactive * zone->inactive_ratio < active)
1410 return 1;
1411
1412 return 0;
1413}
1414
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001415/**
1416 * inactive_anon_is_low - check if anonymous pages need to be deactivated
1417 * @zone: zone to check
1418 * @sc: scan control of this context
1419 *
1420 * Returns true if the zone does not have enough inactive anon pages,
1421 * meaning some active anon pages need to be deactivated.
1422 */
1423static int inactive_anon_is_low(struct zone *zone, struct scan_control *sc)
1424{
1425 int low;
1426
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001427 if (scanning_global_lru(sc))
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001428 low = inactive_anon_is_low_global(zone);
1429 else
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001430 low = mem_cgroup_inactive_anon_is_low(sc->mem_cgroup);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001431 return low;
1432}
1433
Rik van Riel56e49d22009-06-16 15:32:28 -07001434static int inactive_file_is_low_global(struct zone *zone)
1435{
1436 unsigned long active, inactive;
1437
1438 active = zone_page_state(zone, NR_ACTIVE_FILE);
1439 inactive = zone_page_state(zone, NR_INACTIVE_FILE);
1440
1441 return (active > inactive);
1442}
1443
1444/**
1445 * inactive_file_is_low - check if file pages need to be deactivated
1446 * @zone: zone to check
1447 * @sc: scan control of this context
1448 *
1449 * When the system is doing streaming IO, memory pressure here
1450 * ensures that active file pages get deactivated, until more
1451 * than half of the file pages are on the inactive list.
1452 *
1453 * Once we get to that situation, protect the system's working
1454 * set from being evicted by disabling active file page aging.
1455 *
1456 * This uses a different ratio than the anonymous pages, because
1457 * the page cache uses a use-once replacement algorithm.
1458 */
1459static int inactive_file_is_low(struct zone *zone, struct scan_control *sc)
1460{
1461 int low;
1462
1463 if (scanning_global_lru(sc))
1464 low = inactive_file_is_low_global(zone);
1465 else
1466 low = mem_cgroup_inactive_file_is_low(sc->mem_cgroup);
1467 return low;
1468}
1469
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001470static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
Christoph Lameterb69408e2008-10-18 20:26:14 -07001471 struct zone *zone, struct scan_control *sc, int priority)
1472{
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001473 int file = is_file_lru(lru);
1474
Rik van Riel56e49d22009-06-16 15:32:28 -07001475 if (lru == LRU_ACTIVE_FILE && inactive_file_is_low(zone, sc)) {
Rik van Riel556adec2008-10-18 20:26:34 -07001476 shrink_active_list(nr_to_scan, zone, sc, priority, file);
1477 return 0;
1478 }
1479
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001480 if (lru == LRU_ACTIVE_ANON && inactive_anon_is_low(zone, sc)) {
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001481 shrink_active_list(nr_to_scan, zone, sc, priority, file);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001482 return 0;
1483 }
Rik van Riel33c120e2008-10-18 20:26:36 -07001484 return shrink_inactive_list(nr_to_scan, zone, sc, priority, file);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001485}
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487/*
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001488 * Determine how aggressively the anon and file LRU lists should be
1489 * scanned. The relative value of each set of LRU lists is determined
1490 * by looking at the fraction of the pages scanned we did rotate back
1491 * onto the active list instead of evict.
1492 *
1493 * percent[0] specifies how much pressure to put on ram/swap backed
1494 * memory, while percent[1] determines pressure on the file LRUs.
1495 */
1496static void get_scan_ratio(struct zone *zone, struct scan_control *sc,
1497 unsigned long *percent)
1498{
1499 unsigned long anon, file, free;
1500 unsigned long anon_prio, file_prio;
1501 unsigned long ap, fp;
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001502 struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001503
Vincent Li0b217672009-09-21 17:03:09 -07001504 anon = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_ANON) +
1505 zone_nr_lru_pages(zone, sc, LRU_INACTIVE_ANON);
1506 file = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_FILE) +
1507 zone_nr_lru_pages(zone, sc, LRU_INACTIVE_FILE);
Hugh Dickinsb9627162009-01-06 14:39:41 -08001508
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001509 if (scanning_global_lru(sc)) {
KOSAKI Motohiroeeee9a82009-01-07 18:08:17 -08001510 free = zone_page_state(zone, NR_FREE_PAGES);
1511 /* If we have very few page cache pages,
1512 force-scan anon pages. */
Mel Gorman41858962009-06-16 15:32:12 -07001513 if (unlikely(file + free <= high_wmark_pages(zone))) {
KOSAKI Motohiroeeee9a82009-01-07 18:08:17 -08001514 percent[0] = 100;
1515 percent[1] = 0;
1516 return;
1517 }
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001518 }
1519
1520 /*
1521 * OK, so we have swap space and a fair amount of page cache
1522 * pages. We use the recently rotated / recently scanned
1523 * ratios to determine how valuable each cache is.
1524 *
1525 * Because workloads change over time (and to avoid overflow)
1526 * we keep these statistics as a floating average, which ends
1527 * up weighing recent references more than old ones.
1528 *
1529 * anon in [0], file in [1]
1530 */
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001531 if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001532 spin_lock_irq(&zone->lru_lock);
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001533 reclaim_stat->recent_scanned[0] /= 2;
1534 reclaim_stat->recent_rotated[0] /= 2;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001535 spin_unlock_irq(&zone->lru_lock);
1536 }
1537
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001538 if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001539 spin_lock_irq(&zone->lru_lock);
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001540 reclaim_stat->recent_scanned[1] /= 2;
1541 reclaim_stat->recent_rotated[1] /= 2;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001542 spin_unlock_irq(&zone->lru_lock);
1543 }
1544
1545 /*
1546 * With swappiness at 100, anonymous and file have the same priority.
1547 * This scanning priority is essentially the inverse of IO cost.
1548 */
1549 anon_prio = sc->swappiness;
1550 file_prio = 200 - sc->swappiness;
1551
1552 /*
Rik van Riel00d80892008-11-19 15:36:44 -08001553 * The amount of pressure on anon vs file pages is inversely
1554 * proportional to the fraction of recently scanned pages on
1555 * each list that were recently referenced and in active use.
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001556 */
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001557 ap = (anon_prio + 1) * (reclaim_stat->recent_scanned[0] + 1);
1558 ap /= reclaim_stat->recent_rotated[0] + 1;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001559
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001560 fp = (file_prio + 1) * (reclaim_stat->recent_scanned[1] + 1);
1561 fp /= reclaim_stat->recent_rotated[1] + 1;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001562
1563 /* Normalize to percentages */
1564 percent[0] = 100 * ap / (ap + fp + 1);
1565 percent[1] = 100 - percent[0];
1566}
1567
Wu Fengguang6e08a362009-06-16 15:32:29 -07001568/*
1569 * Smallish @nr_to_scan's are deposited in @nr_saved_scan,
1570 * until we collected @swap_cluster_max pages to scan.
1571 */
1572static unsigned long nr_scan_try_batch(unsigned long nr_to_scan,
1573 unsigned long *nr_saved_scan,
1574 unsigned long swap_cluster_max)
1575{
1576 unsigned long nr;
1577
1578 *nr_saved_scan += nr_to_scan;
1579 nr = *nr_saved_scan;
1580
1581 if (nr >= swap_cluster_max)
1582 *nr_saved_scan = 0;
1583 else
1584 nr = 0;
1585
1586 return nr;
1587}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001588
1589/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1591 */
Rik van Riela79311c2009-01-06 14:40:01 -08001592static void shrink_zone(int priority, struct zone *zone,
Andrew Morton05ff5132006-03-22 00:08:20 -08001593 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Christoph Lameterb69408e2008-10-18 20:26:14 -07001595 unsigned long nr[NR_LRU_LISTS];
Christoph Lameter86959492006-03-22 00:08:18 -08001596 unsigned long nr_to_scan;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001597 unsigned long percent[2]; /* anon @ 0; file @ 1 */
Christoph Lameterb69408e2008-10-18 20:26:14 -07001598 enum lru_list l;
KOSAKI Motohiro01dbe5c2009-01-06 14:40:02 -08001599 unsigned long nr_reclaimed = sc->nr_reclaimed;
1600 unsigned long swap_cluster_max = sc->swap_cluster_max;
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08001601 unsigned long nr_to_reclaim = sc->nr_to_reclaim;
Wu Fengguangf8629632009-09-21 17:03:11 -07001602 struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
Daisuke Nishimura9198e962009-06-16 15:33:15 -07001603 int noswap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Daisuke Nishimura9198e962009-06-16 15:33:15 -07001605 /* If we have no swap space, do not bother scanning anon pages. */
1606 if (!sc->may_swap || (nr_swap_pages <= 0)) {
1607 noswap = 1;
1608 percent[0] = 0;
1609 percent[1] = 100;
1610 } else
1611 get_scan_ratio(zone, sc, percent);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001612
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001613 for_each_evictable_lru(l) {
KOSAKI Motohiro9439c1c2009-01-07 18:08:21 -08001614 int file = is_file_lru(l);
Andrew Morton8713e012009-04-30 15:08:55 -07001615 unsigned long scan;
Johannes Weinere0f79b82008-10-18 20:26:55 -07001616
Vincent Li0b217672009-09-21 17:03:09 -07001617 scan = zone_nr_lru_pages(zone, sc, l);
Daisuke Nishimura9198e962009-06-16 15:33:15 -07001618 if (priority || noswap) {
KOSAKI Motohiro9439c1c2009-01-07 18:08:21 -08001619 scan >>= priority;
1620 scan = (scan * percent[file]) / 100;
1621 }
Wu Fengguangf8629632009-09-21 17:03:11 -07001622 nr[l] = nr_scan_try_batch(scan,
1623 &reclaim_stat->nr_saved_scan[l],
1624 swap_cluster_max);
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001625 }
1626
Rik van Riel556adec2008-10-18 20:26:34 -07001627 while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
1628 nr[LRU_INACTIVE_FILE]) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001629 for_each_evictable_lru(l) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07001630 if (nr[l]) {
KOSAKI Motohiro01dbe5c2009-01-06 14:40:02 -08001631 nr_to_scan = min(nr[l], swap_cluster_max);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001632 nr[l] -= nr_to_scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
KOSAKI Motohiro01dbe5c2009-01-06 14:40:02 -08001634 nr_reclaimed += shrink_list(l, nr_to_scan,
1635 zone, sc, priority);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
Rik van Riela79311c2009-01-06 14:40:01 -08001638 /*
1639 * On large memory systems, scan >> priority can become
1640 * really large. This is fine for the starting priority;
1641 * we want to put equal scanning pressure on each zone.
1642 * However, if the VM has a harder time of freeing pages,
1643 * with multiple processes reclaiming pages, the total
1644 * freeing target can get unreasonably large.
1645 */
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08001646 if (nr_reclaimed > nr_to_reclaim && priority < DEF_PRIORITY)
Rik van Riela79311c2009-01-06 14:40:01 -08001647 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
KOSAKI Motohiro01dbe5c2009-01-06 14:40:02 -08001650 sc->nr_reclaimed = nr_reclaimed;
1651
Rik van Riel556adec2008-10-18 20:26:34 -07001652 /*
1653 * Even if we did not try to evict anon pages at all, we want to
1654 * rebalance the anon lru active/inactive ratio.
1655 */
MinChan Kim69c85482009-06-16 15:32:44 -07001656 if (inactive_anon_is_low(zone, sc) && nr_swap_pages > 0)
Rik van Riel556adec2008-10-18 20:26:34 -07001657 shrink_active_list(SWAP_CLUSTER_MAX, zone, sc, priority, 0);
1658
Andrew Morton232ea4d2007-02-28 20:13:21 -08001659 throttle_vm_writeout(sc->gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
1662/*
1663 * This is the direct reclaim path, for page-allocating processes. We only
1664 * try to reclaim pages from zones which will satisfy the caller's allocation
1665 * request.
1666 *
Mel Gorman41858962009-06-16 15:32:12 -07001667 * We reclaim from a zone even if that zone is over high_wmark_pages(zone).
1668 * Because:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1670 * allocation or
Mel Gorman41858962009-06-16 15:32:12 -07001671 * b) The target zone may be at high_wmark_pages(zone) but the lower zones
1672 * must go *over* high_wmark_pages(zone) to satisfy the `incremental min'
1673 * zone defense algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 * If a zone is deemed to be full of pinned pages then just give it a light
1676 * scan then give up on it.
1677 */
Rik van Riela79311c2009-01-06 14:40:01 -08001678static void shrink_zones(int priority, struct zonelist *zonelist,
Andrew Morton05ff5132006-03-22 00:08:20 -08001679 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
Mel Gorman54a6eb52008-04-28 02:12:16 -07001681 enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask);
Mel Gormandd1a2392008-04-28 02:12:17 -07001682 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001683 struct zone *zone;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001684
Nick Piggin408d8542006-09-25 23:31:27 -07001685 sc->all_unreclaimable = 1;
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07001686 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1687 sc->nodemask) {
Con Kolivasf3fe6512006-01-06 00:11:15 -08001688 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 continue;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001690 /*
1691 * Take care memory controller reclaiming has small influence
1692 * to global LRU.
1693 */
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001694 if (scanning_global_lru(sc)) {
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001695 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1696 continue;
1697 note_zone_scanning_priority(zone, priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001699 if (zone_is_all_unreclaimable(zone) &&
1700 priority != DEF_PRIORITY)
1701 continue; /* Let kswapd poll it */
1702 sc->all_unreclaimable = 0;
1703 } else {
1704 /*
1705 * Ignore cpuset limitation here. We just want to reduce
1706 * # of used pages by us regardless of memory shortage.
1707 */
1708 sc->all_unreclaimable = 0;
1709 mem_cgroup_note_reclaim_priority(sc->mem_cgroup,
1710 priority);
1711 }
Nick Piggin408d8542006-09-25 23:31:27 -07001712
Rik van Riela79311c2009-01-06 14:40:01 -08001713 shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717/*
1718 * This is the main entry point to direct page reclaim.
1719 *
1720 * If a full scan of the inactive list fails to free enough memory then we
1721 * are "out of memory" and something needs to be killed.
1722 *
1723 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1724 * high - the zone may be full of dirty or under-writeback pages, which this
Jens Axboe5b0830c2009-09-23 19:37:09 +02001725 * caller can't do much about. We kick the writeback threads and take explicit
1726 * naps in the hope that some of these pages can be written. But if the
1727 * allocating task holds filesystem locks which prevent writeout this might not
1728 * work, and the allocation attempt will fail.
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07001729 *
1730 * returns: 0, if no pages reclaimed
1731 * else, the number of pages reclaimed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 */
Mel Gormandac1d272008-04-28 02:12:12 -07001733static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
Mel Gormandd1a2392008-04-28 02:12:17 -07001734 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 int priority;
kosaki.motohiro@jp.fujitsu.comc700be32008-06-12 15:21:27 -07001737 unsigned long ret = 0;
Andrew Morton69e05942006-03-22 00:08:19 -08001738 unsigned long total_scanned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 struct reclaim_state *reclaim_state = current->reclaim_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 unsigned long lru_pages = 0;
Mel Gormandd1a2392008-04-28 02:12:17 -07001741 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001742 struct zone *zone;
Mel Gormandd1a2392008-04-28 02:12:17 -07001743 enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask);
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08001744 unsigned long writeback_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Keika Kobayashi873b4772008-07-25 01:48:52 -07001746 delayacct_freepages_start();
1747
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001748 if (scanning_global_lru(sc))
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001749 count_vm_event(ALLOCSTALL);
1750 /*
1751 * mem_cgroup will not do shrink_slab.
1752 */
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001753 if (scanning_global_lru(sc)) {
Mel Gorman54a6eb52008-04-28 02:12:16 -07001754 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001756 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1757 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Wu Fengguangadea02a2009-09-21 17:01:42 -07001759 lru_pages += zone_reclaimable_pages(zone);
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762
1763 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
Balbir Singh66e17072008-02-07 00:13:56 -08001764 sc->nr_scanned = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001765 if (!priority)
1766 disable_swap_token();
Rik van Riela79311c2009-01-06 14:40:01 -08001767 shrink_zones(priority, zonelist, sc);
Balbir Singh66e17072008-02-07 00:13:56 -08001768 /*
1769 * Don't shrink slabs when reclaiming memory from
1770 * over limit cgroups
1771 */
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001772 if (scanning_global_lru(sc)) {
Mel Gormandd1a2392008-04-28 02:12:17 -07001773 shrink_slab(sc->nr_scanned, sc->gfp_mask, lru_pages);
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -08001774 if (reclaim_state) {
Rik van Riela79311c2009-01-06 14:40:01 -08001775 sc->nr_reclaimed += reclaim_state->reclaimed_slab;
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -08001776 reclaim_state->reclaimed_slab = 0;
1777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
Balbir Singh66e17072008-02-07 00:13:56 -08001779 total_scanned += sc->nr_scanned;
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08001780 if (sc->nr_reclaimed >= sc->nr_to_reclaim) {
Rik van Riela79311c2009-01-06 14:40:01 -08001781 ret = sc->nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 goto out;
1783 }
1784
1785 /*
1786 * Try to write back as many pages as we just scanned. This
1787 * tends to cause slow streaming writers to write data to the
1788 * disk smoothly, at the dirtying rate, which is nice. But
1789 * that's undesirable in laptop mode, where we *want* lumpy
1790 * writeout. So in laptop mode, write out the whole world.
1791 */
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08001792 writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
1793 if (total_scanned > writeback_threshold) {
Jens Axboe03ba3782009-09-09 09:08:54 +02001794 wakeup_flusher_threads(laptop_mode ? 0 : total_scanned);
Balbir Singh66e17072008-02-07 00:13:56 -08001795 sc->may_writepage = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797
1798 /* Take a nap, wait for some writeback to complete */
Andrew Morton4dd4b922008-03-24 12:29:52 -07001799 if (sc->nr_scanned && priority < DEF_PRIORITY - 2)
Jens Axboe8aa7e842009-07-09 14:52:32 +02001800 congestion_wait(BLK_RW_ASYNC, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
Fernando Luis Vazquez Cao87547ee2008-07-29 22:33:42 -07001802 /* top priority shrink_zones still had more to do? don't OOM, then */
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001803 if (!sc->all_unreclaimable && scanning_global_lru(sc))
Rik van Riela79311c2009-01-06 14:40:01 -08001804 ret = sc->nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805out:
Martin Bligh3bb1a852006-10-28 10:38:24 -07001806 /*
1807 * Now that we've scanned all the zones at this priority level, note
1808 * that level within the zone so that the next thread which performs
1809 * scanning of this zone will immediately start out at this priority
1810 * level. This affects only the decision whether or not to bring
1811 * mapped pages onto the inactive list.
1812 */
1813 if (priority < 0)
1814 priority = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001816 if (scanning_global_lru(sc)) {
Mel Gorman54a6eb52008-04-28 02:12:16 -07001817 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001819 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1820 continue;
1821
1822 zone->prev_priority = priority;
1823 }
1824 } else
1825 mem_cgroup_record_reclaim_priority(sc->mem_cgroup, priority);
1826
Keika Kobayashi873b4772008-07-25 01:48:52 -07001827 delayacct_freepages_end();
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return ret;
1830}
1831
Mel Gormandac1d272008-04-28 02:12:12 -07001832unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07001833 gfp_t gfp_mask, nodemask_t *nodemask)
Balbir Singh66e17072008-02-07 00:13:56 -08001834{
1835 struct scan_control sc = {
1836 .gfp_mask = gfp_mask,
1837 .may_writepage = !laptop_mode,
1838 .swap_cluster_max = SWAP_CLUSTER_MAX,
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08001839 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Johannes Weinera6dc60f2009-03-31 15:19:30 -07001840 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07001841 .may_swap = 1,
Balbir Singh66e17072008-02-07 00:13:56 -08001842 .swappiness = vm_swappiness,
1843 .order = order,
1844 .mem_cgroup = NULL,
1845 .isolate_pages = isolate_pages_global,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07001846 .nodemask = nodemask,
Balbir Singh66e17072008-02-07 00:13:56 -08001847 };
1848
Mel Gormandd1a2392008-04-28 02:12:17 -07001849 return do_try_to_free_pages(zonelist, &sc);
Balbir Singh66e17072008-02-07 00:13:56 -08001850}
1851
Balbir Singh00f0b822008-03-04 14:28:39 -08001852#ifdef CONFIG_CGROUP_MEM_RES_CTLR
Balbir Singh66e17072008-02-07 00:13:56 -08001853
Balbir Singh4e416952009-09-23 15:56:39 -07001854unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *mem,
1855 gfp_t gfp_mask, bool noswap,
1856 unsigned int swappiness,
1857 struct zone *zone, int nid)
1858{
1859 struct scan_control sc = {
1860 .may_writepage = !laptop_mode,
1861 .may_unmap = 1,
1862 .may_swap = !noswap,
1863 .swap_cluster_max = SWAP_CLUSTER_MAX,
1864 .swappiness = swappiness,
1865 .order = 0,
1866 .mem_cgroup = mem,
1867 .isolate_pages = mem_cgroup_isolate_pages,
1868 };
1869 nodemask_t nm = nodemask_of_node(nid);
1870
1871 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
1872 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
1873 sc.nodemask = &nm;
1874 sc.nr_reclaimed = 0;
1875 sc.nr_scanned = 0;
1876 /*
1877 * NOTE: Although we can get the priority field, using it
1878 * here is not a good idea, since it limits the pages we can scan.
1879 * if we don't reclaim here, the shrink_zone from balance_pgdat
1880 * will pick up pages from other mem cgroup's as well. We hack
1881 * the priority and make it zero.
1882 */
1883 shrink_zone(0, zone, &sc);
1884 return sc.nr_reclaimed;
1885}
1886
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001887unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001888 gfp_t gfp_mask,
1889 bool noswap,
1890 unsigned int swappiness)
Balbir Singh66e17072008-02-07 00:13:56 -08001891{
Balbir Singh4e416952009-09-23 15:56:39 -07001892 struct zonelist *zonelist;
Balbir Singh66e17072008-02-07 00:13:56 -08001893 struct scan_control sc = {
Balbir Singh66e17072008-02-07 00:13:56 -08001894 .may_writepage = !laptop_mode,
Johannes Weinera6dc60f2009-03-31 15:19:30 -07001895 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07001896 .may_swap = !noswap,
Balbir Singh66e17072008-02-07 00:13:56 -08001897 .swap_cluster_max = SWAP_CLUSTER_MAX,
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08001898 .nr_to_reclaim = SWAP_CLUSTER_MAX,
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001899 .swappiness = swappiness,
Balbir Singh66e17072008-02-07 00:13:56 -08001900 .order = 0,
1901 .mem_cgroup = mem_cont,
1902 .isolate_pages = mem_cgroup_isolate_pages,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07001903 .nodemask = NULL, /* we don't care the placement */
Balbir Singh66e17072008-02-07 00:13:56 -08001904 };
Balbir Singh66e17072008-02-07 00:13:56 -08001905
Mel Gormandd1a2392008-04-28 02:12:17 -07001906 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
1907 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
1908 zonelist = NODE_DATA(numa_node_id())->node_zonelists;
1909 return do_try_to_free_pages(zonelist, &sc);
Balbir Singh66e17072008-02-07 00:13:56 -08001910}
1911#endif
1912
Mel Gormanf50de2d2009-12-14 17:58:53 -08001913/* is kswapd sleeping prematurely? */
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08001914static int sleeping_prematurely(pg_data_t *pgdat, int order, long remaining)
Mel Gormanf50de2d2009-12-14 17:58:53 -08001915{
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08001916 int i;
Mel Gormanf50de2d2009-12-14 17:58:53 -08001917
1918 /* If a direct reclaimer woke kswapd within HZ/10, it's premature */
1919 if (remaining)
1920 return 1;
1921
1922 /* If after HZ/10, a zone is below the high mark, it's premature */
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08001923 for (i = 0; i < pgdat->nr_zones; i++) {
1924 struct zone *zone = pgdat->node_zones + i;
1925
1926 if (!populated_zone(zone))
1927 continue;
1928
Mel Gormanf50de2d2009-12-14 17:58:53 -08001929 if (!zone_watermark_ok(zone, order, high_wmark_pages(zone),
1930 0, 0))
1931 return 1;
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08001932 }
Mel Gormanf50de2d2009-12-14 17:58:53 -08001933
1934 return 0;
1935}
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937/*
1938 * For kswapd, balance_pgdat() will work across all this node's zones until
Mel Gorman41858962009-06-16 15:32:12 -07001939 * they are all at high_wmark_pages(zone).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 * Returns the number of pages which were actually freed.
1942 *
1943 * There is special handling here for zones which are full of pinned pages.
1944 * This can happen if the pages are all mlocked, or if they are all used by
1945 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
1946 * What we do is to detect the case where all pages in the zone have been
1947 * scanned twice and there has been zero successful reclaim. Mark the zone as
1948 * dead and from now on, only perform a short scan. Basically we're polling
1949 * the zone for when the problem goes away.
1950 *
1951 * kswapd scans the zones in the highmem->normal->dma direction. It skips
Mel Gorman41858962009-06-16 15:32:12 -07001952 * zones which have free_pages > high_wmark_pages(zone), but once a zone is
1953 * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the
1954 * lower zones regardless of the number of free pages in the lower zones. This
1955 * interoperates with the page allocator fallback scheme to ensure that aging
1956 * of pages is balanced across the zones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001958static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 int all_zones_ok;
1961 int priority;
1962 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08001963 unsigned long total_scanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08001965 struct scan_control sc = {
1966 .gfp_mask = GFP_KERNEL,
Johannes Weinera6dc60f2009-03-31 15:19:30 -07001967 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07001968 .may_swap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001969 .swap_cluster_max = SWAP_CLUSTER_MAX,
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08001970 /*
1971 * kswapd doesn't want to be bailed out while reclaim. because
1972 * we want to put equal scanning pressure on each zone.
1973 */
1974 .nr_to_reclaim = ULONG_MAX,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07001975 .swappiness = vm_swappiness,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001976 .order = order,
Balbir Singh66e17072008-02-07 00:13:56 -08001977 .mem_cgroup = NULL,
1978 .isolate_pages = isolate_pages_global,
Andrew Morton179e9632006-03-22 00:08:18 -08001979 };
Martin Bligh3bb1a852006-10-28 10:38:24 -07001980 /*
1981 * temp_priority is used to remember the scanning priority at which
Mel Gorman41858962009-06-16 15:32:12 -07001982 * this zone was successfully refilled to
1983 * free_pages == high_wmark_pages(zone).
Martin Bligh3bb1a852006-10-28 10:38:24 -07001984 */
1985 int temp_priority[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987loop_again:
1988 total_scanned = 0;
Rik van Riela79311c2009-01-06 14:40:01 -08001989 sc.nr_reclaimed = 0;
Christoph Lameterc0bbbc72006-06-11 15:22:26 -07001990 sc.may_writepage = !laptop_mode;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001991 count_vm_event(PAGEOUTRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Martin Bligh3bb1a852006-10-28 10:38:24 -07001993 for (i = 0; i < pgdat->nr_zones; i++)
1994 temp_priority[i] = DEF_PRIORITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
1997 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
1998 unsigned long lru_pages = 0;
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08001999 int has_under_min_watermark_zone = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Rik van Rielf7b7fd82005-11-28 13:44:07 -08002001 /* The swap token gets in the way of swapout... */
2002 if (!priority)
2003 disable_swap_token();
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 all_zones_ok = 1;
2006
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002007 /*
2008 * Scan in the highmem->dma direction for the highest
2009 * zone which needs scanning
2010 */
2011 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
2012 struct zone *zone = pgdat->node_zones + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002014 if (!populated_zone(zone))
2015 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
David Rientjese815af92007-10-16 23:25:54 -07002017 if (zone_is_all_unreclaimable(zone) &&
2018 priority != DEF_PRIORITY)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002019 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Rik van Riel556adec2008-10-18 20:26:34 -07002021 /*
2022 * Do some background aging of the anon list, to give
2023 * pages a chance to be referenced before reclaiming.
2024 */
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002025 if (inactive_anon_is_low(zone, &sc))
Rik van Riel556adec2008-10-18 20:26:34 -07002026 shrink_active_list(SWAP_CLUSTER_MAX, zone,
2027 &sc, priority, 0);
2028
Mel Gorman41858962009-06-16 15:32:12 -07002029 if (!zone_watermark_ok(zone, order,
2030 high_wmark_pages(zone), 0, 0)) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002031 end_zone = i;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08002032 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Andrew Mortone1dbeda2006-12-06 20:32:01 -08002035 if (i < 0)
2036 goto out;
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 for (i = 0; i <= end_zone; i++) {
2039 struct zone *zone = pgdat->node_zones + i;
2040
Wu Fengguangadea02a2009-09-21 17:01:42 -07002041 lru_pages += zone_reclaimable_pages(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043
2044 /*
2045 * Now scan the zone in the dma->highmem direction, stopping
2046 * at the last zone which needs scanning.
2047 *
2048 * We do this because the page allocator works in the opposite
2049 * direction. This prevents the page allocator from allocating
2050 * pages behind kswapd's direction of progress, which would
2051 * cause too much scanning of the lower zones.
2052 */
2053 for (i = 0; i <= end_zone; i++) {
2054 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07002055 int nr_slab;
Balbir Singh4e416952009-09-23 15:56:39 -07002056 int nid, zid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Con Kolivasf3fe6512006-01-06 00:11:15 -08002058 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 continue;
2060
David Rientjese815af92007-10-16 23:25:54 -07002061 if (zone_is_all_unreclaimable(zone) &&
2062 priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 continue;
2064
Mel Gorman41858962009-06-16 15:32:12 -07002065 if (!zone_watermark_ok(zone, order,
2066 high_wmark_pages(zone), end_zone, 0))
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002067 all_zones_ok = 0;
Martin Bligh3bb1a852006-10-28 10:38:24 -07002068 temp_priority[i] = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 sc.nr_scanned = 0;
Martin Bligh3bb1a852006-10-28 10:38:24 -07002070 note_zone_scanning_priority(zone, priority);
Balbir Singh4e416952009-09-23 15:56:39 -07002071
2072 nid = pgdat->node_id;
2073 zid = zone_idx(zone);
2074 /*
2075 * Call soft limit reclaim before calling shrink_zone.
2076 * For now we ignore the return value
2077 */
2078 mem_cgroup_soft_limit_reclaim(zone, order, sc.gfp_mask,
2079 nid, zid);
Rik van Riel32a43302007-10-16 01:24:50 -07002080 /*
2081 * We put equal pressure on every zone, unless one
2082 * zone has way too many pages free already.
2083 */
Mel Gorman41858962009-06-16 15:32:12 -07002084 if (!zone_watermark_ok(zone, order,
2085 8*high_wmark_pages(zone), end_zone, 0))
Rik van Riela79311c2009-01-06 14:40:01 -08002086 shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07002088 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
2089 lru_pages);
Rik van Riela79311c2009-01-06 14:40:01 -08002090 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 total_scanned += sc.nr_scanned;
David Rientjese815af92007-10-16 23:25:54 -07002092 if (zone_is_all_unreclaimable(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 continue;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07002094 if (nr_slab == 0 && zone->pages_scanned >=
Wu Fengguangadea02a2009-09-21 17:01:42 -07002095 (zone_reclaimable_pages(zone) * 6))
David Rientjese815af92007-10-16 23:25:54 -07002096 zone_set_flag(zone,
2097 ZONE_ALL_UNRECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 /*
2099 * If we've done a decent amount of scanning and
2100 * the reclaim ratio is low, start doing writepage
2101 * even in laptop mode
2102 */
2103 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Rik van Riela79311c2009-01-06 14:40:01 -08002104 total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 sc.may_writepage = 1;
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002106
2107 /*
2108 * We are still under min water mark. it mean we have
2109 * GFP_ATOMIC allocation failure risk. Hurry up!
2110 */
2111 if (!zone_watermark_ok(zone, order, min_wmark_pages(zone),
2112 end_zone, 0))
2113 has_under_min_watermark_zone = 1;
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (all_zones_ok)
2117 break; /* kswapd: all done */
2118 /*
2119 * OK, kswapd is getting into trouble. Take a nap, then take
2120 * another pass across the zones.
2121 */
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002122 if (total_scanned && (priority < DEF_PRIORITY - 2)) {
2123 if (has_under_min_watermark_zone)
2124 count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT);
2125 else
2126 congestion_wait(BLK_RW_ASYNC, HZ/10);
2127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 /*
2130 * We do this so kswapd doesn't build up large priorities for
2131 * example when it is freeing in parallel with allocators. It
2132 * matches the direct reclaim path behaviour in terms of impact
2133 * on zone->*_priority.
2134 */
Rik van Riela79311c2009-01-06 14:40:01 -08002135 if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 break;
2137 }
2138out:
Martin Bligh3bb1a852006-10-28 10:38:24 -07002139 /*
2140 * Note within each zone the priority level at which this zone was
2141 * brought into a happy state. So that the next thread which scans this
2142 * zone will start out at that priority level.
2143 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 for (i = 0; i < pgdat->nr_zones; i++) {
2145 struct zone *zone = pgdat->node_zones + i;
2146
Martin Bligh3bb1a852006-10-28 10:38:24 -07002147 zone->prev_priority = temp_priority[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149 if (!all_zones_ok) {
2150 cond_resched();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002151
2152 try_to_freeze();
2153
KOSAKI Motohiro73ce02e2009-01-06 14:40:33 -08002154 /*
2155 * Fragmentation may mean that the system cannot be
2156 * rebalanced for high-order allocations in all zones.
2157 * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
2158 * it means the zones have been fully scanned and are still
2159 * not balanced. For high-order allocations, there is
2160 * little point trying all over again as kswapd may
2161 * infinite loop.
2162 *
2163 * Instead, recheck all watermarks at order-0 as they
2164 * are the most important. If watermarks are ok, kswapd will go
2165 * back to sleep. High-order users can still perform direct
2166 * reclaim if they wish.
2167 */
2168 if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
2169 order = sc.order = 0;
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 goto loop_again;
2172 }
2173
Rik van Riela79311c2009-01-06 14:40:01 -08002174 return sc.nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
2177/*
2178 * The background pageout daemon, started as a kernel thread
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002179 * from the init process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 *
2181 * This basically trickles out pages so that we have _some_
2182 * free memory available even if there is no other activity
2183 * that frees anything up. This is needed for things like routing
2184 * etc, where we otherwise might have all activity going on in
2185 * asynchronous contexts that cannot page things out.
2186 *
2187 * If there are applications that are active memory-allocators
2188 * (most normal use), this basically shouldn't matter.
2189 */
2190static int kswapd(void *p)
2191{
2192 unsigned long order;
2193 pg_data_t *pgdat = (pg_data_t*)p;
2194 struct task_struct *tsk = current;
2195 DEFINE_WAIT(wait);
2196 struct reclaim_state reclaim_state = {
2197 .reclaimed_slab = 0,
2198 };
Rusty Russella70f7302009-03-13 14:49:46 +10302199 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Nick Piggincf40bd12009-01-21 08:12:39 +01002201 lockdep_set_current_reclaim_state(GFP_KERNEL);
2202
Rusty Russell174596a2009-01-01 10:12:29 +10302203 if (!cpumask_empty(cpumask))
Mike Travisc5f59f02008-04-04 18:11:10 -07002204 set_cpus_allowed_ptr(tsk, cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 current->reclaim_state = &reclaim_state;
2206
2207 /*
2208 * Tell the memory management that we're a "memory allocator",
2209 * and that if we need more memory we should get access to it
2210 * regardless (see "__alloc_pages()"). "kswapd" should
2211 * never get caught in the normal page freeing logic.
2212 *
2213 * (Kswapd normally doesn't need memory anyway, but sometimes
2214 * you need a small amount of memory in order to be able to
2215 * page out something else, and this flag essentially protects
2216 * us from recursively trying to free more memory as we're
2217 * trying to free the first piece of memory in the first place).
2218 */
Christoph Lameter930d9152006-01-08 01:00:47 -08002219 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Rafael J. Wysocki83144182007-07-17 04:03:35 -07002220 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 order = 0;
2223 for ( ; ; ) {
2224 unsigned long new_order;
David Rientjes8fe23e02009-12-14 17:58:33 -08002225 int ret;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2228 new_order = pgdat->kswapd_max_order;
2229 pgdat->kswapd_max_order = 0;
2230 if (order < new_order) {
2231 /*
2232 * Don't sleep if someone wants a larger 'order'
2233 * allocation
2234 */
2235 order = new_order;
2236 } else {
Mel Gormanf50de2d2009-12-14 17:58:53 -08002237 if (!freezing(current) && !kthread_should_stop()) {
2238 long remaining = 0;
2239
2240 /* Try to sleep for a short interval */
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002241 if (!sleeping_prematurely(pgdat, order, remaining)) {
Mel Gormanf50de2d2009-12-14 17:58:53 -08002242 remaining = schedule_timeout(HZ/10);
2243 finish_wait(&pgdat->kswapd_wait, &wait);
2244 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2245 }
2246
2247 /*
2248 * After a short sleep, check if it was a
2249 * premature sleep. If not, then go fully
2250 * to sleep until explicitly woken up
2251 */
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002252 if (!sleeping_prematurely(pgdat, order, remaining))
Mel Gormanf50de2d2009-12-14 17:58:53 -08002253 schedule();
2254 else {
2255 if (remaining)
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002256 count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
Mel Gormanf50de2d2009-12-14 17:58:53 -08002257 else
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002258 count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
Mel Gormanf50de2d2009-12-14 17:58:53 -08002259 }
2260 }
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 order = pgdat->kswapd_max_order;
2263 }
2264 finish_wait(&pgdat->kswapd_wait, &wait);
2265
David Rientjes8fe23e02009-12-14 17:58:33 -08002266 ret = try_to_freeze();
2267 if (kthread_should_stop())
2268 break;
2269
2270 /*
2271 * We can speed up thawing tasks if we don't call balance_pgdat
2272 * after returning from the refrigerator
2273 */
2274 if (!ret)
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07002275 balance_pgdat(pgdat, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
2277 return 0;
2278}
2279
2280/*
2281 * A zone is low on free memory, so wake its kswapd task to service it.
2282 */
2283void wakeup_kswapd(struct zone *zone, int order)
2284{
2285 pg_data_t *pgdat;
2286
Con Kolivasf3fe6512006-01-06 00:11:15 -08002287 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return;
2289
2290 pgdat = zone->zone_pgdat;
Mel Gorman41858962009-06-16 15:32:12 -07002291 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return;
2293 if (pgdat->kswapd_max_order < order)
2294 pgdat->kswapd_max_order = order;
Paul Jackson02a0e532006-12-13 00:34:25 -08002295 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07002297 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07002299 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
Wu Fengguangadea02a2009-09-21 17:01:42 -07002302/*
2303 * The reclaimable count would be mostly accurate.
2304 * The less reclaimable pages may be
2305 * - mlocked pages, which will be moved to unevictable list when encountered
2306 * - mapped pages, which may require several travels to be reclaimed
2307 * - dirty pages, which is not "instantly" reclaimable
2308 */
2309unsigned long global_reclaimable_pages(void)
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002310{
Wu Fengguangadea02a2009-09-21 17:01:42 -07002311 int nr;
2312
2313 nr = global_page_state(NR_ACTIVE_FILE) +
2314 global_page_state(NR_INACTIVE_FILE);
2315
2316 if (nr_swap_pages > 0)
2317 nr += global_page_state(NR_ACTIVE_ANON) +
2318 global_page_state(NR_INACTIVE_ANON);
2319
2320 return nr;
2321}
2322
2323unsigned long zone_reclaimable_pages(struct zone *zone)
2324{
2325 int nr;
2326
2327 nr = zone_page_state(zone, NR_ACTIVE_FILE) +
2328 zone_page_state(zone, NR_INACTIVE_FILE);
2329
2330 if (nr_swap_pages > 0)
2331 nr += zone_page_state(zone, NR_ACTIVE_ANON) +
2332 zone_page_state(zone, NR_INACTIVE_ANON);
2333
2334 return nr;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002335}
2336
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02002337#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338/*
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002339 * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
MinChan Kimd9796772009-03-31 15:19:34 -07002340 * from LRU lists system-wide, for given pass and priority.
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002341 *
2342 * For pass > 3 we also try to shrink the LRU lists that contain a few pages
2343 */
MinChan Kimd9796772009-03-31 15:19:34 -07002344static void shrink_all_zones(unsigned long nr_pages, int prio,
Nigel Cunninghame07aa052006-12-22 01:07:21 -08002345 int pass, struct scan_control *sc)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002346{
2347 struct zone *zone;
MinChan Kimd9796772009-03-31 15:19:34 -07002348 unsigned long nr_reclaimed = 0;
Wu Fengguangf8629632009-09-21 17:03:11 -07002349 struct zone_reclaim_stat *reclaim_stat;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002350
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07002351 for_each_populated_zone(zone) {
Johannes Weiner0cb57252009-02-14 02:04:10 +01002352 enum lru_list l;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002353
David Rientjese815af92007-10-16 23:25:54 -07002354 if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002355 continue;
2356
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002357 for_each_evictable_lru(l) {
Johannes Weiner0cb57252009-02-14 02:04:10 +01002358 enum zone_stat_item ls = NR_LRU_BASE + l;
2359 unsigned long lru_pages = zone_page_state(zone, ls);
2360
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002361 /* For pass = 0, we don't shrink the active list */
Johannes Weiner0cb57252009-02-14 02:04:10 +01002362 if (pass == 0 && (l == LRU_ACTIVE_ANON ||
2363 l == LRU_ACTIVE_FILE))
Christoph Lameterb69408e2008-10-18 20:26:14 -07002364 continue;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002365
Wu Fengguangf8629632009-09-21 17:03:11 -07002366 reclaim_stat = get_reclaim_stat(zone, sc);
2367 reclaim_stat->nr_saved_scan[l] +=
2368 (lru_pages >> prio) + 1;
2369 if (reclaim_stat->nr_saved_scan[l]
2370 >= nr_pages || pass > 3) {
Johannes Weiner0cb57252009-02-14 02:04:10 +01002371 unsigned long nr_to_scan;
2372
Wu Fengguangf8629632009-09-21 17:03:11 -07002373 reclaim_stat->nr_saved_scan[l] = 0;
Johannes Weiner0cb57252009-02-14 02:04:10 +01002374 nr_to_scan = min(nr_pages, lru_pages);
MinChan Kimd9796772009-03-31 15:19:34 -07002375 nr_reclaimed += shrink_list(l, nr_to_scan, zone,
Christoph Lameterb69408e2008-10-18 20:26:14 -07002376 sc, prio);
MinChan Kimd9796772009-03-31 15:19:34 -07002377 if (nr_reclaimed >= nr_pages) {
Rafael J. Wysockia21e2552009-04-18 17:23:41 +02002378 sc->nr_reclaimed += nr_reclaimed;
MinChan Kimd9796772009-03-31 15:19:34 -07002379 return;
2380 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07002381 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002382 }
2383 }
Rafael J. Wysockia21e2552009-04-18 17:23:41 +02002384 sc->nr_reclaimed += nr_reclaimed;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002385}
2386
2387/*
2388 * Try to free `nr_pages' of memory, system-wide, and return the number of
2389 * freed pages.
2390 *
2391 * Rather than trying to age LRUs the aim is to preserve the overall
2392 * LRU order by reclaiming preferentially
2393 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 */
Andrew Morton69e05942006-03-22 00:08:19 -08002395unsigned long shrink_all_memory(unsigned long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002397 unsigned long lru_pages, nr_slab;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002398 int pass;
2399 struct reclaim_state reclaim_state;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002400 struct scan_control sc = {
2401 .gfp_mask = GFP_KERNEL,
Johannes Weinera6dc60f2009-03-31 15:19:30 -07002402 .may_unmap = 0,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002403 .may_writepage = 1,
Balbir Singh66e17072008-02-07 00:13:56 -08002404 .isolate_pages = isolate_pages_global,
Rafael J. Wysockia21e2552009-04-18 17:23:41 +02002405 .nr_reclaimed = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 };
2407
2408 current->reclaim_state = &reclaim_state;
Andrew Morton69e05942006-03-22 00:08:19 -08002409
Wu Fengguangadea02a2009-09-21 17:01:42 -07002410 lru_pages = global_reclaimable_pages();
Christoph Lameter972d1a72006-09-25 23:31:51 -07002411 nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002412 /* If slab caches are huge, it's better to hit them first */
2413 while (nr_slab >= lru_pages) {
2414 reclaim_state.reclaimed_slab = 0;
2415 shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
2416 if (!reclaim_state.reclaimed_slab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 break;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002418
MinChan Kimd9796772009-03-31 15:19:34 -07002419 sc.nr_reclaimed += reclaim_state.reclaimed_slab;
2420 if (sc.nr_reclaimed >= nr_pages)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002421 goto out;
2422
2423 nr_slab -= reclaim_state.reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002425
2426 /*
2427 * We try to shrink LRUs in 5 passes:
2428 * 0 = Reclaim from inactive_list only
2429 * 1 = Reclaim from active list but don't reclaim mapped
2430 * 2 = 2nd pass of type 1
2431 * 3 = Reclaim mapped (normal reclaim)
2432 * 4 = 2nd pass of type 3
2433 */
2434 for (pass = 0; pass < 5; pass++) {
2435 int prio;
2436
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002437 /* Force reclaiming mapped pages in the passes #3 and #4 */
Johannes Weiner30491032009-02-14 02:03:08 +01002438 if (pass > 2)
Johannes Weinera6dc60f2009-03-31 15:19:30 -07002439 sc.may_unmap = 1;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002440
2441 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
MinChan Kimd9796772009-03-31 15:19:34 -07002442 unsigned long nr_to_scan = nr_pages - sc.nr_reclaimed;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002443
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002444 sc.nr_scanned = 0;
Johannes Weiner9786bf82009-03-31 15:19:35 -07002445 sc.swap_cluster_max = nr_to_scan;
MinChan Kimd9796772009-03-31 15:19:34 -07002446 shrink_all_zones(nr_to_scan, prio, pass, &sc);
2447 if (sc.nr_reclaimed >= nr_pages)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002448 goto out;
2449
2450 reclaim_state.reclaimed_slab = 0;
Andrew Morton76395d32007-01-05 16:37:05 -08002451 shrink_slab(sc.nr_scanned, sc.gfp_mask,
Wu Fengguangadea02a2009-09-21 17:01:42 -07002452 global_reclaimable_pages());
MinChan Kimd9796772009-03-31 15:19:34 -07002453 sc.nr_reclaimed += reclaim_state.reclaimed_slab;
2454 if (sc.nr_reclaimed >= nr_pages)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002455 goto out;
2456
2457 if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
Jens Axboe8aa7e842009-07-09 14:52:32 +02002458 congestion_wait(BLK_RW_ASYNC, HZ / 10);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002459 }
Rafael J. Wysocki248a0302006-03-22 00:09:04 -08002460 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002461
2462 /*
MinChan Kimd9796772009-03-31 15:19:34 -07002463 * If sc.nr_reclaimed = 0, we could not shrink LRUs, but there may be
2464 * something in slab caches
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002465 */
MinChan Kimd9796772009-03-31 15:19:34 -07002466 if (!sc.nr_reclaimed) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002467 do {
2468 reclaim_state.reclaimed_slab = 0;
Wu Fengguangadea02a2009-09-21 17:01:42 -07002469 shrink_slab(nr_pages, sc.gfp_mask,
2470 global_reclaimable_pages());
MinChan Kimd9796772009-03-31 15:19:34 -07002471 sc.nr_reclaimed += reclaim_state.reclaimed_slab;
2472 } while (sc.nr_reclaimed < nr_pages &&
2473 reclaim_state.reclaimed_slab > 0);
Andrew Morton76395d32007-01-05 16:37:05 -08002474 }
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002475
MinChan Kimd9796772009-03-31 15:19:34 -07002476
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002477out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 current->reclaim_state = NULL;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002479
MinChan Kimd9796772009-03-31 15:19:34 -07002480 return sc.nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481}
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02002482#endif /* CONFIG_HIBERNATION */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484/* It's optimal to keep kswapds on the same CPUs as their memory, but
2485 not required for correctness. So if the last cpu in a node goes
2486 away, we get changed to run anywhere: as the first one comes back,
2487 restore their cpu bindings. */
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07002488static int __devinit cpu_callback(struct notifier_block *nfb,
Andrew Morton69e05942006-03-22 00:08:19 -08002489 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07002491 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002493 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07002494 for_each_node_state(nid, N_HIGH_MEMORY) {
Mike Travisc5f59f02008-04-04 18:11:10 -07002495 pg_data_t *pgdat = NODE_DATA(nid);
Rusty Russella70f7302009-03-13 14:49:46 +10302496 const struct cpumask *mask;
2497
2498 mask = cpumask_of_node(pgdat->node_id);
Mike Travisc5f59f02008-04-04 18:11:10 -07002499
Rusty Russell3e597942009-01-01 10:12:24 +10302500 if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 /* One of our CPUs online: restore mask */
Mike Travisc5f59f02008-04-04 18:11:10 -07002502 set_cpus_allowed_ptr(pgdat->kswapd, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
2504 }
2505 return NOTIFY_OK;
2506}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Yasunori Goto3218ae12006-06-27 02:53:33 -07002508/*
2509 * This kswapd start function will be called by init and node-hot-add.
2510 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
2511 */
2512int kswapd_run(int nid)
2513{
2514 pg_data_t *pgdat = NODE_DATA(nid);
2515 int ret = 0;
2516
2517 if (pgdat->kswapd)
2518 return 0;
2519
2520 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
2521 if (IS_ERR(pgdat->kswapd)) {
2522 /* failure at boot is fatal */
2523 BUG_ON(system_state == SYSTEM_BOOTING);
2524 printk("Failed to start kswapd on node %d\n",nid);
2525 ret = -1;
2526 }
2527 return ret;
2528}
2529
David Rientjes8fe23e02009-12-14 17:58:33 -08002530/*
2531 * Called by memory hotplug when all memory in a node is offlined.
2532 */
2533void kswapd_stop(int nid)
2534{
2535 struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
2536
2537 if (kswapd)
2538 kthread_stop(kswapd);
2539}
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541static int __init kswapd_init(void)
2542{
Yasunori Goto3218ae12006-06-27 02:53:33 -07002543 int nid;
Andrew Morton69e05942006-03-22 00:08:19 -08002544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 swap_setup();
Christoph Lameter9422ffb2007-10-16 01:25:31 -07002546 for_each_node_state(nid, N_HIGH_MEMORY)
Yasunori Goto3218ae12006-06-27 02:53:33 -07002547 kswapd_run(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 hotcpu_notifier(cpu_callback, 0);
2549 return 0;
2550}
2551
2552module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08002553
2554#ifdef CONFIG_NUMA
2555/*
2556 * Zone reclaim mode
2557 *
2558 * If non-zero call zone_reclaim when the number of free pages falls below
2559 * the watermarks.
Christoph Lameter9eeff232006-01-18 17:42:31 -08002560 */
2561int zone_reclaim_mode __read_mostly;
2562
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08002563#define RECLAIM_OFF 0
Fernando Luis Vazquez Cao7d034312008-07-29 22:33:41 -07002564#define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08002565#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
2566#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
2567
Christoph Lameter9eeff232006-01-18 17:42:31 -08002568/*
Christoph Lametera92f7122006-02-01 03:05:32 -08002569 * Priority for ZONE_RECLAIM. This determines the fraction of pages
2570 * of a node considered for each zone_reclaim. 4 scans 1/16th of
2571 * a zone.
2572 */
2573#define ZONE_RECLAIM_PRIORITY 4
2574
Christoph Lameter9eeff232006-01-18 17:42:31 -08002575/*
Christoph Lameter96146342006-07-03 00:24:13 -07002576 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
2577 * occur.
2578 */
2579int sysctl_min_unmapped_ratio = 1;
2580
2581/*
Christoph Lameter0ff38492006-09-25 23:31:52 -07002582 * If the number of slab pages in a zone grows beyond this percentage then
2583 * slab reclaim needs to occur.
2584 */
2585int sysctl_min_slab_ratio = 5;
2586
Mel Gorman90afa5d2009-06-16 15:33:20 -07002587static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
2588{
2589 unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
2590 unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
2591 zone_page_state(zone, NR_ACTIVE_FILE);
2592
2593 /*
2594 * It's possible for there to be more file mapped pages than
2595 * accounted for by the pages on the file LRU lists because
2596 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
2597 */
2598 return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
2599}
2600
2601/* Work out how many page cache pages we can reclaim in this reclaim_mode */
2602static long zone_pagecache_reclaimable(struct zone *zone)
2603{
2604 long nr_pagecache_reclaimable;
2605 long delta = 0;
2606
2607 /*
2608 * If RECLAIM_SWAP is set, then all file pages are considered
2609 * potentially reclaimable. Otherwise, we have to worry about
2610 * pages like swapcache and zone_unmapped_file_pages() provides
2611 * a better estimate
2612 */
2613 if (zone_reclaim_mode & RECLAIM_SWAP)
2614 nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
2615 else
2616 nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
2617
2618 /* If we can't clean pages, remove dirty pages from consideration */
2619 if (!(zone_reclaim_mode & RECLAIM_WRITE))
2620 delta += zone_page_state(zone, NR_FILE_DIRTY);
2621
2622 /* Watch for any possible underflows due to delta */
2623 if (unlikely(delta > nr_pagecache_reclaimable))
2624 delta = nr_pagecache_reclaimable;
2625
2626 return nr_pagecache_reclaimable - delta;
2627}
2628
Christoph Lameter0ff38492006-09-25 23:31:52 -07002629/*
Christoph Lameter9eeff232006-01-18 17:42:31 -08002630 * Try to free up some pages from this zone through reclaim.
2631 */
Andrew Morton179e9632006-03-22 00:08:18 -08002632static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08002633{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08002634 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08002635 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08002636 struct task_struct *p = current;
2637 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08002638 int priority;
Andrew Morton179e9632006-03-22 00:08:18 -08002639 struct scan_control sc = {
2640 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
Johannes Weinera6dc60f2009-03-31 15:19:30 -07002641 .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07002642 .may_swap = 1,
Andrew Morton69e05942006-03-22 00:08:19 -08002643 .swap_cluster_max = max_t(unsigned long, nr_pages,
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08002644 SWAP_CLUSTER_MAX),
2645 .nr_to_reclaim = max_t(unsigned long, nr_pages,
2646 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08002647 .gfp_mask = gfp_mask,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002648 .swappiness = vm_swappiness,
Johannes Weinerbd2f6192009-03-31 15:19:38 -07002649 .order = order,
Balbir Singh66e17072008-02-07 00:13:56 -08002650 .isolate_pages = isolate_pages_global,
Andrew Morton179e9632006-03-22 00:08:18 -08002651 };
Christoph Lameter83e33a42006-09-25 23:31:53 -07002652 unsigned long slab_reclaimable;
Christoph Lameter9eeff232006-01-18 17:42:31 -08002653
2654 disable_swap_token();
Christoph Lameter9eeff232006-01-18 17:42:31 -08002655 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08002656 /*
2657 * We need to be able to allocate from the reserves for RECLAIM_SWAP
2658 * and we also need to be able to write out pages for RECLAIM_WRITE
2659 * and RECLAIM_SWAP.
2660 */
2661 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
Christoph Lameter9eeff232006-01-18 17:42:31 -08002662 reclaim_state.reclaimed_slab = 0;
2663 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08002664
Mel Gorman90afa5d2009-06-16 15:33:20 -07002665 if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
Christoph Lameter0ff38492006-09-25 23:31:52 -07002666 /*
2667 * Free memory by calling shrink zone with increasing
2668 * priorities until we have enough memory freed.
2669 */
2670 priority = ZONE_RECLAIM_PRIORITY;
2671 do {
Martin Bligh3bb1a852006-10-28 10:38:24 -07002672 note_zone_scanning_priority(zone, priority);
Rik van Riela79311c2009-01-06 14:40:01 -08002673 shrink_zone(priority, zone, &sc);
Christoph Lameter0ff38492006-09-25 23:31:52 -07002674 priority--;
Rik van Riela79311c2009-01-06 14:40:01 -08002675 } while (priority >= 0 && sc.nr_reclaimed < nr_pages);
Christoph Lameter0ff38492006-09-25 23:31:52 -07002676 }
Christoph Lameterc84db232006-02-01 03:05:29 -08002677
Christoph Lameter83e33a42006-09-25 23:31:53 -07002678 slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
2679 if (slab_reclaimable > zone->min_slab_pages) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08002680 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08002681 * shrink_slab() does not currently allow us to determine how
Christoph Lameter0ff38492006-09-25 23:31:52 -07002682 * many pages were freed in this zone. So we take the current
2683 * number of slab pages and shake the slab until it is reduced
2684 * by the same nr_pages that we used for reclaiming unmapped
2685 * pages.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08002686 *
Christoph Lameter0ff38492006-09-25 23:31:52 -07002687 * Note that shrink_slab will free memory on all zones and may
2688 * take a long time.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08002689 */
Christoph Lameter0ff38492006-09-25 23:31:52 -07002690 while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
Christoph Lameter83e33a42006-09-25 23:31:53 -07002691 zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
2692 slab_reclaimable - nr_pages)
Christoph Lameter0ff38492006-09-25 23:31:52 -07002693 ;
Christoph Lameter83e33a42006-09-25 23:31:53 -07002694
2695 /*
2696 * Update nr_reclaimed by the number of slab pages we
2697 * reclaimed from this zone.
2698 */
Rik van Riela79311c2009-01-06 14:40:01 -08002699 sc.nr_reclaimed += slab_reclaimable -
Christoph Lameter83e33a42006-09-25 23:31:53 -07002700 zone_page_state(zone, NR_SLAB_RECLAIMABLE);
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08002701 }
2702
Christoph Lameter9eeff232006-01-18 17:42:31 -08002703 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08002704 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
Rik van Riela79311c2009-01-06 14:40:01 -08002705 return sc.nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08002706}
Andrew Morton179e9632006-03-22 00:08:18 -08002707
2708int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
2709{
Andrew Morton179e9632006-03-22 00:08:18 -08002710 int node_id;
David Rientjesd773ed62007-10-16 23:26:01 -07002711 int ret;
Andrew Morton179e9632006-03-22 00:08:18 -08002712
2713 /*
Christoph Lameter0ff38492006-09-25 23:31:52 -07002714 * Zone reclaim reclaims unmapped file backed pages and
2715 * slab pages if we are over the defined limits.
Christoph Lameter34aa1332006-06-30 01:55:37 -07002716 *
Christoph Lameter96146342006-07-03 00:24:13 -07002717 * A small portion of unmapped file backed pages is needed for
2718 * file I/O otherwise pages read by file I/O will be immediately
2719 * thrown out if the zone is overallocated. So we do not reclaim
2720 * if less than a specified percentage of the zone is used by
2721 * unmapped file backed pages.
Andrew Morton179e9632006-03-22 00:08:18 -08002722 */
Mel Gorman90afa5d2009-06-16 15:33:20 -07002723 if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
2724 zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
Mel Gormanfa5e0842009-06-16 15:33:22 -07002725 return ZONE_RECLAIM_FULL;
Andrew Morton179e9632006-03-22 00:08:18 -08002726
David Rientjesd773ed62007-10-16 23:26:01 -07002727 if (zone_is_all_unreclaimable(zone))
Mel Gormanfa5e0842009-06-16 15:33:22 -07002728 return ZONE_RECLAIM_FULL;
David Rientjesd773ed62007-10-16 23:26:01 -07002729
Andrew Morton179e9632006-03-22 00:08:18 -08002730 /*
David Rientjesd773ed62007-10-16 23:26:01 -07002731 * Do not scan if the allocation should not be delayed.
Andrew Morton179e9632006-03-22 00:08:18 -08002732 */
David Rientjesd773ed62007-10-16 23:26:01 -07002733 if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
Mel Gormanfa5e0842009-06-16 15:33:22 -07002734 return ZONE_RECLAIM_NOSCAN;
Andrew Morton179e9632006-03-22 00:08:18 -08002735
2736 /*
2737 * Only run zone reclaim on the local zone or on zones that do not
2738 * have associated processors. This will favor the local processor
2739 * over remote processors and spread off node memory allocations
2740 * as wide as possible.
2741 */
Christoph Lameter89fa3022006-09-25 23:31:55 -07002742 node_id = zone_to_nid(zone);
Christoph Lameter37c07082007-10-16 01:25:36 -07002743 if (node_state(node_id, N_CPU) && node_id != numa_node_id())
Mel Gormanfa5e0842009-06-16 15:33:22 -07002744 return ZONE_RECLAIM_NOSCAN;
David Rientjesd773ed62007-10-16 23:26:01 -07002745
2746 if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
Mel Gormanfa5e0842009-06-16 15:33:22 -07002747 return ZONE_RECLAIM_NOSCAN;
2748
David Rientjesd773ed62007-10-16 23:26:01 -07002749 ret = __zone_reclaim(zone, gfp_mask, order);
2750 zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
2751
Mel Gorman24cf725182009-06-16 15:33:23 -07002752 if (!ret)
2753 count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
2754
David Rientjesd773ed62007-10-16 23:26:01 -07002755 return ret;
Andrew Morton179e9632006-03-22 00:08:18 -08002756}
Christoph Lameter9eeff232006-01-18 17:42:31 -08002757#endif
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002758
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002759/*
2760 * page_evictable - test whether a page is evictable
2761 * @page: the page to test
2762 * @vma: the VMA in which the page is or will be mapped, may be NULL
2763 *
2764 * Test whether page is evictable--i.e., should be placed on active/inactive
Nick Pigginb291f002008-10-18 20:26:44 -07002765 * lists vs unevictable list. The vma argument is !NULL when called from the
2766 * fault path to determine how to instantate a new page.
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002767 *
2768 * Reasons page might not be evictable:
Lee Schermerhornba9ddf42008-10-18 20:26:42 -07002769 * (1) page's mapping marked unevictable
Nick Pigginb291f002008-10-18 20:26:44 -07002770 * (2) page is part of an mlocked VMA
Lee Schermerhornba9ddf42008-10-18 20:26:42 -07002771 *
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002772 */
2773int page_evictable(struct page *page, struct vm_area_struct *vma)
2774{
2775
Lee Schermerhornba9ddf42008-10-18 20:26:42 -07002776 if (mapping_unevictable(page_mapping(page)))
2777 return 0;
2778
Nick Pigginb291f002008-10-18 20:26:44 -07002779 if (PageMlocked(page) || (vma && is_mlocked_vma(vma, page)))
2780 return 0;
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002781
2782 return 1;
2783}
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002784
2785/**
2786 * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list
2787 * @page: page to check evictability and move to appropriate lru list
2788 * @zone: zone page is in
2789 *
2790 * Checks a page for evictability and moves the page to the appropriate
2791 * zone lru list.
2792 *
2793 * Restrictions: zone->lru_lock must be held, page must be on LRU and must
2794 * have PageUnevictable set.
2795 */
2796static void check_move_unevictable_page(struct page *page, struct zone *zone)
2797{
2798 VM_BUG_ON(PageActive(page));
2799
2800retry:
2801 ClearPageUnevictable(page);
2802 if (page_evictable(page, NULL)) {
Johannes Weiner401a8e12009-09-21 17:02:58 -07002803 enum lru_list l = page_lru_base_type(page);
Lee Schermerhornaf936a12008-10-18 20:26:53 -07002804
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002805 __dec_zone_state(zone, NR_UNEVICTABLE);
2806 list_move(&page->lru, &zone->lru[l].list);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002807 mem_cgroup_move_lists(page, LRU_UNEVICTABLE, l);
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002808 __inc_zone_state(zone, NR_INACTIVE_ANON + l);
2809 __count_vm_event(UNEVICTABLE_PGRESCUED);
2810 } else {
2811 /*
2812 * rotate unevictable list
2813 */
2814 SetPageUnevictable(page);
2815 list_move(&page->lru, &zone->lru[LRU_UNEVICTABLE].list);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002816 mem_cgroup_rotate_lru_list(page, LRU_UNEVICTABLE);
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002817 if (page_evictable(page, NULL))
2818 goto retry;
2819 }
2820}
2821
2822/**
2823 * scan_mapping_unevictable_pages - scan an address space for evictable pages
2824 * @mapping: struct address_space to scan for evictable pages
2825 *
2826 * Scan all pages in mapping. Check unevictable pages for
2827 * evictability and move them to the appropriate zone lru list.
2828 */
2829void scan_mapping_unevictable_pages(struct address_space *mapping)
2830{
2831 pgoff_t next = 0;
2832 pgoff_t end = (i_size_read(mapping->host) + PAGE_CACHE_SIZE - 1) >>
2833 PAGE_CACHE_SHIFT;
2834 struct zone *zone;
2835 struct pagevec pvec;
2836
2837 if (mapping->nrpages == 0)
2838 return;
2839
2840 pagevec_init(&pvec, 0);
2841 while (next < end &&
2842 pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
2843 int i;
2844 int pg_scanned = 0;
2845
2846 zone = NULL;
2847
2848 for (i = 0; i < pagevec_count(&pvec); i++) {
2849 struct page *page = pvec.pages[i];
2850 pgoff_t page_index = page->index;
2851 struct zone *pagezone = page_zone(page);
2852
2853 pg_scanned++;
2854 if (page_index > next)
2855 next = page_index;
2856 next++;
2857
2858 if (pagezone != zone) {
2859 if (zone)
2860 spin_unlock_irq(&zone->lru_lock);
2861 zone = pagezone;
2862 spin_lock_irq(&zone->lru_lock);
2863 }
2864
2865 if (PageLRU(page) && PageUnevictable(page))
2866 check_move_unevictable_page(page, zone);
2867 }
2868 if (zone)
2869 spin_unlock_irq(&zone->lru_lock);
2870 pagevec_release(&pvec);
2871
2872 count_vm_events(UNEVICTABLE_PGSCANNED, pg_scanned);
2873 }
2874
2875}
Lee Schermerhornaf936a12008-10-18 20:26:53 -07002876
2877/**
2878 * scan_zone_unevictable_pages - check unevictable list for evictable pages
2879 * @zone - zone of which to scan the unevictable list
2880 *
2881 * Scan @zone's unevictable LRU lists to check for pages that have become
2882 * evictable. Move those that have to @zone's inactive list where they
2883 * become candidates for reclaim, unless shrink_inactive_zone() decides
2884 * to reactivate them. Pages that are still unevictable are rotated
2885 * back onto @zone's unevictable list.
2886 */
2887#define SCAN_UNEVICTABLE_BATCH_SIZE 16UL /* arbitrary lock hold batch size */
KOSAKI Motohiro14b90b22009-01-06 14:39:45 -08002888static void scan_zone_unevictable_pages(struct zone *zone)
Lee Schermerhornaf936a12008-10-18 20:26:53 -07002889{
2890 struct list_head *l_unevictable = &zone->lru[LRU_UNEVICTABLE].list;
2891 unsigned long scan;
2892 unsigned long nr_to_scan = zone_page_state(zone, NR_UNEVICTABLE);
2893
2894 while (nr_to_scan > 0) {
2895 unsigned long batch_size = min(nr_to_scan,
2896 SCAN_UNEVICTABLE_BATCH_SIZE);
2897
2898 spin_lock_irq(&zone->lru_lock);
2899 for (scan = 0; scan < batch_size; scan++) {
2900 struct page *page = lru_to_page(l_unevictable);
2901
2902 if (!trylock_page(page))
2903 continue;
2904
2905 prefetchw_prev_lru_page(page, l_unevictable, flags);
2906
2907 if (likely(PageLRU(page) && PageUnevictable(page)))
2908 check_move_unevictable_page(page, zone);
2909
2910 unlock_page(page);
2911 }
2912 spin_unlock_irq(&zone->lru_lock);
2913
2914 nr_to_scan -= batch_size;
2915 }
2916}
2917
2918
2919/**
2920 * scan_all_zones_unevictable_pages - scan all unevictable lists for evictable pages
2921 *
2922 * A really big hammer: scan all zones' unevictable LRU lists to check for
2923 * pages that have become evictable. Move those back to the zones'
2924 * inactive list where they become candidates for reclaim.
2925 * This occurs when, e.g., we have unswappable pages on the unevictable lists,
2926 * and we add swap to the system. As such, it runs in the context of a task
2927 * that has possibly/probably made some previously unevictable pages
2928 * evictable.
2929 */
KOSAKI Motohiroff301532009-01-06 14:39:44 -08002930static void scan_all_zones_unevictable_pages(void)
Lee Schermerhornaf936a12008-10-18 20:26:53 -07002931{
2932 struct zone *zone;
2933
2934 for_each_zone(zone) {
2935 scan_zone_unevictable_pages(zone);
2936 }
2937}
2938
2939/*
2940 * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of
2941 * all nodes' unevictable lists for evictable pages
2942 */
2943unsigned long scan_unevictable_pages;
2944
2945int scan_unevictable_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002946 void __user *buffer,
Lee Schermerhornaf936a12008-10-18 20:26:53 -07002947 size_t *length, loff_t *ppos)
2948{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002949 proc_doulongvec_minmax(table, write, buffer, length, ppos);
Lee Schermerhornaf936a12008-10-18 20:26:53 -07002950
2951 if (write && *(unsigned long *)table->data)
2952 scan_all_zones_unevictable_pages();
2953
2954 scan_unevictable_pages = 0;
2955 return 0;
2956}
2957
2958/*
2959 * per node 'scan_unevictable_pages' attribute. On demand re-scan of
2960 * a specified node's per zone unevictable lists for evictable pages.
2961 */
2962
2963static ssize_t read_scan_unevictable_node(struct sys_device *dev,
2964 struct sysdev_attribute *attr,
2965 char *buf)
2966{
2967 return sprintf(buf, "0\n"); /* always zero; should fit... */
2968}
2969
2970static ssize_t write_scan_unevictable_node(struct sys_device *dev,
2971 struct sysdev_attribute *attr,
2972 const char *buf, size_t count)
2973{
2974 struct zone *node_zones = NODE_DATA(dev->id)->node_zones;
2975 struct zone *zone;
2976 unsigned long res;
2977 unsigned long req = strict_strtoul(buf, 10, &res);
2978
2979 if (!req)
2980 return 1; /* zero is no-op */
2981
2982 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
2983 if (!populated_zone(zone))
2984 continue;
2985 scan_zone_unevictable_pages(zone);
2986 }
2987 return 1;
2988}
2989
2990
2991static SYSDEV_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
2992 read_scan_unevictable_node,
2993 write_scan_unevictable_node);
2994
2995int scan_unevictable_register_node(struct node *node)
2996{
2997 return sysdev_create_file(&node->sysdev, &attr_scan_unevictable_pages);
2998}
2999
3000void scan_unevictable_unregister_node(struct node *node)
3001{
3002 sysdev_remove_file(&node->sysdev, &attr_scan_unevictable_pages);
3003}
3004