blob: d9fc2dce93af4e2eb08aebdc457cdff9ccd6e1fa [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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
Mel Gorman33906bc2010-08-09 17:19:16 -070051#define CREATE_TRACE_POINTS
52#include <trace/events/vmscan.h>
53
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -070054enum lumpy_mode {
55 LUMPY_MODE_NONE,
56 LUMPY_MODE_ASYNC,
57 LUMPY_MODE_SYNC,
58};
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060struct scan_control {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /* Incremented by the number of inactive pages that were scanned */
62 unsigned long nr_scanned;
63
Rik van Riela79311c2009-01-06 14:40:01 -080064 /* Number of pages freed so far during a call to shrink_zones() */
65 unsigned long nr_reclaimed;
66
KOSAKI Motohiro22fba332009-12-14 17:59:10 -080067 /* How many pages shrink_list() should reclaim */
68 unsigned long nr_to_reclaim;
69
KOSAKI Motohiro7b517552009-12-14 17:59:12 -080070 unsigned long hibernation_mode;
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040073 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 int may_writepage;
76
Johannes Weinera6dc60f82009-03-31 15:19:30 -070077 /* Can mapped pages be reclaimed? */
78 int may_unmap;
Christoph Lameterf1fd1062006-01-18 17:42:30 -080079
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -070080 /* Can pages be swapped as part of reclaim? */
81 int may_swap;
82
Rafael J. Wysockid6277db2006-06-23 02:03:18 -070083 int swappiness;
Nick Piggin408d8542006-09-25 23:31:27 -070084
Andy Whitcroft5ad333e2007-07-17 04:03:16 -070085 int order;
Balbir Singh66e17072008-02-07 00:13:56 -080086
KOSAKI Motohiro5f53e762010-05-24 14:32:37 -070087 /*
Nikanth Karthikesan415b54e2010-08-17 15:39:09 +053088 * Intend to reclaim enough continuous memory rather than reclaim
89 * enough amount of memory. i.e, mode for high order allocation.
KOSAKI Motohiro5f53e762010-05-24 14:32:37 -070090 */
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -070091 enum lumpy_mode lumpy_reclaim_mode;
KOSAKI Motohiro5f53e762010-05-24 14:32:37 -070092
Balbir Singh66e17072008-02-07 00:13:56 -080093 /* Which cgroup do we reclaim from */
94 struct mem_cgroup *mem_cgroup;
95
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -070096 /*
97 * Nodemask of nodes allowed by the caller. If NULL, all nodes
98 * are scanned.
99 */
100 nodemask_t *nodemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
104
105#ifdef ARCH_HAS_PREFETCH
106#define prefetch_prev_lru_page(_page, _base, _field) \
107 do { \
108 if ((_page)->lru.prev != _base) { \
109 struct page *prev; \
110 \
111 prev = lru_to_page(&(_page->lru)); \
112 prefetch(&prev->_field); \
113 } \
114 } while (0)
115#else
116#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
117#endif
118
119#ifdef ARCH_HAS_PREFETCHW
120#define prefetchw_prev_lru_page(_page, _base, _field) \
121 do { \
122 if ((_page)->lru.prev != _base) { \
123 struct page *prev; \
124 \
125 prev = lru_to_page(&(_page->lru)); \
126 prefetchw(&prev->_field); \
127 } \
128 } while (0)
129#else
130#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
131#endif
132
133/*
134 * From 0 .. 100. Higher means more swappy.
135 */
136int vm_swappiness = 60;
Andrew Mortonbd1e22b2006-06-23 02:03:47 -0700137long vm_total_pages; /* The total number of pages which the VM controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139static LIST_HEAD(shrinker_list);
140static DECLARE_RWSEM(shrinker_rwsem);
141
Balbir Singh00f0b822008-03-04 14:28:39 -0800142#ifdef CONFIG_CGROUP_MEM_RES_CTLR
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -0800143#define scanning_global_lru(sc) (!(sc)->mem_cgroup)
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -0800144#else
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -0800145#define scanning_global_lru(sc) (1)
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -0800146#endif
147
KOSAKI Motohiro6e901572009-01-07 18:08:15 -0800148static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone,
149 struct scan_control *sc)
150{
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -0800151 if (!scanning_global_lru(sc))
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800152 return mem_cgroup_get_reclaim_stat(sc->mem_cgroup, zone);
153
KOSAKI Motohiro6e901572009-01-07 18:08:15 -0800154 return &zone->reclaim_stat;
155}
156
Vincent Li0b217672009-09-21 17:03:09 -0700157static unsigned long zone_nr_lru_pages(struct zone *zone,
158 struct scan_control *sc, enum lru_list lru)
KOSAKI Motohiroc9f299d2009-01-07 18:08:16 -0800159{
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -0800160 if (!scanning_global_lru(sc))
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800161 return mem_cgroup_zone_nr_pages(sc->mem_cgroup, zone, lru);
162
KOSAKI Motohiroc9f299d2009-01-07 18:08:16 -0800163 return zone_page_state(zone, NR_LRU_BASE + lru);
164}
165
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
168 * Add a shrinker callback to be called from the vm
169 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700170void register_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Rusty Russell8e1f9362007-07-17 04:03:17 -0700172 shrinker->nr = 0;
173 down_write(&shrinker_rwsem);
174 list_add_tail(&shrinker->list, &shrinker_list);
175 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700177EXPORT_SYMBOL(register_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/*
180 * Remove one
181 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700182void unregister_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 down_write(&shrinker_rwsem);
185 list_del(&shrinker->list);
186 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700188EXPORT_SYMBOL(unregister_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190#define SHRINK_BATCH 128
191/*
192 * Call the shrink functions to age shrinkable caches
193 *
194 * Here we assume it costs one seek to replace a lru page and that it also
195 * takes a seek to recreate a cache object. With this in mind we age equal
196 * percentages of the lru and ageable caches. This should balance the seeks
197 * generated by these structures.
198 *
Simon Arlott183ff222007-10-20 01:27:18 +0200199 * If the vm encountered mapped pages on the LRU it increase the pressure on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * slab to avoid swapping.
201 *
202 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
203 *
204 * `lru_pages' represents the number of on-LRU pages in all the zones which
205 * are eligible for the caller's allocation attempt. It is used for balancing
206 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700207 *
208 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
Andrew Morton69e05942006-03-22 00:08:19 -0800210unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
211 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800214 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 if (scanned == 0)
217 scanned = SWAP_CLUSTER_MAX;
218
219 if (!down_read_trylock(&shrinker_rwsem))
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700220 return 1; /* Assume we'll be able to shrink next time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 list_for_each_entry(shrinker, &shrinker_list, list) {
223 unsigned long long delta;
224 unsigned long total_scan;
Dave Chinner7f8275d2010-07-19 14:56:17 +1000225 unsigned long max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Dave Chinner7f8275d2010-07-19 14:56:17 +1000227 max_pass = (*shrinker->shrink)(shrinker, 0, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 delta = (4 * scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800229 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 do_div(delta, lru_pages + 1);
231 shrinker->nr += delta;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800232 if (shrinker->nr < 0) {
David Rientjes88c3bd72009-03-31 15:23:29 -0700233 printk(KERN_ERR "shrink_slab: %pF negative objects to "
234 "delete nr=%ld\n",
235 shrinker->shrink, shrinker->nr);
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800236 shrinker->nr = max_pass;
237 }
238
239 /*
240 * Avoid risking looping forever due to too large nr value:
241 * never try to free more than twice the estimate number of
242 * freeable entries.
243 */
244 if (shrinker->nr > max_pass * 2)
245 shrinker->nr = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 total_scan = shrinker->nr;
248 shrinker->nr = 0;
249
250 while (total_scan >= SHRINK_BATCH) {
251 long this_scan = SHRINK_BATCH;
252 int shrink_ret;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700253 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Dave Chinner7f8275d2010-07-19 14:56:17 +1000255 nr_before = (*shrinker->shrink)(shrinker, 0, gfp_mask);
256 shrink_ret = (*shrinker->shrink)(shrinker, this_scan,
257 gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (shrink_ret == -1)
259 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700260 if (shrink_ret < nr_before)
261 ret += nr_before - shrink_ret;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700262 count_vm_events(SLABS_SCANNED, this_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 total_scan -= this_scan;
264
265 cond_resched();
266 }
267
268 shrinker->nr += total_scan;
269 }
270 up_read(&shrinker_rwsem);
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700271 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700274static void set_lumpy_reclaim_mode(int priority, struct scan_control *sc,
275 bool sync)
276{
277 enum lumpy_mode mode = sync ? LUMPY_MODE_SYNC : LUMPY_MODE_ASYNC;
278
279 /*
280 * Some reclaim have alredy been failed. No worth to try synchronous
281 * lumpy reclaim.
282 */
283 if (sync && sc->lumpy_reclaim_mode == LUMPY_MODE_NONE)
284 return;
285
286 /*
287 * If we need a large contiguous chunk of memory, or have
288 * trouble getting a small set of contiguous pages, we
289 * will reclaim both active and inactive pages.
290 */
291 if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
292 sc->lumpy_reclaim_mode = mode;
293 else if (sc->order && priority < DEF_PRIORITY - 2)
294 sc->lumpy_reclaim_mode = mode;
295 else
296 sc->lumpy_reclaim_mode = LUMPY_MODE_NONE;
297}
298
299static void disable_lumpy_reclaim_mode(struct scan_control *sc)
300{
301 sc->lumpy_reclaim_mode = LUMPY_MODE_NONE;
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static inline int is_page_cache_freeable(struct page *page)
305{
Johannes Weinerceddc3a2009-09-21 17:03:00 -0700306 /*
307 * A freeable page cache page is referenced only by the caller
308 * that isolated the page, the page cache radix tree and
309 * optional buffer heads at page->private.
310 */
Johannes Weineredcf4742009-09-21 17:02:59 -0700311 return page_count(page) - page_has_private(page) == 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700314static int may_write_to_queue(struct backing_dev_info *bdi,
315 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Christoph Lameter930d9152006-01-08 01:00:47 -0800317 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 1;
319 if (!bdi_write_congested(bdi))
320 return 1;
321 if (bdi == current->backing_dev_info)
322 return 1;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700323
324 /* lumpy reclaim for hugepage often need a lot of write */
325 if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
326 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return 0;
328}
329
330/*
331 * We detected a synchronous write error writing a page out. Probably
332 * -ENOSPC. We need to propagate that into the address_space for a subsequent
333 * fsync(), msync() or close().
334 *
335 * The tricky part is that after writepage we cannot touch the mapping: nothing
336 * prevents it from being freed up. But we have a ref on the page and once
337 * that page is locked, the mapping is pinned.
338 *
339 * We're allowed to run sleeping lock_page() here because we know the caller has
340 * __GFP_FS.
341 */
342static void handle_write_error(struct address_space *mapping,
343 struct page *page, int error)
344{
Nick Piggina6aa62a2010-07-20 13:24:25 -0700345 lock_page_nosync(page);
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -0700346 if (page_mapping(page) == mapping)
347 mapping_set_error(mapping, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unlock_page(page);
349}
350
Christoph Lameter04e62a22006-06-23 02:03:38 -0700351/* possible outcome of pageout() */
352typedef enum {
353 /* failed to write page out, page is locked */
354 PAGE_KEEP,
355 /* move page to the active list, page is locked */
356 PAGE_ACTIVATE,
357 /* page has been sent to the disk successfully, page is unlocked */
358 PAGE_SUCCESS,
359 /* page is clean and locked */
360 PAGE_CLEAN,
361} pageout_t;
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/*
Andrew Morton1742f192006-03-22 00:08:21 -0800364 * pageout is called by shrink_page_list() for each dirty page.
365 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700367static pageout_t pageout(struct page *page, struct address_space *mapping,
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700368 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 /*
371 * If the page is dirty, only perform writeback if that write
372 * will be non-blocking. To prevent this allocation from being
373 * stalled by pagecache activity. But note that there may be
374 * stalls if we need to run get_block(). We could test
375 * PagePrivate for that.
376 *
Vincent Li6aceb532009-12-14 17:58:49 -0800377 * If this process is currently in __generic_file_aio_write() against
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * this page's queue, we can perform writeback even if that
379 * will block.
380 *
381 * If the page is swapcache, write it back even if that would
382 * block, for some throttling. This happens by accident, because
383 * swap_backing_dev_info is bust: it doesn't reflect the
384 * congestion state of the swapdevs. Easy to fix, if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 */
386 if (!is_page_cache_freeable(page))
387 return PAGE_KEEP;
388 if (!mapping) {
389 /*
390 * Some data journaling orphaned pages can have
391 * page->mapping == NULL while being dirty with clean buffers.
392 */
David Howells266cf652009-04-03 16:42:36 +0100393 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (try_to_free_buffers(page)) {
395 ClearPageDirty(page);
Harvey Harrisond40cee22008-04-30 00:55:07 -0700396 printk("%s: orphaned page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return PAGE_CLEAN;
398 }
399 }
400 return PAGE_KEEP;
401 }
402 if (mapping->a_ops->writepage == NULL)
403 return PAGE_ACTIVATE;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700404 if (!may_write_to_queue(mapping->backing_dev_info, sc)) {
405 disable_lumpy_reclaim_mode(sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return PAGE_KEEP;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (clear_page_dirty_for_io(page)) {
410 int res;
411 struct writeback_control wbc = {
412 .sync_mode = WB_SYNC_NONE,
413 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700414 .range_start = 0,
415 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 .for_reclaim = 1,
417 };
418
419 SetPageReclaim(page);
420 res = mapping->a_ops->writepage(page, &wbc);
421 if (res < 0)
422 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800423 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ClearPageReclaim(page);
425 return PAGE_ACTIVATE;
426 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700427
428 /*
429 * Wait on writeback if requested to. This happens when
430 * direct reclaiming a large contiguous area and the
431 * first attempt to free a range of pages fails.
432 */
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700433 if (PageWriteback(page) &&
434 sc->lumpy_reclaim_mode == LUMPY_MODE_SYNC)
Andy Whitcroftc661b072007-08-22 14:01:26 -0700435 wait_on_page_writeback(page);
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!PageWriteback(page)) {
438 /* synchronous write or broken a_ops? */
439 ClearPageReclaim(page);
440 }
Mel Gorman755f0222010-08-09 17:19:18 -0700441 trace_mm_vmscan_writepage(page,
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700442 trace_reclaim_flags(page, sc->lumpy_reclaim_mode));
Andrew Mortone129b5c2006-09-27 01:50:00 -0700443 inc_zone_page_state(page, NR_VMSCAN_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return PAGE_SUCCESS;
445 }
446
447 return PAGE_CLEAN;
448}
449
Andrew Mortona649fd92006-10-17 00:09:36 -0700450/*
Nick Piggine2867812008-07-25 19:45:30 -0700451 * Same as remove_mapping, but if the page is removed from the mapping, it
452 * gets returned with a refcount of 0.
Andrew Mortona649fd92006-10-17 00:09:36 -0700453 */
Nick Piggine2867812008-07-25 19:45:30 -0700454static int __remove_mapping(struct address_space *mapping, struct page *page)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800455{
Nick Piggin28e4d962006-09-25 23:31:23 -0700456 BUG_ON(!PageLocked(page));
457 BUG_ON(mapping != page_mapping(page));
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800458
Nick Piggin19fd6232008-07-25 19:45:32 -0700459 spin_lock_irq(&mapping->tree_lock);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800460 /*
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700461 * The non racy check for a busy page.
462 *
463 * Must be careful with the order of the tests. When someone has
464 * a ref to the page, it may be possible that they dirty it then
465 * drop the reference. So if PageDirty is tested before page_count
466 * here, then the following race may occur:
467 *
468 * get_user_pages(&page);
469 * [user mapping goes away]
470 * write_to(page);
471 * !PageDirty(page) [good]
472 * SetPageDirty(page);
473 * put_page(page);
474 * !page_count(page) [good, discard it]
475 *
476 * [oops, our write_to data is lost]
477 *
478 * Reversing the order of the tests ensures such a situation cannot
479 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
480 * load is not satisfied before that of page->_count.
481 *
482 * Note that if SetPageDirty is always performed via set_page_dirty,
483 * and thus under tree_lock, then this ordering is not required.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800484 */
Nick Piggine2867812008-07-25 19:45:30 -0700485 if (!page_freeze_refs(page, 2))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800486 goto cannot_free;
Nick Piggine2867812008-07-25 19:45:30 -0700487 /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
488 if (unlikely(PageDirty(page))) {
489 page_unfreeze_refs(page, 2);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800490 goto cannot_free;
Nick Piggine2867812008-07-25 19:45:30 -0700491 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800492
493 if (PageSwapCache(page)) {
494 swp_entry_t swap = { .val = page_private(page) };
495 __delete_from_swap_cache(page);
Nick Piggin19fd6232008-07-25 19:45:32 -0700496 spin_unlock_irq(&mapping->tree_lock);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700497 swapcache_free(swap, page);
Nick Piggine2867812008-07-25 19:45:30 -0700498 } else {
499 __remove_from_page_cache(page);
Nick Piggin19fd6232008-07-25 19:45:32 -0700500 spin_unlock_irq(&mapping->tree_lock);
Daisuke Nishimurae767e052009-05-28 14:34:28 -0700501 mem_cgroup_uncharge_cache_page(page);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800502 }
503
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800504 return 1;
505
506cannot_free:
Nick Piggin19fd6232008-07-25 19:45:32 -0700507 spin_unlock_irq(&mapping->tree_lock);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800508 return 0;
509}
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/*
Nick Piggine2867812008-07-25 19:45:30 -0700512 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
513 * someone else has a ref on the page, abort and return 0. If it was
514 * successfully detached, return 1. Assumes the caller has a single ref on
515 * this page.
516 */
517int remove_mapping(struct address_space *mapping, struct page *page)
518{
519 if (__remove_mapping(mapping, page)) {
520 /*
521 * Unfreezing the refcount with 1 rather than 2 effectively
522 * drops the pagecache ref for us without requiring another
523 * atomic operation.
524 */
525 page_unfreeze_refs(page, 1);
526 return 1;
527 }
528 return 0;
529}
530
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700531/**
532 * putback_lru_page - put previously isolated page onto appropriate LRU list
533 * @page: page to be put back to appropriate lru list
534 *
535 * Add previously isolated @page to appropriate LRU list.
536 * Page may still be unevictable for other reasons.
537 *
538 * lru_lock must not be held, interrupts must be enabled.
539 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700540void putback_lru_page(struct page *page)
541{
542 int lru;
543 int active = !!TestClearPageActive(page);
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700544 int was_unevictable = PageUnevictable(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700545
546 VM_BUG_ON(PageLRU(page));
547
548redo:
549 ClearPageUnevictable(page);
550
551 if (page_evictable(page, NULL)) {
552 /*
553 * For evictable pages, we can use the cache.
554 * In event of a race, worst case is we end up with an
555 * unevictable page on [in]active list.
556 * We know how to handle that.
557 */
Johannes Weiner401a8e12009-09-21 17:02:58 -0700558 lru = active + page_lru_base_type(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700559 lru_cache_add_lru(page, lru);
560 } else {
561 /*
562 * Put unevictable pages directly on zone's unevictable
563 * list.
564 */
565 lru = LRU_UNEVICTABLE;
566 add_page_to_unevictable_list(page);
Johannes Weiner6a7b9542009-10-26 16:50:00 -0700567 /*
568 * When racing with an mlock clearing (page is
569 * unlocked), make sure that if the other thread does
570 * not observe our setting of PG_lru and fails
571 * isolation, we see PG_mlocked cleared below and move
572 * the page back to the evictable list.
573 *
574 * The other side is TestClearPageMlocked().
575 */
576 smp_mb();
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700577 }
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700578
579 /*
580 * page's status can change while we move it among lru. If an evictable
581 * page is on unevictable list, it never be freed. To avoid that,
582 * check after we added it to the list, again.
583 */
584 if (lru == LRU_UNEVICTABLE && page_evictable(page, NULL)) {
585 if (!isolate_lru_page(page)) {
586 put_page(page);
587 goto redo;
588 }
589 /* This means someone else dropped this page from LRU
590 * So, it will be freed or putback to LRU again. There is
591 * nothing to do here.
592 */
593 }
594
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700595 if (was_unevictable && lru != LRU_UNEVICTABLE)
596 count_vm_event(UNEVICTABLE_PGRESCUED);
597 else if (!was_unevictable && lru == LRU_UNEVICTABLE)
598 count_vm_event(UNEVICTABLE_PGCULLED);
599
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700600 put_page(page); /* drop ref from isolate */
601}
602
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800603enum page_references {
604 PAGEREF_RECLAIM,
605 PAGEREF_RECLAIM_CLEAN,
Johannes Weiner645747462010-03-05 13:42:22 -0800606 PAGEREF_KEEP,
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800607 PAGEREF_ACTIVATE,
608};
609
610static enum page_references page_check_references(struct page *page,
611 struct scan_control *sc)
612{
Johannes Weiner645747462010-03-05 13:42:22 -0800613 int referenced_ptes, referenced_page;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800614 unsigned long vm_flags;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800615
Johannes Weiner645747462010-03-05 13:42:22 -0800616 referenced_ptes = page_referenced(page, 1, sc->mem_cgroup, &vm_flags);
617 referenced_page = TestClearPageReferenced(page);
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800618
619 /* Lumpy reclaim - ignore references */
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700620 if (sc->lumpy_reclaim_mode != LUMPY_MODE_NONE)
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800621 return PAGEREF_RECLAIM;
622
623 /*
624 * Mlock lost the isolation race with us. Let try_to_unmap()
625 * move the page to the unevictable list.
626 */
627 if (vm_flags & VM_LOCKED)
628 return PAGEREF_RECLAIM;
629
Johannes Weiner645747462010-03-05 13:42:22 -0800630 if (referenced_ptes) {
631 if (PageAnon(page))
632 return PAGEREF_ACTIVATE;
633 /*
634 * All mapped pages start out with page table
635 * references from the instantiating fault, so we need
636 * to look twice if a mapped file page is used more
637 * than once.
638 *
639 * Mark it and spare it for another trip around the
640 * inactive list. Another page table reference will
641 * lead to its activation.
642 *
643 * Note: the mark is set for activated pages as well
644 * so that recently deactivated but used pages are
645 * quickly recovered.
646 */
647 SetPageReferenced(page);
648
649 if (referenced_page)
650 return PAGEREF_ACTIVATE;
651
652 return PAGEREF_KEEP;
653 }
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800654
655 /* Reclaim if clean, defer dirty pages to writeback */
Johannes Weiner645747462010-03-05 13:42:22 -0800656 if (referenced_page)
657 return PAGEREF_RECLAIM_CLEAN;
658
659 return PAGEREF_RECLAIM;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800660}
661
Mel Gormanabe4c3b2010-08-09 17:19:31 -0700662static noinline_for_stack void free_page_list(struct list_head *free_pages)
663{
664 struct pagevec freed_pvec;
665 struct page *page, *tmp;
666
667 pagevec_init(&freed_pvec, 1);
668
669 list_for_each_entry_safe(page, tmp, free_pages, lru) {
670 list_del(&page->lru);
671 if (!pagevec_add(&freed_pvec, page)) {
672 __pagevec_free(&freed_pvec);
673 pagevec_reinit(&freed_pvec);
674 }
675 }
676
677 pagevec_free(&freed_pvec);
678}
679
Nick Piggine2867812008-07-25 19:45:30 -0700680/*
Andrew Morton1742f192006-03-22 00:08:21 -0800681 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Andrew Morton1742f192006-03-22 00:08:21 -0800683static unsigned long shrink_page_list(struct list_head *page_list,
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700684 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 LIST_HEAD(ret_pages);
Mel Gormanabe4c3b2010-08-09 17:19:31 -0700687 LIST_HEAD(free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 int pgactivate = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800689 unsigned long nr_reclaimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 cond_resched();
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 while (!list_empty(page_list)) {
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800694 enum page_references references;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct address_space *mapping;
696 struct page *page;
697 int may_enter_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 cond_resched();
700
701 page = lru_to_page(page_list);
702 list_del(&page->lru);
703
Nick Piggin529ae9a2008-08-02 12:01:03 +0200704 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 goto keep;
706
Nick Piggin725d7042006-09-25 23:30:55 -0700707 VM_BUG_ON(PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800710
Nick Pigginb291f002008-10-18 20:26:44 -0700711 if (unlikely(!page_evictable(page, NULL)))
712 goto cull_mlocked;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700713
Johannes Weinera6dc60f82009-03-31 15:19:30 -0700714 if (!sc->may_unmap && page_mapped(page))
Christoph Lameter80e43422006-02-11 17:55:53 -0800715 goto keep_locked;
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* Double the slab pressure for mapped and swapcache pages */
718 if (page_mapped(page) || PageSwapCache(page))
719 sc->nr_scanned++;
720
Andy Whitcroftc661b072007-08-22 14:01:26 -0700721 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
722 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
723
724 if (PageWriteback(page)) {
725 /*
726 * Synchronous reclaim is performed in two passes,
727 * first an asynchronous pass over the list to
728 * start parallel writeback, and a second synchronous
729 * pass to wait for the IO to complete. Wait here
730 * for any page for which writeback has already
731 * started.
732 */
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700733 if (sc->lumpy_reclaim_mode == LUMPY_MODE_SYNC &&
734 may_enter_fs)
Andy Whitcroftc661b072007-08-22 14:01:26 -0700735 wait_on_page_writeback(page);
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700736 else {
737 unlock_page(page);
738 goto keep_lumpy;
739 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800742 references = page_check_references(page, sc);
743 switch (references) {
744 case PAGEREF_ACTIVATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 goto activate_locked;
Johannes Weiner645747462010-03-05 13:42:22 -0800746 case PAGEREF_KEEP:
747 goto keep_locked;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800748 case PAGEREF_RECLAIM:
749 case PAGEREF_RECLAIM_CLEAN:
750 ; /* try to reclaim the page below */
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /*
754 * Anonymous process memory has backing store?
755 * Try to allocate it some swap space here.
756 */
Nick Pigginb291f002008-10-18 20:26:44 -0700757 if (PageAnon(page) && !PageSwapCache(page)) {
Hugh Dickins63eb6b932008-11-19 15:36:37 -0800758 if (!(sc->gfp_mask & __GFP_IO))
759 goto keep_locked;
Hugh Dickinsac47b002009-01-06 14:39:39 -0800760 if (!add_to_swap(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 goto activate_locked;
Hugh Dickins63eb6b932008-11-19 15:36:37 -0800762 may_enter_fs = 1;
Nick Pigginb291f002008-10-18 20:26:44 -0700763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 /*
768 * The page is mapped into the page tables of one or more
769 * processes. Try to unmap it here.
770 */
771 if (page_mapped(page) && mapping) {
Andi Kleen14fa31b2009-09-16 11:50:10 +0200772 switch (try_to_unmap(page, TTU_UNMAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 case SWAP_FAIL:
774 goto activate_locked;
775 case SWAP_AGAIN:
776 goto keep_locked;
Nick Pigginb291f002008-10-18 20:26:44 -0700777 case SWAP_MLOCK:
778 goto cull_mlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 case SWAP_SUCCESS:
780 ; /* try to free the page below */
781 }
782 }
783
784 if (PageDirty(page)) {
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800785 if (references == PAGEREF_RECLAIM_CLEAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto keep_locked;
Andrew Morton4dd4b922008-03-24 12:29:52 -0700787 if (!may_enter_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800789 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goto keep_locked;
791
792 /* Page is dirty, try to write it out here */
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700793 switch (pageout(page, mapping, sc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 case PAGE_KEEP:
795 goto keep_locked;
796 case PAGE_ACTIVATE:
797 goto activate_locked;
798 case PAGE_SUCCESS:
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700799 if (PageWriteback(page))
800 goto keep_lumpy;
801 if (PageDirty(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 goto keep;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /*
805 * A synchronous write - probably a ramdisk. Go
806 * ahead and try to reclaim the page.
807 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200808 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 goto keep;
810 if (PageDirty(page) || PageWriteback(page))
811 goto keep_locked;
812 mapping = page_mapping(page);
813 case PAGE_CLEAN:
814 ; /* try to free the page below */
815 }
816 }
817
818 /*
819 * If the page has buffers, try to free the buffer mappings
820 * associated with this page. If we succeed we try to free
821 * the page as well.
822 *
823 * We do this even if the page is PageDirty().
824 * try_to_release_page() does not perform I/O, but it is
825 * possible for a page to have PageDirty set, but it is actually
826 * clean (all its buffers are clean). This happens if the
827 * buffers were written out directly, with submit_bh(). ext3
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700828 * will do this, as well as the blockdev mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * try_to_release_page() will discover that cleanness and will
830 * drop the buffers and mark the page clean - it can be freed.
831 *
832 * Rarely, pages can have buffers and no ->mapping. These are
833 * the pages which were not successfully invalidated in
834 * truncate_complete_page(). We try to drop those buffers here
835 * and if that worked, and the page is no longer mapped into
836 * process address space (page_count == 1) it can be freed.
837 * Otherwise, leave the page on the LRU so it is swappable.
838 */
David Howells266cf652009-04-03 16:42:36 +0100839 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (!try_to_release_page(page, sc->gfp_mask))
841 goto activate_locked;
Nick Piggine2867812008-07-25 19:45:30 -0700842 if (!mapping && page_count(page) == 1) {
843 unlock_page(page);
844 if (put_page_testzero(page))
845 goto free_it;
846 else {
847 /*
848 * rare race with speculative reference.
849 * the speculative reference will free
850 * this page shortly, so we may
851 * increment nr_reclaimed here (and
852 * leave it off the LRU).
853 */
854 nr_reclaimed++;
855 continue;
856 }
857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859
Nick Piggine2867812008-07-25 19:45:30 -0700860 if (!mapping || !__remove_mapping(mapping, page))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800861 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Nick Piggina978d6f2008-10-18 20:26:58 -0700863 /*
864 * At this point, we have no other references and there is
865 * no way to pick any more up (removed from LRU, removed
866 * from pagecache). Can use non-atomic bitops now (and
867 * we obviously don't have to worry about waking up a process
868 * waiting on the page lock, because there are no references.
869 */
870 __clear_page_locked(page);
Nick Piggine2867812008-07-25 19:45:30 -0700871free_it:
Andrew Morton05ff5132006-03-22 00:08:20 -0800872 nr_reclaimed++;
Mel Gormanabe4c3b2010-08-09 17:19:31 -0700873
874 /*
875 * Is there need to periodically free_page_list? It would
876 * appear not as the counts should be low
877 */
878 list_add(&page->lru, &free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 continue;
880
Nick Pigginb291f002008-10-18 20:26:44 -0700881cull_mlocked:
Hugh Dickins63d6c5a2009-01-06 14:39:38 -0800882 if (PageSwapCache(page))
883 try_to_free_swap(page);
Nick Pigginb291f002008-10-18 20:26:44 -0700884 unlock_page(page);
885 putback_lru_page(page);
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700886 disable_lumpy_reclaim_mode(sc);
Nick Pigginb291f002008-10-18 20:26:44 -0700887 continue;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889activate_locked:
Rik van Riel68a223942008-10-18 20:26:23 -0700890 /* Not a candidate for swapping, so reclaim swap space. */
891 if (PageSwapCache(page) && vm_swap_full())
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800892 try_to_free_swap(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700893 VM_BUG_ON(PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 SetPageActive(page);
895 pgactivate++;
896keep_locked:
897 unlock_page(page);
898keep:
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700899 disable_lumpy_reclaim_mode(sc);
900keep_lumpy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 list_add(&page->lru, &ret_pages);
Nick Pigginb291f002008-10-18 20:26:44 -0700902 VM_BUG_ON(PageLRU(page) || PageUnevictable(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Mel Gormanabe4c3b2010-08-09 17:19:31 -0700904
905 free_page_list(&free_pages);
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 list_splice(&ret_pages, page_list);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700908 count_vm_events(PGACTIVATE, pgactivate);
Andrew Morton05ff5132006-03-22 00:08:20 -0800909 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700912/*
913 * Attempt to remove the specified page from its LRU. Only take this page
914 * if it is of the appropriate PageActive status. Pages which are being
915 * freed elsewhere are also ignored.
916 *
917 * page: page to consider
918 * mode: one of the LRU isolation modes defined above
919 *
920 * returns 0 on success, -ve errno on failure.
921 */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700922int __isolate_lru_page(struct page *page, int mode, int file)
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700923{
924 int ret = -EINVAL;
925
926 /* Only take pages on the LRU. */
927 if (!PageLRU(page))
928 return ret;
929
930 /*
931 * When checking the active state, we need to be sure we are
932 * dealing with comparible boolean values. Take the logical not
933 * of each.
934 */
935 if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode))
936 return ret;
937
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700938 if (mode != ISOLATE_BOTH && page_is_file_cache(page) != file)
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700939 return ret;
940
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700941 /*
942 * When this function is being called for lumpy reclaim, we
943 * initially look into all LRU pages, active, inactive and
944 * unevictable; only give shrink_page_list evictable pages.
945 */
946 if (PageUnevictable(page))
947 return ret;
948
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700949 ret = -EBUSY;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800950
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700951 if (likely(get_page_unless_zero(page))) {
952 /*
953 * Be careful not to clear PageLRU until after we're
954 * sure the page is not being freed elsewhere -- the
955 * page release code relies on it.
956 */
957 ClearPageLRU(page);
958 ret = 0;
959 }
960
961 return ret;
962}
963
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800964/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 * zone->lru_lock is heavily contended. Some of the functions that
966 * shrink the lists perform better by taking out a batch of pages
967 * and working on them outside the LRU lock.
968 *
969 * For pagecache intensive workloads, this function is the hottest
970 * spot in the kernel (apart from copy_*_user functions).
971 *
972 * Appropriate locks must be held before calling this function.
973 *
974 * @nr_to_scan: The number of pages to look through on the list.
975 * @src: The LRU list to pull pages off.
976 * @dst: The temp list to put pages on to.
977 * @scanned: The number of pages that were scanned.
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700978 * @order: The caller's attempted allocation order
979 * @mode: One of the LRU isolation modes
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700980 * @file: True [1] if isolating file [!anon] pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 *
982 * returns how many pages were moved onto *@dst.
983 */
Andrew Morton69e05942006-03-22 00:08:19 -0800984static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
985 struct list_head *src, struct list_head *dst,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700986 unsigned long *scanned, int order, int mode, int file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Andrew Morton69e05942006-03-22 00:08:19 -0800988 unsigned long nr_taken = 0;
Mel Gormana8a94d12010-08-09 17:19:17 -0700989 unsigned long nr_lumpy_taken = 0;
990 unsigned long nr_lumpy_dirty = 0;
991 unsigned long nr_lumpy_failed = 0;
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800992 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Wu Fengguangc9b02d92006-03-22 00:08:23 -0800994 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -0700995 struct page *page;
996 unsigned long pfn;
997 unsigned long end_pfn;
998 unsigned long page_pfn;
999 int zone_id;
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 page = lru_to_page(src);
1002 prefetchw_prev_lru_page(page, src, flags);
1003
Nick Piggin725d7042006-09-25 23:30:55 -07001004 VM_BUG_ON(!PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -08001005
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001006 switch (__isolate_lru_page(page, mode, file)) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001007 case 0:
1008 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001009 mem_cgroup_del_lru(page);
Nick Piggin7c8ee9a2006-03-22 00:08:03 -08001010 nr_taken++;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001011 break;
Nick Piggin46453a62006-03-22 00:07:58 -08001012
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001013 case -EBUSY:
1014 /* else it is being freed elsewhere */
1015 list_move(&page->lru, src);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001016 mem_cgroup_rotate_lru_list(page, page_lru(page));
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001017 continue;
1018
1019 default:
1020 BUG();
1021 }
1022
1023 if (!order)
1024 continue;
1025
1026 /*
1027 * Attempt to take all pages in the order aligned region
1028 * surrounding the tag page. Only take those pages of
1029 * the same active state as that tag page. We may safely
1030 * round the target page pfn down to the requested order
1031 * as the mem_map is guarenteed valid out to MAX_ORDER,
1032 * where that page is in a different zone we will detect
1033 * it from its zone id and abort this block scan.
1034 */
1035 zone_id = page_zone_id(page);
1036 page_pfn = page_to_pfn(page);
1037 pfn = page_pfn & ~((1 << order) - 1);
1038 end_pfn = pfn + (1 << order);
1039 for (; pfn < end_pfn; pfn++) {
1040 struct page *cursor_page;
1041
1042 /* The target page is in the block, ignore it. */
1043 if (unlikely(pfn == page_pfn))
1044 continue;
1045
1046 /* Avoid holes within the zone. */
1047 if (unlikely(!pfn_valid_within(pfn)))
1048 break;
1049
1050 cursor_page = pfn_to_page(pfn);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001051
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001052 /* Check that we have not crossed a zone boundary. */
1053 if (unlikely(page_zone_id(cursor_page) != zone_id))
1054 continue;
Minchan Kimde2e7562009-09-21 17:01:43 -07001055
1056 /*
1057 * If we don't have enough swap space, reclaiming of
1058 * anon page which don't already have a swap slot is
1059 * pointless.
1060 */
1061 if (nr_swap_pages <= 0 && PageAnon(cursor_page) &&
1062 !PageSwapCache(cursor_page))
1063 continue;
1064
KAMEZAWA Hiroyukiee993b12009-06-16 15:33:24 -07001065 if (__isolate_lru_page(cursor_page, mode, file) == 0) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001066 list_move(&cursor_page->lru, dst);
KAMEZAWA Hiroyukicb4cbcf2009-06-23 08:57:55 +09001067 mem_cgroup_del_lru(cursor_page);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001068 nr_taken++;
Mel Gormana8a94d12010-08-09 17:19:17 -07001069 nr_lumpy_taken++;
1070 if (PageDirty(cursor_page))
1071 nr_lumpy_dirty++;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001072 scan++;
Mel Gormana8a94d12010-08-09 17:19:17 -07001073 } else {
1074 if (mode == ISOLATE_BOTH &&
1075 page_count(cursor_page))
1076 nr_lumpy_failed++;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001077 }
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
1081 *scanned = scan;
Mel Gormana8a94d12010-08-09 17:19:17 -07001082
1083 trace_mm_vmscan_lru_isolate(order,
1084 nr_to_scan, scan,
1085 nr_taken,
1086 nr_lumpy_taken, nr_lumpy_dirty, nr_lumpy_failed,
1087 mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return nr_taken;
1089}
1090
Balbir Singh66e17072008-02-07 00:13:56 -08001091static unsigned long isolate_pages_global(unsigned long nr,
1092 struct list_head *dst,
1093 unsigned long *scanned, int order,
1094 int mode, struct zone *z,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001095 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001096{
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001097 int lru = LRU_BASE;
Balbir Singh66e17072008-02-07 00:13:56 -08001098 if (active)
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001099 lru += LRU_ACTIVE;
1100 if (file)
1101 lru += LRU_FILE;
1102 return isolate_lru_pages(nr, &z->lru[lru].list, dst, scanned, order,
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001103 mode, file);
Balbir Singh66e17072008-02-07 00:13:56 -08001104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/*
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001107 * clear_active_flags() is a helper for shrink_active_list(), clearing
1108 * any active bits from the pages in the list.
1109 */
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001110static unsigned long clear_active_flags(struct list_head *page_list,
1111 unsigned int *count)
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001112{
1113 int nr_active = 0;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001114 int lru;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001115 struct page *page;
1116
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001117 list_for_each_entry(page, page_list, lru) {
Johannes Weiner401a8e12009-09-21 17:02:58 -07001118 lru = page_lru_base_type(page);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001119 if (PageActive(page)) {
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001120 lru += LRU_ACTIVE;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001121 ClearPageActive(page);
1122 nr_active++;
1123 }
Mel Gorman1489fa12010-08-09 17:19:33 -07001124 if (count)
1125 count[lru]++;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001126 }
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001127
1128 return nr_active;
1129}
1130
Nick Piggin62695a82008-10-18 20:26:09 -07001131/**
1132 * isolate_lru_page - tries to isolate a page from its LRU list
1133 * @page: page to isolate from its LRU list
1134 *
1135 * Isolates a @page from an LRU list, clears PageLRU and adjusts the
1136 * vmstat statistic corresponding to whatever LRU list the page was on.
1137 *
1138 * Returns 0 if the page was removed from an LRU list.
1139 * Returns -EBUSY if the page was not on an LRU list.
1140 *
1141 * The returned page will have PageLRU() cleared. If it was found on
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001142 * the active list, it will have PageActive set. If it was found on
1143 * the unevictable list, it will have the PageUnevictable bit set. That flag
1144 * may need to be cleared by the caller before letting the page go.
Nick Piggin62695a82008-10-18 20:26:09 -07001145 *
1146 * The vmstat statistic corresponding to the list on which the page was
1147 * found will be decremented.
1148 *
1149 * Restrictions:
1150 * (1) Must be called with an elevated refcount on the page. This is a
1151 * fundamentnal difference from isolate_lru_pages (which is called
1152 * without a stable reference).
1153 * (2) the lru_lock must not be held.
1154 * (3) interrupts must be enabled.
1155 */
1156int isolate_lru_page(struct page *page)
1157{
1158 int ret = -EBUSY;
1159
1160 if (PageLRU(page)) {
1161 struct zone *zone = page_zone(page);
1162
1163 spin_lock_irq(&zone->lru_lock);
1164 if (PageLRU(page) && get_page_unless_zero(page)) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001165 int lru = page_lru(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001166 ret = 0;
1167 ClearPageLRU(page);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001168
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001169 del_page_from_lru_list(zone, page, lru);
Nick Piggin62695a82008-10-18 20:26:09 -07001170 }
1171 spin_unlock_irq(&zone->lru_lock);
1172 }
1173 return ret;
1174}
1175
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001176/*
Rik van Riel35cd7812009-09-21 17:01:38 -07001177 * Are there way too many processes in the direct reclaim path already?
1178 */
1179static int too_many_isolated(struct zone *zone, int file,
1180 struct scan_control *sc)
1181{
1182 unsigned long inactive, isolated;
1183
1184 if (current_is_kswapd())
1185 return 0;
1186
1187 if (!scanning_global_lru(sc))
1188 return 0;
1189
1190 if (file) {
1191 inactive = zone_page_state(zone, NR_INACTIVE_FILE);
1192 isolated = zone_page_state(zone, NR_ISOLATED_FILE);
1193 } else {
1194 inactive = zone_page_state(zone, NR_INACTIVE_ANON);
1195 isolated = zone_page_state(zone, NR_ISOLATED_ANON);
1196 }
1197
1198 return isolated > inactive;
1199}
1200
1201/*
Mel Gorman66635622010-08-09 17:19:30 -07001202 * TODO: Try merging with migrations version of putback_lru_pages
1203 */
1204static noinline_for_stack void
Mel Gorman1489fa12010-08-09 17:19:33 -07001205putback_lru_pages(struct zone *zone, struct scan_control *sc,
Mel Gorman66635622010-08-09 17:19:30 -07001206 unsigned long nr_anon, unsigned long nr_file,
1207 struct list_head *page_list)
1208{
1209 struct page *page;
1210 struct pagevec pvec;
Mel Gorman1489fa12010-08-09 17:19:33 -07001211 struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
Mel Gorman66635622010-08-09 17:19:30 -07001212
1213 pagevec_init(&pvec, 1);
1214
1215 /*
1216 * Put back any unfreeable pages.
1217 */
1218 spin_lock(&zone->lru_lock);
1219 while (!list_empty(page_list)) {
1220 int lru;
1221 page = lru_to_page(page_list);
1222 VM_BUG_ON(PageLRU(page));
1223 list_del(&page->lru);
1224 if (unlikely(!page_evictable(page, NULL))) {
1225 spin_unlock_irq(&zone->lru_lock);
1226 putback_lru_page(page);
1227 spin_lock_irq(&zone->lru_lock);
1228 continue;
1229 }
1230 SetPageLRU(page);
1231 lru = page_lru(page);
1232 add_page_to_lru_list(zone, page, lru);
1233 if (is_active_lru(lru)) {
1234 int file = is_file_lru(lru);
1235 reclaim_stat->recent_rotated[file]++;
1236 }
1237 if (!pagevec_add(&pvec, page)) {
1238 spin_unlock_irq(&zone->lru_lock);
1239 __pagevec_release(&pvec);
1240 spin_lock_irq(&zone->lru_lock);
1241 }
1242 }
1243 __mod_zone_page_state(zone, NR_ISOLATED_ANON, -nr_anon);
1244 __mod_zone_page_state(zone, NR_ISOLATED_FILE, -nr_file);
1245
1246 spin_unlock_irq(&zone->lru_lock);
1247 pagevec_release(&pvec);
1248}
1249
Mel Gorman1489fa12010-08-09 17:19:33 -07001250static noinline_for_stack void update_isolated_counts(struct zone *zone,
1251 struct scan_control *sc,
1252 unsigned long *nr_anon,
1253 unsigned long *nr_file,
1254 struct list_head *isolated_list)
1255{
1256 unsigned long nr_active;
1257 unsigned int count[NR_LRU_LISTS] = { 0, };
1258 struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
1259
1260 nr_active = clear_active_flags(isolated_list, count);
1261 __count_vm_events(PGDEACTIVATE, nr_active);
1262
1263 __mod_zone_page_state(zone, NR_ACTIVE_FILE,
1264 -count[LRU_ACTIVE_FILE]);
1265 __mod_zone_page_state(zone, NR_INACTIVE_FILE,
1266 -count[LRU_INACTIVE_FILE]);
1267 __mod_zone_page_state(zone, NR_ACTIVE_ANON,
1268 -count[LRU_ACTIVE_ANON]);
1269 __mod_zone_page_state(zone, NR_INACTIVE_ANON,
1270 -count[LRU_INACTIVE_ANON]);
1271
1272 *nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON];
1273 *nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE];
1274 __mod_zone_page_state(zone, NR_ISOLATED_ANON, *nr_anon);
1275 __mod_zone_page_state(zone, NR_ISOLATED_FILE, *nr_file);
1276
1277 reclaim_stat->recent_scanned[0] += *nr_anon;
1278 reclaim_stat->recent_scanned[1] += *nr_file;
1279}
1280
Mel Gorman66635622010-08-09 17:19:30 -07001281/*
Wu Fengguange31f36982010-08-09 17:20:01 -07001282 * Returns true if the caller should wait to clean dirty/writeback pages.
1283 *
1284 * If we are direct reclaiming for contiguous pages and we do not reclaim
1285 * everything in the list, try again and wait for writeback IO to complete.
1286 * This will stall high-order allocations noticeably. Only do that when really
1287 * need to free the pages under high memory pressure.
1288 */
1289static inline bool should_reclaim_stall(unsigned long nr_taken,
1290 unsigned long nr_freed,
1291 int priority,
1292 struct scan_control *sc)
1293{
1294 int lumpy_stall_priority;
1295
1296 /* kswapd should not stall on sync IO */
1297 if (current_is_kswapd())
1298 return false;
1299
1300 /* Only stall on lumpy reclaim */
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001301 if (sc->lumpy_reclaim_mode == LUMPY_MODE_NONE)
Wu Fengguange31f36982010-08-09 17:20:01 -07001302 return false;
1303
1304 /* If we have relaimed everything on the isolated list, no stall */
1305 if (nr_freed == nr_taken)
1306 return false;
1307
1308 /*
1309 * For high-order allocations, there are two stall thresholds.
1310 * High-cost allocations stall immediately where as lower
1311 * order allocations such as stacks require the scanning
1312 * priority to be much higher before stalling.
1313 */
1314 if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
1315 lumpy_stall_priority = DEF_PRIORITY;
1316 else
1317 lumpy_stall_priority = DEF_PRIORITY / 3;
1318
1319 return priority <= lumpy_stall_priority;
1320}
1321
1322/*
Andrew Morton1742f192006-03-22 00:08:21 -08001323 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
1324 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 */
Mel Gorman66635622010-08-09 17:19:30 -07001326static noinline_for_stack unsigned long
1327shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
1328 struct scan_control *sc, int priority, int file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 LIST_HEAD(page_list);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001331 unsigned long nr_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001332 unsigned long nr_reclaimed = 0;
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001333 unsigned long nr_taken;
1334 unsigned long nr_active;
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001335 unsigned long nr_anon;
1336 unsigned long nr_file;
KOSAKI Motohiro78dc5832009-06-16 15:31:40 -07001337
Rik van Riel35cd7812009-09-21 17:01:38 -07001338 while (unlikely(too_many_isolated(zone, file, sc))) {
KOSAKI Motohiro58355c72009-10-26 16:49:35 -07001339 congestion_wait(BLK_RW_ASYNC, HZ/10);
Rik van Riel35cd7812009-09-21 17:01:38 -07001340
1341 /* We are about to die and free our memory. Return now. */
1342 if (fatal_signal_pending(current))
1343 return SWAP_CLUSTER_MAX;
1344 }
1345
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001346 set_lumpy_reclaim_mode(priority, sc, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 lru_add_drain();
1348 spin_lock_irq(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001350 if (scanning_global_lru(sc)) {
1351 nr_taken = isolate_pages_global(nr_to_scan,
1352 &page_list, &nr_scanned, sc->order,
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001353 sc->lumpy_reclaim_mode == LUMPY_MODE_NONE ?
1354 ISOLATE_INACTIVE : ISOLATE_BOTH,
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001355 zone, 0, file);
1356 zone->pages_scanned += nr_scanned;
KOSAKI Motohirob35ea172009-09-21 17:01:36 -07001357 if (current_is_kswapd())
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001358 __count_zone_vm_events(PGSCAN_KSWAPD, zone,
1359 nr_scanned);
1360 else
1361 __count_zone_vm_events(PGSCAN_DIRECT, zone,
1362 nr_scanned);
1363 } else {
1364 nr_taken = mem_cgroup_isolate_pages(nr_to_scan,
1365 &page_list, &nr_scanned, sc->order,
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001366 sc->lumpy_reclaim_mode == LUMPY_MODE_NONE ?
1367 ISOLATE_INACTIVE : ISOLATE_BOTH,
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001368 zone, sc->mem_cgroup,
1369 0, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /*
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001371 * mem_cgroup_isolate_pages() keeps track of
1372 * scanned pages on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 */
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001374 }
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07001375
Mel Gorman66635622010-08-09 17:19:30 -07001376 if (nr_taken == 0) {
1377 spin_unlock_irq(&zone->lru_lock);
1378 return 0;
1379 }
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001380
Mel Gorman1489fa12010-08-09 17:19:33 -07001381 update_isolated_counts(zone, sc, &nr_anon, &nr_file, &page_list);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001382
1383 spin_unlock_irq(&zone->lru_lock);
1384
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001385 nr_reclaimed = shrink_page_list(&page_list, sc);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001386
Wu Fengguange31f36982010-08-09 17:20:01 -07001387 /* Check if we should syncronously wait for writeback */
1388 if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001389 /*
1390 * The attempt at page out may have made some
1391 * of the pages active, mark them inactive again.
1392 */
Mel Gorman1489fa12010-08-09 17:19:33 -07001393 nr_active = clear_active_flags(&page_list, NULL);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001394 count_vm_events(PGDEACTIVATE, nr_active);
1395
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -07001396 set_lumpy_reclaim_mode(priority, sc, true);
1397 nr_reclaimed += shrink_page_list(&page_list, sc);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001398 }
1399
1400 local_irq_disable();
1401 if (current_is_kswapd())
1402 __count_vm_events(KSWAPD_STEAL, nr_reclaimed);
1403 __count_zone_vm_events(PGSTEAL, zone, nr_reclaimed);
1404
Mel Gorman1489fa12010-08-09 17:19:33 -07001405 putback_lru_pages(zone, sc, nr_anon, nr_file, &page_list);
Mel Gormane11da5b2010-10-26 14:21:40 -07001406
1407 trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id,
1408 zone_idx(zone),
1409 nr_scanned, nr_reclaimed,
1410 priority,
1411 trace_shrink_flags(file, sc->lumpy_reclaim_mode));
Andrew Morton05ff5132006-03-22 00:08:20 -08001412 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001415/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * This moves pages from the active list to the inactive list.
1417 *
1418 * We move them the other way if the page is referenced by one or more
1419 * processes, from rmap.
1420 *
1421 * If the pages are mostly unmapped, the processing is fast and it is
1422 * appropriate to hold zone->lru_lock across the whole operation. But if
1423 * the pages are mapped, the processing is slow (page_referenced()) so we
1424 * should drop zone->lru_lock around each page. It's impossible to balance
1425 * this, so instead we remove the pages from the LRU while processing them.
1426 * It is safe to rely on PG_active against the non-LRU pages in here because
1427 * nobody will play with that bit on a non-LRU page.
1428 *
1429 * The downside is that we have to touch page->_count against each page.
1430 * But we had to alter page->flags anyway.
1431 */
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001432
Wu Fengguang3eb41402009-06-16 15:33:13 -07001433static void move_active_pages_to_lru(struct zone *zone,
1434 struct list_head *list,
1435 enum lru_list lru)
1436{
1437 unsigned long pgmoved = 0;
1438 struct pagevec pvec;
1439 struct page *page;
1440
1441 pagevec_init(&pvec, 1);
1442
1443 while (!list_empty(list)) {
1444 page = lru_to_page(list);
Wu Fengguang3eb41402009-06-16 15:33:13 -07001445
1446 VM_BUG_ON(PageLRU(page));
1447 SetPageLRU(page);
1448
Wu Fengguang3eb41402009-06-16 15:33:13 -07001449 list_move(&page->lru, &zone->lru[lru].list);
1450 mem_cgroup_add_lru_list(page, lru);
1451 pgmoved++;
1452
1453 if (!pagevec_add(&pvec, page) || list_empty(list)) {
1454 spin_unlock_irq(&zone->lru_lock);
1455 if (buffer_heads_over_limit)
1456 pagevec_strip(&pvec);
1457 __pagevec_release(&pvec);
1458 spin_lock_irq(&zone->lru_lock);
1459 }
1460 }
1461 __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
1462 if (!is_active_lru(lru))
1463 __count_vm_events(PGDEACTIVATE, pgmoved);
1464}
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001465
Andrew Morton1742f192006-03-22 00:08:21 -08001466static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001467 struct scan_control *sc, int priority, int file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001469 unsigned long nr_taken;
Andrew Morton69e05942006-03-22 00:08:19 -08001470 unsigned long pgscanned;
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -07001471 unsigned long vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 LIST_HEAD(l_hold); /* The pages which were snipped off */
Wu Fengguang8cab4752009-06-16 15:33:12 -07001473 LIST_HEAD(l_active);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001474 LIST_HEAD(l_inactive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 struct page *page;
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001476 struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001477 unsigned long nr_rotated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 lru_add_drain();
1480 spin_lock_irq(&zone->lru_lock);
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001481 if (scanning_global_lru(sc)) {
Johannes Weiner8b25c6d2010-05-24 14:32:40 -07001482 nr_taken = isolate_pages_global(nr_pages, &l_hold,
1483 &pgscanned, sc->order,
1484 ISOLATE_ACTIVE, zone,
1485 1, file);
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001486 zone->pages_scanned += pgscanned;
Johannes Weiner8b25c6d2010-05-24 14:32:40 -07001487 } else {
1488 nr_taken = mem_cgroup_isolate_pages(nr_pages, &l_hold,
1489 &pgscanned, sc->order,
1490 ISOLATE_ACTIVE, zone,
1491 sc->mem_cgroup, 1, file);
1492 /*
1493 * mem_cgroup_isolate_pages() keeps track of
1494 * scanned pages on its own.
1495 */
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001496 }
Johannes Weiner8b25c6d2010-05-24 14:32:40 -07001497
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001498 reclaim_stat->recent_scanned[file] += nr_taken;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001499
Wu Fengguang3eb41402009-06-16 15:33:13 -07001500 __count_zone_vm_events(PGREFILL, zone, pgscanned);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001501 if (file)
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001502 __mod_zone_page_state(zone, NR_ACTIVE_FILE, -nr_taken);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001503 else
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001504 __mod_zone_page_state(zone, NR_ACTIVE_ANON, -nr_taken);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07001505 __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 spin_unlock_irq(&zone->lru_lock);
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 while (!list_empty(&l_hold)) {
1509 cond_resched();
1510 page = lru_to_page(&l_hold);
1511 list_del(&page->lru);
Rik van Riel7e9cd482008-10-18 20:26:35 -07001512
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001513 if (unlikely(!page_evictable(page, NULL))) {
1514 putback_lru_page(page);
1515 continue;
1516 }
1517
Johannes Weiner645747462010-03-05 13:42:22 -08001518 if (page_referenced(page, 0, sc->mem_cgroup, &vm_flags)) {
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001519 nr_rotated++;
Wu Fengguang8cab4752009-06-16 15:33:12 -07001520 /*
1521 * Identify referenced, file-backed active pages and
1522 * give them one more trip around the active list. So
1523 * that executable code get better chances to stay in
1524 * memory under moderate memory pressure. Anon pages
1525 * are not likely to be evicted by use-once streaming
1526 * IO, plus JVM can create lots of anon VM_EXEC pages,
1527 * so we ignore them here.
1528 */
Wu Fengguang41e20982009-10-26 16:49:53 -07001529 if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
Wu Fengguang8cab4752009-06-16 15:33:12 -07001530 list_add(&page->lru, &l_active);
1531 continue;
1532 }
1533 }
Rik van Riel7e9cd482008-10-18 20:26:35 -07001534
KOSAKI Motohiro5205e562009-09-21 17:01:44 -07001535 ClearPageActive(page); /* we are de-activating */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 list_add(&page->lru, &l_inactive);
1537 }
1538
Andrew Mortonb5557492009-01-06 14:40:13 -08001539 /*
Wu Fengguang8cab4752009-06-16 15:33:12 -07001540 * Move pages back to the lru list.
Andrew Mortonb5557492009-01-06 14:40:13 -08001541 */
Johannes Weiner2a1dc502008-12-01 03:00:35 +01001542 spin_lock_irq(&zone->lru_lock);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001543 /*
Wu Fengguang8cab4752009-06-16 15:33:12 -07001544 * Count referenced pages from currently used mappings as rotated,
1545 * even though only some of them are actually re-activated. This
1546 * helps balance scan pressure between file and anonymous pages in
1547 * get_scan_ratio.
Rik van Riel7e9cd482008-10-18 20:26:35 -07001548 */
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001549 reclaim_stat->recent_rotated[file] += nr_rotated;
Rik van Riel556adec2008-10-18 20:26:34 -07001550
Wu Fengguang3eb41402009-06-16 15:33:13 -07001551 move_active_pages_to_lru(zone, &l_active,
1552 LRU_ACTIVE + file * LRU_FILE);
1553 move_active_pages_to_lru(zone, &l_inactive,
1554 LRU_BASE + file * LRU_FILE);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07001555 __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001556 spin_unlock_irq(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Minchan Kim74e3f3c2010-10-26 14:21:31 -07001559#ifdef CONFIG_SWAP
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001560static int inactive_anon_is_low_global(struct zone *zone)
KOSAKI Motohirof89eb902009-01-07 18:08:14 -08001561{
1562 unsigned long active, inactive;
1563
1564 active = zone_page_state(zone, NR_ACTIVE_ANON);
1565 inactive = zone_page_state(zone, NR_INACTIVE_ANON);
1566
1567 if (inactive * zone->inactive_ratio < active)
1568 return 1;
1569
1570 return 0;
1571}
1572
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001573/**
1574 * inactive_anon_is_low - check if anonymous pages need to be deactivated
1575 * @zone: zone to check
1576 * @sc: scan control of this context
1577 *
1578 * Returns true if the zone does not have enough inactive anon pages,
1579 * meaning some active anon pages need to be deactivated.
1580 */
1581static int inactive_anon_is_low(struct zone *zone, struct scan_control *sc)
1582{
1583 int low;
1584
Minchan Kim74e3f3c2010-10-26 14:21:31 -07001585 /*
1586 * If we don't have swap space, anonymous page deactivation
1587 * is pointless.
1588 */
1589 if (!total_swap_pages)
1590 return 0;
1591
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001592 if (scanning_global_lru(sc))
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001593 low = inactive_anon_is_low_global(zone);
1594 else
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001595 low = mem_cgroup_inactive_anon_is_low(sc->mem_cgroup);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001596 return low;
1597}
Minchan Kim74e3f3c2010-10-26 14:21:31 -07001598#else
1599static inline int inactive_anon_is_low(struct zone *zone,
1600 struct scan_control *sc)
1601{
1602 return 0;
1603}
1604#endif
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001605
Rik van Riel56e49d22009-06-16 15:32:28 -07001606static int inactive_file_is_low_global(struct zone *zone)
1607{
1608 unsigned long active, inactive;
1609
1610 active = zone_page_state(zone, NR_ACTIVE_FILE);
1611 inactive = zone_page_state(zone, NR_INACTIVE_FILE);
1612
1613 return (active > inactive);
1614}
1615
1616/**
1617 * inactive_file_is_low - check if file pages need to be deactivated
1618 * @zone: zone to check
1619 * @sc: scan control of this context
1620 *
1621 * When the system is doing streaming IO, memory pressure here
1622 * ensures that active file pages get deactivated, until more
1623 * than half of the file pages are on the inactive list.
1624 *
1625 * Once we get to that situation, protect the system's working
1626 * set from being evicted by disabling active file page aging.
1627 *
1628 * This uses a different ratio than the anonymous pages, because
1629 * the page cache uses a use-once replacement algorithm.
1630 */
1631static int inactive_file_is_low(struct zone *zone, struct scan_control *sc)
1632{
1633 int low;
1634
1635 if (scanning_global_lru(sc))
1636 low = inactive_file_is_low_global(zone);
1637 else
1638 low = mem_cgroup_inactive_file_is_low(sc->mem_cgroup);
1639 return low;
1640}
1641
Rik van Rielb39415b2009-12-14 17:59:48 -08001642static int inactive_list_is_low(struct zone *zone, struct scan_control *sc,
1643 int file)
1644{
1645 if (file)
1646 return inactive_file_is_low(zone, sc);
1647 else
1648 return inactive_anon_is_low(zone, sc);
1649}
1650
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001651static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
Christoph Lameterb69408e2008-10-18 20:26:14 -07001652 struct zone *zone, struct scan_control *sc, int priority)
1653{
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001654 int file = is_file_lru(lru);
1655
Rik van Rielb39415b2009-12-14 17:59:48 -08001656 if (is_active_lru(lru)) {
1657 if (inactive_list_is_low(zone, sc, file))
1658 shrink_active_list(nr_to_scan, zone, sc, priority, file);
Rik van Riel556adec2008-10-18 20:26:34 -07001659 return 0;
1660 }
1661
Rik van Riel33c120e2008-10-18 20:26:36 -07001662 return shrink_inactive_list(nr_to_scan, zone, sc, priority, file);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001663}
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665/*
Shaohua Li76a33fc2010-05-24 14:32:36 -07001666 * Smallish @nr_to_scan's are deposited in @nr_saved_scan,
1667 * until we collected @swap_cluster_max pages to scan.
1668 */
1669static unsigned long nr_scan_try_batch(unsigned long nr_to_scan,
1670 unsigned long *nr_saved_scan)
1671{
1672 unsigned long nr;
1673
1674 *nr_saved_scan += nr_to_scan;
1675 nr = *nr_saved_scan;
1676
1677 if (nr >= SWAP_CLUSTER_MAX)
1678 *nr_saved_scan = 0;
1679 else
1680 nr = 0;
1681
1682 return nr;
1683}
1684
1685/*
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001686 * Determine how aggressively the anon and file LRU lists should be
1687 * scanned. The relative value of each set of LRU lists is determined
1688 * by looking at the fraction of the pages scanned we did rotate back
1689 * onto the active list instead of evict.
1690 *
Shaohua Li76a33fc2010-05-24 14:32:36 -07001691 * nr[0] = anon pages to scan; nr[1] = file pages to scan
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001692 */
Shaohua Li76a33fc2010-05-24 14:32:36 -07001693static void get_scan_count(struct zone *zone, struct scan_control *sc,
1694 unsigned long *nr, int priority)
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001695{
1696 unsigned long anon, file, free;
1697 unsigned long anon_prio, file_prio;
1698 unsigned long ap, fp;
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001699 struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
Shaohua Li76a33fc2010-05-24 14:32:36 -07001700 u64 fraction[2], denominator;
1701 enum lru_list l;
1702 int noswap = 0;
1703
1704 /* If we have no swap space, do not bother scanning anon pages. */
1705 if (!sc->may_swap || (nr_swap_pages <= 0)) {
1706 noswap = 1;
1707 fraction[0] = 0;
1708 fraction[1] = 1;
1709 denominator = 1;
1710 goto out;
1711 }
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001712
Vincent Li0b217672009-09-21 17:03:09 -07001713 anon = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_ANON) +
1714 zone_nr_lru_pages(zone, sc, LRU_INACTIVE_ANON);
1715 file = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_FILE) +
1716 zone_nr_lru_pages(zone, sc, LRU_INACTIVE_FILE);
Hugh Dickinsb9627162009-01-06 14:39:41 -08001717
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001718 if (scanning_global_lru(sc)) {
KOSAKI Motohiroeeee9a82009-01-07 18:08:17 -08001719 free = zone_page_state(zone, NR_FREE_PAGES);
1720 /* If we have very few page cache pages,
1721 force-scan anon pages. */
Mel Gorman41858962009-06-16 15:32:12 -07001722 if (unlikely(file + free <= high_wmark_pages(zone))) {
Shaohua Li76a33fc2010-05-24 14:32:36 -07001723 fraction[0] = 1;
1724 fraction[1] = 0;
1725 denominator = 1;
1726 goto out;
KOSAKI Motohiroeeee9a82009-01-07 18:08:17 -08001727 }
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001728 }
1729
1730 /*
KOSAKI Motohiro58c37f62010-08-09 17:19:51 -07001731 * With swappiness at 100, anonymous and file have the same priority.
1732 * This scanning priority is essentially the inverse of IO cost.
1733 */
1734 anon_prio = sc->swappiness;
1735 file_prio = 200 - sc->swappiness;
1736
1737 /*
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001738 * OK, so we have swap space and a fair amount of page cache
1739 * pages. We use the recently rotated / recently scanned
1740 * ratios to determine how valuable each cache is.
1741 *
1742 * Because workloads change over time (and to avoid overflow)
1743 * we keep these statistics as a floating average, which ends
1744 * up weighing recent references more than old ones.
1745 *
1746 * anon in [0], file in [1]
1747 */
KOSAKI Motohiro58c37f62010-08-09 17:19:51 -07001748 spin_lock_irq(&zone->lru_lock);
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001749 if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001750 reclaim_stat->recent_scanned[0] /= 2;
1751 reclaim_stat->recent_rotated[0] /= 2;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001752 }
1753
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001754 if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001755 reclaim_stat->recent_scanned[1] /= 2;
1756 reclaim_stat->recent_rotated[1] /= 2;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001757 }
1758
1759 /*
Rik van Riel00d80892008-11-19 15:36:44 -08001760 * The amount of pressure on anon vs file pages is inversely
1761 * proportional to the fraction of recently scanned pages on
1762 * each list that were recently referenced and in active use.
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001763 */
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001764 ap = (anon_prio + 1) * (reclaim_stat->recent_scanned[0] + 1);
1765 ap /= reclaim_stat->recent_rotated[0] + 1;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001766
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001767 fp = (file_prio + 1) * (reclaim_stat->recent_scanned[1] + 1);
1768 fp /= reclaim_stat->recent_rotated[1] + 1;
KOSAKI Motohiro58c37f62010-08-09 17:19:51 -07001769 spin_unlock_irq(&zone->lru_lock);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001770
Shaohua Li76a33fc2010-05-24 14:32:36 -07001771 fraction[0] = ap;
1772 fraction[1] = fp;
1773 denominator = ap + fp + 1;
1774out:
1775 for_each_evictable_lru(l) {
1776 int file = is_file_lru(l);
1777 unsigned long scan;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001778
Shaohua Li76a33fc2010-05-24 14:32:36 -07001779 scan = zone_nr_lru_pages(zone, sc, l);
1780 if (priority || noswap) {
1781 scan >>= priority;
1782 scan = div64_u64(scan * fraction[file], denominator);
1783 }
1784 nr[l] = nr_scan_try_batch(scan,
1785 &reclaim_stat->nr_saved_scan[l]);
1786 }
Wu Fengguang6e08a362009-06-16 15:32:29 -07001787}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001788
1789/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1791 */
Rik van Riela79311c2009-01-06 14:40:01 -08001792static void shrink_zone(int priority, struct zone *zone,
Andrew Morton05ff5132006-03-22 00:08:20 -08001793 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Christoph Lameterb69408e2008-10-18 20:26:14 -07001795 unsigned long nr[NR_LRU_LISTS];
Christoph Lameter86959492006-03-22 00:08:18 -08001796 unsigned long nr_to_scan;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001797 enum lru_list l;
KOSAKI Motohiro01dbe5c2009-01-06 14:40:02 -08001798 unsigned long nr_reclaimed = sc->nr_reclaimed;
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08001799 unsigned long nr_to_reclaim = sc->nr_to_reclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Shaohua Li76a33fc2010-05-24 14:32:36 -07001801 get_scan_count(zone, sc, nr, priority);
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001802
Rik van Riel556adec2008-10-18 20:26:34 -07001803 while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
1804 nr[LRU_INACTIVE_FILE]) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001805 for_each_evictable_lru(l) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07001806 if (nr[l]) {
KOSAKI Motohiroece74b22009-12-14 17:59:14 -08001807 nr_to_scan = min_t(unsigned long,
1808 nr[l], SWAP_CLUSTER_MAX);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001809 nr[l] -= nr_to_scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
KOSAKI Motohiro01dbe5c2009-01-06 14:40:02 -08001811 nr_reclaimed += shrink_list(l, nr_to_scan,
1812 zone, sc, priority);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
Rik van Riela79311c2009-01-06 14:40:01 -08001815 /*
1816 * On large memory systems, scan >> priority can become
1817 * really large. This is fine for the starting priority;
1818 * we want to put equal scanning pressure on each zone.
1819 * However, if the VM has a harder time of freeing pages,
1820 * with multiple processes reclaiming pages, the total
1821 * freeing target can get unreasonably large.
1822 */
KOSAKI Motohiro338fde92009-12-14 17:59:15 -08001823 if (nr_reclaimed >= nr_to_reclaim && priority < DEF_PRIORITY)
Rik van Riela79311c2009-01-06 14:40:01 -08001824 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826
KOSAKI Motohiro01dbe5c2009-01-06 14:40:02 -08001827 sc->nr_reclaimed = nr_reclaimed;
1828
Rik van Riel556adec2008-10-18 20:26:34 -07001829 /*
1830 * Even if we did not try to evict anon pages at all, we want to
1831 * rebalance the anon lru active/inactive ratio.
1832 */
Minchan Kim74e3f3c2010-10-26 14:21:31 -07001833 if (inactive_anon_is_low(zone, sc))
Rik van Riel556adec2008-10-18 20:26:34 -07001834 shrink_active_list(SWAP_CLUSTER_MAX, zone, sc, priority, 0);
1835
Andrew Morton232ea4d2007-02-28 20:13:21 -08001836 throttle_vm_writeout(sc->gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
1839/*
1840 * This is the direct reclaim path, for page-allocating processes. We only
1841 * try to reclaim pages from zones which will satisfy the caller's allocation
1842 * request.
1843 *
Mel Gorman41858962009-06-16 15:32:12 -07001844 * We reclaim from a zone even if that zone is over high_wmark_pages(zone).
1845 * Because:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
1847 * allocation or
Mel Gorman41858962009-06-16 15:32:12 -07001848 * b) The target zone may be at high_wmark_pages(zone) but the lower zones
1849 * must go *over* high_wmark_pages(zone) to satisfy the `incremental min'
1850 * zone defense algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 * If a zone is deemed to be full of pinned pages then just give it a light
1853 * scan then give up on it.
1854 */
Minchan Kimd1908362010-09-22 13:05:01 -07001855static void shrink_zones(int priority, struct zonelist *zonelist,
Andrew Morton05ff5132006-03-22 00:08:20 -08001856 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Mel Gormandd1a2392008-04-28 02:12:17 -07001858 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001859 struct zone *zone;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001860
Mel Gormand4debc62010-08-09 17:19:29 -07001861 for_each_zone_zonelist_nodemask(zone, z, zonelist,
1862 gfp_zone(sc->gfp_mask), sc->nodemask) {
Con Kolivasf3fe6512006-01-06 00:11:15 -08001863 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 continue;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001865 /*
1866 * Take care memory controller reclaiming has small influence
1867 * to global LRU.
1868 */
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001869 if (scanning_global_lru(sc)) {
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001870 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1871 continue;
KOSAKI Motohiro93e4a89a2010-03-05 13:41:55 -08001872 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001873 continue; /* Let kswapd poll it */
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001874 }
Nick Piggin408d8542006-09-25 23:31:27 -07001875
Rik van Riela79311c2009-01-06 14:40:01 -08001876 shrink_zone(priority, zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Minchan Kimd1908362010-09-22 13:05:01 -07001878}
1879
1880static bool zone_reclaimable(struct zone *zone)
1881{
1882 return zone->pages_scanned < zone_reclaimable_pages(zone) * 6;
1883}
1884
1885/*
1886 * As hibernation is going on, kswapd is freezed so that it can't mark
1887 * the zone into all_unreclaimable. It can't handle OOM during hibernation.
1888 * So let's check zone's unreclaimable in direct reclaim as well as kswapd.
1889 */
1890static bool all_unreclaimable(struct zonelist *zonelist,
1891 struct scan_control *sc)
1892{
1893 struct zoneref *z;
1894 struct zone *zone;
1895 bool all_unreclaimable = true;
1896
1897 for_each_zone_zonelist_nodemask(zone, z, zonelist,
1898 gfp_zone(sc->gfp_mask), sc->nodemask) {
1899 if (!populated_zone(zone))
1900 continue;
1901 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1902 continue;
1903 if (zone_reclaimable(zone)) {
1904 all_unreclaimable = false;
1905 break;
1906 }
1907 }
1908
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07001909 return all_unreclaimable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912/*
1913 * This is the main entry point to direct page reclaim.
1914 *
1915 * If a full scan of the inactive list fails to free enough memory then we
1916 * are "out of memory" and something needs to be killed.
1917 *
1918 * If the caller is !__GFP_FS then the probability of a failure is reasonably
1919 * high - the zone may be full of dirty or under-writeback pages, which this
Jens Axboe5b0830c2009-09-23 19:37:09 +02001920 * caller can't do much about. We kick the writeback threads and take explicit
1921 * naps in the hope that some of these pages can be written. But if the
1922 * allocating task holds filesystem locks which prevent writeout this might not
1923 * work, and the allocation attempt will fail.
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07001924 *
1925 * returns: 0, if no pages reclaimed
1926 * else, the number of pages reclaimed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 */
Mel Gormandac1d272008-04-28 02:12:12 -07001928static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
Mel Gormandd1a2392008-04-28 02:12:17 -07001929 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 int priority;
Andrew Morton69e05942006-03-22 00:08:19 -08001932 unsigned long total_scanned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 struct reclaim_state *reclaim_state = current->reclaim_state;
Mel Gormandd1a2392008-04-28 02:12:17 -07001934 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001935 struct zone *zone;
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08001936 unsigned long writeback_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Miao Xiec0ff7452010-05-24 14:32:08 -07001938 get_mems_allowed();
Keika Kobayashi873b4772008-07-25 01:48:52 -07001939 delayacct_freepages_start();
1940
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001941 if (scanning_global_lru(sc))
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001942 count_vm_event(ALLOCSTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
Balbir Singh66e17072008-02-07 00:13:56 -08001945 sc->nr_scanned = 0;
Rik van Rielf7b7fd82005-11-28 13:44:07 -08001946 if (!priority)
1947 disable_swap_token();
Minchan Kimd1908362010-09-22 13:05:01 -07001948 shrink_zones(priority, zonelist, sc);
Balbir Singh66e17072008-02-07 00:13:56 -08001949 /*
1950 * Don't shrink slabs when reclaiming memory from
1951 * over limit cgroups
1952 */
KAMEZAWA Hiroyukie72e2bd2009-01-07 18:08:23 -08001953 if (scanning_global_lru(sc)) {
KOSAKI Motohiroc6a8a8c2010-08-09 17:19:14 -07001954 unsigned long lru_pages = 0;
Mel Gormand4debc62010-08-09 17:19:29 -07001955 for_each_zone_zonelist(zone, z, zonelist,
1956 gfp_zone(sc->gfp_mask)) {
KOSAKI Motohiroc6a8a8c2010-08-09 17:19:14 -07001957 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1958 continue;
1959
1960 lru_pages += zone_reclaimable_pages(zone);
1961 }
1962
Mel Gormandd1a2392008-04-28 02:12:17 -07001963 shrink_slab(sc->nr_scanned, sc->gfp_mask, lru_pages);
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -08001964 if (reclaim_state) {
Rik van Riela79311c2009-01-06 14:40:01 -08001965 sc->nr_reclaimed += reclaim_state->reclaimed_slab;
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -08001966 reclaim_state->reclaimed_slab = 0;
1967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Balbir Singh66e17072008-02-07 00:13:56 -08001969 total_scanned += sc->nr_scanned;
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07001970 if (sc->nr_reclaimed >= sc->nr_to_reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 /*
1974 * Try to write back as many pages as we just scanned. This
1975 * tends to cause slow streaming writers to write data to the
1976 * disk smoothly, at the dirtying rate, which is nice. But
1977 * that's undesirable in laptop mode, where we *want* lumpy
1978 * writeout. So in laptop mode, write out the whole world.
1979 */
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08001980 writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
1981 if (total_scanned > writeback_threshold) {
Jens Axboe03ba3782009-09-09 09:08:54 +02001982 wakeup_flusher_threads(laptop_mode ? 0 : total_scanned);
Balbir Singh66e17072008-02-07 00:13:56 -08001983 sc->may_writepage = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985
1986 /* Take a nap, wait for some writeback to complete */
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08001987 if (!sc->hibernation_mode && sc->nr_scanned &&
1988 priority < DEF_PRIORITY - 2)
Jens Axboe8aa7e842009-07-09 14:52:32 +02001989 congestion_wait(BLK_RW_ASYNC, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07001991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992out:
Keika Kobayashi873b4772008-07-25 01:48:52 -07001993 delayacct_freepages_end();
Miao Xiec0ff7452010-05-24 14:32:08 -07001994 put_mems_allowed();
Keika Kobayashi873b4772008-07-25 01:48:52 -07001995
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07001996 if (sc->nr_reclaimed)
1997 return sc->nr_reclaimed;
1998
1999 /* top priority shrink_zones still had more to do? don't OOM, then */
Minchan Kimd1908362010-09-22 13:05:01 -07002000 if (scanning_global_lru(sc) && !all_unreclaimable(zonelist, sc))
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002001 return 1;
2002
2003 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
Mel Gormandac1d272008-04-28 02:12:12 -07002006unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07002007 gfp_t gfp_mask, nodemask_t *nodemask)
Balbir Singh66e17072008-02-07 00:13:56 -08002008{
Mel Gorman33906bc2010-08-09 17:19:16 -07002009 unsigned long nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08002010 struct scan_control sc = {
2011 .gfp_mask = gfp_mask,
2012 .may_writepage = !laptop_mode,
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08002013 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07002014 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07002015 .may_swap = 1,
Balbir Singh66e17072008-02-07 00:13:56 -08002016 .swappiness = vm_swappiness,
2017 .order = order,
2018 .mem_cgroup = NULL,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07002019 .nodemask = nodemask,
Balbir Singh66e17072008-02-07 00:13:56 -08002020 };
2021
Mel Gorman33906bc2010-08-09 17:19:16 -07002022 trace_mm_vmscan_direct_reclaim_begin(order,
2023 sc.may_writepage,
2024 gfp_mask);
2025
2026 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
2027
2028 trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
2029
2030 return nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08002031}
2032
Balbir Singh00f0b822008-03-04 14:28:39 -08002033#ifdef CONFIG_CGROUP_MEM_RES_CTLR
Balbir Singh66e17072008-02-07 00:13:56 -08002034
Balbir Singh4e416952009-09-23 15:56:39 -07002035unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *mem,
2036 gfp_t gfp_mask, bool noswap,
2037 unsigned int swappiness,
KOSAKI Motohiro14fec792010-08-10 18:03:05 -07002038 struct zone *zone)
Balbir Singh4e416952009-09-23 15:56:39 -07002039{
2040 struct scan_control sc = {
KOSAKI Motohirob8f5c562010-08-10 18:03:02 -07002041 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Balbir Singh4e416952009-09-23 15:56:39 -07002042 .may_writepage = !laptop_mode,
2043 .may_unmap = 1,
2044 .may_swap = !noswap,
Balbir Singh4e416952009-09-23 15:56:39 -07002045 .swappiness = swappiness,
2046 .order = 0,
2047 .mem_cgroup = mem,
Balbir Singh4e416952009-09-23 15:56:39 -07002048 };
Balbir Singh4e416952009-09-23 15:56:39 -07002049 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
2050 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07002051
2052 trace_mm_vmscan_memcg_softlimit_reclaim_begin(0,
2053 sc.may_writepage,
2054 sc.gfp_mask);
2055
Balbir Singh4e416952009-09-23 15:56:39 -07002056 /*
2057 * NOTE: Although we can get the priority field, using it
2058 * here is not a good idea, since it limits the pages we can scan.
2059 * if we don't reclaim here, the shrink_zone from balance_pgdat
2060 * will pick up pages from other mem cgroup's as well. We hack
2061 * the priority and make it zero.
2062 */
2063 shrink_zone(0, zone, &sc);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07002064
2065 trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
2066
Balbir Singh4e416952009-09-23 15:56:39 -07002067 return sc.nr_reclaimed;
2068}
2069
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002070unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002071 gfp_t gfp_mask,
2072 bool noswap,
2073 unsigned int swappiness)
Balbir Singh66e17072008-02-07 00:13:56 -08002074{
Balbir Singh4e416952009-09-23 15:56:39 -07002075 struct zonelist *zonelist;
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07002076 unsigned long nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08002077 struct scan_control sc = {
Balbir Singh66e17072008-02-07 00:13:56 -08002078 .may_writepage = !laptop_mode,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07002079 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07002080 .may_swap = !noswap,
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08002081 .nr_to_reclaim = SWAP_CLUSTER_MAX,
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002082 .swappiness = swappiness,
Balbir Singh66e17072008-02-07 00:13:56 -08002083 .order = 0,
2084 .mem_cgroup = mem_cont,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07002085 .nodemask = NULL, /* we don't care the placement */
Balbir Singh66e17072008-02-07 00:13:56 -08002086 };
Balbir Singh66e17072008-02-07 00:13:56 -08002087
Mel Gormandd1a2392008-04-28 02:12:17 -07002088 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
2089 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
2090 zonelist = NODE_DATA(numa_node_id())->node_zonelists;
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07002091
2092 trace_mm_vmscan_memcg_reclaim_begin(0,
2093 sc.may_writepage,
2094 sc.gfp_mask);
2095
2096 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
2097
2098 trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
2099
2100 return nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08002101}
2102#endif
2103
Mel Gormanf50de2d2009-12-14 17:58:53 -08002104/* is kswapd sleeping prematurely? */
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002105static int sleeping_prematurely(pg_data_t *pgdat, int order, long remaining)
Mel Gormanf50de2d2009-12-14 17:58:53 -08002106{
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002107 int i;
Mel Gormanf50de2d2009-12-14 17:58:53 -08002108
2109 /* If a direct reclaimer woke kswapd within HZ/10, it's premature */
2110 if (remaining)
2111 return 1;
2112
2113 /* If after HZ/10, a zone is below the high mark, it's premature */
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002114 for (i = 0; i < pgdat->nr_zones; i++) {
2115 struct zone *zone = pgdat->node_zones + i;
2116
2117 if (!populated_zone(zone))
2118 continue;
2119
KOSAKI Motohiro93e4a89a2010-03-05 13:41:55 -08002120 if (zone->all_unreclaimable)
KOSAKI Motohirode3fab32010-01-15 17:01:25 -08002121 continue;
2122
Mel Gormanf50de2d2009-12-14 17:58:53 -08002123 if (!zone_watermark_ok(zone, order, high_wmark_pages(zone),
2124 0, 0))
2125 return 1;
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002126 }
Mel Gormanf50de2d2009-12-14 17:58:53 -08002127
2128 return 0;
2129}
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131/*
2132 * For kswapd, balance_pgdat() will work across all this node's zones until
Mel Gorman41858962009-06-16 15:32:12 -07002133 * they are all at high_wmark_pages(zone).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 * Returns the number of pages which were actually freed.
2136 *
2137 * There is special handling here for zones which are full of pinned pages.
2138 * This can happen if the pages are all mlocked, or if they are all used by
2139 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
2140 * What we do is to detect the case where all pages in the zone have been
2141 * scanned twice and there has been zero successful reclaim. Mark the zone as
2142 * dead and from now on, only perform a short scan. Basically we're polling
2143 * the zone for when the problem goes away.
2144 *
2145 * kswapd scans the zones in the highmem->normal->dma direction. It skips
Mel Gorman41858962009-06-16 15:32:12 -07002146 * zones which have free_pages > high_wmark_pages(zone), but once a zone is
2147 * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the
2148 * lower zones regardless of the number of free pages in the lower zones. This
2149 * interoperates with the page allocator fallback scheme to ensure that aging
2150 * of pages is balanced across the zones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 */
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002152static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 int all_zones_ok;
2155 int priority;
2156 int i;
Andrew Morton69e05942006-03-22 00:08:19 -08002157 unsigned long total_scanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 struct reclaim_state *reclaim_state = current->reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08002159 struct scan_control sc = {
2160 .gfp_mask = GFP_KERNEL,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07002161 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07002162 .may_swap = 1,
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08002163 /*
2164 * kswapd doesn't want to be bailed out while reclaim. because
2165 * we want to put equal scanning pressure on each zone.
2166 */
2167 .nr_to_reclaim = ULONG_MAX,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002168 .swappiness = vm_swappiness,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07002169 .order = order,
Balbir Singh66e17072008-02-07 00:13:56 -08002170 .mem_cgroup = NULL,
Andrew Morton179e9632006-03-22 00:08:18 -08002171 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172loop_again:
2173 total_scanned = 0;
Rik van Riela79311c2009-01-06 14:40:01 -08002174 sc.nr_reclaimed = 0;
Christoph Lameterc0bbbc72006-06-11 15:22:26 -07002175 sc.may_writepage = !laptop_mode;
Christoph Lameterf8891e52006-06-30 01:55:45 -07002176 count_vm_event(PAGEOUTRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 for (priority = DEF_PRIORITY; priority >= 0; priority--) {
2179 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
2180 unsigned long lru_pages = 0;
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002181 int has_under_min_watermark_zone = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Rik van Rielf7b7fd82005-11-28 13:44:07 -08002183 /* The swap token gets in the way of swapout... */
2184 if (!priority)
2185 disable_swap_token();
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 all_zones_ok = 1;
2188
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002189 /*
2190 * Scan in the highmem->dma direction for the highest
2191 * zone which needs scanning
2192 */
2193 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
2194 struct zone *zone = pgdat->node_zones + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002196 if (!populated_zone(zone))
2197 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
KOSAKI Motohiro93e4a89a2010-03-05 13:41:55 -08002199 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002200 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Rik van Riel556adec2008-10-18 20:26:34 -07002202 /*
2203 * Do some background aging of the anon list, to give
2204 * pages a chance to be referenced before reclaiming.
2205 */
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002206 if (inactive_anon_is_low(zone, &sc))
Rik van Riel556adec2008-10-18 20:26:34 -07002207 shrink_active_list(SWAP_CLUSTER_MAX, zone,
2208 &sc, priority, 0);
2209
Mel Gorman41858962009-06-16 15:32:12 -07002210 if (!zone_watermark_ok(zone, order,
2211 high_wmark_pages(zone), 0, 0)) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002212 end_zone = i;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08002213 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
Andrew Mortone1dbeda2006-12-06 20:32:01 -08002216 if (i < 0)
2217 goto out;
2218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 for (i = 0; i <= end_zone; i++) {
2220 struct zone *zone = pgdat->node_zones + i;
2221
Wu Fengguangadea02a2009-09-21 17:01:42 -07002222 lru_pages += zone_reclaimable_pages(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
2224
2225 /*
2226 * Now scan the zone in the dma->highmem direction, stopping
2227 * at the last zone which needs scanning.
2228 *
2229 * We do this because the page allocator works in the opposite
2230 * direction. This prevents the page allocator from allocating
2231 * pages behind kswapd's direction of progress, which would
2232 * cause too much scanning of the lower zones.
2233 */
2234 for (i = 0; i <= end_zone; i++) {
2235 struct zone *zone = pgdat->node_zones + i;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07002236 int nr_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Con Kolivasf3fe6512006-01-06 00:11:15 -08002238 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 continue;
2240
KOSAKI Motohiro93e4a89a2010-03-05 13:41:55 -08002241 if (zone->all_unreclaimable && priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 continue;
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 sc.nr_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07002245
Balbir Singh4e416952009-09-23 15:56:39 -07002246 /*
2247 * Call soft limit reclaim before calling shrink_zone.
2248 * For now we ignore the return value
2249 */
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07002250 mem_cgroup_soft_limit_reclaim(zone, order, sc.gfp_mask);
2251
Rik van Riel32a43302007-10-16 01:24:50 -07002252 /*
2253 * We put equal pressure on every zone, unless one
2254 * zone has way too many pages free already.
2255 */
Mel Gorman41858962009-06-16 15:32:12 -07002256 if (!zone_watermark_ok(zone, order,
2257 8*high_wmark_pages(zone), end_zone, 0))
Rik van Riela79311c2009-01-06 14:40:01 -08002258 shrink_zone(priority, zone, &sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 reclaim_state->reclaimed_slab = 0;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -07002260 nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
2261 lru_pages);
Rik van Riela79311c2009-01-06 14:40:01 -08002262 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 total_scanned += sc.nr_scanned;
KOSAKI Motohiro93e4a89a2010-03-05 13:41:55 -08002264 if (zone->all_unreclaimable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 continue;
Minchan Kimd1908362010-09-22 13:05:01 -07002266 if (nr_slab == 0 && !zone_reclaimable(zone))
KOSAKI Motohiro93e4a89a2010-03-05 13:41:55 -08002267 zone->all_unreclaimable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 /*
2269 * If we've done a decent amount of scanning and
2270 * the reclaim ratio is low, start doing writepage
2271 * even in laptop mode
2272 */
2273 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
Rik van Riela79311c2009-01-06 14:40:01 -08002274 total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 sc.may_writepage = 1;
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002276
Minchan Kim45973d72010-03-05 13:41:45 -08002277 if (!zone_watermark_ok(zone, order,
2278 high_wmark_pages(zone), end_zone, 0)) {
2279 all_zones_ok = 0;
2280 /*
2281 * We are still under min water mark. This
2282 * means that we have a GFP_ATOMIC allocation
2283 * failure risk. Hurry up!
2284 */
2285 if (!zone_watermark_ok(zone, order,
2286 min_wmark_pages(zone), end_zone, 0))
2287 has_under_min_watermark_zone = 1;
2288 }
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (all_zones_ok)
2292 break; /* kswapd: all done */
2293 /*
2294 * OK, kswapd is getting into trouble. Take a nap, then take
2295 * another pass across the zones.
2296 */
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002297 if (total_scanned && (priority < DEF_PRIORITY - 2)) {
2298 if (has_under_min_watermark_zone)
2299 count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT);
2300 else
2301 congestion_wait(BLK_RW_ASYNC, HZ/10);
2302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 /*
2305 * We do this so kswapd doesn't build up large priorities for
2306 * example when it is freeing in parallel with allocators. It
2307 * matches the direct reclaim path behaviour in terms of impact
2308 * on zone->*_priority.
2309 */
Rik van Riela79311c2009-01-06 14:40:01 -08002310 if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 break;
2312 }
2313out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (!all_zones_ok) {
2315 cond_resched();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002316
2317 try_to_freeze();
2318
KOSAKI Motohiro73ce02e2009-01-06 14:40:33 -08002319 /*
2320 * Fragmentation may mean that the system cannot be
2321 * rebalanced for high-order allocations in all zones.
2322 * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
2323 * it means the zones have been fully scanned and are still
2324 * not balanced. For high-order allocations, there is
2325 * little point trying all over again as kswapd may
2326 * infinite loop.
2327 *
2328 * Instead, recheck all watermarks at order-0 as they
2329 * are the most important. If watermarks are ok, kswapd will go
2330 * back to sleep. High-order users can still perform direct
2331 * reclaim if they wish.
2332 */
2333 if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
2334 order = sc.order = 0;
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 goto loop_again;
2337 }
2338
Rik van Riela79311c2009-01-06 14:40:01 -08002339 return sc.nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
2342/*
2343 * The background pageout daemon, started as a kernel thread
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002344 * from the init process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 *
2346 * This basically trickles out pages so that we have _some_
2347 * free memory available even if there is no other activity
2348 * that frees anything up. This is needed for things like routing
2349 * etc, where we otherwise might have all activity going on in
2350 * asynchronous contexts that cannot page things out.
2351 *
2352 * If there are applications that are active memory-allocators
2353 * (most normal use), this basically shouldn't matter.
2354 */
2355static int kswapd(void *p)
2356{
2357 unsigned long order;
2358 pg_data_t *pgdat = (pg_data_t*)p;
2359 struct task_struct *tsk = current;
2360 DEFINE_WAIT(wait);
2361 struct reclaim_state reclaim_state = {
2362 .reclaimed_slab = 0,
2363 };
Rusty Russella70f7302009-03-13 14:49:46 +10302364 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Nick Piggincf40bd12009-01-21 08:12:39 +01002366 lockdep_set_current_reclaim_state(GFP_KERNEL);
2367
Rusty Russell174596a2009-01-01 10:12:29 +10302368 if (!cpumask_empty(cpumask))
Mike Travisc5f59f02008-04-04 18:11:10 -07002369 set_cpus_allowed_ptr(tsk, cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 current->reclaim_state = &reclaim_state;
2371
2372 /*
2373 * Tell the memory management that we're a "memory allocator",
2374 * and that if we need more memory we should get access to it
2375 * regardless (see "__alloc_pages()"). "kswapd" should
2376 * never get caught in the normal page freeing logic.
2377 *
2378 * (Kswapd normally doesn't need memory anyway, but sometimes
2379 * you need a small amount of memory in order to be able to
2380 * page out something else, and this flag essentially protects
2381 * us from recursively trying to free more memory as we're
2382 * trying to free the first piece of memory in the first place).
2383 */
Christoph Lameter930d9152006-01-08 01:00:47 -08002384 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Rafael J. Wysocki83144182007-07-17 04:03:35 -07002385 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 order = 0;
2388 for ( ; ; ) {
2389 unsigned long new_order;
David Rientjes8fe23e02009-12-14 17:58:33 -08002390 int ret;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2393 new_order = pgdat->kswapd_max_order;
2394 pgdat->kswapd_max_order = 0;
2395 if (order < new_order) {
2396 /*
2397 * Don't sleep if someone wants a larger 'order'
2398 * allocation
2399 */
2400 order = new_order;
2401 } else {
Mel Gormanf50de2d2009-12-14 17:58:53 -08002402 if (!freezing(current) && !kthread_should_stop()) {
2403 long remaining = 0;
2404
2405 /* Try to sleep for a short interval */
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002406 if (!sleeping_prematurely(pgdat, order, remaining)) {
Mel Gormanf50de2d2009-12-14 17:58:53 -08002407 remaining = schedule_timeout(HZ/10);
2408 finish_wait(&pgdat->kswapd_wait, &wait);
2409 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2410 }
2411
2412 /*
2413 * After a short sleep, check if it was a
2414 * premature sleep. If not, then go fully
2415 * to sleep until explicitly woken up
2416 */
Mel Gorman33906bc2010-08-09 17:19:16 -07002417 if (!sleeping_prematurely(pgdat, order, remaining)) {
2418 trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
Mel Gormanf50de2d2009-12-14 17:58:53 -08002419 schedule();
Mel Gorman33906bc2010-08-09 17:19:16 -07002420 } else {
Mel Gormanf50de2d2009-12-14 17:58:53 -08002421 if (remaining)
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002422 count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
Mel Gormanf50de2d2009-12-14 17:58:53 -08002423 else
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002424 count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
Mel Gormanf50de2d2009-12-14 17:58:53 -08002425 }
2426 }
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07002427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 order = pgdat->kswapd_max_order;
2429 }
2430 finish_wait(&pgdat->kswapd_wait, &wait);
2431
David Rientjes8fe23e02009-12-14 17:58:33 -08002432 ret = try_to_freeze();
2433 if (kthread_should_stop())
2434 break;
2435
2436 /*
2437 * We can speed up thawing tasks if we don't call balance_pgdat
2438 * after returning from the refrigerator
2439 */
Mel Gorman33906bc2010-08-09 17:19:16 -07002440 if (!ret) {
2441 trace_mm_vmscan_kswapd_wake(pgdat->node_id, order);
Rafael J. Wysockib1296cc2007-05-06 14:50:48 -07002442 balance_pgdat(pgdat, order);
Mel Gorman33906bc2010-08-09 17:19:16 -07002443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445 return 0;
2446}
2447
2448/*
2449 * A zone is low on free memory, so wake its kswapd task to service it.
2450 */
2451void wakeup_kswapd(struct zone *zone, int order)
2452{
2453 pg_data_t *pgdat;
2454
Con Kolivasf3fe6512006-01-06 00:11:15 -08002455 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return;
2457
2458 pgdat = zone->zone_pgdat;
Mel Gorman41858962009-06-16 15:32:12 -07002459 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return;
2461 if (pgdat->kswapd_max_order < order)
2462 pgdat->kswapd_max_order = order;
Mel Gorman33906bc2010-08-09 17:19:16 -07002463 trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
Paul Jackson02a0e532006-12-13 00:34:25 -08002464 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07002466 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 return;
Con Kolivas8d0986e2005-09-13 01:25:07 -07002468 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
2470
Wu Fengguangadea02a2009-09-21 17:01:42 -07002471/*
2472 * The reclaimable count would be mostly accurate.
2473 * The less reclaimable pages may be
2474 * - mlocked pages, which will be moved to unevictable list when encountered
2475 * - mapped pages, which may require several travels to be reclaimed
2476 * - dirty pages, which is not "instantly" reclaimable
2477 */
2478unsigned long global_reclaimable_pages(void)
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002479{
Wu Fengguangadea02a2009-09-21 17:01:42 -07002480 int nr;
2481
2482 nr = global_page_state(NR_ACTIVE_FILE) +
2483 global_page_state(NR_INACTIVE_FILE);
2484
2485 if (nr_swap_pages > 0)
2486 nr += global_page_state(NR_ACTIVE_ANON) +
2487 global_page_state(NR_INACTIVE_ANON);
2488
2489 return nr;
2490}
2491
2492unsigned long zone_reclaimable_pages(struct zone *zone)
2493{
2494 int nr;
2495
2496 nr = zone_page_state(zone, NR_ACTIVE_FILE) +
2497 zone_page_state(zone, NR_INACTIVE_FILE);
2498
2499 if (nr_swap_pages > 0)
2500 nr += zone_page_state(zone, NR_ACTIVE_ANON) +
2501 zone_page_state(zone, NR_INACTIVE_ANON);
2502
2503 return nr;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002504}
2505
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02002506#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507/*
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08002508 * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002509 * freed pages.
2510 *
2511 * Rather than trying to age LRUs the aim is to preserve the overall
2512 * LRU order by reclaiming preferentially
2513 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 */
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08002515unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002517 struct reclaim_state reclaim_state;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002518 struct scan_control sc = {
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08002519 .gfp_mask = GFP_HIGHUSER_MOVABLE,
2520 .may_swap = 1,
2521 .may_unmap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002522 .may_writepage = 1,
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08002523 .nr_to_reclaim = nr_to_reclaim,
2524 .hibernation_mode = 1,
2525 .swappiness = vm_swappiness,
2526 .order = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 };
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08002528 struct zonelist * zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
2529 struct task_struct *p = current;
2530 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08002532 p->flags |= PF_MEMALLOC;
2533 lockdep_set_current_reclaim_state(sc.gfp_mask);
2534 reclaim_state.reclaimed_slab = 0;
2535 p->reclaim_state = &reclaim_state;
Andrew Morton69e05942006-03-22 00:08:19 -08002536
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08002537 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002538
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08002539 p->reclaim_state = NULL;
2540 lockdep_clear_current_reclaim_state();
2541 p->flags &= ~PF_MEMALLOC;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002542
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08002543 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544}
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02002545#endif /* CONFIG_HIBERNATION */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547/* It's optimal to keep kswapds on the same CPUs as their memory, but
2548 not required for correctness. So if the last cpu in a node goes
2549 away, we get changed to run anywhere: as the first one comes back,
2550 restore their cpu bindings. */
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07002551static int __devinit cpu_callback(struct notifier_block *nfb,
Andrew Morton69e05942006-03-22 00:08:19 -08002552 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07002554 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002556 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07002557 for_each_node_state(nid, N_HIGH_MEMORY) {
Mike Travisc5f59f02008-04-04 18:11:10 -07002558 pg_data_t *pgdat = NODE_DATA(nid);
Rusty Russella70f7302009-03-13 14:49:46 +10302559 const struct cpumask *mask;
2560
2561 mask = cpumask_of_node(pgdat->node_id);
Mike Travisc5f59f02008-04-04 18:11:10 -07002562
Rusty Russell3e597942009-01-01 10:12:24 +10302563 if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 /* One of our CPUs online: restore mask */
Mike Travisc5f59f02008-04-04 18:11:10 -07002565 set_cpus_allowed_ptr(pgdat->kswapd, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567 }
2568 return NOTIFY_OK;
2569}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Yasunori Goto3218ae12006-06-27 02:53:33 -07002571/*
2572 * This kswapd start function will be called by init and node-hot-add.
2573 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
2574 */
2575int kswapd_run(int nid)
2576{
2577 pg_data_t *pgdat = NODE_DATA(nid);
2578 int ret = 0;
2579
2580 if (pgdat->kswapd)
2581 return 0;
2582
2583 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
2584 if (IS_ERR(pgdat->kswapd)) {
2585 /* failure at boot is fatal */
2586 BUG_ON(system_state == SYSTEM_BOOTING);
2587 printk("Failed to start kswapd on node %d\n",nid);
2588 ret = -1;
2589 }
2590 return ret;
2591}
2592
David Rientjes8fe23e02009-12-14 17:58:33 -08002593/*
2594 * Called by memory hotplug when all memory in a node is offlined.
2595 */
2596void kswapd_stop(int nid)
2597{
2598 struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
2599
2600 if (kswapd)
2601 kthread_stop(kswapd);
2602}
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604static int __init kswapd_init(void)
2605{
Yasunori Goto3218ae12006-06-27 02:53:33 -07002606 int nid;
Andrew Morton69e05942006-03-22 00:08:19 -08002607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 swap_setup();
Christoph Lameter9422ffb2007-10-16 01:25:31 -07002609 for_each_node_state(nid, N_HIGH_MEMORY)
Yasunori Goto3218ae12006-06-27 02:53:33 -07002610 kswapd_run(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 hotcpu_notifier(cpu_callback, 0);
2612 return 0;
2613}
2614
2615module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08002616
2617#ifdef CONFIG_NUMA
2618/*
2619 * Zone reclaim mode
2620 *
2621 * If non-zero call zone_reclaim when the number of free pages falls below
2622 * the watermarks.
Christoph Lameter9eeff232006-01-18 17:42:31 -08002623 */
2624int zone_reclaim_mode __read_mostly;
2625
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08002626#define RECLAIM_OFF 0
Fernando Luis Vazquez Cao7d034312008-07-29 22:33:41 -07002627#define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08002628#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
2629#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
2630
Christoph Lameter9eeff232006-01-18 17:42:31 -08002631/*
Christoph Lametera92f7122006-02-01 03:05:32 -08002632 * Priority for ZONE_RECLAIM. This determines the fraction of pages
2633 * of a node considered for each zone_reclaim. 4 scans 1/16th of
2634 * a zone.
2635 */
2636#define ZONE_RECLAIM_PRIORITY 4
2637
Christoph Lameter9eeff232006-01-18 17:42:31 -08002638/*
Christoph Lameter96146342006-07-03 00:24:13 -07002639 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
2640 * occur.
2641 */
2642int sysctl_min_unmapped_ratio = 1;
2643
2644/*
Christoph Lameter0ff38492006-09-25 23:31:52 -07002645 * If the number of slab pages in a zone grows beyond this percentage then
2646 * slab reclaim needs to occur.
2647 */
2648int sysctl_min_slab_ratio = 5;
2649
Mel Gorman90afa5d2009-06-16 15:33:20 -07002650static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
2651{
2652 unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
2653 unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
2654 zone_page_state(zone, NR_ACTIVE_FILE);
2655
2656 /*
2657 * It's possible for there to be more file mapped pages than
2658 * accounted for by the pages on the file LRU lists because
2659 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
2660 */
2661 return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
2662}
2663
2664/* Work out how many page cache pages we can reclaim in this reclaim_mode */
2665static long zone_pagecache_reclaimable(struct zone *zone)
2666{
2667 long nr_pagecache_reclaimable;
2668 long delta = 0;
2669
2670 /*
2671 * If RECLAIM_SWAP is set, then all file pages are considered
2672 * potentially reclaimable. Otherwise, we have to worry about
2673 * pages like swapcache and zone_unmapped_file_pages() provides
2674 * a better estimate
2675 */
2676 if (zone_reclaim_mode & RECLAIM_SWAP)
2677 nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
2678 else
2679 nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
2680
2681 /* If we can't clean pages, remove dirty pages from consideration */
2682 if (!(zone_reclaim_mode & RECLAIM_WRITE))
2683 delta += zone_page_state(zone, NR_FILE_DIRTY);
2684
2685 /* Watch for any possible underflows due to delta */
2686 if (unlikely(delta > nr_pagecache_reclaimable))
2687 delta = nr_pagecache_reclaimable;
2688
2689 return nr_pagecache_reclaimable - delta;
2690}
2691
Christoph Lameter0ff38492006-09-25 23:31:52 -07002692/*
Christoph Lameter9eeff232006-01-18 17:42:31 -08002693 * Try to free up some pages from this zone through reclaim.
2694 */
Andrew Morton179e9632006-03-22 00:08:18 -08002695static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08002696{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08002697 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08002698 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08002699 struct task_struct *p = current;
2700 struct reclaim_state reclaim_state;
Christoph Lameter86959492006-03-22 00:08:18 -08002701 int priority;
Andrew Morton179e9632006-03-22 00:08:18 -08002702 struct scan_control sc = {
2703 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
Johannes Weinera6dc60f82009-03-31 15:19:30 -07002704 .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07002705 .may_swap = 1,
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08002706 .nr_to_reclaim = max_t(unsigned long, nr_pages,
2707 SWAP_CLUSTER_MAX),
Andrew Morton179e9632006-03-22 00:08:18 -08002708 .gfp_mask = gfp_mask,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002709 .swappiness = vm_swappiness,
Johannes Weinerbd2f6192009-03-31 15:19:38 -07002710 .order = order,
Andrew Morton179e9632006-03-22 00:08:18 -08002711 };
KOSAKI Motohiro15748042010-08-09 17:19:50 -07002712 unsigned long nr_slab_pages0, nr_slab_pages1;
Christoph Lameter9eeff232006-01-18 17:42:31 -08002713
Christoph Lameter9eeff232006-01-18 17:42:31 -08002714 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08002715 /*
2716 * We need to be able to allocate from the reserves for RECLAIM_SWAP
2717 * and we also need to be able to write out pages for RECLAIM_WRITE
2718 * and RECLAIM_SWAP.
2719 */
2720 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
KOSAKI Motohiro76ca5422010-03-05 13:41:47 -08002721 lockdep_set_current_reclaim_state(gfp_mask);
Christoph Lameter9eeff232006-01-18 17:42:31 -08002722 reclaim_state.reclaimed_slab = 0;
2723 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db232006-02-01 03:05:29 -08002724
Mel Gorman90afa5d2009-06-16 15:33:20 -07002725 if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
Christoph Lameter0ff38492006-09-25 23:31:52 -07002726 /*
2727 * Free memory by calling shrink zone with increasing
2728 * priorities until we have enough memory freed.
2729 */
2730 priority = ZONE_RECLAIM_PRIORITY;
2731 do {
Rik van Riela79311c2009-01-06 14:40:01 -08002732 shrink_zone(priority, zone, &sc);
Christoph Lameter0ff38492006-09-25 23:31:52 -07002733 priority--;
Rik van Riela79311c2009-01-06 14:40:01 -08002734 } while (priority >= 0 && sc.nr_reclaimed < nr_pages);
Christoph Lameter0ff38492006-09-25 23:31:52 -07002735 }
Christoph Lameterc84db232006-02-01 03:05:29 -08002736
KOSAKI Motohiro15748042010-08-09 17:19:50 -07002737 nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
2738 if (nr_slab_pages0 > zone->min_slab_pages) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08002739 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08002740 * shrink_slab() does not currently allow us to determine how
Christoph Lameter0ff38492006-09-25 23:31:52 -07002741 * many pages were freed in this zone. So we take the current
2742 * number of slab pages and shake the slab until it is reduced
2743 * by the same nr_pages that we used for reclaiming unmapped
2744 * pages.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08002745 *
Christoph Lameter0ff38492006-09-25 23:31:52 -07002746 * Note that shrink_slab will free memory on all zones and may
2747 * take a long time.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08002748 */
KOSAKI Motohiro4dc4b3d2010-08-09 17:19:54 -07002749 for (;;) {
2750 unsigned long lru_pages = zone_reclaimable_pages(zone);
2751
2752 /* No reclaimable slab or very low memory pressure */
2753 if (!shrink_slab(sc.nr_scanned, gfp_mask, lru_pages))
2754 break;
2755
2756 /* Freed enough memory */
2757 nr_slab_pages1 = zone_page_state(zone,
2758 NR_SLAB_RECLAIMABLE);
2759 if (nr_slab_pages1 + nr_pages <= nr_slab_pages0)
2760 break;
2761 }
Christoph Lameter83e33a42006-09-25 23:31:53 -07002762
2763 /*
2764 * Update nr_reclaimed by the number of slab pages we
2765 * reclaimed from this zone.
2766 */
KOSAKI Motohiro15748042010-08-09 17:19:50 -07002767 nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
2768 if (nr_slab_pages1 < nr_slab_pages0)
2769 sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1;
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08002770 }
2771
Christoph Lameter9eeff232006-01-18 17:42:31 -08002772 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08002773 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
KOSAKI Motohiro76ca5422010-03-05 13:41:47 -08002774 lockdep_clear_current_reclaim_state();
Rik van Riela79311c2009-01-06 14:40:01 -08002775 return sc.nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08002776}
Andrew Morton179e9632006-03-22 00:08:18 -08002777
2778int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
2779{
Andrew Morton179e9632006-03-22 00:08:18 -08002780 int node_id;
David Rientjesd773ed62007-10-16 23:26:01 -07002781 int ret;
Andrew Morton179e9632006-03-22 00:08:18 -08002782
2783 /*
Christoph Lameter0ff38492006-09-25 23:31:52 -07002784 * Zone reclaim reclaims unmapped file backed pages and
2785 * slab pages if we are over the defined limits.
Christoph Lameter34aa1332006-06-30 01:55:37 -07002786 *
Christoph Lameter96146342006-07-03 00:24:13 -07002787 * A small portion of unmapped file backed pages is needed for
2788 * file I/O otherwise pages read by file I/O will be immediately
2789 * thrown out if the zone is overallocated. So we do not reclaim
2790 * if less than a specified percentage of the zone is used by
2791 * unmapped file backed pages.
Andrew Morton179e9632006-03-22 00:08:18 -08002792 */
Mel Gorman90afa5d2009-06-16 15:33:20 -07002793 if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
2794 zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
Mel Gormanfa5e0842009-06-16 15:33:22 -07002795 return ZONE_RECLAIM_FULL;
Andrew Morton179e9632006-03-22 00:08:18 -08002796
KOSAKI Motohiro93e4a89a2010-03-05 13:41:55 -08002797 if (zone->all_unreclaimable)
Mel Gormanfa5e0842009-06-16 15:33:22 -07002798 return ZONE_RECLAIM_FULL;
David Rientjesd773ed62007-10-16 23:26:01 -07002799
Andrew Morton179e9632006-03-22 00:08:18 -08002800 /*
David Rientjesd773ed62007-10-16 23:26:01 -07002801 * Do not scan if the allocation should not be delayed.
Andrew Morton179e9632006-03-22 00:08:18 -08002802 */
David Rientjesd773ed62007-10-16 23:26:01 -07002803 if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
Mel Gormanfa5e0842009-06-16 15:33:22 -07002804 return ZONE_RECLAIM_NOSCAN;
Andrew Morton179e9632006-03-22 00:08:18 -08002805
2806 /*
2807 * Only run zone reclaim on the local zone or on zones that do not
2808 * have associated processors. This will favor the local processor
2809 * over remote processors and spread off node memory allocations
2810 * as wide as possible.
2811 */
Christoph Lameter89fa3022006-09-25 23:31:55 -07002812 node_id = zone_to_nid(zone);
Christoph Lameter37c07082007-10-16 01:25:36 -07002813 if (node_state(node_id, N_CPU) && node_id != numa_node_id())
Mel Gormanfa5e0842009-06-16 15:33:22 -07002814 return ZONE_RECLAIM_NOSCAN;
David Rientjesd773ed62007-10-16 23:26:01 -07002815
2816 if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
Mel Gormanfa5e0842009-06-16 15:33:22 -07002817 return ZONE_RECLAIM_NOSCAN;
2818
David Rientjesd773ed62007-10-16 23:26:01 -07002819 ret = __zone_reclaim(zone, gfp_mask, order);
2820 zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
2821
Mel Gorman24cf725182009-06-16 15:33:23 -07002822 if (!ret)
2823 count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
2824
David Rientjesd773ed62007-10-16 23:26:01 -07002825 return ret;
Andrew Morton179e9632006-03-22 00:08:18 -08002826}
Christoph Lameter9eeff232006-01-18 17:42:31 -08002827#endif
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002828
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002829/*
2830 * page_evictable - test whether a page is evictable
2831 * @page: the page to test
2832 * @vma: the VMA in which the page is or will be mapped, may be NULL
2833 *
2834 * Test whether page is evictable--i.e., should be placed on active/inactive
Nick Pigginb291f002008-10-18 20:26:44 -07002835 * lists vs unevictable list. The vma argument is !NULL when called from the
2836 * fault path to determine how to instantate a new page.
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002837 *
2838 * Reasons page might not be evictable:
Lee Schermerhornba9ddf42008-10-18 20:26:42 -07002839 * (1) page's mapping marked unevictable
Nick Pigginb291f002008-10-18 20:26:44 -07002840 * (2) page is part of an mlocked VMA
Lee Schermerhornba9ddf42008-10-18 20:26:42 -07002841 *
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002842 */
2843int page_evictable(struct page *page, struct vm_area_struct *vma)
2844{
2845
Lee Schermerhornba9ddf42008-10-18 20:26:42 -07002846 if (mapping_unevictable(page_mapping(page)))
2847 return 0;
2848
Nick Pigginb291f002008-10-18 20:26:44 -07002849 if (PageMlocked(page) || (vma && is_mlocked_vma(vma, page)))
2850 return 0;
Lee Schermerhorn894bc312008-10-18 20:26:39 -07002851
2852 return 1;
2853}
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002854
2855/**
2856 * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list
2857 * @page: page to check evictability and move to appropriate lru list
2858 * @zone: zone page is in
2859 *
2860 * Checks a page for evictability and moves the page to the appropriate
2861 * zone lru list.
2862 *
2863 * Restrictions: zone->lru_lock must be held, page must be on LRU and must
2864 * have PageUnevictable set.
2865 */
2866static void check_move_unevictable_page(struct page *page, struct zone *zone)
2867{
2868 VM_BUG_ON(PageActive(page));
2869
2870retry:
2871 ClearPageUnevictable(page);
2872 if (page_evictable(page, NULL)) {
Johannes Weiner401a8e12009-09-21 17:02:58 -07002873 enum lru_list l = page_lru_base_type(page);
Lee Schermerhornaf936a12008-10-18 20:26:53 -07002874
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002875 __dec_zone_state(zone, NR_UNEVICTABLE);
2876 list_move(&page->lru, &zone->lru[l].list);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002877 mem_cgroup_move_lists(page, LRU_UNEVICTABLE, l);
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002878 __inc_zone_state(zone, NR_INACTIVE_ANON + l);
2879 __count_vm_event(UNEVICTABLE_PGRESCUED);
2880 } else {
2881 /*
2882 * rotate unevictable list
2883 */
2884 SetPageUnevictable(page);
2885 list_move(&page->lru, &zone->lru[LRU_UNEVICTABLE].list);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002886 mem_cgroup_rotate_lru_list(page, LRU_UNEVICTABLE);
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002887 if (page_evictable(page, NULL))
2888 goto retry;
2889 }
2890}
2891
2892/**
2893 * scan_mapping_unevictable_pages - scan an address space for evictable pages
2894 * @mapping: struct address_space to scan for evictable pages
2895 *
2896 * Scan all pages in mapping. Check unevictable pages for
2897 * evictability and move them to the appropriate zone lru list.
2898 */
2899void scan_mapping_unevictable_pages(struct address_space *mapping)
2900{
2901 pgoff_t next = 0;
2902 pgoff_t end = (i_size_read(mapping->host) + PAGE_CACHE_SIZE - 1) >>
2903 PAGE_CACHE_SHIFT;
2904 struct zone *zone;
2905 struct pagevec pvec;
2906
2907 if (mapping->nrpages == 0)
2908 return;
2909
2910 pagevec_init(&pvec, 0);
2911 while (next < end &&
2912 pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
2913 int i;
2914 int pg_scanned = 0;
2915
2916 zone = NULL;
2917
2918 for (i = 0; i < pagevec_count(&pvec); i++) {
2919 struct page *page = pvec.pages[i];
2920 pgoff_t page_index = page->index;
2921 struct zone *pagezone = page_zone(page);
2922
2923 pg_scanned++;
2924 if (page_index > next)
2925 next = page_index;
2926 next++;
2927
2928 if (pagezone != zone) {
2929 if (zone)
2930 spin_unlock_irq(&zone->lru_lock);
2931 zone = pagezone;
2932 spin_lock_irq(&zone->lru_lock);
2933 }
2934
2935 if (PageLRU(page) && PageUnevictable(page))
2936 check_move_unevictable_page(page, zone);
2937 }
2938 if (zone)
2939 spin_unlock_irq(&zone->lru_lock);
2940 pagevec_release(&pvec);
2941
2942 count_vm_events(UNEVICTABLE_PGSCANNED, pg_scanned);
2943 }
2944
2945}
Lee Schermerhornaf936a12008-10-18 20:26:53 -07002946
2947/**
2948 * scan_zone_unevictable_pages - check unevictable list for evictable pages
2949 * @zone - zone of which to scan the unevictable list
2950 *
2951 * Scan @zone's unevictable LRU lists to check for pages that have become
2952 * evictable. Move those that have to @zone's inactive list where they
2953 * become candidates for reclaim, unless shrink_inactive_zone() decides
2954 * to reactivate them. Pages that are still unevictable are rotated
2955 * back onto @zone's unevictable list.
2956 */
2957#define SCAN_UNEVICTABLE_BATCH_SIZE 16UL /* arbitrary lock hold batch size */
KOSAKI Motohiro14b90b22009-01-06 14:39:45 -08002958static void scan_zone_unevictable_pages(struct zone *zone)
Lee Schermerhornaf936a12008-10-18 20:26:53 -07002959{
2960 struct list_head *l_unevictable = &zone->lru[LRU_UNEVICTABLE].list;
2961 unsigned long scan;
2962 unsigned long nr_to_scan = zone_page_state(zone, NR_UNEVICTABLE);
2963
2964 while (nr_to_scan > 0) {
2965 unsigned long batch_size = min(nr_to_scan,
2966 SCAN_UNEVICTABLE_BATCH_SIZE);
2967
2968 spin_lock_irq(&zone->lru_lock);
2969 for (scan = 0; scan < batch_size; scan++) {
2970 struct page *page = lru_to_page(l_unevictable);
2971
2972 if (!trylock_page(page))
2973 continue;
2974
2975 prefetchw_prev_lru_page(page, l_unevictable, flags);
2976
2977 if (likely(PageLRU(page) && PageUnevictable(page)))
2978 check_move_unevictable_page(page, zone);
2979
2980 unlock_page(page);
2981 }
2982 spin_unlock_irq(&zone->lru_lock);
2983
2984 nr_to_scan -= batch_size;
2985 }
2986}
2987
2988
2989/**
2990 * scan_all_zones_unevictable_pages - scan all unevictable lists for evictable pages
2991 *
2992 * A really big hammer: scan all zones' unevictable LRU lists to check for
2993 * pages that have become evictable. Move those back to the zones'
2994 * inactive list where they become candidates for reclaim.
2995 * This occurs when, e.g., we have unswappable pages on the unevictable lists,
2996 * and we add swap to the system. As such, it runs in the context of a task
2997 * that has possibly/probably made some previously unevictable pages
2998 * evictable.
2999 */
KOSAKI Motohiroff301532009-01-06 14:39:44 -08003000static void scan_all_zones_unevictable_pages(void)
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003001{
3002 struct zone *zone;
3003
3004 for_each_zone(zone) {
3005 scan_zone_unevictable_pages(zone);
3006 }
3007}
3008
3009/*
3010 * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of
3011 * all nodes' unevictable lists for evictable pages
3012 */
3013unsigned long scan_unevictable_pages;
3014
3015int scan_unevictable_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003016 void __user *buffer,
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003017 size_t *length, loff_t *ppos)
3018{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003019 proc_doulongvec_minmax(table, write, buffer, length, ppos);
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003020
3021 if (write && *(unsigned long *)table->data)
3022 scan_all_zones_unevictable_pages();
3023
3024 scan_unevictable_pages = 0;
3025 return 0;
3026}
3027
Thadeu Lima de Souza Cascardoe4455ab2010-10-26 14:21:28 -07003028#ifdef CONFIG_NUMA
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003029/*
3030 * per node 'scan_unevictable_pages' attribute. On demand re-scan of
3031 * a specified node's per zone unevictable lists for evictable pages.
3032 */
3033
3034static ssize_t read_scan_unevictable_node(struct sys_device *dev,
3035 struct sysdev_attribute *attr,
3036 char *buf)
3037{
3038 return sprintf(buf, "0\n"); /* always zero; should fit... */
3039}
3040
3041static ssize_t write_scan_unevictable_node(struct sys_device *dev,
3042 struct sysdev_attribute *attr,
3043 const char *buf, size_t count)
3044{
3045 struct zone *node_zones = NODE_DATA(dev->id)->node_zones;
3046 struct zone *zone;
3047 unsigned long res;
3048 unsigned long req = strict_strtoul(buf, 10, &res);
3049
3050 if (!req)
3051 return 1; /* zero is no-op */
3052
3053 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
3054 if (!populated_zone(zone))
3055 continue;
3056 scan_zone_unevictable_pages(zone);
3057 }
3058 return 1;
3059}
3060
3061
3062static SYSDEV_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
3063 read_scan_unevictable_node,
3064 write_scan_unevictable_node);
3065
3066int scan_unevictable_register_node(struct node *node)
3067{
3068 return sysdev_create_file(&node->sysdev, &attr_scan_unevictable_pages);
3069}
3070
3071void scan_unevictable_unregister_node(struct node *node)
3072{
3073 sysdev_remove_file(&node->sysdev, &attr_scan_unevictable_pages);
3074}
Thadeu Lima de Souza Cascardoe4455ab2010-10-26 14:21:28 -07003075#endif