xref: /openbmc/linux/mm/vmscan.c (revision 68243e76ee343d63c6cf76978588a885951e2818)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/vmscan.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  Swap reorganised 29.12.95, Stephen Tweedie.
71da177e4SLinus Torvalds  *  kswapd added: 7.1.96  sct
81da177e4SLinus Torvalds  *  Removed kswapd_ctl limits, and swap out as many pages as needed
91da177e4SLinus Torvalds  *  to bring the system back to freepages.high: 2.4.97, Rik van Riel.
101da177e4SLinus Torvalds  *  Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
111da177e4SLinus Torvalds  *  Multiqueue VM started 5.8.00, Rik van Riel.
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/mm.h>
151da177e4SLinus Torvalds #include <linux/module.h>
165a0e3ad6STejun Heo #include <linux/gfp.h>
171da177e4SLinus Torvalds #include <linux/kernel_stat.h>
181da177e4SLinus Torvalds #include <linux/swap.h>
191da177e4SLinus Torvalds #include <linux/pagemap.h>
201da177e4SLinus Torvalds #include <linux/init.h>
211da177e4SLinus Torvalds #include <linux/highmem.h>
22e129b5c2SAndrew Morton #include <linux/vmstat.h>
231da177e4SLinus Torvalds #include <linux/file.h>
241da177e4SLinus Torvalds #include <linux/writeback.h>
251da177e4SLinus Torvalds #include <linux/blkdev.h>
261da177e4SLinus Torvalds #include <linux/buffer_head.h>	/* for try_to_release_page(),
271da177e4SLinus Torvalds 					buffer_heads_over_limit */
281da177e4SLinus Torvalds #include <linux/mm_inline.h>
291da177e4SLinus Torvalds #include <linux/backing-dev.h>
301da177e4SLinus Torvalds #include <linux/rmap.h>
311da177e4SLinus Torvalds #include <linux/topology.h>
321da177e4SLinus Torvalds #include <linux/cpu.h>
331da177e4SLinus Torvalds #include <linux/cpuset.h>
343e7d3449SMel Gorman #include <linux/compaction.h>
351da177e4SLinus Torvalds #include <linux/notifier.h>
361da177e4SLinus Torvalds #include <linux/rwsem.h>
37248a0301SRafael J. Wysocki #include <linux/delay.h>
383218ae14SYasunori Goto #include <linux/kthread.h>
397dfb7103SNigel Cunningham #include <linux/freezer.h>
4066e1707bSBalbir Singh #include <linux/memcontrol.h>
41873b4771SKeika Kobayashi #include <linux/delayacct.h>
42af936a16SLee Schermerhorn #include <linux/sysctl.h>
43929bea7cSKOSAKI Motohiro #include <linux/oom.h>
44268bb0ceSLinus Torvalds #include <linux/prefetch.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include <asm/tlbflush.h>
471da177e4SLinus Torvalds #include <asm/div64.h>
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #include <linux/swapops.h>
501da177e4SLinus Torvalds 
510f8053a5SNick Piggin #include "internal.h"
520f8053a5SNick Piggin 
5333906bc5SMel Gorman #define CREATE_TRACE_POINTS
5433906bc5SMel Gorman #include <trace/events/vmscan.h>
5533906bc5SMel Gorman 
561da177e4SLinus Torvalds struct scan_control {
571da177e4SLinus Torvalds 	/* Incremented by the number of inactive pages that were scanned */
581da177e4SLinus Torvalds 	unsigned long nr_scanned;
591da177e4SLinus Torvalds 
60a79311c1SRik van Riel 	/* Number of pages freed so far during a call to shrink_zones() */
61a79311c1SRik van Riel 	unsigned long nr_reclaimed;
62a79311c1SRik van Riel 
6322fba335SKOSAKI Motohiro 	/* How many pages shrink_list() should reclaim */
6422fba335SKOSAKI Motohiro 	unsigned long nr_to_reclaim;
6522fba335SKOSAKI Motohiro 
667b51755cSKOSAKI Motohiro 	unsigned long hibernation_mode;
677b51755cSKOSAKI Motohiro 
681da177e4SLinus Torvalds 	/* This context's GFP mask */
696daa0e28SAl Viro 	gfp_t gfp_mask;
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	int may_writepage;
721da177e4SLinus Torvalds 
73a6dc60f8SJohannes Weiner 	/* Can mapped pages be reclaimed? */
74a6dc60f8SJohannes Weiner 	int may_unmap;
75f1fd1067SChristoph Lameter 
762e2e4259SKOSAKI Motohiro 	/* Can pages be swapped as part of reclaim? */
772e2e4259SKOSAKI Motohiro 	int may_swap;
782e2e4259SKOSAKI Motohiro 
795ad333ebSAndy Whitcroft 	int order;
8066e1707bSBalbir Singh 
819e3b2f8cSKonstantin Khlebnikov 	/* Scan (total_size >> priority) pages at once */
829e3b2f8cSKonstantin Khlebnikov 	int priority;
839e3b2f8cSKonstantin Khlebnikov 
845f53e762SKOSAKI Motohiro 	/*
85f16015fbSJohannes Weiner 	 * The memory cgroup that hit its limit and as a result is the
86f16015fbSJohannes Weiner 	 * primary target of this reclaim invocation.
87f16015fbSJohannes Weiner 	 */
88f16015fbSJohannes Weiner 	struct mem_cgroup *target_mem_cgroup;
8966e1707bSBalbir Singh 
90327c0e96SKAMEZAWA Hiroyuki 	/*
91327c0e96SKAMEZAWA Hiroyuki 	 * Nodemask of nodes allowed by the caller. If NULL, all nodes
92327c0e96SKAMEZAWA Hiroyuki 	 * are scanned.
93327c0e96SKAMEZAWA Hiroyuki 	 */
94327c0e96SKAMEZAWA Hiroyuki 	nodemask_t	*nodemask;
951da177e4SLinus Torvalds };
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH
1001da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field)			\
1011da177e4SLinus Torvalds 	do {								\
1021da177e4SLinus Torvalds 		if ((_page)->lru.prev != _base) {			\
1031da177e4SLinus Torvalds 			struct page *prev;				\
1041da177e4SLinus Torvalds 									\
1051da177e4SLinus Torvalds 			prev = lru_to_page(&(_page->lru));		\
1061da177e4SLinus Torvalds 			prefetch(&prev->_field);			\
1071da177e4SLinus Torvalds 		}							\
1081da177e4SLinus Torvalds 	} while (0)
1091da177e4SLinus Torvalds #else
1101da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
1111da177e4SLinus Torvalds #endif
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW
1141da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field)			\
1151da177e4SLinus Torvalds 	do {								\
1161da177e4SLinus Torvalds 		if ((_page)->lru.prev != _base) {			\
1171da177e4SLinus Torvalds 			struct page *prev;				\
1181da177e4SLinus Torvalds 									\
1191da177e4SLinus Torvalds 			prev = lru_to_page(&(_page->lru));		\
1201da177e4SLinus Torvalds 			prefetchw(&prev->_field);			\
1211da177e4SLinus Torvalds 		}							\
1221da177e4SLinus Torvalds 	} while (0)
1231da177e4SLinus Torvalds #else
1241da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
1251da177e4SLinus Torvalds #endif
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /*
1281da177e4SLinus Torvalds  * From 0 .. 100.  Higher means more swappy.
1291da177e4SLinus Torvalds  */
1301da177e4SLinus Torvalds int vm_swappiness = 60;
131bd1e22b8SAndrew Morton long vm_total_pages;	/* The total number of pages which the VM controls */
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static LIST_HEAD(shrinker_list);
1341da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem);
1351da177e4SLinus Torvalds 
136c255a458SAndrew Morton #ifdef CONFIG_MEMCG
13789b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc)
13889b5fae5SJohannes Weiner {
139f16015fbSJohannes Weiner 	return !sc->target_mem_cgroup;
14089b5fae5SJohannes Weiner }
14191a45470SKAMEZAWA Hiroyuki #else
14289b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc)
14389b5fae5SJohannes Weiner {
14489b5fae5SJohannes Weiner 	return true;
14589b5fae5SJohannes Weiner }
14691a45470SKAMEZAWA Hiroyuki #endif
14791a45470SKAMEZAWA Hiroyuki 
1484d7dcca2SHugh Dickins static unsigned long get_lru_size(struct lruvec *lruvec, enum lru_list lru)
149c9f299d9SKOSAKI Motohiro {
150c3c787e8SHugh Dickins 	if (!mem_cgroup_disabled())
1514d7dcca2SHugh Dickins 		return mem_cgroup_get_lru_size(lruvec, lru);
152a3d8e054SKOSAKI Motohiro 
153074291feSKonstantin Khlebnikov 	return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru);
154c9f299d9SKOSAKI Motohiro }
155c9f299d9SKOSAKI Motohiro 
1561da177e4SLinus Torvalds /*
1571da177e4SLinus Torvalds  * Add a shrinker callback to be called from the vm
1581da177e4SLinus Torvalds  */
1598e1f936bSRusty Russell void register_shrinker(struct shrinker *shrinker)
1601da177e4SLinus Torvalds {
16183aeeadaSKonstantin Khlebnikov 	atomic_long_set(&shrinker->nr_in_batch, 0);
1621da177e4SLinus Torvalds 	down_write(&shrinker_rwsem);
1631da177e4SLinus Torvalds 	list_add_tail(&shrinker->list, &shrinker_list);
1641da177e4SLinus Torvalds 	up_write(&shrinker_rwsem);
1651da177e4SLinus Torvalds }
1668e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker);
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds /*
1691da177e4SLinus Torvalds  * Remove one
1701da177e4SLinus Torvalds  */
1718e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds 	down_write(&shrinker_rwsem);
1741da177e4SLinus Torvalds 	list_del(&shrinker->list);
1751da177e4SLinus Torvalds 	up_write(&shrinker_rwsem);
1761da177e4SLinus Torvalds }
1778e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker);
1781da177e4SLinus Torvalds 
1791495f230SYing Han static inline int do_shrinker_shrink(struct shrinker *shrinker,
1801495f230SYing Han 				     struct shrink_control *sc,
1811495f230SYing Han 				     unsigned long nr_to_scan)
1821495f230SYing Han {
1831495f230SYing Han 	sc->nr_to_scan = nr_to_scan;
1841495f230SYing Han 	return (*shrinker->shrink)(shrinker, sc);
1851495f230SYing Han }
1861495f230SYing Han 
1871da177e4SLinus Torvalds #define SHRINK_BATCH 128
1881da177e4SLinus Torvalds /*
1891da177e4SLinus Torvalds  * Call the shrink functions to age shrinkable caches
1901da177e4SLinus Torvalds  *
1911da177e4SLinus Torvalds  * Here we assume it costs one seek to replace a lru page and that it also
1921da177e4SLinus Torvalds  * takes a seek to recreate a cache object.  With this in mind we age equal
1931da177e4SLinus Torvalds  * percentages of the lru and ageable caches.  This should balance the seeks
1941da177e4SLinus Torvalds  * generated by these structures.
1951da177e4SLinus Torvalds  *
196183ff22bSSimon Arlott  * If the vm encountered mapped pages on the LRU it increase the pressure on
1971da177e4SLinus Torvalds  * slab to avoid swapping.
1981da177e4SLinus Torvalds  *
1991da177e4SLinus Torvalds  * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
2001da177e4SLinus Torvalds  *
2011da177e4SLinus Torvalds  * `lru_pages' represents the number of on-LRU pages in all the zones which
2021da177e4SLinus Torvalds  * are eligible for the caller's allocation attempt.  It is used for balancing
2031da177e4SLinus Torvalds  * slab reclaim versus page reclaim.
204b15e0905Sakpm@osdl.org  *
205b15e0905Sakpm@osdl.org  * Returns the number of slab objects which we shrunk.
2061da177e4SLinus Torvalds  */
207a09ed5e0SYing Han unsigned long shrink_slab(struct shrink_control *shrink,
2081495f230SYing Han 			  unsigned long nr_pages_scanned,
20969e05944SAndrew Morton 			  unsigned long lru_pages)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	struct shrinker *shrinker;
21269e05944SAndrew Morton 	unsigned long ret = 0;
2131da177e4SLinus Torvalds 
2141495f230SYing Han 	if (nr_pages_scanned == 0)
2151495f230SYing Han 		nr_pages_scanned = SWAP_CLUSTER_MAX;
2161da177e4SLinus Torvalds 
217f06590bdSMinchan Kim 	if (!down_read_trylock(&shrinker_rwsem)) {
218f06590bdSMinchan Kim 		/* Assume we'll be able to shrink next time */
219f06590bdSMinchan Kim 		ret = 1;
220f06590bdSMinchan Kim 		goto out;
221f06590bdSMinchan Kim 	}
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	list_for_each_entry(shrinker, &shrinker_list, list) {
2241da177e4SLinus Torvalds 		unsigned long long delta;
225635697c6SKonstantin Khlebnikov 		long total_scan;
226635697c6SKonstantin Khlebnikov 		long max_pass;
22709576073SDave Chinner 		int shrink_ret = 0;
228acf92b48SDave Chinner 		long nr;
229acf92b48SDave Chinner 		long new_nr;
230e9299f50SDave Chinner 		long batch_size = shrinker->batch ? shrinker->batch
231e9299f50SDave Chinner 						  : SHRINK_BATCH;
2321da177e4SLinus Torvalds 
233635697c6SKonstantin Khlebnikov 		max_pass = do_shrinker_shrink(shrinker, shrink, 0);
234635697c6SKonstantin Khlebnikov 		if (max_pass <= 0)
235635697c6SKonstantin Khlebnikov 			continue;
236635697c6SKonstantin Khlebnikov 
237acf92b48SDave Chinner 		/*
238acf92b48SDave Chinner 		 * copy the current shrinker scan count into a local variable
239acf92b48SDave Chinner 		 * and zero it so that other concurrent shrinker invocations
240acf92b48SDave Chinner 		 * don't also do this scanning work.
241acf92b48SDave Chinner 		 */
24283aeeadaSKonstantin Khlebnikov 		nr = atomic_long_xchg(&shrinker->nr_in_batch, 0);
243acf92b48SDave Chinner 
244acf92b48SDave Chinner 		total_scan = nr;
2451495f230SYing Han 		delta = (4 * nr_pages_scanned) / shrinker->seeks;
246ea164d73SAndrea Arcangeli 		delta *= max_pass;
2471da177e4SLinus Torvalds 		do_div(delta, lru_pages + 1);
248acf92b48SDave Chinner 		total_scan += delta;
249acf92b48SDave Chinner 		if (total_scan < 0) {
25088c3bd70SDavid Rientjes 			printk(KERN_ERR "shrink_slab: %pF negative objects to "
25188c3bd70SDavid Rientjes 			       "delete nr=%ld\n",
252acf92b48SDave Chinner 			       shrinker->shrink, total_scan);
253acf92b48SDave Chinner 			total_scan = max_pass;
254ea164d73SAndrea Arcangeli 		}
255ea164d73SAndrea Arcangeli 
256ea164d73SAndrea Arcangeli 		/*
2573567b59aSDave Chinner 		 * We need to avoid excessive windup on filesystem shrinkers
2583567b59aSDave Chinner 		 * due to large numbers of GFP_NOFS allocations causing the
2593567b59aSDave Chinner 		 * shrinkers to return -1 all the time. This results in a large
2603567b59aSDave Chinner 		 * nr being built up so when a shrink that can do some work
2613567b59aSDave Chinner 		 * comes along it empties the entire cache due to nr >>>
2623567b59aSDave Chinner 		 * max_pass.  This is bad for sustaining a working set in
2633567b59aSDave Chinner 		 * memory.
2643567b59aSDave Chinner 		 *
2653567b59aSDave Chinner 		 * Hence only allow the shrinker to scan the entire cache when
2663567b59aSDave Chinner 		 * a large delta change is calculated directly.
2673567b59aSDave Chinner 		 */
2683567b59aSDave Chinner 		if (delta < max_pass / 4)
2693567b59aSDave Chinner 			total_scan = min(total_scan, max_pass / 2);
2703567b59aSDave Chinner 
2713567b59aSDave Chinner 		/*
272ea164d73SAndrea Arcangeli 		 * Avoid risking looping forever due to too large nr value:
273ea164d73SAndrea Arcangeli 		 * never try to free more than twice the estimate number of
274ea164d73SAndrea Arcangeli 		 * freeable entries.
275ea164d73SAndrea Arcangeli 		 */
276acf92b48SDave Chinner 		if (total_scan > max_pass * 2)
277acf92b48SDave Chinner 			total_scan = max_pass * 2;
2781da177e4SLinus Torvalds 
279acf92b48SDave Chinner 		trace_mm_shrink_slab_start(shrinker, shrink, nr,
28009576073SDave Chinner 					nr_pages_scanned, lru_pages,
28109576073SDave Chinner 					max_pass, delta, total_scan);
28209576073SDave Chinner 
283e9299f50SDave Chinner 		while (total_scan >= batch_size) {
284b15e0905Sakpm@osdl.org 			int nr_before;
2851da177e4SLinus Torvalds 
2861495f230SYing Han 			nr_before = do_shrinker_shrink(shrinker, shrink, 0);
2871495f230SYing Han 			shrink_ret = do_shrinker_shrink(shrinker, shrink,
288e9299f50SDave Chinner 							batch_size);
2891da177e4SLinus Torvalds 			if (shrink_ret == -1)
2901da177e4SLinus Torvalds 				break;
291b15e0905Sakpm@osdl.org 			if (shrink_ret < nr_before)
292b15e0905Sakpm@osdl.org 				ret += nr_before - shrink_ret;
293e9299f50SDave Chinner 			count_vm_events(SLABS_SCANNED, batch_size);
294e9299f50SDave Chinner 			total_scan -= batch_size;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 			cond_resched();
2971da177e4SLinus Torvalds 		}
2981da177e4SLinus Torvalds 
299acf92b48SDave Chinner 		/*
300acf92b48SDave Chinner 		 * move the unused scan count back into the shrinker in a
301acf92b48SDave Chinner 		 * manner that handles concurrent updates. If we exhausted the
302acf92b48SDave Chinner 		 * scan, there is no need to do an update.
303acf92b48SDave Chinner 		 */
30483aeeadaSKonstantin Khlebnikov 		if (total_scan > 0)
30583aeeadaSKonstantin Khlebnikov 			new_nr = atomic_long_add_return(total_scan,
30683aeeadaSKonstantin Khlebnikov 					&shrinker->nr_in_batch);
30783aeeadaSKonstantin Khlebnikov 		else
30883aeeadaSKonstantin Khlebnikov 			new_nr = atomic_long_read(&shrinker->nr_in_batch);
309acf92b48SDave Chinner 
310acf92b48SDave Chinner 		trace_mm_shrink_slab_end(shrinker, shrink_ret, nr, new_nr);
3111da177e4SLinus Torvalds 	}
3121da177e4SLinus Torvalds 	up_read(&shrinker_rwsem);
313f06590bdSMinchan Kim out:
314f06590bdSMinchan Kim 	cond_resched();
315b15e0905Sakpm@osdl.org 	return ret;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page)
3191da177e4SLinus Torvalds {
320ceddc3a5SJohannes Weiner 	/*
321ceddc3a5SJohannes Weiner 	 * A freeable page cache page is referenced only by the caller
322ceddc3a5SJohannes Weiner 	 * that isolated the page, the page cache radix tree and
323ceddc3a5SJohannes Weiner 	 * optional buffer heads at page->private.
324ceddc3a5SJohannes Weiner 	 */
325edcf4748SJohannes Weiner 	return page_count(page) - page_has_private(page) == 2;
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
3287d3579e8SKOSAKI Motohiro static int may_write_to_queue(struct backing_dev_info *bdi,
3297d3579e8SKOSAKI Motohiro 			      struct scan_control *sc)
3301da177e4SLinus Torvalds {
331930d9152SChristoph Lameter 	if (current->flags & PF_SWAPWRITE)
3321da177e4SLinus Torvalds 		return 1;
3331da177e4SLinus Torvalds 	if (!bdi_write_congested(bdi))
3341da177e4SLinus Torvalds 		return 1;
3351da177e4SLinus Torvalds 	if (bdi == current->backing_dev_info)
3361da177e4SLinus Torvalds 		return 1;
3371da177e4SLinus Torvalds 	return 0;
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds /*
3411da177e4SLinus Torvalds  * We detected a synchronous write error writing a page out.  Probably
3421da177e4SLinus Torvalds  * -ENOSPC.  We need to propagate that into the address_space for a subsequent
3431da177e4SLinus Torvalds  * fsync(), msync() or close().
3441da177e4SLinus Torvalds  *
3451da177e4SLinus Torvalds  * The tricky part is that after writepage we cannot touch the mapping: nothing
3461da177e4SLinus Torvalds  * prevents it from being freed up.  But we have a ref on the page and once
3471da177e4SLinus Torvalds  * that page is locked, the mapping is pinned.
3481da177e4SLinus Torvalds  *
3491da177e4SLinus Torvalds  * We're allowed to run sleeping lock_page() here because we know the caller has
3501da177e4SLinus Torvalds  * __GFP_FS.
3511da177e4SLinus Torvalds  */
3521da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping,
3531da177e4SLinus Torvalds 				struct page *page, int error)
3541da177e4SLinus Torvalds {
3557eaceaccSJens Axboe 	lock_page(page);
3563e9f45bdSGuillaume Chazarain 	if (page_mapping(page) == mapping)
3573e9f45bdSGuillaume Chazarain 		mapping_set_error(mapping, error);
3581da177e4SLinus Torvalds 	unlock_page(page);
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
36104e62a29SChristoph Lameter /* possible outcome of pageout() */
36204e62a29SChristoph Lameter typedef enum {
36304e62a29SChristoph Lameter 	/* failed to write page out, page is locked */
36404e62a29SChristoph Lameter 	PAGE_KEEP,
36504e62a29SChristoph Lameter 	/* move page to the active list, page is locked */
36604e62a29SChristoph Lameter 	PAGE_ACTIVATE,
36704e62a29SChristoph Lameter 	/* page has been sent to the disk successfully, page is unlocked */
36804e62a29SChristoph Lameter 	PAGE_SUCCESS,
36904e62a29SChristoph Lameter 	/* page is clean and locked */
37004e62a29SChristoph Lameter 	PAGE_CLEAN,
37104e62a29SChristoph Lameter } pageout_t;
37204e62a29SChristoph Lameter 
3731da177e4SLinus Torvalds /*
3741742f19fSAndrew Morton  * pageout is called by shrink_page_list() for each dirty page.
3751742f19fSAndrew Morton  * Calls ->writepage().
3761da177e4SLinus Torvalds  */
377c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping,
3787d3579e8SKOSAKI Motohiro 			 struct scan_control *sc)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	/*
3811da177e4SLinus Torvalds 	 * If the page is dirty, only perform writeback if that write
3821da177e4SLinus Torvalds 	 * will be non-blocking.  To prevent this allocation from being
3831da177e4SLinus Torvalds 	 * stalled by pagecache activity.  But note that there may be
3841da177e4SLinus Torvalds 	 * stalls if we need to run get_block().  We could test
3851da177e4SLinus Torvalds 	 * PagePrivate for that.
3861da177e4SLinus Torvalds 	 *
3876aceb53bSVincent Li 	 * If this process is currently in __generic_file_aio_write() against
3881da177e4SLinus Torvalds 	 * this page's queue, we can perform writeback even if that
3891da177e4SLinus Torvalds 	 * will block.
3901da177e4SLinus Torvalds 	 *
3911da177e4SLinus Torvalds 	 * If the page is swapcache, write it back even if that would
3921da177e4SLinus Torvalds 	 * block, for some throttling. This happens by accident, because
3931da177e4SLinus Torvalds 	 * swap_backing_dev_info is bust: it doesn't reflect the
3941da177e4SLinus Torvalds 	 * congestion state of the swapdevs.  Easy to fix, if needed.
3951da177e4SLinus Torvalds 	 */
3961da177e4SLinus Torvalds 	if (!is_page_cache_freeable(page))
3971da177e4SLinus Torvalds 		return PAGE_KEEP;
3981da177e4SLinus Torvalds 	if (!mapping) {
3991da177e4SLinus Torvalds 		/*
4001da177e4SLinus Torvalds 		 * Some data journaling orphaned pages can have
4011da177e4SLinus Torvalds 		 * page->mapping == NULL while being dirty with clean buffers.
4021da177e4SLinus Torvalds 		 */
403266cf658SDavid Howells 		if (page_has_private(page)) {
4041da177e4SLinus Torvalds 			if (try_to_free_buffers(page)) {
4051da177e4SLinus Torvalds 				ClearPageDirty(page);
406d40cee24SHarvey Harrison 				printk("%s: orphaned page\n", __func__);
4071da177e4SLinus Torvalds 				return PAGE_CLEAN;
4081da177e4SLinus Torvalds 			}
4091da177e4SLinus Torvalds 		}
4101da177e4SLinus Torvalds 		return PAGE_KEEP;
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 	if (mapping->a_ops->writepage == NULL)
4131da177e4SLinus Torvalds 		return PAGE_ACTIVATE;
4140e093d99SMel Gorman 	if (!may_write_to_queue(mapping->backing_dev_info, sc))
4151da177e4SLinus Torvalds 		return PAGE_KEEP;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	if (clear_page_dirty_for_io(page)) {
4181da177e4SLinus Torvalds 		int res;
4191da177e4SLinus Torvalds 		struct writeback_control wbc = {
4201da177e4SLinus Torvalds 			.sync_mode = WB_SYNC_NONE,
4211da177e4SLinus Torvalds 			.nr_to_write = SWAP_CLUSTER_MAX,
422111ebb6eSOGAWA Hirofumi 			.range_start = 0,
423111ebb6eSOGAWA Hirofumi 			.range_end = LLONG_MAX,
4241da177e4SLinus Torvalds 			.for_reclaim = 1,
4251da177e4SLinus Torvalds 		};
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 		SetPageReclaim(page);
4281da177e4SLinus Torvalds 		res = mapping->a_ops->writepage(page, &wbc);
4291da177e4SLinus Torvalds 		if (res < 0)
4301da177e4SLinus Torvalds 			handle_write_error(mapping, page, res);
431994fc28cSZach Brown 		if (res == AOP_WRITEPAGE_ACTIVATE) {
4321da177e4SLinus Torvalds 			ClearPageReclaim(page);
4331da177e4SLinus Torvalds 			return PAGE_ACTIVATE;
4341da177e4SLinus Torvalds 		}
435c661b078SAndy Whitcroft 
4361da177e4SLinus Torvalds 		if (!PageWriteback(page)) {
4371da177e4SLinus Torvalds 			/* synchronous write or broken a_ops? */
4381da177e4SLinus Torvalds 			ClearPageReclaim(page);
4391da177e4SLinus Torvalds 		}
44023b9da55SMel Gorman 		trace_mm_vmscan_writepage(page, trace_reclaim_flags(page));
441e129b5c2SAndrew Morton 		inc_zone_page_state(page, NR_VMSCAN_WRITE);
4421da177e4SLinus Torvalds 		return PAGE_SUCCESS;
4431da177e4SLinus Torvalds 	}
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	return PAGE_CLEAN;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
448a649fd92SAndrew Morton /*
449e286781dSNick Piggin  * Same as remove_mapping, but if the page is removed from the mapping, it
450e286781dSNick Piggin  * gets returned with a refcount of 0.
451a649fd92SAndrew Morton  */
452e286781dSNick Piggin static int __remove_mapping(struct address_space *mapping, struct page *page)
45349d2e9ccSChristoph Lameter {
45428e4d965SNick Piggin 	BUG_ON(!PageLocked(page));
45528e4d965SNick Piggin 	BUG_ON(mapping != page_mapping(page));
45649d2e9ccSChristoph Lameter 
45719fd6231SNick Piggin 	spin_lock_irq(&mapping->tree_lock);
45849d2e9ccSChristoph Lameter 	/*
4590fd0e6b0SNick Piggin 	 * The non racy check for a busy page.
4600fd0e6b0SNick Piggin 	 *
4610fd0e6b0SNick Piggin 	 * Must be careful with the order of the tests. When someone has
4620fd0e6b0SNick Piggin 	 * a ref to the page, it may be possible that they dirty it then
4630fd0e6b0SNick Piggin 	 * drop the reference. So if PageDirty is tested before page_count
4640fd0e6b0SNick Piggin 	 * here, then the following race may occur:
4650fd0e6b0SNick Piggin 	 *
4660fd0e6b0SNick Piggin 	 * get_user_pages(&page);
4670fd0e6b0SNick Piggin 	 * [user mapping goes away]
4680fd0e6b0SNick Piggin 	 * write_to(page);
4690fd0e6b0SNick Piggin 	 *				!PageDirty(page)    [good]
4700fd0e6b0SNick Piggin 	 * SetPageDirty(page);
4710fd0e6b0SNick Piggin 	 * put_page(page);
4720fd0e6b0SNick Piggin 	 *				!page_count(page)   [good, discard it]
4730fd0e6b0SNick Piggin 	 *
4740fd0e6b0SNick Piggin 	 * [oops, our write_to data is lost]
4750fd0e6b0SNick Piggin 	 *
4760fd0e6b0SNick Piggin 	 * Reversing the order of the tests ensures such a situation cannot
4770fd0e6b0SNick Piggin 	 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
4780fd0e6b0SNick Piggin 	 * load is not satisfied before that of page->_count.
4790fd0e6b0SNick Piggin 	 *
4800fd0e6b0SNick Piggin 	 * Note that if SetPageDirty is always performed via set_page_dirty,
4810fd0e6b0SNick Piggin 	 * and thus under tree_lock, then this ordering is not required.
48249d2e9ccSChristoph Lameter 	 */
483e286781dSNick Piggin 	if (!page_freeze_refs(page, 2))
48449d2e9ccSChristoph Lameter 		goto cannot_free;
485e286781dSNick Piggin 	/* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
486e286781dSNick Piggin 	if (unlikely(PageDirty(page))) {
487e286781dSNick Piggin 		page_unfreeze_refs(page, 2);
48849d2e9ccSChristoph Lameter 		goto cannot_free;
489e286781dSNick Piggin 	}
49049d2e9ccSChristoph Lameter 
49149d2e9ccSChristoph Lameter 	if (PageSwapCache(page)) {
49249d2e9ccSChristoph Lameter 		swp_entry_t swap = { .val = page_private(page) };
49349d2e9ccSChristoph Lameter 		__delete_from_swap_cache(page);
49419fd6231SNick Piggin 		spin_unlock_irq(&mapping->tree_lock);
495cb4b86baSKAMEZAWA Hiroyuki 		swapcache_free(swap, page);
496e286781dSNick Piggin 	} else {
4976072d13cSLinus Torvalds 		void (*freepage)(struct page *);
4986072d13cSLinus Torvalds 
4996072d13cSLinus Torvalds 		freepage = mapping->a_ops->freepage;
5006072d13cSLinus Torvalds 
501e64a782fSMinchan Kim 		__delete_from_page_cache(page);
50219fd6231SNick Piggin 		spin_unlock_irq(&mapping->tree_lock);
503e767e056SDaisuke Nishimura 		mem_cgroup_uncharge_cache_page(page);
5046072d13cSLinus Torvalds 
5056072d13cSLinus Torvalds 		if (freepage != NULL)
5066072d13cSLinus Torvalds 			freepage(page);
507e286781dSNick Piggin 	}
508e286781dSNick Piggin 
50949d2e9ccSChristoph Lameter 	return 1;
51049d2e9ccSChristoph Lameter 
51149d2e9ccSChristoph Lameter cannot_free:
51219fd6231SNick Piggin 	spin_unlock_irq(&mapping->tree_lock);
51349d2e9ccSChristoph Lameter 	return 0;
51449d2e9ccSChristoph Lameter }
51549d2e9ccSChristoph Lameter 
5161da177e4SLinus Torvalds /*
517e286781dSNick Piggin  * Attempt to detach a locked page from its ->mapping.  If it is dirty or if
518e286781dSNick Piggin  * someone else has a ref on the page, abort and return 0.  If it was
519e286781dSNick Piggin  * successfully detached, return 1.  Assumes the caller has a single ref on
520e286781dSNick Piggin  * this page.
521e286781dSNick Piggin  */
522e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page)
523e286781dSNick Piggin {
524e286781dSNick Piggin 	if (__remove_mapping(mapping, page)) {
525e286781dSNick Piggin 		/*
526e286781dSNick Piggin 		 * Unfreezing the refcount with 1 rather than 2 effectively
527e286781dSNick Piggin 		 * drops the pagecache ref for us without requiring another
528e286781dSNick Piggin 		 * atomic operation.
529e286781dSNick Piggin 		 */
530e286781dSNick Piggin 		page_unfreeze_refs(page, 1);
531e286781dSNick Piggin 		return 1;
532e286781dSNick Piggin 	}
533e286781dSNick Piggin 	return 0;
534e286781dSNick Piggin }
535e286781dSNick Piggin 
536894bc310SLee Schermerhorn /**
537894bc310SLee Schermerhorn  * putback_lru_page - put previously isolated page onto appropriate LRU list
538894bc310SLee Schermerhorn  * @page: page to be put back to appropriate lru list
539894bc310SLee Schermerhorn  *
540894bc310SLee Schermerhorn  * Add previously isolated @page to appropriate LRU list.
541894bc310SLee Schermerhorn  * Page may still be unevictable for other reasons.
542894bc310SLee Schermerhorn  *
543894bc310SLee Schermerhorn  * lru_lock must not be held, interrupts must be enabled.
544894bc310SLee Schermerhorn  */
545894bc310SLee Schermerhorn void putback_lru_page(struct page *page)
546894bc310SLee Schermerhorn {
547894bc310SLee Schermerhorn 	int lru;
548894bc310SLee Schermerhorn 	int active = !!TestClearPageActive(page);
549bbfd28eeSLee Schermerhorn 	int was_unevictable = PageUnevictable(page);
550894bc310SLee Schermerhorn 
551894bc310SLee Schermerhorn 	VM_BUG_ON(PageLRU(page));
552894bc310SLee Schermerhorn 
553894bc310SLee Schermerhorn redo:
554894bc310SLee Schermerhorn 	ClearPageUnevictable(page);
555894bc310SLee Schermerhorn 
556894bc310SLee Schermerhorn 	if (page_evictable(page, NULL)) {
557894bc310SLee Schermerhorn 		/*
558894bc310SLee Schermerhorn 		 * For evictable pages, we can use the cache.
559894bc310SLee Schermerhorn 		 * In event of a race, worst case is we end up with an
560894bc310SLee Schermerhorn 		 * unevictable page on [in]active list.
561894bc310SLee Schermerhorn 		 * We know how to handle that.
562894bc310SLee Schermerhorn 		 */
563401a8e1cSJohannes Weiner 		lru = active + page_lru_base_type(page);
564894bc310SLee Schermerhorn 		lru_cache_add_lru(page, lru);
565894bc310SLee Schermerhorn 	} else {
566894bc310SLee Schermerhorn 		/*
567894bc310SLee Schermerhorn 		 * Put unevictable pages directly on zone's unevictable
568894bc310SLee Schermerhorn 		 * list.
569894bc310SLee Schermerhorn 		 */
570894bc310SLee Schermerhorn 		lru = LRU_UNEVICTABLE;
571894bc310SLee Schermerhorn 		add_page_to_unevictable_list(page);
5726a7b9548SJohannes Weiner 		/*
57321ee9f39SMinchan Kim 		 * When racing with an mlock or AS_UNEVICTABLE clearing
57421ee9f39SMinchan Kim 		 * (page is unlocked) make sure that if the other thread
57521ee9f39SMinchan Kim 		 * does not observe our setting of PG_lru and fails
57624513264SHugh Dickins 		 * isolation/check_move_unevictable_pages,
57721ee9f39SMinchan Kim 		 * we see PG_mlocked/AS_UNEVICTABLE cleared below and move
5786a7b9548SJohannes Weiner 		 * the page back to the evictable list.
5796a7b9548SJohannes Weiner 		 *
58021ee9f39SMinchan Kim 		 * The other side is TestClearPageMlocked() or shmem_lock().
5816a7b9548SJohannes Weiner 		 */
5826a7b9548SJohannes Weiner 		smp_mb();
583894bc310SLee Schermerhorn 	}
584894bc310SLee Schermerhorn 
585894bc310SLee Schermerhorn 	/*
586894bc310SLee Schermerhorn 	 * page's status can change while we move it among lru. If an evictable
587894bc310SLee Schermerhorn 	 * page is on unevictable list, it never be freed. To avoid that,
588894bc310SLee Schermerhorn 	 * check after we added it to the list, again.
589894bc310SLee Schermerhorn 	 */
590894bc310SLee Schermerhorn 	if (lru == LRU_UNEVICTABLE && page_evictable(page, NULL)) {
591894bc310SLee Schermerhorn 		if (!isolate_lru_page(page)) {
592894bc310SLee Schermerhorn 			put_page(page);
593894bc310SLee Schermerhorn 			goto redo;
594894bc310SLee Schermerhorn 		}
595894bc310SLee Schermerhorn 		/* This means someone else dropped this page from LRU
596894bc310SLee Schermerhorn 		 * So, it will be freed or putback to LRU again. There is
597894bc310SLee Schermerhorn 		 * nothing to do here.
598894bc310SLee Schermerhorn 		 */
599894bc310SLee Schermerhorn 	}
600894bc310SLee Schermerhorn 
601bbfd28eeSLee Schermerhorn 	if (was_unevictable && lru != LRU_UNEVICTABLE)
602bbfd28eeSLee Schermerhorn 		count_vm_event(UNEVICTABLE_PGRESCUED);
603bbfd28eeSLee Schermerhorn 	else if (!was_unevictable && lru == LRU_UNEVICTABLE)
604bbfd28eeSLee Schermerhorn 		count_vm_event(UNEVICTABLE_PGCULLED);
605bbfd28eeSLee Schermerhorn 
606894bc310SLee Schermerhorn 	put_page(page);		/* drop ref from isolate */
607894bc310SLee Schermerhorn }
608894bc310SLee Schermerhorn 
609dfc8d636SJohannes Weiner enum page_references {
610dfc8d636SJohannes Weiner 	PAGEREF_RECLAIM,
611dfc8d636SJohannes Weiner 	PAGEREF_RECLAIM_CLEAN,
61264574746SJohannes Weiner 	PAGEREF_KEEP,
613dfc8d636SJohannes Weiner 	PAGEREF_ACTIVATE,
614dfc8d636SJohannes Weiner };
615dfc8d636SJohannes Weiner 
616dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page,
617dfc8d636SJohannes Weiner 						  struct scan_control *sc)
618dfc8d636SJohannes Weiner {
61964574746SJohannes Weiner 	int referenced_ptes, referenced_page;
620dfc8d636SJohannes Weiner 	unsigned long vm_flags;
621dfc8d636SJohannes Weiner 
622c3ac9a8aSJohannes Weiner 	referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup,
623c3ac9a8aSJohannes Weiner 					  &vm_flags);
62464574746SJohannes Weiner 	referenced_page = TestClearPageReferenced(page);
625dfc8d636SJohannes Weiner 
626dfc8d636SJohannes Weiner 	/*
627dfc8d636SJohannes Weiner 	 * Mlock lost the isolation race with us.  Let try_to_unmap()
628dfc8d636SJohannes Weiner 	 * move the page to the unevictable list.
629dfc8d636SJohannes Weiner 	 */
630dfc8d636SJohannes Weiner 	if (vm_flags & VM_LOCKED)
631dfc8d636SJohannes Weiner 		return PAGEREF_RECLAIM;
632dfc8d636SJohannes Weiner 
63364574746SJohannes Weiner 	if (referenced_ptes) {
634e4898273SMichal Hocko 		if (PageSwapBacked(page))
63564574746SJohannes Weiner 			return PAGEREF_ACTIVATE;
63664574746SJohannes Weiner 		/*
63764574746SJohannes Weiner 		 * All mapped pages start out with page table
63864574746SJohannes Weiner 		 * references from the instantiating fault, so we need
63964574746SJohannes Weiner 		 * to look twice if a mapped file page is used more
64064574746SJohannes Weiner 		 * than once.
64164574746SJohannes Weiner 		 *
64264574746SJohannes Weiner 		 * Mark it and spare it for another trip around the
64364574746SJohannes Weiner 		 * inactive list.  Another page table reference will
64464574746SJohannes Weiner 		 * lead to its activation.
64564574746SJohannes Weiner 		 *
64664574746SJohannes Weiner 		 * Note: the mark is set for activated pages as well
64764574746SJohannes Weiner 		 * so that recently deactivated but used pages are
64864574746SJohannes Weiner 		 * quickly recovered.
64964574746SJohannes Weiner 		 */
65064574746SJohannes Weiner 		SetPageReferenced(page);
65164574746SJohannes Weiner 
65234dbc67aSKonstantin Khlebnikov 		if (referenced_page || referenced_ptes > 1)
653dfc8d636SJohannes Weiner 			return PAGEREF_ACTIVATE;
654dfc8d636SJohannes Weiner 
655c909e993SKonstantin Khlebnikov 		/*
656c909e993SKonstantin Khlebnikov 		 * Activate file-backed executable pages after first usage.
657c909e993SKonstantin Khlebnikov 		 */
658c909e993SKonstantin Khlebnikov 		if (vm_flags & VM_EXEC)
659c909e993SKonstantin Khlebnikov 			return PAGEREF_ACTIVATE;
660c909e993SKonstantin Khlebnikov 
66164574746SJohannes Weiner 		return PAGEREF_KEEP;
66264574746SJohannes Weiner 	}
66364574746SJohannes Weiner 
664dfc8d636SJohannes Weiner 	/* Reclaim if clean, defer dirty pages to writeback */
6652e30244aSKOSAKI Motohiro 	if (referenced_page && !PageSwapBacked(page))
666dfc8d636SJohannes Weiner 		return PAGEREF_RECLAIM_CLEAN;
66764574746SJohannes Weiner 
66864574746SJohannes Weiner 	return PAGEREF_RECLAIM;
669dfc8d636SJohannes Weiner }
670dfc8d636SJohannes Weiner 
671e286781dSNick Piggin /*
6721742f19fSAndrew Morton  * shrink_page_list() returns the number of reclaimed pages
6731da177e4SLinus Torvalds  */
6741742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list,
6756a18adb3SKonstantin Khlebnikov 				      struct zone *zone,
676f84f6e2bSMel Gorman 				      struct scan_control *sc,
67792df3a72SMel Gorman 				      unsigned long *ret_nr_dirty,
67892df3a72SMel Gorman 				      unsigned long *ret_nr_writeback)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	LIST_HEAD(ret_pages);
681abe4c3b5SMel Gorman 	LIST_HEAD(free_pages);
6821da177e4SLinus Torvalds 	int pgactivate = 0;
6830e093d99SMel Gorman 	unsigned long nr_dirty = 0;
6840e093d99SMel Gorman 	unsigned long nr_congested = 0;
68505ff5137SAndrew Morton 	unsigned long nr_reclaimed = 0;
68692df3a72SMel Gorman 	unsigned long nr_writeback = 0;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	cond_resched();
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	while (!list_empty(page_list)) {
691dfc8d636SJohannes Weiner 		enum page_references references;
6921da177e4SLinus Torvalds 		struct address_space *mapping;
6931da177e4SLinus Torvalds 		struct page *page;
6941da177e4SLinus Torvalds 		int may_enter_fs;
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 		cond_resched();
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 		page = lru_to_page(page_list);
6991da177e4SLinus Torvalds 		list_del(&page->lru);
7001da177e4SLinus Torvalds 
701529ae9aaSNick Piggin 		if (!trylock_page(page))
7021da177e4SLinus Torvalds 			goto keep;
7031da177e4SLinus Torvalds 
704725d704eSNick Piggin 		VM_BUG_ON(PageActive(page));
7056a18adb3SKonstantin Khlebnikov 		VM_BUG_ON(page_zone(page) != zone);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 		sc->nr_scanned++;
70880e43426SChristoph Lameter 
709b291f000SNick Piggin 		if (unlikely(!page_evictable(page, NULL)))
710b291f000SNick Piggin 			goto cull_mlocked;
711894bc310SLee Schermerhorn 
712a6dc60f8SJohannes Weiner 		if (!sc->may_unmap && page_mapped(page))
71380e43426SChristoph Lameter 			goto keep_locked;
71480e43426SChristoph Lameter 
7151da177e4SLinus Torvalds 		/* Double the slab pressure for mapped and swapcache pages */
7161da177e4SLinus Torvalds 		if (page_mapped(page) || PageSwapCache(page))
7171da177e4SLinus Torvalds 			sc->nr_scanned++;
7181da177e4SLinus Torvalds 
719c661b078SAndy Whitcroft 		may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
720c661b078SAndy Whitcroft 			(PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
721c661b078SAndy Whitcroft 
722c661b078SAndy Whitcroft 		if (PageWriteback(page)) {
72392df3a72SMel Gorman 			nr_writeback++;
7247d3579e8SKOSAKI Motohiro 			unlock_page(page);
72541ac1999SMel Gorman 			goto keep;
726c661b078SAndy Whitcroft 		}
7271da177e4SLinus Torvalds 
7286a18adb3SKonstantin Khlebnikov 		references = page_check_references(page, sc);
729dfc8d636SJohannes Weiner 		switch (references) {
730dfc8d636SJohannes Weiner 		case PAGEREF_ACTIVATE:
7311da177e4SLinus Torvalds 			goto activate_locked;
73264574746SJohannes Weiner 		case PAGEREF_KEEP:
73364574746SJohannes Weiner 			goto keep_locked;
734dfc8d636SJohannes Weiner 		case PAGEREF_RECLAIM:
735dfc8d636SJohannes Weiner 		case PAGEREF_RECLAIM_CLEAN:
736dfc8d636SJohannes Weiner 			; /* try to reclaim the page below */
737dfc8d636SJohannes Weiner 		}
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 		/*
7401da177e4SLinus Torvalds 		 * Anonymous process memory has backing store?
7411da177e4SLinus Torvalds 		 * Try to allocate it some swap space here.
7421da177e4SLinus Torvalds 		 */
743b291f000SNick Piggin 		if (PageAnon(page) && !PageSwapCache(page)) {
74463eb6b93SHugh Dickins 			if (!(sc->gfp_mask & __GFP_IO))
74563eb6b93SHugh Dickins 				goto keep_locked;
746ac47b003SHugh Dickins 			if (!add_to_swap(page))
7471da177e4SLinus Torvalds 				goto activate_locked;
74863eb6b93SHugh Dickins 			may_enter_fs = 1;
749b291f000SNick Piggin 		}
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 		mapping = page_mapping(page);
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 		/*
7541da177e4SLinus Torvalds 		 * The page is mapped into the page tables of one or more
7551da177e4SLinus Torvalds 		 * processes. Try to unmap it here.
7561da177e4SLinus Torvalds 		 */
7571da177e4SLinus Torvalds 		if (page_mapped(page) && mapping) {
75814fa31b8SAndi Kleen 			switch (try_to_unmap(page, TTU_UNMAP)) {
7591da177e4SLinus Torvalds 			case SWAP_FAIL:
7601da177e4SLinus Torvalds 				goto activate_locked;
7611da177e4SLinus Torvalds 			case SWAP_AGAIN:
7621da177e4SLinus Torvalds 				goto keep_locked;
763b291f000SNick Piggin 			case SWAP_MLOCK:
764b291f000SNick Piggin 				goto cull_mlocked;
7651da177e4SLinus Torvalds 			case SWAP_SUCCESS:
7661da177e4SLinus Torvalds 				; /* try to free the page below */
7671da177e4SLinus Torvalds 			}
7681da177e4SLinus Torvalds 		}
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 		if (PageDirty(page)) {
7710e093d99SMel Gorman 			nr_dirty++;
7720e093d99SMel Gorman 
773ee72886dSMel Gorman 			/*
774ee72886dSMel Gorman 			 * Only kswapd can writeback filesystem pages to
775f84f6e2bSMel Gorman 			 * avoid risk of stack overflow but do not writeback
776f84f6e2bSMel Gorman 			 * unless under significant pressure.
777ee72886dSMel Gorman 			 */
778f84f6e2bSMel Gorman 			if (page_is_file_cache(page) &&
7799e3b2f8cSKonstantin Khlebnikov 					(!current_is_kswapd() ||
7809e3b2f8cSKonstantin Khlebnikov 					 sc->priority >= DEF_PRIORITY - 2)) {
78149ea7eb6SMel Gorman 				/*
78249ea7eb6SMel Gorman 				 * Immediately reclaim when written back.
78349ea7eb6SMel Gorman 				 * Similar in principal to deactivate_page()
78449ea7eb6SMel Gorman 				 * except we already have the page isolated
78549ea7eb6SMel Gorman 				 * and know it's dirty
78649ea7eb6SMel Gorman 				 */
78749ea7eb6SMel Gorman 				inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE);
78849ea7eb6SMel Gorman 				SetPageReclaim(page);
78949ea7eb6SMel Gorman 
790ee72886dSMel Gorman 				goto keep_locked;
791ee72886dSMel Gorman 			}
792ee72886dSMel Gorman 
793dfc8d636SJohannes Weiner 			if (references == PAGEREF_RECLAIM_CLEAN)
7941da177e4SLinus Torvalds 				goto keep_locked;
7954dd4b920SAndrew Morton 			if (!may_enter_fs)
7961da177e4SLinus Torvalds 				goto keep_locked;
79752a8363eSChristoph Lameter 			if (!sc->may_writepage)
7981da177e4SLinus Torvalds 				goto keep_locked;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 			/* Page is dirty, try to write it out here */
8017d3579e8SKOSAKI Motohiro 			switch (pageout(page, mapping, sc)) {
8021da177e4SLinus Torvalds 			case PAGE_KEEP:
8030e093d99SMel Gorman 				nr_congested++;
8041da177e4SLinus Torvalds 				goto keep_locked;
8051da177e4SLinus Torvalds 			case PAGE_ACTIVATE:
8061da177e4SLinus Torvalds 				goto activate_locked;
8071da177e4SLinus Torvalds 			case PAGE_SUCCESS:
8087d3579e8SKOSAKI Motohiro 				if (PageWriteback(page))
80941ac1999SMel Gorman 					goto keep;
8107d3579e8SKOSAKI Motohiro 				if (PageDirty(page))
8111da177e4SLinus Torvalds 					goto keep;
8127d3579e8SKOSAKI Motohiro 
8131da177e4SLinus Torvalds 				/*
8141da177e4SLinus Torvalds 				 * A synchronous write - probably a ramdisk.  Go
8151da177e4SLinus Torvalds 				 * ahead and try to reclaim the page.
8161da177e4SLinus Torvalds 				 */
817529ae9aaSNick Piggin 				if (!trylock_page(page))
8181da177e4SLinus Torvalds 					goto keep;
8191da177e4SLinus Torvalds 				if (PageDirty(page) || PageWriteback(page))
8201da177e4SLinus Torvalds 					goto keep_locked;
8211da177e4SLinus Torvalds 				mapping = page_mapping(page);
8221da177e4SLinus Torvalds 			case PAGE_CLEAN:
8231da177e4SLinus Torvalds 				; /* try to free the page below */
8241da177e4SLinus Torvalds 			}
8251da177e4SLinus Torvalds 		}
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 		/*
8281da177e4SLinus Torvalds 		 * If the page has buffers, try to free the buffer mappings
8291da177e4SLinus Torvalds 		 * associated with this page. If we succeed we try to free
8301da177e4SLinus Torvalds 		 * the page as well.
8311da177e4SLinus Torvalds 		 *
8321da177e4SLinus Torvalds 		 * We do this even if the page is PageDirty().
8331da177e4SLinus Torvalds 		 * try_to_release_page() does not perform I/O, but it is
8341da177e4SLinus Torvalds 		 * possible for a page to have PageDirty set, but it is actually
8351da177e4SLinus Torvalds 		 * clean (all its buffers are clean).  This happens if the
8361da177e4SLinus Torvalds 		 * buffers were written out directly, with submit_bh(). ext3
8371da177e4SLinus Torvalds 		 * will do this, as well as the blockdev mapping.
8381da177e4SLinus Torvalds 		 * try_to_release_page() will discover that cleanness and will
8391da177e4SLinus Torvalds 		 * drop the buffers and mark the page clean - it can be freed.
8401da177e4SLinus Torvalds 		 *
8411da177e4SLinus Torvalds 		 * Rarely, pages can have buffers and no ->mapping.  These are
8421da177e4SLinus Torvalds 		 * the pages which were not successfully invalidated in
8431da177e4SLinus Torvalds 		 * truncate_complete_page().  We try to drop those buffers here
8441da177e4SLinus Torvalds 		 * and if that worked, and the page is no longer mapped into
8451da177e4SLinus Torvalds 		 * process address space (page_count == 1) it can be freed.
8461da177e4SLinus Torvalds 		 * Otherwise, leave the page on the LRU so it is swappable.
8471da177e4SLinus Torvalds 		 */
848266cf658SDavid Howells 		if (page_has_private(page)) {
8491da177e4SLinus Torvalds 			if (!try_to_release_page(page, sc->gfp_mask))
8501da177e4SLinus Torvalds 				goto activate_locked;
851e286781dSNick Piggin 			if (!mapping && page_count(page) == 1) {
852e286781dSNick Piggin 				unlock_page(page);
853e286781dSNick Piggin 				if (put_page_testzero(page))
8541da177e4SLinus Torvalds 					goto free_it;
855e286781dSNick Piggin 				else {
856e286781dSNick Piggin 					/*
857e286781dSNick Piggin 					 * rare race with speculative reference.
858e286781dSNick Piggin 					 * the speculative reference will free
859e286781dSNick Piggin 					 * this page shortly, so we may
860e286781dSNick Piggin 					 * increment nr_reclaimed here (and
861e286781dSNick Piggin 					 * leave it off the LRU).
862e286781dSNick Piggin 					 */
863e286781dSNick Piggin 					nr_reclaimed++;
864e286781dSNick Piggin 					continue;
865e286781dSNick Piggin 				}
866e286781dSNick Piggin 			}
8671da177e4SLinus Torvalds 		}
8681da177e4SLinus Torvalds 
869e286781dSNick Piggin 		if (!mapping || !__remove_mapping(mapping, page))
87049d2e9ccSChristoph Lameter 			goto keep_locked;
8711da177e4SLinus Torvalds 
872a978d6f5SNick Piggin 		/*
873a978d6f5SNick Piggin 		 * At this point, we have no other references and there is
874a978d6f5SNick Piggin 		 * no way to pick any more up (removed from LRU, removed
875a978d6f5SNick Piggin 		 * from pagecache). Can use non-atomic bitops now (and
876a978d6f5SNick Piggin 		 * we obviously don't have to worry about waking up a process
877a978d6f5SNick Piggin 		 * waiting on the page lock, because there are no references.
878a978d6f5SNick Piggin 		 */
879a978d6f5SNick Piggin 		__clear_page_locked(page);
880e286781dSNick Piggin free_it:
88105ff5137SAndrew Morton 		nr_reclaimed++;
882abe4c3b5SMel Gorman 
883abe4c3b5SMel Gorman 		/*
884abe4c3b5SMel Gorman 		 * Is there need to periodically free_page_list? It would
885abe4c3b5SMel Gorman 		 * appear not as the counts should be low
886abe4c3b5SMel Gorman 		 */
887abe4c3b5SMel Gorman 		list_add(&page->lru, &free_pages);
8881da177e4SLinus Torvalds 		continue;
8891da177e4SLinus Torvalds 
890b291f000SNick Piggin cull_mlocked:
89163d6c5adSHugh Dickins 		if (PageSwapCache(page))
89263d6c5adSHugh Dickins 			try_to_free_swap(page);
893b291f000SNick Piggin 		unlock_page(page);
894b291f000SNick Piggin 		putback_lru_page(page);
895b291f000SNick Piggin 		continue;
896b291f000SNick Piggin 
8971da177e4SLinus Torvalds activate_locked:
89868a22394SRik van Riel 		/* Not a candidate for swapping, so reclaim swap space. */
89968a22394SRik van Riel 		if (PageSwapCache(page) && vm_swap_full())
900a2c43eedSHugh Dickins 			try_to_free_swap(page);
901894bc310SLee Schermerhorn 		VM_BUG_ON(PageActive(page));
9021da177e4SLinus Torvalds 		SetPageActive(page);
9031da177e4SLinus Torvalds 		pgactivate++;
9041da177e4SLinus Torvalds keep_locked:
9051da177e4SLinus Torvalds 		unlock_page(page);
9061da177e4SLinus Torvalds keep:
9071da177e4SLinus Torvalds 		list_add(&page->lru, &ret_pages);
908b291f000SNick Piggin 		VM_BUG_ON(PageLRU(page) || PageUnevictable(page));
9091da177e4SLinus Torvalds 	}
910abe4c3b5SMel Gorman 
9110e093d99SMel Gorman 	/*
9120e093d99SMel Gorman 	 * Tag a zone as congested if all the dirty pages encountered were
9130e093d99SMel Gorman 	 * backed by a congested BDI. In this case, reclaimers should just
9140e093d99SMel Gorman 	 * back off and wait for congestion to clear because further reclaim
9150e093d99SMel Gorman 	 * will encounter the same problem
9160e093d99SMel Gorman 	 */
91789b5fae5SJohannes Weiner 	if (nr_dirty && nr_dirty == nr_congested && global_reclaim(sc))
9186a18adb3SKonstantin Khlebnikov 		zone_set_flag(zone, ZONE_CONGESTED);
9190e093d99SMel Gorman 
920cc59850eSKonstantin Khlebnikov 	free_hot_cold_page_list(&free_pages, 1);
921abe4c3b5SMel Gorman 
9221da177e4SLinus Torvalds 	list_splice(&ret_pages, page_list);
923f8891e5eSChristoph Lameter 	count_vm_events(PGACTIVATE, pgactivate);
92492df3a72SMel Gorman 	*ret_nr_dirty += nr_dirty;
92592df3a72SMel Gorman 	*ret_nr_writeback += nr_writeback;
92605ff5137SAndrew Morton 	return nr_reclaimed;
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
9295ad333ebSAndy Whitcroft /*
9305ad333ebSAndy Whitcroft  * Attempt to remove the specified page from its LRU.  Only take this page
9315ad333ebSAndy Whitcroft  * if it is of the appropriate PageActive status.  Pages which are being
9325ad333ebSAndy Whitcroft  * freed elsewhere are also ignored.
9335ad333ebSAndy Whitcroft  *
9345ad333ebSAndy Whitcroft  * page:	page to consider
9355ad333ebSAndy Whitcroft  * mode:	one of the LRU isolation modes defined above
9365ad333ebSAndy Whitcroft  *
9375ad333ebSAndy Whitcroft  * returns 0 on success, -ve errno on failure.
9385ad333ebSAndy Whitcroft  */
939f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode)
9405ad333ebSAndy Whitcroft {
9415ad333ebSAndy Whitcroft 	int ret = -EINVAL;
9425ad333ebSAndy Whitcroft 
9435ad333ebSAndy Whitcroft 	/* Only take pages on the LRU. */
9445ad333ebSAndy Whitcroft 	if (!PageLRU(page))
9455ad333ebSAndy Whitcroft 		return ret;
9465ad333ebSAndy Whitcroft 
947c53919adSMel Gorman 	/* Do not give back unevictable pages for compaction */
948894bc310SLee Schermerhorn 	if (PageUnevictable(page))
949894bc310SLee Schermerhorn 		return ret;
950894bc310SLee Schermerhorn 
9515ad333ebSAndy Whitcroft 	ret = -EBUSY;
95208e552c6SKAMEZAWA Hiroyuki 
953c8244935SMel Gorman 	/*
954c8244935SMel Gorman 	 * To minimise LRU disruption, the caller can indicate that it only
955c8244935SMel Gorman 	 * wants to isolate pages it will be able to operate on without
956c8244935SMel Gorman 	 * blocking - clean pages for the most part.
957c8244935SMel Gorman 	 *
958c8244935SMel Gorman 	 * ISOLATE_CLEAN means that only clean pages should be isolated. This
959c8244935SMel Gorman 	 * is used by reclaim when it is cannot write to backing storage
960c8244935SMel Gorman 	 *
961c8244935SMel Gorman 	 * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages
962c8244935SMel Gorman 	 * that it is possible to migrate without blocking
963c8244935SMel Gorman 	 */
964c8244935SMel Gorman 	if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) {
965c8244935SMel Gorman 		/* All the caller can do on PageWriteback is block */
966c8244935SMel Gorman 		if (PageWriteback(page))
96739deaf85SMinchan Kim 			return ret;
96839deaf85SMinchan Kim 
969c8244935SMel Gorman 		if (PageDirty(page)) {
970c8244935SMel Gorman 			struct address_space *mapping;
971c8244935SMel Gorman 
972c8244935SMel Gorman 			/* ISOLATE_CLEAN means only clean pages */
973c8244935SMel Gorman 			if (mode & ISOLATE_CLEAN)
974c8244935SMel Gorman 				return ret;
975c8244935SMel Gorman 
976c8244935SMel Gorman 			/*
977c8244935SMel Gorman 			 * Only pages without mappings or that have a
978c8244935SMel Gorman 			 * ->migratepage callback are possible to migrate
979c8244935SMel Gorman 			 * without blocking
980c8244935SMel Gorman 			 */
981c8244935SMel Gorman 			mapping = page_mapping(page);
982c8244935SMel Gorman 			if (mapping && !mapping->a_ops->migratepage)
983c8244935SMel Gorman 				return ret;
984c8244935SMel Gorman 		}
985c8244935SMel Gorman 	}
986c8244935SMel Gorman 
987f80c0673SMinchan Kim 	if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
988f80c0673SMinchan Kim 		return ret;
989f80c0673SMinchan Kim 
9905ad333ebSAndy Whitcroft 	if (likely(get_page_unless_zero(page))) {
9915ad333ebSAndy Whitcroft 		/*
9925ad333ebSAndy Whitcroft 		 * Be careful not to clear PageLRU until after we're
9935ad333ebSAndy Whitcroft 		 * sure the page is not being freed elsewhere -- the
9945ad333ebSAndy Whitcroft 		 * page release code relies on it.
9955ad333ebSAndy Whitcroft 		 */
9965ad333ebSAndy Whitcroft 		ClearPageLRU(page);
9975ad333ebSAndy Whitcroft 		ret = 0;
9985ad333ebSAndy Whitcroft 	}
9995ad333ebSAndy Whitcroft 
10005ad333ebSAndy Whitcroft 	return ret;
10015ad333ebSAndy Whitcroft }
10025ad333ebSAndy Whitcroft 
100349d2e9ccSChristoph Lameter /*
10041da177e4SLinus Torvalds  * zone->lru_lock is heavily contended.  Some of the functions that
10051da177e4SLinus Torvalds  * shrink the lists perform better by taking out a batch of pages
10061da177e4SLinus Torvalds  * and working on them outside the LRU lock.
10071da177e4SLinus Torvalds  *
10081da177e4SLinus Torvalds  * For pagecache intensive workloads, this function is the hottest
10091da177e4SLinus Torvalds  * spot in the kernel (apart from copy_*_user functions).
10101da177e4SLinus Torvalds  *
10111da177e4SLinus Torvalds  * Appropriate locks must be held before calling this function.
10121da177e4SLinus Torvalds  *
10131da177e4SLinus Torvalds  * @nr_to_scan:	The number of pages to look through on the list.
10145dc35979SKonstantin Khlebnikov  * @lruvec:	The LRU vector to pull pages from.
10151da177e4SLinus Torvalds  * @dst:	The temp list to put pages on to.
1016f626012dSHugh Dickins  * @nr_scanned:	The number of pages that were scanned.
1017fe2c2a10SRik van Riel  * @sc:		The scan_control struct for this reclaim session
10185ad333ebSAndy Whitcroft  * @mode:	One of the LRU isolation modes
10193cb99451SKonstantin Khlebnikov  * @lru:	LRU list id for isolating
10201da177e4SLinus Torvalds  *
10211da177e4SLinus Torvalds  * returns how many pages were moved onto *@dst.
10221da177e4SLinus Torvalds  */
102369e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
10245dc35979SKonstantin Khlebnikov 		struct lruvec *lruvec, struct list_head *dst,
1025fe2c2a10SRik van Riel 		unsigned long *nr_scanned, struct scan_control *sc,
10263cb99451SKonstantin Khlebnikov 		isolate_mode_t mode, enum lru_list lru)
10271da177e4SLinus Torvalds {
102875b00af7SHugh Dickins 	struct list_head *src = &lruvec->lists[lru];
102969e05944SAndrew Morton 	unsigned long nr_taken = 0;
1030c9b02d97SWu Fengguang 	unsigned long scan;
10311da177e4SLinus Torvalds 
1032c9b02d97SWu Fengguang 	for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
10335ad333ebSAndy Whitcroft 		struct page *page;
1034fa9add64SHugh Dickins 		int nr_pages;
10355ad333ebSAndy Whitcroft 
10361da177e4SLinus Torvalds 		page = lru_to_page(src);
10371da177e4SLinus Torvalds 		prefetchw_prev_lru_page(page, src, flags);
10381da177e4SLinus Torvalds 
1039725d704eSNick Piggin 		VM_BUG_ON(!PageLRU(page));
10408d438f96SNick Piggin 
1041f3fd4a61SKonstantin Khlebnikov 		switch (__isolate_lru_page(page, mode)) {
10425ad333ebSAndy Whitcroft 		case 0:
1043fa9add64SHugh Dickins 			nr_pages = hpage_nr_pages(page);
1044fa9add64SHugh Dickins 			mem_cgroup_update_lru_size(lruvec, lru, -nr_pages);
10455ad333ebSAndy Whitcroft 			list_move(&page->lru, dst);
1046fa9add64SHugh Dickins 			nr_taken += nr_pages;
10475ad333ebSAndy Whitcroft 			break;
10487c8ee9a8SNick Piggin 
10495ad333ebSAndy Whitcroft 		case -EBUSY:
10505ad333ebSAndy Whitcroft 			/* else it is being freed elsewhere */
10515ad333ebSAndy Whitcroft 			list_move(&page->lru, src);
10525ad333ebSAndy Whitcroft 			continue;
10535ad333ebSAndy Whitcroft 
10545ad333ebSAndy Whitcroft 		default:
10555ad333ebSAndy Whitcroft 			BUG();
10565ad333ebSAndy Whitcroft 		}
10575ad333ebSAndy Whitcroft 	}
10581da177e4SLinus Torvalds 
1059f626012dSHugh Dickins 	*nr_scanned = scan;
106075b00af7SHugh Dickins 	trace_mm_vmscan_lru_isolate(sc->order, nr_to_scan, scan,
106175b00af7SHugh Dickins 				    nr_taken, mode, is_file_lru(lru));
10621da177e4SLinus Torvalds 	return nr_taken;
10631da177e4SLinus Torvalds }
10641da177e4SLinus Torvalds 
106562695a84SNick Piggin /**
106662695a84SNick Piggin  * isolate_lru_page - tries to isolate a page from its LRU list
106762695a84SNick Piggin  * @page: page to isolate from its LRU list
106862695a84SNick Piggin  *
106962695a84SNick Piggin  * Isolates a @page from an LRU list, clears PageLRU and adjusts the
107062695a84SNick Piggin  * vmstat statistic corresponding to whatever LRU list the page was on.
107162695a84SNick Piggin  *
107262695a84SNick Piggin  * Returns 0 if the page was removed from an LRU list.
107362695a84SNick Piggin  * Returns -EBUSY if the page was not on an LRU list.
107462695a84SNick Piggin  *
107562695a84SNick Piggin  * The returned page will have PageLRU() cleared.  If it was found on
1076894bc310SLee Schermerhorn  * the active list, it will have PageActive set.  If it was found on
1077894bc310SLee Schermerhorn  * the unevictable list, it will have the PageUnevictable bit set. That flag
1078894bc310SLee Schermerhorn  * may need to be cleared by the caller before letting the page go.
107962695a84SNick Piggin  *
108062695a84SNick Piggin  * The vmstat statistic corresponding to the list on which the page was
108162695a84SNick Piggin  * found will be decremented.
108262695a84SNick Piggin  *
108362695a84SNick Piggin  * Restrictions:
108462695a84SNick Piggin  * (1) Must be called with an elevated refcount on the page. This is a
108562695a84SNick Piggin  *     fundamentnal difference from isolate_lru_pages (which is called
108662695a84SNick Piggin  *     without a stable reference).
108762695a84SNick Piggin  * (2) the lru_lock must not be held.
108862695a84SNick Piggin  * (3) interrupts must be enabled.
108962695a84SNick Piggin  */
109062695a84SNick Piggin int isolate_lru_page(struct page *page)
109162695a84SNick Piggin {
109262695a84SNick Piggin 	int ret = -EBUSY;
109362695a84SNick Piggin 
10940c917313SKonstantin Khlebnikov 	VM_BUG_ON(!page_count(page));
10950c917313SKonstantin Khlebnikov 
109662695a84SNick Piggin 	if (PageLRU(page)) {
109762695a84SNick Piggin 		struct zone *zone = page_zone(page);
1098fa9add64SHugh Dickins 		struct lruvec *lruvec;
109962695a84SNick Piggin 
110062695a84SNick Piggin 		spin_lock_irq(&zone->lru_lock);
1101fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
11020c917313SKonstantin Khlebnikov 		if (PageLRU(page)) {
1103894bc310SLee Schermerhorn 			int lru = page_lru(page);
11040c917313SKonstantin Khlebnikov 			get_page(page);
110562695a84SNick Piggin 			ClearPageLRU(page);
1106fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
1107fa9add64SHugh Dickins 			ret = 0;
110862695a84SNick Piggin 		}
110962695a84SNick Piggin 		spin_unlock_irq(&zone->lru_lock);
111062695a84SNick Piggin 	}
111162695a84SNick Piggin 	return ret;
111262695a84SNick Piggin }
111362695a84SNick Piggin 
11145ad333ebSAndy Whitcroft /*
111535cd7815SRik van Riel  * Are there way too many processes in the direct reclaim path already?
111635cd7815SRik van Riel  */
111735cd7815SRik van Riel static int too_many_isolated(struct zone *zone, int file,
111835cd7815SRik van Riel 		struct scan_control *sc)
111935cd7815SRik van Riel {
112035cd7815SRik van Riel 	unsigned long inactive, isolated;
112135cd7815SRik van Riel 
112235cd7815SRik van Riel 	if (current_is_kswapd())
112335cd7815SRik van Riel 		return 0;
112435cd7815SRik van Riel 
112589b5fae5SJohannes Weiner 	if (!global_reclaim(sc))
112635cd7815SRik van Riel 		return 0;
112735cd7815SRik van Riel 
112835cd7815SRik van Riel 	if (file) {
112935cd7815SRik van Riel 		inactive = zone_page_state(zone, NR_INACTIVE_FILE);
113035cd7815SRik van Riel 		isolated = zone_page_state(zone, NR_ISOLATED_FILE);
113135cd7815SRik van Riel 	} else {
113235cd7815SRik van Riel 		inactive = zone_page_state(zone, NR_INACTIVE_ANON);
113335cd7815SRik van Riel 		isolated = zone_page_state(zone, NR_ISOLATED_ANON);
113435cd7815SRik van Riel 	}
113535cd7815SRik van Riel 
113635cd7815SRik van Riel 	return isolated > inactive;
113735cd7815SRik van Riel }
113835cd7815SRik van Riel 
113966635629SMel Gorman static noinline_for_stack void
114075b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list)
114166635629SMel Gorman {
114227ac81d8SKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
114327ac81d8SKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
11443f79768fSHugh Dickins 	LIST_HEAD(pages_to_free);
114566635629SMel Gorman 
114666635629SMel Gorman 	/*
114766635629SMel Gorman 	 * Put back any unfreeable pages.
114866635629SMel Gorman 	 */
114966635629SMel Gorman 	while (!list_empty(page_list)) {
11503f79768fSHugh Dickins 		struct page *page = lru_to_page(page_list);
115166635629SMel Gorman 		int lru;
11523f79768fSHugh Dickins 
115366635629SMel Gorman 		VM_BUG_ON(PageLRU(page));
115466635629SMel Gorman 		list_del(&page->lru);
115566635629SMel Gorman 		if (unlikely(!page_evictable(page, NULL))) {
115666635629SMel Gorman 			spin_unlock_irq(&zone->lru_lock);
115766635629SMel Gorman 			putback_lru_page(page);
115866635629SMel Gorman 			spin_lock_irq(&zone->lru_lock);
115966635629SMel Gorman 			continue;
116066635629SMel Gorman 		}
1161fa9add64SHugh Dickins 
1162fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
1163fa9add64SHugh Dickins 
11647a608572SLinus Torvalds 		SetPageLRU(page);
116566635629SMel Gorman 		lru = page_lru(page);
1166fa9add64SHugh Dickins 		add_page_to_lru_list(page, lruvec, lru);
1167fa9add64SHugh Dickins 
116866635629SMel Gorman 		if (is_active_lru(lru)) {
116966635629SMel Gorman 			int file = is_file_lru(lru);
11709992af10SRik van Riel 			int numpages = hpage_nr_pages(page);
11719992af10SRik van Riel 			reclaim_stat->recent_rotated[file] += numpages;
117266635629SMel Gorman 		}
11732bcf8879SHugh Dickins 		if (put_page_testzero(page)) {
11742bcf8879SHugh Dickins 			__ClearPageLRU(page);
11752bcf8879SHugh Dickins 			__ClearPageActive(page);
1176fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
11772bcf8879SHugh Dickins 
11782bcf8879SHugh Dickins 			if (unlikely(PageCompound(page))) {
117966635629SMel Gorman 				spin_unlock_irq(&zone->lru_lock);
11802bcf8879SHugh Dickins 				(*get_compound_page_dtor(page))(page);
118166635629SMel Gorman 				spin_lock_irq(&zone->lru_lock);
11822bcf8879SHugh Dickins 			} else
11832bcf8879SHugh Dickins 				list_add(&page->lru, &pages_to_free);
118466635629SMel Gorman 		}
118566635629SMel Gorman 	}
118666635629SMel Gorman 
11873f79768fSHugh Dickins 	/*
11883f79768fSHugh Dickins 	 * To save our caller's stack, now use input list for pages to free.
11893f79768fSHugh Dickins 	 */
11903f79768fSHugh Dickins 	list_splice(&pages_to_free, page_list);
119166635629SMel Gorman }
119266635629SMel Gorman 
119366635629SMel Gorman /*
11941742f19fSAndrew Morton  * shrink_inactive_list() is a helper for shrink_zone().  It returns the number
11951742f19fSAndrew Morton  * of reclaimed pages
11961da177e4SLinus Torvalds  */
119766635629SMel Gorman static noinline_for_stack unsigned long
11981a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
11999e3b2f8cSKonstantin Khlebnikov 		     struct scan_control *sc, enum lru_list lru)
12001da177e4SLinus Torvalds {
12011da177e4SLinus Torvalds 	LIST_HEAD(page_list);
1202e247dbceSKOSAKI Motohiro 	unsigned long nr_scanned;
120305ff5137SAndrew Morton 	unsigned long nr_reclaimed = 0;
1204e247dbceSKOSAKI Motohiro 	unsigned long nr_taken;
120592df3a72SMel Gorman 	unsigned long nr_dirty = 0;
120692df3a72SMel Gorman 	unsigned long nr_writeback = 0;
1207f3fd4a61SKonstantin Khlebnikov 	isolate_mode_t isolate_mode = 0;
12083cb99451SKonstantin Khlebnikov 	int file = is_file_lru(lru);
12091a93be0eSKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
12101a93be0eSKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
121178dc583dSKOSAKI Motohiro 
121235cd7815SRik van Riel 	while (unlikely(too_many_isolated(zone, file, sc))) {
121358355c78SKOSAKI Motohiro 		congestion_wait(BLK_RW_ASYNC, HZ/10);
121435cd7815SRik van Riel 
121535cd7815SRik van Riel 		/* We are about to die and free our memory. Return now. */
121635cd7815SRik van Riel 		if (fatal_signal_pending(current))
121735cd7815SRik van Riel 			return SWAP_CLUSTER_MAX;
121835cd7815SRik van Riel 	}
121935cd7815SRik van Riel 
12201da177e4SLinus Torvalds 	lru_add_drain();
1221f80c0673SMinchan Kim 
1222f80c0673SMinchan Kim 	if (!sc->may_unmap)
122361317289SHillf Danton 		isolate_mode |= ISOLATE_UNMAPPED;
1224f80c0673SMinchan Kim 	if (!sc->may_writepage)
122561317289SHillf Danton 		isolate_mode |= ISOLATE_CLEAN;
1226f80c0673SMinchan Kim 
12271da177e4SLinus Torvalds 	spin_lock_irq(&zone->lru_lock);
12281da177e4SLinus Torvalds 
12295dc35979SKonstantin Khlebnikov 	nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
12305dc35979SKonstantin Khlebnikov 				     &nr_scanned, sc, isolate_mode, lru);
123195d918fcSKonstantin Khlebnikov 
123295d918fcSKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken);
123395d918fcSKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
123495d918fcSKonstantin Khlebnikov 
123589b5fae5SJohannes Weiner 	if (global_reclaim(sc)) {
1236e247dbceSKOSAKI Motohiro 		zone->pages_scanned += nr_scanned;
1237b35ea17bSKOSAKI Motohiro 		if (current_is_kswapd())
123875b00af7SHugh Dickins 			__count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scanned);
1239b35ea17bSKOSAKI Motohiro 		else
124075b00af7SHugh Dickins 			__count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scanned);
1241b35ea17bSKOSAKI Motohiro 	}
124266635629SMel Gorman 	spin_unlock_irq(&zone->lru_lock);
1243d563c050SHillf Danton 
1244d563c050SHillf Danton 	if (nr_taken == 0)
124566635629SMel Gorman 		return 0;
1246b35ea17bSKOSAKI Motohiro 
12476a18adb3SKonstantin Khlebnikov 	nr_reclaimed = shrink_page_list(&page_list, zone, sc,
124892df3a72SMel Gorman 						&nr_dirty, &nr_writeback);
1249c661b078SAndy Whitcroft 
12503f79768fSHugh Dickins 	spin_lock_irq(&zone->lru_lock);
12513f79768fSHugh Dickins 
125295d918fcSKonstantin Khlebnikov 	reclaim_stat->recent_scanned[file] += nr_taken;
1253d563c050SHillf Danton 
1254904249aaSYing Han 	if (global_reclaim(sc)) {
1255b35ea17bSKOSAKI Motohiro 		if (current_is_kswapd())
1256904249aaSYing Han 			__count_zone_vm_events(PGSTEAL_KSWAPD, zone,
1257904249aaSYing Han 					       nr_reclaimed);
1258904249aaSYing Han 		else
1259904249aaSYing Han 			__count_zone_vm_events(PGSTEAL_DIRECT, zone,
1260904249aaSYing Han 					       nr_reclaimed);
1261904249aaSYing Han 	}
1262a74609faSNick Piggin 
126327ac81d8SKonstantin Khlebnikov 	putback_inactive_pages(lruvec, &page_list);
12643f79768fSHugh Dickins 
126595d918fcSKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
12663f79768fSHugh Dickins 
12673f79768fSHugh Dickins 	spin_unlock_irq(&zone->lru_lock);
12683f79768fSHugh Dickins 
12693f79768fSHugh Dickins 	free_hot_cold_page_list(&page_list, 1);
1270e11da5b4SMel Gorman 
127192df3a72SMel Gorman 	/*
127292df3a72SMel Gorman 	 * If reclaim is isolating dirty pages under writeback, it implies
127392df3a72SMel Gorman 	 * that the long-lived page allocation rate is exceeding the page
127492df3a72SMel Gorman 	 * laundering rate. Either the global limits are not being effective
127592df3a72SMel Gorman 	 * at throttling processes due to the page distribution throughout
127692df3a72SMel Gorman 	 * zones or there is heavy usage of a slow backing device. The
127792df3a72SMel Gorman 	 * only option is to throttle from reclaim context which is not ideal
127892df3a72SMel Gorman 	 * as there is no guarantee the dirtying process is throttled in the
127992df3a72SMel Gorman 	 * same way balance_dirty_pages() manages.
128092df3a72SMel Gorman 	 *
128192df3a72SMel Gorman 	 * This scales the number of dirty pages that must be under writeback
128292df3a72SMel Gorman 	 * before throttling depending on priority. It is a simple backoff
128392df3a72SMel Gorman 	 * function that has the most effect in the range DEF_PRIORITY to
128492df3a72SMel Gorman 	 * DEF_PRIORITY-2 which is the priority reclaim is considered to be
128592df3a72SMel Gorman 	 * in trouble and reclaim is considered to be in trouble.
128692df3a72SMel Gorman 	 *
128792df3a72SMel Gorman 	 * DEF_PRIORITY   100% isolated pages must be PageWriteback to throttle
128892df3a72SMel Gorman 	 * DEF_PRIORITY-1  50% must be PageWriteback
128992df3a72SMel Gorman 	 * DEF_PRIORITY-2  25% must be PageWriteback, kswapd in trouble
129092df3a72SMel Gorman 	 * ...
129192df3a72SMel Gorman 	 * DEF_PRIORITY-6 For SWAP_CLUSTER_MAX isolated pages, throttle if any
129292df3a72SMel Gorman 	 *                     isolated page is PageWriteback
129392df3a72SMel Gorman 	 */
12949e3b2f8cSKonstantin Khlebnikov 	if (nr_writeback && nr_writeback >=
12959e3b2f8cSKonstantin Khlebnikov 			(nr_taken >> (DEF_PRIORITY - sc->priority)))
129692df3a72SMel Gorman 		wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
129792df3a72SMel Gorman 
1298e11da5b4SMel Gorman 	trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id,
1299e11da5b4SMel Gorman 		zone_idx(zone),
1300e11da5b4SMel Gorman 		nr_scanned, nr_reclaimed,
13019e3b2f8cSKonstantin Khlebnikov 		sc->priority,
130223b9da55SMel Gorman 		trace_shrink_flags(file));
130305ff5137SAndrew Morton 	return nr_reclaimed;
13041da177e4SLinus Torvalds }
13051da177e4SLinus Torvalds 
13063bb1a852SMartin Bligh /*
13071cfb419bSKAMEZAWA Hiroyuki  * This moves pages from the active list to the inactive list.
13081cfb419bSKAMEZAWA Hiroyuki  *
13091cfb419bSKAMEZAWA Hiroyuki  * We move them the other way if the page is referenced by one or more
13101cfb419bSKAMEZAWA Hiroyuki  * processes, from rmap.
13111cfb419bSKAMEZAWA Hiroyuki  *
13121cfb419bSKAMEZAWA Hiroyuki  * If the pages are mostly unmapped, the processing is fast and it is
13131cfb419bSKAMEZAWA Hiroyuki  * appropriate to hold zone->lru_lock across the whole operation.  But if
13141cfb419bSKAMEZAWA Hiroyuki  * the pages are mapped, the processing is slow (page_referenced()) so we
13151cfb419bSKAMEZAWA Hiroyuki  * should drop zone->lru_lock around each page.  It's impossible to balance
13161cfb419bSKAMEZAWA Hiroyuki  * this, so instead we remove the pages from the LRU while processing them.
13171cfb419bSKAMEZAWA Hiroyuki  * It is safe to rely on PG_active against the non-LRU pages in here because
13181cfb419bSKAMEZAWA Hiroyuki  * nobody will play with that bit on a non-LRU page.
13191cfb419bSKAMEZAWA Hiroyuki  *
13201cfb419bSKAMEZAWA Hiroyuki  * The downside is that we have to touch page->_count against each page.
13211cfb419bSKAMEZAWA Hiroyuki  * But we had to alter page->flags anyway.
13221cfb419bSKAMEZAWA Hiroyuki  */
13231cfb419bSKAMEZAWA Hiroyuki 
1324fa9add64SHugh Dickins static void move_active_pages_to_lru(struct lruvec *lruvec,
13253eb4140fSWu Fengguang 				     struct list_head *list,
13262bcf8879SHugh Dickins 				     struct list_head *pages_to_free,
13273eb4140fSWu Fengguang 				     enum lru_list lru)
13283eb4140fSWu Fengguang {
1329fa9add64SHugh Dickins 	struct zone *zone = lruvec_zone(lruvec);
13303eb4140fSWu Fengguang 	unsigned long pgmoved = 0;
13313eb4140fSWu Fengguang 	struct page *page;
1332fa9add64SHugh Dickins 	int nr_pages;
13333eb4140fSWu Fengguang 
13343eb4140fSWu Fengguang 	while (!list_empty(list)) {
13353eb4140fSWu Fengguang 		page = lru_to_page(list);
1336fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
13373eb4140fSWu Fengguang 
13383eb4140fSWu Fengguang 		VM_BUG_ON(PageLRU(page));
13393eb4140fSWu Fengguang 		SetPageLRU(page);
13403eb4140fSWu Fengguang 
1341fa9add64SHugh Dickins 		nr_pages = hpage_nr_pages(page);
1342fa9add64SHugh Dickins 		mem_cgroup_update_lru_size(lruvec, lru, nr_pages);
1343925b7673SJohannes Weiner 		list_move(&page->lru, &lruvec->lists[lru]);
1344fa9add64SHugh Dickins 		pgmoved += nr_pages;
13453eb4140fSWu Fengguang 
13462bcf8879SHugh Dickins 		if (put_page_testzero(page)) {
13472bcf8879SHugh Dickins 			__ClearPageLRU(page);
13482bcf8879SHugh Dickins 			__ClearPageActive(page);
1349fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
13502bcf8879SHugh Dickins 
13512bcf8879SHugh Dickins 			if (unlikely(PageCompound(page))) {
13523eb4140fSWu Fengguang 				spin_unlock_irq(&zone->lru_lock);
13532bcf8879SHugh Dickins 				(*get_compound_page_dtor(page))(page);
13543eb4140fSWu Fengguang 				spin_lock_irq(&zone->lru_lock);
13552bcf8879SHugh Dickins 			} else
13562bcf8879SHugh Dickins 				list_add(&page->lru, pages_to_free);
13573eb4140fSWu Fengguang 		}
13583eb4140fSWu Fengguang 	}
13593eb4140fSWu Fengguang 	__mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
13603eb4140fSWu Fengguang 	if (!is_active_lru(lru))
13613eb4140fSWu Fengguang 		__count_vm_events(PGDEACTIVATE, pgmoved);
13623eb4140fSWu Fengguang }
13631cfb419bSKAMEZAWA Hiroyuki 
1364f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan,
13651a93be0eSKonstantin Khlebnikov 			       struct lruvec *lruvec,
1366f16015fbSJohannes Weiner 			       struct scan_control *sc,
13679e3b2f8cSKonstantin Khlebnikov 			       enum lru_list lru)
13681cfb419bSKAMEZAWA Hiroyuki {
136944c241f1SKOSAKI Motohiro 	unsigned long nr_taken;
1370f626012dSHugh Dickins 	unsigned long nr_scanned;
13716fe6b7e3SWu Fengguang 	unsigned long vm_flags;
13721cfb419bSKAMEZAWA Hiroyuki 	LIST_HEAD(l_hold);	/* The pages which were snipped off */
13738cab4754SWu Fengguang 	LIST_HEAD(l_active);
1374b69408e8SChristoph Lameter 	LIST_HEAD(l_inactive);
13751cfb419bSKAMEZAWA Hiroyuki 	struct page *page;
13761a93be0eSKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
137744c241f1SKOSAKI Motohiro 	unsigned long nr_rotated = 0;
1378f3fd4a61SKonstantin Khlebnikov 	isolate_mode_t isolate_mode = 0;
13793cb99451SKonstantin Khlebnikov 	int file = is_file_lru(lru);
13801a93be0eSKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
13811cfb419bSKAMEZAWA Hiroyuki 
13821da177e4SLinus Torvalds 	lru_add_drain();
1383f80c0673SMinchan Kim 
1384f80c0673SMinchan Kim 	if (!sc->may_unmap)
138561317289SHillf Danton 		isolate_mode |= ISOLATE_UNMAPPED;
1386f80c0673SMinchan Kim 	if (!sc->may_writepage)
138761317289SHillf Danton 		isolate_mode |= ISOLATE_CLEAN;
1388f80c0673SMinchan Kim 
13891da177e4SLinus Torvalds 	spin_lock_irq(&zone->lru_lock);
1390925b7673SJohannes Weiner 
13915dc35979SKonstantin Khlebnikov 	nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
13925dc35979SKonstantin Khlebnikov 				     &nr_scanned, sc, isolate_mode, lru);
139389b5fae5SJohannes Weiner 	if (global_reclaim(sc))
1394f626012dSHugh Dickins 		zone->pages_scanned += nr_scanned;
139589b5fae5SJohannes Weiner 
1396b7c46d15SJohannes Weiner 	reclaim_stat->recent_scanned[file] += nr_taken;
13971cfb419bSKAMEZAWA Hiroyuki 
1398f626012dSHugh Dickins 	__count_zone_vm_events(PGREFILL, zone, nr_scanned);
13993cb99451SKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken);
1400a731286dSKOSAKI Motohiro 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
14011da177e4SLinus Torvalds 	spin_unlock_irq(&zone->lru_lock);
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	while (!list_empty(&l_hold)) {
14041da177e4SLinus Torvalds 		cond_resched();
14051da177e4SLinus Torvalds 		page = lru_to_page(&l_hold);
14061da177e4SLinus Torvalds 		list_del(&page->lru);
14077e9cd484SRik van Riel 
1408894bc310SLee Schermerhorn 		if (unlikely(!page_evictable(page, NULL))) {
1409894bc310SLee Schermerhorn 			putback_lru_page(page);
1410894bc310SLee Schermerhorn 			continue;
1411894bc310SLee Schermerhorn 		}
1412894bc310SLee Schermerhorn 
1413cc715d99SMel Gorman 		if (unlikely(buffer_heads_over_limit)) {
1414cc715d99SMel Gorman 			if (page_has_private(page) && trylock_page(page)) {
1415cc715d99SMel Gorman 				if (page_has_private(page))
1416cc715d99SMel Gorman 					try_to_release_page(page, 0);
1417cc715d99SMel Gorman 				unlock_page(page);
1418cc715d99SMel Gorman 			}
1419cc715d99SMel Gorman 		}
1420cc715d99SMel Gorman 
1421c3ac9a8aSJohannes Weiner 		if (page_referenced(page, 0, sc->target_mem_cgroup,
1422c3ac9a8aSJohannes Weiner 				    &vm_flags)) {
14239992af10SRik van Riel 			nr_rotated += hpage_nr_pages(page);
14248cab4754SWu Fengguang 			/*
14258cab4754SWu Fengguang 			 * Identify referenced, file-backed active pages and
14268cab4754SWu Fengguang 			 * give them one more trip around the active list. So
14278cab4754SWu Fengguang 			 * that executable code get better chances to stay in
14288cab4754SWu Fengguang 			 * memory under moderate memory pressure.  Anon pages
14298cab4754SWu Fengguang 			 * are not likely to be evicted by use-once streaming
14308cab4754SWu Fengguang 			 * IO, plus JVM can create lots of anon VM_EXEC pages,
14318cab4754SWu Fengguang 			 * so we ignore them here.
14328cab4754SWu Fengguang 			 */
143341e20983SWu Fengguang 			if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
14348cab4754SWu Fengguang 				list_add(&page->lru, &l_active);
14358cab4754SWu Fengguang 				continue;
14368cab4754SWu Fengguang 			}
14378cab4754SWu Fengguang 		}
14387e9cd484SRik van Riel 
14395205e56eSKOSAKI Motohiro 		ClearPageActive(page);	/* we are de-activating */
14401da177e4SLinus Torvalds 		list_add(&page->lru, &l_inactive);
14411da177e4SLinus Torvalds 	}
14421da177e4SLinus Torvalds 
1443b555749aSAndrew Morton 	/*
14448cab4754SWu Fengguang 	 * Move pages back to the lru list.
1445b555749aSAndrew Morton 	 */
14462a1dc509SJohannes Weiner 	spin_lock_irq(&zone->lru_lock);
14474f98a2feSRik van Riel 	/*
14488cab4754SWu Fengguang 	 * Count referenced pages from currently used mappings as rotated,
14498cab4754SWu Fengguang 	 * even though only some of them are actually re-activated.  This
14508cab4754SWu Fengguang 	 * helps balance scan pressure between file and anonymous pages in
14518cab4754SWu Fengguang 	 * get_scan_ratio.
1452556adecbSRik van Riel 	 */
1453b7c46d15SJohannes Weiner 	reclaim_stat->recent_rotated[file] += nr_rotated;
1454556adecbSRik van Riel 
1455fa9add64SHugh Dickins 	move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru);
1456fa9add64SHugh Dickins 	move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE);
1457a731286dSKOSAKI Motohiro 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
1458f8891e5eSChristoph Lameter 	spin_unlock_irq(&zone->lru_lock);
14592bcf8879SHugh Dickins 
14602bcf8879SHugh Dickins 	free_hot_cold_page_list(&l_hold, 1);
14611da177e4SLinus Torvalds }
14621da177e4SLinus Torvalds 
146374e3f3c3SMinchan Kim #ifdef CONFIG_SWAP
146414797e23SKOSAKI Motohiro static int inactive_anon_is_low_global(struct zone *zone)
1465f89eb90eSKOSAKI Motohiro {
1466f89eb90eSKOSAKI Motohiro 	unsigned long active, inactive;
1467f89eb90eSKOSAKI Motohiro 
1468f89eb90eSKOSAKI Motohiro 	active = zone_page_state(zone, NR_ACTIVE_ANON);
1469f89eb90eSKOSAKI Motohiro 	inactive = zone_page_state(zone, NR_INACTIVE_ANON);
1470f89eb90eSKOSAKI Motohiro 
1471f89eb90eSKOSAKI Motohiro 	if (inactive * zone->inactive_ratio < active)
1472f89eb90eSKOSAKI Motohiro 		return 1;
1473f89eb90eSKOSAKI Motohiro 
1474f89eb90eSKOSAKI Motohiro 	return 0;
1475f89eb90eSKOSAKI Motohiro }
1476f89eb90eSKOSAKI Motohiro 
147714797e23SKOSAKI Motohiro /**
147814797e23SKOSAKI Motohiro  * inactive_anon_is_low - check if anonymous pages need to be deactivated
1479c56d5c7dSKonstantin Khlebnikov  * @lruvec: LRU vector to check
148014797e23SKOSAKI Motohiro  *
148114797e23SKOSAKI Motohiro  * Returns true if the zone does not have enough inactive anon pages,
148214797e23SKOSAKI Motohiro  * meaning some active anon pages need to be deactivated.
148314797e23SKOSAKI Motohiro  */
1484c56d5c7dSKonstantin Khlebnikov static int inactive_anon_is_low(struct lruvec *lruvec)
148514797e23SKOSAKI Motohiro {
148674e3f3c3SMinchan Kim 	/*
148774e3f3c3SMinchan Kim 	 * If we don't have swap space, anonymous page deactivation
148874e3f3c3SMinchan Kim 	 * is pointless.
148974e3f3c3SMinchan Kim 	 */
149074e3f3c3SMinchan Kim 	if (!total_swap_pages)
149174e3f3c3SMinchan Kim 		return 0;
149274e3f3c3SMinchan Kim 
1493c3c787e8SHugh Dickins 	if (!mem_cgroup_disabled())
1494c56d5c7dSKonstantin Khlebnikov 		return mem_cgroup_inactive_anon_is_low(lruvec);
1495f16015fbSJohannes Weiner 
1496c56d5c7dSKonstantin Khlebnikov 	return inactive_anon_is_low_global(lruvec_zone(lruvec));
149714797e23SKOSAKI Motohiro }
149874e3f3c3SMinchan Kim #else
1499c56d5c7dSKonstantin Khlebnikov static inline int inactive_anon_is_low(struct lruvec *lruvec)
150074e3f3c3SMinchan Kim {
150174e3f3c3SMinchan Kim 	return 0;
150274e3f3c3SMinchan Kim }
150374e3f3c3SMinchan Kim #endif
150414797e23SKOSAKI Motohiro 
150556e49d21SRik van Riel static int inactive_file_is_low_global(struct zone *zone)
150656e49d21SRik van Riel {
150756e49d21SRik van Riel 	unsigned long active, inactive;
150856e49d21SRik van Riel 
150956e49d21SRik van Riel 	active = zone_page_state(zone, NR_ACTIVE_FILE);
151056e49d21SRik van Riel 	inactive = zone_page_state(zone, NR_INACTIVE_FILE);
151156e49d21SRik van Riel 
151256e49d21SRik van Riel 	return (active > inactive);
151356e49d21SRik van Riel }
151456e49d21SRik van Riel 
151556e49d21SRik van Riel /**
151656e49d21SRik van Riel  * inactive_file_is_low - check if file pages need to be deactivated
1517c56d5c7dSKonstantin Khlebnikov  * @lruvec: LRU vector to check
151856e49d21SRik van Riel  *
151956e49d21SRik van Riel  * When the system is doing streaming IO, memory pressure here
152056e49d21SRik van Riel  * ensures that active file pages get deactivated, until more
152156e49d21SRik van Riel  * than half of the file pages are on the inactive list.
152256e49d21SRik van Riel  *
152356e49d21SRik van Riel  * Once we get to that situation, protect the system's working
152456e49d21SRik van Riel  * set from being evicted by disabling active file page aging.
152556e49d21SRik van Riel  *
152656e49d21SRik van Riel  * This uses a different ratio than the anonymous pages, because
152756e49d21SRik van Riel  * the page cache uses a use-once replacement algorithm.
152856e49d21SRik van Riel  */
1529c56d5c7dSKonstantin Khlebnikov static int inactive_file_is_low(struct lruvec *lruvec)
153056e49d21SRik van Riel {
1531c3c787e8SHugh Dickins 	if (!mem_cgroup_disabled())
1532c56d5c7dSKonstantin Khlebnikov 		return mem_cgroup_inactive_file_is_low(lruvec);
153356e49d21SRik van Riel 
1534c56d5c7dSKonstantin Khlebnikov 	return inactive_file_is_low_global(lruvec_zone(lruvec));
153556e49d21SRik van Riel }
153656e49d21SRik van Riel 
153775b00af7SHugh Dickins static int inactive_list_is_low(struct lruvec *lruvec, enum lru_list lru)
1538b39415b2SRik van Riel {
153975b00af7SHugh Dickins 	if (is_file_lru(lru))
1540c56d5c7dSKonstantin Khlebnikov 		return inactive_file_is_low(lruvec);
1541b39415b2SRik van Riel 	else
1542c56d5c7dSKonstantin Khlebnikov 		return inactive_anon_is_low(lruvec);
1543b39415b2SRik van Riel }
1544b39415b2SRik van Riel 
15454f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
15461a93be0eSKonstantin Khlebnikov 				 struct lruvec *lruvec, struct scan_control *sc)
1547b69408e8SChristoph Lameter {
1548b39415b2SRik van Riel 	if (is_active_lru(lru)) {
154975b00af7SHugh Dickins 		if (inactive_list_is_low(lruvec, lru))
15501a93be0eSKonstantin Khlebnikov 			shrink_active_list(nr_to_scan, lruvec, sc, lru);
1551556adecbSRik van Riel 		return 0;
1552556adecbSRik van Riel 	}
1553556adecbSRik van Riel 
15541a93be0eSKonstantin Khlebnikov 	return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
1555b69408e8SChristoph Lameter }
1556b69408e8SChristoph Lameter 
15573d58ab5cSKonstantin Khlebnikov static int vmscan_swappiness(struct scan_control *sc)
15581f4c025bSKAMEZAWA Hiroyuki {
155989b5fae5SJohannes Weiner 	if (global_reclaim(sc))
15601f4c025bSKAMEZAWA Hiroyuki 		return vm_swappiness;
15613d58ab5cSKonstantin Khlebnikov 	return mem_cgroup_swappiness(sc->target_mem_cgroup);
15621f4c025bSKAMEZAWA Hiroyuki }
15631f4c025bSKAMEZAWA Hiroyuki 
15641da177e4SLinus Torvalds /*
15654f98a2feSRik van Riel  * Determine how aggressively the anon and file LRU lists should be
15664f98a2feSRik van Riel  * scanned.  The relative value of each set of LRU lists is determined
15674f98a2feSRik van Riel  * by looking at the fraction of the pages scanned we did rotate back
15684f98a2feSRik van Riel  * onto the active list instead of evict.
15694f98a2feSRik van Riel  *
1570be7bd59dSWanpeng Li  * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
1571be7bd59dSWanpeng Li  * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
15724f98a2feSRik van Riel  */
157390126375SKonstantin Khlebnikov static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
15749e3b2f8cSKonstantin Khlebnikov 			   unsigned long *nr)
15754f98a2feSRik van Riel {
15764f98a2feSRik van Riel 	unsigned long anon, file, free;
15774f98a2feSRik van Riel 	unsigned long anon_prio, file_prio;
15784f98a2feSRik van Riel 	unsigned long ap, fp;
157990126375SKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
158076a33fc3SShaohua Li 	u64 fraction[2], denominator;
15814111304dSHugh Dickins 	enum lru_list lru;
158276a33fc3SShaohua Li 	int noswap = 0;
1583a4d3e9e7SJohannes Weiner 	bool force_scan = false;
158490126375SKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
1585246e87a9SKAMEZAWA Hiroyuki 
1586f11c0ca5SJohannes Weiner 	/*
1587f11c0ca5SJohannes Weiner 	 * If the zone or memcg is small, nr[l] can be 0.  This
1588f11c0ca5SJohannes Weiner 	 * results in no scanning on this priority and a potential
1589f11c0ca5SJohannes Weiner 	 * priority drop.  Global direct reclaim can go to the next
1590f11c0ca5SJohannes Weiner 	 * zone and tends to have no problems. Global kswapd is for
1591f11c0ca5SJohannes Weiner 	 * zone balancing and it needs to scan a minimum amount. When
1592f11c0ca5SJohannes Weiner 	 * reclaiming for a memcg, a priority drop can cause high
1593f11c0ca5SJohannes Weiner 	 * latencies, so it's better to scan a minimum amount there as
1594f11c0ca5SJohannes Weiner 	 * well.
1595f11c0ca5SJohannes Weiner 	 */
159690126375SKonstantin Khlebnikov 	if (current_is_kswapd() && zone->all_unreclaimable)
1597a4d3e9e7SJohannes Weiner 		force_scan = true;
159889b5fae5SJohannes Weiner 	if (!global_reclaim(sc))
1599a4d3e9e7SJohannes Weiner 		force_scan = true;
160076a33fc3SShaohua Li 
160176a33fc3SShaohua Li 	/* If we have no swap space, do not bother scanning anon pages. */
160276a33fc3SShaohua Li 	if (!sc->may_swap || (nr_swap_pages <= 0)) {
160376a33fc3SShaohua Li 		noswap = 1;
160476a33fc3SShaohua Li 		fraction[0] = 0;
160576a33fc3SShaohua Li 		fraction[1] = 1;
160676a33fc3SShaohua Li 		denominator = 1;
160776a33fc3SShaohua Li 		goto out;
160876a33fc3SShaohua Li 	}
16094f98a2feSRik van Riel 
16104d7dcca2SHugh Dickins 	anon  = get_lru_size(lruvec, LRU_ACTIVE_ANON) +
16114d7dcca2SHugh Dickins 		get_lru_size(lruvec, LRU_INACTIVE_ANON);
16124d7dcca2SHugh Dickins 	file  = get_lru_size(lruvec, LRU_ACTIVE_FILE) +
16134d7dcca2SHugh Dickins 		get_lru_size(lruvec, LRU_INACTIVE_FILE);
1614a4d3e9e7SJohannes Weiner 
161589b5fae5SJohannes Weiner 	if (global_reclaim(sc)) {
161690126375SKonstantin Khlebnikov 		free  = zone_page_state(zone, NR_FREE_PAGES);
1617eeee9a8cSKOSAKI Motohiro 		/* If we have very few page cache pages,
1618eeee9a8cSKOSAKI Motohiro 		   force-scan anon pages. */
161990126375SKonstantin Khlebnikov 		if (unlikely(file + free <= high_wmark_pages(zone))) {
162076a33fc3SShaohua Li 			fraction[0] = 1;
162176a33fc3SShaohua Li 			fraction[1] = 0;
162276a33fc3SShaohua Li 			denominator = 1;
162376a33fc3SShaohua Li 			goto out;
16244f98a2feSRik van Riel 		}
1625eeee9a8cSKOSAKI Motohiro 	}
16264f98a2feSRik van Riel 
16274f98a2feSRik van Riel 	/*
162858c37f6eSKOSAKI Motohiro 	 * With swappiness at 100, anonymous and file have the same priority.
162958c37f6eSKOSAKI Motohiro 	 * This scanning priority is essentially the inverse of IO cost.
163058c37f6eSKOSAKI Motohiro 	 */
16313d58ab5cSKonstantin Khlebnikov 	anon_prio = vmscan_swappiness(sc);
163275b00af7SHugh Dickins 	file_prio = 200 - anon_prio;
163358c37f6eSKOSAKI Motohiro 
163458c37f6eSKOSAKI Motohiro 	/*
16354f98a2feSRik van Riel 	 * OK, so we have swap space and a fair amount of page cache
16364f98a2feSRik van Riel 	 * pages.  We use the recently rotated / recently scanned
16374f98a2feSRik van Riel 	 * ratios to determine how valuable each cache is.
16384f98a2feSRik van Riel 	 *
16394f98a2feSRik van Riel 	 * Because workloads change over time (and to avoid overflow)
16404f98a2feSRik van Riel 	 * we keep these statistics as a floating average, which ends
16414f98a2feSRik van Riel 	 * up weighing recent references more than old ones.
16424f98a2feSRik van Riel 	 *
16434f98a2feSRik van Riel 	 * anon in [0], file in [1]
16444f98a2feSRik van Riel 	 */
164590126375SKonstantin Khlebnikov 	spin_lock_irq(&zone->lru_lock);
164658c37f6eSKOSAKI Motohiro 	if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
16476e901571SKOSAKI Motohiro 		reclaim_stat->recent_scanned[0] /= 2;
16486e901571SKOSAKI Motohiro 		reclaim_stat->recent_rotated[0] /= 2;
16494f98a2feSRik van Riel 	}
16504f98a2feSRik van Riel 
16516e901571SKOSAKI Motohiro 	if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
16526e901571SKOSAKI Motohiro 		reclaim_stat->recent_scanned[1] /= 2;
16536e901571SKOSAKI Motohiro 		reclaim_stat->recent_rotated[1] /= 2;
16544f98a2feSRik van Riel 	}
16554f98a2feSRik van Riel 
16564f98a2feSRik van Riel 	/*
165700d8089cSRik van Riel 	 * The amount of pressure on anon vs file pages is inversely
165800d8089cSRik van Riel 	 * proportional to the fraction of recently scanned pages on
165900d8089cSRik van Riel 	 * each list that were recently referenced and in active use.
16604f98a2feSRik van Riel 	 */
1661fe35004fSSatoru Moriya 	ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1);
16626e901571SKOSAKI Motohiro 	ap /= reclaim_stat->recent_rotated[0] + 1;
16634f98a2feSRik van Riel 
1664fe35004fSSatoru Moriya 	fp = file_prio * (reclaim_stat->recent_scanned[1] + 1);
16656e901571SKOSAKI Motohiro 	fp /= reclaim_stat->recent_rotated[1] + 1;
166690126375SKonstantin Khlebnikov 	spin_unlock_irq(&zone->lru_lock);
16674f98a2feSRik van Riel 
166876a33fc3SShaohua Li 	fraction[0] = ap;
166976a33fc3SShaohua Li 	fraction[1] = fp;
167076a33fc3SShaohua Li 	denominator = ap + fp + 1;
167176a33fc3SShaohua Li out:
16724111304dSHugh Dickins 	for_each_evictable_lru(lru) {
16734111304dSHugh Dickins 		int file = is_file_lru(lru);
167476a33fc3SShaohua Li 		unsigned long scan;
167576a33fc3SShaohua Li 
16764d7dcca2SHugh Dickins 		scan = get_lru_size(lruvec, lru);
16779e3b2f8cSKonstantin Khlebnikov 		if (sc->priority || noswap || !vmscan_swappiness(sc)) {
16789e3b2f8cSKonstantin Khlebnikov 			scan >>= sc->priority;
1679f11c0ca5SJohannes Weiner 			if (!scan && force_scan)
1680f11c0ca5SJohannes Weiner 				scan = SWAP_CLUSTER_MAX;
168176a33fc3SShaohua Li 			scan = div64_u64(scan * fraction[file], denominator);
16824f98a2feSRik van Riel 		}
16834111304dSHugh Dickins 		nr[lru] = scan;
168476a33fc3SShaohua Li 	}
16856e08a369SWu Fengguang }
16864f98a2feSRik van Riel 
168723b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */
16889e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc)
168923b9da55SMel Gorman {
169023b9da55SMel Gorman 	if (COMPACTION_BUILD && sc->order &&
169123b9da55SMel Gorman 			(sc->order > PAGE_ALLOC_COSTLY_ORDER ||
16929e3b2f8cSKonstantin Khlebnikov 			 sc->priority < DEF_PRIORITY - 2))
169323b9da55SMel Gorman 		return true;
169423b9da55SMel Gorman 
169523b9da55SMel Gorman 	return false;
169623b9da55SMel Gorman }
169723b9da55SMel Gorman 
16984f98a2feSRik van Riel /*
169923b9da55SMel Gorman  * Reclaim/compaction is used for high-order allocation requests. It reclaims
170023b9da55SMel Gorman  * order-0 pages before compacting the zone. should_continue_reclaim() returns
170123b9da55SMel Gorman  * true if more pages should be reclaimed such that when the page allocator
170223b9da55SMel Gorman  * calls try_to_compact_zone() that it will have enough free pages to succeed.
170323b9da55SMel Gorman  * It will give up earlier than that if there is difficulty reclaiming pages.
17043e7d3449SMel Gorman  */
170590bdcfafSKonstantin Khlebnikov static inline bool should_continue_reclaim(struct lruvec *lruvec,
17063e7d3449SMel Gorman 					unsigned long nr_reclaimed,
17073e7d3449SMel Gorman 					unsigned long nr_scanned,
17083e7d3449SMel Gorman 					struct scan_control *sc)
17093e7d3449SMel Gorman {
17103e7d3449SMel Gorman 	unsigned long pages_for_compaction;
17113e7d3449SMel Gorman 	unsigned long inactive_lru_pages;
17123e7d3449SMel Gorman 
17133e7d3449SMel Gorman 	/* If not in reclaim/compaction mode, stop */
17149e3b2f8cSKonstantin Khlebnikov 	if (!in_reclaim_compaction(sc))
17153e7d3449SMel Gorman 		return false;
17163e7d3449SMel Gorman 
17172876592fSMel Gorman 	/* Consider stopping depending on scan and reclaim activity */
17182876592fSMel Gorman 	if (sc->gfp_mask & __GFP_REPEAT) {
17193e7d3449SMel Gorman 		/*
17202876592fSMel Gorman 		 * For __GFP_REPEAT allocations, stop reclaiming if the
17212876592fSMel Gorman 		 * full LRU list has been scanned and we are still failing
17222876592fSMel Gorman 		 * to reclaim pages. This full LRU scan is potentially
17232876592fSMel Gorman 		 * expensive but a __GFP_REPEAT caller really wants to succeed
17243e7d3449SMel Gorman 		 */
17253e7d3449SMel Gorman 		if (!nr_reclaimed && !nr_scanned)
17263e7d3449SMel Gorman 			return false;
17272876592fSMel Gorman 	} else {
17282876592fSMel Gorman 		/*
17292876592fSMel Gorman 		 * For non-__GFP_REPEAT allocations which can presumably
17302876592fSMel Gorman 		 * fail without consequence, stop if we failed to reclaim
17312876592fSMel Gorman 		 * any pages from the last SWAP_CLUSTER_MAX number of
17322876592fSMel Gorman 		 * pages that were scanned. This will return to the
17332876592fSMel Gorman 		 * caller faster at the risk reclaim/compaction and
17342876592fSMel Gorman 		 * the resulting allocation attempt fails
17352876592fSMel Gorman 		 */
17362876592fSMel Gorman 		if (!nr_reclaimed)
17372876592fSMel Gorman 			return false;
17382876592fSMel Gorman 	}
17393e7d3449SMel Gorman 
17403e7d3449SMel Gorman 	/*
17413e7d3449SMel Gorman 	 * If we have not reclaimed enough pages for compaction and the
17423e7d3449SMel Gorman 	 * inactive lists are large enough, continue reclaiming
17433e7d3449SMel Gorman 	 */
17443e7d3449SMel Gorman 	pages_for_compaction = (2UL << sc->order);
17454d7dcca2SHugh Dickins 	inactive_lru_pages = get_lru_size(lruvec, LRU_INACTIVE_FILE);
174686cfd3a4SMinchan Kim 	if (nr_swap_pages > 0)
17474d7dcca2SHugh Dickins 		inactive_lru_pages += get_lru_size(lruvec, LRU_INACTIVE_ANON);
17483e7d3449SMel Gorman 	if (sc->nr_reclaimed < pages_for_compaction &&
17493e7d3449SMel Gorman 			inactive_lru_pages > pages_for_compaction)
17503e7d3449SMel Gorman 		return true;
17513e7d3449SMel Gorman 
17523e7d3449SMel Gorman 	/* If compaction would go ahead or the allocation would succeed, stop */
175390bdcfafSKonstantin Khlebnikov 	switch (compaction_suitable(lruvec_zone(lruvec), sc->order)) {
17543e7d3449SMel Gorman 	case COMPACT_PARTIAL:
17553e7d3449SMel Gorman 	case COMPACT_CONTINUE:
17563e7d3449SMel Gorman 		return false;
17573e7d3449SMel Gorman 	default:
17583e7d3449SMel Gorman 		return true;
17593e7d3449SMel Gorman 	}
17603e7d3449SMel Gorman }
17613e7d3449SMel Gorman 
17623e7d3449SMel Gorman /*
17631da177e4SLinus Torvalds  * This is a basic per-zone page freer.  Used by both kswapd and direct reclaim.
17641da177e4SLinus Torvalds  */
1765f9be23d6SKonstantin Khlebnikov static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
17661da177e4SLinus Torvalds {
1767b69408e8SChristoph Lameter 	unsigned long nr[NR_LRU_LISTS];
17688695949aSChristoph Lameter 	unsigned long nr_to_scan;
17694111304dSHugh Dickins 	enum lru_list lru;
1770f0fdc5e8SJohannes Weiner 	unsigned long nr_reclaimed, nr_scanned;
177122fba335SKOSAKI Motohiro 	unsigned long nr_to_reclaim = sc->nr_to_reclaim;
17723da367c3SShaohua Li 	struct blk_plug plug;
17731da177e4SLinus Torvalds 
17743e7d3449SMel Gorman restart:
17753e7d3449SMel Gorman 	nr_reclaimed = 0;
1776f0fdc5e8SJohannes Weiner 	nr_scanned = sc->nr_scanned;
177790126375SKonstantin Khlebnikov 	get_scan_count(lruvec, sc, nr);
17781cfb419bSKAMEZAWA Hiroyuki 
17793da367c3SShaohua Li 	blk_start_plug(&plug);
1780556adecbSRik van Riel 	while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
1781556adecbSRik van Riel 					nr[LRU_INACTIVE_FILE]) {
17824111304dSHugh Dickins 		for_each_evictable_lru(lru) {
17834111304dSHugh Dickins 			if (nr[lru]) {
1784ece74b2eSKOSAKI Motohiro 				nr_to_scan = min_t(unsigned long,
17854111304dSHugh Dickins 						   nr[lru], SWAP_CLUSTER_MAX);
17864111304dSHugh Dickins 				nr[lru] -= nr_to_scan;
1787b69408e8SChristoph Lameter 
17884111304dSHugh Dickins 				nr_reclaimed += shrink_list(lru, nr_to_scan,
17891a93be0eSKonstantin Khlebnikov 							    lruvec, sc);
17901da177e4SLinus Torvalds 			}
17911da177e4SLinus Torvalds 		}
1792a79311c1SRik van Riel 		/*
1793a79311c1SRik van Riel 		 * On large memory systems, scan >> priority can become
1794a79311c1SRik van Riel 		 * really large. This is fine for the starting priority;
1795a79311c1SRik van Riel 		 * we want to put equal scanning pressure on each zone.
1796a79311c1SRik van Riel 		 * However, if the VM has a harder time of freeing pages,
1797a79311c1SRik van Riel 		 * with multiple processes reclaiming pages, the total
1798a79311c1SRik van Riel 		 * freeing target can get unreasonably large.
1799a79311c1SRik van Riel 		 */
18009e3b2f8cSKonstantin Khlebnikov 		if (nr_reclaimed >= nr_to_reclaim &&
18019e3b2f8cSKonstantin Khlebnikov 		    sc->priority < DEF_PRIORITY)
1802a79311c1SRik van Riel 			break;
18031da177e4SLinus Torvalds 	}
18043da367c3SShaohua Li 	blk_finish_plug(&plug);
18053e7d3449SMel Gorman 	sc->nr_reclaimed += nr_reclaimed;
180601dbe5c9SKOSAKI Motohiro 
1807556adecbSRik van Riel 	/*
1808556adecbSRik van Riel 	 * Even if we did not try to evict anon pages at all, we want to
1809556adecbSRik van Riel 	 * rebalance the anon lru active/inactive ratio.
1810556adecbSRik van Riel 	 */
1811c56d5c7dSKonstantin Khlebnikov 	if (inactive_anon_is_low(lruvec))
18121a93be0eSKonstantin Khlebnikov 		shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
18139e3b2f8cSKonstantin Khlebnikov 				   sc, LRU_ACTIVE_ANON);
1814556adecbSRik van Riel 
18153e7d3449SMel Gorman 	/* reclaim/compaction might need reclaim to continue */
181690bdcfafSKonstantin Khlebnikov 	if (should_continue_reclaim(lruvec, nr_reclaimed,
18179e3b2f8cSKonstantin Khlebnikov 				    sc->nr_scanned - nr_scanned, sc))
18183e7d3449SMel Gorman 		goto restart;
18193e7d3449SMel Gorman 
1820232ea4d6SAndrew Morton 	throttle_vm_writeout(sc->gfp_mask);
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds 
18239e3b2f8cSKonstantin Khlebnikov static void shrink_zone(struct zone *zone, struct scan_control *sc)
1824f16015fbSJohannes Weiner {
18255660048cSJohannes Weiner 	struct mem_cgroup *root = sc->target_mem_cgroup;
18265660048cSJohannes Weiner 	struct mem_cgroup_reclaim_cookie reclaim = {
18275660048cSJohannes Weiner 		.zone = zone,
18289e3b2f8cSKonstantin Khlebnikov 		.priority = sc->priority,
18295660048cSJohannes Weiner 	};
18305660048cSJohannes Weiner 	struct mem_cgroup *memcg;
18315660048cSJohannes Weiner 
18325660048cSJohannes Weiner 	memcg = mem_cgroup_iter(root, NULL, &reclaim);
18335660048cSJohannes Weiner 	do {
1834f9be23d6SKonstantin Khlebnikov 		struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
18355660048cSJohannes Weiner 
1836f9be23d6SKonstantin Khlebnikov 		shrink_lruvec(lruvec, sc);
1837f9be23d6SKonstantin Khlebnikov 
18385660048cSJohannes Weiner 		/*
18395660048cSJohannes Weiner 		 * Limit reclaim has historically picked one memcg and
18405660048cSJohannes Weiner 		 * scanned it with decreasing priority levels until
18415660048cSJohannes Weiner 		 * nr_to_reclaim had been reclaimed.  This priority
18425660048cSJohannes Weiner 		 * cycle is thus over after a single memcg.
1843b95a2f2dSJohannes Weiner 		 *
1844b95a2f2dSJohannes Weiner 		 * Direct reclaim and kswapd, on the other hand, have
1845b95a2f2dSJohannes Weiner 		 * to scan all memory cgroups to fulfill the overall
1846b95a2f2dSJohannes Weiner 		 * scan target for the zone.
18475660048cSJohannes Weiner 		 */
18485660048cSJohannes Weiner 		if (!global_reclaim(sc)) {
18495660048cSJohannes Weiner 			mem_cgroup_iter_break(root, memcg);
18505660048cSJohannes Weiner 			break;
18515660048cSJohannes Weiner 		}
18525660048cSJohannes Weiner 		memcg = mem_cgroup_iter(root, memcg, &reclaim);
18535660048cSJohannes Weiner 	} while (memcg);
1854f16015fbSJohannes Weiner }
1855f16015fbSJohannes Weiner 
1856fe4b1b24SMel Gorman /* Returns true if compaction should go ahead for a high-order request */
1857fe4b1b24SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
1858fe4b1b24SMel Gorman {
1859fe4b1b24SMel Gorman 	unsigned long balance_gap, watermark;
1860fe4b1b24SMel Gorman 	bool watermark_ok;
1861fe4b1b24SMel Gorman 
1862fe4b1b24SMel Gorman 	/* Do not consider compaction for orders reclaim is meant to satisfy */
1863fe4b1b24SMel Gorman 	if (sc->order <= PAGE_ALLOC_COSTLY_ORDER)
1864fe4b1b24SMel Gorman 		return false;
1865fe4b1b24SMel Gorman 
1866fe4b1b24SMel Gorman 	/*
1867fe4b1b24SMel Gorman 	 * Compaction takes time to run and there are potentially other
1868fe4b1b24SMel Gorman 	 * callers using the pages just freed. Continue reclaiming until
1869fe4b1b24SMel Gorman 	 * there is a buffer of free pages available to give compaction
1870fe4b1b24SMel Gorman 	 * a reasonable chance of completing and allocating the page
1871fe4b1b24SMel Gorman 	 */
1872fe4b1b24SMel Gorman 	balance_gap = min(low_wmark_pages(zone),
1873fe4b1b24SMel Gorman 		(zone->present_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
1874fe4b1b24SMel Gorman 			KSWAPD_ZONE_BALANCE_GAP_RATIO);
1875fe4b1b24SMel Gorman 	watermark = high_wmark_pages(zone) + balance_gap + (2UL << sc->order);
1876fe4b1b24SMel Gorman 	watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0);
1877fe4b1b24SMel Gorman 
1878fe4b1b24SMel Gorman 	/*
1879fe4b1b24SMel Gorman 	 * If compaction is deferred, reclaim up to a point where
1880fe4b1b24SMel Gorman 	 * compaction will have a chance of success when re-enabled
1881fe4b1b24SMel Gorman 	 */
1882aff62249SRik van Riel 	if (compaction_deferred(zone, sc->order))
1883fe4b1b24SMel Gorman 		return watermark_ok;
1884fe4b1b24SMel Gorman 
1885fe4b1b24SMel Gorman 	/* If compaction is not ready to start, keep reclaiming */
1886fe4b1b24SMel Gorman 	if (!compaction_suitable(zone, sc->order))
1887fe4b1b24SMel Gorman 		return false;
1888fe4b1b24SMel Gorman 
1889fe4b1b24SMel Gorman 	return watermark_ok;
1890fe4b1b24SMel Gorman }
1891fe4b1b24SMel Gorman 
18921da177e4SLinus Torvalds /*
18931da177e4SLinus Torvalds  * This is the direct reclaim path, for page-allocating processes.  We only
18941da177e4SLinus Torvalds  * try to reclaim pages from zones which will satisfy the caller's allocation
18951da177e4SLinus Torvalds  * request.
18961da177e4SLinus Torvalds  *
189741858966SMel Gorman  * We reclaim from a zone even if that zone is over high_wmark_pages(zone).
189841858966SMel Gorman  * Because:
18991da177e4SLinus Torvalds  * a) The caller may be trying to free *extra* pages to satisfy a higher-order
19001da177e4SLinus Torvalds  *    allocation or
190141858966SMel Gorman  * b) The target zone may be at high_wmark_pages(zone) but the lower zones
190241858966SMel Gorman  *    must go *over* high_wmark_pages(zone) to satisfy the `incremental min'
190341858966SMel Gorman  *    zone defense algorithm.
19041da177e4SLinus Torvalds  *
19051da177e4SLinus Torvalds  * If a zone is deemed to be full of pinned pages then just give it a light
19061da177e4SLinus Torvalds  * scan then give up on it.
1907e0c23279SMel Gorman  *
1908e0c23279SMel Gorman  * This function returns true if a zone is being reclaimed for a costly
1909fe4b1b24SMel Gorman  * high-order allocation and compaction is ready to begin. This indicates to
19100cee34fdSMel Gorman  * the caller that it should consider retrying the allocation instead of
19110cee34fdSMel Gorman  * further reclaim.
19121da177e4SLinus Torvalds  */
19139e3b2f8cSKonstantin Khlebnikov static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
19141da177e4SLinus Torvalds {
1915dd1a239fSMel Gorman 	struct zoneref *z;
191654a6eb5cSMel Gorman 	struct zone *zone;
1917d149e3b2SYing Han 	unsigned long nr_soft_reclaimed;
1918d149e3b2SYing Han 	unsigned long nr_soft_scanned;
19190cee34fdSMel Gorman 	bool aborted_reclaim = false;
19201cfb419bSKAMEZAWA Hiroyuki 
1921cc715d99SMel Gorman 	/*
1922cc715d99SMel Gorman 	 * If the number of buffer_heads in the machine exceeds the maximum
1923cc715d99SMel Gorman 	 * allowed level, force direct reclaim to scan the highmem zone as
1924cc715d99SMel Gorman 	 * highmem pages could be pinning lowmem pages storing buffer_heads
1925cc715d99SMel Gorman 	 */
1926cc715d99SMel Gorman 	if (buffer_heads_over_limit)
1927cc715d99SMel Gorman 		sc->gfp_mask |= __GFP_HIGHMEM;
1928cc715d99SMel Gorman 
1929d4debc66SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
1930d4debc66SMel Gorman 					gfp_zone(sc->gfp_mask), sc->nodemask) {
1931f3fe6512SCon Kolivas 		if (!populated_zone(zone))
19321da177e4SLinus Torvalds 			continue;
19331cfb419bSKAMEZAWA Hiroyuki 		/*
19341cfb419bSKAMEZAWA Hiroyuki 		 * Take care memory controller reclaiming has small influence
19351cfb419bSKAMEZAWA Hiroyuki 		 * to global LRU.
19361cfb419bSKAMEZAWA Hiroyuki 		 */
193789b5fae5SJohannes Weiner 		if (global_reclaim(sc)) {
193802a0e53dSPaul Jackson 			if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
19391da177e4SLinus Torvalds 				continue;
19409e3b2f8cSKonstantin Khlebnikov 			if (zone->all_unreclaimable &&
19419e3b2f8cSKonstantin Khlebnikov 					sc->priority != DEF_PRIORITY)
19421da177e4SLinus Torvalds 				continue;	/* Let kswapd poll it */
1943e0887c19SRik van Riel 			if (COMPACTION_BUILD) {
1944e0887c19SRik van Riel 				/*
1945e0c23279SMel Gorman 				 * If we already have plenty of memory free for
1946e0c23279SMel Gorman 				 * compaction in this zone, don't free any more.
1947e0c23279SMel Gorman 				 * Even though compaction is invoked for any
1948e0c23279SMel Gorman 				 * non-zero order, only frequent costly order
1949e0c23279SMel Gorman 				 * reclamation is disruptive enough to become a
1950c7cfa37bSCopot Alexandru 				 * noticeable problem, like transparent huge
1951c7cfa37bSCopot Alexandru 				 * page allocations.
1952e0887c19SRik van Riel 				 */
1953fe4b1b24SMel Gorman 				if (compaction_ready(zone, sc)) {
19540cee34fdSMel Gorman 					aborted_reclaim = true;
1955e0887c19SRik van Riel 					continue;
1956e0887c19SRik van Riel 				}
1957e0c23279SMel Gorman 			}
1958ac34a1a3SKAMEZAWA Hiroyuki 			/*
1959ac34a1a3SKAMEZAWA Hiroyuki 			 * This steals pages from memory cgroups over softlimit
1960ac34a1a3SKAMEZAWA Hiroyuki 			 * and returns the number of reclaimed pages and
1961ac34a1a3SKAMEZAWA Hiroyuki 			 * scanned pages. This works for global memory pressure
1962ac34a1a3SKAMEZAWA Hiroyuki 			 * and balancing, not for a memcg's limit.
1963ac34a1a3SKAMEZAWA Hiroyuki 			 */
1964d149e3b2SYing Han 			nr_soft_scanned = 0;
1965d149e3b2SYing Han 			nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
1966d149e3b2SYing Han 						sc->order, sc->gfp_mask,
1967d149e3b2SYing Han 						&nr_soft_scanned);
1968d149e3b2SYing Han 			sc->nr_reclaimed += nr_soft_reclaimed;
1969ac34a1a3SKAMEZAWA Hiroyuki 			sc->nr_scanned += nr_soft_scanned;
1970ac34a1a3SKAMEZAWA Hiroyuki 			/* need some check for avoid more shrink_zone() */
1971ac34a1a3SKAMEZAWA Hiroyuki 		}
1972d149e3b2SYing Han 
19739e3b2f8cSKonstantin Khlebnikov 		shrink_zone(zone, sc);
19741da177e4SLinus Torvalds 	}
1975e0c23279SMel Gorman 
19760cee34fdSMel Gorman 	return aborted_reclaim;
1977d1908362SMinchan Kim }
1978d1908362SMinchan Kim 
1979d1908362SMinchan Kim static bool zone_reclaimable(struct zone *zone)
1980d1908362SMinchan Kim {
1981d1908362SMinchan Kim 	return zone->pages_scanned < zone_reclaimable_pages(zone) * 6;
1982d1908362SMinchan Kim }
1983d1908362SMinchan Kim 
1984929bea7cSKOSAKI Motohiro /* All zones in zonelist are unreclaimable? */
1985d1908362SMinchan Kim static bool all_unreclaimable(struct zonelist *zonelist,
1986d1908362SMinchan Kim 		struct scan_control *sc)
1987d1908362SMinchan Kim {
1988d1908362SMinchan Kim 	struct zoneref *z;
1989d1908362SMinchan Kim 	struct zone *zone;
1990d1908362SMinchan Kim 
1991d1908362SMinchan Kim 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
1992d1908362SMinchan Kim 			gfp_zone(sc->gfp_mask), sc->nodemask) {
1993d1908362SMinchan Kim 		if (!populated_zone(zone))
1994d1908362SMinchan Kim 			continue;
1995d1908362SMinchan Kim 		if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1996d1908362SMinchan Kim 			continue;
1997929bea7cSKOSAKI Motohiro 		if (!zone->all_unreclaimable)
1998929bea7cSKOSAKI Motohiro 			return false;
1999d1908362SMinchan Kim 	}
2000d1908362SMinchan Kim 
2001929bea7cSKOSAKI Motohiro 	return true;
20021da177e4SLinus Torvalds }
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds /*
20051da177e4SLinus Torvalds  * This is the main entry point to direct page reclaim.
20061da177e4SLinus Torvalds  *
20071da177e4SLinus Torvalds  * If a full scan of the inactive list fails to free enough memory then we
20081da177e4SLinus Torvalds  * are "out of memory" and something needs to be killed.
20091da177e4SLinus Torvalds  *
20101da177e4SLinus Torvalds  * If the caller is !__GFP_FS then the probability of a failure is reasonably
20111da177e4SLinus Torvalds  * high - the zone may be full of dirty or under-writeback pages, which this
20125b0830cbSJens Axboe  * caller can't do much about.  We kick the writeback threads and take explicit
20135b0830cbSJens Axboe  * naps in the hope that some of these pages can be written.  But if the
20145b0830cbSJens Axboe  * allocating task holds filesystem locks which prevent writeout this might not
20155b0830cbSJens Axboe  * work, and the allocation attempt will fail.
2016a41f24eaSNishanth Aravamudan  *
2017a41f24eaSNishanth Aravamudan  * returns:	0, if no pages reclaimed
2018a41f24eaSNishanth Aravamudan  * 		else, the number of pages reclaimed
20191da177e4SLinus Torvalds  */
2020dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
2021a09ed5e0SYing Han 					struct scan_control *sc,
2022a09ed5e0SYing Han 					struct shrink_control *shrink)
20231da177e4SLinus Torvalds {
202469e05944SAndrew Morton 	unsigned long total_scanned = 0;
20251da177e4SLinus Torvalds 	struct reclaim_state *reclaim_state = current->reclaim_state;
2026dd1a239fSMel Gorman 	struct zoneref *z;
202754a6eb5cSMel Gorman 	struct zone *zone;
202822fba335SKOSAKI Motohiro 	unsigned long writeback_threshold;
20290cee34fdSMel Gorman 	bool aborted_reclaim;
20301da177e4SLinus Torvalds 
2031873b4771SKeika Kobayashi 	delayacct_freepages_start();
2032873b4771SKeika Kobayashi 
203389b5fae5SJohannes Weiner 	if (global_reclaim(sc))
2034f8891e5eSChristoph Lameter 		count_vm_event(ALLOCSTALL);
20351da177e4SLinus Torvalds 
20369e3b2f8cSKonstantin Khlebnikov 	do {
203766e1707bSBalbir Singh 		sc->nr_scanned = 0;
20389e3b2f8cSKonstantin Khlebnikov 		aborted_reclaim = shrink_zones(zonelist, sc);
2039e0c23279SMel Gorman 
204066e1707bSBalbir Singh 		/*
204166e1707bSBalbir Singh 		 * Don't shrink slabs when reclaiming memory from
204266e1707bSBalbir Singh 		 * over limit cgroups
204366e1707bSBalbir Singh 		 */
204489b5fae5SJohannes Weiner 		if (global_reclaim(sc)) {
2045c6a8a8c5SKOSAKI Motohiro 			unsigned long lru_pages = 0;
2046d4debc66SMel Gorman 			for_each_zone_zonelist(zone, z, zonelist,
2047d4debc66SMel Gorman 					gfp_zone(sc->gfp_mask)) {
2048c6a8a8c5SKOSAKI Motohiro 				if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
2049c6a8a8c5SKOSAKI Motohiro 					continue;
2050c6a8a8c5SKOSAKI Motohiro 
2051c6a8a8c5SKOSAKI Motohiro 				lru_pages += zone_reclaimable_pages(zone);
2052c6a8a8c5SKOSAKI Motohiro 			}
2053c6a8a8c5SKOSAKI Motohiro 
20541495f230SYing Han 			shrink_slab(shrink, sc->nr_scanned, lru_pages);
20551da177e4SLinus Torvalds 			if (reclaim_state) {
2056a79311c1SRik van Riel 				sc->nr_reclaimed += reclaim_state->reclaimed_slab;
20571da177e4SLinus Torvalds 				reclaim_state->reclaimed_slab = 0;
20581da177e4SLinus Torvalds 			}
205991a45470SKAMEZAWA Hiroyuki 		}
206066e1707bSBalbir Singh 		total_scanned += sc->nr_scanned;
2061bb21c7ceSKOSAKI Motohiro 		if (sc->nr_reclaimed >= sc->nr_to_reclaim)
20621da177e4SLinus Torvalds 			goto out;
20631da177e4SLinus Torvalds 
20641da177e4SLinus Torvalds 		/*
20651da177e4SLinus Torvalds 		 * Try to write back as many pages as we just scanned.  This
20661da177e4SLinus Torvalds 		 * tends to cause slow streaming writers to write data to the
20671da177e4SLinus Torvalds 		 * disk smoothly, at the dirtying rate, which is nice.   But
20681da177e4SLinus Torvalds 		 * that's undesirable in laptop mode, where we *want* lumpy
20691da177e4SLinus Torvalds 		 * writeout.  So in laptop mode, write out the whole world.
20701da177e4SLinus Torvalds 		 */
207122fba335SKOSAKI Motohiro 		writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
207222fba335SKOSAKI Motohiro 		if (total_scanned > writeback_threshold) {
20730e175a18SCurt Wohlgemuth 			wakeup_flusher_threads(laptop_mode ? 0 : total_scanned,
20740e175a18SCurt Wohlgemuth 						WB_REASON_TRY_TO_FREE_PAGES);
207566e1707bSBalbir Singh 			sc->may_writepage = 1;
20761da177e4SLinus Torvalds 		}
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 		/* Take a nap, wait for some writeback to complete */
20797b51755cSKOSAKI Motohiro 		if (!sc->hibernation_mode && sc->nr_scanned &&
20809e3b2f8cSKonstantin Khlebnikov 		    sc->priority < DEF_PRIORITY - 2) {
20810e093d99SMel Gorman 			struct zone *preferred_zone;
20820e093d99SMel Gorman 
20830e093d99SMel Gorman 			first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask),
2084f33261d7SDavid Rientjes 						&cpuset_current_mems_allowed,
2085f33261d7SDavid Rientjes 						&preferred_zone);
20860e093d99SMel Gorman 			wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10);
20870e093d99SMel Gorman 		}
20889e3b2f8cSKonstantin Khlebnikov 	} while (--sc->priority >= 0);
2089bb21c7ceSKOSAKI Motohiro 
20901da177e4SLinus Torvalds out:
2091873b4771SKeika Kobayashi 	delayacct_freepages_end();
2092873b4771SKeika Kobayashi 
2093bb21c7ceSKOSAKI Motohiro 	if (sc->nr_reclaimed)
2094bb21c7ceSKOSAKI Motohiro 		return sc->nr_reclaimed;
2095bb21c7ceSKOSAKI Motohiro 
2096929bea7cSKOSAKI Motohiro 	/*
2097929bea7cSKOSAKI Motohiro 	 * As hibernation is going on, kswapd is freezed so that it can't mark
2098929bea7cSKOSAKI Motohiro 	 * the zone into all_unreclaimable. Thus bypassing all_unreclaimable
2099929bea7cSKOSAKI Motohiro 	 * check.
2100929bea7cSKOSAKI Motohiro 	 */
2101929bea7cSKOSAKI Motohiro 	if (oom_killer_disabled)
2102929bea7cSKOSAKI Motohiro 		return 0;
2103929bea7cSKOSAKI Motohiro 
21040cee34fdSMel Gorman 	/* Aborted reclaim to try compaction? don't OOM, then */
21050cee34fdSMel Gorman 	if (aborted_reclaim)
21067335084dSMel Gorman 		return 1;
21077335084dSMel Gorman 
2108bb21c7ceSKOSAKI Motohiro 	/* top priority shrink_zones still had more to do? don't OOM, then */
210989b5fae5SJohannes Weiner 	if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc))
2110bb21c7ceSKOSAKI Motohiro 		return 1;
2111bb21c7ceSKOSAKI Motohiro 
2112bb21c7ceSKOSAKI Motohiro 	return 0;
21131da177e4SLinus Torvalds }
21141da177e4SLinus Torvalds 
21155515061dSMel Gorman static bool pfmemalloc_watermark_ok(pg_data_t *pgdat)
21165515061dSMel Gorman {
21175515061dSMel Gorman 	struct zone *zone;
21185515061dSMel Gorman 	unsigned long pfmemalloc_reserve = 0;
21195515061dSMel Gorman 	unsigned long free_pages = 0;
21205515061dSMel Gorman 	int i;
21215515061dSMel Gorman 	bool wmark_ok;
21225515061dSMel Gorman 
21235515061dSMel Gorman 	for (i = 0; i <= ZONE_NORMAL; i++) {
21245515061dSMel Gorman 		zone = &pgdat->node_zones[i];
21255515061dSMel Gorman 		pfmemalloc_reserve += min_wmark_pages(zone);
21265515061dSMel Gorman 		free_pages += zone_page_state(zone, NR_FREE_PAGES);
21275515061dSMel Gorman 	}
21285515061dSMel Gorman 
21295515061dSMel Gorman 	wmark_ok = free_pages > pfmemalloc_reserve / 2;
21305515061dSMel Gorman 
21315515061dSMel Gorman 	/* kswapd must be awake if processes are being throttled */
21325515061dSMel Gorman 	if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
21335515061dSMel Gorman 		pgdat->classzone_idx = min(pgdat->classzone_idx,
21345515061dSMel Gorman 						(enum zone_type)ZONE_NORMAL);
21355515061dSMel Gorman 		wake_up_interruptible(&pgdat->kswapd_wait);
21365515061dSMel Gorman 	}
21375515061dSMel Gorman 
21385515061dSMel Gorman 	return wmark_ok;
21395515061dSMel Gorman }
21405515061dSMel Gorman 
21415515061dSMel Gorman /*
21425515061dSMel Gorman  * Throttle direct reclaimers if backing storage is backed by the network
21435515061dSMel Gorman  * and the PFMEMALLOC reserve for the preferred node is getting dangerously
21445515061dSMel Gorman  * depleted. kswapd will continue to make progress and wake the processes
21455515061dSMel Gorman  * when the low watermark is reached
21465515061dSMel Gorman  */
21475515061dSMel Gorman static void throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
21485515061dSMel Gorman 					nodemask_t *nodemask)
21495515061dSMel Gorman {
21505515061dSMel Gorman 	struct zone *zone;
21515515061dSMel Gorman 	int high_zoneidx = gfp_zone(gfp_mask);
21525515061dSMel Gorman 	pg_data_t *pgdat;
21535515061dSMel Gorman 
21545515061dSMel Gorman 	/*
21555515061dSMel Gorman 	 * Kernel threads should not be throttled as they may be indirectly
21565515061dSMel Gorman 	 * responsible for cleaning pages necessary for reclaim to make forward
21575515061dSMel Gorman 	 * progress. kjournald for example may enter direct reclaim while
21585515061dSMel Gorman 	 * committing a transaction where throttling it could forcing other
21595515061dSMel Gorman 	 * processes to block on log_wait_commit().
21605515061dSMel Gorman 	 */
21615515061dSMel Gorman 	if (current->flags & PF_KTHREAD)
21625515061dSMel Gorman 		return;
21635515061dSMel Gorman 
21645515061dSMel Gorman 	/* Check if the pfmemalloc reserves are ok */
21655515061dSMel Gorman 	first_zones_zonelist(zonelist, high_zoneidx, NULL, &zone);
21665515061dSMel Gorman 	pgdat = zone->zone_pgdat;
21675515061dSMel Gorman 	if (pfmemalloc_watermark_ok(pgdat))
21685515061dSMel Gorman 		return;
21695515061dSMel Gorman 
2170*68243e76SMel Gorman 	/* Account for the throttling */
2171*68243e76SMel Gorman 	count_vm_event(PGSCAN_DIRECT_THROTTLE);
2172*68243e76SMel Gorman 
21735515061dSMel Gorman 	/*
21745515061dSMel Gorman 	 * If the caller cannot enter the filesystem, it's possible that it
21755515061dSMel Gorman 	 * is due to the caller holding an FS lock or performing a journal
21765515061dSMel Gorman 	 * transaction in the case of a filesystem like ext[3|4]. In this case,
21775515061dSMel Gorman 	 * it is not safe to block on pfmemalloc_wait as kswapd could be
21785515061dSMel Gorman 	 * blocked waiting on the same lock. Instead, throttle for up to a
21795515061dSMel Gorman 	 * second before continuing.
21805515061dSMel Gorman 	 */
21815515061dSMel Gorman 	if (!(gfp_mask & __GFP_FS)) {
21825515061dSMel Gorman 		wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
21835515061dSMel Gorman 			pfmemalloc_watermark_ok(pgdat), HZ);
21845515061dSMel Gorman 		return;
21855515061dSMel Gorman 	}
21865515061dSMel Gorman 
21875515061dSMel Gorman 	/* Throttle until kswapd wakes the process */
21885515061dSMel Gorman 	wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
21895515061dSMel Gorman 		pfmemalloc_watermark_ok(pgdat));
21905515061dSMel Gorman }
21915515061dSMel Gorman 
2192dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
2193327c0e96SKAMEZAWA Hiroyuki 				gfp_t gfp_mask, nodemask_t *nodemask)
219466e1707bSBalbir Singh {
219533906bc5SMel Gorman 	unsigned long nr_reclaimed;
219666e1707bSBalbir Singh 	struct scan_control sc = {
219766e1707bSBalbir Singh 		.gfp_mask = gfp_mask,
219866e1707bSBalbir Singh 		.may_writepage = !laptop_mode,
219922fba335SKOSAKI Motohiro 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
2200a6dc60f8SJohannes Weiner 		.may_unmap = 1,
22012e2e4259SKOSAKI Motohiro 		.may_swap = 1,
220266e1707bSBalbir Singh 		.order = order,
22039e3b2f8cSKonstantin Khlebnikov 		.priority = DEF_PRIORITY,
2204f16015fbSJohannes Weiner 		.target_mem_cgroup = NULL,
2205327c0e96SKAMEZAWA Hiroyuki 		.nodemask = nodemask,
220666e1707bSBalbir Singh 	};
2207a09ed5e0SYing Han 	struct shrink_control shrink = {
2208a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
2209a09ed5e0SYing Han 	};
221066e1707bSBalbir Singh 
22115515061dSMel Gorman 	throttle_direct_reclaim(gfp_mask, zonelist, nodemask);
22125515061dSMel Gorman 
22135515061dSMel Gorman 	/*
22145515061dSMel Gorman 	 * Do not enter reclaim if fatal signal is pending. 1 is returned so
22155515061dSMel Gorman 	 * that the page allocator does not consider triggering OOM
22165515061dSMel Gorman 	 */
22175515061dSMel Gorman 	if (fatal_signal_pending(current))
22185515061dSMel Gorman 		return 1;
22195515061dSMel Gorman 
222033906bc5SMel Gorman 	trace_mm_vmscan_direct_reclaim_begin(order,
222133906bc5SMel Gorman 				sc.may_writepage,
222233906bc5SMel Gorman 				gfp_mask);
222333906bc5SMel Gorman 
2224a09ed5e0SYing Han 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
222533906bc5SMel Gorman 
222633906bc5SMel Gorman 	trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
222733906bc5SMel Gorman 
222833906bc5SMel Gorman 	return nr_reclaimed;
222966e1707bSBalbir Singh }
223066e1707bSBalbir Singh 
2231c255a458SAndrew Morton #ifdef CONFIG_MEMCG
223266e1707bSBalbir Singh 
223372835c86SJohannes Weiner unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg,
22344e416953SBalbir Singh 						gfp_t gfp_mask, bool noswap,
22350ae5e89cSYing Han 						struct zone *zone,
22360ae5e89cSYing Han 						unsigned long *nr_scanned)
22374e416953SBalbir Singh {
22384e416953SBalbir Singh 	struct scan_control sc = {
22390ae5e89cSYing Han 		.nr_scanned = 0,
2240b8f5c566SKOSAKI Motohiro 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
22414e416953SBalbir Singh 		.may_writepage = !laptop_mode,
22424e416953SBalbir Singh 		.may_unmap = 1,
22434e416953SBalbir Singh 		.may_swap = !noswap,
22444e416953SBalbir Singh 		.order = 0,
22459e3b2f8cSKonstantin Khlebnikov 		.priority = 0,
224672835c86SJohannes Weiner 		.target_mem_cgroup = memcg,
22474e416953SBalbir Singh 	};
2248f9be23d6SKonstantin Khlebnikov 	struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
22490ae5e89cSYing Han 
22504e416953SBalbir Singh 	sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
22514e416953SBalbir Singh 			(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
2252bdce6d9eSKOSAKI Motohiro 
22539e3b2f8cSKonstantin Khlebnikov 	trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
2254bdce6d9eSKOSAKI Motohiro 						      sc.may_writepage,
2255bdce6d9eSKOSAKI Motohiro 						      sc.gfp_mask);
2256bdce6d9eSKOSAKI Motohiro 
22574e416953SBalbir Singh 	/*
22584e416953SBalbir Singh 	 * NOTE: Although we can get the priority field, using it
22594e416953SBalbir Singh 	 * here is not a good idea, since it limits the pages we can scan.
22604e416953SBalbir Singh 	 * if we don't reclaim here, the shrink_zone from balance_pgdat
22614e416953SBalbir Singh 	 * will pick up pages from other mem cgroup's as well. We hack
22624e416953SBalbir Singh 	 * the priority and make it zero.
22634e416953SBalbir Singh 	 */
2264f9be23d6SKonstantin Khlebnikov 	shrink_lruvec(lruvec, &sc);
2265bdce6d9eSKOSAKI Motohiro 
2266bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
2267bdce6d9eSKOSAKI Motohiro 
22680ae5e89cSYing Han 	*nr_scanned = sc.nr_scanned;
22694e416953SBalbir Singh 	return sc.nr_reclaimed;
22704e416953SBalbir Singh }
22714e416953SBalbir Singh 
227272835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
22738c7c6e34SKAMEZAWA Hiroyuki 					   gfp_t gfp_mask,
2274185efc0fSJohannes Weiner 					   bool noswap)
227566e1707bSBalbir Singh {
22764e416953SBalbir Singh 	struct zonelist *zonelist;
2277bdce6d9eSKOSAKI Motohiro 	unsigned long nr_reclaimed;
2278889976dbSYing Han 	int nid;
227966e1707bSBalbir Singh 	struct scan_control sc = {
228066e1707bSBalbir Singh 		.may_writepage = !laptop_mode,
2281a6dc60f8SJohannes Weiner 		.may_unmap = 1,
22822e2e4259SKOSAKI Motohiro 		.may_swap = !noswap,
228322fba335SKOSAKI Motohiro 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
228466e1707bSBalbir Singh 		.order = 0,
22859e3b2f8cSKonstantin Khlebnikov 		.priority = DEF_PRIORITY,
228672835c86SJohannes Weiner 		.target_mem_cgroup = memcg,
2287327c0e96SKAMEZAWA Hiroyuki 		.nodemask = NULL, /* we don't care the placement */
2288a09ed5e0SYing Han 		.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
2289a09ed5e0SYing Han 				(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
2290a09ed5e0SYing Han 	};
2291a09ed5e0SYing Han 	struct shrink_control shrink = {
2292a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
229366e1707bSBalbir Singh 	};
229466e1707bSBalbir Singh 
2295889976dbSYing Han 	/*
2296889976dbSYing Han 	 * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't
2297889976dbSYing Han 	 * take care of from where we get pages. So the node where we start the
2298889976dbSYing Han 	 * scan does not need to be the current node.
2299889976dbSYing Han 	 */
230072835c86SJohannes Weiner 	nid = mem_cgroup_select_victim_node(memcg);
2301889976dbSYing Han 
2302889976dbSYing Han 	zonelist = NODE_DATA(nid)->node_zonelists;
2303bdce6d9eSKOSAKI Motohiro 
2304bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_reclaim_begin(0,
2305bdce6d9eSKOSAKI Motohiro 					    sc.may_writepage,
2306bdce6d9eSKOSAKI Motohiro 					    sc.gfp_mask);
2307bdce6d9eSKOSAKI Motohiro 
2308a09ed5e0SYing Han 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
2309bdce6d9eSKOSAKI Motohiro 
2310bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
2311bdce6d9eSKOSAKI Motohiro 
2312bdce6d9eSKOSAKI Motohiro 	return nr_reclaimed;
231366e1707bSBalbir Singh }
231466e1707bSBalbir Singh #endif
231566e1707bSBalbir Singh 
23169e3b2f8cSKonstantin Khlebnikov static void age_active_anon(struct zone *zone, struct scan_control *sc)
2317f16015fbSJohannes Weiner {
2318b95a2f2dSJohannes Weiner 	struct mem_cgroup *memcg;
2319b95a2f2dSJohannes Weiner 
2320b95a2f2dSJohannes Weiner 	if (!total_swap_pages)
2321b95a2f2dSJohannes Weiner 		return;
2322b95a2f2dSJohannes Weiner 
2323b95a2f2dSJohannes Weiner 	memcg = mem_cgroup_iter(NULL, NULL, NULL);
2324b95a2f2dSJohannes Weiner 	do {
2325c56d5c7dSKonstantin Khlebnikov 		struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
2326f16015fbSJohannes Weiner 
2327c56d5c7dSKonstantin Khlebnikov 		if (inactive_anon_is_low(lruvec))
23281a93be0eSKonstantin Khlebnikov 			shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
23299e3b2f8cSKonstantin Khlebnikov 					   sc, LRU_ACTIVE_ANON);
2330b95a2f2dSJohannes Weiner 
2331b95a2f2dSJohannes Weiner 		memcg = mem_cgroup_iter(NULL, memcg, NULL);
2332b95a2f2dSJohannes Weiner 	} while (memcg);
2333f16015fbSJohannes Weiner }
2334f16015fbSJohannes Weiner 
23351741c877SMel Gorman /*
23361741c877SMel Gorman  * pgdat_balanced is used when checking if a node is balanced for high-order
23371741c877SMel Gorman  * allocations. Only zones that meet watermarks and are in a zone allowed
23381741c877SMel Gorman  * by the callers classzone_idx are added to balanced_pages. The total of
23391741c877SMel Gorman  * balanced pages must be at least 25% of the zones allowed by classzone_idx
23401741c877SMel Gorman  * for the node to be considered balanced. Forcing all zones to be balanced
23411741c877SMel Gorman  * for high orders can cause excessive reclaim when there are imbalanced zones.
23421741c877SMel Gorman  * The choice of 25% is due to
23431741c877SMel Gorman  *   o a 16M DMA zone that is balanced will not balance a zone on any
23441741c877SMel Gorman  *     reasonable sized machine
23451741c877SMel Gorman  *   o On all other machines, the top zone must be at least a reasonable
234625985edcSLucas De Marchi  *     percentage of the middle zones. For example, on 32-bit x86, highmem
23471741c877SMel Gorman  *     would need to be at least 256M for it to be balance a whole node.
23481741c877SMel Gorman  *     Similarly, on x86-64 the Normal zone would need to be at least 1G
23491741c877SMel Gorman  *     to balance a node on its own. These seemed like reasonable ratios.
23501741c877SMel Gorman  */
23511741c877SMel Gorman static bool pgdat_balanced(pg_data_t *pgdat, unsigned long balanced_pages,
23521741c877SMel Gorman 						int classzone_idx)
23531741c877SMel Gorman {
23541741c877SMel Gorman 	unsigned long present_pages = 0;
23551741c877SMel Gorman 	int i;
23561741c877SMel Gorman 
23571741c877SMel Gorman 	for (i = 0; i <= classzone_idx; i++)
23581741c877SMel Gorman 		present_pages += pgdat->node_zones[i].present_pages;
23591741c877SMel Gorman 
23604746efdeSShaohua Li 	/* A special case here: if zone has no page, we think it's balanced */
23614746efdeSShaohua Li 	return balanced_pages >= (present_pages >> 2);
23621741c877SMel Gorman }
23631741c877SMel Gorman 
23645515061dSMel Gorman /*
23655515061dSMel Gorman  * Prepare kswapd for sleeping. This verifies that there are no processes
23665515061dSMel Gorman  * waiting in throttle_direct_reclaim() and that watermarks have been met.
23675515061dSMel Gorman  *
23685515061dSMel Gorman  * Returns true if kswapd is ready to sleep
23695515061dSMel Gorman  */
23705515061dSMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, long remaining,
2371dc83edd9SMel Gorman 					int classzone_idx)
2372f50de2d3SMel Gorman {
2373bb3ab596SKOSAKI Motohiro 	int i;
23741741c877SMel Gorman 	unsigned long balanced = 0;
23751741c877SMel Gorman 	bool all_zones_ok = true;
2376f50de2d3SMel Gorman 
2377f50de2d3SMel Gorman 	/* If a direct reclaimer woke kswapd within HZ/10, it's premature */
2378f50de2d3SMel Gorman 	if (remaining)
23795515061dSMel Gorman 		return false;
23805515061dSMel Gorman 
23815515061dSMel Gorman 	/*
23825515061dSMel Gorman 	 * There is a potential race between when kswapd checks its watermarks
23835515061dSMel Gorman 	 * and a process gets throttled. There is also a potential race if
23845515061dSMel Gorman 	 * processes get throttled, kswapd wakes, a large process exits therby
23855515061dSMel Gorman 	 * balancing the zones that causes kswapd to miss a wakeup. If kswapd
23865515061dSMel Gorman 	 * is going to sleep, no process should be sleeping on pfmemalloc_wait
23875515061dSMel Gorman 	 * so wake them now if necessary. If necessary, processes will wake
23885515061dSMel Gorman 	 * kswapd and get throttled again
23895515061dSMel Gorman 	 */
23905515061dSMel Gorman 	if (waitqueue_active(&pgdat->pfmemalloc_wait)) {
23915515061dSMel Gorman 		wake_up(&pgdat->pfmemalloc_wait);
23925515061dSMel Gorman 		return false;
23935515061dSMel Gorman 	}
2394f50de2d3SMel Gorman 
23950abdee2bSMel Gorman 	/* Check the watermark levels */
239608951e54SMel Gorman 	for (i = 0; i <= classzone_idx; i++) {
2397bb3ab596SKOSAKI Motohiro 		struct zone *zone = pgdat->node_zones + i;
2398bb3ab596SKOSAKI Motohiro 
2399bb3ab596SKOSAKI Motohiro 		if (!populated_zone(zone))
2400bb3ab596SKOSAKI Motohiro 			continue;
2401bb3ab596SKOSAKI Motohiro 
2402355b09c4SMel Gorman 		/*
2403355b09c4SMel Gorman 		 * balance_pgdat() skips over all_unreclaimable after
2404355b09c4SMel Gorman 		 * DEF_PRIORITY. Effectively, it considers them balanced so
2405355b09c4SMel Gorman 		 * they must be considered balanced here as well if kswapd
2406355b09c4SMel Gorman 		 * is to sleep
2407355b09c4SMel Gorman 		 */
2408355b09c4SMel Gorman 		if (zone->all_unreclaimable) {
2409355b09c4SMel Gorman 			balanced += zone->present_pages;
2410de3fab39SKOSAKI Motohiro 			continue;
2411355b09c4SMel Gorman 		}
2412de3fab39SKOSAKI Motohiro 
241388f5acf8SMel Gorman 		if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone),
2414da175d06SMel Gorman 							i, 0))
24151741c877SMel Gorman 			all_zones_ok = false;
24161741c877SMel Gorman 		else
24171741c877SMel Gorman 			balanced += zone->present_pages;
2418bb3ab596SKOSAKI Motohiro 	}
2419f50de2d3SMel Gorman 
24201741c877SMel Gorman 	/*
24211741c877SMel Gorman 	 * For high-order requests, the balanced zones must contain at least
24221741c877SMel Gorman 	 * 25% of the nodes pages for kswapd to sleep. For order-0, all zones
24231741c877SMel Gorman 	 * must be balanced
24241741c877SMel Gorman 	 */
24251741c877SMel Gorman 	if (order)
24265515061dSMel Gorman 		return pgdat_balanced(pgdat, balanced, classzone_idx);
24271741c877SMel Gorman 	else
24285515061dSMel Gorman 		return all_zones_ok;
2429f50de2d3SMel Gorman }
2430f50de2d3SMel Gorman 
24311da177e4SLinus Torvalds /*
24321da177e4SLinus Torvalds  * For kswapd, balance_pgdat() will work across all this node's zones until
243341858966SMel Gorman  * they are all at high_wmark_pages(zone).
24341da177e4SLinus Torvalds  *
24350abdee2bSMel Gorman  * Returns the final order kswapd was reclaiming at
24361da177e4SLinus Torvalds  *
24371da177e4SLinus Torvalds  * There is special handling here for zones which are full of pinned pages.
24381da177e4SLinus Torvalds  * This can happen if the pages are all mlocked, or if they are all used by
24391da177e4SLinus Torvalds  * device drivers (say, ZONE_DMA).  Or if they are all in use by hugetlb.
24401da177e4SLinus Torvalds  * What we do is to detect the case where all pages in the zone have been
24411da177e4SLinus Torvalds  * scanned twice and there has been zero successful reclaim.  Mark the zone as
24421da177e4SLinus Torvalds  * dead and from now on, only perform a short scan.  Basically we're polling
24431da177e4SLinus Torvalds  * the zone for when the problem goes away.
24441da177e4SLinus Torvalds  *
24451da177e4SLinus Torvalds  * kswapd scans the zones in the highmem->normal->dma direction.  It skips
244641858966SMel Gorman  * zones which have free_pages > high_wmark_pages(zone), but once a zone is
244741858966SMel Gorman  * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the
244841858966SMel Gorman  * lower zones regardless of the number of free pages in the lower zones. This
244941858966SMel Gorman  * interoperates with the page allocator fallback scheme to ensure that aging
245041858966SMel Gorman  * of pages is balanced across the zones.
24511da177e4SLinus Torvalds  */
245299504748SMel Gorman static unsigned long balance_pgdat(pg_data_t *pgdat, int order,
2453dc83edd9SMel Gorman 							int *classzone_idx)
24541da177e4SLinus Torvalds {
24551da177e4SLinus Torvalds 	int all_zones_ok;
24561741c877SMel Gorman 	unsigned long balanced;
24571da177e4SLinus Torvalds 	int i;
245899504748SMel Gorman 	int end_zone = 0;	/* Inclusive.  0 = ZONE_DMA */
245969e05944SAndrew Morton 	unsigned long total_scanned;
24601da177e4SLinus Torvalds 	struct reclaim_state *reclaim_state = current->reclaim_state;
24610ae5e89cSYing Han 	unsigned long nr_soft_reclaimed;
24620ae5e89cSYing Han 	unsigned long nr_soft_scanned;
2463179e9639SAndrew Morton 	struct scan_control sc = {
2464179e9639SAndrew Morton 		.gfp_mask = GFP_KERNEL,
2465a6dc60f8SJohannes Weiner 		.may_unmap = 1,
24662e2e4259SKOSAKI Motohiro 		.may_swap = 1,
246722fba335SKOSAKI Motohiro 		/*
246822fba335SKOSAKI Motohiro 		 * kswapd doesn't want to be bailed out while reclaim. because
246922fba335SKOSAKI Motohiro 		 * we want to put equal scanning pressure on each zone.
247022fba335SKOSAKI Motohiro 		 */
247122fba335SKOSAKI Motohiro 		.nr_to_reclaim = ULONG_MAX,
24725ad333ebSAndy Whitcroft 		.order = order,
2473f16015fbSJohannes Weiner 		.target_mem_cgroup = NULL,
2474179e9639SAndrew Morton 	};
2475a09ed5e0SYing Han 	struct shrink_control shrink = {
2476a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
2477a09ed5e0SYing Han 	};
24781da177e4SLinus Torvalds loop_again:
24791da177e4SLinus Torvalds 	total_scanned = 0;
24809e3b2f8cSKonstantin Khlebnikov 	sc.priority = DEF_PRIORITY;
2481a79311c1SRik van Riel 	sc.nr_reclaimed = 0;
2482c0bbbc73SChristoph Lameter 	sc.may_writepage = !laptop_mode;
2483f8891e5eSChristoph Lameter 	count_vm_event(PAGEOUTRUN);
24841da177e4SLinus Torvalds 
24859e3b2f8cSKonstantin Khlebnikov 	do {
24861da177e4SLinus Torvalds 		unsigned long lru_pages = 0;
2487bb3ab596SKOSAKI Motohiro 		int has_under_min_watermark_zone = 0;
24881da177e4SLinus Torvalds 
24891da177e4SLinus Torvalds 		all_zones_ok = 1;
24901741c877SMel Gorman 		balanced = 0;
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds 		/*
24931da177e4SLinus Torvalds 		 * Scan in the highmem->dma direction for the highest
24941da177e4SLinus Torvalds 		 * zone which needs scanning
24951da177e4SLinus Torvalds 		 */
24961da177e4SLinus Torvalds 		for (i = pgdat->nr_zones - 1; i >= 0; i--) {
24971da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
24981da177e4SLinus Torvalds 
2499f3fe6512SCon Kolivas 			if (!populated_zone(zone))
25001da177e4SLinus Torvalds 				continue;
25011da177e4SLinus Torvalds 
25029e3b2f8cSKonstantin Khlebnikov 			if (zone->all_unreclaimable &&
25039e3b2f8cSKonstantin Khlebnikov 			    sc.priority != DEF_PRIORITY)
25041da177e4SLinus Torvalds 				continue;
25051da177e4SLinus Torvalds 
2506556adecbSRik van Riel 			/*
2507556adecbSRik van Riel 			 * Do some background aging of the anon list, to give
2508556adecbSRik van Riel 			 * pages a chance to be referenced before reclaiming.
2509556adecbSRik van Riel 			 */
25109e3b2f8cSKonstantin Khlebnikov 			age_active_anon(zone, &sc);
2511556adecbSRik van Riel 
2512cc715d99SMel Gorman 			/*
2513cc715d99SMel Gorman 			 * If the number of buffer_heads in the machine
2514cc715d99SMel Gorman 			 * exceeds the maximum allowed level and this node
2515cc715d99SMel Gorman 			 * has a highmem zone, force kswapd to reclaim from
2516cc715d99SMel Gorman 			 * it to relieve lowmem pressure.
2517cc715d99SMel Gorman 			 */
2518cc715d99SMel Gorman 			if (buffer_heads_over_limit && is_highmem_idx(i)) {
2519cc715d99SMel Gorman 				end_zone = i;
2520cc715d99SMel Gorman 				break;
2521cc715d99SMel Gorman 			}
2522cc715d99SMel Gorman 
252388f5acf8SMel Gorman 			if (!zone_watermark_ok_safe(zone, order,
252441858966SMel Gorman 					high_wmark_pages(zone), 0, 0)) {
25251da177e4SLinus Torvalds 				end_zone = i;
2526e1dbeda6SAndrew Morton 				break;
2527439423f6SShaohua Li 			} else {
2528439423f6SShaohua Li 				/* If balanced, clear the congested flag */
2529439423f6SShaohua Li 				zone_clear_flag(zone, ZONE_CONGESTED);
25301da177e4SLinus Torvalds 			}
25311da177e4SLinus Torvalds 		}
2532e1dbeda6SAndrew Morton 		if (i < 0)
25331da177e4SLinus Torvalds 			goto out;
2534e1dbeda6SAndrew Morton 
25351da177e4SLinus Torvalds 		for (i = 0; i <= end_zone; i++) {
25361da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
25371da177e4SLinus Torvalds 
2538adea02a1SWu Fengguang 			lru_pages += zone_reclaimable_pages(zone);
25391da177e4SLinus Torvalds 		}
25401da177e4SLinus Torvalds 
25411da177e4SLinus Torvalds 		/*
25421da177e4SLinus Torvalds 		 * Now scan the zone in the dma->highmem direction, stopping
25431da177e4SLinus Torvalds 		 * at the last zone which needs scanning.
25441da177e4SLinus Torvalds 		 *
25451da177e4SLinus Torvalds 		 * We do this because the page allocator works in the opposite
25461da177e4SLinus Torvalds 		 * direction.  This prevents the page allocator from allocating
25471da177e4SLinus Torvalds 		 * pages behind kswapd's direction of progress, which would
25481da177e4SLinus Torvalds 		 * cause too much scanning of the lower zones.
25491da177e4SLinus Torvalds 		 */
25501da177e4SLinus Torvalds 		for (i = 0; i <= end_zone; i++) {
25511da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
2552fe2c2a10SRik van Riel 			int nr_slab, testorder;
25538afdceceSMel Gorman 			unsigned long balance_gap;
25541da177e4SLinus Torvalds 
2555f3fe6512SCon Kolivas 			if (!populated_zone(zone))
25561da177e4SLinus Torvalds 				continue;
25571da177e4SLinus Torvalds 
25589e3b2f8cSKonstantin Khlebnikov 			if (zone->all_unreclaimable &&
25599e3b2f8cSKonstantin Khlebnikov 			    sc.priority != DEF_PRIORITY)
25601da177e4SLinus Torvalds 				continue;
25611da177e4SLinus Torvalds 
25621da177e4SLinus Torvalds 			sc.nr_scanned = 0;
25634e416953SBalbir Singh 
25640ae5e89cSYing Han 			nr_soft_scanned = 0;
25654e416953SBalbir Singh 			/*
25664e416953SBalbir Singh 			 * Call soft limit reclaim before calling shrink_zone.
25674e416953SBalbir Singh 			 */
25680ae5e89cSYing Han 			nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
25690ae5e89cSYing Han 							order, sc.gfp_mask,
25700ae5e89cSYing Han 							&nr_soft_scanned);
25710ae5e89cSYing Han 			sc.nr_reclaimed += nr_soft_reclaimed;
25720ae5e89cSYing Han 			total_scanned += nr_soft_scanned;
257300918b6aSKOSAKI Motohiro 
257432a4330dSRik van Riel 			/*
25758afdceceSMel Gorman 			 * We put equal pressure on every zone, unless
25768afdceceSMel Gorman 			 * one zone has way too many pages free
25778afdceceSMel Gorman 			 * already. The "too many pages" is defined
25788afdceceSMel Gorman 			 * as the high wmark plus a "gap" where the
25798afdceceSMel Gorman 			 * gap is either the low watermark or 1%
25808afdceceSMel Gorman 			 * of the zone, whichever is smaller.
258132a4330dSRik van Riel 			 */
25828afdceceSMel Gorman 			balance_gap = min(low_wmark_pages(zone),
25838afdceceSMel Gorman 				(zone->present_pages +
25848afdceceSMel Gorman 					KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
25858afdceceSMel Gorman 				KSWAPD_ZONE_BALANCE_GAP_RATIO);
2586fe2c2a10SRik van Riel 			/*
2587fe2c2a10SRik van Riel 			 * Kswapd reclaims only single pages with compaction
2588fe2c2a10SRik van Riel 			 * enabled. Trying too hard to reclaim until contiguous
2589fe2c2a10SRik van Riel 			 * free pages have become available can hurt performance
2590fe2c2a10SRik van Riel 			 * by evicting too much useful data from memory.
2591fe2c2a10SRik van Riel 			 * Do not reclaim more than needed for compaction.
2592fe2c2a10SRik van Riel 			 */
2593fe2c2a10SRik van Riel 			testorder = order;
2594fe2c2a10SRik van Riel 			if (COMPACTION_BUILD && order &&
2595fe2c2a10SRik van Riel 					compaction_suitable(zone, order) !=
2596fe2c2a10SRik van Riel 						COMPACT_SKIPPED)
2597fe2c2a10SRik van Riel 				testorder = 0;
2598fe2c2a10SRik van Riel 
2599cc715d99SMel Gorman 			if ((buffer_heads_over_limit && is_highmem_idx(i)) ||
2600643ac9fcSHugh Dickins 				    !zone_watermark_ok_safe(zone, testorder,
26018afdceceSMel Gorman 					high_wmark_pages(zone) + balance_gap,
2602d7868daeSMel Gorman 					end_zone, 0)) {
26039e3b2f8cSKonstantin Khlebnikov 				shrink_zone(zone, &sc);
2604d7868daeSMel Gorman 
26051da177e4SLinus Torvalds 				reclaim_state->reclaimed_slab = 0;
26061495f230SYing Han 				nr_slab = shrink_slab(&shrink, sc.nr_scanned, lru_pages);
2607a79311c1SRik van Riel 				sc.nr_reclaimed += reclaim_state->reclaimed_slab;
26081da177e4SLinus Torvalds 				total_scanned += sc.nr_scanned;
26095a03b051SAndrea Arcangeli 
2610d7868daeSMel Gorman 				if (nr_slab == 0 && !zone_reclaimable(zone))
261193e4a89aSKOSAKI Motohiro 					zone->all_unreclaimable = 1;
2612d7868daeSMel Gorman 			}
2613d7868daeSMel Gorman 
26141da177e4SLinus Torvalds 			/*
26151da177e4SLinus Torvalds 			 * If we've done a decent amount of scanning and
26161da177e4SLinus Torvalds 			 * the reclaim ratio is low, start doing writepage
26171da177e4SLinus Torvalds 			 * even in laptop mode
26181da177e4SLinus Torvalds 			 */
26191da177e4SLinus Torvalds 			if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
2620a79311c1SRik van Riel 			    total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2)
26211da177e4SLinus Torvalds 				sc.may_writepage = 1;
2622bb3ab596SKOSAKI Motohiro 
2623215ddd66SMel Gorman 			if (zone->all_unreclaimable) {
2624215ddd66SMel Gorman 				if (end_zone && end_zone == i)
2625215ddd66SMel Gorman 					end_zone--;
2626d7868daeSMel Gorman 				continue;
2627215ddd66SMel Gorman 			}
2628d7868daeSMel Gorman 
2629fe2c2a10SRik van Riel 			if (!zone_watermark_ok_safe(zone, testorder,
263045973d74SMinchan Kim 					high_wmark_pages(zone), end_zone, 0)) {
263145973d74SMinchan Kim 				all_zones_ok = 0;
2632bb3ab596SKOSAKI Motohiro 				/*
263345973d74SMinchan Kim 				 * We are still under min water mark.  This
263445973d74SMinchan Kim 				 * means that we have a GFP_ATOMIC allocation
263545973d74SMinchan Kim 				 * failure risk. Hurry up!
2636bb3ab596SKOSAKI Motohiro 				 */
263788f5acf8SMel Gorman 				if (!zone_watermark_ok_safe(zone, order,
263845973d74SMinchan Kim 					    min_wmark_pages(zone), end_zone, 0))
2639bb3ab596SKOSAKI Motohiro 					has_under_min_watermark_zone = 1;
26400e093d99SMel Gorman 			} else {
26410e093d99SMel Gorman 				/*
26420e093d99SMel Gorman 				 * If a zone reaches its high watermark,
26430e093d99SMel Gorman 				 * consider it to be no longer congested. It's
26440e093d99SMel Gorman 				 * possible there are dirty pages backed by
26450e093d99SMel Gorman 				 * congested BDIs but as pressure is relieved,
2646ab8704b8SWanpeng Li 				 * speculatively avoid congestion waits
26470e093d99SMel Gorman 				 */
26480e093d99SMel Gorman 				zone_clear_flag(zone, ZONE_CONGESTED);
2649dc83edd9SMel Gorman 				if (i <= *classzone_idx)
26501741c877SMel Gorman 					balanced += zone->present_pages;
265145973d74SMinchan Kim 			}
2652bb3ab596SKOSAKI Motohiro 
26531da177e4SLinus Torvalds 		}
26545515061dSMel Gorman 
26555515061dSMel Gorman 		/*
26565515061dSMel Gorman 		 * If the low watermark is met there is no need for processes
26575515061dSMel Gorman 		 * to be throttled on pfmemalloc_wait as they should not be
26585515061dSMel Gorman 		 * able to safely make forward progress. Wake them
26595515061dSMel Gorman 		 */
26605515061dSMel Gorman 		if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
26615515061dSMel Gorman 				pfmemalloc_watermark_ok(pgdat))
26625515061dSMel Gorman 			wake_up(&pgdat->pfmemalloc_wait);
26635515061dSMel Gorman 
2664dc83edd9SMel Gorman 		if (all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))
26651da177e4SLinus Torvalds 			break;		/* kswapd: all done */
26661da177e4SLinus Torvalds 		/*
26671da177e4SLinus Torvalds 		 * OK, kswapd is getting into trouble.  Take a nap, then take
26681da177e4SLinus Torvalds 		 * another pass across the zones.
26691da177e4SLinus Torvalds 		 */
26709e3b2f8cSKonstantin Khlebnikov 		if (total_scanned && (sc.priority < DEF_PRIORITY - 2)) {
2671bb3ab596SKOSAKI Motohiro 			if (has_under_min_watermark_zone)
2672bb3ab596SKOSAKI Motohiro 				count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT);
2673bb3ab596SKOSAKI Motohiro 			else
26748aa7e847SJens Axboe 				congestion_wait(BLK_RW_ASYNC, HZ/10);
2675bb3ab596SKOSAKI Motohiro 		}
26761da177e4SLinus Torvalds 
26771da177e4SLinus Torvalds 		/*
26781da177e4SLinus Torvalds 		 * We do this so kswapd doesn't build up large priorities for
26791da177e4SLinus Torvalds 		 * example when it is freeing in parallel with allocators. It
26801da177e4SLinus Torvalds 		 * matches the direct reclaim path behaviour in terms of impact
26811da177e4SLinus Torvalds 		 * on zone->*_priority.
26821da177e4SLinus Torvalds 		 */
2683a79311c1SRik van Riel 		if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
26841da177e4SLinus Torvalds 			break;
26859e3b2f8cSKonstantin Khlebnikov 	} while (--sc.priority >= 0);
26861da177e4SLinus Torvalds out:
268799504748SMel Gorman 
268899504748SMel Gorman 	/*
268999504748SMel Gorman 	 * order-0: All zones must meet high watermark for a balanced node
26901741c877SMel Gorman 	 * high-order: Balanced zones must make up at least 25% of the node
26911741c877SMel Gorman 	 *             for the node to be balanced
269299504748SMel Gorman 	 */
2693dc83edd9SMel Gorman 	if (!(all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))) {
26941da177e4SLinus Torvalds 		cond_resched();
26958357376dSRafael J. Wysocki 
26968357376dSRafael J. Wysocki 		try_to_freeze();
26978357376dSRafael J. Wysocki 
269873ce02e9SKOSAKI Motohiro 		/*
269973ce02e9SKOSAKI Motohiro 		 * Fragmentation may mean that the system cannot be
270073ce02e9SKOSAKI Motohiro 		 * rebalanced for high-order allocations in all zones.
270173ce02e9SKOSAKI Motohiro 		 * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
270273ce02e9SKOSAKI Motohiro 		 * it means the zones have been fully scanned and are still
270373ce02e9SKOSAKI Motohiro 		 * not balanced. For high-order allocations, there is
270473ce02e9SKOSAKI Motohiro 		 * little point trying all over again as kswapd may
270573ce02e9SKOSAKI Motohiro 		 * infinite loop.
270673ce02e9SKOSAKI Motohiro 		 *
270773ce02e9SKOSAKI Motohiro 		 * Instead, recheck all watermarks at order-0 as they
270873ce02e9SKOSAKI Motohiro 		 * are the most important. If watermarks are ok, kswapd will go
270973ce02e9SKOSAKI Motohiro 		 * back to sleep. High-order users can still perform direct
271073ce02e9SKOSAKI Motohiro 		 * reclaim if they wish.
271173ce02e9SKOSAKI Motohiro 		 */
271273ce02e9SKOSAKI Motohiro 		if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
271373ce02e9SKOSAKI Motohiro 			order = sc.order = 0;
271473ce02e9SKOSAKI Motohiro 
27151da177e4SLinus Torvalds 		goto loop_again;
27161da177e4SLinus Torvalds 	}
27171da177e4SLinus Torvalds 
271899504748SMel Gorman 	/*
271999504748SMel Gorman 	 * If kswapd was reclaiming at a higher order, it has the option of
272099504748SMel Gorman 	 * sleeping without all zones being balanced. Before it does, it must
272199504748SMel Gorman 	 * ensure that the watermarks for order-0 on *all* zones are met and
272299504748SMel Gorman 	 * that the congestion flags are cleared. The congestion flag must
272399504748SMel Gorman 	 * be cleared as kswapd is the only mechanism that clears the flag
272499504748SMel Gorman 	 * and it is potentially going to sleep here.
272599504748SMel Gorman 	 */
272699504748SMel Gorman 	if (order) {
27277be62de9SRik van Riel 		int zones_need_compaction = 1;
27287be62de9SRik van Riel 
272999504748SMel Gorman 		for (i = 0; i <= end_zone; i++) {
273099504748SMel Gorman 			struct zone *zone = pgdat->node_zones + i;
273199504748SMel Gorman 
273299504748SMel Gorman 			if (!populated_zone(zone))
273399504748SMel Gorman 				continue;
273499504748SMel Gorman 
27359e3b2f8cSKonstantin Khlebnikov 			if (zone->all_unreclaimable &&
27369e3b2f8cSKonstantin Khlebnikov 			    sc.priority != DEF_PRIORITY)
273799504748SMel Gorman 				continue;
273899504748SMel Gorman 
2739fe2c2a10SRik van Riel 			/* Would compaction fail due to lack of free memory? */
2740496b919bSRik van Riel 			if (COMPACTION_BUILD &&
2741496b919bSRik van Riel 			    compaction_suitable(zone, order) == COMPACT_SKIPPED)
2742fe2c2a10SRik van Riel 				goto loop_again;
2743fe2c2a10SRik van Riel 
274499504748SMel Gorman 			/* Confirm the zone is balanced for order-0 */
274599504748SMel Gorman 			if (!zone_watermark_ok(zone, 0,
274699504748SMel Gorman 					high_wmark_pages(zone), 0, 0)) {
274799504748SMel Gorman 				order = sc.order = 0;
274899504748SMel Gorman 				goto loop_again;
274999504748SMel Gorman 			}
275099504748SMel Gorman 
27517be62de9SRik van Riel 			/* Check if the memory needs to be defragmented. */
27527be62de9SRik van Riel 			if (zone_watermark_ok(zone, order,
27537be62de9SRik van Riel 				    low_wmark_pages(zone), *classzone_idx, 0))
27547be62de9SRik van Riel 				zones_need_compaction = 0;
27557be62de9SRik van Riel 
275699504748SMel Gorman 			/* If balanced, clear the congested flag */
275799504748SMel Gorman 			zone_clear_flag(zone, ZONE_CONGESTED);
275899504748SMel Gorman 		}
27597be62de9SRik van Riel 
27607be62de9SRik van Riel 		if (zones_need_compaction)
27617be62de9SRik van Riel 			compact_pgdat(pgdat, order);
276299504748SMel Gorman 	}
276399504748SMel Gorman 
27640abdee2bSMel Gorman 	/*
27655515061dSMel Gorman 	 * Return the order we were reclaiming at so prepare_kswapd_sleep()
27660abdee2bSMel Gorman 	 * makes a decision on the order we were last reclaiming at. However,
27670abdee2bSMel Gorman 	 * if another caller entered the allocator slow path while kswapd
27680abdee2bSMel Gorman 	 * was awake, order will remain at the higher level
27690abdee2bSMel Gorman 	 */
2770dc83edd9SMel Gorman 	*classzone_idx = end_zone;
27710abdee2bSMel Gorman 	return order;
27721da177e4SLinus Torvalds }
27731da177e4SLinus Torvalds 
2774dc83edd9SMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx)
2775f0bc0a60SKOSAKI Motohiro {
2776f0bc0a60SKOSAKI Motohiro 	long remaining = 0;
2777f0bc0a60SKOSAKI Motohiro 	DEFINE_WAIT(wait);
2778f0bc0a60SKOSAKI Motohiro 
2779f0bc0a60SKOSAKI Motohiro 	if (freezing(current) || kthread_should_stop())
2780f0bc0a60SKOSAKI Motohiro 		return;
2781f0bc0a60SKOSAKI Motohiro 
2782f0bc0a60SKOSAKI Motohiro 	prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2783f0bc0a60SKOSAKI Motohiro 
2784f0bc0a60SKOSAKI Motohiro 	/* Try to sleep for a short interval */
27855515061dSMel Gorman 	if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
2786f0bc0a60SKOSAKI Motohiro 		remaining = schedule_timeout(HZ/10);
2787f0bc0a60SKOSAKI Motohiro 		finish_wait(&pgdat->kswapd_wait, &wait);
2788f0bc0a60SKOSAKI Motohiro 		prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2789f0bc0a60SKOSAKI Motohiro 	}
2790f0bc0a60SKOSAKI Motohiro 
2791f0bc0a60SKOSAKI Motohiro 	/*
2792f0bc0a60SKOSAKI Motohiro 	 * After a short sleep, check if it was a premature sleep. If not, then
2793f0bc0a60SKOSAKI Motohiro 	 * go fully to sleep until explicitly woken up.
2794f0bc0a60SKOSAKI Motohiro 	 */
27955515061dSMel Gorman 	if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
2796f0bc0a60SKOSAKI Motohiro 		trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
2797f0bc0a60SKOSAKI Motohiro 
2798f0bc0a60SKOSAKI Motohiro 		/*
2799f0bc0a60SKOSAKI Motohiro 		 * vmstat counters are not perfectly accurate and the estimated
2800f0bc0a60SKOSAKI Motohiro 		 * value for counters such as NR_FREE_PAGES can deviate from the
2801f0bc0a60SKOSAKI Motohiro 		 * true value by nr_online_cpus * threshold. To avoid the zone
2802f0bc0a60SKOSAKI Motohiro 		 * watermarks being breached while under pressure, we reduce the
2803f0bc0a60SKOSAKI Motohiro 		 * per-cpu vmstat threshold while kswapd is awake and restore
2804f0bc0a60SKOSAKI Motohiro 		 * them before going back to sleep.
2805f0bc0a60SKOSAKI Motohiro 		 */
2806f0bc0a60SKOSAKI Motohiro 		set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
28071c7e7f6cSAaditya Kumar 
28081c7e7f6cSAaditya Kumar 		if (!kthread_should_stop())
2809f0bc0a60SKOSAKI Motohiro 			schedule();
28101c7e7f6cSAaditya Kumar 
2811f0bc0a60SKOSAKI Motohiro 		set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
2812f0bc0a60SKOSAKI Motohiro 	} else {
2813f0bc0a60SKOSAKI Motohiro 		if (remaining)
2814f0bc0a60SKOSAKI Motohiro 			count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
2815f0bc0a60SKOSAKI Motohiro 		else
2816f0bc0a60SKOSAKI Motohiro 			count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
2817f0bc0a60SKOSAKI Motohiro 	}
2818f0bc0a60SKOSAKI Motohiro 	finish_wait(&pgdat->kswapd_wait, &wait);
2819f0bc0a60SKOSAKI Motohiro }
2820f0bc0a60SKOSAKI Motohiro 
28211da177e4SLinus Torvalds /*
28221da177e4SLinus Torvalds  * The background pageout daemon, started as a kernel thread
28231da177e4SLinus Torvalds  * from the init process.
28241da177e4SLinus Torvalds  *
28251da177e4SLinus Torvalds  * This basically trickles out pages so that we have _some_
28261da177e4SLinus Torvalds  * free memory available even if there is no other activity
28271da177e4SLinus Torvalds  * that frees anything up. This is needed for things like routing
28281da177e4SLinus Torvalds  * etc, where we otherwise might have all activity going on in
28291da177e4SLinus Torvalds  * asynchronous contexts that cannot page things out.
28301da177e4SLinus Torvalds  *
28311da177e4SLinus Torvalds  * If there are applications that are active memory-allocators
28321da177e4SLinus Torvalds  * (most normal use), this basically shouldn't matter.
28331da177e4SLinus Torvalds  */
28341da177e4SLinus Torvalds static int kswapd(void *p)
28351da177e4SLinus Torvalds {
2836215ddd66SMel Gorman 	unsigned long order, new_order;
2837d2ebd0f6SAlex,Shi 	unsigned balanced_order;
2838215ddd66SMel Gorman 	int classzone_idx, new_classzone_idx;
2839d2ebd0f6SAlex,Shi 	int balanced_classzone_idx;
28401da177e4SLinus Torvalds 	pg_data_t *pgdat = (pg_data_t*)p;
28411da177e4SLinus Torvalds 	struct task_struct *tsk = current;
2842f0bc0a60SKOSAKI Motohiro 
28431da177e4SLinus Torvalds 	struct reclaim_state reclaim_state = {
28441da177e4SLinus Torvalds 		.reclaimed_slab = 0,
28451da177e4SLinus Torvalds 	};
2846a70f7302SRusty Russell 	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
28471da177e4SLinus Torvalds 
2848cf40bd16SNick Piggin 	lockdep_set_current_reclaim_state(GFP_KERNEL);
2849cf40bd16SNick Piggin 
2850174596a0SRusty Russell 	if (!cpumask_empty(cpumask))
2851c5f59f08SMike Travis 		set_cpus_allowed_ptr(tsk, cpumask);
28521da177e4SLinus Torvalds 	current->reclaim_state = &reclaim_state;
28531da177e4SLinus Torvalds 
28541da177e4SLinus Torvalds 	/*
28551da177e4SLinus Torvalds 	 * Tell the memory management that we're a "memory allocator",
28561da177e4SLinus Torvalds 	 * and that if we need more memory we should get access to it
28571da177e4SLinus Torvalds 	 * regardless (see "__alloc_pages()"). "kswapd" should
28581da177e4SLinus Torvalds 	 * never get caught in the normal page freeing logic.
28591da177e4SLinus Torvalds 	 *
28601da177e4SLinus Torvalds 	 * (Kswapd normally doesn't need memory anyway, but sometimes
28611da177e4SLinus Torvalds 	 * you need a small amount of memory in order to be able to
28621da177e4SLinus Torvalds 	 * page out something else, and this flag essentially protects
28631da177e4SLinus Torvalds 	 * us from recursively trying to free more memory as we're
28641da177e4SLinus Torvalds 	 * trying to free the first piece of memory in the first place).
28651da177e4SLinus Torvalds 	 */
2866930d9152SChristoph Lameter 	tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
286783144186SRafael J. Wysocki 	set_freezable();
28681da177e4SLinus Torvalds 
2869215ddd66SMel Gorman 	order = new_order = 0;
2870d2ebd0f6SAlex,Shi 	balanced_order = 0;
2871215ddd66SMel Gorman 	classzone_idx = new_classzone_idx = pgdat->nr_zones - 1;
2872d2ebd0f6SAlex,Shi 	balanced_classzone_idx = classzone_idx;
28731da177e4SLinus Torvalds 	for ( ; ; ) {
28748fe23e05SDavid Rientjes 		int ret;
28753e1d1d28SChristoph Lameter 
2876215ddd66SMel Gorman 		/*
2877215ddd66SMel Gorman 		 * If the last balance_pgdat was unsuccessful it's unlikely a
2878215ddd66SMel Gorman 		 * new request of a similar or harder type will succeed soon
2879215ddd66SMel Gorman 		 * so consider going to sleep on the basis we reclaimed at
2880215ddd66SMel Gorman 		 */
2881d2ebd0f6SAlex,Shi 		if (balanced_classzone_idx >= new_classzone_idx &&
2882d2ebd0f6SAlex,Shi 					balanced_order == new_order) {
28831da177e4SLinus Torvalds 			new_order = pgdat->kswapd_max_order;
288499504748SMel Gorman 			new_classzone_idx = pgdat->classzone_idx;
28851da177e4SLinus Torvalds 			pgdat->kswapd_max_order =  0;
2886215ddd66SMel Gorman 			pgdat->classzone_idx = pgdat->nr_zones - 1;
2887215ddd66SMel Gorman 		}
2888215ddd66SMel Gorman 
288999504748SMel Gorman 		if (order < new_order || classzone_idx > new_classzone_idx) {
28901da177e4SLinus Torvalds 			/*
28911da177e4SLinus Torvalds 			 * Don't sleep if someone wants a larger 'order'
289299504748SMel Gorman 			 * allocation or has tigher zone constraints
28931da177e4SLinus Torvalds 			 */
28941da177e4SLinus Torvalds 			order = new_order;
289599504748SMel Gorman 			classzone_idx = new_classzone_idx;
28961da177e4SLinus Torvalds 		} else {
2897d2ebd0f6SAlex,Shi 			kswapd_try_to_sleep(pgdat, balanced_order,
2898d2ebd0f6SAlex,Shi 						balanced_classzone_idx);
28991da177e4SLinus Torvalds 			order = pgdat->kswapd_max_order;
290099504748SMel Gorman 			classzone_idx = pgdat->classzone_idx;
2901f0dfcde0SAlex,Shi 			new_order = order;
2902f0dfcde0SAlex,Shi 			new_classzone_idx = classzone_idx;
29034d40502eSMel Gorman 			pgdat->kswapd_max_order = 0;
2904215ddd66SMel Gorman 			pgdat->classzone_idx = pgdat->nr_zones - 1;
29051da177e4SLinus Torvalds 		}
29061da177e4SLinus Torvalds 
29078fe23e05SDavid Rientjes 		ret = try_to_freeze();
29088fe23e05SDavid Rientjes 		if (kthread_should_stop())
29098fe23e05SDavid Rientjes 			break;
29108fe23e05SDavid Rientjes 
29118fe23e05SDavid Rientjes 		/*
29128fe23e05SDavid Rientjes 		 * We can speed up thawing tasks if we don't call balance_pgdat
29138fe23e05SDavid Rientjes 		 * after returning from the refrigerator
2914b1296cc4SRafael J. Wysocki 		 */
291533906bc5SMel Gorman 		if (!ret) {
291633906bc5SMel Gorman 			trace_mm_vmscan_kswapd_wake(pgdat->node_id, order);
2917d2ebd0f6SAlex,Shi 			balanced_classzone_idx = classzone_idx;
2918d2ebd0f6SAlex,Shi 			balanced_order = balance_pgdat(pgdat, order,
2919d2ebd0f6SAlex,Shi 						&balanced_classzone_idx);
29201da177e4SLinus Torvalds 		}
292133906bc5SMel Gorman 	}
29221da177e4SLinus Torvalds 	return 0;
29231da177e4SLinus Torvalds }
29241da177e4SLinus Torvalds 
29251da177e4SLinus Torvalds /*
29261da177e4SLinus Torvalds  * A zone is low on free memory, so wake its kswapd task to service it.
29271da177e4SLinus Torvalds  */
292899504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
29291da177e4SLinus Torvalds {
29301da177e4SLinus Torvalds 	pg_data_t *pgdat;
29311da177e4SLinus Torvalds 
2932f3fe6512SCon Kolivas 	if (!populated_zone(zone))
29331da177e4SLinus Torvalds 		return;
29341da177e4SLinus Torvalds 
293502a0e53dSPaul Jackson 	if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
29361da177e4SLinus Torvalds 		return;
293788f5acf8SMel Gorman 	pgdat = zone->zone_pgdat;
293899504748SMel Gorman 	if (pgdat->kswapd_max_order < order) {
293988f5acf8SMel Gorman 		pgdat->kswapd_max_order = order;
294099504748SMel Gorman 		pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx);
294199504748SMel Gorman 	}
29428d0986e2SCon Kolivas 	if (!waitqueue_active(&pgdat->kswapd_wait))
29431da177e4SLinus Torvalds 		return;
294488f5acf8SMel Gorman 	if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0))
294588f5acf8SMel Gorman 		return;
294688f5acf8SMel Gorman 
294788f5acf8SMel Gorman 	trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
29488d0986e2SCon Kolivas 	wake_up_interruptible(&pgdat->kswapd_wait);
29491da177e4SLinus Torvalds }
29501da177e4SLinus Torvalds 
2951adea02a1SWu Fengguang /*
2952adea02a1SWu Fengguang  * The reclaimable count would be mostly accurate.
2953adea02a1SWu Fengguang  * The less reclaimable pages may be
2954adea02a1SWu Fengguang  * - mlocked pages, which will be moved to unevictable list when encountered
2955adea02a1SWu Fengguang  * - mapped pages, which may require several travels to be reclaimed
2956adea02a1SWu Fengguang  * - dirty pages, which is not "instantly" reclaimable
2957adea02a1SWu Fengguang  */
2958adea02a1SWu Fengguang unsigned long global_reclaimable_pages(void)
29594f98a2feSRik van Riel {
2960adea02a1SWu Fengguang 	int nr;
2961adea02a1SWu Fengguang 
2962adea02a1SWu Fengguang 	nr = global_page_state(NR_ACTIVE_FILE) +
2963adea02a1SWu Fengguang 	     global_page_state(NR_INACTIVE_FILE);
2964adea02a1SWu Fengguang 
2965adea02a1SWu Fengguang 	if (nr_swap_pages > 0)
2966adea02a1SWu Fengguang 		nr += global_page_state(NR_ACTIVE_ANON) +
2967adea02a1SWu Fengguang 		      global_page_state(NR_INACTIVE_ANON);
2968adea02a1SWu Fengguang 
2969adea02a1SWu Fengguang 	return nr;
2970adea02a1SWu Fengguang }
2971adea02a1SWu Fengguang 
2972adea02a1SWu Fengguang unsigned long zone_reclaimable_pages(struct zone *zone)
2973adea02a1SWu Fengguang {
2974adea02a1SWu Fengguang 	int nr;
2975adea02a1SWu Fengguang 
2976adea02a1SWu Fengguang 	nr = zone_page_state(zone, NR_ACTIVE_FILE) +
2977adea02a1SWu Fengguang 	     zone_page_state(zone, NR_INACTIVE_FILE);
2978adea02a1SWu Fengguang 
2979adea02a1SWu Fengguang 	if (nr_swap_pages > 0)
2980adea02a1SWu Fengguang 		nr += zone_page_state(zone, NR_ACTIVE_ANON) +
2981adea02a1SWu Fengguang 		      zone_page_state(zone, NR_INACTIVE_ANON);
2982adea02a1SWu Fengguang 
2983adea02a1SWu Fengguang 	return nr;
29844f98a2feSRik van Riel }
29854f98a2feSRik van Riel 
2986c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
29871da177e4SLinus Torvalds /*
29887b51755cSKOSAKI Motohiro  * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
2989d6277db4SRafael J. Wysocki  * freed pages.
2990d6277db4SRafael J. Wysocki  *
2991d6277db4SRafael J. Wysocki  * Rather than trying to age LRUs the aim is to preserve the overall
2992d6277db4SRafael J. Wysocki  * LRU order by reclaiming preferentially
2993d6277db4SRafael J. Wysocki  * inactive > active > active referenced > active mapped
29941da177e4SLinus Torvalds  */
29957b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
29961da177e4SLinus Torvalds {
2997d6277db4SRafael J. Wysocki 	struct reclaim_state reclaim_state;
2998d6277db4SRafael J. Wysocki 	struct scan_control sc = {
29997b51755cSKOSAKI Motohiro 		.gfp_mask = GFP_HIGHUSER_MOVABLE,
30007b51755cSKOSAKI Motohiro 		.may_swap = 1,
30017b51755cSKOSAKI Motohiro 		.may_unmap = 1,
3002d6277db4SRafael J. Wysocki 		.may_writepage = 1,
30037b51755cSKOSAKI Motohiro 		.nr_to_reclaim = nr_to_reclaim,
30047b51755cSKOSAKI Motohiro 		.hibernation_mode = 1,
30057b51755cSKOSAKI Motohiro 		.order = 0,
30069e3b2f8cSKonstantin Khlebnikov 		.priority = DEF_PRIORITY,
30071da177e4SLinus Torvalds 	};
3008a09ed5e0SYing Han 	struct shrink_control shrink = {
3009a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
3010a09ed5e0SYing Han 	};
30117b51755cSKOSAKI Motohiro 	struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
30127b51755cSKOSAKI Motohiro 	struct task_struct *p = current;
30137b51755cSKOSAKI Motohiro 	unsigned long nr_reclaimed;
30141da177e4SLinus Torvalds 
30157b51755cSKOSAKI Motohiro 	p->flags |= PF_MEMALLOC;
30167b51755cSKOSAKI Motohiro 	lockdep_set_current_reclaim_state(sc.gfp_mask);
3017d6277db4SRafael J. Wysocki 	reclaim_state.reclaimed_slab = 0;
30187b51755cSKOSAKI Motohiro 	p->reclaim_state = &reclaim_state;
3019d6277db4SRafael J. Wysocki 
3020a09ed5e0SYing Han 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
3021d6277db4SRafael J. Wysocki 
30227b51755cSKOSAKI Motohiro 	p->reclaim_state = NULL;
30237b51755cSKOSAKI Motohiro 	lockdep_clear_current_reclaim_state();
30247b51755cSKOSAKI Motohiro 	p->flags &= ~PF_MEMALLOC;
3025d6277db4SRafael J. Wysocki 
30267b51755cSKOSAKI Motohiro 	return nr_reclaimed;
30271da177e4SLinus Torvalds }
3028c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */
30291da177e4SLinus Torvalds 
30301da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but
30311da177e4SLinus Torvalds    not required for correctness.  So if the last cpu in a node goes
30321da177e4SLinus Torvalds    away, we get changed to run anywhere: as the first one comes back,
30331da177e4SLinus Torvalds    restore their cpu bindings. */
30349c7b216dSChandra Seetharaman static int __devinit cpu_callback(struct notifier_block *nfb,
303569e05944SAndrew Morton 				  unsigned long action, void *hcpu)
30361da177e4SLinus Torvalds {
303758c0a4a7SYasunori Goto 	int nid;
30381da177e4SLinus Torvalds 
30398bb78442SRafael J. Wysocki 	if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
304058c0a4a7SYasunori Goto 		for_each_node_state(nid, N_HIGH_MEMORY) {
3041c5f59f08SMike Travis 			pg_data_t *pgdat = NODE_DATA(nid);
3042a70f7302SRusty Russell 			const struct cpumask *mask;
3043a70f7302SRusty Russell 
3044a70f7302SRusty Russell 			mask = cpumask_of_node(pgdat->node_id);
3045c5f59f08SMike Travis 
30463e597945SRusty Russell 			if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
30471da177e4SLinus Torvalds 				/* One of our CPUs online: restore mask */
3048c5f59f08SMike Travis 				set_cpus_allowed_ptr(pgdat->kswapd, mask);
30491da177e4SLinus Torvalds 		}
30501da177e4SLinus Torvalds 	}
30511da177e4SLinus Torvalds 	return NOTIFY_OK;
30521da177e4SLinus Torvalds }
30531da177e4SLinus Torvalds 
30543218ae14SYasunori Goto /*
30553218ae14SYasunori Goto  * This kswapd start function will be called by init and node-hot-add.
30563218ae14SYasunori Goto  * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
30573218ae14SYasunori Goto  */
30583218ae14SYasunori Goto int kswapd_run(int nid)
30593218ae14SYasunori Goto {
30603218ae14SYasunori Goto 	pg_data_t *pgdat = NODE_DATA(nid);
30613218ae14SYasunori Goto 	int ret = 0;
30623218ae14SYasunori Goto 
30633218ae14SYasunori Goto 	if (pgdat->kswapd)
30643218ae14SYasunori Goto 		return 0;
30653218ae14SYasunori Goto 
30663218ae14SYasunori Goto 	pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
30673218ae14SYasunori Goto 	if (IS_ERR(pgdat->kswapd)) {
30683218ae14SYasunori Goto 		/* failure at boot is fatal */
30693218ae14SYasunori Goto 		BUG_ON(system_state == SYSTEM_BOOTING);
30703218ae14SYasunori Goto 		printk("Failed to start kswapd on node %d\n",nid);
30713218ae14SYasunori Goto 		ret = -1;
30723218ae14SYasunori Goto 	}
30733218ae14SYasunori Goto 	return ret;
30743218ae14SYasunori Goto }
30753218ae14SYasunori Goto 
30768fe23e05SDavid Rientjes /*
3077d8adde17SJiang Liu  * Called by memory hotplug when all memory in a node is offlined.  Caller must
3078d8adde17SJiang Liu  * hold lock_memory_hotplug().
30798fe23e05SDavid Rientjes  */
30808fe23e05SDavid Rientjes void kswapd_stop(int nid)
30818fe23e05SDavid Rientjes {
30828fe23e05SDavid Rientjes 	struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
30838fe23e05SDavid Rientjes 
3084d8adde17SJiang Liu 	if (kswapd) {
30858fe23e05SDavid Rientjes 		kthread_stop(kswapd);
3086d8adde17SJiang Liu 		NODE_DATA(nid)->kswapd = NULL;
3087d8adde17SJiang Liu 	}
30888fe23e05SDavid Rientjes }
30898fe23e05SDavid Rientjes 
30901da177e4SLinus Torvalds static int __init kswapd_init(void)
30911da177e4SLinus Torvalds {
30923218ae14SYasunori Goto 	int nid;
309369e05944SAndrew Morton 
30941da177e4SLinus Torvalds 	swap_setup();
30959422ffbaSChristoph Lameter 	for_each_node_state(nid, N_HIGH_MEMORY)
30963218ae14SYasunori Goto  		kswapd_run(nid);
30971da177e4SLinus Torvalds 	hotcpu_notifier(cpu_callback, 0);
30981da177e4SLinus Torvalds 	return 0;
30991da177e4SLinus Torvalds }
31001da177e4SLinus Torvalds 
31011da177e4SLinus Torvalds module_init(kswapd_init)
31029eeff239SChristoph Lameter 
31039eeff239SChristoph Lameter #ifdef CONFIG_NUMA
31049eeff239SChristoph Lameter /*
31059eeff239SChristoph Lameter  * Zone reclaim mode
31069eeff239SChristoph Lameter  *
31079eeff239SChristoph Lameter  * If non-zero call zone_reclaim when the number of free pages falls below
31089eeff239SChristoph Lameter  * the watermarks.
31099eeff239SChristoph Lameter  */
31109eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly;
31119eeff239SChristoph Lameter 
31121b2ffb78SChristoph Lameter #define RECLAIM_OFF 0
31137d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0)	/* Run shrink_inactive_list on the zone */
31141b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1)	/* Writeout pages during reclaim */
31151b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2)	/* Swap pages out during reclaim */
31161b2ffb78SChristoph Lameter 
31179eeff239SChristoph Lameter /*
3118a92f7126SChristoph Lameter  * Priority for ZONE_RECLAIM. This determines the fraction of pages
3119a92f7126SChristoph Lameter  * of a node considered for each zone_reclaim. 4 scans 1/16th of
3120a92f7126SChristoph Lameter  * a zone.
3121a92f7126SChristoph Lameter  */
3122a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4
3123a92f7126SChristoph Lameter 
31249eeff239SChristoph Lameter /*
31259614634fSChristoph Lameter  * Percentage of pages in a zone that must be unmapped for zone_reclaim to
31269614634fSChristoph Lameter  * occur.
31279614634fSChristoph Lameter  */
31289614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1;
31299614634fSChristoph Lameter 
31309614634fSChristoph Lameter /*
31310ff38490SChristoph Lameter  * If the number of slab pages in a zone grows beyond this percentage then
31320ff38490SChristoph Lameter  * slab reclaim needs to occur.
31330ff38490SChristoph Lameter  */
31340ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5;
31350ff38490SChristoph Lameter 
313690afa5deSMel Gorman static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
313790afa5deSMel Gorman {
313890afa5deSMel Gorman 	unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
313990afa5deSMel Gorman 	unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
314090afa5deSMel Gorman 		zone_page_state(zone, NR_ACTIVE_FILE);
314190afa5deSMel Gorman 
314290afa5deSMel Gorman 	/*
314390afa5deSMel Gorman 	 * It's possible for there to be more file mapped pages than
314490afa5deSMel Gorman 	 * accounted for by the pages on the file LRU lists because
314590afa5deSMel Gorman 	 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
314690afa5deSMel Gorman 	 */
314790afa5deSMel Gorman 	return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
314890afa5deSMel Gorman }
314990afa5deSMel Gorman 
315090afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */
315190afa5deSMel Gorman static long zone_pagecache_reclaimable(struct zone *zone)
315290afa5deSMel Gorman {
315390afa5deSMel Gorman 	long nr_pagecache_reclaimable;
315490afa5deSMel Gorman 	long delta = 0;
315590afa5deSMel Gorman 
315690afa5deSMel Gorman 	/*
315790afa5deSMel Gorman 	 * If RECLAIM_SWAP is set, then all file pages are considered
315890afa5deSMel Gorman 	 * potentially reclaimable. Otherwise, we have to worry about
315990afa5deSMel Gorman 	 * pages like swapcache and zone_unmapped_file_pages() provides
316090afa5deSMel Gorman 	 * a better estimate
316190afa5deSMel Gorman 	 */
316290afa5deSMel Gorman 	if (zone_reclaim_mode & RECLAIM_SWAP)
316390afa5deSMel Gorman 		nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
316490afa5deSMel Gorman 	else
316590afa5deSMel Gorman 		nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
316690afa5deSMel Gorman 
316790afa5deSMel Gorman 	/* If we can't clean pages, remove dirty pages from consideration */
316890afa5deSMel Gorman 	if (!(zone_reclaim_mode & RECLAIM_WRITE))
316990afa5deSMel Gorman 		delta += zone_page_state(zone, NR_FILE_DIRTY);
317090afa5deSMel Gorman 
317190afa5deSMel Gorman 	/* Watch for any possible underflows due to delta */
317290afa5deSMel Gorman 	if (unlikely(delta > nr_pagecache_reclaimable))
317390afa5deSMel Gorman 		delta = nr_pagecache_reclaimable;
317490afa5deSMel Gorman 
317590afa5deSMel Gorman 	return nr_pagecache_reclaimable - delta;
317690afa5deSMel Gorman }
317790afa5deSMel Gorman 
31780ff38490SChristoph Lameter /*
31799eeff239SChristoph Lameter  * Try to free up some pages from this zone through reclaim.
31809eeff239SChristoph Lameter  */
3181179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
31829eeff239SChristoph Lameter {
31837fb2d46dSChristoph Lameter 	/* Minimum pages needed in order to stay on node */
318469e05944SAndrew Morton 	const unsigned long nr_pages = 1 << order;
31859eeff239SChristoph Lameter 	struct task_struct *p = current;
31869eeff239SChristoph Lameter 	struct reclaim_state reclaim_state;
3187179e9639SAndrew Morton 	struct scan_control sc = {
3188179e9639SAndrew Morton 		.may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
3189a6dc60f8SJohannes Weiner 		.may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
31902e2e4259SKOSAKI Motohiro 		.may_swap = 1,
319122fba335SKOSAKI Motohiro 		.nr_to_reclaim = max_t(unsigned long, nr_pages,
319222fba335SKOSAKI Motohiro 				       SWAP_CLUSTER_MAX),
3193179e9639SAndrew Morton 		.gfp_mask = gfp_mask,
3194bd2f6199SJohannes Weiner 		.order = order,
31959e3b2f8cSKonstantin Khlebnikov 		.priority = ZONE_RECLAIM_PRIORITY,
3196179e9639SAndrew Morton 	};
3197a09ed5e0SYing Han 	struct shrink_control shrink = {
3198a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
3199a09ed5e0SYing Han 	};
320015748048SKOSAKI Motohiro 	unsigned long nr_slab_pages0, nr_slab_pages1;
32019eeff239SChristoph Lameter 
32029eeff239SChristoph Lameter 	cond_resched();
3203d4f7796eSChristoph Lameter 	/*
3204d4f7796eSChristoph Lameter 	 * We need to be able to allocate from the reserves for RECLAIM_SWAP
3205d4f7796eSChristoph Lameter 	 * and we also need to be able to write out pages for RECLAIM_WRITE
3206d4f7796eSChristoph Lameter 	 * and RECLAIM_SWAP.
3207d4f7796eSChristoph Lameter 	 */
3208d4f7796eSChristoph Lameter 	p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
320976ca542dSKOSAKI Motohiro 	lockdep_set_current_reclaim_state(gfp_mask);
32109eeff239SChristoph Lameter 	reclaim_state.reclaimed_slab = 0;
32119eeff239SChristoph Lameter 	p->reclaim_state = &reclaim_state;
3212c84db23cSChristoph Lameter 
321390afa5deSMel Gorman 	if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
3214a92f7126SChristoph Lameter 		/*
32150ff38490SChristoph Lameter 		 * Free memory by calling shrink zone with increasing
32160ff38490SChristoph Lameter 		 * priorities until we have enough memory freed.
3217a92f7126SChristoph Lameter 		 */
3218a92f7126SChristoph Lameter 		do {
32199e3b2f8cSKonstantin Khlebnikov 			shrink_zone(zone, &sc);
32209e3b2f8cSKonstantin Khlebnikov 		} while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
32210ff38490SChristoph Lameter 	}
3222a92f7126SChristoph Lameter 
322315748048SKOSAKI Motohiro 	nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
322415748048SKOSAKI Motohiro 	if (nr_slab_pages0 > zone->min_slab_pages) {
32252a16e3f4SChristoph Lameter 		/*
32267fb2d46dSChristoph Lameter 		 * shrink_slab() does not currently allow us to determine how
32270ff38490SChristoph Lameter 		 * many pages were freed in this zone. So we take the current
32280ff38490SChristoph Lameter 		 * number of slab pages and shake the slab until it is reduced
32290ff38490SChristoph Lameter 		 * by the same nr_pages that we used for reclaiming unmapped
32300ff38490SChristoph Lameter 		 * pages.
32312a16e3f4SChristoph Lameter 		 *
32320ff38490SChristoph Lameter 		 * Note that shrink_slab will free memory on all zones and may
32330ff38490SChristoph Lameter 		 * take a long time.
32342a16e3f4SChristoph Lameter 		 */
32354dc4b3d9SKOSAKI Motohiro 		for (;;) {
32364dc4b3d9SKOSAKI Motohiro 			unsigned long lru_pages = zone_reclaimable_pages(zone);
32374dc4b3d9SKOSAKI Motohiro 
32384dc4b3d9SKOSAKI Motohiro 			/* No reclaimable slab or very low memory pressure */
32391495f230SYing Han 			if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages))
32404dc4b3d9SKOSAKI Motohiro 				break;
32414dc4b3d9SKOSAKI Motohiro 
32424dc4b3d9SKOSAKI Motohiro 			/* Freed enough memory */
32434dc4b3d9SKOSAKI Motohiro 			nr_slab_pages1 = zone_page_state(zone,
32444dc4b3d9SKOSAKI Motohiro 							NR_SLAB_RECLAIMABLE);
32454dc4b3d9SKOSAKI Motohiro 			if (nr_slab_pages1 + nr_pages <= nr_slab_pages0)
32464dc4b3d9SKOSAKI Motohiro 				break;
32474dc4b3d9SKOSAKI Motohiro 		}
324883e33a47SChristoph Lameter 
324983e33a47SChristoph Lameter 		/*
325083e33a47SChristoph Lameter 		 * Update nr_reclaimed by the number of slab pages we
325183e33a47SChristoph Lameter 		 * reclaimed from this zone.
325283e33a47SChristoph Lameter 		 */
325315748048SKOSAKI Motohiro 		nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
325415748048SKOSAKI Motohiro 		if (nr_slab_pages1 < nr_slab_pages0)
325515748048SKOSAKI Motohiro 			sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1;
32562a16e3f4SChristoph Lameter 	}
32572a16e3f4SChristoph Lameter 
32589eeff239SChristoph Lameter 	p->reclaim_state = NULL;
3259d4f7796eSChristoph Lameter 	current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
326076ca542dSKOSAKI Motohiro 	lockdep_clear_current_reclaim_state();
3261a79311c1SRik van Riel 	return sc.nr_reclaimed >= nr_pages;
32629eeff239SChristoph Lameter }
3263179e9639SAndrew Morton 
3264179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
3265179e9639SAndrew Morton {
3266179e9639SAndrew Morton 	int node_id;
3267d773ed6bSDavid Rientjes 	int ret;
3268179e9639SAndrew Morton 
3269179e9639SAndrew Morton 	/*
32700ff38490SChristoph Lameter 	 * Zone reclaim reclaims unmapped file backed pages and
32710ff38490SChristoph Lameter 	 * slab pages if we are over the defined limits.
327234aa1330SChristoph Lameter 	 *
32739614634fSChristoph Lameter 	 * A small portion of unmapped file backed pages is needed for
32749614634fSChristoph Lameter 	 * file I/O otherwise pages read by file I/O will be immediately
32759614634fSChristoph Lameter 	 * thrown out if the zone is overallocated. So we do not reclaim
32769614634fSChristoph Lameter 	 * if less than a specified percentage of the zone is used by
32779614634fSChristoph Lameter 	 * unmapped file backed pages.
3278179e9639SAndrew Morton 	 */
327990afa5deSMel Gorman 	if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
328090afa5deSMel Gorman 	    zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
3281fa5e084eSMel Gorman 		return ZONE_RECLAIM_FULL;
3282179e9639SAndrew Morton 
328393e4a89aSKOSAKI Motohiro 	if (zone->all_unreclaimable)
3284fa5e084eSMel Gorman 		return ZONE_RECLAIM_FULL;
3285d773ed6bSDavid Rientjes 
3286179e9639SAndrew Morton 	/*
3287d773ed6bSDavid Rientjes 	 * Do not scan if the allocation should not be delayed.
3288179e9639SAndrew Morton 	 */
3289d773ed6bSDavid Rientjes 	if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
3290fa5e084eSMel Gorman 		return ZONE_RECLAIM_NOSCAN;
3291179e9639SAndrew Morton 
3292179e9639SAndrew Morton 	/*
3293179e9639SAndrew Morton 	 * Only run zone reclaim on the local zone or on zones that do not
3294179e9639SAndrew Morton 	 * have associated processors. This will favor the local processor
3295179e9639SAndrew Morton 	 * over remote processors and spread off node memory allocations
3296179e9639SAndrew Morton 	 * as wide as possible.
3297179e9639SAndrew Morton 	 */
329889fa3024SChristoph Lameter 	node_id = zone_to_nid(zone);
329937c0708dSChristoph Lameter 	if (node_state(node_id, N_CPU) && node_id != numa_node_id())
3300fa5e084eSMel Gorman 		return ZONE_RECLAIM_NOSCAN;
3301d773ed6bSDavid Rientjes 
3302d773ed6bSDavid Rientjes 	if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
3303fa5e084eSMel Gorman 		return ZONE_RECLAIM_NOSCAN;
3304fa5e084eSMel Gorman 
3305d773ed6bSDavid Rientjes 	ret = __zone_reclaim(zone, gfp_mask, order);
3306d773ed6bSDavid Rientjes 	zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
3307d773ed6bSDavid Rientjes 
330824cf7251SMel Gorman 	if (!ret)
330924cf7251SMel Gorman 		count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
331024cf7251SMel Gorman 
3311d773ed6bSDavid Rientjes 	return ret;
3312179e9639SAndrew Morton }
33139eeff239SChristoph Lameter #endif
3314894bc310SLee Schermerhorn 
3315894bc310SLee Schermerhorn /*
3316894bc310SLee Schermerhorn  * page_evictable - test whether a page is evictable
3317894bc310SLee Schermerhorn  * @page: the page to test
3318894bc310SLee Schermerhorn  * @vma: the VMA in which the page is or will be mapped, may be NULL
3319894bc310SLee Schermerhorn  *
3320894bc310SLee Schermerhorn  * Test whether page is evictable--i.e., should be placed on active/inactive
3321b291f000SNick Piggin  * lists vs unevictable list.  The vma argument is !NULL when called from the
3322b291f000SNick Piggin  * fault path to determine how to instantate a new page.
3323894bc310SLee Schermerhorn  *
3324894bc310SLee Schermerhorn  * Reasons page might not be evictable:
3325ba9ddf49SLee Schermerhorn  * (1) page's mapping marked unevictable
3326b291f000SNick Piggin  * (2) page is part of an mlocked VMA
3327ba9ddf49SLee Schermerhorn  *
3328894bc310SLee Schermerhorn  */
3329894bc310SLee Schermerhorn int page_evictable(struct page *page, struct vm_area_struct *vma)
3330894bc310SLee Schermerhorn {
3331894bc310SLee Schermerhorn 
3332ba9ddf49SLee Schermerhorn 	if (mapping_unevictable(page_mapping(page)))
3333ba9ddf49SLee Schermerhorn 		return 0;
3334ba9ddf49SLee Schermerhorn 
3335096a7cf4SYing Han 	if (PageMlocked(page) || (vma && mlocked_vma_newpage(vma, page)))
3336b291f000SNick Piggin 		return 0;
3337894bc310SLee Schermerhorn 
3338894bc310SLee Schermerhorn 	return 1;
3339894bc310SLee Schermerhorn }
334089e004eaSLee Schermerhorn 
334185046579SHugh Dickins #ifdef CONFIG_SHMEM
334289e004eaSLee Schermerhorn /**
334324513264SHugh Dickins  * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list
334424513264SHugh Dickins  * @pages:	array of pages to check
334524513264SHugh Dickins  * @nr_pages:	number of pages to check
334689e004eaSLee Schermerhorn  *
334724513264SHugh Dickins  * Checks pages for evictability and moves them to the appropriate lru list.
334885046579SHugh Dickins  *
334985046579SHugh Dickins  * This function is only used for SysV IPC SHM_UNLOCK.
335089e004eaSLee Schermerhorn  */
335124513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages)
335289e004eaSLee Schermerhorn {
3353925b7673SJohannes Weiner 	struct lruvec *lruvec;
335424513264SHugh Dickins 	struct zone *zone = NULL;
335524513264SHugh Dickins 	int pgscanned = 0;
335624513264SHugh Dickins 	int pgrescued = 0;
335789e004eaSLee Schermerhorn 	int i;
335889e004eaSLee Schermerhorn 
335924513264SHugh Dickins 	for (i = 0; i < nr_pages; i++) {
336024513264SHugh Dickins 		struct page *page = pages[i];
336124513264SHugh Dickins 		struct zone *pagezone;
336289e004eaSLee Schermerhorn 
336324513264SHugh Dickins 		pgscanned++;
336424513264SHugh Dickins 		pagezone = page_zone(page);
336589e004eaSLee Schermerhorn 		if (pagezone != zone) {
336689e004eaSLee Schermerhorn 			if (zone)
336789e004eaSLee Schermerhorn 				spin_unlock_irq(&zone->lru_lock);
336889e004eaSLee Schermerhorn 			zone = pagezone;
336989e004eaSLee Schermerhorn 			spin_lock_irq(&zone->lru_lock);
337089e004eaSLee Schermerhorn 		}
3371fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
337289e004eaSLee Schermerhorn 
337324513264SHugh Dickins 		if (!PageLRU(page) || !PageUnevictable(page))
337424513264SHugh Dickins 			continue;
337589e004eaSLee Schermerhorn 
337624513264SHugh Dickins 		if (page_evictable(page, NULL)) {
337724513264SHugh Dickins 			enum lru_list lru = page_lru_base_type(page);
337824513264SHugh Dickins 
337924513264SHugh Dickins 			VM_BUG_ON(PageActive(page));
338024513264SHugh Dickins 			ClearPageUnevictable(page);
3381fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
3382fa9add64SHugh Dickins 			add_page_to_lru_list(page, lruvec, lru);
338324513264SHugh Dickins 			pgrescued++;
338489e004eaSLee Schermerhorn 		}
338589e004eaSLee Schermerhorn 	}
338624513264SHugh Dickins 
338724513264SHugh Dickins 	if (zone) {
338824513264SHugh Dickins 		__count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
338924513264SHugh Dickins 		__count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
339024513264SHugh Dickins 		spin_unlock_irq(&zone->lru_lock);
339124513264SHugh Dickins 	}
339285046579SHugh Dickins }
339385046579SHugh Dickins #endif /* CONFIG_SHMEM */
3394af936a16SLee Schermerhorn 
3395264e56d8SJohannes Weiner static void warn_scan_unevictable_pages(void)
3396af936a16SLee Schermerhorn {
3397264e56d8SJohannes Weiner 	printk_once(KERN_WARNING
339825bd91bdSKOSAKI Motohiro 		    "%s: The scan_unevictable_pages sysctl/node-interface has been "
3399264e56d8SJohannes Weiner 		    "disabled for lack of a legitimate use case.  If you have "
340025bd91bdSKOSAKI Motohiro 		    "one, please send an email to linux-mm@kvack.org.\n",
340125bd91bdSKOSAKI Motohiro 		    current->comm);
3402af936a16SLee Schermerhorn }
3403af936a16SLee Schermerhorn 
3404af936a16SLee Schermerhorn /*
3405af936a16SLee Schermerhorn  * scan_unevictable_pages [vm] sysctl handler.  On demand re-scan of
3406af936a16SLee Schermerhorn  * all nodes' unevictable lists for evictable pages
3407af936a16SLee Schermerhorn  */
3408af936a16SLee Schermerhorn unsigned long scan_unevictable_pages;
3409af936a16SLee Schermerhorn 
3410af936a16SLee Schermerhorn int scan_unevictable_handler(struct ctl_table *table, int write,
34118d65af78SAlexey Dobriyan 			   void __user *buffer,
3412af936a16SLee Schermerhorn 			   size_t *length, loff_t *ppos)
3413af936a16SLee Schermerhorn {
3414264e56d8SJohannes Weiner 	warn_scan_unevictable_pages();
34158d65af78SAlexey Dobriyan 	proc_doulongvec_minmax(table, write, buffer, length, ppos);
3416af936a16SLee Schermerhorn 	scan_unevictable_pages = 0;
3417af936a16SLee Schermerhorn 	return 0;
3418af936a16SLee Schermerhorn }
3419af936a16SLee Schermerhorn 
3420e4455abbSThadeu Lima de Souza Cascardo #ifdef CONFIG_NUMA
3421af936a16SLee Schermerhorn /*
3422af936a16SLee Schermerhorn  * per node 'scan_unevictable_pages' attribute.  On demand re-scan of
3423af936a16SLee Schermerhorn  * a specified node's per zone unevictable lists for evictable pages.
3424af936a16SLee Schermerhorn  */
3425af936a16SLee Schermerhorn 
342610fbcf4cSKay Sievers static ssize_t read_scan_unevictable_node(struct device *dev,
342710fbcf4cSKay Sievers 					  struct device_attribute *attr,
3428af936a16SLee Schermerhorn 					  char *buf)
3429af936a16SLee Schermerhorn {
3430264e56d8SJohannes Weiner 	warn_scan_unevictable_pages();
3431af936a16SLee Schermerhorn 	return sprintf(buf, "0\n");	/* always zero; should fit... */
3432af936a16SLee Schermerhorn }
3433af936a16SLee Schermerhorn 
343410fbcf4cSKay Sievers static ssize_t write_scan_unevictable_node(struct device *dev,
343510fbcf4cSKay Sievers 					   struct device_attribute *attr,
3436af936a16SLee Schermerhorn 					const char *buf, size_t count)
3437af936a16SLee Schermerhorn {
3438264e56d8SJohannes Weiner 	warn_scan_unevictable_pages();
3439af936a16SLee Schermerhorn 	return 1;
3440af936a16SLee Schermerhorn }
3441af936a16SLee Schermerhorn 
3442af936a16SLee Schermerhorn 
344310fbcf4cSKay Sievers static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
3444af936a16SLee Schermerhorn 			read_scan_unevictable_node,
3445af936a16SLee Schermerhorn 			write_scan_unevictable_node);
3446af936a16SLee Schermerhorn 
3447af936a16SLee Schermerhorn int scan_unevictable_register_node(struct node *node)
3448af936a16SLee Schermerhorn {
344910fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages);
3450af936a16SLee Schermerhorn }
3451af936a16SLee Schermerhorn 
3452af936a16SLee Schermerhorn void scan_unevictable_unregister_node(struct node *node)
3453af936a16SLee Schermerhorn {
345410fbcf4cSKay Sievers 	device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages);
3455af936a16SLee Schermerhorn }
3456e4455abbSThadeu Lima de Souza Cascardo #endif
3457