xref: /openbmc/linux/mm/vmscan.c (revision 95bbc0c7210a7397fec1cd219f896ca95bf29e3e)
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 
14b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15b1de0d13SMitchel Humpherys 
161da177e4SLinus Torvalds #include <linux/mm.h>
171da177e4SLinus Torvalds #include <linux/module.h>
185a0e3ad6STejun Heo #include <linux/gfp.h>
191da177e4SLinus Torvalds #include <linux/kernel_stat.h>
201da177e4SLinus Torvalds #include <linux/swap.h>
211da177e4SLinus Torvalds #include <linux/pagemap.h>
221da177e4SLinus Torvalds #include <linux/init.h>
231da177e4SLinus Torvalds #include <linux/highmem.h>
2470ddf637SAnton Vorontsov #include <linux/vmpressure.h>
25e129b5c2SAndrew Morton #include <linux/vmstat.h>
261da177e4SLinus Torvalds #include <linux/file.h>
271da177e4SLinus Torvalds #include <linux/writeback.h>
281da177e4SLinus Torvalds #include <linux/blkdev.h>
291da177e4SLinus Torvalds #include <linux/buffer_head.h>	/* for try_to_release_page(),
301da177e4SLinus Torvalds 					buffer_heads_over_limit */
311da177e4SLinus Torvalds #include <linux/mm_inline.h>
321da177e4SLinus Torvalds #include <linux/backing-dev.h>
331da177e4SLinus Torvalds #include <linux/rmap.h>
341da177e4SLinus Torvalds #include <linux/topology.h>
351da177e4SLinus Torvalds #include <linux/cpu.h>
361da177e4SLinus Torvalds #include <linux/cpuset.h>
373e7d3449SMel Gorman #include <linux/compaction.h>
381da177e4SLinus Torvalds #include <linux/notifier.h>
391da177e4SLinus Torvalds #include <linux/rwsem.h>
40248a0301SRafael J. Wysocki #include <linux/delay.h>
413218ae14SYasunori Goto #include <linux/kthread.h>
427dfb7103SNigel Cunningham #include <linux/freezer.h>
4366e1707bSBalbir Singh #include <linux/memcontrol.h>
44873b4771SKeika Kobayashi #include <linux/delayacct.h>
45af936a16SLee Schermerhorn #include <linux/sysctl.h>
46929bea7cSKOSAKI Motohiro #include <linux/oom.h>
47268bb0ceSLinus Torvalds #include <linux/prefetch.h>
48b1de0d13SMitchel Humpherys #include <linux/printk.h>
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <asm/tlbflush.h>
511da177e4SLinus Torvalds #include <asm/div64.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #include <linux/swapops.h>
54117aad1eSRafael Aquini #include <linux/balloon_compaction.h>
551da177e4SLinus Torvalds 
560f8053a5SNick Piggin #include "internal.h"
570f8053a5SNick Piggin 
5833906bc5SMel Gorman #define CREATE_TRACE_POINTS
5933906bc5SMel Gorman #include <trace/events/vmscan.h>
6033906bc5SMel Gorman 
611da177e4SLinus Torvalds struct scan_control {
6222fba335SKOSAKI Motohiro 	/* How many pages shrink_list() should reclaim */
6322fba335SKOSAKI Motohiro 	unsigned long nr_to_reclaim;
6422fba335SKOSAKI Motohiro 
651da177e4SLinus Torvalds 	/* This context's GFP mask */
666daa0e28SAl Viro 	gfp_t gfp_mask;
671da177e4SLinus Torvalds 
68ee814fe2SJohannes Weiner 	/* Allocation order */
695ad333ebSAndy Whitcroft 	int order;
7066e1707bSBalbir Singh 
71ee814fe2SJohannes Weiner 	/*
72ee814fe2SJohannes Weiner 	 * Nodemask of nodes allowed by the caller. If NULL, all nodes
73ee814fe2SJohannes Weiner 	 * are scanned.
74ee814fe2SJohannes Weiner 	 */
75ee814fe2SJohannes Weiner 	nodemask_t	*nodemask;
769e3b2f8cSKonstantin Khlebnikov 
775f53e762SKOSAKI Motohiro 	/*
78f16015fbSJohannes Weiner 	 * The memory cgroup that hit its limit and as a result is the
79f16015fbSJohannes Weiner 	 * primary target of this reclaim invocation.
80f16015fbSJohannes Weiner 	 */
81f16015fbSJohannes Weiner 	struct mem_cgroup *target_mem_cgroup;
8266e1707bSBalbir Singh 
83ee814fe2SJohannes Weiner 	/* Scan (total_size >> priority) pages at once */
84ee814fe2SJohannes Weiner 	int priority;
85ee814fe2SJohannes Weiner 
86ee814fe2SJohannes Weiner 	unsigned int may_writepage:1;
87ee814fe2SJohannes Weiner 
88ee814fe2SJohannes Weiner 	/* Can mapped pages be reclaimed? */
89ee814fe2SJohannes Weiner 	unsigned int may_unmap:1;
90ee814fe2SJohannes Weiner 
91ee814fe2SJohannes Weiner 	/* Can pages be swapped as part of reclaim? */
92ee814fe2SJohannes Weiner 	unsigned int may_swap:1;
93ee814fe2SJohannes Weiner 
94241994edSJohannes Weiner 	/* Can cgroups be reclaimed below their normal consumption range? */
95241994edSJohannes Weiner 	unsigned int may_thrash:1;
96241994edSJohannes Weiner 
97ee814fe2SJohannes Weiner 	unsigned int hibernation_mode:1;
98ee814fe2SJohannes Weiner 
99ee814fe2SJohannes Weiner 	/* One of the zones is ready for compaction */
100ee814fe2SJohannes Weiner 	unsigned int compaction_ready:1;
101ee814fe2SJohannes Weiner 
102ee814fe2SJohannes Weiner 	/* Incremented by the number of inactive pages that were scanned */
103ee814fe2SJohannes Weiner 	unsigned long nr_scanned;
104ee814fe2SJohannes Weiner 
105ee814fe2SJohannes Weiner 	/* Number of pages freed so far during a call to shrink_zones() */
106ee814fe2SJohannes Weiner 	unsigned long nr_reclaimed;
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH
1121da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field)			\
1131da177e4SLinus Torvalds 	do {								\
1141da177e4SLinus Torvalds 		if ((_page)->lru.prev != _base) {			\
1151da177e4SLinus Torvalds 			struct page *prev;				\
1161da177e4SLinus Torvalds 									\
1171da177e4SLinus Torvalds 			prev = lru_to_page(&(_page->lru));		\
1181da177e4SLinus Torvalds 			prefetch(&prev->_field);			\
1191da177e4SLinus Torvalds 		}							\
1201da177e4SLinus Torvalds 	} while (0)
1211da177e4SLinus Torvalds #else
1221da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
1231da177e4SLinus Torvalds #endif
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW
1261da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field)			\
1271da177e4SLinus Torvalds 	do {								\
1281da177e4SLinus Torvalds 		if ((_page)->lru.prev != _base) {			\
1291da177e4SLinus Torvalds 			struct page *prev;				\
1301da177e4SLinus Torvalds 									\
1311da177e4SLinus Torvalds 			prev = lru_to_page(&(_page->lru));		\
1321da177e4SLinus Torvalds 			prefetchw(&prev->_field);			\
1331da177e4SLinus Torvalds 		}							\
1341da177e4SLinus Torvalds 	} while (0)
1351da177e4SLinus Torvalds #else
1361da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
1371da177e4SLinus Torvalds #endif
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds  * From 0 .. 100.  Higher means more swappy.
1411da177e4SLinus Torvalds  */
1421da177e4SLinus Torvalds int vm_swappiness = 60;
143d0480be4SWang Sheng-Hui /*
144d0480be4SWang Sheng-Hui  * The total number of pages which are beyond the high watermark within all
145d0480be4SWang Sheng-Hui  * zones.
146d0480be4SWang Sheng-Hui  */
147d0480be4SWang Sheng-Hui unsigned long vm_total_pages;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static LIST_HEAD(shrinker_list);
1501da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem);
1511da177e4SLinus Torvalds 
152c255a458SAndrew Morton #ifdef CONFIG_MEMCG
15389b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc)
15489b5fae5SJohannes Weiner {
155f16015fbSJohannes Weiner 	return !sc->target_mem_cgroup;
15689b5fae5SJohannes Weiner }
15791a45470SKAMEZAWA Hiroyuki #else
15889b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc)
15989b5fae5SJohannes Weiner {
16089b5fae5SJohannes Weiner 	return true;
16189b5fae5SJohannes Weiner }
16291a45470SKAMEZAWA Hiroyuki #endif
16391a45470SKAMEZAWA Hiroyuki 
164a1c3bfb2SJohannes Weiner static unsigned long zone_reclaimable_pages(struct zone *zone)
1656e543d57SLisa Du {
1666e543d57SLisa Du 	int nr;
1676e543d57SLisa Du 
1686e543d57SLisa Du 	nr = zone_page_state(zone, NR_ACTIVE_FILE) +
1696e543d57SLisa Du 	     zone_page_state(zone, NR_INACTIVE_FILE);
1706e543d57SLisa Du 
1716e543d57SLisa Du 	if (get_nr_swap_pages() > 0)
1726e543d57SLisa Du 		nr += zone_page_state(zone, NR_ACTIVE_ANON) +
1736e543d57SLisa Du 		      zone_page_state(zone, NR_INACTIVE_ANON);
1746e543d57SLisa Du 
1756e543d57SLisa Du 	return nr;
1766e543d57SLisa Du }
1776e543d57SLisa Du 
1786e543d57SLisa Du bool zone_reclaimable(struct zone *zone)
1796e543d57SLisa Du {
1800d5d823aSMel Gorman 	return zone_page_state(zone, NR_PAGES_SCANNED) <
1810d5d823aSMel Gorman 		zone_reclaimable_pages(zone) * 6;
1826e543d57SLisa Du }
1836e543d57SLisa Du 
1844d7dcca2SHugh Dickins static unsigned long get_lru_size(struct lruvec *lruvec, enum lru_list lru)
185c9f299d9SKOSAKI Motohiro {
186c3c787e8SHugh Dickins 	if (!mem_cgroup_disabled())
1874d7dcca2SHugh Dickins 		return mem_cgroup_get_lru_size(lruvec, lru);
188a3d8e054SKOSAKI Motohiro 
189074291feSKonstantin Khlebnikov 	return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru);
190c9f299d9SKOSAKI Motohiro }
191c9f299d9SKOSAKI Motohiro 
1921da177e4SLinus Torvalds /*
1931d3d4437SGlauber Costa  * Add a shrinker callback to be called from the vm.
1941da177e4SLinus Torvalds  */
1951d3d4437SGlauber Costa int register_shrinker(struct shrinker *shrinker)
1961da177e4SLinus Torvalds {
1971d3d4437SGlauber Costa 	size_t size = sizeof(*shrinker->nr_deferred);
1981d3d4437SGlauber Costa 
1991d3d4437SGlauber Costa 	/*
2001d3d4437SGlauber Costa 	 * If we only have one possible node in the system anyway, save
2011d3d4437SGlauber Costa 	 * ourselves the trouble and disable NUMA aware behavior. This way we
2021d3d4437SGlauber Costa 	 * will save memory and some small loop time later.
2031d3d4437SGlauber Costa 	 */
2041d3d4437SGlauber Costa 	if (nr_node_ids == 1)
2051d3d4437SGlauber Costa 		shrinker->flags &= ~SHRINKER_NUMA_AWARE;
2061d3d4437SGlauber Costa 
2071d3d4437SGlauber Costa 	if (shrinker->flags & SHRINKER_NUMA_AWARE)
2081d3d4437SGlauber Costa 		size *= nr_node_ids;
2091d3d4437SGlauber Costa 
2101d3d4437SGlauber Costa 	shrinker->nr_deferred = kzalloc(size, GFP_KERNEL);
2111d3d4437SGlauber Costa 	if (!shrinker->nr_deferred)
2121d3d4437SGlauber Costa 		return -ENOMEM;
2131d3d4437SGlauber Costa 
2141da177e4SLinus Torvalds 	down_write(&shrinker_rwsem);
2151da177e4SLinus Torvalds 	list_add_tail(&shrinker->list, &shrinker_list);
2161da177e4SLinus Torvalds 	up_write(&shrinker_rwsem);
2171d3d4437SGlauber Costa 	return 0;
2181da177e4SLinus Torvalds }
2198e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker);
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds /*
2221da177e4SLinus Torvalds  * Remove one
2231da177e4SLinus Torvalds  */
2248e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker)
2251da177e4SLinus Torvalds {
2261da177e4SLinus Torvalds 	down_write(&shrinker_rwsem);
2271da177e4SLinus Torvalds 	list_del(&shrinker->list);
2281da177e4SLinus Torvalds 	up_write(&shrinker_rwsem);
229ae393321SAndrew Vagin 	kfree(shrinker->nr_deferred);
2301da177e4SLinus Torvalds }
2318e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker);
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds #define SHRINK_BATCH 128
2341d3d4437SGlauber Costa 
235cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
2366b4f7799SJohannes Weiner 				    struct shrinker *shrinker,
2376b4f7799SJohannes Weiner 				    unsigned long nr_scanned,
2386b4f7799SJohannes Weiner 				    unsigned long nr_eligible)
2391da177e4SLinus Torvalds {
24024f7c6b9SDave Chinner 	unsigned long freed = 0;
2411da177e4SLinus Torvalds 	unsigned long long delta;
242635697c6SKonstantin Khlebnikov 	long total_scan;
243d5bc5fd3SVladimir Davydov 	long freeable;
244acf92b48SDave Chinner 	long nr;
245acf92b48SDave Chinner 	long new_nr;
2461d3d4437SGlauber Costa 	int nid = shrinkctl->nid;
247e9299f50SDave Chinner 	long batch_size = shrinker->batch ? shrinker->batch
248e9299f50SDave Chinner 					  : SHRINK_BATCH;
2491da177e4SLinus Torvalds 
250d5bc5fd3SVladimir Davydov 	freeable = shrinker->count_objects(shrinker, shrinkctl);
251d5bc5fd3SVladimir Davydov 	if (freeable == 0)
2521d3d4437SGlauber Costa 		return 0;
253635697c6SKonstantin Khlebnikov 
254acf92b48SDave Chinner 	/*
255acf92b48SDave Chinner 	 * copy the current shrinker scan count into a local variable
256acf92b48SDave Chinner 	 * and zero it so that other concurrent shrinker invocations
257acf92b48SDave Chinner 	 * don't also do this scanning work.
258acf92b48SDave Chinner 	 */
2591d3d4437SGlauber Costa 	nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0);
260acf92b48SDave Chinner 
261acf92b48SDave Chinner 	total_scan = nr;
2626b4f7799SJohannes Weiner 	delta = (4 * nr_scanned) / shrinker->seeks;
263d5bc5fd3SVladimir Davydov 	delta *= freeable;
2646b4f7799SJohannes Weiner 	do_div(delta, nr_eligible + 1);
265acf92b48SDave Chinner 	total_scan += delta;
266acf92b48SDave Chinner 	if (total_scan < 0) {
2678612c663SPintu Kumar 		pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n",
268a0b02131SDave Chinner 		       shrinker->scan_objects, total_scan);
269d5bc5fd3SVladimir Davydov 		total_scan = freeable;
270ea164d73SAndrea Arcangeli 	}
271ea164d73SAndrea Arcangeli 
272ea164d73SAndrea Arcangeli 	/*
2733567b59aSDave Chinner 	 * We need to avoid excessive windup on filesystem shrinkers
2743567b59aSDave Chinner 	 * due to large numbers of GFP_NOFS allocations causing the
2753567b59aSDave Chinner 	 * shrinkers to return -1 all the time. This results in a large
2763567b59aSDave Chinner 	 * nr being built up so when a shrink that can do some work
2773567b59aSDave Chinner 	 * comes along it empties the entire cache due to nr >>>
278d5bc5fd3SVladimir Davydov 	 * freeable. This is bad for sustaining a working set in
2793567b59aSDave Chinner 	 * memory.
2803567b59aSDave Chinner 	 *
2813567b59aSDave Chinner 	 * Hence only allow the shrinker to scan the entire cache when
2823567b59aSDave Chinner 	 * a large delta change is calculated directly.
2833567b59aSDave Chinner 	 */
284d5bc5fd3SVladimir Davydov 	if (delta < freeable / 4)
285d5bc5fd3SVladimir Davydov 		total_scan = min(total_scan, freeable / 2);
2863567b59aSDave Chinner 
2873567b59aSDave Chinner 	/*
288ea164d73SAndrea Arcangeli 	 * Avoid risking looping forever due to too large nr value:
289ea164d73SAndrea Arcangeli 	 * never try to free more than twice the estimate number of
290ea164d73SAndrea Arcangeli 	 * freeable entries.
291ea164d73SAndrea Arcangeli 	 */
292d5bc5fd3SVladimir Davydov 	if (total_scan > freeable * 2)
293d5bc5fd3SVladimir Davydov 		total_scan = freeable * 2;
2941da177e4SLinus Torvalds 
29524f7c6b9SDave Chinner 	trace_mm_shrink_slab_start(shrinker, shrinkctl, nr,
2966b4f7799SJohannes Weiner 				   nr_scanned, nr_eligible,
297d5bc5fd3SVladimir Davydov 				   freeable, delta, total_scan);
29809576073SDave Chinner 
2990b1fb40aSVladimir Davydov 	/*
3000b1fb40aSVladimir Davydov 	 * Normally, we should not scan less than batch_size objects in one
3010b1fb40aSVladimir Davydov 	 * pass to avoid too frequent shrinker calls, but if the slab has less
3020b1fb40aSVladimir Davydov 	 * than batch_size objects in total and we are really tight on memory,
3030b1fb40aSVladimir Davydov 	 * we will try to reclaim all available objects, otherwise we can end
3040b1fb40aSVladimir Davydov 	 * up failing allocations although there are plenty of reclaimable
3050b1fb40aSVladimir Davydov 	 * objects spread over several slabs with usage less than the
3060b1fb40aSVladimir Davydov 	 * batch_size.
3070b1fb40aSVladimir Davydov 	 *
3080b1fb40aSVladimir Davydov 	 * We detect the "tight on memory" situations by looking at the total
3090b1fb40aSVladimir Davydov 	 * number of objects we want to scan (total_scan). If it is greater
310d5bc5fd3SVladimir Davydov 	 * than the total number of objects on slab (freeable), we must be
3110b1fb40aSVladimir Davydov 	 * scanning at high prio and therefore should try to reclaim as much as
3120b1fb40aSVladimir Davydov 	 * possible.
3130b1fb40aSVladimir Davydov 	 */
3140b1fb40aSVladimir Davydov 	while (total_scan >= batch_size ||
315d5bc5fd3SVladimir Davydov 	       total_scan >= freeable) {
31624f7c6b9SDave Chinner 		unsigned long ret;
3170b1fb40aSVladimir Davydov 		unsigned long nr_to_scan = min(batch_size, total_scan);
3181da177e4SLinus Torvalds 
3190b1fb40aSVladimir Davydov 		shrinkctl->nr_to_scan = nr_to_scan;
32024f7c6b9SDave Chinner 		ret = shrinker->scan_objects(shrinker, shrinkctl);
32124f7c6b9SDave Chinner 		if (ret == SHRINK_STOP)
3221da177e4SLinus Torvalds 			break;
32324f7c6b9SDave Chinner 		freed += ret;
32424f7c6b9SDave Chinner 
3250b1fb40aSVladimir Davydov 		count_vm_events(SLABS_SCANNED, nr_to_scan);
3260b1fb40aSVladimir Davydov 		total_scan -= nr_to_scan;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 		cond_resched();
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds 
331acf92b48SDave Chinner 	/*
332acf92b48SDave Chinner 	 * move the unused scan count back into the shrinker in a
333acf92b48SDave Chinner 	 * manner that handles concurrent updates. If we exhausted the
334acf92b48SDave Chinner 	 * scan, there is no need to do an update.
335acf92b48SDave Chinner 	 */
33683aeeadaSKonstantin Khlebnikov 	if (total_scan > 0)
33783aeeadaSKonstantin Khlebnikov 		new_nr = atomic_long_add_return(total_scan,
3381d3d4437SGlauber Costa 						&shrinker->nr_deferred[nid]);
33983aeeadaSKonstantin Khlebnikov 	else
3401d3d4437SGlauber Costa 		new_nr = atomic_long_read(&shrinker->nr_deferred[nid]);
341acf92b48SDave Chinner 
342df9024a8SDave Hansen 	trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan);
3431d3d4437SGlauber Costa 	return freed;
3441d3d4437SGlauber Costa }
3451d3d4437SGlauber Costa 
3466b4f7799SJohannes Weiner /**
347cb731d6cSVladimir Davydov  * shrink_slab - shrink slab caches
3486b4f7799SJohannes Weiner  * @gfp_mask: allocation context
3496b4f7799SJohannes Weiner  * @nid: node whose slab caches to target
350cb731d6cSVladimir Davydov  * @memcg: memory cgroup whose slab caches to target
3516b4f7799SJohannes Weiner  * @nr_scanned: pressure numerator
3526b4f7799SJohannes Weiner  * @nr_eligible: pressure denominator
3531d3d4437SGlauber Costa  *
3546b4f7799SJohannes Weiner  * Call the shrink functions to age shrinkable caches.
3551d3d4437SGlauber Costa  *
3566b4f7799SJohannes Weiner  * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set,
3576b4f7799SJohannes Weiner  * unaware shrinkers will receive a node id of 0 instead.
3581d3d4437SGlauber Costa  *
359cb731d6cSVladimir Davydov  * @memcg specifies the memory cgroup to target. If it is not NULL,
360cb731d6cSVladimir Davydov  * only shrinkers with SHRINKER_MEMCG_AWARE set will be called to scan
361cb731d6cSVladimir Davydov  * objects from the memory cgroup specified. Otherwise all shrinkers
362cb731d6cSVladimir Davydov  * are called, and memcg aware shrinkers are supposed to scan the
363cb731d6cSVladimir Davydov  * global list then.
364cb731d6cSVladimir Davydov  *
3656b4f7799SJohannes Weiner  * @nr_scanned and @nr_eligible form a ratio that indicate how much of
3666b4f7799SJohannes Weiner  * the available objects should be scanned.  Page reclaim for example
3676b4f7799SJohannes Weiner  * passes the number of pages scanned and the number of pages on the
3686b4f7799SJohannes Weiner  * LRU lists that it considered on @nid, plus a bias in @nr_scanned
3696b4f7799SJohannes Weiner  * when it encountered mapped pages.  The ratio is further biased by
3706b4f7799SJohannes Weiner  * the ->seeks setting of the shrink function, which indicates the
3716b4f7799SJohannes Weiner  * cost to recreate an object relative to that of an LRU page.
3721d3d4437SGlauber Costa  *
3736b4f7799SJohannes Weiner  * Returns the number of reclaimed slab objects.
3741d3d4437SGlauber Costa  */
375cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
376cb731d6cSVladimir Davydov 				 struct mem_cgroup *memcg,
3776b4f7799SJohannes Weiner 				 unsigned long nr_scanned,
3786b4f7799SJohannes Weiner 				 unsigned long nr_eligible)
3791d3d4437SGlauber Costa {
3801d3d4437SGlauber Costa 	struct shrinker *shrinker;
3811d3d4437SGlauber Costa 	unsigned long freed = 0;
3821d3d4437SGlauber Costa 
383cb731d6cSVladimir Davydov 	if (memcg && !memcg_kmem_is_active(memcg))
384cb731d6cSVladimir Davydov 		return 0;
385cb731d6cSVladimir Davydov 
3866b4f7799SJohannes Weiner 	if (nr_scanned == 0)
3876b4f7799SJohannes Weiner 		nr_scanned = SWAP_CLUSTER_MAX;
3881d3d4437SGlauber Costa 
3891d3d4437SGlauber Costa 	if (!down_read_trylock(&shrinker_rwsem)) {
3901d3d4437SGlauber Costa 		/*
3911d3d4437SGlauber Costa 		 * If we would return 0, our callers would understand that we
3921d3d4437SGlauber Costa 		 * have nothing else to shrink and give up trying. By returning
3931d3d4437SGlauber Costa 		 * 1 we keep it going and assume we'll be able to shrink next
3941d3d4437SGlauber Costa 		 * time.
3951d3d4437SGlauber Costa 		 */
3961d3d4437SGlauber Costa 		freed = 1;
3971d3d4437SGlauber Costa 		goto out;
3981d3d4437SGlauber Costa 	}
3991d3d4437SGlauber Costa 
4001d3d4437SGlauber Costa 	list_for_each_entry(shrinker, &shrinker_list, list) {
4016b4f7799SJohannes Weiner 		struct shrink_control sc = {
4026b4f7799SJohannes Weiner 			.gfp_mask = gfp_mask,
4036b4f7799SJohannes Weiner 			.nid = nid,
404cb731d6cSVladimir Davydov 			.memcg = memcg,
4056b4f7799SJohannes Weiner 		};
4066b4f7799SJohannes Weiner 
407cb731d6cSVladimir Davydov 		if (memcg && !(shrinker->flags & SHRINKER_MEMCG_AWARE))
408cb731d6cSVladimir Davydov 			continue;
409cb731d6cSVladimir Davydov 
4106b4f7799SJohannes Weiner 		if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
4116b4f7799SJohannes Weiner 			sc.nid = 0;
4126b4f7799SJohannes Weiner 
413cb731d6cSVladimir Davydov 		freed += do_shrink_slab(&sc, shrinker, nr_scanned, nr_eligible);
414ec97097bSVladimir Davydov 	}
4151d3d4437SGlauber Costa 
4161da177e4SLinus Torvalds 	up_read(&shrinker_rwsem);
417f06590bdSMinchan Kim out:
418f06590bdSMinchan Kim 	cond_resched();
41924f7c6b9SDave Chinner 	return freed;
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
422cb731d6cSVladimir Davydov void drop_slab_node(int nid)
423cb731d6cSVladimir Davydov {
424cb731d6cSVladimir Davydov 	unsigned long freed;
425cb731d6cSVladimir Davydov 
426cb731d6cSVladimir Davydov 	do {
427cb731d6cSVladimir Davydov 		struct mem_cgroup *memcg = NULL;
428cb731d6cSVladimir Davydov 
429cb731d6cSVladimir Davydov 		freed = 0;
430cb731d6cSVladimir Davydov 		do {
431cb731d6cSVladimir Davydov 			freed += shrink_slab(GFP_KERNEL, nid, memcg,
432cb731d6cSVladimir Davydov 					     1000, 1000);
433cb731d6cSVladimir Davydov 		} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
434cb731d6cSVladimir Davydov 	} while (freed > 10);
435cb731d6cSVladimir Davydov }
436cb731d6cSVladimir Davydov 
437cb731d6cSVladimir Davydov void drop_slab(void)
438cb731d6cSVladimir Davydov {
439cb731d6cSVladimir Davydov 	int nid;
440cb731d6cSVladimir Davydov 
441cb731d6cSVladimir Davydov 	for_each_online_node(nid)
442cb731d6cSVladimir Davydov 		drop_slab_node(nid);
443cb731d6cSVladimir Davydov }
444cb731d6cSVladimir Davydov 
4451da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page)
4461da177e4SLinus Torvalds {
447ceddc3a5SJohannes Weiner 	/*
448ceddc3a5SJohannes Weiner 	 * A freeable page cache page is referenced only by the caller
449ceddc3a5SJohannes Weiner 	 * that isolated the page, the page cache radix tree and
450ceddc3a5SJohannes Weiner 	 * optional buffer heads at page->private.
451ceddc3a5SJohannes Weiner 	 */
452edcf4748SJohannes Weiner 	return page_count(page) - page_has_private(page) == 2;
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4557d3579e8SKOSAKI Motohiro static int may_write_to_queue(struct backing_dev_info *bdi,
4567d3579e8SKOSAKI Motohiro 			      struct scan_control *sc)
4571da177e4SLinus Torvalds {
458930d9152SChristoph Lameter 	if (current->flags & PF_SWAPWRITE)
4591da177e4SLinus Torvalds 		return 1;
4601da177e4SLinus Torvalds 	if (!bdi_write_congested(bdi))
4611da177e4SLinus Torvalds 		return 1;
4621da177e4SLinus Torvalds 	if (bdi == current->backing_dev_info)
4631da177e4SLinus Torvalds 		return 1;
4641da177e4SLinus Torvalds 	return 0;
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds /*
4681da177e4SLinus Torvalds  * We detected a synchronous write error writing a page out.  Probably
4691da177e4SLinus Torvalds  * -ENOSPC.  We need to propagate that into the address_space for a subsequent
4701da177e4SLinus Torvalds  * fsync(), msync() or close().
4711da177e4SLinus Torvalds  *
4721da177e4SLinus Torvalds  * The tricky part is that after writepage we cannot touch the mapping: nothing
4731da177e4SLinus Torvalds  * prevents it from being freed up.  But we have a ref on the page and once
4741da177e4SLinus Torvalds  * that page is locked, the mapping is pinned.
4751da177e4SLinus Torvalds  *
4761da177e4SLinus Torvalds  * We're allowed to run sleeping lock_page() here because we know the caller has
4771da177e4SLinus Torvalds  * __GFP_FS.
4781da177e4SLinus Torvalds  */
4791da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping,
4801da177e4SLinus Torvalds 				struct page *page, int error)
4811da177e4SLinus Torvalds {
4827eaceaccSJens Axboe 	lock_page(page);
4833e9f45bdSGuillaume Chazarain 	if (page_mapping(page) == mapping)
4843e9f45bdSGuillaume Chazarain 		mapping_set_error(mapping, error);
4851da177e4SLinus Torvalds 	unlock_page(page);
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
48804e62a29SChristoph Lameter /* possible outcome of pageout() */
48904e62a29SChristoph Lameter typedef enum {
49004e62a29SChristoph Lameter 	/* failed to write page out, page is locked */
49104e62a29SChristoph Lameter 	PAGE_KEEP,
49204e62a29SChristoph Lameter 	/* move page to the active list, page is locked */
49304e62a29SChristoph Lameter 	PAGE_ACTIVATE,
49404e62a29SChristoph Lameter 	/* page has been sent to the disk successfully, page is unlocked */
49504e62a29SChristoph Lameter 	PAGE_SUCCESS,
49604e62a29SChristoph Lameter 	/* page is clean and locked */
49704e62a29SChristoph Lameter 	PAGE_CLEAN,
49804e62a29SChristoph Lameter } pageout_t;
49904e62a29SChristoph Lameter 
5001da177e4SLinus Torvalds /*
5011742f19fSAndrew Morton  * pageout is called by shrink_page_list() for each dirty page.
5021742f19fSAndrew Morton  * Calls ->writepage().
5031da177e4SLinus Torvalds  */
504c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping,
5057d3579e8SKOSAKI Motohiro 			 struct scan_control *sc)
5061da177e4SLinus Torvalds {
5071da177e4SLinus Torvalds 	/*
5081da177e4SLinus Torvalds 	 * If the page is dirty, only perform writeback if that write
5091da177e4SLinus Torvalds 	 * will be non-blocking.  To prevent this allocation from being
5101da177e4SLinus Torvalds 	 * stalled by pagecache activity.  But note that there may be
5111da177e4SLinus Torvalds 	 * stalls if we need to run get_block().  We could test
5121da177e4SLinus Torvalds 	 * PagePrivate for that.
5131da177e4SLinus Torvalds 	 *
5148174202bSAl Viro 	 * If this process is currently in __generic_file_write_iter() against
5151da177e4SLinus Torvalds 	 * this page's queue, we can perform writeback even if that
5161da177e4SLinus Torvalds 	 * will block.
5171da177e4SLinus Torvalds 	 *
5181da177e4SLinus Torvalds 	 * If the page is swapcache, write it back even if that would
5191da177e4SLinus Torvalds 	 * block, for some throttling. This happens by accident, because
5201da177e4SLinus Torvalds 	 * swap_backing_dev_info is bust: it doesn't reflect the
5211da177e4SLinus Torvalds 	 * congestion state of the swapdevs.  Easy to fix, if needed.
5221da177e4SLinus Torvalds 	 */
5231da177e4SLinus Torvalds 	if (!is_page_cache_freeable(page))
5241da177e4SLinus Torvalds 		return PAGE_KEEP;
5251da177e4SLinus Torvalds 	if (!mapping) {
5261da177e4SLinus Torvalds 		/*
5271da177e4SLinus Torvalds 		 * Some data journaling orphaned pages can have
5281da177e4SLinus Torvalds 		 * page->mapping == NULL while being dirty with clean buffers.
5291da177e4SLinus Torvalds 		 */
530266cf658SDavid Howells 		if (page_has_private(page)) {
5311da177e4SLinus Torvalds 			if (try_to_free_buffers(page)) {
5321da177e4SLinus Torvalds 				ClearPageDirty(page);
533b1de0d13SMitchel Humpherys 				pr_info("%s: orphaned page\n", __func__);
5341da177e4SLinus Torvalds 				return PAGE_CLEAN;
5351da177e4SLinus Torvalds 			}
5361da177e4SLinus Torvalds 		}
5371da177e4SLinus Torvalds 		return PAGE_KEEP;
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds 	if (mapping->a_ops->writepage == NULL)
5401da177e4SLinus Torvalds 		return PAGE_ACTIVATE;
541de1414a6SChristoph Hellwig 	if (!may_write_to_queue(inode_to_bdi(mapping->host), sc))
5421da177e4SLinus Torvalds 		return PAGE_KEEP;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	if (clear_page_dirty_for_io(page)) {
5451da177e4SLinus Torvalds 		int res;
5461da177e4SLinus Torvalds 		struct writeback_control wbc = {
5471da177e4SLinus Torvalds 			.sync_mode = WB_SYNC_NONE,
5481da177e4SLinus Torvalds 			.nr_to_write = SWAP_CLUSTER_MAX,
549111ebb6eSOGAWA Hirofumi 			.range_start = 0,
550111ebb6eSOGAWA Hirofumi 			.range_end = LLONG_MAX,
5511da177e4SLinus Torvalds 			.for_reclaim = 1,
5521da177e4SLinus Torvalds 		};
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 		SetPageReclaim(page);
5551da177e4SLinus Torvalds 		res = mapping->a_ops->writepage(page, &wbc);
5561da177e4SLinus Torvalds 		if (res < 0)
5571da177e4SLinus Torvalds 			handle_write_error(mapping, page, res);
558994fc28cSZach Brown 		if (res == AOP_WRITEPAGE_ACTIVATE) {
5591da177e4SLinus Torvalds 			ClearPageReclaim(page);
5601da177e4SLinus Torvalds 			return PAGE_ACTIVATE;
5611da177e4SLinus Torvalds 		}
562c661b078SAndy Whitcroft 
5631da177e4SLinus Torvalds 		if (!PageWriteback(page)) {
5641da177e4SLinus Torvalds 			/* synchronous write or broken a_ops? */
5651da177e4SLinus Torvalds 			ClearPageReclaim(page);
5661da177e4SLinus Torvalds 		}
56723b9da55SMel Gorman 		trace_mm_vmscan_writepage(page, trace_reclaim_flags(page));
568e129b5c2SAndrew Morton 		inc_zone_page_state(page, NR_VMSCAN_WRITE);
5691da177e4SLinus Torvalds 		return PAGE_SUCCESS;
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	return PAGE_CLEAN;
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds 
575a649fd92SAndrew Morton /*
576e286781dSNick Piggin  * Same as remove_mapping, but if the page is removed from the mapping, it
577e286781dSNick Piggin  * gets returned with a refcount of 0.
578a649fd92SAndrew Morton  */
579a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page,
580a528910eSJohannes Weiner 			    bool reclaimed)
58149d2e9ccSChristoph Lameter {
58228e4d965SNick Piggin 	BUG_ON(!PageLocked(page));
58328e4d965SNick Piggin 	BUG_ON(mapping != page_mapping(page));
58449d2e9ccSChristoph Lameter 
58519fd6231SNick Piggin 	spin_lock_irq(&mapping->tree_lock);
58649d2e9ccSChristoph Lameter 	/*
5870fd0e6b0SNick Piggin 	 * The non racy check for a busy page.
5880fd0e6b0SNick Piggin 	 *
5890fd0e6b0SNick Piggin 	 * Must be careful with the order of the tests. When someone has
5900fd0e6b0SNick Piggin 	 * a ref to the page, it may be possible that they dirty it then
5910fd0e6b0SNick Piggin 	 * drop the reference. So if PageDirty is tested before page_count
5920fd0e6b0SNick Piggin 	 * here, then the following race may occur:
5930fd0e6b0SNick Piggin 	 *
5940fd0e6b0SNick Piggin 	 * get_user_pages(&page);
5950fd0e6b0SNick Piggin 	 * [user mapping goes away]
5960fd0e6b0SNick Piggin 	 * write_to(page);
5970fd0e6b0SNick Piggin 	 *				!PageDirty(page)    [good]
5980fd0e6b0SNick Piggin 	 * SetPageDirty(page);
5990fd0e6b0SNick Piggin 	 * put_page(page);
6000fd0e6b0SNick Piggin 	 *				!page_count(page)   [good, discard it]
6010fd0e6b0SNick Piggin 	 *
6020fd0e6b0SNick Piggin 	 * [oops, our write_to data is lost]
6030fd0e6b0SNick Piggin 	 *
6040fd0e6b0SNick Piggin 	 * Reversing the order of the tests ensures such a situation cannot
6050fd0e6b0SNick Piggin 	 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
6060fd0e6b0SNick Piggin 	 * load is not satisfied before that of page->_count.
6070fd0e6b0SNick Piggin 	 *
6080fd0e6b0SNick Piggin 	 * Note that if SetPageDirty is always performed via set_page_dirty,
6090fd0e6b0SNick Piggin 	 * and thus under tree_lock, then this ordering is not required.
61049d2e9ccSChristoph Lameter 	 */
611e286781dSNick Piggin 	if (!page_freeze_refs(page, 2))
61249d2e9ccSChristoph Lameter 		goto cannot_free;
613e286781dSNick Piggin 	/* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
614e286781dSNick Piggin 	if (unlikely(PageDirty(page))) {
615e286781dSNick Piggin 		page_unfreeze_refs(page, 2);
61649d2e9ccSChristoph Lameter 		goto cannot_free;
617e286781dSNick Piggin 	}
61849d2e9ccSChristoph Lameter 
61949d2e9ccSChristoph Lameter 	if (PageSwapCache(page)) {
62049d2e9ccSChristoph Lameter 		swp_entry_t swap = { .val = page_private(page) };
6210a31bc97SJohannes Weiner 		mem_cgroup_swapout(page, swap);
62249d2e9ccSChristoph Lameter 		__delete_from_swap_cache(page);
62319fd6231SNick Piggin 		spin_unlock_irq(&mapping->tree_lock);
6240a31bc97SJohannes Weiner 		swapcache_free(swap);
625e286781dSNick Piggin 	} else {
6266072d13cSLinus Torvalds 		void (*freepage)(struct page *);
627a528910eSJohannes Weiner 		void *shadow = NULL;
6286072d13cSLinus Torvalds 
6296072d13cSLinus Torvalds 		freepage = mapping->a_ops->freepage;
630a528910eSJohannes Weiner 		/*
631a528910eSJohannes Weiner 		 * Remember a shadow entry for reclaimed file cache in
632a528910eSJohannes Weiner 		 * order to detect refaults, thus thrashing, later on.
633a528910eSJohannes Weiner 		 *
634a528910eSJohannes Weiner 		 * But don't store shadows in an address space that is
635a528910eSJohannes Weiner 		 * already exiting.  This is not just an optizimation,
636a528910eSJohannes Weiner 		 * inode reclaim needs to empty out the radix tree or
637a528910eSJohannes Weiner 		 * the nodes are lost.  Don't plant shadows behind its
638a528910eSJohannes Weiner 		 * back.
639a528910eSJohannes Weiner 		 */
640a528910eSJohannes Weiner 		if (reclaimed && page_is_file_cache(page) &&
641a528910eSJohannes Weiner 		    !mapping_exiting(mapping))
642a528910eSJohannes Weiner 			shadow = workingset_eviction(mapping, page);
643a528910eSJohannes Weiner 		__delete_from_page_cache(page, shadow);
64419fd6231SNick Piggin 		spin_unlock_irq(&mapping->tree_lock);
6456072d13cSLinus Torvalds 
6466072d13cSLinus Torvalds 		if (freepage != NULL)
6476072d13cSLinus Torvalds 			freepage(page);
648e286781dSNick Piggin 	}
649e286781dSNick Piggin 
65049d2e9ccSChristoph Lameter 	return 1;
65149d2e9ccSChristoph Lameter 
65249d2e9ccSChristoph Lameter cannot_free:
65319fd6231SNick Piggin 	spin_unlock_irq(&mapping->tree_lock);
65449d2e9ccSChristoph Lameter 	return 0;
65549d2e9ccSChristoph Lameter }
65649d2e9ccSChristoph Lameter 
6571da177e4SLinus Torvalds /*
658e286781dSNick Piggin  * Attempt to detach a locked page from its ->mapping.  If it is dirty or if
659e286781dSNick Piggin  * someone else has a ref on the page, abort and return 0.  If it was
660e286781dSNick Piggin  * successfully detached, return 1.  Assumes the caller has a single ref on
661e286781dSNick Piggin  * this page.
662e286781dSNick Piggin  */
663e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page)
664e286781dSNick Piggin {
665a528910eSJohannes Weiner 	if (__remove_mapping(mapping, page, false)) {
666e286781dSNick Piggin 		/*
667e286781dSNick Piggin 		 * Unfreezing the refcount with 1 rather than 2 effectively
668e286781dSNick Piggin 		 * drops the pagecache ref for us without requiring another
669e286781dSNick Piggin 		 * atomic operation.
670e286781dSNick Piggin 		 */
671e286781dSNick Piggin 		page_unfreeze_refs(page, 1);
672e286781dSNick Piggin 		return 1;
673e286781dSNick Piggin 	}
674e286781dSNick Piggin 	return 0;
675e286781dSNick Piggin }
676e286781dSNick Piggin 
677894bc310SLee Schermerhorn /**
678894bc310SLee Schermerhorn  * putback_lru_page - put previously isolated page onto appropriate LRU list
679894bc310SLee Schermerhorn  * @page: page to be put back to appropriate lru list
680894bc310SLee Schermerhorn  *
681894bc310SLee Schermerhorn  * Add previously isolated @page to appropriate LRU list.
682894bc310SLee Schermerhorn  * Page may still be unevictable for other reasons.
683894bc310SLee Schermerhorn  *
684894bc310SLee Schermerhorn  * lru_lock must not be held, interrupts must be enabled.
685894bc310SLee Schermerhorn  */
686894bc310SLee Schermerhorn void putback_lru_page(struct page *page)
687894bc310SLee Schermerhorn {
6880ec3b74cSVlastimil Babka 	bool is_unevictable;
689bbfd28eeSLee Schermerhorn 	int was_unevictable = PageUnevictable(page);
690894bc310SLee Schermerhorn 
691309381feSSasha Levin 	VM_BUG_ON_PAGE(PageLRU(page), page);
692894bc310SLee Schermerhorn 
693894bc310SLee Schermerhorn redo:
694894bc310SLee Schermerhorn 	ClearPageUnevictable(page);
695894bc310SLee Schermerhorn 
69639b5f29aSHugh Dickins 	if (page_evictable(page)) {
697894bc310SLee Schermerhorn 		/*
698894bc310SLee Schermerhorn 		 * For evictable pages, we can use the cache.
699894bc310SLee Schermerhorn 		 * In event of a race, worst case is we end up with an
700894bc310SLee Schermerhorn 		 * unevictable page on [in]active list.
701894bc310SLee Schermerhorn 		 * We know how to handle that.
702894bc310SLee Schermerhorn 		 */
7030ec3b74cSVlastimil Babka 		is_unevictable = false;
704c53954a0SMel Gorman 		lru_cache_add(page);
705894bc310SLee Schermerhorn 	} else {
706894bc310SLee Schermerhorn 		/*
707894bc310SLee Schermerhorn 		 * Put unevictable pages directly on zone's unevictable
708894bc310SLee Schermerhorn 		 * list.
709894bc310SLee Schermerhorn 		 */
7100ec3b74cSVlastimil Babka 		is_unevictable = true;
711894bc310SLee Schermerhorn 		add_page_to_unevictable_list(page);
7126a7b9548SJohannes Weiner 		/*
71321ee9f39SMinchan Kim 		 * When racing with an mlock or AS_UNEVICTABLE clearing
71421ee9f39SMinchan Kim 		 * (page is unlocked) make sure that if the other thread
71521ee9f39SMinchan Kim 		 * does not observe our setting of PG_lru and fails
71624513264SHugh Dickins 		 * isolation/check_move_unevictable_pages,
71721ee9f39SMinchan Kim 		 * we see PG_mlocked/AS_UNEVICTABLE cleared below and move
7186a7b9548SJohannes Weiner 		 * the page back to the evictable list.
7196a7b9548SJohannes Weiner 		 *
72021ee9f39SMinchan Kim 		 * The other side is TestClearPageMlocked() or shmem_lock().
7216a7b9548SJohannes Weiner 		 */
7226a7b9548SJohannes Weiner 		smp_mb();
723894bc310SLee Schermerhorn 	}
724894bc310SLee Schermerhorn 
725894bc310SLee Schermerhorn 	/*
726894bc310SLee Schermerhorn 	 * page's status can change while we move it among lru. If an evictable
727894bc310SLee Schermerhorn 	 * page is on unevictable list, it never be freed. To avoid that,
728894bc310SLee Schermerhorn 	 * check after we added it to the list, again.
729894bc310SLee Schermerhorn 	 */
7300ec3b74cSVlastimil Babka 	if (is_unevictable && page_evictable(page)) {
731894bc310SLee Schermerhorn 		if (!isolate_lru_page(page)) {
732894bc310SLee Schermerhorn 			put_page(page);
733894bc310SLee Schermerhorn 			goto redo;
734894bc310SLee Schermerhorn 		}
735894bc310SLee Schermerhorn 		/* This means someone else dropped this page from LRU
736894bc310SLee Schermerhorn 		 * So, it will be freed or putback to LRU again. There is
737894bc310SLee Schermerhorn 		 * nothing to do here.
738894bc310SLee Schermerhorn 		 */
739894bc310SLee Schermerhorn 	}
740894bc310SLee Schermerhorn 
7410ec3b74cSVlastimil Babka 	if (was_unevictable && !is_unevictable)
742bbfd28eeSLee Schermerhorn 		count_vm_event(UNEVICTABLE_PGRESCUED);
7430ec3b74cSVlastimil Babka 	else if (!was_unevictable && is_unevictable)
744bbfd28eeSLee Schermerhorn 		count_vm_event(UNEVICTABLE_PGCULLED);
745bbfd28eeSLee Schermerhorn 
746894bc310SLee Schermerhorn 	put_page(page);		/* drop ref from isolate */
747894bc310SLee Schermerhorn }
748894bc310SLee Schermerhorn 
749dfc8d636SJohannes Weiner enum page_references {
750dfc8d636SJohannes Weiner 	PAGEREF_RECLAIM,
751dfc8d636SJohannes Weiner 	PAGEREF_RECLAIM_CLEAN,
75264574746SJohannes Weiner 	PAGEREF_KEEP,
753dfc8d636SJohannes Weiner 	PAGEREF_ACTIVATE,
754dfc8d636SJohannes Weiner };
755dfc8d636SJohannes Weiner 
756dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page,
757dfc8d636SJohannes Weiner 						  struct scan_control *sc)
758dfc8d636SJohannes Weiner {
75964574746SJohannes Weiner 	int referenced_ptes, referenced_page;
760dfc8d636SJohannes Weiner 	unsigned long vm_flags;
761dfc8d636SJohannes Weiner 
762c3ac9a8aSJohannes Weiner 	referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup,
763c3ac9a8aSJohannes Weiner 					  &vm_flags);
76464574746SJohannes Weiner 	referenced_page = TestClearPageReferenced(page);
765dfc8d636SJohannes Weiner 
766dfc8d636SJohannes Weiner 	/*
767dfc8d636SJohannes Weiner 	 * Mlock lost the isolation race with us.  Let try_to_unmap()
768dfc8d636SJohannes Weiner 	 * move the page to the unevictable list.
769dfc8d636SJohannes Weiner 	 */
770dfc8d636SJohannes Weiner 	if (vm_flags & VM_LOCKED)
771dfc8d636SJohannes Weiner 		return PAGEREF_RECLAIM;
772dfc8d636SJohannes Weiner 
77364574746SJohannes Weiner 	if (referenced_ptes) {
774e4898273SMichal Hocko 		if (PageSwapBacked(page))
77564574746SJohannes Weiner 			return PAGEREF_ACTIVATE;
77664574746SJohannes Weiner 		/*
77764574746SJohannes Weiner 		 * All mapped pages start out with page table
77864574746SJohannes Weiner 		 * references from the instantiating fault, so we need
77964574746SJohannes Weiner 		 * to look twice if a mapped file page is used more
78064574746SJohannes Weiner 		 * than once.
78164574746SJohannes Weiner 		 *
78264574746SJohannes Weiner 		 * Mark it and spare it for another trip around the
78364574746SJohannes Weiner 		 * inactive list.  Another page table reference will
78464574746SJohannes Weiner 		 * lead to its activation.
78564574746SJohannes Weiner 		 *
78664574746SJohannes Weiner 		 * Note: the mark is set for activated pages as well
78764574746SJohannes Weiner 		 * so that recently deactivated but used pages are
78864574746SJohannes Weiner 		 * quickly recovered.
78964574746SJohannes Weiner 		 */
79064574746SJohannes Weiner 		SetPageReferenced(page);
79164574746SJohannes Weiner 
79234dbc67aSKonstantin Khlebnikov 		if (referenced_page || referenced_ptes > 1)
793dfc8d636SJohannes Weiner 			return PAGEREF_ACTIVATE;
794dfc8d636SJohannes Weiner 
795c909e993SKonstantin Khlebnikov 		/*
796c909e993SKonstantin Khlebnikov 		 * Activate file-backed executable pages after first usage.
797c909e993SKonstantin Khlebnikov 		 */
798c909e993SKonstantin Khlebnikov 		if (vm_flags & VM_EXEC)
799c909e993SKonstantin Khlebnikov 			return PAGEREF_ACTIVATE;
800c909e993SKonstantin Khlebnikov 
80164574746SJohannes Weiner 		return PAGEREF_KEEP;
80264574746SJohannes Weiner 	}
80364574746SJohannes Weiner 
804dfc8d636SJohannes Weiner 	/* Reclaim if clean, defer dirty pages to writeback */
8052e30244aSKOSAKI Motohiro 	if (referenced_page && !PageSwapBacked(page))
806dfc8d636SJohannes Weiner 		return PAGEREF_RECLAIM_CLEAN;
80764574746SJohannes Weiner 
80864574746SJohannes Weiner 	return PAGEREF_RECLAIM;
809dfc8d636SJohannes Weiner }
810dfc8d636SJohannes Weiner 
811e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */
812e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page,
813e2be15f6SMel Gorman 				       bool *dirty, bool *writeback)
814e2be15f6SMel Gorman {
815b4597226SMel Gorman 	struct address_space *mapping;
816b4597226SMel Gorman 
817e2be15f6SMel Gorman 	/*
818e2be15f6SMel Gorman 	 * Anonymous pages are not handled by flushers and must be written
819e2be15f6SMel Gorman 	 * from reclaim context. Do not stall reclaim based on them
820e2be15f6SMel Gorman 	 */
821e2be15f6SMel Gorman 	if (!page_is_file_cache(page)) {
822e2be15f6SMel Gorman 		*dirty = false;
823e2be15f6SMel Gorman 		*writeback = false;
824e2be15f6SMel Gorman 		return;
825e2be15f6SMel Gorman 	}
826e2be15f6SMel Gorman 
827e2be15f6SMel Gorman 	/* By default assume that the page flags are accurate */
828e2be15f6SMel Gorman 	*dirty = PageDirty(page);
829e2be15f6SMel Gorman 	*writeback = PageWriteback(page);
830b4597226SMel Gorman 
831b4597226SMel Gorman 	/* Verify dirty/writeback state if the filesystem supports it */
832b4597226SMel Gorman 	if (!page_has_private(page))
833b4597226SMel Gorman 		return;
834b4597226SMel Gorman 
835b4597226SMel Gorman 	mapping = page_mapping(page);
836b4597226SMel Gorman 	if (mapping && mapping->a_ops->is_dirty_writeback)
837b4597226SMel Gorman 		mapping->a_ops->is_dirty_writeback(page, dirty, writeback);
838e2be15f6SMel Gorman }
839e2be15f6SMel Gorman 
840e286781dSNick Piggin /*
8411742f19fSAndrew Morton  * shrink_page_list() returns the number of reclaimed pages
8421da177e4SLinus Torvalds  */
8431742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list,
8446a18adb3SKonstantin Khlebnikov 				      struct zone *zone,
845f84f6e2bSMel Gorman 				      struct scan_control *sc,
84602c6de8dSMinchan Kim 				      enum ttu_flags ttu_flags,
8478e950282SMel Gorman 				      unsigned long *ret_nr_dirty,
848d43006d5SMel Gorman 				      unsigned long *ret_nr_unqueued_dirty,
8498e950282SMel Gorman 				      unsigned long *ret_nr_congested,
85002c6de8dSMinchan Kim 				      unsigned long *ret_nr_writeback,
851b1a6f21eSMel Gorman 				      unsigned long *ret_nr_immediate,
85202c6de8dSMinchan Kim 				      bool force_reclaim)
8531da177e4SLinus Torvalds {
8541da177e4SLinus Torvalds 	LIST_HEAD(ret_pages);
855abe4c3b5SMel Gorman 	LIST_HEAD(free_pages);
8561da177e4SLinus Torvalds 	int pgactivate = 0;
857d43006d5SMel Gorman 	unsigned long nr_unqueued_dirty = 0;
8580e093d99SMel Gorman 	unsigned long nr_dirty = 0;
8590e093d99SMel Gorman 	unsigned long nr_congested = 0;
86005ff5137SAndrew Morton 	unsigned long nr_reclaimed = 0;
86192df3a72SMel Gorman 	unsigned long nr_writeback = 0;
862b1a6f21eSMel Gorman 	unsigned long nr_immediate = 0;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	cond_resched();
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	while (!list_empty(page_list)) {
8671da177e4SLinus Torvalds 		struct address_space *mapping;
8681da177e4SLinus Torvalds 		struct page *page;
8691da177e4SLinus Torvalds 		int may_enter_fs;
87002c6de8dSMinchan Kim 		enum page_references references = PAGEREF_RECLAIM_CLEAN;
871e2be15f6SMel Gorman 		bool dirty, writeback;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 		cond_resched();
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 		page = lru_to_page(page_list);
8761da177e4SLinus Torvalds 		list_del(&page->lru);
8771da177e4SLinus Torvalds 
878529ae9aaSNick Piggin 		if (!trylock_page(page))
8791da177e4SLinus Torvalds 			goto keep;
8801da177e4SLinus Torvalds 
881309381feSSasha Levin 		VM_BUG_ON_PAGE(PageActive(page), page);
882309381feSSasha Levin 		VM_BUG_ON_PAGE(page_zone(page) != zone, page);
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 		sc->nr_scanned++;
88580e43426SChristoph Lameter 
88639b5f29aSHugh Dickins 		if (unlikely(!page_evictable(page)))
887b291f000SNick Piggin 			goto cull_mlocked;
888894bc310SLee Schermerhorn 
889a6dc60f8SJohannes Weiner 		if (!sc->may_unmap && page_mapped(page))
89080e43426SChristoph Lameter 			goto keep_locked;
89180e43426SChristoph Lameter 
8921da177e4SLinus Torvalds 		/* Double the slab pressure for mapped and swapcache pages */
8931da177e4SLinus Torvalds 		if (page_mapped(page) || PageSwapCache(page))
8941da177e4SLinus Torvalds 			sc->nr_scanned++;
8951da177e4SLinus Torvalds 
896c661b078SAndy Whitcroft 		may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
897c661b078SAndy Whitcroft 			(PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
898c661b078SAndy Whitcroft 
899e62e384eSMichal Hocko 		/*
900e2be15f6SMel Gorman 		 * The number of dirty pages determines if a zone is marked
901e2be15f6SMel Gorman 		 * reclaim_congested which affects wait_iff_congested. kswapd
902e2be15f6SMel Gorman 		 * will stall and start writing pages if the tail of the LRU
903e2be15f6SMel Gorman 		 * is all dirty unqueued pages.
904e2be15f6SMel Gorman 		 */
905e2be15f6SMel Gorman 		page_check_dirty_writeback(page, &dirty, &writeback);
906e2be15f6SMel Gorman 		if (dirty || writeback)
907e2be15f6SMel Gorman 			nr_dirty++;
908e2be15f6SMel Gorman 
909e2be15f6SMel Gorman 		if (dirty && !writeback)
910e2be15f6SMel Gorman 			nr_unqueued_dirty++;
911e2be15f6SMel Gorman 
912d04e8acdSMel Gorman 		/*
913d04e8acdSMel Gorman 		 * Treat this page as congested if the underlying BDI is or if
914d04e8acdSMel Gorman 		 * pages are cycling through the LRU so quickly that the
915d04e8acdSMel Gorman 		 * pages marked for immediate reclaim are making it to the
916d04e8acdSMel Gorman 		 * end of the LRU a second time.
917d04e8acdSMel Gorman 		 */
918e2be15f6SMel Gorman 		mapping = page_mapping(page);
9191da58ee2SJamie Liu 		if (((dirty || writeback) && mapping &&
920de1414a6SChristoph Hellwig 		     bdi_write_congested(inode_to_bdi(mapping->host))) ||
921d04e8acdSMel Gorman 		    (writeback && PageReclaim(page)))
922e2be15f6SMel Gorman 			nr_congested++;
923e2be15f6SMel Gorman 
924e2be15f6SMel Gorman 		/*
925283aba9fSMel Gorman 		 * If a page at the tail of the LRU is under writeback, there
926283aba9fSMel Gorman 		 * are three cases to consider.
927e62e384eSMichal Hocko 		 *
928283aba9fSMel Gorman 		 * 1) If reclaim is encountering an excessive number of pages
929283aba9fSMel Gorman 		 *    under writeback and this page is both under writeback and
930283aba9fSMel Gorman 		 *    PageReclaim then it indicates that pages are being queued
931283aba9fSMel Gorman 		 *    for IO but are being recycled through the LRU before the
932283aba9fSMel Gorman 		 *    IO can complete. Waiting on the page itself risks an
933283aba9fSMel Gorman 		 *    indefinite stall if it is impossible to writeback the
934283aba9fSMel Gorman 		 *    page due to IO error or disconnected storage so instead
935b1a6f21eSMel Gorman 		 *    note that the LRU is being scanned too quickly and the
936b1a6f21eSMel Gorman 		 *    caller can stall after page list has been processed.
937c3b94f44SHugh Dickins 		 *
938283aba9fSMel Gorman 		 * 2) Global reclaim encounters a page, memcg encounters a
939283aba9fSMel Gorman 		 *    page that is not marked for immediate reclaim or
940283aba9fSMel Gorman 		 *    the caller does not have __GFP_IO. In this case mark
941283aba9fSMel Gorman 		 *    the page for immediate reclaim and continue scanning.
942283aba9fSMel Gorman 		 *
943283aba9fSMel Gorman 		 *    __GFP_IO is checked  because a loop driver thread might
944283aba9fSMel Gorman 		 *    enter reclaim, and deadlock if it waits on a page for
945283aba9fSMel Gorman 		 *    which it is needed to do the write (loop masks off
946283aba9fSMel Gorman 		 *    __GFP_IO|__GFP_FS for this reason); but more thought
947283aba9fSMel Gorman 		 *    would probably show more reasons.
948283aba9fSMel Gorman 		 *
949283aba9fSMel Gorman 		 *    Don't require __GFP_FS, since we're not going into the
950283aba9fSMel Gorman 		 *    FS, just waiting on its writeback completion. Worryingly,
951283aba9fSMel Gorman 		 *    ext4 gfs2 and xfs allocate pages with
952283aba9fSMel Gorman 		 *    grab_cache_page_write_begin(,,AOP_FLAG_NOFS), so testing
953283aba9fSMel Gorman 		 *    may_enter_fs here is liable to OOM on them.
954283aba9fSMel Gorman 		 *
955283aba9fSMel Gorman 		 * 3) memcg encounters a page that is not already marked
956283aba9fSMel Gorman 		 *    PageReclaim. memcg does not have any dirty pages
957283aba9fSMel Gorman 		 *    throttling so we could easily OOM just because too many
958283aba9fSMel Gorman 		 *    pages are in writeback and there is nothing else to
959283aba9fSMel Gorman 		 *    reclaim. Wait for the writeback to complete.
960e62e384eSMichal Hocko 		 */
961283aba9fSMel Gorman 		if (PageWriteback(page)) {
962283aba9fSMel Gorman 			/* Case 1 above */
963283aba9fSMel Gorman 			if (current_is_kswapd() &&
964283aba9fSMel Gorman 			    PageReclaim(page) &&
96557054651SJohannes Weiner 			    test_bit(ZONE_WRITEBACK, &zone->flags)) {
966b1a6f21eSMel Gorman 				nr_immediate++;
967b1a6f21eSMel Gorman 				goto keep_locked;
968283aba9fSMel Gorman 
969283aba9fSMel Gorman 			/* Case 2 above */
970283aba9fSMel Gorman 			} else if (global_reclaim(sc) ||
971c3b94f44SHugh Dickins 			    !PageReclaim(page) || !(sc->gfp_mask & __GFP_IO)) {
972c3b94f44SHugh Dickins 				/*
973c3b94f44SHugh Dickins 				 * This is slightly racy - end_page_writeback()
974c3b94f44SHugh Dickins 				 * might have just cleared PageReclaim, then
975c3b94f44SHugh Dickins 				 * setting PageReclaim here end up interpreted
976c3b94f44SHugh Dickins 				 * as PageReadahead - but that does not matter
977c3b94f44SHugh Dickins 				 * enough to care.  What we do want is for this
978c3b94f44SHugh Dickins 				 * page to have PageReclaim set next time memcg
979c3b94f44SHugh Dickins 				 * reclaim reaches the tests above, so it will
980c3b94f44SHugh Dickins 				 * then wait_on_page_writeback() to avoid OOM;
981c3b94f44SHugh Dickins 				 * and it's also appropriate in global reclaim.
982c3b94f44SHugh Dickins 				 */
983c3b94f44SHugh Dickins 				SetPageReclaim(page);
98492df3a72SMel Gorman 				nr_writeback++;
985283aba9fSMel Gorman 
986c3b94f44SHugh Dickins 				goto keep_locked;
987283aba9fSMel Gorman 
988283aba9fSMel Gorman 			/* Case 3 above */
989283aba9fSMel Gorman 			} else {
990c3b94f44SHugh Dickins 				wait_on_page_writeback(page);
991e62e384eSMichal Hocko 			}
992283aba9fSMel Gorman 		}
9931da177e4SLinus Torvalds 
99402c6de8dSMinchan Kim 		if (!force_reclaim)
9956a18adb3SKonstantin Khlebnikov 			references = page_check_references(page, sc);
99602c6de8dSMinchan Kim 
997dfc8d636SJohannes Weiner 		switch (references) {
998dfc8d636SJohannes Weiner 		case PAGEREF_ACTIVATE:
9991da177e4SLinus Torvalds 			goto activate_locked;
100064574746SJohannes Weiner 		case PAGEREF_KEEP:
100164574746SJohannes Weiner 			goto keep_locked;
1002dfc8d636SJohannes Weiner 		case PAGEREF_RECLAIM:
1003dfc8d636SJohannes Weiner 		case PAGEREF_RECLAIM_CLEAN:
1004dfc8d636SJohannes Weiner 			; /* try to reclaim the page below */
1005dfc8d636SJohannes Weiner 		}
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 		/*
10081da177e4SLinus Torvalds 		 * Anonymous process memory has backing store?
10091da177e4SLinus Torvalds 		 * Try to allocate it some swap space here.
10101da177e4SLinus Torvalds 		 */
1011b291f000SNick Piggin 		if (PageAnon(page) && !PageSwapCache(page)) {
101263eb6b93SHugh Dickins 			if (!(sc->gfp_mask & __GFP_IO))
101363eb6b93SHugh Dickins 				goto keep_locked;
10145bc7b8acSShaohua Li 			if (!add_to_swap(page, page_list))
10151da177e4SLinus Torvalds 				goto activate_locked;
101663eb6b93SHugh Dickins 			may_enter_fs = 1;
10171da177e4SLinus Torvalds 
1018e2be15f6SMel Gorman 			/* Adding to swap updated mapping */
10191da177e4SLinus Torvalds 			mapping = page_mapping(page);
1020e2be15f6SMel Gorman 		}
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 		/*
10231da177e4SLinus Torvalds 		 * The page is mapped into the page tables of one or more
10241da177e4SLinus Torvalds 		 * processes. Try to unmap it here.
10251da177e4SLinus Torvalds 		 */
10261da177e4SLinus Torvalds 		if (page_mapped(page) && mapping) {
102702c6de8dSMinchan Kim 			switch (try_to_unmap(page, ttu_flags)) {
10281da177e4SLinus Torvalds 			case SWAP_FAIL:
10291da177e4SLinus Torvalds 				goto activate_locked;
10301da177e4SLinus Torvalds 			case SWAP_AGAIN:
10311da177e4SLinus Torvalds 				goto keep_locked;
1032b291f000SNick Piggin 			case SWAP_MLOCK:
1033b291f000SNick Piggin 				goto cull_mlocked;
10341da177e4SLinus Torvalds 			case SWAP_SUCCESS:
10351da177e4SLinus Torvalds 				; /* try to free the page below */
10361da177e4SLinus Torvalds 			}
10371da177e4SLinus Torvalds 		}
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 		if (PageDirty(page)) {
1040ee72886dSMel Gorman 			/*
1041ee72886dSMel Gorman 			 * Only kswapd can writeback filesystem pages to
1042d43006d5SMel Gorman 			 * avoid risk of stack overflow but only writeback
1043d43006d5SMel Gorman 			 * if many dirty pages have been encountered.
1044ee72886dSMel Gorman 			 */
1045f84f6e2bSMel Gorman 			if (page_is_file_cache(page) &&
10469e3b2f8cSKonstantin Khlebnikov 					(!current_is_kswapd() ||
104757054651SJohannes Weiner 					 !test_bit(ZONE_DIRTY, &zone->flags))) {
104849ea7eb6SMel Gorman 				/*
104949ea7eb6SMel Gorman 				 * Immediately reclaim when written back.
105049ea7eb6SMel Gorman 				 * Similar in principal to deactivate_page()
105149ea7eb6SMel Gorman 				 * except we already have the page isolated
105249ea7eb6SMel Gorman 				 * and know it's dirty
105349ea7eb6SMel Gorman 				 */
105449ea7eb6SMel Gorman 				inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE);
105549ea7eb6SMel Gorman 				SetPageReclaim(page);
105649ea7eb6SMel Gorman 
1057ee72886dSMel Gorman 				goto keep_locked;
1058ee72886dSMel Gorman 			}
1059ee72886dSMel Gorman 
1060dfc8d636SJohannes Weiner 			if (references == PAGEREF_RECLAIM_CLEAN)
10611da177e4SLinus Torvalds 				goto keep_locked;
10624dd4b920SAndrew Morton 			if (!may_enter_fs)
10631da177e4SLinus Torvalds 				goto keep_locked;
106452a8363eSChristoph Lameter 			if (!sc->may_writepage)
10651da177e4SLinus Torvalds 				goto keep_locked;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 			/* Page is dirty, try to write it out here */
10687d3579e8SKOSAKI Motohiro 			switch (pageout(page, mapping, sc)) {
10691da177e4SLinus Torvalds 			case PAGE_KEEP:
10701da177e4SLinus Torvalds 				goto keep_locked;
10711da177e4SLinus Torvalds 			case PAGE_ACTIVATE:
10721da177e4SLinus Torvalds 				goto activate_locked;
10731da177e4SLinus Torvalds 			case PAGE_SUCCESS:
10747d3579e8SKOSAKI Motohiro 				if (PageWriteback(page))
107541ac1999SMel Gorman 					goto keep;
10767d3579e8SKOSAKI Motohiro 				if (PageDirty(page))
10771da177e4SLinus Torvalds 					goto keep;
10787d3579e8SKOSAKI Motohiro 
10791da177e4SLinus Torvalds 				/*
10801da177e4SLinus Torvalds 				 * A synchronous write - probably a ramdisk.  Go
10811da177e4SLinus Torvalds 				 * ahead and try to reclaim the page.
10821da177e4SLinus Torvalds 				 */
1083529ae9aaSNick Piggin 				if (!trylock_page(page))
10841da177e4SLinus Torvalds 					goto keep;
10851da177e4SLinus Torvalds 				if (PageDirty(page) || PageWriteback(page))
10861da177e4SLinus Torvalds 					goto keep_locked;
10871da177e4SLinus Torvalds 				mapping = page_mapping(page);
10881da177e4SLinus Torvalds 			case PAGE_CLEAN:
10891da177e4SLinus Torvalds 				; /* try to free the page below */
10901da177e4SLinus Torvalds 			}
10911da177e4SLinus Torvalds 		}
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 		/*
10941da177e4SLinus Torvalds 		 * If the page has buffers, try to free the buffer mappings
10951da177e4SLinus Torvalds 		 * associated with this page. If we succeed we try to free
10961da177e4SLinus Torvalds 		 * the page as well.
10971da177e4SLinus Torvalds 		 *
10981da177e4SLinus Torvalds 		 * We do this even if the page is PageDirty().
10991da177e4SLinus Torvalds 		 * try_to_release_page() does not perform I/O, but it is
11001da177e4SLinus Torvalds 		 * possible for a page to have PageDirty set, but it is actually
11011da177e4SLinus Torvalds 		 * clean (all its buffers are clean).  This happens if the
11021da177e4SLinus Torvalds 		 * buffers were written out directly, with submit_bh(). ext3
11031da177e4SLinus Torvalds 		 * will do this, as well as the blockdev mapping.
11041da177e4SLinus Torvalds 		 * try_to_release_page() will discover that cleanness and will
11051da177e4SLinus Torvalds 		 * drop the buffers and mark the page clean - it can be freed.
11061da177e4SLinus Torvalds 		 *
11071da177e4SLinus Torvalds 		 * Rarely, pages can have buffers and no ->mapping.  These are
11081da177e4SLinus Torvalds 		 * the pages which were not successfully invalidated in
11091da177e4SLinus Torvalds 		 * truncate_complete_page().  We try to drop those buffers here
11101da177e4SLinus Torvalds 		 * and if that worked, and the page is no longer mapped into
11111da177e4SLinus Torvalds 		 * process address space (page_count == 1) it can be freed.
11121da177e4SLinus Torvalds 		 * Otherwise, leave the page on the LRU so it is swappable.
11131da177e4SLinus Torvalds 		 */
1114266cf658SDavid Howells 		if (page_has_private(page)) {
11151da177e4SLinus Torvalds 			if (!try_to_release_page(page, sc->gfp_mask))
11161da177e4SLinus Torvalds 				goto activate_locked;
1117e286781dSNick Piggin 			if (!mapping && page_count(page) == 1) {
1118e286781dSNick Piggin 				unlock_page(page);
1119e286781dSNick Piggin 				if (put_page_testzero(page))
11201da177e4SLinus Torvalds 					goto free_it;
1121e286781dSNick Piggin 				else {
1122e286781dSNick Piggin 					/*
1123e286781dSNick Piggin 					 * rare race with speculative reference.
1124e286781dSNick Piggin 					 * the speculative reference will free
1125e286781dSNick Piggin 					 * this page shortly, so we may
1126e286781dSNick Piggin 					 * increment nr_reclaimed here (and
1127e286781dSNick Piggin 					 * leave it off the LRU).
1128e286781dSNick Piggin 					 */
1129e286781dSNick Piggin 					nr_reclaimed++;
1130e286781dSNick Piggin 					continue;
1131e286781dSNick Piggin 				}
1132e286781dSNick Piggin 			}
11331da177e4SLinus Torvalds 		}
11341da177e4SLinus Torvalds 
1135a528910eSJohannes Weiner 		if (!mapping || !__remove_mapping(mapping, page, true))
113649d2e9ccSChristoph Lameter 			goto keep_locked;
11371da177e4SLinus Torvalds 
1138a978d6f5SNick Piggin 		/*
1139a978d6f5SNick Piggin 		 * At this point, we have no other references and there is
1140a978d6f5SNick Piggin 		 * no way to pick any more up (removed from LRU, removed
1141a978d6f5SNick Piggin 		 * from pagecache). Can use non-atomic bitops now (and
1142a978d6f5SNick Piggin 		 * we obviously don't have to worry about waking up a process
1143a978d6f5SNick Piggin 		 * waiting on the page lock, because there are no references.
1144a978d6f5SNick Piggin 		 */
1145a978d6f5SNick Piggin 		__clear_page_locked(page);
1146e286781dSNick Piggin free_it:
114705ff5137SAndrew Morton 		nr_reclaimed++;
1148abe4c3b5SMel Gorman 
1149abe4c3b5SMel Gorman 		/*
1150abe4c3b5SMel Gorman 		 * Is there need to periodically free_page_list? It would
1151abe4c3b5SMel Gorman 		 * appear not as the counts should be low
1152abe4c3b5SMel Gorman 		 */
1153abe4c3b5SMel Gorman 		list_add(&page->lru, &free_pages);
11541da177e4SLinus Torvalds 		continue;
11551da177e4SLinus Torvalds 
1156b291f000SNick Piggin cull_mlocked:
115763d6c5adSHugh Dickins 		if (PageSwapCache(page))
115863d6c5adSHugh Dickins 			try_to_free_swap(page);
1159b291f000SNick Piggin 		unlock_page(page);
1160b291f000SNick Piggin 		putback_lru_page(page);
1161b291f000SNick Piggin 		continue;
1162b291f000SNick Piggin 
11631da177e4SLinus Torvalds activate_locked:
116468a22394SRik van Riel 		/* Not a candidate for swapping, so reclaim swap space. */
116568a22394SRik van Riel 		if (PageSwapCache(page) && vm_swap_full())
1166a2c43eedSHugh Dickins 			try_to_free_swap(page);
1167309381feSSasha Levin 		VM_BUG_ON_PAGE(PageActive(page), page);
11681da177e4SLinus Torvalds 		SetPageActive(page);
11691da177e4SLinus Torvalds 		pgactivate++;
11701da177e4SLinus Torvalds keep_locked:
11711da177e4SLinus Torvalds 		unlock_page(page);
11721da177e4SLinus Torvalds keep:
11731da177e4SLinus Torvalds 		list_add(&page->lru, &ret_pages);
1174309381feSSasha Levin 		VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page);
11751da177e4SLinus Torvalds 	}
1176abe4c3b5SMel Gorman 
1177747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&free_pages);
1178b745bc85SMel Gorman 	free_hot_cold_page_list(&free_pages, true);
1179abe4c3b5SMel Gorman 
11801da177e4SLinus Torvalds 	list_splice(&ret_pages, page_list);
1181f8891e5eSChristoph Lameter 	count_vm_events(PGACTIVATE, pgactivate);
11820a31bc97SJohannes Weiner 
11838e950282SMel Gorman 	*ret_nr_dirty += nr_dirty;
11848e950282SMel Gorman 	*ret_nr_congested += nr_congested;
1185d43006d5SMel Gorman 	*ret_nr_unqueued_dirty += nr_unqueued_dirty;
118692df3a72SMel Gorman 	*ret_nr_writeback += nr_writeback;
1187b1a6f21eSMel Gorman 	*ret_nr_immediate += nr_immediate;
118805ff5137SAndrew Morton 	return nr_reclaimed;
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds 
119102c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone,
119202c6de8dSMinchan Kim 					    struct list_head *page_list)
119302c6de8dSMinchan Kim {
119402c6de8dSMinchan Kim 	struct scan_control sc = {
119502c6de8dSMinchan Kim 		.gfp_mask = GFP_KERNEL,
119602c6de8dSMinchan Kim 		.priority = DEF_PRIORITY,
119702c6de8dSMinchan Kim 		.may_unmap = 1,
119802c6de8dSMinchan Kim 	};
11998e950282SMel Gorman 	unsigned long ret, dummy1, dummy2, dummy3, dummy4, dummy5;
120002c6de8dSMinchan Kim 	struct page *page, *next;
120102c6de8dSMinchan Kim 	LIST_HEAD(clean_pages);
120202c6de8dSMinchan Kim 
120302c6de8dSMinchan Kim 	list_for_each_entry_safe(page, next, page_list, lru) {
1204117aad1eSRafael Aquini 		if (page_is_file_cache(page) && !PageDirty(page) &&
1205117aad1eSRafael Aquini 		    !isolated_balloon_page(page)) {
120602c6de8dSMinchan Kim 			ClearPageActive(page);
120702c6de8dSMinchan Kim 			list_move(&page->lru, &clean_pages);
120802c6de8dSMinchan Kim 		}
120902c6de8dSMinchan Kim 	}
121002c6de8dSMinchan Kim 
121102c6de8dSMinchan Kim 	ret = shrink_page_list(&clean_pages, zone, &sc,
121202c6de8dSMinchan Kim 			TTU_UNMAP|TTU_IGNORE_ACCESS,
12138e950282SMel Gorman 			&dummy1, &dummy2, &dummy3, &dummy4, &dummy5, true);
121402c6de8dSMinchan Kim 	list_splice(&clean_pages, page_list);
121583da7510SChristoph Lameter 	mod_zone_page_state(zone, NR_ISOLATED_FILE, -ret);
121602c6de8dSMinchan Kim 	return ret;
121702c6de8dSMinchan Kim }
121802c6de8dSMinchan Kim 
12195ad333ebSAndy Whitcroft /*
12205ad333ebSAndy Whitcroft  * Attempt to remove the specified page from its LRU.  Only take this page
12215ad333ebSAndy Whitcroft  * if it is of the appropriate PageActive status.  Pages which are being
12225ad333ebSAndy Whitcroft  * freed elsewhere are also ignored.
12235ad333ebSAndy Whitcroft  *
12245ad333ebSAndy Whitcroft  * page:	page to consider
12255ad333ebSAndy Whitcroft  * mode:	one of the LRU isolation modes defined above
12265ad333ebSAndy Whitcroft  *
12275ad333ebSAndy Whitcroft  * returns 0 on success, -ve errno on failure.
12285ad333ebSAndy Whitcroft  */
1229f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode)
12305ad333ebSAndy Whitcroft {
12315ad333ebSAndy Whitcroft 	int ret = -EINVAL;
12325ad333ebSAndy Whitcroft 
12335ad333ebSAndy Whitcroft 	/* Only take pages on the LRU. */
12345ad333ebSAndy Whitcroft 	if (!PageLRU(page))
12355ad333ebSAndy Whitcroft 		return ret;
12365ad333ebSAndy Whitcroft 
1237e46a2879SMinchan Kim 	/* Compaction should not handle unevictable pages but CMA can do so */
1238e46a2879SMinchan Kim 	if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE))
1239894bc310SLee Schermerhorn 		return ret;
1240894bc310SLee Schermerhorn 
12415ad333ebSAndy Whitcroft 	ret = -EBUSY;
124208e552c6SKAMEZAWA Hiroyuki 
1243c8244935SMel Gorman 	/*
1244c8244935SMel Gorman 	 * To minimise LRU disruption, the caller can indicate that it only
1245c8244935SMel Gorman 	 * wants to isolate pages it will be able to operate on without
1246c8244935SMel Gorman 	 * blocking - clean pages for the most part.
1247c8244935SMel Gorman 	 *
1248c8244935SMel Gorman 	 * ISOLATE_CLEAN means that only clean pages should be isolated. This
1249c8244935SMel Gorman 	 * is used by reclaim when it is cannot write to backing storage
1250c8244935SMel Gorman 	 *
1251c8244935SMel Gorman 	 * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages
1252c8244935SMel Gorman 	 * that it is possible to migrate without blocking
1253c8244935SMel Gorman 	 */
1254c8244935SMel Gorman 	if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) {
1255c8244935SMel Gorman 		/* All the caller can do on PageWriteback is block */
1256c8244935SMel Gorman 		if (PageWriteback(page))
125739deaf85SMinchan Kim 			return ret;
125839deaf85SMinchan Kim 
1259c8244935SMel Gorman 		if (PageDirty(page)) {
1260c8244935SMel Gorman 			struct address_space *mapping;
1261c8244935SMel Gorman 
1262c8244935SMel Gorman 			/* ISOLATE_CLEAN means only clean pages */
1263c8244935SMel Gorman 			if (mode & ISOLATE_CLEAN)
1264c8244935SMel Gorman 				return ret;
1265c8244935SMel Gorman 
1266c8244935SMel Gorman 			/*
1267c8244935SMel Gorman 			 * Only pages without mappings or that have a
1268c8244935SMel Gorman 			 * ->migratepage callback are possible to migrate
1269c8244935SMel Gorman 			 * without blocking
1270c8244935SMel Gorman 			 */
1271c8244935SMel Gorman 			mapping = page_mapping(page);
1272c8244935SMel Gorman 			if (mapping && !mapping->a_ops->migratepage)
1273c8244935SMel Gorman 				return ret;
1274c8244935SMel Gorman 		}
1275c8244935SMel Gorman 	}
1276c8244935SMel Gorman 
1277f80c0673SMinchan Kim 	if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
1278f80c0673SMinchan Kim 		return ret;
1279f80c0673SMinchan Kim 
12805ad333ebSAndy Whitcroft 	if (likely(get_page_unless_zero(page))) {
12815ad333ebSAndy Whitcroft 		/*
12825ad333ebSAndy Whitcroft 		 * Be careful not to clear PageLRU until after we're
12835ad333ebSAndy Whitcroft 		 * sure the page is not being freed elsewhere -- the
12845ad333ebSAndy Whitcroft 		 * page release code relies on it.
12855ad333ebSAndy Whitcroft 		 */
12865ad333ebSAndy Whitcroft 		ClearPageLRU(page);
12875ad333ebSAndy Whitcroft 		ret = 0;
12885ad333ebSAndy Whitcroft 	}
12895ad333ebSAndy Whitcroft 
12905ad333ebSAndy Whitcroft 	return ret;
12915ad333ebSAndy Whitcroft }
12925ad333ebSAndy Whitcroft 
129349d2e9ccSChristoph Lameter /*
12941da177e4SLinus Torvalds  * zone->lru_lock is heavily contended.  Some of the functions that
12951da177e4SLinus Torvalds  * shrink the lists perform better by taking out a batch of pages
12961da177e4SLinus Torvalds  * and working on them outside the LRU lock.
12971da177e4SLinus Torvalds  *
12981da177e4SLinus Torvalds  * For pagecache intensive workloads, this function is the hottest
12991da177e4SLinus Torvalds  * spot in the kernel (apart from copy_*_user functions).
13001da177e4SLinus Torvalds  *
13011da177e4SLinus Torvalds  * Appropriate locks must be held before calling this function.
13021da177e4SLinus Torvalds  *
13031da177e4SLinus Torvalds  * @nr_to_scan:	The number of pages to look through on the list.
13045dc35979SKonstantin Khlebnikov  * @lruvec:	The LRU vector to pull pages from.
13051da177e4SLinus Torvalds  * @dst:	The temp list to put pages on to.
1306f626012dSHugh Dickins  * @nr_scanned:	The number of pages that were scanned.
1307fe2c2a10SRik van Riel  * @sc:		The scan_control struct for this reclaim session
13085ad333ebSAndy Whitcroft  * @mode:	One of the LRU isolation modes
13093cb99451SKonstantin Khlebnikov  * @lru:	LRU list id for isolating
13101da177e4SLinus Torvalds  *
13111da177e4SLinus Torvalds  * returns how many pages were moved onto *@dst.
13121da177e4SLinus Torvalds  */
131369e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
13145dc35979SKonstantin Khlebnikov 		struct lruvec *lruvec, struct list_head *dst,
1315fe2c2a10SRik van Riel 		unsigned long *nr_scanned, struct scan_control *sc,
13163cb99451SKonstantin Khlebnikov 		isolate_mode_t mode, enum lru_list lru)
13171da177e4SLinus Torvalds {
131875b00af7SHugh Dickins 	struct list_head *src = &lruvec->lists[lru];
131969e05944SAndrew Morton 	unsigned long nr_taken = 0;
1320c9b02d97SWu Fengguang 	unsigned long scan;
13211da177e4SLinus Torvalds 
1322c9b02d97SWu Fengguang 	for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
13235ad333ebSAndy Whitcroft 		struct page *page;
1324fa9add64SHugh Dickins 		int nr_pages;
13255ad333ebSAndy Whitcroft 
13261da177e4SLinus Torvalds 		page = lru_to_page(src);
13271da177e4SLinus Torvalds 		prefetchw_prev_lru_page(page, src, flags);
13281da177e4SLinus Torvalds 
1329309381feSSasha Levin 		VM_BUG_ON_PAGE(!PageLRU(page), page);
13308d438f96SNick Piggin 
1331f3fd4a61SKonstantin Khlebnikov 		switch (__isolate_lru_page(page, mode)) {
13325ad333ebSAndy Whitcroft 		case 0:
1333fa9add64SHugh Dickins 			nr_pages = hpage_nr_pages(page);
1334fa9add64SHugh Dickins 			mem_cgroup_update_lru_size(lruvec, lru, -nr_pages);
13355ad333ebSAndy Whitcroft 			list_move(&page->lru, dst);
1336fa9add64SHugh Dickins 			nr_taken += nr_pages;
13375ad333ebSAndy Whitcroft 			break;
13387c8ee9a8SNick Piggin 
13395ad333ebSAndy Whitcroft 		case -EBUSY:
13405ad333ebSAndy Whitcroft 			/* else it is being freed elsewhere */
13415ad333ebSAndy Whitcroft 			list_move(&page->lru, src);
13425ad333ebSAndy Whitcroft 			continue;
13435ad333ebSAndy Whitcroft 
13445ad333ebSAndy Whitcroft 		default:
13455ad333ebSAndy Whitcroft 			BUG();
13465ad333ebSAndy Whitcroft 		}
13475ad333ebSAndy Whitcroft 	}
13481da177e4SLinus Torvalds 
1349f626012dSHugh Dickins 	*nr_scanned = scan;
135075b00af7SHugh Dickins 	trace_mm_vmscan_lru_isolate(sc->order, nr_to_scan, scan,
135175b00af7SHugh Dickins 				    nr_taken, mode, is_file_lru(lru));
13521da177e4SLinus Torvalds 	return nr_taken;
13531da177e4SLinus Torvalds }
13541da177e4SLinus Torvalds 
135562695a84SNick Piggin /**
135662695a84SNick Piggin  * isolate_lru_page - tries to isolate a page from its LRU list
135762695a84SNick Piggin  * @page: page to isolate from its LRU list
135862695a84SNick Piggin  *
135962695a84SNick Piggin  * Isolates a @page from an LRU list, clears PageLRU and adjusts the
136062695a84SNick Piggin  * vmstat statistic corresponding to whatever LRU list the page was on.
136162695a84SNick Piggin  *
136262695a84SNick Piggin  * Returns 0 if the page was removed from an LRU list.
136362695a84SNick Piggin  * Returns -EBUSY if the page was not on an LRU list.
136462695a84SNick Piggin  *
136562695a84SNick Piggin  * The returned page will have PageLRU() cleared.  If it was found on
1366894bc310SLee Schermerhorn  * the active list, it will have PageActive set.  If it was found on
1367894bc310SLee Schermerhorn  * the unevictable list, it will have the PageUnevictable bit set. That flag
1368894bc310SLee Schermerhorn  * may need to be cleared by the caller before letting the page go.
136962695a84SNick Piggin  *
137062695a84SNick Piggin  * The vmstat statistic corresponding to the list on which the page was
137162695a84SNick Piggin  * found will be decremented.
137262695a84SNick Piggin  *
137362695a84SNick Piggin  * Restrictions:
137462695a84SNick Piggin  * (1) Must be called with an elevated refcount on the page. This is a
137562695a84SNick Piggin  *     fundamentnal difference from isolate_lru_pages (which is called
137662695a84SNick Piggin  *     without a stable reference).
137762695a84SNick Piggin  * (2) the lru_lock must not be held.
137862695a84SNick Piggin  * (3) interrupts must be enabled.
137962695a84SNick Piggin  */
138062695a84SNick Piggin int isolate_lru_page(struct page *page)
138162695a84SNick Piggin {
138262695a84SNick Piggin 	int ret = -EBUSY;
138362695a84SNick Piggin 
1384309381feSSasha Levin 	VM_BUG_ON_PAGE(!page_count(page), page);
13850c917313SKonstantin Khlebnikov 
138662695a84SNick Piggin 	if (PageLRU(page)) {
138762695a84SNick Piggin 		struct zone *zone = page_zone(page);
1388fa9add64SHugh Dickins 		struct lruvec *lruvec;
138962695a84SNick Piggin 
139062695a84SNick Piggin 		spin_lock_irq(&zone->lru_lock);
1391fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
13920c917313SKonstantin Khlebnikov 		if (PageLRU(page)) {
1393894bc310SLee Schermerhorn 			int lru = page_lru(page);
13940c917313SKonstantin Khlebnikov 			get_page(page);
139562695a84SNick Piggin 			ClearPageLRU(page);
1396fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
1397fa9add64SHugh Dickins 			ret = 0;
139862695a84SNick Piggin 		}
139962695a84SNick Piggin 		spin_unlock_irq(&zone->lru_lock);
140062695a84SNick Piggin 	}
140162695a84SNick Piggin 	return ret;
140262695a84SNick Piggin }
140362695a84SNick Piggin 
14045ad333ebSAndy Whitcroft /*
1405d37dd5dcSFengguang Wu  * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
1406d37dd5dcSFengguang Wu  * then get resheduled. When there are massive number of tasks doing page
1407d37dd5dcSFengguang Wu  * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
1408d37dd5dcSFengguang Wu  * the LRU list will go small and be scanned faster than necessary, leading to
1409d37dd5dcSFengguang Wu  * unnecessary swapping, thrashing and OOM.
141035cd7815SRik van Riel  */
141135cd7815SRik van Riel static int too_many_isolated(struct zone *zone, int file,
141235cd7815SRik van Riel 		struct scan_control *sc)
141335cd7815SRik van Riel {
141435cd7815SRik van Riel 	unsigned long inactive, isolated;
141535cd7815SRik van Riel 
141635cd7815SRik van Riel 	if (current_is_kswapd())
141735cd7815SRik van Riel 		return 0;
141835cd7815SRik van Riel 
141989b5fae5SJohannes Weiner 	if (!global_reclaim(sc))
142035cd7815SRik van Riel 		return 0;
142135cd7815SRik van Riel 
142235cd7815SRik van Riel 	if (file) {
142335cd7815SRik van Riel 		inactive = zone_page_state(zone, NR_INACTIVE_FILE);
142435cd7815SRik van Riel 		isolated = zone_page_state(zone, NR_ISOLATED_FILE);
142535cd7815SRik van Riel 	} else {
142635cd7815SRik van Riel 		inactive = zone_page_state(zone, NR_INACTIVE_ANON);
142735cd7815SRik van Riel 		isolated = zone_page_state(zone, NR_ISOLATED_ANON);
142835cd7815SRik van Riel 	}
142935cd7815SRik van Riel 
14303cf23841SFengguang Wu 	/*
14313cf23841SFengguang Wu 	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
14323cf23841SFengguang Wu 	 * won't get blocked by normal direct-reclaimers, forming a circular
14333cf23841SFengguang Wu 	 * deadlock.
14343cf23841SFengguang Wu 	 */
14353cf23841SFengguang Wu 	if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS)
14363cf23841SFengguang Wu 		inactive >>= 3;
14373cf23841SFengguang Wu 
143835cd7815SRik van Riel 	return isolated > inactive;
143935cd7815SRik van Riel }
144035cd7815SRik van Riel 
144166635629SMel Gorman static noinline_for_stack void
144275b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list)
144366635629SMel Gorman {
144427ac81d8SKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
144527ac81d8SKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
14463f79768fSHugh Dickins 	LIST_HEAD(pages_to_free);
144766635629SMel Gorman 
144866635629SMel Gorman 	/*
144966635629SMel Gorman 	 * Put back any unfreeable pages.
145066635629SMel Gorman 	 */
145166635629SMel Gorman 	while (!list_empty(page_list)) {
14523f79768fSHugh Dickins 		struct page *page = lru_to_page(page_list);
145366635629SMel Gorman 		int lru;
14543f79768fSHugh Dickins 
1455309381feSSasha Levin 		VM_BUG_ON_PAGE(PageLRU(page), page);
145666635629SMel Gorman 		list_del(&page->lru);
145739b5f29aSHugh Dickins 		if (unlikely(!page_evictable(page))) {
145866635629SMel Gorman 			spin_unlock_irq(&zone->lru_lock);
145966635629SMel Gorman 			putback_lru_page(page);
146066635629SMel Gorman 			spin_lock_irq(&zone->lru_lock);
146166635629SMel Gorman 			continue;
146266635629SMel Gorman 		}
1463fa9add64SHugh Dickins 
1464fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
1465fa9add64SHugh Dickins 
14667a608572SLinus Torvalds 		SetPageLRU(page);
146766635629SMel Gorman 		lru = page_lru(page);
1468fa9add64SHugh Dickins 		add_page_to_lru_list(page, lruvec, lru);
1469fa9add64SHugh Dickins 
147066635629SMel Gorman 		if (is_active_lru(lru)) {
147166635629SMel Gorman 			int file = is_file_lru(lru);
14729992af10SRik van Riel 			int numpages = hpage_nr_pages(page);
14739992af10SRik van Riel 			reclaim_stat->recent_rotated[file] += numpages;
147466635629SMel Gorman 		}
14752bcf8879SHugh Dickins 		if (put_page_testzero(page)) {
14762bcf8879SHugh Dickins 			__ClearPageLRU(page);
14772bcf8879SHugh Dickins 			__ClearPageActive(page);
1478fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
14792bcf8879SHugh Dickins 
14802bcf8879SHugh Dickins 			if (unlikely(PageCompound(page))) {
148166635629SMel Gorman 				spin_unlock_irq(&zone->lru_lock);
1482747db954SJohannes Weiner 				mem_cgroup_uncharge(page);
14832bcf8879SHugh Dickins 				(*get_compound_page_dtor(page))(page);
148466635629SMel Gorman 				spin_lock_irq(&zone->lru_lock);
14852bcf8879SHugh Dickins 			} else
14862bcf8879SHugh Dickins 				list_add(&page->lru, &pages_to_free);
148766635629SMel Gorman 		}
148866635629SMel Gorman 	}
148966635629SMel Gorman 
14903f79768fSHugh Dickins 	/*
14913f79768fSHugh Dickins 	 * To save our caller's stack, now use input list for pages to free.
14923f79768fSHugh Dickins 	 */
14933f79768fSHugh Dickins 	list_splice(&pages_to_free, page_list);
149466635629SMel Gorman }
149566635629SMel Gorman 
149666635629SMel Gorman /*
1497399ba0b9SNeilBrown  * If a kernel thread (such as nfsd for loop-back mounts) services
1498399ba0b9SNeilBrown  * a backing device by writing to the page cache it sets PF_LESS_THROTTLE.
1499399ba0b9SNeilBrown  * In that case we should only throttle if the backing device it is
1500399ba0b9SNeilBrown  * writing to is congested.  In other cases it is safe to throttle.
1501399ba0b9SNeilBrown  */
1502399ba0b9SNeilBrown static int current_may_throttle(void)
1503399ba0b9SNeilBrown {
1504399ba0b9SNeilBrown 	return !(current->flags & PF_LESS_THROTTLE) ||
1505399ba0b9SNeilBrown 		current->backing_dev_info == NULL ||
1506399ba0b9SNeilBrown 		bdi_write_congested(current->backing_dev_info);
1507399ba0b9SNeilBrown }
1508399ba0b9SNeilBrown 
1509399ba0b9SNeilBrown /*
15101742f19fSAndrew Morton  * shrink_inactive_list() is a helper for shrink_zone().  It returns the number
15111742f19fSAndrew Morton  * of reclaimed pages
15121da177e4SLinus Torvalds  */
151366635629SMel Gorman static noinline_for_stack unsigned long
15141a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
15159e3b2f8cSKonstantin Khlebnikov 		     struct scan_control *sc, enum lru_list lru)
15161da177e4SLinus Torvalds {
15171da177e4SLinus Torvalds 	LIST_HEAD(page_list);
1518e247dbceSKOSAKI Motohiro 	unsigned long nr_scanned;
151905ff5137SAndrew Morton 	unsigned long nr_reclaimed = 0;
1520e247dbceSKOSAKI Motohiro 	unsigned long nr_taken;
15218e950282SMel Gorman 	unsigned long nr_dirty = 0;
15228e950282SMel Gorman 	unsigned long nr_congested = 0;
1523e2be15f6SMel Gorman 	unsigned long nr_unqueued_dirty = 0;
152492df3a72SMel Gorman 	unsigned long nr_writeback = 0;
1525b1a6f21eSMel Gorman 	unsigned long nr_immediate = 0;
1526f3fd4a61SKonstantin Khlebnikov 	isolate_mode_t isolate_mode = 0;
15273cb99451SKonstantin Khlebnikov 	int file = is_file_lru(lru);
15281a93be0eSKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
15291a93be0eSKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
153078dc583dSKOSAKI Motohiro 
153135cd7815SRik van Riel 	while (unlikely(too_many_isolated(zone, file, sc))) {
153258355c78SKOSAKI Motohiro 		congestion_wait(BLK_RW_ASYNC, HZ/10);
153335cd7815SRik van Riel 
153435cd7815SRik van Riel 		/* We are about to die and free our memory. Return now. */
153535cd7815SRik van Riel 		if (fatal_signal_pending(current))
153635cd7815SRik van Riel 			return SWAP_CLUSTER_MAX;
153735cd7815SRik van Riel 	}
153835cd7815SRik van Riel 
15391da177e4SLinus Torvalds 	lru_add_drain();
1540f80c0673SMinchan Kim 
1541f80c0673SMinchan Kim 	if (!sc->may_unmap)
154261317289SHillf Danton 		isolate_mode |= ISOLATE_UNMAPPED;
1543f80c0673SMinchan Kim 	if (!sc->may_writepage)
154461317289SHillf Danton 		isolate_mode |= ISOLATE_CLEAN;
1545f80c0673SMinchan Kim 
15461da177e4SLinus Torvalds 	spin_lock_irq(&zone->lru_lock);
15471da177e4SLinus Torvalds 
15485dc35979SKonstantin Khlebnikov 	nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
15495dc35979SKonstantin Khlebnikov 				     &nr_scanned, sc, isolate_mode, lru);
155095d918fcSKonstantin Khlebnikov 
155195d918fcSKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken);
155295d918fcSKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
155395d918fcSKonstantin Khlebnikov 
155489b5fae5SJohannes Weiner 	if (global_reclaim(sc)) {
15550d5d823aSMel Gorman 		__mod_zone_page_state(zone, NR_PAGES_SCANNED, nr_scanned);
1556b35ea17bSKOSAKI Motohiro 		if (current_is_kswapd())
155775b00af7SHugh Dickins 			__count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scanned);
1558b35ea17bSKOSAKI Motohiro 		else
155975b00af7SHugh Dickins 			__count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scanned);
1560b35ea17bSKOSAKI Motohiro 	}
156166635629SMel Gorman 	spin_unlock_irq(&zone->lru_lock);
1562d563c050SHillf Danton 
1563d563c050SHillf Danton 	if (nr_taken == 0)
156466635629SMel Gorman 		return 0;
1565b35ea17bSKOSAKI Motohiro 
156602c6de8dSMinchan Kim 	nr_reclaimed = shrink_page_list(&page_list, zone, sc, TTU_UNMAP,
15678e950282SMel Gorman 				&nr_dirty, &nr_unqueued_dirty, &nr_congested,
15688e950282SMel Gorman 				&nr_writeback, &nr_immediate,
1569b1a6f21eSMel Gorman 				false);
1570c661b078SAndy Whitcroft 
15713f79768fSHugh Dickins 	spin_lock_irq(&zone->lru_lock);
15723f79768fSHugh Dickins 
157395d918fcSKonstantin Khlebnikov 	reclaim_stat->recent_scanned[file] += nr_taken;
1574d563c050SHillf Danton 
1575904249aaSYing Han 	if (global_reclaim(sc)) {
1576b35ea17bSKOSAKI Motohiro 		if (current_is_kswapd())
1577904249aaSYing Han 			__count_zone_vm_events(PGSTEAL_KSWAPD, zone,
1578904249aaSYing Han 					       nr_reclaimed);
1579904249aaSYing Han 		else
1580904249aaSYing Han 			__count_zone_vm_events(PGSTEAL_DIRECT, zone,
1581904249aaSYing Han 					       nr_reclaimed);
1582904249aaSYing Han 	}
1583a74609faSNick Piggin 
158427ac81d8SKonstantin Khlebnikov 	putback_inactive_pages(lruvec, &page_list);
15853f79768fSHugh Dickins 
158695d918fcSKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
15873f79768fSHugh Dickins 
15883f79768fSHugh Dickins 	spin_unlock_irq(&zone->lru_lock);
15893f79768fSHugh Dickins 
1590747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&page_list);
1591b745bc85SMel Gorman 	free_hot_cold_page_list(&page_list, true);
1592e11da5b4SMel Gorman 
159392df3a72SMel Gorman 	/*
159492df3a72SMel Gorman 	 * If reclaim is isolating dirty pages under writeback, it implies
159592df3a72SMel Gorman 	 * that the long-lived page allocation rate is exceeding the page
159692df3a72SMel Gorman 	 * laundering rate. Either the global limits are not being effective
159792df3a72SMel Gorman 	 * at throttling processes due to the page distribution throughout
159892df3a72SMel Gorman 	 * zones or there is heavy usage of a slow backing device. The
159992df3a72SMel Gorman 	 * only option is to throttle from reclaim context which is not ideal
160092df3a72SMel Gorman 	 * as there is no guarantee the dirtying process is throttled in the
160192df3a72SMel Gorman 	 * same way balance_dirty_pages() manages.
160292df3a72SMel Gorman 	 *
16038e950282SMel Gorman 	 * Once a zone is flagged ZONE_WRITEBACK, kswapd will count the number
16048e950282SMel Gorman 	 * of pages under pages flagged for immediate reclaim and stall if any
16058e950282SMel Gorman 	 * are encountered in the nr_immediate check below.
160692df3a72SMel Gorman 	 */
1607918fc718SMel Gorman 	if (nr_writeback && nr_writeback == nr_taken)
160857054651SJohannes Weiner 		set_bit(ZONE_WRITEBACK, &zone->flags);
160992df3a72SMel Gorman 
1610d43006d5SMel Gorman 	/*
1611b1a6f21eSMel Gorman 	 * memcg will stall in page writeback so only consider forcibly
1612b1a6f21eSMel Gorman 	 * stalling for global reclaim
1613d43006d5SMel Gorman 	 */
1614b1a6f21eSMel Gorman 	if (global_reclaim(sc)) {
1615b1a6f21eSMel Gorman 		/*
16168e950282SMel Gorman 		 * Tag a zone as congested if all the dirty pages scanned were
16178e950282SMel Gorman 		 * backed by a congested BDI and wait_iff_congested will stall.
16188e950282SMel Gorman 		 */
16198e950282SMel Gorman 		if (nr_dirty && nr_dirty == nr_congested)
162057054651SJohannes Weiner 			set_bit(ZONE_CONGESTED, &zone->flags);
16218e950282SMel Gorman 
16228e950282SMel Gorman 		/*
1623b1a6f21eSMel Gorman 		 * If dirty pages are scanned that are not queued for IO, it
1624b1a6f21eSMel Gorman 		 * implies that flushers are not keeping up. In this case, flag
162557054651SJohannes Weiner 		 * the zone ZONE_DIRTY and kswapd will start writing pages from
162657054651SJohannes Weiner 		 * reclaim context.
1627b1a6f21eSMel Gorman 		 */
1628b1a6f21eSMel Gorman 		if (nr_unqueued_dirty == nr_taken)
162957054651SJohannes Weiner 			set_bit(ZONE_DIRTY, &zone->flags);
1630b1a6f21eSMel Gorman 
1631b1a6f21eSMel Gorman 		/*
1632b738d764SLinus Torvalds 		 * If kswapd scans pages marked marked for immediate
1633b738d764SLinus Torvalds 		 * reclaim and under writeback (nr_immediate), it implies
1634b738d764SLinus Torvalds 		 * that pages are cycling through the LRU faster than
1635b1a6f21eSMel Gorman 		 * they are written so also forcibly stall.
1636b1a6f21eSMel Gorman 		 */
1637b738d764SLinus Torvalds 		if (nr_immediate && current_may_throttle())
1638b1a6f21eSMel Gorman 			congestion_wait(BLK_RW_ASYNC, HZ/10);
1639e2be15f6SMel Gorman 	}
1640d43006d5SMel Gorman 
16418e950282SMel Gorman 	/*
16428e950282SMel Gorman 	 * Stall direct reclaim for IO completions if underlying BDIs or zone
16438e950282SMel Gorman 	 * is congested. Allow kswapd to continue until it starts encountering
16448e950282SMel Gorman 	 * unqueued dirty pages or cycling through the LRU too quickly.
16458e950282SMel Gorman 	 */
1646399ba0b9SNeilBrown 	if (!sc->hibernation_mode && !current_is_kswapd() &&
1647399ba0b9SNeilBrown 	    current_may_throttle())
16488e950282SMel Gorman 		wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
16498e950282SMel Gorman 
1650e11da5b4SMel Gorman 	trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id,
1651e11da5b4SMel Gorman 		zone_idx(zone),
1652e11da5b4SMel Gorman 		nr_scanned, nr_reclaimed,
16539e3b2f8cSKonstantin Khlebnikov 		sc->priority,
165423b9da55SMel Gorman 		trace_shrink_flags(file));
165505ff5137SAndrew Morton 	return nr_reclaimed;
16561da177e4SLinus Torvalds }
16571da177e4SLinus Torvalds 
16583bb1a852SMartin Bligh /*
16591cfb419bSKAMEZAWA Hiroyuki  * This moves pages from the active list to the inactive list.
16601cfb419bSKAMEZAWA Hiroyuki  *
16611cfb419bSKAMEZAWA Hiroyuki  * We move them the other way if the page is referenced by one or more
16621cfb419bSKAMEZAWA Hiroyuki  * processes, from rmap.
16631cfb419bSKAMEZAWA Hiroyuki  *
16641cfb419bSKAMEZAWA Hiroyuki  * If the pages are mostly unmapped, the processing is fast and it is
16651cfb419bSKAMEZAWA Hiroyuki  * appropriate to hold zone->lru_lock across the whole operation.  But if
16661cfb419bSKAMEZAWA Hiroyuki  * the pages are mapped, the processing is slow (page_referenced()) so we
16671cfb419bSKAMEZAWA Hiroyuki  * should drop zone->lru_lock around each page.  It's impossible to balance
16681cfb419bSKAMEZAWA Hiroyuki  * this, so instead we remove the pages from the LRU while processing them.
16691cfb419bSKAMEZAWA Hiroyuki  * It is safe to rely on PG_active against the non-LRU pages in here because
16701cfb419bSKAMEZAWA Hiroyuki  * nobody will play with that bit on a non-LRU page.
16711cfb419bSKAMEZAWA Hiroyuki  *
16721cfb419bSKAMEZAWA Hiroyuki  * The downside is that we have to touch page->_count against each page.
16731cfb419bSKAMEZAWA Hiroyuki  * But we had to alter page->flags anyway.
16741cfb419bSKAMEZAWA Hiroyuki  */
16751cfb419bSKAMEZAWA Hiroyuki 
1676fa9add64SHugh Dickins static void move_active_pages_to_lru(struct lruvec *lruvec,
16773eb4140fSWu Fengguang 				     struct list_head *list,
16782bcf8879SHugh Dickins 				     struct list_head *pages_to_free,
16793eb4140fSWu Fengguang 				     enum lru_list lru)
16803eb4140fSWu Fengguang {
1681fa9add64SHugh Dickins 	struct zone *zone = lruvec_zone(lruvec);
16823eb4140fSWu Fengguang 	unsigned long pgmoved = 0;
16833eb4140fSWu Fengguang 	struct page *page;
1684fa9add64SHugh Dickins 	int nr_pages;
16853eb4140fSWu Fengguang 
16863eb4140fSWu Fengguang 	while (!list_empty(list)) {
16873eb4140fSWu Fengguang 		page = lru_to_page(list);
1688fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
16893eb4140fSWu Fengguang 
1690309381feSSasha Levin 		VM_BUG_ON_PAGE(PageLRU(page), page);
16913eb4140fSWu Fengguang 		SetPageLRU(page);
16923eb4140fSWu Fengguang 
1693fa9add64SHugh Dickins 		nr_pages = hpage_nr_pages(page);
1694fa9add64SHugh Dickins 		mem_cgroup_update_lru_size(lruvec, lru, nr_pages);
1695925b7673SJohannes Weiner 		list_move(&page->lru, &lruvec->lists[lru]);
1696fa9add64SHugh Dickins 		pgmoved += nr_pages;
16973eb4140fSWu Fengguang 
16982bcf8879SHugh Dickins 		if (put_page_testzero(page)) {
16992bcf8879SHugh Dickins 			__ClearPageLRU(page);
17002bcf8879SHugh Dickins 			__ClearPageActive(page);
1701fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, lru);
17022bcf8879SHugh Dickins 
17032bcf8879SHugh Dickins 			if (unlikely(PageCompound(page))) {
17043eb4140fSWu Fengguang 				spin_unlock_irq(&zone->lru_lock);
1705747db954SJohannes Weiner 				mem_cgroup_uncharge(page);
17062bcf8879SHugh Dickins 				(*get_compound_page_dtor(page))(page);
17073eb4140fSWu Fengguang 				spin_lock_irq(&zone->lru_lock);
17082bcf8879SHugh Dickins 			} else
17092bcf8879SHugh Dickins 				list_add(&page->lru, pages_to_free);
17103eb4140fSWu Fengguang 		}
17113eb4140fSWu Fengguang 	}
17123eb4140fSWu Fengguang 	__mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
17133eb4140fSWu Fengguang 	if (!is_active_lru(lru))
17143eb4140fSWu Fengguang 		__count_vm_events(PGDEACTIVATE, pgmoved);
17153eb4140fSWu Fengguang }
17161cfb419bSKAMEZAWA Hiroyuki 
1717f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan,
17181a93be0eSKonstantin Khlebnikov 			       struct lruvec *lruvec,
1719f16015fbSJohannes Weiner 			       struct scan_control *sc,
17209e3b2f8cSKonstantin Khlebnikov 			       enum lru_list lru)
17211cfb419bSKAMEZAWA Hiroyuki {
172244c241f1SKOSAKI Motohiro 	unsigned long nr_taken;
1723f626012dSHugh Dickins 	unsigned long nr_scanned;
17246fe6b7e3SWu Fengguang 	unsigned long vm_flags;
17251cfb419bSKAMEZAWA Hiroyuki 	LIST_HEAD(l_hold);	/* The pages which were snipped off */
17268cab4754SWu Fengguang 	LIST_HEAD(l_active);
1727b69408e8SChristoph Lameter 	LIST_HEAD(l_inactive);
17281cfb419bSKAMEZAWA Hiroyuki 	struct page *page;
17291a93be0eSKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
173044c241f1SKOSAKI Motohiro 	unsigned long nr_rotated = 0;
1731f3fd4a61SKonstantin Khlebnikov 	isolate_mode_t isolate_mode = 0;
17323cb99451SKonstantin Khlebnikov 	int file = is_file_lru(lru);
17331a93be0eSKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
17341cfb419bSKAMEZAWA Hiroyuki 
17351da177e4SLinus Torvalds 	lru_add_drain();
1736f80c0673SMinchan Kim 
1737f80c0673SMinchan Kim 	if (!sc->may_unmap)
173861317289SHillf Danton 		isolate_mode |= ISOLATE_UNMAPPED;
1739f80c0673SMinchan Kim 	if (!sc->may_writepage)
174061317289SHillf Danton 		isolate_mode |= ISOLATE_CLEAN;
1741f80c0673SMinchan Kim 
17421da177e4SLinus Torvalds 	spin_lock_irq(&zone->lru_lock);
1743925b7673SJohannes Weiner 
17445dc35979SKonstantin Khlebnikov 	nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
17455dc35979SKonstantin Khlebnikov 				     &nr_scanned, sc, isolate_mode, lru);
174689b5fae5SJohannes Weiner 	if (global_reclaim(sc))
17470d5d823aSMel Gorman 		__mod_zone_page_state(zone, NR_PAGES_SCANNED, nr_scanned);
174889b5fae5SJohannes Weiner 
1749b7c46d15SJohannes Weiner 	reclaim_stat->recent_scanned[file] += nr_taken;
17501cfb419bSKAMEZAWA Hiroyuki 
1751f626012dSHugh Dickins 	__count_zone_vm_events(PGREFILL, zone, nr_scanned);
17523cb99451SKonstantin Khlebnikov 	__mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken);
1753a731286dSKOSAKI Motohiro 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
17541da177e4SLinus Torvalds 	spin_unlock_irq(&zone->lru_lock);
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	while (!list_empty(&l_hold)) {
17571da177e4SLinus Torvalds 		cond_resched();
17581da177e4SLinus Torvalds 		page = lru_to_page(&l_hold);
17591da177e4SLinus Torvalds 		list_del(&page->lru);
17607e9cd484SRik van Riel 
176139b5f29aSHugh Dickins 		if (unlikely(!page_evictable(page))) {
1762894bc310SLee Schermerhorn 			putback_lru_page(page);
1763894bc310SLee Schermerhorn 			continue;
1764894bc310SLee Schermerhorn 		}
1765894bc310SLee Schermerhorn 
1766cc715d99SMel Gorman 		if (unlikely(buffer_heads_over_limit)) {
1767cc715d99SMel Gorman 			if (page_has_private(page) && trylock_page(page)) {
1768cc715d99SMel Gorman 				if (page_has_private(page))
1769cc715d99SMel Gorman 					try_to_release_page(page, 0);
1770cc715d99SMel Gorman 				unlock_page(page);
1771cc715d99SMel Gorman 			}
1772cc715d99SMel Gorman 		}
1773cc715d99SMel Gorman 
1774c3ac9a8aSJohannes Weiner 		if (page_referenced(page, 0, sc->target_mem_cgroup,
1775c3ac9a8aSJohannes Weiner 				    &vm_flags)) {
17769992af10SRik van Riel 			nr_rotated += hpage_nr_pages(page);
17778cab4754SWu Fengguang 			/*
17788cab4754SWu Fengguang 			 * Identify referenced, file-backed active pages and
17798cab4754SWu Fengguang 			 * give them one more trip around the active list. So
17808cab4754SWu Fengguang 			 * that executable code get better chances to stay in
17818cab4754SWu Fengguang 			 * memory under moderate memory pressure.  Anon pages
17828cab4754SWu Fengguang 			 * are not likely to be evicted by use-once streaming
17838cab4754SWu Fengguang 			 * IO, plus JVM can create lots of anon VM_EXEC pages,
17848cab4754SWu Fengguang 			 * so we ignore them here.
17858cab4754SWu Fengguang 			 */
178641e20983SWu Fengguang 			if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
17878cab4754SWu Fengguang 				list_add(&page->lru, &l_active);
17888cab4754SWu Fengguang 				continue;
17898cab4754SWu Fengguang 			}
17908cab4754SWu Fengguang 		}
17917e9cd484SRik van Riel 
17925205e56eSKOSAKI Motohiro 		ClearPageActive(page);	/* we are de-activating */
17931da177e4SLinus Torvalds 		list_add(&page->lru, &l_inactive);
17941da177e4SLinus Torvalds 	}
17951da177e4SLinus Torvalds 
1796b555749aSAndrew Morton 	/*
17978cab4754SWu Fengguang 	 * Move pages back to the lru list.
1798b555749aSAndrew Morton 	 */
17992a1dc509SJohannes Weiner 	spin_lock_irq(&zone->lru_lock);
18004f98a2feSRik van Riel 	/*
18018cab4754SWu Fengguang 	 * Count referenced pages from currently used mappings as rotated,
18028cab4754SWu Fengguang 	 * even though only some of them are actually re-activated.  This
18038cab4754SWu Fengguang 	 * helps balance scan pressure between file and anonymous pages in
18047c0db9e9SJerome Marchand 	 * get_scan_count.
1805556adecbSRik van Riel 	 */
1806b7c46d15SJohannes Weiner 	reclaim_stat->recent_rotated[file] += nr_rotated;
1807556adecbSRik van Riel 
1808fa9add64SHugh Dickins 	move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru);
1809fa9add64SHugh Dickins 	move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE);
1810a731286dSKOSAKI Motohiro 	__mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
1811f8891e5eSChristoph Lameter 	spin_unlock_irq(&zone->lru_lock);
18122bcf8879SHugh Dickins 
1813747db954SJohannes Weiner 	mem_cgroup_uncharge_list(&l_hold);
1814b745bc85SMel Gorman 	free_hot_cold_page_list(&l_hold, true);
18151da177e4SLinus Torvalds }
18161da177e4SLinus Torvalds 
181774e3f3c3SMinchan Kim #ifdef CONFIG_SWAP
181814797e23SKOSAKI Motohiro static int inactive_anon_is_low_global(struct zone *zone)
1819f89eb90eSKOSAKI Motohiro {
1820f89eb90eSKOSAKI Motohiro 	unsigned long active, inactive;
1821f89eb90eSKOSAKI Motohiro 
1822f89eb90eSKOSAKI Motohiro 	active = zone_page_state(zone, NR_ACTIVE_ANON);
1823f89eb90eSKOSAKI Motohiro 	inactive = zone_page_state(zone, NR_INACTIVE_ANON);
1824f89eb90eSKOSAKI Motohiro 
1825f89eb90eSKOSAKI Motohiro 	if (inactive * zone->inactive_ratio < active)
1826f89eb90eSKOSAKI Motohiro 		return 1;
1827f89eb90eSKOSAKI Motohiro 
1828f89eb90eSKOSAKI Motohiro 	return 0;
1829f89eb90eSKOSAKI Motohiro }
1830f89eb90eSKOSAKI Motohiro 
183114797e23SKOSAKI Motohiro /**
183214797e23SKOSAKI Motohiro  * inactive_anon_is_low - check if anonymous pages need to be deactivated
1833c56d5c7dSKonstantin Khlebnikov  * @lruvec: LRU vector to check
183414797e23SKOSAKI Motohiro  *
183514797e23SKOSAKI Motohiro  * Returns true if the zone does not have enough inactive anon pages,
183614797e23SKOSAKI Motohiro  * meaning some active anon pages need to be deactivated.
183714797e23SKOSAKI Motohiro  */
1838c56d5c7dSKonstantin Khlebnikov static int inactive_anon_is_low(struct lruvec *lruvec)
183914797e23SKOSAKI Motohiro {
184074e3f3c3SMinchan Kim 	/*
184174e3f3c3SMinchan Kim 	 * If we don't have swap space, anonymous page deactivation
184274e3f3c3SMinchan Kim 	 * is pointless.
184374e3f3c3SMinchan Kim 	 */
184474e3f3c3SMinchan Kim 	if (!total_swap_pages)
184574e3f3c3SMinchan Kim 		return 0;
184674e3f3c3SMinchan Kim 
1847c3c787e8SHugh Dickins 	if (!mem_cgroup_disabled())
1848c56d5c7dSKonstantin Khlebnikov 		return mem_cgroup_inactive_anon_is_low(lruvec);
1849f16015fbSJohannes Weiner 
1850c56d5c7dSKonstantin Khlebnikov 	return inactive_anon_is_low_global(lruvec_zone(lruvec));
185114797e23SKOSAKI Motohiro }
185274e3f3c3SMinchan Kim #else
1853c56d5c7dSKonstantin Khlebnikov static inline int inactive_anon_is_low(struct lruvec *lruvec)
185474e3f3c3SMinchan Kim {
185574e3f3c3SMinchan Kim 	return 0;
185674e3f3c3SMinchan Kim }
185774e3f3c3SMinchan Kim #endif
185814797e23SKOSAKI Motohiro 
185956e49d21SRik van Riel /**
186056e49d21SRik van Riel  * inactive_file_is_low - check if file pages need to be deactivated
1861c56d5c7dSKonstantin Khlebnikov  * @lruvec: LRU vector to check
186256e49d21SRik van Riel  *
186356e49d21SRik van Riel  * When the system is doing streaming IO, memory pressure here
186456e49d21SRik van Riel  * ensures that active file pages get deactivated, until more
186556e49d21SRik van Riel  * than half of the file pages are on the inactive list.
186656e49d21SRik van Riel  *
186756e49d21SRik van Riel  * Once we get to that situation, protect the system's working
186856e49d21SRik van Riel  * set from being evicted by disabling active file page aging.
186956e49d21SRik van Riel  *
187056e49d21SRik van Riel  * This uses a different ratio than the anonymous pages, because
187156e49d21SRik van Riel  * the page cache uses a use-once replacement algorithm.
187256e49d21SRik van Riel  */
1873c56d5c7dSKonstantin Khlebnikov static int inactive_file_is_low(struct lruvec *lruvec)
187456e49d21SRik van Riel {
1875e3790144SJohannes Weiner 	unsigned long inactive;
1876e3790144SJohannes Weiner 	unsigned long active;
187756e49d21SRik van Riel 
1878e3790144SJohannes Weiner 	inactive = get_lru_size(lruvec, LRU_INACTIVE_FILE);
1879e3790144SJohannes Weiner 	active = get_lru_size(lruvec, LRU_ACTIVE_FILE);
1880e3790144SJohannes Weiner 
1881e3790144SJohannes Weiner 	return active > inactive;
188256e49d21SRik van Riel }
188356e49d21SRik van Riel 
188475b00af7SHugh Dickins static int inactive_list_is_low(struct lruvec *lruvec, enum lru_list lru)
1885b39415b2SRik van Riel {
188675b00af7SHugh Dickins 	if (is_file_lru(lru))
1887c56d5c7dSKonstantin Khlebnikov 		return inactive_file_is_low(lruvec);
1888b39415b2SRik van Riel 	else
1889c56d5c7dSKonstantin Khlebnikov 		return inactive_anon_is_low(lruvec);
1890b39415b2SRik van Riel }
1891b39415b2SRik van Riel 
18924f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
18931a93be0eSKonstantin Khlebnikov 				 struct lruvec *lruvec, struct scan_control *sc)
1894b69408e8SChristoph Lameter {
1895b39415b2SRik van Riel 	if (is_active_lru(lru)) {
189675b00af7SHugh Dickins 		if (inactive_list_is_low(lruvec, lru))
18971a93be0eSKonstantin Khlebnikov 			shrink_active_list(nr_to_scan, lruvec, sc, lru);
1898556adecbSRik van Riel 		return 0;
1899556adecbSRik van Riel 	}
1900556adecbSRik van Riel 
19011a93be0eSKonstantin Khlebnikov 	return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
1902b69408e8SChristoph Lameter }
1903b69408e8SChristoph Lameter 
19049a265114SJohannes Weiner enum scan_balance {
19059a265114SJohannes Weiner 	SCAN_EQUAL,
19069a265114SJohannes Weiner 	SCAN_FRACT,
19079a265114SJohannes Weiner 	SCAN_ANON,
19089a265114SJohannes Weiner 	SCAN_FILE,
19099a265114SJohannes Weiner };
19109a265114SJohannes Weiner 
19111da177e4SLinus Torvalds /*
19124f98a2feSRik van Riel  * Determine how aggressively the anon and file LRU lists should be
19134f98a2feSRik van Riel  * scanned.  The relative value of each set of LRU lists is determined
19144f98a2feSRik van Riel  * by looking at the fraction of the pages scanned we did rotate back
19154f98a2feSRik van Riel  * onto the active list instead of evict.
19164f98a2feSRik van Riel  *
1917be7bd59dSWanpeng Li  * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
1918be7bd59dSWanpeng Li  * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
19194f98a2feSRik van Riel  */
192002695175SJohannes Weiner static void get_scan_count(struct lruvec *lruvec, int swappiness,
19216b4f7799SJohannes Weiner 			   struct scan_control *sc, unsigned long *nr,
19226b4f7799SJohannes Weiner 			   unsigned long *lru_pages)
19234f98a2feSRik van Riel {
192490126375SKonstantin Khlebnikov 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
19259a265114SJohannes Weiner 	u64 fraction[2];
19269a265114SJohannes Weiner 	u64 denominator = 0;	/* gcc */
192790126375SKonstantin Khlebnikov 	struct zone *zone = lruvec_zone(lruvec);
19289a265114SJohannes Weiner 	unsigned long anon_prio, file_prio;
19299a265114SJohannes Weiner 	enum scan_balance scan_balance;
19300bf1457fSJohannes Weiner 	unsigned long anon, file;
19319a265114SJohannes Weiner 	bool force_scan = false;
19329a265114SJohannes Weiner 	unsigned long ap, fp;
19339a265114SJohannes Weiner 	enum lru_list lru;
19346f04f48dSSuleiman Souhlal 	bool some_scanned;
19356f04f48dSSuleiman Souhlal 	int pass;
1936246e87a9SKAMEZAWA Hiroyuki 
1937f11c0ca5SJohannes Weiner 	/*
1938f11c0ca5SJohannes Weiner 	 * If the zone or memcg is small, nr[l] can be 0.  This
1939f11c0ca5SJohannes Weiner 	 * results in no scanning on this priority and a potential
1940f11c0ca5SJohannes Weiner 	 * priority drop.  Global direct reclaim can go to the next
1941f11c0ca5SJohannes Weiner 	 * zone and tends to have no problems. Global kswapd is for
1942f11c0ca5SJohannes Weiner 	 * zone balancing and it needs to scan a minimum amount. When
1943f11c0ca5SJohannes Weiner 	 * reclaiming for a memcg, a priority drop can cause high
1944f11c0ca5SJohannes Weiner 	 * latencies, so it's better to scan a minimum amount there as
1945f11c0ca5SJohannes Weiner 	 * well.
1946f11c0ca5SJohannes Weiner 	 */
194790cbc250SVladimir Davydov 	if (current_is_kswapd()) {
194890cbc250SVladimir Davydov 		if (!zone_reclaimable(zone))
1949a4d3e9e7SJohannes Weiner 			force_scan = true;
195090cbc250SVladimir Davydov 		if (!mem_cgroup_lruvec_online(lruvec))
195190cbc250SVladimir Davydov 			force_scan = true;
195290cbc250SVladimir Davydov 	}
195389b5fae5SJohannes Weiner 	if (!global_reclaim(sc))
1954a4d3e9e7SJohannes Weiner 		force_scan = true;
195576a33fc3SShaohua Li 
195676a33fc3SShaohua Li 	/* If we have no swap space, do not bother scanning anon pages. */
1957ec8acf20SShaohua Li 	if (!sc->may_swap || (get_nr_swap_pages() <= 0)) {
19589a265114SJohannes Weiner 		scan_balance = SCAN_FILE;
195976a33fc3SShaohua Li 		goto out;
196076a33fc3SShaohua Li 	}
19614f98a2feSRik van Riel 
196210316b31SJohannes Weiner 	/*
196310316b31SJohannes Weiner 	 * Global reclaim will swap to prevent OOM even with no
196410316b31SJohannes Weiner 	 * swappiness, but memcg users want to use this knob to
196510316b31SJohannes Weiner 	 * disable swapping for individual groups completely when
196610316b31SJohannes Weiner 	 * using the memory controller's swap limit feature would be
196710316b31SJohannes Weiner 	 * too expensive.
196810316b31SJohannes Weiner 	 */
196902695175SJohannes Weiner 	if (!global_reclaim(sc) && !swappiness) {
19709a265114SJohannes Weiner 		scan_balance = SCAN_FILE;
197110316b31SJohannes Weiner 		goto out;
197210316b31SJohannes Weiner 	}
197310316b31SJohannes Weiner 
197410316b31SJohannes Weiner 	/*
197510316b31SJohannes Weiner 	 * Do not apply any pressure balancing cleverness when the
197610316b31SJohannes Weiner 	 * system is close to OOM, scan both anon and file equally
197710316b31SJohannes Weiner 	 * (unless the swappiness setting disagrees with swapping).
197810316b31SJohannes Weiner 	 */
197902695175SJohannes Weiner 	if (!sc->priority && swappiness) {
19809a265114SJohannes Weiner 		scan_balance = SCAN_EQUAL;
198110316b31SJohannes Weiner 		goto out;
198210316b31SJohannes Weiner 	}
198310316b31SJohannes Weiner 
198411d16c25SJohannes Weiner 	/*
198562376251SJohannes Weiner 	 * Prevent the reclaimer from falling into the cache trap: as
198662376251SJohannes Weiner 	 * cache pages start out inactive, every cache fault will tip
198762376251SJohannes Weiner 	 * the scan balance towards the file LRU.  And as the file LRU
198862376251SJohannes Weiner 	 * shrinks, so does the window for rotation from references.
198962376251SJohannes Weiner 	 * This means we have a runaway feedback loop where a tiny
199062376251SJohannes Weiner 	 * thrashing file LRU becomes infinitely more attractive than
199162376251SJohannes Weiner 	 * anon pages.  Try to detect this based on file LRU size.
199262376251SJohannes Weiner 	 */
199362376251SJohannes Weiner 	if (global_reclaim(sc)) {
19942ab051e1SJerome Marchand 		unsigned long zonefile;
19952ab051e1SJerome Marchand 		unsigned long zonefree;
199662376251SJohannes Weiner 
19972ab051e1SJerome Marchand 		zonefree = zone_page_state(zone, NR_FREE_PAGES);
19982ab051e1SJerome Marchand 		zonefile = zone_page_state(zone, NR_ACTIVE_FILE) +
19992ab051e1SJerome Marchand 			   zone_page_state(zone, NR_INACTIVE_FILE);
20002ab051e1SJerome Marchand 
20012ab051e1SJerome Marchand 		if (unlikely(zonefile + zonefree <= high_wmark_pages(zone))) {
200262376251SJohannes Weiner 			scan_balance = SCAN_ANON;
200362376251SJohannes Weiner 			goto out;
200462376251SJohannes Weiner 		}
200562376251SJohannes Weiner 	}
200662376251SJohannes Weiner 
200762376251SJohannes Weiner 	/*
20087c5bd705SJohannes Weiner 	 * There is enough inactive page cache, do not reclaim
20097c5bd705SJohannes Weiner 	 * anything from the anonymous working set right now.
2010e9868505SRik van Riel 	 */
20117c5bd705SJohannes Weiner 	if (!inactive_file_is_low(lruvec)) {
20129a265114SJohannes Weiner 		scan_balance = SCAN_FILE;
2013e9868505SRik van Riel 		goto out;
20144f98a2feSRik van Riel 	}
20154f98a2feSRik van Riel 
20169a265114SJohannes Weiner 	scan_balance = SCAN_FRACT;
20179a265114SJohannes Weiner 
20184f98a2feSRik van Riel 	/*
201958c37f6eSKOSAKI Motohiro 	 * With swappiness at 100, anonymous and file have the same priority.
202058c37f6eSKOSAKI Motohiro 	 * This scanning priority is essentially the inverse of IO cost.
202158c37f6eSKOSAKI Motohiro 	 */
202202695175SJohannes Weiner 	anon_prio = swappiness;
202375b00af7SHugh Dickins 	file_prio = 200 - anon_prio;
202458c37f6eSKOSAKI Motohiro 
202558c37f6eSKOSAKI Motohiro 	/*
20264f98a2feSRik van Riel 	 * OK, so we have swap space and a fair amount of page cache
20274f98a2feSRik van Riel 	 * pages.  We use the recently rotated / recently scanned
20284f98a2feSRik van Riel 	 * ratios to determine how valuable each cache is.
20294f98a2feSRik van Riel 	 *
20304f98a2feSRik van Riel 	 * Because workloads change over time (and to avoid overflow)
20314f98a2feSRik van Riel 	 * we keep these statistics as a floating average, which ends
20324f98a2feSRik van Riel 	 * up weighing recent references more than old ones.
20334f98a2feSRik van Riel 	 *
20344f98a2feSRik van Riel 	 * anon in [0], file in [1]
20354f98a2feSRik van Riel 	 */
20362ab051e1SJerome Marchand 
20372ab051e1SJerome Marchand 	anon  = get_lru_size(lruvec, LRU_ACTIVE_ANON) +
20382ab051e1SJerome Marchand 		get_lru_size(lruvec, LRU_INACTIVE_ANON);
20392ab051e1SJerome Marchand 	file  = get_lru_size(lruvec, LRU_ACTIVE_FILE) +
20402ab051e1SJerome Marchand 		get_lru_size(lruvec, LRU_INACTIVE_FILE);
20412ab051e1SJerome Marchand 
204290126375SKonstantin Khlebnikov 	spin_lock_irq(&zone->lru_lock);
204358c37f6eSKOSAKI Motohiro 	if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
20446e901571SKOSAKI Motohiro 		reclaim_stat->recent_scanned[0] /= 2;
20456e901571SKOSAKI Motohiro 		reclaim_stat->recent_rotated[0] /= 2;
20464f98a2feSRik van Riel 	}
20474f98a2feSRik van Riel 
20486e901571SKOSAKI Motohiro 	if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
20496e901571SKOSAKI Motohiro 		reclaim_stat->recent_scanned[1] /= 2;
20506e901571SKOSAKI Motohiro 		reclaim_stat->recent_rotated[1] /= 2;
20514f98a2feSRik van Riel 	}
20524f98a2feSRik van Riel 
20534f98a2feSRik van Riel 	/*
205400d8089cSRik van Riel 	 * The amount of pressure on anon vs file pages is inversely
205500d8089cSRik van Riel 	 * proportional to the fraction of recently scanned pages on
205600d8089cSRik van Riel 	 * each list that were recently referenced and in active use.
20574f98a2feSRik van Riel 	 */
2058fe35004fSSatoru Moriya 	ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1);
20596e901571SKOSAKI Motohiro 	ap /= reclaim_stat->recent_rotated[0] + 1;
20604f98a2feSRik van Riel 
2061fe35004fSSatoru Moriya 	fp = file_prio * (reclaim_stat->recent_scanned[1] + 1);
20626e901571SKOSAKI Motohiro 	fp /= reclaim_stat->recent_rotated[1] + 1;
206390126375SKonstantin Khlebnikov 	spin_unlock_irq(&zone->lru_lock);
20644f98a2feSRik van Riel 
206576a33fc3SShaohua Li 	fraction[0] = ap;
206676a33fc3SShaohua Li 	fraction[1] = fp;
206776a33fc3SShaohua Li 	denominator = ap + fp + 1;
206876a33fc3SShaohua Li out:
20696f04f48dSSuleiman Souhlal 	some_scanned = false;
20706f04f48dSSuleiman Souhlal 	/* Only use force_scan on second pass. */
20716f04f48dSSuleiman Souhlal 	for (pass = 0; !some_scanned && pass < 2; pass++) {
20726b4f7799SJohannes Weiner 		*lru_pages = 0;
20734111304dSHugh Dickins 		for_each_evictable_lru(lru) {
20744111304dSHugh Dickins 			int file = is_file_lru(lru);
2075d778df51SJohannes Weiner 			unsigned long size;
207676a33fc3SShaohua Li 			unsigned long scan;
207776a33fc3SShaohua Li 
2078d778df51SJohannes Weiner 			size = get_lru_size(lruvec, lru);
2079d778df51SJohannes Weiner 			scan = size >> sc->priority;
20809a265114SJohannes Weiner 
20816f04f48dSSuleiman Souhlal 			if (!scan && pass && force_scan)
2082d778df51SJohannes Weiner 				scan = min(size, SWAP_CLUSTER_MAX);
20839a265114SJohannes Weiner 
20849a265114SJohannes Weiner 			switch (scan_balance) {
20859a265114SJohannes Weiner 			case SCAN_EQUAL:
20869a265114SJohannes Weiner 				/* Scan lists relative to size */
20879a265114SJohannes Weiner 				break;
20889a265114SJohannes Weiner 			case SCAN_FRACT:
20899a265114SJohannes Weiner 				/*
20909a265114SJohannes Weiner 				 * Scan types proportional to swappiness and
20919a265114SJohannes Weiner 				 * their relative recent reclaim efficiency.
20929a265114SJohannes Weiner 				 */
20936f04f48dSSuleiman Souhlal 				scan = div64_u64(scan * fraction[file],
20946f04f48dSSuleiman Souhlal 							denominator);
20959a265114SJohannes Weiner 				break;
20969a265114SJohannes Weiner 			case SCAN_FILE:
20979a265114SJohannes Weiner 			case SCAN_ANON:
20989a265114SJohannes Weiner 				/* Scan one type exclusively */
20996b4f7799SJohannes Weiner 				if ((scan_balance == SCAN_FILE) != file) {
21006b4f7799SJohannes Weiner 					size = 0;
21019a265114SJohannes Weiner 					scan = 0;
21026b4f7799SJohannes Weiner 				}
21039a265114SJohannes Weiner 				break;
21049a265114SJohannes Weiner 			default:
21059a265114SJohannes Weiner 				/* Look ma, no brain */
21069a265114SJohannes Weiner 				BUG();
21079a265114SJohannes Weiner 			}
21086b4f7799SJohannes Weiner 
21096b4f7799SJohannes Weiner 			*lru_pages += size;
21104111304dSHugh Dickins 			nr[lru] = scan;
21116b4f7799SJohannes Weiner 
21126f04f48dSSuleiman Souhlal 			/*
21136f04f48dSSuleiman Souhlal 			 * Skip the second pass and don't force_scan,
21146f04f48dSSuleiman Souhlal 			 * if we found something to scan.
21156f04f48dSSuleiman Souhlal 			 */
21166f04f48dSSuleiman Souhlal 			some_scanned |= !!scan;
21176f04f48dSSuleiman Souhlal 		}
211876a33fc3SShaohua Li 	}
21196e08a369SWu Fengguang }
21204f98a2feSRik van Riel 
21219b4f98cdSJohannes Weiner /*
21229b4f98cdSJohannes Weiner  * This is a basic per-zone page freer.  Used by both kswapd and direct reclaim.
21239b4f98cdSJohannes Weiner  */
212402695175SJohannes Weiner static void shrink_lruvec(struct lruvec *lruvec, int swappiness,
21256b4f7799SJohannes Weiner 			  struct scan_control *sc, unsigned long *lru_pages)
21269b4f98cdSJohannes Weiner {
21279b4f98cdSJohannes Weiner 	unsigned long nr[NR_LRU_LISTS];
2128e82e0561SMel Gorman 	unsigned long targets[NR_LRU_LISTS];
21299b4f98cdSJohannes Weiner 	unsigned long nr_to_scan;
21309b4f98cdSJohannes Weiner 	enum lru_list lru;
21319b4f98cdSJohannes Weiner 	unsigned long nr_reclaimed = 0;
21329b4f98cdSJohannes Weiner 	unsigned long nr_to_reclaim = sc->nr_to_reclaim;
21339b4f98cdSJohannes Weiner 	struct blk_plug plug;
21341a501907SMel Gorman 	bool scan_adjusted;
21359b4f98cdSJohannes Weiner 
21366b4f7799SJohannes Weiner 	get_scan_count(lruvec, swappiness, sc, nr, lru_pages);
21379b4f98cdSJohannes Weiner 
2138e82e0561SMel Gorman 	/* Record the original scan target for proportional adjustments later */
2139e82e0561SMel Gorman 	memcpy(targets, nr, sizeof(nr));
2140e82e0561SMel Gorman 
21411a501907SMel Gorman 	/*
21421a501907SMel Gorman 	 * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal
21431a501907SMel Gorman 	 * event that can occur when there is little memory pressure e.g.
21441a501907SMel Gorman 	 * multiple streaming readers/writers. Hence, we do not abort scanning
21451a501907SMel Gorman 	 * when the requested number of pages are reclaimed when scanning at
21461a501907SMel Gorman 	 * DEF_PRIORITY on the assumption that the fact we are direct
21471a501907SMel Gorman 	 * reclaiming implies that kswapd is not keeping up and it is best to
21481a501907SMel Gorman 	 * do a batch of work at once. For memcg reclaim one check is made to
21491a501907SMel Gorman 	 * abort proportional reclaim if either the file or anon lru has already
21501a501907SMel Gorman 	 * dropped to zero at the first pass.
21511a501907SMel Gorman 	 */
21521a501907SMel Gorman 	scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() &&
21531a501907SMel Gorman 			 sc->priority == DEF_PRIORITY);
21541a501907SMel Gorman 
21559b4f98cdSJohannes Weiner 	blk_start_plug(&plug);
21569b4f98cdSJohannes Weiner 	while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
21579b4f98cdSJohannes Weiner 					nr[LRU_INACTIVE_FILE]) {
2158e82e0561SMel Gorman 		unsigned long nr_anon, nr_file, percentage;
2159e82e0561SMel Gorman 		unsigned long nr_scanned;
2160e82e0561SMel Gorman 
21619b4f98cdSJohannes Weiner 		for_each_evictable_lru(lru) {
21629b4f98cdSJohannes Weiner 			if (nr[lru]) {
21639b4f98cdSJohannes Weiner 				nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX);
21649b4f98cdSJohannes Weiner 				nr[lru] -= nr_to_scan;
21659b4f98cdSJohannes Weiner 
21669b4f98cdSJohannes Weiner 				nr_reclaimed += shrink_list(lru, nr_to_scan,
21679b4f98cdSJohannes Weiner 							    lruvec, sc);
21689b4f98cdSJohannes Weiner 			}
21699b4f98cdSJohannes Weiner 		}
2170e82e0561SMel Gorman 
2171e82e0561SMel Gorman 		if (nr_reclaimed < nr_to_reclaim || scan_adjusted)
2172e82e0561SMel Gorman 			continue;
2173e82e0561SMel Gorman 
21749b4f98cdSJohannes Weiner 		/*
2175e82e0561SMel Gorman 		 * For kswapd and memcg, reclaim at least the number of pages
21761a501907SMel Gorman 		 * requested. Ensure that the anon and file LRUs are scanned
2177e82e0561SMel Gorman 		 * proportionally what was requested by get_scan_count(). We
2178e82e0561SMel Gorman 		 * stop reclaiming one LRU and reduce the amount scanning
2179e82e0561SMel Gorman 		 * proportional to the original scan target.
2180e82e0561SMel Gorman 		 */
2181e82e0561SMel Gorman 		nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE];
2182e82e0561SMel Gorman 		nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON];
2183e82e0561SMel Gorman 
21841a501907SMel Gorman 		/*
21851a501907SMel Gorman 		 * It's just vindictive to attack the larger once the smaller
21861a501907SMel Gorman 		 * has gone to zero.  And given the way we stop scanning the
21871a501907SMel Gorman 		 * smaller below, this makes sure that we only make one nudge
21881a501907SMel Gorman 		 * towards proportionality once we've got nr_to_reclaim.
21891a501907SMel Gorman 		 */
21901a501907SMel Gorman 		if (!nr_file || !nr_anon)
21911a501907SMel Gorman 			break;
21921a501907SMel Gorman 
2193e82e0561SMel Gorman 		if (nr_file > nr_anon) {
2194e82e0561SMel Gorman 			unsigned long scan_target = targets[LRU_INACTIVE_ANON] +
2195e82e0561SMel Gorman 						targets[LRU_ACTIVE_ANON] + 1;
2196e82e0561SMel Gorman 			lru = LRU_BASE;
2197e82e0561SMel Gorman 			percentage = nr_anon * 100 / scan_target;
2198e82e0561SMel Gorman 		} else {
2199e82e0561SMel Gorman 			unsigned long scan_target = targets[LRU_INACTIVE_FILE] +
2200e82e0561SMel Gorman 						targets[LRU_ACTIVE_FILE] + 1;
2201e82e0561SMel Gorman 			lru = LRU_FILE;
2202e82e0561SMel Gorman 			percentage = nr_file * 100 / scan_target;
2203e82e0561SMel Gorman 		}
2204e82e0561SMel Gorman 
2205e82e0561SMel Gorman 		/* Stop scanning the smaller of the LRU */
2206e82e0561SMel Gorman 		nr[lru] = 0;
2207e82e0561SMel Gorman 		nr[lru + LRU_ACTIVE] = 0;
2208e82e0561SMel Gorman 
2209e82e0561SMel Gorman 		/*
2210e82e0561SMel Gorman 		 * Recalculate the other LRU scan count based on its original
2211e82e0561SMel Gorman 		 * scan target and the percentage scanning already complete
2212e82e0561SMel Gorman 		 */
2213e82e0561SMel Gorman 		lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE;
2214e82e0561SMel Gorman 		nr_scanned = targets[lru] - nr[lru];
2215e82e0561SMel Gorman 		nr[lru] = targets[lru] * (100 - percentage) / 100;
2216e82e0561SMel Gorman 		nr[lru] -= min(nr[lru], nr_scanned);
2217e82e0561SMel Gorman 
2218e82e0561SMel Gorman 		lru += LRU_ACTIVE;
2219e82e0561SMel Gorman 		nr_scanned = targets[lru] - nr[lru];
2220e82e0561SMel Gorman 		nr[lru] = targets[lru] * (100 - percentage) / 100;
2221e82e0561SMel Gorman 		nr[lru] -= min(nr[lru], nr_scanned);
2222e82e0561SMel Gorman 
2223e82e0561SMel Gorman 		scan_adjusted = true;
22249b4f98cdSJohannes Weiner 	}
22259b4f98cdSJohannes Weiner 	blk_finish_plug(&plug);
22269b4f98cdSJohannes Weiner 	sc->nr_reclaimed += nr_reclaimed;
22279b4f98cdSJohannes Weiner 
22289b4f98cdSJohannes Weiner 	/*
22299b4f98cdSJohannes Weiner 	 * Even if we did not try to evict anon pages at all, we want to
22309b4f98cdSJohannes Weiner 	 * rebalance the anon lru active/inactive ratio.
22319b4f98cdSJohannes Weiner 	 */
22329b4f98cdSJohannes Weiner 	if (inactive_anon_is_low(lruvec))
22339b4f98cdSJohannes Weiner 		shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
22349b4f98cdSJohannes Weiner 				   sc, LRU_ACTIVE_ANON);
22359b4f98cdSJohannes Weiner 
22369b4f98cdSJohannes Weiner 	throttle_vm_writeout(sc->gfp_mask);
22379b4f98cdSJohannes Weiner }
22389b4f98cdSJohannes Weiner 
223923b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */
22409e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc)
224123b9da55SMel Gorman {
2242d84da3f9SKirill A. Shutemov 	if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
224323b9da55SMel Gorman 			(sc->order > PAGE_ALLOC_COSTLY_ORDER ||
22449e3b2f8cSKonstantin Khlebnikov 			 sc->priority < DEF_PRIORITY - 2))
224523b9da55SMel Gorman 		return true;
224623b9da55SMel Gorman 
224723b9da55SMel Gorman 	return false;
224823b9da55SMel Gorman }
224923b9da55SMel Gorman 
22504f98a2feSRik van Riel /*
225123b9da55SMel Gorman  * Reclaim/compaction is used for high-order allocation requests. It reclaims
225223b9da55SMel Gorman  * order-0 pages before compacting the zone. should_continue_reclaim() returns
225323b9da55SMel Gorman  * true if more pages should be reclaimed such that when the page allocator
225423b9da55SMel Gorman  * calls try_to_compact_zone() that it will have enough free pages to succeed.
225523b9da55SMel Gorman  * It will give up earlier than that if there is difficulty reclaiming pages.
22563e7d3449SMel Gorman  */
22579b4f98cdSJohannes Weiner static inline bool should_continue_reclaim(struct zone *zone,
22583e7d3449SMel Gorman 					unsigned long nr_reclaimed,
22593e7d3449SMel Gorman 					unsigned long nr_scanned,
22603e7d3449SMel Gorman 					struct scan_control *sc)
22613e7d3449SMel Gorman {
22623e7d3449SMel Gorman 	unsigned long pages_for_compaction;
22633e7d3449SMel Gorman 	unsigned long inactive_lru_pages;
22643e7d3449SMel Gorman 
22653e7d3449SMel Gorman 	/* If not in reclaim/compaction mode, stop */
22669e3b2f8cSKonstantin Khlebnikov 	if (!in_reclaim_compaction(sc))
22673e7d3449SMel Gorman 		return false;
22683e7d3449SMel Gorman 
22692876592fSMel Gorman 	/* Consider stopping depending on scan and reclaim activity */
22702876592fSMel Gorman 	if (sc->gfp_mask & __GFP_REPEAT) {
22713e7d3449SMel Gorman 		/*
22722876592fSMel Gorman 		 * For __GFP_REPEAT allocations, stop reclaiming if the
22732876592fSMel Gorman 		 * full LRU list has been scanned and we are still failing
22742876592fSMel Gorman 		 * to reclaim pages. This full LRU scan is potentially
22752876592fSMel Gorman 		 * expensive but a __GFP_REPEAT caller really wants to succeed
22763e7d3449SMel Gorman 		 */
22773e7d3449SMel Gorman 		if (!nr_reclaimed && !nr_scanned)
22783e7d3449SMel Gorman 			return false;
22792876592fSMel Gorman 	} else {
22802876592fSMel Gorman 		/*
22812876592fSMel Gorman 		 * For non-__GFP_REPEAT allocations which can presumably
22822876592fSMel Gorman 		 * fail without consequence, stop if we failed to reclaim
22832876592fSMel Gorman 		 * any pages from the last SWAP_CLUSTER_MAX number of
22842876592fSMel Gorman 		 * pages that were scanned. This will return to the
22852876592fSMel Gorman 		 * caller faster at the risk reclaim/compaction and
22862876592fSMel Gorman 		 * the resulting allocation attempt fails
22872876592fSMel Gorman 		 */
22882876592fSMel Gorman 		if (!nr_reclaimed)
22892876592fSMel Gorman 			return false;
22902876592fSMel Gorman 	}
22913e7d3449SMel Gorman 
22923e7d3449SMel Gorman 	/*
22933e7d3449SMel Gorman 	 * If we have not reclaimed enough pages for compaction and the
22943e7d3449SMel Gorman 	 * inactive lists are large enough, continue reclaiming
22953e7d3449SMel Gorman 	 */
22963e7d3449SMel Gorman 	pages_for_compaction = (2UL << sc->order);
22979b4f98cdSJohannes Weiner 	inactive_lru_pages = zone_page_state(zone, NR_INACTIVE_FILE);
2298ec8acf20SShaohua Li 	if (get_nr_swap_pages() > 0)
22999b4f98cdSJohannes Weiner 		inactive_lru_pages += zone_page_state(zone, NR_INACTIVE_ANON);
23003e7d3449SMel Gorman 	if (sc->nr_reclaimed < pages_for_compaction &&
23013e7d3449SMel Gorman 			inactive_lru_pages > pages_for_compaction)
23023e7d3449SMel Gorman 		return true;
23033e7d3449SMel Gorman 
23043e7d3449SMel Gorman 	/* If compaction would go ahead or the allocation would succeed, stop */
2305ebff3980SVlastimil Babka 	switch (compaction_suitable(zone, sc->order, 0, 0)) {
23063e7d3449SMel Gorman 	case COMPACT_PARTIAL:
23073e7d3449SMel Gorman 	case COMPACT_CONTINUE:
23083e7d3449SMel Gorman 		return false;
23093e7d3449SMel Gorman 	default:
23103e7d3449SMel Gorman 		return true;
23113e7d3449SMel Gorman 	}
23123e7d3449SMel Gorman }
23133e7d3449SMel Gorman 
23146b4f7799SJohannes Weiner static bool shrink_zone(struct zone *zone, struct scan_control *sc,
23156b4f7799SJohannes Weiner 			bool is_classzone)
2316f16015fbSJohannes Weiner {
2317cb731d6cSVladimir Davydov 	struct reclaim_state *reclaim_state = current->reclaim_state;
23189b4f98cdSJohannes Weiner 	unsigned long nr_reclaimed, nr_scanned;
23192344d7e4SJohannes Weiner 	bool reclaimable = false;
23209b4f98cdSJohannes Weiner 
23219b4f98cdSJohannes Weiner 	do {
23225660048cSJohannes Weiner 		struct mem_cgroup *root = sc->target_mem_cgroup;
23235660048cSJohannes Weiner 		struct mem_cgroup_reclaim_cookie reclaim = {
23245660048cSJohannes Weiner 			.zone = zone,
23259e3b2f8cSKonstantin Khlebnikov 			.priority = sc->priority,
23265660048cSJohannes Weiner 		};
23276b4f7799SJohannes Weiner 		unsigned long zone_lru_pages = 0;
2328694fbc0fSAndrew Morton 		struct mem_cgroup *memcg;
23295660048cSJohannes Weiner 
23309b4f98cdSJohannes Weiner 		nr_reclaimed = sc->nr_reclaimed;
23319b4f98cdSJohannes Weiner 		nr_scanned = sc->nr_scanned;
23329b4f98cdSJohannes Weiner 
2333694fbc0fSAndrew Morton 		memcg = mem_cgroup_iter(root, NULL, &reclaim);
2334694fbc0fSAndrew Morton 		do {
23356b4f7799SJohannes Weiner 			unsigned long lru_pages;
2336cb731d6cSVladimir Davydov 			unsigned long scanned;
23379b4f98cdSJohannes Weiner 			struct lruvec *lruvec;
233802695175SJohannes Weiner 			int swappiness;
23399b4f98cdSJohannes Weiner 
2340241994edSJohannes Weiner 			if (mem_cgroup_low(root, memcg)) {
2341241994edSJohannes Weiner 				if (!sc->may_thrash)
2342241994edSJohannes Weiner 					continue;
2343241994edSJohannes Weiner 				mem_cgroup_events(memcg, MEMCG_LOW, 1);
2344241994edSJohannes Weiner 			}
2345241994edSJohannes Weiner 
23469b4f98cdSJohannes Weiner 			lruvec = mem_cgroup_zone_lruvec(zone, memcg);
234702695175SJohannes Weiner 			swappiness = mem_cgroup_swappiness(memcg);
2348cb731d6cSVladimir Davydov 			scanned = sc->nr_scanned;
23495660048cSJohannes Weiner 
23506b4f7799SJohannes Weiner 			shrink_lruvec(lruvec, swappiness, sc, &lru_pages);
23516b4f7799SJohannes Weiner 			zone_lru_pages += lru_pages;
2352f9be23d6SKonstantin Khlebnikov 
2353cb731d6cSVladimir Davydov 			if (memcg && is_classzone)
2354cb731d6cSVladimir Davydov 				shrink_slab(sc->gfp_mask, zone_to_nid(zone),
2355cb731d6cSVladimir Davydov 					    memcg, sc->nr_scanned - scanned,
2356cb731d6cSVladimir Davydov 					    lru_pages);
2357cb731d6cSVladimir Davydov 
23585660048cSJohannes Weiner 			/*
2359a394cb8eSMichal Hocko 			 * Direct reclaim and kswapd have to scan all memory
2360a394cb8eSMichal Hocko 			 * cgroups to fulfill the overall scan target for the
23619b4f98cdSJohannes Weiner 			 * zone.
2362a394cb8eSMichal Hocko 			 *
2363a394cb8eSMichal Hocko 			 * Limit reclaim, on the other hand, only cares about
2364a394cb8eSMichal Hocko 			 * nr_to_reclaim pages to be reclaimed and it will
2365a394cb8eSMichal Hocko 			 * retry with decreasing priority if one round over the
2366a394cb8eSMichal Hocko 			 * whole hierarchy is not sufficient.
23675660048cSJohannes Weiner 			 */
2368a394cb8eSMichal Hocko 			if (!global_reclaim(sc) &&
2369a394cb8eSMichal Hocko 					sc->nr_reclaimed >= sc->nr_to_reclaim) {
23705660048cSJohannes Weiner 				mem_cgroup_iter_break(root, memcg);
23715660048cSJohannes Weiner 				break;
23725660048cSJohannes Weiner 			}
2373241994edSJohannes Weiner 		} while ((memcg = mem_cgroup_iter(root, memcg, &reclaim)));
237470ddf637SAnton Vorontsov 
23756b4f7799SJohannes Weiner 		/*
23766b4f7799SJohannes Weiner 		 * Shrink the slab caches in the same proportion that
23776b4f7799SJohannes Weiner 		 * the eligible LRU pages were scanned.
23786b4f7799SJohannes Weiner 		 */
2379cb731d6cSVladimir Davydov 		if (global_reclaim(sc) && is_classzone)
2380cb731d6cSVladimir Davydov 			shrink_slab(sc->gfp_mask, zone_to_nid(zone), NULL,
23816b4f7799SJohannes Weiner 				    sc->nr_scanned - nr_scanned,
23826b4f7799SJohannes Weiner 				    zone_lru_pages);
23836b4f7799SJohannes Weiner 
23846b4f7799SJohannes Weiner 		if (reclaim_state) {
2385cb731d6cSVladimir Davydov 			sc->nr_reclaimed += reclaim_state->reclaimed_slab;
23866b4f7799SJohannes Weiner 			reclaim_state->reclaimed_slab = 0;
23876b4f7799SJohannes Weiner 		}
23886b4f7799SJohannes Weiner 
238970ddf637SAnton Vorontsov 		vmpressure(sc->gfp_mask, sc->target_mem_cgroup,
239070ddf637SAnton Vorontsov 			   sc->nr_scanned - nr_scanned,
239170ddf637SAnton Vorontsov 			   sc->nr_reclaimed - nr_reclaimed);
239270ddf637SAnton Vorontsov 
23932344d7e4SJohannes Weiner 		if (sc->nr_reclaimed - nr_reclaimed)
23942344d7e4SJohannes Weiner 			reclaimable = true;
23952344d7e4SJohannes Weiner 
23969b4f98cdSJohannes Weiner 	} while (should_continue_reclaim(zone, sc->nr_reclaimed - nr_reclaimed,
23979b4f98cdSJohannes Weiner 					 sc->nr_scanned - nr_scanned, sc));
23982344d7e4SJohannes Weiner 
23992344d7e4SJohannes Weiner 	return reclaimable;
2400f16015fbSJohannes Weiner }
2401f16015fbSJohannes Weiner 
240253853e2dSVlastimil Babka /*
240353853e2dSVlastimil Babka  * Returns true if compaction should go ahead for a high-order request, or
240453853e2dSVlastimil Babka  * the high-order allocation would succeed without compaction.
240553853e2dSVlastimil Babka  */
24060b06496aSJohannes Weiner static inline bool compaction_ready(struct zone *zone, int order)
2407fe4b1b24SMel Gorman {
2408fe4b1b24SMel Gorman 	unsigned long balance_gap, watermark;
2409fe4b1b24SMel Gorman 	bool watermark_ok;
2410fe4b1b24SMel Gorman 
2411fe4b1b24SMel Gorman 	/*
2412fe4b1b24SMel Gorman 	 * Compaction takes time to run and there are potentially other
2413fe4b1b24SMel Gorman 	 * callers using the pages just freed. Continue reclaiming until
2414fe4b1b24SMel Gorman 	 * there is a buffer of free pages available to give compaction
2415fe4b1b24SMel Gorman 	 * a reasonable chance of completing and allocating the page
2416fe4b1b24SMel Gorman 	 */
24174be89a34SJianyu Zhan 	balance_gap = min(low_wmark_pages(zone), DIV_ROUND_UP(
24184be89a34SJianyu Zhan 			zone->managed_pages, KSWAPD_ZONE_BALANCE_GAP_RATIO));
24190b06496aSJohannes Weiner 	watermark = high_wmark_pages(zone) + balance_gap + (2UL << order);
2420fe4b1b24SMel Gorman 	watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0);
2421fe4b1b24SMel Gorman 
2422fe4b1b24SMel Gorman 	/*
2423fe4b1b24SMel Gorman 	 * If compaction is deferred, reclaim up to a point where
2424fe4b1b24SMel Gorman 	 * compaction will have a chance of success when re-enabled
2425fe4b1b24SMel Gorman 	 */
24260b06496aSJohannes Weiner 	if (compaction_deferred(zone, order))
2427fe4b1b24SMel Gorman 		return watermark_ok;
2428fe4b1b24SMel Gorman 
242953853e2dSVlastimil Babka 	/*
243053853e2dSVlastimil Babka 	 * If compaction is not ready to start and allocation is not likely
243153853e2dSVlastimil Babka 	 * to succeed without it, then keep reclaiming.
243253853e2dSVlastimil Babka 	 */
2433ebff3980SVlastimil Babka 	if (compaction_suitable(zone, order, 0, 0) == COMPACT_SKIPPED)
2434fe4b1b24SMel Gorman 		return false;
2435fe4b1b24SMel Gorman 
2436fe4b1b24SMel Gorman 	return watermark_ok;
2437fe4b1b24SMel Gorman }
2438fe4b1b24SMel Gorman 
24391da177e4SLinus Torvalds /*
24401da177e4SLinus Torvalds  * This is the direct reclaim path, for page-allocating processes.  We only
24411da177e4SLinus Torvalds  * try to reclaim pages from zones which will satisfy the caller's allocation
24421da177e4SLinus Torvalds  * request.
24431da177e4SLinus Torvalds  *
244441858966SMel Gorman  * We reclaim from a zone even if that zone is over high_wmark_pages(zone).
244541858966SMel Gorman  * Because:
24461da177e4SLinus Torvalds  * a) The caller may be trying to free *extra* pages to satisfy a higher-order
24471da177e4SLinus Torvalds  *    allocation or
244841858966SMel Gorman  * b) The target zone may be at high_wmark_pages(zone) but the lower zones
244941858966SMel Gorman  *    must go *over* high_wmark_pages(zone) to satisfy the `incremental min'
245041858966SMel Gorman  *    zone defense algorithm.
24511da177e4SLinus Torvalds  *
24521da177e4SLinus Torvalds  * If a zone is deemed to be full of pinned pages then just give it a light
24531da177e4SLinus Torvalds  * scan then give up on it.
24542344d7e4SJohannes Weiner  *
24552344d7e4SJohannes Weiner  * Returns true if a zone was reclaimable.
24561da177e4SLinus Torvalds  */
24572344d7e4SJohannes Weiner static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
24581da177e4SLinus Torvalds {
2459dd1a239fSMel Gorman 	struct zoneref *z;
246054a6eb5cSMel Gorman 	struct zone *zone;
24610608f43dSAndrew Morton 	unsigned long nr_soft_reclaimed;
24620608f43dSAndrew Morton 	unsigned long nr_soft_scanned;
2463619d0d76SWeijie Yang 	gfp_t orig_mask;
24649bbc04eeSWeijie Yang 	enum zone_type requested_highidx = gfp_zone(sc->gfp_mask);
24652344d7e4SJohannes Weiner 	bool reclaimable = false;
24661cfb419bSKAMEZAWA Hiroyuki 
2467cc715d99SMel Gorman 	/*
2468cc715d99SMel Gorman 	 * If the number of buffer_heads in the machine exceeds the maximum
2469cc715d99SMel Gorman 	 * allowed level, force direct reclaim to scan the highmem zone as
2470cc715d99SMel Gorman 	 * highmem pages could be pinning lowmem pages storing buffer_heads
2471cc715d99SMel Gorman 	 */
2472619d0d76SWeijie Yang 	orig_mask = sc->gfp_mask;
2473cc715d99SMel Gorman 	if (buffer_heads_over_limit)
2474cc715d99SMel Gorman 		sc->gfp_mask |= __GFP_HIGHMEM;
2475cc715d99SMel Gorman 
2476d4debc66SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
24776b4f7799SJohannes Weiner 					requested_highidx, sc->nodemask) {
24786b4f7799SJohannes Weiner 		enum zone_type classzone_idx;
24796b4f7799SJohannes Weiner 
2480f3fe6512SCon Kolivas 		if (!populated_zone(zone))
24811da177e4SLinus Torvalds 			continue;
24826b4f7799SJohannes Weiner 
24836b4f7799SJohannes Weiner 		classzone_idx = requested_highidx;
24846b4f7799SJohannes Weiner 		while (!populated_zone(zone->zone_pgdat->node_zones +
24856b4f7799SJohannes Weiner 							classzone_idx))
24866b4f7799SJohannes Weiner 			classzone_idx--;
24876b4f7799SJohannes Weiner 
24881cfb419bSKAMEZAWA Hiroyuki 		/*
24891cfb419bSKAMEZAWA Hiroyuki 		 * Take care memory controller reclaiming has small influence
24901cfb419bSKAMEZAWA Hiroyuki 		 * to global LRU.
24911cfb419bSKAMEZAWA Hiroyuki 		 */
249289b5fae5SJohannes Weiner 		if (global_reclaim(sc)) {
2493344736f2SVladimir Davydov 			if (!cpuset_zone_allowed(zone,
2494344736f2SVladimir Davydov 						 GFP_KERNEL | __GFP_HARDWALL))
24951da177e4SLinus Torvalds 				continue;
249665ec02cbSVladimir Davydov 
24976e543d57SLisa Du 			if (sc->priority != DEF_PRIORITY &&
24986e543d57SLisa Du 			    !zone_reclaimable(zone))
24991da177e4SLinus Torvalds 				continue;	/* Let kswapd poll it */
25000b06496aSJohannes Weiner 
2501e0887c19SRik van Riel 			/*
2502e0c23279SMel Gorman 			 * If we already have plenty of memory free for
2503e0c23279SMel Gorman 			 * compaction in this zone, don't free any more.
2504e0c23279SMel Gorman 			 * Even though compaction is invoked for any
2505e0c23279SMel Gorman 			 * non-zero order, only frequent costly order
2506e0c23279SMel Gorman 			 * reclamation is disruptive enough to become a
2507c7cfa37bSCopot Alexandru 			 * noticeable problem, like transparent huge
2508c7cfa37bSCopot Alexandru 			 * page allocations.
2509e0887c19SRik van Riel 			 */
25100b06496aSJohannes Weiner 			if (IS_ENABLED(CONFIG_COMPACTION) &&
25110b06496aSJohannes Weiner 			    sc->order > PAGE_ALLOC_COSTLY_ORDER &&
25120b06496aSJohannes Weiner 			    zonelist_zone_idx(z) <= requested_highidx &&
25130b06496aSJohannes Weiner 			    compaction_ready(zone, sc->order)) {
25140b06496aSJohannes Weiner 				sc->compaction_ready = true;
2515e0887c19SRik van Riel 				continue;
2516e0887c19SRik van Riel 			}
25170b06496aSJohannes Weiner 
25180608f43dSAndrew Morton 			/*
25190608f43dSAndrew Morton 			 * This steals pages from memory cgroups over softlimit
25200608f43dSAndrew Morton 			 * and returns the number of reclaimed pages and
25210608f43dSAndrew Morton 			 * scanned pages. This works for global memory pressure
25220608f43dSAndrew Morton 			 * and balancing, not for a memcg's limit.
25230608f43dSAndrew Morton 			 */
25240608f43dSAndrew Morton 			nr_soft_scanned = 0;
25250608f43dSAndrew Morton 			nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
25260608f43dSAndrew Morton 						sc->order, sc->gfp_mask,
25270608f43dSAndrew Morton 						&nr_soft_scanned);
25280608f43dSAndrew Morton 			sc->nr_reclaimed += nr_soft_reclaimed;
25290608f43dSAndrew Morton 			sc->nr_scanned += nr_soft_scanned;
25302344d7e4SJohannes Weiner 			if (nr_soft_reclaimed)
25312344d7e4SJohannes Weiner 				reclaimable = true;
2532ac34a1a3SKAMEZAWA Hiroyuki 			/* need some check for avoid more shrink_zone() */
2533ac34a1a3SKAMEZAWA Hiroyuki 		}
2534d149e3b2SYing Han 
25356b4f7799SJohannes Weiner 		if (shrink_zone(zone, sc, zone_idx(zone) == classzone_idx))
25362344d7e4SJohannes Weiner 			reclaimable = true;
25372344d7e4SJohannes Weiner 
25382344d7e4SJohannes Weiner 		if (global_reclaim(sc) &&
25392344d7e4SJohannes Weiner 		    !reclaimable && zone_reclaimable(zone))
25402344d7e4SJohannes Weiner 			reclaimable = true;
25411da177e4SLinus Torvalds 	}
2542e0c23279SMel Gorman 
254365ec02cbSVladimir Davydov 	/*
2544619d0d76SWeijie Yang 	 * Restore to original mask to avoid the impact on the caller if we
2545619d0d76SWeijie Yang 	 * promoted it to __GFP_HIGHMEM.
2546619d0d76SWeijie Yang 	 */
2547619d0d76SWeijie Yang 	sc->gfp_mask = orig_mask;
2548d1908362SMinchan Kim 
25492344d7e4SJohannes Weiner 	return reclaimable;
25501da177e4SLinus Torvalds }
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds /*
25531da177e4SLinus Torvalds  * This is the main entry point to direct page reclaim.
25541da177e4SLinus Torvalds  *
25551da177e4SLinus Torvalds  * If a full scan of the inactive list fails to free enough memory then we
25561da177e4SLinus Torvalds  * are "out of memory" and something needs to be killed.
25571da177e4SLinus Torvalds  *
25581da177e4SLinus Torvalds  * If the caller is !__GFP_FS then the probability of a failure is reasonably
25591da177e4SLinus Torvalds  * high - the zone may be full of dirty or under-writeback pages, which this
25605b0830cbSJens Axboe  * caller can't do much about.  We kick the writeback threads and take explicit
25615b0830cbSJens Axboe  * naps in the hope that some of these pages can be written.  But if the
25625b0830cbSJens Axboe  * allocating task holds filesystem locks which prevent writeout this might not
25635b0830cbSJens Axboe  * work, and the allocation attempt will fail.
2564a41f24eaSNishanth Aravamudan  *
2565a41f24eaSNishanth Aravamudan  * returns:	0, if no pages reclaimed
2566a41f24eaSNishanth Aravamudan  * 		else, the number of pages reclaimed
25671da177e4SLinus Torvalds  */
2568dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
25693115cd91SVladimir Davydov 					  struct scan_control *sc)
25701da177e4SLinus Torvalds {
2571241994edSJohannes Weiner 	int initial_priority = sc->priority;
257269e05944SAndrew Morton 	unsigned long total_scanned = 0;
257322fba335SKOSAKI Motohiro 	unsigned long writeback_threshold;
25742344d7e4SJohannes Weiner 	bool zones_reclaimable;
2575241994edSJohannes Weiner retry:
2576873b4771SKeika Kobayashi 	delayacct_freepages_start();
2577873b4771SKeika Kobayashi 
257889b5fae5SJohannes Weiner 	if (global_reclaim(sc))
2579f8891e5eSChristoph Lameter 		count_vm_event(ALLOCSTALL);
25801da177e4SLinus Torvalds 
25819e3b2f8cSKonstantin Khlebnikov 	do {
258270ddf637SAnton Vorontsov 		vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup,
258370ddf637SAnton Vorontsov 				sc->priority);
258466e1707bSBalbir Singh 		sc->nr_scanned = 0;
25852344d7e4SJohannes Weiner 		zones_reclaimable = shrink_zones(zonelist, sc);
2586e0c23279SMel Gorman 
258766e1707bSBalbir Singh 		total_scanned += sc->nr_scanned;
2588bb21c7ceSKOSAKI Motohiro 		if (sc->nr_reclaimed >= sc->nr_to_reclaim)
25890b06496aSJohannes Weiner 			break;
25900b06496aSJohannes Weiner 
25910b06496aSJohannes Weiner 		if (sc->compaction_ready)
25920b06496aSJohannes Weiner 			break;
25931da177e4SLinus Torvalds 
25941da177e4SLinus Torvalds 		/*
25950e50ce3bSMinchan Kim 		 * If we're getting trouble reclaiming, start doing
25960e50ce3bSMinchan Kim 		 * writepage even in laptop mode.
25970e50ce3bSMinchan Kim 		 */
25980e50ce3bSMinchan Kim 		if (sc->priority < DEF_PRIORITY - 2)
25990e50ce3bSMinchan Kim 			sc->may_writepage = 1;
26000e50ce3bSMinchan Kim 
26010e50ce3bSMinchan Kim 		/*
26021da177e4SLinus Torvalds 		 * Try to write back as many pages as we just scanned.  This
26031da177e4SLinus Torvalds 		 * tends to cause slow streaming writers to write data to the
26041da177e4SLinus Torvalds 		 * disk smoothly, at the dirtying rate, which is nice.   But
26051da177e4SLinus Torvalds 		 * that's undesirable in laptop mode, where we *want* lumpy
26061da177e4SLinus Torvalds 		 * writeout.  So in laptop mode, write out the whole world.
26071da177e4SLinus Torvalds 		 */
260822fba335SKOSAKI Motohiro 		writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
260922fba335SKOSAKI Motohiro 		if (total_scanned > writeback_threshold) {
26100e175a18SCurt Wohlgemuth 			wakeup_flusher_threads(laptop_mode ? 0 : total_scanned,
26110e175a18SCurt Wohlgemuth 						WB_REASON_TRY_TO_FREE_PAGES);
261266e1707bSBalbir Singh 			sc->may_writepage = 1;
26131da177e4SLinus Torvalds 		}
26140b06496aSJohannes Weiner 	} while (--sc->priority >= 0);
2615bb21c7ceSKOSAKI Motohiro 
2616873b4771SKeika Kobayashi 	delayacct_freepages_end();
2617873b4771SKeika Kobayashi 
2618bb21c7ceSKOSAKI Motohiro 	if (sc->nr_reclaimed)
2619bb21c7ceSKOSAKI Motohiro 		return sc->nr_reclaimed;
2620bb21c7ceSKOSAKI Motohiro 
26210cee34fdSMel Gorman 	/* Aborted reclaim to try compaction? don't OOM, then */
26220b06496aSJohannes Weiner 	if (sc->compaction_ready)
26237335084dSMel Gorman 		return 1;
26247335084dSMel Gorman 
2625241994edSJohannes Weiner 	/* Untapped cgroup reserves?  Don't OOM, retry. */
2626241994edSJohannes Weiner 	if (!sc->may_thrash) {
2627241994edSJohannes Weiner 		sc->priority = initial_priority;
2628241994edSJohannes Weiner 		sc->may_thrash = 1;
2629241994edSJohannes Weiner 		goto retry;
2630241994edSJohannes Weiner 	}
2631241994edSJohannes Weiner 
26322344d7e4SJohannes Weiner 	/* Any of the zones still reclaimable?  Don't OOM. */
26332344d7e4SJohannes Weiner 	if (zones_reclaimable)
2634bb21c7ceSKOSAKI Motohiro 		return 1;
2635bb21c7ceSKOSAKI Motohiro 
2636bb21c7ceSKOSAKI Motohiro 	return 0;
26371da177e4SLinus Torvalds }
26381da177e4SLinus Torvalds 
26395515061dSMel Gorman static bool pfmemalloc_watermark_ok(pg_data_t *pgdat)
26405515061dSMel Gorman {
26415515061dSMel Gorman 	struct zone *zone;
26425515061dSMel Gorman 	unsigned long pfmemalloc_reserve = 0;
26435515061dSMel Gorman 	unsigned long free_pages = 0;
26445515061dSMel Gorman 	int i;
26455515061dSMel Gorman 	bool wmark_ok;
26465515061dSMel Gorman 
26475515061dSMel Gorman 	for (i = 0; i <= ZONE_NORMAL; i++) {
26485515061dSMel Gorman 		zone = &pgdat->node_zones[i];
2649f012a84aSNishanth Aravamudan 		if (!populated_zone(zone) ||
2650f012a84aSNishanth Aravamudan 		    zone_reclaimable_pages(zone) == 0)
2651675becceSMel Gorman 			continue;
2652675becceSMel Gorman 
26535515061dSMel Gorman 		pfmemalloc_reserve += min_wmark_pages(zone);
26545515061dSMel Gorman 		free_pages += zone_page_state(zone, NR_FREE_PAGES);
26555515061dSMel Gorman 	}
26565515061dSMel Gorman 
2657675becceSMel Gorman 	/* If there are no reserves (unexpected config) then do not throttle */
2658675becceSMel Gorman 	if (!pfmemalloc_reserve)
2659675becceSMel Gorman 		return true;
2660675becceSMel Gorman 
26615515061dSMel Gorman 	wmark_ok = free_pages > pfmemalloc_reserve / 2;
26625515061dSMel Gorman 
26635515061dSMel Gorman 	/* kswapd must be awake if processes are being throttled */
26645515061dSMel Gorman 	if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
26655515061dSMel Gorman 		pgdat->classzone_idx = min(pgdat->classzone_idx,
26665515061dSMel Gorman 						(enum zone_type)ZONE_NORMAL);
26675515061dSMel Gorman 		wake_up_interruptible(&pgdat->kswapd_wait);
26685515061dSMel Gorman 	}
26695515061dSMel Gorman 
26705515061dSMel Gorman 	return wmark_ok;
26715515061dSMel Gorman }
26725515061dSMel Gorman 
26735515061dSMel Gorman /*
26745515061dSMel Gorman  * Throttle direct reclaimers if backing storage is backed by the network
26755515061dSMel Gorman  * and the PFMEMALLOC reserve for the preferred node is getting dangerously
26765515061dSMel Gorman  * depleted. kswapd will continue to make progress and wake the processes
267750694c28SMel Gorman  * when the low watermark is reached.
267850694c28SMel Gorman  *
267950694c28SMel Gorman  * Returns true if a fatal signal was delivered during throttling. If this
268050694c28SMel Gorman  * happens, the page allocator should not consider triggering the OOM killer.
26815515061dSMel Gorman  */
268250694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
26835515061dSMel Gorman 					nodemask_t *nodemask)
26845515061dSMel Gorman {
2685675becceSMel Gorman 	struct zoneref *z;
26865515061dSMel Gorman 	struct zone *zone;
2687675becceSMel Gorman 	pg_data_t *pgdat = NULL;
26885515061dSMel Gorman 
26895515061dSMel Gorman 	/*
26905515061dSMel Gorman 	 * Kernel threads should not be throttled as they may be indirectly
26915515061dSMel Gorman 	 * responsible for cleaning pages necessary for reclaim to make forward
26925515061dSMel Gorman 	 * progress. kjournald for example may enter direct reclaim while
26935515061dSMel Gorman 	 * committing a transaction where throttling it could forcing other
26945515061dSMel Gorman 	 * processes to block on log_wait_commit().
26955515061dSMel Gorman 	 */
26965515061dSMel Gorman 	if (current->flags & PF_KTHREAD)
269750694c28SMel Gorman 		goto out;
269850694c28SMel Gorman 
269950694c28SMel Gorman 	/*
270050694c28SMel Gorman 	 * If a fatal signal is pending, this process should not throttle.
270150694c28SMel Gorman 	 * It should return quickly so it can exit and free its memory
270250694c28SMel Gorman 	 */
270350694c28SMel Gorman 	if (fatal_signal_pending(current))
270450694c28SMel Gorman 		goto out;
27055515061dSMel Gorman 
2706675becceSMel Gorman 	/*
2707675becceSMel Gorman 	 * Check if the pfmemalloc reserves are ok by finding the first node
2708675becceSMel Gorman 	 * with a usable ZONE_NORMAL or lower zone. The expectation is that
2709675becceSMel Gorman 	 * GFP_KERNEL will be required for allocating network buffers when
2710675becceSMel Gorman 	 * swapping over the network so ZONE_HIGHMEM is unusable.
2711675becceSMel Gorman 	 *
2712675becceSMel Gorman 	 * Throttling is based on the first usable node and throttled processes
2713675becceSMel Gorman 	 * wait on a queue until kswapd makes progress and wakes them. There
2714675becceSMel Gorman 	 * is an affinity then between processes waking up and where reclaim
2715675becceSMel Gorman 	 * progress has been made assuming the process wakes on the same node.
2716675becceSMel Gorman 	 * More importantly, processes running on remote nodes will not compete
2717675becceSMel Gorman 	 * for remote pfmemalloc reserves and processes on different nodes
2718675becceSMel Gorman 	 * should make reasonable progress.
2719675becceSMel Gorman 	 */
2720675becceSMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist,
272117636faaSMichael S. Tsirkin 					gfp_zone(gfp_mask), nodemask) {
2722675becceSMel Gorman 		if (zone_idx(zone) > ZONE_NORMAL)
2723675becceSMel Gorman 			continue;
2724675becceSMel Gorman 
2725675becceSMel Gorman 		/* Throttle based on the first usable node */
27265515061dSMel Gorman 		pgdat = zone->zone_pgdat;
27275515061dSMel Gorman 		if (pfmemalloc_watermark_ok(pgdat))
272850694c28SMel Gorman 			goto out;
2729675becceSMel Gorman 		break;
2730675becceSMel Gorman 	}
2731675becceSMel Gorman 
2732675becceSMel Gorman 	/* If no zone was usable by the allocation flags then do not throttle */
2733675becceSMel Gorman 	if (!pgdat)
2734675becceSMel Gorman 		goto out;
27355515061dSMel Gorman 
273668243e76SMel Gorman 	/* Account for the throttling */
273768243e76SMel Gorman 	count_vm_event(PGSCAN_DIRECT_THROTTLE);
273868243e76SMel Gorman 
27395515061dSMel Gorman 	/*
27405515061dSMel Gorman 	 * If the caller cannot enter the filesystem, it's possible that it
27415515061dSMel Gorman 	 * is due to the caller holding an FS lock or performing a journal
27425515061dSMel Gorman 	 * transaction in the case of a filesystem like ext[3|4]. In this case,
27435515061dSMel Gorman 	 * it is not safe to block on pfmemalloc_wait as kswapd could be
27445515061dSMel Gorman 	 * blocked waiting on the same lock. Instead, throttle for up to a
27455515061dSMel Gorman 	 * second before continuing.
27465515061dSMel Gorman 	 */
27475515061dSMel Gorman 	if (!(gfp_mask & __GFP_FS)) {
27485515061dSMel Gorman 		wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
27495515061dSMel Gorman 			pfmemalloc_watermark_ok(pgdat), HZ);
275050694c28SMel Gorman 
275150694c28SMel Gorman 		goto check_pending;
27525515061dSMel Gorman 	}
27535515061dSMel Gorman 
27545515061dSMel Gorman 	/* Throttle until kswapd wakes the process */
27555515061dSMel Gorman 	wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
27565515061dSMel Gorman 		pfmemalloc_watermark_ok(pgdat));
275750694c28SMel Gorman 
275850694c28SMel Gorman check_pending:
275950694c28SMel Gorman 	if (fatal_signal_pending(current))
276050694c28SMel Gorman 		return true;
276150694c28SMel Gorman 
276250694c28SMel Gorman out:
276350694c28SMel Gorman 	return false;
27645515061dSMel Gorman }
27655515061dSMel Gorman 
2766dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
2767327c0e96SKAMEZAWA Hiroyuki 				gfp_t gfp_mask, nodemask_t *nodemask)
276866e1707bSBalbir Singh {
276933906bc5SMel Gorman 	unsigned long nr_reclaimed;
277066e1707bSBalbir Singh 	struct scan_control sc = {
277122fba335SKOSAKI Motohiro 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
2772ee814fe2SJohannes Weiner 		.gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)),
2773ee814fe2SJohannes Weiner 		.order = order,
2774ee814fe2SJohannes Weiner 		.nodemask = nodemask,
2775ee814fe2SJohannes Weiner 		.priority = DEF_PRIORITY,
2776ee814fe2SJohannes Weiner 		.may_writepage = !laptop_mode,
2777a6dc60f8SJohannes Weiner 		.may_unmap = 1,
27782e2e4259SKOSAKI Motohiro 		.may_swap = 1,
277966e1707bSBalbir Singh 	};
278066e1707bSBalbir Singh 
27815515061dSMel Gorman 	/*
278250694c28SMel Gorman 	 * Do not enter reclaim if fatal signal was delivered while throttled.
278350694c28SMel Gorman 	 * 1 is returned so that the page allocator does not OOM kill at this
278450694c28SMel Gorman 	 * point.
27855515061dSMel Gorman 	 */
278650694c28SMel Gorman 	if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask))
27875515061dSMel Gorman 		return 1;
27885515061dSMel Gorman 
278933906bc5SMel Gorman 	trace_mm_vmscan_direct_reclaim_begin(order,
279033906bc5SMel Gorman 				sc.may_writepage,
279133906bc5SMel Gorman 				gfp_mask);
279233906bc5SMel Gorman 
27933115cd91SVladimir Davydov 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
279433906bc5SMel Gorman 
279533906bc5SMel Gorman 	trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
279633906bc5SMel Gorman 
279733906bc5SMel Gorman 	return nr_reclaimed;
279866e1707bSBalbir Singh }
279966e1707bSBalbir Singh 
2800c255a458SAndrew Morton #ifdef CONFIG_MEMCG
280166e1707bSBalbir Singh 
280272835c86SJohannes Weiner unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg,
28034e416953SBalbir Singh 						gfp_t gfp_mask, bool noswap,
28040ae5e89cSYing Han 						struct zone *zone,
28050ae5e89cSYing Han 						unsigned long *nr_scanned)
28064e416953SBalbir Singh {
28074e416953SBalbir Singh 	struct scan_control sc = {
2808b8f5c566SKOSAKI Motohiro 		.nr_to_reclaim = SWAP_CLUSTER_MAX,
2809ee814fe2SJohannes Weiner 		.target_mem_cgroup = memcg,
28104e416953SBalbir Singh 		.may_writepage = !laptop_mode,
28114e416953SBalbir Singh 		.may_unmap = 1,
28124e416953SBalbir Singh 		.may_swap = !noswap,
28134e416953SBalbir Singh 	};
2814f9be23d6SKonstantin Khlebnikov 	struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
281502695175SJohannes Weiner 	int swappiness = mem_cgroup_swappiness(memcg);
28166b4f7799SJohannes Weiner 	unsigned long lru_pages;
28170ae5e89cSYing Han 
28184e416953SBalbir Singh 	sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
28194e416953SBalbir Singh 			(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
2820bdce6d9eSKOSAKI Motohiro 
28219e3b2f8cSKonstantin Khlebnikov 	trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
2822bdce6d9eSKOSAKI Motohiro 						      sc.may_writepage,
2823bdce6d9eSKOSAKI Motohiro 						      sc.gfp_mask);
2824bdce6d9eSKOSAKI Motohiro 
28254e416953SBalbir Singh 	/*
28264e416953SBalbir Singh 	 * NOTE: Although we can get the priority field, using it
28274e416953SBalbir Singh 	 * here is not a good idea, since it limits the pages we can scan.
28284e416953SBalbir Singh 	 * if we don't reclaim here, the shrink_zone from balance_pgdat
28294e416953SBalbir Singh 	 * will pick up pages from other mem cgroup's as well. We hack
28304e416953SBalbir Singh 	 * the priority and make it zero.
28314e416953SBalbir Singh 	 */
28326b4f7799SJohannes Weiner 	shrink_lruvec(lruvec, swappiness, &sc, &lru_pages);
2833bdce6d9eSKOSAKI Motohiro 
2834bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
2835bdce6d9eSKOSAKI Motohiro 
28360ae5e89cSYing Han 	*nr_scanned = sc.nr_scanned;
28374e416953SBalbir Singh 	return sc.nr_reclaimed;
28384e416953SBalbir Singh }
28394e416953SBalbir Singh 
284072835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
2841b70a2a21SJohannes Weiner 					   unsigned long nr_pages,
28428c7c6e34SKAMEZAWA Hiroyuki 					   gfp_t gfp_mask,
2843b70a2a21SJohannes Weiner 					   bool may_swap)
284466e1707bSBalbir Singh {
28454e416953SBalbir Singh 	struct zonelist *zonelist;
2846bdce6d9eSKOSAKI Motohiro 	unsigned long nr_reclaimed;
2847889976dbSYing Han 	int nid;
284866e1707bSBalbir Singh 	struct scan_control sc = {
2849b70a2a21SJohannes Weiner 		.nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
2850ee814fe2SJohannes Weiner 		.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
2851ee814fe2SJohannes Weiner 				(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
2852ee814fe2SJohannes Weiner 		.target_mem_cgroup = memcg,
2853ee814fe2SJohannes Weiner 		.priority = DEF_PRIORITY,
285466e1707bSBalbir Singh 		.may_writepage = !laptop_mode,
2855a6dc60f8SJohannes Weiner 		.may_unmap = 1,
2856b70a2a21SJohannes Weiner 		.may_swap = may_swap,
2857a09ed5e0SYing Han 	};
285866e1707bSBalbir Singh 
2859889976dbSYing Han 	/*
2860889976dbSYing Han 	 * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't
2861889976dbSYing Han 	 * take care of from where we get pages. So the node where we start the
2862889976dbSYing Han 	 * scan does not need to be the current node.
2863889976dbSYing Han 	 */
286472835c86SJohannes Weiner 	nid = mem_cgroup_select_victim_node(memcg);
2865889976dbSYing Han 
2866889976dbSYing Han 	zonelist = NODE_DATA(nid)->node_zonelists;
2867bdce6d9eSKOSAKI Motohiro 
2868bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_reclaim_begin(0,
2869bdce6d9eSKOSAKI Motohiro 					    sc.may_writepage,
2870bdce6d9eSKOSAKI Motohiro 					    sc.gfp_mask);
2871bdce6d9eSKOSAKI Motohiro 
28723115cd91SVladimir Davydov 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
2873bdce6d9eSKOSAKI Motohiro 
2874bdce6d9eSKOSAKI Motohiro 	trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
2875bdce6d9eSKOSAKI Motohiro 
2876bdce6d9eSKOSAKI Motohiro 	return nr_reclaimed;
287766e1707bSBalbir Singh }
287866e1707bSBalbir Singh #endif
287966e1707bSBalbir Singh 
28809e3b2f8cSKonstantin Khlebnikov static void age_active_anon(struct zone *zone, struct scan_control *sc)
2881f16015fbSJohannes Weiner {
2882b95a2f2dSJohannes Weiner 	struct mem_cgroup *memcg;
2883b95a2f2dSJohannes Weiner 
2884b95a2f2dSJohannes Weiner 	if (!total_swap_pages)
2885b95a2f2dSJohannes Weiner 		return;
2886b95a2f2dSJohannes Weiner 
2887b95a2f2dSJohannes Weiner 	memcg = mem_cgroup_iter(NULL, NULL, NULL);
2888b95a2f2dSJohannes Weiner 	do {
2889c56d5c7dSKonstantin Khlebnikov 		struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
2890f16015fbSJohannes Weiner 
2891c56d5c7dSKonstantin Khlebnikov 		if (inactive_anon_is_low(lruvec))
28921a93be0eSKonstantin Khlebnikov 			shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
28939e3b2f8cSKonstantin Khlebnikov 					   sc, LRU_ACTIVE_ANON);
2894b95a2f2dSJohannes Weiner 
2895b95a2f2dSJohannes Weiner 		memcg = mem_cgroup_iter(NULL, memcg, NULL);
2896b95a2f2dSJohannes Weiner 	} while (memcg);
2897f16015fbSJohannes Weiner }
2898f16015fbSJohannes Weiner 
289960cefed4SJohannes Weiner static bool zone_balanced(struct zone *zone, int order,
290060cefed4SJohannes Weiner 			  unsigned long balance_gap, int classzone_idx)
290160cefed4SJohannes Weiner {
290260cefed4SJohannes Weiner 	if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone) +
290360cefed4SJohannes Weiner 				    balance_gap, classzone_idx, 0))
290460cefed4SJohannes Weiner 		return false;
290560cefed4SJohannes Weiner 
2906ebff3980SVlastimil Babka 	if (IS_ENABLED(CONFIG_COMPACTION) && order && compaction_suitable(zone,
2907ebff3980SVlastimil Babka 				order, 0, classzone_idx) == COMPACT_SKIPPED)
290860cefed4SJohannes Weiner 		return false;
290960cefed4SJohannes Weiner 
291060cefed4SJohannes Weiner 	return true;
291160cefed4SJohannes Weiner }
291260cefed4SJohannes Weiner 
29131741c877SMel Gorman /*
29144ae0a48bSZlatko Calusic  * pgdat_balanced() is used when checking if a node is balanced.
29154ae0a48bSZlatko Calusic  *
29164ae0a48bSZlatko Calusic  * For order-0, all zones must be balanced!
29174ae0a48bSZlatko Calusic  *
29184ae0a48bSZlatko Calusic  * For high-order allocations only zones that meet watermarks and are in a
29194ae0a48bSZlatko Calusic  * zone allowed by the callers classzone_idx are added to balanced_pages. The
29204ae0a48bSZlatko Calusic  * total of balanced pages must be at least 25% of the zones allowed by
29214ae0a48bSZlatko Calusic  * classzone_idx for the node to be considered balanced. Forcing all zones to
29224ae0a48bSZlatko Calusic  * be balanced for high orders can cause excessive reclaim when there are
29234ae0a48bSZlatko Calusic  * imbalanced zones.
29241741c877SMel Gorman  * The choice of 25% is due to
29251741c877SMel Gorman  *   o a 16M DMA zone that is balanced will not balance a zone on any
29261741c877SMel Gorman  *     reasonable sized machine
29271741c877SMel Gorman  *   o On all other machines, the top zone must be at least a reasonable
292825985edcSLucas De Marchi  *     percentage of the middle zones. For example, on 32-bit x86, highmem
29291741c877SMel Gorman  *     would need to be at least 256M for it to be balance a whole node.
29301741c877SMel Gorman  *     Similarly, on x86-64 the Normal zone would need to be at least 1G
29311741c877SMel Gorman  *     to balance a node on its own. These seemed like reasonable ratios.
29321741c877SMel Gorman  */
29334ae0a48bSZlatko Calusic static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx)
29341741c877SMel Gorman {
2935b40da049SJiang Liu 	unsigned long managed_pages = 0;
29364ae0a48bSZlatko Calusic 	unsigned long balanced_pages = 0;
29371741c877SMel Gorman 	int i;
29381741c877SMel Gorman 
29394ae0a48bSZlatko Calusic 	/* Check the watermark levels */
29404ae0a48bSZlatko Calusic 	for (i = 0; i <= classzone_idx; i++) {
29414ae0a48bSZlatko Calusic 		struct zone *zone = pgdat->node_zones + i;
29421741c877SMel Gorman 
29434ae0a48bSZlatko Calusic 		if (!populated_zone(zone))
29444ae0a48bSZlatko Calusic 			continue;
29454ae0a48bSZlatko Calusic 
2946b40da049SJiang Liu 		managed_pages += zone->managed_pages;
29474ae0a48bSZlatko Calusic 
29484ae0a48bSZlatko Calusic 		/*
29494ae0a48bSZlatko Calusic 		 * A special case here:
29504ae0a48bSZlatko Calusic 		 *
29514ae0a48bSZlatko Calusic 		 * balance_pgdat() skips over all_unreclaimable after
29524ae0a48bSZlatko Calusic 		 * DEF_PRIORITY. Effectively, it considers them balanced so
29534ae0a48bSZlatko Calusic 		 * they must be considered balanced here as well!
29544ae0a48bSZlatko Calusic 		 */
29556e543d57SLisa Du 		if (!zone_reclaimable(zone)) {
2956b40da049SJiang Liu 			balanced_pages += zone->managed_pages;
29574ae0a48bSZlatko Calusic 			continue;
29584ae0a48bSZlatko Calusic 		}
29594ae0a48bSZlatko Calusic 
29604ae0a48bSZlatko Calusic 		if (zone_balanced(zone, order, 0, i))
2961b40da049SJiang Liu 			balanced_pages += zone->managed_pages;
29624ae0a48bSZlatko Calusic 		else if (!order)
29634ae0a48bSZlatko Calusic 			return false;
29644ae0a48bSZlatko Calusic 	}
29654ae0a48bSZlatko Calusic 
29664ae0a48bSZlatko Calusic 	if (order)
2967b40da049SJiang Liu 		return balanced_pages >= (managed_pages >> 2);
29684ae0a48bSZlatko Calusic 	else
29694ae0a48bSZlatko Calusic 		return true;
29701741c877SMel Gorman }
29711741c877SMel Gorman 
29725515061dSMel Gorman /*
29735515061dSMel Gorman  * Prepare kswapd for sleeping. This verifies that there are no processes
29745515061dSMel Gorman  * waiting in throttle_direct_reclaim() and that watermarks have been met.
29755515061dSMel Gorman  *
29765515061dSMel Gorman  * Returns true if kswapd is ready to sleep
29775515061dSMel Gorman  */
29785515061dSMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, long remaining,
2979dc83edd9SMel Gorman 					int classzone_idx)
2980f50de2d3SMel Gorman {
2981f50de2d3SMel Gorman 	/* If a direct reclaimer woke kswapd within HZ/10, it's premature */
2982f50de2d3SMel Gorman 	if (remaining)
29835515061dSMel Gorman 		return false;
29845515061dSMel Gorman 
29855515061dSMel Gorman 	/*
29869e5e3661SVlastimil Babka 	 * The throttled processes are normally woken up in balance_pgdat() as
29879e5e3661SVlastimil Babka 	 * soon as pfmemalloc_watermark_ok() is true. But there is a potential
29889e5e3661SVlastimil Babka 	 * race between when kswapd checks the watermarks and a process gets
29899e5e3661SVlastimil Babka 	 * throttled. There is also a potential race if processes get
29909e5e3661SVlastimil Babka 	 * throttled, kswapd wakes, a large process exits thereby balancing the
29919e5e3661SVlastimil Babka 	 * zones, which causes kswapd to exit balance_pgdat() before reaching
29929e5e3661SVlastimil Babka 	 * the wake up checks. If kswapd is going to sleep, no process should
29939e5e3661SVlastimil Babka 	 * be sleeping on pfmemalloc_wait, so wake them now if necessary. If
29949e5e3661SVlastimil Babka 	 * the wake up is premature, processes will wake kswapd and get
29959e5e3661SVlastimil Babka 	 * throttled again. The difference from wake ups in balance_pgdat() is
29969e5e3661SVlastimil Babka 	 * that here we are under prepare_to_wait().
29975515061dSMel Gorman 	 */
29989e5e3661SVlastimil Babka 	if (waitqueue_active(&pgdat->pfmemalloc_wait))
29999e5e3661SVlastimil Babka 		wake_up_all(&pgdat->pfmemalloc_wait);
3000f50de2d3SMel Gorman 
30014ae0a48bSZlatko Calusic 	return pgdat_balanced(pgdat, order, classzone_idx);
3002f50de2d3SMel Gorman }
3003f50de2d3SMel Gorman 
30041da177e4SLinus Torvalds /*
300575485363SMel Gorman  * kswapd shrinks the zone by the number of pages required to reach
300675485363SMel Gorman  * the high watermark.
3007b8e83b94SMel Gorman  *
3008b8e83b94SMel Gorman  * Returns true if kswapd scanned at least the requested number of pages to
3009283aba9fSMel Gorman  * reclaim or if the lack of progress was due to pages under writeback.
3010283aba9fSMel Gorman  * This is used to determine if the scanning priority needs to be raised.
301175485363SMel Gorman  */
3012b8e83b94SMel Gorman static bool kswapd_shrink_zone(struct zone *zone,
30137c954f6dSMel Gorman 			       int classzone_idx,
301475485363SMel Gorman 			       struct scan_control *sc,
30152ab44f43SMel Gorman 			       unsigned long *nr_attempted)
301675485363SMel Gorman {
30177c954f6dSMel Gorman 	int testorder = sc->order;
30187c954f6dSMel Gorman 	unsigned long balance_gap;
30197c954f6dSMel Gorman 	bool lowmem_pressure;
302075485363SMel Gorman 
302175485363SMel Gorman 	/* Reclaim above the high watermark. */
302275485363SMel Gorman 	sc->nr_to_reclaim = max(SWAP_CLUSTER_MAX, high_wmark_pages(zone));
30237c954f6dSMel Gorman 
30247c954f6dSMel Gorman 	/*
30257c954f6dSMel Gorman 	 * Kswapd reclaims only single pages with compaction enabled. Trying
30267c954f6dSMel Gorman 	 * too hard to reclaim until contiguous free pages have become
30277c954f6dSMel Gorman 	 * available can hurt performance by evicting too much useful data
30287c954f6dSMel Gorman 	 * from memory. Do not reclaim more than needed for compaction.
30297c954f6dSMel Gorman 	 */
30307c954f6dSMel Gorman 	if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
3031ebff3980SVlastimil Babka 			compaction_suitable(zone, sc->order, 0, classzone_idx)
3032ebff3980SVlastimil Babka 							!= COMPACT_SKIPPED)
30337c954f6dSMel Gorman 		testorder = 0;
30347c954f6dSMel Gorman 
30357c954f6dSMel Gorman 	/*
30367c954f6dSMel Gorman 	 * We put equal pressure on every zone, unless one zone has way too
30377c954f6dSMel Gorman 	 * many pages free already. The "too many pages" is defined as the
30387c954f6dSMel Gorman 	 * high wmark plus a "gap" where the gap is either the low
30397c954f6dSMel Gorman 	 * watermark or 1% of the zone, whichever is smaller.
30407c954f6dSMel Gorman 	 */
30414be89a34SJianyu Zhan 	balance_gap = min(low_wmark_pages(zone), DIV_ROUND_UP(
30424be89a34SJianyu Zhan 			zone->managed_pages, KSWAPD_ZONE_BALANCE_GAP_RATIO));
30437c954f6dSMel Gorman 
30447c954f6dSMel Gorman 	/*
30457c954f6dSMel Gorman 	 * If there is no low memory pressure or the zone is balanced then no
30467c954f6dSMel Gorman 	 * reclaim is necessary
30477c954f6dSMel Gorman 	 */
30487c954f6dSMel Gorman 	lowmem_pressure = (buffer_heads_over_limit && is_highmem(zone));
30497c954f6dSMel Gorman 	if (!lowmem_pressure && zone_balanced(zone, testorder,
30507c954f6dSMel Gorman 						balance_gap, classzone_idx))
30517c954f6dSMel Gorman 		return true;
30527c954f6dSMel Gorman 
30536b4f7799SJohannes Weiner 	shrink_zone(zone, sc, zone_idx(zone) == classzone_idx);
305475485363SMel Gorman 
30552ab44f43SMel Gorman 	/* Account for the number of pages attempted to reclaim */
30562ab44f43SMel Gorman 	*nr_attempted += sc->nr_to_reclaim;
30572ab44f43SMel Gorman 
305857054651SJohannes Weiner 	clear_bit(ZONE_WRITEBACK, &zone->flags);
3059283aba9fSMel Gorman 
30607c954f6dSMel Gorman 	/*
30617c954f6dSMel Gorman 	 * If a zone reaches its high watermark, consider it to be no longer
30627c954f6dSMel Gorman 	 * congested. It's possible there are dirty pages backed by congested
30637c954f6dSMel Gorman 	 * BDIs but as pressure is relieved, speculatively avoid congestion
30647c954f6dSMel Gorman 	 * waits.
30657c954f6dSMel Gorman 	 */
30666e543d57SLisa Du 	if (zone_reclaimable(zone) &&
30677c954f6dSMel Gorman 	    zone_balanced(zone, testorder, 0, classzone_idx)) {
306857054651SJohannes Weiner 		clear_bit(ZONE_CONGESTED, &zone->flags);
306957054651SJohannes Weiner 		clear_bit(ZONE_DIRTY, &zone->flags);
30707c954f6dSMel Gorman 	}
30717c954f6dSMel Gorman 
3072b8e83b94SMel Gorman 	return sc->nr_scanned >= sc->nr_to_reclaim;
307375485363SMel Gorman }
307475485363SMel Gorman 
307575485363SMel Gorman /*
30761da177e4SLinus Torvalds  * For kswapd, balance_pgdat() will work across all this node's zones until
307741858966SMel Gorman  * they are all at high_wmark_pages(zone).
30781da177e4SLinus Torvalds  *
30790abdee2bSMel Gorman  * Returns the final order kswapd was reclaiming at
30801da177e4SLinus Torvalds  *
30811da177e4SLinus Torvalds  * There is special handling here for zones which are full of pinned pages.
30821da177e4SLinus Torvalds  * This can happen if the pages are all mlocked, or if they are all used by
30831da177e4SLinus Torvalds  * device drivers (say, ZONE_DMA).  Or if they are all in use by hugetlb.
30841da177e4SLinus Torvalds  * What we do is to detect the case where all pages in the zone have been
30851da177e4SLinus Torvalds  * scanned twice and there has been zero successful reclaim.  Mark the zone as
30861da177e4SLinus Torvalds  * dead and from now on, only perform a short scan.  Basically we're polling
30871da177e4SLinus Torvalds  * the zone for when the problem goes away.
30881da177e4SLinus Torvalds  *
30891da177e4SLinus Torvalds  * kswapd scans the zones in the highmem->normal->dma direction.  It skips
309041858966SMel Gorman  * zones which have free_pages > high_wmark_pages(zone), but once a zone is
309141858966SMel Gorman  * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the
309241858966SMel Gorman  * lower zones regardless of the number of free pages in the lower zones. This
309341858966SMel Gorman  * interoperates with the page allocator fallback scheme to ensure that aging
309441858966SMel Gorman  * of pages is balanced across the zones.
30951da177e4SLinus Torvalds  */
309699504748SMel Gorman static unsigned long balance_pgdat(pg_data_t *pgdat, int order,
3097dc83edd9SMel Gorman 							int *classzone_idx)
30981da177e4SLinus Torvalds {
30991da177e4SLinus Torvalds 	int i;
310099504748SMel Gorman 	int end_zone = 0;	/* Inclusive.  0 = ZONE_DMA */
31010608f43dSAndrew Morton 	unsigned long nr_soft_reclaimed;
31020608f43dSAndrew Morton 	unsigned long nr_soft_scanned;
3103179e9639SAndrew Morton 	struct scan_control sc = {
3104179e9639SAndrew Morton 		.gfp_mask = GFP_KERNEL,
3105ee814fe2SJohannes Weiner 		.order = order,
3106b8e83b94SMel Gorman 		.priority = DEF_PRIORITY,
3107ee814fe2SJohannes Weiner 		.may_writepage = !laptop_mode,
3108a6dc60f8SJohannes Weiner 		.may_unmap = 1,
31092e2e4259SKOSAKI Motohiro 		.may_swap = 1,
3110179e9639SAndrew Morton 	};
3111f8891e5eSChristoph Lameter 	count_vm_event(PAGEOUTRUN);
31121da177e4SLinus Torvalds 
31139e3b2f8cSKonstantin Khlebnikov 	do {
31142ab44f43SMel Gorman 		unsigned long nr_attempted = 0;
3115b8e83b94SMel Gorman 		bool raise_priority = true;
31162ab44f43SMel Gorman 		bool pgdat_needs_compaction = (order > 0);
3117b8e83b94SMel Gorman 
3118b8e83b94SMel Gorman 		sc.nr_reclaimed = 0;
31191da177e4SLinus Torvalds 
31201da177e4SLinus Torvalds 		/*
31211da177e4SLinus Torvalds 		 * Scan in the highmem->dma direction for the highest
31221da177e4SLinus Torvalds 		 * zone which needs scanning
31231da177e4SLinus Torvalds 		 */
31241da177e4SLinus Torvalds 		for (i = pgdat->nr_zones - 1; i >= 0; i--) {
31251da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
31261da177e4SLinus Torvalds 
3127f3fe6512SCon Kolivas 			if (!populated_zone(zone))
31281da177e4SLinus Torvalds 				continue;
31291da177e4SLinus Torvalds 
31306e543d57SLisa Du 			if (sc.priority != DEF_PRIORITY &&
31316e543d57SLisa Du 			    !zone_reclaimable(zone))
31321da177e4SLinus Torvalds 				continue;
31331da177e4SLinus Torvalds 
3134556adecbSRik van Riel 			/*
3135556adecbSRik van Riel 			 * Do some background aging of the anon list, to give
3136556adecbSRik van Riel 			 * pages a chance to be referenced before reclaiming.
3137556adecbSRik van Riel 			 */
31389e3b2f8cSKonstantin Khlebnikov 			age_active_anon(zone, &sc);
3139556adecbSRik van Riel 
3140cc715d99SMel Gorman 			/*
3141cc715d99SMel Gorman 			 * If the number of buffer_heads in the machine
3142cc715d99SMel Gorman 			 * exceeds the maximum allowed level and this node
3143cc715d99SMel Gorman 			 * has a highmem zone, force kswapd to reclaim from
3144cc715d99SMel Gorman 			 * it to relieve lowmem pressure.
3145cc715d99SMel Gorman 			 */
3146cc715d99SMel Gorman 			if (buffer_heads_over_limit && is_highmem_idx(i)) {
3147cc715d99SMel Gorman 				end_zone = i;
3148cc715d99SMel Gorman 				break;
3149cc715d99SMel Gorman 			}
3150cc715d99SMel Gorman 
315160cefed4SJohannes Weiner 			if (!zone_balanced(zone, order, 0, 0)) {
31521da177e4SLinus Torvalds 				end_zone = i;
3153e1dbeda6SAndrew Morton 				break;
3154439423f6SShaohua Li 			} else {
3155d43006d5SMel Gorman 				/*
3156d43006d5SMel Gorman 				 * If balanced, clear the dirty and congested
3157d43006d5SMel Gorman 				 * flags
3158d43006d5SMel Gorman 				 */
315957054651SJohannes Weiner 				clear_bit(ZONE_CONGESTED, &zone->flags);
316057054651SJohannes Weiner 				clear_bit(ZONE_DIRTY, &zone->flags);
31611da177e4SLinus Torvalds 			}
31621da177e4SLinus Torvalds 		}
3163dafcb73eSZlatko Calusic 
3164b8e83b94SMel Gorman 		if (i < 0)
31651da177e4SLinus Torvalds 			goto out;
3166e1dbeda6SAndrew Morton 
31671da177e4SLinus Torvalds 		for (i = 0; i <= end_zone; i++) {
31681da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
31691da177e4SLinus Torvalds 
31702ab44f43SMel Gorman 			if (!populated_zone(zone))
31712ab44f43SMel Gorman 				continue;
31722ab44f43SMel Gorman 
31732ab44f43SMel Gorman 			/*
31742ab44f43SMel Gorman 			 * If any zone is currently balanced then kswapd will
31752ab44f43SMel Gorman 			 * not call compaction as it is expected that the
31762ab44f43SMel Gorman 			 * necessary pages are already available.
31772ab44f43SMel Gorman 			 */
31782ab44f43SMel Gorman 			if (pgdat_needs_compaction &&
31792ab44f43SMel Gorman 					zone_watermark_ok(zone, order,
31802ab44f43SMel Gorman 						low_wmark_pages(zone),
31812ab44f43SMel Gorman 						*classzone_idx, 0))
31822ab44f43SMel Gorman 				pgdat_needs_compaction = false;
31831da177e4SLinus Torvalds 		}
31841da177e4SLinus Torvalds 
31851da177e4SLinus Torvalds 		/*
3186b7ea3c41SMel Gorman 		 * If we're getting trouble reclaiming, start doing writepage
3187b7ea3c41SMel Gorman 		 * even in laptop mode.
3188b7ea3c41SMel Gorman 		 */
3189b7ea3c41SMel Gorman 		if (sc.priority < DEF_PRIORITY - 2)
3190b7ea3c41SMel Gorman 			sc.may_writepage = 1;
3191b7ea3c41SMel Gorman 
3192b7ea3c41SMel Gorman 		/*
31931da177e4SLinus Torvalds 		 * Now scan the zone in the dma->highmem direction, stopping
31941da177e4SLinus Torvalds 		 * at the last zone which needs scanning.
31951da177e4SLinus Torvalds 		 *
31961da177e4SLinus Torvalds 		 * We do this because the page allocator works in the opposite
31971da177e4SLinus Torvalds 		 * direction.  This prevents the page allocator from allocating
31981da177e4SLinus Torvalds 		 * pages behind kswapd's direction of progress, which would
31991da177e4SLinus Torvalds 		 * cause too much scanning of the lower zones.
32001da177e4SLinus Torvalds 		 */
32011da177e4SLinus Torvalds 		for (i = 0; i <= end_zone; i++) {
32021da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
32031da177e4SLinus Torvalds 
3204f3fe6512SCon Kolivas 			if (!populated_zone(zone))
32051da177e4SLinus Torvalds 				continue;
32061da177e4SLinus Torvalds 
32076e543d57SLisa Du 			if (sc.priority != DEF_PRIORITY &&
32086e543d57SLisa Du 			    !zone_reclaimable(zone))
32091da177e4SLinus Torvalds 				continue;
32101da177e4SLinus Torvalds 
32111da177e4SLinus Torvalds 			sc.nr_scanned = 0;
32124e416953SBalbir Singh 
32130608f43dSAndrew Morton 			nr_soft_scanned = 0;
32140608f43dSAndrew Morton 			/*
32150608f43dSAndrew Morton 			 * Call soft limit reclaim before calling shrink_zone.
32160608f43dSAndrew Morton 			 */
32170608f43dSAndrew Morton 			nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
32180608f43dSAndrew Morton 							order, sc.gfp_mask,
32190608f43dSAndrew Morton 							&nr_soft_scanned);
32200608f43dSAndrew Morton 			sc.nr_reclaimed += nr_soft_reclaimed;
32210608f43dSAndrew Morton 
322232a4330dSRik van Riel 			/*
32237c954f6dSMel Gorman 			 * There should be no need to raise the scanning
32247c954f6dSMel Gorman 			 * priority if enough pages are already being scanned
32257c954f6dSMel Gorman 			 * that that high watermark would be met at 100%
32267c954f6dSMel Gorman 			 * efficiency.
322732a4330dSRik van Riel 			 */
32286b4f7799SJohannes Weiner 			if (kswapd_shrink_zone(zone, end_zone,
32296b4f7799SJohannes Weiner 					       &sc, &nr_attempted))
3230b8e83b94SMel Gorman 				raise_priority = false;
3231b8e83b94SMel Gorman 		}
3232d7868daeSMel Gorman 
32335515061dSMel Gorman 		/*
32345515061dSMel Gorman 		 * If the low watermark is met there is no need for processes
32355515061dSMel Gorman 		 * to be throttled on pfmemalloc_wait as they should not be
32365515061dSMel Gorman 		 * able to safely make forward progress. Wake them
32375515061dSMel Gorman 		 */
32385515061dSMel Gorman 		if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
32395515061dSMel Gorman 				pfmemalloc_watermark_ok(pgdat))
3240cfc51155SVlastimil Babka 			wake_up_all(&pgdat->pfmemalloc_wait);
32415515061dSMel Gorman 
32421da177e4SLinus Torvalds 		/*
3243b8e83b94SMel Gorman 		 * Fragmentation may mean that the system cannot be rebalanced
3244b8e83b94SMel Gorman 		 * for high-order allocations in all zones. If twice the
3245b8e83b94SMel Gorman 		 * allocation size has been reclaimed and the zones are still
3246b8e83b94SMel Gorman 		 * not balanced then recheck the watermarks at order-0 to
3247b8e83b94SMel Gorman 		 * prevent kswapd reclaiming excessively. Assume that a
3248b8e83b94SMel Gorman 		 * process requested a high-order can direct reclaim/compact.
32491da177e4SLinus Torvalds 		 */
3250b8e83b94SMel Gorman 		if (order && sc.nr_reclaimed >= 2UL << order)
325173ce02e9SKOSAKI Motohiro 			order = sc.order = 0;
325273ce02e9SKOSAKI Motohiro 
3253b8e83b94SMel Gorman 		/* Check if kswapd should be suspending */
3254b8e83b94SMel Gorman 		if (try_to_freeze() || kthread_should_stop())
3255b8e83b94SMel Gorman 			break;
3256b8e83b94SMel Gorman 
3257b8e83b94SMel Gorman 		/*
32582ab44f43SMel Gorman 		 * Compact if necessary and kswapd is reclaiming at least the
32592ab44f43SMel Gorman 		 * high watermark number of pages as requsted
32602ab44f43SMel Gorman 		 */
32612ab44f43SMel Gorman 		if (pgdat_needs_compaction && sc.nr_reclaimed > nr_attempted)
32622ab44f43SMel Gorman 			compact_pgdat(pgdat, order);
32632ab44f43SMel Gorman 
32642ab44f43SMel Gorman 		/*
3265b8e83b94SMel Gorman 		 * Raise priority if scanning rate is too low or there was no
3266b8e83b94SMel Gorman 		 * progress in reclaiming pages
3267b8e83b94SMel Gorman 		 */
3268b8e83b94SMel Gorman 		if (raise_priority || !sc.nr_reclaimed)
3269b8e83b94SMel Gorman 			sc.priority--;
32709aa41348SMel Gorman 	} while (sc.priority >= 1 &&
3271b8e83b94SMel Gorman 		 !pgdat_balanced(pgdat, order, *classzone_idx));
32721da177e4SLinus Torvalds 
3273b8e83b94SMel Gorman out:
32740abdee2bSMel Gorman 	/*
32755515061dSMel Gorman 	 * Return the order we were reclaiming at so prepare_kswapd_sleep()
32760abdee2bSMel Gorman 	 * makes a decision on the order we were last reclaiming at. However,
32770abdee2bSMel Gorman 	 * if another caller entered the allocator slow path while kswapd
32780abdee2bSMel Gorman 	 * was awake, order will remain at the higher level
32790abdee2bSMel Gorman 	 */
3280dc83edd9SMel Gorman 	*classzone_idx = end_zone;
32810abdee2bSMel Gorman 	return order;
32821da177e4SLinus Torvalds }
32831da177e4SLinus Torvalds 
3284dc83edd9SMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx)
3285f0bc0a60SKOSAKI Motohiro {
3286f0bc0a60SKOSAKI Motohiro 	long remaining = 0;
3287f0bc0a60SKOSAKI Motohiro 	DEFINE_WAIT(wait);
3288f0bc0a60SKOSAKI Motohiro 
3289f0bc0a60SKOSAKI Motohiro 	if (freezing(current) || kthread_should_stop())
3290f0bc0a60SKOSAKI Motohiro 		return;
3291f0bc0a60SKOSAKI Motohiro 
3292f0bc0a60SKOSAKI Motohiro 	prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
3293f0bc0a60SKOSAKI Motohiro 
3294f0bc0a60SKOSAKI Motohiro 	/* Try to sleep for a short interval */
32955515061dSMel Gorman 	if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
3296f0bc0a60SKOSAKI Motohiro 		remaining = schedule_timeout(HZ/10);
3297f0bc0a60SKOSAKI Motohiro 		finish_wait(&pgdat->kswapd_wait, &wait);
3298f0bc0a60SKOSAKI Motohiro 		prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
3299f0bc0a60SKOSAKI Motohiro 	}
3300f0bc0a60SKOSAKI Motohiro 
3301f0bc0a60SKOSAKI Motohiro 	/*
3302f0bc0a60SKOSAKI Motohiro 	 * After a short sleep, check if it was a premature sleep. If not, then
3303f0bc0a60SKOSAKI Motohiro 	 * go fully to sleep until explicitly woken up.
3304f0bc0a60SKOSAKI Motohiro 	 */
33055515061dSMel Gorman 	if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
3306f0bc0a60SKOSAKI Motohiro 		trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
3307f0bc0a60SKOSAKI Motohiro 
3308f0bc0a60SKOSAKI Motohiro 		/*
3309f0bc0a60SKOSAKI Motohiro 		 * vmstat counters are not perfectly accurate and the estimated
3310f0bc0a60SKOSAKI Motohiro 		 * value for counters such as NR_FREE_PAGES can deviate from the
3311f0bc0a60SKOSAKI Motohiro 		 * true value by nr_online_cpus * threshold. To avoid the zone
3312f0bc0a60SKOSAKI Motohiro 		 * watermarks being breached while under pressure, we reduce the
3313f0bc0a60SKOSAKI Motohiro 		 * per-cpu vmstat threshold while kswapd is awake and restore
3314f0bc0a60SKOSAKI Motohiro 		 * them before going back to sleep.
3315f0bc0a60SKOSAKI Motohiro 		 */
3316f0bc0a60SKOSAKI Motohiro 		set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
33171c7e7f6cSAaditya Kumar 
331862997027SMel Gorman 		/*
331962997027SMel Gorman 		 * Compaction records what page blocks it recently failed to
332062997027SMel Gorman 		 * isolate pages from and skips them in the future scanning.
332162997027SMel Gorman 		 * When kswapd is going to sleep, it is reasonable to assume
332262997027SMel Gorman 		 * that pages and compaction may succeed so reset the cache.
332362997027SMel Gorman 		 */
332462997027SMel Gorman 		reset_isolation_suitable(pgdat);
332562997027SMel Gorman 
33261c7e7f6cSAaditya Kumar 		if (!kthread_should_stop())
3327f0bc0a60SKOSAKI Motohiro 			schedule();
33281c7e7f6cSAaditya Kumar 
3329f0bc0a60SKOSAKI Motohiro 		set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
3330f0bc0a60SKOSAKI Motohiro 	} else {
3331f0bc0a60SKOSAKI Motohiro 		if (remaining)
3332f0bc0a60SKOSAKI Motohiro 			count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
3333f0bc0a60SKOSAKI Motohiro 		else
3334f0bc0a60SKOSAKI Motohiro 			count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
3335f0bc0a60SKOSAKI Motohiro 	}
3336f0bc0a60SKOSAKI Motohiro 	finish_wait(&pgdat->kswapd_wait, &wait);
3337f0bc0a60SKOSAKI Motohiro }
3338f0bc0a60SKOSAKI Motohiro 
33391da177e4SLinus Torvalds /*
33401da177e4SLinus Torvalds  * The background pageout daemon, started as a kernel thread
33411da177e4SLinus Torvalds  * from the init process.
33421da177e4SLinus Torvalds  *
33431da177e4SLinus Torvalds  * This basically trickles out pages so that we have _some_
33441da177e4SLinus Torvalds  * free memory available even if there is no other activity
33451da177e4SLinus Torvalds  * that frees anything up. This is needed for things like routing
33461da177e4SLinus Torvalds  * etc, where we otherwise might have all activity going on in
33471da177e4SLinus Torvalds  * asynchronous contexts that cannot page things out.
33481da177e4SLinus Torvalds  *
33491da177e4SLinus Torvalds  * If there are applications that are active memory-allocators
33501da177e4SLinus Torvalds  * (most normal use), this basically shouldn't matter.
33511da177e4SLinus Torvalds  */
33521da177e4SLinus Torvalds static int kswapd(void *p)
33531da177e4SLinus Torvalds {
3354215ddd66SMel Gorman 	unsigned long order, new_order;
3355d2ebd0f6SAlex,Shi 	unsigned balanced_order;
3356215ddd66SMel Gorman 	int classzone_idx, new_classzone_idx;
3357d2ebd0f6SAlex,Shi 	int balanced_classzone_idx;
33581da177e4SLinus Torvalds 	pg_data_t *pgdat = (pg_data_t*)p;
33591da177e4SLinus Torvalds 	struct task_struct *tsk = current;
3360f0bc0a60SKOSAKI Motohiro 
33611da177e4SLinus Torvalds 	struct reclaim_state reclaim_state = {
33621da177e4SLinus Torvalds 		.reclaimed_slab = 0,
33631da177e4SLinus Torvalds 	};
3364a70f7302SRusty Russell 	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
33651da177e4SLinus Torvalds 
3366cf40bd16SNick Piggin 	lockdep_set_current_reclaim_state(GFP_KERNEL);
3367cf40bd16SNick Piggin 
3368174596a0SRusty Russell 	if (!cpumask_empty(cpumask))
3369c5f59f08SMike Travis 		set_cpus_allowed_ptr(tsk, cpumask);
33701da177e4SLinus Torvalds 	current->reclaim_state = &reclaim_state;
33711da177e4SLinus Torvalds 
33721da177e4SLinus Torvalds 	/*
33731da177e4SLinus Torvalds 	 * Tell the memory management that we're a "memory allocator",
33741da177e4SLinus Torvalds 	 * and that if we need more memory we should get access to it
33751da177e4SLinus Torvalds 	 * regardless (see "__alloc_pages()"). "kswapd" should
33761da177e4SLinus Torvalds 	 * never get caught in the normal page freeing logic.
33771da177e4SLinus Torvalds 	 *
33781da177e4SLinus Torvalds 	 * (Kswapd normally doesn't need memory anyway, but sometimes
33791da177e4SLinus Torvalds 	 * you need a small amount of memory in order to be able to
33801da177e4SLinus Torvalds 	 * page out something else, and this flag essentially protects
33811da177e4SLinus Torvalds 	 * us from recursively trying to free more memory as we're
33821da177e4SLinus Torvalds 	 * trying to free the first piece of memory in the first place).
33831da177e4SLinus Torvalds 	 */
3384930d9152SChristoph Lameter 	tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
338583144186SRafael J. Wysocki 	set_freezable();
33861da177e4SLinus Torvalds 
3387215ddd66SMel Gorman 	order = new_order = 0;
3388d2ebd0f6SAlex,Shi 	balanced_order = 0;
3389215ddd66SMel Gorman 	classzone_idx = new_classzone_idx = pgdat->nr_zones - 1;
3390d2ebd0f6SAlex,Shi 	balanced_classzone_idx = classzone_idx;
33911da177e4SLinus Torvalds 	for ( ; ; ) {
33926f6313d4SJeff Liu 		bool ret;
33933e1d1d28SChristoph Lameter 
3394215ddd66SMel Gorman 		/*
3395215ddd66SMel Gorman 		 * If the last balance_pgdat was unsuccessful it's unlikely a
3396215ddd66SMel Gorman 		 * new request of a similar or harder type will succeed soon
3397215ddd66SMel Gorman 		 * so consider going to sleep on the basis we reclaimed at
3398215ddd66SMel Gorman 		 */
3399d2ebd0f6SAlex,Shi 		if (balanced_classzone_idx >= new_classzone_idx &&
3400d2ebd0f6SAlex,Shi 					balanced_order == new_order) {
34011da177e4SLinus Torvalds 			new_order = pgdat->kswapd_max_order;
340299504748SMel Gorman 			new_classzone_idx = pgdat->classzone_idx;
34031da177e4SLinus Torvalds 			pgdat->kswapd_max_order =  0;
3404215ddd66SMel Gorman 			pgdat->classzone_idx = pgdat->nr_zones - 1;
3405215ddd66SMel Gorman 		}
3406215ddd66SMel Gorman 
340799504748SMel Gorman 		if (order < new_order || classzone_idx > new_classzone_idx) {
34081da177e4SLinus Torvalds 			/*
34091da177e4SLinus Torvalds 			 * Don't sleep if someone wants a larger 'order'
341099504748SMel Gorman 			 * allocation or has tigher zone constraints
34111da177e4SLinus Torvalds 			 */
34121da177e4SLinus Torvalds 			order = new_order;
341399504748SMel Gorman 			classzone_idx = new_classzone_idx;
34141da177e4SLinus Torvalds 		} else {
3415d2ebd0f6SAlex,Shi 			kswapd_try_to_sleep(pgdat, balanced_order,
3416d2ebd0f6SAlex,Shi 						balanced_classzone_idx);
34171da177e4SLinus Torvalds 			order = pgdat->kswapd_max_order;
341899504748SMel Gorman 			classzone_idx = pgdat->classzone_idx;
3419f0dfcde0SAlex,Shi 			new_order = order;
3420f0dfcde0SAlex,Shi 			new_classzone_idx = classzone_idx;
34214d40502eSMel Gorman 			pgdat->kswapd_max_order = 0;
3422215ddd66SMel Gorman 			pgdat->classzone_idx = pgdat->nr_zones - 1;
34231da177e4SLinus Torvalds 		}
34241da177e4SLinus Torvalds 
34258fe23e05SDavid Rientjes 		ret = try_to_freeze();
34268fe23e05SDavid Rientjes 		if (kthread_should_stop())
34278fe23e05SDavid Rientjes 			break;
34288fe23e05SDavid Rientjes 
34298fe23e05SDavid Rientjes 		/*
34308fe23e05SDavid Rientjes 		 * We can speed up thawing tasks if we don't call balance_pgdat
34318fe23e05SDavid Rientjes 		 * after returning from the refrigerator
3432b1296cc4SRafael J. Wysocki 		 */
343333906bc5SMel Gorman 		if (!ret) {
343433906bc5SMel Gorman 			trace_mm_vmscan_kswapd_wake(pgdat->node_id, order);
3435d2ebd0f6SAlex,Shi 			balanced_classzone_idx = classzone_idx;
3436d2ebd0f6SAlex,Shi 			balanced_order = balance_pgdat(pgdat, order,
3437d2ebd0f6SAlex,Shi 						&balanced_classzone_idx);
34381da177e4SLinus Torvalds 		}
343933906bc5SMel Gorman 	}
3440b0a8cc58STakamori Yamaguchi 
344171abdc15SJohannes Weiner 	tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD);
3442b0a8cc58STakamori Yamaguchi 	current->reclaim_state = NULL;
344371abdc15SJohannes Weiner 	lockdep_clear_current_reclaim_state();
344471abdc15SJohannes Weiner 
34451da177e4SLinus Torvalds 	return 0;
34461da177e4SLinus Torvalds }
34471da177e4SLinus Torvalds 
34481da177e4SLinus Torvalds /*
34491da177e4SLinus Torvalds  * A zone is low on free memory, so wake its kswapd task to service it.
34501da177e4SLinus Torvalds  */
345199504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
34521da177e4SLinus Torvalds {
34531da177e4SLinus Torvalds 	pg_data_t *pgdat;
34541da177e4SLinus Torvalds 
3455f3fe6512SCon Kolivas 	if (!populated_zone(zone))
34561da177e4SLinus Torvalds 		return;
34571da177e4SLinus Torvalds 
3458344736f2SVladimir Davydov 	if (!cpuset_zone_allowed(zone, GFP_KERNEL | __GFP_HARDWALL))
34591da177e4SLinus Torvalds 		return;
346088f5acf8SMel Gorman 	pgdat = zone->zone_pgdat;
346199504748SMel Gorman 	if (pgdat->kswapd_max_order < order) {
346288f5acf8SMel Gorman 		pgdat->kswapd_max_order = order;
346399504748SMel Gorman 		pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx);
346499504748SMel Gorman 	}
34658d0986e2SCon Kolivas 	if (!waitqueue_active(&pgdat->kswapd_wait))
34661da177e4SLinus Torvalds 		return;
3467892f795dSJohannes Weiner 	if (zone_balanced(zone, order, 0, 0))
346888f5acf8SMel Gorman 		return;
346988f5acf8SMel Gorman 
347088f5acf8SMel Gorman 	trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
34718d0986e2SCon Kolivas 	wake_up_interruptible(&pgdat->kswapd_wait);
34721da177e4SLinus Torvalds }
34731da177e4SLinus Torvalds 
3474c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
34751da177e4SLinus Torvalds /*
34767b51755cSKOSAKI Motohiro  * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
3477d6277db4SRafael J. Wysocki  * freed pages.
3478d6277db4SRafael J. Wysocki  *
3479d6277db4SRafael J. Wysocki  * Rather than trying to age LRUs the aim is to preserve the overall
3480d6277db4SRafael J. Wysocki  * LRU order by reclaiming preferentially
3481d6277db4SRafael J. Wysocki  * inactive > active > active referenced > active mapped
34821da177e4SLinus Torvalds  */
34837b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
34841da177e4SLinus Torvalds {
3485d6277db4SRafael J. Wysocki 	struct reclaim_state reclaim_state;
3486d6277db4SRafael J. Wysocki 	struct scan_control sc = {
34877b51755cSKOSAKI Motohiro 		.nr_to_reclaim = nr_to_reclaim,
3488ee814fe2SJohannes Weiner 		.gfp_mask = GFP_HIGHUSER_MOVABLE,
34899e3b2f8cSKonstantin Khlebnikov 		.priority = DEF_PRIORITY,
3490ee814fe2SJohannes Weiner 		.may_writepage = 1,
3491ee814fe2SJohannes Weiner 		.may_unmap = 1,
3492ee814fe2SJohannes Weiner 		.may_swap = 1,
3493ee814fe2SJohannes Weiner 		.hibernation_mode = 1,
34941da177e4SLinus Torvalds 	};
34957b51755cSKOSAKI Motohiro 	struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
34967b51755cSKOSAKI Motohiro 	struct task_struct *p = current;
34977b51755cSKOSAKI Motohiro 	unsigned long nr_reclaimed;
34981da177e4SLinus Torvalds 
34997b51755cSKOSAKI Motohiro 	p->flags |= PF_MEMALLOC;
35007b51755cSKOSAKI Motohiro 	lockdep_set_current_reclaim_state(sc.gfp_mask);
3501d6277db4SRafael J. Wysocki 	reclaim_state.reclaimed_slab = 0;
35027b51755cSKOSAKI Motohiro 	p->reclaim_state = &reclaim_state;
3503d6277db4SRafael J. Wysocki 
35043115cd91SVladimir Davydov 	nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
3505d6277db4SRafael J. Wysocki 
35067b51755cSKOSAKI Motohiro 	p->reclaim_state = NULL;
35077b51755cSKOSAKI Motohiro 	lockdep_clear_current_reclaim_state();
35087b51755cSKOSAKI Motohiro 	p->flags &= ~PF_MEMALLOC;
3509d6277db4SRafael J. Wysocki 
35107b51755cSKOSAKI Motohiro 	return nr_reclaimed;
35111da177e4SLinus Torvalds }
3512c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */
35131da177e4SLinus Torvalds 
35141da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but
35151da177e4SLinus Torvalds    not required for correctness.  So if the last cpu in a node goes
35161da177e4SLinus Torvalds    away, we get changed to run anywhere: as the first one comes back,
35171da177e4SLinus Torvalds    restore their cpu bindings. */
3518fcb35a9bSGreg Kroah-Hartman static int cpu_callback(struct notifier_block *nfb, unsigned long action,
3519fcb35a9bSGreg Kroah-Hartman 			void *hcpu)
35201da177e4SLinus Torvalds {
352158c0a4a7SYasunori Goto 	int nid;
35221da177e4SLinus Torvalds 
35238bb78442SRafael J. Wysocki 	if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
352448fb2e24SLai Jiangshan 		for_each_node_state(nid, N_MEMORY) {
3525c5f59f08SMike Travis 			pg_data_t *pgdat = NODE_DATA(nid);
3526a70f7302SRusty Russell 			const struct cpumask *mask;
3527a70f7302SRusty Russell 
3528a70f7302SRusty Russell 			mask = cpumask_of_node(pgdat->node_id);
3529c5f59f08SMike Travis 
35303e597945SRusty Russell 			if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
35311da177e4SLinus Torvalds 				/* One of our CPUs online: restore mask */
3532c5f59f08SMike Travis 				set_cpus_allowed_ptr(pgdat->kswapd, mask);
35331da177e4SLinus Torvalds 		}
35341da177e4SLinus Torvalds 	}
35351da177e4SLinus Torvalds 	return NOTIFY_OK;
35361da177e4SLinus Torvalds }
35371da177e4SLinus Torvalds 
35383218ae14SYasunori Goto /*
35393218ae14SYasunori Goto  * This kswapd start function will be called by init and node-hot-add.
35403218ae14SYasunori Goto  * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
35413218ae14SYasunori Goto  */
35423218ae14SYasunori Goto int kswapd_run(int nid)
35433218ae14SYasunori Goto {
35443218ae14SYasunori Goto 	pg_data_t *pgdat = NODE_DATA(nid);
35453218ae14SYasunori Goto 	int ret = 0;
35463218ae14SYasunori Goto 
35473218ae14SYasunori Goto 	if (pgdat->kswapd)
35483218ae14SYasunori Goto 		return 0;
35493218ae14SYasunori Goto 
35503218ae14SYasunori Goto 	pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
35513218ae14SYasunori Goto 	if (IS_ERR(pgdat->kswapd)) {
35523218ae14SYasunori Goto 		/* failure at boot is fatal */
35533218ae14SYasunori Goto 		BUG_ON(system_state == SYSTEM_BOOTING);
3554d5dc0ad9SGavin Shan 		pr_err("Failed to start kswapd on node %d\n", nid);
3555d5dc0ad9SGavin Shan 		ret = PTR_ERR(pgdat->kswapd);
3556d72515b8SXishi Qiu 		pgdat->kswapd = NULL;
35573218ae14SYasunori Goto 	}
35583218ae14SYasunori Goto 	return ret;
35593218ae14SYasunori Goto }
35603218ae14SYasunori Goto 
35618fe23e05SDavid Rientjes /*
3562d8adde17SJiang Liu  * Called by memory hotplug when all memory in a node is offlined.  Caller must
3563bfc8c901SVladimir Davydov  * hold mem_hotplug_begin/end().
35648fe23e05SDavid Rientjes  */
35658fe23e05SDavid Rientjes void kswapd_stop(int nid)
35668fe23e05SDavid Rientjes {
35678fe23e05SDavid Rientjes 	struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
35688fe23e05SDavid Rientjes 
3569d8adde17SJiang Liu 	if (kswapd) {
35708fe23e05SDavid Rientjes 		kthread_stop(kswapd);
3571d8adde17SJiang Liu 		NODE_DATA(nid)->kswapd = NULL;
3572d8adde17SJiang Liu 	}
35738fe23e05SDavid Rientjes }
35748fe23e05SDavid Rientjes 
35751da177e4SLinus Torvalds static int __init kswapd_init(void)
35761da177e4SLinus Torvalds {
35773218ae14SYasunori Goto 	int nid;
357869e05944SAndrew Morton 
35791da177e4SLinus Torvalds 	swap_setup();
358048fb2e24SLai Jiangshan 	for_each_node_state(nid, N_MEMORY)
35813218ae14SYasunori Goto  		kswapd_run(nid);
35821da177e4SLinus Torvalds 	hotcpu_notifier(cpu_callback, 0);
35831da177e4SLinus Torvalds 	return 0;
35841da177e4SLinus Torvalds }
35851da177e4SLinus Torvalds 
35861da177e4SLinus Torvalds module_init(kswapd_init)
35879eeff239SChristoph Lameter 
35889eeff239SChristoph Lameter #ifdef CONFIG_NUMA
35899eeff239SChristoph Lameter /*
35909eeff239SChristoph Lameter  * Zone reclaim mode
35919eeff239SChristoph Lameter  *
35929eeff239SChristoph Lameter  * If non-zero call zone_reclaim when the number of free pages falls below
35939eeff239SChristoph Lameter  * the watermarks.
35949eeff239SChristoph Lameter  */
35959eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly;
35969eeff239SChristoph Lameter 
35971b2ffb78SChristoph Lameter #define RECLAIM_OFF 0
35987d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0)	/* Run shrink_inactive_list on the zone */
35991b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1)	/* Writeout pages during reclaim */
3600*95bbc0c7SZhihui Zhang #define RECLAIM_UNMAP (1<<2)	/* Unmap pages during reclaim */
36011b2ffb78SChristoph Lameter 
36029eeff239SChristoph Lameter /*
3603a92f7126SChristoph Lameter  * Priority for ZONE_RECLAIM. This determines the fraction of pages
3604a92f7126SChristoph Lameter  * of a node considered for each zone_reclaim. 4 scans 1/16th of
3605a92f7126SChristoph Lameter  * a zone.
3606a92f7126SChristoph Lameter  */
3607a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4
3608a92f7126SChristoph Lameter 
36099eeff239SChristoph Lameter /*
36109614634fSChristoph Lameter  * Percentage of pages in a zone that must be unmapped for zone_reclaim to
36119614634fSChristoph Lameter  * occur.
36129614634fSChristoph Lameter  */
36139614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1;
36149614634fSChristoph Lameter 
36159614634fSChristoph Lameter /*
36160ff38490SChristoph Lameter  * If the number of slab pages in a zone grows beyond this percentage then
36170ff38490SChristoph Lameter  * slab reclaim needs to occur.
36180ff38490SChristoph Lameter  */
36190ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5;
36200ff38490SChristoph Lameter 
362190afa5deSMel Gorman static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
362290afa5deSMel Gorman {
362390afa5deSMel Gorman 	unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
362490afa5deSMel Gorman 	unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
362590afa5deSMel Gorman 		zone_page_state(zone, NR_ACTIVE_FILE);
362690afa5deSMel Gorman 
362790afa5deSMel Gorman 	/*
362890afa5deSMel Gorman 	 * It's possible for there to be more file mapped pages than
362990afa5deSMel Gorman 	 * accounted for by the pages on the file LRU lists because
363090afa5deSMel Gorman 	 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
363190afa5deSMel Gorman 	 */
363290afa5deSMel Gorman 	return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
363390afa5deSMel Gorman }
363490afa5deSMel Gorman 
363590afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */
363690afa5deSMel Gorman static long zone_pagecache_reclaimable(struct zone *zone)
363790afa5deSMel Gorman {
363890afa5deSMel Gorman 	long nr_pagecache_reclaimable;
363990afa5deSMel Gorman 	long delta = 0;
364090afa5deSMel Gorman 
364190afa5deSMel Gorman 	/*
3642*95bbc0c7SZhihui Zhang 	 * If RECLAIM_UNMAP is set, then all file pages are considered
364390afa5deSMel Gorman 	 * potentially reclaimable. Otherwise, we have to worry about
364490afa5deSMel Gorman 	 * pages like swapcache and zone_unmapped_file_pages() provides
364590afa5deSMel Gorman 	 * a better estimate
364690afa5deSMel Gorman 	 */
3647*95bbc0c7SZhihui Zhang 	if (zone_reclaim_mode & RECLAIM_UNMAP)
364890afa5deSMel Gorman 		nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
364990afa5deSMel Gorman 	else
365090afa5deSMel Gorman 		nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
365190afa5deSMel Gorman 
365290afa5deSMel Gorman 	/* If we can't clean pages, remove dirty pages from consideration */
365390afa5deSMel Gorman 	if (!(zone_reclaim_mode & RECLAIM_WRITE))
365490afa5deSMel Gorman 		delta += zone_page_state(zone, NR_FILE_DIRTY);
365590afa5deSMel Gorman 
365690afa5deSMel Gorman 	/* Watch for any possible underflows due to delta */
365790afa5deSMel Gorman 	if (unlikely(delta > nr_pagecache_reclaimable))
365890afa5deSMel Gorman 		delta = nr_pagecache_reclaimable;
365990afa5deSMel Gorman 
366090afa5deSMel Gorman 	return nr_pagecache_reclaimable - delta;
366190afa5deSMel Gorman }
366290afa5deSMel Gorman 
36630ff38490SChristoph Lameter /*
36649eeff239SChristoph Lameter  * Try to free up some pages from this zone through reclaim.
36659eeff239SChristoph Lameter  */
3666179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
36679eeff239SChristoph Lameter {
36687fb2d46dSChristoph Lameter 	/* Minimum pages needed in order to stay on node */
366969e05944SAndrew Morton 	const unsigned long nr_pages = 1 << order;
36709eeff239SChristoph Lameter 	struct task_struct *p = current;
36719eeff239SChristoph Lameter 	struct reclaim_state reclaim_state;
3672179e9639SAndrew Morton 	struct scan_control sc = {
367362b726c1SAndrew Morton 		.nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
367421caf2fcSMing Lei 		.gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)),
3675bd2f6199SJohannes Weiner 		.order = order,
36769e3b2f8cSKonstantin Khlebnikov 		.priority = ZONE_RECLAIM_PRIORITY,
3677ee814fe2SJohannes Weiner 		.may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
3678*95bbc0c7SZhihui Zhang 		.may_unmap = !!(zone_reclaim_mode & RECLAIM_UNMAP),
3679ee814fe2SJohannes Weiner 		.may_swap = 1,
3680179e9639SAndrew Morton 	};
36819eeff239SChristoph Lameter 
36829eeff239SChristoph Lameter 	cond_resched();
3683d4f7796eSChristoph Lameter 	/*
3684*95bbc0c7SZhihui Zhang 	 * We need to be able to allocate from the reserves for RECLAIM_UNMAP
3685d4f7796eSChristoph Lameter 	 * and we also need to be able to write out pages for RECLAIM_WRITE
3686*95bbc0c7SZhihui Zhang 	 * and RECLAIM_UNMAP.
3687d4f7796eSChristoph Lameter 	 */
3688d4f7796eSChristoph Lameter 	p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
368976ca542dSKOSAKI Motohiro 	lockdep_set_current_reclaim_state(gfp_mask);
36909eeff239SChristoph Lameter 	reclaim_state.reclaimed_slab = 0;
36919eeff239SChristoph Lameter 	p->reclaim_state = &reclaim_state;
3692c84db23cSChristoph Lameter 
369390afa5deSMel Gorman 	if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
3694a92f7126SChristoph Lameter 		/*
36950ff38490SChristoph Lameter 		 * Free memory by calling shrink zone with increasing
36960ff38490SChristoph Lameter 		 * priorities until we have enough memory freed.
3697a92f7126SChristoph Lameter 		 */
3698a92f7126SChristoph Lameter 		do {
36996b4f7799SJohannes Weiner 			shrink_zone(zone, &sc, true);
37009e3b2f8cSKonstantin Khlebnikov 		} while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
37010ff38490SChristoph Lameter 	}
3702a92f7126SChristoph Lameter 
37039eeff239SChristoph Lameter 	p->reclaim_state = NULL;
3704d4f7796eSChristoph Lameter 	current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
370576ca542dSKOSAKI Motohiro 	lockdep_clear_current_reclaim_state();
3706a79311c1SRik van Riel 	return sc.nr_reclaimed >= nr_pages;
37079eeff239SChristoph Lameter }
3708179e9639SAndrew Morton 
3709179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
3710179e9639SAndrew Morton {
3711179e9639SAndrew Morton 	int node_id;
3712d773ed6bSDavid Rientjes 	int ret;
3713179e9639SAndrew Morton 
3714179e9639SAndrew Morton 	/*
37150ff38490SChristoph Lameter 	 * Zone reclaim reclaims unmapped file backed pages and
37160ff38490SChristoph Lameter 	 * slab pages if we are over the defined limits.
371734aa1330SChristoph Lameter 	 *
37189614634fSChristoph Lameter 	 * A small portion of unmapped file backed pages is needed for
37199614634fSChristoph Lameter 	 * file I/O otherwise pages read by file I/O will be immediately
37209614634fSChristoph Lameter 	 * thrown out if the zone is overallocated. So we do not reclaim
37219614634fSChristoph Lameter 	 * if less than a specified percentage of the zone is used by
37229614634fSChristoph Lameter 	 * unmapped file backed pages.
3723179e9639SAndrew Morton 	 */
372490afa5deSMel Gorman 	if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
372590afa5deSMel Gorman 	    zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
3726fa5e084eSMel Gorman 		return ZONE_RECLAIM_FULL;
3727179e9639SAndrew Morton 
37286e543d57SLisa Du 	if (!zone_reclaimable(zone))
3729fa5e084eSMel Gorman 		return ZONE_RECLAIM_FULL;
3730d773ed6bSDavid Rientjes 
3731179e9639SAndrew Morton 	/*
3732d773ed6bSDavid Rientjes 	 * Do not scan if the allocation should not be delayed.
3733179e9639SAndrew Morton 	 */
3734d773ed6bSDavid Rientjes 	if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
3735fa5e084eSMel Gorman 		return ZONE_RECLAIM_NOSCAN;
3736179e9639SAndrew Morton 
3737179e9639SAndrew Morton 	/*
3738179e9639SAndrew Morton 	 * Only run zone reclaim on the local zone or on zones that do not
3739179e9639SAndrew Morton 	 * have associated processors. This will favor the local processor
3740179e9639SAndrew Morton 	 * over remote processors and spread off node memory allocations
3741179e9639SAndrew Morton 	 * as wide as possible.
3742179e9639SAndrew Morton 	 */
374389fa3024SChristoph Lameter 	node_id = zone_to_nid(zone);
374437c0708dSChristoph Lameter 	if (node_state(node_id, N_CPU) && node_id != numa_node_id())
3745fa5e084eSMel Gorman 		return ZONE_RECLAIM_NOSCAN;
3746d773ed6bSDavid Rientjes 
374757054651SJohannes Weiner 	if (test_and_set_bit(ZONE_RECLAIM_LOCKED, &zone->flags))
3748fa5e084eSMel Gorman 		return ZONE_RECLAIM_NOSCAN;
3749fa5e084eSMel Gorman 
3750d773ed6bSDavid Rientjes 	ret = __zone_reclaim(zone, gfp_mask, order);
375157054651SJohannes Weiner 	clear_bit(ZONE_RECLAIM_LOCKED, &zone->flags);
3752d773ed6bSDavid Rientjes 
375324cf7251SMel Gorman 	if (!ret)
375424cf7251SMel Gorman 		count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
375524cf7251SMel Gorman 
3756d773ed6bSDavid Rientjes 	return ret;
3757179e9639SAndrew Morton }
37589eeff239SChristoph Lameter #endif
3759894bc310SLee Schermerhorn 
3760894bc310SLee Schermerhorn /*
3761894bc310SLee Schermerhorn  * page_evictable - test whether a page is evictable
3762894bc310SLee Schermerhorn  * @page: the page to test
3763894bc310SLee Schermerhorn  *
3764894bc310SLee Schermerhorn  * Test whether page is evictable--i.e., should be placed on active/inactive
376539b5f29aSHugh Dickins  * lists vs unevictable list.
3766894bc310SLee Schermerhorn  *
3767894bc310SLee Schermerhorn  * Reasons page might not be evictable:
3768ba9ddf49SLee Schermerhorn  * (1) page's mapping marked unevictable
3769b291f000SNick Piggin  * (2) page is part of an mlocked VMA
3770ba9ddf49SLee Schermerhorn  *
3771894bc310SLee Schermerhorn  */
377239b5f29aSHugh Dickins int page_evictable(struct page *page)
3773894bc310SLee Schermerhorn {
377439b5f29aSHugh Dickins 	return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
3775894bc310SLee Schermerhorn }
377689e004eaSLee Schermerhorn 
377785046579SHugh Dickins #ifdef CONFIG_SHMEM
377889e004eaSLee Schermerhorn /**
377924513264SHugh Dickins  * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list
378024513264SHugh Dickins  * @pages:	array of pages to check
378124513264SHugh Dickins  * @nr_pages:	number of pages to check
378289e004eaSLee Schermerhorn  *
378324513264SHugh Dickins  * Checks pages for evictability and moves them to the appropriate lru list.
378485046579SHugh Dickins  *
378585046579SHugh Dickins  * This function is only used for SysV IPC SHM_UNLOCK.
378689e004eaSLee Schermerhorn  */
378724513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages)
378889e004eaSLee Schermerhorn {
3789925b7673SJohannes Weiner 	struct lruvec *lruvec;
379024513264SHugh Dickins 	struct zone *zone = NULL;
379124513264SHugh Dickins 	int pgscanned = 0;
379224513264SHugh Dickins 	int pgrescued = 0;
379389e004eaSLee Schermerhorn 	int i;
379489e004eaSLee Schermerhorn 
379524513264SHugh Dickins 	for (i = 0; i < nr_pages; i++) {
379624513264SHugh Dickins 		struct page *page = pages[i];
379724513264SHugh Dickins 		struct zone *pagezone;
379889e004eaSLee Schermerhorn 
379924513264SHugh Dickins 		pgscanned++;
380024513264SHugh Dickins 		pagezone = page_zone(page);
380189e004eaSLee Schermerhorn 		if (pagezone != zone) {
380289e004eaSLee Schermerhorn 			if (zone)
380389e004eaSLee Schermerhorn 				spin_unlock_irq(&zone->lru_lock);
380489e004eaSLee Schermerhorn 			zone = pagezone;
380589e004eaSLee Schermerhorn 			spin_lock_irq(&zone->lru_lock);
380689e004eaSLee Schermerhorn 		}
3807fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
380889e004eaSLee Schermerhorn 
380924513264SHugh Dickins 		if (!PageLRU(page) || !PageUnevictable(page))
381024513264SHugh Dickins 			continue;
381189e004eaSLee Schermerhorn 
381239b5f29aSHugh Dickins 		if (page_evictable(page)) {
381324513264SHugh Dickins 			enum lru_list lru = page_lru_base_type(page);
381424513264SHugh Dickins 
3815309381feSSasha Levin 			VM_BUG_ON_PAGE(PageActive(page), page);
381624513264SHugh Dickins 			ClearPageUnevictable(page);
3817fa9add64SHugh Dickins 			del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
3818fa9add64SHugh Dickins 			add_page_to_lru_list(page, lruvec, lru);
381924513264SHugh Dickins 			pgrescued++;
382089e004eaSLee Schermerhorn 		}
382189e004eaSLee Schermerhorn 	}
382224513264SHugh Dickins 
382324513264SHugh Dickins 	if (zone) {
382424513264SHugh Dickins 		__count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
382524513264SHugh Dickins 		__count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
382624513264SHugh Dickins 		spin_unlock_irq(&zone->lru_lock);
382724513264SHugh Dickins 	}
382885046579SHugh Dickins }
382985046579SHugh Dickins #endif /* CONFIG_SHMEM */
3830