xref: /openbmc/linux/mm/vmscan.c (revision 5bc7b8aca942d03bf2716ddcfcb4e0b57e43a1b8)
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>
2270ddf637SAnton Vorontsov #include <linux/vmpressure.h>
23e129b5c2SAndrew Morton #include <linux/vmstat.h>
241da177e4SLinus Torvalds #include <linux/file.h>
251da177e4SLinus Torvalds #include <linux/writeback.h>
261da177e4SLinus Torvalds #include <linux/blkdev.h>
271da177e4SLinus Torvalds #include <linux/buffer_head.h>	/* for try_to_release_page(),
281da177e4SLinus Torvalds 					buffer_heads_over_limit */
291da177e4SLinus Torvalds #include <linux/mm_inline.h>
301da177e4SLinus Torvalds #include <linux/backing-dev.h>
311da177e4SLinus Torvalds #include <linux/rmap.h>
321da177e4SLinus Torvalds #include <linux/topology.h>
331da177e4SLinus Torvalds #include <linux/cpu.h>
341da177e4SLinus Torvalds #include <linux/cpuset.h>
353e7d3449SMel Gorman #include <linux/compaction.h>
361da177e4SLinus Torvalds #include <linux/notifier.h>
371da177e4SLinus Torvalds #include <linux/rwsem.h>
38248a0301SRafael J. Wysocki #include <linux/delay.h>
393218ae14SYasunori Goto #include <linux/kthread.h>
407dfb7103SNigel Cunningham #include <linux/freezer.h>
4166e1707bSBalbir Singh #include <linux/memcontrol.h>
42873b4771SKeika Kobayashi #include <linux/delayacct.h>
43af936a16SLee Schermerhorn #include <linux/sysctl.h>
44929bea7cSKOSAKI Motohiro #include <linux/oom.h>
45268bb0ceSLinus Torvalds #include <linux/prefetch.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <asm/tlbflush.h>
481da177e4SLinus Torvalds #include <asm/div64.h>
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <linux/swapops.h>
511da177e4SLinus Torvalds 
520f8053a5SNick Piggin #include "internal.h"
530f8053a5SNick Piggin 
5433906bc5SMel Gorman #define CREATE_TRACE_POINTS
5533906bc5SMel Gorman #include <trace/events/vmscan.h>
5633906bc5SMel Gorman 
571da177e4SLinus Torvalds struct scan_control {
581da177e4SLinus Torvalds 	/* Incremented by the number of inactive pages that were scanned */
591da177e4SLinus Torvalds 	unsigned long nr_scanned;
601da177e4SLinus Torvalds 
61a79311c1SRik van Riel 	/* Number of pages freed so far during a call to shrink_zones() */
62a79311c1SRik van Riel 	unsigned long nr_reclaimed;
63a79311c1SRik van Riel 
6422fba335SKOSAKI Motohiro 	/* How many pages shrink_list() should reclaim */
6522fba335SKOSAKI Motohiro 	unsigned long nr_to_reclaim;
6622fba335SKOSAKI Motohiro 
677b51755cSKOSAKI Motohiro 	unsigned long hibernation_mode;
687b51755cSKOSAKI Motohiro 
691da177e4SLinus Torvalds 	/* This context's GFP mask */
706daa0e28SAl Viro 	gfp_t gfp_mask;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	int may_writepage;
731da177e4SLinus Torvalds 
74a6dc60f8SJohannes Weiner 	/* Can mapped pages be reclaimed? */
75a6dc60f8SJohannes Weiner 	int may_unmap;
76f1fd1067SChristoph Lameter 
772e2e4259SKOSAKI Motohiro 	/* Can pages be swapped as part of reclaim? */
782e2e4259SKOSAKI Motohiro 	int may_swap;
792e2e4259SKOSAKI Motohiro 
805ad333ebSAndy Whitcroft 	int order;
8166e1707bSBalbir Singh 
829e3b2f8cSKonstantin Khlebnikov 	/* Scan (total_size >> priority) pages at once */
839e3b2f8cSKonstantin Khlebnikov 	int priority;
849e3b2f8cSKonstantin Khlebnikov 
855f53e762SKOSAKI Motohiro 	/*
86f16015fbSJohannes Weiner 	 * The memory cgroup that hit its limit and as a result is the
87f16015fbSJohannes Weiner 	 * primary target of this reclaim invocation.
88f16015fbSJohannes Weiner 	 */
89f16015fbSJohannes Weiner 	struct mem_cgroup *target_mem_cgroup;
9066e1707bSBalbir Singh 
91327c0e96SKAMEZAWA Hiroyuki 	/*
92327c0e96SKAMEZAWA Hiroyuki 	 * Nodemask of nodes allowed by the caller. If NULL, all nodes
93327c0e96SKAMEZAWA Hiroyuki 	 * are scanned.
94327c0e96SKAMEZAWA Hiroyuki 	 */
95327c0e96SKAMEZAWA Hiroyuki 	nodemask_t	*nodemask;
961da177e4SLinus Torvalds };
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH
1011da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field)			\
1021da177e4SLinus Torvalds 	do {								\
1031da177e4SLinus Torvalds 		if ((_page)->lru.prev != _base) {			\
1041da177e4SLinus Torvalds 			struct page *prev;				\
1051da177e4SLinus Torvalds 									\
1061da177e4SLinus Torvalds 			prev = lru_to_page(&(_page->lru));		\
1071da177e4SLinus Torvalds 			prefetch(&prev->_field);			\
1081da177e4SLinus Torvalds 		}							\
1091da177e4SLinus Torvalds 	} while (0)
1101da177e4SLinus Torvalds #else
1111da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
1121da177e4SLinus Torvalds #endif
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW
1151da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field)			\
1161da177e4SLinus Torvalds 	do {								\
1171da177e4SLinus Torvalds 		if ((_page)->lru.prev != _base) {			\
1181da177e4SLinus Torvalds 			struct page *prev;				\
1191da177e4SLinus Torvalds 									\
1201da177e4SLinus Torvalds 			prev = lru_to_page(&(_page->lru));		\
1211da177e4SLinus Torvalds 			prefetchw(&prev->_field);			\
1221da177e4SLinus Torvalds 		}							\
1231da177e4SLinus Torvalds 	} while (0)
1241da177e4SLinus Torvalds #else
1251da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
1261da177e4SLinus Torvalds #endif
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /*
1291da177e4SLinus Torvalds  * From 0 .. 100.  Higher means more swappy.
1301da177e4SLinus Torvalds  */
1311da177e4SLinus Torvalds int vm_swappiness = 60;
132b21e0b90SZhang Yanfei unsigned long vm_total_pages;	/* The total number of pages which the VM controls */
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds static LIST_HEAD(shrinker_list);
1351da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem);
1361da177e4SLinus Torvalds 
137c255a458SAndrew Morton #ifdef CONFIG_MEMCG
13889b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc)
13989b5fae5SJohannes Weiner {
140f16015fbSJohannes Weiner 	return !sc->target_mem_cgroup;
14189b5fae5SJohannes Weiner }
14291a45470SKAMEZAWA Hiroyuki #else
14389b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc)
14489b5fae5SJohannes Weiner {
14589b5fae5SJohannes Weiner 	return true;
14689b5fae5SJohannes Weiner }
14791a45470SKAMEZAWA Hiroyuki #endif
14891a45470SKAMEZAWA Hiroyuki 
1494d7dcca2SHugh Dickins static unsigned long get_lru_size(struct lruvec *lruvec, enum lru_list lru)
150c9f299d9SKOSAKI Motohiro {
151c3c787e8SHugh Dickins 	if (!mem_cgroup_disabled())
1524d7dcca2SHugh Dickins 		return mem_cgroup_get_lru_size(lruvec, lru);
153a3d8e054SKOSAKI Motohiro 
154074291feSKonstantin Khlebnikov 	return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru);
155c9f299d9SKOSAKI Motohiro }
156c9f299d9SKOSAKI Motohiro 
1571da177e4SLinus Torvalds /*
1581da177e4SLinus Torvalds  * Add a shrinker callback to be called from the vm
1591da177e4SLinus Torvalds  */
1608e1f936bSRusty Russell void register_shrinker(struct shrinker *shrinker)
1611da177e4SLinus Torvalds {
16283aeeadaSKonstantin Khlebnikov 	atomic_long_set(&shrinker->nr_in_batch, 0);
1631da177e4SLinus Torvalds 	down_write(&shrinker_rwsem);
1641da177e4SLinus Torvalds 	list_add_tail(&shrinker->list, &shrinker_list);
1651da177e4SLinus Torvalds 	up_write(&shrinker_rwsem);
1661da177e4SLinus Torvalds }
1678e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds /*
1701da177e4SLinus Torvalds  * Remove one
1711da177e4SLinus Torvalds  */
1728e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	down_write(&shrinker_rwsem);
1751da177e4SLinus Torvalds 	list_del(&shrinker->list);
1761da177e4SLinus Torvalds 	up_write(&shrinker_rwsem);
1771da177e4SLinus Torvalds }
1788e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker);
1791da177e4SLinus Torvalds 
1801495f230SYing Han static inline int do_shrinker_shrink(struct shrinker *shrinker,
1811495f230SYing Han 				     struct shrink_control *sc,
1821495f230SYing Han 				     unsigned long nr_to_scan)
1831495f230SYing Han {
1841495f230SYing Han 	sc->nr_to_scan = nr_to_scan;
1851495f230SYing Han 	return (*shrinker->shrink)(shrinker, sc);
1861495f230SYing Han }
1871495f230SYing Han 
1881da177e4SLinus Torvalds #define SHRINK_BATCH 128
1891da177e4SLinus Torvalds /*
1901da177e4SLinus Torvalds  * Call the shrink functions to age shrinkable caches
1911da177e4SLinus Torvalds  *
1921da177e4SLinus Torvalds  * Here we assume it costs one seek to replace a lru page and that it also
1931da177e4SLinus Torvalds  * takes a seek to recreate a cache object.  With this in mind we age equal
1941da177e4SLinus Torvalds  * percentages of the lru and ageable caches.  This should balance the seeks
1951da177e4SLinus Torvalds  * generated by these structures.
1961da177e4SLinus Torvalds  *
197183ff22bSSimon Arlott  * If the vm encountered mapped pages on the LRU it increase the pressure on
1981da177e4SLinus Torvalds  * slab to avoid swapping.
1991da177e4SLinus Torvalds  *
2001da177e4SLinus Torvalds  * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
2011da177e4SLinus Torvalds  *
2021da177e4SLinus Torvalds  * `lru_pages' represents the number of on-LRU pages in all the zones which
2031da177e4SLinus Torvalds  * are eligible for the caller's allocation attempt.  It is used for balancing
2041da177e4SLinus Torvalds  * slab reclaim versus page reclaim.
205b15e0905Sakpm@osdl.org  *
206b15e0905Sakpm@osdl.org  * Returns the number of slab objects which we shrunk.
2071da177e4SLinus Torvalds  */
208a09ed5e0SYing Han unsigned long shrink_slab(struct shrink_control *shrink,
2091495f230SYing Han 			  unsigned long nr_pages_scanned,
21069e05944SAndrew Morton 			  unsigned long lru_pages)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	struct shrinker *shrinker;
21369e05944SAndrew Morton 	unsigned long ret = 0;
2141da177e4SLinus Torvalds 
2151495f230SYing Han 	if (nr_pages_scanned == 0)
2161495f230SYing Han 		nr_pages_scanned = SWAP_CLUSTER_MAX;
2171da177e4SLinus Torvalds 
218f06590bdSMinchan Kim 	if (!down_read_trylock(&shrinker_rwsem)) {
219f06590bdSMinchan Kim 		/* Assume we'll be able to shrink next time */
220f06590bdSMinchan Kim 		ret = 1;
221f06590bdSMinchan Kim 		goto out;
222f06590bdSMinchan Kim 	}
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	list_for_each_entry(shrinker, &shrinker_list, list) {
2251da177e4SLinus Torvalds 		unsigned long long delta;
226635697c6SKonstantin Khlebnikov 		long total_scan;
227635697c6SKonstantin Khlebnikov 		long max_pass;
22809576073SDave Chinner 		int shrink_ret = 0;
229acf92b48SDave Chinner 		long nr;
230acf92b48SDave Chinner 		long new_nr;
231e9299f50SDave Chinner 		long batch_size = shrinker->batch ? shrinker->batch
232e9299f50SDave Chinner 						  : SHRINK_BATCH;
2331da177e4SLinus Torvalds 
234635697c6SKonstantin Khlebnikov 		max_pass = do_shrinker_shrink(shrinker, shrink, 0);
235635697c6SKonstantin Khlebnikov 		if (max_pass <= 0)
236635697c6SKonstantin Khlebnikov 			continue;
237635697c6SKonstantin Khlebnikov 
238acf92b48SDave Chinner 		/*
239acf92b48SDave Chinner 		 * copy the current shrinker scan count into a local variable
240acf92b48SDave Chinner 		 * and zero it so that other concurrent shrinker invocations
241acf92b48SDave Chinner 		 * don't also do this scanning work.
242acf92b48SDave Chinner 		 */
24383aeeadaSKonstantin Khlebnikov 		nr = atomic_long_xchg(&shrinker->nr_in_batch, 0);
244acf92b48SDave Chinner 
245acf92b48SDave Chinner 		total_scan = nr;
2461495f230SYing Han 		delta = (4 * nr_pages_scanned) / shrinker->seeks;
247ea164d73SAndrea Arcangeli 		delta *= max_pass;
2481da177e4SLinus Torvalds 		do_div(delta, lru_pages + 1);
249acf92b48SDave Chinner 		total_scan += delta;
250acf92b48SDave Chinner 		if (total_scan < 0) {
25188c3bd70SDavid Rientjes 			printk(KERN_ERR "shrink_slab: %pF negative objects to "
25288c3bd70SDavid Rientjes 			       "delete nr=%ld\n",
253acf92b48SDave Chinner 			       shrinker->shrink, total_scan);
254acf92b48SDave Chinner 			total_scan = max_pass;
255ea164d73SAndrea Arcangeli 		}
256ea164d73SAndrea Arcangeli 
257ea164d73SAndrea Arcangeli 		/*
2583567b59aSDave Chinner 		 * We need to avoid excessive windup on filesystem shrinkers
2593567b59aSDave Chinner 		 * due to large numbers of GFP_NOFS allocations causing the
2603567b59aSDave Chinner 		 * shrinkers to return -1 all the time. This results in a large
2613567b59aSDave Chinner 		 * nr being built up so when a shrink that can do some work
2623567b59aSDave Chinner 		 * comes along it empties the entire cache due to nr >>>
2633567b59aSDave Chinner 		 * max_pass.  This is bad for sustaining a working set in
2643567b59aSDave Chinner 		 * memory.
2653567b59aSDave Chinner 		 *
2663567b59aSDave Chinner 		 * Hence only allow the shrinker to scan the entire cache when
2673567b59aSDave Chinner 		 * a large delta change is calculated directly.
2683567b59aSDave Chinner 		 */
2693567b59aSDave Chinner 		if (delta < max_pass / 4)
2703567b59aSDave Chinner 			total_scan = min(total_scan, max_pass / 2);
2713567b59aSDave Chinner 
2723567b59aSDave Chinner 		/*
273ea164d73SAndrea Arcangeli 		 * Avoid risking looping forever due to too large nr value:
274ea164d73SAndrea Arcangeli 		 * never try to free more than twice the estimate number of
275ea164d73SAndrea Arcangeli 		 * freeable entries.
276ea164d73SAndrea Arcangeli 		 */
277acf92b48SDave Chinner 		if (total_scan > max_pass * 2)
278acf92b48SDave Chinner 			total_scan = max_pass * 2;
2791da177e4SLinus Torvalds 
280acf92b48SDave Chinner 		trace_mm_shrink_slab_start(shrinker, shrink, nr,
28109576073SDave Chinner 					nr_pages_scanned, lru_pages,
28209576073SDave Chinner 					max_pass, delta, total_scan);
28309576073SDave Chinner 
284e9299f50SDave Chinner 		while (total_scan >= batch_size) {
285b15e0905Sakpm@osdl.org 			int nr_before;
2861da177e4SLinus Torvalds 
2871495f230SYing Han 			nr_before = do_shrinker_shrink(shrinker, shrink, 0);
2881495f230SYing Han 			shrink_ret = do_shrinker_shrink(shrinker, shrink,
289e9299f50SDave Chinner 							batch_size);
2901da177e4SLinus Torvalds 			if (shrink_ret == -1)
2911da177e4SLinus Torvalds 				break;
292b15e0905Sakpm@osdl.org 			if (shrink_ret < nr_before)
293b15e0905Sakpm@osdl.org 				ret += nr_before - shrink_ret;
294e9299f50SDave Chinner 			count_vm_events(SLABS_SCANNED, batch_size);
295e9299f50SDave Chinner 			total_scan -= batch_size;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 			cond_resched();
2981da177e4SLinus Torvalds 		}
2991da177e4SLinus Torvalds 
300acf92b48SDave Chinner 		/*
301acf92b48SDave Chinner 		 * move the unused scan count back into the shrinker in a
302acf92b48SDave Chinner 		 * manner that handles concurrent updates. If we exhausted the
303acf92b48SDave Chinner 		 * scan, there is no need to do an update.
304acf92b48SDave Chinner 		 */
30583aeeadaSKonstantin Khlebnikov 		if (total_scan > 0)
30683aeeadaSKonstantin Khlebnikov 			new_nr = atomic_long_add_return(total_scan,
30783aeeadaSKonstantin Khlebnikov 					&shrinker->nr_in_batch);
30883aeeadaSKonstantin Khlebnikov 		else
30983aeeadaSKonstantin Khlebnikov 			new_nr = atomic_long_read(&shrinker->nr_in_batch);
310acf92b48SDave Chinner 
311acf92b48SDave Chinner 		trace_mm_shrink_slab_end(shrinker, shrink_ret, nr, new_nr);
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 	up_read(&shrinker_rwsem);
314f06590bdSMinchan Kim out:
315f06590bdSMinchan Kim 	cond_resched();
316b15e0905Sakpm@osdl.org 	return ret;
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page)
3201da177e4SLinus Torvalds {
321ceddc3a5SJohannes Weiner 	/*
322ceddc3a5SJohannes Weiner 	 * A freeable page cache page is referenced only by the caller
323ceddc3a5SJohannes Weiner 	 * that isolated the page, the page cache radix tree and
324ceddc3a5SJohannes Weiner 	 * optional buffer heads at page->private.
325ceddc3a5SJohannes Weiner 	 */
326edcf4748SJohannes Weiner 	return page_count(page) - page_has_private(page) == 2;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3297d3579e8SKOSAKI Motohiro static int may_write_to_queue(struct backing_dev_info *bdi,
3307d3579e8SKOSAKI Motohiro 			      struct scan_control *sc)
3311da177e4SLinus Torvalds {
332930d9152SChristoph Lameter 	if (current->flags & PF_SWAPWRITE)
3331da177e4SLinus Torvalds 		return 1;
3341da177e4SLinus Torvalds 	if (!bdi_write_congested(bdi))
3351da177e4SLinus Torvalds 		return 1;
3361da177e4SLinus Torvalds 	if (bdi == current->backing_dev_info)
3371da177e4SLinus Torvalds 		return 1;
3381da177e4SLinus Torvalds 	return 0;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds /*
3421da177e4SLinus Torvalds  * We detected a synchronous write error writing a page out.  Probably
3431da177e4SLinus Torvalds  * -ENOSPC.  We need to propagate that into the address_space for a subsequent
3441da177e4SLinus Torvalds  * fsync(), msync() or close().
3451da177e4SLinus Torvalds  *
3461da177e4SLinus Torvalds  * The tricky part is that after writepage we cannot touch the mapping: nothing
3471da177e4SLinus Torvalds  * prevents it from being freed up.  But we have a ref on the page and once
3481da177e4SLinus Torvalds  * that page is locked, the mapping is pinned.
3491da177e4SLinus Torvalds  *
3501da177e4SLinus Torvalds  * We're allowed to run sleeping lock_page() here because we know the caller has
3511da177e4SLinus Torvalds  * __GFP_FS.
3521da177e4SLinus Torvalds  */
3531da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping,
3541da177e4SLinus Torvalds 				struct page *page, int error)
3551da177e4SLinus Torvalds {
3567eaceaccSJens Axboe 	lock_page(page);
3573e9f45bdSGuillaume Chazarain 	if (page_mapping(page) == mapping)
3583e9f45bdSGuillaume Chazarain 		mapping_set_error(mapping, error);
3591da177e4SLinus Torvalds 	unlock_page(page);
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
36204e62a29SChristoph Lameter /* possible outcome of pageout() */
36304e62a29SChristoph Lameter typedef enum {
36404e62a29SChristoph Lameter 	/* failed to write page out, page is locked */
36504e62a29SChristoph Lameter 	PAGE_KEEP,
36604e62a29SChristoph Lameter 	/* move page to the active list, page is locked */
36704e62a29SChristoph Lameter 	PAGE_ACTIVATE,
36804e62a29SChristoph Lameter 	/* page has been sent to the disk successfully, page is unlocked */
36904e62a29SChristoph Lameter 	PAGE_SUCCESS,
37004e62a29SChristoph Lameter 	/* page is clean and locked */
37104e62a29SChristoph Lameter 	PAGE_CLEAN,
37204e62a29SChristoph Lameter } pageout_t;
37304e62a29SChristoph Lameter 
3741da177e4SLinus Torvalds /*
3751742f19fSAndrew Morton  * pageout is called by shrink_page_list() for each dirty page.
3761742f19fSAndrew Morton  * Calls ->writepage().
3771da177e4SLinus Torvalds  */
378c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping,
3797d3579e8SKOSAKI Motohiro 			 struct scan_control *sc)
3801da177e4SLinus Torvalds {
3811da177e4SLinus Torvalds 	/*
3821da177e4SLinus Torvalds 	 * If the page is dirty, only perform writeback if that write
3831da177e4SLinus Torvalds 	 * will be non-blocking.  To prevent this allocation from being
3841da177e4SLinus Torvalds 	 * stalled by pagecache activity.  But note that there may be
3851da177e4SLinus Torvalds 	 * stalls if we need to run get_block().  We could test
3861da177e4SLinus Torvalds 	 * PagePrivate for that.
3871da177e4SLinus Torvalds 	 *
3886aceb53bSVincent Li 	 * If this process is currently in __generic_file_aio_write() against
3891da177e4SLinus Torvalds 	 * this page's queue, we can perform writeback even if that
3901da177e4SLinus Torvalds 	 * will block.
3911da177e4SLinus Torvalds 	 *
3921da177e4SLinus Torvalds 	 * If the page is swapcache, write it back even if that would
3931da177e4SLinus Torvalds 	 * block, for some throttling. This happens by accident, because
3941da177e4SLinus Torvalds 	 * swap_backing_dev_info is bust: it doesn't reflect the
3951da177e4SLinus Torvalds 	 * congestion state of the swapdevs.  Easy to fix, if needed.
3961da177e4SLinus Torvalds 	 */
3971da177e4SLinus Torvalds 	if (!is_page_cache_freeable(page))
3981da177e4SLinus Torvalds 		return PAGE_KEEP;
3991da177e4SLinus Torvalds 	if (!mapping) {
4001da177e4SLinus Torvalds 		/*
4011da177e4SLinus Torvalds 		 * Some data journaling orphaned pages can have
4021da177e4SLinus Torvalds 		 * page->mapping == NULL while being dirty with clean buffers.
4031da177e4SLinus Torvalds 		 */
404266cf658SDavid Howells 		if (page_has_private(page)) {
4051da177e4SLinus Torvalds 			if (try_to_free_buffers(page)) {
4061da177e4SLinus Torvalds 				ClearPageDirty(page);
407d40cee24SHarvey Harrison 				printk("%s: orphaned page\n", __func__);
4081da177e4SLinus Torvalds 				return PAGE_CLEAN;
4091da177e4SLinus Torvalds 			}
4101da177e4SLinus Torvalds 		}
4111da177e4SLinus Torvalds 		return PAGE_KEEP;
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds 	if (mapping->a_ops->writepage == NULL)
4141da177e4SLinus Torvalds 		return PAGE_ACTIVATE;
4150e093d99SMel Gorman 	if (!may_write_to_queue(mapping->backing_dev_info, sc))
4161da177e4SLinus Torvalds 		return PAGE_KEEP;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	if (clear_page_dirty_for_io(page)) {
4191da177e4SLinus Torvalds 		int res;
4201da177e4SLinus Torvalds 		struct writeback_control wbc = {
4211da177e4SLinus Torvalds 			.sync_mode = WB_SYNC_NONE,
4221da177e4SLinus Torvalds 			.nr_to_write = SWAP_CLUSTER_MAX,
423111ebb6eSOGAWA Hirofumi 			.range_start = 0,
424111ebb6eSOGAWA Hirofumi 			.range_end = LLONG_MAX,
4251da177e4SLinus Torvalds 			.for_reclaim = 1,
4261da177e4SLinus Torvalds 		};
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 		SetPageReclaim(page);
4291da177e4SLinus Torvalds 		res = mapping->a_ops->writepage(page, &wbc);
4301da177e4SLinus Torvalds 		if (res < 0)
4311da177e4SLinus Torvalds 			handle_write_error(mapping, page, res);
432994fc28cSZach Brown 		if (res == AOP_WRITEPAGE_ACTIVATE) {
4331da177e4SLinus Torvalds 			ClearPageReclaim(page);
4341da177e4SLinus Torvalds 			return PAGE_ACTIVATE;
4351da177e4SLinus Torvalds 		}
436c661b078SAndy Whitcroft 
4371da177e4SLinus Torvalds 		if (!PageWriteback(page)) {
4381da177e4SLinus Torvalds 			/* synchronous write or broken a_ops? */
4391da177e4SLinus Torvalds 			ClearPageReclaim(page);
4401da177e4SLinus Torvalds 		}
44123b9da55SMel Gorman 		trace_mm_vmscan_writepage(page, trace_reclaim_flags(page));
442e129b5c2SAndrew Morton 		inc_zone_page_state(page, NR_VMSCAN_WRITE);
4431da177e4SLinus Torvalds 		return PAGE_SUCCESS;
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	return PAGE_CLEAN;
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
449a649fd92SAndrew Morton /*
450e286781dSNick Piggin  * Same as remove_mapping, but if the page is removed from the mapping, it
451e286781dSNick Piggin  * gets returned with a refcount of 0.
452a649fd92SAndrew Morton  */
453e286781dSNick Piggin static int __remove_mapping(struct address_space *mapping, struct page *page)
45449d2e9ccSChristoph Lameter {
45528e4d965SNick Piggin 	BUG_ON(!PageLocked(page));
45628e4d965SNick Piggin 	BUG_ON(mapping != page_mapping(page));
45749d2e9ccSChristoph Lameter 
45819fd6231SNick Piggin 	spin_lock_irq(&mapping->tree_lock);
45949d2e9ccSChristoph Lameter 	/*
4600fd0e6b0SNick Piggin 	 * The non racy check for a busy page.
4610fd0e6b0SNick Piggin 	 *
4620fd0e6b0SNick Piggin 	 * Must be careful with the order of the tests. When someone has
4630fd0e6b0SNick Piggin 	 * a ref to the page, it may be possible that they dirty it then
4640fd0e6b0SNick Piggin 	 * drop the reference. So if PageDirty is tested before page_count
4650fd0e6b0SNick Piggin 	 * here, then the following race may occur:
4660fd0e6b0SNick Piggin 	 *
4670fd0e6b0SNick Piggin 	 * get_user_pages(&page);
4680fd0e6b0SNick Piggin 	 * [user mapping goes away]
4690fd0e6b0SNick Piggin 	 * write_to(page);
4700fd0e6b0SNick Piggin 	 *				!PageDirty(page)    [good]
4710fd0e6b0SNick Piggin 	 * SetPageDirty(page);
4720fd0e6b0SNick Piggin 	 * put_page(page);
4730fd0e6b0SNick Piggin 	 *				!page_count(page)   [good, discard it]
4740fd0e6b0SNick Piggin 	 *
4750fd0e6b0SNick Piggin 	 * [oops, our write_to data is lost]
4760fd0e6b0SNick Piggin 	 *
4770fd0e6b0SNick Piggin 	 * Reversing the order of the tests ensures such a situation cannot
4780fd0e6b0SNick Piggin 	 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
4790fd0e6b0SNick Piggin 	 * load is not satisfied before that of page->_count.
4800fd0e6b0SNick Piggin 	 *
4810fd0e6b0SNick Piggin 	 * Note that if SetPageDirty is always performed via set_page_dirty,
4820fd0e6b0SNick Piggin 	 * and thus under tree_lock, then this ordering is not required.
48349d2e9ccSChristoph Lameter 	 */
484e286781dSNick Piggin 	if (!page_freeze_refs(page, 2))
48549d2e9ccSChristoph Lameter 		goto cannot_free;
486e286781dSNick Piggin 	/* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
487e286781dSNick Piggin 	if (unlikely(PageDirty(page))) {
488e286781dSNick Piggin 		page_unfreeze_refs(page, 2);
48949d2e9ccSChristoph Lameter 		goto cannot_free;
490e286781dSNick Piggin 	}
49149d2e9ccSChristoph Lameter 
49249d2e9ccSChristoph Lameter 	if (PageSwapCache(page)) {
49349d2e9ccSChristoph Lameter 		swp_entry_t swap = { .val = page_private(page) };
49449d2e9ccSChristoph Lameter 		__delete_from_swap_cache(page);
49519fd6231SNick Piggin 		spin_unlock_irq(&mapping->tree_lock);
496cb4b86baSKAMEZAWA Hiroyuki 		swapcache_free(swap, page);
497e286781dSNick Piggin 	} else {
4986072d13cSLinus Torvalds 		void (*freepage)(struct page *);
4996072d13cSLinus Torvalds 
5006072d13cSLinus Torvalds 		freepage = mapping->a_ops->freepage;
5016072d13cSLinus Torvalds 
502e64a782fSMinchan Kim 		__delete_from_page_cache(page);
50319fd6231SNick Piggin 		spin_unlock_irq(&mapping->tree_lock);
504e767e056SDaisuke Nishimura 		mem_cgroup_uncharge_cache_page(page);
5056072d13cSLinus Torvalds 
5066072d13cSLinus Torvalds 		if (freepage != NULL)
5076072d13cSLinus Torvalds 			freepage(page);
508e286781dSNick Piggin 	}
509e286781dSNick Piggin 
51049d2e9ccSChristoph Lameter 	return 1;
51149d2e9ccSChristoph Lameter 
51249d2e9ccSChristoph Lameter cannot_free:
51319fd6231SNick Piggin 	spin_unlock_irq(&mapping->tree_lock);
51449d2e9ccSChristoph Lameter 	return 0;
51549d2e9ccSChristoph Lameter }
51649d2e9ccSChristoph Lameter 
5171da177e4SLinus Torvalds /*
518e286781dSNick Piggin  * Attempt to detach a locked page from its ->mapping.  If it is dirty or if
519e286781dSNick Piggin  * someone else has a ref on the page, abort and return 0.  If it was
520e286781dSNick Piggin  * successfully detached, return 1.  Assumes the caller has a single ref on
521e286781dSNick Piggin  * this page.
522e286781dSNick Piggin  */
523e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page)
524e286781dSNick Piggin {
525e286781dSNick Piggin 	if (__remove_mapping(mapping, page)) {
526e286781dSNick Piggin 		/*
527e286781dSNick Piggin 		 * Unfreezing the refcount with 1 rather than 2 effectively
528e286781dSNick Piggin 		 * drops the pagecache ref for us without requiring another
529e286781dSNick Piggin 		 * atomic operation.
530e286781dSNick Piggin 		 */
531e286781dSNick Piggin 		page_unfreeze_refs(page, 1);
532e286781dSNick Piggin 		return 1;
533e286781dSNick Piggin 	}
534e286781dSNick Piggin 	return 0;
535e286781dSNick Piggin }
536e286781dSNick Piggin 
537894bc310SLee Schermerhorn /**
538894bc310SLee Schermerhorn  * putback_lru_page - put previously isolated page onto appropriate LRU list
539894bc310SLee Schermerhorn  * @page: page to be put back to appropriate lru list
540894bc310SLee Schermerhorn  *
541894bc310SLee Schermerhorn  * Add previously isolated @page to appropriate LRU list.
542894bc310SLee Schermerhorn  * Page may still be unevictable for other reasons.
543894bc310SLee Schermerhorn  *
544894bc310SLee Schermerhorn  * lru_lock must not be held, interrupts must be enabled.
545894bc310SLee Schermerhorn  */
546894bc310SLee Schermerhorn void putback_lru_page(struct page *page)
547894bc310SLee Schermerhorn {
548894bc310SLee Schermerhorn 	int lru;
549894bc310SLee Schermerhorn 	int active = !!TestClearPageActive(page);
550bbfd28eeSLee Schermerhorn 	int was_unevictable = PageUnevictable(page);
551894bc310SLee Schermerhorn 
552894bc310SLee Schermerhorn 	VM_BUG_ON(PageLRU(page));
553894bc310SLee Schermerhorn 
554894bc310SLee Schermerhorn redo:
555894bc310SLee Schermerhorn 	ClearPageUnevictable(page);
556894bc310SLee Schermerhorn 
55739b5f29aSHugh Dickins 	if (page_evictable(page)) {
558894bc310SLee Schermerhorn 		/*
559894bc310SLee Schermerhorn 		 * For evictable pages, we can use the cache.
560894bc310SLee Schermerhorn 		 * In event of a race, worst case is we end up with an
561894bc310SLee Schermerhorn 		 * unevictable page on [in]active list.
562894bc310SLee Schermerhorn 		 * We know how to handle that.
563894bc310SLee Schermerhorn 		 */
564401a8e1cSJohannes Weiner 		lru = active + page_lru_base_type(page);
565894bc310SLee Schermerhorn 		lru_cache_add_lru(page, lru);
566894bc310SLee Schermerhorn 	} else {
567894bc310SLee Schermerhorn 		/*
568894bc310SLee Schermerhorn 		 * Put unevictable pages directly on zone's unevictable
569894bc310SLee Schermerhorn 		 * list.
570894bc310SLee Schermerhorn 		 */
571894bc310SLee Schermerhorn 		lru = LRU_UNEVICTABLE;
572894bc310SLee Schermerhorn 		add_page_to_unevictable_list(page);
5736a7b9548SJohannes Weiner 		/*
57421ee9f39SMinchan Kim 		 * When racing with an mlock or AS_UNEVICTABLE clearing
57521ee9f39SMinchan Kim 		 * (page is unlocked) make sure that if the other thread
57621ee9f39SMinchan Kim 		 * does not observe our setting of PG_lru and fails
57724513264SHugh Dickins 		 * isolation/check_move_unevictable_pages,
57821ee9f39SMinchan Kim 		 * we see PG_mlocked/AS_UNEVICTABLE cleared below and move
5796a7b9548SJohannes Weiner 		 * the page back to the evictable list.
5806a7b9548SJohannes Weiner 		 *
58121ee9f39SMinchan Kim 		 * The other side is TestClearPageMlocked() or shmem_lock().
5826a7b9548SJohannes Weiner 		 */
5836a7b9548SJohannes Weiner 		smp_mb();
584894bc310SLee Schermerhorn 	}
585894bc310SLee Schermerhorn 
586894bc310SLee Schermerhorn 	/*
587894bc310SLee Schermerhorn 	 * page's status can change while we move it among lru. If an evictable
588894bc310SLee Schermerhorn 	 * page is on unevictable list, it never be freed. To avoid that,
589894bc310SLee Schermerhorn 	 * check after we added it to the list, again.
590894bc310SLee Schermerhorn 	 */
59139b5f29aSHugh Dickins 	if (lru == LRU_UNEVICTABLE && page_evictable(page)) {
592894bc310SLee Schermerhorn 		if (!isolate_lru_page(page)) {
593894bc310SLee Schermerhorn 			put_page(page);
594894bc310SLee Schermerhorn 			goto redo;
595894bc310SLee Schermerhorn 		}
596894bc310SLee Schermerhorn 		/* This means someone else dropped this page from LRU
597894bc310SLee Schermerhorn 		 * So, it will be freed or putback to LRU again. There is
598894bc310SLee Schermerhorn 		 * nothing to do here.
599894bc310SLee Schermerhorn 		 */
600894bc310SLee Schermerhorn 	}
601894bc310SLee Schermerhorn 
602bbfd28eeSLee Schermerhorn 	if (was_unevictable && lru != LRU_UNEVICTABLE)
603bbfd28eeSLee Schermerhorn 		count_vm_event(UNEVICTABLE_PGRESCUED);
604bbfd28eeSLee Schermerhorn 	else if (!was_unevictable && lru == LRU_UNEVICTABLE)
605bbfd28eeSLee Schermerhorn 		count_vm_event(UNEVICTABLE_PGCULLED);
606bbfd28eeSLee Schermerhorn 
607894bc310SLee Schermerhorn 	put_page(page);		/* drop ref from isolate */
608894bc310SLee Schermerhorn }
609894bc310SLee Schermerhorn 
610dfc8d636SJohannes Weiner enum page_references {
611dfc8d636SJohannes Weiner 	PAGEREF_RECLAIM,
612dfc8d636SJohannes Weiner 	PAGEREF_RECLAIM_CLEAN,
61364574746SJohannes Weiner 	PAGEREF_KEEP,
614dfc8d636SJohannes Weiner 	PAGEREF_ACTIVATE,
615dfc8d636SJohannes Weiner };
616dfc8d636SJohannes Weiner 
617dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page,
618dfc8d636SJohannes Weiner 						  struct scan_control *sc)
619dfc8d636SJohannes Weiner {
62064574746SJohannes Weiner 	int referenced_ptes, referenced_page;
621dfc8d636SJohannes Weiner 	unsigned long vm_flags;
622dfc8d636SJohannes Weiner 
623c3ac9a8aSJohannes Weiner 	referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup,
624c3ac9a8aSJohannes Weiner 					  &vm_flags);
62564574746SJohannes Weiner 	referenced_page = TestClearPageReferenced(page);
626dfc8d636SJohannes Weiner 
627dfc8d636SJohannes Weiner 	/*
628dfc8d636SJohannes Weiner 	 * Mlock lost the isolation race with us.  Let try_to_unmap()
629dfc8d636SJohannes Weiner 	 * move the page to the unevictable list.
630dfc8d636SJohannes Weiner 	 */
631dfc8d636SJohannes Weiner 	if (vm_flags & VM_LOCKED)
632dfc8d636SJohannes Weiner 		return PAGEREF_RECLAIM;
633dfc8d636SJohannes Weiner 
63464574746SJohannes Weiner 	if (referenced_ptes) {
635e4898273SMichal Hocko 		if (PageSwapBacked(page))
63664574746SJohannes Weiner 			return PAGEREF_ACTIVATE;
63764574746SJohannes Weiner 		/*
63864574746SJohannes Weiner 		 * All mapped pages start out with page table
63964574746SJohannes Weiner 		 * references from the instantiating fault, so we need
64064574746SJohannes Weiner 		 * to look twice if a mapped file page is used more
64164574746SJohannes Weiner 		 * than once.
64264574746SJohannes Weiner 		 *
64364574746SJohannes Weiner 		 * Mark it and spare it for another trip around the
64464574746SJohannes Weiner 		 * inactive list.  Another page table reference will
64564574746SJohannes Weiner 		 * lead to its activation.
64664574746SJohannes Weiner 		 *
64764574746SJohannes Weiner 		 * Note: the mark is set for activated pages as well
64864574746SJohannes Weiner 		 * so that recently deactivated but used pages are
64964574746SJohannes Weiner 		 * quickly recovered.
65064574746SJohannes Weiner 		 */
65164574746SJohannes Weiner 		SetPageReferenced(page);
65264574746SJohannes Weiner 
65334dbc67aSKonstantin Khlebnikov 		if (referenced_page || referenced_ptes > 1)
654dfc8d636SJohannes Weiner 			return PAGEREF_ACTIVATE;
655dfc8d636SJohannes Weiner 
656c909e993SKonstantin Khlebnikov 		/*
657c909e993SKonstantin Khlebnikov 		 * Activate file-backed executable pages after first usage.
658c909e993SKonstantin Khlebnikov 		 */
659c909e993SKonstantin Khlebnikov 		if (vm_flags & VM_EXEC)
660c909e993SKonstantin Khlebnikov 			return PAGEREF_ACTIVATE;
661c909e993SKonstantin Khlebnikov 
66264574746SJohannes Weiner 		return PAGEREF_KEEP;
66364574746SJohannes Weiner 	}
66464574746SJohannes Weiner 
665dfc8d636SJohannes Weiner 	/* Reclaim if clean, defer dirty pages to writeback */
6662e30244aSKOSAKI Motohiro 	if (referenced_page && !PageSwapBacked(page))
667dfc8d636SJohannes Weiner 		return PAGEREF_RECLAIM_CLEAN;
66864574746SJohannes Weiner 
66964574746SJohannes Weiner 	return PAGEREF_RECLAIM;
670dfc8d636SJohannes Weiner }
671dfc8d636SJohannes Weiner 
672e286781dSNick Piggin /*
6731742f19fSAndrew Morton  * shrink_page_list() returns the number of reclaimed pages
6741da177e4SLinus Torvalds  */
6751742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list,
6766a18adb3SKonstantin Khlebnikov 				      struct zone *zone,
677f84f6e2bSMel Gorman 				      struct scan_control *sc,
67802c6de8dSMinchan Kim 				      enum ttu_flags ttu_flags,
67992df3a72SMel Gorman 				      unsigned long *ret_nr_dirty,
68002c6de8dSMinchan Kim 				      unsigned long *ret_nr_writeback,
68102c6de8dSMinchan Kim 				      bool force_reclaim)
6821da177e4SLinus Torvalds {
6831da177e4SLinus Torvalds 	LIST_HEAD(ret_pages);
684abe4c3b5SMel Gorman 	LIST_HEAD(free_pages);
6851da177e4SLinus Torvalds 	int pgactivate = 0;
6860e093d99SMel Gorman 	unsigned long nr_dirty = 0;
6870e093d99SMel Gorman 	unsigned long nr_congested = 0;
68805ff5137SAndrew Morton 	unsigned long nr_reclaimed = 0;
68992df3a72SMel Gorman 	unsigned long nr_writeback = 0;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	cond_resched();
6921da177e4SLinus Torvalds 
69369980e31STim Chen 	mem_cgroup_uncharge_start();
6941da177e4SLinus Torvalds 	while (!list_empty(page_list)) {
6951da177e4SLinus Torvalds 		struct address_space *mapping;
6961da177e4SLinus Torvalds 		struct page *page;
6971da177e4SLinus Torvalds 		int may_enter_fs;
69802c6de8dSMinchan Kim 		enum page_references references = PAGEREF_RECLAIM_CLEAN;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 		cond_resched();
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 		page = lru_to_page(page_list);
7031da177e4SLinus Torvalds 		list_del(&page->lru);
7041da177e4SLinus Torvalds 
705529ae9aaSNick Piggin 		if (!trylock_page(page))
7061da177e4SLinus Torvalds 			goto keep;
7071da177e4SLinus Torvalds 
708725d704eSNick Piggin 		VM_BUG_ON(PageActive(page));
7096a18adb3SKonstantin Khlebnikov 		VM_BUG_ON(page_zone(page) != zone);
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 		sc->nr_scanned++;
71280e43426SChristoph Lameter 
71339b5f29aSHugh Dickins 		if (unlikely(!page_evictable(page)))
714b291f000SNick Piggin 			goto cull_mlocked;
715894bc310SLee Schermerhorn 
716a6dc60f8SJohannes Weiner 		if (!sc->may_unmap && page_mapped(page))
71780e43426SChristoph Lameter 			goto keep_locked;
71880e43426SChristoph Lameter 
7191da177e4SLinus Torvalds 		/* Double the slab pressure for mapped and swapcache pages */
7201da177e4SLinus Torvalds 		if (page_mapped(page) || PageSwapCache(page))
7211da177e4SLinus Torvalds 			sc->nr_scanned++;
7221da177e4SLinus Torvalds 
723c661b078SAndy Whitcroft 		may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
724c661b078SAndy Whitcroft 			(PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
725c661b078SAndy Whitcroft 
726c661b078SAndy Whitcroft 		if (PageWriteback(page)) {
727e62e384eSMichal Hocko 			/*
728e62e384eSMichal Hocko 			 * memcg doesn't have any dirty pages throttling so we
729e62e384eSMichal Hocko 			 * could easily OOM just because too many pages are in
730c3b94f44SHugh Dickins 			 * writeback and there is nothing else to reclaim.
731e62e384eSMichal Hocko 			 *
732c3b94f44SHugh Dickins 			 * Check __GFP_IO, certainly because a loop driver
733e62e384eSMichal Hocko 			 * thread might enter reclaim, and deadlock if it waits
734e62e384eSMichal Hocko 			 * on a page for which it is needed to do the write
735e62e384eSMichal Hocko 			 * (loop masks off __GFP_IO|__GFP_FS for this reason);
736e62e384eSMichal Hocko 			 * but more thought would probably show more reasons.
737c3b94f44SHugh Dickins 			 *
738c3b94f44SHugh Dickins 			 * Don't require __GFP_FS, since we're not going into
739c3b94f44SHugh Dickins 			 * the FS, just waiting on its writeback completion.
740c3b94f44SHugh Dickins 			 * Worryingly, ext4 gfs2 and xfs allocate pages with
741c3b94f44SHugh Dickins 			 * grab_cache_page_write_begin(,,AOP_FLAG_NOFS), so
742c3b94f44SHugh Dickins 			 * testing may_enter_fs here is liable to OOM on them.
743e62e384eSMichal Hocko 			 */
744c3b94f44SHugh Dickins 			if (global_reclaim(sc) ||
745c3b94f44SHugh Dickins 			    !PageReclaim(page) || !(sc->gfp_mask & __GFP_IO)) {
746c3b94f44SHugh Dickins 				/*
747c3b94f44SHugh Dickins 				 * This is slightly racy - end_page_writeback()
748c3b94f44SHugh Dickins 				 * might have just cleared PageReclaim, then
749c3b94f44SHugh Dickins 				 * setting PageReclaim here end up interpreted
750c3b94f44SHugh Dickins 				 * as PageReadahead - but that does not matter
751c3b94f44SHugh Dickins 				 * enough to care.  What we do want is for this
752c3b94f44SHugh Dickins 				 * page to have PageReclaim set next time memcg
753c3b94f44SHugh Dickins 				 * reclaim reaches the tests above, so it will
754c3b94f44SHugh Dickins 				 * then wait_on_page_writeback() to avoid OOM;
755c3b94f44SHugh Dickins 				 * and it's also appropriate in global reclaim.
756c3b94f44SHugh Dickins 				 */
757c3b94f44SHugh Dickins 				SetPageReclaim(page);
75892df3a72SMel Gorman 				nr_writeback++;
759c3b94f44SHugh Dickins 				goto keep_locked;
760c661b078SAndy Whitcroft 			}
761c3b94f44SHugh Dickins 			wait_on_page_writeback(page);
762e62e384eSMichal Hocko 		}
7631da177e4SLinus Torvalds 
76402c6de8dSMinchan Kim 		if (!force_reclaim)
7656a18adb3SKonstantin Khlebnikov 			references = page_check_references(page, sc);
76602c6de8dSMinchan Kim 
767dfc8d636SJohannes Weiner 		switch (references) {
768dfc8d636SJohannes Weiner 		case PAGEREF_ACTIVATE:
7691da177e4SLinus Torvalds 			goto activate_locked;
77064574746SJohannes Weiner 		case PAGEREF_KEEP:
77164574746SJohannes Weiner 			goto keep_locked;
772dfc8d636SJohannes Weiner 		case PAGEREF_RECLAIM:
773dfc8d636SJohannes Weiner 		case PAGEREF_RECLAIM_CLEAN:
774dfc8d636SJohannes Weiner 			; /* try to reclaim the page below */
775dfc8d636SJohannes Weiner 		}
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 		/*
7781da177e4SLinus Torvalds 		 * Anonymous process memory has backing store?
7791da177e4SLinus Torvalds 		 * Try to allocate it some swap space here.
7801da177e4SLinus Torvalds 		 */
781b291f000SNick Piggin 		if (PageAnon(page) && !PageSwapCache(page)) {
78263eb6b93SHugh Dickins 			if (!(sc->gfp_mask & __GFP_IO))
78363eb6b93SHugh Dickins 				goto keep_locked;
784*5bc7b8acSShaohua Li 			if (!add_to_swap(page, page_list))
7851da177e4SLinus Torvalds 				goto activate_locked;
78663eb6b93SHugh Dickins 			may_enter_fs = 1;
787b291f000SNick Piggin 		}
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 		mapping = page_mapping(page);
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 		/*
7921da177e4SLinus Torvalds 		 * The page is mapped into the page tables of one or more
7931da177e4SLinus Torvalds 		 * processes. Try to unmap it here.
7941da177e4SLinus Torvalds 		 */
7951da177e4SLinus Torvalds 		if (page_mapped(page) && mapping) {
79602c6de8dSMinchan Kim 			switch (try_to_unmap(page, ttu_flags)) {
7971da177e4SLinus Torvalds 			case SWAP_FAIL:
7981da177e4SLinus Torvalds 				goto activate_locked;
7991da177e4SLinus Torvalds 			case SWAP_AGAIN:
8001da177e4SLinus Torvalds 				goto keep_locked;
801b291f000SNick Piggin 			case SWAP_MLOCK:
802b291f000SNick Piggin 				goto cull_mlocked;
8031da177e4SLinus Torvalds 			case SWAP_SUCCESS:
8041da177e4SLinus Torvalds 				; /* try to free the page below */
8051da177e4SLinus Torvalds 			}
8061da177e4SLinus Torvalds 		}
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 		if (PageDirty(page)) {
8090e093d99SMel Gorman 			nr_dirty++;
8100e093d99SMel Gorman 
811ee72886dSMel Gorman 			/*
812ee72886dSMel Gorman 			 * Only kswapd can writeback filesystem pages to
813f84f6e2bSMel Gorman 			 * avoid risk of stack overflow but do not writeback
814f84f6e2bSMel Gorman 			 * unless under significant pressure.
815ee72886dSMel Gorman 			 */
816f84f6e2bSMel Gorman 			if (page_is_file_cache(page) &&
8179e3b2f8cSKonstantin Khlebnikov 					(!current_is_kswapd() ||
8189e3b2f8cSKonstantin Khlebnikov 					 sc->priority >= DEF_PRIORITY - 2)) {
81949ea7eb6SMel Gorman 				/*
82049ea7eb6SMel Gorman 				 * Immediately reclaim when written back.
82149ea7eb6SMel Gorman 				 * Similar in principal to deactivate_page()
82249ea7eb6SMel Gorman 				 * except we already have the page isolated
82349ea7eb6SMel Gorman 				 * and know it's dirty
82449ea7eb6SMel Gorman 				 */
82549ea7eb6SMel Gorman 				inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE);
82649ea7eb6SMel Gorman 				SetPageReclaim(page);
82749ea7eb6SMel Gorman 
828ee72886dSMel Gorman 				goto keep_locked;
829ee72886dSMel Gorman 			}
830ee72886dSMel Gorman 
831dfc8d636SJohannes Weiner 			if (references == PAGEREF_RECLAIM_CLEAN)
8321da177e4SLinus Torvalds 				goto keep_locked;
8334dd4b920SAndrew Morton 			if (!may_enter_fs)
8341da177e4SLinus Torvalds 				goto keep_locked;
83552a8363eSChristoph Lameter 			if (!sc->may_writepage)
8361da177e4SLinus Torvalds 				goto keep_locked;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 			/* Page is dirty, try to write it out here */
8397d3579e8SKOSAKI Motohiro 			switch (pageout(page, mapping, sc)) {
8401da177e4SLinus Torvalds 			case PAGE_KEEP:
8410e093d99SMel Gorman 				nr_congested++;
8421da177e4SLinus Torvalds 				goto keep_locked;
8431da177e4SLinus Torvalds 			case PAGE_ACTIVATE:
8441da177e4SLinus Torvalds 				goto activate_locked;
8451da177e4SLinus Torvalds 			case PAGE_SUCCESS:
8467d3579e8SKOSAKI Motohiro 				if (PageWriteback(page))
84741ac1999SMel Gorman 					goto keep;
8487d3579e8SKOSAKI Motohiro 				if (PageDirty(page))
8491da177e4SLinus Torvalds 					goto keep;
8507d3579e8SKOSAKI Motohiro 
8511da177e4SLinus Torvalds 				/*
8521da177e4SLinus Torvalds 				 * A synchronous write - probably a ramdisk.  Go
8531da177e4SLinus Torvalds 				 * ahead and try to reclaim the page.
8541da177e4SLinus Torvalds 				 */
855529ae9aaSNick Piggin 				if (!trylock_page(page))
8561da177e4SLinus Torvalds 					goto keep;
8571da177e4SLinus Torvalds 				if (PageDirty(page) || PageWriteback(page))
8581da177e4SLinus Torvalds 					goto keep_locked;
8591da177e4SLinus Torvalds 				mapping = page_mapping(page);
8601da177e4SLinus Torvalds 			case PAGE_CLEAN:
8611da177e4SLinus Torvalds 				; /* try to free the page below */
8621da177e4SLinus Torvalds 			}
8631da177e4SLinus Torvalds 		}
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 		/*
8661da177e4SLinus Torvalds 		 * If the page has buffers, try to free the buffer mappings
8671da177e4SLinus Torvalds 		 * associated with this page. If we succeed we try to free
8681da177e4SLinus Torvalds 		 * the page as well.
8691da177e4SLinus Torvalds 		 *
8701da177e4SLinus Torvalds 		 * We do this even if the page is PageDirty().
8711da177e4SLinus Torvalds 		 * try_to_release_page() does not perform I/O, but it is
8721da177e4SLinus Torvalds 		 * possible for a page to have PageDirty set, but it is actually
8731da177e4SLinus Torvalds 		 * clean (all its buffers are clean).  This happens if the
8741da177e4SLinus Torvalds 		 * buffers were written out directly, with submit_bh(). ext3
8751da177e4SLinus Torvalds 		 * will do this, as well as the blockdev mapping.
8761da177e4SLinus Torvalds 		 * try_to_release_page() will discover that cleanness and will
8771da177e4SLinus Torvalds 		 * drop the buffers and mark the page clean - it can be freed.
8781da177e4SLinus Torvalds 		 *
8791da177e4SLinus Torvalds 		 * Rarely, pages can have buffers and no ->mapping.  These are
8801da177e4SLinus Torvalds 		 * the pages which were not successfully invalidated in
8811da177e4SLinus Torvalds 		 * truncate_complete_page().  We try to drop those buffers here
8821da177e4SLinus Torvalds 		 * and if that worked, and the page is no longer mapped into
8831da177e4SLinus Torvalds 		 * process address space (page_count == 1) it can be freed.
8841da177e4SLinus Torvalds 		 * Otherwise, leave the page on the LRU so it is swappable.
8851da177e4SLinus Torvalds 		 */
886266cf658SDavid Howells 		if (page_has_private(page)) {
8871da177e4SLinus Torvalds 			if (!try_to_release_page(page, sc->gfp_mask))
8881da177e4SLinus Torvalds 				goto activate_locked;
889e286781dSNick Piggin 			if (!mapping && page_count(page) == 1) {
890e286781dSNick Piggin 				unlock_page(page);
891e286781dSNick Piggin 				if (put_page_testzero(page))
8921da177e4SLinus Torvalds 					goto free_it;
893e286781dSNick Piggin 				else {
894e286781dSNick Piggin 					/*
895e286781dSNick Piggin 					 * rare race with speculative reference.
896e286781dSNick Piggin 					 * the speculative reference will free
897e286781dSNick Piggin 					 * this page shortly, so we may
898e286781dSNick Piggin 					 * increment nr_reclaimed here (and
899e286781dSNick Piggin 					 * leave it off the LRU).
900e286781dSNick Piggin 					 */
901e286781dSNick Piggin 					nr_reclaimed++;
902e286781dSNick Piggin 					continue;
903e286781dSNick Piggin 				}
904e286781dSNick Piggin 			}
9051da177e4SLinus Torvalds 		}
9061da177e4SLinus Torvalds 
907e286781dSNick Piggin 		if (!mapping || !__remove_mapping(mapping, page))
90849d2e9ccSChristoph Lameter 			goto keep_locked;
9091da177e4SLinus Torvalds 
910a978d6f5SNick Piggin 		/*
911a978d6f5SNick Piggin 		 * At this point, we have no other references and there is
912a978d6f5SNick Piggin 		 * no way to pick any more up (removed from LRU, removed
913a978d6f5SNick Piggin 		 * from pagecache). Can use non-atomic bitops now (and
914a978d6f5SNick Piggin 		 * we obviously don't have to worry about waking up a process
915a978d6f5SNick Piggin 		 * waiting on the page lock, because there are no references.
916a978d6f5SNick Piggin 		 */
917a978d6f5SNick Piggin 		__clear_page_locked(page);
918e286781dSNick Piggin free_it:
91905ff5137SAndrew Morton 		nr_reclaimed++;
920abe4c3b5SMel Gorman 
921abe4c3b5SMel Gorman 		/*
922abe4c3b5SMel Gorman 		 * Is there need to periodically free_page_list? It would
923abe4c3b5SMel Gorman 		 * appear not as the counts should be low
924abe4c3b5SMel Gorman 		 */
925abe4c3b5SMel Gorman 		list_add(&page->lru, &free_pages);
9261da177e4SLinus Torvalds 		continue;
9271da177e4SLinus Torvalds 
928b291f000SNick Piggin cull_mlocked:
92963d6c5adSHugh Dickins 		if (PageSwapCache(page))
93063d6c5adSHugh Dickins 			try_to_free_swap(page);
931b291f000SNick Piggin 		unlock_page(page);
932b291f000SNick Piggin 		putback_lru_page(page);
933b291f000SNick Piggin 		continue;
934b291f000SNick Piggin 
9351da177e4SLinus Torvalds activate_locked:
93668a22394SRik van Riel 		/* Not a candidate for swapping, so reclaim swap space. */
93768a22394SRik van Riel 		if (PageSwapCache(page) && vm_swap_full())
938a2c43eedSHugh Dickins 			try_to_free_swap(page);
939894bc310SLee Schermerhorn 		VM_BUG_ON(PageActive(page));
9401da177e4SLinus Torvalds 		SetPageActive(page);
9411da177e4SLinus Torvalds 		pgactivate++;
9421da177e4SLinus Torvalds keep_locked:
9431da177e4SLinus Torvalds 		unlock_page(page);
9441da177e4SLinus Torvalds keep:
9451da177e4SLinus Torvalds 		list_add(&page->lru, &ret_pages);
946b291f000SNick Piggin 		VM_BUG_ON(PageLRU(page) || PageUnevictable(page));
9471da177e4SLinus Torvalds 	}
948abe4c3b5SMel Gorman 
9490e093d99SMel Gorman 	/*
9500e093d99SMel Gorman 	 * Tag a zone as congested if all the dirty pages encountered were
9510e093d99SMel Gorman 	 * backed by a congested BDI. In this case, reclaimers should just
9520e093d99SMel Gorman 	 * back off and wait for congestion to clear because further reclaim
9530e093d99SMel Gorman 	 * will encounter the same problem
9540e093d99SMel Gorman 	 */
95589b5fae5SJohannes Weiner 	if (nr_dirty && nr_dirty == nr_congested && global_reclaim(sc))
9566a18adb3SKonstantin Khlebnikov 		zone_set_flag(zone, ZONE_CONGESTED);
9570e093d99SMel Gorman 
958cc59850eSKonstantin Khlebnikov 	free_hot_cold_page_list(&free_pages, 1);
959abe4c3b5SMel Gorman 
9601da177e4SLinus Torvalds 	list_splice(&ret_pages, page_list);
961f8891e5eSChristoph Lameter 	count_vm_events(PGACTIVATE, pgactivate);
96269980e31STim Chen 	mem_cgroup_uncharge_end();
96392df3a72SMel Gorman 	*ret_nr_dirty += nr_dirty;
96492df3a72SMel Gorman 	*ret_nr_writeback += nr_writeback;
96505ff5137SAndrew Morton 	return nr_reclaimed;
9661da177e4SLinus Torvalds }
9671da177e4SLinus Torvalds 
96802c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone,
96902c6de8dSMinchan Kim 					    struct list_head *page_list)
97002c6de8dSMinchan Kim {
97102c6de8dSMinchan Kim 	struct scan_control sc = {
97202c6de8dSMinchan Kim 		.gfp_mask = GFP_KERNEL,
97302c6de8dSMinchan Kim 		.priority = DEF_PRIORITY,
97402c6de8dSMinchan Kim 		.may_unmap = 1,
97502c6de8dSMinchan Kim 	};
97602c6de8dSMinchan Kim 	unsigned long ret, dummy1, dummy2;
97702c6de8dSMinchan Kim 	struct page *page, *next;
97802c6de8dSMinchan Kim 	LIST_HEAD(clean_pages);
97902c6de8dSMinchan Kim 
98002c6de8dSMinchan Kim 	list_for_each_entry_safe(page, next, page_list, lru) {
98102c6de8dSMinchan Kim 		if (page_is_file_cache(page) && !PageDirty(page)) {
98202c6de8dSMinchan Kim 			ClearPageActive(page);
98302c6de8dSMinchan Kim 			list_move(&page->lru, &clean_pages);
98402c6de8dSMinchan Kim 		}
98502c6de8dSMinchan Kim 	}
98602c6de8dSMinchan Kim 
98702c6de8dSMinchan Kim 	ret = shrink_page_list(&clean_pages, zone, &sc,
98802c6de8dSMinchan Kim 				TTU_UNMAP|TTU_IGNORE_ACCESS,
98902c6de8dSMinchan Kim 				&dummy1, &dummy2, true);
99002c6de8dSMinchan Kim 	list_splice(&clean_pages, page_list);
99102c6de8dSMinchan Kim 	__mod_zone_page_state(zone, NR_ISOLATED_FILE, -ret);
99202c6de8dSMinchan Kim 	return ret;
99302c6de8dSMinchan Kim }
99402c6de8dSMinchan Kim 
9955ad333ebSAndy Whitcroft /*
9965ad333ebSAndy Whitcroft  * Attempt to remove the specified page from its LRU.  Only take this page
9975ad333ebSAndy Whitcroft  * if it is of the appropriate PageActive status.  Pages which are being
9985ad333ebSAndy Whitcroft  * freed elsewhere are also ignored.
9995ad333ebSAndy Whitcroft  *
10005ad333ebSAndy Whitcroft  * page:	page to consider
10015ad333ebSAndy Whitcroft  * mode:	one of the LRU isolation modes defined above
10025ad333ebSAndy Whitcroft  *
10035ad333ebSAndy Whitcroft  * returns 0 on success, -ve errno on failure.
10045ad333ebSAndy Whitcroft  */
1005f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode)
10065ad333ebSAndy Whitcroft {
10075ad333ebSAndy Whitcroft 	int ret = -EINVAL;
10085ad333ebSAndy Whitcroft 
10095ad333ebSAndy Whitcroft 	/* Only take pages on the LRU. */
10105ad333ebSAndy Whitcroft 	if (!PageLRU(page))
10115ad333ebSAndy Whitcroft 		return ret;
10125ad333ebSAndy Whitcroft 
1013e46a2879SMinchan Kim 	/* Compaction should not handle unevictable pages but CMA can do so */
1014e46a2879SMinchan Kim 	if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE))
1015894bc310SLee Schermerhorn 		return ret;
1016894bc310SLee Schermerhorn 
10175ad333ebSAndy Whitcroft 	ret = -EBUSY;
101808e552c6SKAMEZAWA Hiroyuki 
1019c8244935SMel Gorman 	/*
1020c8244935SMel Gorman 	 * To minimise LRU disruption, the caller can indicate that it only
1021c8244935SMel Gorman 	 * wants to isolate pages it will be able to operate on without
1022c8244935SMel Gorman 	 * blocking - clean pages for the most part.
1023c8244935SMel Gorman 	 *
1024c8244935SMel Gorman 	 * ISOLATE_CLEAN means that only clean pages should be isolated. This
1025c8244935SMel Gorman 	 * is used by reclaim when it is cannot write to backing storage
1026c8244935SMel Gorman 	 *
1027c8244935SMel Gorman 	 * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages
1028c8244935SMel Gorman 	 * that it is possible to migrate without blocking
1029c8244935SMel Gorman 	 */
1030c8244935SMel Gorman 	if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) {
1031c8244935SMel Gorman 		/* All the caller can do on PageWriteback is block */
1032c8244935SMel Gorman 		if (PageWriteback(page))
103339deaf85SMinchan Kim 			return ret;
103439deaf85SMinchan Kim 
1035c8244935SMel Gorman 		if (PageDirty(page)) {
1036c8244935SMel Gorman 			struct address_space *mapping;
1037c8244935SMel Gorman 
1038c8244935SMel Gorman 			/* ISOLATE_CLEAN means only clean pages */
1039c8244935SMel Gorman 			if (mode & ISOLATE_CLEAN)
1040c8244935SMel Gorman 				return ret;
1041c8244935SMel Gorman 
1042c8244935SMel Gorman 			/*
1043c8244935SMel Gorman 			 * Only pages without mappings or that have a
1044c8244935SMel Gorman 			 * ->migratepage callback are possible to migrate
1045c8244935SMel Gorman 			 * without blocking
1046c8244935SMel Gorman 			 */
1047c8244935SMel Gorman 			mapping = page_mapping(page);
1048c8244935SMel Gorman 			if (mapping && !mapping->a_ops->migratepage)
1049c8244935SMel Gorman 				return ret;
1050c8244935SMel Gorman 		}
1051c8244935SMel Gorman 	}
1052c8244935SMel Gorman 
1053f80c0673SMinchan Kim 	if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
1054f80c0673SMinchan Kim 		return ret;
1055f80c0673SMinchan Kim 
10565ad333ebSAndy Whitcroft 	if (likely(get_page_unless_zero(page))) {
10575ad333ebSAndy Whitcroft 		/*
10585ad333ebSAndy Whitcroft 		 * Be careful not to clear PageLRU until after we're
10595ad333ebSAndy Whitcroft 		 * sure the page is not being freed elsewhere -- the
10605ad333ebSAndy Whitcroft 		 * page release code relies on it.
10615ad333ebSAndy Whitcroft 		 */
10625ad333ebSAndy Whitcroft 		ClearPageLRU(page);
10635ad333ebSAndy Whitcroft 		ret = 0;
10645ad333ebSAndy Whitcroft 	}
10655ad333ebSAndy Whitcroft 
10665ad333ebSAndy Whitcroft 	return ret;
10675ad333ebSAndy Whitcroft }
10685ad333ebSAndy Whitcroft 
106949d2e9ccSChristoph Lameter /*
10701da177e4SLinus Torvalds  * zone->lru_lock is heavily contended.  Some of the functions that
10711da177e4SLinus Torvalds  * shrink the lists perform better by taking out a batch of pages
10721da177e4SLinus Torvalds  * and working on them outside the LRU lock.
10731da177e4SLinus Torvalds  *
10741da177e4SLinus Torvalds  * For pagecache intensive workloads, this function is the hottest
10751da177e4SLinus Torvalds  * spot in the kernel (apart from copy_*_user functions).
10761da177e4SLinus Torvalds  *
10771da177e4SLinus Torvalds  * Appropriate locks must be held before calling this function.
10781da177e4SLinus Torvalds  *
10791da177e4SLinus Torvalds  * @nr_to_scan:	The number of pages to look through on the list.
10805dc35979SKonstantin Khlebnikov  * @lruvec:	The LRU vector to pull pages from.
10811da177e4SLinus Torvalds  * @dst:	The temp list to put pages on to.
1082f626012dSHugh Dickins  * @nr_scanned:	The number of pages that were scanned.
1083fe2c2a10SRik van Riel  * @sc:		The scan_control struct for this reclaim session
10845ad333ebSAndy Whitcroft  * @mode:	One of the LRU isolation modes
10853cb99451SKonstantin Khlebnikov  * @lru:	LRU list id for isolating
10861da177e4SLinus Torvalds  *
10871da177e4SLinus Torvalds  * returns how many pages were moved onto *@dst.
10881da177e4SLinus Torvalds  */
108969e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
10905dc35979SKonstantin Khlebnikov 		struct lruvec *lruvec, struct list_head *dst,
1091fe2c2a10SRik van Riel 		unsigned long *nr_scanned, struct scan_control *sc,
10923cb99451SKonstantin Khlebnikov 		isolate_mode_t mode, enum lru_list lru)
10931da177e4SLinus Torvalds {
109475b00af7SHugh Dickins 	struct list_head *src = &lruvec->lists[lru];
109569e05944SAndrew Morton 	unsigned long nr_taken = 0;
1096c9b02d97SWu Fengguang 	unsigned long scan;
10971da177e4SLinus Torvalds 
1098c9b02d97SWu Fengguang 	for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
10995ad333ebSAndy Whitcroft 		struct page *page;
1100fa9add64SHugh Dickins 		int nr_pages;
11015ad333ebSAndy Whitcroft 
11021da177e4SLinus Torvalds 		page = lru_to_page(src);
11031da177e4SLinus Torvalds 		prefetchw_prev_lru_page(page, src, flags);
11041da177e4SLinus Torvalds 
1105725d704eSNick Piggin 		VM_BUG_ON(!PageLRU(page));
11068d438f96SNick Piggin 
1107f3fd4a61SKonstantin Khlebnikov 		switch (__isolate_lru_page(page, mode)) {
11085ad333ebSAndy Whitcroft 		case 0:
1109fa9add64SHugh Dickins 			nr_pages = hpage_nr_pages(page);
1110fa9add64SHugh Dickins 			mem_cgroup_update_lru_size(lruvec, lru, -nr_pages);
11115ad333ebSAndy Whitcroft 			list_move(&page->lru, dst);
1112fa9add64SHugh Dickins 			nr_taken += nr_pages;
11135ad333ebSAndy Whitcroft 			break;
11147c8ee9a8SNick Piggin 
11155ad333ebSAndy Whitcroft 		case -EBUSY:
11165ad333ebSAndy Whitcroft 			/* else it is being freed elsewhere */
11175ad333ebSAndy Whitcroft 			list_move(&page->lru, src);
11185ad333ebSAndy Whitcroft 			continue;
11195ad333ebSAndy Whitcroft 
11205ad333ebSAndy Whitcroft 		default:
11215ad333ebSAndy Whitcroft 			BUG();
11225ad333ebSAndy Whitcroft 		}
11235ad333ebSAndy Whitcroft 	}
11241da177e4SLinus Torvalds 
1125f626012dSHugh Dickins 	*nr_scanned = scan;
112675b00af7SHugh Dickins 	trace_mm_vmscan_lru_isolate(sc->order, nr_to_scan, scan,
112775b00af7SHugh Dickins 				    nr_taken, mode, is_file_lru(lru));
11281da177e4SLinus Torvalds 	return nr_taken;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
113162695a84SNick Piggin /**
113262695a84SNick Piggin  * isolate_lru_page - tries to isolate a page from its LRU list
113362695a84SNick Piggin  * @page: page to isolate from its LRU list
113462695a84SNick Piggin  *
113562695a84SNick Piggin  * Isolates a @page from an LRU list, clears PageLRU and adjusts the
113662695a84SNick Piggin  * vmstat statistic corresponding to whatever LRU list the page was on.
113762695a84SNick Piggin  *
113862695a84SNick Piggin  * Returns 0 if the page was removed from an LRU list.
113962695a84SNick Piggin  * Returns -EBUSY if the page was not on an LRU list.
114062695a84SNick Piggin  *
114162695a84SNick Piggin  * The returned page will have PageLRU() cleared.  If it was found on
1142894bc310SLee Schermerhorn  * the active list, it will have PageActive set.  If it was found on
1143894bc310SLee Schermerhorn  * the unevictable list, it will have the PageUnevictable bit set. That flag
1144894bc310SLee Schermerhorn  * may need to be cleared by the caller before letting the page go.
114562695a84SNick Piggin  *
114662695a84SNick Piggin  * The vmstat statistic corresponding to the list on which the page was
114762695a84SNick Piggin  * found will be decremented.
114862695a84SNick Piggin  *
114962695a84SNick Piggin  * Restrictions:
115062695a84SNick Piggin  * (1) Must be called with an elevated refcount on the page. This is a
115162695a84SNick Piggin  *     fundamentnal difference from isolate_lru_pages (which is called
115262695a84SNick Piggin  *     without a stable reference).
115362695a84SNick Piggin  * (2) the lru_lock must not be held.
115462695a84SNick Piggin  * (3) interrupts must be enabled.
115562695a84SNick Piggin  */
115662695a84SNick Piggin int isolate_lru_page(struct page *page)
115762695a84SNick Piggin {
115862695a84SNick Piggin 	int ret = -EBUSY;
115962695a84SNick Piggin 
11600c917313SKonstantin Khlebnikov 	VM_BUG_ON(!page_count(page));
11610c917313SKonstantin Khlebnikov 
116262695a84SNick Piggin 	if (PageLRU(page)) {
116362695a84SNick Piggin 		struct zone *zone = page_zone(page);
1164fa9add64SHugh Dickins 		struct lruvec *lruvec;
116562695a84SNick Piggin 
116662695a84SNick Piggin 		spin_lock_irq(&zone->lru_lock);
1167fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
11680c917313SKonstantin Khlebnikov 		if (PageLRU(page)) {
1169894bc310SLee Schermerhorn 			int lru = page_lru(page);
11700c917313SKonstantin Khlebnikov 			get_page(page);
117162695a84SNick Piggin 			ClearPageLRU(page);
1172fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
1173fa9add64SHugh Dickins 			ret = 0;
117462695a84SNick Piggin 		}
117562695a84SNick Piggin 		spin_unlock_irq(&zone->lru_lock);
117662695a84SNick Piggin 	}
117762695a84SNick Piggin 	return ret;
117862695a84SNick Piggin }
117962695a84SNick Piggin 
11805ad333ebSAndy Whitcroft /*
1181d37dd5dcSFengguang Wu  * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
1182d37dd5dcSFengguang Wu  * then get resheduled. When there are massive number of tasks doing page
1183d37dd5dcSFengguang Wu  * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
1184d37dd5dcSFengguang Wu  * the LRU list will go small and be scanned faster than necessary, leading to
1185d37dd5dcSFengguang Wu  * unnecessary swapping, thrashing and OOM.
118635cd7815SRik van Riel  */
118735cd7815SRik van Riel static int too_many_isolated(struct zone *zone, int file,
118835cd7815SRik van Riel 		struct scan_control *sc)
118935cd7815SRik van Riel {
119035cd7815SRik van Riel 	unsigned long inactive, isolated;
119135cd7815SRik van Riel 
119235cd7815SRik van Riel 	if (current_is_kswapd())
119335cd7815SRik van Riel 		return 0;
119435cd7815SRik van Riel 
119589b5fae5SJohannes Weiner 	if (!global_reclaim(sc))
119635cd7815SRik van Riel 		return 0;
119735cd7815SRik van Riel 
119835cd7815SRik van Riel 	if (file) {
119935cd7815SRik van Riel 		inactive = zone_page_state(zone, NR_INACTIVE_FILE);
120035cd7815SRik van Riel 		isolated = zone_page_state(zone, NR_ISOLATED_FILE);
120135cd7815SRik van Riel 	} else {
120235cd7815SRik van Riel 		inactive = zone_page_state(zone, NR_INACTIVE_ANON);
120335cd7815SRik van Riel 		isolated = zone_page_state(zone, NR_ISOLATED_ANON);
120435cd7815SRik van Riel 	}
120535cd7815SRik van Riel 
12063cf23841SFengguang Wu 	/*
12073cf23841SFengguang Wu 	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
12083cf23841SFengguang Wu 	 * won't get blocked by normal direct-reclaimers, forming a circular
12093cf23841SFengguang Wu 	 * deadlock.
12103cf23841SFengguang Wu 	 */
12113cf23841SFengguang Wu 	if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS)
12123cf23841SFengguang Wu 		inactive >>= 3;
12133cf23841SFengguang Wu 
121435cd7815SRik van Riel 	return isolated > inactive;
121535cd7815SRik van Riel }
121635cd7815SRik van Riel 
121766635629SMel Gorman static noinline_for_stack void
121875b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list)
121966635629SMel Gorman {
122027ac81d8SKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
122127ac81d8SKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
12223f79768fSHugh Dickins 	LIST_HEAD(pages_to_free);
122366635629SMel Gorman 
122466635629SMel Gorman 	/*
122566635629SMel Gorman 	 * Put back any unfreeable pages.
122666635629SMel Gorman 	 */
122766635629SMel Gorman 	while (!list_empty(page_list)) {
12283f79768fSHugh Dickins 		struct page *page = lru_to_page(page_list);
122966635629SMel Gorman 		int lru;
12303f79768fSHugh Dickins 
123166635629SMel Gorman 		VM_BUG_ON(PageLRU(page));
123266635629SMel Gorman 		list_del(&page->lru);
123339b5f29aSHugh Dickins 		if (unlikely(!page_evictable(page))) {
123466635629SMel Gorman 			spin_unlock_irq(&zone->lru_lock);
123566635629SMel Gorman 			putback_lru_page(page);
123666635629SMel Gorman 			spin_lock_irq(&zone->lru_lock);
123766635629SMel Gorman 			continue;
123866635629SMel Gorman 		}
1239fa9add64SHugh Dickins 
1240fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
1241fa9add64SHugh Dickins 
12427a608572SLinus Torvalds 		SetPageLRU(page);
124366635629SMel Gorman 		lru = page_lru(page);
1244fa9add64SHugh Dickins 		add_page_to_lru_list(page, lruvec, lru);
1245fa9add64SHugh Dickins 
124666635629SMel Gorman 		if (is_active_lru(lru)) {
124766635629SMel Gorman 			int file = is_file_lru(lru);
12489992af10SRik van Riel 			int numpages = hpage_nr_pages(page);
12499992af10SRik van Riel 			reclaim_stat->recent_rotated[file] += numpages;
125066635629SMel Gorman 		}
12512bcf8879SHugh Dickins 		if (put_page_testzero(page)) {
12522bcf8879SHugh Dickins 			__ClearPageLRU(page);
12532bcf8879SHugh Dickins 			__ClearPageActive(page);
1254fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
12552bcf8879SHugh Dickins 
12562bcf8879SHugh Dickins 			if (unlikely(PageCompound(page))) {
125766635629SMel Gorman 				spin_unlock_irq(&zone->lru_lock);
12582bcf8879SHugh Dickins 				(*get_compound_page_dtor(page))(page);
125966635629SMel Gorman 				spin_lock_irq(&zone->lru_lock);
12602bcf8879SHugh Dickins 			} else
12612bcf8879SHugh Dickins 				list_add(&page->lru, &pages_to_free);
126266635629SMel Gorman 		}
126366635629SMel Gorman 	}
126466635629SMel Gorman 
12653f79768fSHugh Dickins 	/*
12663f79768fSHugh Dickins 	 * To save our caller's stack, now use input list for pages to free.
12673f79768fSHugh Dickins 	 */
12683f79768fSHugh Dickins 	list_splice(&pages_to_free, page_list);
126966635629SMel Gorman }
127066635629SMel Gorman 
127166635629SMel Gorman /*
12721742f19fSAndrew Morton  * shrink_inactive_list() is a helper for shrink_zone().  It returns the number
12731742f19fSAndrew Morton  * of reclaimed pages
12741da177e4SLinus Torvalds  */
127566635629SMel Gorman static noinline_for_stack unsigned long
12761a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
12779e3b2f8cSKonstantin Khlebnikov 		     struct scan_control *sc, enum lru_list lru)
12781da177e4SLinus Torvalds {
12791da177e4SLinus Torvalds 	LIST_HEAD(page_list);
1280e247dbceSKOSAKI Motohiro 	unsigned long nr_scanned;
128105ff5137SAndrew Morton 	unsigned long nr_reclaimed = 0;
1282e247dbceSKOSAKI Motohiro 	unsigned long nr_taken;
128392df3a72SMel Gorman 	unsigned long nr_dirty = 0;
128492df3a72SMel Gorman 	unsigned long nr_writeback = 0;
1285f3fd4a61SKonstantin Khlebnikov 	isolate_mode_t isolate_mode = 0;
12863cb99451SKonstantin Khlebnikov 	int file = is_file_lru(lru);
12871a93be0eSKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
12881a93be0eSKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
128978dc583dSKOSAKI Motohiro 
129035cd7815SRik van Riel 	while (unlikely(too_many_isolated(zone, file, sc))) {
129158355c78SKOSAKI Motohiro 		congestion_wait(BLK_RW_ASYNC, HZ/10);
129235cd7815SRik van Riel 
129335cd7815SRik van Riel 		/* We are about to die and free our memory. Return now. */
129435cd7815SRik van Riel 		if (fatal_signal_pending(current))
129535cd7815SRik van Riel 			return SWAP_CLUSTER_MAX;
129635cd7815SRik van Riel 	}
129735cd7815SRik van Riel 
12981da177e4SLinus Torvalds 	lru_add_drain();
1299f80c0673SMinchan Kim 
1300f80c0673SMinchan Kim 	if (!sc->may_unmap)
130161317289SHillf Danton 		isolate_mode |= ISOLATE_UNMAPPED;
1302f80c0673SMinchan Kim 	if (!sc->may_writepage)
130361317289SHillf Danton 		isolate_mode |= ISOLATE_CLEAN;
1304f80c0673SMinchan Kim 
13051da177e4SLinus Torvalds 	spin_lock_irq(&zone->lru_lock);
13061da177e4SLinus Torvalds 
13075dc35979SKonstantin Khlebnikov 	nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
13085dc35979SKonstantin Khlebnikov 				     &nr_scanned, sc, isolate_mode, lru);
130995d918fcSKonstantin Khlebnikov 
131095d918fcSKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken);
131195d918fcSKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
131295d918fcSKonstantin Khlebnikov 
131389b5fae5SJohannes Weiner 	if (global_reclaim(sc)) {
1314e247dbceSKOSAKI Motohiro 		zone->pages_scanned += nr_scanned;
1315b35ea17bSKOSAKI Motohiro 		if (current_is_kswapd())
131675b00af7SHugh Dickins 			__count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scanned);
1317b35ea17bSKOSAKI Motohiro 		else
131875b00af7SHugh Dickins 			__count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scanned);
1319b35ea17bSKOSAKI Motohiro 	}
132066635629SMel Gorman 	spin_unlock_irq(&zone->lru_lock);
1321d563c050SHillf Danton 
1322d563c050SHillf Danton 	if (nr_taken == 0)
132366635629SMel Gorman 		return 0;
1324b35ea17bSKOSAKI Motohiro 
132502c6de8dSMinchan Kim 	nr_reclaimed = shrink_page_list(&page_list, zone, sc, TTU_UNMAP,
132602c6de8dSMinchan Kim 					&nr_dirty, &nr_writeback, false);
1327c661b078SAndy Whitcroft 
13283f79768fSHugh Dickins 	spin_lock_irq(&zone->lru_lock);
13293f79768fSHugh Dickins 
133095d918fcSKonstantin Khlebnikov 	reclaim_stat->recent_scanned[file] += nr_taken;
1331d563c050SHillf Danton 
1332904249aaSYing Han 	if (global_reclaim(sc)) {
1333b35ea17bSKOSAKI Motohiro 		if (current_is_kswapd())
1334904249aaSYing Han 			__count_zone_vm_events(PGSTEAL_KSWAPD, zone,
1335904249aaSYing Han 					       nr_reclaimed);
1336904249aaSYing Han 		else
1337904249aaSYing Han 			__count_zone_vm_events(PGSTEAL_DIRECT, zone,
1338904249aaSYing Han 					       nr_reclaimed);
1339904249aaSYing Han 	}
1340a74609faSNick Piggin 
134127ac81d8SKonstantin Khlebnikov 	putback_inactive_pages(lruvec, &page_list);
13423f79768fSHugh Dickins 
134395d918fcSKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
13443f79768fSHugh Dickins 
13453f79768fSHugh Dickins 	spin_unlock_irq(&zone->lru_lock);
13463f79768fSHugh Dickins 
13473f79768fSHugh Dickins 	free_hot_cold_page_list(&page_list, 1);
1348e11da5b4SMel Gorman 
134992df3a72SMel Gorman 	/*
135092df3a72SMel Gorman 	 * If reclaim is isolating dirty pages under writeback, it implies
135192df3a72SMel Gorman 	 * that the long-lived page allocation rate is exceeding the page
135292df3a72SMel Gorman 	 * laundering rate. Either the global limits are not being effective
135392df3a72SMel Gorman 	 * at throttling processes due to the page distribution throughout
135492df3a72SMel Gorman 	 * zones or there is heavy usage of a slow backing device. The
135592df3a72SMel Gorman 	 * only option is to throttle from reclaim context which is not ideal
135692df3a72SMel Gorman 	 * as there is no guarantee the dirtying process is throttled in the
135792df3a72SMel Gorman 	 * same way balance_dirty_pages() manages.
135892df3a72SMel Gorman 	 *
135992df3a72SMel Gorman 	 * This scales the number of dirty pages that must be under writeback
136092df3a72SMel Gorman 	 * before throttling depending on priority. It is a simple backoff
136192df3a72SMel Gorman 	 * function that has the most effect in the range DEF_PRIORITY to
136292df3a72SMel Gorman 	 * DEF_PRIORITY-2 which is the priority reclaim is considered to be
136392df3a72SMel Gorman 	 * in trouble and reclaim is considered to be in trouble.
136492df3a72SMel Gorman 	 *
136592df3a72SMel Gorman 	 * DEF_PRIORITY   100% isolated pages must be PageWriteback to throttle
136692df3a72SMel Gorman 	 * DEF_PRIORITY-1  50% must be PageWriteback
136792df3a72SMel Gorman 	 * DEF_PRIORITY-2  25% must be PageWriteback, kswapd in trouble
136892df3a72SMel Gorman 	 * ...
136992df3a72SMel Gorman 	 * DEF_PRIORITY-6 For SWAP_CLUSTER_MAX isolated pages, throttle if any
137092df3a72SMel Gorman 	 *                     isolated page is PageWriteback
137192df3a72SMel Gorman 	 */
13729e3b2f8cSKonstantin Khlebnikov 	if (nr_writeback && nr_writeback >=
13739e3b2f8cSKonstantin Khlebnikov 			(nr_taken >> (DEF_PRIORITY - sc->priority)))
137492df3a72SMel Gorman 		wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
137592df3a72SMel Gorman 
1376e11da5b4SMel Gorman 	trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id,
1377e11da5b4SMel Gorman 		zone_idx(zone),
1378e11da5b4SMel Gorman 		nr_scanned, nr_reclaimed,
13799e3b2f8cSKonstantin Khlebnikov 		sc->priority,
138023b9da55SMel Gorman 		trace_shrink_flags(file));
138105ff5137SAndrew Morton 	return nr_reclaimed;
13821da177e4SLinus Torvalds }
13831da177e4SLinus Torvalds 
13843bb1a852SMartin Bligh /*
13851cfb419bSKAMEZAWA Hiroyuki  * This moves pages from the active list to the inactive list.
13861cfb419bSKAMEZAWA Hiroyuki  *
13871cfb419bSKAMEZAWA Hiroyuki  * We move them the other way if the page is referenced by one or more
13881cfb419bSKAMEZAWA Hiroyuki  * processes, from rmap.
13891cfb419bSKAMEZAWA Hiroyuki  *
13901cfb419bSKAMEZAWA Hiroyuki  * If the pages are mostly unmapped, the processing is fast and it is
13911cfb419bSKAMEZAWA Hiroyuki  * appropriate to hold zone->lru_lock across the whole operation.  But if
13921cfb419bSKAMEZAWA Hiroyuki  * the pages are mapped, the processing is slow (page_referenced()) so we
13931cfb419bSKAMEZAWA Hiroyuki  * should drop zone->lru_lock around each page.  It's impossible to balance
13941cfb419bSKAMEZAWA Hiroyuki  * this, so instead we remove the pages from the LRU while processing them.
13951cfb419bSKAMEZAWA Hiroyuki  * It is safe to rely on PG_active against the non-LRU pages in here because
13961cfb419bSKAMEZAWA Hiroyuki  * nobody will play with that bit on a non-LRU page.
13971cfb419bSKAMEZAWA Hiroyuki  *
13981cfb419bSKAMEZAWA Hiroyuki  * The downside is that we have to touch page->_count against each page.
13991cfb419bSKAMEZAWA Hiroyuki  * But we had to alter page->flags anyway.
14001cfb419bSKAMEZAWA Hiroyuki  */
14011cfb419bSKAMEZAWA Hiroyuki 
1402fa9add64SHugh Dickins static void move_active_pages_to_lru(struct lruvec *lruvec,
14033eb4140fSWu Fengguang 				     struct list_head *list,
14042bcf8879SHugh Dickins 				     struct list_head *pages_to_free,
14053eb4140fSWu Fengguang 				     enum lru_list lru)
14063eb4140fSWu Fengguang {
1407fa9add64SHugh Dickins 	struct zone *zone = lruvec_zone(lruvec);
14083eb4140fSWu Fengguang 	unsigned long pgmoved = 0;
14093eb4140fSWu Fengguang 	struct page *page;
1410fa9add64SHugh Dickins 	int nr_pages;
14113eb4140fSWu Fengguang 
14123eb4140fSWu Fengguang 	while (!list_empty(list)) {
14133eb4140fSWu Fengguang 		page = lru_to_page(list);
1414fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
14153eb4140fSWu Fengguang 
14163eb4140fSWu Fengguang 		VM_BUG_ON(PageLRU(page));
14173eb4140fSWu Fengguang 		SetPageLRU(page);
14183eb4140fSWu Fengguang 
1419fa9add64SHugh Dickins 		nr_pages = hpage_nr_pages(page);
1420fa9add64SHugh Dickins 		mem_cgroup_update_lru_size(lruvec, lru, nr_pages);
1421925b7673SJohannes Weiner 		list_move(&page->lru, &lruvec->lists[lru]);
1422fa9add64SHugh Dickins 		pgmoved += nr_pages;
14233eb4140fSWu Fengguang 
14242bcf8879SHugh Dickins 		if (put_page_testzero(page)) {
14252bcf8879SHugh Dickins 			__ClearPageLRU(page);
14262bcf8879SHugh Dickins 			__ClearPageActive(page);
1427fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
14282bcf8879SHugh Dickins 
14292bcf8879SHugh Dickins 			if (unlikely(PageCompound(page))) {
14303eb4140fSWu Fengguang 				spin_unlock_irq(&zone->lru_lock);
14312bcf8879SHugh Dickins 				(*get_compound_page_dtor(page))(page);
14323eb4140fSWu Fengguang 				spin_lock_irq(&zone->lru_lock);
14332bcf8879SHugh Dickins 			} else
14342bcf8879SHugh Dickins 				list_add(&page->lru, pages_to_free);
14353eb4140fSWu Fengguang 		}
14363eb4140fSWu Fengguang 	}
14373eb4140fSWu Fengguang 	__mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
14383eb4140fSWu Fengguang 	if (!is_active_lru(lru))
14393eb4140fSWu Fengguang 		__count_vm_events(PGDEACTIVATE, pgmoved);
14403eb4140fSWu Fengguang }
14411cfb419bSKAMEZAWA Hiroyuki 
1442f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan,
14431a93be0eSKonstantin Khlebnikov 			       struct lruvec *lruvec,
1444f16015fbSJohannes Weiner 			       struct scan_control *sc,
14459e3b2f8cSKonstantin Khlebnikov 			       enum lru_list lru)
14461cfb419bSKAMEZAWA Hiroyuki {
144744c241f1SKOSAKI Motohiro 	unsigned long nr_taken;
1448f626012dSHugh Dickins 	unsigned long nr_scanned;
14496fe6b7e3SWu Fengguang 	unsigned long vm_flags;
14501cfb419bSKAMEZAWA Hiroyuki 	LIST_HEAD(l_hold);	/* The pages which were snipped off */
14518cab4754SWu Fengguang 	LIST_HEAD(l_active);
1452b69408e8SChristoph Lameter 	LIST_HEAD(l_inactive);
14531cfb419bSKAMEZAWA Hiroyuki 	struct page *page;
14541a93be0eSKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
145544c241f1SKOSAKI Motohiro 	unsigned long nr_rotated = 0;
1456f3fd4a61SKonstantin Khlebnikov 	isolate_mode_t isolate_mode = 0;
14573cb99451SKonstantin Khlebnikov 	int file = is_file_lru(lru);
14581a93be0eSKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
14591cfb419bSKAMEZAWA Hiroyuki 
14601da177e4SLinus Torvalds 	lru_add_drain();
1461f80c0673SMinchan Kim 
1462f80c0673SMinchan Kim 	if (!sc->may_unmap)
146361317289SHillf Danton 		isolate_mode |= ISOLATE_UNMAPPED;
1464f80c0673SMinchan Kim 	if (!sc->may_writepage)
146561317289SHillf Danton 		isolate_mode |= ISOLATE_CLEAN;
1466f80c0673SMinchan Kim 
14671da177e4SLinus Torvalds 	spin_lock_irq(&zone->lru_lock);
1468925b7673SJohannes Weiner 
14695dc35979SKonstantin Khlebnikov 	nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
14705dc35979SKonstantin Khlebnikov 				     &nr_scanned, sc, isolate_mode, lru);
147189b5fae5SJohannes Weiner 	if (global_reclaim(sc))
1472f626012dSHugh Dickins 		zone->pages_scanned += nr_scanned;
147389b5fae5SJohannes Weiner 
1474b7c46d15SJohannes Weiner 	reclaim_stat->recent_scanned[file] += nr_taken;
14751cfb419bSKAMEZAWA Hiroyuki 
1476f626012dSHugh Dickins 	__count_zone_vm_events(PGREFILL, zone, nr_scanned);
14773cb99451SKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken);
1478a731286dSKOSAKI Motohiro 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
14791da177e4SLinus Torvalds 	spin_unlock_irq(&zone->lru_lock);
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	while (!list_empty(&l_hold)) {
14821da177e4SLinus Torvalds 		cond_resched();
14831da177e4SLinus Torvalds 		page = lru_to_page(&l_hold);
14841da177e4SLinus Torvalds 		list_del(&page->lru);
14857e9cd484SRik van Riel 
148639b5f29aSHugh Dickins 		if (unlikely(!page_evictable(page))) {
1487894bc310SLee Schermerhorn 			putback_lru_page(page);
1488894bc310SLee Schermerhorn 			continue;
1489894bc310SLee Schermerhorn 		}
1490894bc310SLee Schermerhorn 
1491cc715d99SMel Gorman 		if (unlikely(buffer_heads_over_limit)) {
1492cc715d99SMel Gorman 			if (page_has_private(page) && trylock_page(page)) {
1493cc715d99SMel Gorman 				if (page_has_private(page))
1494cc715d99SMel Gorman 					try_to_release_page(page, 0);
1495cc715d99SMel Gorman 				unlock_page(page);
1496cc715d99SMel Gorman 			}
1497cc715d99SMel Gorman 		}
1498cc715d99SMel Gorman 
1499c3ac9a8aSJohannes Weiner 		if (page_referenced(page, 0, sc->target_mem_cgroup,
1500c3ac9a8aSJohannes Weiner 				    &vm_flags)) {
15019992af10SRik van Riel 			nr_rotated += hpage_nr_pages(page);
15028cab4754SWu Fengguang 			/*
15038cab4754SWu Fengguang 			 * Identify referenced, file-backed active pages and
15048cab4754SWu Fengguang 			 * give them one more trip around the active list. So
15058cab4754SWu Fengguang 			 * that executable code get better chances to stay in
15068cab4754SWu Fengguang 			 * memory under moderate memory pressure.  Anon pages
15078cab4754SWu Fengguang 			 * are not likely to be evicted by use-once streaming
15088cab4754SWu Fengguang 			 * IO, plus JVM can create lots of anon VM_EXEC pages,
15098cab4754SWu Fengguang 			 * so we ignore them here.
15108cab4754SWu Fengguang 			 */
151141e20983SWu Fengguang 			if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
15128cab4754SWu Fengguang 				list_add(&page->lru, &l_active);
15138cab4754SWu Fengguang 				continue;
15148cab4754SWu Fengguang 			}
15158cab4754SWu Fengguang 		}
15167e9cd484SRik van Riel 
15175205e56eSKOSAKI Motohiro 		ClearPageActive(page);	/* we are de-activating */
15181da177e4SLinus Torvalds 		list_add(&page->lru, &l_inactive);
15191da177e4SLinus Torvalds 	}
15201da177e4SLinus Torvalds 
1521b555749aSAndrew Morton 	/*
15228cab4754SWu Fengguang 	 * Move pages back to the lru list.
1523b555749aSAndrew Morton 	 */
15242a1dc509SJohannes Weiner 	spin_lock_irq(&zone->lru_lock);
15254f98a2feSRik van Riel 	/*
15268cab4754SWu Fengguang 	 * Count referenced pages from currently used mappings as rotated,
15278cab4754SWu Fengguang 	 * even though only some of them are actually re-activated.  This
15288cab4754SWu Fengguang 	 * helps balance scan pressure between file and anonymous pages in
15298cab4754SWu Fengguang 	 * get_scan_ratio.
1530556adecbSRik van Riel 	 */
1531b7c46d15SJohannes Weiner 	reclaim_stat->recent_rotated[file] += nr_rotated;
1532556adecbSRik van Riel 
1533fa9add64SHugh Dickins 	move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru);
1534fa9add64SHugh Dickins 	move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE);
1535a731286dSKOSAKI Motohiro 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
1536f8891e5eSChristoph Lameter 	spin_unlock_irq(&zone->lru_lock);
15372bcf8879SHugh Dickins 
15382bcf8879SHugh Dickins 	free_hot_cold_page_list(&l_hold, 1);
15391da177e4SLinus Torvalds }
15401da177e4SLinus Torvalds 
154174e3f3c3SMinchan Kim #ifdef CONFIG_SWAP
154214797e23SKOSAKI Motohiro static int inactive_anon_is_low_global(struct zone *zone)
1543f89eb90eSKOSAKI Motohiro {
1544f89eb90eSKOSAKI Motohiro 	unsigned long active, inactive;
1545f89eb90eSKOSAKI Motohiro 
1546f89eb90eSKOSAKI Motohiro 	active = zone_page_state(zone, NR_ACTIVE_ANON);
1547f89eb90eSKOSAKI Motohiro 	inactive = zone_page_state(zone, NR_INACTIVE_ANON);
1548f89eb90eSKOSAKI Motohiro 
1549f89eb90eSKOSAKI Motohiro 	if (inactive * zone->inactive_ratio < active)
1550f89eb90eSKOSAKI Motohiro 		return 1;
1551f89eb90eSKOSAKI Motohiro 
1552f89eb90eSKOSAKI Motohiro 	return 0;
1553f89eb90eSKOSAKI Motohiro }
1554f89eb90eSKOSAKI Motohiro 
155514797e23SKOSAKI Motohiro /**
155614797e23SKOSAKI Motohiro  * inactive_anon_is_low - check if anonymous pages need to be deactivated
1557c56d5c7dSKonstantin Khlebnikov  * @lruvec: LRU vector to check
155814797e23SKOSAKI Motohiro  *
155914797e23SKOSAKI Motohiro  * Returns true if the zone does not have enough inactive anon pages,
156014797e23SKOSAKI Motohiro  * meaning some active anon pages need to be deactivated.
156114797e23SKOSAKI Motohiro  */
1562c56d5c7dSKonstantin Khlebnikov static int inactive_anon_is_low(struct lruvec *lruvec)
156314797e23SKOSAKI Motohiro {
156474e3f3c3SMinchan Kim 	/*
156574e3f3c3SMinchan Kim 	 * If we don't have swap space, anonymous page deactivation
156674e3f3c3SMinchan Kim 	 * is pointless.
156774e3f3c3SMinchan Kim 	 */
156874e3f3c3SMinchan Kim 	if (!total_swap_pages)
156974e3f3c3SMinchan Kim 		return 0;
157074e3f3c3SMinchan Kim 
1571c3c787e8SHugh Dickins 	if (!mem_cgroup_disabled())
1572c56d5c7dSKonstantin Khlebnikov 		return mem_cgroup_inactive_anon_is_low(lruvec);
1573f16015fbSJohannes Weiner 
1574c56d5c7dSKonstantin Khlebnikov 	return inactive_anon_is_low_global(lruvec_zone(lruvec));
157514797e23SKOSAKI Motohiro }
157674e3f3c3SMinchan Kim #else
1577c56d5c7dSKonstantin Khlebnikov static inline int inactive_anon_is_low(struct lruvec *lruvec)
157874e3f3c3SMinchan Kim {
157974e3f3c3SMinchan Kim 	return 0;
158074e3f3c3SMinchan Kim }
158174e3f3c3SMinchan Kim #endif
158214797e23SKOSAKI Motohiro 
158356e49d21SRik van Riel /**
158456e49d21SRik van Riel  * inactive_file_is_low - check if file pages need to be deactivated
1585c56d5c7dSKonstantin Khlebnikov  * @lruvec: LRU vector to check
158656e49d21SRik van Riel  *
158756e49d21SRik van Riel  * When the system is doing streaming IO, memory pressure here
158856e49d21SRik van Riel  * ensures that active file pages get deactivated, until more
158956e49d21SRik van Riel  * than half of the file pages are on the inactive list.
159056e49d21SRik van Riel  *
159156e49d21SRik van Riel  * Once we get to that situation, protect the system's working
159256e49d21SRik van Riel  * set from being evicted by disabling active file page aging.
159356e49d21SRik van Riel  *
159456e49d21SRik van Riel  * This uses a different ratio than the anonymous pages, because
159556e49d21SRik van Riel  * the page cache uses a use-once replacement algorithm.
159656e49d21SRik van Riel  */
1597c56d5c7dSKonstantin Khlebnikov static int inactive_file_is_low(struct lruvec *lruvec)
159856e49d21SRik van Riel {
1599e3790144SJohannes Weiner 	unsigned long inactive;
1600e3790144SJohannes Weiner 	unsigned long active;
160156e49d21SRik van Riel 
1602e3790144SJohannes Weiner 	inactive = get_lru_size(lruvec, LRU_INACTIVE_FILE);
1603e3790144SJohannes Weiner 	active = get_lru_size(lruvec, LRU_ACTIVE_FILE);
1604e3790144SJohannes Weiner 
1605e3790144SJohannes Weiner 	return active > inactive;
160656e49d21SRik van Riel }
160756e49d21SRik van Riel 
160875b00af7SHugh Dickins static int inactive_list_is_low(struct lruvec *lruvec, enum lru_list lru)
1609b39415b2SRik van Riel {
161075b00af7SHugh Dickins 	if (is_file_lru(lru))
1611c56d5c7dSKonstantin Khlebnikov 		return inactive_file_is_low(lruvec);
1612b39415b2SRik van Riel 	else
1613c56d5c7dSKonstantin Khlebnikov 		return inactive_anon_is_low(lruvec);
1614b39415b2SRik van Riel }
1615b39415b2SRik van Riel 
16164f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
16171a93be0eSKonstantin Khlebnikov 				 struct lruvec *lruvec, struct scan_control *sc)
1618b69408e8SChristoph Lameter {
1619b39415b2SRik van Riel 	if (is_active_lru(lru)) {
162075b00af7SHugh Dickins 		if (inactive_list_is_low(lruvec, lru))
16211a93be0eSKonstantin Khlebnikov 			shrink_active_list(nr_to_scan, lruvec, sc, lru);
1622556adecbSRik van Riel 		return 0;
1623556adecbSRik van Riel 	}
1624556adecbSRik van Riel 
16251a93be0eSKonstantin Khlebnikov 	return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
1626b69408e8SChristoph Lameter }
1627b69408e8SChristoph Lameter 
16283d58ab5cSKonstantin Khlebnikov static int vmscan_swappiness(struct scan_control *sc)
16291f4c025bSKAMEZAWA Hiroyuki {
163089b5fae5SJohannes Weiner 	if (global_reclaim(sc))
16311f4c025bSKAMEZAWA Hiroyuki 		return vm_swappiness;
16323d58ab5cSKonstantin Khlebnikov 	return mem_cgroup_swappiness(sc->target_mem_cgroup);
16331f4c025bSKAMEZAWA Hiroyuki }
16341f4c025bSKAMEZAWA Hiroyuki 
16359a265114SJohannes Weiner enum scan_balance {
16369a265114SJohannes Weiner 	SCAN_EQUAL,
16379a265114SJohannes Weiner 	SCAN_FRACT,
16389a265114SJohannes Weiner 	SCAN_ANON,
16399a265114SJohannes Weiner 	SCAN_FILE,
16409a265114SJohannes Weiner };
16419a265114SJohannes Weiner 
16421da177e4SLinus Torvalds /*
16434f98a2feSRik van Riel  * Determine how aggressively the anon and file LRU lists should be
16444f98a2feSRik van Riel  * scanned.  The relative value of each set of LRU lists is determined
16454f98a2feSRik van Riel  * by looking at the fraction of the pages scanned we did rotate back
16464f98a2feSRik van Riel  * onto the active list instead of evict.
16474f98a2feSRik van Riel  *
1648be7bd59dSWanpeng Li  * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
1649be7bd59dSWanpeng Li  * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
16504f98a2feSRik van Riel  */
165190126375SKonstantin Khlebnikov static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
16529e3b2f8cSKonstantin Khlebnikov 			   unsigned long *nr)
16534f98a2feSRik van Riel {
165490126375SKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
16559a265114SJohannes Weiner 	u64 fraction[2];
16569a265114SJohannes Weiner 	u64 denominator = 0;	/* gcc */
165790126375SKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
16589a265114SJohannes Weiner 	unsigned long anon_prio, file_prio;
16599a265114SJohannes Weiner 	enum scan_balance scan_balance;
16609a265114SJohannes Weiner 	unsigned long anon, file, free;
16619a265114SJohannes Weiner 	bool force_scan = false;
16629a265114SJohannes Weiner 	unsigned long ap, fp;
16639a265114SJohannes Weiner 	enum lru_list lru;
1664246e87a9SKAMEZAWA Hiroyuki 
1665f11c0ca5SJohannes Weiner 	/*
1666f11c0ca5SJohannes Weiner 	 * If the zone or memcg is small, nr[l] can be 0.  This
1667f11c0ca5SJohannes Weiner 	 * results in no scanning on this priority and a potential
1668f11c0ca5SJohannes Weiner 	 * priority drop.  Global direct reclaim can go to the next
1669f11c0ca5SJohannes Weiner 	 * zone and tends to have no problems. Global kswapd is for
1670f11c0ca5SJohannes Weiner 	 * zone balancing and it needs to scan a minimum amount. When
1671f11c0ca5SJohannes Weiner 	 * reclaiming for a memcg, a priority drop can cause high
1672f11c0ca5SJohannes Weiner 	 * latencies, so it's better to scan a minimum amount there as
1673f11c0ca5SJohannes Weiner 	 * well.
1674f11c0ca5SJohannes Weiner 	 */
167590126375SKonstantin Khlebnikov 	if (current_is_kswapd() && zone->all_unreclaimable)
1676a4d3e9e7SJohannes Weiner 		force_scan = true;
167789b5fae5SJohannes Weiner 	if (!global_reclaim(sc))
1678a4d3e9e7SJohannes Weiner 		force_scan = true;
167976a33fc3SShaohua Li 
168076a33fc3SShaohua Li 	/* If we have no swap space, do not bother scanning anon pages. */
1681ec8acf20SShaohua Li 	if (!sc->may_swap || (get_nr_swap_pages() <= 0)) {
16829a265114SJohannes Weiner 		scan_balance = SCAN_FILE;
168376a33fc3SShaohua Li 		goto out;
168476a33fc3SShaohua Li 	}
16854f98a2feSRik van Riel 
168610316b31SJohannes Weiner 	/*
168710316b31SJohannes Weiner 	 * Global reclaim will swap to prevent OOM even with no
168810316b31SJohannes Weiner 	 * swappiness, but memcg users want to use this knob to
168910316b31SJohannes Weiner 	 * disable swapping for individual groups completely when
169010316b31SJohannes Weiner 	 * using the memory controller's swap limit feature would be
169110316b31SJohannes Weiner 	 * too expensive.
169210316b31SJohannes Weiner 	 */
169310316b31SJohannes Weiner 	if (!global_reclaim(sc) && !vmscan_swappiness(sc)) {
16949a265114SJohannes Weiner 		scan_balance = SCAN_FILE;
169510316b31SJohannes Weiner 		goto out;
169610316b31SJohannes Weiner 	}
169710316b31SJohannes Weiner 
169810316b31SJohannes Weiner 	/*
169910316b31SJohannes Weiner 	 * Do not apply any pressure balancing cleverness when the
170010316b31SJohannes Weiner 	 * system is close to OOM, scan both anon and file equally
170110316b31SJohannes Weiner 	 * (unless the swappiness setting disagrees with swapping).
170210316b31SJohannes Weiner 	 */
170310316b31SJohannes Weiner 	if (!sc->priority && vmscan_swappiness(sc)) {
17049a265114SJohannes Weiner 		scan_balance = SCAN_EQUAL;
170510316b31SJohannes Weiner 		goto out;
170610316b31SJohannes Weiner 	}
170710316b31SJohannes Weiner 
17084d7dcca2SHugh Dickins 	anon  = get_lru_size(lruvec, LRU_ACTIVE_ANON) +
17094d7dcca2SHugh Dickins 		get_lru_size(lruvec, LRU_INACTIVE_ANON);
17104d7dcca2SHugh Dickins 	file  = get_lru_size(lruvec, LRU_ACTIVE_FILE) +
17114d7dcca2SHugh Dickins 		get_lru_size(lruvec, LRU_INACTIVE_FILE);
1712a4d3e9e7SJohannes Weiner 
171311d16c25SJohannes Weiner 	/*
171411d16c25SJohannes Weiner 	 * If it's foreseeable that reclaiming the file cache won't be
171511d16c25SJohannes Weiner 	 * enough to get the zone back into a desirable shape, we have
171611d16c25SJohannes Weiner 	 * to swap.  Better start now and leave the - probably heavily
171711d16c25SJohannes Weiner 	 * thrashing - remaining file pages alone.
171811d16c25SJohannes Weiner 	 */
171989b5fae5SJohannes Weiner 	if (global_reclaim(sc)) {
172090126375SKonstantin Khlebnikov 		free = zone_page_state(zone, NR_FREE_PAGES);
172190126375SKonstantin Khlebnikov 		if (unlikely(file + free <= high_wmark_pages(zone))) {
17229a265114SJohannes Weiner 			scan_balance = SCAN_ANON;
172376a33fc3SShaohua Li 			goto out;
17247c5bd705SJohannes Weiner 		}
17257c5bd705SJohannes Weiner 	}
17267c5bd705SJohannes Weiner 
1727e9868505SRik van Riel 	/*
17287c5bd705SJohannes Weiner 	 * There is enough inactive page cache, do not reclaim
17297c5bd705SJohannes Weiner 	 * anything from the anonymous working set right now.
1730e9868505SRik van Riel 	 */
17317c5bd705SJohannes Weiner 	if (!inactive_file_is_low(lruvec)) {
17329a265114SJohannes Weiner 		scan_balance = SCAN_FILE;
1733e9868505SRik van Riel 		goto out;
17344f98a2feSRik van Riel 	}
17354f98a2feSRik van Riel 
17369a265114SJohannes Weiner 	scan_balance = SCAN_FRACT;
17379a265114SJohannes Weiner 
17384f98a2feSRik van Riel 	/*
173958c37f6eSKOSAKI Motohiro 	 * With swappiness at 100, anonymous and file have the same priority.
174058c37f6eSKOSAKI Motohiro 	 * This scanning priority is essentially the inverse of IO cost.
174158c37f6eSKOSAKI Motohiro 	 */
17423d58ab5cSKonstantin Khlebnikov 	anon_prio = vmscan_swappiness(sc);
174375b00af7SHugh Dickins 	file_prio = 200 - anon_prio;
174458c37f6eSKOSAKI Motohiro 
174558c37f6eSKOSAKI Motohiro 	/*
17464f98a2feSRik van Riel 	 * OK, so we have swap space and a fair amount of page cache
17474f98a2feSRik van Riel 	 * pages.  We use the recently rotated / recently scanned
17484f98a2feSRik van Riel 	 * ratios to determine how valuable each cache is.
17494f98a2feSRik van Riel 	 *
17504f98a2feSRik van Riel 	 * Because workloads change over time (and to avoid overflow)
17514f98a2feSRik van Riel 	 * we keep these statistics as a floating average, which ends
17524f98a2feSRik van Riel 	 * up weighing recent references more than old ones.
17534f98a2feSRik van Riel 	 *
17544f98a2feSRik van Riel 	 * anon in [0], file in [1]
17554f98a2feSRik van Riel 	 */
175690126375SKonstantin Khlebnikov 	spin_lock_irq(&zone->lru_lock);
175758c37f6eSKOSAKI Motohiro 	if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
17586e901571SKOSAKI Motohiro 		reclaim_stat->recent_scanned[0] /= 2;
17596e901571SKOSAKI Motohiro 		reclaim_stat->recent_rotated[0] /= 2;
17604f98a2feSRik van Riel 	}
17614f98a2feSRik van Riel 
17626e901571SKOSAKI Motohiro 	if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
17636e901571SKOSAKI Motohiro 		reclaim_stat->recent_scanned[1] /= 2;
17646e901571SKOSAKI Motohiro 		reclaim_stat->recent_rotated[1] /= 2;
17654f98a2feSRik van Riel 	}
17664f98a2feSRik van Riel 
17674f98a2feSRik van Riel 	/*
176800d8089cSRik van Riel 	 * The amount of pressure on anon vs file pages is inversely
176900d8089cSRik van Riel 	 * proportional to the fraction of recently scanned pages on
177000d8089cSRik van Riel 	 * each list that were recently referenced and in active use.
17714f98a2feSRik van Riel 	 */
1772fe35004fSSatoru Moriya 	ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1);
17736e901571SKOSAKI Motohiro 	ap /= reclaim_stat->recent_rotated[0] + 1;
17744f98a2feSRik van Riel 
1775fe35004fSSatoru Moriya 	fp = file_prio * (reclaim_stat->recent_scanned[1] + 1);
17766e901571SKOSAKI Motohiro 	fp /= reclaim_stat->recent_rotated[1] + 1;
177790126375SKonstantin Khlebnikov 	spin_unlock_irq(&zone->lru_lock);
17784f98a2feSRik van Riel 
177976a33fc3SShaohua Li 	fraction[0] = ap;
178076a33fc3SShaohua Li 	fraction[1] = fp;
178176a33fc3SShaohua Li 	denominator = ap + fp + 1;
178276a33fc3SShaohua Li out:
17834111304dSHugh Dickins 	for_each_evictable_lru(lru) {
17844111304dSHugh Dickins 		int file = is_file_lru(lru);
1785d778df51SJohannes Weiner 		unsigned long size;
178676a33fc3SShaohua Li 		unsigned long scan;
178776a33fc3SShaohua Li 
1788d778df51SJohannes Weiner 		size = get_lru_size(lruvec, lru);
1789d778df51SJohannes Weiner 		scan = size >> sc->priority;
17909a265114SJohannes Weiner 
1791f11c0ca5SJohannes Weiner 		if (!scan && force_scan)
1792d778df51SJohannes Weiner 			scan = min(size, SWAP_CLUSTER_MAX);
17939a265114SJohannes Weiner 
17949a265114SJohannes Weiner 		switch (scan_balance) {
17959a265114SJohannes Weiner 		case SCAN_EQUAL:
17969a265114SJohannes Weiner 			/* Scan lists relative to size */
17979a265114SJohannes Weiner 			break;
17989a265114SJohannes Weiner 		case SCAN_FRACT:
17999a265114SJohannes Weiner 			/*
18009a265114SJohannes Weiner 			 * Scan types proportional to swappiness and
18019a265114SJohannes Weiner 			 * their relative recent reclaim efficiency.
18029a265114SJohannes Weiner 			 */
180376a33fc3SShaohua Li 			scan = div64_u64(scan * fraction[file], denominator);
18049a265114SJohannes Weiner 			break;
18059a265114SJohannes Weiner 		case SCAN_FILE:
18069a265114SJohannes Weiner 		case SCAN_ANON:
18079a265114SJohannes Weiner 			/* Scan one type exclusively */
18089a265114SJohannes Weiner 			if ((scan_balance == SCAN_FILE) != file)
18099a265114SJohannes Weiner 				scan = 0;
18109a265114SJohannes Weiner 			break;
18119a265114SJohannes Weiner 		default:
18129a265114SJohannes Weiner 			/* Look ma, no brain */
18139a265114SJohannes Weiner 			BUG();
18149a265114SJohannes Weiner 		}
18154111304dSHugh Dickins 		nr[lru] = scan;
181676a33fc3SShaohua Li 	}
18176e08a369SWu Fengguang }
18184f98a2feSRik van Riel 
18199b4f98cdSJohannes Weiner /*
18209b4f98cdSJohannes Weiner  * This is a basic per-zone page freer.  Used by both kswapd and direct reclaim.
18219b4f98cdSJohannes Weiner  */
18229b4f98cdSJohannes Weiner static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
18239b4f98cdSJohannes Weiner {
18249b4f98cdSJohannes Weiner 	unsigned long nr[NR_LRU_LISTS];
18259b4f98cdSJohannes Weiner 	unsigned long nr_to_scan;
18269b4f98cdSJohannes Weiner 	enum lru_list lru;
18279b4f98cdSJohannes Weiner 	unsigned long nr_reclaimed = 0;
18289b4f98cdSJohannes Weiner 	unsigned long nr_to_reclaim = sc->nr_to_reclaim;
18299b4f98cdSJohannes Weiner 	struct blk_plug plug;
18309b4f98cdSJohannes Weiner 
18319b4f98cdSJohannes Weiner 	get_scan_count(lruvec, sc, nr);
18329b4f98cdSJohannes Weiner 
18339b4f98cdSJohannes Weiner 	blk_start_plug(&plug);
18349b4f98cdSJohannes Weiner 	while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
18359b4f98cdSJohannes Weiner 					nr[LRU_INACTIVE_FILE]) {
18369b4f98cdSJohannes Weiner 		for_each_evictable_lru(lru) {
18379b4f98cdSJohannes Weiner 			if (nr[lru]) {
18389b4f98cdSJohannes Weiner 				nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX);
18399b4f98cdSJohannes Weiner 				nr[lru] -= nr_to_scan;
18409b4f98cdSJohannes Weiner 
18419b4f98cdSJohannes Weiner 				nr_reclaimed += shrink_list(lru, nr_to_scan,
18429b4f98cdSJohannes Weiner 							    lruvec, sc);
18439b4f98cdSJohannes Weiner 			}
18449b4f98cdSJohannes Weiner 		}
18459b4f98cdSJohannes Weiner 		/*
18469b4f98cdSJohannes Weiner 		 * On large memory systems, scan >> priority can become
18479b4f98cdSJohannes Weiner 		 * really large. This is fine for the starting priority;
18489b4f98cdSJohannes Weiner 		 * we want to put equal scanning pressure on each zone.
18499b4f98cdSJohannes Weiner 		 * However, if the VM has a harder time of freeing pages,
18509b4f98cdSJohannes Weiner 		 * with multiple processes reclaiming pages, the total
18519b4f98cdSJohannes Weiner 		 * freeing target can get unreasonably large.
18529b4f98cdSJohannes Weiner 		 */
18539b4f98cdSJohannes Weiner 		if (nr_reclaimed >= nr_to_reclaim &&
18549b4f98cdSJohannes Weiner 		    sc->priority < DEF_PRIORITY)
18559b4f98cdSJohannes Weiner 			break;
18569b4f98cdSJohannes Weiner 	}
18579b4f98cdSJohannes Weiner 	blk_finish_plug(&plug);
18589b4f98cdSJohannes Weiner 	sc->nr_reclaimed += nr_reclaimed;
18599b4f98cdSJohannes Weiner 
18609b4f98cdSJohannes Weiner 	/*
18619b4f98cdSJohannes Weiner 	 * Even if we did not try to evict anon pages at all, we want to
18629b4f98cdSJohannes Weiner 	 * rebalance the anon lru active/inactive ratio.
18639b4f98cdSJohannes Weiner 	 */
18649b4f98cdSJohannes Weiner 	if (inactive_anon_is_low(lruvec))
18659b4f98cdSJohannes Weiner 		shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
18669b4f98cdSJohannes Weiner 				   sc, LRU_ACTIVE_ANON);
18679b4f98cdSJohannes Weiner 
18689b4f98cdSJohannes Weiner 	throttle_vm_writeout(sc->gfp_mask);
18699b4f98cdSJohannes Weiner }
18709b4f98cdSJohannes Weiner 
187123b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */
18729e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc)
187323b9da55SMel Gorman {
1874d84da3f9SKirill A. Shutemov 	if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
187523b9da55SMel Gorman 			(sc->order > PAGE_ALLOC_COSTLY_ORDER ||
18769e3b2f8cSKonstantin Khlebnikov 			 sc->priority < DEF_PRIORITY - 2))
187723b9da55SMel Gorman 		return true;
187823b9da55SMel Gorman 
187923b9da55SMel Gorman 	return false;
188023b9da55SMel Gorman }
188123b9da55SMel Gorman 
18824f98a2feSRik van Riel /*
188323b9da55SMel Gorman  * Reclaim/compaction is used for high-order allocation requests. It reclaims
188423b9da55SMel Gorman  * order-0 pages before compacting the zone. should_continue_reclaim() returns
188523b9da55SMel Gorman  * true if more pages should be reclaimed such that when the page allocator
188623b9da55SMel Gorman  * calls try_to_compact_zone() that it will have enough free pages to succeed.
188723b9da55SMel Gorman  * It will give up earlier than that if there is difficulty reclaiming pages.
18883e7d3449SMel Gorman  */
18899b4f98cdSJohannes Weiner static inline bool should_continue_reclaim(struct zone *zone,
18903e7d3449SMel Gorman 					unsigned long nr_reclaimed,
18913e7d3449SMel Gorman 					unsigned long nr_scanned,
18923e7d3449SMel Gorman 					struct scan_control *sc)
18933e7d3449SMel Gorman {
18943e7d3449SMel Gorman 	unsigned long pages_for_compaction;
18953e7d3449SMel Gorman 	unsigned long inactive_lru_pages;
18963e7d3449SMel Gorman 
18973e7d3449SMel Gorman 	/* If not in reclaim/compaction mode, stop */
18989e3b2f8cSKonstantin Khlebnikov 	if (!in_reclaim_compaction(sc))
18993e7d3449SMel Gorman 		return false;
19003e7d3449SMel Gorman 
19012876592fSMel Gorman 	/* Consider stopping depending on scan and reclaim activity */
19022876592fSMel Gorman 	if (sc->gfp_mask & __GFP_REPEAT) {
19033e7d3449SMel Gorman 		/*
19042876592fSMel Gorman 		 * For __GFP_REPEAT allocations, stop reclaiming if the
19052876592fSMel Gorman 		 * full LRU list has been scanned and we are still failing
19062876592fSMel Gorman 		 * to reclaim pages. This full LRU scan is potentially
19072876592fSMel Gorman 		 * expensive but a __GFP_REPEAT caller really wants to succeed
19083e7d3449SMel Gorman 		 */
19093e7d3449SMel Gorman 		if (!nr_reclaimed && !nr_scanned)
19103e7d3449SMel Gorman 			return false;
19112876592fSMel Gorman 	} else {
19122876592fSMel Gorman 		/*
19132876592fSMel Gorman 		 * For non-__GFP_REPEAT allocations which can presumably
19142876592fSMel Gorman 		 * fail without consequence, stop if we failed to reclaim
19152876592fSMel Gorman 		 * any pages from the last SWAP_CLUSTER_MAX number of
19162876592fSMel Gorman 		 * pages that were scanned. This will return to the
19172876592fSMel Gorman 		 * caller faster at the risk reclaim/compaction and
19182876592fSMel Gorman 		 * the resulting allocation attempt fails
19192876592fSMel Gorman 		 */
19202876592fSMel Gorman 		if (!nr_reclaimed)
19212876592fSMel Gorman 			return false;
19222876592fSMel Gorman 	}
19233e7d3449SMel Gorman 
19243e7d3449SMel Gorman 	/*
19253e7d3449SMel Gorman 	 * If we have not reclaimed enough pages for compaction and the
19263e7d3449SMel Gorman 	 * inactive lists are large enough, continue reclaiming
19273e7d3449SMel Gorman 	 */
19283e7d3449SMel Gorman 	pages_for_compaction = (2UL << sc->order);
19299b4f98cdSJohannes Weiner 	inactive_lru_pages = zone_page_state(zone, NR_INACTIVE_FILE);
1930ec8acf20SShaohua Li 	if (get_nr_swap_pages() > 0)
19319b4f98cdSJohannes Weiner 		inactive_lru_pages += zone_page_state(zone, NR_INACTIVE_ANON);
19323e7d3449SMel Gorman 	if (sc->nr_reclaimed < pages_for_compaction &&
19333e7d3449SMel Gorman 			inactive_lru_pages > pages_for_compaction)
19343e7d3449SMel Gorman 		return true;
19353e7d3449SMel Gorman 
19363e7d3449SMel Gorman 	/* If compaction would go ahead or the allocation would succeed, stop */
19379b4f98cdSJohannes Weiner 	switch (compaction_suitable(zone, sc->order)) {
19383e7d3449SMel Gorman 	case COMPACT_PARTIAL:
19393e7d3449SMel Gorman 	case COMPACT_CONTINUE:
19403e7d3449SMel Gorman 		return false;
19413e7d3449SMel Gorman 	default:
19423e7d3449SMel Gorman 		return true;
19433e7d3449SMel Gorman 	}
19443e7d3449SMel Gorman }
19453e7d3449SMel Gorman 
19469e3b2f8cSKonstantin Khlebnikov static void shrink_zone(struct zone *zone, struct scan_control *sc)
1947f16015fbSJohannes Weiner {
19489b4f98cdSJohannes Weiner 	unsigned long nr_reclaimed, nr_scanned;
19499b4f98cdSJohannes Weiner 
19509b4f98cdSJohannes Weiner 	do {
19515660048cSJohannes Weiner 		struct mem_cgroup *root = sc->target_mem_cgroup;
19525660048cSJohannes Weiner 		struct mem_cgroup_reclaim_cookie reclaim = {
19535660048cSJohannes Weiner 			.zone = zone,
19549e3b2f8cSKonstantin Khlebnikov 			.priority = sc->priority,
19555660048cSJohannes Weiner 		};
19565660048cSJohannes Weiner 		struct mem_cgroup *memcg;
19575660048cSJohannes Weiner 
19589b4f98cdSJohannes Weiner 		nr_reclaimed = sc->nr_reclaimed;
19599b4f98cdSJohannes Weiner 		nr_scanned = sc->nr_scanned;
19609b4f98cdSJohannes Weiner 
19615660048cSJohannes Weiner 		memcg = mem_cgroup_iter(root, NULL, &reclaim);
19625660048cSJohannes Weiner 		do {
19639b4f98cdSJohannes Weiner 			struct lruvec *lruvec;
19649b4f98cdSJohannes Weiner 
19659b4f98cdSJohannes Weiner 			lruvec = mem_cgroup_zone_lruvec(zone, memcg);
19665660048cSJohannes Weiner 
1967f9be23d6SKonstantin Khlebnikov 			shrink_lruvec(lruvec, sc);
1968f9be23d6SKonstantin Khlebnikov 
19695660048cSJohannes Weiner 			/*
1970a394cb8eSMichal Hocko 			 * Direct reclaim and kswapd have to scan all memory
1971a394cb8eSMichal Hocko 			 * cgroups to fulfill the overall scan target for the
19729b4f98cdSJohannes Weiner 			 * zone.
1973a394cb8eSMichal Hocko 			 *
1974a394cb8eSMichal Hocko 			 * Limit reclaim, on the other hand, only cares about
1975a394cb8eSMichal Hocko 			 * nr_to_reclaim pages to be reclaimed and it will
1976a394cb8eSMichal Hocko 			 * retry with decreasing priority if one round over the
1977a394cb8eSMichal Hocko 			 * whole hierarchy is not sufficient.
19785660048cSJohannes Weiner 			 */
1979a394cb8eSMichal Hocko 			if (!global_reclaim(sc) &&
1980a394cb8eSMichal Hocko 					sc->nr_reclaimed >= sc->nr_to_reclaim) {
19815660048cSJohannes Weiner 				mem_cgroup_iter_break(root, memcg);
19825660048cSJohannes Weiner 				break;
19835660048cSJohannes Weiner 			}
19845660048cSJohannes Weiner 			memcg = mem_cgroup_iter(root, memcg, &reclaim);
19855660048cSJohannes Weiner 		} while (memcg);
198670ddf637SAnton Vorontsov 
198770ddf637SAnton Vorontsov 		vmpressure(sc->gfp_mask, sc->target_mem_cgroup,
198870ddf637SAnton Vorontsov 			   sc->nr_scanned - nr_scanned,
198970ddf637SAnton Vorontsov 			   sc->nr_reclaimed - nr_reclaimed);
199070ddf637SAnton Vorontsov 
19919b4f98cdSJohannes Weiner 	} while (should_continue_reclaim(zone, sc->nr_reclaimed - nr_reclaimed,
19929b4f98cdSJohannes Weiner 					 sc->nr_scanned - nr_scanned, sc));
1993f16015fbSJohannes Weiner }
1994f16015fbSJohannes Weiner 
1995fe4b1b24SMel Gorman /* Returns true if compaction should go ahead for a high-order request */
1996fe4b1b24SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
1997fe4b1b24SMel Gorman {
1998fe4b1b24SMel Gorman 	unsigned long balance_gap, watermark;
1999fe4b1b24SMel Gorman 	bool watermark_ok;
2000fe4b1b24SMel Gorman 
2001fe4b1b24SMel Gorman 	/* Do not consider compaction for orders reclaim is meant to satisfy */
2002fe4b1b24SMel Gorman 	if (sc->order <= PAGE_ALLOC_COSTLY_ORDER)
2003fe4b1b24SMel Gorman 		return false;
2004fe4b1b24SMel Gorman 
2005fe4b1b24SMel Gorman 	/*
2006fe4b1b24SMel Gorman 	 * Compaction takes time to run and there are potentially other
2007fe4b1b24SMel Gorman 	 * callers using the pages just freed. Continue reclaiming until
2008fe4b1b24SMel Gorman 	 * there is a buffer of free pages available to give compaction
2009fe4b1b24SMel Gorman 	 * a reasonable chance of completing and allocating the page
2010fe4b1b24SMel Gorman 	 */
2011fe4b1b24SMel Gorman 	balance_gap = min(low_wmark_pages(zone),
2012b40da049SJiang Liu 		(zone->managed_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
2013fe4b1b24SMel Gorman 			KSWAPD_ZONE_BALANCE_GAP_RATIO);
2014fe4b1b24SMel Gorman 	watermark = high_wmark_pages(zone) + balance_gap + (2UL << sc->order);
2015fe4b1b24SMel Gorman 	watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0);
2016fe4b1b24SMel Gorman 
2017fe4b1b24SMel Gorman 	/*
2018fe4b1b24SMel Gorman 	 * If compaction is deferred, reclaim up to a point where
2019fe4b1b24SMel Gorman 	 * compaction will have a chance of success when re-enabled
2020fe4b1b24SMel Gorman 	 */
2021aff62249SRik van Riel 	if (compaction_deferred(zone, sc->order))
2022fe4b1b24SMel Gorman 		return watermark_ok;
2023fe4b1b24SMel Gorman 
2024fe4b1b24SMel Gorman 	/* If compaction is not ready to start, keep reclaiming */
2025fe4b1b24SMel Gorman 	if (!compaction_suitable(zone, sc->order))
2026fe4b1b24SMel Gorman 		return false;
2027fe4b1b24SMel Gorman 
2028fe4b1b24SMel Gorman 	return watermark_ok;
2029fe4b1b24SMel Gorman }
2030fe4b1b24SMel Gorman 
20311da177e4SLinus Torvalds /*
20321da177e4SLinus Torvalds  * This is the direct reclaim path, for page-allocating processes.  We only
20331da177e4SLinus Torvalds  * try to reclaim pages from zones which will satisfy the caller's allocation
20341da177e4SLinus Torvalds  * request.
20351da177e4SLinus Torvalds  *
203641858966SMel Gorman  * We reclaim from a zone even if that zone is over high_wmark_pages(zone).
203741858966SMel Gorman  * Because:
20381da177e4SLinus Torvalds  * a) The caller may be trying to free *extra* pages to satisfy a higher-order
20391da177e4SLinus Torvalds  *    allocation or
204041858966SMel Gorman  * b) The target zone may be at high_wmark_pages(zone) but the lower zones
204141858966SMel Gorman  *    must go *over* high_wmark_pages(zone) to satisfy the `incremental min'
204241858966SMel Gorman  *    zone defense algorithm.
20431da177e4SLinus Torvalds  *
20441da177e4SLinus Torvalds  * If a zone is deemed to be full of pinned pages then just give it a light
20451da177e4SLinus Torvalds  * scan then give up on it.
2046e0c23279SMel Gorman  *
2047e0c23279SMel Gorman  * This function returns true if a zone is being reclaimed for a costly
2048fe4b1b24SMel Gorman  * high-order allocation and compaction is ready to begin. This indicates to
20490cee34fdSMel Gorman  * the caller that it should consider retrying the allocation instead of
20500cee34fdSMel Gorman  * further reclaim.
20511da177e4SLinus Torvalds  */
20529e3b2f8cSKonstantin Khlebnikov static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
20531da177e4SLinus Torvalds {
2054dd1a239fSMel Gorman 	struct zoneref *z;
205554a6eb5cSMel Gorman 	struct zone *zone;
2056d149e3b2SYing Han 	unsigned long nr_soft_reclaimed;
2057d149e3b2SYing Han 	unsigned long nr_soft_scanned;
20580cee34fdSMel Gorman 	bool aborted_reclaim = false;
20591cfb419bSKAMEZAWA Hiroyuki 
2060cc715d99SMel Gorman 	/*
2061cc715d99SMel Gorman 	 * If the number of buffer_heads in the machine exceeds the maximum
2062cc715d99SMel Gorman 	 * allowed level, force direct reclaim to scan the highmem zone as
2063cc715d99SMel Gorman 	 * highmem pages could be pinning lowmem pages storing buffer_heads
2064cc715d99SMel Gorman 	 */
2065cc715d99SMel Gorman 	if (buffer_heads_over_limit)
2066cc715d99SMel Gorman 		sc->gfp_mask |= __GFP_HIGHMEM;
2067cc715d99SMel Gorman 
2068d4debc66SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
2069d4debc66SMel Gorman 					gfp_zone(sc->gfp_mask), sc->nodemask) {
2070f3fe6512SCon Kolivas 		if (!populated_zone(zone))
20711da177e4SLinus Torvalds 			continue;
20721cfb419bSKAMEZAWA Hiroyuki 		/*
20731cfb419bSKAMEZAWA Hiroyuki 		 * Take care memory controller reclaiming has small influence
20741cfb419bSKAMEZAWA Hiroyuki 		 * to global LRU.
20751cfb419bSKAMEZAWA Hiroyuki 		 */
207689b5fae5SJohannes Weiner 		if (global_reclaim(sc)) {
207702a0e53dSPaul Jackson 			if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
20781da177e4SLinus Torvalds 				continue;
20799e3b2f8cSKonstantin Khlebnikov 			if (zone->all_unreclaimable &&
20809e3b2f8cSKonstantin Khlebnikov 					sc->priority != DEF_PRIORITY)
20811da177e4SLinus Torvalds 				continue;	/* Let kswapd poll it */
2082d84da3f9SKirill A. Shutemov 			if (IS_ENABLED(CONFIG_COMPACTION)) {
2083e0887c19SRik van Riel 				/*
2084e0c23279SMel Gorman 				 * If we already have plenty of memory free for
2085e0c23279SMel Gorman 				 * compaction in this zone, don't free any more.
2086e0c23279SMel Gorman 				 * Even though compaction is invoked for any
2087e0c23279SMel Gorman 				 * non-zero order, only frequent costly order
2088e0c23279SMel Gorman 				 * reclamation is disruptive enough to become a
2089c7cfa37bSCopot Alexandru 				 * noticeable problem, like transparent huge
2090c7cfa37bSCopot Alexandru 				 * page allocations.
2091e0887c19SRik van Riel 				 */
2092fe4b1b24SMel Gorman 				if (compaction_ready(zone, sc)) {
20930cee34fdSMel Gorman 					aborted_reclaim = true;
2094e0887c19SRik van Riel 					continue;
2095e0887c19SRik van Riel 				}
2096e0c23279SMel Gorman 			}
2097ac34a1a3SKAMEZAWA Hiroyuki 			/*
2098ac34a1a3SKAMEZAWA Hiroyuki 			 * This steals pages from memory cgroups over softlimit
2099ac34a1a3SKAMEZAWA Hiroyuki 			 * and returns the number of reclaimed pages and
2100ac34a1a3SKAMEZAWA Hiroyuki 			 * scanned pages. This works for global memory pressure
2101ac34a1a3SKAMEZAWA Hiroyuki 			 * and balancing, not for a memcg's limit.
2102ac34a1a3SKAMEZAWA Hiroyuki 			 */
2103d149e3b2SYing Han 			nr_soft_scanned = 0;
2104d149e3b2SYing Han 			nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
2105d149e3b2SYing Han 						sc->order, sc->gfp_mask,
2106d149e3b2SYing Han 						&nr_soft_scanned);
2107d149e3b2SYing Han 			sc->nr_reclaimed += nr_soft_reclaimed;
2108ac34a1a3SKAMEZAWA Hiroyuki 			sc->nr_scanned += nr_soft_scanned;
2109ac34a1a3SKAMEZAWA Hiroyuki 			/* need some check for avoid more shrink_zone() */
2110ac34a1a3SKAMEZAWA Hiroyuki 		}
2111d149e3b2SYing Han 
21129e3b2f8cSKonstantin Khlebnikov 		shrink_zone(zone, sc);
21131da177e4SLinus Torvalds 	}
2114e0c23279SMel Gorman 
21150cee34fdSMel Gorman 	return aborted_reclaim;
2116d1908362SMinchan Kim }
2117d1908362SMinchan Kim 
2118d1908362SMinchan Kim static bool zone_reclaimable(struct zone *zone)
2119d1908362SMinchan Kim {
2120d1908362SMinchan Kim 	return zone->pages_scanned < zone_reclaimable_pages(zone) * 6;
2121d1908362SMinchan Kim }
2122d1908362SMinchan Kim 
2123929bea7cSKOSAKI Motohiro /* All zones in zonelist are unreclaimable? */
2124d1908362SMinchan Kim static bool all_unreclaimable(struct zonelist *zonelist,
2125d1908362SMinchan Kim 		struct scan_control *sc)
2126d1908362SMinchan Kim {
2127d1908362SMinchan Kim 	struct zoneref *z;
2128d1908362SMinchan Kim 	struct zone *zone;
2129d1908362SMinchan Kim 
2130d1908362SMinchan Kim 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
2131d1908362SMinchan Kim 			gfp_zone(sc->gfp_mask), sc->nodemask) {
2132d1908362SMinchan Kim 		if (!populated_zone(zone))
2133d1908362SMinchan Kim 			continue;
2134d1908362SMinchan Kim 		if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
2135d1908362SMinchan Kim 			continue;
2136929bea7cSKOSAKI Motohiro 		if (!zone->all_unreclaimable)
2137929bea7cSKOSAKI Motohiro 			return false;
2138d1908362SMinchan Kim 	}
2139d1908362SMinchan Kim 
2140929bea7cSKOSAKI Motohiro 	return true;
21411da177e4SLinus Torvalds }
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds /*
21441da177e4SLinus Torvalds  * This is the main entry point to direct page reclaim.
21451da177e4SLinus Torvalds  *
21461da177e4SLinus Torvalds  * If a full scan of the inactive list fails to free enough memory then we
21471da177e4SLinus Torvalds  * are "out of memory" and something needs to be killed.
21481da177e4SLinus Torvalds  *
21491da177e4SLinus Torvalds  * If the caller is !__GFP_FS then the probability of a failure is reasonably
21501da177e4SLinus Torvalds  * high - the zone may be full of dirty or under-writeback pages, which this
21515b0830cbSJens Axboe  * caller can't do much about.  We kick the writeback threads and take explicit
21525b0830cbSJens Axboe  * naps in the hope that some of these pages can be written.  But if the
21535b0830cbSJens Axboe  * allocating task holds filesystem locks which prevent writeout this might not
21545b0830cbSJens Axboe  * work, and the allocation attempt will fail.
2155a41f24eaSNishanth Aravamudan  *
2156a41f24eaSNishanth Aravamudan  * returns:	0, if no pages reclaimed
2157a41f24eaSNishanth Aravamudan  * 		else, the number of pages reclaimed
21581da177e4SLinus Torvalds  */
2159dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
2160a09ed5e0SYing Han 					struct scan_control *sc,
2161a09ed5e0SYing Han 					struct shrink_control *shrink)
21621da177e4SLinus Torvalds {
216369e05944SAndrew Morton 	unsigned long total_scanned = 0;
21641da177e4SLinus Torvalds 	struct reclaim_state *reclaim_state = current->reclaim_state;
2165dd1a239fSMel Gorman 	struct zoneref *z;
216654a6eb5cSMel Gorman 	struct zone *zone;
216722fba335SKOSAKI Motohiro 	unsigned long writeback_threshold;
21680cee34fdSMel Gorman 	bool aborted_reclaim;
21691da177e4SLinus Torvalds 
2170873b4771SKeika Kobayashi 	delayacct_freepages_start();
2171873b4771SKeika Kobayashi 
217289b5fae5SJohannes Weiner 	if (global_reclaim(sc))
2173f8891e5eSChristoph Lameter 		count_vm_event(ALLOCSTALL);
21741da177e4SLinus Torvalds 
21759e3b2f8cSKonstantin Khlebnikov 	do {
217670ddf637SAnton Vorontsov 		vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup,
217770ddf637SAnton Vorontsov 				sc->priority);
217866e1707bSBalbir Singh 		sc->nr_scanned = 0;
21799e3b2f8cSKonstantin Khlebnikov 		aborted_reclaim = shrink_zones(zonelist, sc);
2180e0c23279SMel Gorman 
218166e1707bSBalbir Singh 		/*
218266e1707bSBalbir Singh 		 * Don't shrink slabs when reclaiming memory from
218366e1707bSBalbir Singh 		 * over limit cgroups
218466e1707bSBalbir Singh 		 */
218589b5fae5SJohannes Weiner 		if (global_reclaim(sc)) {
2186c6a8a8c5SKOSAKI Motohiro 			unsigned long lru_pages = 0;
2187d4debc66SMel Gorman 			for_each_zone_zonelist(zone, z, zonelist,
2188d4debc66SMel Gorman 					gfp_zone(sc->gfp_mask)) {
2189c6a8a8c5SKOSAKI Motohiro 				if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
2190c6a8a8c5SKOSAKI Motohiro 					continue;
2191c6a8a8c5SKOSAKI Motohiro 
2192c6a8a8c5SKOSAKI Motohiro 				lru_pages += zone_reclaimable_pages(zone);
2193c6a8a8c5SKOSAKI Motohiro 			}
2194c6a8a8c5SKOSAKI Motohiro 
21951495f230SYing Han 			shrink_slab(shrink, sc->nr_scanned, lru_pages);
21961da177e4SLinus Torvalds 			if (reclaim_state) {
2197a79311c1SRik van Riel 				sc->nr_reclaimed += reclaim_state->reclaimed_slab;
21981da177e4SLinus Torvalds 				reclaim_state->reclaimed_slab = 0;
21991da177e4SLinus Torvalds 			}
220091a45470SKAMEZAWA Hiroyuki 		}
220166e1707bSBalbir Singh 		total_scanned += sc->nr_scanned;
2202bb21c7ceSKOSAKI Motohiro 		if (sc->nr_reclaimed >= sc->nr_to_reclaim)
22031da177e4SLinus Torvalds 			goto out;
22041da177e4SLinus Torvalds 
22051da177e4SLinus Torvalds 		/*
22060e50ce3bSMinchan Kim 		 * If we're getting trouble reclaiming, start doing
22070e50ce3bSMinchan Kim 		 * writepage even in laptop mode.
22080e50ce3bSMinchan Kim 		 */
22090e50ce3bSMinchan Kim 		if (sc->priority < DEF_PRIORITY - 2)
22100e50ce3bSMinchan Kim 			sc->may_writepage = 1;
22110e50ce3bSMinchan Kim 
22120e50ce3bSMinchan Kim 		/*
22131da177e4SLinus Torvalds 		 * Try to write back as many pages as we just scanned.  This
22141da177e4SLinus Torvalds 		 * tends to cause slow streaming writers to write data to the
22151da177e4SLinus Torvalds 		 * disk smoothly, at the dirtying rate, which is nice.   But
22161da177e4SLinus Torvalds 		 * that's undesirable in laptop mode, where we *want* lumpy
22171da177e4SLinus Torvalds 		 * writeout.  So in laptop mode, write out the whole world.
22181da177e4SLinus Torvalds 		 */
221922fba335SKOSAKI Motohiro 		writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
222022fba335SKOSAKI Motohiro 		if (total_scanned > writeback_threshold) {
22210e175a18SCurt Wohlgemuth 			wakeup_flusher_threads(laptop_mode ? 0 : total_scanned,
22220e175a18SCurt Wohlgemuth 						WB_REASON_TRY_TO_FREE_PAGES);
222366e1707bSBalbir Singh 			sc->may_writepage = 1;
22241da177e4SLinus Torvalds 		}
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 		/* Take a nap, wait for some writeback to complete */
22277b51755cSKOSAKI Motohiro 		if (!sc->hibernation_mode && sc->nr_scanned &&
22289e3b2f8cSKonstantin Khlebnikov 		    sc->priority < DEF_PRIORITY - 2) {
22290e093d99SMel Gorman 			struct zone *preferred_zone;
22300e093d99SMel Gorman 
22310e093d99SMel Gorman 			first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask),
2232f33261d7SDavid Rientjes 						&cpuset_current_mems_allowed,
2233f33261d7SDavid Rientjes 						&preferred_zone);
22340e093d99SMel Gorman 			wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10);
22350e093d99SMel Gorman 		}
22369e3b2f8cSKonstantin Khlebnikov 	} while (--sc->priority >= 0);
2237bb21c7ceSKOSAKI Motohiro 
22381da177e4SLinus Torvalds out:
2239873b4771SKeika Kobayashi 	delayacct_freepages_end();
2240873b4771SKeika Kobayashi 
2241bb21c7ceSKOSAKI Motohiro 	if (sc->nr_reclaimed)
2242bb21c7ceSKOSAKI Motohiro 		return sc->nr_reclaimed;
2243bb21c7ceSKOSAKI Motohiro 
2244929bea7cSKOSAKI Motohiro 	/*
2245929bea7cSKOSAKI Motohiro 	 * As hibernation is going on, kswapd is freezed so that it can't mark
2246929bea7cSKOSAKI Motohiro 	 * the zone into all_unreclaimable. Thus bypassing all_unreclaimable
2247929bea7cSKOSAKI Motohiro 	 * check.
2248929bea7cSKOSAKI Motohiro 	 */
2249929bea7cSKOSAKI Motohiro 	if (oom_killer_disabled)
2250929bea7cSKOSAKI Motohiro 		return 0;
2251929bea7cSKOSAKI Motohiro 
22520cee34fdSMel Gorman 	/* Aborted reclaim to try compaction? don't OOM, then */
22530cee34fdSMel Gorman 	if (aborted_reclaim)
22547335084dSMel Gorman 		return 1;
22557335084dSMel Gorman 
2256bb21c7ceSKOSAKI Motohiro 	/* top priority shrink_zones still had more to do? don't OOM, then */
225789b5fae5SJohannes Weiner 	if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc))
2258bb21c7ceSKOSAKI Motohiro 		return 1;
2259bb21c7ceSKOSAKI Motohiro 
2260bb21c7ceSKOSAKI Motohiro 	return 0;
22611da177e4SLinus Torvalds }
22621da177e4SLinus Torvalds 
22635515061dSMel Gorman static bool pfmemalloc_watermark_ok(pg_data_t *pgdat)
22645515061dSMel Gorman {
22655515061dSMel Gorman 	struct zone *zone;
22665515061dSMel Gorman 	unsigned long pfmemalloc_reserve = 0;
22675515061dSMel Gorman 	unsigned long free_pages = 0;
22685515061dSMel Gorman 	int i;
22695515061dSMel Gorman 	bool wmark_ok;
22705515061dSMel Gorman 
22715515061dSMel Gorman 	for (i = 0; i <= ZONE_NORMAL; i++) {
22725515061dSMel Gorman 		zone = &pgdat->node_zones[i];
22735515061dSMel Gorman 		pfmemalloc_reserve += min_wmark_pages(zone);
22745515061dSMel Gorman 		free_pages += zone_page_state(zone, NR_FREE_PAGES);
22755515061dSMel Gorman 	}
22765515061dSMel Gorman 
22775515061dSMel Gorman 	wmark_ok = free_pages > pfmemalloc_reserve / 2;
22785515061dSMel Gorman 
22795515061dSMel Gorman 	/* kswapd must be awake if processes are being throttled */
22805515061dSMel Gorman 	if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
22815515061dSMel Gorman 		pgdat->classzone_idx = min(pgdat->classzone_idx,
22825515061dSMel Gorman 						(enum zone_type)ZONE_NORMAL);
22835515061dSMel Gorman 		wake_up_interruptible(&pgdat->kswapd_wait);
22845515061dSMel Gorman 	}
22855515061dSMel Gorman 
22865515061dSMel Gorman 	return wmark_ok;
22875515061dSMel Gorman }
22885515061dSMel Gorman 
22895515061dSMel Gorman /*
22905515061dSMel Gorman  * Throttle direct reclaimers if backing storage is backed by the network
22915515061dSMel Gorman  * and the PFMEMALLOC reserve for the preferred node is getting dangerously
22925515061dSMel Gorman  * depleted. kswapd will continue to make progress and wake the processes
229350694c28SMel Gorman  * when the low watermark is reached.
229450694c28SMel Gorman  *
229550694c28SMel Gorman  * Returns true if a fatal signal was delivered during throttling. If this
229650694c28SMel Gorman  * happens, the page allocator should not consider triggering the OOM killer.
22975515061dSMel Gorman  */
229850694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
22995515061dSMel Gorman 					nodemask_t *nodemask)
23005515061dSMel Gorman {
23015515061dSMel Gorman 	struct zone *zone;
23025515061dSMel Gorman 	int high_zoneidx = gfp_zone(gfp_mask);
23035515061dSMel Gorman 	pg_data_t *pgdat;
23045515061dSMel Gorman 
23055515061dSMel Gorman 	/*
23065515061dSMel Gorman 	 * Kernel threads should not be throttled as they may be indirectly
23075515061dSMel Gorman 	 * responsible for cleaning pages necessary for reclaim to make forward
23085515061dSMel Gorman 	 * progress. kjournald for example may enter direct reclaim while
23095515061dSMel Gorman 	 * committing a transaction where throttling it could forcing other
23105515061dSMel Gorman 	 * processes to block on log_wait_commit().
23115515061dSMel Gorman 	 */
23125515061dSMel Gorman 	if (current->flags & PF_KTHREAD)
231350694c28SMel Gorman 		goto out;
231450694c28SMel Gorman 
231550694c28SMel Gorman 	/*
231650694c28SMel Gorman 	 * If a fatal signal is pending, this process should not throttle.
231750694c28SMel Gorman 	 * It should return quickly so it can exit and free its memory
231850694c28SMel Gorman 	 */
231950694c28SMel Gorman 	if (fatal_signal_pending(current))
232050694c28SMel Gorman 		goto out;
23215515061dSMel Gorman 
23225515061dSMel Gorman 	/* Check if the pfmemalloc reserves are ok */
23235515061dSMel Gorman 	first_zones_zonelist(zonelist, high_zoneidx, NULL, &zone);
23245515061dSMel Gorman 	pgdat = zone->zone_pgdat;
23255515061dSMel Gorman 	if (pfmemalloc_watermark_ok(pgdat))
232650694c28SMel Gorman 		goto out;
23275515061dSMel Gorman 
232868243e76SMel Gorman 	/* Account for the throttling */
232968243e76SMel Gorman 	count_vm_event(PGSCAN_DIRECT_THROTTLE);
233068243e76SMel Gorman 
23315515061dSMel Gorman 	/*
23325515061dSMel Gorman 	 * If the caller cannot enter the filesystem, it's possible that it
23335515061dSMel Gorman 	 * is due to the caller holding an FS lock or performing a journal
23345515061dSMel Gorman 	 * transaction in the case of a filesystem like ext[3|4]. In this case,
23355515061dSMel Gorman 	 * it is not safe to block on pfmemalloc_wait as kswapd could be
23365515061dSMel Gorman 	 * blocked waiting on the same lock. Instead, throttle for up to a
23375515061dSMel Gorman 	 * second before continuing.
23385515061dSMel Gorman 	 */
23395515061dSMel Gorman 	if (!(gfp_mask & __GFP_FS)) {
23405515061dSMel Gorman 		wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
23415515061dSMel Gorman 			pfmemalloc_watermark_ok(pgdat), HZ);
234250694c28SMel Gorman 
234350694c28SMel Gorman 		goto check_pending;
23445515061dSMel Gorman 	}
23455515061dSMel Gorman 
23465515061dSMel Gorman 	/* Throttle until kswapd wakes the process */
23475515061dSMel Gorman 	wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
23485515061dSMel Gorman 		pfmemalloc_watermark_ok(pgdat));
234950694c28SMel Gorman 
235050694c28SMel Gorman check_pending:
235150694c28SMel Gorman 	if (fatal_signal_pending(current))
235250694c28SMel Gorman 		return true;
235350694c28SMel Gorman 
235450694c28SMel Gorman out:
235550694c28SMel Gorman 	return false;
23565515061dSMel Gorman }
23575515061dSMel Gorman 
2358dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
2359327c0e96SKAMEZAWA Hiroyuki 				gfp_t gfp_mask, nodemask_t *nodemask)
236066e1707bSBalbir Singh {
236133906bc5SMel Gorman 	unsigned long nr_reclaimed;
236266e1707bSBalbir Singh 	struct scan_control sc = {
236321caf2fcSMing Lei 		.gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)),
236466e1707bSBalbir Singh 		.may_writepage = !laptop_mode,
236522fba335SKOSAKI Motohiro 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
2366a6dc60f8SJohannes Weiner 		.may_unmap = 1,
23672e2e4259SKOSAKI Motohiro 		.may_swap = 1,
236866e1707bSBalbir Singh 		.order = order,
23699e3b2f8cSKonstantin Khlebnikov 		.priority = DEF_PRIORITY,
2370f16015fbSJohannes Weiner 		.target_mem_cgroup = NULL,
2371327c0e96SKAMEZAWA Hiroyuki 		.nodemask = nodemask,
237266e1707bSBalbir Singh 	};
2373a09ed5e0SYing Han 	struct shrink_control shrink = {
2374a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
2375a09ed5e0SYing Han 	};
237666e1707bSBalbir Singh 
23775515061dSMel Gorman 	/*
237850694c28SMel Gorman 	 * Do not enter reclaim if fatal signal was delivered while throttled.
237950694c28SMel Gorman 	 * 1 is returned so that the page allocator does not OOM kill at this
238050694c28SMel Gorman 	 * point.
23815515061dSMel Gorman 	 */
238250694c28SMel Gorman 	if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask))
23835515061dSMel Gorman 		return 1;
23845515061dSMel Gorman 
238533906bc5SMel Gorman 	trace_mm_vmscan_direct_reclaim_begin(order,
238633906bc5SMel Gorman 				sc.may_writepage,
238733906bc5SMel Gorman 				gfp_mask);
238833906bc5SMel Gorman 
2389a09ed5e0SYing Han 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
239033906bc5SMel Gorman 
239133906bc5SMel Gorman 	trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
239233906bc5SMel Gorman 
239333906bc5SMel Gorman 	return nr_reclaimed;
239466e1707bSBalbir Singh }
239566e1707bSBalbir Singh 
2396c255a458SAndrew Morton #ifdef CONFIG_MEMCG
239766e1707bSBalbir Singh 
239872835c86SJohannes Weiner unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg,
23994e416953SBalbir Singh 						gfp_t gfp_mask, bool noswap,
24000ae5e89cSYing Han 						struct zone *zone,
24010ae5e89cSYing Han 						unsigned long *nr_scanned)
24024e416953SBalbir Singh {
24034e416953SBalbir Singh 	struct scan_control sc = {
24040ae5e89cSYing Han 		.nr_scanned = 0,
2405b8f5c566SKOSAKI Motohiro 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
24064e416953SBalbir Singh 		.may_writepage = !laptop_mode,
24074e416953SBalbir Singh 		.may_unmap = 1,
24084e416953SBalbir Singh 		.may_swap = !noswap,
24094e416953SBalbir Singh 		.order = 0,
24109e3b2f8cSKonstantin Khlebnikov 		.priority = 0,
241172835c86SJohannes Weiner 		.target_mem_cgroup = memcg,
24124e416953SBalbir Singh 	};
2413f9be23d6SKonstantin Khlebnikov 	struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
24140ae5e89cSYing Han 
24154e416953SBalbir Singh 	sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
24164e416953SBalbir Singh 			(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
2417bdce6d9eSKOSAKI Motohiro 
24189e3b2f8cSKonstantin Khlebnikov 	trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
2419bdce6d9eSKOSAKI Motohiro 						      sc.may_writepage,
2420bdce6d9eSKOSAKI Motohiro 						      sc.gfp_mask);
2421bdce6d9eSKOSAKI Motohiro 
24224e416953SBalbir Singh 	/*
24234e416953SBalbir Singh 	 * NOTE: Although we can get the priority field, using it
24244e416953SBalbir Singh 	 * here is not a good idea, since it limits the pages we can scan.
24254e416953SBalbir Singh 	 * if we don't reclaim here, the shrink_zone from balance_pgdat
24264e416953SBalbir Singh 	 * will pick up pages from other mem cgroup's as well. We hack
24274e416953SBalbir Singh 	 * the priority and make it zero.
24284e416953SBalbir Singh 	 */
2429f9be23d6SKonstantin Khlebnikov 	shrink_lruvec(lruvec, &sc);
2430bdce6d9eSKOSAKI Motohiro 
2431bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
2432bdce6d9eSKOSAKI Motohiro 
24330ae5e89cSYing Han 	*nr_scanned = sc.nr_scanned;
24344e416953SBalbir Singh 	return sc.nr_reclaimed;
24354e416953SBalbir Singh }
24364e416953SBalbir Singh 
243772835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
24388c7c6e34SKAMEZAWA Hiroyuki 					   gfp_t gfp_mask,
2439185efc0fSJohannes Weiner 					   bool noswap)
244066e1707bSBalbir Singh {
24414e416953SBalbir Singh 	struct zonelist *zonelist;
2442bdce6d9eSKOSAKI Motohiro 	unsigned long nr_reclaimed;
2443889976dbSYing Han 	int nid;
244466e1707bSBalbir Singh 	struct scan_control sc = {
244566e1707bSBalbir Singh 		.may_writepage = !laptop_mode,
2446a6dc60f8SJohannes Weiner 		.may_unmap = 1,
24472e2e4259SKOSAKI Motohiro 		.may_swap = !noswap,
244822fba335SKOSAKI Motohiro 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
244966e1707bSBalbir Singh 		.order = 0,
24509e3b2f8cSKonstantin Khlebnikov 		.priority = DEF_PRIORITY,
245172835c86SJohannes Weiner 		.target_mem_cgroup = memcg,
2452327c0e96SKAMEZAWA Hiroyuki 		.nodemask = NULL, /* we don't care the placement */
2453a09ed5e0SYing Han 		.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
2454a09ed5e0SYing Han 				(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
2455a09ed5e0SYing Han 	};
2456a09ed5e0SYing Han 	struct shrink_control shrink = {
2457a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
245866e1707bSBalbir Singh 	};
245966e1707bSBalbir Singh 
2460889976dbSYing Han 	/*
2461889976dbSYing Han 	 * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't
2462889976dbSYing Han 	 * take care of from where we get pages. So the node where we start the
2463889976dbSYing Han 	 * scan does not need to be the current node.
2464889976dbSYing Han 	 */
246572835c86SJohannes Weiner 	nid = mem_cgroup_select_victim_node(memcg);
2466889976dbSYing Han 
2467889976dbSYing Han 	zonelist = NODE_DATA(nid)->node_zonelists;
2468bdce6d9eSKOSAKI Motohiro 
2469bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_reclaim_begin(0,
2470bdce6d9eSKOSAKI Motohiro 					    sc.may_writepage,
2471bdce6d9eSKOSAKI Motohiro 					    sc.gfp_mask);
2472bdce6d9eSKOSAKI Motohiro 
2473a09ed5e0SYing Han 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
2474bdce6d9eSKOSAKI Motohiro 
2475bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
2476bdce6d9eSKOSAKI Motohiro 
2477bdce6d9eSKOSAKI Motohiro 	return nr_reclaimed;
247866e1707bSBalbir Singh }
247966e1707bSBalbir Singh #endif
248066e1707bSBalbir Singh 
24819e3b2f8cSKonstantin Khlebnikov static void age_active_anon(struct zone *zone, struct scan_control *sc)
2482f16015fbSJohannes Weiner {
2483b95a2f2dSJohannes Weiner 	struct mem_cgroup *memcg;
2484b95a2f2dSJohannes Weiner 
2485b95a2f2dSJohannes Weiner 	if (!total_swap_pages)
2486b95a2f2dSJohannes Weiner 		return;
2487b95a2f2dSJohannes Weiner 
2488b95a2f2dSJohannes Weiner 	memcg = mem_cgroup_iter(NULL, NULL, NULL);
2489b95a2f2dSJohannes Weiner 	do {
2490c56d5c7dSKonstantin Khlebnikov 		struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
2491f16015fbSJohannes Weiner 
2492c56d5c7dSKonstantin Khlebnikov 		if (inactive_anon_is_low(lruvec))
24931a93be0eSKonstantin Khlebnikov 			shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
24949e3b2f8cSKonstantin Khlebnikov 					   sc, LRU_ACTIVE_ANON);
2495b95a2f2dSJohannes Weiner 
2496b95a2f2dSJohannes Weiner 		memcg = mem_cgroup_iter(NULL, memcg, NULL);
2497b95a2f2dSJohannes Weiner 	} while (memcg);
2498f16015fbSJohannes Weiner }
2499f16015fbSJohannes Weiner 
250060cefed4SJohannes Weiner static bool zone_balanced(struct zone *zone, int order,
250160cefed4SJohannes Weiner 			  unsigned long balance_gap, int classzone_idx)
250260cefed4SJohannes Weiner {
250360cefed4SJohannes Weiner 	if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone) +
250460cefed4SJohannes Weiner 				    balance_gap, classzone_idx, 0))
250560cefed4SJohannes Weiner 		return false;
250660cefed4SJohannes Weiner 
2507d84da3f9SKirill A. Shutemov 	if (IS_ENABLED(CONFIG_COMPACTION) && order &&
2508d84da3f9SKirill A. Shutemov 	    !compaction_suitable(zone, order))
250960cefed4SJohannes Weiner 		return false;
251060cefed4SJohannes Weiner 
251160cefed4SJohannes Weiner 	return true;
251260cefed4SJohannes Weiner }
251360cefed4SJohannes Weiner 
25141741c877SMel Gorman /*
25154ae0a48bSZlatko Calusic  * pgdat_balanced() is used when checking if a node is balanced.
25164ae0a48bSZlatko Calusic  *
25174ae0a48bSZlatko Calusic  * For order-0, all zones must be balanced!
25184ae0a48bSZlatko Calusic  *
25194ae0a48bSZlatko Calusic  * For high-order allocations only zones that meet watermarks and are in a
25204ae0a48bSZlatko Calusic  * zone allowed by the callers classzone_idx are added to balanced_pages. The
25214ae0a48bSZlatko Calusic  * total of balanced pages must be at least 25% of the zones allowed by
25224ae0a48bSZlatko Calusic  * classzone_idx for the node to be considered balanced. Forcing all zones to
25234ae0a48bSZlatko Calusic  * be balanced for high orders can cause excessive reclaim when there are
25244ae0a48bSZlatko Calusic  * imbalanced zones.
25251741c877SMel Gorman  * The choice of 25% is due to
25261741c877SMel Gorman  *   o a 16M DMA zone that is balanced will not balance a zone on any
25271741c877SMel Gorman  *     reasonable sized machine
25281741c877SMel Gorman  *   o On all other machines, the top zone must be at least a reasonable
252925985edcSLucas De Marchi  *     percentage of the middle zones. For example, on 32-bit x86, highmem
25301741c877SMel Gorman  *     would need to be at least 256M for it to be balance a whole node.
25311741c877SMel Gorman  *     Similarly, on x86-64 the Normal zone would need to be at least 1G
25321741c877SMel Gorman  *     to balance a node on its own. These seemed like reasonable ratios.
25331741c877SMel Gorman  */
25344ae0a48bSZlatko Calusic static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx)
25351741c877SMel Gorman {
2536b40da049SJiang Liu 	unsigned long managed_pages = 0;
25374ae0a48bSZlatko Calusic 	unsigned long balanced_pages = 0;
25381741c877SMel Gorman 	int i;
25391741c877SMel Gorman 
25404ae0a48bSZlatko Calusic 	/* Check the watermark levels */
25414ae0a48bSZlatko Calusic 	for (i = 0; i <= classzone_idx; i++) {
25424ae0a48bSZlatko Calusic 		struct zone *zone = pgdat->node_zones + i;
25431741c877SMel Gorman 
25444ae0a48bSZlatko Calusic 		if (!populated_zone(zone))
25454ae0a48bSZlatko Calusic 			continue;
25464ae0a48bSZlatko Calusic 
2547b40da049SJiang Liu 		managed_pages += zone->managed_pages;
25484ae0a48bSZlatko Calusic 
25494ae0a48bSZlatko Calusic 		/*
25504ae0a48bSZlatko Calusic 		 * A special case here:
25514ae0a48bSZlatko Calusic 		 *
25524ae0a48bSZlatko Calusic 		 * balance_pgdat() skips over all_unreclaimable after
25534ae0a48bSZlatko Calusic 		 * DEF_PRIORITY. Effectively, it considers them balanced so
25544ae0a48bSZlatko Calusic 		 * they must be considered balanced here as well!
25554ae0a48bSZlatko Calusic 		 */
25564ae0a48bSZlatko Calusic 		if (zone->all_unreclaimable) {
2557b40da049SJiang Liu 			balanced_pages += zone->managed_pages;
25584ae0a48bSZlatko Calusic 			continue;
25594ae0a48bSZlatko Calusic 		}
25604ae0a48bSZlatko Calusic 
25614ae0a48bSZlatko Calusic 		if (zone_balanced(zone, order, 0, i))
2562b40da049SJiang Liu 			balanced_pages += zone->managed_pages;
25634ae0a48bSZlatko Calusic 		else if (!order)
25644ae0a48bSZlatko Calusic 			return false;
25654ae0a48bSZlatko Calusic 	}
25664ae0a48bSZlatko Calusic 
25674ae0a48bSZlatko Calusic 	if (order)
2568b40da049SJiang Liu 		return balanced_pages >= (managed_pages >> 2);
25694ae0a48bSZlatko Calusic 	else
25704ae0a48bSZlatko Calusic 		return true;
25711741c877SMel Gorman }
25721741c877SMel Gorman 
25735515061dSMel Gorman /*
25745515061dSMel Gorman  * Prepare kswapd for sleeping. This verifies that there are no processes
25755515061dSMel Gorman  * waiting in throttle_direct_reclaim() and that watermarks have been met.
25765515061dSMel Gorman  *
25775515061dSMel Gorman  * Returns true if kswapd is ready to sleep
25785515061dSMel Gorman  */
25795515061dSMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, long remaining,
2580dc83edd9SMel Gorman 					int classzone_idx)
2581f50de2d3SMel Gorman {
2582f50de2d3SMel Gorman 	/* If a direct reclaimer woke kswapd within HZ/10, it's premature */
2583f50de2d3SMel Gorman 	if (remaining)
25845515061dSMel Gorman 		return false;
25855515061dSMel Gorman 
25865515061dSMel Gorman 	/*
25875515061dSMel Gorman 	 * There is a potential race between when kswapd checks its watermarks
25885515061dSMel Gorman 	 * and a process gets throttled. There is also a potential race if
25895515061dSMel Gorman 	 * processes get throttled, kswapd wakes, a large process exits therby
25905515061dSMel Gorman 	 * balancing the zones that causes kswapd to miss a wakeup. If kswapd
25915515061dSMel Gorman 	 * is going to sleep, no process should be sleeping on pfmemalloc_wait
25925515061dSMel Gorman 	 * so wake them now if necessary. If necessary, processes will wake
25935515061dSMel Gorman 	 * kswapd and get throttled again
25945515061dSMel Gorman 	 */
25955515061dSMel Gorman 	if (waitqueue_active(&pgdat->pfmemalloc_wait)) {
25965515061dSMel Gorman 		wake_up(&pgdat->pfmemalloc_wait);
25975515061dSMel Gorman 		return false;
25985515061dSMel Gorman 	}
2599f50de2d3SMel Gorman 
26004ae0a48bSZlatko Calusic 	return pgdat_balanced(pgdat, order, classzone_idx);
2601f50de2d3SMel Gorman }
2602f50de2d3SMel Gorman 
26031da177e4SLinus Torvalds /*
26041da177e4SLinus Torvalds  * For kswapd, balance_pgdat() will work across all this node's zones until
260541858966SMel Gorman  * they are all at high_wmark_pages(zone).
26061da177e4SLinus Torvalds  *
26070abdee2bSMel Gorman  * Returns the final order kswapd was reclaiming at
26081da177e4SLinus Torvalds  *
26091da177e4SLinus Torvalds  * There is special handling here for zones which are full of pinned pages.
26101da177e4SLinus Torvalds  * This can happen if the pages are all mlocked, or if they are all used by
26111da177e4SLinus Torvalds  * device drivers (say, ZONE_DMA).  Or if they are all in use by hugetlb.
26121da177e4SLinus Torvalds  * What we do is to detect the case where all pages in the zone have been
26131da177e4SLinus Torvalds  * scanned twice and there has been zero successful reclaim.  Mark the zone as
26141da177e4SLinus Torvalds  * dead and from now on, only perform a short scan.  Basically we're polling
26151da177e4SLinus Torvalds  * the zone for when the problem goes away.
26161da177e4SLinus Torvalds  *
26171da177e4SLinus Torvalds  * kswapd scans the zones in the highmem->normal->dma direction.  It skips
261841858966SMel Gorman  * zones which have free_pages > high_wmark_pages(zone), but once a zone is
261941858966SMel Gorman  * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the
262041858966SMel Gorman  * lower zones regardless of the number of free pages in the lower zones. This
262141858966SMel Gorman  * interoperates with the page allocator fallback scheme to ensure that aging
262241858966SMel Gorman  * of pages is balanced across the zones.
26231da177e4SLinus Torvalds  */
262499504748SMel Gorman static unsigned long balance_pgdat(pg_data_t *pgdat, int order,
2625dc83edd9SMel Gorman 							int *classzone_idx)
26261da177e4SLinus Torvalds {
2627dafcb73eSZlatko Calusic 	bool pgdat_is_balanced = false;
26281da177e4SLinus Torvalds 	int i;
262999504748SMel Gorman 	int end_zone = 0;	/* Inclusive.  0 = ZONE_DMA */
26301da177e4SLinus Torvalds 	struct reclaim_state *reclaim_state = current->reclaim_state;
26310ae5e89cSYing Han 	unsigned long nr_soft_reclaimed;
26320ae5e89cSYing Han 	unsigned long nr_soft_scanned;
2633179e9639SAndrew Morton 	struct scan_control sc = {
2634179e9639SAndrew Morton 		.gfp_mask = GFP_KERNEL,
2635a6dc60f8SJohannes Weiner 		.may_unmap = 1,
26362e2e4259SKOSAKI Motohiro 		.may_swap = 1,
263722fba335SKOSAKI Motohiro 		/*
263822fba335SKOSAKI Motohiro 		 * kswapd doesn't want to be bailed out while reclaim. because
263922fba335SKOSAKI Motohiro 		 * we want to put equal scanning pressure on each zone.
264022fba335SKOSAKI Motohiro 		 */
264122fba335SKOSAKI Motohiro 		.nr_to_reclaim = ULONG_MAX,
26425ad333ebSAndy Whitcroft 		.order = order,
2643f16015fbSJohannes Weiner 		.target_mem_cgroup = NULL,
2644179e9639SAndrew Morton 	};
2645a09ed5e0SYing Han 	struct shrink_control shrink = {
2646a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
2647a09ed5e0SYing Han 	};
26481da177e4SLinus Torvalds loop_again:
26499e3b2f8cSKonstantin Khlebnikov 	sc.priority = DEF_PRIORITY;
2650a79311c1SRik van Riel 	sc.nr_reclaimed = 0;
2651c0bbbc73SChristoph Lameter 	sc.may_writepage = !laptop_mode;
2652f8891e5eSChristoph Lameter 	count_vm_event(PAGEOUTRUN);
26531da177e4SLinus Torvalds 
26549e3b2f8cSKonstantin Khlebnikov 	do {
26551da177e4SLinus Torvalds 		unsigned long lru_pages = 0;
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds 		/*
26581da177e4SLinus Torvalds 		 * Scan in the highmem->dma direction for the highest
26591da177e4SLinus Torvalds 		 * zone which needs scanning
26601da177e4SLinus Torvalds 		 */
26611da177e4SLinus Torvalds 		for (i = pgdat->nr_zones - 1; i >= 0; i--) {
26621da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
26631da177e4SLinus Torvalds 
2664f3fe6512SCon Kolivas 			if (!populated_zone(zone))
26651da177e4SLinus Torvalds 				continue;
26661da177e4SLinus Torvalds 
26679e3b2f8cSKonstantin Khlebnikov 			if (zone->all_unreclaimable &&
26689e3b2f8cSKonstantin Khlebnikov 			    sc.priority != DEF_PRIORITY)
26691da177e4SLinus Torvalds 				continue;
26701da177e4SLinus Torvalds 
2671556adecbSRik van Riel 			/*
2672556adecbSRik van Riel 			 * Do some background aging of the anon list, to give
2673556adecbSRik van Riel 			 * pages a chance to be referenced before reclaiming.
2674556adecbSRik van Riel 			 */
26759e3b2f8cSKonstantin Khlebnikov 			age_active_anon(zone, &sc);
2676556adecbSRik van Riel 
2677cc715d99SMel Gorman 			/*
2678cc715d99SMel Gorman 			 * If the number of buffer_heads in the machine
2679cc715d99SMel Gorman 			 * exceeds the maximum allowed level and this node
2680cc715d99SMel Gorman 			 * has a highmem zone, force kswapd to reclaim from
2681cc715d99SMel Gorman 			 * it to relieve lowmem pressure.
2682cc715d99SMel Gorman 			 */
2683cc715d99SMel Gorman 			if (buffer_heads_over_limit && is_highmem_idx(i)) {
2684cc715d99SMel Gorman 				end_zone = i;
2685cc715d99SMel Gorman 				break;
2686cc715d99SMel Gorman 			}
2687cc715d99SMel Gorman 
268860cefed4SJohannes Weiner 			if (!zone_balanced(zone, order, 0, 0)) {
26891da177e4SLinus Torvalds 				end_zone = i;
2690e1dbeda6SAndrew Morton 				break;
2691439423f6SShaohua Li 			} else {
2692439423f6SShaohua Li 				/* If balanced, clear the congested flag */
2693439423f6SShaohua Li 				zone_clear_flag(zone, ZONE_CONGESTED);
26941da177e4SLinus Torvalds 			}
26951da177e4SLinus Torvalds 		}
2696dafcb73eSZlatko Calusic 
2697dafcb73eSZlatko Calusic 		if (i < 0) {
2698dafcb73eSZlatko Calusic 			pgdat_is_balanced = true;
26991da177e4SLinus Torvalds 			goto out;
2700dafcb73eSZlatko Calusic 		}
2701e1dbeda6SAndrew Morton 
27021da177e4SLinus Torvalds 		for (i = 0; i <= end_zone; i++) {
27031da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
27041da177e4SLinus Torvalds 
2705adea02a1SWu Fengguang 			lru_pages += zone_reclaimable_pages(zone);
27061da177e4SLinus Torvalds 		}
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds 		/*
27091da177e4SLinus Torvalds 		 * Now scan the zone in the dma->highmem direction, stopping
27101da177e4SLinus Torvalds 		 * at the last zone which needs scanning.
27111da177e4SLinus Torvalds 		 *
27121da177e4SLinus Torvalds 		 * We do this because the page allocator works in the opposite
27131da177e4SLinus Torvalds 		 * direction.  This prevents the page allocator from allocating
27141da177e4SLinus Torvalds 		 * pages behind kswapd's direction of progress, which would
27151da177e4SLinus Torvalds 		 * cause too much scanning of the lower zones.
27161da177e4SLinus Torvalds 		 */
27171da177e4SLinus Torvalds 		for (i = 0; i <= end_zone; i++) {
27181da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
2719fe2c2a10SRik van Riel 			int nr_slab, testorder;
27208afdceceSMel Gorman 			unsigned long balance_gap;
27211da177e4SLinus Torvalds 
2722f3fe6512SCon Kolivas 			if (!populated_zone(zone))
27231da177e4SLinus Torvalds 				continue;
27241da177e4SLinus Torvalds 
27259e3b2f8cSKonstantin Khlebnikov 			if (zone->all_unreclaimable &&
27269e3b2f8cSKonstantin Khlebnikov 			    sc.priority != DEF_PRIORITY)
27271da177e4SLinus Torvalds 				continue;
27281da177e4SLinus Torvalds 
27291da177e4SLinus Torvalds 			sc.nr_scanned = 0;
27304e416953SBalbir Singh 
27310ae5e89cSYing Han 			nr_soft_scanned = 0;
27324e416953SBalbir Singh 			/*
27334e416953SBalbir Singh 			 * Call soft limit reclaim before calling shrink_zone.
27344e416953SBalbir Singh 			 */
27350ae5e89cSYing Han 			nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
27360ae5e89cSYing Han 							order, sc.gfp_mask,
27370ae5e89cSYing Han 							&nr_soft_scanned);
27380ae5e89cSYing Han 			sc.nr_reclaimed += nr_soft_reclaimed;
273900918b6aSKOSAKI Motohiro 
274032a4330dSRik van Riel 			/*
27418afdceceSMel Gorman 			 * We put equal pressure on every zone, unless
27428afdceceSMel Gorman 			 * one zone has way too many pages free
27438afdceceSMel Gorman 			 * already. The "too many pages" is defined
27448afdceceSMel Gorman 			 * as the high wmark plus a "gap" where the
27458afdceceSMel Gorman 			 * gap is either the low watermark or 1%
27468afdceceSMel Gorman 			 * of the zone, whichever is smaller.
274732a4330dSRik van Riel 			 */
27488afdceceSMel Gorman 			balance_gap = min(low_wmark_pages(zone),
2749b40da049SJiang Liu 				(zone->managed_pages +
27508afdceceSMel Gorman 					KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
27518afdceceSMel Gorman 				KSWAPD_ZONE_BALANCE_GAP_RATIO);
2752fe2c2a10SRik van Riel 			/*
2753fe2c2a10SRik van Riel 			 * Kswapd reclaims only single pages with compaction
2754fe2c2a10SRik van Riel 			 * enabled. Trying too hard to reclaim until contiguous
2755fe2c2a10SRik van Riel 			 * free pages have become available can hurt performance
2756fe2c2a10SRik van Riel 			 * by evicting too much useful data from memory.
2757fe2c2a10SRik van Riel 			 * Do not reclaim more than needed for compaction.
2758fe2c2a10SRik van Riel 			 */
2759fe2c2a10SRik van Riel 			testorder = order;
2760d84da3f9SKirill A. Shutemov 			if (IS_ENABLED(CONFIG_COMPACTION) && order &&
2761fe2c2a10SRik van Riel 					compaction_suitable(zone, order) !=
2762fe2c2a10SRik van Riel 						COMPACT_SKIPPED)
2763fe2c2a10SRik van Riel 				testorder = 0;
2764fe2c2a10SRik van Riel 
2765cc715d99SMel Gorman 			if ((buffer_heads_over_limit && is_highmem_idx(i)) ||
276660cefed4SJohannes Weiner 			    !zone_balanced(zone, testorder,
276760cefed4SJohannes Weiner 					   balance_gap, end_zone)) {
27689e3b2f8cSKonstantin Khlebnikov 				shrink_zone(zone, &sc);
2769d7868daeSMel Gorman 
27701da177e4SLinus Torvalds 				reclaim_state->reclaimed_slab = 0;
27711495f230SYing Han 				nr_slab = shrink_slab(&shrink, sc.nr_scanned, lru_pages);
2772a79311c1SRik van Riel 				sc.nr_reclaimed += reclaim_state->reclaimed_slab;
27735a03b051SAndrea Arcangeli 
2774d7868daeSMel Gorman 				if (nr_slab == 0 && !zone_reclaimable(zone))
277593e4a89aSKOSAKI Motohiro 					zone->all_unreclaimable = 1;
2776d7868daeSMel Gorman 			}
2777d7868daeSMel Gorman 
27781da177e4SLinus Torvalds 			/*
27790e50ce3bSMinchan Kim 			 * If we're getting trouble reclaiming, start doing
27800e50ce3bSMinchan Kim 			 * writepage even in laptop mode.
27811da177e4SLinus Torvalds 			 */
27820e50ce3bSMinchan Kim 			if (sc.priority < DEF_PRIORITY - 2)
27831da177e4SLinus Torvalds 				sc.may_writepage = 1;
2784bb3ab596SKOSAKI Motohiro 
2785215ddd66SMel Gorman 			if (zone->all_unreclaimable) {
2786215ddd66SMel Gorman 				if (end_zone && end_zone == i)
2787215ddd66SMel Gorman 					end_zone--;
2788d7868daeSMel Gorman 				continue;
2789215ddd66SMel Gorman 			}
2790d7868daeSMel Gorman 
2791258401a6SZlatko Calusic 			if (zone_balanced(zone, testorder, 0, end_zone))
27920e093d99SMel Gorman 				/*
27930e093d99SMel Gorman 				 * If a zone reaches its high watermark,
27940e093d99SMel Gorman 				 * consider it to be no longer congested. It's
27950e093d99SMel Gorman 				 * possible there are dirty pages backed by
27960e093d99SMel Gorman 				 * congested BDIs but as pressure is relieved,
2797ab8704b8SWanpeng Li 				 * speculatively avoid congestion waits
27980e093d99SMel Gorman 				 */
27990e093d99SMel Gorman 				zone_clear_flag(zone, ZONE_CONGESTED);
280045973d74SMinchan Kim 		}
2801bb3ab596SKOSAKI Motohiro 
28025515061dSMel Gorman 		/*
28035515061dSMel Gorman 		 * If the low watermark is met there is no need for processes
28045515061dSMel Gorman 		 * to be throttled on pfmemalloc_wait as they should not be
28055515061dSMel Gorman 		 * able to safely make forward progress. Wake them
28065515061dSMel Gorman 		 */
28075515061dSMel Gorman 		if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
28085515061dSMel Gorman 				pfmemalloc_watermark_ok(pgdat))
28095515061dSMel Gorman 			wake_up(&pgdat->pfmemalloc_wait);
28105515061dSMel Gorman 
2811dafcb73eSZlatko Calusic 		if (pgdat_balanced(pgdat, order, *classzone_idx)) {
2812dafcb73eSZlatko Calusic 			pgdat_is_balanced = true;
28131da177e4SLinus Torvalds 			break;		/* kswapd: all done */
2814dafcb73eSZlatko Calusic 		}
2815dafcb73eSZlatko Calusic 
28161da177e4SLinus Torvalds 		/*
28171da177e4SLinus Torvalds 		 * We do this so kswapd doesn't build up large priorities for
28181da177e4SLinus Torvalds 		 * example when it is freeing in parallel with allocators. It
28191da177e4SLinus Torvalds 		 * matches the direct reclaim path behaviour in terms of impact
28201da177e4SLinus Torvalds 		 * on zone->*_priority.
28211da177e4SLinus Torvalds 		 */
2822a79311c1SRik van Riel 		if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
28231da177e4SLinus Torvalds 			break;
28249e3b2f8cSKonstantin Khlebnikov 	} while (--sc.priority >= 0);
282599504748SMel Gorman 
2826dafcb73eSZlatko Calusic out:
2827dafcb73eSZlatko Calusic 	if (!pgdat_is_balanced) {
28281da177e4SLinus Torvalds 		cond_resched();
28298357376dSRafael J. Wysocki 
28308357376dSRafael J. Wysocki 		try_to_freeze();
28318357376dSRafael J. Wysocki 
283273ce02e9SKOSAKI Motohiro 		/*
283373ce02e9SKOSAKI Motohiro 		 * Fragmentation may mean that the system cannot be
283473ce02e9SKOSAKI Motohiro 		 * rebalanced for high-order allocations in all zones.
283573ce02e9SKOSAKI Motohiro 		 * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
283673ce02e9SKOSAKI Motohiro 		 * it means the zones have been fully scanned and are still
283773ce02e9SKOSAKI Motohiro 		 * not balanced. For high-order allocations, there is
283873ce02e9SKOSAKI Motohiro 		 * little point trying all over again as kswapd may
283973ce02e9SKOSAKI Motohiro 		 * infinite loop.
284073ce02e9SKOSAKI Motohiro 		 *
284173ce02e9SKOSAKI Motohiro 		 * Instead, recheck all watermarks at order-0 as they
284273ce02e9SKOSAKI Motohiro 		 * are the most important. If watermarks are ok, kswapd will go
284373ce02e9SKOSAKI Motohiro 		 * back to sleep. High-order users can still perform direct
284473ce02e9SKOSAKI Motohiro 		 * reclaim if they wish.
284573ce02e9SKOSAKI Motohiro 		 */
284673ce02e9SKOSAKI Motohiro 		if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
284773ce02e9SKOSAKI Motohiro 			order = sc.order = 0;
284873ce02e9SKOSAKI Motohiro 
28491da177e4SLinus Torvalds 		goto loop_again;
28501da177e4SLinus Torvalds 	}
28511da177e4SLinus Torvalds 
285299504748SMel Gorman 	/*
285399504748SMel Gorman 	 * If kswapd was reclaiming at a higher order, it has the option of
285499504748SMel Gorman 	 * sleeping without all zones being balanced. Before it does, it must
285599504748SMel Gorman 	 * ensure that the watermarks for order-0 on *all* zones are met and
285699504748SMel Gorman 	 * that the congestion flags are cleared. The congestion flag must
285799504748SMel Gorman 	 * be cleared as kswapd is the only mechanism that clears the flag
285899504748SMel Gorman 	 * and it is potentially going to sleep here.
285999504748SMel Gorman 	 */
286099504748SMel Gorman 	if (order) {
28617be62de9SRik van Riel 		int zones_need_compaction = 1;
28627be62de9SRik van Riel 
286399504748SMel Gorman 		for (i = 0; i <= end_zone; i++) {
286499504748SMel Gorman 			struct zone *zone = pgdat->node_zones + i;
286599504748SMel Gorman 
286699504748SMel Gorman 			if (!populated_zone(zone))
286799504748SMel Gorman 				continue;
286899504748SMel Gorman 
28697be62de9SRik van Riel 			/* Check if the memory needs to be defragmented. */
28707be62de9SRik van Riel 			if (zone_watermark_ok(zone, order,
28717be62de9SRik van Riel 				    low_wmark_pages(zone), *classzone_idx, 0))
28727be62de9SRik van Riel 				zones_need_compaction = 0;
287399504748SMel Gorman 		}
28747be62de9SRik van Riel 
28757be62de9SRik van Riel 		if (zones_need_compaction)
28767be62de9SRik van Riel 			compact_pgdat(pgdat, order);
287799504748SMel Gorman 	}
287899504748SMel Gorman 
28790abdee2bSMel Gorman 	/*
28805515061dSMel Gorman 	 * Return the order we were reclaiming at so prepare_kswapd_sleep()
28810abdee2bSMel Gorman 	 * makes a decision on the order we were last reclaiming at. However,
28820abdee2bSMel Gorman 	 * if another caller entered the allocator slow path while kswapd
28830abdee2bSMel Gorman 	 * was awake, order will remain at the higher level
28840abdee2bSMel Gorman 	 */
2885dc83edd9SMel Gorman 	*classzone_idx = end_zone;
28860abdee2bSMel Gorman 	return order;
28871da177e4SLinus Torvalds }
28881da177e4SLinus Torvalds 
2889dc83edd9SMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx)
2890f0bc0a60SKOSAKI Motohiro {
2891f0bc0a60SKOSAKI Motohiro 	long remaining = 0;
2892f0bc0a60SKOSAKI Motohiro 	DEFINE_WAIT(wait);
2893f0bc0a60SKOSAKI Motohiro 
2894f0bc0a60SKOSAKI Motohiro 	if (freezing(current) || kthread_should_stop())
2895f0bc0a60SKOSAKI Motohiro 		return;
2896f0bc0a60SKOSAKI Motohiro 
2897f0bc0a60SKOSAKI Motohiro 	prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2898f0bc0a60SKOSAKI Motohiro 
2899f0bc0a60SKOSAKI Motohiro 	/* Try to sleep for a short interval */
29005515061dSMel Gorman 	if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
2901f0bc0a60SKOSAKI Motohiro 		remaining = schedule_timeout(HZ/10);
2902f0bc0a60SKOSAKI Motohiro 		finish_wait(&pgdat->kswapd_wait, &wait);
2903f0bc0a60SKOSAKI Motohiro 		prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2904f0bc0a60SKOSAKI Motohiro 	}
2905f0bc0a60SKOSAKI Motohiro 
2906f0bc0a60SKOSAKI Motohiro 	/*
2907f0bc0a60SKOSAKI Motohiro 	 * After a short sleep, check if it was a premature sleep. If not, then
2908f0bc0a60SKOSAKI Motohiro 	 * go fully to sleep until explicitly woken up.
2909f0bc0a60SKOSAKI Motohiro 	 */
29105515061dSMel Gorman 	if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
2911f0bc0a60SKOSAKI Motohiro 		trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
2912f0bc0a60SKOSAKI Motohiro 
2913f0bc0a60SKOSAKI Motohiro 		/*
2914f0bc0a60SKOSAKI Motohiro 		 * vmstat counters are not perfectly accurate and the estimated
2915f0bc0a60SKOSAKI Motohiro 		 * value for counters such as NR_FREE_PAGES can deviate from the
2916f0bc0a60SKOSAKI Motohiro 		 * true value by nr_online_cpus * threshold. To avoid the zone
2917f0bc0a60SKOSAKI Motohiro 		 * watermarks being breached while under pressure, we reduce the
2918f0bc0a60SKOSAKI Motohiro 		 * per-cpu vmstat threshold while kswapd is awake and restore
2919f0bc0a60SKOSAKI Motohiro 		 * them before going back to sleep.
2920f0bc0a60SKOSAKI Motohiro 		 */
2921f0bc0a60SKOSAKI Motohiro 		set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
29221c7e7f6cSAaditya Kumar 
292362997027SMel Gorman 		/*
292462997027SMel Gorman 		 * Compaction records what page blocks it recently failed to
292562997027SMel Gorman 		 * isolate pages from and skips them in the future scanning.
292662997027SMel Gorman 		 * When kswapd is going to sleep, it is reasonable to assume
292762997027SMel Gorman 		 * that pages and compaction may succeed so reset the cache.
292862997027SMel Gorman 		 */
292962997027SMel Gorman 		reset_isolation_suitable(pgdat);
293062997027SMel Gorman 
29311c7e7f6cSAaditya Kumar 		if (!kthread_should_stop())
2932f0bc0a60SKOSAKI Motohiro 			schedule();
29331c7e7f6cSAaditya Kumar 
2934f0bc0a60SKOSAKI Motohiro 		set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
2935f0bc0a60SKOSAKI Motohiro 	} else {
2936f0bc0a60SKOSAKI Motohiro 		if (remaining)
2937f0bc0a60SKOSAKI Motohiro 			count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
2938f0bc0a60SKOSAKI Motohiro 		else
2939f0bc0a60SKOSAKI Motohiro 			count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
2940f0bc0a60SKOSAKI Motohiro 	}
2941f0bc0a60SKOSAKI Motohiro 	finish_wait(&pgdat->kswapd_wait, &wait);
2942f0bc0a60SKOSAKI Motohiro }
2943f0bc0a60SKOSAKI Motohiro 
29441da177e4SLinus Torvalds /*
29451da177e4SLinus Torvalds  * The background pageout daemon, started as a kernel thread
29461da177e4SLinus Torvalds  * from the init process.
29471da177e4SLinus Torvalds  *
29481da177e4SLinus Torvalds  * This basically trickles out pages so that we have _some_
29491da177e4SLinus Torvalds  * free memory available even if there is no other activity
29501da177e4SLinus Torvalds  * that frees anything up. This is needed for things like routing
29511da177e4SLinus Torvalds  * etc, where we otherwise might have all activity going on in
29521da177e4SLinus Torvalds  * asynchronous contexts that cannot page things out.
29531da177e4SLinus Torvalds  *
29541da177e4SLinus Torvalds  * If there are applications that are active memory-allocators
29551da177e4SLinus Torvalds  * (most normal use), this basically shouldn't matter.
29561da177e4SLinus Torvalds  */
29571da177e4SLinus Torvalds static int kswapd(void *p)
29581da177e4SLinus Torvalds {
2959215ddd66SMel Gorman 	unsigned long order, new_order;
2960d2ebd0f6SAlex,Shi 	unsigned balanced_order;
2961215ddd66SMel Gorman 	int classzone_idx, new_classzone_idx;
2962d2ebd0f6SAlex,Shi 	int balanced_classzone_idx;
29631da177e4SLinus Torvalds 	pg_data_t *pgdat = (pg_data_t*)p;
29641da177e4SLinus Torvalds 	struct task_struct *tsk = current;
2965f0bc0a60SKOSAKI Motohiro 
29661da177e4SLinus Torvalds 	struct reclaim_state reclaim_state = {
29671da177e4SLinus Torvalds 		.reclaimed_slab = 0,
29681da177e4SLinus Torvalds 	};
2969a70f7302SRusty Russell 	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
29701da177e4SLinus Torvalds 
2971cf40bd16SNick Piggin 	lockdep_set_current_reclaim_state(GFP_KERNEL);
2972cf40bd16SNick Piggin 
2973174596a0SRusty Russell 	if (!cpumask_empty(cpumask))
2974c5f59f08SMike Travis 		set_cpus_allowed_ptr(tsk, cpumask);
29751da177e4SLinus Torvalds 	current->reclaim_state = &reclaim_state;
29761da177e4SLinus Torvalds 
29771da177e4SLinus Torvalds 	/*
29781da177e4SLinus Torvalds 	 * Tell the memory management that we're a "memory allocator",
29791da177e4SLinus Torvalds 	 * and that if we need more memory we should get access to it
29801da177e4SLinus Torvalds 	 * regardless (see "__alloc_pages()"). "kswapd" should
29811da177e4SLinus Torvalds 	 * never get caught in the normal page freeing logic.
29821da177e4SLinus Torvalds 	 *
29831da177e4SLinus Torvalds 	 * (Kswapd normally doesn't need memory anyway, but sometimes
29841da177e4SLinus Torvalds 	 * you need a small amount of memory in order to be able to
29851da177e4SLinus Torvalds 	 * page out something else, and this flag essentially protects
29861da177e4SLinus Torvalds 	 * us from recursively trying to free more memory as we're
29871da177e4SLinus Torvalds 	 * trying to free the first piece of memory in the first place).
29881da177e4SLinus Torvalds 	 */
2989930d9152SChristoph Lameter 	tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
299083144186SRafael J. Wysocki 	set_freezable();
29911da177e4SLinus Torvalds 
2992215ddd66SMel Gorman 	order = new_order = 0;
2993d2ebd0f6SAlex,Shi 	balanced_order = 0;
2994215ddd66SMel Gorman 	classzone_idx = new_classzone_idx = pgdat->nr_zones - 1;
2995d2ebd0f6SAlex,Shi 	balanced_classzone_idx = classzone_idx;
29961da177e4SLinus Torvalds 	for ( ; ; ) {
29976f6313d4SJeff Liu 		bool ret;
29983e1d1d28SChristoph Lameter 
2999215ddd66SMel Gorman 		/*
3000215ddd66SMel Gorman 		 * If the last balance_pgdat was unsuccessful it's unlikely a
3001215ddd66SMel Gorman 		 * new request of a similar or harder type will succeed soon
3002215ddd66SMel Gorman 		 * so consider going to sleep on the basis we reclaimed at
3003215ddd66SMel Gorman 		 */
3004d2ebd0f6SAlex,Shi 		if (balanced_classzone_idx >= new_classzone_idx &&
3005d2ebd0f6SAlex,Shi 					balanced_order == new_order) {
30061da177e4SLinus Torvalds 			new_order = pgdat->kswapd_max_order;
300799504748SMel Gorman 			new_classzone_idx = pgdat->classzone_idx;
30081da177e4SLinus Torvalds 			pgdat->kswapd_max_order =  0;
3009215ddd66SMel Gorman 			pgdat->classzone_idx = pgdat->nr_zones - 1;
3010215ddd66SMel Gorman 		}
3011215ddd66SMel Gorman 
301299504748SMel Gorman 		if (order < new_order || classzone_idx > new_classzone_idx) {
30131da177e4SLinus Torvalds 			/*
30141da177e4SLinus Torvalds 			 * Don't sleep if someone wants a larger 'order'
301599504748SMel Gorman 			 * allocation or has tigher zone constraints
30161da177e4SLinus Torvalds 			 */
30171da177e4SLinus Torvalds 			order = new_order;
301899504748SMel Gorman 			classzone_idx = new_classzone_idx;
30191da177e4SLinus Torvalds 		} else {
3020d2ebd0f6SAlex,Shi 			kswapd_try_to_sleep(pgdat, balanced_order,
3021d2ebd0f6SAlex,Shi 						balanced_classzone_idx);
30221da177e4SLinus Torvalds 			order = pgdat->kswapd_max_order;
302399504748SMel Gorman 			classzone_idx = pgdat->classzone_idx;
3024f0dfcde0SAlex,Shi 			new_order = order;
3025f0dfcde0SAlex,Shi 			new_classzone_idx = classzone_idx;
30264d40502eSMel Gorman 			pgdat->kswapd_max_order = 0;
3027215ddd66SMel Gorman 			pgdat->classzone_idx = pgdat->nr_zones - 1;
30281da177e4SLinus Torvalds 		}
30291da177e4SLinus Torvalds 
30308fe23e05SDavid Rientjes 		ret = try_to_freeze();
30318fe23e05SDavid Rientjes 		if (kthread_should_stop())
30328fe23e05SDavid Rientjes 			break;
30338fe23e05SDavid Rientjes 
30348fe23e05SDavid Rientjes 		/*
30358fe23e05SDavid Rientjes 		 * We can speed up thawing tasks if we don't call balance_pgdat
30368fe23e05SDavid Rientjes 		 * after returning from the refrigerator
3037b1296cc4SRafael J. Wysocki 		 */
303833906bc5SMel Gorman 		if (!ret) {
303933906bc5SMel Gorman 			trace_mm_vmscan_kswapd_wake(pgdat->node_id, order);
3040d2ebd0f6SAlex,Shi 			balanced_classzone_idx = classzone_idx;
3041d2ebd0f6SAlex,Shi 			balanced_order = balance_pgdat(pgdat, order,
3042d2ebd0f6SAlex,Shi 						&balanced_classzone_idx);
30431da177e4SLinus Torvalds 		}
304433906bc5SMel Gorman 	}
3045b0a8cc58STakamori Yamaguchi 
3046b0a8cc58STakamori Yamaguchi 	current->reclaim_state = NULL;
30471da177e4SLinus Torvalds 	return 0;
30481da177e4SLinus Torvalds }
30491da177e4SLinus Torvalds 
30501da177e4SLinus Torvalds /*
30511da177e4SLinus Torvalds  * A zone is low on free memory, so wake its kswapd task to service it.
30521da177e4SLinus Torvalds  */
305399504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
30541da177e4SLinus Torvalds {
30551da177e4SLinus Torvalds 	pg_data_t *pgdat;
30561da177e4SLinus Torvalds 
3057f3fe6512SCon Kolivas 	if (!populated_zone(zone))
30581da177e4SLinus Torvalds 		return;
30591da177e4SLinus Torvalds 
306002a0e53dSPaul Jackson 	if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
30611da177e4SLinus Torvalds 		return;
306288f5acf8SMel Gorman 	pgdat = zone->zone_pgdat;
306399504748SMel Gorman 	if (pgdat->kswapd_max_order < order) {
306488f5acf8SMel Gorman 		pgdat->kswapd_max_order = order;
306599504748SMel Gorman 		pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx);
306699504748SMel Gorman 	}
30678d0986e2SCon Kolivas 	if (!waitqueue_active(&pgdat->kswapd_wait))
30681da177e4SLinus Torvalds 		return;
306988f5acf8SMel Gorman 	if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0))
307088f5acf8SMel Gorman 		return;
307188f5acf8SMel Gorman 
307288f5acf8SMel Gorman 	trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
30738d0986e2SCon Kolivas 	wake_up_interruptible(&pgdat->kswapd_wait);
30741da177e4SLinus Torvalds }
30751da177e4SLinus Torvalds 
3076adea02a1SWu Fengguang /*
3077adea02a1SWu Fengguang  * The reclaimable count would be mostly accurate.
3078adea02a1SWu Fengguang  * The less reclaimable pages may be
3079adea02a1SWu Fengguang  * - mlocked pages, which will be moved to unevictable list when encountered
3080adea02a1SWu Fengguang  * - mapped pages, which may require several travels to be reclaimed
3081adea02a1SWu Fengguang  * - dirty pages, which is not "instantly" reclaimable
3082adea02a1SWu Fengguang  */
3083adea02a1SWu Fengguang unsigned long global_reclaimable_pages(void)
30844f98a2feSRik van Riel {
3085adea02a1SWu Fengguang 	int nr;
3086adea02a1SWu Fengguang 
3087adea02a1SWu Fengguang 	nr = global_page_state(NR_ACTIVE_FILE) +
3088adea02a1SWu Fengguang 	     global_page_state(NR_INACTIVE_FILE);
3089adea02a1SWu Fengguang 
3090ec8acf20SShaohua Li 	if (get_nr_swap_pages() > 0)
3091adea02a1SWu Fengguang 		nr += global_page_state(NR_ACTIVE_ANON) +
3092adea02a1SWu Fengguang 		      global_page_state(NR_INACTIVE_ANON);
3093adea02a1SWu Fengguang 
3094adea02a1SWu Fengguang 	return nr;
3095adea02a1SWu Fengguang }
3096adea02a1SWu Fengguang 
3097adea02a1SWu Fengguang unsigned long zone_reclaimable_pages(struct zone *zone)
3098adea02a1SWu Fengguang {
3099adea02a1SWu Fengguang 	int nr;
3100adea02a1SWu Fengguang 
3101adea02a1SWu Fengguang 	nr = zone_page_state(zone, NR_ACTIVE_FILE) +
3102adea02a1SWu Fengguang 	     zone_page_state(zone, NR_INACTIVE_FILE);
3103adea02a1SWu Fengguang 
3104ec8acf20SShaohua Li 	if (get_nr_swap_pages() > 0)
3105adea02a1SWu Fengguang 		nr += zone_page_state(zone, NR_ACTIVE_ANON) +
3106adea02a1SWu Fengguang 		      zone_page_state(zone, NR_INACTIVE_ANON);
3107adea02a1SWu Fengguang 
3108adea02a1SWu Fengguang 	return nr;
31094f98a2feSRik van Riel }
31104f98a2feSRik van Riel 
3111c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
31121da177e4SLinus Torvalds /*
31137b51755cSKOSAKI Motohiro  * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
3114d6277db4SRafael J. Wysocki  * freed pages.
3115d6277db4SRafael J. Wysocki  *
3116d6277db4SRafael J. Wysocki  * Rather than trying to age LRUs the aim is to preserve the overall
3117d6277db4SRafael J. Wysocki  * LRU order by reclaiming preferentially
3118d6277db4SRafael J. Wysocki  * inactive > active > active referenced > active mapped
31191da177e4SLinus Torvalds  */
31207b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
31211da177e4SLinus Torvalds {
3122d6277db4SRafael J. Wysocki 	struct reclaim_state reclaim_state;
3123d6277db4SRafael J. Wysocki 	struct scan_control sc = {
31247b51755cSKOSAKI Motohiro 		.gfp_mask = GFP_HIGHUSER_MOVABLE,
31257b51755cSKOSAKI Motohiro 		.may_swap = 1,
31267b51755cSKOSAKI Motohiro 		.may_unmap = 1,
3127d6277db4SRafael J. Wysocki 		.may_writepage = 1,
31287b51755cSKOSAKI Motohiro 		.nr_to_reclaim = nr_to_reclaim,
31297b51755cSKOSAKI Motohiro 		.hibernation_mode = 1,
31307b51755cSKOSAKI Motohiro 		.order = 0,
31319e3b2f8cSKonstantin Khlebnikov 		.priority = DEF_PRIORITY,
31321da177e4SLinus Torvalds 	};
3133a09ed5e0SYing Han 	struct shrink_control shrink = {
3134a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
3135a09ed5e0SYing Han 	};
31367b51755cSKOSAKI Motohiro 	struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
31377b51755cSKOSAKI Motohiro 	struct task_struct *p = current;
31387b51755cSKOSAKI Motohiro 	unsigned long nr_reclaimed;
31391da177e4SLinus Torvalds 
31407b51755cSKOSAKI Motohiro 	p->flags |= PF_MEMALLOC;
31417b51755cSKOSAKI Motohiro 	lockdep_set_current_reclaim_state(sc.gfp_mask);
3142d6277db4SRafael J. Wysocki 	reclaim_state.reclaimed_slab = 0;
31437b51755cSKOSAKI Motohiro 	p->reclaim_state = &reclaim_state;
3144d6277db4SRafael J. Wysocki 
3145a09ed5e0SYing Han 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
3146d6277db4SRafael J. Wysocki 
31477b51755cSKOSAKI Motohiro 	p->reclaim_state = NULL;
31487b51755cSKOSAKI Motohiro 	lockdep_clear_current_reclaim_state();
31497b51755cSKOSAKI Motohiro 	p->flags &= ~PF_MEMALLOC;
3150d6277db4SRafael J. Wysocki 
31517b51755cSKOSAKI Motohiro 	return nr_reclaimed;
31521da177e4SLinus Torvalds }
3153c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */
31541da177e4SLinus Torvalds 
31551da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but
31561da177e4SLinus Torvalds    not required for correctness.  So if the last cpu in a node goes
31571da177e4SLinus Torvalds    away, we get changed to run anywhere: as the first one comes back,
31581da177e4SLinus Torvalds    restore their cpu bindings. */
3159fcb35a9bSGreg Kroah-Hartman static int cpu_callback(struct notifier_block *nfb, unsigned long action,
3160fcb35a9bSGreg Kroah-Hartman 			void *hcpu)
31611da177e4SLinus Torvalds {
316258c0a4a7SYasunori Goto 	int nid;
31631da177e4SLinus Torvalds 
31648bb78442SRafael J. Wysocki 	if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
316548fb2e24SLai Jiangshan 		for_each_node_state(nid, N_MEMORY) {
3166c5f59f08SMike Travis 			pg_data_t *pgdat = NODE_DATA(nid);
3167a70f7302SRusty Russell 			const struct cpumask *mask;
3168a70f7302SRusty Russell 
3169a70f7302SRusty Russell 			mask = cpumask_of_node(pgdat->node_id);
3170c5f59f08SMike Travis 
31713e597945SRusty Russell 			if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
31721da177e4SLinus Torvalds 				/* One of our CPUs online: restore mask */
3173c5f59f08SMike Travis 				set_cpus_allowed_ptr(pgdat->kswapd, mask);
31741da177e4SLinus Torvalds 		}
31751da177e4SLinus Torvalds 	}
31761da177e4SLinus Torvalds 	return NOTIFY_OK;
31771da177e4SLinus Torvalds }
31781da177e4SLinus Torvalds 
31793218ae14SYasunori Goto /*
31803218ae14SYasunori Goto  * This kswapd start function will be called by init and node-hot-add.
31813218ae14SYasunori Goto  * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
31823218ae14SYasunori Goto  */
31833218ae14SYasunori Goto int kswapd_run(int nid)
31843218ae14SYasunori Goto {
31853218ae14SYasunori Goto 	pg_data_t *pgdat = NODE_DATA(nid);
31863218ae14SYasunori Goto 	int ret = 0;
31873218ae14SYasunori Goto 
31883218ae14SYasunori Goto 	if (pgdat->kswapd)
31893218ae14SYasunori Goto 		return 0;
31903218ae14SYasunori Goto 
31913218ae14SYasunori Goto 	pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
31923218ae14SYasunori Goto 	if (IS_ERR(pgdat->kswapd)) {
31933218ae14SYasunori Goto 		/* failure at boot is fatal */
31943218ae14SYasunori Goto 		BUG_ON(system_state == SYSTEM_BOOTING);
3195d5dc0ad9SGavin Shan 		pr_err("Failed to start kswapd on node %d\n", nid);
3196d5dc0ad9SGavin Shan 		ret = PTR_ERR(pgdat->kswapd);
3197d72515b8SXishi Qiu 		pgdat->kswapd = NULL;
31983218ae14SYasunori Goto 	}
31993218ae14SYasunori Goto 	return ret;
32003218ae14SYasunori Goto }
32013218ae14SYasunori Goto 
32028fe23e05SDavid Rientjes /*
3203d8adde17SJiang Liu  * Called by memory hotplug when all memory in a node is offlined.  Caller must
3204d8adde17SJiang Liu  * hold lock_memory_hotplug().
32058fe23e05SDavid Rientjes  */
32068fe23e05SDavid Rientjes void kswapd_stop(int nid)
32078fe23e05SDavid Rientjes {
32088fe23e05SDavid Rientjes 	struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
32098fe23e05SDavid Rientjes 
3210d8adde17SJiang Liu 	if (kswapd) {
32118fe23e05SDavid Rientjes 		kthread_stop(kswapd);
3212d8adde17SJiang Liu 		NODE_DATA(nid)->kswapd = NULL;
3213d8adde17SJiang Liu 	}
32148fe23e05SDavid Rientjes }
32158fe23e05SDavid Rientjes 
32161da177e4SLinus Torvalds static int __init kswapd_init(void)
32171da177e4SLinus Torvalds {
32183218ae14SYasunori Goto 	int nid;
321969e05944SAndrew Morton 
32201da177e4SLinus Torvalds 	swap_setup();
322148fb2e24SLai Jiangshan 	for_each_node_state(nid, N_MEMORY)
32223218ae14SYasunori Goto  		kswapd_run(nid);
32231da177e4SLinus Torvalds 	hotcpu_notifier(cpu_callback, 0);
32241da177e4SLinus Torvalds 	return 0;
32251da177e4SLinus Torvalds }
32261da177e4SLinus Torvalds 
32271da177e4SLinus Torvalds module_init(kswapd_init)
32289eeff239SChristoph Lameter 
32299eeff239SChristoph Lameter #ifdef CONFIG_NUMA
32309eeff239SChristoph Lameter /*
32319eeff239SChristoph Lameter  * Zone reclaim mode
32329eeff239SChristoph Lameter  *
32339eeff239SChristoph Lameter  * If non-zero call zone_reclaim when the number of free pages falls below
32349eeff239SChristoph Lameter  * the watermarks.
32359eeff239SChristoph Lameter  */
32369eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly;
32379eeff239SChristoph Lameter 
32381b2ffb78SChristoph Lameter #define RECLAIM_OFF 0
32397d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0)	/* Run shrink_inactive_list on the zone */
32401b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1)	/* Writeout pages during reclaim */
32411b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2)	/* Swap pages out during reclaim */
32421b2ffb78SChristoph Lameter 
32439eeff239SChristoph Lameter /*
3244a92f7126SChristoph Lameter  * Priority for ZONE_RECLAIM. This determines the fraction of pages
3245a92f7126SChristoph Lameter  * of a node considered for each zone_reclaim. 4 scans 1/16th of
3246a92f7126SChristoph Lameter  * a zone.
3247a92f7126SChristoph Lameter  */
3248a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4
3249a92f7126SChristoph Lameter 
32509eeff239SChristoph Lameter /*
32519614634fSChristoph Lameter  * Percentage of pages in a zone that must be unmapped for zone_reclaim to
32529614634fSChristoph Lameter  * occur.
32539614634fSChristoph Lameter  */
32549614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1;
32559614634fSChristoph Lameter 
32569614634fSChristoph Lameter /*
32570ff38490SChristoph Lameter  * If the number of slab pages in a zone grows beyond this percentage then
32580ff38490SChristoph Lameter  * slab reclaim needs to occur.
32590ff38490SChristoph Lameter  */
32600ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5;
32610ff38490SChristoph Lameter 
326290afa5deSMel Gorman static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
326390afa5deSMel Gorman {
326490afa5deSMel Gorman 	unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
326590afa5deSMel Gorman 	unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
326690afa5deSMel Gorman 		zone_page_state(zone, NR_ACTIVE_FILE);
326790afa5deSMel Gorman 
326890afa5deSMel Gorman 	/*
326990afa5deSMel Gorman 	 * It's possible for there to be more file mapped pages than
327090afa5deSMel Gorman 	 * accounted for by the pages on the file LRU lists because
327190afa5deSMel Gorman 	 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
327290afa5deSMel Gorman 	 */
327390afa5deSMel Gorman 	return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
327490afa5deSMel Gorman }
327590afa5deSMel Gorman 
327690afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */
327790afa5deSMel Gorman static long zone_pagecache_reclaimable(struct zone *zone)
327890afa5deSMel Gorman {
327990afa5deSMel Gorman 	long nr_pagecache_reclaimable;
328090afa5deSMel Gorman 	long delta = 0;
328190afa5deSMel Gorman 
328290afa5deSMel Gorman 	/*
328390afa5deSMel Gorman 	 * If RECLAIM_SWAP is set, then all file pages are considered
328490afa5deSMel Gorman 	 * potentially reclaimable. Otherwise, we have to worry about
328590afa5deSMel Gorman 	 * pages like swapcache and zone_unmapped_file_pages() provides
328690afa5deSMel Gorman 	 * a better estimate
328790afa5deSMel Gorman 	 */
328890afa5deSMel Gorman 	if (zone_reclaim_mode & RECLAIM_SWAP)
328990afa5deSMel Gorman 		nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
329090afa5deSMel Gorman 	else
329190afa5deSMel Gorman 		nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
329290afa5deSMel Gorman 
329390afa5deSMel Gorman 	/* If we can't clean pages, remove dirty pages from consideration */
329490afa5deSMel Gorman 	if (!(zone_reclaim_mode & RECLAIM_WRITE))
329590afa5deSMel Gorman 		delta += zone_page_state(zone, NR_FILE_DIRTY);
329690afa5deSMel Gorman 
329790afa5deSMel Gorman 	/* Watch for any possible underflows due to delta */
329890afa5deSMel Gorman 	if (unlikely(delta > nr_pagecache_reclaimable))
329990afa5deSMel Gorman 		delta = nr_pagecache_reclaimable;
330090afa5deSMel Gorman 
330190afa5deSMel Gorman 	return nr_pagecache_reclaimable - delta;
330290afa5deSMel Gorman }
330390afa5deSMel Gorman 
33040ff38490SChristoph Lameter /*
33059eeff239SChristoph Lameter  * Try to free up some pages from this zone through reclaim.
33069eeff239SChristoph Lameter  */
3307179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
33089eeff239SChristoph Lameter {
33097fb2d46dSChristoph Lameter 	/* Minimum pages needed in order to stay on node */
331069e05944SAndrew Morton 	const unsigned long nr_pages = 1 << order;
33119eeff239SChristoph Lameter 	struct task_struct *p = current;
33129eeff239SChristoph Lameter 	struct reclaim_state reclaim_state;
3313179e9639SAndrew Morton 	struct scan_control sc = {
3314179e9639SAndrew Morton 		.may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
3315a6dc60f8SJohannes Weiner 		.may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
33162e2e4259SKOSAKI Motohiro 		.may_swap = 1,
331762b726c1SAndrew Morton 		.nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
331821caf2fcSMing Lei 		.gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)),
3319bd2f6199SJohannes Weiner 		.order = order,
33209e3b2f8cSKonstantin Khlebnikov 		.priority = ZONE_RECLAIM_PRIORITY,
3321179e9639SAndrew Morton 	};
3322a09ed5e0SYing Han 	struct shrink_control shrink = {
3323a09ed5e0SYing Han 		.gfp_mask = sc.gfp_mask,
3324a09ed5e0SYing Han 	};
332515748048SKOSAKI Motohiro 	unsigned long nr_slab_pages0, nr_slab_pages1;
33269eeff239SChristoph Lameter 
33279eeff239SChristoph Lameter 	cond_resched();
3328d4f7796eSChristoph Lameter 	/*
3329d4f7796eSChristoph Lameter 	 * We need to be able to allocate from the reserves for RECLAIM_SWAP
3330d4f7796eSChristoph Lameter 	 * and we also need to be able to write out pages for RECLAIM_WRITE
3331d4f7796eSChristoph Lameter 	 * and RECLAIM_SWAP.
3332d4f7796eSChristoph Lameter 	 */
3333d4f7796eSChristoph Lameter 	p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
333476ca542dSKOSAKI Motohiro 	lockdep_set_current_reclaim_state(gfp_mask);
33359eeff239SChristoph Lameter 	reclaim_state.reclaimed_slab = 0;
33369eeff239SChristoph Lameter 	p->reclaim_state = &reclaim_state;
3337c84db23cSChristoph Lameter 
333890afa5deSMel Gorman 	if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
3339a92f7126SChristoph Lameter 		/*
33400ff38490SChristoph Lameter 		 * Free memory by calling shrink zone with increasing
33410ff38490SChristoph Lameter 		 * priorities until we have enough memory freed.
3342a92f7126SChristoph Lameter 		 */
3343a92f7126SChristoph Lameter 		do {
33449e3b2f8cSKonstantin Khlebnikov 			shrink_zone(zone, &sc);
33459e3b2f8cSKonstantin Khlebnikov 		} while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
33460ff38490SChristoph Lameter 	}
3347a92f7126SChristoph Lameter 
334815748048SKOSAKI Motohiro 	nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
334915748048SKOSAKI Motohiro 	if (nr_slab_pages0 > zone->min_slab_pages) {
33502a16e3f4SChristoph Lameter 		/*
33517fb2d46dSChristoph Lameter 		 * shrink_slab() does not currently allow us to determine how
33520ff38490SChristoph Lameter 		 * many pages were freed in this zone. So we take the current
33530ff38490SChristoph Lameter 		 * number of slab pages and shake the slab until it is reduced
33540ff38490SChristoph Lameter 		 * by the same nr_pages that we used for reclaiming unmapped
33550ff38490SChristoph Lameter 		 * pages.
33562a16e3f4SChristoph Lameter 		 *
33570ff38490SChristoph Lameter 		 * Note that shrink_slab will free memory on all zones and may
33580ff38490SChristoph Lameter 		 * take a long time.
33592a16e3f4SChristoph Lameter 		 */
33604dc4b3d9SKOSAKI Motohiro 		for (;;) {
33614dc4b3d9SKOSAKI Motohiro 			unsigned long lru_pages = zone_reclaimable_pages(zone);
33624dc4b3d9SKOSAKI Motohiro 
33634dc4b3d9SKOSAKI Motohiro 			/* No reclaimable slab or very low memory pressure */
33641495f230SYing Han 			if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages))
33654dc4b3d9SKOSAKI Motohiro 				break;
33664dc4b3d9SKOSAKI Motohiro 
33674dc4b3d9SKOSAKI Motohiro 			/* Freed enough memory */
33684dc4b3d9SKOSAKI Motohiro 			nr_slab_pages1 = zone_page_state(zone,
33694dc4b3d9SKOSAKI Motohiro 							NR_SLAB_RECLAIMABLE);
33704dc4b3d9SKOSAKI Motohiro 			if (nr_slab_pages1 + nr_pages <= nr_slab_pages0)
33714dc4b3d9SKOSAKI Motohiro 				break;
33724dc4b3d9SKOSAKI Motohiro 		}
337383e33a47SChristoph Lameter 
337483e33a47SChristoph Lameter 		/*
337583e33a47SChristoph Lameter 		 * Update nr_reclaimed by the number of slab pages we
337683e33a47SChristoph Lameter 		 * reclaimed from this zone.
337783e33a47SChristoph Lameter 		 */
337815748048SKOSAKI Motohiro 		nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
337915748048SKOSAKI Motohiro 		if (nr_slab_pages1 < nr_slab_pages0)
338015748048SKOSAKI Motohiro 			sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1;
33812a16e3f4SChristoph Lameter 	}
33822a16e3f4SChristoph Lameter 
33839eeff239SChristoph Lameter 	p->reclaim_state = NULL;
3384d4f7796eSChristoph Lameter 	current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
338576ca542dSKOSAKI Motohiro 	lockdep_clear_current_reclaim_state();
3386a79311c1SRik van Riel 	return sc.nr_reclaimed >= nr_pages;
33879eeff239SChristoph Lameter }
3388179e9639SAndrew Morton 
3389179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
3390179e9639SAndrew Morton {
3391179e9639SAndrew Morton 	int node_id;
3392d773ed6bSDavid Rientjes 	int ret;
3393179e9639SAndrew Morton 
3394179e9639SAndrew Morton 	/*
33950ff38490SChristoph Lameter 	 * Zone reclaim reclaims unmapped file backed pages and
33960ff38490SChristoph Lameter 	 * slab pages if we are over the defined limits.
339734aa1330SChristoph Lameter 	 *
33989614634fSChristoph Lameter 	 * A small portion of unmapped file backed pages is needed for
33999614634fSChristoph Lameter 	 * file I/O otherwise pages read by file I/O will be immediately
34009614634fSChristoph Lameter 	 * thrown out if the zone is overallocated. So we do not reclaim
34019614634fSChristoph Lameter 	 * if less than a specified percentage of the zone is used by
34029614634fSChristoph Lameter 	 * unmapped file backed pages.
3403179e9639SAndrew Morton 	 */
340490afa5deSMel Gorman 	if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
340590afa5deSMel Gorman 	    zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
3406fa5e084eSMel Gorman 		return ZONE_RECLAIM_FULL;
3407179e9639SAndrew Morton 
340893e4a89aSKOSAKI Motohiro 	if (zone->all_unreclaimable)
3409fa5e084eSMel Gorman 		return ZONE_RECLAIM_FULL;
3410d773ed6bSDavid Rientjes 
3411179e9639SAndrew Morton 	/*
3412d773ed6bSDavid Rientjes 	 * Do not scan if the allocation should not be delayed.
3413179e9639SAndrew Morton 	 */
3414d773ed6bSDavid Rientjes 	if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
3415fa5e084eSMel Gorman 		return ZONE_RECLAIM_NOSCAN;
3416179e9639SAndrew Morton 
3417179e9639SAndrew Morton 	/*
3418179e9639SAndrew Morton 	 * Only run zone reclaim on the local zone or on zones that do not
3419179e9639SAndrew Morton 	 * have associated processors. This will favor the local processor
3420179e9639SAndrew Morton 	 * over remote processors and spread off node memory allocations
3421179e9639SAndrew Morton 	 * as wide as possible.
3422179e9639SAndrew Morton 	 */
342389fa3024SChristoph Lameter 	node_id = zone_to_nid(zone);
342437c0708dSChristoph Lameter 	if (node_state(node_id, N_CPU) && node_id != numa_node_id())
3425fa5e084eSMel Gorman 		return ZONE_RECLAIM_NOSCAN;
3426d773ed6bSDavid Rientjes 
3427d773ed6bSDavid Rientjes 	if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
3428fa5e084eSMel Gorman 		return ZONE_RECLAIM_NOSCAN;
3429fa5e084eSMel Gorman 
3430d773ed6bSDavid Rientjes 	ret = __zone_reclaim(zone, gfp_mask, order);
3431d773ed6bSDavid Rientjes 	zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
3432d773ed6bSDavid Rientjes 
343324cf7251SMel Gorman 	if (!ret)
343424cf7251SMel Gorman 		count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
343524cf7251SMel Gorman 
3436d773ed6bSDavid Rientjes 	return ret;
3437179e9639SAndrew Morton }
34389eeff239SChristoph Lameter #endif
3439894bc310SLee Schermerhorn 
3440894bc310SLee Schermerhorn /*
3441894bc310SLee Schermerhorn  * page_evictable - test whether a page is evictable
3442894bc310SLee Schermerhorn  * @page: the page to test
3443894bc310SLee Schermerhorn  *
3444894bc310SLee Schermerhorn  * Test whether page is evictable--i.e., should be placed on active/inactive
344539b5f29aSHugh Dickins  * lists vs unevictable list.
3446894bc310SLee Schermerhorn  *
3447894bc310SLee Schermerhorn  * Reasons page might not be evictable:
3448ba9ddf49SLee Schermerhorn  * (1) page's mapping marked unevictable
3449b291f000SNick Piggin  * (2) page is part of an mlocked VMA
3450ba9ddf49SLee Schermerhorn  *
3451894bc310SLee Schermerhorn  */
345239b5f29aSHugh Dickins int page_evictable(struct page *page)
3453894bc310SLee Schermerhorn {
345439b5f29aSHugh Dickins 	return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
3455894bc310SLee Schermerhorn }
345689e004eaSLee Schermerhorn 
345785046579SHugh Dickins #ifdef CONFIG_SHMEM
345889e004eaSLee Schermerhorn /**
345924513264SHugh Dickins  * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list
346024513264SHugh Dickins  * @pages:	array of pages to check
346124513264SHugh Dickins  * @nr_pages:	number of pages to check
346289e004eaSLee Schermerhorn  *
346324513264SHugh Dickins  * Checks pages for evictability and moves them to the appropriate lru list.
346485046579SHugh Dickins  *
346585046579SHugh Dickins  * This function is only used for SysV IPC SHM_UNLOCK.
346689e004eaSLee Schermerhorn  */
346724513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages)
346889e004eaSLee Schermerhorn {
3469925b7673SJohannes Weiner 	struct lruvec *lruvec;
347024513264SHugh Dickins 	struct zone *zone = NULL;
347124513264SHugh Dickins 	int pgscanned = 0;
347224513264SHugh Dickins 	int pgrescued = 0;
347389e004eaSLee Schermerhorn 	int i;
347489e004eaSLee Schermerhorn 
347524513264SHugh Dickins 	for (i = 0; i < nr_pages; i++) {
347624513264SHugh Dickins 		struct page *page = pages[i];
347724513264SHugh Dickins 		struct zone *pagezone;
347889e004eaSLee Schermerhorn 
347924513264SHugh Dickins 		pgscanned++;
348024513264SHugh Dickins 		pagezone = page_zone(page);
348189e004eaSLee Schermerhorn 		if (pagezone != zone) {
348289e004eaSLee Schermerhorn 			if (zone)
348389e004eaSLee Schermerhorn 				spin_unlock_irq(&zone->lru_lock);
348489e004eaSLee Schermerhorn 			zone = pagezone;
348589e004eaSLee Schermerhorn 			spin_lock_irq(&zone->lru_lock);
348689e004eaSLee Schermerhorn 		}
3487fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
348889e004eaSLee Schermerhorn 
348924513264SHugh Dickins 		if (!PageLRU(page) || !PageUnevictable(page))
349024513264SHugh Dickins 			continue;
349189e004eaSLee Schermerhorn 
349239b5f29aSHugh Dickins 		if (page_evictable(page)) {
349324513264SHugh Dickins 			enum lru_list lru = page_lru_base_type(page);
349424513264SHugh Dickins 
349524513264SHugh Dickins 			VM_BUG_ON(PageActive(page));
349624513264SHugh Dickins 			ClearPageUnevictable(page);
3497fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
3498fa9add64SHugh Dickins 			add_page_to_lru_list(page, lruvec, lru);
349924513264SHugh Dickins 			pgrescued++;
350089e004eaSLee Schermerhorn 		}
350189e004eaSLee Schermerhorn 	}
350224513264SHugh Dickins 
350324513264SHugh Dickins 	if (zone) {
350424513264SHugh Dickins 		__count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
350524513264SHugh Dickins 		__count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
350624513264SHugh Dickins 		spin_unlock_irq(&zone->lru_lock);
350724513264SHugh Dickins 	}
350885046579SHugh Dickins }
350985046579SHugh Dickins #endif /* CONFIG_SHMEM */
3510af936a16SLee Schermerhorn 
3511264e56d8SJohannes Weiner static void warn_scan_unevictable_pages(void)
3512af936a16SLee Schermerhorn {
3513264e56d8SJohannes Weiner 	printk_once(KERN_WARNING
351425bd91bdSKOSAKI Motohiro 		    "%s: The scan_unevictable_pages sysctl/node-interface has been "
3515264e56d8SJohannes Weiner 		    "disabled for lack of a legitimate use case.  If you have "
351625bd91bdSKOSAKI Motohiro 		    "one, please send an email to linux-mm@kvack.org.\n",
351725bd91bdSKOSAKI Motohiro 		    current->comm);
3518af936a16SLee Schermerhorn }
3519af936a16SLee Schermerhorn 
3520af936a16SLee Schermerhorn /*
3521af936a16SLee Schermerhorn  * scan_unevictable_pages [vm] sysctl handler.  On demand re-scan of
3522af936a16SLee Schermerhorn  * all nodes' unevictable lists for evictable pages
3523af936a16SLee Schermerhorn  */
3524af936a16SLee Schermerhorn unsigned long scan_unevictable_pages;
3525af936a16SLee Schermerhorn 
3526af936a16SLee Schermerhorn int scan_unevictable_handler(struct ctl_table *table, int write,
35278d65af78SAlexey Dobriyan 			   void __user *buffer,
3528af936a16SLee Schermerhorn 			   size_t *length, loff_t *ppos)
3529af936a16SLee Schermerhorn {
3530264e56d8SJohannes Weiner 	warn_scan_unevictable_pages();
35318d65af78SAlexey Dobriyan 	proc_doulongvec_minmax(table, write, buffer, length, ppos);
3532af936a16SLee Schermerhorn 	scan_unevictable_pages = 0;
3533af936a16SLee Schermerhorn 	return 0;
3534af936a16SLee Schermerhorn }
3535af936a16SLee Schermerhorn 
3536e4455abbSThadeu Lima de Souza Cascardo #ifdef CONFIG_NUMA
3537af936a16SLee Schermerhorn /*
3538af936a16SLee Schermerhorn  * per node 'scan_unevictable_pages' attribute.  On demand re-scan of
3539af936a16SLee Schermerhorn  * a specified node's per zone unevictable lists for evictable pages.
3540af936a16SLee Schermerhorn  */
3541af936a16SLee Schermerhorn 
354210fbcf4cSKay Sievers static ssize_t read_scan_unevictable_node(struct device *dev,
354310fbcf4cSKay Sievers 					  struct device_attribute *attr,
3544af936a16SLee Schermerhorn 					  char *buf)
3545af936a16SLee Schermerhorn {
3546264e56d8SJohannes Weiner 	warn_scan_unevictable_pages();
3547af936a16SLee Schermerhorn 	return sprintf(buf, "0\n");	/* always zero; should fit... */
3548af936a16SLee Schermerhorn }
3549af936a16SLee Schermerhorn 
355010fbcf4cSKay Sievers static ssize_t write_scan_unevictable_node(struct device *dev,
355110fbcf4cSKay Sievers 					   struct device_attribute *attr,
3552af936a16SLee Schermerhorn 					const char *buf, size_t count)
3553af936a16SLee Schermerhorn {
3554264e56d8SJohannes Weiner 	warn_scan_unevictable_pages();
3555af936a16SLee Schermerhorn 	return 1;
3556af936a16SLee Schermerhorn }
3557af936a16SLee Schermerhorn 
3558af936a16SLee Schermerhorn 
355910fbcf4cSKay Sievers static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
3560af936a16SLee Schermerhorn 			read_scan_unevictable_node,
3561af936a16SLee Schermerhorn 			write_scan_unevictable_node);
3562af936a16SLee Schermerhorn 
3563af936a16SLee Schermerhorn int scan_unevictable_register_node(struct node *node)
3564af936a16SLee Schermerhorn {
356510fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages);
3566af936a16SLee Schermerhorn }
3567af936a16SLee Schermerhorn 
3568af936a16SLee Schermerhorn void scan_unevictable_unregister_node(struct node *node)
3569af936a16SLee Schermerhorn {
357010fbcf4cSKay Sievers 	device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages);
3571af936a16SLee Schermerhorn }
3572e4455abbSThadeu Lima de Souza Cascardo #endif
3573