xref: /openbmc/linux/mm/vmscan.c (revision 3cf23841b4b76eb94d3f8d0fb3627690e4431413)
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 
55639b5f29aSHugh Dickins 	if (page_evictable(page)) {
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 	 */
59039b5f29aSHugh Dickins 	if (lru == LRU_UNEVICTABLE && page_evictable(page)) {
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,
67702c6de8dSMinchan Kim 				      enum ttu_flags ttu_flags,
67892df3a72SMel Gorman 				      unsigned long *ret_nr_dirty,
67902c6de8dSMinchan Kim 				      unsigned long *ret_nr_writeback,
68002c6de8dSMinchan Kim 				      bool force_reclaim)
6811da177e4SLinus Torvalds {
6821da177e4SLinus Torvalds 	LIST_HEAD(ret_pages);
683abe4c3b5SMel Gorman 	LIST_HEAD(free_pages);
6841da177e4SLinus Torvalds 	int pgactivate = 0;
6850e093d99SMel Gorman 	unsigned long nr_dirty = 0;
6860e093d99SMel Gorman 	unsigned long nr_congested = 0;
68705ff5137SAndrew Morton 	unsigned long nr_reclaimed = 0;
68892df3a72SMel Gorman 	unsigned long nr_writeback = 0;
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	cond_resched();
6911da177e4SLinus Torvalds 
69269980e31STim Chen 	mem_cgroup_uncharge_start();
6931da177e4SLinus Torvalds 	while (!list_empty(page_list)) {
6941da177e4SLinus Torvalds 		struct address_space *mapping;
6951da177e4SLinus Torvalds 		struct page *page;
6961da177e4SLinus Torvalds 		int may_enter_fs;
69702c6de8dSMinchan Kim 		enum page_references references = PAGEREF_RECLAIM_CLEAN;
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 		cond_resched();
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 		page = lru_to_page(page_list);
7021da177e4SLinus Torvalds 		list_del(&page->lru);
7031da177e4SLinus Torvalds 
704529ae9aaSNick Piggin 		if (!trylock_page(page))
7051da177e4SLinus Torvalds 			goto keep;
7061da177e4SLinus Torvalds 
707725d704eSNick Piggin 		VM_BUG_ON(PageActive(page));
7086a18adb3SKonstantin Khlebnikov 		VM_BUG_ON(page_zone(page) != zone);
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 		sc->nr_scanned++;
71180e43426SChristoph Lameter 
71239b5f29aSHugh Dickins 		if (unlikely(!page_evictable(page)))
713b291f000SNick Piggin 			goto cull_mlocked;
714894bc310SLee Schermerhorn 
715a6dc60f8SJohannes Weiner 		if (!sc->may_unmap && page_mapped(page))
71680e43426SChristoph Lameter 			goto keep_locked;
71780e43426SChristoph Lameter 
7181da177e4SLinus Torvalds 		/* Double the slab pressure for mapped and swapcache pages */
7191da177e4SLinus Torvalds 		if (page_mapped(page) || PageSwapCache(page))
7201da177e4SLinus Torvalds 			sc->nr_scanned++;
7211da177e4SLinus Torvalds 
722c661b078SAndy Whitcroft 		may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
723c661b078SAndy Whitcroft 			(PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
724c661b078SAndy Whitcroft 
725c661b078SAndy Whitcroft 		if (PageWriteback(page)) {
726e62e384eSMichal Hocko 			/*
727e62e384eSMichal Hocko 			 * memcg doesn't have any dirty pages throttling so we
728e62e384eSMichal Hocko 			 * could easily OOM just because too many pages are in
729c3b94f44SHugh Dickins 			 * writeback and there is nothing else to reclaim.
730e62e384eSMichal Hocko 			 *
731c3b94f44SHugh Dickins 			 * Check __GFP_IO, certainly because a loop driver
732e62e384eSMichal Hocko 			 * thread might enter reclaim, and deadlock if it waits
733e62e384eSMichal Hocko 			 * on a page for which it is needed to do the write
734e62e384eSMichal Hocko 			 * (loop masks off __GFP_IO|__GFP_FS for this reason);
735e62e384eSMichal Hocko 			 * but more thought would probably show more reasons.
736c3b94f44SHugh Dickins 			 *
737c3b94f44SHugh Dickins 			 * Don't require __GFP_FS, since we're not going into
738c3b94f44SHugh Dickins 			 * the FS, just waiting on its writeback completion.
739c3b94f44SHugh Dickins 			 * Worryingly, ext4 gfs2 and xfs allocate pages with
740c3b94f44SHugh Dickins 			 * grab_cache_page_write_begin(,,AOP_FLAG_NOFS), so
741c3b94f44SHugh Dickins 			 * testing may_enter_fs here is liable to OOM on them.
742e62e384eSMichal Hocko 			 */
743c3b94f44SHugh Dickins 			if (global_reclaim(sc) ||
744c3b94f44SHugh Dickins 			    !PageReclaim(page) || !(sc->gfp_mask & __GFP_IO)) {
745c3b94f44SHugh Dickins 				/*
746c3b94f44SHugh Dickins 				 * This is slightly racy - end_page_writeback()
747c3b94f44SHugh Dickins 				 * might have just cleared PageReclaim, then
748c3b94f44SHugh Dickins 				 * setting PageReclaim here end up interpreted
749c3b94f44SHugh Dickins 				 * as PageReadahead - but that does not matter
750c3b94f44SHugh Dickins 				 * enough to care.  What we do want is for this
751c3b94f44SHugh Dickins 				 * page to have PageReclaim set next time memcg
752c3b94f44SHugh Dickins 				 * reclaim reaches the tests above, so it will
753c3b94f44SHugh Dickins 				 * then wait_on_page_writeback() to avoid OOM;
754c3b94f44SHugh Dickins 				 * and it's also appropriate in global reclaim.
755c3b94f44SHugh Dickins 				 */
756c3b94f44SHugh Dickins 				SetPageReclaim(page);
75792df3a72SMel Gorman 				nr_writeback++;
758c3b94f44SHugh Dickins 				goto keep_locked;
759c661b078SAndy Whitcroft 			}
760c3b94f44SHugh Dickins 			wait_on_page_writeback(page);
761e62e384eSMichal Hocko 		}
7621da177e4SLinus Torvalds 
76302c6de8dSMinchan Kim 		if (!force_reclaim)
7646a18adb3SKonstantin Khlebnikov 			references = page_check_references(page, sc);
76502c6de8dSMinchan Kim 
766dfc8d636SJohannes Weiner 		switch (references) {
767dfc8d636SJohannes Weiner 		case PAGEREF_ACTIVATE:
7681da177e4SLinus Torvalds 			goto activate_locked;
76964574746SJohannes Weiner 		case PAGEREF_KEEP:
77064574746SJohannes Weiner 			goto keep_locked;
771dfc8d636SJohannes Weiner 		case PAGEREF_RECLAIM:
772dfc8d636SJohannes Weiner 		case PAGEREF_RECLAIM_CLEAN:
773dfc8d636SJohannes Weiner 			; /* try to reclaim the page below */
774dfc8d636SJohannes Weiner 		}
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 		/*
7771da177e4SLinus Torvalds 		 * Anonymous process memory has backing store?
7781da177e4SLinus Torvalds 		 * Try to allocate it some swap space here.
7791da177e4SLinus Torvalds 		 */
780b291f000SNick Piggin 		if (PageAnon(page) && !PageSwapCache(page)) {
78163eb6b93SHugh Dickins 			if (!(sc->gfp_mask & __GFP_IO))
78263eb6b93SHugh Dickins 				goto keep_locked;
783ac47b003SHugh Dickins 			if (!add_to_swap(page))
7841da177e4SLinus Torvalds 				goto activate_locked;
78563eb6b93SHugh Dickins 			may_enter_fs = 1;
786b291f000SNick Piggin 		}
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 		mapping = page_mapping(page);
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 		/*
7911da177e4SLinus Torvalds 		 * The page is mapped into the page tables of one or more
7921da177e4SLinus Torvalds 		 * processes. Try to unmap it here.
7931da177e4SLinus Torvalds 		 */
7941da177e4SLinus Torvalds 		if (page_mapped(page) && mapping) {
79502c6de8dSMinchan Kim 			switch (try_to_unmap(page, ttu_flags)) {
7961da177e4SLinus Torvalds 			case SWAP_FAIL:
7971da177e4SLinus Torvalds 				goto activate_locked;
7981da177e4SLinus Torvalds 			case SWAP_AGAIN:
7991da177e4SLinus Torvalds 				goto keep_locked;
800b291f000SNick Piggin 			case SWAP_MLOCK:
801b291f000SNick Piggin 				goto cull_mlocked;
8021da177e4SLinus Torvalds 			case SWAP_SUCCESS:
8031da177e4SLinus Torvalds 				; /* try to free the page below */
8041da177e4SLinus Torvalds 			}
8051da177e4SLinus Torvalds 		}
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 		if (PageDirty(page)) {
8080e093d99SMel Gorman 			nr_dirty++;
8090e093d99SMel Gorman 
810ee72886dSMel Gorman 			/*
811ee72886dSMel Gorman 			 * Only kswapd can writeback filesystem pages to
812f84f6e2bSMel Gorman 			 * avoid risk of stack overflow but do not writeback
813f84f6e2bSMel Gorman 			 * unless under significant pressure.
814ee72886dSMel Gorman 			 */
815f84f6e2bSMel Gorman 			if (page_is_file_cache(page) &&
8169e3b2f8cSKonstantin Khlebnikov 					(!current_is_kswapd() ||
8179e3b2f8cSKonstantin Khlebnikov 					 sc->priority >= DEF_PRIORITY - 2)) {
81849ea7eb6SMel Gorman 				/*
81949ea7eb6SMel Gorman 				 * Immediately reclaim when written back.
82049ea7eb6SMel Gorman 				 * Similar in principal to deactivate_page()
82149ea7eb6SMel Gorman 				 * except we already have the page isolated
82249ea7eb6SMel Gorman 				 * and know it's dirty
82349ea7eb6SMel Gorman 				 */
82449ea7eb6SMel Gorman 				inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE);
82549ea7eb6SMel Gorman 				SetPageReclaim(page);
82649ea7eb6SMel Gorman 
827ee72886dSMel Gorman 				goto keep_locked;
828ee72886dSMel Gorman 			}
829ee72886dSMel Gorman 
830dfc8d636SJohannes Weiner 			if (references == PAGEREF_RECLAIM_CLEAN)
8311da177e4SLinus Torvalds 				goto keep_locked;
8324dd4b920SAndrew Morton 			if (!may_enter_fs)
8331da177e4SLinus Torvalds 				goto keep_locked;
83452a8363eSChristoph Lameter 			if (!sc->may_writepage)
8351da177e4SLinus Torvalds 				goto keep_locked;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 			/* Page is dirty, try to write it out here */
8387d3579e8SKOSAKI Motohiro 			switch (pageout(page, mapping, sc)) {
8391da177e4SLinus Torvalds 			case PAGE_KEEP:
8400e093d99SMel Gorman 				nr_congested++;
8411da177e4SLinus Torvalds 				goto keep_locked;
8421da177e4SLinus Torvalds 			case PAGE_ACTIVATE:
8431da177e4SLinus Torvalds 				goto activate_locked;
8441da177e4SLinus Torvalds 			case PAGE_SUCCESS:
8457d3579e8SKOSAKI Motohiro 				if (PageWriteback(page))
84641ac1999SMel Gorman 					goto keep;
8477d3579e8SKOSAKI Motohiro 				if (PageDirty(page))
8481da177e4SLinus Torvalds 					goto keep;
8497d3579e8SKOSAKI Motohiro 
8501da177e4SLinus Torvalds 				/*
8511da177e4SLinus Torvalds 				 * A synchronous write - probably a ramdisk.  Go
8521da177e4SLinus Torvalds 				 * ahead and try to reclaim the page.
8531da177e4SLinus Torvalds 				 */
854529ae9aaSNick Piggin 				if (!trylock_page(page))
8551da177e4SLinus Torvalds 					goto keep;
8561da177e4SLinus Torvalds 				if (PageDirty(page) || PageWriteback(page))
8571da177e4SLinus Torvalds 					goto keep_locked;
8581da177e4SLinus Torvalds 				mapping = page_mapping(page);
8591da177e4SLinus Torvalds 			case PAGE_CLEAN:
8601da177e4SLinus Torvalds 				; /* try to free the page below */
8611da177e4SLinus Torvalds 			}
8621da177e4SLinus Torvalds 		}
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 		/*
8651da177e4SLinus Torvalds 		 * If the page has buffers, try to free the buffer mappings
8661da177e4SLinus Torvalds 		 * associated with this page. If we succeed we try to free
8671da177e4SLinus Torvalds 		 * the page as well.
8681da177e4SLinus Torvalds 		 *
8691da177e4SLinus Torvalds 		 * We do this even if the page is PageDirty().
8701da177e4SLinus Torvalds 		 * try_to_release_page() does not perform I/O, but it is
8711da177e4SLinus Torvalds 		 * possible for a page to have PageDirty set, but it is actually
8721da177e4SLinus Torvalds 		 * clean (all its buffers are clean).  This happens if the
8731da177e4SLinus Torvalds 		 * buffers were written out directly, with submit_bh(). ext3
8741da177e4SLinus Torvalds 		 * will do this, as well as the blockdev mapping.
8751da177e4SLinus Torvalds 		 * try_to_release_page() will discover that cleanness and will
8761da177e4SLinus Torvalds 		 * drop the buffers and mark the page clean - it can be freed.
8771da177e4SLinus Torvalds 		 *
8781da177e4SLinus Torvalds 		 * Rarely, pages can have buffers and no ->mapping.  These are
8791da177e4SLinus Torvalds 		 * the pages which were not successfully invalidated in
8801da177e4SLinus Torvalds 		 * truncate_complete_page().  We try to drop those buffers here
8811da177e4SLinus Torvalds 		 * and if that worked, and the page is no longer mapped into
8821da177e4SLinus Torvalds 		 * process address space (page_count == 1) it can be freed.
8831da177e4SLinus Torvalds 		 * Otherwise, leave the page on the LRU so it is swappable.
8841da177e4SLinus Torvalds 		 */
885266cf658SDavid Howells 		if (page_has_private(page)) {
8861da177e4SLinus Torvalds 			if (!try_to_release_page(page, sc->gfp_mask))
8871da177e4SLinus Torvalds 				goto activate_locked;
888e286781dSNick Piggin 			if (!mapping && page_count(page) == 1) {
889e286781dSNick Piggin 				unlock_page(page);
890e286781dSNick Piggin 				if (put_page_testzero(page))
8911da177e4SLinus Torvalds 					goto free_it;
892e286781dSNick Piggin 				else {
893e286781dSNick Piggin 					/*
894e286781dSNick Piggin 					 * rare race with speculative reference.
895e286781dSNick Piggin 					 * the speculative reference will free
896e286781dSNick Piggin 					 * this page shortly, so we may
897e286781dSNick Piggin 					 * increment nr_reclaimed here (and
898e286781dSNick Piggin 					 * leave it off the LRU).
899e286781dSNick Piggin 					 */
900e286781dSNick Piggin 					nr_reclaimed++;
901e286781dSNick Piggin 					continue;
902e286781dSNick Piggin 				}
903e286781dSNick Piggin 			}
9041da177e4SLinus Torvalds 		}
9051da177e4SLinus Torvalds 
906e286781dSNick Piggin 		if (!mapping || !__remove_mapping(mapping, page))
90749d2e9ccSChristoph Lameter 			goto keep_locked;
9081da177e4SLinus Torvalds 
909a978d6f5SNick Piggin 		/*
910a978d6f5SNick Piggin 		 * At this point, we have no other references and there is
911a978d6f5SNick Piggin 		 * no way to pick any more up (removed from LRU, removed
912a978d6f5SNick Piggin 		 * from pagecache). Can use non-atomic bitops now (and
913a978d6f5SNick Piggin 		 * we obviously don't have to worry about waking up a process
914a978d6f5SNick Piggin 		 * waiting on the page lock, because there are no references.
915a978d6f5SNick Piggin 		 */
916a978d6f5SNick Piggin 		__clear_page_locked(page);
917e286781dSNick Piggin free_it:
91805ff5137SAndrew Morton 		nr_reclaimed++;
919abe4c3b5SMel Gorman 
920abe4c3b5SMel Gorman 		/*
921abe4c3b5SMel Gorman 		 * Is there need to periodically free_page_list? It would
922abe4c3b5SMel Gorman 		 * appear not as the counts should be low
923abe4c3b5SMel Gorman 		 */
924abe4c3b5SMel Gorman 		list_add(&page->lru, &free_pages);
9251da177e4SLinus Torvalds 		continue;
9261da177e4SLinus Torvalds 
927b291f000SNick Piggin cull_mlocked:
92863d6c5adSHugh Dickins 		if (PageSwapCache(page))
92963d6c5adSHugh Dickins 			try_to_free_swap(page);
930b291f000SNick Piggin 		unlock_page(page);
931b291f000SNick Piggin 		putback_lru_page(page);
932b291f000SNick Piggin 		continue;
933b291f000SNick Piggin 
9341da177e4SLinus Torvalds activate_locked:
93568a22394SRik van Riel 		/* Not a candidate for swapping, so reclaim swap space. */
93668a22394SRik van Riel 		if (PageSwapCache(page) && vm_swap_full())
937a2c43eedSHugh Dickins 			try_to_free_swap(page);
938894bc310SLee Schermerhorn 		VM_BUG_ON(PageActive(page));
9391da177e4SLinus Torvalds 		SetPageActive(page);
9401da177e4SLinus Torvalds 		pgactivate++;
9411da177e4SLinus Torvalds keep_locked:
9421da177e4SLinus Torvalds 		unlock_page(page);
9431da177e4SLinus Torvalds keep:
9441da177e4SLinus Torvalds 		list_add(&page->lru, &ret_pages);
945b291f000SNick Piggin 		VM_BUG_ON(PageLRU(page) || PageUnevictable(page));
9461da177e4SLinus Torvalds 	}
947abe4c3b5SMel Gorman 
9480e093d99SMel Gorman 	/*
9490e093d99SMel Gorman 	 * Tag a zone as congested if all the dirty pages encountered were
9500e093d99SMel Gorman 	 * backed by a congested BDI. In this case, reclaimers should just
9510e093d99SMel Gorman 	 * back off and wait for congestion to clear because further reclaim
9520e093d99SMel Gorman 	 * will encounter the same problem
9530e093d99SMel Gorman 	 */
95489b5fae5SJohannes Weiner 	if (nr_dirty && nr_dirty == nr_congested && global_reclaim(sc))
9556a18adb3SKonstantin Khlebnikov 		zone_set_flag(zone, ZONE_CONGESTED);
9560e093d99SMel Gorman 
957cc59850eSKonstantin Khlebnikov 	free_hot_cold_page_list(&free_pages, 1);
958abe4c3b5SMel Gorman 
9591da177e4SLinus Torvalds 	list_splice(&ret_pages, page_list);
960f8891e5eSChristoph Lameter 	count_vm_events(PGACTIVATE, pgactivate);
96169980e31STim Chen 	mem_cgroup_uncharge_end();
96292df3a72SMel Gorman 	*ret_nr_dirty += nr_dirty;
96392df3a72SMel Gorman 	*ret_nr_writeback += nr_writeback;
96405ff5137SAndrew Morton 	return nr_reclaimed;
9651da177e4SLinus Torvalds }
9661da177e4SLinus Torvalds 
96702c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone,
96802c6de8dSMinchan Kim 					    struct list_head *page_list)
96902c6de8dSMinchan Kim {
97002c6de8dSMinchan Kim 	struct scan_control sc = {
97102c6de8dSMinchan Kim 		.gfp_mask = GFP_KERNEL,
97202c6de8dSMinchan Kim 		.priority = DEF_PRIORITY,
97302c6de8dSMinchan Kim 		.may_unmap = 1,
97402c6de8dSMinchan Kim 	};
97502c6de8dSMinchan Kim 	unsigned long ret, dummy1, dummy2;
97602c6de8dSMinchan Kim 	struct page *page, *next;
97702c6de8dSMinchan Kim 	LIST_HEAD(clean_pages);
97802c6de8dSMinchan Kim 
97902c6de8dSMinchan Kim 	list_for_each_entry_safe(page, next, page_list, lru) {
98002c6de8dSMinchan Kim 		if (page_is_file_cache(page) && !PageDirty(page)) {
98102c6de8dSMinchan Kim 			ClearPageActive(page);
98202c6de8dSMinchan Kim 			list_move(&page->lru, &clean_pages);
98302c6de8dSMinchan Kim 		}
98402c6de8dSMinchan Kim 	}
98502c6de8dSMinchan Kim 
98602c6de8dSMinchan Kim 	ret = shrink_page_list(&clean_pages, zone, &sc,
98702c6de8dSMinchan Kim 				TTU_UNMAP|TTU_IGNORE_ACCESS,
98802c6de8dSMinchan Kim 				&dummy1, &dummy2, true);
98902c6de8dSMinchan Kim 	list_splice(&clean_pages, page_list);
99002c6de8dSMinchan Kim 	__mod_zone_page_state(zone, NR_ISOLATED_FILE, -ret);
99102c6de8dSMinchan Kim 	return ret;
99202c6de8dSMinchan Kim }
99302c6de8dSMinchan Kim 
9945ad333ebSAndy Whitcroft /*
9955ad333ebSAndy Whitcroft  * Attempt to remove the specified page from its LRU.  Only take this page
9965ad333ebSAndy Whitcroft  * if it is of the appropriate PageActive status.  Pages which are being
9975ad333ebSAndy Whitcroft  * freed elsewhere are also ignored.
9985ad333ebSAndy Whitcroft  *
9995ad333ebSAndy Whitcroft  * page:	page to consider
10005ad333ebSAndy Whitcroft  * mode:	one of the LRU isolation modes defined above
10015ad333ebSAndy Whitcroft  *
10025ad333ebSAndy Whitcroft  * returns 0 on success, -ve errno on failure.
10035ad333ebSAndy Whitcroft  */
1004f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode)
10055ad333ebSAndy Whitcroft {
10065ad333ebSAndy Whitcroft 	int ret = -EINVAL;
10075ad333ebSAndy Whitcroft 
10085ad333ebSAndy Whitcroft 	/* Only take pages on the LRU. */
10095ad333ebSAndy Whitcroft 	if (!PageLRU(page))
10105ad333ebSAndy Whitcroft 		return ret;
10115ad333ebSAndy Whitcroft 
1012e46a2879SMinchan Kim 	/* Compaction should not handle unevictable pages but CMA can do so */
1013e46a2879SMinchan Kim 	if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE))
1014894bc310SLee Schermerhorn 		return ret;
1015894bc310SLee Schermerhorn 
10165ad333ebSAndy Whitcroft 	ret = -EBUSY;
101708e552c6SKAMEZAWA Hiroyuki 
1018c8244935SMel Gorman 	/*
1019c8244935SMel Gorman 	 * To minimise LRU disruption, the caller can indicate that it only
1020c8244935SMel Gorman 	 * wants to isolate pages it will be able to operate on without
1021c8244935SMel Gorman 	 * blocking - clean pages for the most part.
1022c8244935SMel Gorman 	 *
1023c8244935SMel Gorman 	 * ISOLATE_CLEAN means that only clean pages should be isolated. This
1024c8244935SMel Gorman 	 * is used by reclaim when it is cannot write to backing storage
1025c8244935SMel Gorman 	 *
1026c8244935SMel Gorman 	 * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages
1027c8244935SMel Gorman 	 * that it is possible to migrate without blocking
1028c8244935SMel Gorman 	 */
1029c8244935SMel Gorman 	if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) {
1030c8244935SMel Gorman 		/* All the caller can do on PageWriteback is block */
1031c8244935SMel Gorman 		if (PageWriteback(page))
103239deaf85SMinchan Kim 			return ret;
103339deaf85SMinchan Kim 
1034c8244935SMel Gorman 		if (PageDirty(page)) {
1035c8244935SMel Gorman 			struct address_space *mapping;
1036c8244935SMel Gorman 
1037c8244935SMel Gorman 			/* ISOLATE_CLEAN means only clean pages */
1038c8244935SMel Gorman 			if (mode & ISOLATE_CLEAN)
1039c8244935SMel Gorman 				return ret;
1040c8244935SMel Gorman 
1041c8244935SMel Gorman 			/*
1042c8244935SMel Gorman 			 * Only pages without mappings or that have a
1043c8244935SMel Gorman 			 * ->migratepage callback are possible to migrate
1044c8244935SMel Gorman 			 * without blocking
1045c8244935SMel Gorman 			 */
1046c8244935SMel Gorman 			mapping = page_mapping(page);
1047c8244935SMel Gorman 			if (mapping && !mapping->a_ops->migratepage)
1048c8244935SMel Gorman 				return ret;
1049c8244935SMel Gorman 		}
1050c8244935SMel Gorman 	}
1051c8244935SMel Gorman 
1052f80c0673SMinchan Kim 	if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
1053f80c0673SMinchan Kim 		return ret;
1054f80c0673SMinchan Kim 
10555ad333ebSAndy Whitcroft 	if (likely(get_page_unless_zero(page))) {
10565ad333ebSAndy Whitcroft 		/*
10575ad333ebSAndy Whitcroft 		 * Be careful not to clear PageLRU until after we're
10585ad333ebSAndy Whitcroft 		 * sure the page is not being freed elsewhere -- the
10595ad333ebSAndy Whitcroft 		 * page release code relies on it.
10605ad333ebSAndy Whitcroft 		 */
10615ad333ebSAndy Whitcroft 		ClearPageLRU(page);
10625ad333ebSAndy Whitcroft 		ret = 0;
10635ad333ebSAndy Whitcroft 	}
10645ad333ebSAndy Whitcroft 
10655ad333ebSAndy Whitcroft 	return ret;
10665ad333ebSAndy Whitcroft }
10675ad333ebSAndy Whitcroft 
106849d2e9ccSChristoph Lameter /*
10691da177e4SLinus Torvalds  * zone->lru_lock is heavily contended.  Some of the functions that
10701da177e4SLinus Torvalds  * shrink the lists perform better by taking out a batch of pages
10711da177e4SLinus Torvalds  * and working on them outside the LRU lock.
10721da177e4SLinus Torvalds  *
10731da177e4SLinus Torvalds  * For pagecache intensive workloads, this function is the hottest
10741da177e4SLinus Torvalds  * spot in the kernel (apart from copy_*_user functions).
10751da177e4SLinus Torvalds  *
10761da177e4SLinus Torvalds  * Appropriate locks must be held before calling this function.
10771da177e4SLinus Torvalds  *
10781da177e4SLinus Torvalds  * @nr_to_scan:	The number of pages to look through on the list.
10795dc35979SKonstantin Khlebnikov  * @lruvec:	The LRU vector to pull pages from.
10801da177e4SLinus Torvalds  * @dst:	The temp list to put pages on to.
1081f626012dSHugh Dickins  * @nr_scanned:	The number of pages that were scanned.
1082fe2c2a10SRik van Riel  * @sc:		The scan_control struct for this reclaim session
10835ad333ebSAndy Whitcroft  * @mode:	One of the LRU isolation modes
10843cb99451SKonstantin Khlebnikov  * @lru:	LRU list id for isolating
10851da177e4SLinus Torvalds  *
10861da177e4SLinus Torvalds  * returns how many pages were moved onto *@dst.
10871da177e4SLinus Torvalds  */
108869e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
10895dc35979SKonstantin Khlebnikov 		struct lruvec *lruvec, struct list_head *dst,
1090fe2c2a10SRik van Riel 		unsigned long *nr_scanned, struct scan_control *sc,
10913cb99451SKonstantin Khlebnikov 		isolate_mode_t mode, enum lru_list lru)
10921da177e4SLinus Torvalds {
109375b00af7SHugh Dickins 	struct list_head *src = &lruvec->lists[lru];
109469e05944SAndrew Morton 	unsigned long nr_taken = 0;
1095c9b02d97SWu Fengguang 	unsigned long scan;
10961da177e4SLinus Torvalds 
1097c9b02d97SWu Fengguang 	for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
10985ad333ebSAndy Whitcroft 		struct page *page;
1099fa9add64SHugh Dickins 		int nr_pages;
11005ad333ebSAndy Whitcroft 
11011da177e4SLinus Torvalds 		page = lru_to_page(src);
11021da177e4SLinus Torvalds 		prefetchw_prev_lru_page(page, src, flags);
11031da177e4SLinus Torvalds 
1104725d704eSNick Piggin 		VM_BUG_ON(!PageLRU(page));
11058d438f96SNick Piggin 
1106f3fd4a61SKonstantin Khlebnikov 		switch (__isolate_lru_page(page, mode)) {
11075ad333ebSAndy Whitcroft 		case 0:
1108fa9add64SHugh Dickins 			nr_pages = hpage_nr_pages(page);
1109fa9add64SHugh Dickins 			mem_cgroup_update_lru_size(lruvec, lru, -nr_pages);
11105ad333ebSAndy Whitcroft 			list_move(&page->lru, dst);
1111fa9add64SHugh Dickins 			nr_taken += nr_pages;
11125ad333ebSAndy Whitcroft 			break;
11137c8ee9a8SNick Piggin 
11145ad333ebSAndy Whitcroft 		case -EBUSY:
11155ad333ebSAndy Whitcroft 			/* else it is being freed elsewhere */
11165ad333ebSAndy Whitcroft 			list_move(&page->lru, src);
11175ad333ebSAndy Whitcroft 			continue;
11185ad333ebSAndy Whitcroft 
11195ad333ebSAndy Whitcroft 		default:
11205ad333ebSAndy Whitcroft 			BUG();
11215ad333ebSAndy Whitcroft 		}
11225ad333ebSAndy Whitcroft 	}
11231da177e4SLinus Torvalds 
1124f626012dSHugh Dickins 	*nr_scanned = scan;
112575b00af7SHugh Dickins 	trace_mm_vmscan_lru_isolate(sc->order, nr_to_scan, scan,
112675b00af7SHugh Dickins 				    nr_taken, mode, is_file_lru(lru));
11271da177e4SLinus Torvalds 	return nr_taken;
11281da177e4SLinus Torvalds }
11291da177e4SLinus Torvalds 
113062695a84SNick Piggin /**
113162695a84SNick Piggin  * isolate_lru_page - tries to isolate a page from its LRU list
113262695a84SNick Piggin  * @page: page to isolate from its LRU list
113362695a84SNick Piggin  *
113462695a84SNick Piggin  * Isolates a @page from an LRU list, clears PageLRU and adjusts the
113562695a84SNick Piggin  * vmstat statistic corresponding to whatever LRU list the page was on.
113662695a84SNick Piggin  *
113762695a84SNick Piggin  * Returns 0 if the page was removed from an LRU list.
113862695a84SNick Piggin  * Returns -EBUSY if the page was not on an LRU list.
113962695a84SNick Piggin  *
114062695a84SNick Piggin  * The returned page will have PageLRU() cleared.  If it was found on
1141894bc310SLee Schermerhorn  * the active list, it will have PageActive set.  If it was found on
1142894bc310SLee Schermerhorn  * the unevictable list, it will have the PageUnevictable bit set. That flag
1143894bc310SLee Schermerhorn  * may need to be cleared by the caller before letting the page go.
114462695a84SNick Piggin  *
114562695a84SNick Piggin  * The vmstat statistic corresponding to the list on which the page was
114662695a84SNick Piggin  * found will be decremented.
114762695a84SNick Piggin  *
114862695a84SNick Piggin  * Restrictions:
114962695a84SNick Piggin  * (1) Must be called with an elevated refcount on the page. This is a
115062695a84SNick Piggin  *     fundamentnal difference from isolate_lru_pages (which is called
115162695a84SNick Piggin  *     without a stable reference).
115262695a84SNick Piggin  * (2) the lru_lock must not be held.
115362695a84SNick Piggin  * (3) interrupts must be enabled.
115462695a84SNick Piggin  */
115562695a84SNick Piggin int isolate_lru_page(struct page *page)
115662695a84SNick Piggin {
115762695a84SNick Piggin 	int ret = -EBUSY;
115862695a84SNick Piggin 
11590c917313SKonstantin Khlebnikov 	VM_BUG_ON(!page_count(page));
11600c917313SKonstantin Khlebnikov 
116162695a84SNick Piggin 	if (PageLRU(page)) {
116262695a84SNick Piggin 		struct zone *zone = page_zone(page);
1163fa9add64SHugh Dickins 		struct lruvec *lruvec;
116462695a84SNick Piggin 
116562695a84SNick Piggin 		spin_lock_irq(&zone->lru_lock);
1166fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
11670c917313SKonstantin Khlebnikov 		if (PageLRU(page)) {
1168894bc310SLee Schermerhorn 			int lru = page_lru(page);
11690c917313SKonstantin Khlebnikov 			get_page(page);
117062695a84SNick Piggin 			ClearPageLRU(page);
1171fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
1172fa9add64SHugh Dickins 			ret = 0;
117362695a84SNick Piggin 		}
117462695a84SNick Piggin 		spin_unlock_irq(&zone->lru_lock);
117562695a84SNick Piggin 	}
117662695a84SNick Piggin 	return ret;
117762695a84SNick Piggin }
117862695a84SNick Piggin 
11795ad333ebSAndy Whitcroft /*
1180d37dd5dcSFengguang Wu  * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
1181d37dd5dcSFengguang Wu  * then get resheduled. When there are massive number of tasks doing page
1182d37dd5dcSFengguang Wu  * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
1183d37dd5dcSFengguang Wu  * the LRU list will go small and be scanned faster than necessary, leading to
1184d37dd5dcSFengguang Wu  * unnecessary swapping, thrashing and OOM.
118535cd7815SRik van Riel  */
118635cd7815SRik van Riel static int too_many_isolated(struct zone *zone, int file,
118735cd7815SRik van Riel 		struct scan_control *sc)
118835cd7815SRik van Riel {
118935cd7815SRik van Riel 	unsigned long inactive, isolated;
119035cd7815SRik van Riel 
119135cd7815SRik van Riel 	if (current_is_kswapd())
119235cd7815SRik van Riel 		return 0;
119335cd7815SRik van Riel 
119489b5fae5SJohannes Weiner 	if (!global_reclaim(sc))
119535cd7815SRik van Riel 		return 0;
119635cd7815SRik van Riel 
119735cd7815SRik van Riel 	if (file) {
119835cd7815SRik van Riel 		inactive = zone_page_state(zone, NR_INACTIVE_FILE);
119935cd7815SRik van Riel 		isolated = zone_page_state(zone, NR_ISOLATED_FILE);
120035cd7815SRik van Riel 	} else {
120135cd7815SRik van Riel 		inactive = zone_page_state(zone, NR_INACTIVE_ANON);
120235cd7815SRik van Riel 		isolated = zone_page_state(zone, NR_ISOLATED_ANON);
120335cd7815SRik van Riel 	}
120435cd7815SRik van Riel 
1205*3cf23841SFengguang Wu 	/*
1206*3cf23841SFengguang Wu 	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
1207*3cf23841SFengguang Wu 	 * won't get blocked by normal direct-reclaimers, forming a circular
1208*3cf23841SFengguang Wu 	 * deadlock.
1209*3cf23841SFengguang Wu 	 */
1210*3cf23841SFengguang Wu 	if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS)
1211*3cf23841SFengguang Wu 		inactive >>= 3;
1212*3cf23841SFengguang Wu 
121335cd7815SRik van Riel 	return isolated > inactive;
121435cd7815SRik van Riel }
121535cd7815SRik van Riel 
121666635629SMel Gorman static noinline_for_stack void
121775b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list)
121866635629SMel Gorman {
121927ac81d8SKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
122027ac81d8SKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
12213f79768fSHugh Dickins 	LIST_HEAD(pages_to_free);
122266635629SMel Gorman 
122366635629SMel Gorman 	/*
122466635629SMel Gorman 	 * Put back any unfreeable pages.
122566635629SMel Gorman 	 */
122666635629SMel Gorman 	while (!list_empty(page_list)) {
12273f79768fSHugh Dickins 		struct page *page = lru_to_page(page_list);
122866635629SMel Gorman 		int lru;
12293f79768fSHugh Dickins 
123066635629SMel Gorman 		VM_BUG_ON(PageLRU(page));
123166635629SMel Gorman 		list_del(&page->lru);
123239b5f29aSHugh Dickins 		if (unlikely(!page_evictable(page))) {
123366635629SMel Gorman 			spin_unlock_irq(&zone->lru_lock);
123466635629SMel Gorman 			putback_lru_page(page);
123566635629SMel Gorman 			spin_lock_irq(&zone->lru_lock);
123666635629SMel Gorman 			continue;
123766635629SMel Gorman 		}
1238fa9add64SHugh Dickins 
1239fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
1240fa9add64SHugh Dickins 
12417a608572SLinus Torvalds 		SetPageLRU(page);
124266635629SMel Gorman 		lru = page_lru(page);
1243fa9add64SHugh Dickins 		add_page_to_lru_list(page, lruvec, lru);
1244fa9add64SHugh Dickins 
124566635629SMel Gorman 		if (is_active_lru(lru)) {
124666635629SMel Gorman 			int file = is_file_lru(lru);
12479992af10SRik van Riel 			int numpages = hpage_nr_pages(page);
12489992af10SRik van Riel 			reclaim_stat->recent_rotated[file] += numpages;
124966635629SMel Gorman 		}
12502bcf8879SHugh Dickins 		if (put_page_testzero(page)) {
12512bcf8879SHugh Dickins 			__ClearPageLRU(page);
12522bcf8879SHugh Dickins 			__ClearPageActive(page);
1253fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
12542bcf8879SHugh Dickins 
12552bcf8879SHugh Dickins 			if (unlikely(PageCompound(page))) {
125666635629SMel Gorman 				spin_unlock_irq(&zone->lru_lock);
12572bcf8879SHugh Dickins 				(*get_compound_page_dtor(page))(page);
125866635629SMel Gorman 				spin_lock_irq(&zone->lru_lock);
12592bcf8879SHugh Dickins 			} else
12602bcf8879SHugh Dickins 				list_add(&page->lru, &pages_to_free);
126166635629SMel Gorman 		}
126266635629SMel Gorman 	}
126366635629SMel Gorman 
12643f79768fSHugh Dickins 	/*
12653f79768fSHugh Dickins 	 * To save our caller's stack, now use input list for pages to free.
12663f79768fSHugh Dickins 	 */
12673f79768fSHugh Dickins 	list_splice(&pages_to_free, page_list);
126866635629SMel Gorman }
126966635629SMel Gorman 
127066635629SMel Gorman /*
12711742f19fSAndrew Morton  * shrink_inactive_list() is a helper for shrink_zone().  It returns the number
12721742f19fSAndrew Morton  * of reclaimed pages
12731da177e4SLinus Torvalds  */
127466635629SMel Gorman static noinline_for_stack unsigned long
12751a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
12769e3b2f8cSKonstantin Khlebnikov 		     struct scan_control *sc, enum lru_list lru)
12771da177e4SLinus Torvalds {
12781da177e4SLinus Torvalds 	LIST_HEAD(page_list);
1279e247dbceSKOSAKI Motohiro 	unsigned long nr_scanned;
128005ff5137SAndrew Morton 	unsigned long nr_reclaimed = 0;
1281e247dbceSKOSAKI Motohiro 	unsigned long nr_taken;
128292df3a72SMel Gorman 	unsigned long nr_dirty = 0;
128392df3a72SMel Gorman 	unsigned long nr_writeback = 0;
1284f3fd4a61SKonstantin Khlebnikov 	isolate_mode_t isolate_mode = 0;
12853cb99451SKonstantin Khlebnikov 	int file = is_file_lru(lru);
12861a93be0eSKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
12871a93be0eSKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
128878dc583dSKOSAKI Motohiro 
128935cd7815SRik van Riel 	while (unlikely(too_many_isolated(zone, file, sc))) {
129058355c78SKOSAKI Motohiro 		congestion_wait(BLK_RW_ASYNC, HZ/10);
129135cd7815SRik van Riel 
129235cd7815SRik van Riel 		/* We are about to die and free our memory. Return now. */
129335cd7815SRik van Riel 		if (fatal_signal_pending(current))
129435cd7815SRik van Riel 			return SWAP_CLUSTER_MAX;
129535cd7815SRik van Riel 	}
129635cd7815SRik van Riel 
12971da177e4SLinus Torvalds 	lru_add_drain();
1298f80c0673SMinchan Kim 
1299f80c0673SMinchan Kim 	if (!sc->may_unmap)
130061317289SHillf Danton 		isolate_mode |= ISOLATE_UNMAPPED;
1301f80c0673SMinchan Kim 	if (!sc->may_writepage)
130261317289SHillf Danton 		isolate_mode |= ISOLATE_CLEAN;
1303f80c0673SMinchan Kim 
13041da177e4SLinus Torvalds 	spin_lock_irq(&zone->lru_lock);
13051da177e4SLinus Torvalds 
13065dc35979SKonstantin Khlebnikov 	nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
13075dc35979SKonstantin Khlebnikov 				     &nr_scanned, sc, isolate_mode, lru);
130895d918fcSKonstantin Khlebnikov 
130995d918fcSKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken);
131095d918fcSKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
131195d918fcSKonstantin Khlebnikov 
131289b5fae5SJohannes Weiner 	if (global_reclaim(sc)) {
1313e247dbceSKOSAKI Motohiro 		zone->pages_scanned += nr_scanned;
1314b35ea17bSKOSAKI Motohiro 		if (current_is_kswapd())
131575b00af7SHugh Dickins 			__count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scanned);
1316b35ea17bSKOSAKI Motohiro 		else
131775b00af7SHugh Dickins 			__count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scanned);
1318b35ea17bSKOSAKI Motohiro 	}
131966635629SMel Gorman 	spin_unlock_irq(&zone->lru_lock);
1320d563c050SHillf Danton 
1321d563c050SHillf Danton 	if (nr_taken == 0)
132266635629SMel Gorman 		return 0;
1323b35ea17bSKOSAKI Motohiro 
132402c6de8dSMinchan Kim 	nr_reclaimed = shrink_page_list(&page_list, zone, sc, TTU_UNMAP,
132502c6de8dSMinchan Kim 					&nr_dirty, &nr_writeback, false);
1326c661b078SAndy Whitcroft 
13273f79768fSHugh Dickins 	spin_lock_irq(&zone->lru_lock);
13283f79768fSHugh Dickins 
132995d918fcSKonstantin Khlebnikov 	reclaim_stat->recent_scanned[file] += nr_taken;
1330d563c050SHillf Danton 
1331904249aaSYing Han 	if (global_reclaim(sc)) {
1332b35ea17bSKOSAKI Motohiro 		if (current_is_kswapd())
1333904249aaSYing Han 			__count_zone_vm_events(PGSTEAL_KSWAPD, zone,
1334904249aaSYing Han 					       nr_reclaimed);
1335904249aaSYing Han 		else
1336904249aaSYing Han 			__count_zone_vm_events(PGSTEAL_DIRECT, zone,
1337904249aaSYing Han 					       nr_reclaimed);
1338904249aaSYing Han 	}
1339a74609faSNick Piggin 
134027ac81d8SKonstantin Khlebnikov 	putback_inactive_pages(lruvec, &page_list);
13413f79768fSHugh Dickins 
134295d918fcSKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
13433f79768fSHugh Dickins 
13443f79768fSHugh Dickins 	spin_unlock_irq(&zone->lru_lock);
13453f79768fSHugh Dickins 
13463f79768fSHugh Dickins 	free_hot_cold_page_list(&page_list, 1);
1347e11da5b4SMel Gorman 
134892df3a72SMel Gorman 	/*
134992df3a72SMel Gorman 	 * If reclaim is isolating dirty pages under writeback, it implies
135092df3a72SMel Gorman 	 * that the long-lived page allocation rate is exceeding the page
135192df3a72SMel Gorman 	 * laundering rate. Either the global limits are not being effective
135292df3a72SMel Gorman 	 * at throttling processes due to the page distribution throughout
135392df3a72SMel Gorman 	 * zones or there is heavy usage of a slow backing device. The
135492df3a72SMel Gorman 	 * only option is to throttle from reclaim context which is not ideal
135592df3a72SMel Gorman 	 * as there is no guarantee the dirtying process is throttled in the
135692df3a72SMel Gorman 	 * same way balance_dirty_pages() manages.
135792df3a72SMel Gorman 	 *
135892df3a72SMel Gorman 	 * This scales the number of dirty pages that must be under writeback
135992df3a72SMel Gorman 	 * before throttling depending on priority. It is a simple backoff
136092df3a72SMel Gorman 	 * function that has the most effect in the range DEF_PRIORITY to
136192df3a72SMel Gorman 	 * DEF_PRIORITY-2 which is the priority reclaim is considered to be
136292df3a72SMel Gorman 	 * in trouble and reclaim is considered to be in trouble.
136392df3a72SMel Gorman 	 *
136492df3a72SMel Gorman 	 * DEF_PRIORITY   100% isolated pages must be PageWriteback to throttle
136592df3a72SMel Gorman 	 * DEF_PRIORITY-1  50% must be PageWriteback
136692df3a72SMel Gorman 	 * DEF_PRIORITY-2  25% must be PageWriteback, kswapd in trouble
136792df3a72SMel Gorman 	 * ...
136892df3a72SMel Gorman 	 * DEF_PRIORITY-6 For SWAP_CLUSTER_MAX isolated pages, throttle if any
136992df3a72SMel Gorman 	 *                     isolated page is PageWriteback
137092df3a72SMel Gorman 	 */
13719e3b2f8cSKonstantin Khlebnikov 	if (nr_writeback && nr_writeback >=
13729e3b2f8cSKonstantin Khlebnikov 			(nr_taken >> (DEF_PRIORITY - sc->priority)))
137392df3a72SMel Gorman 		wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
137492df3a72SMel Gorman 
1375e11da5b4SMel Gorman 	trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id,
1376e11da5b4SMel Gorman 		zone_idx(zone),
1377e11da5b4SMel Gorman 		nr_scanned, nr_reclaimed,
13789e3b2f8cSKonstantin Khlebnikov 		sc->priority,
137923b9da55SMel Gorman 		trace_shrink_flags(file));
138005ff5137SAndrew Morton 	return nr_reclaimed;
13811da177e4SLinus Torvalds }
13821da177e4SLinus Torvalds 
13833bb1a852SMartin Bligh /*
13841cfb419bSKAMEZAWA Hiroyuki  * This moves pages from the active list to the inactive list.
13851cfb419bSKAMEZAWA Hiroyuki  *
13861cfb419bSKAMEZAWA Hiroyuki  * We move them the other way if the page is referenced by one or more
13871cfb419bSKAMEZAWA Hiroyuki  * processes, from rmap.
13881cfb419bSKAMEZAWA Hiroyuki  *
13891cfb419bSKAMEZAWA Hiroyuki  * If the pages are mostly unmapped, the processing is fast and it is
13901cfb419bSKAMEZAWA Hiroyuki  * appropriate to hold zone->lru_lock across the whole operation.  But if
13911cfb419bSKAMEZAWA Hiroyuki  * the pages are mapped, the processing is slow (page_referenced()) so we
13921cfb419bSKAMEZAWA Hiroyuki  * should drop zone->lru_lock around each page.  It's impossible to balance
13931cfb419bSKAMEZAWA Hiroyuki  * this, so instead we remove the pages from the LRU while processing them.
13941cfb419bSKAMEZAWA Hiroyuki  * It is safe to rely on PG_active against the non-LRU pages in here because
13951cfb419bSKAMEZAWA Hiroyuki  * nobody will play with that bit on a non-LRU page.
13961cfb419bSKAMEZAWA Hiroyuki  *
13971cfb419bSKAMEZAWA Hiroyuki  * The downside is that we have to touch page->_count against each page.
13981cfb419bSKAMEZAWA Hiroyuki  * But we had to alter page->flags anyway.
13991cfb419bSKAMEZAWA Hiroyuki  */
14001cfb419bSKAMEZAWA Hiroyuki 
1401fa9add64SHugh Dickins static void move_active_pages_to_lru(struct lruvec *lruvec,
14023eb4140fSWu Fengguang 				     struct list_head *list,
14032bcf8879SHugh Dickins 				     struct list_head *pages_to_free,
14043eb4140fSWu Fengguang 				     enum lru_list lru)
14053eb4140fSWu Fengguang {
1406fa9add64SHugh Dickins 	struct zone *zone = lruvec_zone(lruvec);
14073eb4140fSWu Fengguang 	unsigned long pgmoved = 0;
14083eb4140fSWu Fengguang 	struct page *page;
1409fa9add64SHugh Dickins 	int nr_pages;
14103eb4140fSWu Fengguang 
14113eb4140fSWu Fengguang 	while (!list_empty(list)) {
14123eb4140fSWu Fengguang 		page = lru_to_page(list);
1413fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
14143eb4140fSWu Fengguang 
14153eb4140fSWu Fengguang 		VM_BUG_ON(PageLRU(page));
14163eb4140fSWu Fengguang 		SetPageLRU(page);
14173eb4140fSWu Fengguang 
1418fa9add64SHugh Dickins 		nr_pages = hpage_nr_pages(page);
1419fa9add64SHugh Dickins 		mem_cgroup_update_lru_size(lruvec, lru, nr_pages);
1420925b7673SJohannes Weiner 		list_move(&page->lru, &lruvec->lists[lru]);
1421fa9add64SHugh Dickins 		pgmoved += nr_pages;
14223eb4140fSWu Fengguang 
14232bcf8879SHugh Dickins 		if (put_page_testzero(page)) {
14242bcf8879SHugh Dickins 			__ClearPageLRU(page);
14252bcf8879SHugh Dickins 			__ClearPageActive(page);
1426fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
14272bcf8879SHugh Dickins 
14282bcf8879SHugh Dickins 			if (unlikely(PageCompound(page))) {
14293eb4140fSWu Fengguang 				spin_unlock_irq(&zone->lru_lock);
14302bcf8879SHugh Dickins 				(*get_compound_page_dtor(page))(page);
14313eb4140fSWu Fengguang 				spin_lock_irq(&zone->lru_lock);
14322bcf8879SHugh Dickins 			} else
14332bcf8879SHugh Dickins 				list_add(&page->lru, pages_to_free);
14343eb4140fSWu Fengguang 		}
14353eb4140fSWu Fengguang 	}
14363eb4140fSWu Fengguang 	__mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
14373eb4140fSWu Fengguang 	if (!is_active_lru(lru))
14383eb4140fSWu Fengguang 		__count_vm_events(PGDEACTIVATE, pgmoved);
14393eb4140fSWu Fengguang }
14401cfb419bSKAMEZAWA Hiroyuki 
1441f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan,
14421a93be0eSKonstantin Khlebnikov 			       struct lruvec *lruvec,
1443f16015fbSJohannes Weiner 			       struct scan_control *sc,
14449e3b2f8cSKonstantin Khlebnikov 			       enum lru_list lru)
14451cfb419bSKAMEZAWA Hiroyuki {
144644c241f1SKOSAKI Motohiro 	unsigned long nr_taken;
1447f626012dSHugh Dickins 	unsigned long nr_scanned;
14486fe6b7e3SWu Fengguang 	unsigned long vm_flags;
14491cfb419bSKAMEZAWA Hiroyuki 	LIST_HEAD(l_hold);	/* The pages which were snipped off */
14508cab4754SWu Fengguang 	LIST_HEAD(l_active);
1451b69408e8SChristoph Lameter 	LIST_HEAD(l_inactive);
14521cfb419bSKAMEZAWA Hiroyuki 	struct page *page;
14531a93be0eSKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
145444c241f1SKOSAKI Motohiro 	unsigned long nr_rotated = 0;
1455f3fd4a61SKonstantin Khlebnikov 	isolate_mode_t isolate_mode = 0;
14563cb99451SKonstantin Khlebnikov 	int file = is_file_lru(lru);
14571a93be0eSKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
14581cfb419bSKAMEZAWA Hiroyuki 
14591da177e4SLinus Torvalds 	lru_add_drain();
1460f80c0673SMinchan Kim 
1461f80c0673SMinchan Kim 	if (!sc->may_unmap)
146261317289SHillf Danton 		isolate_mode |= ISOLATE_UNMAPPED;
1463f80c0673SMinchan Kim 	if (!sc->may_writepage)
146461317289SHillf Danton 		isolate_mode |= ISOLATE_CLEAN;
1465f80c0673SMinchan Kim 
14661da177e4SLinus Torvalds 	spin_lock_irq(&zone->lru_lock);
1467925b7673SJohannes Weiner 
14685dc35979SKonstantin Khlebnikov 	nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
14695dc35979SKonstantin Khlebnikov 				     &nr_scanned, sc, isolate_mode, lru);
147089b5fae5SJohannes Weiner 	if (global_reclaim(sc))
1471f626012dSHugh Dickins 		zone->pages_scanned += nr_scanned;
147289b5fae5SJohannes Weiner 
1473b7c46d15SJohannes Weiner 	reclaim_stat->recent_scanned[file] += nr_taken;
14741cfb419bSKAMEZAWA Hiroyuki 
1475f626012dSHugh Dickins 	__count_zone_vm_events(PGREFILL, zone, nr_scanned);
14763cb99451SKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken);
1477a731286dSKOSAKI Motohiro 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
14781da177e4SLinus Torvalds 	spin_unlock_irq(&zone->lru_lock);
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 	while (!list_empty(&l_hold)) {
14811da177e4SLinus Torvalds 		cond_resched();
14821da177e4SLinus Torvalds 		page = lru_to_page(&l_hold);
14831da177e4SLinus Torvalds 		list_del(&page->lru);
14847e9cd484SRik van Riel 
148539b5f29aSHugh Dickins 		if (unlikely(!page_evictable(page))) {
1486894bc310SLee Schermerhorn 			putback_lru_page(page);
1487894bc310SLee Schermerhorn 			continue;
1488894bc310SLee Schermerhorn 		}
1489894bc310SLee Schermerhorn 
1490cc715d99SMel Gorman 		if (unlikely(buffer_heads_over_limit)) {
1491cc715d99SMel Gorman 			if (page_has_private(page) && trylock_page(page)) {
1492cc715d99SMel Gorman 				if (page_has_private(page))
1493cc715d99SMel Gorman 					try_to_release_page(page, 0);
1494cc715d99SMel Gorman 				unlock_page(page);
1495cc715d99SMel Gorman 			}
1496cc715d99SMel Gorman 		}
1497cc715d99SMel Gorman 
1498c3ac9a8aSJohannes Weiner 		if (page_referenced(page, 0, sc->target_mem_cgroup,
1499c3ac9a8aSJohannes Weiner 				    &vm_flags)) {
15009992af10SRik van Riel 			nr_rotated += hpage_nr_pages(page);
15018cab4754SWu Fengguang 			/*
15028cab4754SWu Fengguang 			 * Identify referenced, file-backed active pages and
15038cab4754SWu Fengguang 			 * give them one more trip around the active list. So
15048cab4754SWu Fengguang 			 * that executable code get better chances to stay in
15058cab4754SWu Fengguang 			 * memory under moderate memory pressure.  Anon pages
15068cab4754SWu Fengguang 			 * are not likely to be evicted by use-once streaming
15078cab4754SWu Fengguang 			 * IO, plus JVM can create lots of anon VM_EXEC pages,
15088cab4754SWu Fengguang 			 * so we ignore them here.
15098cab4754SWu Fengguang 			 */
151041e20983SWu Fengguang 			if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
15118cab4754SWu Fengguang 				list_add(&page->lru, &l_active);
15128cab4754SWu Fengguang 				continue;
15138cab4754SWu Fengguang 			}
15148cab4754SWu Fengguang 		}
15157e9cd484SRik van Riel 
15165205e56eSKOSAKI Motohiro 		ClearPageActive(page);	/* we are de-activating */
15171da177e4SLinus Torvalds 		list_add(&page->lru, &l_inactive);
15181da177e4SLinus Torvalds 	}
15191da177e4SLinus Torvalds 
1520b555749aSAndrew Morton 	/*
15218cab4754SWu Fengguang 	 * Move pages back to the lru list.
1522b555749aSAndrew Morton 	 */
15232a1dc509SJohannes Weiner 	spin_lock_irq(&zone->lru_lock);
15244f98a2feSRik van Riel 	/*
15258cab4754SWu Fengguang 	 * Count referenced pages from currently used mappings as rotated,
15268cab4754SWu Fengguang 	 * even though only some of them are actually re-activated.  This
15278cab4754SWu Fengguang 	 * helps balance scan pressure between file and anonymous pages in
15288cab4754SWu Fengguang 	 * get_scan_ratio.
1529556adecbSRik van Riel 	 */
1530b7c46d15SJohannes Weiner 	reclaim_stat->recent_rotated[file] += nr_rotated;
1531556adecbSRik van Riel 
1532fa9add64SHugh Dickins 	move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru);
1533fa9add64SHugh Dickins 	move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE);
1534a731286dSKOSAKI Motohiro 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
1535f8891e5eSChristoph Lameter 	spin_unlock_irq(&zone->lru_lock);
15362bcf8879SHugh Dickins 
15372bcf8879SHugh Dickins 	free_hot_cold_page_list(&l_hold, 1);
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds 
154074e3f3c3SMinchan Kim #ifdef CONFIG_SWAP
154114797e23SKOSAKI Motohiro static int inactive_anon_is_low_global(struct zone *zone)
1542f89eb90eSKOSAKI Motohiro {
1543f89eb90eSKOSAKI Motohiro 	unsigned long active, inactive;
1544f89eb90eSKOSAKI Motohiro 
1545f89eb90eSKOSAKI Motohiro 	active = zone_page_state(zone, NR_ACTIVE_ANON);
1546f89eb90eSKOSAKI Motohiro 	inactive = zone_page_state(zone, NR_INACTIVE_ANON);
1547f89eb90eSKOSAKI Motohiro 
1548f89eb90eSKOSAKI Motohiro 	if (inactive * zone->inactive_ratio < active)
1549f89eb90eSKOSAKI Motohiro 		return 1;
1550f89eb90eSKOSAKI Motohiro 
1551f89eb90eSKOSAKI Motohiro 	return 0;
1552f89eb90eSKOSAKI Motohiro }
1553f89eb90eSKOSAKI Motohiro 
155414797e23SKOSAKI Motohiro /**
155514797e23SKOSAKI Motohiro  * inactive_anon_is_low - check if anonymous pages need to be deactivated
1556c56d5c7dSKonstantin Khlebnikov  * @lruvec: LRU vector to check
155714797e23SKOSAKI Motohiro  *
155814797e23SKOSAKI Motohiro  * Returns true if the zone does not have enough inactive anon pages,
155914797e23SKOSAKI Motohiro  * meaning some active anon pages need to be deactivated.
156014797e23SKOSAKI Motohiro  */
1561c56d5c7dSKonstantin Khlebnikov static int inactive_anon_is_low(struct lruvec *lruvec)
156214797e23SKOSAKI Motohiro {
156374e3f3c3SMinchan Kim 	/*
156474e3f3c3SMinchan Kim 	 * If we don't have swap space, anonymous page deactivation
156574e3f3c3SMinchan Kim 	 * is pointless.
156674e3f3c3SMinchan Kim 	 */
156774e3f3c3SMinchan Kim 	if (!total_swap_pages)
156874e3f3c3SMinchan Kim 		return 0;
156974e3f3c3SMinchan Kim 
1570c3c787e8SHugh Dickins 	if (!mem_cgroup_disabled())
1571c56d5c7dSKonstantin Khlebnikov 		return mem_cgroup_inactive_anon_is_low(lruvec);
1572f16015fbSJohannes Weiner 
1573c56d5c7dSKonstantin Khlebnikov 	return inactive_anon_is_low_global(lruvec_zone(lruvec));
157414797e23SKOSAKI Motohiro }
157574e3f3c3SMinchan Kim #else
1576c56d5c7dSKonstantin Khlebnikov static inline int inactive_anon_is_low(struct lruvec *lruvec)
157774e3f3c3SMinchan Kim {
157874e3f3c3SMinchan Kim 	return 0;
157974e3f3c3SMinchan Kim }
158074e3f3c3SMinchan Kim #endif
158114797e23SKOSAKI Motohiro 
158256e49d21SRik van Riel static int inactive_file_is_low_global(struct zone *zone)
158356e49d21SRik van Riel {
158456e49d21SRik van Riel 	unsigned long active, inactive;
158556e49d21SRik van Riel 
158656e49d21SRik van Riel 	active = zone_page_state(zone, NR_ACTIVE_FILE);
158756e49d21SRik van Riel 	inactive = zone_page_state(zone, NR_INACTIVE_FILE);
158856e49d21SRik van Riel 
158956e49d21SRik van Riel 	return (active > inactive);
159056e49d21SRik van Riel }
159156e49d21SRik van Riel 
159256e49d21SRik van Riel /**
159356e49d21SRik van Riel  * inactive_file_is_low - check if file pages need to be deactivated
1594c56d5c7dSKonstantin Khlebnikov  * @lruvec: LRU vector to check
159556e49d21SRik van Riel  *
159656e49d21SRik van Riel  * When the system is doing streaming IO, memory pressure here
159756e49d21SRik van Riel  * ensures that active file pages get deactivated, until more
159856e49d21SRik van Riel  * than half of the file pages are on the inactive list.
159956e49d21SRik van Riel  *
160056e49d21SRik van Riel  * Once we get to that situation, protect the system's working
160156e49d21SRik van Riel  * set from being evicted by disabling active file page aging.
160256e49d21SRik van Riel  *
160356e49d21SRik van Riel  * This uses a different ratio than the anonymous pages, because
160456e49d21SRik van Riel  * the page cache uses a use-once replacement algorithm.
160556e49d21SRik van Riel  */
1606c56d5c7dSKonstantin Khlebnikov static int inactive_file_is_low(struct lruvec *lruvec)
160756e49d21SRik van Riel {
1608c3c787e8SHugh Dickins 	if (!mem_cgroup_disabled())
1609c56d5c7dSKonstantin Khlebnikov 		return mem_cgroup_inactive_file_is_low(lruvec);
161056e49d21SRik van Riel 
1611c56d5c7dSKonstantin Khlebnikov 	return inactive_file_is_low_global(lruvec_zone(lruvec));
161256e49d21SRik van Riel }
161356e49d21SRik van Riel 
161475b00af7SHugh Dickins static int inactive_list_is_low(struct lruvec *lruvec, enum lru_list lru)
1615b39415b2SRik van Riel {
161675b00af7SHugh Dickins 	if (is_file_lru(lru))
1617c56d5c7dSKonstantin Khlebnikov 		return inactive_file_is_low(lruvec);
1618b39415b2SRik van Riel 	else
1619c56d5c7dSKonstantin Khlebnikov 		return inactive_anon_is_low(lruvec);
1620b39415b2SRik van Riel }
1621b39415b2SRik van Riel 
16224f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
16231a93be0eSKonstantin Khlebnikov 				 struct lruvec *lruvec, struct scan_control *sc)
1624b69408e8SChristoph Lameter {
1625b39415b2SRik van Riel 	if (is_active_lru(lru)) {
162675b00af7SHugh Dickins 		if (inactive_list_is_low(lruvec, lru))
16271a93be0eSKonstantin Khlebnikov 			shrink_active_list(nr_to_scan, lruvec, sc, lru);
1628556adecbSRik van Riel 		return 0;
1629556adecbSRik van Riel 	}
1630556adecbSRik van Riel 
16311a93be0eSKonstantin Khlebnikov 	return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
1632b69408e8SChristoph Lameter }
1633b69408e8SChristoph Lameter 
16343d58ab5cSKonstantin Khlebnikov static int vmscan_swappiness(struct scan_control *sc)
16351f4c025bSKAMEZAWA Hiroyuki {
163689b5fae5SJohannes Weiner 	if (global_reclaim(sc))
16371f4c025bSKAMEZAWA Hiroyuki 		return vm_swappiness;
16383d58ab5cSKonstantin Khlebnikov 	return mem_cgroup_swappiness(sc->target_mem_cgroup);
16391f4c025bSKAMEZAWA Hiroyuki }
16401f4c025bSKAMEZAWA Hiroyuki 
16411da177e4SLinus Torvalds /*
16424f98a2feSRik van Riel  * Determine how aggressively the anon and file LRU lists should be
16434f98a2feSRik van Riel  * scanned.  The relative value of each set of LRU lists is determined
16444f98a2feSRik van Riel  * by looking at the fraction of the pages scanned we did rotate back
16454f98a2feSRik van Riel  * onto the active list instead of evict.
16464f98a2feSRik van Riel  *
1647be7bd59dSWanpeng Li  * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
1648be7bd59dSWanpeng Li  * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
16494f98a2feSRik van Riel  */
165090126375SKonstantin Khlebnikov static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
16519e3b2f8cSKonstantin Khlebnikov 			   unsigned long *nr)
16524f98a2feSRik van Riel {
16534f98a2feSRik van Riel 	unsigned long anon, file, free;
16544f98a2feSRik van Riel 	unsigned long anon_prio, file_prio;
16554f98a2feSRik van Riel 	unsigned long ap, fp;
165690126375SKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
165776a33fc3SShaohua Li 	u64 fraction[2], denominator;
16584111304dSHugh Dickins 	enum lru_list lru;
165976a33fc3SShaohua Li 	int noswap = 0;
1660a4d3e9e7SJohannes Weiner 	bool force_scan = false;
166190126375SKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
1662246e87a9SKAMEZAWA Hiroyuki 
1663f11c0ca5SJohannes Weiner 	/*
1664f11c0ca5SJohannes Weiner 	 * If the zone or memcg is small, nr[l] can be 0.  This
1665f11c0ca5SJohannes Weiner 	 * results in no scanning on this priority and a potential
1666f11c0ca5SJohannes Weiner 	 * priority drop.  Global direct reclaim can go to the next
1667f11c0ca5SJohannes Weiner 	 * zone and tends to have no problems. Global kswapd is for
1668f11c0ca5SJohannes Weiner 	 * zone balancing and it needs to scan a minimum amount. When
1669f11c0ca5SJohannes Weiner 	 * reclaiming for a memcg, a priority drop can cause high
1670f11c0ca5SJohannes Weiner 	 * latencies, so it's better to scan a minimum amount there as
1671f11c0ca5SJohannes Weiner 	 * well.
1672f11c0ca5SJohannes Weiner 	 */
167390126375SKonstantin Khlebnikov 	if (current_is_kswapd() && zone->all_unreclaimable)
1674a4d3e9e7SJohannes Weiner 		force_scan = true;
167589b5fae5SJohannes Weiner 	if (!global_reclaim(sc))
1676a4d3e9e7SJohannes Weiner 		force_scan = true;
167776a33fc3SShaohua Li 
167876a33fc3SShaohua Li 	/* If we have no swap space, do not bother scanning anon pages. */
167976a33fc3SShaohua Li 	if (!sc->may_swap || (nr_swap_pages <= 0)) {
168076a33fc3SShaohua Li 		noswap = 1;
168176a33fc3SShaohua Li 		fraction[0] = 0;
168276a33fc3SShaohua Li 		fraction[1] = 1;
168376a33fc3SShaohua Li 		denominator = 1;
168476a33fc3SShaohua Li 		goto out;
168576a33fc3SShaohua Li 	}
16864f98a2feSRik van Riel 
16874d7dcca2SHugh Dickins 	anon  = get_lru_size(lruvec, LRU_ACTIVE_ANON) +
16884d7dcca2SHugh Dickins 		get_lru_size(lruvec, LRU_INACTIVE_ANON);
16894d7dcca2SHugh Dickins 	file  = get_lru_size(lruvec, LRU_ACTIVE_FILE) +
16904d7dcca2SHugh Dickins 		get_lru_size(lruvec, LRU_INACTIVE_FILE);
1691a4d3e9e7SJohannes Weiner 
169289b5fae5SJohannes Weiner 	if (global_reclaim(sc)) {
169390126375SKonstantin Khlebnikov 		free  = zone_page_state(zone, NR_FREE_PAGES);
169490126375SKonstantin Khlebnikov 		if (unlikely(file + free <= high_wmark_pages(zone))) {
1695e9868505SRik van Riel 			/*
1696e9868505SRik van Riel 			 * If we have very few page cache pages, force-scan
1697e9868505SRik van Riel 			 * anon pages.
1698e9868505SRik van Riel 			 */
169976a33fc3SShaohua Li 			fraction[0] = 1;
170076a33fc3SShaohua Li 			fraction[1] = 0;
170176a33fc3SShaohua Li 			denominator = 1;
170276a33fc3SShaohua Li 			goto out;
1703e9868505SRik van Riel 		} else if (!inactive_file_is_low_global(zone)) {
1704e9868505SRik van Riel 			/*
1705e9868505SRik van Riel 			 * There is enough inactive page cache, do not
1706e9868505SRik van Riel 			 * reclaim anything from the working set right now.
1707e9868505SRik van Riel 			 */
1708e9868505SRik van Riel 			fraction[0] = 0;
1709e9868505SRik van Riel 			fraction[1] = 1;
1710e9868505SRik van Riel 			denominator = 1;
1711e9868505SRik van Riel 			goto out;
17124f98a2feSRik van Riel 		}
1713eeee9a8cSKOSAKI Motohiro 	}
17144f98a2feSRik van Riel 
17154f98a2feSRik van Riel 	/*
171658c37f6eSKOSAKI Motohiro 	 * With swappiness at 100, anonymous and file have the same priority.
171758c37f6eSKOSAKI Motohiro 	 * This scanning priority is essentially the inverse of IO cost.
171858c37f6eSKOSAKI Motohiro 	 */
17193d58ab5cSKonstantin Khlebnikov 	anon_prio = vmscan_swappiness(sc);
172075b00af7SHugh Dickins 	file_prio = 200 - anon_prio;
172158c37f6eSKOSAKI Motohiro 
172258c37f6eSKOSAKI Motohiro 	/*
17234f98a2feSRik van Riel 	 * OK, so we have swap space and a fair amount of page cache
17244f98a2feSRik van Riel 	 * pages.  We use the recently rotated / recently scanned
17254f98a2feSRik van Riel 	 * ratios to determine how valuable each cache is.
17264f98a2feSRik van Riel 	 *
17274f98a2feSRik van Riel 	 * Because workloads change over time (and to avoid overflow)
17284f98a2feSRik van Riel 	 * we keep these statistics as a floating average, which ends
17294f98a2feSRik van Riel 	 * up weighing recent references more than old ones.
17304f98a2feSRik van Riel 	 *
17314f98a2feSRik van Riel 	 * anon in [0], file in [1]
17324f98a2feSRik van Riel 	 */
173390126375SKonstantin Khlebnikov 	spin_lock_irq(&zone->lru_lock);
173458c37f6eSKOSAKI Motohiro 	if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
17356e901571SKOSAKI Motohiro 		reclaim_stat->recent_scanned[0] /= 2;
17366e901571SKOSAKI Motohiro 		reclaim_stat->recent_rotated[0] /= 2;
17374f98a2feSRik van Riel 	}
17384f98a2feSRik van Riel 
17396e901571SKOSAKI Motohiro 	if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
17406e901571SKOSAKI Motohiro 		reclaim_stat->recent_scanned[1] /= 2;
17416e901571SKOSAKI Motohiro 		reclaim_stat->recent_rotated[1] /= 2;
17424f98a2feSRik van Riel 	}
17434f98a2feSRik van Riel 
17444f98a2feSRik van Riel 	/*
174500d8089cSRik van Riel 	 * The amount of pressure on anon vs file pages is inversely
174600d8089cSRik van Riel 	 * proportional to the fraction of recently scanned pages on
174700d8089cSRik van Riel 	 * each list that were recently referenced and in active use.
17484f98a2feSRik van Riel 	 */
1749fe35004fSSatoru Moriya 	ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1);
17506e901571SKOSAKI Motohiro 	ap /= reclaim_stat->recent_rotated[0] + 1;
17514f98a2feSRik van Riel 
1752fe35004fSSatoru Moriya 	fp = file_prio * (reclaim_stat->recent_scanned[1] + 1);
17536e901571SKOSAKI Motohiro 	fp /= reclaim_stat->recent_rotated[1] + 1;
175490126375SKonstantin Khlebnikov 	spin_unlock_irq(&zone->lru_lock);
17554f98a2feSRik van Riel 
175676a33fc3SShaohua Li 	fraction[0] = ap;
175776a33fc3SShaohua Li 	fraction[1] = fp;
175876a33fc3SShaohua Li 	denominator = ap + fp + 1;
175976a33fc3SShaohua Li out:
17604111304dSHugh Dickins 	for_each_evictable_lru(lru) {
17614111304dSHugh Dickins 		int file = is_file_lru(lru);
176276a33fc3SShaohua Li 		unsigned long scan;
176376a33fc3SShaohua Li 
17644d7dcca2SHugh Dickins 		scan = get_lru_size(lruvec, lru);
17659e3b2f8cSKonstantin Khlebnikov 		if (sc->priority || noswap || !vmscan_swappiness(sc)) {
17669e3b2f8cSKonstantin Khlebnikov 			scan >>= sc->priority;
1767f11c0ca5SJohannes Weiner 			if (!scan && force_scan)
1768f11c0ca5SJohannes Weiner 				scan = SWAP_CLUSTER_MAX;
176976a33fc3SShaohua Li 			scan = div64_u64(scan * fraction[file], denominator);
17704f98a2feSRik van Riel 		}
17714111304dSHugh Dickins 		nr[lru] = scan;
177276a33fc3SShaohua Li 	}
17736e08a369SWu Fengguang }
17744f98a2feSRik van Riel 
177523b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */
17769e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc)
177723b9da55SMel Gorman {
1778d84da3f9SKirill A. Shutemov 	if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
177923b9da55SMel Gorman 			(sc->order > PAGE_ALLOC_COSTLY_ORDER ||
17809e3b2f8cSKonstantin Khlebnikov 			 sc->priority < DEF_PRIORITY - 2))
178123b9da55SMel Gorman 		return true;
178223b9da55SMel Gorman 
178323b9da55SMel Gorman 	return false;
178423b9da55SMel Gorman }
178523b9da55SMel Gorman 
17864f98a2feSRik van Riel /*
178723b9da55SMel Gorman  * Reclaim/compaction is used for high-order allocation requests. It reclaims
178823b9da55SMel Gorman  * order-0 pages before compacting the zone. should_continue_reclaim() returns
178923b9da55SMel Gorman  * true if more pages should be reclaimed such that when the page allocator
179023b9da55SMel Gorman  * calls try_to_compact_zone() that it will have enough free pages to succeed.
179123b9da55SMel Gorman  * It will give up earlier than that if there is difficulty reclaiming pages.
17923e7d3449SMel Gorman  */
179390bdcfafSKonstantin Khlebnikov static inline bool should_continue_reclaim(struct lruvec *lruvec,
17943e7d3449SMel Gorman 					unsigned long nr_reclaimed,
17953e7d3449SMel Gorman 					unsigned long nr_scanned,
17963e7d3449SMel Gorman 					struct scan_control *sc)
17973e7d3449SMel Gorman {
17983e7d3449SMel Gorman 	unsigned long pages_for_compaction;
17993e7d3449SMel Gorman 	unsigned long inactive_lru_pages;
18003e7d3449SMel Gorman 
18013e7d3449SMel Gorman 	/* If not in reclaim/compaction mode, stop */
18029e3b2f8cSKonstantin Khlebnikov 	if (!in_reclaim_compaction(sc))
18033e7d3449SMel Gorman 		return false;
18043e7d3449SMel Gorman 
18052876592fSMel Gorman 	/* Consider stopping depending on scan and reclaim activity */
18062876592fSMel Gorman 	if (sc->gfp_mask & __GFP_REPEAT) {
18073e7d3449SMel Gorman 		/*
18082876592fSMel Gorman 		 * For __GFP_REPEAT allocations, stop reclaiming if the
18092876592fSMel Gorman 		 * full LRU list has been scanned and we are still failing
18102876592fSMel Gorman 		 * to reclaim pages. This full LRU scan is potentially
18112876592fSMel Gorman 		 * expensive but a __GFP_REPEAT caller really wants to succeed
18123e7d3449SMel Gorman 		 */
18133e7d3449SMel Gorman 		if (!nr_reclaimed && !nr_scanned)
18143e7d3449SMel Gorman 			return false;
18152876592fSMel Gorman 	} else {
18162876592fSMel Gorman 		/*
18172876592fSMel Gorman 		 * For non-__GFP_REPEAT allocations which can presumably
18182876592fSMel Gorman 		 * fail without consequence, stop if we failed to reclaim
18192876592fSMel Gorman 		 * any pages from the last SWAP_CLUSTER_MAX number of
18202876592fSMel Gorman 		 * pages that were scanned. This will return to the
18212876592fSMel Gorman 		 * caller faster at the risk reclaim/compaction and
18222876592fSMel Gorman 		 * the resulting allocation attempt fails
18232876592fSMel Gorman 		 */
18242876592fSMel Gorman 		if (!nr_reclaimed)
18252876592fSMel Gorman 			return false;
18262876592fSMel Gorman 	}
18273e7d3449SMel Gorman 
18283e7d3449SMel Gorman 	/*
18293e7d3449SMel Gorman 	 * If we have not reclaimed enough pages for compaction and the
18303e7d3449SMel Gorman 	 * inactive lists are large enough, continue reclaiming
18313e7d3449SMel Gorman 	 */
18323e7d3449SMel Gorman 	pages_for_compaction = (2UL << sc->order);
18334d7dcca2SHugh Dickins 	inactive_lru_pages = get_lru_size(lruvec, LRU_INACTIVE_FILE);
183486cfd3a4SMinchan Kim 	if (nr_swap_pages > 0)
18354d7dcca2SHugh Dickins 		inactive_lru_pages += get_lru_size(lruvec, LRU_INACTIVE_ANON);
18363e7d3449SMel Gorman 	if (sc->nr_reclaimed < pages_for_compaction &&
18373e7d3449SMel Gorman 			inactive_lru_pages > pages_for_compaction)
18383e7d3449SMel Gorman 		return true;
18393e7d3449SMel Gorman 
18403e7d3449SMel Gorman 	/* If compaction would go ahead or the allocation would succeed, stop */
184190bdcfafSKonstantin Khlebnikov 	switch (compaction_suitable(lruvec_zone(lruvec), sc->order)) {
18423e7d3449SMel Gorman 	case COMPACT_PARTIAL:
18433e7d3449SMel Gorman 	case COMPACT_CONTINUE:
18443e7d3449SMel Gorman 		return false;
18453e7d3449SMel Gorman 	default:
18463e7d3449SMel Gorman 		return true;
18473e7d3449SMel Gorman 	}
18483e7d3449SMel Gorman }
18493e7d3449SMel Gorman 
18503e7d3449SMel Gorman /*
18511da177e4SLinus Torvalds  * This is a basic per-zone page freer.  Used by both kswapd and direct reclaim.
18521da177e4SLinus Torvalds  */
1853f9be23d6SKonstantin Khlebnikov static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
18541da177e4SLinus Torvalds {
1855b69408e8SChristoph Lameter 	unsigned long nr[NR_LRU_LISTS];
18568695949aSChristoph Lameter 	unsigned long nr_to_scan;
18574111304dSHugh Dickins 	enum lru_list lru;
1858f0fdc5e8SJohannes Weiner 	unsigned long nr_reclaimed, nr_scanned;
185922fba335SKOSAKI Motohiro 	unsigned long nr_to_reclaim = sc->nr_to_reclaim;
18603da367c3SShaohua Li 	struct blk_plug plug;
18611da177e4SLinus Torvalds 
18623e7d3449SMel Gorman restart:
18633e7d3449SMel Gorman 	nr_reclaimed = 0;
1864f0fdc5e8SJohannes Weiner 	nr_scanned = sc->nr_scanned;
186590126375SKonstantin Khlebnikov 	get_scan_count(lruvec, sc, nr);
18661cfb419bSKAMEZAWA Hiroyuki 
18673da367c3SShaohua Li 	blk_start_plug(&plug);
1868556adecbSRik van Riel 	while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
1869556adecbSRik van Riel 					nr[LRU_INACTIVE_FILE]) {
18704111304dSHugh Dickins 		for_each_evictable_lru(lru) {
18714111304dSHugh Dickins 			if (nr[lru]) {
1872ece74b2eSKOSAKI Motohiro 				nr_to_scan = min_t(unsigned long,
18734111304dSHugh Dickins 						   nr[lru], SWAP_CLUSTER_MAX);
18744111304dSHugh Dickins 				nr[lru] -= nr_to_scan;
1875b69408e8SChristoph Lameter 
18764111304dSHugh Dickins 				nr_reclaimed += shrink_list(lru, nr_to_scan,
18771a93be0eSKonstantin Khlebnikov 							    lruvec, sc);
18781da177e4SLinus Torvalds 			}
18791da177e4SLinus Torvalds 		}
1880a79311c1SRik van Riel 		/*
1881a79311c1SRik van Riel 		 * On large memory systems, scan >> priority can become
1882a79311c1SRik van Riel 		 * really large. This is fine for the starting priority;
1883a79311c1SRik van Riel 		 * we want to put equal scanning pressure on each zone.
1884a79311c1SRik van Riel 		 * However, if the VM has a harder time of freeing pages,
1885a79311c1SRik van Riel 		 * with multiple processes reclaiming pages, the total
1886a79311c1SRik van Riel 		 * freeing target can get unreasonably large.
1887a79311c1SRik van Riel 		 */
18889e3b2f8cSKonstantin Khlebnikov 		if (nr_reclaimed >= nr_to_reclaim &&
18899e3b2f8cSKonstantin Khlebnikov 		    sc->priority < DEF_PRIORITY)
1890a79311c1SRik van Riel 			break;
18911da177e4SLinus Torvalds 	}
18923da367c3SShaohua Li 	blk_finish_plug(&plug);
18933e7d3449SMel Gorman 	sc->nr_reclaimed += nr_reclaimed;
189401dbe5c9SKOSAKI Motohiro 
1895556adecbSRik van Riel 	/*
1896556adecbSRik van Riel 	 * Even if we did not try to evict anon pages at all, we want to
1897556adecbSRik van Riel 	 * rebalance the anon lru active/inactive ratio.
1898556adecbSRik van Riel 	 */
1899c56d5c7dSKonstantin Khlebnikov 	if (inactive_anon_is_low(lruvec))
19001a93be0eSKonstantin Khlebnikov 		shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
19019e3b2f8cSKonstantin Khlebnikov 				   sc, LRU_ACTIVE_ANON);
1902556adecbSRik van Riel 
19033e7d3449SMel Gorman 	/* reclaim/compaction might need reclaim to continue */
190490bdcfafSKonstantin Khlebnikov 	if (should_continue_reclaim(lruvec, nr_reclaimed,
19059e3b2f8cSKonstantin Khlebnikov 				    sc->nr_scanned - nr_scanned, sc))
19063e7d3449SMel Gorman 		goto restart;
19073e7d3449SMel Gorman 
1908232ea4d6SAndrew Morton 	throttle_vm_writeout(sc->gfp_mask);
19091da177e4SLinus Torvalds }
19101da177e4SLinus Torvalds 
19119e3b2f8cSKonstantin Khlebnikov static void shrink_zone(struct zone *zone, struct scan_control *sc)
1912f16015fbSJohannes Weiner {
19135660048cSJohannes Weiner 	struct mem_cgroup *root = sc->target_mem_cgroup;
19145660048cSJohannes Weiner 	struct mem_cgroup_reclaim_cookie reclaim = {
19155660048cSJohannes Weiner 		.zone = zone,
19169e3b2f8cSKonstantin Khlebnikov 		.priority = sc->priority,
19175660048cSJohannes Weiner 	};
19185660048cSJohannes Weiner 	struct mem_cgroup *memcg;
19195660048cSJohannes Weiner 
19205660048cSJohannes Weiner 	memcg = mem_cgroup_iter(root, NULL, &reclaim);
19215660048cSJohannes Weiner 	do {
1922f9be23d6SKonstantin Khlebnikov 		struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
19235660048cSJohannes Weiner 
1924f9be23d6SKonstantin Khlebnikov 		shrink_lruvec(lruvec, sc);
1925f9be23d6SKonstantin Khlebnikov 
19265660048cSJohannes Weiner 		/*
19275660048cSJohannes Weiner 		 * Limit reclaim has historically picked one memcg and
19285660048cSJohannes Weiner 		 * scanned it with decreasing priority levels until
19295660048cSJohannes Weiner 		 * nr_to_reclaim had been reclaimed.  This priority
19305660048cSJohannes Weiner 		 * cycle is thus over after a single memcg.
1931b95a2f2dSJohannes Weiner 		 *
1932b95a2f2dSJohannes Weiner 		 * Direct reclaim and kswapd, on the other hand, have
1933b95a2f2dSJohannes Weiner 		 * to scan all memory cgroups to fulfill the overall
1934b95a2f2dSJohannes Weiner 		 * scan target for the zone.
19355660048cSJohannes Weiner 		 */
19365660048cSJohannes Weiner 		if (!global_reclaim(sc)) {
19375660048cSJohannes Weiner 			mem_cgroup_iter_break(root, memcg);
19385660048cSJohannes Weiner 			break;
19395660048cSJohannes Weiner 		}
19405660048cSJohannes Weiner 		memcg = mem_cgroup_iter(root, memcg, &reclaim);
19415660048cSJohannes Weiner 	} while (memcg);
1942f16015fbSJohannes Weiner }
1943f16015fbSJohannes Weiner 
1944fe4b1b24SMel Gorman /* Returns true if compaction should go ahead for a high-order request */
1945fe4b1b24SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
1946fe4b1b24SMel Gorman {
1947fe4b1b24SMel Gorman 	unsigned long balance_gap, watermark;
1948fe4b1b24SMel Gorman 	bool watermark_ok;
1949fe4b1b24SMel Gorman 
1950fe4b1b24SMel Gorman 	/* Do not consider compaction for orders reclaim is meant to satisfy */
1951fe4b1b24SMel Gorman 	if (sc->order <= PAGE_ALLOC_COSTLY_ORDER)
1952fe4b1b24SMel Gorman 		return false;
1953fe4b1b24SMel Gorman 
1954fe4b1b24SMel Gorman 	/*
1955fe4b1b24SMel Gorman 	 * Compaction takes time to run and there are potentially other
1956fe4b1b24SMel Gorman 	 * callers using the pages just freed. Continue reclaiming until
1957fe4b1b24SMel Gorman 	 * there is a buffer of free pages available to give compaction
1958fe4b1b24SMel Gorman 	 * a reasonable chance of completing and allocating the page
1959fe4b1b24SMel Gorman 	 */
1960fe4b1b24SMel Gorman 	balance_gap = min(low_wmark_pages(zone),
1961fe4b1b24SMel Gorman 		(zone->present_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
1962fe4b1b24SMel Gorman 			KSWAPD_ZONE_BALANCE_GAP_RATIO);
1963fe4b1b24SMel Gorman 	watermark = high_wmark_pages(zone) + balance_gap + (2UL << sc->order);
1964fe4b1b24SMel Gorman 	watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0);
1965fe4b1b24SMel Gorman 
1966fe4b1b24SMel Gorman 	/*
1967fe4b1b24SMel Gorman 	 * If compaction is deferred, reclaim up to a point where
1968fe4b1b24SMel Gorman 	 * compaction will have a chance of success when re-enabled
1969fe4b1b24SMel Gorman 	 */
1970aff62249SRik van Riel 	if (compaction_deferred(zone, sc->order))
1971fe4b1b24SMel Gorman 		return watermark_ok;
1972fe4b1b24SMel Gorman 
1973fe4b1b24SMel Gorman 	/* If compaction is not ready to start, keep reclaiming */
1974fe4b1b24SMel Gorman 	if (!compaction_suitable(zone, sc->order))
1975fe4b1b24SMel Gorman 		return false;
1976fe4b1b24SMel Gorman 
1977fe4b1b24SMel Gorman 	return watermark_ok;
1978fe4b1b24SMel Gorman }
1979fe4b1b24SMel Gorman 
19801da177e4SLinus Torvalds /*
19811da177e4SLinus Torvalds  * This is the direct reclaim path, for page-allocating processes.  We only
19821da177e4SLinus Torvalds  * try to reclaim pages from zones which will satisfy the caller's allocation
19831da177e4SLinus Torvalds  * request.
19841da177e4SLinus Torvalds  *
198541858966SMel Gorman  * We reclaim from a zone even if that zone is over high_wmark_pages(zone).
198641858966SMel Gorman  * Because:
19871da177e4SLinus Torvalds  * a) The caller may be trying to free *extra* pages to satisfy a higher-order
19881da177e4SLinus Torvalds  *    allocation or
198941858966SMel Gorman  * b) The target zone may be at high_wmark_pages(zone) but the lower zones
199041858966SMel Gorman  *    must go *over* high_wmark_pages(zone) to satisfy the `incremental min'
199141858966SMel Gorman  *    zone defense algorithm.
19921da177e4SLinus Torvalds  *
19931da177e4SLinus Torvalds  * If a zone is deemed to be full of pinned pages then just give it a light
19941da177e4SLinus Torvalds  * scan then give up on it.
1995e0c23279SMel Gorman  *
1996e0c23279SMel Gorman  * This function returns true if a zone is being reclaimed for a costly
1997fe4b1b24SMel Gorman  * high-order allocation and compaction is ready to begin. This indicates to
19980cee34fdSMel Gorman  * the caller that it should consider retrying the allocation instead of
19990cee34fdSMel Gorman  * further reclaim.
20001da177e4SLinus Torvalds  */
20019e3b2f8cSKonstantin Khlebnikov static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
20021da177e4SLinus Torvalds {
2003dd1a239fSMel Gorman 	struct zoneref *z;
200454a6eb5cSMel Gorman 	struct zone *zone;
2005d149e3b2SYing Han 	unsigned long nr_soft_reclaimed;
2006d149e3b2SYing Han 	unsigned long nr_soft_scanned;
20070cee34fdSMel Gorman 	bool aborted_reclaim = false;
20081cfb419bSKAMEZAWA Hiroyuki 
2009cc715d99SMel Gorman 	/*
2010cc715d99SMel Gorman 	 * If the number of buffer_heads in the machine exceeds the maximum
2011cc715d99SMel Gorman 	 * allowed level, force direct reclaim to scan the highmem zone as
2012cc715d99SMel Gorman 	 * highmem pages could be pinning lowmem pages storing buffer_heads
2013cc715d99SMel Gorman 	 */
2014cc715d99SMel Gorman 	if (buffer_heads_over_limit)
2015cc715d99SMel Gorman 		sc->gfp_mask |= __GFP_HIGHMEM;
2016cc715d99SMel Gorman 
2017d4debc66SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
2018d4debc66SMel Gorman 					gfp_zone(sc->gfp_mask), sc->nodemask) {
2019f3fe6512SCon Kolivas 		if (!populated_zone(zone))
20201da177e4SLinus Torvalds 			continue;
20211cfb419bSKAMEZAWA Hiroyuki 		/*
20221cfb419bSKAMEZAWA Hiroyuki 		 * Take care memory controller reclaiming has small influence
20231cfb419bSKAMEZAWA Hiroyuki 		 * to global LRU.
20241cfb419bSKAMEZAWA Hiroyuki 		 */
202589b5fae5SJohannes Weiner 		if (global_reclaim(sc)) {
202602a0e53dSPaul Jackson 			if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
20271da177e4SLinus Torvalds 				continue;
20289e3b2f8cSKonstantin Khlebnikov 			if (zone->all_unreclaimable &&
20299e3b2f8cSKonstantin Khlebnikov 					sc->priority != DEF_PRIORITY)
20301da177e4SLinus Torvalds 				continue;	/* Let kswapd poll it */
2031d84da3f9SKirill A. Shutemov 			if (IS_ENABLED(CONFIG_COMPACTION)) {
2032e0887c19SRik van Riel 				/*
2033e0c23279SMel Gorman 				 * If we already have plenty of memory free for
2034e0c23279SMel Gorman 				 * compaction in this zone, don't free any more.
2035e0c23279SMel Gorman 				 * Even though compaction is invoked for any
2036e0c23279SMel Gorman 				 * non-zero order, only frequent costly order
2037e0c23279SMel Gorman 				 * reclamation is disruptive enough to become a
2038c7cfa37bSCopot Alexandru 				 * noticeable problem, like transparent huge
2039c7cfa37bSCopot Alexandru 				 * page allocations.
2040e0887c19SRik van Riel 				 */
2041fe4b1b24SMel Gorman 				if (compaction_ready(zone, sc)) {
20420cee34fdSMel Gorman 					aborted_reclaim = true;
2043e0887c19SRik van Riel 					continue;
2044e0887c19SRik van Riel 				}
2045e0c23279SMel Gorman 			}
2046ac34a1a3SKAMEZAWA Hiroyuki 			/*
2047ac34a1a3SKAMEZAWA Hiroyuki 			 * This steals pages from memory cgroups over softlimit
2048ac34a1a3SKAMEZAWA Hiroyuki 			 * and returns the number of reclaimed pages and
2049ac34a1a3SKAMEZAWA Hiroyuki 			 * scanned pages. This works for global memory pressure
2050ac34a1a3SKAMEZAWA Hiroyuki 			 * and balancing, not for a memcg's limit.
2051ac34a1a3SKAMEZAWA Hiroyuki 			 */
2052d149e3b2SYing Han 			nr_soft_scanned = 0;
2053d149e3b2SYing Han 			nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
2054d149e3b2SYing Han 						sc->order, sc->gfp_mask,
2055d149e3b2SYing Han 						&nr_soft_scanned);
2056d149e3b2SYing Han 			sc->nr_reclaimed += nr_soft_reclaimed;
2057ac34a1a3SKAMEZAWA Hiroyuki 			sc->nr_scanned += nr_soft_scanned;
2058ac34a1a3SKAMEZAWA Hiroyuki 			/* need some check for avoid more shrink_zone() */
2059ac34a1a3SKAMEZAWA Hiroyuki 		}
2060d149e3b2SYing Han 
20619e3b2f8cSKonstantin Khlebnikov 		shrink_zone(zone, sc);
20621da177e4SLinus Torvalds 	}
2063e0c23279SMel Gorman 
20640cee34fdSMel Gorman 	return aborted_reclaim;
2065d1908362SMinchan Kim }
2066d1908362SMinchan Kim 
2067d1908362SMinchan Kim static bool zone_reclaimable(struct zone *zone)
2068d1908362SMinchan Kim {
2069d1908362SMinchan Kim 	return zone->pages_scanned < zone_reclaimable_pages(zone) * 6;
2070d1908362SMinchan Kim }
2071d1908362SMinchan Kim 
2072929bea7cSKOSAKI Motohiro /* All zones in zonelist are unreclaimable? */
2073d1908362SMinchan Kim static bool all_unreclaimable(struct zonelist *zonelist,
2074d1908362SMinchan Kim 		struct scan_control *sc)
2075d1908362SMinchan Kim {
2076d1908362SMinchan Kim 	struct zoneref *z;
2077d1908362SMinchan Kim 	struct zone *zone;
2078d1908362SMinchan Kim 
2079d1908362SMinchan Kim 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
2080d1908362SMinchan Kim 			gfp_zone(sc->gfp_mask), sc->nodemask) {
2081d1908362SMinchan Kim 		if (!populated_zone(zone))
2082d1908362SMinchan Kim 			continue;
2083d1908362SMinchan Kim 		if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
2084d1908362SMinchan Kim 			continue;
2085929bea7cSKOSAKI Motohiro 		if (!zone->all_unreclaimable)
2086929bea7cSKOSAKI Motohiro 			return false;
2087d1908362SMinchan Kim 	}
2088d1908362SMinchan Kim 
2089929bea7cSKOSAKI Motohiro 	return true;
20901da177e4SLinus Torvalds }
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds /*
20931da177e4SLinus Torvalds  * This is the main entry point to direct page reclaim.
20941da177e4SLinus Torvalds  *
20951da177e4SLinus Torvalds  * If a full scan of the inactive list fails to free enough memory then we
20961da177e4SLinus Torvalds  * are "out of memory" and something needs to be killed.
20971da177e4SLinus Torvalds  *
20981da177e4SLinus Torvalds  * If the caller is !__GFP_FS then the probability of a failure is reasonably
20991da177e4SLinus Torvalds  * high - the zone may be full of dirty or under-writeback pages, which this
21005b0830cbSJens Axboe  * caller can't do much about.  We kick the writeback threads and take explicit
21015b0830cbSJens Axboe  * naps in the hope that some of these pages can be written.  But if the
21025b0830cbSJens Axboe  * allocating task holds filesystem locks which prevent writeout this might not
21035b0830cbSJens Axboe  * work, and the allocation attempt will fail.
2104a41f24eaSNishanth Aravamudan  *
2105a41f24eaSNishanth Aravamudan  * returns:	0, if no pages reclaimed
2106a41f24eaSNishanth Aravamudan  * 		else, the number of pages reclaimed
21071da177e4SLinus Torvalds  */
2108dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
2109a09ed5e0SYing Han 					struct scan_control *sc,
2110a09ed5e0SYing Han 					struct shrink_control *shrink)
21111da177e4SLinus Torvalds {
211269e05944SAndrew Morton 	unsigned long total_scanned = 0;
21131da177e4SLinus Torvalds 	struct reclaim_state *reclaim_state = current->reclaim_state;
2114dd1a239fSMel Gorman 	struct zoneref *z;
211554a6eb5cSMel Gorman 	struct zone *zone;
211622fba335SKOSAKI Motohiro 	unsigned long writeback_threshold;
21170cee34fdSMel Gorman 	bool aborted_reclaim;
21181da177e4SLinus Torvalds 
2119873b4771SKeika Kobayashi 	delayacct_freepages_start();
2120873b4771SKeika Kobayashi 
212189b5fae5SJohannes Weiner 	if (global_reclaim(sc))
2122f8891e5eSChristoph Lameter 		count_vm_event(ALLOCSTALL);
21231da177e4SLinus Torvalds 
21249e3b2f8cSKonstantin Khlebnikov 	do {
212566e1707bSBalbir Singh 		sc->nr_scanned = 0;
21269e3b2f8cSKonstantin Khlebnikov 		aborted_reclaim = shrink_zones(zonelist, sc);
2127e0c23279SMel Gorman 
212866e1707bSBalbir Singh 		/*
212966e1707bSBalbir Singh 		 * Don't shrink slabs when reclaiming memory from
213066e1707bSBalbir Singh 		 * over limit cgroups
213166e1707bSBalbir Singh 		 */
213289b5fae5SJohannes Weiner 		if (global_reclaim(sc)) {
2133c6a8a8c5SKOSAKI Motohiro 			unsigned long lru_pages = 0;
2134d4debc66SMel Gorman 			for_each_zone_zonelist(zone, z, zonelist,
2135d4debc66SMel Gorman 					gfp_zone(sc->gfp_mask)) {
2136c6a8a8c5SKOSAKI Motohiro 				if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
2137c6a8a8c5SKOSAKI Motohiro 					continue;
2138c6a8a8c5SKOSAKI Motohiro 
2139c6a8a8c5SKOSAKI Motohiro 				lru_pages += zone_reclaimable_pages(zone);
2140c6a8a8c5SKOSAKI Motohiro 			}
2141c6a8a8c5SKOSAKI Motohiro 
21421495f230SYing Han 			shrink_slab(shrink, sc->nr_scanned, lru_pages);
21431da177e4SLinus Torvalds 			if (reclaim_state) {
2144a79311c1SRik van Riel 				sc->nr_reclaimed += reclaim_state->reclaimed_slab;
21451da177e4SLinus Torvalds 				reclaim_state->reclaimed_slab = 0;
21461da177e4SLinus Torvalds 			}
214791a45470SKAMEZAWA Hiroyuki 		}
214866e1707bSBalbir Singh 		total_scanned += sc->nr_scanned;
2149bb21c7ceSKOSAKI Motohiro 		if (sc->nr_reclaimed >= sc->nr_to_reclaim)
21501da177e4SLinus Torvalds 			goto out;
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds 		/*
21531da177e4SLinus Torvalds 		 * Try to write back as many pages as we just scanned.  This
21541da177e4SLinus Torvalds 		 * tends to cause slow streaming writers to write data to the
21551da177e4SLinus Torvalds 		 * disk smoothly, at the dirtying rate, which is nice.   But
21561da177e4SLinus Torvalds 		 * that's undesirable in laptop mode, where we *want* lumpy
21571da177e4SLinus Torvalds 		 * writeout.  So in laptop mode, write out the whole world.
21581da177e4SLinus Torvalds 		 */
215922fba335SKOSAKI Motohiro 		writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
216022fba335SKOSAKI Motohiro 		if (total_scanned > writeback_threshold) {
21610e175a18SCurt Wohlgemuth 			wakeup_flusher_threads(laptop_mode ? 0 : total_scanned,
21620e175a18SCurt Wohlgemuth 						WB_REASON_TRY_TO_FREE_PAGES);
216366e1707bSBalbir Singh 			sc->may_writepage = 1;
21641da177e4SLinus Torvalds 		}
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds 		/* Take a nap, wait for some writeback to complete */
21677b51755cSKOSAKI Motohiro 		if (!sc->hibernation_mode && sc->nr_scanned &&
21689e3b2f8cSKonstantin Khlebnikov 		    sc->priority < DEF_PRIORITY - 2) {
21690e093d99SMel Gorman 			struct zone *preferred_zone;
21700e093d99SMel Gorman 
21710e093d99SMel Gorman 			first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask),
2172f33261d7SDavid Rientjes 						&cpuset_current_mems_allowed,
2173f33261d7SDavid Rientjes 						&preferred_zone);
21740e093d99SMel Gorman 			wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10);
21750e093d99SMel Gorman 		}
21769e3b2f8cSKonstantin Khlebnikov 	} while (--sc->priority >= 0);
2177bb21c7ceSKOSAKI Motohiro 
21781da177e4SLinus Torvalds out:
2179873b4771SKeika Kobayashi 	delayacct_freepages_end();
2180873b4771SKeika Kobayashi 
2181bb21c7ceSKOSAKI Motohiro 	if (sc->nr_reclaimed)
2182bb21c7ceSKOSAKI Motohiro 		return sc->nr_reclaimed;
2183bb21c7ceSKOSAKI Motohiro 
2184929bea7cSKOSAKI Motohiro 	/*
2185929bea7cSKOSAKI Motohiro 	 * As hibernation is going on, kswapd is freezed so that it can't mark
2186929bea7cSKOSAKI Motohiro 	 * the zone into all_unreclaimable. Thus bypassing all_unreclaimable
2187929bea7cSKOSAKI Motohiro 	 * check.
2188929bea7cSKOSAKI Motohiro 	 */
2189929bea7cSKOSAKI Motohiro 	if (oom_killer_disabled)
2190929bea7cSKOSAKI Motohiro 		return 0;
2191929bea7cSKOSAKI Motohiro 
21920cee34fdSMel Gorman 	/* Aborted reclaim to try compaction? don't OOM, then */
21930cee34fdSMel Gorman 	if (aborted_reclaim)
21947335084dSMel Gorman 		return 1;
21957335084dSMel Gorman 
2196bb21c7ceSKOSAKI Motohiro 	/* top priority shrink_zones still had more to do? don't OOM, then */
219789b5fae5SJohannes Weiner 	if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc))
2198bb21c7ceSKOSAKI Motohiro 		return 1;
2199bb21c7ceSKOSAKI Motohiro 
2200bb21c7ceSKOSAKI Motohiro 	return 0;
22011da177e4SLinus Torvalds }
22021da177e4SLinus Torvalds 
22035515061dSMel Gorman static bool pfmemalloc_watermark_ok(pg_data_t *pgdat)
22045515061dSMel Gorman {
22055515061dSMel Gorman 	struct zone *zone;
22065515061dSMel Gorman 	unsigned long pfmemalloc_reserve = 0;
22075515061dSMel Gorman 	unsigned long free_pages = 0;
22085515061dSMel Gorman 	int i;
22095515061dSMel Gorman 	bool wmark_ok;
22105515061dSMel Gorman 
22115515061dSMel Gorman 	for (i = 0; i <= ZONE_NORMAL; i++) {
22125515061dSMel Gorman 		zone = &pgdat->node_zones[i];
22135515061dSMel Gorman 		pfmemalloc_reserve += min_wmark_pages(zone);
22145515061dSMel Gorman 		free_pages += zone_page_state(zone, NR_FREE_PAGES);
22155515061dSMel Gorman 	}
22165515061dSMel Gorman 
22175515061dSMel Gorman 	wmark_ok = free_pages > pfmemalloc_reserve / 2;
22185515061dSMel Gorman 
22195515061dSMel Gorman 	/* kswapd must be awake if processes are being throttled */
22205515061dSMel Gorman 	if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
22215515061dSMel Gorman 		pgdat->classzone_idx = min(pgdat->classzone_idx,
22225515061dSMel Gorman 						(enum zone_type)ZONE_NORMAL);
22235515061dSMel Gorman 		wake_up_interruptible(&pgdat->kswapd_wait);
22245515061dSMel Gorman 	}
22255515061dSMel Gorman 
22265515061dSMel Gorman 	return wmark_ok;
22275515061dSMel Gorman }
22285515061dSMel Gorman 
22295515061dSMel Gorman /*
22305515061dSMel Gorman  * Throttle direct reclaimers if backing storage is backed by the network
22315515061dSMel Gorman  * and the PFMEMALLOC reserve for the preferred node is getting dangerously
22325515061dSMel Gorman  * depleted. kswapd will continue to make progress and wake the processes
223350694c28SMel Gorman  * when the low watermark is reached.
223450694c28SMel Gorman  *
223550694c28SMel Gorman  * Returns true if a fatal signal was delivered during throttling. If this
223650694c28SMel Gorman  * happens, the page allocator should not consider triggering the OOM killer.
22375515061dSMel Gorman  */
223850694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
22395515061dSMel Gorman 					nodemask_t *nodemask)
22405515061dSMel Gorman {
22415515061dSMel Gorman 	struct zone *zone;
22425515061dSMel Gorman 	int high_zoneidx = gfp_zone(gfp_mask);
22435515061dSMel Gorman 	pg_data_t *pgdat;
22445515061dSMel Gorman 
22455515061dSMel Gorman 	/*
22465515061dSMel Gorman 	 * Kernel threads should not be throttled as they may be indirectly
22475515061dSMel Gorman 	 * responsible for cleaning pages necessary for reclaim to make forward
22485515061dSMel Gorman 	 * progress. kjournald for example may enter direct reclaim while
22495515061dSMel Gorman 	 * committing a transaction where throttling it could forcing other
22505515061dSMel Gorman 	 * processes to block on log_wait_commit().
22515515061dSMel Gorman 	 */
22525515061dSMel Gorman 	if (current->flags & PF_KTHREAD)
225350694c28SMel Gorman 		goto out;
225450694c28SMel Gorman 
225550694c28SMel Gorman 	/*
225650694c28SMel Gorman 	 * If a fatal signal is pending, this process should not throttle.
225750694c28SMel Gorman 	 * It should return quickly so it can exit and free its memory
225850694c28SMel Gorman 	 */
225950694c28SMel Gorman 	if (fatal_signal_pending(current))
226050694c28SMel Gorman 		goto out;
22615515061dSMel Gorman 
22625515061dSMel Gorman 	/* Check if the pfmemalloc reserves are ok */
22635515061dSMel Gorman 	first_zones_zonelist(zonelist, high_zoneidx, NULL, &zone);
22645515061dSMel Gorman 	pgdat = zone->zone_pgdat;
22655515061dSMel Gorman 	if (pfmemalloc_watermark_ok(pgdat))
226650694c28SMel Gorman 		goto out;
22675515061dSMel Gorman 
226868243e76SMel Gorman 	/* Account for the throttling */
226968243e76SMel Gorman 	count_vm_event(PGSCAN_DIRECT_THROTTLE);
227068243e76SMel Gorman 
22715515061dSMel Gorman 	/*
22725515061dSMel Gorman 	 * If the caller cannot enter the filesystem, it's possible that it
22735515061dSMel Gorman 	 * is due to the caller holding an FS lock or performing a journal
22745515061dSMel Gorman 	 * transaction in the case of a filesystem like ext[3|4]. In this case,
22755515061dSMel Gorman 	 * it is not safe to block on pfmemalloc_wait as kswapd could be
22765515061dSMel Gorman 	 * blocked waiting on the same lock. Instead, throttle for up to a
22775515061dSMel Gorman 	 * second before continuing.
22785515061dSMel Gorman 	 */
22795515061dSMel Gorman 	if (!(gfp_mask & __GFP_FS)) {
22805515061dSMel Gorman 		wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
22815515061dSMel Gorman 			pfmemalloc_watermark_ok(pgdat), HZ);
228250694c28SMel Gorman 
228350694c28SMel Gorman 		goto check_pending;
22845515061dSMel Gorman 	}
22855515061dSMel Gorman 
22865515061dSMel Gorman 	/* Throttle until kswapd wakes the process */
22875515061dSMel Gorman 	wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
22885515061dSMel Gorman 		pfmemalloc_watermark_ok(pgdat));
228950694c28SMel Gorman 
229050694c28SMel Gorman check_pending:
229150694c28SMel Gorman 	if (fatal_signal_pending(current))
229250694c28SMel Gorman 		return true;
229350694c28SMel Gorman 
229450694c28SMel Gorman out:
229550694c28SMel Gorman 	return false;
22965515061dSMel Gorman }
22975515061dSMel Gorman 
2298dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
2299327c0e96SKAMEZAWA Hiroyuki 				gfp_t gfp_mask, nodemask_t *nodemask)
230066e1707bSBalbir Singh {
230133906bc5SMel Gorman 	unsigned long nr_reclaimed;
230266e1707bSBalbir Singh 	struct scan_control sc = {
230366e1707bSBalbir Singh 		.gfp_mask = gfp_mask,
230466e1707bSBalbir Singh 		.may_writepage = !laptop_mode,
230522fba335SKOSAKI Motohiro 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
2306a6dc60f8SJohannes Weiner 		.may_unmap = 1,
23072e2e4259SKOSAKI Motohiro 		.may_swap = 1,
230866e1707bSBalbir Singh 		.order = order,
23099e3b2f8cSKonstantin Khlebnikov 		.priority = DEF_PRIORITY,
2310f16015fbSJohannes Weiner 		.target_mem_cgroup = NULL,
2311327c0e96SKAMEZAWA Hiroyuki 		.nodemask = nodemask,
231266e1707bSBalbir Singh 	};
2313a09ed5e0SYing Han 	struct shrink_control shrink = {
2314a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
2315a09ed5e0SYing Han 	};
231666e1707bSBalbir Singh 
23175515061dSMel Gorman 	/*
231850694c28SMel Gorman 	 * Do not enter reclaim if fatal signal was delivered while throttled.
231950694c28SMel Gorman 	 * 1 is returned so that the page allocator does not OOM kill at this
232050694c28SMel Gorman 	 * point.
23215515061dSMel Gorman 	 */
232250694c28SMel Gorman 	if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask))
23235515061dSMel Gorman 		return 1;
23245515061dSMel Gorman 
232533906bc5SMel Gorman 	trace_mm_vmscan_direct_reclaim_begin(order,
232633906bc5SMel Gorman 				sc.may_writepage,
232733906bc5SMel Gorman 				gfp_mask);
232833906bc5SMel Gorman 
2329a09ed5e0SYing Han 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
233033906bc5SMel Gorman 
233133906bc5SMel Gorman 	trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
233233906bc5SMel Gorman 
233333906bc5SMel Gorman 	return nr_reclaimed;
233466e1707bSBalbir Singh }
233566e1707bSBalbir Singh 
2336c255a458SAndrew Morton #ifdef CONFIG_MEMCG
233766e1707bSBalbir Singh 
233872835c86SJohannes Weiner unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg,
23394e416953SBalbir Singh 						gfp_t gfp_mask, bool noswap,
23400ae5e89cSYing Han 						struct zone *zone,
23410ae5e89cSYing Han 						unsigned long *nr_scanned)
23424e416953SBalbir Singh {
23434e416953SBalbir Singh 	struct scan_control sc = {
23440ae5e89cSYing Han 		.nr_scanned = 0,
2345b8f5c566SKOSAKI Motohiro 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
23464e416953SBalbir Singh 		.may_writepage = !laptop_mode,
23474e416953SBalbir Singh 		.may_unmap = 1,
23484e416953SBalbir Singh 		.may_swap = !noswap,
23494e416953SBalbir Singh 		.order = 0,
23509e3b2f8cSKonstantin Khlebnikov 		.priority = 0,
235172835c86SJohannes Weiner 		.target_mem_cgroup = memcg,
23524e416953SBalbir Singh 	};
2353f9be23d6SKonstantin Khlebnikov 	struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
23540ae5e89cSYing Han 
23554e416953SBalbir Singh 	sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
23564e416953SBalbir Singh 			(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
2357bdce6d9eSKOSAKI Motohiro 
23589e3b2f8cSKonstantin Khlebnikov 	trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
2359bdce6d9eSKOSAKI Motohiro 						      sc.may_writepage,
2360bdce6d9eSKOSAKI Motohiro 						      sc.gfp_mask);
2361bdce6d9eSKOSAKI Motohiro 
23624e416953SBalbir Singh 	/*
23634e416953SBalbir Singh 	 * NOTE: Although we can get the priority field, using it
23644e416953SBalbir Singh 	 * here is not a good idea, since it limits the pages we can scan.
23654e416953SBalbir Singh 	 * if we don't reclaim here, the shrink_zone from balance_pgdat
23664e416953SBalbir Singh 	 * will pick up pages from other mem cgroup's as well. We hack
23674e416953SBalbir Singh 	 * the priority and make it zero.
23684e416953SBalbir Singh 	 */
2369f9be23d6SKonstantin Khlebnikov 	shrink_lruvec(lruvec, &sc);
2370bdce6d9eSKOSAKI Motohiro 
2371bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
2372bdce6d9eSKOSAKI Motohiro 
23730ae5e89cSYing Han 	*nr_scanned = sc.nr_scanned;
23744e416953SBalbir Singh 	return sc.nr_reclaimed;
23754e416953SBalbir Singh }
23764e416953SBalbir Singh 
237772835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
23788c7c6e34SKAMEZAWA Hiroyuki 					   gfp_t gfp_mask,
2379185efc0fSJohannes Weiner 					   bool noswap)
238066e1707bSBalbir Singh {
23814e416953SBalbir Singh 	struct zonelist *zonelist;
2382bdce6d9eSKOSAKI Motohiro 	unsigned long nr_reclaimed;
2383889976dbSYing Han 	int nid;
238466e1707bSBalbir Singh 	struct scan_control sc = {
238566e1707bSBalbir Singh 		.may_writepage = !laptop_mode,
2386a6dc60f8SJohannes Weiner 		.may_unmap = 1,
23872e2e4259SKOSAKI Motohiro 		.may_swap = !noswap,
238822fba335SKOSAKI Motohiro 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
238966e1707bSBalbir Singh 		.order = 0,
23909e3b2f8cSKonstantin Khlebnikov 		.priority = DEF_PRIORITY,
239172835c86SJohannes Weiner 		.target_mem_cgroup = memcg,
2392327c0e96SKAMEZAWA Hiroyuki 		.nodemask = NULL, /* we don't care the placement */
2393a09ed5e0SYing Han 		.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
2394a09ed5e0SYing Han 				(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
2395a09ed5e0SYing Han 	};
2396a09ed5e0SYing Han 	struct shrink_control shrink = {
2397a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
239866e1707bSBalbir Singh 	};
239966e1707bSBalbir Singh 
2400889976dbSYing Han 	/*
2401889976dbSYing Han 	 * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't
2402889976dbSYing Han 	 * take care of from where we get pages. So the node where we start the
2403889976dbSYing Han 	 * scan does not need to be the current node.
2404889976dbSYing Han 	 */
240572835c86SJohannes Weiner 	nid = mem_cgroup_select_victim_node(memcg);
2406889976dbSYing Han 
2407889976dbSYing Han 	zonelist = NODE_DATA(nid)->node_zonelists;
2408bdce6d9eSKOSAKI Motohiro 
2409bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_reclaim_begin(0,
2410bdce6d9eSKOSAKI Motohiro 					    sc.may_writepage,
2411bdce6d9eSKOSAKI Motohiro 					    sc.gfp_mask);
2412bdce6d9eSKOSAKI Motohiro 
2413a09ed5e0SYing Han 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
2414bdce6d9eSKOSAKI Motohiro 
2415bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
2416bdce6d9eSKOSAKI Motohiro 
2417bdce6d9eSKOSAKI Motohiro 	return nr_reclaimed;
241866e1707bSBalbir Singh }
241966e1707bSBalbir Singh #endif
242066e1707bSBalbir Singh 
24219e3b2f8cSKonstantin Khlebnikov static void age_active_anon(struct zone *zone, struct scan_control *sc)
2422f16015fbSJohannes Weiner {
2423b95a2f2dSJohannes Weiner 	struct mem_cgroup *memcg;
2424b95a2f2dSJohannes Weiner 
2425b95a2f2dSJohannes Weiner 	if (!total_swap_pages)
2426b95a2f2dSJohannes Weiner 		return;
2427b95a2f2dSJohannes Weiner 
2428b95a2f2dSJohannes Weiner 	memcg = mem_cgroup_iter(NULL, NULL, NULL);
2429b95a2f2dSJohannes Weiner 	do {
2430c56d5c7dSKonstantin Khlebnikov 		struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
2431f16015fbSJohannes Weiner 
2432c56d5c7dSKonstantin Khlebnikov 		if (inactive_anon_is_low(lruvec))
24331a93be0eSKonstantin Khlebnikov 			shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
24349e3b2f8cSKonstantin Khlebnikov 					   sc, LRU_ACTIVE_ANON);
2435b95a2f2dSJohannes Weiner 
2436b95a2f2dSJohannes Weiner 		memcg = mem_cgroup_iter(NULL, memcg, NULL);
2437b95a2f2dSJohannes Weiner 	} while (memcg);
2438f16015fbSJohannes Weiner }
2439f16015fbSJohannes Weiner 
244060cefed4SJohannes Weiner static bool zone_balanced(struct zone *zone, int order,
244160cefed4SJohannes Weiner 			  unsigned long balance_gap, int classzone_idx)
244260cefed4SJohannes Weiner {
244360cefed4SJohannes Weiner 	if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone) +
244460cefed4SJohannes Weiner 				    balance_gap, classzone_idx, 0))
244560cefed4SJohannes Weiner 		return false;
244660cefed4SJohannes Weiner 
2447d84da3f9SKirill A. Shutemov 	if (IS_ENABLED(CONFIG_COMPACTION) && order &&
2448d84da3f9SKirill A. Shutemov 	    !compaction_suitable(zone, order))
244960cefed4SJohannes Weiner 		return false;
245060cefed4SJohannes Weiner 
245160cefed4SJohannes Weiner 	return true;
245260cefed4SJohannes Weiner }
245360cefed4SJohannes Weiner 
24541741c877SMel Gorman /*
24551741c877SMel Gorman  * pgdat_balanced is used when checking if a node is balanced for high-order
24561741c877SMel Gorman  * allocations. Only zones that meet watermarks and are in a zone allowed
24571741c877SMel Gorman  * by the callers classzone_idx are added to balanced_pages. The total of
24581741c877SMel Gorman  * balanced pages must be at least 25% of the zones allowed by classzone_idx
24591741c877SMel Gorman  * for the node to be considered balanced. Forcing all zones to be balanced
24601741c877SMel Gorman  * for high orders can cause excessive reclaim when there are imbalanced zones.
24611741c877SMel Gorman  * The choice of 25% is due to
24621741c877SMel Gorman  *   o a 16M DMA zone that is balanced will not balance a zone on any
24631741c877SMel Gorman  *     reasonable sized machine
24641741c877SMel Gorman  *   o On all other machines, the top zone must be at least a reasonable
246525985edcSLucas De Marchi  *     percentage of the middle zones. For example, on 32-bit x86, highmem
24661741c877SMel Gorman  *     would need to be at least 256M for it to be balance a whole node.
24671741c877SMel Gorman  *     Similarly, on x86-64 the Normal zone would need to be at least 1G
24681741c877SMel Gorman  *     to balance a node on its own. These seemed like reasonable ratios.
24691741c877SMel Gorman  */
24701741c877SMel Gorman static bool pgdat_balanced(pg_data_t *pgdat, unsigned long balanced_pages,
24711741c877SMel Gorman 						int classzone_idx)
24721741c877SMel Gorman {
24731741c877SMel Gorman 	unsigned long present_pages = 0;
24741741c877SMel Gorman 	int i;
24751741c877SMel Gorman 
24761741c877SMel Gorman 	for (i = 0; i <= classzone_idx; i++)
24771741c877SMel Gorman 		present_pages += pgdat->node_zones[i].present_pages;
24781741c877SMel Gorman 
24794746efdeSShaohua Li 	/* A special case here: if zone has no page, we think it's balanced */
24804746efdeSShaohua Li 	return balanced_pages >= (present_pages >> 2);
24811741c877SMel Gorman }
24821741c877SMel Gorman 
24835515061dSMel Gorman /*
24845515061dSMel Gorman  * Prepare kswapd for sleeping. This verifies that there are no processes
24855515061dSMel Gorman  * waiting in throttle_direct_reclaim() and that watermarks have been met.
24865515061dSMel Gorman  *
24875515061dSMel Gorman  * Returns true if kswapd is ready to sleep
24885515061dSMel Gorman  */
24895515061dSMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, long remaining,
2490dc83edd9SMel Gorman 					int classzone_idx)
2491f50de2d3SMel Gorman {
2492bb3ab596SKOSAKI Motohiro 	int i;
24931741c877SMel Gorman 	unsigned long balanced = 0;
24941741c877SMel Gorman 	bool all_zones_ok = true;
2495f50de2d3SMel Gorman 
2496f50de2d3SMel Gorman 	/* If a direct reclaimer woke kswapd within HZ/10, it's premature */
2497f50de2d3SMel Gorman 	if (remaining)
24985515061dSMel Gorman 		return false;
24995515061dSMel Gorman 
25005515061dSMel Gorman 	/*
25015515061dSMel Gorman 	 * There is a potential race between when kswapd checks its watermarks
25025515061dSMel Gorman 	 * and a process gets throttled. There is also a potential race if
25035515061dSMel Gorman 	 * processes get throttled, kswapd wakes, a large process exits therby
25045515061dSMel Gorman 	 * balancing the zones that causes kswapd to miss a wakeup. If kswapd
25055515061dSMel Gorman 	 * is going to sleep, no process should be sleeping on pfmemalloc_wait
25065515061dSMel Gorman 	 * so wake them now if necessary. If necessary, processes will wake
25075515061dSMel Gorman 	 * kswapd and get throttled again
25085515061dSMel Gorman 	 */
25095515061dSMel Gorman 	if (waitqueue_active(&pgdat->pfmemalloc_wait)) {
25105515061dSMel Gorman 		wake_up(&pgdat->pfmemalloc_wait);
25115515061dSMel Gorman 		return false;
25125515061dSMel Gorman 	}
2513f50de2d3SMel Gorman 
25140abdee2bSMel Gorman 	/* Check the watermark levels */
251508951e54SMel Gorman 	for (i = 0; i <= classzone_idx; i++) {
2516bb3ab596SKOSAKI Motohiro 		struct zone *zone = pgdat->node_zones + i;
2517bb3ab596SKOSAKI Motohiro 
2518bb3ab596SKOSAKI Motohiro 		if (!populated_zone(zone))
2519bb3ab596SKOSAKI Motohiro 			continue;
2520bb3ab596SKOSAKI Motohiro 
2521355b09c4SMel Gorman 		/*
2522355b09c4SMel Gorman 		 * balance_pgdat() skips over all_unreclaimable after
2523355b09c4SMel Gorman 		 * DEF_PRIORITY. Effectively, it considers them balanced so
2524355b09c4SMel Gorman 		 * they must be considered balanced here as well if kswapd
2525355b09c4SMel Gorman 		 * is to sleep
2526355b09c4SMel Gorman 		 */
2527355b09c4SMel Gorman 		if (zone->all_unreclaimable) {
2528355b09c4SMel Gorman 			balanced += zone->present_pages;
2529de3fab39SKOSAKI Motohiro 			continue;
2530355b09c4SMel Gorman 		}
2531de3fab39SKOSAKI Motohiro 
253260cefed4SJohannes Weiner 		if (!zone_balanced(zone, order, 0, i))
25331741c877SMel Gorman 			all_zones_ok = false;
25341741c877SMel Gorman 		else
25351741c877SMel Gorman 			balanced += zone->present_pages;
2536bb3ab596SKOSAKI Motohiro 	}
2537f50de2d3SMel Gorman 
25381741c877SMel Gorman 	/*
25391741c877SMel Gorman 	 * For high-order requests, the balanced zones must contain at least
25401741c877SMel Gorman 	 * 25% of the nodes pages for kswapd to sleep. For order-0, all zones
25411741c877SMel Gorman 	 * must be balanced
25421741c877SMel Gorman 	 */
25431741c877SMel Gorman 	if (order)
25445515061dSMel Gorman 		return pgdat_balanced(pgdat, balanced, classzone_idx);
25451741c877SMel Gorman 	else
25465515061dSMel Gorman 		return all_zones_ok;
2547f50de2d3SMel Gorman }
2548f50de2d3SMel Gorman 
25491da177e4SLinus Torvalds /*
25501da177e4SLinus Torvalds  * For kswapd, balance_pgdat() will work across all this node's zones until
255141858966SMel Gorman  * they are all at high_wmark_pages(zone).
25521da177e4SLinus Torvalds  *
25530abdee2bSMel Gorman  * Returns the final order kswapd was reclaiming at
25541da177e4SLinus Torvalds  *
25551da177e4SLinus Torvalds  * There is special handling here for zones which are full of pinned pages.
25561da177e4SLinus Torvalds  * This can happen if the pages are all mlocked, or if they are all used by
25571da177e4SLinus Torvalds  * device drivers (say, ZONE_DMA).  Or if they are all in use by hugetlb.
25581da177e4SLinus Torvalds  * What we do is to detect the case where all pages in the zone have been
25591da177e4SLinus Torvalds  * scanned twice and there has been zero successful reclaim.  Mark the zone as
25601da177e4SLinus Torvalds  * dead and from now on, only perform a short scan.  Basically we're polling
25611da177e4SLinus Torvalds  * the zone for when the problem goes away.
25621da177e4SLinus Torvalds  *
25631da177e4SLinus Torvalds  * kswapd scans the zones in the highmem->normal->dma direction.  It skips
256441858966SMel Gorman  * zones which have free_pages > high_wmark_pages(zone), but once a zone is
256541858966SMel Gorman  * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the
256641858966SMel Gorman  * lower zones regardless of the number of free pages in the lower zones. This
256741858966SMel Gorman  * interoperates with the page allocator fallback scheme to ensure that aging
256841858966SMel Gorman  * of pages is balanced across the zones.
25691da177e4SLinus Torvalds  */
257099504748SMel Gorman static unsigned long balance_pgdat(pg_data_t *pgdat, int order,
2571dc83edd9SMel Gorman 							int *classzone_idx)
25721da177e4SLinus Torvalds {
25731da177e4SLinus Torvalds 	int all_zones_ok;
25741741c877SMel Gorman 	unsigned long balanced;
25751da177e4SLinus Torvalds 	int i;
257699504748SMel Gorman 	int end_zone = 0;	/* Inclusive.  0 = ZONE_DMA */
257769e05944SAndrew Morton 	unsigned long total_scanned;
25781da177e4SLinus Torvalds 	struct reclaim_state *reclaim_state = current->reclaim_state;
25790ae5e89cSYing Han 	unsigned long nr_soft_reclaimed;
25800ae5e89cSYing Han 	unsigned long nr_soft_scanned;
2581179e9639SAndrew Morton 	struct scan_control sc = {
2582179e9639SAndrew Morton 		.gfp_mask = GFP_KERNEL,
2583a6dc60f8SJohannes Weiner 		.may_unmap = 1,
25842e2e4259SKOSAKI Motohiro 		.may_swap = 1,
258522fba335SKOSAKI Motohiro 		/*
258622fba335SKOSAKI Motohiro 		 * kswapd doesn't want to be bailed out while reclaim. because
258722fba335SKOSAKI Motohiro 		 * we want to put equal scanning pressure on each zone.
258822fba335SKOSAKI Motohiro 		 */
258922fba335SKOSAKI Motohiro 		.nr_to_reclaim = ULONG_MAX,
25905ad333ebSAndy Whitcroft 		.order = order,
2591f16015fbSJohannes Weiner 		.target_mem_cgroup = NULL,
2592179e9639SAndrew Morton 	};
2593a09ed5e0SYing Han 	struct shrink_control shrink = {
2594a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
2595a09ed5e0SYing Han 	};
25961da177e4SLinus Torvalds loop_again:
25971da177e4SLinus Torvalds 	total_scanned = 0;
25989e3b2f8cSKonstantin Khlebnikov 	sc.priority = DEF_PRIORITY;
2599a79311c1SRik van Riel 	sc.nr_reclaimed = 0;
2600c0bbbc73SChristoph Lameter 	sc.may_writepage = !laptop_mode;
2601f8891e5eSChristoph Lameter 	count_vm_event(PAGEOUTRUN);
26021da177e4SLinus Torvalds 
26039e3b2f8cSKonstantin Khlebnikov 	do {
26041da177e4SLinus Torvalds 		unsigned long lru_pages = 0;
2605bb3ab596SKOSAKI Motohiro 		int has_under_min_watermark_zone = 0;
26061da177e4SLinus Torvalds 
26071da177e4SLinus Torvalds 		all_zones_ok = 1;
26081741c877SMel Gorman 		balanced = 0;
26091da177e4SLinus Torvalds 
26101da177e4SLinus Torvalds 		/*
26111da177e4SLinus Torvalds 		 * Scan in the highmem->dma direction for the highest
26121da177e4SLinus Torvalds 		 * zone which needs scanning
26131da177e4SLinus Torvalds 		 */
26141da177e4SLinus Torvalds 		for (i = pgdat->nr_zones - 1; i >= 0; i--) {
26151da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
26161da177e4SLinus Torvalds 
2617f3fe6512SCon Kolivas 			if (!populated_zone(zone))
26181da177e4SLinus Torvalds 				continue;
26191da177e4SLinus Torvalds 
26209e3b2f8cSKonstantin Khlebnikov 			if (zone->all_unreclaimable &&
26219e3b2f8cSKonstantin Khlebnikov 			    sc.priority != DEF_PRIORITY)
26221da177e4SLinus Torvalds 				continue;
26231da177e4SLinus Torvalds 
2624556adecbSRik van Riel 			/*
2625556adecbSRik van Riel 			 * Do some background aging of the anon list, to give
2626556adecbSRik van Riel 			 * pages a chance to be referenced before reclaiming.
2627556adecbSRik van Riel 			 */
26289e3b2f8cSKonstantin Khlebnikov 			age_active_anon(zone, &sc);
2629556adecbSRik van Riel 
2630cc715d99SMel Gorman 			/*
2631cc715d99SMel Gorman 			 * If the number of buffer_heads in the machine
2632cc715d99SMel Gorman 			 * exceeds the maximum allowed level and this node
2633cc715d99SMel Gorman 			 * has a highmem zone, force kswapd to reclaim from
2634cc715d99SMel Gorman 			 * it to relieve lowmem pressure.
2635cc715d99SMel Gorman 			 */
2636cc715d99SMel Gorman 			if (buffer_heads_over_limit && is_highmem_idx(i)) {
2637cc715d99SMel Gorman 				end_zone = i;
2638cc715d99SMel Gorman 				break;
2639cc715d99SMel Gorman 			}
2640cc715d99SMel Gorman 
264160cefed4SJohannes Weiner 			if (!zone_balanced(zone, order, 0, 0)) {
26421da177e4SLinus Torvalds 				end_zone = i;
2643e1dbeda6SAndrew Morton 				break;
2644439423f6SShaohua Li 			} else {
2645439423f6SShaohua Li 				/* If balanced, clear the congested flag */
2646439423f6SShaohua Li 				zone_clear_flag(zone, ZONE_CONGESTED);
26471da177e4SLinus Torvalds 			}
26481da177e4SLinus Torvalds 		}
2649e1dbeda6SAndrew Morton 		if (i < 0)
26501da177e4SLinus Torvalds 			goto out;
2651e1dbeda6SAndrew Morton 
26521da177e4SLinus Torvalds 		for (i = 0; i <= end_zone; i++) {
26531da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
26541da177e4SLinus Torvalds 
2655adea02a1SWu Fengguang 			lru_pages += zone_reclaimable_pages(zone);
26561da177e4SLinus Torvalds 		}
26571da177e4SLinus Torvalds 
26581da177e4SLinus Torvalds 		/*
26591da177e4SLinus Torvalds 		 * Now scan the zone in the dma->highmem direction, stopping
26601da177e4SLinus Torvalds 		 * at the last zone which needs scanning.
26611da177e4SLinus Torvalds 		 *
26621da177e4SLinus Torvalds 		 * We do this because the page allocator works in the opposite
26631da177e4SLinus Torvalds 		 * direction.  This prevents the page allocator from allocating
26641da177e4SLinus Torvalds 		 * pages behind kswapd's direction of progress, which would
26651da177e4SLinus Torvalds 		 * cause too much scanning of the lower zones.
26661da177e4SLinus Torvalds 		 */
26671da177e4SLinus Torvalds 		for (i = 0; i <= end_zone; i++) {
26681da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
2669fe2c2a10SRik van Riel 			int nr_slab, testorder;
26708afdceceSMel Gorman 			unsigned long balance_gap;
26711da177e4SLinus Torvalds 
2672f3fe6512SCon Kolivas 			if (!populated_zone(zone))
26731da177e4SLinus Torvalds 				continue;
26741da177e4SLinus Torvalds 
26759e3b2f8cSKonstantin Khlebnikov 			if (zone->all_unreclaimable &&
26769e3b2f8cSKonstantin Khlebnikov 			    sc.priority != DEF_PRIORITY)
26771da177e4SLinus Torvalds 				continue;
26781da177e4SLinus Torvalds 
26791da177e4SLinus Torvalds 			sc.nr_scanned = 0;
26804e416953SBalbir Singh 
26810ae5e89cSYing Han 			nr_soft_scanned = 0;
26824e416953SBalbir Singh 			/*
26834e416953SBalbir Singh 			 * Call soft limit reclaim before calling shrink_zone.
26844e416953SBalbir Singh 			 */
26850ae5e89cSYing Han 			nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
26860ae5e89cSYing Han 							order, sc.gfp_mask,
26870ae5e89cSYing Han 							&nr_soft_scanned);
26880ae5e89cSYing Han 			sc.nr_reclaimed += nr_soft_reclaimed;
26890ae5e89cSYing Han 			total_scanned += nr_soft_scanned;
269000918b6aSKOSAKI Motohiro 
269132a4330dSRik van Riel 			/*
26928afdceceSMel Gorman 			 * We put equal pressure on every zone, unless
26938afdceceSMel Gorman 			 * one zone has way too many pages free
26948afdceceSMel Gorman 			 * already. The "too many pages" is defined
26958afdceceSMel Gorman 			 * as the high wmark plus a "gap" where the
26968afdceceSMel Gorman 			 * gap is either the low watermark or 1%
26978afdceceSMel Gorman 			 * of the zone, whichever is smaller.
269832a4330dSRik van Riel 			 */
26998afdceceSMel Gorman 			balance_gap = min(low_wmark_pages(zone),
27008afdceceSMel Gorman 				(zone->present_pages +
27018afdceceSMel Gorman 					KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
27028afdceceSMel Gorman 				KSWAPD_ZONE_BALANCE_GAP_RATIO);
2703fe2c2a10SRik van Riel 			/*
2704fe2c2a10SRik van Riel 			 * Kswapd reclaims only single pages with compaction
2705fe2c2a10SRik van Riel 			 * enabled. Trying too hard to reclaim until contiguous
2706fe2c2a10SRik van Riel 			 * free pages have become available can hurt performance
2707fe2c2a10SRik van Riel 			 * by evicting too much useful data from memory.
2708fe2c2a10SRik van Riel 			 * Do not reclaim more than needed for compaction.
2709fe2c2a10SRik van Riel 			 */
2710fe2c2a10SRik van Riel 			testorder = order;
2711d84da3f9SKirill A. Shutemov 			if (IS_ENABLED(CONFIG_COMPACTION) && order &&
2712fe2c2a10SRik van Riel 					compaction_suitable(zone, order) !=
2713fe2c2a10SRik van Riel 						COMPACT_SKIPPED)
2714fe2c2a10SRik van Riel 				testorder = 0;
2715fe2c2a10SRik van Riel 
2716cc715d99SMel Gorman 			if ((buffer_heads_over_limit && is_highmem_idx(i)) ||
271760cefed4SJohannes Weiner 			    !zone_balanced(zone, testorder,
271860cefed4SJohannes Weiner 					   balance_gap, end_zone)) {
27199e3b2f8cSKonstantin Khlebnikov 				shrink_zone(zone, &sc);
2720d7868daeSMel Gorman 
27211da177e4SLinus Torvalds 				reclaim_state->reclaimed_slab = 0;
27221495f230SYing Han 				nr_slab = shrink_slab(&shrink, sc.nr_scanned, lru_pages);
2723a79311c1SRik van Riel 				sc.nr_reclaimed += reclaim_state->reclaimed_slab;
27241da177e4SLinus Torvalds 				total_scanned += sc.nr_scanned;
27255a03b051SAndrea Arcangeli 
2726d7868daeSMel Gorman 				if (nr_slab == 0 && !zone_reclaimable(zone))
272793e4a89aSKOSAKI Motohiro 					zone->all_unreclaimable = 1;
2728d7868daeSMel Gorman 			}
2729d7868daeSMel Gorman 
27301da177e4SLinus Torvalds 			/*
27311da177e4SLinus Torvalds 			 * If we've done a decent amount of scanning and
27321da177e4SLinus Torvalds 			 * the reclaim ratio is low, start doing writepage
27331da177e4SLinus Torvalds 			 * even in laptop mode
27341da177e4SLinus Torvalds 			 */
27351da177e4SLinus Torvalds 			if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
2736a79311c1SRik van Riel 			    total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2)
27371da177e4SLinus Torvalds 				sc.may_writepage = 1;
2738bb3ab596SKOSAKI Motohiro 
2739215ddd66SMel Gorman 			if (zone->all_unreclaimable) {
2740215ddd66SMel Gorman 				if (end_zone && end_zone == i)
2741215ddd66SMel Gorman 					end_zone--;
2742d7868daeSMel Gorman 				continue;
2743215ddd66SMel Gorman 			}
2744d7868daeSMel Gorman 
274560cefed4SJohannes Weiner 			if (!zone_balanced(zone, testorder, 0, end_zone)) {
274645973d74SMinchan Kim 				all_zones_ok = 0;
2747bb3ab596SKOSAKI Motohiro 				/*
274845973d74SMinchan Kim 				 * We are still under min water mark.  This
274945973d74SMinchan Kim 				 * means that we have a GFP_ATOMIC allocation
275045973d74SMinchan Kim 				 * failure risk. Hurry up!
2751bb3ab596SKOSAKI Motohiro 				 */
275288f5acf8SMel Gorman 				if (!zone_watermark_ok_safe(zone, order,
275345973d74SMinchan Kim 					    min_wmark_pages(zone), end_zone, 0))
2754bb3ab596SKOSAKI Motohiro 					has_under_min_watermark_zone = 1;
27550e093d99SMel Gorman 			} else {
27560e093d99SMel Gorman 				/*
27570e093d99SMel Gorman 				 * If a zone reaches its high watermark,
27580e093d99SMel Gorman 				 * consider it to be no longer congested. It's
27590e093d99SMel Gorman 				 * possible there are dirty pages backed by
27600e093d99SMel Gorman 				 * congested BDIs but as pressure is relieved,
2761ab8704b8SWanpeng Li 				 * speculatively avoid congestion waits
27620e093d99SMel Gorman 				 */
27630e093d99SMel Gorman 				zone_clear_flag(zone, ZONE_CONGESTED);
2764dc83edd9SMel Gorman 				if (i <= *classzone_idx)
27651741c877SMel Gorman 					balanced += zone->present_pages;
276645973d74SMinchan Kim 			}
2767bb3ab596SKOSAKI Motohiro 
27681da177e4SLinus Torvalds 		}
27695515061dSMel Gorman 
27705515061dSMel Gorman 		/*
27715515061dSMel Gorman 		 * If the low watermark is met there is no need for processes
27725515061dSMel Gorman 		 * to be throttled on pfmemalloc_wait as they should not be
27735515061dSMel Gorman 		 * able to safely make forward progress. Wake them
27745515061dSMel Gorman 		 */
27755515061dSMel Gorman 		if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
27765515061dSMel Gorman 				pfmemalloc_watermark_ok(pgdat))
27775515061dSMel Gorman 			wake_up(&pgdat->pfmemalloc_wait);
27785515061dSMel Gorman 
2779dc83edd9SMel Gorman 		if (all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))
27801da177e4SLinus Torvalds 			break;		/* kswapd: all done */
27811da177e4SLinus Torvalds 		/*
27821da177e4SLinus Torvalds 		 * OK, kswapd is getting into trouble.  Take a nap, then take
27831da177e4SLinus Torvalds 		 * another pass across the zones.
27841da177e4SLinus Torvalds 		 */
27859e3b2f8cSKonstantin Khlebnikov 		if (total_scanned && (sc.priority < DEF_PRIORITY - 2)) {
2786bb3ab596SKOSAKI Motohiro 			if (has_under_min_watermark_zone)
2787bb3ab596SKOSAKI Motohiro 				count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT);
2788bb3ab596SKOSAKI Motohiro 			else
27898aa7e847SJens Axboe 				congestion_wait(BLK_RW_ASYNC, HZ/10);
2790bb3ab596SKOSAKI Motohiro 		}
27911da177e4SLinus Torvalds 
27921da177e4SLinus Torvalds 		/*
27931da177e4SLinus Torvalds 		 * We do this so kswapd doesn't build up large priorities for
27941da177e4SLinus Torvalds 		 * example when it is freeing in parallel with allocators. It
27951da177e4SLinus Torvalds 		 * matches the direct reclaim path behaviour in terms of impact
27961da177e4SLinus Torvalds 		 * on zone->*_priority.
27971da177e4SLinus Torvalds 		 */
2798a79311c1SRik van Riel 		if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
27991da177e4SLinus Torvalds 			break;
28009e3b2f8cSKonstantin Khlebnikov 	} while (--sc.priority >= 0);
28011da177e4SLinus Torvalds out:
280299504748SMel Gorman 
280399504748SMel Gorman 	/*
280499504748SMel Gorman 	 * order-0: All zones must meet high watermark for a balanced node
28051741c877SMel Gorman 	 * high-order: Balanced zones must make up at least 25% of the node
28061741c877SMel Gorman 	 *             for the node to be balanced
280799504748SMel Gorman 	 */
2808dc83edd9SMel Gorman 	if (!(all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))) {
28091da177e4SLinus Torvalds 		cond_resched();
28108357376dSRafael J. Wysocki 
28118357376dSRafael J. Wysocki 		try_to_freeze();
28128357376dSRafael J. Wysocki 
281373ce02e9SKOSAKI Motohiro 		/*
281473ce02e9SKOSAKI Motohiro 		 * Fragmentation may mean that the system cannot be
281573ce02e9SKOSAKI Motohiro 		 * rebalanced for high-order allocations in all zones.
281673ce02e9SKOSAKI Motohiro 		 * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
281773ce02e9SKOSAKI Motohiro 		 * it means the zones have been fully scanned and are still
281873ce02e9SKOSAKI Motohiro 		 * not balanced. For high-order allocations, there is
281973ce02e9SKOSAKI Motohiro 		 * little point trying all over again as kswapd may
282073ce02e9SKOSAKI Motohiro 		 * infinite loop.
282173ce02e9SKOSAKI Motohiro 		 *
282273ce02e9SKOSAKI Motohiro 		 * Instead, recheck all watermarks at order-0 as they
282373ce02e9SKOSAKI Motohiro 		 * are the most important. If watermarks are ok, kswapd will go
282473ce02e9SKOSAKI Motohiro 		 * back to sleep. High-order users can still perform direct
282573ce02e9SKOSAKI Motohiro 		 * reclaim if they wish.
282673ce02e9SKOSAKI Motohiro 		 */
282773ce02e9SKOSAKI Motohiro 		if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
282873ce02e9SKOSAKI Motohiro 			order = sc.order = 0;
282973ce02e9SKOSAKI Motohiro 
28301da177e4SLinus Torvalds 		goto loop_again;
28311da177e4SLinus Torvalds 	}
28321da177e4SLinus Torvalds 
283399504748SMel Gorman 	/*
283499504748SMel Gorman 	 * If kswapd was reclaiming at a higher order, it has the option of
283599504748SMel Gorman 	 * sleeping without all zones being balanced. Before it does, it must
283699504748SMel Gorman 	 * ensure that the watermarks for order-0 on *all* zones are met and
283799504748SMel Gorman 	 * that the congestion flags are cleared. The congestion flag must
283899504748SMel Gorman 	 * be cleared as kswapd is the only mechanism that clears the flag
283999504748SMel Gorman 	 * and it is potentially going to sleep here.
284099504748SMel Gorman 	 */
284199504748SMel Gorman 	if (order) {
28427be62de9SRik van Riel 		int zones_need_compaction = 1;
28437be62de9SRik van Riel 
284499504748SMel Gorman 		for (i = 0; i <= end_zone; i++) {
284599504748SMel Gorman 			struct zone *zone = pgdat->node_zones + i;
284699504748SMel Gorman 
284799504748SMel Gorman 			if (!populated_zone(zone))
284899504748SMel Gorman 				continue;
284999504748SMel Gorman 
28507be62de9SRik van Riel 			/* Check if the memory needs to be defragmented. */
28517be62de9SRik van Riel 			if (zone_watermark_ok(zone, order,
28527be62de9SRik van Riel 				    low_wmark_pages(zone), *classzone_idx, 0))
28537be62de9SRik van Riel 				zones_need_compaction = 0;
285499504748SMel Gorman 		}
28557be62de9SRik van Riel 
28567be62de9SRik van Riel 		if (zones_need_compaction)
28577be62de9SRik van Riel 			compact_pgdat(pgdat, order);
285899504748SMel Gorman 	}
285999504748SMel Gorman 
28600abdee2bSMel Gorman 	/*
28615515061dSMel Gorman 	 * Return the order we were reclaiming at so prepare_kswapd_sleep()
28620abdee2bSMel Gorman 	 * makes a decision on the order we were last reclaiming at. However,
28630abdee2bSMel Gorman 	 * if another caller entered the allocator slow path while kswapd
28640abdee2bSMel Gorman 	 * was awake, order will remain at the higher level
28650abdee2bSMel Gorman 	 */
2866dc83edd9SMel Gorman 	*classzone_idx = end_zone;
28670abdee2bSMel Gorman 	return order;
28681da177e4SLinus Torvalds }
28691da177e4SLinus Torvalds 
2870dc83edd9SMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx)
2871f0bc0a60SKOSAKI Motohiro {
2872f0bc0a60SKOSAKI Motohiro 	long remaining = 0;
2873f0bc0a60SKOSAKI Motohiro 	DEFINE_WAIT(wait);
2874f0bc0a60SKOSAKI Motohiro 
2875f0bc0a60SKOSAKI Motohiro 	if (freezing(current) || kthread_should_stop())
2876f0bc0a60SKOSAKI Motohiro 		return;
2877f0bc0a60SKOSAKI Motohiro 
2878f0bc0a60SKOSAKI Motohiro 	prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2879f0bc0a60SKOSAKI Motohiro 
2880f0bc0a60SKOSAKI Motohiro 	/* Try to sleep for a short interval */
28815515061dSMel Gorman 	if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
2882f0bc0a60SKOSAKI Motohiro 		remaining = schedule_timeout(HZ/10);
2883f0bc0a60SKOSAKI Motohiro 		finish_wait(&pgdat->kswapd_wait, &wait);
2884f0bc0a60SKOSAKI Motohiro 		prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2885f0bc0a60SKOSAKI Motohiro 	}
2886f0bc0a60SKOSAKI Motohiro 
2887f0bc0a60SKOSAKI Motohiro 	/*
2888f0bc0a60SKOSAKI Motohiro 	 * After a short sleep, check if it was a premature sleep. If not, then
2889f0bc0a60SKOSAKI Motohiro 	 * go fully to sleep until explicitly woken up.
2890f0bc0a60SKOSAKI Motohiro 	 */
28915515061dSMel Gorman 	if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
2892f0bc0a60SKOSAKI Motohiro 		trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
2893f0bc0a60SKOSAKI Motohiro 
2894f0bc0a60SKOSAKI Motohiro 		/*
2895f0bc0a60SKOSAKI Motohiro 		 * vmstat counters are not perfectly accurate and the estimated
2896f0bc0a60SKOSAKI Motohiro 		 * value for counters such as NR_FREE_PAGES can deviate from the
2897f0bc0a60SKOSAKI Motohiro 		 * true value by nr_online_cpus * threshold. To avoid the zone
2898f0bc0a60SKOSAKI Motohiro 		 * watermarks being breached while under pressure, we reduce the
2899f0bc0a60SKOSAKI Motohiro 		 * per-cpu vmstat threshold while kswapd is awake and restore
2900f0bc0a60SKOSAKI Motohiro 		 * them before going back to sleep.
2901f0bc0a60SKOSAKI Motohiro 		 */
2902f0bc0a60SKOSAKI Motohiro 		set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
29031c7e7f6cSAaditya Kumar 
290462997027SMel Gorman 		/*
290562997027SMel Gorman 		 * Compaction records what page blocks it recently failed to
290662997027SMel Gorman 		 * isolate pages from and skips them in the future scanning.
290762997027SMel Gorman 		 * When kswapd is going to sleep, it is reasonable to assume
290862997027SMel Gorman 		 * that pages and compaction may succeed so reset the cache.
290962997027SMel Gorman 		 */
291062997027SMel Gorman 		reset_isolation_suitable(pgdat);
291162997027SMel Gorman 
29121c7e7f6cSAaditya Kumar 		if (!kthread_should_stop())
2913f0bc0a60SKOSAKI Motohiro 			schedule();
29141c7e7f6cSAaditya Kumar 
2915f0bc0a60SKOSAKI Motohiro 		set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
2916f0bc0a60SKOSAKI Motohiro 	} else {
2917f0bc0a60SKOSAKI Motohiro 		if (remaining)
2918f0bc0a60SKOSAKI Motohiro 			count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
2919f0bc0a60SKOSAKI Motohiro 		else
2920f0bc0a60SKOSAKI Motohiro 			count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
2921f0bc0a60SKOSAKI Motohiro 	}
2922f0bc0a60SKOSAKI Motohiro 	finish_wait(&pgdat->kswapd_wait, &wait);
2923f0bc0a60SKOSAKI Motohiro }
2924f0bc0a60SKOSAKI Motohiro 
29251da177e4SLinus Torvalds /*
29261da177e4SLinus Torvalds  * The background pageout daemon, started as a kernel thread
29271da177e4SLinus Torvalds  * from the init process.
29281da177e4SLinus Torvalds  *
29291da177e4SLinus Torvalds  * This basically trickles out pages so that we have _some_
29301da177e4SLinus Torvalds  * free memory available even if there is no other activity
29311da177e4SLinus Torvalds  * that frees anything up. This is needed for things like routing
29321da177e4SLinus Torvalds  * etc, where we otherwise might have all activity going on in
29331da177e4SLinus Torvalds  * asynchronous contexts that cannot page things out.
29341da177e4SLinus Torvalds  *
29351da177e4SLinus Torvalds  * If there are applications that are active memory-allocators
29361da177e4SLinus Torvalds  * (most normal use), this basically shouldn't matter.
29371da177e4SLinus Torvalds  */
29381da177e4SLinus Torvalds static int kswapd(void *p)
29391da177e4SLinus Torvalds {
2940215ddd66SMel Gorman 	unsigned long order, new_order;
2941d2ebd0f6SAlex,Shi 	unsigned balanced_order;
2942215ddd66SMel Gorman 	int classzone_idx, new_classzone_idx;
2943d2ebd0f6SAlex,Shi 	int balanced_classzone_idx;
29441da177e4SLinus Torvalds 	pg_data_t *pgdat = (pg_data_t*)p;
29451da177e4SLinus Torvalds 	struct task_struct *tsk = current;
2946f0bc0a60SKOSAKI Motohiro 
29471da177e4SLinus Torvalds 	struct reclaim_state reclaim_state = {
29481da177e4SLinus Torvalds 		.reclaimed_slab = 0,
29491da177e4SLinus Torvalds 	};
2950a70f7302SRusty Russell 	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
29511da177e4SLinus Torvalds 
2952cf40bd16SNick Piggin 	lockdep_set_current_reclaim_state(GFP_KERNEL);
2953cf40bd16SNick Piggin 
2954174596a0SRusty Russell 	if (!cpumask_empty(cpumask))
2955c5f59f08SMike Travis 		set_cpus_allowed_ptr(tsk, cpumask);
29561da177e4SLinus Torvalds 	current->reclaim_state = &reclaim_state;
29571da177e4SLinus Torvalds 
29581da177e4SLinus Torvalds 	/*
29591da177e4SLinus Torvalds 	 * Tell the memory management that we're a "memory allocator",
29601da177e4SLinus Torvalds 	 * and that if we need more memory we should get access to it
29611da177e4SLinus Torvalds 	 * regardless (see "__alloc_pages()"). "kswapd" should
29621da177e4SLinus Torvalds 	 * never get caught in the normal page freeing logic.
29631da177e4SLinus Torvalds 	 *
29641da177e4SLinus Torvalds 	 * (Kswapd normally doesn't need memory anyway, but sometimes
29651da177e4SLinus Torvalds 	 * you need a small amount of memory in order to be able to
29661da177e4SLinus Torvalds 	 * page out something else, and this flag essentially protects
29671da177e4SLinus Torvalds 	 * us from recursively trying to free more memory as we're
29681da177e4SLinus Torvalds 	 * trying to free the first piece of memory in the first place).
29691da177e4SLinus Torvalds 	 */
2970930d9152SChristoph Lameter 	tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
297183144186SRafael J. Wysocki 	set_freezable();
29721da177e4SLinus Torvalds 
2973215ddd66SMel Gorman 	order = new_order = 0;
2974d2ebd0f6SAlex,Shi 	balanced_order = 0;
2975215ddd66SMel Gorman 	classzone_idx = new_classzone_idx = pgdat->nr_zones - 1;
2976d2ebd0f6SAlex,Shi 	balanced_classzone_idx = classzone_idx;
29771da177e4SLinus Torvalds 	for ( ; ; ) {
29786f6313d4SJeff Liu 		bool ret;
29793e1d1d28SChristoph Lameter 
2980215ddd66SMel Gorman 		/*
2981215ddd66SMel Gorman 		 * If the last balance_pgdat was unsuccessful it's unlikely a
2982215ddd66SMel Gorman 		 * new request of a similar or harder type will succeed soon
2983215ddd66SMel Gorman 		 * so consider going to sleep on the basis we reclaimed at
2984215ddd66SMel Gorman 		 */
2985d2ebd0f6SAlex,Shi 		if (balanced_classzone_idx >= new_classzone_idx &&
2986d2ebd0f6SAlex,Shi 					balanced_order == new_order) {
29871da177e4SLinus Torvalds 			new_order = pgdat->kswapd_max_order;
298899504748SMel Gorman 			new_classzone_idx = pgdat->classzone_idx;
29891da177e4SLinus Torvalds 			pgdat->kswapd_max_order =  0;
2990215ddd66SMel Gorman 			pgdat->classzone_idx = pgdat->nr_zones - 1;
2991215ddd66SMel Gorman 		}
2992215ddd66SMel Gorman 
299399504748SMel Gorman 		if (order < new_order || classzone_idx > new_classzone_idx) {
29941da177e4SLinus Torvalds 			/*
29951da177e4SLinus Torvalds 			 * Don't sleep if someone wants a larger 'order'
299699504748SMel Gorman 			 * allocation or has tigher zone constraints
29971da177e4SLinus Torvalds 			 */
29981da177e4SLinus Torvalds 			order = new_order;
299999504748SMel Gorman 			classzone_idx = new_classzone_idx;
30001da177e4SLinus Torvalds 		} else {
3001d2ebd0f6SAlex,Shi 			kswapd_try_to_sleep(pgdat, balanced_order,
3002d2ebd0f6SAlex,Shi 						balanced_classzone_idx);
30031da177e4SLinus Torvalds 			order = pgdat->kswapd_max_order;
300499504748SMel Gorman 			classzone_idx = pgdat->classzone_idx;
3005f0dfcde0SAlex,Shi 			new_order = order;
3006f0dfcde0SAlex,Shi 			new_classzone_idx = classzone_idx;
30074d40502eSMel Gorman 			pgdat->kswapd_max_order = 0;
3008215ddd66SMel Gorman 			pgdat->classzone_idx = pgdat->nr_zones - 1;
30091da177e4SLinus Torvalds 		}
30101da177e4SLinus Torvalds 
30118fe23e05SDavid Rientjes 		ret = try_to_freeze();
30128fe23e05SDavid Rientjes 		if (kthread_should_stop())
30138fe23e05SDavid Rientjes 			break;
30148fe23e05SDavid Rientjes 
30158fe23e05SDavid Rientjes 		/*
30168fe23e05SDavid Rientjes 		 * We can speed up thawing tasks if we don't call balance_pgdat
30178fe23e05SDavid Rientjes 		 * after returning from the refrigerator
3018b1296cc4SRafael J. Wysocki 		 */
301933906bc5SMel Gorman 		if (!ret) {
302033906bc5SMel Gorman 			trace_mm_vmscan_kswapd_wake(pgdat->node_id, order);
3021d2ebd0f6SAlex,Shi 			balanced_classzone_idx = classzone_idx;
3022d2ebd0f6SAlex,Shi 			balanced_order = balance_pgdat(pgdat, order,
3023d2ebd0f6SAlex,Shi 						&balanced_classzone_idx);
30241da177e4SLinus Torvalds 		}
302533906bc5SMel Gorman 	}
3026b0a8cc58STakamori Yamaguchi 
3027b0a8cc58STakamori Yamaguchi 	current->reclaim_state = NULL;
30281da177e4SLinus Torvalds 	return 0;
30291da177e4SLinus Torvalds }
30301da177e4SLinus Torvalds 
30311da177e4SLinus Torvalds /*
30321da177e4SLinus Torvalds  * A zone is low on free memory, so wake its kswapd task to service it.
30331da177e4SLinus Torvalds  */
303499504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
30351da177e4SLinus Torvalds {
30361da177e4SLinus Torvalds 	pg_data_t *pgdat;
30371da177e4SLinus Torvalds 
3038f3fe6512SCon Kolivas 	if (!populated_zone(zone))
30391da177e4SLinus Torvalds 		return;
30401da177e4SLinus Torvalds 
304102a0e53dSPaul Jackson 	if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
30421da177e4SLinus Torvalds 		return;
304388f5acf8SMel Gorman 	pgdat = zone->zone_pgdat;
304499504748SMel Gorman 	if (pgdat->kswapd_max_order < order) {
304588f5acf8SMel Gorman 		pgdat->kswapd_max_order = order;
304699504748SMel Gorman 		pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx);
304799504748SMel Gorman 	}
30488d0986e2SCon Kolivas 	if (!waitqueue_active(&pgdat->kswapd_wait))
30491da177e4SLinus Torvalds 		return;
305088f5acf8SMel Gorman 	if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0))
305188f5acf8SMel Gorman 		return;
305288f5acf8SMel Gorman 
305388f5acf8SMel Gorman 	trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
30548d0986e2SCon Kolivas 	wake_up_interruptible(&pgdat->kswapd_wait);
30551da177e4SLinus Torvalds }
30561da177e4SLinus Torvalds 
3057adea02a1SWu Fengguang /*
3058adea02a1SWu Fengguang  * The reclaimable count would be mostly accurate.
3059adea02a1SWu Fengguang  * The less reclaimable pages may be
3060adea02a1SWu Fengguang  * - mlocked pages, which will be moved to unevictable list when encountered
3061adea02a1SWu Fengguang  * - mapped pages, which may require several travels to be reclaimed
3062adea02a1SWu Fengguang  * - dirty pages, which is not "instantly" reclaimable
3063adea02a1SWu Fengguang  */
3064adea02a1SWu Fengguang unsigned long global_reclaimable_pages(void)
30654f98a2feSRik van Riel {
3066adea02a1SWu Fengguang 	int nr;
3067adea02a1SWu Fengguang 
3068adea02a1SWu Fengguang 	nr = global_page_state(NR_ACTIVE_FILE) +
3069adea02a1SWu Fengguang 	     global_page_state(NR_INACTIVE_FILE);
3070adea02a1SWu Fengguang 
3071adea02a1SWu Fengguang 	if (nr_swap_pages > 0)
3072adea02a1SWu Fengguang 		nr += global_page_state(NR_ACTIVE_ANON) +
3073adea02a1SWu Fengguang 		      global_page_state(NR_INACTIVE_ANON);
3074adea02a1SWu Fengguang 
3075adea02a1SWu Fengguang 	return nr;
3076adea02a1SWu Fengguang }
3077adea02a1SWu Fengguang 
3078adea02a1SWu Fengguang unsigned long zone_reclaimable_pages(struct zone *zone)
3079adea02a1SWu Fengguang {
3080adea02a1SWu Fengguang 	int nr;
3081adea02a1SWu Fengguang 
3082adea02a1SWu Fengguang 	nr = zone_page_state(zone, NR_ACTIVE_FILE) +
3083adea02a1SWu Fengguang 	     zone_page_state(zone, NR_INACTIVE_FILE);
3084adea02a1SWu Fengguang 
3085adea02a1SWu Fengguang 	if (nr_swap_pages > 0)
3086adea02a1SWu Fengguang 		nr += zone_page_state(zone, NR_ACTIVE_ANON) +
3087adea02a1SWu Fengguang 		      zone_page_state(zone, NR_INACTIVE_ANON);
3088adea02a1SWu Fengguang 
3089adea02a1SWu Fengguang 	return nr;
30904f98a2feSRik van Riel }
30914f98a2feSRik van Riel 
3092c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
30931da177e4SLinus Torvalds /*
30947b51755cSKOSAKI Motohiro  * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
3095d6277db4SRafael J. Wysocki  * freed pages.
3096d6277db4SRafael J. Wysocki  *
3097d6277db4SRafael J. Wysocki  * Rather than trying to age LRUs the aim is to preserve the overall
3098d6277db4SRafael J. Wysocki  * LRU order by reclaiming preferentially
3099d6277db4SRafael J. Wysocki  * inactive > active > active referenced > active mapped
31001da177e4SLinus Torvalds  */
31017b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
31021da177e4SLinus Torvalds {
3103d6277db4SRafael J. Wysocki 	struct reclaim_state reclaim_state;
3104d6277db4SRafael J. Wysocki 	struct scan_control sc = {
31057b51755cSKOSAKI Motohiro 		.gfp_mask = GFP_HIGHUSER_MOVABLE,
31067b51755cSKOSAKI Motohiro 		.may_swap = 1,
31077b51755cSKOSAKI Motohiro 		.may_unmap = 1,
3108d6277db4SRafael J. Wysocki 		.may_writepage = 1,
31097b51755cSKOSAKI Motohiro 		.nr_to_reclaim = nr_to_reclaim,
31107b51755cSKOSAKI Motohiro 		.hibernation_mode = 1,
31117b51755cSKOSAKI Motohiro 		.order = 0,
31129e3b2f8cSKonstantin Khlebnikov 		.priority = DEF_PRIORITY,
31131da177e4SLinus Torvalds 	};
3114a09ed5e0SYing Han 	struct shrink_control shrink = {
3115a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
3116a09ed5e0SYing Han 	};
31177b51755cSKOSAKI Motohiro 	struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
31187b51755cSKOSAKI Motohiro 	struct task_struct *p = current;
31197b51755cSKOSAKI Motohiro 	unsigned long nr_reclaimed;
31201da177e4SLinus Torvalds 
31217b51755cSKOSAKI Motohiro 	p->flags |= PF_MEMALLOC;
31227b51755cSKOSAKI Motohiro 	lockdep_set_current_reclaim_state(sc.gfp_mask);
3123d6277db4SRafael J. Wysocki 	reclaim_state.reclaimed_slab = 0;
31247b51755cSKOSAKI Motohiro 	p->reclaim_state = &reclaim_state;
3125d6277db4SRafael J. Wysocki 
3126a09ed5e0SYing Han 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
3127d6277db4SRafael J. Wysocki 
31287b51755cSKOSAKI Motohiro 	p->reclaim_state = NULL;
31297b51755cSKOSAKI Motohiro 	lockdep_clear_current_reclaim_state();
31307b51755cSKOSAKI Motohiro 	p->flags &= ~PF_MEMALLOC;
3131d6277db4SRafael J. Wysocki 
31327b51755cSKOSAKI Motohiro 	return nr_reclaimed;
31331da177e4SLinus Torvalds }
3134c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */
31351da177e4SLinus Torvalds 
31361da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but
31371da177e4SLinus Torvalds    not required for correctness.  So if the last cpu in a node goes
31381da177e4SLinus Torvalds    away, we get changed to run anywhere: as the first one comes back,
31391da177e4SLinus Torvalds    restore their cpu bindings. */
31409c7b216dSChandra Seetharaman static int __devinit cpu_callback(struct notifier_block *nfb,
314169e05944SAndrew Morton 				  unsigned long action, void *hcpu)
31421da177e4SLinus Torvalds {
314358c0a4a7SYasunori Goto 	int nid;
31441da177e4SLinus Torvalds 
31458bb78442SRafael J. Wysocki 	if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
314648fb2e24SLai Jiangshan 		for_each_node_state(nid, N_MEMORY) {
3147c5f59f08SMike Travis 			pg_data_t *pgdat = NODE_DATA(nid);
3148a70f7302SRusty Russell 			const struct cpumask *mask;
3149a70f7302SRusty Russell 
3150a70f7302SRusty Russell 			mask = cpumask_of_node(pgdat->node_id);
3151c5f59f08SMike Travis 
31523e597945SRusty Russell 			if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
31531da177e4SLinus Torvalds 				/* One of our CPUs online: restore mask */
3154c5f59f08SMike Travis 				set_cpus_allowed_ptr(pgdat->kswapd, mask);
31551da177e4SLinus Torvalds 		}
31561da177e4SLinus Torvalds 	}
31571da177e4SLinus Torvalds 	return NOTIFY_OK;
31581da177e4SLinus Torvalds }
31591da177e4SLinus Torvalds 
31603218ae14SYasunori Goto /*
31613218ae14SYasunori Goto  * This kswapd start function will be called by init and node-hot-add.
31623218ae14SYasunori Goto  * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
31633218ae14SYasunori Goto  */
31643218ae14SYasunori Goto int kswapd_run(int nid)
31653218ae14SYasunori Goto {
31663218ae14SYasunori Goto 	pg_data_t *pgdat = NODE_DATA(nid);
31673218ae14SYasunori Goto 	int ret = 0;
31683218ae14SYasunori Goto 
31693218ae14SYasunori Goto 	if (pgdat->kswapd)
31703218ae14SYasunori Goto 		return 0;
31713218ae14SYasunori Goto 
31723218ae14SYasunori Goto 	pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
31733218ae14SYasunori Goto 	if (IS_ERR(pgdat->kswapd)) {
31743218ae14SYasunori Goto 		/* failure at boot is fatal */
31753218ae14SYasunori Goto 		BUG_ON(system_state == SYSTEM_BOOTING);
317618b48d58SWen Congyang 		pgdat->kswapd = NULL;
3177d5dc0ad9SGavin Shan 		pr_err("Failed to start kswapd on node %d\n", nid);
3178d5dc0ad9SGavin Shan 		ret = PTR_ERR(pgdat->kswapd);
31793218ae14SYasunori Goto 	}
31803218ae14SYasunori Goto 	return ret;
31813218ae14SYasunori Goto }
31823218ae14SYasunori Goto 
31838fe23e05SDavid Rientjes /*
3184d8adde17SJiang Liu  * Called by memory hotplug when all memory in a node is offlined.  Caller must
3185d8adde17SJiang Liu  * hold lock_memory_hotplug().
31868fe23e05SDavid Rientjes  */
31878fe23e05SDavid Rientjes void kswapd_stop(int nid)
31888fe23e05SDavid Rientjes {
31898fe23e05SDavid Rientjes 	struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
31908fe23e05SDavid Rientjes 
3191d8adde17SJiang Liu 	if (kswapd) {
31928fe23e05SDavid Rientjes 		kthread_stop(kswapd);
3193d8adde17SJiang Liu 		NODE_DATA(nid)->kswapd = NULL;
3194d8adde17SJiang Liu 	}
31958fe23e05SDavid Rientjes }
31968fe23e05SDavid Rientjes 
31971da177e4SLinus Torvalds static int __init kswapd_init(void)
31981da177e4SLinus Torvalds {
31993218ae14SYasunori Goto 	int nid;
320069e05944SAndrew Morton 
32011da177e4SLinus Torvalds 	swap_setup();
320248fb2e24SLai Jiangshan 	for_each_node_state(nid, N_MEMORY)
32033218ae14SYasunori Goto  		kswapd_run(nid);
32041da177e4SLinus Torvalds 	hotcpu_notifier(cpu_callback, 0);
32051da177e4SLinus Torvalds 	return 0;
32061da177e4SLinus Torvalds }
32071da177e4SLinus Torvalds 
32081da177e4SLinus Torvalds module_init(kswapd_init)
32099eeff239SChristoph Lameter 
32109eeff239SChristoph Lameter #ifdef CONFIG_NUMA
32119eeff239SChristoph Lameter /*
32129eeff239SChristoph Lameter  * Zone reclaim mode
32139eeff239SChristoph Lameter  *
32149eeff239SChristoph Lameter  * If non-zero call zone_reclaim when the number of free pages falls below
32159eeff239SChristoph Lameter  * the watermarks.
32169eeff239SChristoph Lameter  */
32179eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly;
32189eeff239SChristoph Lameter 
32191b2ffb78SChristoph Lameter #define RECLAIM_OFF 0
32207d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0)	/* Run shrink_inactive_list on the zone */
32211b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1)	/* Writeout pages during reclaim */
32221b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2)	/* Swap pages out during reclaim */
32231b2ffb78SChristoph Lameter 
32249eeff239SChristoph Lameter /*
3225a92f7126SChristoph Lameter  * Priority for ZONE_RECLAIM. This determines the fraction of pages
3226a92f7126SChristoph Lameter  * of a node considered for each zone_reclaim. 4 scans 1/16th of
3227a92f7126SChristoph Lameter  * a zone.
3228a92f7126SChristoph Lameter  */
3229a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4
3230a92f7126SChristoph Lameter 
32319eeff239SChristoph Lameter /*
32329614634fSChristoph Lameter  * Percentage of pages in a zone that must be unmapped for zone_reclaim to
32339614634fSChristoph Lameter  * occur.
32349614634fSChristoph Lameter  */
32359614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1;
32369614634fSChristoph Lameter 
32379614634fSChristoph Lameter /*
32380ff38490SChristoph Lameter  * If the number of slab pages in a zone grows beyond this percentage then
32390ff38490SChristoph Lameter  * slab reclaim needs to occur.
32400ff38490SChristoph Lameter  */
32410ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5;
32420ff38490SChristoph Lameter 
324390afa5deSMel Gorman static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
324490afa5deSMel Gorman {
324590afa5deSMel Gorman 	unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
324690afa5deSMel Gorman 	unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
324790afa5deSMel Gorman 		zone_page_state(zone, NR_ACTIVE_FILE);
324890afa5deSMel Gorman 
324990afa5deSMel Gorman 	/*
325090afa5deSMel Gorman 	 * It's possible for there to be more file mapped pages than
325190afa5deSMel Gorman 	 * accounted for by the pages on the file LRU lists because
325290afa5deSMel Gorman 	 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
325390afa5deSMel Gorman 	 */
325490afa5deSMel Gorman 	return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
325590afa5deSMel Gorman }
325690afa5deSMel Gorman 
325790afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */
325890afa5deSMel Gorman static long zone_pagecache_reclaimable(struct zone *zone)
325990afa5deSMel Gorman {
326090afa5deSMel Gorman 	long nr_pagecache_reclaimable;
326190afa5deSMel Gorman 	long delta = 0;
326290afa5deSMel Gorman 
326390afa5deSMel Gorman 	/*
326490afa5deSMel Gorman 	 * If RECLAIM_SWAP is set, then all file pages are considered
326590afa5deSMel Gorman 	 * potentially reclaimable. Otherwise, we have to worry about
326690afa5deSMel Gorman 	 * pages like swapcache and zone_unmapped_file_pages() provides
326790afa5deSMel Gorman 	 * a better estimate
326890afa5deSMel Gorman 	 */
326990afa5deSMel Gorman 	if (zone_reclaim_mode & RECLAIM_SWAP)
327090afa5deSMel Gorman 		nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
327190afa5deSMel Gorman 	else
327290afa5deSMel Gorman 		nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
327390afa5deSMel Gorman 
327490afa5deSMel Gorman 	/* If we can't clean pages, remove dirty pages from consideration */
327590afa5deSMel Gorman 	if (!(zone_reclaim_mode & RECLAIM_WRITE))
327690afa5deSMel Gorman 		delta += zone_page_state(zone, NR_FILE_DIRTY);
327790afa5deSMel Gorman 
327890afa5deSMel Gorman 	/* Watch for any possible underflows due to delta */
327990afa5deSMel Gorman 	if (unlikely(delta > nr_pagecache_reclaimable))
328090afa5deSMel Gorman 		delta = nr_pagecache_reclaimable;
328190afa5deSMel Gorman 
328290afa5deSMel Gorman 	return nr_pagecache_reclaimable - delta;
328390afa5deSMel Gorman }
328490afa5deSMel Gorman 
32850ff38490SChristoph Lameter /*
32869eeff239SChristoph Lameter  * Try to free up some pages from this zone through reclaim.
32879eeff239SChristoph Lameter  */
3288179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
32899eeff239SChristoph Lameter {
32907fb2d46dSChristoph Lameter 	/* Minimum pages needed in order to stay on node */
329169e05944SAndrew Morton 	const unsigned long nr_pages = 1 << order;
32929eeff239SChristoph Lameter 	struct task_struct *p = current;
32939eeff239SChristoph Lameter 	struct reclaim_state reclaim_state;
3294179e9639SAndrew Morton 	struct scan_control sc = {
3295179e9639SAndrew Morton 		.may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
3296a6dc60f8SJohannes Weiner 		.may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
32972e2e4259SKOSAKI Motohiro 		.may_swap = 1,
329822fba335SKOSAKI Motohiro 		.nr_to_reclaim = max_t(unsigned long, nr_pages,
329922fba335SKOSAKI Motohiro 				       SWAP_CLUSTER_MAX),
3300179e9639SAndrew Morton 		.gfp_mask = gfp_mask,
3301bd2f6199SJohannes Weiner 		.order = order,
33029e3b2f8cSKonstantin Khlebnikov 		.priority = ZONE_RECLAIM_PRIORITY,
3303179e9639SAndrew Morton 	};
3304a09ed5e0SYing Han 	struct shrink_control shrink = {
3305a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
3306a09ed5e0SYing Han 	};
330715748048SKOSAKI Motohiro 	unsigned long nr_slab_pages0, nr_slab_pages1;
33089eeff239SChristoph Lameter 
33099eeff239SChristoph Lameter 	cond_resched();
3310d4f7796eSChristoph Lameter 	/*
3311d4f7796eSChristoph Lameter 	 * We need to be able to allocate from the reserves for RECLAIM_SWAP
3312d4f7796eSChristoph Lameter 	 * and we also need to be able to write out pages for RECLAIM_WRITE
3313d4f7796eSChristoph Lameter 	 * and RECLAIM_SWAP.
3314d4f7796eSChristoph Lameter 	 */
3315d4f7796eSChristoph Lameter 	p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
331676ca542dSKOSAKI Motohiro 	lockdep_set_current_reclaim_state(gfp_mask);
33179eeff239SChristoph Lameter 	reclaim_state.reclaimed_slab = 0;
33189eeff239SChristoph Lameter 	p->reclaim_state = &reclaim_state;
3319c84db23cSChristoph Lameter 
332090afa5deSMel Gorman 	if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
3321a92f7126SChristoph Lameter 		/*
33220ff38490SChristoph Lameter 		 * Free memory by calling shrink zone with increasing
33230ff38490SChristoph Lameter 		 * priorities until we have enough memory freed.
3324a92f7126SChristoph Lameter 		 */
3325a92f7126SChristoph Lameter 		do {
33269e3b2f8cSKonstantin Khlebnikov 			shrink_zone(zone, &sc);
33279e3b2f8cSKonstantin Khlebnikov 		} while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
33280ff38490SChristoph Lameter 	}
3329a92f7126SChristoph Lameter 
333015748048SKOSAKI Motohiro 	nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
333115748048SKOSAKI Motohiro 	if (nr_slab_pages0 > zone->min_slab_pages) {
33322a16e3f4SChristoph Lameter 		/*
33337fb2d46dSChristoph Lameter 		 * shrink_slab() does not currently allow us to determine how
33340ff38490SChristoph Lameter 		 * many pages were freed in this zone. So we take the current
33350ff38490SChristoph Lameter 		 * number of slab pages and shake the slab until it is reduced
33360ff38490SChristoph Lameter 		 * by the same nr_pages that we used for reclaiming unmapped
33370ff38490SChristoph Lameter 		 * pages.
33382a16e3f4SChristoph Lameter 		 *
33390ff38490SChristoph Lameter 		 * Note that shrink_slab will free memory on all zones and may
33400ff38490SChristoph Lameter 		 * take a long time.
33412a16e3f4SChristoph Lameter 		 */
33424dc4b3d9SKOSAKI Motohiro 		for (;;) {
33434dc4b3d9SKOSAKI Motohiro 			unsigned long lru_pages = zone_reclaimable_pages(zone);
33444dc4b3d9SKOSAKI Motohiro 
33454dc4b3d9SKOSAKI Motohiro 			/* No reclaimable slab or very low memory pressure */
33461495f230SYing Han 			if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages))
33474dc4b3d9SKOSAKI Motohiro 				break;
33484dc4b3d9SKOSAKI Motohiro 
33494dc4b3d9SKOSAKI Motohiro 			/* Freed enough memory */
33504dc4b3d9SKOSAKI Motohiro 			nr_slab_pages1 = zone_page_state(zone,
33514dc4b3d9SKOSAKI Motohiro 							NR_SLAB_RECLAIMABLE);
33524dc4b3d9SKOSAKI Motohiro 			if (nr_slab_pages1 + nr_pages <= nr_slab_pages0)
33534dc4b3d9SKOSAKI Motohiro 				break;
33544dc4b3d9SKOSAKI Motohiro 		}
335583e33a47SChristoph Lameter 
335683e33a47SChristoph Lameter 		/*
335783e33a47SChristoph Lameter 		 * Update nr_reclaimed by the number of slab pages we
335883e33a47SChristoph Lameter 		 * reclaimed from this zone.
335983e33a47SChristoph Lameter 		 */
336015748048SKOSAKI Motohiro 		nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
336115748048SKOSAKI Motohiro 		if (nr_slab_pages1 < nr_slab_pages0)
336215748048SKOSAKI Motohiro 			sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1;
33632a16e3f4SChristoph Lameter 	}
33642a16e3f4SChristoph Lameter 
33659eeff239SChristoph Lameter 	p->reclaim_state = NULL;
3366d4f7796eSChristoph Lameter 	current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
336776ca542dSKOSAKI Motohiro 	lockdep_clear_current_reclaim_state();
3368a79311c1SRik van Riel 	return sc.nr_reclaimed >= nr_pages;
33699eeff239SChristoph Lameter }
3370179e9639SAndrew Morton 
3371179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
3372179e9639SAndrew Morton {
3373179e9639SAndrew Morton 	int node_id;
3374d773ed6bSDavid Rientjes 	int ret;
3375179e9639SAndrew Morton 
3376179e9639SAndrew Morton 	/*
33770ff38490SChristoph Lameter 	 * Zone reclaim reclaims unmapped file backed pages and
33780ff38490SChristoph Lameter 	 * slab pages if we are over the defined limits.
337934aa1330SChristoph Lameter 	 *
33809614634fSChristoph Lameter 	 * A small portion of unmapped file backed pages is needed for
33819614634fSChristoph Lameter 	 * file I/O otherwise pages read by file I/O will be immediately
33829614634fSChristoph Lameter 	 * thrown out if the zone is overallocated. So we do not reclaim
33839614634fSChristoph Lameter 	 * if less than a specified percentage of the zone is used by
33849614634fSChristoph Lameter 	 * unmapped file backed pages.
3385179e9639SAndrew Morton 	 */
338690afa5deSMel Gorman 	if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
338790afa5deSMel Gorman 	    zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
3388fa5e084eSMel Gorman 		return ZONE_RECLAIM_FULL;
3389179e9639SAndrew Morton 
339093e4a89aSKOSAKI Motohiro 	if (zone->all_unreclaimable)
3391fa5e084eSMel Gorman 		return ZONE_RECLAIM_FULL;
3392d773ed6bSDavid Rientjes 
3393179e9639SAndrew Morton 	/*
3394d773ed6bSDavid Rientjes 	 * Do not scan if the allocation should not be delayed.
3395179e9639SAndrew Morton 	 */
3396d773ed6bSDavid Rientjes 	if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
3397fa5e084eSMel Gorman 		return ZONE_RECLAIM_NOSCAN;
3398179e9639SAndrew Morton 
3399179e9639SAndrew Morton 	/*
3400179e9639SAndrew Morton 	 * Only run zone reclaim on the local zone or on zones that do not
3401179e9639SAndrew Morton 	 * have associated processors. This will favor the local processor
3402179e9639SAndrew Morton 	 * over remote processors and spread off node memory allocations
3403179e9639SAndrew Morton 	 * as wide as possible.
3404179e9639SAndrew Morton 	 */
340589fa3024SChristoph Lameter 	node_id = zone_to_nid(zone);
340637c0708dSChristoph Lameter 	if (node_state(node_id, N_CPU) && node_id != numa_node_id())
3407fa5e084eSMel Gorman 		return ZONE_RECLAIM_NOSCAN;
3408d773ed6bSDavid Rientjes 
3409d773ed6bSDavid Rientjes 	if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
3410fa5e084eSMel Gorman 		return ZONE_RECLAIM_NOSCAN;
3411fa5e084eSMel Gorman 
3412d773ed6bSDavid Rientjes 	ret = __zone_reclaim(zone, gfp_mask, order);
3413d773ed6bSDavid Rientjes 	zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
3414d773ed6bSDavid Rientjes 
341524cf7251SMel Gorman 	if (!ret)
341624cf7251SMel Gorman 		count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
341724cf7251SMel Gorman 
3418d773ed6bSDavid Rientjes 	return ret;
3419179e9639SAndrew Morton }
34209eeff239SChristoph Lameter #endif
3421894bc310SLee Schermerhorn 
3422894bc310SLee Schermerhorn /*
3423894bc310SLee Schermerhorn  * page_evictable - test whether a page is evictable
3424894bc310SLee Schermerhorn  * @page: the page to test
3425894bc310SLee Schermerhorn  *
3426894bc310SLee Schermerhorn  * Test whether page is evictable--i.e., should be placed on active/inactive
342739b5f29aSHugh Dickins  * lists vs unevictable list.
3428894bc310SLee Schermerhorn  *
3429894bc310SLee Schermerhorn  * Reasons page might not be evictable:
3430ba9ddf49SLee Schermerhorn  * (1) page's mapping marked unevictable
3431b291f000SNick Piggin  * (2) page is part of an mlocked VMA
3432ba9ddf49SLee Schermerhorn  *
3433894bc310SLee Schermerhorn  */
343439b5f29aSHugh Dickins int page_evictable(struct page *page)
3435894bc310SLee Schermerhorn {
343639b5f29aSHugh Dickins 	return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
3437894bc310SLee Schermerhorn }
343889e004eaSLee Schermerhorn 
343985046579SHugh Dickins #ifdef CONFIG_SHMEM
344089e004eaSLee Schermerhorn /**
344124513264SHugh Dickins  * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list
344224513264SHugh Dickins  * @pages:	array of pages to check
344324513264SHugh Dickins  * @nr_pages:	number of pages to check
344489e004eaSLee Schermerhorn  *
344524513264SHugh Dickins  * Checks pages for evictability and moves them to the appropriate lru list.
344685046579SHugh Dickins  *
344785046579SHugh Dickins  * This function is only used for SysV IPC SHM_UNLOCK.
344889e004eaSLee Schermerhorn  */
344924513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages)
345089e004eaSLee Schermerhorn {
3451925b7673SJohannes Weiner 	struct lruvec *lruvec;
345224513264SHugh Dickins 	struct zone *zone = NULL;
345324513264SHugh Dickins 	int pgscanned = 0;
345424513264SHugh Dickins 	int pgrescued = 0;
345589e004eaSLee Schermerhorn 	int i;
345689e004eaSLee Schermerhorn 
345724513264SHugh Dickins 	for (i = 0; i < nr_pages; i++) {
345824513264SHugh Dickins 		struct page *page = pages[i];
345924513264SHugh Dickins 		struct zone *pagezone;
346089e004eaSLee Schermerhorn 
346124513264SHugh Dickins 		pgscanned++;
346224513264SHugh Dickins 		pagezone = page_zone(page);
346389e004eaSLee Schermerhorn 		if (pagezone != zone) {
346489e004eaSLee Schermerhorn 			if (zone)
346589e004eaSLee Schermerhorn 				spin_unlock_irq(&zone->lru_lock);
346689e004eaSLee Schermerhorn 			zone = pagezone;
346789e004eaSLee Schermerhorn 			spin_lock_irq(&zone->lru_lock);
346889e004eaSLee Schermerhorn 		}
3469fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
347089e004eaSLee Schermerhorn 
347124513264SHugh Dickins 		if (!PageLRU(page) || !PageUnevictable(page))
347224513264SHugh Dickins 			continue;
347389e004eaSLee Schermerhorn 
347439b5f29aSHugh Dickins 		if (page_evictable(page)) {
347524513264SHugh Dickins 			enum lru_list lru = page_lru_base_type(page);
347624513264SHugh Dickins 
347724513264SHugh Dickins 			VM_BUG_ON(PageActive(page));
347824513264SHugh Dickins 			ClearPageUnevictable(page);
3479fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
3480fa9add64SHugh Dickins 			add_page_to_lru_list(page, lruvec, lru);
348124513264SHugh Dickins 			pgrescued++;
348289e004eaSLee Schermerhorn 		}
348389e004eaSLee Schermerhorn 	}
348424513264SHugh Dickins 
348524513264SHugh Dickins 	if (zone) {
348624513264SHugh Dickins 		__count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
348724513264SHugh Dickins 		__count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
348824513264SHugh Dickins 		spin_unlock_irq(&zone->lru_lock);
348924513264SHugh Dickins 	}
349085046579SHugh Dickins }
349185046579SHugh Dickins #endif /* CONFIG_SHMEM */
3492af936a16SLee Schermerhorn 
3493264e56d8SJohannes Weiner static void warn_scan_unevictable_pages(void)
3494af936a16SLee Schermerhorn {
3495264e56d8SJohannes Weiner 	printk_once(KERN_WARNING
349625bd91bdSKOSAKI Motohiro 		    "%s: The scan_unevictable_pages sysctl/node-interface has been "
3497264e56d8SJohannes Weiner 		    "disabled for lack of a legitimate use case.  If you have "
349825bd91bdSKOSAKI Motohiro 		    "one, please send an email to linux-mm@kvack.org.\n",
349925bd91bdSKOSAKI Motohiro 		    current->comm);
3500af936a16SLee Schermerhorn }
3501af936a16SLee Schermerhorn 
3502af936a16SLee Schermerhorn /*
3503af936a16SLee Schermerhorn  * scan_unevictable_pages [vm] sysctl handler.  On demand re-scan of
3504af936a16SLee Schermerhorn  * all nodes' unevictable lists for evictable pages
3505af936a16SLee Schermerhorn  */
3506af936a16SLee Schermerhorn unsigned long scan_unevictable_pages;
3507af936a16SLee Schermerhorn 
3508af936a16SLee Schermerhorn int scan_unevictable_handler(struct ctl_table *table, int write,
35098d65af78SAlexey Dobriyan 			   void __user *buffer,
3510af936a16SLee Schermerhorn 			   size_t *length, loff_t *ppos)
3511af936a16SLee Schermerhorn {
3512264e56d8SJohannes Weiner 	warn_scan_unevictable_pages();
35138d65af78SAlexey Dobriyan 	proc_doulongvec_minmax(table, write, buffer, length, ppos);
3514af936a16SLee Schermerhorn 	scan_unevictable_pages = 0;
3515af936a16SLee Schermerhorn 	return 0;
3516af936a16SLee Schermerhorn }
3517af936a16SLee Schermerhorn 
3518e4455abbSThadeu Lima de Souza Cascardo #ifdef CONFIG_NUMA
3519af936a16SLee Schermerhorn /*
3520af936a16SLee Schermerhorn  * per node 'scan_unevictable_pages' attribute.  On demand re-scan of
3521af936a16SLee Schermerhorn  * a specified node's per zone unevictable lists for evictable pages.
3522af936a16SLee Schermerhorn  */
3523af936a16SLee Schermerhorn 
352410fbcf4cSKay Sievers static ssize_t read_scan_unevictable_node(struct device *dev,
352510fbcf4cSKay Sievers 					  struct device_attribute *attr,
3526af936a16SLee Schermerhorn 					  char *buf)
3527af936a16SLee Schermerhorn {
3528264e56d8SJohannes Weiner 	warn_scan_unevictable_pages();
3529af936a16SLee Schermerhorn 	return sprintf(buf, "0\n");	/* always zero; should fit... */
3530af936a16SLee Schermerhorn }
3531af936a16SLee Schermerhorn 
353210fbcf4cSKay Sievers static ssize_t write_scan_unevictable_node(struct device *dev,
353310fbcf4cSKay Sievers 					   struct device_attribute *attr,
3534af936a16SLee Schermerhorn 					const char *buf, size_t count)
3535af936a16SLee Schermerhorn {
3536264e56d8SJohannes Weiner 	warn_scan_unevictable_pages();
3537af936a16SLee Schermerhorn 	return 1;
3538af936a16SLee Schermerhorn }
3539af936a16SLee Schermerhorn 
3540af936a16SLee Schermerhorn 
354110fbcf4cSKay Sievers static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
3542af936a16SLee Schermerhorn 			read_scan_unevictable_node,
3543af936a16SLee Schermerhorn 			write_scan_unevictable_node);
3544af936a16SLee Schermerhorn 
3545af936a16SLee Schermerhorn int scan_unevictable_register_node(struct node *node)
3546af936a16SLee Schermerhorn {
354710fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages);
3548af936a16SLee Schermerhorn }
3549af936a16SLee Schermerhorn 
3550af936a16SLee Schermerhorn void scan_unevictable_unregister_node(struct node *node)
3551af936a16SLee Schermerhorn {
355210fbcf4cSKay Sievers 	device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages);
3553af936a16SLee Schermerhorn }
3554e4455abbSThadeu Lima de Souza Cascardo #endif
3555