blob: d5a7f07dadaf24392d4af7bac0c3b8d49b46f85e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/backing-dev.h>
30#include <linux/rmap.h>
31#include <linux/topology.h>
32#include <linux/cpu.h>
33#include <linux/cpuset.h>
Mel Gorman3e7d3442011-01-13 15:45:56 -080034#include <linux/compaction.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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>
KOSAKI Motohiro929bea72011-04-14 15:22:12 -070043#include <linux/oom.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070044#include <linux/prefetch.h>
Rebecca Schultz Zavinc9b7d472012-10-05 13:54:59 -070045#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <asm/tlbflush.h>
48#include <asm/div64.h>
49
50#include <linux/swapops.h>
51
Nick Piggin0f8053a2006-03-22 00:08:33 -080052#include "internal.h"
53
Mel Gorman33906bc2010-08-09 17:19:16 -070054#define CREATE_TRACE_POINTS
55#include <trace/events/vmscan.h>
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057struct scan_control {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 /* Incremented by the number of inactive pages that were scanned */
59 unsigned long nr_scanned;
60
Rik van Riela79311c2009-01-06 14:40:01 -080061 /* Number of pages freed so far during a call to shrink_zones() */
62 unsigned long nr_reclaimed;
63
KOSAKI Motohiro22fba332009-12-14 17:59:10 -080064 /* How many pages shrink_list() should reclaim */
65 unsigned long nr_to_reclaim;
66
KOSAKI Motohiro7b517552009-12-14 17:59:12 -080067 unsigned long hibernation_mode;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 /* This context's GFP mask */
Al Viro6daa0e22005-10-21 03:18:50 -040070 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 int may_writepage;
73
Johannes Weinera6dc60f82009-03-31 15:19:30 -070074 /* Can mapped pages be reclaimed? */
75 int may_unmap;
Christoph Lameterf1fd1062006-01-18 17:42:30 -080076
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -070077 /* Can pages be swapped as part of reclaim? */
78 int may_swap;
79
Andy Whitcroft5ad333e2007-07-17 04:03:16 -070080 int order;
Balbir Singh66e17072008-02-07 00:13:56 -080081
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -070082 /* Scan (total_size >> priority) pages at once */
83 int priority;
84
KOSAKI Motohiro5f53e762010-05-24 14:32:37 -070085 /*
Johannes Weinerf16015f2012-01-12 17:17:52 -080086 * The memory cgroup that hit its limit and as a result is the
87 * primary target of this reclaim invocation.
88 */
89 struct mem_cgroup *target_mem_cgroup;
Balbir Singh66e17072008-02-07 00:13:56 -080090
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -070091 /*
92 * Nodemask of nodes allowed by the caller. If NULL, all nodes
93 * are scanned.
94 */
95 nodemask_t *nodemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
99
100#ifdef ARCH_HAS_PREFETCH
101#define prefetch_prev_lru_page(_page, _base, _field) \
102 do { \
103 if ((_page)->lru.prev != _base) { \
104 struct page *prev; \
105 \
106 prev = lru_to_page(&(_page->lru)); \
107 prefetch(&prev->_field); \
108 } \
109 } while (0)
110#else
111#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
112#endif
113
114#ifdef ARCH_HAS_PREFETCHW
115#define prefetchw_prev_lru_page(_page, _base, _field) \
116 do { \
117 if ((_page)->lru.prev != _base) { \
118 struct page *prev; \
119 \
120 prev = lru_to_page(&(_page->lru)); \
121 prefetchw(&prev->_field); \
122 } \
123 } while (0)
124#else
125#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
126#endif
127
128/*
129 * From 0 .. 100. Higher means more swappy.
130 */
131int vm_swappiness = 60;
Zhang Yanfeib21e0b92013-02-22 16:35:48 -0800132unsigned long vm_total_pages; /* The total number of pages which the VM controls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134static LIST_HEAD(shrinker_list);
135static DECLARE_RWSEM(shrinker_rwsem);
136
Andrew Mortonc255a452012-07-31 16:43:02 -0700137#ifdef CONFIG_MEMCG
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800138static bool global_reclaim(struct scan_control *sc)
139{
Johannes Weinerf16015f2012-01-12 17:17:52 -0800140 return !sc->target_mem_cgroup;
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800141}
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -0800142#else
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800143static bool global_reclaim(struct scan_control *sc)
144{
145 return true;
146}
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -0800147#endif
148
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700149static unsigned long get_lru_size(struct lruvec *lruvec, enum lru_list lru)
KOSAKI Motohiroc9f299d2009-01-07 18:08:16 -0800150{
Hugh Dickinsc3c787e2012-05-29 15:06:52 -0700151 if (!mem_cgroup_disabled())
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700152 return mem_cgroup_get_lru_size(lruvec, lru);
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800153
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700154 return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru);
KOSAKI Motohiroc9f299d2009-01-07 18:08:16 -0800155}
156
Rebecca Schultz Zavinc9b7d472012-10-05 13:54:59 -0700157struct dentry *debug_file;
158
159static int debug_shrinker_show(struct seq_file *s, void *unused)
160{
161 struct shrinker *shrinker;
162 struct shrink_control sc;
163
164 sc.gfp_mask = -1;
165 sc.nr_to_scan = 0;
166
167 down_read(&shrinker_rwsem);
168 list_for_each_entry(shrinker, &shrinker_list, list) {
169 char name[64];
170 int num_objs;
171
172 num_objs = shrinker->shrink(shrinker, &sc);
173 seq_printf(s, "%pf %d\n", shrinker->shrink, num_objs);
174 }
175 up_read(&shrinker_rwsem);
176 return 0;
177}
178
179static int debug_shrinker_open(struct inode *inode, struct file *file)
180{
181 return single_open(file, debug_shrinker_show, inode->i_private);
182}
183
184static const struct file_operations debug_shrinker_fops = {
185 .open = debug_shrinker_open,
186 .read = seq_read,
187 .llseek = seq_lseek,
188 .release = single_release,
189};
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
192 * Add a shrinker callback to be called from the vm
193 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700194void register_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Konstantin Khlebnikov83aeead2011-12-08 14:33:54 -0800196 atomic_long_set(&shrinker->nr_in_batch, 0);
Rusty Russell8e1f9362007-07-17 04:03:17 -0700197 down_write(&shrinker_rwsem);
198 list_add_tail(&shrinker->list, &shrinker_list);
199 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700201EXPORT_SYMBOL(register_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Rebecca Schultz Zavinc9b7d472012-10-05 13:54:59 -0700203static int __init add_shrinker_debug(void)
204{
205 debugfs_create_file("shrinker", 0644, NULL, NULL,
206 &debug_shrinker_fops);
207 return 0;
208}
209
210late_initcall(add_shrinker_debug);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
213 * Remove one
214 */
Rusty Russell8e1f9362007-07-17 04:03:17 -0700215void unregister_shrinker(struct shrinker *shrinker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 down_write(&shrinker_rwsem);
218 list_del(&shrinker->list);
219 up_write(&shrinker_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
Rusty Russell8e1f9362007-07-17 04:03:17 -0700221EXPORT_SYMBOL(unregister_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Ying Han1495f232011-05-24 17:12:27 -0700223static inline int do_shrinker_shrink(struct shrinker *shrinker,
224 struct shrink_control *sc,
225 unsigned long nr_to_scan)
226{
227 sc->nr_to_scan = nr_to_scan;
228 return (*shrinker->shrink)(shrinker, sc);
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#define SHRINK_BATCH 128
232/*
233 * Call the shrink functions to age shrinkable caches
234 *
235 * Here we assume it costs one seek to replace a lru page and that it also
236 * takes a seek to recreate a cache object. With this in mind we age equal
237 * percentages of the lru and ageable caches. This should balance the seeks
238 * generated by these structures.
239 *
Simon Arlott183ff222007-10-20 01:27:18 +0200240 * If the vm encountered mapped pages on the LRU it increase the pressure on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * slab to avoid swapping.
242 *
243 * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
244 *
245 * `lru_pages' represents the number of on-LRU pages in all the zones which
246 * are eligible for the caller's allocation attempt. It is used for balancing
247 * slab reclaim versus page reclaim.
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700248 *
249 * Returns the number of slab objects which we shrunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 */
Ying Hana09ed5e2011-05-24 17:12:26 -0700251unsigned long shrink_slab(struct shrink_control *shrink,
Ying Han1495f232011-05-24 17:12:27 -0700252 unsigned long nr_pages_scanned,
Ying Hana09ed5e2011-05-24 17:12:26 -0700253 unsigned long lru_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct shrinker *shrinker;
Andrew Morton69e05942006-03-22 00:08:19 -0800256 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Ying Han1495f232011-05-24 17:12:27 -0700258 if (nr_pages_scanned == 0)
259 nr_pages_scanned = SWAP_CLUSTER_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Minchan Kimf06590b2011-05-24 17:11:11 -0700261 if (!down_read_trylock(&shrinker_rwsem)) {
262 /* Assume we'll be able to shrink next time */
263 ret = 1;
264 goto out;
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 list_for_each_entry(shrinker, &shrinker_list, list) {
268 unsigned long long delta;
Konstantin Khlebnikov635697c2011-12-08 14:33:51 -0800269 long total_scan;
270 long max_pass;
Dave Chinner09576072011-07-08 14:14:34 +1000271 int shrink_ret = 0;
Dave Chinneracf92b42011-07-08 14:14:35 +1000272 long nr;
273 long new_nr;
Dave Chinnere9299f52011-07-08 14:14:37 +1000274 long batch_size = shrinker->batch ? shrinker->batch
275 : SHRINK_BATCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Konstantin Khlebnikov635697c2011-12-08 14:33:51 -0800277 max_pass = do_shrinker_shrink(shrinker, shrink, 0);
278 if (max_pass <= 0)
279 continue;
280
Dave Chinneracf92b42011-07-08 14:14:35 +1000281 /*
282 * copy the current shrinker scan count into a local variable
283 * and zero it so that other concurrent shrinker invocations
284 * don't also do this scanning work.
285 */
Konstantin Khlebnikov83aeead2011-12-08 14:33:54 -0800286 nr = atomic_long_xchg(&shrinker->nr_in_batch, 0);
Dave Chinneracf92b42011-07-08 14:14:35 +1000287
288 total_scan = nr;
Ying Han1495f232011-05-24 17:12:27 -0700289 delta = (4 * nr_pages_scanned) / shrinker->seeks;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800290 delta *= max_pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 do_div(delta, lru_pages + 1);
Dave Chinneracf92b42011-07-08 14:14:35 +1000292 total_scan += delta;
293 if (total_scan < 0) {
David Rientjes88c3bd72009-03-31 15:23:29 -0700294 printk(KERN_ERR "shrink_slab: %pF negative objects to "
295 "delete nr=%ld\n",
Dave Chinneracf92b42011-07-08 14:14:35 +1000296 shrinker->shrink, total_scan);
297 total_scan = max_pass;
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800298 }
299
300 /*
Dave Chinner3567b592011-07-08 14:14:36 +1000301 * We need to avoid excessive windup on filesystem shrinkers
302 * due to large numbers of GFP_NOFS allocations causing the
303 * shrinkers to return -1 all the time. This results in a large
304 * nr being built up so when a shrink that can do some work
305 * comes along it empties the entire cache due to nr >>>
306 * max_pass. This is bad for sustaining a working set in
307 * memory.
308 *
309 * Hence only allow the shrinker to scan the entire cache when
310 * a large delta change is calculated directly.
311 */
312 if (delta < max_pass / 4)
313 total_scan = min(total_scan, max_pass / 2);
314
315 /*
Andrea Arcangeliea164d72005-11-28 13:44:15 -0800316 * Avoid risking looping forever due to too large nr value:
317 * never try to free more than twice the estimate number of
318 * freeable entries.
319 */
Dave Chinneracf92b42011-07-08 14:14:35 +1000320 if (total_scan > max_pass * 2)
321 total_scan = max_pass * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Dave Chinneracf92b42011-07-08 14:14:35 +1000323 trace_mm_shrink_slab_start(shrinker, shrink, nr,
Dave Chinner09576072011-07-08 14:14:34 +1000324 nr_pages_scanned, lru_pages,
325 max_pass, delta, total_scan);
326
Dave Chinnere9299f52011-07-08 14:14:37 +1000327 while (total_scan >= batch_size) {
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700328 int nr_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Ying Han1495f232011-05-24 17:12:27 -0700330 nr_before = do_shrinker_shrink(shrinker, shrink, 0);
331 shrink_ret = do_shrinker_shrink(shrinker, shrink,
Dave Chinnere9299f52011-07-08 14:14:37 +1000332 batch_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (shrink_ret == -1)
334 break;
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700335 if (shrink_ret < nr_before)
336 ret += nr_before - shrink_ret;
Dave Chinnere9299f52011-07-08 14:14:37 +1000337 count_vm_events(SLABS_SCANNED, batch_size);
338 total_scan -= batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 cond_resched();
341 }
342
Dave Chinneracf92b42011-07-08 14:14:35 +1000343 /*
344 * move the unused scan count back into the shrinker in a
345 * manner that handles concurrent updates. If we exhausted the
346 * scan, there is no need to do an update.
347 */
Konstantin Khlebnikov83aeead2011-12-08 14:33:54 -0800348 if (total_scan > 0)
349 new_nr = atomic_long_add_return(total_scan,
350 &shrinker->nr_in_batch);
351 else
352 new_nr = atomic_long_read(&shrinker->nr_in_batch);
Dave Chinneracf92b42011-07-08 14:14:35 +1000353
354 trace_mm_shrink_slab_end(shrinker, shrink_ret, nr, new_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 up_read(&shrinker_rwsem);
Minchan Kimf06590b2011-05-24 17:11:11 -0700357out:
358 cond_resched();
akpm@osdl.orgb15e0902005-06-21 17:14:35 -0700359 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static inline int is_page_cache_freeable(struct page *page)
363{
Johannes Weinerceddc3a2009-09-21 17:03:00 -0700364 /*
365 * A freeable page cache page is referenced only by the caller
366 * that isolated the page, the page cache radix tree and
367 * optional buffer heads at page->private.
368 */
Johannes Weineredcf4742009-09-21 17:02:59 -0700369 return page_count(page) - page_has_private(page) == 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700372static int may_write_to_queue(struct backing_dev_info *bdi,
373 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Christoph Lameter930d9152006-01-08 01:00:47 -0800375 if (current->flags & PF_SWAPWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 1;
377 if (!bdi_write_congested(bdi))
378 return 1;
379 if (bdi == current->backing_dev_info)
380 return 1;
381 return 0;
382}
383
384/*
385 * We detected a synchronous write error writing a page out. Probably
386 * -ENOSPC. We need to propagate that into the address_space for a subsequent
387 * fsync(), msync() or close().
388 *
389 * The tricky part is that after writepage we cannot touch the mapping: nothing
390 * prevents it from being freed up. But we have a ref on the page and once
391 * that page is locked, the mapping is pinned.
392 *
393 * We're allowed to run sleeping lock_page() here because we know the caller has
394 * __GFP_FS.
395 */
396static void handle_write_error(struct address_space *mapping,
397 struct page *page, int error)
398{
Jens Axboe7eaceac2011-03-10 08:52:07 +0100399 lock_page(page);
Guillaume Chazarain3e9f45b2007-05-08 00:23:25 -0700400 if (page_mapping(page) == mapping)
401 mapping_set_error(mapping, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 unlock_page(page);
403}
404
Christoph Lameter04e62a22006-06-23 02:03:38 -0700405/* possible outcome of pageout() */
406typedef enum {
407 /* failed to write page out, page is locked */
408 PAGE_KEEP,
409 /* move page to the active list, page is locked */
410 PAGE_ACTIVATE,
411 /* page has been sent to the disk successfully, page is unlocked */
412 PAGE_SUCCESS,
413 /* page is clean and locked */
414 PAGE_CLEAN,
415} pageout_t;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/*
Andrew Morton1742f192006-03-22 00:08:21 -0800418 * pageout is called by shrink_page_list() for each dirty page.
419 * Calls ->writepage().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Andy Whitcroftc661b072007-08-22 14:01:26 -0700421static pageout_t pageout(struct page *page, struct address_space *mapping,
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700422 struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 /*
425 * If the page is dirty, only perform writeback if that write
426 * will be non-blocking. To prevent this allocation from being
427 * stalled by pagecache activity. But note that there may be
428 * stalls if we need to run get_block(). We could test
429 * PagePrivate for that.
430 *
Vincent Li6aceb532009-12-14 17:58:49 -0800431 * If this process is currently in __generic_file_aio_write() against
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * this page's queue, we can perform writeback even if that
433 * will block.
434 *
435 * If the page is swapcache, write it back even if that would
436 * block, for some throttling. This happens by accident, because
437 * swap_backing_dev_info is bust: it doesn't reflect the
438 * congestion state of the swapdevs. Easy to fix, if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 */
440 if (!is_page_cache_freeable(page))
441 return PAGE_KEEP;
442 if (!mapping) {
443 /*
444 * Some data journaling orphaned pages can have
445 * page->mapping == NULL while being dirty with clean buffers.
446 */
David Howells266cf652009-04-03 16:42:36 +0100447 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (try_to_free_buffers(page)) {
449 ClearPageDirty(page);
Harvey Harrisond40cee22008-04-30 00:55:07 -0700450 printk("%s: orphaned page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return PAGE_CLEAN;
452 }
453 }
454 return PAGE_KEEP;
455 }
456 if (mapping->a_ops->writepage == NULL)
457 return PAGE_ACTIVATE;
Mel Gorman0e093d992010-10-26 14:21:45 -0700458 if (!may_write_to_queue(mapping->backing_dev_info, sc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return PAGE_KEEP;
460
461 if (clear_page_dirty_for_io(page)) {
462 int res;
463 struct writeback_control wbc = {
464 .sync_mode = WB_SYNC_NONE,
465 .nr_to_write = SWAP_CLUSTER_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700466 .range_start = 0,
467 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 .for_reclaim = 1,
469 };
470
471 SetPageReclaim(page);
472 res = mapping->a_ops->writepage(page, &wbc);
473 if (res < 0)
474 handle_write_error(mapping, page, res);
Zach Brown994fc28c2005-12-15 14:28:17 -0800475 if (res == AOP_WRITEPAGE_ACTIVATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 ClearPageReclaim(page);
477 return PAGE_ACTIVATE;
478 }
Andy Whitcroftc661b072007-08-22 14:01:26 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (!PageWriteback(page)) {
481 /* synchronous write or broken a_ops? */
482 ClearPageReclaim(page);
483 }
Mel Gorman23b9da52012-05-29 15:06:20 -0700484 trace_mm_vmscan_writepage(page, trace_reclaim_flags(page));
Andrew Mortone129b5c2006-09-27 01:50:00 -0700485 inc_zone_page_state(page, NR_VMSCAN_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return PAGE_SUCCESS;
487 }
488
489 return PAGE_CLEAN;
490}
491
Andrew Mortona649fd92006-10-17 00:09:36 -0700492/*
Nick Piggine2867812008-07-25 19:45:30 -0700493 * Same as remove_mapping, but if the page is removed from the mapping, it
494 * gets returned with a refcount of 0.
Andrew Mortona649fd92006-10-17 00:09:36 -0700495 */
Nick Piggine2867812008-07-25 19:45:30 -0700496static int __remove_mapping(struct address_space *mapping, struct page *page)
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800497{
Nick Piggin28e4d962006-09-25 23:31:23 -0700498 BUG_ON(!PageLocked(page));
499 BUG_ON(mapping != page_mapping(page));
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800500
Nick Piggin19fd6232008-07-25 19:45:32 -0700501 spin_lock_irq(&mapping->tree_lock);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800502 /*
Nick Piggin0fd0e6b2006-09-27 01:50:02 -0700503 * The non racy check for a busy page.
504 *
505 * Must be careful with the order of the tests. When someone has
506 * a ref to the page, it may be possible that they dirty it then
507 * drop the reference. So if PageDirty is tested before page_count
508 * here, then the following race may occur:
509 *
510 * get_user_pages(&page);
511 * [user mapping goes away]
512 * write_to(page);
513 * !PageDirty(page) [good]
514 * SetPageDirty(page);
515 * put_page(page);
516 * !page_count(page) [good, discard it]
517 *
518 * [oops, our write_to data is lost]
519 *
520 * Reversing the order of the tests ensures such a situation cannot
521 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
522 * load is not satisfied before that of page->_count.
523 *
524 * Note that if SetPageDirty is always performed via set_page_dirty,
525 * and thus under tree_lock, then this ordering is not required.
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800526 */
Nick Piggine2867812008-07-25 19:45:30 -0700527 if (!page_freeze_refs(page, 2))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800528 goto cannot_free;
Nick Piggine2867812008-07-25 19:45:30 -0700529 /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
530 if (unlikely(PageDirty(page))) {
531 page_unfreeze_refs(page, 2);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800532 goto cannot_free;
Nick Piggine2867812008-07-25 19:45:30 -0700533 }
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800534
535 if (PageSwapCache(page)) {
536 swp_entry_t swap = { .val = page_private(page) };
537 __delete_from_swap_cache(page);
Nick Piggin19fd6232008-07-25 19:45:32 -0700538 spin_unlock_irq(&mapping->tree_lock);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700539 swapcache_free(swap, page);
Nick Piggine2867812008-07-25 19:45:30 -0700540 } else {
Linus Torvalds6072d132010-12-01 13:35:19 -0500541 void (*freepage)(struct page *);
542
543 freepage = mapping->a_ops->freepage;
544
Minchan Kime64a7822011-03-22 16:32:44 -0700545 __delete_from_page_cache(page);
Nick Piggin19fd6232008-07-25 19:45:32 -0700546 spin_unlock_irq(&mapping->tree_lock);
Daisuke Nishimurae767e052009-05-28 14:34:28 -0700547 mem_cgroup_uncharge_cache_page(page);
Linus Torvalds6072d132010-12-01 13:35:19 -0500548
549 if (freepage != NULL)
550 freepage(page);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800551 }
552
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800553 return 1;
554
555cannot_free:
Nick Piggin19fd6232008-07-25 19:45:32 -0700556 spin_unlock_irq(&mapping->tree_lock);
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800557 return 0;
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560/*
Nick Piggine2867812008-07-25 19:45:30 -0700561 * Attempt to detach a locked page from its ->mapping. If it is dirty or if
562 * someone else has a ref on the page, abort and return 0. If it was
563 * successfully detached, return 1. Assumes the caller has a single ref on
564 * this page.
565 */
566int remove_mapping(struct address_space *mapping, struct page *page)
567{
568 if (__remove_mapping(mapping, page)) {
569 /*
570 * Unfreezing the refcount with 1 rather than 2 effectively
571 * drops the pagecache ref for us without requiring another
572 * atomic operation.
573 */
574 page_unfreeze_refs(page, 1);
575 return 1;
576 }
577 return 0;
578}
579
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700580/**
581 * putback_lru_page - put previously isolated page onto appropriate LRU list
582 * @page: page to be put back to appropriate lru list
583 *
584 * Add previously isolated @page to appropriate LRU list.
585 * Page may still be unevictable for other reasons.
586 *
587 * lru_lock must not be held, interrupts must be enabled.
588 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700589void putback_lru_page(struct page *page)
590{
591 int lru;
592 int active = !!TestClearPageActive(page);
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700593 int was_unevictable = PageUnevictable(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700594
595 VM_BUG_ON(PageLRU(page));
596
597redo:
598 ClearPageUnevictable(page);
599
Hugh Dickins39b5f292012-10-08 16:33:18 -0700600 if (page_evictable(page)) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700601 /*
602 * For evictable pages, we can use the cache.
603 * In event of a race, worst case is we end up with an
604 * unevictable page on [in]active list.
605 * We know how to handle that.
606 */
Johannes Weiner401a8e12009-09-21 17:02:58 -0700607 lru = active + page_lru_base_type(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700608 lru_cache_add_lru(page, lru);
609 } else {
610 /*
611 * Put unevictable pages directly on zone's unevictable
612 * list.
613 */
614 lru = LRU_UNEVICTABLE;
615 add_page_to_unevictable_list(page);
Johannes Weiner6a7b9542009-10-26 16:50:00 -0700616 /*
Minchan Kim21ee9f32011-10-31 17:09:28 -0700617 * When racing with an mlock or AS_UNEVICTABLE clearing
618 * (page is unlocked) make sure that if the other thread
619 * does not observe our setting of PG_lru and fails
Hugh Dickins24513262012-01-20 14:34:21 -0800620 * isolation/check_move_unevictable_pages,
Minchan Kim21ee9f32011-10-31 17:09:28 -0700621 * we see PG_mlocked/AS_UNEVICTABLE cleared below and move
Johannes Weiner6a7b9542009-10-26 16:50:00 -0700622 * the page back to the evictable list.
623 *
Minchan Kim21ee9f32011-10-31 17:09:28 -0700624 * The other side is TestClearPageMlocked() or shmem_lock().
Johannes Weiner6a7b9542009-10-26 16:50:00 -0700625 */
626 smp_mb();
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700627 }
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700628
629 /*
630 * page's status can change while we move it among lru. If an evictable
631 * page is on unevictable list, it never be freed. To avoid that,
632 * check after we added it to the list, again.
633 */
Hugh Dickins39b5f292012-10-08 16:33:18 -0700634 if (lru == LRU_UNEVICTABLE && page_evictable(page)) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700635 if (!isolate_lru_page(page)) {
636 put_page(page);
637 goto redo;
638 }
639 /* This means someone else dropped this page from LRU
640 * So, it will be freed or putback to LRU again. There is
641 * nothing to do here.
642 */
643 }
644
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700645 if (was_unevictable && lru != LRU_UNEVICTABLE)
646 count_vm_event(UNEVICTABLE_PGRESCUED);
647 else if (!was_unevictable && lru == LRU_UNEVICTABLE)
648 count_vm_event(UNEVICTABLE_PGCULLED);
649
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700650 put_page(page); /* drop ref from isolate */
651}
652
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800653enum page_references {
654 PAGEREF_RECLAIM,
655 PAGEREF_RECLAIM_CLEAN,
Johannes Weiner645747462010-03-05 13:42:22 -0800656 PAGEREF_KEEP,
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800657 PAGEREF_ACTIVATE,
658};
659
660static enum page_references page_check_references(struct page *page,
661 struct scan_control *sc)
662{
Johannes Weiner645747462010-03-05 13:42:22 -0800663 int referenced_ptes, referenced_page;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800664 unsigned long vm_flags;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800665
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700666 referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup,
667 &vm_flags);
Johannes Weiner645747462010-03-05 13:42:22 -0800668 referenced_page = TestClearPageReferenced(page);
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800669
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800670 /*
671 * Mlock lost the isolation race with us. Let try_to_unmap()
672 * move the page to the unevictable list.
673 */
674 if (vm_flags & VM_LOCKED)
675 return PAGEREF_RECLAIM;
676
Johannes Weiner645747462010-03-05 13:42:22 -0800677 if (referenced_ptes) {
Michal Hockoe48982732012-05-29 15:06:45 -0700678 if (PageSwapBacked(page))
Johannes Weiner645747462010-03-05 13:42:22 -0800679 return PAGEREF_ACTIVATE;
680 /*
681 * All mapped pages start out with page table
682 * references from the instantiating fault, so we need
683 * to look twice if a mapped file page is used more
684 * than once.
685 *
686 * Mark it and spare it for another trip around the
687 * inactive list. Another page table reference will
688 * lead to its activation.
689 *
690 * Note: the mark is set for activated pages as well
691 * so that recently deactivated but used pages are
692 * quickly recovered.
693 */
694 SetPageReferenced(page);
695
Konstantin Khlebnikov34dbc672012-01-10 15:06:59 -0800696 if (referenced_page || referenced_ptes > 1)
Johannes Weiner645747462010-03-05 13:42:22 -0800697 return PAGEREF_ACTIVATE;
698
Konstantin Khlebnikovc909e992012-01-10 15:07:03 -0800699 /*
700 * Activate file-backed executable pages after first usage.
701 */
702 if (vm_flags & VM_EXEC)
703 return PAGEREF_ACTIVATE;
704
Johannes Weiner645747462010-03-05 13:42:22 -0800705 return PAGEREF_KEEP;
706 }
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800707
708 /* Reclaim if clean, defer dirty pages to writeback */
KOSAKI Motohiro2e302442010-10-26 14:21:46 -0700709 if (referenced_page && !PageSwapBacked(page))
Johannes Weiner645747462010-03-05 13:42:22 -0800710 return PAGEREF_RECLAIM_CLEAN;
711
712 return PAGEREF_RECLAIM;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800713}
714
Nick Piggine2867812008-07-25 19:45:30 -0700715/*
Andrew Morton1742f192006-03-22 00:08:21 -0800716 * shrink_page_list() returns the number of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 */
Andrew Morton1742f192006-03-22 00:08:21 -0800718static unsigned long shrink_page_list(struct list_head *page_list,
Konstantin Khlebnikov6a18adb2012-05-29 15:06:59 -0700719 struct zone *zone,
Mel Gormanf84f6e22011-10-31 17:07:51 -0700720 struct scan_control *sc,
Minchan Kim02c6de82012-10-08 16:31:55 -0700721 enum ttu_flags ttu_flags,
Mel Gorman92df3a72011-10-31 17:07:56 -0700722 unsigned long *ret_nr_dirty,
Minchan Kim02c6de82012-10-08 16:31:55 -0700723 unsigned long *ret_nr_writeback,
724 bool force_reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 LIST_HEAD(ret_pages);
Mel Gormanabe4c3b2010-08-09 17:19:31 -0700727 LIST_HEAD(free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 int pgactivate = 0;
Mel Gorman0e093d992010-10-26 14:21:45 -0700729 unsigned long nr_dirty = 0;
730 unsigned long nr_congested = 0;
Andrew Morton05ff5132006-03-22 00:08:20 -0800731 unsigned long nr_reclaimed = 0;
Mel Gorman92df3a72011-10-31 17:07:56 -0700732 unsigned long nr_writeback = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 cond_resched();
735
Tim Chen69980e32012-07-31 16:46:08 -0700736 mem_cgroup_uncharge_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 while (!list_empty(page_list)) {
738 struct address_space *mapping;
739 struct page *page;
740 int may_enter_fs;
Minchan Kim02c6de82012-10-08 16:31:55 -0700741 enum page_references references = PAGEREF_RECLAIM_CLEAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 cond_resched();
744
745 page = lru_to_page(page_list);
746 list_del(&page->lru);
747
Nick Piggin529ae9a2008-08-02 12:01:03 +0200748 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 goto keep;
750
Nick Piggin725d7042006-09-25 23:30:55 -0700751 VM_BUG_ON(PageActive(page));
Konstantin Khlebnikov6a18adb2012-05-29 15:06:59 -0700752 VM_BUG_ON(page_zone(page) != zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 sc->nr_scanned++;
Christoph Lameter80e43422006-02-11 17:55:53 -0800755
Hugh Dickins39b5f292012-10-08 16:33:18 -0700756 if (unlikely(!page_evictable(page)))
Nick Pigginb291f002008-10-18 20:26:44 -0700757 goto cull_mlocked;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700758
Johannes Weinera6dc60f82009-03-31 15:19:30 -0700759 if (!sc->may_unmap && page_mapped(page))
Christoph Lameter80e43422006-02-11 17:55:53 -0800760 goto keep_locked;
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* Double the slab pressure for mapped and swapcache pages */
763 if (page_mapped(page) || PageSwapCache(page))
764 sc->nr_scanned++;
765
Andy Whitcroftc661b072007-08-22 14:01:26 -0700766 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
767 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
768
769 if (PageWriteback(page)) {
Michal Hockoe62e3842012-07-31 16:45:55 -0700770 /*
771 * memcg doesn't have any dirty pages throttling so we
772 * could easily OOM just because too many pages are in
Hugh Dickinsc3b94f42012-07-31 16:45:59 -0700773 * writeback and there is nothing else to reclaim.
Michal Hockoe62e3842012-07-31 16:45:55 -0700774 *
Hugh Dickinsc3b94f42012-07-31 16:45:59 -0700775 * Check __GFP_IO, certainly because a loop driver
Michal Hockoe62e3842012-07-31 16:45:55 -0700776 * thread might enter reclaim, and deadlock if it waits
777 * on a page for which it is needed to do the write
778 * (loop masks off __GFP_IO|__GFP_FS for this reason);
779 * but more thought would probably show more reasons.
Hugh Dickinsc3b94f42012-07-31 16:45:59 -0700780 *
781 * Don't require __GFP_FS, since we're not going into
782 * the FS, just waiting on its writeback completion.
783 * Worryingly, ext4 gfs2 and xfs allocate pages with
784 * grab_cache_page_write_begin(,,AOP_FLAG_NOFS), so
785 * testing may_enter_fs here is liable to OOM on them.
Michal Hockoe62e3842012-07-31 16:45:55 -0700786 */
Hugh Dickinsc3b94f42012-07-31 16:45:59 -0700787 if (global_reclaim(sc) ||
788 !PageReclaim(page) || !(sc->gfp_mask & __GFP_IO)) {
789 /*
790 * This is slightly racy - end_page_writeback()
791 * might have just cleared PageReclaim, then
792 * setting PageReclaim here end up interpreted
793 * as PageReadahead - but that does not matter
794 * enough to care. What we do want is for this
795 * page to have PageReclaim set next time memcg
796 * reclaim reaches the tests above, so it will
797 * then wait_on_page_writeback() to avoid OOM;
798 * and it's also appropriate in global reclaim.
799 */
800 SetPageReclaim(page);
Michal Hockoe62e3842012-07-31 16:45:55 -0700801 nr_writeback++;
Hugh Dickinsc3b94f42012-07-31 16:45:59 -0700802 goto keep_locked;
Michal Hockoe62e3842012-07-31 16:45:55 -0700803 }
Hugh Dickinsc3b94f42012-07-31 16:45:59 -0700804 wait_on_page_writeback(page);
Andy Whitcroftc661b072007-08-22 14:01:26 -0700805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Minchan Kim02c6de82012-10-08 16:31:55 -0700807 if (!force_reclaim)
808 references = page_check_references(page, sc);
809
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800810 switch (references) {
811 case PAGEREF_ACTIVATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto activate_locked;
Johannes Weiner645747462010-03-05 13:42:22 -0800813 case PAGEREF_KEEP:
814 goto keep_locked;
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800815 case PAGEREF_RECLAIM:
816 case PAGEREF_RECLAIM_CLEAN:
817 ; /* try to reclaim the page below */
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /*
821 * Anonymous process memory has backing store?
822 * Try to allocate it some swap space here.
823 */
Nick Pigginb291f002008-10-18 20:26:44 -0700824 if (PageAnon(page) && !PageSwapCache(page)) {
Hugh Dickins63eb6b92008-11-19 15:36:37 -0800825 if (!(sc->gfp_mask & __GFP_IO))
826 goto keep_locked;
Hugh Dickinsac47b002009-01-06 14:39:39 -0800827 if (!add_to_swap(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 goto activate_locked;
Hugh Dickins63eb6b92008-11-19 15:36:37 -0800829 may_enter_fs = 1;
Nick Pigginb291f002008-10-18 20:26:44 -0700830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /*
835 * The page is mapped into the page tables of one or more
836 * processes. Try to unmap it here.
837 */
838 if (page_mapped(page) && mapping) {
Minchan Kim02c6de82012-10-08 16:31:55 -0700839 switch (try_to_unmap(page, ttu_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 case SWAP_FAIL:
841 goto activate_locked;
842 case SWAP_AGAIN:
843 goto keep_locked;
Nick Pigginb291f002008-10-18 20:26:44 -0700844 case SWAP_MLOCK:
845 goto cull_mlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 case SWAP_SUCCESS:
847 ; /* try to free the page below */
848 }
849 }
850
851 if (PageDirty(page)) {
Mel Gorman0e093d992010-10-26 14:21:45 -0700852 nr_dirty++;
853
Mel Gormanee728862011-10-31 17:07:38 -0700854 /*
855 * Only kswapd can writeback filesystem pages to
Mel Gormanf84f6e22011-10-31 17:07:51 -0700856 * avoid risk of stack overflow but do not writeback
857 * unless under significant pressure.
Mel Gormanee728862011-10-31 17:07:38 -0700858 */
Mel Gormanf84f6e22011-10-31 17:07:51 -0700859 if (page_is_file_cache(page) &&
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -0700860 (!current_is_kswapd() ||
861 sc->priority >= DEF_PRIORITY - 2)) {
Mel Gorman49ea7eb2011-10-31 17:07:59 -0700862 /*
863 * Immediately reclaim when written back.
864 * Similar in principal to deactivate_page()
865 * except we already have the page isolated
866 * and know it's dirty
867 */
868 inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE);
869 SetPageReclaim(page);
870
Mel Gormanee728862011-10-31 17:07:38 -0700871 goto keep_locked;
872 }
873
Johannes Weinerdfc8d632010-03-05 13:42:19 -0800874 if (references == PAGEREF_RECLAIM_CLEAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 goto keep_locked;
Andrew Morton4dd4b922008-03-24 12:29:52 -0700876 if (!may_enter_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 goto keep_locked;
Christoph Lameter52a83632006-02-01 03:05:28 -0800878 if (!sc->may_writepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 goto keep_locked;
880
881 /* Page is dirty, try to write it out here */
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700882 switch (pageout(page, mapping, sc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 case PAGE_KEEP:
Mel Gorman0e093d992010-10-26 14:21:45 -0700884 nr_congested++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 goto keep_locked;
886 case PAGE_ACTIVATE:
887 goto activate_locked;
888 case PAGE_SUCCESS:
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700889 if (PageWriteback(page))
Mel Gorman41ac1992012-05-29 15:06:19 -0700890 goto keep;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700891 if (PageDirty(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 goto keep;
KOSAKI Motohiro7d3579e2010-10-26 14:21:42 -0700893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
895 * A synchronous write - probably a ramdisk. Go
896 * ahead and try to reclaim the page.
897 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200898 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 goto keep;
900 if (PageDirty(page) || PageWriteback(page))
901 goto keep_locked;
902 mapping = page_mapping(page);
903 case PAGE_CLEAN:
904 ; /* try to free the page below */
905 }
906 }
907
908 /*
909 * If the page has buffers, try to free the buffer mappings
910 * associated with this page. If we succeed we try to free
911 * the page as well.
912 *
913 * We do this even if the page is PageDirty().
914 * try_to_release_page() does not perform I/O, but it is
915 * possible for a page to have PageDirty set, but it is actually
916 * clean (all its buffers are clean). This happens if the
917 * buffers were written out directly, with submit_bh(). ext3
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700918 * will do this, as well as the blockdev mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * try_to_release_page() will discover that cleanness and will
920 * drop the buffers and mark the page clean - it can be freed.
921 *
922 * Rarely, pages can have buffers and no ->mapping. These are
923 * the pages which were not successfully invalidated in
924 * truncate_complete_page(). We try to drop those buffers here
925 * and if that worked, and the page is no longer mapped into
926 * process address space (page_count == 1) it can be freed.
927 * Otherwise, leave the page on the LRU so it is swappable.
928 */
David Howells266cf652009-04-03 16:42:36 +0100929 if (page_has_private(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (!try_to_release_page(page, sc->gfp_mask))
931 goto activate_locked;
Nick Piggine2867812008-07-25 19:45:30 -0700932 if (!mapping && page_count(page) == 1) {
933 unlock_page(page);
934 if (put_page_testzero(page))
935 goto free_it;
936 else {
937 /*
938 * rare race with speculative reference.
939 * the speculative reference will free
940 * this page shortly, so we may
941 * increment nr_reclaimed here (and
942 * leave it off the LRU).
943 */
944 nr_reclaimed++;
945 continue;
946 }
947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
Nick Piggine2867812008-07-25 19:45:30 -0700950 if (!mapping || !__remove_mapping(mapping, page))
Christoph Lameter49d2e9c2006-01-08 01:00:48 -0800951 goto keep_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Nick Piggina978d6f2008-10-18 20:26:58 -0700953 /*
954 * At this point, we have no other references and there is
955 * no way to pick any more up (removed from LRU, removed
956 * from pagecache). Can use non-atomic bitops now (and
957 * we obviously don't have to worry about waking up a process
958 * waiting on the page lock, because there are no references.
959 */
960 __clear_page_locked(page);
Nick Piggine2867812008-07-25 19:45:30 -0700961free_it:
Andrew Morton05ff5132006-03-22 00:08:20 -0800962 nr_reclaimed++;
Mel Gormanabe4c3b2010-08-09 17:19:31 -0700963
964 /*
965 * Is there need to periodically free_page_list? It would
966 * appear not as the counts should be low
967 */
968 list_add(&page->lru, &free_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 continue;
970
Nick Pigginb291f002008-10-18 20:26:44 -0700971cull_mlocked:
Hugh Dickins63d6c5a2009-01-06 14:39:38 -0800972 if (PageSwapCache(page))
973 try_to_free_swap(page);
Nick Pigginb291f002008-10-18 20:26:44 -0700974 unlock_page(page);
975 putback_lru_page(page);
976 continue;
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978activate_locked:
Rik van Riel68a223942008-10-18 20:26:23 -0700979 /* Not a candidate for swapping, so reclaim swap space. */
980 if (PageSwapCache(page) && vm_swap_full())
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800981 try_to_free_swap(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700982 VM_BUG_ON(PageActive(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 SetPageActive(page);
984 pgactivate++;
985keep_locked:
986 unlock_page(page);
987keep:
988 list_add(&page->lru, &ret_pages);
Nick Pigginb291f002008-10-18 20:26:44 -0700989 VM_BUG_ON(PageLRU(page) || PageUnevictable(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
Mel Gormanabe4c3b2010-08-09 17:19:31 -0700991
Mel Gorman0e093d992010-10-26 14:21:45 -0700992 /*
993 * Tag a zone as congested if all the dirty pages encountered were
994 * backed by a congested BDI. In this case, reclaimers should just
995 * back off and wait for congestion to clear because further reclaim
996 * will encounter the same problem
997 */
Johannes Weiner89b5fae2012-01-12 17:17:50 -0800998 if (nr_dirty && nr_dirty == nr_congested && global_reclaim(sc))
Konstantin Khlebnikov6a18adb2012-05-29 15:06:59 -0700999 zone_set_flag(zone, ZONE_CONGESTED);
Mel Gorman0e093d992010-10-26 14:21:45 -07001000
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -08001001 free_hot_cold_page_list(&free_pages, 1);
Mel Gormanabe4c3b2010-08-09 17:19:31 -07001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 list_splice(&ret_pages, page_list);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001004 count_vm_events(PGACTIVATE, pgactivate);
Tim Chen69980e32012-07-31 16:46:08 -07001005 mem_cgroup_uncharge_end();
Mel Gorman92df3a72011-10-31 17:07:56 -07001006 *ret_nr_dirty += nr_dirty;
1007 *ret_nr_writeback += nr_writeback;
Andrew Morton05ff5132006-03-22 00:08:20 -08001008 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Minchan Kim02c6de82012-10-08 16:31:55 -07001011unsigned long reclaim_clean_pages_from_list(struct zone *zone,
1012 struct list_head *page_list)
1013{
1014 struct scan_control sc = {
1015 .gfp_mask = GFP_KERNEL,
1016 .priority = DEF_PRIORITY,
1017 .may_unmap = 1,
1018 };
1019 unsigned long ret, dummy1, dummy2;
1020 struct page *page, *next;
1021 LIST_HEAD(clean_pages);
1022
1023 list_for_each_entry_safe(page, next, page_list, lru) {
1024 if (page_is_file_cache(page) && !PageDirty(page)) {
1025 ClearPageActive(page);
1026 list_move(&page->lru, &clean_pages);
1027 }
1028 }
1029
1030 ret = shrink_page_list(&clean_pages, zone, &sc,
1031 TTU_UNMAP|TTU_IGNORE_ACCESS,
1032 &dummy1, &dummy2, true);
1033 list_splice(&clean_pages, page_list);
1034 __mod_zone_page_state(zone, NR_ISOLATED_FILE, -ret);
1035 return ret;
1036}
1037
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001038/*
1039 * Attempt to remove the specified page from its LRU. Only take this page
1040 * if it is of the appropriate PageActive status. Pages which are being
1041 * freed elsewhere are also ignored.
1042 *
1043 * page: page to consider
1044 * mode: one of the LRU isolation modes defined above
1045 *
1046 * returns 0 on success, -ve errno on failure.
1047 */
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -07001048int __isolate_lru_page(struct page *page, isolate_mode_t mode)
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001049{
1050 int ret = -EINVAL;
1051
1052 /* Only take pages on the LRU. */
1053 if (!PageLRU(page))
1054 return ret;
1055
Minchan Kime46a2872012-10-08 16:33:48 -07001056 /* Compaction should not handle unevictable pages but CMA can do so */
1057 if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE))
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001058 return ret;
1059
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001060 ret = -EBUSY;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001061
Mel Gormanc8244932012-01-12 17:19:38 -08001062 /*
1063 * To minimise LRU disruption, the caller can indicate that it only
1064 * wants to isolate pages it will be able to operate on without
1065 * blocking - clean pages for the most part.
1066 *
1067 * ISOLATE_CLEAN means that only clean pages should be isolated. This
1068 * is used by reclaim when it is cannot write to backing storage
1069 *
1070 * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages
1071 * that it is possible to migrate without blocking
1072 */
1073 if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) {
1074 /* All the caller can do on PageWriteback is block */
1075 if (PageWriteback(page))
1076 return ret;
1077
1078 if (PageDirty(page)) {
1079 struct address_space *mapping;
1080
1081 /* ISOLATE_CLEAN means only clean pages */
1082 if (mode & ISOLATE_CLEAN)
1083 return ret;
1084
1085 /*
1086 * Only pages without mappings or that have a
1087 * ->migratepage callback are possible to migrate
1088 * without blocking
1089 */
1090 mapping = page_mapping(page);
1091 if (mapping && !mapping->a_ops->migratepage)
1092 return ret;
1093 }
1094 }
Minchan Kim39deaf82011-10-31 17:06:51 -07001095
Minchan Kimf80c0672011-10-31 17:06:55 -07001096 if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
1097 return ret;
1098
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001099 if (likely(get_page_unless_zero(page))) {
1100 /*
1101 * Be careful not to clear PageLRU until after we're
1102 * sure the page is not being freed elsewhere -- the
1103 * page release code relies on it.
1104 */
1105 ClearPageLRU(page);
1106 ret = 0;
1107 }
1108
1109 return ret;
1110}
1111
Christoph Lameter49d2e9c2006-01-08 01:00:48 -08001112/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 * zone->lru_lock is heavily contended. Some of the functions that
1114 * shrink the lists perform better by taking out a batch of pages
1115 * and working on them outside the LRU lock.
1116 *
1117 * For pagecache intensive workloads, this function is the hottest
1118 * spot in the kernel (apart from copy_*_user functions).
1119 *
1120 * Appropriate locks must be held before calling this function.
1121 *
1122 * @nr_to_scan: The number of pages to look through on the list.
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001123 * @lruvec: The LRU vector to pull pages from.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 * @dst: The temp list to put pages on to.
Hugh Dickinsf6260122012-01-12 17:20:06 -08001125 * @nr_scanned: The number of pages that were scanned.
Rik van Rielfe2c2a12012-03-21 16:33:51 -07001126 * @sc: The scan_control struct for this reclaim session
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001127 * @mode: One of the LRU isolation modes
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001128 * @lru: LRU list id for isolating
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 *
1130 * returns how many pages were moved onto *@dst.
1131 */
Andrew Morton69e05942006-03-22 00:08:19 -08001132static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001133 struct lruvec *lruvec, struct list_head *dst,
Rik van Rielfe2c2a12012-03-21 16:33:51 -07001134 unsigned long *nr_scanned, struct scan_control *sc,
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001135 isolate_mode_t mode, enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Hugh Dickins75b00af2012-05-29 15:07:09 -07001137 struct list_head *src = &lruvec->lists[lru];
Andrew Morton69e05942006-03-22 00:08:19 -08001138 unsigned long nr_taken = 0;
Wu Fengguangc9b02d92006-03-22 00:08:23 -08001139 unsigned long scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Wu Fengguangc9b02d92006-03-22 00:08:23 -08001141 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001142 struct page *page;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001143 int nr_pages;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 page = lru_to_page(src);
1146 prefetchw_prev_lru_page(page, src, flags);
1147
Nick Piggin725d7042006-09-25 23:30:55 -07001148 VM_BUG_ON(!PageLRU(page));
Nick Piggin8d438f92006-03-22 00:07:59 -08001149
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -07001150 switch (__isolate_lru_page(page, mode)) {
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001151 case 0:
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001152 nr_pages = hpage_nr_pages(page);
1153 mem_cgroup_update_lru_size(lruvec, lru, -nr_pages);
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001154 list_move(&page->lru, dst);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001155 nr_taken += nr_pages;
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001156 break;
Nick Piggin46453a62006-03-22 00:07:58 -08001157
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001158 case -EBUSY:
1159 /* else it is being freed elsewhere */
1160 list_move(&page->lru, src);
1161 continue;
1162
1163 default:
1164 BUG();
1165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167
Hugh Dickinsf6260122012-01-12 17:20:06 -08001168 *nr_scanned = scan;
Hugh Dickins75b00af2012-05-29 15:07:09 -07001169 trace_mm_vmscan_lru_isolate(sc->order, nr_to_scan, scan,
1170 nr_taken, mode, is_file_lru(lru));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return nr_taken;
1172}
1173
Nick Piggin62695a82008-10-18 20:26:09 -07001174/**
1175 * isolate_lru_page - tries to isolate a page from its LRU list
1176 * @page: page to isolate from its LRU list
1177 *
1178 * Isolates a @page from an LRU list, clears PageLRU and adjusts the
1179 * vmstat statistic corresponding to whatever LRU list the page was on.
1180 *
1181 * Returns 0 if the page was removed from an LRU list.
1182 * Returns -EBUSY if the page was not on an LRU list.
1183 *
1184 * The returned page will have PageLRU() cleared. If it was found on
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001185 * the active list, it will have PageActive set. If it was found on
1186 * the unevictable list, it will have the PageUnevictable bit set. That flag
1187 * may need to be cleared by the caller before letting the page go.
Nick Piggin62695a82008-10-18 20:26:09 -07001188 *
1189 * The vmstat statistic corresponding to the list on which the page was
1190 * found will be decremented.
1191 *
1192 * Restrictions:
1193 * (1) Must be called with an elevated refcount on the page. This is a
1194 * fundamentnal difference from isolate_lru_pages (which is called
1195 * without a stable reference).
1196 * (2) the lru_lock must not be held.
1197 * (3) interrupts must be enabled.
1198 */
1199int isolate_lru_page(struct page *page)
1200{
1201 int ret = -EBUSY;
1202
Konstantin Khlebnikov0c917312011-05-24 17:12:21 -07001203 VM_BUG_ON(!page_count(page));
1204
Nick Piggin62695a82008-10-18 20:26:09 -07001205 if (PageLRU(page)) {
1206 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001207 struct lruvec *lruvec;
Nick Piggin62695a82008-10-18 20:26:09 -07001208
1209 spin_lock_irq(&zone->lru_lock);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001210 lruvec = mem_cgroup_page_lruvec(page, zone);
Konstantin Khlebnikov0c917312011-05-24 17:12:21 -07001211 if (PageLRU(page)) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001212 int lru = page_lru(page);
Konstantin Khlebnikov0c917312011-05-24 17:12:21 -07001213 get_page(page);
Nick Piggin62695a82008-10-18 20:26:09 -07001214 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001215 del_page_from_lru_list(page, lruvec, lru);
1216 ret = 0;
Nick Piggin62695a82008-10-18 20:26:09 -07001217 }
1218 spin_unlock_irq(&zone->lru_lock);
1219 }
1220 return ret;
1221}
1222
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07001223/*
Fengguang Wud37dd5d2012-12-18 14:23:28 -08001224 * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
1225 * then get resheduled. When there are massive number of tasks doing page
1226 * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
1227 * the LRU list will go small and be scanned faster than necessary, leading to
1228 * unnecessary swapping, thrashing and OOM.
Rik van Riel35cd7812009-09-21 17:01:38 -07001229 */
1230static int too_many_isolated(struct zone *zone, int file,
1231 struct scan_control *sc)
1232{
1233 unsigned long inactive, isolated;
1234
1235 if (current_is_kswapd())
1236 return 0;
1237
Johannes Weiner89b5fae2012-01-12 17:17:50 -08001238 if (!global_reclaim(sc))
Rik van Riel35cd7812009-09-21 17:01:38 -07001239 return 0;
1240
1241 if (file) {
1242 inactive = zone_page_state(zone, NR_INACTIVE_FILE);
1243 isolated = zone_page_state(zone, NR_ISOLATED_FILE);
1244 } else {
1245 inactive = zone_page_state(zone, NR_INACTIVE_ANON);
1246 isolated = zone_page_state(zone, NR_ISOLATED_ANON);
1247 }
1248
Fengguang Wu3cf23842012-12-18 14:23:31 -08001249 /*
1250 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
1251 * won't get blocked by normal direct-reclaimers, forming a circular
1252 * deadlock.
1253 */
1254 if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS)
1255 inactive >>= 3;
1256
Rik van Riel35cd7812009-09-21 17:01:38 -07001257 return isolated > inactive;
1258}
1259
Mel Gorman66635622010-08-09 17:19:30 -07001260static noinline_for_stack void
Hugh Dickins75b00af2012-05-29 15:07:09 -07001261putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list)
Mel Gorman66635622010-08-09 17:19:30 -07001262{
Konstantin Khlebnikov27ac81d2012-05-29 15:07:00 -07001263 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
1264 struct zone *zone = lruvec_zone(lruvec);
Hugh Dickins3f797682012-01-12 17:20:07 -08001265 LIST_HEAD(pages_to_free);
Mel Gorman66635622010-08-09 17:19:30 -07001266
Mel Gorman66635622010-08-09 17:19:30 -07001267 /*
1268 * Put back any unfreeable pages.
1269 */
Mel Gorman66635622010-08-09 17:19:30 -07001270 while (!list_empty(page_list)) {
Hugh Dickins3f797682012-01-12 17:20:07 -08001271 struct page *page = lru_to_page(page_list);
Mel Gorman66635622010-08-09 17:19:30 -07001272 int lru;
Hugh Dickins3f797682012-01-12 17:20:07 -08001273
Mel Gorman66635622010-08-09 17:19:30 -07001274 VM_BUG_ON(PageLRU(page));
1275 list_del(&page->lru);
Hugh Dickins39b5f292012-10-08 16:33:18 -07001276 if (unlikely(!page_evictable(page))) {
Mel Gorman66635622010-08-09 17:19:30 -07001277 spin_unlock_irq(&zone->lru_lock);
1278 putback_lru_page(page);
1279 spin_lock_irq(&zone->lru_lock);
1280 continue;
1281 }
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001282
1283 lruvec = mem_cgroup_page_lruvec(page, zone);
1284
Linus Torvalds7a608572011-01-17 14:42:19 -08001285 SetPageLRU(page);
Mel Gorman66635622010-08-09 17:19:30 -07001286 lru = page_lru(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001287 add_page_to_lru_list(page, lruvec, lru);
1288
Mel Gorman66635622010-08-09 17:19:30 -07001289 if (is_active_lru(lru)) {
1290 int file = is_file_lru(lru);
Rik van Riel9992af12011-01-13 15:47:13 -08001291 int numpages = hpage_nr_pages(page);
1292 reclaim_stat->recent_rotated[file] += numpages;
Mel Gorman66635622010-08-09 17:19:30 -07001293 }
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001294 if (put_page_testzero(page)) {
1295 __ClearPageLRU(page);
1296 __ClearPageActive(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001297 del_page_from_lru_list(page, lruvec, lru);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001298
1299 if (unlikely(PageCompound(page))) {
1300 spin_unlock_irq(&zone->lru_lock);
1301 (*get_compound_page_dtor(page))(page);
1302 spin_lock_irq(&zone->lru_lock);
1303 } else
1304 list_add(&page->lru, &pages_to_free);
Mel Gorman66635622010-08-09 17:19:30 -07001305 }
1306 }
Mel Gorman66635622010-08-09 17:19:30 -07001307
Hugh Dickins3f797682012-01-12 17:20:07 -08001308 /*
1309 * To save our caller's stack, now use input list for pages to free.
1310 */
1311 list_splice(&pages_to_free, page_list);
Mel Gorman66635622010-08-09 17:19:30 -07001312}
1313
1314/*
Andrew Morton1742f192006-03-22 00:08:21 -08001315 * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
1316 * of reclaimed pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
Mel Gorman66635622010-08-09 17:19:30 -07001318static noinline_for_stack unsigned long
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001319shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07001320 struct scan_control *sc, enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 LIST_HEAD(page_list);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001323 unsigned long nr_scanned;
Andrew Morton05ff5132006-03-22 00:08:20 -08001324 unsigned long nr_reclaimed = 0;
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001325 unsigned long nr_taken;
Mel Gorman92df3a72011-10-31 17:07:56 -07001326 unsigned long nr_dirty = 0;
1327 unsigned long nr_writeback = 0;
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -07001328 isolate_mode_t isolate_mode = 0;
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001329 int file = is_file_lru(lru);
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001330 struct zone *zone = lruvec_zone(lruvec);
1331 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
KOSAKI Motohiro78dc5832009-06-16 15:31:40 -07001332
Rik van Riel35cd7812009-09-21 17:01:38 -07001333 while (unlikely(too_many_isolated(zone, file, sc))) {
KOSAKI Motohiro58355c72009-10-26 16:49:35 -07001334 congestion_wait(BLK_RW_ASYNC, HZ/10);
Rik van Riel35cd7812009-09-21 17:01:38 -07001335
1336 /* We are about to die and free our memory. Return now. */
1337 if (fatal_signal_pending(current))
1338 return SWAP_CLUSTER_MAX;
1339 }
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 lru_add_drain();
Minchan Kimf80c0672011-10-31 17:06:55 -07001342
1343 if (!sc->may_unmap)
Hillf Danton61317282012-03-21 16:33:48 -07001344 isolate_mode |= ISOLATE_UNMAPPED;
Minchan Kimf80c0672011-10-31 17:06:55 -07001345 if (!sc->may_writepage)
Hillf Danton61317282012-03-21 16:33:48 -07001346 isolate_mode |= ISOLATE_CLEAN;
Minchan Kimf80c0672011-10-31 17:06:55 -07001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 spin_lock_irq(&zone->lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001350 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
1351 &nr_scanned, sc, isolate_mode, lru);
Konstantin Khlebnikov95d918f2012-05-29 15:06:59 -07001352
1353 __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken);
1354 __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
1355
Johannes Weiner89b5fae2012-01-12 17:17:50 -08001356 if (global_reclaim(sc)) {
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001357 zone->pages_scanned += nr_scanned;
KOSAKI Motohirob35ea172009-09-21 17:01:36 -07001358 if (current_is_kswapd())
Hugh Dickins75b00af2012-05-29 15:07:09 -07001359 __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scanned);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001360 else
Hugh Dickins75b00af2012-05-29 15:07:09 -07001361 __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scanned);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001362 }
Hillf Dantond563c052012-03-21 16:34:02 -07001363 spin_unlock_irq(&zone->lru_lock);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07001364
Hillf Dantond563c052012-03-21 16:34:02 -07001365 if (nr_taken == 0)
Mel Gorman66635622010-08-09 17:19:30 -07001366 return 0;
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001367
Minchan Kim02c6de82012-10-08 16:31:55 -07001368 nr_reclaimed = shrink_page_list(&page_list, zone, sc, TTU_UNMAP,
1369 &nr_dirty, &nr_writeback, false);
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001370
Hugh Dickins3f797682012-01-12 17:20:07 -08001371 spin_lock_irq(&zone->lru_lock);
1372
Konstantin Khlebnikov95d918f2012-05-29 15:06:59 -07001373 reclaim_stat->recent_scanned[file] += nr_taken;
Hillf Dantond563c052012-03-21 16:34:02 -07001374
Ying Han904249a2012-04-25 16:01:48 -07001375 if (global_reclaim(sc)) {
1376 if (current_is_kswapd())
1377 __count_zone_vm_events(PGSTEAL_KSWAPD, zone,
1378 nr_reclaimed);
1379 else
1380 __count_zone_vm_events(PGSTEAL_DIRECT, zone,
1381 nr_reclaimed);
1382 }
KOSAKI Motohiroe247dbc2010-08-09 17:19:28 -07001383
Konstantin Khlebnikov27ac81d2012-05-29 15:07:00 -07001384 putback_inactive_pages(lruvec, &page_list);
Hugh Dickins3f797682012-01-12 17:20:07 -08001385
Konstantin Khlebnikov95d918f2012-05-29 15:06:59 -07001386 __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
Hugh Dickins3f797682012-01-12 17:20:07 -08001387
1388 spin_unlock_irq(&zone->lru_lock);
1389
1390 free_hot_cold_page_list(&page_list, 1);
Mel Gormane11da5b2010-10-26 14:21:40 -07001391
Mel Gorman92df3a72011-10-31 17:07:56 -07001392 /*
1393 * If reclaim is isolating dirty pages under writeback, it implies
1394 * that the long-lived page allocation rate is exceeding the page
1395 * laundering rate. Either the global limits are not being effective
1396 * at throttling processes due to the page distribution throughout
1397 * zones or there is heavy usage of a slow backing device. The
1398 * only option is to throttle from reclaim context which is not ideal
1399 * as there is no guarantee the dirtying process is throttled in the
1400 * same way balance_dirty_pages() manages.
1401 *
1402 * This scales the number of dirty pages that must be under writeback
1403 * before throttling depending on priority. It is a simple backoff
1404 * function that has the most effect in the range DEF_PRIORITY to
1405 * DEF_PRIORITY-2 which is the priority reclaim is considered to be
1406 * in trouble and reclaim is considered to be in trouble.
1407 *
1408 * DEF_PRIORITY 100% isolated pages must be PageWriteback to throttle
1409 * DEF_PRIORITY-1 50% must be PageWriteback
1410 * DEF_PRIORITY-2 25% must be PageWriteback, kswapd in trouble
1411 * ...
1412 * DEF_PRIORITY-6 For SWAP_CLUSTER_MAX isolated pages, throttle if any
1413 * isolated page is PageWriteback
1414 */
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07001415 if (nr_writeback && nr_writeback >=
1416 (nr_taken >> (DEF_PRIORITY - sc->priority)))
Mel Gorman92df3a72011-10-31 17:07:56 -07001417 wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
1418
Mel Gormane11da5b2010-10-26 14:21:40 -07001419 trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id,
1420 zone_idx(zone),
1421 nr_scanned, nr_reclaimed,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07001422 sc->priority,
Mel Gorman23b9da52012-05-29 15:06:20 -07001423 trace_shrink_flags(file));
Andrew Morton05ff5132006-03-22 00:08:20 -08001424 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Martin Bligh3bb1a8522006-10-28 10:38:24 -07001427/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 * This moves pages from the active list to the inactive list.
1429 *
1430 * We move them the other way if the page is referenced by one or more
1431 * processes, from rmap.
1432 *
1433 * If the pages are mostly unmapped, the processing is fast and it is
1434 * appropriate to hold zone->lru_lock across the whole operation. But if
1435 * the pages are mapped, the processing is slow (page_referenced()) so we
1436 * should drop zone->lru_lock around each page. It's impossible to balance
1437 * this, so instead we remove the pages from the LRU while processing them.
1438 * It is safe to rely on PG_active against the non-LRU pages in here because
1439 * nobody will play with that bit on a non-LRU page.
1440 *
1441 * The downside is that we have to touch page->_count against each page.
1442 * But we had to alter page->flags anyway.
1443 */
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001444
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001445static void move_active_pages_to_lru(struct lruvec *lruvec,
Wu Fengguang3eb41402009-06-16 15:33:13 -07001446 struct list_head *list,
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001447 struct list_head *pages_to_free,
Wu Fengguang3eb41402009-06-16 15:33:13 -07001448 enum lru_list lru)
1449{
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001450 struct zone *zone = lruvec_zone(lruvec);
Wu Fengguang3eb41402009-06-16 15:33:13 -07001451 unsigned long pgmoved = 0;
Wu Fengguang3eb41402009-06-16 15:33:13 -07001452 struct page *page;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001453 int nr_pages;
Wu Fengguang3eb41402009-06-16 15:33:13 -07001454
Wu Fengguang3eb41402009-06-16 15:33:13 -07001455 while (!list_empty(list)) {
1456 page = lru_to_page(list);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001457 lruvec = mem_cgroup_page_lruvec(page, zone);
Wu Fengguang3eb41402009-06-16 15:33:13 -07001458
1459 VM_BUG_ON(PageLRU(page));
1460 SetPageLRU(page);
1461
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001462 nr_pages = hpage_nr_pages(page);
1463 mem_cgroup_update_lru_size(lruvec, lru, nr_pages);
Johannes Weiner925b7672012-01-12 17:18:15 -08001464 list_move(&page->lru, &lruvec->lists[lru]);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001465 pgmoved += nr_pages;
Wu Fengguang3eb41402009-06-16 15:33:13 -07001466
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001467 if (put_page_testzero(page)) {
1468 __ClearPageLRU(page);
1469 __ClearPageActive(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001470 del_page_from_lru_list(page, lruvec, lru);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001471
1472 if (unlikely(PageCompound(page))) {
1473 spin_unlock_irq(&zone->lru_lock);
1474 (*get_compound_page_dtor(page))(page);
1475 spin_lock_irq(&zone->lru_lock);
1476 } else
1477 list_add(&page->lru, pages_to_free);
Wu Fengguang3eb41402009-06-16 15:33:13 -07001478 }
1479 }
1480 __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
1481 if (!is_active_lru(lru))
1482 __count_vm_events(PGDEACTIVATE, pgmoved);
1483}
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001484
Hugh Dickinsf6260122012-01-12 17:20:06 -08001485static void shrink_active_list(unsigned long nr_to_scan,
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001486 struct lruvec *lruvec,
Johannes Weinerf16015f2012-01-12 17:17:52 -08001487 struct scan_control *sc,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07001488 enum lru_list lru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001490 unsigned long nr_taken;
Hugh Dickinsf6260122012-01-12 17:20:06 -08001491 unsigned long nr_scanned;
Wu Fengguang6fe6b7e2009-06-16 15:33:05 -07001492 unsigned long vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 LIST_HEAD(l_hold); /* The pages which were snipped off */
Wu Fengguang8cab4752009-06-16 15:33:12 -07001494 LIST_HEAD(l_active);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001495 LIST_HEAD(l_inactive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 struct page *page;
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001497 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
KOSAKI Motohiro44c241f2009-09-21 17:01:35 -07001498 unsigned long nr_rotated = 0;
Konstantin Khlebnikovf3fd4a62012-05-29 15:06:54 -07001499 isolate_mode_t isolate_mode = 0;
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001500 int file = is_file_lru(lru);
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001501 struct zone *zone = lruvec_zone(lruvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 lru_add_drain();
Minchan Kimf80c0672011-10-31 17:06:55 -07001504
1505 if (!sc->may_unmap)
Hillf Danton61317282012-03-21 16:33:48 -07001506 isolate_mode |= ISOLATE_UNMAPPED;
Minchan Kimf80c0672011-10-31 17:06:55 -07001507 if (!sc->may_writepage)
Hillf Danton61317282012-03-21 16:33:48 -07001508 isolate_mode |= ISOLATE_CLEAN;
Minchan Kimf80c0672011-10-31 17:06:55 -07001509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 spin_lock_irq(&zone->lru_lock);
Johannes Weiner925b7672012-01-12 17:18:15 -08001511
Konstantin Khlebnikov5dc35972012-05-29 15:06:58 -07001512 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
1513 &nr_scanned, sc, isolate_mode, lru);
Johannes Weiner89b5fae2012-01-12 17:17:50 -08001514 if (global_reclaim(sc))
Hugh Dickinsf6260122012-01-12 17:20:06 -08001515 zone->pages_scanned += nr_scanned;
Johannes Weiner89b5fae2012-01-12 17:17:50 -08001516
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001517 reclaim_stat->recent_scanned[file] += nr_taken;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08001518
Hugh Dickinsf6260122012-01-12 17:20:06 -08001519 __count_zone_vm_events(PGREFILL, zone, nr_scanned);
Konstantin Khlebnikov3cb99452012-05-29 15:06:53 -07001520 __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07001521 __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 spin_unlock_irq(&zone->lru_lock);
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 while (!list_empty(&l_hold)) {
1525 cond_resched();
1526 page = lru_to_page(&l_hold);
1527 list_del(&page->lru);
Rik van Riel7e9cd482008-10-18 20:26:35 -07001528
Hugh Dickins39b5f292012-10-08 16:33:18 -07001529 if (unlikely(!page_evictable(page))) {
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001530 putback_lru_page(page);
1531 continue;
1532 }
1533
Mel Gormancc715d92012-03-21 16:34:00 -07001534 if (unlikely(buffer_heads_over_limit)) {
1535 if (page_has_private(page) && trylock_page(page)) {
1536 if (page_has_private(page))
1537 try_to_release_page(page, 0);
1538 unlock_page(page);
1539 }
1540 }
1541
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001542 if (page_referenced(page, 0, sc->target_mem_cgroup,
1543 &vm_flags)) {
Rik van Riel9992af12011-01-13 15:47:13 -08001544 nr_rotated += hpage_nr_pages(page);
Wu Fengguang8cab4752009-06-16 15:33:12 -07001545 /*
1546 * Identify referenced, file-backed active pages and
1547 * give them one more trip around the active list. So
1548 * that executable code get better chances to stay in
1549 * memory under moderate memory pressure. Anon pages
1550 * are not likely to be evicted by use-once streaming
1551 * IO, plus JVM can create lots of anon VM_EXEC pages,
1552 * so we ignore them here.
1553 */
Wu Fengguang41e20982009-10-26 16:49:53 -07001554 if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
Wu Fengguang8cab4752009-06-16 15:33:12 -07001555 list_add(&page->lru, &l_active);
1556 continue;
1557 }
1558 }
Rik van Riel7e9cd482008-10-18 20:26:35 -07001559
KOSAKI Motohiro5205e562009-09-21 17:01:44 -07001560 ClearPageActive(page); /* we are de-activating */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 list_add(&page->lru, &l_inactive);
1562 }
1563
Andrew Mortonb5557492009-01-06 14:40:13 -08001564 /*
Wu Fengguang8cab4752009-06-16 15:33:12 -07001565 * Move pages back to the lru list.
Andrew Mortonb5557492009-01-06 14:40:13 -08001566 */
Johannes Weiner2a1dc502008-12-01 03:00:35 +01001567 spin_lock_irq(&zone->lru_lock);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001568 /*
Wu Fengguang8cab4752009-06-16 15:33:12 -07001569 * Count referenced pages from currently used mappings as rotated,
1570 * even though only some of them are actually re-activated. This
1571 * helps balance scan pressure between file and anonymous pages in
1572 * get_scan_ratio.
Rik van Riel7e9cd482008-10-18 20:26:35 -07001573 */
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001574 reclaim_stat->recent_rotated[file] += nr_rotated;
Rik van Riel556adec2008-10-18 20:26:34 -07001575
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001576 move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru);
1577 move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -07001578 __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001579 spin_unlock_irq(&zone->lru_lock);
Hugh Dickins2bcf8872012-01-12 17:19:56 -08001580
1581 free_hot_cold_page_list(&l_hold, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
Minchan Kim74e3f3c2010-10-26 14:21:31 -07001584#ifdef CONFIG_SWAP
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001585static int inactive_anon_is_low_global(struct zone *zone)
KOSAKI Motohirof89eb902009-01-07 18:08:14 -08001586{
1587 unsigned long active, inactive;
1588
1589 active = zone_page_state(zone, NR_ACTIVE_ANON);
1590 inactive = zone_page_state(zone, NR_INACTIVE_ANON);
1591
1592 if (inactive * zone->inactive_ratio < active)
1593 return 1;
1594
1595 return 0;
1596}
1597
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001598/**
1599 * inactive_anon_is_low - check if anonymous pages need to be deactivated
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001600 * @lruvec: LRU vector to check
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001601 *
1602 * Returns true if the zone does not have enough inactive anon pages,
1603 * meaning some active anon pages need to be deactivated.
1604 */
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001605static int inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001606{
Minchan Kim74e3f3c2010-10-26 14:21:31 -07001607 /*
1608 * If we don't have swap space, anonymous page deactivation
1609 * is pointless.
1610 */
1611 if (!total_swap_pages)
1612 return 0;
1613
Hugh Dickinsc3c787e2012-05-29 15:06:52 -07001614 if (!mem_cgroup_disabled())
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001615 return mem_cgroup_inactive_anon_is_low(lruvec);
Johannes Weinerf16015f2012-01-12 17:17:52 -08001616
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001617 return inactive_anon_is_low_global(lruvec_zone(lruvec));
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001618}
Minchan Kim74e3f3c2010-10-26 14:21:31 -07001619#else
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001620static inline int inactive_anon_is_low(struct lruvec *lruvec)
Minchan Kim74e3f3c2010-10-26 14:21:31 -07001621{
1622 return 0;
1623}
1624#endif
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001625
Rik van Riel56e49d22009-06-16 15:32:28 -07001626/**
1627 * inactive_file_is_low - check if file pages need to be deactivated
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001628 * @lruvec: LRU vector to check
Rik van Riel56e49d22009-06-16 15:32:28 -07001629 *
1630 * When the system is doing streaming IO, memory pressure here
1631 * ensures that active file pages get deactivated, until more
1632 * than half of the file pages are on the inactive list.
1633 *
1634 * Once we get to that situation, protect the system's working
1635 * set from being evicted by disabling active file page aging.
1636 *
1637 * This uses a different ratio than the anonymous pages, because
1638 * the page cache uses a use-once replacement algorithm.
1639 */
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001640static int inactive_file_is_low(struct lruvec *lruvec)
Rik van Riel56e49d22009-06-16 15:32:28 -07001641{
Johannes Weinere3790142013-02-22 16:35:19 -08001642 unsigned long inactive;
1643 unsigned long active;
Rik van Riel56e49d22009-06-16 15:32:28 -07001644
Johannes Weinere3790142013-02-22 16:35:19 -08001645 inactive = get_lru_size(lruvec, LRU_INACTIVE_FILE);
1646 active = get_lru_size(lruvec, LRU_ACTIVE_FILE);
1647
1648 return active > inactive;
Rik van Riel56e49d22009-06-16 15:32:28 -07001649}
1650
Hugh Dickins75b00af2012-05-29 15:07:09 -07001651static int inactive_list_is_low(struct lruvec *lruvec, enum lru_list lru)
Rik van Rielb39415b2009-12-14 17:59:48 -08001652{
Hugh Dickins75b00af2012-05-29 15:07:09 -07001653 if (is_file_lru(lru))
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001654 return inactive_file_is_low(lruvec);
Rik van Rielb39415b2009-12-14 17:59:48 -08001655 else
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001656 return inactive_anon_is_low(lruvec);
Rik van Rielb39415b2009-12-14 17:59:48 -08001657}
1658
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001659static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001660 struct lruvec *lruvec, struct scan_control *sc)
Christoph Lameterb69408e2008-10-18 20:26:14 -07001661{
Rik van Rielb39415b2009-12-14 17:59:48 -08001662 if (is_active_lru(lru)) {
Hugh Dickins75b00af2012-05-29 15:07:09 -07001663 if (inactive_list_is_low(lruvec, lru))
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001664 shrink_active_list(nr_to_scan, lruvec, sc, lru);
Rik van Riel556adec2008-10-18 20:26:34 -07001665 return 0;
1666 }
1667
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07001668 return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001669}
1670
Konstantin Khlebnikov3d58ab52012-05-29 15:06:57 -07001671static int vmscan_swappiness(struct scan_control *sc)
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001672{
Johannes Weiner89b5fae2012-01-12 17:17:50 -08001673 if (global_reclaim(sc))
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001674 return vm_swappiness;
Konstantin Khlebnikov3d58ab52012-05-29 15:06:57 -07001675 return mem_cgroup_swappiness(sc->target_mem_cgroup);
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001676}
1677
Johannes Weiner9a265112013-02-22 16:32:17 -08001678enum scan_balance {
1679 SCAN_EQUAL,
1680 SCAN_FRACT,
1681 SCAN_ANON,
1682 SCAN_FILE,
1683};
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685/*
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 *
Wanpeng Libe7bd592012-06-14 20:41:02 +08001691 * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
1692 * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001693 */
Konstantin Khlebnikov90126372012-05-29 15:07:01 -07001694static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07001695 unsigned long *nr)
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001696{
Konstantin Khlebnikov90126372012-05-29 15:07:01 -07001697 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
Johannes Weiner9a265112013-02-22 16:32:17 -08001698 u64 fraction[2];
1699 u64 denominator = 0; /* gcc */
Konstantin Khlebnikov90126372012-05-29 15:07:01 -07001700 struct zone *zone = lruvec_zone(lruvec);
Johannes Weiner9a265112013-02-22 16:32:17 -08001701 unsigned long anon_prio, file_prio;
1702 enum scan_balance scan_balance;
1703 unsigned long anon, file, free;
1704 bool force_scan = false;
1705 unsigned long ap, fp;
1706 enum lru_list lru;
KAMEZAWA Hiroyuki246e87a2011-05-26 16:25:34 -07001707
Johannes Weinerf11c0ca2011-10-31 17:07:27 -07001708 /*
1709 * If the zone or memcg is small, nr[l] can be 0. This
1710 * results in no scanning on this priority and a potential
1711 * priority drop. Global direct reclaim can go to the next
1712 * zone and tends to have no problems. Global kswapd is for
1713 * zone balancing and it needs to scan a minimum amount. When
1714 * reclaiming for a memcg, a priority drop can cause high
1715 * latencies, so it's better to scan a minimum amount there as
1716 * well.
1717 */
Konstantin Khlebnikov90126372012-05-29 15:07:01 -07001718 if (current_is_kswapd() && zone->all_unreclaimable)
Johannes Weinera4d3e9e2011-09-14 16:21:52 -07001719 force_scan = true;
Johannes Weiner89b5fae2012-01-12 17:17:50 -08001720 if (!global_reclaim(sc))
Johannes Weinera4d3e9e2011-09-14 16:21:52 -07001721 force_scan = true;
Shaohua Li76a33fc2010-05-24 14:32:36 -07001722
1723 /* If we have no swap space, do not bother scanning anon pages. */
Shaohua Liec8acf22013-02-22 16:34:38 -08001724 if (!sc->may_swap || (get_nr_swap_pages() <= 0)) {
Johannes Weiner9a265112013-02-22 16:32:17 -08001725 scan_balance = SCAN_FILE;
Shaohua Li76a33fc2010-05-24 14:32:36 -07001726 goto out;
1727 }
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001728
Johannes Weiner10316b32013-02-22 16:32:14 -08001729 /*
1730 * Global reclaim will swap to prevent OOM even with no
1731 * swappiness, but memcg users want to use this knob to
1732 * disable swapping for individual groups completely when
1733 * using the memory controller's swap limit feature would be
1734 * too expensive.
1735 */
1736 if (!global_reclaim(sc) && !vmscan_swappiness(sc)) {
Johannes Weiner9a265112013-02-22 16:32:17 -08001737 scan_balance = SCAN_FILE;
Johannes Weiner10316b32013-02-22 16:32:14 -08001738 goto out;
1739 }
1740
1741 /*
1742 * Do not apply any pressure balancing cleverness when the
1743 * system is close to OOM, scan both anon and file equally
1744 * (unless the swappiness setting disagrees with swapping).
1745 */
1746 if (!sc->priority && vmscan_swappiness(sc)) {
Johannes Weiner9a265112013-02-22 16:32:17 -08001747 scan_balance = SCAN_EQUAL;
Johannes Weiner10316b32013-02-22 16:32:14 -08001748 goto out;
1749 }
1750
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001751 anon = get_lru_size(lruvec, LRU_ACTIVE_ANON) +
1752 get_lru_size(lruvec, LRU_INACTIVE_ANON);
1753 file = get_lru_size(lruvec, LRU_ACTIVE_FILE) +
1754 get_lru_size(lruvec, LRU_INACTIVE_FILE);
Johannes Weinera4d3e9e2011-09-14 16:21:52 -07001755
Johannes Weiner11d16c22013-02-22 16:32:15 -08001756 /*
1757 * If it's foreseeable that reclaiming the file cache won't be
1758 * enough to get the zone back into a desirable shape, we have
1759 * to swap. Better start now and leave the - probably heavily
1760 * thrashing - remaining file pages alone.
1761 */
Johannes Weiner89b5fae2012-01-12 17:17:50 -08001762 if (global_reclaim(sc)) {
Johannes Weiner11d16c22013-02-22 16:32:15 -08001763 free = zone_page_state(zone, NR_FREE_PAGES);
Konstantin Khlebnikov90126372012-05-29 15:07:01 -07001764 if (unlikely(file + free <= high_wmark_pages(zone))) {
Johannes Weiner9a265112013-02-22 16:32:17 -08001765 scan_balance = SCAN_ANON;
Shaohua Li76a33fc2010-05-24 14:32:36 -07001766 goto out;
KOSAKI Motohiroeeee9a82009-01-07 18:08:17 -08001767 }
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001768 }
1769
1770 /*
Johannes Weiner7c5bd702013-02-22 16:32:10 -08001771 * There is enough inactive page cache, do not reclaim
1772 * anything from the anonymous working set right now.
1773 */
1774 if (!inactive_file_is_low(lruvec)) {
Johannes Weiner9a265112013-02-22 16:32:17 -08001775 scan_balance = SCAN_FILE;
Johannes Weiner7c5bd702013-02-22 16:32:10 -08001776 goto out;
1777 }
1778
Johannes Weiner9a265112013-02-22 16:32:17 -08001779 scan_balance = SCAN_FRACT;
1780
Johannes Weiner7c5bd702013-02-22 16:32:10 -08001781 /*
KOSAKI Motohiro58c37f62010-08-09 17:19:51 -07001782 * With swappiness at 100, anonymous and file have the same priority.
1783 * This scanning priority is essentially the inverse of IO cost.
1784 */
Konstantin Khlebnikov3d58ab52012-05-29 15:06:57 -07001785 anon_prio = vmscan_swappiness(sc);
Hugh Dickins75b00af2012-05-29 15:07:09 -07001786 file_prio = 200 - anon_prio;
KOSAKI Motohiro58c37f62010-08-09 17:19:51 -07001787
1788 /*
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001789 * OK, so we have swap space and a fair amount of page cache
1790 * pages. We use the recently rotated / recently scanned
1791 * ratios to determine how valuable each cache is.
1792 *
1793 * Because workloads change over time (and to avoid overflow)
1794 * we keep these statistics as a floating average, which ends
1795 * up weighing recent references more than old ones.
1796 *
1797 * anon in [0], file in [1]
1798 */
Konstantin Khlebnikov90126372012-05-29 15:07:01 -07001799 spin_lock_irq(&zone->lru_lock);
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001800 if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001801 reclaim_stat->recent_scanned[0] /= 2;
1802 reclaim_stat->recent_rotated[0] /= 2;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001803 }
1804
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001805 if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001806 reclaim_stat->recent_scanned[1] /= 2;
1807 reclaim_stat->recent_rotated[1] /= 2;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001808 }
1809
1810 /*
Rik van Riel00d80892008-11-19 15:36:44 -08001811 * The amount of pressure on anon vs file pages is inversely
1812 * proportional to the fraction of recently scanned pages on
1813 * each list that were recently referenced and in active use.
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001814 */
Satoru Moriyafe350042012-05-29 15:06:47 -07001815 ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1);
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001816 ap /= reclaim_stat->recent_rotated[0] + 1;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001817
Satoru Moriyafe350042012-05-29 15:06:47 -07001818 fp = file_prio * (reclaim_stat->recent_scanned[1] + 1);
KOSAKI Motohiro6e901572009-01-07 18:08:15 -08001819 fp /= reclaim_stat->recent_rotated[1] + 1;
Konstantin Khlebnikov90126372012-05-29 15:07:01 -07001820 spin_unlock_irq(&zone->lru_lock);
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001821
Shaohua Li76a33fc2010-05-24 14:32:36 -07001822 fraction[0] = ap;
1823 fraction[1] = fp;
1824 denominator = ap + fp + 1;
1825out:
Hugh Dickins41113042012-01-12 17:20:01 -08001826 for_each_evictable_lru(lru) {
1827 int file = is_file_lru(lru);
Johannes Weinerd778df52013-02-22 16:32:12 -08001828 unsigned long size;
Shaohua Li76a33fc2010-05-24 14:32:36 -07001829 unsigned long scan;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001830
Johannes Weinerd778df52013-02-22 16:32:12 -08001831 size = get_lru_size(lruvec, lru);
Johannes Weiner10316b32013-02-22 16:32:14 -08001832 scan = size >> sc->priority;
Johannes Weiner9a265112013-02-22 16:32:17 -08001833
Johannes Weiner10316b32013-02-22 16:32:14 -08001834 if (!scan && force_scan)
1835 scan = min(size, SWAP_CLUSTER_MAX);
Johannes Weiner9a265112013-02-22 16:32:17 -08001836
1837 switch (scan_balance) {
1838 case SCAN_EQUAL:
1839 /* Scan lists relative to size */
1840 break;
1841 case SCAN_FRACT:
1842 /*
1843 * Scan types proportional to swappiness and
1844 * their relative recent reclaim efficiency.
1845 */
1846 scan = div64_u64(scan * fraction[file], denominator);
1847 break;
1848 case SCAN_FILE:
1849 case SCAN_ANON:
1850 /* Scan one type exclusively */
1851 if ((scan_balance == SCAN_FILE) != file)
1852 scan = 0;
1853 break;
1854 default:
1855 /* Look ma, no brain */
1856 BUG();
1857 }
Hugh Dickins41113042012-01-12 17:20:01 -08001858 nr[lru] = scan;
Shaohua Li76a33fc2010-05-24 14:32:36 -07001859 }
Wu Fengguang6e08a362009-06-16 15:32:29 -07001860}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001861
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08001862/*
1863 * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
1864 */
1865static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
1866{
1867 unsigned long nr[NR_LRU_LISTS];
1868 unsigned long nr_to_scan;
1869 enum lru_list lru;
1870 unsigned long nr_reclaimed = 0;
1871 unsigned long nr_to_reclaim = sc->nr_to_reclaim;
1872 struct blk_plug plug;
1873
1874 get_scan_count(lruvec, sc, nr);
1875
1876 blk_start_plug(&plug);
1877 while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
1878 nr[LRU_INACTIVE_FILE]) {
1879 for_each_evictable_lru(lru) {
1880 if (nr[lru]) {
1881 nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX);
1882 nr[lru] -= nr_to_scan;
1883
1884 nr_reclaimed += shrink_list(lru, nr_to_scan,
1885 lruvec, sc);
1886 }
1887 }
1888 /*
1889 * On large memory systems, scan >> priority can become
1890 * really large. This is fine for the starting priority;
1891 * we want to put equal scanning pressure on each zone.
1892 * However, if the VM has a harder time of freeing pages,
1893 * with multiple processes reclaiming pages, the total
1894 * freeing target can get unreasonably large.
1895 */
1896 if (nr_reclaimed >= nr_to_reclaim &&
1897 sc->priority < DEF_PRIORITY)
1898 break;
1899 }
1900 blk_finish_plug(&plug);
1901 sc->nr_reclaimed += nr_reclaimed;
1902
1903 /*
1904 * Even if we did not try to evict anon pages at all, we want to
1905 * rebalance the anon lru active/inactive ratio.
1906 */
1907 if (inactive_anon_is_low(lruvec))
1908 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
1909 sc, LRU_ACTIVE_ANON);
1910
1911 throttle_vm_writeout(sc->gfp_mask);
1912}
1913
Mel Gorman23b9da52012-05-29 15:06:20 -07001914/* Use reclaim/compaction for costly allocs or under memory pressure */
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07001915static bool in_reclaim_compaction(struct scan_control *sc)
Mel Gorman23b9da52012-05-29 15:06:20 -07001916{
Kirill A. Shutemovd84da3f2012-12-11 16:00:31 -08001917 if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
Mel Gorman23b9da52012-05-29 15:06:20 -07001918 (sc->order > PAGE_ALLOC_COSTLY_ORDER ||
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07001919 sc->priority < DEF_PRIORITY - 2))
Mel Gorman23b9da52012-05-29 15:06:20 -07001920 return true;
1921
1922 return false;
1923}
1924
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001925/*
Mel Gorman23b9da52012-05-29 15:06:20 -07001926 * Reclaim/compaction is used for high-order allocation requests. It reclaims
1927 * order-0 pages before compacting the zone. should_continue_reclaim() returns
1928 * true if more pages should be reclaimed such that when the page allocator
1929 * calls try_to_compact_zone() that it will have enough free pages to succeed.
1930 * It will give up earlier than that if there is difficulty reclaiming pages.
Mel Gorman3e7d3442011-01-13 15:45:56 -08001931 */
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08001932static inline bool should_continue_reclaim(struct zone *zone,
Mel Gorman3e7d3442011-01-13 15:45:56 -08001933 unsigned long nr_reclaimed,
1934 unsigned long nr_scanned,
1935 struct scan_control *sc)
1936{
1937 unsigned long pages_for_compaction;
1938 unsigned long inactive_lru_pages;
1939
1940 /* If not in reclaim/compaction mode, stop */
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07001941 if (!in_reclaim_compaction(sc))
Mel Gorman3e7d3442011-01-13 15:45:56 -08001942 return false;
1943
Mel Gorman28765922011-02-25 14:44:20 -08001944 /* Consider stopping depending on scan and reclaim activity */
1945 if (sc->gfp_mask & __GFP_REPEAT) {
1946 /*
1947 * For __GFP_REPEAT allocations, stop reclaiming if the
1948 * full LRU list has been scanned and we are still failing
1949 * to reclaim pages. This full LRU scan is potentially
1950 * expensive but a __GFP_REPEAT caller really wants to succeed
1951 */
1952 if (!nr_reclaimed && !nr_scanned)
1953 return false;
1954 } else {
1955 /*
1956 * For non-__GFP_REPEAT allocations which can presumably
1957 * fail without consequence, stop if we failed to reclaim
1958 * any pages from the last SWAP_CLUSTER_MAX number of
1959 * pages that were scanned. This will return to the
1960 * caller faster at the risk reclaim/compaction and
1961 * the resulting allocation attempt fails
1962 */
1963 if (!nr_reclaimed)
1964 return false;
1965 }
Mel Gorman3e7d3442011-01-13 15:45:56 -08001966
1967 /*
1968 * If we have not reclaimed enough pages for compaction and the
1969 * inactive lists are large enough, continue reclaiming
1970 */
1971 pages_for_compaction = (2UL << sc->order);
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08001972 inactive_lru_pages = zone_page_state(zone, NR_INACTIVE_FILE);
Shaohua Liec8acf22013-02-22 16:34:38 -08001973 if (get_nr_swap_pages() > 0)
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08001974 inactive_lru_pages += zone_page_state(zone, NR_INACTIVE_ANON);
Mel Gorman3e7d3442011-01-13 15:45:56 -08001975 if (sc->nr_reclaimed < pages_for_compaction &&
1976 inactive_lru_pages > pages_for_compaction)
1977 return true;
1978
1979 /* If compaction would go ahead or the allocation would succeed, stop */
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08001980 switch (compaction_suitable(zone, sc->order)) {
Mel Gorman3e7d3442011-01-13 15:45:56 -08001981 case COMPACT_PARTIAL:
1982 case COMPACT_CONTINUE:
1983 return false;
1984 default:
1985 return true;
1986 }
1987}
1988
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07001989static void shrink_zone(struct zone *zone, struct scan_control *sc)
Johannes Weinerf16015f2012-01-12 17:17:52 -08001990{
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08001991 unsigned long nr_reclaimed, nr_scanned;
Johannes Weinerf16015f2012-01-12 17:17:52 -08001992
Johannes Weiner56600482012-01-12 17:17:59 -08001993 do {
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08001994 struct mem_cgroup *root = sc->target_mem_cgroup;
1995 struct mem_cgroup_reclaim_cookie reclaim = {
1996 .zone = zone,
1997 .priority = sc->priority,
1998 };
1999 struct mem_cgroup *memcg;
Johannes Weiner56600482012-01-12 17:17:59 -08002000
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002001 nr_reclaimed = sc->nr_reclaimed;
2002 nr_scanned = sc->nr_scanned;
Konstantin Khlebnikovf9be23d2012-05-29 15:07:02 -07002003
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002004 memcg = mem_cgroup_iter(root, NULL, &reclaim);
2005 do {
2006 struct lruvec *lruvec;
2007
2008 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
2009
2010 shrink_lruvec(lruvec, sc);
2011
2012 /*
Michal Hockoa394cb82013-02-22 16:32:30 -08002013 * Direct reclaim and kswapd have to scan all memory
2014 * cgroups to fulfill the overall scan target for the
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002015 * zone.
Michal Hockoa394cb82013-02-22 16:32:30 -08002016 *
2017 * Limit reclaim, on the other hand, only cares about
2018 * nr_to_reclaim pages to be reclaimed and it will
2019 * retry with decreasing priority if one round over the
2020 * whole hierarchy is not sufficient.
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002021 */
Michal Hockoa394cb82013-02-22 16:32:30 -08002022 if (!global_reclaim(sc) &&
2023 sc->nr_reclaimed >= sc->nr_to_reclaim) {
Johannes Weiner9b4f98c2013-02-22 16:32:19 -08002024 mem_cgroup_iter_break(root, memcg);
2025 break;
2026 }
2027 memcg = mem_cgroup_iter(root, memcg, &reclaim);
2028 } while (memcg);
2029 } while (should_continue_reclaim(zone, sc->nr_reclaimed - nr_reclaimed,
2030 sc->nr_scanned - nr_scanned, sc));
Johannes Weinerf16015f2012-01-12 17:17:52 -08002031}
2032
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002033/* Returns true if compaction should go ahead for a high-order request */
2034static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
2035{
2036 unsigned long balance_gap, watermark;
2037 bool watermark_ok;
2038
2039 /* Do not consider compaction for orders reclaim is meant to satisfy */
2040 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER)
2041 return false;
2042
2043 /*
2044 * Compaction takes time to run and there are potentially other
2045 * callers using the pages just freed. Continue reclaiming until
2046 * there is a buffer of free pages available to give compaction
2047 * a reasonable chance of completing and allocating the page
2048 */
2049 balance_gap = min(low_wmark_pages(zone),
Jiang Liub40da042013-02-22 16:33:52 -08002050 (zone->managed_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002051 KSWAPD_ZONE_BALANCE_GAP_RATIO);
2052 watermark = high_wmark_pages(zone) + balance_gap + (2UL << sc->order);
2053 watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0);
2054
2055 /*
2056 * If compaction is deferred, reclaim up to a point where
2057 * compaction will have a chance of success when re-enabled
2058 */
Rik van Rielaff62242012-03-21 16:33:52 -07002059 if (compaction_deferred(zone, sc->order))
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002060 return watermark_ok;
2061
2062 /* If compaction is not ready to start, keep reclaiming */
2063 if (!compaction_suitable(zone, sc->order))
2064 return false;
2065
2066 return watermark_ok;
2067}
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069/*
2070 * This is the direct reclaim path, for page-allocating processes. We only
2071 * try to reclaim pages from zones which will satisfy the caller's allocation
2072 * request.
2073 *
Mel Gorman41858962009-06-16 15:32:12 -07002074 * We reclaim from a zone even if that zone is over high_wmark_pages(zone).
2075 * Because:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 * a) The caller may be trying to free *extra* pages to satisfy a higher-order
2077 * allocation or
Mel Gorman41858962009-06-16 15:32:12 -07002078 * b) The target zone may be at high_wmark_pages(zone) but the lower zones
2079 * must go *over* high_wmark_pages(zone) to satisfy the `incremental min'
2080 * zone defense algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 * If a zone is deemed to be full of pinned pages then just give it a light
2083 * scan then give up on it.
Mel Gormane0c23272011-10-31 17:09:33 -07002084 *
2085 * This function returns true if a zone is being reclaimed for a costly
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002086 * high-order allocation and compaction is ready to begin. This indicates to
Mel Gorman0cee34f2012-01-12 17:19:49 -08002087 * the caller that it should consider retrying the allocation instead of
2088 * further reclaim.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 */
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002090static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Mel Gormandd1a2392008-04-28 02:12:17 -07002092 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07002093 struct zone *zone;
Ying Hand149e3b2011-05-26 16:25:27 -07002094 unsigned long nr_soft_reclaimed;
2095 unsigned long nr_soft_scanned;
Mel Gorman0cee34f2012-01-12 17:19:49 -08002096 bool aborted_reclaim = false;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002097
Mel Gormancc715d92012-03-21 16:34:00 -07002098 /*
2099 * If the number of buffer_heads in the machine exceeds the maximum
2100 * allowed level, force direct reclaim to scan the highmem zone as
2101 * highmem pages could be pinning lowmem pages storing buffer_heads
2102 */
2103 if (buffer_heads_over_limit)
2104 sc->gfp_mask |= __GFP_HIGHMEM;
2105
Mel Gormand4debc62010-08-09 17:19:29 -07002106 for_each_zone_zonelist_nodemask(zone, z, zonelist,
2107 gfp_zone(sc->gfp_mask), sc->nodemask) {
Con Kolivasf3fe6512006-01-06 00:11:15 -08002108 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 continue;
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002110 /*
2111 * Take care memory controller reclaiming has small influence
2112 * to global LRU.
2113 */
Johannes Weiner89b5fae2012-01-12 17:17:50 -08002114 if (global_reclaim(sc)) {
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002115 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
2116 continue;
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002117 if (zone->all_unreclaimable &&
2118 sc->priority != DEF_PRIORITY)
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002119 continue; /* Let kswapd poll it */
Kirill A. Shutemovd84da3f2012-12-11 16:00:31 -08002120 if (IS_ENABLED(CONFIG_COMPACTION)) {
Rik van Riele0887c12011-10-31 17:09:31 -07002121 /*
Mel Gormane0c23272011-10-31 17:09:33 -07002122 * If we already have plenty of memory free for
2123 * compaction in this zone, don't free any more.
2124 * Even though compaction is invoked for any
2125 * non-zero order, only frequent costly order
2126 * reclamation is disruptive enough to become a
Copot Alexandruc7cfa372012-03-21 16:34:10 -07002127 * noticeable problem, like transparent huge
2128 * page allocations.
Rik van Riele0887c12011-10-31 17:09:31 -07002129 */
Mel Gormanfe4b1b22012-01-12 17:19:45 -08002130 if (compaction_ready(zone, sc)) {
Mel Gorman0cee34f2012-01-12 17:19:49 -08002131 aborted_reclaim = true;
Rik van Riele0887c12011-10-31 17:09:31 -07002132 continue;
Mel Gormane0c23272011-10-31 17:09:33 -07002133 }
Rik van Riele0887c12011-10-31 17:09:31 -07002134 }
KAMEZAWA Hiroyukiac34a1a2011-06-27 16:18:12 -07002135 /*
2136 * This steals pages from memory cgroups over softlimit
2137 * and returns the number of reclaimed pages and
2138 * scanned pages. This works for global memory pressure
2139 * and balancing, not for a memcg's limit.
2140 */
2141 nr_soft_scanned = 0;
2142 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
2143 sc->order, sc->gfp_mask,
2144 &nr_soft_scanned);
2145 sc->nr_reclaimed += nr_soft_reclaimed;
2146 sc->nr_scanned += nr_soft_scanned;
2147 /* need some check for avoid more shrink_zone() */
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002148 }
Nick Piggin408d8542006-09-25 23:31:27 -07002149
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002150 shrink_zone(zone, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
Mel Gormane0c23272011-10-31 17:09:33 -07002152
Mel Gorman0cee34f2012-01-12 17:19:49 -08002153 return aborted_reclaim;
Minchan Kimd1908362010-09-22 13:05:01 -07002154}
2155
2156static bool zone_reclaimable(struct zone *zone)
2157{
2158 return zone->pages_scanned < zone_reclaimable_pages(zone) * 6;
2159}
2160
KOSAKI Motohiro929bea72011-04-14 15:22:12 -07002161/* All zones in zonelist are unreclaimable? */
Minchan Kimd1908362010-09-22 13:05:01 -07002162static bool all_unreclaimable(struct zonelist *zonelist,
2163 struct scan_control *sc)
2164{
2165 struct zoneref *z;
2166 struct zone *zone;
Minchan Kimd1908362010-09-22 13:05:01 -07002167
2168 for_each_zone_zonelist_nodemask(zone, z, zonelist,
2169 gfp_zone(sc->gfp_mask), sc->nodemask) {
2170 if (!populated_zone(zone))
2171 continue;
2172 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
2173 continue;
KOSAKI Motohiro929bea72011-04-14 15:22:12 -07002174 if (!zone->all_unreclaimable)
2175 return false;
Minchan Kimd1908362010-09-22 13:05:01 -07002176 }
2177
KOSAKI Motohiro929bea72011-04-14 15:22:12 -07002178 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181/*
2182 * This is the main entry point to direct page reclaim.
2183 *
2184 * If a full scan of the inactive list fails to free enough memory then we
2185 * are "out of memory" and something needs to be killed.
2186 *
2187 * If the caller is !__GFP_FS then the probability of a failure is reasonably
2188 * high - the zone may be full of dirty or under-writeback pages, which this
Jens Axboe5b0830c2009-09-23 19:37:09 +02002189 * caller can't do much about. We kick the writeback threads and take explicit
2190 * naps in the hope that some of these pages can be written. But if the
2191 * allocating task holds filesystem locks which prevent writeout this might not
2192 * work, and the allocation attempt will fail.
Nishanth Aravamudana41f24e2008-04-29 00:58:25 -07002193 *
2194 * returns: 0, if no pages reclaimed
2195 * else, the number of pages reclaimed
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 */
Mel Gormandac1d272008-04-28 02:12:12 -07002197static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
Ying Hana09ed5e2011-05-24 17:12:26 -07002198 struct scan_control *sc,
2199 struct shrink_control *shrink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Andrew Morton69e05942006-03-22 00:08:19 -08002201 unsigned long total_scanned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 struct reclaim_state *reclaim_state = current->reclaim_state;
Mel Gormandd1a2392008-04-28 02:12:17 -07002203 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07002204 struct zone *zone;
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08002205 unsigned long writeback_threshold;
Mel Gorman0cee34f2012-01-12 17:19:49 -08002206 bool aborted_reclaim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Keika Kobayashi873b4772008-07-25 01:48:52 -07002208 delayacct_freepages_start();
2209
Johannes Weiner89b5fae2012-01-12 17:17:50 -08002210 if (global_reclaim(sc))
KAMEZAWA Hiroyuki1cfb4192008-02-07 00:14:37 -08002211 count_vm_event(ALLOCSTALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002213 do {
Balbir Singh66e17072008-02-07 00:13:56 -08002214 sc->nr_scanned = 0;
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002215 aborted_reclaim = shrink_zones(zonelist, sc);
Mel Gormane0c23272011-10-31 17:09:33 -07002216
Balbir Singh66e17072008-02-07 00:13:56 -08002217 /*
2218 * Don't shrink slabs when reclaiming memory from
2219 * over limit cgroups
2220 */
Johannes Weiner89b5fae2012-01-12 17:17:50 -08002221 if (global_reclaim(sc)) {
KOSAKI Motohiroc6a8a8c2010-08-09 17:19:14 -07002222 unsigned long lru_pages = 0;
Mel Gormand4debc62010-08-09 17:19:29 -07002223 for_each_zone_zonelist(zone, z, zonelist,
2224 gfp_zone(sc->gfp_mask)) {
KOSAKI Motohiroc6a8a8c2010-08-09 17:19:14 -07002225 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
2226 continue;
2227
2228 lru_pages += zone_reclaimable_pages(zone);
2229 }
2230
Ying Han1495f232011-05-24 17:12:27 -07002231 shrink_slab(shrink, sc->nr_scanned, lru_pages);
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -08002232 if (reclaim_state) {
Rik van Riela79311c2009-01-06 14:40:01 -08002233 sc->nr_reclaimed += reclaim_state->reclaimed_slab;
KAMEZAWA Hiroyuki91a45472008-02-07 00:14:29 -08002234 reclaim_state->reclaimed_slab = 0;
2235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
Balbir Singh66e17072008-02-07 00:13:56 -08002237 total_scanned += sc->nr_scanned;
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002238 if (sc->nr_reclaimed >= sc->nr_to_reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 /*
Minchan Kim0e50ce32013-02-22 16:35:37 -08002242 * If we're getting trouble reclaiming, start doing
2243 * writepage even in laptop mode.
2244 */
2245 if (sc->priority < DEF_PRIORITY - 2)
2246 sc->may_writepage = 1;
2247
2248 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 * Try to write back as many pages as we just scanned. This
2250 * tends to cause slow streaming writers to write data to the
2251 * disk smoothly, at the dirtying rate, which is nice. But
2252 * that's undesirable in laptop mode, where we *want* lumpy
2253 * writeout. So in laptop mode, write out the whole world.
2254 */
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08002255 writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
2256 if (total_scanned > writeback_threshold) {
Curt Wohlgemuth0e175a12011-10-07 21:54:10 -06002257 wakeup_flusher_threads(laptop_mode ? 0 : total_scanned,
2258 WB_REASON_TRY_TO_FREE_PAGES);
Balbir Singh66e17072008-02-07 00:13:56 -08002259 sc->may_writepage = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261
2262 /* Take a nap, wait for some writeback to complete */
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08002263 if (!sc->hibernation_mode && sc->nr_scanned &&
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002264 sc->priority < DEF_PRIORITY - 2) {
Mel Gorman0e093d992010-10-26 14:21:45 -07002265 struct zone *preferred_zone;
2266
2267 first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask),
David Rientjesf33261d2011-01-25 15:07:20 -08002268 &cpuset_current_mems_allowed,
2269 &preferred_zone);
Mel Gorman0e093d992010-10-26 14:21:45 -07002270 wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10);
2271 }
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002272 } while (--sc->priority >= 0);
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274out:
Keika Kobayashi873b4772008-07-25 01:48:52 -07002275 delayacct_freepages_end();
2276
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002277 if (sc->nr_reclaimed)
2278 return sc->nr_reclaimed;
2279
KOSAKI Motohiro929bea72011-04-14 15:22:12 -07002280 /*
2281 * As hibernation is going on, kswapd is freezed so that it can't mark
2282 * the zone into all_unreclaimable. Thus bypassing all_unreclaimable
2283 * check.
2284 */
2285 if (oom_killer_disabled)
2286 return 0;
2287
Mel Gorman0cee34f2012-01-12 17:19:49 -08002288 /* Aborted reclaim to try compaction? don't OOM, then */
2289 if (aborted_reclaim)
Mel Gorman73350842012-01-12 17:19:33 -08002290 return 1;
2291
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002292 /* top priority shrink_zones still had more to do? don't OOM, then */
Johannes Weiner89b5fae2012-01-12 17:17:50 -08002293 if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc))
KOSAKI Motohirobb21c7c2010-06-04 14:15:05 -07002294 return 1;
2295
2296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
Mel Gorman55150612012-07-31 16:44:35 -07002299static bool pfmemalloc_watermark_ok(pg_data_t *pgdat)
2300{
2301 struct zone *zone;
2302 unsigned long pfmemalloc_reserve = 0;
2303 unsigned long free_pages = 0;
2304 int i;
2305 bool wmark_ok;
2306
2307 for (i = 0; i <= ZONE_NORMAL; i++) {
2308 zone = &pgdat->node_zones[i];
2309 pfmemalloc_reserve += min_wmark_pages(zone);
2310 free_pages += zone_page_state(zone, NR_FREE_PAGES);
2311 }
2312
2313 wmark_ok = free_pages > pfmemalloc_reserve / 2;
2314
2315 /* kswapd must be awake if processes are being throttled */
2316 if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
2317 pgdat->classzone_idx = min(pgdat->classzone_idx,
2318 (enum zone_type)ZONE_NORMAL);
2319 wake_up_interruptible(&pgdat->kswapd_wait);
2320 }
2321
2322 return wmark_ok;
2323}
2324
2325/*
2326 * Throttle direct reclaimers if backing storage is backed by the network
2327 * and the PFMEMALLOC reserve for the preferred node is getting dangerously
2328 * depleted. kswapd will continue to make progress and wake the processes
Mel Gorman50694c22012-11-26 16:29:48 -08002329 * when the low watermark is reached.
2330 *
2331 * Returns true if a fatal signal was delivered during throttling. If this
2332 * happens, the page allocator should not consider triggering the OOM killer.
Mel Gorman55150612012-07-31 16:44:35 -07002333 */
Mel Gorman50694c22012-11-26 16:29:48 -08002334static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
Mel Gorman55150612012-07-31 16:44:35 -07002335 nodemask_t *nodemask)
2336{
2337 struct zone *zone;
2338 int high_zoneidx = gfp_zone(gfp_mask);
2339 pg_data_t *pgdat;
2340
2341 /*
2342 * Kernel threads should not be throttled as they may be indirectly
2343 * responsible for cleaning pages necessary for reclaim to make forward
2344 * progress. kjournald for example may enter direct reclaim while
2345 * committing a transaction where throttling it could forcing other
2346 * processes to block on log_wait_commit().
2347 */
2348 if (current->flags & PF_KTHREAD)
Mel Gorman50694c22012-11-26 16:29:48 -08002349 goto out;
2350
2351 /*
2352 * If a fatal signal is pending, this process should not throttle.
2353 * It should return quickly so it can exit and free its memory
2354 */
2355 if (fatal_signal_pending(current))
2356 goto out;
Mel Gorman55150612012-07-31 16:44:35 -07002357
2358 /* Check if the pfmemalloc reserves are ok */
2359 first_zones_zonelist(zonelist, high_zoneidx, NULL, &zone);
2360 pgdat = zone->zone_pgdat;
2361 if (pfmemalloc_watermark_ok(pgdat))
Mel Gorman50694c22012-11-26 16:29:48 -08002362 goto out;
Mel Gorman55150612012-07-31 16:44:35 -07002363
Mel Gorman68243e72012-07-31 16:44:39 -07002364 /* Account for the throttling */
2365 count_vm_event(PGSCAN_DIRECT_THROTTLE);
2366
Mel Gorman55150612012-07-31 16:44:35 -07002367 /*
2368 * If the caller cannot enter the filesystem, it's possible that it
2369 * is due to the caller holding an FS lock or performing a journal
2370 * transaction in the case of a filesystem like ext[3|4]. In this case,
2371 * it is not safe to block on pfmemalloc_wait as kswapd could be
2372 * blocked waiting on the same lock. Instead, throttle for up to a
2373 * second before continuing.
2374 */
2375 if (!(gfp_mask & __GFP_FS)) {
2376 wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
2377 pfmemalloc_watermark_ok(pgdat), HZ);
Mel Gorman50694c22012-11-26 16:29:48 -08002378
2379 goto check_pending;
Mel Gorman55150612012-07-31 16:44:35 -07002380 }
2381
2382 /* Throttle until kswapd wakes the process */
2383 wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
2384 pfmemalloc_watermark_ok(pgdat));
Mel Gorman50694c22012-11-26 16:29:48 -08002385
2386check_pending:
2387 if (fatal_signal_pending(current))
2388 return true;
2389
2390out:
2391 return false;
Mel Gorman55150612012-07-31 16:44:35 -07002392}
2393
Mel Gormandac1d272008-04-28 02:12:12 -07002394unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07002395 gfp_t gfp_mask, nodemask_t *nodemask)
Balbir Singh66e17072008-02-07 00:13:56 -08002396{
Mel Gorman33906bc2010-08-09 17:19:16 -07002397 unsigned long nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08002398 struct scan_control sc = {
Ming Lei21caf2f2013-02-22 16:34:08 -08002399 .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)),
Balbir Singh66e17072008-02-07 00:13:56 -08002400 .may_writepage = !laptop_mode,
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08002401 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07002402 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07002403 .may_swap = 1,
Balbir Singh66e17072008-02-07 00:13:56 -08002404 .order = order,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002405 .priority = DEF_PRIORITY,
Johannes Weinerf16015f2012-01-12 17:17:52 -08002406 .target_mem_cgroup = NULL,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07002407 .nodemask = nodemask,
Balbir Singh66e17072008-02-07 00:13:56 -08002408 };
Ying Hana09ed5e2011-05-24 17:12:26 -07002409 struct shrink_control shrink = {
2410 .gfp_mask = sc.gfp_mask,
2411 };
Balbir Singh66e17072008-02-07 00:13:56 -08002412
Mel Gorman55150612012-07-31 16:44:35 -07002413 /*
Mel Gorman50694c22012-11-26 16:29:48 -08002414 * Do not enter reclaim if fatal signal was delivered while throttled.
2415 * 1 is returned so that the page allocator does not OOM kill at this
2416 * point.
Mel Gorman55150612012-07-31 16:44:35 -07002417 */
Mel Gorman50694c22012-11-26 16:29:48 -08002418 if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask))
Mel Gorman55150612012-07-31 16:44:35 -07002419 return 1;
2420
Mel Gorman33906bc2010-08-09 17:19:16 -07002421 trace_mm_vmscan_direct_reclaim_begin(order,
2422 sc.may_writepage,
2423 gfp_mask);
2424
Ying Hana09ed5e2011-05-24 17:12:26 -07002425 nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
Mel Gorman33906bc2010-08-09 17:19:16 -07002426
2427 trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
2428
2429 return nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08002430}
2431
Andrew Mortonc255a452012-07-31 16:43:02 -07002432#ifdef CONFIG_MEMCG
Balbir Singh66e17072008-02-07 00:13:56 -08002433
Johannes Weiner72835c82012-01-12 17:18:32 -08002434unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07002435 gfp_t gfp_mask, bool noswap,
Ying Han0ae5e892011-05-26 16:25:25 -07002436 struct zone *zone,
2437 unsigned long *nr_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07002438{
2439 struct scan_control sc = {
Ying Han0ae5e892011-05-26 16:25:25 -07002440 .nr_scanned = 0,
KOSAKI Motohirob8f5c562010-08-10 18:03:02 -07002441 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Balbir Singh4e416952009-09-23 15:56:39 -07002442 .may_writepage = !laptop_mode,
2443 .may_unmap = 1,
2444 .may_swap = !noswap,
Balbir Singh4e416952009-09-23 15:56:39 -07002445 .order = 0,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002446 .priority = 0,
Johannes Weiner72835c82012-01-12 17:18:32 -08002447 .target_mem_cgroup = memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07002448 };
Konstantin Khlebnikovf9be23d2012-05-29 15:07:02 -07002449 struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
Ying Han0ae5e892011-05-26 16:25:25 -07002450
Balbir Singh4e416952009-09-23 15:56:39 -07002451 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
2452 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07002453
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002454 trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07002455 sc.may_writepage,
2456 sc.gfp_mask);
2457
Balbir Singh4e416952009-09-23 15:56:39 -07002458 /*
2459 * NOTE: Although we can get the priority field, using it
2460 * here is not a good idea, since it limits the pages we can scan.
2461 * if we don't reclaim here, the shrink_zone from balance_pgdat
2462 * will pick up pages from other mem cgroup's as well. We hack
2463 * the priority and make it zero.
2464 */
Konstantin Khlebnikovf9be23d2012-05-29 15:07:02 -07002465 shrink_lruvec(lruvec, &sc);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07002466
2467 trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
2468
Ying Han0ae5e892011-05-26 16:25:25 -07002469 *nr_scanned = sc.nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07002470 return sc.nr_reclaimed;
2471}
2472
Johannes Weiner72835c82012-01-12 17:18:32 -08002473unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002474 gfp_t gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07002475 bool noswap)
Balbir Singh66e17072008-02-07 00:13:56 -08002476{
Balbir Singh4e416952009-09-23 15:56:39 -07002477 struct zonelist *zonelist;
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07002478 unsigned long nr_reclaimed;
Ying Han889976d2011-05-26 16:25:33 -07002479 int nid;
Balbir Singh66e17072008-02-07 00:13:56 -08002480 struct scan_control sc = {
Balbir Singh66e17072008-02-07 00:13:56 -08002481 .may_writepage = !laptop_mode,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07002482 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07002483 .may_swap = !noswap,
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08002484 .nr_to_reclaim = SWAP_CLUSTER_MAX,
Balbir Singh66e17072008-02-07 00:13:56 -08002485 .order = 0,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002486 .priority = DEF_PRIORITY,
Johannes Weiner72835c82012-01-12 17:18:32 -08002487 .target_mem_cgroup = memcg,
KAMEZAWA Hiroyuki327c0e92009-03-31 15:23:31 -07002488 .nodemask = NULL, /* we don't care the placement */
Ying Hana09ed5e2011-05-24 17:12:26 -07002489 .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
2490 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
2491 };
2492 struct shrink_control shrink = {
2493 .gfp_mask = sc.gfp_mask,
Balbir Singh66e17072008-02-07 00:13:56 -08002494 };
Balbir Singh66e17072008-02-07 00:13:56 -08002495
Ying Han889976d2011-05-26 16:25:33 -07002496 /*
2497 * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't
2498 * take care of from where we get pages. So the node where we start the
2499 * scan does not need to be the current node.
2500 */
Johannes Weiner72835c82012-01-12 17:18:32 -08002501 nid = mem_cgroup_select_victim_node(memcg);
Ying Han889976d2011-05-26 16:25:33 -07002502
2503 zonelist = NODE_DATA(nid)->node_zonelists;
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07002504
2505 trace_mm_vmscan_memcg_reclaim_begin(0,
2506 sc.may_writepage,
2507 sc.gfp_mask);
2508
Ying Hana09ed5e2011-05-24 17:12:26 -07002509 nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
KOSAKI Motohirobdce6d92010-08-09 17:19:56 -07002510
2511 trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
2512
2513 return nr_reclaimed;
Balbir Singh66e17072008-02-07 00:13:56 -08002514}
2515#endif
2516
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002517static void age_active_anon(struct zone *zone, struct scan_control *sc)
Johannes Weinerf16015f2012-01-12 17:17:52 -08002518{
Johannes Weinerb95a2f22012-01-12 17:18:06 -08002519 struct mem_cgroup *memcg;
Johannes Weinerf16015f2012-01-12 17:17:52 -08002520
Johannes Weinerb95a2f22012-01-12 17:18:06 -08002521 if (!total_swap_pages)
2522 return;
2523
2524 memcg = mem_cgroup_iter(NULL, NULL, NULL);
2525 do {
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07002526 struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
Johannes Weinerb95a2f22012-01-12 17:18:06 -08002527
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07002528 if (inactive_anon_is_low(lruvec))
Konstantin Khlebnikov1a93be02012-05-29 15:07:01 -07002529 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002530 sc, LRU_ACTIVE_ANON);
Johannes Weinerb95a2f22012-01-12 17:18:06 -08002531
2532 memcg = mem_cgroup_iter(NULL, memcg, NULL);
2533 } while (memcg);
Johannes Weinerf16015f2012-01-12 17:17:52 -08002534}
2535
Johannes Weiner60cefed2012-11-29 13:54:23 -08002536static bool zone_balanced(struct zone *zone, int order,
2537 unsigned long balance_gap, int classzone_idx)
2538{
2539 if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone) +
2540 balance_gap, classzone_idx, 0))
2541 return false;
2542
Kirill A. Shutemovd84da3f2012-12-11 16:00:31 -08002543 if (IS_ENABLED(CONFIG_COMPACTION) && order &&
2544 !compaction_suitable(zone, order))
Johannes Weiner60cefed2012-11-29 13:54:23 -08002545 return false;
2546
2547 return true;
2548}
2549
Mel Gorman1741c872011-01-13 15:46:21 -08002550/*
Zlatko Calusic4ae0a482012-12-23 15:12:54 +01002551 * pgdat_balanced() is used when checking if a node is balanced.
2552 *
2553 * For order-0, all zones must be balanced!
2554 *
2555 * For high-order allocations only zones that meet watermarks and are in a
2556 * zone allowed by the callers classzone_idx are added to balanced_pages. The
2557 * total of balanced pages must be at least 25% of the zones allowed by
2558 * classzone_idx for the node to be considered balanced. Forcing all zones to
2559 * be balanced for high orders can cause excessive reclaim when there are
2560 * imbalanced zones.
Mel Gorman1741c872011-01-13 15:46:21 -08002561 * The choice of 25% is due to
2562 * o a 16M DMA zone that is balanced will not balance a zone on any
2563 * reasonable sized machine
2564 * o On all other machines, the top zone must be at least a reasonable
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002565 * percentage of the middle zones. For example, on 32-bit x86, highmem
Mel Gorman1741c872011-01-13 15:46:21 -08002566 * would need to be at least 256M for it to be balance a whole node.
2567 * Similarly, on x86-64 the Normal zone would need to be at least 1G
2568 * to balance a node on its own. These seemed like reasonable ratios.
2569 */
Zlatko Calusic4ae0a482012-12-23 15:12:54 +01002570static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx)
Mel Gorman1741c872011-01-13 15:46:21 -08002571{
Jiang Liub40da042013-02-22 16:33:52 -08002572 unsigned long managed_pages = 0;
Zlatko Calusic4ae0a482012-12-23 15:12:54 +01002573 unsigned long balanced_pages = 0;
Mel Gorman1741c872011-01-13 15:46:21 -08002574 int i;
2575
Zlatko Calusic4ae0a482012-12-23 15:12:54 +01002576 /* Check the watermark levels */
2577 for (i = 0; i <= classzone_idx; i++) {
2578 struct zone *zone = pgdat->node_zones + i;
Mel Gorman1741c872011-01-13 15:46:21 -08002579
Zlatko Calusic4ae0a482012-12-23 15:12:54 +01002580 if (!populated_zone(zone))
2581 continue;
2582
Jiang Liub40da042013-02-22 16:33:52 -08002583 managed_pages += zone->managed_pages;
Zlatko Calusic4ae0a482012-12-23 15:12:54 +01002584
2585 /*
2586 * A special case here:
2587 *
2588 * balance_pgdat() skips over all_unreclaimable after
2589 * DEF_PRIORITY. Effectively, it considers them balanced so
2590 * they must be considered balanced here as well!
2591 */
2592 if (zone->all_unreclaimable) {
Jiang Liub40da042013-02-22 16:33:52 -08002593 balanced_pages += zone->managed_pages;
Zlatko Calusic4ae0a482012-12-23 15:12:54 +01002594 continue;
2595 }
2596
2597 if (zone_balanced(zone, order, 0, i))
Jiang Liub40da042013-02-22 16:33:52 -08002598 balanced_pages += zone->managed_pages;
Zlatko Calusic4ae0a482012-12-23 15:12:54 +01002599 else if (!order)
2600 return false;
2601 }
2602
2603 if (order)
Jiang Liub40da042013-02-22 16:33:52 -08002604 return balanced_pages >= (managed_pages >> 2);
Zlatko Calusic4ae0a482012-12-23 15:12:54 +01002605 else
2606 return true;
Mel Gorman1741c872011-01-13 15:46:21 -08002607}
2608
Mel Gorman55150612012-07-31 16:44:35 -07002609/*
2610 * Prepare kswapd for sleeping. This verifies that there are no processes
2611 * waiting in throttle_direct_reclaim() and that watermarks have been met.
2612 *
2613 * Returns true if kswapd is ready to sleep
2614 */
2615static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, long remaining,
Mel Gormandc83edd2011-01-13 15:46:26 -08002616 int classzone_idx)
Mel Gormanf50de2d2009-12-14 17:58:53 -08002617{
Mel Gormanf50de2d2009-12-14 17:58:53 -08002618 /* If a direct reclaimer woke kswapd within HZ/10, it's premature */
2619 if (remaining)
Mel Gorman55150612012-07-31 16:44:35 -07002620 return false;
2621
2622 /*
2623 * There is a potential race between when kswapd checks its watermarks
2624 * and a process gets throttled. There is also a potential race if
2625 * processes get throttled, kswapd wakes, a large process exits therby
2626 * balancing the zones that causes kswapd to miss a wakeup. If kswapd
2627 * is going to sleep, no process should be sleeping on pfmemalloc_wait
2628 * so wake them now if necessary. If necessary, processes will wake
2629 * kswapd and get throttled again
2630 */
2631 if (waitqueue_active(&pgdat->pfmemalloc_wait)) {
2632 wake_up(&pgdat->pfmemalloc_wait);
2633 return false;
2634 }
Mel Gormanf50de2d2009-12-14 17:58:53 -08002635
Zlatko Calusic4ae0a482012-12-23 15:12:54 +01002636 return pgdat_balanced(pgdat, order, classzone_idx);
Mel Gormanf50de2d2009-12-14 17:58:53 -08002637}
2638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639/*
2640 * For kswapd, balance_pgdat() will work across all this node's zones until
Mel Gorman41858962009-06-16 15:32:12 -07002641 * they are all at high_wmark_pages(zone).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 *
Mel Gorman0abdee22011-01-13 15:46:22 -08002643 * Returns the final order kswapd was reclaiming at
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 *
2645 * There is special handling here for zones which are full of pinned pages.
2646 * This can happen if the pages are all mlocked, or if they are all used by
2647 * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
2648 * What we do is to detect the case where all pages in the zone have been
2649 * scanned twice and there has been zero successful reclaim. Mark the zone as
2650 * dead and from now on, only perform a short scan. Basically we're polling
2651 * the zone for when the problem goes away.
2652 *
2653 * kswapd scans the zones in the highmem->normal->dma direction. It skips
Mel Gorman41858962009-06-16 15:32:12 -07002654 * zones which have free_pages > high_wmark_pages(zone), but once a zone is
2655 * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the
2656 * lower zones regardless of the number of free pages in the lower zones. This
2657 * interoperates with the page allocator fallback scheme to ensure that aging
2658 * of pages is balanced across the zones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 */
Mel Gorman99504742011-01-13 15:46:20 -08002660static unsigned long balance_pgdat(pg_data_t *pgdat, int order,
Mel Gormandc83edd2011-01-13 15:46:26 -08002661 int *classzone_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
Zlatko Calusicdafcb732013-02-22 16:32:34 -08002663 bool pgdat_is_balanced = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 int i;
Mel Gorman99504742011-01-13 15:46:20 -08002665 int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
Andrew Morton69e05942006-03-22 00:08:19 -08002666 unsigned long total_scanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 struct reclaim_state *reclaim_state = current->reclaim_state;
Ying Han0ae5e892011-05-26 16:25:25 -07002668 unsigned long nr_soft_reclaimed;
2669 unsigned long nr_soft_scanned;
Andrew Morton179e9632006-03-22 00:08:18 -08002670 struct scan_control sc = {
2671 .gfp_mask = GFP_KERNEL,
Johannes Weinera6dc60f82009-03-31 15:19:30 -07002672 .may_unmap = 1,
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07002673 .may_swap = 1,
KOSAKI Motohiro22fba332009-12-14 17:59:10 -08002674 /*
2675 * kswapd doesn't want to be bailed out while reclaim. because
2676 * we want to put equal scanning pressure on each zone.
2677 */
2678 .nr_to_reclaim = ULONG_MAX,
Andy Whitcroft5ad333e2007-07-17 04:03:16 -07002679 .order = order,
Johannes Weinerf16015f2012-01-12 17:17:52 -08002680 .target_mem_cgroup = NULL,
Andrew Morton179e9632006-03-22 00:08:18 -08002681 };
Ying Hana09ed5e2011-05-24 17:12:26 -07002682 struct shrink_control shrink = {
2683 .gfp_mask = sc.gfp_mask,
2684 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685loop_again:
2686 total_scanned = 0;
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002687 sc.priority = DEF_PRIORITY;
Rik van Riela79311c2009-01-06 14:40:01 -08002688 sc.nr_reclaimed = 0;
Christoph Lameterc0bbbc72006-06-11 15:22:26 -07002689 sc.may_writepage = !laptop_mode;
Christoph Lameterf8891e52006-06-30 01:55:45 -07002690 count_vm_event(PAGEOUTRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002692 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 unsigned long lru_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002695 /*
2696 * Scan in the highmem->dma direction for the highest
2697 * zone which needs scanning
2698 */
2699 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
2700 struct zone *zone = pgdat->node_zones + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002702 if (!populated_zone(zone))
2703 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002705 if (zone->all_unreclaimable &&
2706 sc.priority != DEF_PRIORITY)
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002707 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Rik van Riel556adec2008-10-18 20:26:34 -07002709 /*
2710 * Do some background aging of the anon list, to give
2711 * pages a chance to be referenced before reclaiming.
2712 */
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002713 age_active_anon(zone, &sc);
Rik van Riel556adec2008-10-18 20:26:34 -07002714
Mel Gormancc715d92012-03-21 16:34:00 -07002715 /*
2716 * If the number of buffer_heads in the machine
2717 * exceeds the maximum allowed level and this node
2718 * has a highmem zone, force kswapd to reclaim from
2719 * it to relieve lowmem pressure.
2720 */
2721 if (buffer_heads_over_limit && is_highmem_idx(i)) {
2722 end_zone = i;
2723 break;
2724 }
2725
Johannes Weiner60cefed2012-11-29 13:54:23 -08002726 if (!zone_balanced(zone, order, 0, 0)) {
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07002727 end_zone = i;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08002728 break;
Shaohua Li439423f2011-08-25 15:59:12 -07002729 } else {
2730 /* If balanced, clear the congested flag */
2731 zone_clear_flag(zone, ZONE_CONGESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
Zlatko Calusicdafcb732013-02-22 16:32:34 -08002734
2735 if (i < 0) {
2736 pgdat_is_balanced = true;
Andrew Mortone1dbeda2006-12-06 20:32:01 -08002737 goto out;
Zlatko Calusicdafcb732013-02-22 16:32:34 -08002738 }
Andrew Mortone1dbeda2006-12-06 20:32:01 -08002739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 for (i = 0; i <= end_zone; i++) {
2741 struct zone *zone = pgdat->node_zones + i;
2742
Wu Fengguangadea02a2009-09-21 17:01:42 -07002743 lru_pages += zone_reclaimable_pages(zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 }
2745
2746 /*
2747 * Now scan the zone in the dma->highmem direction, stopping
2748 * at the last zone which needs scanning.
2749 *
2750 * We do this because the page allocator works in the opposite
2751 * direction. This prevents the page allocator from allocating
2752 * pages behind kswapd's direction of progress, which would
2753 * cause too much scanning of the lower zones.
2754 */
2755 for (i = 0; i <= end_zone; i++) {
2756 struct zone *zone = pgdat->node_zones + i;
Rik van Rielfe2c2a12012-03-21 16:33:51 -07002757 int nr_slab, testorder;
Mel Gorman8afdcec2011-03-22 16:33:04 -07002758 unsigned long balance_gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Con Kolivasf3fe6512006-01-06 00:11:15 -08002760 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 continue;
2762
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002763 if (zone->all_unreclaimable &&
2764 sc.priority != DEF_PRIORITY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 continue;
2766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 sc.nr_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07002768
Ying Han0ae5e892011-05-26 16:25:25 -07002769 nr_soft_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07002770 /*
2771 * Call soft limit reclaim before calling shrink_zone.
Balbir Singh4e416952009-09-23 15:56:39 -07002772 */
Ying Han0ae5e892011-05-26 16:25:25 -07002773 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
2774 order, sc.gfp_mask,
2775 &nr_soft_scanned);
2776 sc.nr_reclaimed += nr_soft_reclaimed;
2777 total_scanned += nr_soft_scanned;
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07002778
Rik van Riel32a43302007-10-16 01:24:50 -07002779 /*
Mel Gorman8afdcec2011-03-22 16:33:04 -07002780 * We put equal pressure on every zone, unless
2781 * one zone has way too many pages free
2782 * already. The "too many pages" is defined
2783 * as the high wmark plus a "gap" where the
2784 * gap is either the low watermark or 1%
2785 * of the zone, whichever is smaller.
Rik van Riel32a43302007-10-16 01:24:50 -07002786 */
Mel Gorman8afdcec2011-03-22 16:33:04 -07002787 balance_gap = min(low_wmark_pages(zone),
Jiang Liub40da042013-02-22 16:33:52 -08002788 (zone->managed_pages +
Mel Gorman8afdcec2011-03-22 16:33:04 -07002789 KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
2790 KSWAPD_ZONE_BALANCE_GAP_RATIO);
Rik van Rielfe2c2a12012-03-21 16:33:51 -07002791 /*
2792 * Kswapd reclaims only single pages with compaction
2793 * enabled. Trying too hard to reclaim until contiguous
2794 * free pages have become available can hurt performance
2795 * by evicting too much useful data from memory.
2796 * Do not reclaim more than needed for compaction.
2797 */
2798 testorder = order;
Kirill A. Shutemovd84da3f2012-12-11 16:00:31 -08002799 if (IS_ENABLED(CONFIG_COMPACTION) && order &&
Rik van Rielfe2c2a12012-03-21 16:33:51 -07002800 compaction_suitable(zone, order) !=
2801 COMPACT_SKIPPED)
2802 testorder = 0;
2803
Mel Gormancc715d92012-03-21 16:34:00 -07002804 if ((buffer_heads_over_limit && is_highmem_idx(i)) ||
Johannes Weiner60cefed2012-11-29 13:54:23 -08002805 !zone_balanced(zone, testorder,
2806 balance_gap, end_zone)) {
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002807 shrink_zone(zone, &sc);
Andrea Arcangeli5a03b052011-01-13 15:47:11 -08002808
Mel Gormand7868da2011-07-08 15:39:38 -07002809 reclaim_state->reclaimed_slab = 0;
2810 nr_slab = shrink_slab(&shrink, sc.nr_scanned, lru_pages);
2811 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
2812 total_scanned += sc.nr_scanned;
2813
2814 if (nr_slab == 0 && !zone_reclaimable(zone))
2815 zone->all_unreclaimable = 1;
2816 }
2817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 /*
Minchan Kim0e50ce32013-02-22 16:35:37 -08002819 * If we're getting trouble reclaiming, start doing
2820 * writepage even in laptop mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 */
Minchan Kim0e50ce32013-02-22 16:35:37 -08002822 if (sc.priority < DEF_PRIORITY - 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 sc.may_writepage = 1;
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -08002824
Mel Gorman215ddd62011-07-08 15:39:40 -07002825 if (zone->all_unreclaimable) {
2826 if (end_zone && end_zone == i)
2827 end_zone--;
Mel Gormand7868da2011-07-08 15:39:38 -07002828 continue;
Mel Gorman215ddd62011-07-08 15:39:40 -07002829 }
Mel Gormand7868da2011-07-08 15:39:38 -07002830
Zlatko Calusic258401a2013-02-22 16:34:06 -08002831 if (zone_balanced(zone, testorder, 0, end_zone))
Mel Gorman0e093d992010-10-26 14:21:45 -07002832 /*
2833 * If a zone reaches its high watermark,
2834 * consider it to be no longer congested. It's
2835 * possible there are dirty pages backed by
2836 * congested BDIs but as pressure is relieved,
Wanpeng Liab8704b2012-06-17 09:27:18 +08002837 * speculatively avoid congestion waits
Mel Gorman0e093d992010-10-26 14:21:45 -07002838 */
2839 zone_clear_flag(zone, ZONE_CONGESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
Mel Gorman55150612012-07-31 16:44:35 -07002841
2842 /*
2843 * If the low watermark is met there is no need for processes
2844 * to be throttled on pfmemalloc_wait as they should not be
2845 * able to safely make forward progress. Wake them
2846 */
2847 if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
2848 pfmemalloc_watermark_ok(pgdat))
2849 wake_up(&pgdat->pfmemalloc_wait);
2850
Zlatko Calusicdafcb732013-02-22 16:32:34 -08002851 if (pgdat_balanced(pgdat, order, *classzone_idx)) {
2852 pgdat_is_balanced = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 break; /* kswapd: all done */
Zlatko Calusicdafcb732013-02-22 16:32:34 -08002854 }
2855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 * We do this so kswapd doesn't build up large priorities for
2858 * example when it is freeing in parallel with allocators. It
2859 * matches the direct reclaim path behaviour in terms of impact
2860 * on zone->*_priority.
2861 */
Rik van Riela79311c2009-01-06 14:40:01 -08002862 if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 break;
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07002864 } while (--sc.priority >= 0);
Mel Gorman99504742011-01-13 15:46:20 -08002865
Zlatko Calusicdafcb732013-02-22 16:32:34 -08002866out:
2867 if (!pgdat_is_balanced) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 cond_resched();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002869
2870 try_to_freeze();
2871
KOSAKI Motohiro73ce02e2009-01-06 14:40:33 -08002872 /*
2873 * Fragmentation may mean that the system cannot be
2874 * rebalanced for high-order allocations in all zones.
2875 * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
2876 * it means the zones have been fully scanned and are still
2877 * not balanced. For high-order allocations, there is
2878 * little point trying all over again as kswapd may
2879 * infinite loop.
2880 *
2881 * Instead, recheck all watermarks at order-0 as they
2882 * are the most important. If watermarks are ok, kswapd will go
2883 * back to sleep. High-order users can still perform direct
2884 * reclaim if they wish.
2885 */
2886 if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
2887 order = sc.order = 0;
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 goto loop_again;
2890 }
2891
Mel Gorman99504742011-01-13 15:46:20 -08002892 /*
2893 * If kswapd was reclaiming at a higher order, it has the option of
2894 * sleeping without all zones being balanced. Before it does, it must
2895 * ensure that the watermarks for order-0 on *all* zones are met and
2896 * that the congestion flags are cleared. The congestion flag must
2897 * be cleared as kswapd is the only mechanism that clears the flag
2898 * and it is potentially going to sleep here.
2899 */
2900 if (order) {
Rik van Riel7be62de2012-03-21 16:33:52 -07002901 int zones_need_compaction = 1;
2902
Mel Gorman99504742011-01-13 15:46:20 -08002903 for (i = 0; i <= end_zone; i++) {
2904 struct zone *zone = pgdat->node_zones + i;
2905
2906 if (!populated_zone(zone))
2907 continue;
2908
Rik van Riel7be62de2012-03-21 16:33:52 -07002909 /* Check if the memory needs to be defragmented. */
2910 if (zone_watermark_ok(zone, order,
2911 low_wmark_pages(zone), *classzone_idx, 0))
2912 zones_need_compaction = 0;
Mel Gorman99504742011-01-13 15:46:20 -08002913 }
Rik van Riel7be62de2012-03-21 16:33:52 -07002914
2915 if (zones_need_compaction)
2916 compact_pgdat(pgdat, order);
Mel Gorman99504742011-01-13 15:46:20 -08002917 }
2918
Mel Gorman0abdee22011-01-13 15:46:22 -08002919 /*
Mel Gorman55150612012-07-31 16:44:35 -07002920 * Return the order we were reclaiming at so prepare_kswapd_sleep()
Mel Gorman0abdee22011-01-13 15:46:22 -08002921 * makes a decision on the order we were last reclaiming at. However,
2922 * if another caller entered the allocator slow path while kswapd
2923 * was awake, order will remain at the higher level
2924 */
Mel Gormandc83edd2011-01-13 15:46:26 -08002925 *classzone_idx = end_zone;
Mel Gorman0abdee22011-01-13 15:46:22 -08002926 return order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
Mel Gormandc83edd2011-01-13 15:46:26 -08002929static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx)
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08002930{
2931 long remaining = 0;
2932 DEFINE_WAIT(wait);
2933
2934 if (freezing(current) || kthread_should_stop())
2935 return;
2936
2937 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2938
2939 /* Try to sleep for a short interval */
Mel Gorman55150612012-07-31 16:44:35 -07002940 if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08002941 remaining = schedule_timeout(HZ/10);
2942 finish_wait(&pgdat->kswapd_wait, &wait);
2943 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2944 }
2945
2946 /*
2947 * After a short sleep, check if it was a premature sleep. If not, then
2948 * go fully to sleep until explicitly woken up.
2949 */
Mel Gorman55150612012-07-31 16:44:35 -07002950 if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08002951 trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
2952
2953 /*
2954 * vmstat counters are not perfectly accurate and the estimated
2955 * value for counters such as NR_FREE_PAGES can deviate from the
2956 * true value by nr_online_cpus * threshold. To avoid the zone
2957 * watermarks being breached while under pressure, we reduce the
2958 * per-cpu vmstat threshold while kswapd is awake and restore
2959 * them before going back to sleep.
2960 */
2961 set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
Aaditya Kumar1c7e7f62012-07-17 15:48:07 -07002962
Mel Gorman62997022012-10-08 16:32:47 -07002963 /*
2964 * Compaction records what page blocks it recently failed to
2965 * isolate pages from and skips them in the future scanning.
2966 * When kswapd is going to sleep, it is reasonable to assume
2967 * that pages and compaction may succeed so reset the cache.
2968 */
2969 reset_isolation_suitable(pgdat);
2970
Aaditya Kumar1c7e7f62012-07-17 15:48:07 -07002971 if (!kthread_should_stop())
2972 schedule();
2973
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08002974 set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
2975 } else {
2976 if (remaining)
2977 count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
2978 else
2979 count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
2980 }
2981 finish_wait(&pgdat->kswapd_wait, &wait);
2982}
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984/*
2985 * The background pageout daemon, started as a kernel thread
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002986 * from the init process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 *
2988 * This basically trickles out pages so that we have _some_
2989 * free memory available even if there is no other activity
2990 * that frees anything up. This is needed for things like routing
2991 * etc, where we otherwise might have all activity going on in
2992 * asynchronous contexts that cannot page things out.
2993 *
2994 * If there are applications that are active memory-allocators
2995 * (most normal use), this basically shouldn't matter.
2996 */
2997static int kswapd(void *p)
2998{
Mel Gorman215ddd62011-07-08 15:39:40 -07002999 unsigned long order, new_order;
Alex,Shid2ebd0f62011-10-31 17:08:39 -07003000 unsigned balanced_order;
Mel Gorman215ddd62011-07-08 15:39:40 -07003001 int classzone_idx, new_classzone_idx;
Alex,Shid2ebd0f62011-10-31 17:08:39 -07003002 int balanced_classzone_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 pg_data_t *pgdat = (pg_data_t*)p;
3004 struct task_struct *tsk = current;
KOSAKI Motohirof0bc0a62011-01-13 15:45:50 -08003005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 struct reclaim_state reclaim_state = {
3007 .reclaimed_slab = 0,
3008 };
Rusty Russella70f7302009-03-13 14:49:46 +10303009 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Nick Piggincf40bd12009-01-21 08:12:39 +01003011 lockdep_set_current_reclaim_state(GFP_KERNEL);
3012
Rusty Russell174596a2009-01-01 10:12:29 +10303013 if (!cpumask_empty(cpumask))
Mike Travisc5f59f02008-04-04 18:11:10 -07003014 set_cpus_allowed_ptr(tsk, cpumask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 current->reclaim_state = &reclaim_state;
3016
3017 /*
3018 * Tell the memory management that we're a "memory allocator",
3019 * and that if we need more memory we should get access to it
3020 * regardless (see "__alloc_pages()"). "kswapd" should
3021 * never get caught in the normal page freeing logic.
3022 *
3023 * (Kswapd normally doesn't need memory anyway, but sometimes
3024 * you need a small amount of memory in order to be able to
3025 * page out something else, and this flag essentially protects
3026 * us from recursively trying to free more memory as we're
3027 * trying to free the first piece of memory in the first place).
3028 */
Christoph Lameter930d9152006-01-08 01:00:47 -08003029 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003030 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Mel Gorman215ddd62011-07-08 15:39:40 -07003032 order = new_order = 0;
Alex,Shid2ebd0f62011-10-31 17:08:39 -07003033 balanced_order = 0;
Mel Gorman215ddd62011-07-08 15:39:40 -07003034 classzone_idx = new_classzone_idx = pgdat->nr_zones - 1;
Alex,Shid2ebd0f62011-10-31 17:08:39 -07003035 balanced_classzone_idx = classzone_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 for ( ; ; ) {
Jeff Liu6f6313d2012-12-11 16:02:48 -08003037 bool ret;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07003038
Mel Gorman215ddd62011-07-08 15:39:40 -07003039 /*
3040 * If the last balance_pgdat was unsuccessful it's unlikely a
3041 * new request of a similar or harder type will succeed soon
3042 * so consider going to sleep on the basis we reclaimed at
3043 */
Alex,Shid2ebd0f62011-10-31 17:08:39 -07003044 if (balanced_classzone_idx >= new_classzone_idx &&
3045 balanced_order == new_order) {
Mel Gorman215ddd62011-07-08 15:39:40 -07003046 new_order = pgdat->kswapd_max_order;
3047 new_classzone_idx = pgdat->classzone_idx;
3048 pgdat->kswapd_max_order = 0;
3049 pgdat->classzone_idx = pgdat->nr_zones - 1;
3050 }
3051
Mel Gorman99504742011-01-13 15:46:20 -08003052 if (order < new_order || classzone_idx > new_classzone_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 /*
3054 * Don't sleep if someone wants a larger 'order'
Mel Gorman99504742011-01-13 15:46:20 -08003055 * allocation or has tigher zone constraints
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 */
3057 order = new_order;
Mel Gorman99504742011-01-13 15:46:20 -08003058 classzone_idx = new_classzone_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 } else {
Alex,Shid2ebd0f62011-10-31 17:08:39 -07003060 kswapd_try_to_sleep(pgdat, balanced_order,
3061 balanced_classzone_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 order = pgdat->kswapd_max_order;
Mel Gorman99504742011-01-13 15:46:20 -08003063 classzone_idx = pgdat->classzone_idx;
Alex,Shif0dfcde2011-10-31 17:08:45 -07003064 new_order = order;
3065 new_classzone_idx = classzone_idx;
Mel Gorman4d405022011-01-13 15:46:23 -08003066 pgdat->kswapd_max_order = 0;
Mel Gorman215ddd62011-07-08 15:39:40 -07003067 pgdat->classzone_idx = pgdat->nr_zones - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
David Rientjes8fe23e02009-12-14 17:58:33 -08003070 ret = try_to_freeze();
3071 if (kthread_should_stop())
3072 break;
3073
3074 /*
3075 * We can speed up thawing tasks if we don't call balance_pgdat
3076 * after returning from the refrigerator
3077 */
Mel Gorman33906bc2010-08-09 17:19:16 -07003078 if (!ret) {
3079 trace_mm_vmscan_kswapd_wake(pgdat->node_id, order);
Alex,Shid2ebd0f62011-10-31 17:08:39 -07003080 balanced_classzone_idx = classzone_idx;
3081 balanced_order = balance_pgdat(pgdat, order,
3082 &balanced_classzone_idx);
Mel Gorman33906bc2010-08-09 17:19:16 -07003083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 }
Takamori Yamaguchib0a8cc52012-11-08 15:53:39 -08003085
3086 current->reclaim_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return 0;
3088}
3089
3090/*
3091 * A zone is low on free memory, so wake its kswapd task to service it.
3092 */
Mel Gorman99504742011-01-13 15:46:20 -08003093void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 pg_data_t *pgdat;
3096
Con Kolivasf3fe6512006-01-06 00:11:15 -08003097 if (!populated_zone(zone))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return;
3099
Paul Jackson02a0e532006-12-13 00:34:25 -08003100 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return;
Mel Gorman88f5acf2011-01-13 15:45:41 -08003102 pgdat = zone->zone_pgdat;
Mel Gorman99504742011-01-13 15:46:20 -08003103 if (pgdat->kswapd_max_order < order) {
Mel Gorman88f5acf2011-01-13 15:45:41 -08003104 pgdat->kswapd_max_order = order;
Mel Gorman99504742011-01-13 15:46:20 -08003105 pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx);
3106 }
Con Kolivas8d0986e2005-09-13 01:25:07 -07003107 if (!waitqueue_active(&pgdat->kswapd_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 return;
Mel Gorman88f5acf2011-01-13 15:45:41 -08003109 if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0))
3110 return;
3111
3112 trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
Con Kolivas8d0986e2005-09-13 01:25:07 -07003113 wake_up_interruptible(&pgdat->kswapd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114}
3115
Wu Fengguangadea02a2009-09-21 17:01:42 -07003116/*
3117 * The reclaimable count would be mostly accurate.
3118 * The less reclaimable pages may be
3119 * - mlocked pages, which will be moved to unevictable list when encountered
3120 * - mapped pages, which may require several travels to be reclaimed
3121 * - dirty pages, which is not "instantly" reclaimable
3122 */
3123unsigned long global_reclaimable_pages(void)
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003124{
Wu Fengguangadea02a2009-09-21 17:01:42 -07003125 int nr;
3126
3127 nr = global_page_state(NR_ACTIVE_FILE) +
3128 global_page_state(NR_INACTIVE_FILE);
3129
Shaohua Liec8acf22013-02-22 16:34:38 -08003130 if (get_nr_swap_pages() > 0)
Wu Fengguangadea02a2009-09-21 17:01:42 -07003131 nr += global_page_state(NR_ACTIVE_ANON) +
3132 global_page_state(NR_INACTIVE_ANON);
3133
3134 return nr;
3135}
3136
3137unsigned long zone_reclaimable_pages(struct zone *zone)
3138{
3139 int nr;
3140
3141 nr = zone_page_state(zone, NR_ACTIVE_FILE) +
3142 zone_page_state(zone, NR_INACTIVE_FILE);
3143
Shaohua Liec8acf22013-02-22 16:34:38 -08003144 if (get_nr_swap_pages() > 0)
Wu Fengguangadea02a2009-09-21 17:01:42 -07003145 nr += zone_page_state(zone, NR_ACTIVE_ANON) +
3146 zone_page_state(zone, NR_INACTIVE_ANON);
3147
3148 return nr;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07003149}
3150
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02003151#ifdef CONFIG_HIBERNATION
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152/*
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003153 * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003154 * freed pages.
3155 *
3156 * Rather than trying to age LRUs the aim is to preserve the overall
3157 * LRU order by reclaiming preferentially
3158 * inactive > active > active referenced > active mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 */
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003160unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003162 struct reclaim_state reclaim_state;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003163 struct scan_control sc = {
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003164 .gfp_mask = GFP_HIGHUSER_MOVABLE,
3165 .may_swap = 1,
3166 .may_unmap = 1,
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003167 .may_writepage = 1,
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003168 .nr_to_reclaim = nr_to_reclaim,
3169 .hibernation_mode = 1,
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003170 .order = 0,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003171 .priority = DEF_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 };
Ying Hana09ed5e2011-05-24 17:12:26 -07003173 struct shrink_control shrink = {
3174 .gfp_mask = sc.gfp_mask,
3175 };
3176 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003177 struct task_struct *p = current;
3178 unsigned long nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003180 p->flags |= PF_MEMALLOC;
3181 lockdep_set_current_reclaim_state(sc.gfp_mask);
3182 reclaim_state.reclaimed_slab = 0;
3183 p->reclaim_state = &reclaim_state;
Andrew Morton69e05942006-03-22 00:08:19 -08003184
Ying Hana09ed5e2011-05-24 17:12:26 -07003185 nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003186
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003187 p->reclaim_state = NULL;
3188 lockdep_clear_current_reclaim_state();
3189 p->flags &= ~PF_MEMALLOC;
Rafael J. Wysockid6277db2006-06-23 02:03:18 -07003190
KOSAKI Motohiro7b517552009-12-14 17:59:12 -08003191 return nr_reclaimed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
Rafael J. Wysockic6f37f12009-05-24 22:16:31 +02003193#endif /* CONFIG_HIBERNATION */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195/* It's optimal to keep kswapds on the same CPUs as their memory, but
3196 not required for correctness. So if the last cpu in a node goes
3197 away, we get changed to run anywhere: as the first one comes back,
3198 restore their cpu bindings. */
Greg Kroah-Hartmanfcb35a92012-12-21 15:01:06 -08003199static int cpu_callback(struct notifier_block *nfb, unsigned long action,
3200 void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
Yasunori Goto58c0a4a2007-10-16 01:25:40 -07003202 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003204 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
Lai Jiangshan48fb2e22012-12-12 13:51:43 -08003205 for_each_node_state(nid, N_MEMORY) {
Mike Travisc5f59f02008-04-04 18:11:10 -07003206 pg_data_t *pgdat = NODE_DATA(nid);
Rusty Russella70f7302009-03-13 14:49:46 +10303207 const struct cpumask *mask;
3208
3209 mask = cpumask_of_node(pgdat->node_id);
Mike Travisc5f59f02008-04-04 18:11:10 -07003210
Rusty Russell3e597942009-01-01 10:12:24 +10303211 if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 /* One of our CPUs online: restore mask */
Mike Travisc5f59f02008-04-04 18:11:10 -07003213 set_cpus_allowed_ptr(pgdat->kswapd, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 }
3215 }
3216 return NOTIFY_OK;
3217}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Yasunori Goto3218ae12006-06-27 02:53:33 -07003219/*
3220 * This kswapd start function will be called by init and node-hot-add.
3221 * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
3222 */
3223int kswapd_run(int nid)
3224{
3225 pg_data_t *pgdat = NODE_DATA(nid);
3226 int ret = 0;
3227
3228 if (pgdat->kswapd)
3229 return 0;
3230
3231 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
3232 if (IS_ERR(pgdat->kswapd)) {
3233 /* failure at boot is fatal */
3234 BUG_ON(system_state == SYSTEM_BOOTING);
Gavin Shand5dc0ad2012-10-08 16:29:27 -07003235 pr_err("Failed to start kswapd on node %d\n", nid);
3236 ret = PTR_ERR(pgdat->kswapd);
Xishi Qiud72515b2013-04-17 15:58:34 -07003237 pgdat->kswapd = NULL;
Yasunori Goto3218ae12006-06-27 02:53:33 -07003238 }
3239 return ret;
3240}
3241
David Rientjes8fe23e02009-12-14 17:58:33 -08003242/*
Jiang Liud8adde12012-07-11 14:01:52 -07003243 * Called by memory hotplug when all memory in a node is offlined. Caller must
3244 * hold lock_memory_hotplug().
David Rientjes8fe23e02009-12-14 17:58:33 -08003245 */
3246void kswapd_stop(int nid)
3247{
3248 struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
3249
Jiang Liud8adde12012-07-11 14:01:52 -07003250 if (kswapd) {
David Rientjes8fe23e02009-12-14 17:58:33 -08003251 kthread_stop(kswapd);
Jiang Liud8adde12012-07-11 14:01:52 -07003252 NODE_DATA(nid)->kswapd = NULL;
3253 }
David Rientjes8fe23e02009-12-14 17:58:33 -08003254}
3255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256static int __init kswapd_init(void)
3257{
Yasunori Goto3218ae12006-06-27 02:53:33 -07003258 int nid;
Andrew Morton69e05942006-03-22 00:08:19 -08003259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 swap_setup();
Lai Jiangshan48fb2e22012-12-12 13:51:43 -08003261 for_each_node_state(nid, N_MEMORY)
Yasunori Goto3218ae12006-06-27 02:53:33 -07003262 kswapd_run(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 hotcpu_notifier(cpu_callback, 0);
3264 return 0;
3265}
3266
3267module_init(kswapd_init)
Christoph Lameter9eeff232006-01-18 17:42:31 -08003268
3269#ifdef CONFIG_NUMA
3270/*
3271 * Zone reclaim mode
3272 *
3273 * If non-zero call zone_reclaim when the number of free pages falls below
3274 * the watermarks.
Christoph Lameter9eeff232006-01-18 17:42:31 -08003275 */
3276int zone_reclaim_mode __read_mostly;
3277
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08003278#define RECLAIM_OFF 0
Fernando Luis Vazquez Cao7d034312008-07-29 22:33:41 -07003279#define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
Christoph Lameter1b2ffb72006-02-01 03:05:34 -08003280#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
3281#define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
3282
Christoph Lameter9eeff232006-01-18 17:42:31 -08003283/*
Christoph Lametera92f7122006-02-01 03:05:32 -08003284 * Priority for ZONE_RECLAIM. This determines the fraction of pages
3285 * of a node considered for each zone_reclaim. 4 scans 1/16th of
3286 * a zone.
3287 */
3288#define ZONE_RECLAIM_PRIORITY 4
3289
Christoph Lameter9eeff232006-01-18 17:42:31 -08003290/*
Christoph Lameter96146342006-07-03 00:24:13 -07003291 * Percentage of pages in a zone that must be unmapped for zone_reclaim to
3292 * occur.
3293 */
3294int sysctl_min_unmapped_ratio = 1;
3295
3296/*
Christoph Lameter0ff38492006-09-25 23:31:52 -07003297 * If the number of slab pages in a zone grows beyond this percentage then
3298 * slab reclaim needs to occur.
3299 */
3300int sysctl_min_slab_ratio = 5;
3301
Mel Gorman90afa5d2009-06-16 15:33:20 -07003302static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
3303{
3304 unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
3305 unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
3306 zone_page_state(zone, NR_ACTIVE_FILE);
3307
3308 /*
3309 * It's possible for there to be more file mapped pages than
3310 * accounted for by the pages on the file LRU lists because
3311 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
3312 */
3313 return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
3314}
3315
3316/* Work out how many page cache pages we can reclaim in this reclaim_mode */
3317static long zone_pagecache_reclaimable(struct zone *zone)
3318{
3319 long nr_pagecache_reclaimable;
3320 long delta = 0;
3321
3322 /*
3323 * If RECLAIM_SWAP is set, then all file pages are considered
3324 * potentially reclaimable. Otherwise, we have to worry about
3325 * pages like swapcache and zone_unmapped_file_pages() provides
3326 * a better estimate
3327 */
3328 if (zone_reclaim_mode & RECLAIM_SWAP)
3329 nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
3330 else
3331 nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
3332
3333 /* If we can't clean pages, remove dirty pages from consideration */
3334 if (!(zone_reclaim_mode & RECLAIM_WRITE))
3335 delta += zone_page_state(zone, NR_FILE_DIRTY);
3336
3337 /* Watch for any possible underflows due to delta */
3338 if (unlikely(delta > nr_pagecache_reclaimable))
3339 delta = nr_pagecache_reclaimable;
3340
3341 return nr_pagecache_reclaimable - delta;
3342}
3343
Christoph Lameter0ff38492006-09-25 23:31:52 -07003344/*
Christoph Lameter9eeff232006-01-18 17:42:31 -08003345 * Try to free up some pages from this zone through reclaim.
3346 */
Andrew Morton179e9632006-03-22 00:08:18 -08003347static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
Christoph Lameter9eeff232006-01-18 17:42:31 -08003348{
Christoph Lameter7fb2d462006-03-22 00:08:22 -08003349 /* Minimum pages needed in order to stay on node */
Andrew Morton69e05942006-03-22 00:08:19 -08003350 const unsigned long nr_pages = 1 << order;
Christoph Lameter9eeff232006-01-18 17:42:31 -08003351 struct task_struct *p = current;
3352 struct reclaim_state reclaim_state;
Andrew Morton179e9632006-03-22 00:08:18 -08003353 struct scan_control sc = {
3354 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
Johannes Weinera6dc60f82009-03-31 15:19:30 -07003355 .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
KOSAKI Motohiro2e2e4252009-04-21 12:24:57 -07003356 .may_swap = 1,
Andrew Morton62b726c2013-02-22 16:32:24 -08003357 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
Ming Lei21caf2f2013-02-22 16:34:08 -08003358 .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)),
Johannes Weinerbd2f6192009-03-31 15:19:38 -07003359 .order = order,
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003360 .priority = ZONE_RECLAIM_PRIORITY,
Andrew Morton179e9632006-03-22 00:08:18 -08003361 };
Ying Hana09ed5e2011-05-24 17:12:26 -07003362 struct shrink_control shrink = {
3363 .gfp_mask = sc.gfp_mask,
3364 };
KOSAKI Motohiro15748042010-08-09 17:19:50 -07003365 unsigned long nr_slab_pages0, nr_slab_pages1;
Christoph Lameter9eeff232006-01-18 17:42:31 -08003366
Christoph Lameter9eeff232006-01-18 17:42:31 -08003367 cond_resched();
Christoph Lameterd4f77962006-02-24 13:04:22 -08003368 /*
3369 * We need to be able to allocate from the reserves for RECLAIM_SWAP
3370 * and we also need to be able to write out pages for RECLAIM_WRITE
3371 * and RECLAIM_SWAP.
3372 */
3373 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
KOSAKI Motohiro76ca5422010-03-05 13:41:47 -08003374 lockdep_set_current_reclaim_state(gfp_mask);
Christoph Lameter9eeff232006-01-18 17:42:31 -08003375 reclaim_state.reclaimed_slab = 0;
3376 p->reclaim_state = &reclaim_state;
Christoph Lameterc84db23c2006-02-01 03:05:29 -08003377
Mel Gorman90afa5d2009-06-16 15:33:20 -07003378 if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
Christoph Lameter0ff38492006-09-25 23:31:52 -07003379 /*
3380 * Free memory by calling shrink zone with increasing
3381 * priorities until we have enough memory freed.
3382 */
Christoph Lameter0ff38492006-09-25 23:31:52 -07003383 do {
Konstantin Khlebnikov9e3b2f82012-05-29 15:06:57 -07003384 shrink_zone(zone, &sc);
3385 } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
Christoph Lameter0ff38492006-09-25 23:31:52 -07003386 }
Christoph Lameterc84db23c2006-02-01 03:05:29 -08003387
KOSAKI Motohiro15748042010-08-09 17:19:50 -07003388 nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
3389 if (nr_slab_pages0 > zone->min_slab_pages) {
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08003390 /*
Christoph Lameter7fb2d462006-03-22 00:08:22 -08003391 * shrink_slab() does not currently allow us to determine how
Christoph Lameter0ff38492006-09-25 23:31:52 -07003392 * many pages were freed in this zone. So we take the current
3393 * number of slab pages and shake the slab until it is reduced
3394 * by the same nr_pages that we used for reclaiming unmapped
3395 * pages.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08003396 *
Christoph Lameter0ff38492006-09-25 23:31:52 -07003397 * Note that shrink_slab will free memory on all zones and may
3398 * take a long time.
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08003399 */
KOSAKI Motohiro4dc4b3d2010-08-09 17:19:54 -07003400 for (;;) {
3401 unsigned long lru_pages = zone_reclaimable_pages(zone);
3402
3403 /* No reclaimable slab or very low memory pressure */
Ying Han1495f232011-05-24 17:12:27 -07003404 if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages))
KOSAKI Motohiro4dc4b3d2010-08-09 17:19:54 -07003405 break;
3406
3407 /* Freed enough memory */
3408 nr_slab_pages1 = zone_page_state(zone,
3409 NR_SLAB_RECLAIMABLE);
3410 if (nr_slab_pages1 + nr_pages <= nr_slab_pages0)
3411 break;
3412 }
Christoph Lameter83e33a42006-09-25 23:31:53 -07003413
3414 /*
3415 * Update nr_reclaimed by the number of slab pages we
3416 * reclaimed from this zone.
3417 */
KOSAKI Motohiro15748042010-08-09 17:19:50 -07003418 nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
3419 if (nr_slab_pages1 < nr_slab_pages0)
3420 sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1;
Christoph Lameter2a16e3f2006-02-01 03:05:35 -08003421 }
3422
Christoph Lameter9eeff232006-01-18 17:42:31 -08003423 p->reclaim_state = NULL;
Christoph Lameterd4f77962006-02-24 13:04:22 -08003424 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
KOSAKI Motohiro76ca5422010-03-05 13:41:47 -08003425 lockdep_clear_current_reclaim_state();
Rik van Riela79311c2009-01-06 14:40:01 -08003426 return sc.nr_reclaimed >= nr_pages;
Christoph Lameter9eeff232006-01-18 17:42:31 -08003427}
Andrew Morton179e9632006-03-22 00:08:18 -08003428
3429int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
3430{
Andrew Morton179e9632006-03-22 00:08:18 -08003431 int node_id;
David Rientjesd773ed62007-10-16 23:26:01 -07003432 int ret;
Andrew Morton179e9632006-03-22 00:08:18 -08003433
3434 /*
Christoph Lameter0ff38492006-09-25 23:31:52 -07003435 * Zone reclaim reclaims unmapped file backed pages and
3436 * slab pages if we are over the defined limits.
Christoph Lameter34aa1332006-06-30 01:55:37 -07003437 *
Christoph Lameter96146342006-07-03 00:24:13 -07003438 * A small portion of unmapped file backed pages is needed for
3439 * file I/O otherwise pages read by file I/O will be immediately
3440 * thrown out if the zone is overallocated. So we do not reclaim
3441 * if less than a specified percentage of the zone is used by
3442 * unmapped file backed pages.
Andrew Morton179e9632006-03-22 00:08:18 -08003443 */
Mel Gorman90afa5d2009-06-16 15:33:20 -07003444 if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
3445 zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
Mel Gormanfa5e0842009-06-16 15:33:22 -07003446 return ZONE_RECLAIM_FULL;
Andrew Morton179e9632006-03-22 00:08:18 -08003447
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -08003448 if (zone->all_unreclaimable)
Mel Gormanfa5e0842009-06-16 15:33:22 -07003449 return ZONE_RECLAIM_FULL;
David Rientjesd773ed62007-10-16 23:26:01 -07003450
Andrew Morton179e9632006-03-22 00:08:18 -08003451 /*
David Rientjesd773ed62007-10-16 23:26:01 -07003452 * Do not scan if the allocation should not be delayed.
Andrew Morton179e9632006-03-22 00:08:18 -08003453 */
David Rientjesd773ed62007-10-16 23:26:01 -07003454 if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
Mel Gormanfa5e0842009-06-16 15:33:22 -07003455 return ZONE_RECLAIM_NOSCAN;
Andrew Morton179e9632006-03-22 00:08:18 -08003456
3457 /*
3458 * Only run zone reclaim on the local zone or on zones that do not
3459 * have associated processors. This will favor the local processor
3460 * over remote processors and spread off node memory allocations
3461 * as wide as possible.
3462 */
Christoph Lameter89fa3022006-09-25 23:31:55 -07003463 node_id = zone_to_nid(zone);
Christoph Lameter37c07082007-10-16 01:25:36 -07003464 if (node_state(node_id, N_CPU) && node_id != numa_node_id())
Mel Gormanfa5e0842009-06-16 15:33:22 -07003465 return ZONE_RECLAIM_NOSCAN;
David Rientjesd773ed62007-10-16 23:26:01 -07003466
3467 if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
Mel Gormanfa5e0842009-06-16 15:33:22 -07003468 return ZONE_RECLAIM_NOSCAN;
3469
David Rientjesd773ed62007-10-16 23:26:01 -07003470 ret = __zone_reclaim(zone, gfp_mask, order);
3471 zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
3472
Mel Gorman24cf725182009-06-16 15:33:23 -07003473 if (!ret)
3474 count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
3475
David Rientjesd773ed62007-10-16 23:26:01 -07003476 return ret;
Andrew Morton179e9632006-03-22 00:08:18 -08003477}
Christoph Lameter9eeff232006-01-18 17:42:31 -08003478#endif
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003479
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003480/*
3481 * page_evictable - test whether a page is evictable
3482 * @page: the page to test
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003483 *
3484 * Test whether page is evictable--i.e., should be placed on active/inactive
Hugh Dickins39b5f292012-10-08 16:33:18 -07003485 * lists vs unevictable list.
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003486 *
3487 * Reasons page might not be evictable:
Lee Schermerhornba9ddf42008-10-18 20:26:42 -07003488 * (1) page's mapping marked unevictable
Nick Pigginb291f002008-10-18 20:26:44 -07003489 * (2) page is part of an mlocked VMA
Lee Schermerhornba9ddf42008-10-18 20:26:42 -07003490 *
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003491 */
Hugh Dickins39b5f292012-10-08 16:33:18 -07003492int page_evictable(struct page *page)
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003493{
Hugh Dickins39b5f292012-10-08 16:33:18 -07003494 return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -07003495}
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003496
Hugh Dickins850465792012-01-20 14:34:19 -08003497#ifdef CONFIG_SHMEM
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003498/**
Hugh Dickins24513262012-01-20 14:34:21 -08003499 * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list
3500 * @pages: array of pages to check
3501 * @nr_pages: number of pages to check
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003502 *
Hugh Dickins24513262012-01-20 14:34:21 -08003503 * Checks pages for evictability and moves them to the appropriate lru list.
Hugh Dickins850465792012-01-20 14:34:19 -08003504 *
3505 * This function is only used for SysV IPC SHM_UNLOCK.
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003506 */
Hugh Dickins24513262012-01-20 14:34:21 -08003507void check_move_unevictable_pages(struct page **pages, int nr_pages)
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003508{
Johannes Weiner925b7672012-01-12 17:18:15 -08003509 struct lruvec *lruvec;
Hugh Dickins24513262012-01-20 14:34:21 -08003510 struct zone *zone = NULL;
3511 int pgscanned = 0;
3512 int pgrescued = 0;
3513 int i;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003514
Hugh Dickins24513262012-01-20 14:34:21 -08003515 for (i = 0; i < nr_pages; i++) {
3516 struct page *page = pages[i];
3517 struct zone *pagezone;
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003518
Hugh Dickins24513262012-01-20 14:34:21 -08003519 pgscanned++;
3520 pagezone = page_zone(page);
3521 if (pagezone != zone) {
3522 if (zone)
3523 spin_unlock_irq(&zone->lru_lock);
3524 zone = pagezone;
3525 spin_lock_irq(&zone->lru_lock);
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003526 }
Hugh Dickinsfa9add62012-05-29 15:07:09 -07003527 lruvec = mem_cgroup_page_lruvec(page, zone);
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003528
Hugh Dickins24513262012-01-20 14:34:21 -08003529 if (!PageLRU(page) || !PageUnevictable(page))
3530 continue;
3531
Hugh Dickins39b5f292012-10-08 16:33:18 -07003532 if (page_evictable(page)) {
Hugh Dickins24513262012-01-20 14:34:21 -08003533 enum lru_list lru = page_lru_base_type(page);
3534
3535 VM_BUG_ON(PageActive(page));
3536 ClearPageUnevictable(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07003537 del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
3538 add_page_to_lru_list(page, lruvec, lru);
Hugh Dickins24513262012-01-20 14:34:21 -08003539 pgrescued++;
3540 }
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07003541 }
Hugh Dickins24513262012-01-20 14:34:21 -08003542
3543 if (zone) {
3544 __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
3545 __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
3546 spin_unlock_irq(&zone->lru_lock);
3547 }
Hugh Dickins850465792012-01-20 14:34:19 -08003548}
3549#endif /* CONFIG_SHMEM */
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003550
Johannes Weiner264e56d2011-10-31 17:09:13 -07003551static void warn_scan_unevictable_pages(void)
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003552{
Johannes Weiner264e56d2011-10-31 17:09:13 -07003553 printk_once(KERN_WARNING
KOSAKI Motohiro25bd91b2012-01-10 15:07:40 -08003554 "%s: The scan_unevictable_pages sysctl/node-interface has been "
Johannes Weiner264e56d2011-10-31 17:09:13 -07003555 "disabled for lack of a legitimate use case. If you have "
KOSAKI Motohiro25bd91b2012-01-10 15:07:40 -08003556 "one, please send an email to linux-mm@kvack.org.\n",
3557 current->comm);
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003558}
3559
3560/*
3561 * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of
3562 * all nodes' unevictable lists for evictable pages
3563 */
3564unsigned long scan_unevictable_pages;
3565
3566int scan_unevictable_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003567 void __user *buffer,
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003568 size_t *length, loff_t *ppos)
3569{
Johannes Weiner264e56d2011-10-31 17:09:13 -07003570 warn_scan_unevictable_pages();
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003571 proc_doulongvec_minmax(table, write, buffer, length, ppos);
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003572 scan_unevictable_pages = 0;
3573 return 0;
3574}
3575
Thadeu Lima de Souza Cascardoe4455ab2010-10-26 14:21:28 -07003576#ifdef CONFIG_NUMA
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003577/*
3578 * per node 'scan_unevictable_pages' attribute. On demand re-scan of
3579 * a specified node's per zone unevictable lists for evictable pages.
3580 */
3581
Kay Sievers10fbcf42011-12-21 14:48:43 -08003582static ssize_t read_scan_unevictable_node(struct device *dev,
3583 struct device_attribute *attr,
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003584 char *buf)
3585{
Johannes Weiner264e56d2011-10-31 17:09:13 -07003586 warn_scan_unevictable_pages();
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003587 return sprintf(buf, "0\n"); /* always zero; should fit... */
3588}
3589
Kay Sievers10fbcf42011-12-21 14:48:43 -08003590static ssize_t write_scan_unevictable_node(struct device *dev,
3591 struct device_attribute *attr,
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003592 const char *buf, size_t count)
3593{
Johannes Weiner264e56d2011-10-31 17:09:13 -07003594 warn_scan_unevictable_pages();
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003595 return 1;
3596}
3597
3598
Kay Sievers10fbcf42011-12-21 14:48:43 -08003599static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003600 read_scan_unevictable_node,
3601 write_scan_unevictable_node);
3602
3603int scan_unevictable_register_node(struct node *node)
3604{
Kay Sievers10fbcf42011-12-21 14:48:43 -08003605 return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages);
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003606}
3607
3608void scan_unevictable_unregister_node(struct node *node)
3609{
Kay Sievers10fbcf42011-12-21 14:48:43 -08003610 device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages);
Lee Schermerhornaf936a12008-10-18 20:26:53 -07003611}
Thadeu Lima de Souza Cascardoe4455ab2010-10-26 14:21:28 -07003612#endif